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] libbpf: Fix ALU relocation check for a local value with bit 31 set
Date: Fri, 27 Nov 2026 10:43:34 -0800	[thread overview]
Message-ID: <20261127184334.860594-1-yonghong.song@linux.dev> (raw)

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


             reply	other threads:[~2026-11-27 18:43 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 18:43 Yonghong Song [this message]
2026-12-01  5:20 ` [PATCH bpf-next] libbpf: Fix ALU relocation check for a local value with bit 31 set patchwork-bot+netdevbpf

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=20261127184334.860594-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