All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: liujing <liujing@cmss.chinamobile.com>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH] tools libbpf: Fix signed shift UB in btf_type_info()
Date: Thu, 03 Sep 2026 08:34:04 +0000	[thread overview]
Message-ID: <20260903083404.9C72F1F00A3A@smtp.kernel.org> (raw)
In-Reply-To: <20260903082616.5096-1-liujing@cmss.chinamobile.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] The commit provides an incomplete fix: it resolves the signed shift undefined behavior in the `btf_type_info()` function but misses the exact same undefined behavior in the `BTF_INFO_ENC()` macro defined in the same file (and duplicated in selftests).
--

commit d2829a28ec022a655878147a1269bec0aaf774db
Author: Liu Jing <liujing@cmss.chinamobile.com>

tools libbpf: Fix signed shift UB in btf_type_info()

This commit fixes a signed shift undefined behavior in btf_type_info() by
casting the kflag parameter to an unsigned int before left-shifting by 31
bits.

> diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h
> index 4c46d34fc055d..2fda8c916cc35 100644
> --- a/tools/lib/bpf/libbpf_internal.h
> +++ b/tools/lib/bpf/libbpf_internal.h
> @@ -259,7 +259,7 @@ static inline enum btf_func_linkage btf_func_linkage(const struct btf_type *t)
>  
>  static inline __u32 btf_type_info(int kind, int vlen, int kflag)
>  {
> -	return (kflag << 31) | (kind << 24) | vlen;
> +	return (((unsigned int)kflag << 31)) | (kind << 24) | vlen;
>  }

[Severity: High]
This isn't a bug introduced by this patch, but while fixing the signed
shift undefined behavior in btf_type_info(), it looks like the exact
same issue was missed in the BTF_INFO_ENC() macro defined in this same
file.

The BTF_INFO_ENC() macro evaluates !!(kind_flag) << 31:

#define BTF_INFO_ENC(kind, kind_flag, vlen) \
    ((!!(kind_flag) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN))

Because !!(kind_flag) yields a signed 32-bit int, shifting it left by 31
bits results in 1 << 31 when kind_flag is non-zero (e.g., when encoding
BTF_KIND_DECL_TAG). Doesn't this also trigger the same undefined behavior
that compilers could exploit to generate corrupted BTF data?

Additionally, it looks like this same macro is duplicated in the selftests
under tools/testing/selftests/bpf/test_btf.h and
tools/testing/selftests/bpf/progs/syscall.c. Should those be updated as
well to ensure a complete fix across both libbpf and the selftests?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260903082616.5096-1-liujing@cmss.chinamobile.com?part=1

  reply	other threads:[~2026-09-03  8:34 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-03  8:26 [PATCH] tools libbpf: Fix signed shift UB in btf_type_info() liujing
2026-09-03  8:34 ` sashiko-bot [this message]
2026-09-03  9:05 ` bot+bpf-ci
2026-09-11 17:29 ` Andrii Nakryiko

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=20260903083404.9C72F1F00A3A@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=liujing@cmss.chinamobile.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 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.