bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] bpftool: fix a wrong type cast in btf_dumper_int
@ 2022-08-24 22:59 Lam Thai
  2022-08-25  6:29 ` John Fastabend
  2022-08-25 18:50 ` patchwork-bot+netdevbpf
  0 siblings, 2 replies; 6+ messages in thread
From: Lam Thai @ 2022-08-24 22:59 UTC (permalink / raw)
  To: bpf; +Cc: Lam Thai

When `data` points to a boolean value, casting it to `int *` is problematic
and could lead to a wrong value being passed to `jsonw_bool`. Change the
cast to `bool *` instead.

Fixes: b12d6ec09730 ("bpf: btf: add btf print functionality")
Signed-off-by: Lam Thai <lamthai@arista.com>
---
 tools/bpf/bpftool/btf_dumper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/bpf/bpftool/btf_dumper.c b/tools/bpf/bpftool/btf_dumper.c
index 125798b0bc5d..19924b6ce796 100644
--- a/tools/bpf/bpftool/btf_dumper.c
+++ b/tools/bpf/bpftool/btf_dumper.c
@@ -452,7 +452,7 @@ static int btf_dumper_int(const struct btf_type *t, __u8 bit_offset,
 					     *(char *)data);
 		break;
 	case BTF_INT_BOOL:
-		jsonw_bool(jw, *(int *)data);
+		jsonw_bool(jw, *(bool *)data);
 		break;
 	default:
 		/* shouldn't happen */

base-commit: 6fc2838b148f8fe6aa14fc435e666984a0505018
-- 
2.37.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* RE: [PATCH] bpftool: fix a wrong type cast in btf_dumper_int
  2022-08-24 22:59 [PATCH] bpftool: fix a wrong type cast in btf_dumper_int Lam Thai
@ 2022-08-25  6:29 ` John Fastabend
  2022-08-25  8:56   ` Quentin Monnet
  2022-08-25 18:50 ` patchwork-bot+netdevbpf
  1 sibling, 1 reply; 6+ messages in thread
From: John Fastabend @ 2022-08-25  6:29 UTC (permalink / raw)
  To: Lam Thai, bpf; +Cc: Lam Thai

Lam Thai wrote:
> When `data` points to a boolean value, casting it to `int *` is problematic
> and could lead to a wrong value being passed to `jsonw_bool`. Change the
> cast to `bool *` instead.

How is it problematic? Its from BTF_KIND_INT by my quick reading.

> 
> Fixes: b12d6ec09730 ("bpf: btf: add btf print functionality")
> Signed-off-by: Lam Thai <lamthai@arista.com>
> ---

for bpf-next looks like a nice cleanup, I don't think its needed for bpf
tree?

>  tools/bpf/bpftool/btf_dumper.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/tools/bpf/bpftool/btf_dumper.c b/tools/bpf/bpftool/btf_dumper.c
> index 125798b0bc5d..19924b6ce796 100644
> --- a/tools/bpf/bpftool/btf_dumper.c
> +++ b/tools/bpf/bpftool/btf_dumper.c
> @@ -452,7 +452,7 @@ static int btf_dumper_int(const struct btf_type *t, __u8 bit_offset,
>  					     *(char *)data);
>  		break;
>  	case BTF_INT_BOOL:
> -		jsonw_bool(jw, *(int *)data);
> +		jsonw_bool(jw, *(bool *)data);
>  		break;
>  	default:
>  		/* shouldn't happen */
> 
> base-commit: 6fc2838b148f8fe6aa14fc435e666984a0505018
> -- 
> 2.37.0
> 



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] bpftool: fix a wrong type cast in btf_dumper_int
  2022-08-25  6:29 ` John Fastabend
@ 2022-08-25  8:56   ` Quentin Monnet
  2022-08-25 15:54     ` John Fastabend
  2022-08-25 18:44     ` Andrii Nakryiko
  0 siblings, 2 replies; 6+ messages in thread
From: Quentin Monnet @ 2022-08-25  8:56 UTC (permalink / raw)
  To: John Fastabend, Lam Thai, bpf

On 25/08/2022 07:29, John Fastabend wrote:
> Lam Thai wrote:
>> When `data` points to a boolean value, casting it to `int *` is problematic
>> and could lead to a wrong value being passed to `jsonw_bool`. Change the
>> cast to `bool *` instead.
> 
> How is it problematic? Its from BTF_KIND_INT by my quick reading.

Hi John, it's an INT but it also has a size of 1:

    struct map_value {
       int a;
       int b;
       short c;
       bool d;
    };

    # bpftool btf dump id 1107
    [...]
    [2] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED
    [...]
    [12] STRUCT 'map_value' size=12 vlen=4
            'a' type_id=2 bits_offset=0
            'b' type_id=2 bits_offset=32
            'c' type_id=13 bits_offset=64
            'd' type_id=14 bits_offset=80
    [13] INT 'short' size=2 bits_offset=0 nr_bits=16 encoding=SIGNED
    [14] INT '_Bool' size=1 bits_offset=0 nr_bits=8 encoding=BOOL
    [...]

And Lam reported [0] that the pretty-print for the map does not display
the correct boolean value, because it reads too many bytes from this
*(int *)data.

    # bpftool map dump name my_map --pretty
    [{
            "key": ["0x00","0x00","0x00","0x00"
            ],
            "value":
["0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00"
            ],
            "formatted": {
                "key": 0,
                "value": {
                    "a": 0,
                    "b": 0,
                    "c": 0,
                    "d": true
                }
            }
        }
    ]

The above is before the map gets any update. The bytes in "value" look
correct, but "d" says "true" when it should be "false". So bpf tree
would make sense to me.

[0] https://github.com/libbpf/bpftool/issues/38

> 
>>
>> Fixes: b12d6ec09730 ("bpf: btf: add btf print functionality")
>> Signed-off-by: Lam Thai <lamthai@arista.com>
>> ---
> 
> for bpf-next looks like a nice cleanup, I don't think its needed for bpf
> tree?
> 
>>  tools/bpf/bpftool/btf_dumper.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/tools/bpf/bpftool/btf_dumper.c b/tools/bpf/bpftool/btf_dumper.c
>> index 125798b0bc5d..19924b6ce796 100644
>> --- a/tools/bpf/bpftool/btf_dumper.c
>> +++ b/tools/bpf/bpftool/btf_dumper.c
>> @@ -452,7 +452,7 @@ static int btf_dumper_int(const struct btf_type *t, __u8 bit_offset,
>>  					     *(char *)data);
>>  		break;
>>  	case BTF_INT_BOOL:
>> -		jsonw_bool(jw, *(int *)data);
>> +		jsonw_bool(jw, *(bool *)data);

Looks good, thanks
Reviewed-by: Quentin Monnet <quentin@isovalent.com>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] bpftool: fix a wrong type cast in btf_dumper_int
  2022-08-25  8:56   ` Quentin Monnet
@ 2022-08-25 15:54     ` John Fastabend
  2022-08-25 18:44     ` Andrii Nakryiko
  1 sibling, 0 replies; 6+ messages in thread
From: John Fastabend @ 2022-08-25 15:54 UTC (permalink / raw)
  To: Quentin Monnet, John Fastabend, Lam Thai, bpf

Quentin Monnet wrote:
> On 25/08/2022 07:29, John Fastabend wrote:
> > Lam Thai wrote:
> >> When `data` points to a boolean value, casting it to `int *` is problematic
> >> and could lead to a wrong value being passed to `jsonw_bool`. Change the
> >> cast to `bool *` instead.
> > 
> > How is it problematic? Its from BTF_KIND_INT by my quick reading.
> 
> Hi John, it's an INT but it also has a size of 1:
> 
>     struct map_value {
>        int a;
>        int b;
>        short c;
>        bool d;
>     };
> 
>     # bpftool btf dump id 1107
>     [...]
>     [2] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED
>     [...]
>     [12] STRUCT 'map_value' size=12 vlen=4
>             'a' type_id=2 bits_offset=0
>             'b' type_id=2 bits_offset=32
>             'c' type_id=13 bits_offset=64
>             'd' type_id=14 bits_offset=80
>     [13] INT 'short' size=2 bits_offset=0 nr_bits=16 encoding=SIGNED
>     [14] INT '_Bool' size=1 bits_offset=0 nr_bits=8 encoding=BOOL
>     [...]
> 
> And Lam reported [0] that the pretty-print for the map does not display
> the correct boolean value, because it reads too many bytes from this
> *(int *)data.
> 
>     # bpftool map dump name my_map --pretty
>     [{
>             "key": ["0x00","0x00","0x00","0x00"
>             ],
>             "value":
> ["0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00"
>             ],
>             "formatted": {
>                 "key": 0,
>                 "value": {
>                     "a": 0,
>                     "b": 0,
>                     "c": 0,
>                     "d": true
>                 }
>             }
>         }
>     ]
> 
> The above is before the map gets any update. The bytes in "value" look
> correct, but "d" says "true" when it should be "false". So bpf tree
> would make sense to me.
> 
> [0] https://github.com/libbpf/bpftool/issues/38

Thanks for the explanation. It would be nice to add the above in the
commit message.

Otherwise though.

Acked-by: John Fastabend <john.fastabend@gmail.com>

> 
> > 
> >>
> >> Fixes: b12d6ec09730 ("bpf: btf: add btf print functionality")
> >> Signed-off-by: Lam Thai <lamthai@arista.com>
> >> ---
> > 
> > for bpf-next looks like a nice cleanup, I don't think its needed for bpf
> > tree?
> > 
> >>  tools/bpf/bpftool/btf_dumper.c | 2 +-
> >>  1 file changed, 1 insertion(+), 1 deletion(-)
> >>
> >> diff --git a/tools/bpf/bpftool/btf_dumper.c b/tools/bpf/bpftool/btf_dumper.c
> >> index 125798b0bc5d..19924b6ce796 100644
> >> --- a/tools/bpf/bpftool/btf_dumper.c
> >> +++ b/tools/bpf/bpftool/btf_dumper.c
> >> @@ -452,7 +452,7 @@ static int btf_dumper_int(const struct btf_type *t, __u8 bit_offset,
> >>  					     *(char *)data);
> >>  		break;
> >>  	case BTF_INT_BOOL:
> >> -		jsonw_bool(jw, *(int *)data);
> >> +		jsonw_bool(jw, *(bool *)data);
> 
> Looks good, thanks
> Reviewed-by: Quentin Monnet <quentin@isovalent.com>



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] bpftool: fix a wrong type cast in btf_dumper_int
  2022-08-25  8:56   ` Quentin Monnet
  2022-08-25 15:54     ` John Fastabend
@ 2022-08-25 18:44     ` Andrii Nakryiko
  1 sibling, 0 replies; 6+ messages in thread
From: Andrii Nakryiko @ 2022-08-25 18:44 UTC (permalink / raw)
  To: Quentin Monnet; +Cc: John Fastabend, Lam Thai, bpf

On Thu, Aug 25, 2022 at 1:57 AM Quentin Monnet <quentin@isovalent.com> wrote:
>
> On 25/08/2022 07:29, John Fastabend wrote:
> > Lam Thai wrote:
> >> When `data` points to a boolean value, casting it to `int *` is problematic
> >> and could lead to a wrong value being passed to `jsonw_bool`. Change the
> >> cast to `bool *` instead.
> >
> > How is it problematic? Its from BTF_KIND_INT by my quick reading.
>
> Hi John, it's an INT but it also has a size of 1:
>
>     struct map_value {
>        int a;
>        int b;
>        short c;
>        bool d;
>     };
>
>     # bpftool btf dump id 1107
>     [...]
>     [2] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED
>     [...]
>     [12] STRUCT 'map_value' size=12 vlen=4
>             'a' type_id=2 bits_offset=0
>             'b' type_id=2 bits_offset=32
>             'c' type_id=13 bits_offset=64
>             'd' type_id=14 bits_offset=80
>     [13] INT 'short' size=2 bits_offset=0 nr_bits=16 encoding=SIGNED
>     [14] INT '_Bool' size=1 bits_offset=0 nr_bits=8 encoding=BOOL
>     [...]
>
> And Lam reported [0] that the pretty-print for the map does not display
> the correct boolean value, because it reads too many bytes from this
> *(int *)data.
>
>     # bpftool map dump name my_map --pretty
>     [{
>             "key": ["0x00","0x00","0x00","0x00"
>             ],
>             "value":
> ["0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00"
>             ],
>             "formatted": {
>                 "key": 0,
>                 "value": {
>                     "a": 0,
>                     "b": 0,
>                     "c": 0,
>                     "d": true
>                 }
>             }
>         }
>     ]
>
> The above is before the map gets any update. The bytes in "value" look
> correct, but "d" says "true" when it should be "false". So bpf tree
> would make sense to me.

That code is back from 2018. Alexei recently clarified that bpf is for
hi-pri and urgent fixes. I don't think this one classifies as such.
Plus bpftool itself should be packaged from github mirror, so this fix
will make it there fast. Applied to bpf-next.

>
> [0] https://github.com/libbpf/bpftool/issues/38
>
> >
> >>
> >> Fixes: b12d6ec09730 ("bpf: btf: add btf print functionality")
> >> Signed-off-by: Lam Thai <lamthai@arista.com>
> >> ---
> >
> > for bpf-next looks like a nice cleanup, I don't think its needed for bpf
> > tree?
> >
> >>  tools/bpf/bpftool/btf_dumper.c | 2 +-
> >>  1 file changed, 1 insertion(+), 1 deletion(-)
> >>
> >> diff --git a/tools/bpf/bpftool/btf_dumper.c b/tools/bpf/bpftool/btf_dumper.c
> >> index 125798b0bc5d..19924b6ce796 100644
> >> --- a/tools/bpf/bpftool/btf_dumper.c
> >> +++ b/tools/bpf/bpftool/btf_dumper.c
> >> @@ -452,7 +452,7 @@ static int btf_dumper_int(const struct btf_type *t, __u8 bit_offset,
> >>                                           *(char *)data);
> >>              break;
> >>      case BTF_INT_BOOL:
> >> -            jsonw_bool(jw, *(int *)data);
> >> +            jsonw_bool(jw, *(bool *)data);
>
> Looks good, thanks
> Reviewed-by: Quentin Monnet <quentin@isovalent.com>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] bpftool: fix a wrong type cast in btf_dumper_int
  2022-08-24 22:59 [PATCH] bpftool: fix a wrong type cast in btf_dumper_int Lam Thai
  2022-08-25  6:29 ` John Fastabend
@ 2022-08-25 18:50 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 6+ messages in thread
From: patchwork-bot+netdevbpf @ 2022-08-25 18:50 UTC (permalink / raw)
  To: Lam Thai; +Cc: bpf

Hello:

This patch was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:

On Wed, 24 Aug 2022 15:59:00 -0700 you wrote:
> When `data` points to a boolean value, casting it to `int *` is problematic
> and could lead to a wrong value being passed to `jsonw_bool`. Change the
> cast to `bool *` instead.
> 
> Fixes: b12d6ec09730 ("bpf: btf: add btf print functionality")
> Signed-off-by: Lam Thai <lamthai@arista.com>
> 
> [...]

Here is the summary with links:
  - bpftool: fix a wrong type cast in btf_dumper_int
    https://git.kernel.org/bpf/bpf-next/c/7184aef9c0f7

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-08-25 18:50 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-24 22:59 [PATCH] bpftool: fix a wrong type cast in btf_dumper_int Lam Thai
2022-08-25  6:29 ` John Fastabend
2022-08-25  8:56   ` Quentin Monnet
2022-08-25 15:54     ` John Fastabend
2022-08-25 18:44     ` Andrii Nakryiko
2022-08-25 18:50 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).