All of lore.kernel.org
 help / color / mirror / Atom feed
From: Rik van Riel <riel@surriel.com>
To: Alexei Starovoitov <ast@kernel.org>
Cc: Quentin Monnet <qmo@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	Jiri Olsa <jolsa@kernel.org>,
	Emil Tsalapatis <emil@etsalapatis.com>,
	bpf@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] bpf: fix array-index-out-of-bounds in print_bpf_insn
Date: Mon, 24 Aug 2026 16:41:43 -0400	[thread overview]
Message-ID: <20260824164143.485301ca@fangorn> (raw)

syzkaller triggers UBSAN array-index-out-of-bounds and panic_on_warn
panic in print_bpf_insn() when loading a BPF program containing
BPF_LDX | BPF_MEMSX | BPF_DW.

bpf_ldsx_string[] holds three entries for B/H/W (s8/s16/s32).
BPF_DW gives index 3 and reads past the array. check_subprogs()
prints the program via bpf_diag_program_structure() before
do_check() validates the instruction, so the illegal insn reaches
the disassembler.

    UBSAN: array-index-out-of-bounds in kernel/bpf/disasm.c:306:21
    index 3 is out of range for type 'char *[3]'
    print_bpf_insn+0x2328/0x2940 kernel/bpf/disasm.c:306
    format_disasm_line kernel/bpf/diagnostics.c:633
    diag_print_insn_context+0x3a5/0x930 kernel/bpf/diagnostics.c:783
    bpf_diag_source+0x615/0x14a0 kernel/bpf/diagnostics.c:896
    bpf_diag_program_structure+0x1f9/0x280 kernel/bpf/diagnostics.c:1215
    check_subprogs+0x5c3/0x650 kernel/bpf/verifier.c:3057
    bpf_check+0x1935/0x89f0 kernel/bpf/verifier.c:21097
    bpf_prog_load+0x17dd/0x2990 kernel/bpf/syscall.c:3133

Guard any size index beyond the corresponding string table and emit
BUG_ldx instead of accessing memory beyond the end of the the array.

Fixes: f835bb622299 ("bpf: Add kernel/bpftool asm support for new instructions")
Assisted-by: claw:muse-spark-1.2 syzkaller
Signed-off-by: Rik van Riel <riel@surriel.coM>
---
 kernel/bpf/disasm.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/kernel/bpf/disasm.c b/kernel/bpf/disasm.c
index 50b3ca5149a0..67bdda1c021c 100644
--- a/kernel/bpf/disasm.c
+++ b/kernel/bpf/disasm.c
@@ -295,15 +295,27 @@ void print_bpf_insn(const struct bpf_insn_cbs *cbs,
 			verbose(cbs->private_data, "BUG_st_%02x", insn->code);
 		}
 	} else if (class == BPF_LDX) {
+		unsigned int array_size = ARRAY_SIZE(bpf_ldsx_string);
+		unsigned int size_idx = BPF_SIZE(insn->code) >> 3;
+
 		if (BPF_MODE(insn->code) != BPF_MEM && BPF_MODE(insn->code) != BPF_MEMSX) {
 			verbose(cbs->private_data, "BUG_ldx_%02x", insn->code);
 			return;
 		}
+
+		if (BPF_MODE(insn->code) == BPF_MEM)
+			array_size = ARRAY_SIZE(bpf_ldst_string);
+
+		if (size_idx >= array_size) {
+			verbose(cbs->private_data, "BUG_ldx_%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 ?
-				 bpf_ldst_string[BPF_SIZE(insn->code) >> 3] :
-				 bpf_ldsx_string[BPF_SIZE(insn->code) >> 3],
+				 bpf_ldst_string[size_idx] :
+				 bpf_ldsx_string[size_idx],
 			insn->src_reg, insn->off);
 	} else if (class == BPF_LD) {
 		if (BPF_MODE(insn->code) == BPF_ABS) {
-- 
2.53.0-Meta



             reply	other threads:[~2026-08-24 20:42 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-24 20:41 Rik van Riel [this message]
2026-08-24 20:45 ` [PATCH] bpf: fix array-index-out-of-bounds in print_bpf_insn Kumar Kartikeya Dwivedi
2026-08-24 21:23 ` bot+bpf-ci

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=20260824164143.485301ca@fangorn \
    --to=riel@surriel.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=qmo@kernel.org \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.