2020-11-10 11:52:20

by Kaixu Xia

[permalink] [raw]
Subject: [PATCH] bpf: Fix unsigned 'datasec_id' compared with zero in check_pseudo_btf_id

From: Kaixu Xia <[email protected]>

The unsigned variable datasec_id is assigned a return value from the call
to check_pseudo_btf_id(), which may return negative error code.

Fixes coccicheck warning:

./kernel/bpf/verifier.c:9616:5-15: WARNING: Unsigned expression compared with zero: datasec_id > 0

Reported-by: Tosk Robot <[email protected]>
Signed-off-by: Kaixu Xia <[email protected]>
---
kernel/bpf/verifier.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 6200519582a6..e9d8d4309bb4 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -9572,7 +9572,7 @@ static int check_pseudo_btf_id(struct bpf_verifier_env *env,
struct bpf_insn *insn,
struct bpf_insn_aux_data *aux)
{
- u32 datasec_id, type, id = insn->imm;
+ s32 datasec_id, type, id = insn->imm;
const struct btf_var_secinfo *vsi;
const struct btf_type *datasec;
const struct btf_type *t;
--
2.20.0


2020-11-10 13:05:27

by Denis Kirjanov

[permalink] [raw]
Subject: Re: [PATCH] bpf: Fix unsigned 'datasec_id' compared with zero in check_pseudo_btf_id

On 11/10/20, [email protected] <[email protected]> wrote:
> From: Kaixu Xia <[email protected]>
>
> The unsigned variable datasec_id is assigned a return value from the call
> to check_pseudo_btf_id(), which may return negative error code.
>
> Fixes coccicheck warning:
>
> ./kernel/bpf/verifier.c:9616:5-15: WARNING: Unsigned expression compared
> with zero: datasec_id > 0
>
> Reported-by: Tosk Robot <[email protected]>
> Signed-off-by: Kaixu Xia <[email protected]>
> ---
> kernel/bpf/verifier.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 6200519582a6..e9d8d4309bb4 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -9572,7 +9572,7 @@ static int check_pseudo_btf_id(struct bpf_verifier_env
> *env,
> struct bpf_insn *insn,
> struct bpf_insn_aux_data *aux)
> {
> - u32 datasec_id, type, id = insn->imm;
> + s32 datasec_id, type, id = insn->imm;

but the value is passed as u32 to btf_type_by_id()...

btf_find_by_name_kind() returns s32


> const struct btf_var_secinfo *vsi;
> const struct btf_type *datasec;
> const struct btf_type *t;
> --
> 2.20.0
>
>

2020-11-10 18:44:55

by Andrii Nakryiko

[permalink] [raw]
Subject: Re: [PATCH] bpf: Fix unsigned 'datasec_id' compared with zero in check_pseudo_btf_id

On Tue, Nov 10, 2020 at 3:50 AM <[email protected]> wrote:
>
> From: Kaixu Xia <[email protected]>
>
> The unsigned variable datasec_id is assigned a return value from the call
> to check_pseudo_btf_id(), which may return negative error code.
>
> Fixes coccicheck warning:
>
> ./kernel/bpf/verifier.c:9616:5-15: WARNING: Unsigned expression compared with zero: datasec_id > 0
>
> Reported-by: Tosk Robot <[email protected]>
> Signed-off-by: Kaixu Xia <[email protected]>
> ---
> kernel/bpf/verifier.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 6200519582a6..e9d8d4309bb4 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -9572,7 +9572,7 @@ static int check_pseudo_btf_id(struct bpf_verifier_env *env,
> struct bpf_insn *insn,
> struct bpf_insn_aux_data *aux)
> {
> - u32 datasec_id, type, id = insn->imm;
> + s32 datasec_id, type, id = insn->imm;

you are changing types for type and id variables here, so split out
datasec_id definition into a separate line

> const struct btf_var_secinfo *vsi;
> const struct btf_type *datasec;
> const struct btf_type *t;
> --
> 2.20.0
>

2020-11-10 18:45:22

by Andrii Nakryiko

[permalink] [raw]
Subject: Re: [PATCH] bpf: Fix unsigned 'datasec_id' compared with zero in check_pseudo_btf_id

On Tue, Nov 10, 2020 at 5:02 AM Denis Kirjanov <[email protected]> wrote:
>
> On 11/10/20, [email protected] <[email protected]> wrote:
> > From: Kaixu Xia <[email protected]>
> >
> > The unsigned variable datasec_id is assigned a return value from the call
> > to check_pseudo_btf_id(), which may return negative error code.
> >
> > Fixes coccicheck warning:
> >
> > ./kernel/bpf/verifier.c:9616:5-15: WARNING: Unsigned expression compared
> > with zero: datasec_id > 0
> >
> > Reported-by: Tosk Robot <[email protected]>
> > Signed-off-by: Kaixu Xia <[email protected]>
> > ---
> > kernel/bpf/verifier.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> > index 6200519582a6..e9d8d4309bb4 100644
> > --- a/kernel/bpf/verifier.c
> > +++ b/kernel/bpf/verifier.c
> > @@ -9572,7 +9572,7 @@ static int check_pseudo_btf_id(struct bpf_verifier_env
> > *env,
> > struct bpf_insn *insn,
> > struct bpf_insn_aux_data *aux)
> > {
> > - u32 datasec_id, type, id = insn->imm;
> > + s32 datasec_id, type, id = insn->imm;
>
> but the value is passed as u32 to btf_type_by_id()...
>
> btf_find_by_name_kind() returns s32

Right, valid range of BTF type IDs are >= 0 and (significantly) less
than INT_MAX. So s32 is used to signal valid BTF ID or negative error,
but all the APIs accepting BTF ID accept it as just u32.

>
>
> > const struct btf_var_secinfo *vsi;
> > const struct btf_type *datasec;
> > const struct btf_type *t;
> > --
> > 2.20.0
> >
> >