Skip to contentSkip to conversation

libbpf: fix sign expansion bug in btf_dump_get_enum_value()

PATCH Applied Dan Carpenter 3 participants 5 messages Jul 19, 2022 – Jul 21, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libbpf: fix sign expansion bug in btf_dump_get_enum_value()

The code here is supposed to take a signed int and store it in a signed long long. Unfortunately, the way that the type promotion works with this conditional statement is that it takes a signed int, type promotes it to a __u32, and then stores that as a signed long long. The result is never negative.

Fixes d90ec262b35b ("libbpf: Add enum64 support for btf_dump")
---
 tools/lib/bpf/btf_dump.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/btf_dump.c +1 -1
index 400e84fd0578..627edb5bb6de 100644
@@ -2045,7 +2045,7 @@ static int btf_dump_get_enum_value(struct btf_dump *d,
 		*value = *(__s64 *)data;
 		return 0;
 	case 4:
-		*value = is_signed ? *(__s32 *)data : *(__u32 *)data;
+		*value = is_signed ? (__s64)*(__s32 *)data : *(__u32 *)data;
 		return 0;
 	case 2:
 		*value = is_signed ? *(__s16 *)data : *(__u16 *)data;
Signature · 1 line
2.35.1
#1 lore raw
Quoted 17 lines · Dan Carpenter

The code here is supposed to take a signed int and store it in a signed long long. Unfortunately, the way that the type promotion works with this conditional statement is that it takes a signed int, type promotes it to a __u32, and then stores that as a signed long long. The result is never negative.

Fixes d90ec262b35b ("libbpf: Add enum64 support for btf_dump")
---
 tools/lib/bpf/btf_dump.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/btf_dump.c +0 -0
index 400e84fd0578..627edb5bb6de 100644

tools/lib/bpf/btf_dump.c static int btf_dump_get_enum_value(struct btf_dump *d, · line 2048

Quoted 5 lines
tools/lib/bpf/btf_dump.c +1 -1
@@ -2045,7 +2045,7 @@ static int btf_dump_get_enum_value(struct btf_dump *d,
 		*value = *(__s64 *)data;
 		return 0;
 	case 4:
-		*value = is_signed ? *(__s32 *)data : *(__u32 *)data;
+		*value = is_signed ? (__s64)*(__s32 *)data : *(__u32 *)data;

Only case 4 has issues and what does the standard say ?

Do you have a sample dump to debug this that can be pasted in the commit log?

Quoted 6 lines
tools/lib/bpf/btf_dump.c +0 -0
 		return 0;
 	case 2:
 		*value = is_signed ? *(__s16 *)data : *(__u16 *)data;
Signature · 1 line
2.35.1
#2 lore raw
Quoted 13 lines · Martin KaFai Lau
Quoted 12 lines · Dan Carpenter

The code here is supposed to take a signed int and store it in a signed long long. Unfortunately, the way that the type promotion works with this conditional statement is that it takes a signed int, type promotes it to a __u32, and then stores that as a signed long long. The result is never negative.

Fixes d90ec262b35b ("libbpf: Add enum64 support for btf_dump")
---
 tools/lib/bpf/btf_dump.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines

tools/lib/bpf/btf_dump.c static int btf_dump_get_enum_value(struct btf_dump *d, · line 2048

Quoted 10 lines
tools/lib/bpf/btf_dump.c +1 -1
index 400e84fd0578..627edb5bb6de 100644
@@ -2045,7 +2045,7 @@ static int btf_dump_get_enum_value(struct btf_dump *d,
 		*value = *(__s64 *)data;
 		return 0;
 	case 4:
-		*value = is_signed ? *(__s32 *)data : *(__u32 *)data;
+		*value = is_signed ? (__s64)*(__s32 *)data : *(__u32 *)data;

Only case 4 has issues and what does the standard say ?

It looks weird, doesn't it?

Yes. Everything smaller than int gets type promoted to int so the sign is extended properly. The only thing larger than s/u32 is s/u64 which is already the right size.

Quoted 1 line

Do you have a sample dump to debug this that can be pasted in the commit log?

This is from static analysis, but I made a little test program just to test it before I sent the patch:

#include <stdio.h>

int main(void)

{
        unsigned long long src = -1ULL;
        signed long long dst1, dst2;
        int is_signed = 1;

        dst1 = is_signed ? *(int *)&src : *(unsigned int *)0;
        dst2 = is_signed ? (signed long long)*(int *)&src : *(unsigned int *)0;

        printf("%lld\n", dst1);
        printf("%lld\n", dst2);

        return 0;
}

regards,
dan carpenter

#3 lore raw
Quoted 27 lines · Dan Carpenter
Quoted 13 lines · Martin KaFai Lau
Quoted 12 lines · Dan Carpenter

The code here is supposed to take a signed int and store it in a signed long long. Unfortunately, the way that the type promotion works with this conditional statement is that it takes a signed int, type promotes it to a __u32, and then stores that as a signed long long. The result is never negative.

Fixes d90ec262b35b ("libbpf: Add enum64 support for btf_dump")
---
 tools/lib/bpf/btf_dump.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines

tools/lib/bpf/btf_dump.c static int btf_dump_get_enum_value(struct btf_dump *d, · line 2048

Quoted 10 lines
tools/lib/bpf/btf_dump.c +1 -1
index 400e84fd0578..627edb5bb6de 100644
@@ -2045,7 +2045,7 @@ static int btf_dump_get_enum_value(struct btf_dump *d,
 		*value = *(__s64 *)data;
 		return 0;
 	case 4:
-		*value = is_signed ? *(__s32 *)data : *(__u32 *)data;
+		*value = is_signed ? (__s64)*(__s32 *)data : *(__u32 *)data;

Only case 4 has issues and what does the standard say ?

Quoted 5 lines

It looks weird, doesn't it?

Yes. Everything smaller than int gets type promoted to int so the sign is extended properly. The only thing larger than s/u32 is s/u64 which is already the right size.

Ah. tricky.

Quoted 9 lines
Quoted 2 lines

Do you have a sample dump to debug this that can be pasted in the commit log?

This is from static analysis, but I made a little test program just to test it before I sent the patch:

#include <stdio.h>

int main(void)

Quoted 13 lines
{
        unsigned long long src = -1ULL;
        signed long long dst1, dst2;
        int is_signed = 1;

        dst1 = is_signed ? *(int *)&src : *(unsigned int *)0;
        dst2 = is_signed ? (signed long long)*(int *)&src : *(unsigned int *)0;

        printf("%lld\n", dst1);
        printf("%lld\n", dst2);

        return 0;
}

Thanks for the demo.

#4 lore raw
In reply to Dan Carpenter
This patch was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Hello:

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

Quoted 10 lines · On Tue, 19 Jul 2022 12:49:34 +0300 you wrote:

The code here is supposed to take a signed int and store it in a signed long long. Unfortunately, the way that the type promotion works with this conditional statement is that it takes a signed int, type promotes it to a __u32, and then stores that as a signed long long. The result is never negative.

Fixes d90ec262b35b ("libbpf: Add enum64 support for btf_dump")

[...]

Here is the summary with links:
- libbpf: fix sign expansion bug in btf_dump_get_enum_value()

    https://git.kernel.org/bpf/bpf-next/c/c6018fc6e7b6

You are awesome, thank you!

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