BPF List
 help / color / mirror / Atom feed
From: Dan Carpenter <dan.carpenter@oracle.com>
To: Martin KaFai Lau <kafai@fb.com>
Cc: Andrii Nakryiko <andrii@kernel.org>, Yonghong Song <yhs@fb.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Song Liu <song@kernel.org>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>,
	Stanislav Fomichev <sdf@google.com>, Hao Luo <haoluo@google.com>,
	Jiri Olsa <jolsa@kernel.org>,
	bpf@vger.kernel.org, kernel-janitors@vger.kernel.org
Subject: Re: [PATCH] libbpf: fix sign expansion bug in btf_dump_get_enum_value()
Date: Tue, 19 Jul 2022 21:34:13 +0300	[thread overview]
Message-ID: <20220719183413.GG2338@kadam> (raw)
In-Reply-To: <20220719172640.pfbsfhdgmzn76kos@kafai-mbp.dhcp.thefacebook.com>

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


  reply	other threads:[~2022-07-19 18:35 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2022-07-19 23:39     ` Martin KaFai Lau
2022-07-21 12:30 ` patchwork-bot+netdevbpf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220719183413.GG2338@kadam \
    --to=dan.carpenter@oracle.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kafai@fb.com \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=sdf@google.com \
    --cc=song@kernel.org \
    --cc=yhs@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox