From: Yonghong Song <yonghong.song@linux.dev>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>, bpf@vger.kernel.org
Cc: Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Eduard Zingerman <eddyz87@gmail.com>,
kernel-team@fb.com
Subject: Re: [PATCH bpf-next 1/3] libbpf: Poison an enum value relocation out of the local enumerator range
Date: Thu, 26 Nov 2026 23:11:40 -0800 [thread overview]
Message-ID: <bd3834d3-568e-4ab6-884d-c1ff39e3cb5c@linux.dev> (raw)
In-Reply-To: <DN5F6AJVK33K.25IJTI12XHRKN@gmail.com>
On 11/26/26 12:20 PM, Alexei Starovoitov wrote:
> On Thu, Nov 26, 2026 at 09:56 AM Yonghong Song <yonghong.song@linux.dev> wrote:
>> So a program is only rejected if the poisoned instruction is reachable.
>>
>> This also applies to bpf_core_enum_value(), since libbpf cannot tell the
>> two forms apart. A program like g() above, which could use a target
>> value out of the range of the local enumerator type before, is now
>> rejected if the instruction is reachable. Document this in
>> bpf_core_read.h.
> The usual guard doesn't make it unreachable.
> bpf_core_enum_value_exists() returns 1 here.
> I think this takes back what your commit 776281652ddc allowed.
> The local value is often a placeholder, like in profiler.inc.h:
>
> pids_cgrp_id___local = 123, /* value doesn't matter */
>
> What bpf_core_enum_value() gets today and with the patch:
>
> local enumerator target today patch
> --------------------------------- ----------- ---------- --------
> SCX_ENQ_PREEMPT___local = 0 1ULL << 32 1ULL << 32 poisoned
> PERF_EVENT_STATE_OFF = -1 -1, kflag 0 0xffffffff poisoned
> PERF_EVENT_STATE_OFF = 4294967295 -1, kflag 1 -1 poisoned
>
> Row 2 runs on v5.15. Before commit 4fad237494f1 it got -1.
> Row 3 has vmlinux.h of v5.15 and runs on bpf-next.
> The kernel builds relo_core.c too.
> A light skeleton with row 1 stops loading after a kernel upgrade.
Indeed, looks like based on local enumerator to imply the expected value range
is incorrect.
>
>> The target value is taken as signed or unsigned according to the kflag
>> of the target enum, which pahole sets for a signed enum since v1.24, the
>> minimum version now required for kernel BTF.
> That covers new kernels only.
> Before v6.0 btf_enum_check_meta() refuses an enum with kflag.
> Row 2 ends up in:
>
> val <= INT_MAX val = 0xffffffff, val_signed = 0
>
> Code like f2() gets -1 there today, since s>>= restores the sign.
> So the attribute form fails to load too, and its value is correct.
My current implementation assumes >= v6.0 kernel and it may indeed
reject some cases for < v6.0 kernel.
>
>> +/*
>> + * As in C, the enumerators of an enum have type int if all values of the enum
>> + * fit in int, even for a 1-byte enum made with the packed or mode(byte)
>> + * attribute, and otherwise the type of the enum, which is then 4 or 8 bytes.
>> + */
>> +static bool bpf_core_enum_fits_int(const struct btf_type *t)
> Not with a fixed underlying type. sizeof(N) is 8 with clang 21:
>
> enum E : unsigned long { N = 10 };
>
> This function says int, and 0x100000000 gets poisoned.
> BTF doesn't have the type of the enumerator. The compiler has it.
>
> can clang emit mov for 32-bit enumerators, like for field relos ?
>
> enumerator insn
> ------------ ---------------
> int r0 = 0xa
> unsigned int w0 = 0x80000000
> long r0 = 0xa ll
I remembered that we discussed this before during implementing
llvm commit c080a0b9b673e ([BPF] Support preserve_access_index attribute for enum (#232498)).
We briefly discussed whether we should do 'w0 = ...' or 'r0 = ...'
although eventually we used 'r0 = 0xa ll' style.
Looks like we do need to use 'r0/w0 = ...' style too. This will
gate the value in tight range to simplify libbpf.
>
> bpf_core_enum_value() keeps ld_imm64 and works as before.
> bpf_core_patch_insn() patches ALU imm already, without a range check.
> With mov the diff poisons all three examples from the log.
> Rows 2 and 3 load in both forms.
> The diff skips the sign of a long and fixed types of 1 and 2 bytes.
> No need for bpf_core_check_enumval_range(), bpf_core_enum_fits_int(),
> and for the shifts in f2() and f3().
>
> relo_core.c in place of patch 1, compiles for libbpf and kernel, not run:
>
> diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c
> index 58c782c..b2c8632 100644
> --- a/tools/lib/bpf/relo_core.c
> +++ b/tools/lib/bpf/relo_core.c
> @@ -1031,6 +1031,20 @@ static int insn_bytes_to_bpf_size(__u32 sz)
> }
> }
>
> +/*
> + * ALU zero-extends imm, ALU64 sign-extends it. BTF before v6.0 has no sign of
> + * an enum, so accept both forms when bit 31 of the local value is set.
> + */
> +static bool insn_imm_fits(const struct bpf_insn *insn, __u64 val)
> +{
> + bool zext = val == (__u32)val;
> + bool sext = (__s64)val == (__s32)val;
> +
> + if (insn->imm < 0)
> + return zext || sext;
> + return BPF_CLASS(insn->code) == BPF_ALU ? zext : sext;
> +}
> +
> /*
> * Patch relocatable BPF instruction.
> *
> @@ -1067,13 +1081,18 @@ int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn,
> goto bad_insn;
> if (res->poison)
> return bpf_core_poison_insn(prog_name, relo_idx, insn, insn_idx);
> - if (res->validate && insn->imm != orig_val) {
> + if (res->validate && insn->imm != (__s32)orig_val) {
> pr_warn("prog '%s': relo #%d: unexpected insn #%d (ALU/ALU64) value: got %d, exp %llu -> %llu\n",
> prog_name, relo_idx,
> insn_idx, insn->imm, (unsigned long long)orig_val,
> (unsigned long long)new_val);
> return -EINVAL;
> }
> + if (!insn_imm_fits(insn, new_val)) {
> + pr_warn("prog '%s': relo #%d: insn #%d (ALU/ALU64) value out of range: 0x%llx\n",
> + prog_name, relo_idx, insn_idx, (unsigned long long)new_val);
> + return bpf_core_poison_insn(prog_name, relo_idx, insn, insn_idx);
> + }
> orig_val = insn->imm;
> insn->imm = new_val;
> pr_debug("prog '%s': relo #%d: patched insn #%d (ALU/ALU64) imm %llu -> %llu\n",
I will work on llvm as soon as possible. The above diff looks correct
if llvm side with 'w0/r0 = ...' for relocation.
>
> pw-bot: cr
next prev parent reply other threads:[~2026-11-27 7:11 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
2026-11-26 20:20 ` Alexei Starovoitov
2026-11-27 7:11 ` Yonghong Song [this message]
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=bd3834d3-568e-4ab6-884d-c1ff39e3cb5c@linux.dev \
--to=yonghong.song@linux.dev \
--cc=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=kernel-team@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