Skip to contentSkip to conversation

bpftool: fix a wrong type cast in btf_dumper_int

PATCH Applied Lam Thai 5 participants 6 messages Aug 24, 2022 – Aug 25, 2022

Subsystems (2)
lore
Share & tools
lore

Lam Thai

author
#0 lore raw

PATCH 1/1 bpftool: fix a wrong type cast in btf_dumper_int

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")
---
 tools/bpf/bpftool/btf_dumper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/bpf/bpftool/btf_dumper.c +1 -1
index 125798b0bc5d..19924b6ce796 100644
@@ -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

Signature · 1 line
2.37.0
#1 lore raw
Quoted 3 lines · 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.

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

Quoted 4 lines
Fixes b12d6ec09730 ("bpf: btf: add btf print functionality")

---

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

Quoted 21 lines
 tools/bpf/bpftool/btf_dumper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/bpf/bpftool/btf_dumper.c +1 -1
index 125798b0bc5d..19924b6ce796 100644
@@ -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

Signature · 1 line
2.37.0
Reviewed-by
#2 lore raw
Quoted 6 lines · John Fastabend
Quoted 3 lines · 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.

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

Quoted 12 lines
Quoted 5 lines
Fixes b12d6ec09730 ("bpf: btf: add btf print functionality")

---

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

Quoted 3 lines
 tools/bpf/bpftool/btf_dumper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 10 lines

tools/bpf/bpftool/btf_dumper.c static int btf_dumper_int(const struct btf_type *t, __u8 bit_offset, · line 455

Quoted 10 lines
tools/bpf/bpftool/btf_dumper.c +1 -1
index 125798b0bc5d..19924b6ce796 100644
@@ -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

#3 lore raw
Quoted 53 lines · Quentin Monnet
Quoted 6 lines · John Fastabend
Quoted 3 lines · 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.

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
                }
            }
        }
    ]
Quoted 5 lines

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.

Quoted 26 lines
Quoted 13 lines
Quoted 6 lines
Fixes b12d6ec09730 ("bpf: btf: add btf print functionality")

---

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

Quoted 3 lines
 tools/bpf/bpftool/btf_dumper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 10 lines

tools/bpf/bpftool/btf_dumper.c static int btf_dumper_int(const struct btf_type *t, __u8 bit_offset, · line 455

Quoted 10 lines
tools/bpf/bpftool/btf_dumper.c +1 -1
index 125798b0bc5d..19924b6ce796 100644
@@ -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

#4 lore raw
In reply to Quentin Monnet
Quoted 54 lines · Quentin Monnet
Quoted 6 lines · John Fastabend
Quoted 3 lines · 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.

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
                }
            }
        }
    ]
Quoted 3 lines

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.

Quoted 28 lines

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

Quoted 12 lines
Quoted 5 lines
Fixes b12d6ec09730 ("bpf: btf: add btf print functionality")

---

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

Quoted 3 lines
 tools/bpf/bpftool/btf_dumper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 10 lines

tools/bpf/bpftool/btf_dumper.c static int btf_dumper_int(const struct btf_type *t, __u8 bit_offset, · line 455

Quoted 10 lines
tools/bpf/bpftool/btf_dumper.c +1 -1
index 125798b0bc5d..19924b6ce796 100644
@@ -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

#5 lore raw
In reply to Lam Thai
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Quoted 8 lines · 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")

[...]

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!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html