diff mbox series

[bpf] bpf: Fix tr dereferencing

Message ID 20230914145126.40202-1-hffilwlqm@gmail.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series [bpf] bpf: Fix tr dereferencing | expand

Checks

Context Check Description
bpf/vmtest-bpf-PR success PR summary
bpf/vmtest-bpf-VM_Test-2 success Logs for build for s390x with gcc
bpf/vmtest-bpf-VM_Test-6 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-8 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-9 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-12 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-13 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-10 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-14 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-16 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-17 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-18 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-19 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-20 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-21 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-22 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-27 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-23 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-24 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-26 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-28 success Logs for veristat
bpf/vmtest-bpf-VM_Test-25 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-VM_Test-15 success Logs for test_progs_no_alu32 on s390x with gcc
netdev/tree_selection success Clearly marked for bpf
netdev/apply fail Patch does not apply to bpf
bpf/vmtest-bpf-VM_Test-11 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-VM_Test-7 success Logs for test_maps on s390x with gcc
bpf/vmtest-bpf-VM_Test-5 success Logs for set-matrix
bpf/vmtest-bpf-VM_Test-4 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-0 success Logs for ShellCheck
bpf/vmtest-bpf-VM_Test-3 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-VM_Test-1 success Logs for build for aarch64 with gcc

Commit Message

Leon Hwang Sept. 14, 2023, 2:51 p.m. UTC
Fix 'tr' dereferencing bug when CONFIG_BPF_JIT is turned off.

Like 'bpf_trampoline_get_progs()', return 'ERR_PTR()' and then check by
'IS_ERR()'. As a result, when CONFIG_BPF_JIT is turned off, it's able to
handle the case that 'bpf_trampoline_get()' returns
'ERR_PTR(-EOPNOTSUPP)'.

Fixes: 4a1e7c0c63e0 ("bpf: Support attaching freplace programs to multiple attach points")
Fixes: f7b12b6fea00 ("bpf: verifier: refactor check_attach_btf_id()")
Fixes: 69fd337a975c ("bpf: per-cgroup lsm flavor")
Reported-by: kernel test robot <lkp@intel.com>
Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
Closes: https://lore.kernel.org/r/202309131936.5Nc8eUD0-lkp@intel.com/
Signed-off-by: Leon Hwang <hffilwlqm@gmail.com>
---
 kernel/bpf/syscall.c    | 4 ++--
 kernel/bpf/trampoline.c | 6 +++---
 kernel/bpf/verifier.c   | 4 ++--
 3 files changed, 7 insertions(+), 7 deletions(-)


base-commit: cbb1dbcd99b0ae74c45c4c83c6d213c12c31785c

Comments

Jiri Olsa Sept. 14, 2023, 9:49 p.m. UTC | #1
On Thu, Sep 14, 2023 at 10:51:26PM +0800, Leon Hwang wrote:
> Fix 'tr' dereferencing bug when CONFIG_BPF_JIT is turned off.
> 
> Like 'bpf_trampoline_get_progs()', return 'ERR_PTR()' and then check by
> 'IS_ERR()'. As a result, when CONFIG_BPF_JIT is turned off, it's able to
> handle the case that 'bpf_trampoline_get()' returns
> 'ERR_PTR(-EOPNOTSUPP)'.
> 
> Fixes: 4a1e7c0c63e0 ("bpf: Support attaching freplace programs to multiple attach points")
> Fixes: f7b12b6fea00 ("bpf: verifier: refactor check_attach_btf_id()")
> Fixes: 69fd337a975c ("bpf: per-cgroup lsm flavor")
> Reported-by: kernel test robot <lkp@intel.com>
> Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
> Closes: https://lore.kernel.org/r/202309131936.5Nc8eUD0-lkp@intel.com/
> Signed-off-by: Leon Hwang <hffilwlqm@gmail.com>

it does not apply cleanly on bpf/master for me, but nice catch

Acked-by: Jiri Olsa <jolsa@kernel.org>

jirka

> ---
>  kernel/bpf/syscall.c    | 4 ++--
>  kernel/bpf/trampoline.c | 6 +++---
>  kernel/bpf/verifier.c   | 4 ++--
>  3 files changed, 7 insertions(+), 7 deletions(-)
> 
> diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
> index 6a692f3bea150..5748d01c99854 100644
> --- a/kernel/bpf/syscall.c
> +++ b/kernel/bpf/syscall.c
> @@ -3211,8 +3211,8 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog,
>  		}
>  
>  		tr = bpf_trampoline_get(key, &tgt_info);
> -		if (!tr) {
> -			err = -ENOMEM;
> +		if (IS_ERR(tr)) {
> +			err = PTR_ERR(tr);
>  			goto out_unlock;
>  		}
>  	} else {
> diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c
> index e97aeda3a86b5..1952614778433 100644
> --- a/kernel/bpf/trampoline.c
> +++ b/kernel/bpf/trampoline.c
> @@ -697,8 +697,8 @@ int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,
>  
>  	bpf_lsm_find_cgroup_shim(prog, &bpf_func);
>  	tr = bpf_trampoline_get(key, &tgt_info);
> -	if (!tr)
> -		return  -ENOMEM;
> +	if (IS_ERR(tr))
> +		return PTR_ERR(tr);
>  
>  	mutex_lock(&tr->mutex);
>  
> @@ -775,7 +775,7 @@ struct bpf_trampoline *bpf_trampoline_get(u64 key,
>  
>  	tr = bpf_trampoline_lookup(key);
>  	if (!tr)
> -		return NULL;
> +		return ERR_PTR(-ENOMEM);
>  
>  	mutex_lock(&tr->mutex);
>  	if (tr->func.addr)
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 18e673c0ac159..054063ead0e54 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -19771,8 +19771,8 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
>  
>  	key = bpf_trampoline_compute_key(tgt_prog, prog->aux->attach_btf, btf_id);
>  	tr = bpf_trampoline_get(key, &tgt_info);
> -	if (!tr)
> -		return -ENOMEM;
> +	if (IS_ERR(tr))
> +		return PTR_ERR(tr);
>  
>  	if (tgt_prog && tgt_prog->aux->tail_call_reachable)
>  		tr->flags = BPF_TRAMP_F_TAIL_CALL_CTX;
> 
> base-commit: cbb1dbcd99b0ae74c45c4c83c6d213c12c31785c
> -- 
> 2.41.0
> 
>
Hengqi Chen Sept. 15, 2023, 2:13 a.m. UTC | #2
On Thu, Sep 14, 2023 at 10:51 PM Leon Hwang <hffilwlqm@gmail.com> wrote:
>
> Fix 'tr' dereferencing bug when CONFIG_BPF_JIT is turned off.
>
> Like 'bpf_trampoline_get_progs()', return 'ERR_PTR()' and then check by
> 'IS_ERR()'. As a result, when CONFIG_BPF_JIT is turned off, it's able to
> handle the case that 'bpf_trampoline_get()' returns
> 'ERR_PTR(-EOPNOTSUPP)'.
>
> Fixes: 4a1e7c0c63e0 ("bpf: Support attaching freplace programs to multiple attach points")
> Fixes: f7b12b6fea00 ("bpf: verifier: refactor check_attach_btf_id()")
> Fixes: 69fd337a975c ("bpf: per-cgroup lsm flavor")
> Reported-by: kernel test robot <lkp@intel.com>
> Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
> Closes: https://lore.kernel.org/r/202309131936.5Nc8eUD0-lkp@intel.com/
> Signed-off-by: Leon Hwang <hffilwlqm@gmail.com>
> ---
>  kernel/bpf/syscall.c    | 4 ++--
>  kernel/bpf/trampoline.c | 6 +++---
>  kernel/bpf/verifier.c   | 4 ++--
>  3 files changed, 7 insertions(+), 7 deletions(-)
>
> diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
> index 6a692f3bea150..5748d01c99854 100644
> --- a/kernel/bpf/syscall.c
> +++ b/kernel/bpf/syscall.c
> @@ -3211,8 +3211,8 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog,
>                 }
>
>                 tr = bpf_trampoline_get(key, &tgt_info);
> -               if (!tr) {
> -                       err = -ENOMEM;
> +               if (IS_ERR(tr)) {
> +                       err = PTR_ERR(tr);
>                         goto out_unlock;

IS_ERR does not check the null case, so this should be IS_ERR_OR_NULL instead.

>                 }
>         } else {
> diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c
> index e97aeda3a86b5..1952614778433 100644
> --- a/kernel/bpf/trampoline.c
> +++ b/kernel/bpf/trampoline.c
> @@ -697,8 +697,8 @@ int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,
>
>         bpf_lsm_find_cgroup_shim(prog, &bpf_func);
>         tr = bpf_trampoline_get(key, &tgt_info);
> -       if (!tr)
> -               return  -ENOMEM;
> +       if (IS_ERR(tr))
> +               return PTR_ERR(tr);
>
>         mutex_lock(&tr->mutex);
>
> @@ -775,7 +775,7 @@ struct bpf_trampoline *bpf_trampoline_get(u64 key,
>
>         tr = bpf_trampoline_lookup(key);
>         if (!tr)
> -               return NULL;
> +               return ERR_PTR(-ENOMEM);
>
>         mutex_lock(&tr->mutex);
>         if (tr->func.addr)
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 18e673c0ac159..054063ead0e54 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -19771,8 +19771,8 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
>
>         key = bpf_trampoline_compute_key(tgt_prog, prog->aux->attach_btf, btf_id);
>         tr = bpf_trampoline_get(key, &tgt_info);
> -       if (!tr)
> -               return -ENOMEM;
> +       if (IS_ERR(tr))
> +               return PTR_ERR(tr);
>
>         if (tgt_prog && tgt_prog->aux->tail_call_reachable)
>                 tr->flags = BPF_TRAMP_F_TAIL_CALL_CTX;
>
> base-commit: cbb1dbcd99b0ae74c45c4c83c6d213c12c31785c
> --
> 2.41.0
>
Leon Hwang Sept. 15, 2023, 2:18 a.m. UTC | #3
On 15/9/23 10:13, Hengqi Chen wrote:
> On Thu, Sep 14, 2023 at 10:51 PM Leon Hwang <hffilwlqm@gmail.com> wrote:
>>
>> Fix 'tr' dereferencing bug when CONFIG_BPF_JIT is turned off.
>>
>> Like 'bpf_trampoline_get_progs()', return 'ERR_PTR()' and then check by
>> 'IS_ERR()'. As a result, when CONFIG_BPF_JIT is turned off, it's able to
>> handle the case that 'bpf_trampoline_get()' returns
>> 'ERR_PTR(-EOPNOTSUPP)'.
>>
>> Fixes: 4a1e7c0c63e0 ("bpf: Support attaching freplace programs to multiple attach points")
>> Fixes: f7b12b6fea00 ("bpf: verifier: refactor check_attach_btf_id()")
>> Fixes: 69fd337a975c ("bpf: per-cgroup lsm flavor")
>> Reported-by: kernel test robot <lkp@intel.com>
>> Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
>> Closes: https://lore.kernel.org/r/202309131936.5Nc8eUD0-lkp@intel.com/
>> Signed-off-by: Leon Hwang <hffilwlqm@gmail.com>
>> ---
>>  kernel/bpf/syscall.c    | 4 ++--
>>  kernel/bpf/trampoline.c | 6 +++---
>>  kernel/bpf/verifier.c   | 4 ++--
>>  3 files changed, 7 insertions(+), 7 deletions(-)
>>
>> diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
>> index 6a692f3bea150..5748d01c99854 100644
>> --- a/kernel/bpf/syscall.c
>> +++ b/kernel/bpf/syscall.c
>> @@ -3211,8 +3211,8 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog,
>>                 }
>>
>>                 tr = bpf_trampoline_get(key, &tgt_info);
>> -               if (!tr) {
>> -                       err = -ENOMEM;
>> +               if (IS_ERR(tr)) {
>> +                       err = PTR_ERR(tr);
>>                         goto out_unlock;
> 
> IS_ERR does not check the null case, so this should be IS_ERR_OR_NULL instead.

Actually, bpf_trampoline_get() would not return NULL. It returns ERR_PTR(-ENOMEM) 
or a valid ptr.

Thanks,
Leon

> 
>>                 }
>>         } else {
>> diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c
>> index e97aeda3a86b5..1952614778433 100644
>> --- a/kernel/bpf/trampoline.c
>> +++ b/kernel/bpf/trampoline.c
>> @@ -697,8 +697,8 @@ int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,
>>
>>         bpf_lsm_find_cgroup_shim(prog, &bpf_func);
>>         tr = bpf_trampoline_get(key, &tgt_info);
>> -       if (!tr)
>> -               return  -ENOMEM;
>> +       if (IS_ERR(tr))
>> +               return PTR_ERR(tr);
>>
>>         mutex_lock(&tr->mutex);
>>
>> @@ -775,7 +775,7 @@ struct bpf_trampoline *bpf_trampoline_get(u64 key,
>>
>>         tr = bpf_trampoline_lookup(key);
>>         if (!tr)
>> -               return NULL;
>> +               return ERR_PTR(-ENOMEM);
>>
>>         mutex_lock(&tr->mutex);
>>         if (tr->func.addr)
>> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
>> index 18e673c0ac159..054063ead0e54 100644
>> --- a/kernel/bpf/verifier.c
>> +++ b/kernel/bpf/verifier.c
>> @@ -19771,8 +19771,8 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
>>
>>         key = bpf_trampoline_compute_key(tgt_prog, prog->aux->attach_btf, btf_id);
>>         tr = bpf_trampoline_get(key, &tgt_info);
>> -       if (!tr)
>> -               return -ENOMEM;
>> +       if (IS_ERR(tr))
>> +               return PTR_ERR(tr);
>>
>>         if (tgt_prog && tgt_prog->aux->tail_call_reachable)
>>                 tr->flags = BPF_TRAMP_F_TAIL_CALL_CTX;
>>
>> base-commit: cbb1dbcd99b0ae74c45c4c83c6d213c12c31785c
>> --
>> 2.41.0
>>
Hengqi Chen Sept. 15, 2023, 2:54 a.m. UTC | #4
On Fri, Sep 15, 2023 at 10:18 AM Leon Hwang <hffilwlqm@gmail.com> wrote:
>
>
>
> On 15/9/23 10:13, Hengqi Chen wrote:
> > On Thu, Sep 14, 2023 at 10:51 PM Leon Hwang <hffilwlqm@gmail.com> wrote:
> >>
> >> Fix 'tr' dereferencing bug when CONFIG_BPF_JIT is turned off.
> >>
> >> Like 'bpf_trampoline_get_progs()', return 'ERR_PTR()' and then check by
> >> 'IS_ERR()'. As a result, when CONFIG_BPF_JIT is turned off, it's able to
> >> handle the case that 'bpf_trampoline_get()' returns
> >> 'ERR_PTR(-EOPNOTSUPP)'.
> >>
> >> Fixes: 4a1e7c0c63e0 ("bpf: Support attaching freplace programs to multiple attach points")
> >> Fixes: f7b12b6fea00 ("bpf: verifier: refactor check_attach_btf_id()")
> >> Fixes: 69fd337a975c ("bpf: per-cgroup lsm flavor")
> >> Reported-by: kernel test robot <lkp@intel.com>
> >> Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
> >> Closes: https://lore.kernel.org/r/202309131936.5Nc8eUD0-lkp@intel.com/
> >> Signed-off-by: Leon Hwang <hffilwlqm@gmail.com>
> >> ---
> >>  kernel/bpf/syscall.c    | 4 ++--
> >>  kernel/bpf/trampoline.c | 6 +++---
> >>  kernel/bpf/verifier.c   | 4 ++--
> >>  3 files changed, 7 insertions(+), 7 deletions(-)
> >>
> >> diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
> >> index 6a692f3bea150..5748d01c99854 100644
> >> --- a/kernel/bpf/syscall.c
> >> +++ b/kernel/bpf/syscall.c
> >> @@ -3211,8 +3211,8 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog,
> >>                 }
> >>
> >>                 tr = bpf_trampoline_get(key, &tgt_info);
> >> -               if (!tr) {
> >> -                       err = -ENOMEM;
> >> +               if (IS_ERR(tr)) {
> >> +                       err = PTR_ERR(tr);
> >>                         goto out_unlock;
> >
> > IS_ERR does not check the null case, so this should be IS_ERR_OR_NULL instead.
>
> Actually, bpf_trampoline_get() would not return NULL. It returns ERR_PTR(-ENOMEM)
> or a valid ptr.
>

OK, I missed the change in bpf_trampoline_get(). Anyway,

Reviewed-by: Hengqi Chen <hengqi.chen@gmail.com>

> Thanks,
> Leon
>
> >
> >>                 }
> >>         } else {
> >> diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c
> >> index e97aeda3a86b5..1952614778433 100644
> >> --- a/kernel/bpf/trampoline.c
> >> +++ b/kernel/bpf/trampoline.c
> >> @@ -697,8 +697,8 @@ int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,
> >>
> >>         bpf_lsm_find_cgroup_shim(prog, &bpf_func);
> >>         tr = bpf_trampoline_get(key, &tgt_info);
> >> -       if (!tr)
> >> -               return  -ENOMEM;
> >> +       if (IS_ERR(tr))
> >> +               return PTR_ERR(tr);
> >>
> >>         mutex_lock(&tr->mutex);
> >>
> >> @@ -775,7 +775,7 @@ struct bpf_trampoline *bpf_trampoline_get(u64 key,
> >>
> >>         tr = bpf_trampoline_lookup(key);
> >>         if (!tr)
> >> -               return NULL;
> >> +               return ERR_PTR(-ENOMEM);
> >>
> >>         mutex_lock(&tr->mutex);
> >>         if (tr->func.addr)
> >> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> >> index 18e673c0ac159..054063ead0e54 100644
> >> --- a/kernel/bpf/verifier.c
> >> +++ b/kernel/bpf/verifier.c
> >> @@ -19771,8 +19771,8 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
> >>
> >>         key = bpf_trampoline_compute_key(tgt_prog, prog->aux->attach_btf, btf_id);
> >>         tr = bpf_trampoline_get(key, &tgt_info);
> >> -       if (!tr)
> >> -               return -ENOMEM;
> >> +       if (IS_ERR(tr))
> >> +               return PTR_ERR(tr);
> >>
> >>         if (tgt_prog && tgt_prog->aux->tail_call_reachable)
> >>                 tr->flags = BPF_TRAMP_F_TAIL_CALL_CTX;
> >>
> >> base-commit: cbb1dbcd99b0ae74c45c4c83c6d213c12c31785c
> >> --
> >> 2.41.0
> >>
Alexei Starovoitov Sept. 15, 2023, 5:38 p.m. UTC | #5
On Thu, Sep 14, 2023 at 7:54 PM Hengqi Chen <hengqi.chen@gmail.com> wrote:
>
> On Fri, Sep 15, 2023 at 10:18 AM Leon Hwang <hffilwlqm@gmail.com> wrote:
> >
> >
> >
> > On 15/9/23 10:13, Hengqi Chen wrote:
> > > On Thu, Sep 14, 2023 at 10:51 PM Leon Hwang <hffilwlqm@gmail.com> wrote:
> > >>
> > >> Fix 'tr' dereferencing bug when CONFIG_BPF_JIT is turned off.
> > >>
> > >> Like 'bpf_trampoline_get_progs()', return 'ERR_PTR()' and then check by
> > >> 'IS_ERR()'. As a result, when CONFIG_BPF_JIT is turned off, it's able to
> > >> handle the case that 'bpf_trampoline_get()' returns
> > >> 'ERR_PTR(-EOPNOTSUPP)'.
> > >>
> > >> Fixes: 4a1e7c0c63e0 ("bpf: Support attaching freplace programs to multiple attach points")
> > >> Fixes: f7b12b6fea00 ("bpf: verifier: refactor check_attach_btf_id()")
> > >> Fixes: 69fd337a975c ("bpf: per-cgroup lsm flavor")
> > >> Reported-by: kernel test robot <lkp@intel.com>
> > >> Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
> > >> Closes: https://lore.kernel.org/r/202309131936.5Nc8eUD0-lkp@intel.com/
> > >> Signed-off-by: Leon Hwang <hffilwlqm@gmail.com>
> > >> ---
> > >>  kernel/bpf/syscall.c    | 4 ++--
> > >>  kernel/bpf/trampoline.c | 6 +++---
> > >>  kernel/bpf/verifier.c   | 4 ++--
> > >>  3 files changed, 7 insertions(+), 7 deletions(-)
> > >>
> > >> diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
> > >> index 6a692f3bea150..5748d01c99854 100644
> > >> --- a/kernel/bpf/syscall.c
> > >> +++ b/kernel/bpf/syscall.c
> > >> @@ -3211,8 +3211,8 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog,
> > >>                 }
> > >>
> > >>                 tr = bpf_trampoline_get(key, &tgt_info);
> > >> -               if (!tr) {
> > >> -                       err = -ENOMEM;
> > >> +               if (IS_ERR(tr)) {
> > >> +                       err = PTR_ERR(tr);
> > >>                         goto out_unlock;
> > >
> > > IS_ERR does not check the null case, so this should be IS_ERR_OR_NULL instead.
> >
> > Actually, bpf_trampoline_get() would not return NULL. It returns ERR_PTR(-ENOMEM)
> > or a valid ptr.
> >
>
> OK, I missed the change in bpf_trampoline_get(). Anyway,
>
> Reviewed-by: Hengqi Chen <hengqi.chen@gmail.com>

That's too much churn to address !JIT config.
Just make it return NULL in that case,
instead of hacking things all over the place.
Leon Hwang Sept. 17, 2023, 2:29 p.m. UTC | #6
On 2023/9/16 01:38, Alexei Starovoitov wrote:
> On Thu, Sep 14, 2023 at 7:54 PM Hengqi Chen <hengqi.chen@gmail.com> wrote:
>>
>> On Fri, Sep 15, 2023 at 10:18 AM Leon Hwang <hffilwlqm@gmail.com> wrote:
>>>
>>>
>>>
>>> On 15/9/23 10:13, Hengqi Chen wrote:
>>>> On Thu, Sep 14, 2023 at 10:51 PM Leon Hwang <hffilwlqm@gmail.com> wrote:
>>>>>
>>>>> Fix 'tr' dereferencing bug when CONFIG_BPF_JIT is turned off.
>>>>>
>>>>> Like 'bpf_trampoline_get_progs()', return 'ERR_PTR()' and then check by
>>>>> 'IS_ERR()'. As a result, when CONFIG_BPF_JIT is turned off, it's able to
>>>>> handle the case that 'bpf_trampoline_get()' returns
>>>>> 'ERR_PTR(-EOPNOTSUPP)'.
>>>>>
>>>>> Fixes: 4a1e7c0c63e0 ("bpf: Support attaching freplace programs to multiple attach points")
>>>>> Fixes: f7b12b6fea00 ("bpf: verifier: refactor check_attach_btf_id()")
>>>>> Fixes: 69fd337a975c ("bpf: per-cgroup lsm flavor")
>>>>> Reported-by: kernel test robot <lkp@intel.com>
>>>>> Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
>>>>> Closes: https://lore.kernel.org/r/202309131936.5Nc8eUD0-lkp@intel.com/
>>>>> Signed-off-by: Leon Hwang <hffilwlqm@gmail.com>
>>>>> ---
>>>>>  kernel/bpf/syscall.c    | 4 ++--
>>>>>  kernel/bpf/trampoline.c | 6 +++---
>>>>>  kernel/bpf/verifier.c   | 4 ++--
>>>>>  3 files changed, 7 insertions(+), 7 deletions(-)
>>>>>
>>>>> diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
>>>>> index 6a692f3bea150..5748d01c99854 100644
>>>>> --- a/kernel/bpf/syscall.c
>>>>> +++ b/kernel/bpf/syscall.c
>>>>> @@ -3211,8 +3211,8 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog,
>>>>>                 }
>>>>>
>>>>>                 tr = bpf_trampoline_get(key, &tgt_info);
>>>>> -               if (!tr) {
>>>>> -                       err = -ENOMEM;
>>>>> +               if (IS_ERR(tr)) {
>>>>> +                       err = PTR_ERR(tr);
>>>>>                         goto out_unlock;
>>>>
>>>> IS_ERR does not check the null case, so this should be IS_ERR_OR_NULL instead.
>>>
>>> Actually, bpf_trampoline_get() would not return NULL. It returns ERR_PTR(-ENOMEM)
>>> or a valid ptr.
>>>
>>
>> OK, I missed the change in bpf_trampoline_get(). Anyway,
>>
>> Reviewed-by: Hengqi Chen <hengqi.chen@gmail.com>
> 
> That's too much churn to address !JIT config.
> Just make it return NULL in that case,
> instead of hacking things all over the place.

OK, I'll do it in v2 patch.

Thanks,
Leon
diff mbox series

Patch

diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 6a692f3bea150..5748d01c99854 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -3211,8 +3211,8 @@  static int bpf_tracing_prog_attach(struct bpf_prog *prog,
 		}
 
 		tr = bpf_trampoline_get(key, &tgt_info);
-		if (!tr) {
-			err = -ENOMEM;
+		if (IS_ERR(tr)) {
+			err = PTR_ERR(tr);
 			goto out_unlock;
 		}
 	} else {
diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c
index e97aeda3a86b5..1952614778433 100644
--- a/kernel/bpf/trampoline.c
+++ b/kernel/bpf/trampoline.c
@@ -697,8 +697,8 @@  int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,
 
 	bpf_lsm_find_cgroup_shim(prog, &bpf_func);
 	tr = bpf_trampoline_get(key, &tgt_info);
-	if (!tr)
-		return  -ENOMEM;
+	if (IS_ERR(tr))
+		return PTR_ERR(tr);
 
 	mutex_lock(&tr->mutex);
 
@@ -775,7 +775,7 @@  struct bpf_trampoline *bpf_trampoline_get(u64 key,
 
 	tr = bpf_trampoline_lookup(key);
 	if (!tr)
-		return NULL;
+		return ERR_PTR(-ENOMEM);
 
 	mutex_lock(&tr->mutex);
 	if (tr->func.addr)
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 18e673c0ac159..054063ead0e54 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -19771,8 +19771,8 @@  static int check_attach_btf_id(struct bpf_verifier_env *env)
 
 	key = bpf_trampoline_compute_key(tgt_prog, prog->aux->attach_btf, btf_id);
 	tr = bpf_trampoline_get(key, &tgt_info);
-	if (!tr)
-		return -ENOMEM;
+	if (IS_ERR(tr))
+		return PTR_ERR(tr);
 
 	if (tgt_prog && tgt_prog->aux->tail_call_reachable)
 		tr->flags = BPF_TRAMP_F_TAIL_CALL_CTX;