Skip to contentSkip to conversation

Reject invalid LDSX instruction in disassembler

PATCH Applied Kumar Kartikeya Dwivedi 3 participants 5 messages Aug 20

Subsystems (4)
lore
Share & tools
lore

Kumar Kartikeya Dwivedi

author maintainer
#0 lore raw

cover Reject invalid LDSX instruction in disassembler

Reject invalid LDSX insn encoding in print_bpf_insn. This was reported by syzbot in [0]. See commit logs for details.

[0]: https://lore.kernel.org/bpf/[email protected]

Kumar Kartikeya Dwivedi (2):
bpf: Reject invalid LDSX instruction in disassembly selftests/bpf: Test invalid DW LDSX diagnostics

 kernel/bpf/disasm.c                              |  3 ++-
 tools/testing/selftests/bpf/progs/verifier_cfg.c | 14 ++++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

base-commit: f79066c784022fda83f5936559a1af414e41b603

Signature · 1 line
2.53.0
author maintainer
#1 lore raw

PATCH 1/2 bpf: Reject invalid LDSX instruction in disassembly

The signed-load mnemonic table has entries for byte, half-word, and word loads because BPF_MEMSX does not support double-word loads. A BPF_MEMSX | BPF_DW instruction nevertheless selects index 3, past the end of this table.

Program Structure diagnostics can disassemble a malformed instruction before check_and_resolve_insns() rejects its opcode. Placing the invalid signed double-word load at the end of a program therefore triggers an out-of-bounds access while reporting subprogram fallthrough.

Treat signed double-word loads as invalid in the disassembler and use the existing BUG_ldx fallback instead.

Fixes a8f427835394 ("bpf: Report Program Structure CFG errors")
Reported-by [email protected]
---
 kernel/bpf/disasm.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/bpf/disasm.c +2 -1
index 50b3ca5149a0..b1a3fbe3fda5 100644
@@ -295,7 +295,8 @@ void print_bpf_insn(const struct bpf_insn_cbs *cbs,
 			verbose(cbs->private_data, "BUG_st_%02x", insn->code);
 		}
 	} else if (class == BPF_LDX) {
-		if (BPF_MODE(insn->code) != BPF_MEM && BPF_MODE(insn->code) != BPF_MEMSX) {
+		if ((BPF_MODE(insn->code) != BPF_MEM && BPF_MODE(insn->code) != BPF_MEMSX) ||
+		    (BPF_MODE(insn->code) == BPF_MEMSX && BPF_SIZE(insn->code) == BPF_DW)) {
 			verbose(cbs->private_data, "BUG_ldx_%02x", insn->code);
 			return;
 		}
Signature · 1 line
2.53.0
author maintainer
#3 lore raw

PATCH 2/2 selftests/bpf: Test invalid DW LDSX diagnostics

In reply to Kumar Kartikeya Dwivedi

An invalid BPF_MEMSX | BPF_DW instruction can reach Program Structure diagnostics before opcode validation when placed at the end of a subprogram. Exercise this path and require the disassembler fallback so table bounds regressions are caught.

---
 tools/testing/selftests/bpf/progs/verifier_cfg.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
tools/testing/selftests/bpf/progs/verifier_cfg.c +14 -0
index c1f55e1d80a4..3c3bb03e8217 100644
@@ -3,6 +3,7 @@
 
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
+#include "../../../include/linux/filter.h"
 #include "bpf_misc.h"
 
 SEC("socket")
@@ -55,6 +56,19 @@ __naked void out_of_range_jump2(void)
 "	::: __clobber_all);
 }
 
+SEC("socket")
+__description("invalid DW LDSX instruction in diagnostics")
+__failure __msg("BUG_ldx_99")
+__log_level(2)
+__naked void invalid_dw_ldsx(void)
+{
+	asm volatile ("					\
+	.8byte %[ldsx_dw];				\
+"	:
+	: __imm_insn(ldsx_dw, BPF_RAW_INSN(BPF_LDX | BPF_MEMSX | BPF_DW, BPF_REG_0, BPF_REG_0, 0, 0))
+	: __clobber_all);
+}
+
 SEC("socket")
 __description("loop (back-edge)")
 __failure __msg("unreachable insn 1")
Signature · 1 line
2.53.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH bpf-next v1 1/2] bpf: Reject invalid LDSX instruction in disassembly

In reply to Kumar Kartikeya Dwivedi
Quoted 10 lines · Kumar Kartikeya Dwivedi

The signed-load mnemonic table has entries for byte, half-word, and word loads because BPF_MEMSX does not support double-word loads. A BPF_MEMSX | BPF_DW instruction nevertheless selects index 3, past the end of this table.

Program Structure diagnostics can disassemble a malformed instruction before check_and_resolve_insns() rejects its opcode. Placing the invalid signed double-word load at the end of a program therefore triggers an out-of-bounds access while reporting subprogram fallthrough.

Quoted 6 lines

Treat signed double-word loads as invalid in the disassembler and use the existing BUG_ldx fallback instead.

Fixes a8f427835394 ("bpf: Report Program Structure CFG errors")
Reported-by [email protected]

Sign-extending a 64-bit load is truly meaningless for printing

and this opcode is already treated as invalid in bpf_opcode_in_insntable()

Quoted 18 lines
---
  kernel/bpf/disasm.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
kernel/bpf/disasm.c +2 -1
index 50b3ca5149a0..b1a3fbe3fda5 100644
@@ -295,7 +295,8 @@ void print_bpf_insn(const struct bpf_insn_cbs *cbs,
  			verbose(cbs->private_data, "BUG_st_%02x", insn->code);
  		}
  	} else if (class == BPF_LDX) {
-		if (BPF_MODE(insn->code) != BPF_MEM && BPF_MODE(insn->code) != BPF_MEMSX) {
+		if ((BPF_MODE(insn->code) != BPF_MEM && BPF_MODE(insn->code) != BPF_MEMSX) ||
+		    (BPF_MODE(insn->code) == BPF_MEMSX && BPF_SIZE(insn->code) == BPF_DW)) {
  			verbose(cbs->private_data, "BUG_ldx_%02x", insn->code);
  			return;
  		}
#4 lore raw
In reply to Kumar Kartikeya Dwivedi
This series was applied to bpf/bpf.git (master) by Daniel Borkmann <[email protected]>:

Hello:

This series was applied to bpf/bpf.git (master)
by Daniel Borkmann <[email protected]>:

Quoted 10 lines · On Thu, 20 Aug 2026 04:20:17 +0200 you wrote:

Reject invalid LDSX insn encoding in print_bpf_insn. This was reported by syzbot in [0]. See commit logs for details.

[0]: https://lore.kernel.org/bpf/[email protected]

Kumar Kartikeya Dwivedi (2):
bpf: Reject invalid LDSX instruction in disassembly selftests/bpf: Test invalid DW LDSX diagnostics

[...]

Here is the summary with links:
- [bpf-next,v1,1/2] bpf: Reject invalid LDSX instruction in disassembly

    https://git.kernel.org/bpf/bpf/c/37e5c4f4d285
  - [bpf-next,v1,2/2] selftests/bpf: Test invalid DW LDSX diagnostics
    https://git.kernel.org/bpf/bpf/c/175a58668e2d

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html