BPF List
 help / color / mirror / Atom feed
From: Yonghong Song <yonghong.song@linux.dev>
To: bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Eduard Zingerman <eddyz87@gmail.com>,
	kernel-team@fb.com
Subject: [PATCH bpf-next v6 1/2] libbpf: Fix CO-RE relocations of unsigned 32-bit enum values
Date: Mon, 23 Nov 2026 16:05:19 -0800	[thread overview]
Message-ID: <20261124000519.2956216-1-yonghong.song@linux.dev> (raw)
In-Reply-To: <20261124000514.2955878-1-yonghong.song@linux.dev>

For a BPF_CORE_ENUMVAL_VALUE relocation, bpf_core_calc_enumval_relo()
reads the value of a 32-bit enumerator from btf_enum->val, which is
__s32, so the value is sign-extended to 64 bits even if the enum is
unsigned. A value in [0x80000000, 0xffffffff] of an unsigned enum
becomes 0xffffffff80000000 or above.

Clang emits such a value zero-extended in the ld_imm64 of the
relocation, so the check of the original value fails, and the whole
object is refused even if the target value is the same:

  enum dentry_flags { DCACHE_PRIVATE = 0x80000000 };
  out = bpf_core_enum_value(enum dentry_flags, DCACHE_PRIVATE);

  libbpf: prog 'p': relo #0: unexpected insn #0 (LDIMM64) value: got
          2147483648, exp 18446744071562067968 -> 18446744071562067968
  libbpf: prog 'p': relo #0: failed to patch insn #0: -EINVAL

And a target value in that range is patched sign-extended, which is
wrong wherever the program uses it as a 64-bit value. The x86 vmlinux
has 29 enums with such values, e.g., dentry_flags, init_ns_ino and
e820_type.

Commit 89e7a42e3030 ("libbpf: Normalize enum values according to
signedness") fixed this for btf_dump and map fields, but not for CO-RE
relocations, whose relo_core.c is also built into the kernel. Extend a
32-bit enum value according to the kflag of its enum in
bpf_core_calc_enumval_relo(), i.e., sign-extend it if the kflag is 1,
and zero-extend it otherwise, for both the local and the target value.

The kflag 0 is taken as unsigned. pahole records the signedness since
v1.24. The BTF of a kernel before v6.0 has the kflag 0 on every enum,
though, so a negative target value there is now patched zero-extended.

GCC sign-extends every 32-bit enum value in the ld_imm64, since its
__builtin_preserve_enum_value() returns int, so a GCC object with an
unsigned enum value in [0x80000000, 0xffffffff] is now refused. That
needs to be fixed in GCC, whose builtin should return long, as clang's
does.

Fixes: 776281652ddc ("libbpf: Permit 64bit relocation value")
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
 tools/lib/bpf/relo_core.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c
index 2672623a4198..58c782cc60e8 100644
--- a/tools/lib/bpf/relo_core.c
+++ b/tools/lib/bpf/relo_core.c
@@ -866,6 +866,7 @@ static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,
 				      __u64 *val)
 {
 	const struct btf_type *t;
+	const struct btf_enum *e;
 
 	switch (relo->kind) {
 	case BPF_CORE_ENUMVAL_EXISTS:
@@ -875,10 +876,12 @@ static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,
 		if (!spec)
 			return -EUCLEAN; /* request instruction poisoning */
 		t = btf_type_by_id(spec->btf, spec->spec[0].type_id);
-		if (btf_is_enum(t))
-			*val = btf_enum(t)[spec->spec[0].idx].val;
-		else
+		if (btf_is_enum(t)) {
+			e = btf_enum(t) + spec->spec[0].idx;
+			*val = BTF_INFO_KFLAG(t->info) ? (__s64)e->val : (__u32)e->val;
+		} else {
 			*val = btf_enum64_value(btf_enum64(t) + spec->spec[0].idx);
+		}
 		break;
 	default:
 		return -EOPNOTSUPP;
-- 
2.53.1-Meta


  reply	other threads:[~2026-11-24  0:05 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24  0:05 [PATCH bpf-next v6 0/2] libbpf: Fix CO-RE relocations of unsigned 32-bit enum values Yonghong Song
2026-11-24  0:05 ` Yonghong Song [this message]
2026-11-24  0:05 ` [PATCH bpf-next v6 2/2] selftests/bpf: Test " Yonghong Song

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=20261124000519.2956216-1-yonghong.song@linux.dev \
    --to=yonghong.song@linux.dev \
    --cc=andrii@kernel.org \
    --cc=ast@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