* [PATCH] libbpf: fix sign expansion bug in btf_dump_get_enum_value()
@ 2022-07-19 9:49 Dan Carpenter
2022-07-19 17:26 ` Martin KaFai Lau
2022-07-21 12:30 ` patchwork-bot+netdevbpf
0 siblings, 2 replies; 5+ messages in thread
From: Dan Carpenter @ 2022-07-19 9:49 UTC (permalink / raw)
To: Andrii Nakryiko, Yonghong Song
Cc: Alexei Starovoitov, Daniel Borkmann, Martin KaFai Lau, Song Liu,
John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
bpf, kernel-janitors
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")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
---
tools/lib/bpf/btf_dump.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
index 400e84fd0578..627edb5bb6de 100644
--- a/tools/lib/bpf/btf_dump.c
+++ b/tools/lib/bpf/btf_dump.c
@@ -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;
--
2.35.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] libbpf: fix sign expansion bug in btf_dump_get_enum_value()
2022-07-19 9:49 [PATCH] libbpf: fix sign expansion bug in btf_dump_get_enum_value() Dan Carpenter
@ 2022-07-19 17:26 ` Martin KaFai Lau
2022-07-19 18:34 ` Dan Carpenter
2022-07-21 12:30 ` patchwork-bot+netdevbpf
1 sibling, 1 reply; 5+ messages in thread
From: Martin KaFai Lau @ 2022-07-19 17:26 UTC (permalink / raw)
To: Dan Carpenter
Cc: Andrii Nakryiko, Yonghong Song, Alexei Starovoitov,
Daniel Borkmann, Martin KaFai Lau, Song Liu, John Fastabend,
KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, bpf,
kernel-janitors
On Tue, Jul 19, 2022 at 12:49:34PM +0300, Dan Carpenter 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")
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> ---
> tools/lib/bpf/btf_dump.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
> index 400e84fd0578..627edb5bb6de 100644
> --- a/tools/lib/bpf/btf_dump.c
> +++ b/tools/lib/bpf/btf_dump.c
> @@ -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?
> return 0;
> case 2:
> *value = is_signed ? *(__s16 *)data : *(__u16 *)data;
> --
> 2.35.1
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] libbpf: fix sign expansion bug in btf_dump_get_enum_value()
2022-07-19 17:26 ` Martin KaFai Lau
@ 2022-07-19 18:34 ` Dan Carpenter
2022-07-19 23:39 ` Martin KaFai Lau
0 siblings, 1 reply; 5+ messages in thread
From: Dan Carpenter @ 2022-07-19 18:34 UTC (permalink / raw)
To: Martin KaFai Lau
Cc: Andrii Nakryiko, Yonghong Song, Alexei Starovoitov,
Daniel Borkmann, Martin KaFai Lau, Song Liu, John Fastabend,
KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, bpf,
kernel-janitors
On Tue, Jul 19, 2022 at 10:26:40AM -0700, Martin KaFai Lau wrote:
> On Tue, Jul 19, 2022 at 12:49:34PM +0300, Dan Carpenter 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")
> > Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> > ---
> > tools/lib/bpf/btf_dump.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
> > index 400e84fd0578..627edb5bb6de 100644
> > --- a/tools/lib/bpf/btf_dump.c
> > +++ b/tools/lib/bpf/btf_dump.c
> > @@ -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.
> 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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] libbpf: fix sign expansion bug in btf_dump_get_enum_value()
2022-07-19 18:34 ` Dan Carpenter
@ 2022-07-19 23:39 ` Martin KaFai Lau
0 siblings, 0 replies; 5+ messages in thread
From: Martin KaFai Lau @ 2022-07-19 23:39 UTC (permalink / raw)
To: Dan Carpenter
Cc: Andrii Nakryiko, Yonghong Song, Alexei Starovoitov,
Daniel Borkmann, Martin KaFai Lau, Song Liu, John Fastabend,
KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, bpf,
kernel-janitors
On Tue, Jul 19, 2022 at 09:34:13PM +0300, Dan Carpenter wrote:
> On Tue, Jul 19, 2022 at 10:26:40AM -0700, Martin KaFai Lau wrote:
> > On Tue, Jul 19, 2022 at 12:49:34PM +0300, Dan Carpenter 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")
> > > Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> > > ---
> > > tools/lib/bpf/btf_dump.c | 2 +-
> > > 1 file changed, 1 insertion(+), 1 deletion(-)
> > >
> > > diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
> > > index 400e84fd0578..627edb5bb6de 100644
> > > --- a/tools/lib/bpf/btf_dump.c
> > > +++ b/tools/lib/bpf/btf_dump.c
> > > @@ -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.
Ah. tricky.
>
> > 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;
> }
Thanks for the demo.
Acked-by: Martin KaFai Lau <kafai@fb.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] libbpf: fix sign expansion bug in btf_dump_get_enum_value()
2022-07-19 9:49 [PATCH] libbpf: fix sign expansion bug in btf_dump_get_enum_value() Dan Carpenter
2022-07-19 17:26 ` Martin KaFai Lau
@ 2022-07-21 12:30 ` patchwork-bot+netdevbpf
1 sibling, 0 replies; 5+ messages in thread
From: patchwork-bot+netdevbpf @ 2022-07-21 12:30 UTC (permalink / raw)
To: Dan Carpenter
Cc: andrii, yhs, ast, daniel, martin.lau, song, john.fastabend,
kpsingh, sdf, haoluo, jolsa, bpf, kernel-janitors
Hello:
This patch was applied to bpf/bpf-next.git (master)
by Daniel Borkmann <daniel@iogearbox.net>:
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")
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
>
> [...]
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!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2022-07-21 12:30 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-19 9:49 [PATCH] libbpf: fix sign expansion bug in btf_dump_get_enum_value() Dan Carpenter
2022-07-19 17:26 ` Martin KaFai Lau
2022-07-19 18:34 ` Dan Carpenter
2022-07-19 23:39 ` Martin KaFai Lau
2022-07-21 12:30 ` patchwork-bot+netdevbpf
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.