* [PATCH bpf 1/2] bpf, arm64, insn: Reject out-of-range B.cond targets
@ 2026-04-15 12:14 Daniel Borkmann
2026-04-15 12:14 ` [PATCH bpf 2/2] bpf, arm64: Fix off-by-one in check_imm signed range check Daniel Borkmann
2026-04-15 19:20 ` [PATCH bpf 1/2] bpf, arm64, insn: Reject out-of-range B.cond targets patchwork-bot+netdevbpf
0 siblings, 2 replies; 3+ messages in thread
From: Daniel Borkmann @ 2026-04-15 12:14 UTC (permalink / raw)
To: bpf; +Cc: ast, puranjay, xukuohai
aarch64_insn_gen_cond_branch_imm() calls label_imm_common() to
compute a 19-bit signed byte offset for a conditional branch,
but unlike its siblings aarch64_insn_gen_branch_imm() and
aarch64_insn_gen_comp_branch_imm(), it does not check whether
label_imm_common() returned its out-of-range sentinel (range)
before feeding the value to aarch64_insn_encode_immediate().
aarch64_insn_encode_immediate() unconditionally masks the value
with the 19-bit field mask, so an offset that was rejected by
label_imm_common() gets silently truncated. With the sentinel
value SZ_1M, the resulting field ends up with bit 18 (the sign
bit of the 19-bit signed displacement) set, and the CPU decodes
it as a ~1 MiB *backward* branch, producing an incorrectly
targeted B.cond instruction. For code-gen locations like the
emit_bpf_tail_call() this function is the only barrier between
an overflowing displacement and a silently miscompiled branch.
Fix it by returning AARCH64_BREAK_FAULT when the offset is out
of range, so callers see a loud failure instead of a silently
misencoded branch. validate_code() scans the generated image
for any AARCH64_BREAK_FAULT and then lets the JIT fail.
Fixes: 345e0d35ecdd ("arm64: introduce aarch64_insn_gen_cond_branch_imm()")
Fixes: c94ae4f7c5ec ("arm64: insn: remove BUG_ON from codegen")
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Puranjay Mohan <puranjay@kernel.org>
---
arch/arm64/lib/insn.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/arch/arm64/lib/insn.c b/arch/arm64/lib/insn.c
index cc5b40917d0d..37ce75f7f1f0 100644
--- a/arch/arm64/lib/insn.c
+++ b/arch/arm64/lib/insn.c
@@ -338,6 +338,8 @@ u32 aarch64_insn_gen_cond_branch_imm(unsigned long pc, unsigned long addr,
long offset;
offset = label_imm_common(pc, addr, SZ_1M);
+ if (offset >= SZ_1M)
+ return AARCH64_BREAK_FAULT;
insn = aarch64_insn_get_bcond_value();
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* [PATCH bpf 2/2] bpf, arm64: Fix off-by-one in check_imm signed range check
2026-04-15 12:14 [PATCH bpf 1/2] bpf, arm64, insn: Reject out-of-range B.cond targets Daniel Borkmann
@ 2026-04-15 12:14 ` Daniel Borkmann
2026-04-15 19:20 ` [PATCH bpf 1/2] bpf, arm64, insn: Reject out-of-range B.cond targets patchwork-bot+netdevbpf
1 sibling, 0 replies; 3+ messages in thread
From: Daniel Borkmann @ 2026-04-15 12:14 UTC (permalink / raw)
To: bpf; +Cc: ast, puranjay, xukuohai
check_imm(bits, imm) is used in the arm64 BPF JIT to verify that
a branch displacement (in arm64 instruction units) fits into the
signed N-bit immediate field of a B, B.cond or CBZ/CBNZ encoding
before it is handed to the encoder. The macro currently tests for
(imm > 0 && imm >> bits) || (imm < 0 && ~imm >> bits) which admits
values in [-2^N, 2^N) — effectively a signed (N+1)-bit range. A
signed N-bit field only holds [-2^(N-1), 2^(N-1)), so the check
admits one extra bit of range on each side.
In particular, for check_imm19(), values in [2^18, 2^19) slip past
the check but do not fit into the 19-bit signed imm19 field of
B.cond. aarch64_insn_encode_immediate() then masks the raw value
into the 19-bit field, setting bit 18 (the sign bit) and flipping
a forward branch into a backward one. Same class of issue exists
for check_imm26() and the B/BL encoding. Shift by (bits - 1)
instead of bits so the actual signed N-bit range is enforced.
Fixes: e54bcde3d69d ("arm64: eBPF JIT compiler")
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Puranjay Mohan <puranjay@kernel.org>
---
arch/arm64/net/bpf_jit_comp.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c
index adf84962d579..4aad9483f8a5 100644
--- a/arch/arm64/net/bpf_jit_comp.c
+++ b/arch/arm64/net/bpf_jit_comp.c
@@ -35,8 +35,8 @@
#define ARENA_VM_START (MAX_BPF_JIT_REG + 5)
#define check_imm(bits, imm) do { \
- if ((((imm) > 0) && ((imm) >> (bits))) || \
- (((imm) < 0) && (~(imm) >> (bits)))) { \
+ if ((((imm) > 0) && ((imm) >> ((bits) - 1))) || \
+ (((imm) < 0) && (~(imm) >> ((bits) - 1)))) { \
pr_info("[%2d] imm=%d(0x%x) out of range\n", \
i, imm, imm); \
return -EINVAL; \
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH bpf 1/2] bpf, arm64, insn: Reject out-of-range B.cond targets
2026-04-15 12:14 [PATCH bpf 1/2] bpf, arm64, insn: Reject out-of-range B.cond targets Daniel Borkmann
2026-04-15 12:14 ` [PATCH bpf 2/2] bpf, arm64: Fix off-by-one in check_imm signed range check Daniel Borkmann
@ 2026-04-15 19:20 ` patchwork-bot+netdevbpf
1 sibling, 0 replies; 3+ messages in thread
From: patchwork-bot+netdevbpf @ 2026-04-15 19:20 UTC (permalink / raw)
To: Daniel Borkmann; +Cc: bpf, ast, puranjay, xukuohai
Hello:
This series was applied to bpf/bpf.git (master)
by Alexei Starovoitov <ast@kernel.org>:
On Wed, 15 Apr 2026 14:14:02 +0200 you wrote:
> aarch64_insn_gen_cond_branch_imm() calls label_imm_common() to
> compute a 19-bit signed byte offset for a conditional branch,
> but unlike its siblings aarch64_insn_gen_branch_imm() and
> aarch64_insn_gen_comp_branch_imm(), it does not check whether
> label_imm_common() returned its out-of-range sentinel (range)
> before feeding the value to aarch64_insn_encode_immediate().
>
> [...]
Here is the summary with links:
- [bpf,1/2] bpf, arm64, insn: Reject out-of-range B.cond targets
https://git.kernel.org/bpf/bpf/c/48d83d94930e
- [bpf,2/2] bpf, arm64: Fix off-by-one in check_imm signed range check
https://git.kernel.org/bpf/bpf/c/1dd8be4ec722
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-04-15 19:20 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-15 12:14 [PATCH bpf 1/2] bpf, arm64, insn: Reject out-of-range B.cond targets Daniel Borkmann
2026-04-15 12:14 ` [PATCH bpf 2/2] bpf, arm64: Fix off-by-one in check_imm signed range check Daniel Borkmann
2026-04-15 19:20 ` [PATCH bpf 1/2] bpf, arm64, insn: Reject out-of-range B.cond targets 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