bpf: disasm: guard print_bpf_insn against BPF_MEMSX | BPF_DW
PATCH 1/1 bpf: disasm: guard print_bpf_insn against BPF_MEMSX | BPF_DW
print_bpf_insn() renders a BPF_LDX by indexing one of two size tables with BPF_SIZE(insn->code) >> 3. For BPF_MEMSX it uses bpf_ldsx_string[], which has only three entries (W/H/B) because a sign-extended doubleword load is not a valid instruction. The LDX branch checks only that the mode is BPF_MEM or BPF_MEMSX; it never rejects BPF_MEMSX | BPF_DW. For that opcode BPF_SIZE(code) >> 3 == BPF_DW >> 3 == 3, one element past the end of the 3-entry array:
UBSAN: array-index-out-of-bounds: index 3 out of range for 'char *[3]' KASAN: global-out-of-bounds: 8-byte read in print_bpf_insn
The out-of-bounds slot is then dereferenced as %s.
The disassembler is expected to run on unvalidated instructions. Since commit b9c5d822f677 ("bpf: Add source and instruction diagnostic context") the diagnostics facility disassembles instruction context from check_subprogs(), before check_insn_fields() has rejected the opcode, so a raw BPF_MEMSX | BPF_DW insn reaches print_bpf_insn on the ordinary bpf_prog_load() path whenever a verifier log is requested (log_level >= 1).
Reject the impossible size the same way the branch already rejects an unexpected mode, keeping the disassembler total for any opcode.
--- kernel/bpf/disasm.c | 4 ++++ 1 file changed, 4 insertions(+)
kernel/bpf/disasm.c +4 -0
@@ -299,6 +299,10 @@ void print_bpf_insn(const struct bpf_insn_cbs *cbs,verbose (cbs->private_data,"BUG_ldx_%02x" , insn->code);return ; } +if (BPF_MODE (insn->code) ==BPF_MEMSX &&BPF_SIZE (insn->code) ==BPF_DW ) { +verbose (cbs->private_data,"BUG_ldsx_%02x" , insn->code); +return ; + } verbose(cbs->private_data, "(%02x) r%d = *(%s *)(r%d %+d)", insn->code, insn->dst_reg, BPF_MODE(insn->code) == BPF_MEM ?
Signature · 1 line
2.34.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The patch adds a redundant check for `BPF_MEMSX` with `BPF_DW` size that is unreachable dead…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The patch adds a redundant check for BPF_MEMSX with BPF_DW size that is unreachable dead code, because the preceding if statement already checks the exact same condition and returns. --
commit ae7a2255ead6c3e59e5776083e563c01d78b10e7
Author: Utku Erol <[email protected]>
bpf: disasm: guard print_bpf_insn against BPF_MEMSX | BPF_DW
This patch adds a check in print_bpf_insn() to reject BPF_MEMSX instructions with BPF_DW size. It intends to prevent an out-of-bounds array access that occurs on unvalidated instructions during disassembly.
[ ... ]
Quoted 2 lines
The LDX branch checks only that the mode is
BPF_MEMorBPF_MEMSX; it never rejectsBPF_MEMSX|BPF_DW. For that opcode
[Severity: Medium]
Is this description accurate?
Looking at print_bpf_insn(), the preceding check in the baseline code already explicitly evaluates and rejects the (BPF_MEMSX | BPF_DW) condition.
[ ... ]
kernel/bpf/disasm.c void print_bpf_insn(const struct bpf_insn_cbs *cbs, · line 303
Quoted 9 lines
kernel/bpf/disasm.c +1
-0@@ -300,6 +300,10 @@ void print_bpf_insn(const struct bpf_insn_cbs *cbs,verbose (cbs->private_data,"BUG_ldx_%02x" , insn->code);return ; } +if (BPF_MODE (insn->code) ==BPF_MEMSX &&BPF_SIZE (insn->code) ==BPF_DW ) {
[Severity: Medium]
Does this code ever execute?
The immediately preceding if statement checks the exact same condition:
} else if (class == BPF_LDX) {
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;
}Since the previous check unconditionally returns, won't this newly added block be completely unreachable dead code?
Quoted 6 lines
kernel/bpf/disasm.c +3
-0+verbose (cbs->private_data,"BUG_ldsx_%02x" , insn->code); +return ; + } verbose(cbs->private_data, "(%02x) r%d = *(%s *)(r%d %+d)", insn->code, insn->dst_reg, BPF_MODE(insn->code) == BPF_MEM ?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Kumar Kartikeya Dwivedi
maintainerQuoted 34 lines · Utku Erol
print_bpf_insn()renders aBPF_LDXby indexing one of two size tables withBPF_SIZE(insn->code)>> 3. ForBPF_MEMSXit uses bpf_ldsx_string[], which has only three entries (W/H/B) because a sign-extended doubleword load is not a valid instruction. The LDX branch checks only that the mode isBPF_MEMorBPF_MEMSX; it never rejectsBPF_MEMSX|BPF_DW. For that opcodeBPF_SIZE(code)>> 3 ==BPF_DW>> 3 == 3, one element past the end of the 3-entry array:UBSAN: array-index-out-of-bounds: index 3 out of range for 'char *[3]' KASAN: global-out-of-bounds: 8-byte read in print_bpf_insnThe out-of-bounds slot is then dereferenced as %s.
The disassembler is expected to run on unvalidated instructions. Since commit
b9c5d822f677("bpf: Add source and instruction diagnostic context") the diagnostics facility disassembles instruction context fromcheck_subprogs(), beforecheck_insn_fields()has rejected the opcode, so a rawBPF_MEMSX|BPF_DWinsn reachesprint_bpf_insnon the ordinarybpf_prog_load()path whenever a verifier log is requested (log_level>= 1).Reject the impossible size the same way the branch already rejects an unexpected mode, keeping the disassembler total for any opcode.
Fixes b9c5d822f677 ("bpf: Add source and instruction diagnostic context")Signed-off-by Utku Erol <[email protected]>--- kernel/bpf/disasm.c | 4 ++++ 1 file changed, 4 insertions(+)kernel/bpf/disasm.c +0
-0
kernel/bpf/disasm.c void print_bpf_insn(const struct bpf_insn_cbs *cbs, · line 305
Quoted 7 lines
kernel/bpf/disasm.c +4
-0@@ -299,6 +299,10 @@ void print_bpf_insn(const struct bpf_insn_cbs *cbs,verbose (cbs->private_data,"BUG_ldx_%02x" , insn->code);return ; } +if (BPF_MODE (insn->code) ==BPF_MEMSX &&BPF_SIZE (insn->code) ==BPF_DW ) { +verbose (cbs->private_data,"BUG_ldsx_%02x" , insn->code); +return ; + }
Already fixed in https://lore.kernel.org/bpf/[email protected].
At the very least, fetch the latest tree before working on fixes.
pw-bot: cr
Quoted 3 lines
kernel/bpf/disasm.c +0
-0verbose(cbs->private_data, "(%02x) r%d = *(%s *)(r%d %+d)", insn->code, insn->dst_reg, BPF_MODE(insn->code) == BPF_MEM ?
[email protected]
AI reviewDoes check_insn_fields() actually reject this opcode? Looking at the BPF_LDX case in check_insn_fields():
Quoted 11 lines
print_bpf_insn()renders aBPF_LDXby indexing one of two size tables withBPF_SIZE(insn->code)>> 3. ForBPF_MEMSXit uses bpf_ldsx_string[], which has only three entries (W/H/B) because a sign-extended doubleword load is not a valid instruction. The LDX branch checks only that the mode isBPF_MEMorBPF_MEMSX; it never rejectsBPF_MEMSX|BPF_DW. For that opcodeBPF_SIZE(code)>> 3 ==BPF_DW>> 3 == 3, one element past the end of the 3-entry array:UBSAN: array-index-out-of-bounds: index 3 out of range for 'char *[3]' KASAN: global-out-of-bounds: 8-byte read in print_bpf_insn
Quoted 8 lines
The out-of-bounds slot is then dereferenced as %s.
The disassembler is expected to run on unvalidated instructions. Since commit
b9c5d822f677("bpf: Add source and instruction diagnostic context") the diagnostics facility disassembles instruction context fromcheck_subprogs(), beforecheck_insn_fields()has rejected the opcode, so a rawBPF_MEMSX|BPF_DWinsn reachesprint_bpf_insnon the ordinarybpf_prog_load()path whenever a verifier log is requested (log_level>= 1).
Does check_insn_fields() actually reject this opcode? Looking at the BPF_LDX case in check_insn_fields():
case BPF_LDX:
if ((BPF_MODE(insn->code) != BPF_MEM && BPF_MODE(insn->code) != BPF_MEMSX) ||
insn->imm != 0) {
verbose(env, "BPF_LDX uses reserved fields\n");
return -EINVAL;
}
return 0;BPF_MEMSX passes that test regardless of size, so check_insn_fields() returns 0 for BPF_LDX | BPF_MEMSX | BPF_DW.
What actually rejects this opcode is the bpf_opcode_in_insntable() call immediately above check_insn_fields() in check_and_resolve_insns() (kernel/bpf/verifier.c), which fails because kernel/bpf/core.c registers only INSN_3(LDX, MEMSX, B/H/W) in public_insntable[].
The reachability conclusion is unaffected since both checks happen in check_and_resolve_insns(), which bpf_check() calls after check_subprogs(). Could the commit message cite bpf_opcode_in_insntable() instead to help readers find the actual rejection site?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/33762867197