All of lore.kernel.org
 help / color / mirror / Atom feed
From: Yonghong Song <yonghong.song@linux.dev>
To: bot+bpf-ci@kernel.org, bpf@vger.kernel.org
Cc: ast@kernel.org, andrii@kernel.org, daniel@iogearbox.net,
	eddyz87@gmail.com, kernel-team@fb.com, martin.lau@kernel.org,
	mason@kernel.org, ihor.solodrai@linux.dev
Subject: Re: [PATCH bpf-next 1/3] libbpf: Poison an enum value relocation out of the local enumerator range
Date: Thu, 26 Nov 2026 11:35:34 -0800	[thread overview]
Message-ID: <9d2c8268-0ad4-4f39-80e9-ba9393d88f15@linux.dev> (raw)
In-Reply-To: <2c14a84ab2ca76b12c34e5be65c4e51a969151f0c831386519a47a9c6245671b@mail.kernel.org>



On 11/26/26 10:36 AM, bot+bpf-ci@kernel.org wrote:
>> libbpf: Poison an enum value relocation out of the local enumerator range
> TL;DR: Possible wrong behaviour in relo_core.c: enum value relocations may be
> wrongly poisoned when target BTF lacks the kflag (pre-6.0 kernels) and
> negative enumerators, so valid programs could fail to load.

Please take a look at commit 6036922777d3 or
    https://lore.kernel.org/all/20261124000514.2955878-1-yonghong.song@linux.dev/
We do assume that kflag 0 is taken as unsigned.

>
>> diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c
>> --- a/tools/lib/bpf/relo_core.c
>> +++ b/tools/lib/bpf/relo_core.c
> [ ... ]
>
>> +static bool bpf_core_enum_val_fits_int(__u64 val, bool val_signed)
>> +{
>> +	return val_signed ? (__s64)val == (__s32)val : val <= INT_MAX;
>> +}
> [ ... ]
>
>> +	local_t = btf_type_by_id(local_spec->btf, local_spec->spec[0].type_id);
>> +	targ_t = btf_type_by_id(targ_spec->btf, targ_spec->spec[0].type_id);
>> +	local_signed = BTF_INFO_KFLAG(local_t->info);
>> +	targ_signed = BTF_INFO_KFLAG(targ_t->info);
>> +
>> +	if (bpf_core_enum_fits_int(local_t)) {
>> +		type = "int";
>> +		fits = bpf_core_enum_val_fits_int(targ_val, targ_signed);
>> +	} else if (local_t->size == 4) {
>> +		type = "unsigned int";
>> +		/* a negative value is sign-extended, so it is also too big */
>> +		fits = targ_val <= UINT_MAX;
> Can this reject valid programs when the target BTF has no kflag on a signed
> enum?
>
> Before commit 6089fb325cf7 ("bpf: Add btf enum64 support"),
> btf_enum_check_meta() rejected any BTF_KIND_ENUM with kind_flag set, so
> vmlinux and module BTF from pre-6.0 kernels (and from kernels built with
> pahole older than 1.24) have kflag == 0 and store negative enumerators as
> their u32 bit pattern.
>
> For kflag == 0, bpf_core_enum_val() returns (__u32)e->val, so a target
> enumerator of -1 becomes targ_val = 0xffffffff with targ_signed = false.
>
> Consider a local enum such as enum perf_event_state from a modern
> vmlinux.h, where PERF_EVENT_STATE_OFF = -1, running on a 5.x kernel:
>
>      bpf_core_enum_fits_int(local_t)                 -> true
>      bpf_core_enum_val_fits_int(0xffffffff, false)   -> 0xffffffff <= INT_MAX
>                                                         -> false
>
> The relocation is poisoned and the program fails to load if the
> instruction is reachable.  Before this patch the ld_imm64 got 0xffffffff,
> which truncates to the correct -1 in an int context such as:
>
>      int x = bpf_core_enum_value(enum perf_event_state,
>                                  PERF_EVENT_STATE_OFF);
>
> The mirror case also looks affected.  If the local enum comes from a
> vmlinux.h generated on an old kernel, btf_dump prints the kflag == 0
> values with %u (e.g. PERF_EVENT_STATE_OFF = 4294967295), so the local
> enum is a 4-byte unsigned enum.  On a 6.0+ kernel with kflag set, targ_val
> is 0xffffffffffffffff, which fails "targ_val <= UINT_MAX" and is
> poisoned, where bpf_core_enum_value() previously returned the kernel's -1.
>
> The commit message says pahole sets the kflag for a signed enum since
> v1.24, "the minimum version now required for kernel BTF".  Doesn't that
> only cover kernels built from this tree, rather than the older running
> kernels that CO-RE targets?
>
> The selftests added later in the series appear to use only target BTF
> compiled by a modern clang that sets kflag, so they would not exercise
> this.
>
> Would it make sense to judge a 4-byte BTF_KIND_ENUM target without kflag
> by its low 32 bits, accepting the value if it fits as either int or
> unsigned int against a 32-bit local enumerator?
>
>
> ---
> AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
> See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
>
> CI run summary: https://github.com/kernel-patches/bpf/actions/runs/45873143557


  reply	other threads:[~2026-11-26 19:35 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26 17:56 [PATCH bpf-next 0/3] libbpf: Check enum value relocations against the local enumerator type Yonghong Song
2026-11-26 17:56 ` [PATCH bpf-next 1/3] libbpf: Poison an enum value relocation out of the local enumerator range Yonghong Song
2026-11-26 18:36   ` bot+bpf-ci
2026-11-26 19:35     ` Yonghong Song [this message]
2026-11-26 20:20   ` Alexei Starovoitov
2026-11-27  7:11     ` Yonghong Song
2026-11-26 17:56 ` [PATCH bpf-next 2/3] selftests/bpf: Test enum value relocations within " Yonghong Song
2026-11-26 18:00   ` sashiko-bot
2026-11-26 17:56 ` [PATCH bpf-next 3/3] selftests/bpf: Test enum value relocations out of " Yonghong Song
2026-11-26 18:02   ` sashiko-bot

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=9d2c8268-0ad4-4f39-80e9-ba9393d88f15@linux.dev \
    --to=yonghong.song@linux.dev \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bot+bpf-ci@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=kernel-team@fb.com \
    --cc=martin.lau@kernel.org \
    --cc=mason@kernel.org \
    /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.