BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next] libbpf: Fix ALU relocation check for a local value with bit 31 set
@ 2026-11-27 18:43 Yonghong Song
  2026-12-01  5:20 ` patchwork-bot+netdevbpf
  0 siblings, 1 reply; 2+ messages in thread
From: Yonghong Song @ 2026-11-27 18:43 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Eduard Zingerman, kernel-team

Since commit 776281652ddc ("libbpf: Permit 64bit relocation value"),
the local value of a relocation is a __u64, and libbpf checks it against
the imm of an ALU or ALU64 instruction with:

  if (res->validate && insn->imm != orig_val)

The __s32 imm is sign-extended to 64 bits for the comparison, so it does
not match a zero-extended local value with bit 31 set, e.g., 0x80000000,
and the relocation fails with "unexpected insn". Before that commit, both
sides were compared as 32-bit values.

The values patched into an imm so far, field offsets and sizes, bit
shifts and type sizes, are below 2^31, so this did not show up. With
the llvm change [1], clang loads the value of an unsigned int enumerator
of a preserve_access_index enum with a 32-bit mov:

  enum U { M = 0x80000000 } __attribute__((preserve_access_index));

  unsigned long f3(void) { return M; }

is compiled to:

  f3:
    w0 = 0x80000000      /* CO-RE <enumval_value> enum U::M */
    exit

Without this patch, libbpf rejects the relocation of such a mov with
"unexpected insn", so a program built with the llvm change fails to
load. Compare the imm with the low 32 bits of the local value, which is
what the imm holds. Any local value accepted before is still accepted,
since its low 32 bits are the imm. What is newly accepted is a value
whose upper 32 bits are not the sign extension of bit 31, e.g., an
unsigned int value with bit 31 set.

  [1] https://github.com/llvm/llvm-project/pull/246702

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

diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c
index 58c782cc60e8..b54227faf670 100644
--- a/tools/lib/bpf/relo_core.c
+++ b/tools/lib/bpf/relo_core.c
@@ -1067,7 +1067,7 @@ 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,
-- 
2.53.1-Meta


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-01  5:20 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 18:43 [PATCH bpf-next] libbpf: Fix ALU relocation check for a local value with bit 31 set Yonghong Song
2026-12-01  5:20 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox