BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>, bpf@vger.kernel.org
Subject: Re: [PATCH bpf 23/47] bpf: Fix reads past an enum in prints of BTF data
Date: Wed, 25 Nov 2026 03:52:03 +0000	[thread overview]
Message-ID: <sashiko-outbox-232521@kernel.org> (raw)
In-Reply-To: <20261125032938.670542-24-alexei.starovoitov@gmail.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] bpf: truncation of 8-byte enum values to 32-bit int in
  btf_enum_show()
- [Medium] bpf: hardcoded 32-bit bitfield limit in
  btf_enum_check_kflag_member()
--

commit c14b9d6d46e8fb4261828cf2cb022187e0695c07
Author: Alexei Starovoitov <ast@kernel.org>

bpf: Fix reads past an enum in prints of BTF data

This commit changes BTF enum printing and struct member size validation to
use the actual size of the enum type instead of assuming 4 or 8 bytes.

> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> index 8c934aa8ad609..7b3291556e6ee 100644
> --- a/kernel/bpf/btf.c
> +++ b/kernel/bpf/btf.c
> @@ -4579,7 +4579,7 @@ static int btf_enum_check_kflag_member(struct btf_verifier_env *env,
>  			return -EINVAL;
>  		}
>  
> -		nr_bits = int_bitsize;
> +		nr_bits = member_type->size * BITS_PER_BYTE;
>  	} else if (nr_bits > int_bitsize) {
>  		btf_verifier_log_member(env, struct_type, member,
>  					"Invalid member bitfield_size");

[Severity: Medium]
Should int_bitsize also be computed from member_type->size * BITS_PER_BYTE
for the bitfield check?

Leaving int_bitsize hardcoded to sizeof(int) * BITS_PER_BYTE causes
btf_enum_check_kflag_member() to reject valid 33..64-bit bitfields of
8-byte BTF_KIND_ENUM and BTF_KIND_ENUM64 types, while accepting 1-byte or
2-byte enum bitfields with nr_bits up to 32.  When __btf_struct_show()
formats such an oversized bitfield on a 1-byte or 2-byte enum,
btf_show_start_type() only ensures member_type->size bytes remain in
show->obj.safe[], whereas btf_bitfield_show() copies
BITS_ROUNDUP_BYTES(bitfield_size + bits8_offset) bytes and can read past
the end of show->obj.safe[].

> @@ -4662,12 +4662,30 @@ static void btf_enum_log(struct btf_verifier_env *env,
>  	btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
>  }
>  
> +static s64 btf_enum_read(const struct btf_type *t, const void *data)
> +{
> +	bool sign = btf_type_kflag(t);
> +
> +	switch (t->size) {
> +	case 1:
> +		return sign ? *(s8 *)data : *(u8 *)data;
> +	case 2:
> +		return sign ? *(s16 *)data : *(u16 *)data;
> +	case 4:
> +		return sign ? (s64)*(s32 *)data : *(u32 *)data;
> +	default:
> +		return *(s64 *)data;
> +	}
> +}
> +
>  static void btf_enum_show(const struct btf *btf, const struct btf_type *t,
>  			  u32 type_id, void *data, u8 bits_offset,
>  			  struct btf_show *show)
>  {
>  	const struct btf_enum *enums = btf_type_enum(t);
>  	u32 i, nr_enums = btf_type_vlen(t);
> +	/* Old compilers don't set the flag for negative values */
> +	u32 mask = t->size < 4 ? BIT(t->size * BITS_PER_BYTE) - 1 : U32_MAX;
>  	void *safe_data;
>  	int v;
>  
> @@ -4675,10 +4693,10 @@ static void btf_enum_show(const struct btf *btf, const struct btf_type *t,
>  	if (!safe_data)
>  		return;
>  
> -	v = *(int *)safe_data;
> +	v = btf_enum_read(t, safe_data);
>  
>  	for (i = 0; i < nr_enums; i++) {
> -		if (v != enums[i].val)
> +		if ((v ^ enums[i].val) & mask)
>  			continue;
>  
>  		btf_show_type_value(show, "%s",

[Severity: Medium]
Does storing the s64 return value of btf_enum_read() into int v (and
masking with u32 mask) truncate 8-byte BTF_KIND_ENUM values in
btf_enum_show()?

When t->size is 8, btf_enum_read() reads 64 bits via its default branch,
but assigning the result to 32-bit int v discards the upper 32 bits.  An
8-byte BTF_KIND_ENUM value with non-zero upper bits will falsely match an
enumerator whose 32-bit value matches the lower 32 bits, or print a
truncated 32-bit integer when unmatched.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261125032938.670542-1-alexei.starovoitov@gmail.com?part=23

  reply	other threads:[~2026-11-25  3:52 UTC|newest]

Thread overview: 61+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25  3:28 [PATCH bpf 00/47] bpf: Fixes for hooks, tail calls, CAP_BPF, JITs and sockmap Alexei Starovoitov
2026-11-25  3:28 ` [PATCH bpf 01/47] bpf: Do not trust pointers in flexible arrays Alexei Starovoitov
2026-11-25  3:28 ` [PATCH bpf 02/47] bpf: Look at the type of a program next to expected_attach_type Alexei Starovoitov
2026-11-25  3:28 ` [PATCH bpf 03/47] bpf: Do not take bpf_get_retval() of struct bpf_tcp_ops for an errno Alexei Starovoitov
2026-11-25  3:28 ` [PATCH bpf 04/47] bpf: Do not allow bpf_d_path() by an id of another BTF Alexei Starovoitov
2026-11-25  3:28 ` [PATCH bpf 05/47] bpf: Check that an extension fits the hook of its target Alexei Starovoitov
2026-11-25  3:28 ` [PATCH bpf 06/47] bpf: Take the attach type of the target for tail calls of an extension Alexei Starovoitov
2026-11-25  3:28 ` [PATCH bpf 07/47] bpf: Compare what a hook tells next to its prototype at tail calls Alexei Starovoitov
2026-11-25  3:58   ` sashiko-bot
2026-11-25  3:28 ` [PATCH bpf 08/47] bpf: Reserve cookie and ip in the trampoline for " Alexei Starovoitov
2026-11-25  3:29 ` [PATCH bpf 09/47] bpf: Check that a program and a struct_ops map are for the same struct Alexei Starovoitov
2026-11-25  3:29 ` [PATCH bpf 10/47] bpf: Keep the names in front of a number in delegate options Alexei Starovoitov
2026-11-25  3:29 ` [PATCH bpf 11/47] bpf: Do not trust the neighbour of tp_btf/neigh_create Alexei Starovoitov
2026-11-25  3:29 ` [PATCH bpf 12/47] bpf: Check for NULL in helpers that take a kernel pointer Alexei Starovoitov
2026-11-25  3:48   ` sashiko-bot
2026-11-25  3:29 ` [PATCH bpf 13/47] bpf: Leave nothing behind in the frame of bpf_throw() Alexei Starovoitov
2026-11-25  3:29 ` [PATCH bpf 14/47] bpf: Add the symbol of a prog before its id Alexei Starovoitov
2026-11-25  3:29 ` [PATCH bpf 15/47] bpf: Do not fill jited line info for a hidden subprog Alexei Starovoitov
2026-11-25  3:29 ` [PATCH bpf 16/47] bpf: Fix NULL dereference in XDP redirect from async callbacks Alexei Starovoitov
2026-11-25  3:29 ` [PATCH bpf 17/47] bpf: Check the attach type when a cgroup link gets another program Alexei Starovoitov
2026-11-25  3:29 ` [PATCH bpf 18/47] bpf: Refuse metadata maps of signed programs that can be mapped writable Alexei Starovoitov
2026-11-25  3:29 ` [PATCH bpf 19/47] bpf: Take bpf_lock with interrupts disabled Alexei Starovoitov
2026-11-25  4:08   ` bot+bpf-ci
2026-11-25  3:29 ` [PATCH bpf 20/47] bpf: Bound the time that dumps of the stack take Alexei Starovoitov
2026-11-25  3:29 ` [PATCH bpf 21/47] bpf: Refuse mmap() of a part of an arena Alexei Starovoitov
2026-11-25  3:29 ` [PATCH bpf 22/47] bpf: Flush TLB when a page of arena replaces the scratch page Alexei Starovoitov
2026-11-25  3:48   ` sashiko-bot
2026-11-25  3:29 ` [PATCH bpf 23/47] bpf: Fix reads past an enum in prints of BTF data Alexei Starovoitov
2026-11-25  3:52   ` sashiko-bot [this message]
2026-11-25  3:29 ` [PATCH bpf 24/47] bpf: Give the error of bpf_res_spin_lock() a 32-bit range Alexei Starovoitov
2026-11-25  3:29 ` [PATCH bpf 25/47] bpf: Give syscall programs without CAP_PERFMON the base helpers only Alexei Starovoitov
2026-11-25  3:55   ` sashiko-bot
2026-11-25  4:08   ` bot+bpf-ci
2026-11-25  3:29 ` [PATCH bpf 26/47] bpf: Require CAP_PERFMON for kfuncs that leak pointers or touch tasks Alexei Starovoitov
2026-11-25  4:08   ` bot+bpf-ci
2026-11-25  3:29 ` [PATCH bpf 27/47] bpf: Require CAP_PERFMON to read streams Alexei Starovoitov
2026-11-25  3:29 ` [PATCH bpf 28/47] bpf: Limit reads of variables of the kernel Alexei Starovoitov
2026-11-25  4:08   ` bot+bpf-ci
2026-11-25  3:29 ` [PATCH bpf 29/47] bpf: Require CAP_PERFMON for dumps of xlated insns Alexei Starovoitov
2026-11-25  3:29 ` [PATCH bpf 30/47] bpf: Zero bpf_list_head in copies of map values Alexei Starovoitov
2026-11-25  3:29 ` [PATCH bpf 31/47] bpf: Require CAP_PERFMON to read pinned maps with special fields Alexei Starovoitov
2026-11-25  3:29 ` [PATCH bpf 32/47] bpf: Require CAP_PERFMON to pass values of insn_array as memory Alexei Starovoitov
2026-11-25  3:29 ` [PATCH bpf 33/47] bpf: Require CAP_PERFMON for the dump of all storages of sockets Alexei Starovoitov
2026-11-25  3:29 ` [PATCH bpf 34/47] bpf, x86: Do not use the paranoid ITS thunk Alexei Starovoitov
2026-11-25  3:29 ` [PATCH bpf 35/47] bpf, x86: Enforce alignment of atomics in arenas Alexei Starovoitov
2026-11-25  3:29 ` [PATCH bpf 36/47] bpf, x86: Fix the range of BPF code that has a frame pointer Alexei Starovoitov
2026-11-25  3:29 ` [PATCH bpf 37/47] bpf, arm64: Restore the shadow call stack pointer after an exception Alexei Starovoitov
2026-11-25  3:29 ` [PATCH bpf 38/47] bpf: Clear the data that bpf_msg_push_data() adds Alexei Starovoitov
2026-11-25  3:29 ` [PATCH bpf 39/47] bpf: Fail loads from a network header that was not set Alexei Starovoitov
2026-11-25  3:29 ` [PATCH bpf 40/47] bpf: Fix slices behind the end of an XDP packet Alexei Starovoitov
2026-11-25  3:29 ` [PATCH bpf 41/47] bpf: Clear family and port of a short address for cgroup sockaddr hooks Alexei Starovoitov
2026-11-25  3:29 ` [PATCH bpf 42/47] bpf: Take a neighbour of the L3 master in bpf_redirect_neigh() Alexei Starovoitov
2026-11-25  4:08   ` bot+bpf-ci
2026-11-25  4:09   ` sashiko-bot
2026-11-25  3:29 ` [PATCH bpf 43/47] bpf: Keep the MAC header in bpf_skb_change_tail() Alexei Starovoitov
2026-11-25  3:29 ` [PATCH bpf 44/47] bpf, sockmap: Keep the parser and sk_data_ready in sync Alexei Starovoitov
2026-11-25  4:09   ` sashiko-bot
2026-11-25  3:29 ` [PATCH bpf 45/47] bpf, sockmap: Hold the psock of the work in sk_psock_backlog() Alexei Starovoitov
2026-11-25  3:29 ` [PATCH bpf 46/47] libbpf: Fix struct_ops members 59-61 and optional struct_ops maps Alexei Starovoitov
2026-11-25  3:29 ` [PATCH bpf 47/47] bpf: Check the type of R0 at the exit of a struct_ops program Alexei Starovoitov
2026-11-25  4:08   ` bot+bpf-ci

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=sashiko-outbox-232521@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=alexei.starovoitov@gmail.com \
    --cc=bpf@vger.kernel.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox