Skip to contentSkip to conversation

bpf: fix out-of-bounds write in bpf_bprintf_prepare with %pI4/%pI6

PATCH New Ibrahim Zein 2 participants 2 messages Mar 19

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: fix out-of-bounds write in bpf_bprintf_prepare with %pI4/%pI6

In bpf_bprintf_prepare(), the bounds check for %pI4 and %pI6 format specifiers uses sizeof_cur_ip (4 for IPv4, 16 for IPv6), which is the raw byte count of the IP address. However, snprintf() returns the length of the formatted string, not the raw bytes. For IPv4 this can be up to 15 characters (255.255.255.255) and for IPv6 up to 39.

tmp_buf is then advanced by (err + 1) using the full string length, which can push tmp_buf past tmp_buf_end. The next iteration's bounds check underflows due to unsigned arithmetic and passes, allowing a write past the end of the per-CPU bin_args buffer.

Fix this by checking against the maximum formatted string size:
16 bytes for IPv4 and 40 bytes for IPv6.

---
 kernel/bpf/helpers.c                          |  2 +-
 .../bpf/prog_tests/test_snprintf_ip.c         | 54 +++++++++++++
 .../selftests/bpf/progs/test_snprintf_ip.c    | 81 +++++++++++++++++++
 3 files changed, 136 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/test_snprintf_ip.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_snprintf_ip.c
kernel/bpf/helpers.c +1 -1
index cb6d242bd..dcaa822ba 100644
@@ -930,7 +930,7 @@ int bpf_bprintf_prepare(const char *fmt, u32 fmt_size, const u64 *raw_args,
 				goto nocopy_fmt;
 
 			sizeof_cur_ip = (fmt[i] == '4') ? 4 : 16;
-			if (tmp_buf_end - tmp_buf < sizeof_cur_ip) {
+			if (tmp_buf_end - tmp_buf < (size_t)((fmt[i] == '4') ? 16 : 40)) {
 				err = -ENOSPC;
 				goto out;
 			}
tools/testing/selftests/bpf/prog_tests/test_snprintf_ip.c new file +54 -0

54 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_snprintf_ip.c new file +81 -0

81 lines not on this page · raw message

Signature · 1 line
2.52.0
#1 lore raw
The comment says "we use fewer args here since BPF_SNPRINTF macro has a limit", but this call passes 25 variadic arguments (24 integers plus one pointer). The BPF_SNPRINTF macro only supports up to…
Quoted 5 lines
tools/testing/selftests/bpf/progs/test_snprintf_ip.c new file +0 -0
new file mode 100644
index 000000000000..44cc4d74c5e2

[ ... ]

Quoted 8 lines
tools/testing/selftests/bpf/progs/test_snprintf_ip.c new file +8 -0
+	/*
+	 * Test 4: near-overflow scenario.
+	 * Fill bin_args with scalar args (%d), then use %pI4.
+	 * Before the fix: tmp_buf overflows by 8 bytes.
+	 * After the fix: correctly returns -ENOSPC or fits exactly.
+	 *
+	 * We use fewer args here since BPF_SNPRINTF macro has a limit,
+	 * but this validates the format string parsing path.

tools/testing/selftests/bpf/progs/test_snprintf_ip.c line 76

Quoted 10 lines
tools/testing/selftests/bpf/progs/test_snprintf_ip.c new file +10 -0
+	 */
+	near_overflow_ret = BPF_SNPRINTF(NULL, 0,
+		"%d %d %d %d %d %d %d %d "
+		"%d %d %d %d %d %d %d %d "
+		"%d %d %d %d %d %d %d %d "
+		"%pI4",
+		1, 2, 3, 4, 5, 6, 7, 8,
+		9, 10, 11, 12, 13, 14, 15, 16,
+		17, 18, 19, 20, 21, 22, 23, 24,
+		&ip4_worst);

The comment says "we use fewer args here since BPF_SNPRINTF macro has a limit", but this call passes 25 variadic arguments (24 integers plus one pointer). The BPF_SNPRINTF macro only supports up to 12 arguments.

The ___bpf_narg() counting macro in tools/lib/bpf/bpf_helpers.h handles at most 12 arguments, and ___bpf_fill is only defined for ___bpf_fill0 through ___bpf_fill12. With 25 arguments, ___bpf_narg overflows and the expansion references ___bpf_fill13, which does not exist.

Won't this fail to compile? The argument count would need to be reduced to at most 12 (11 scalars plus the %pI4 pointer, for example) for the BPF program to build.

Additionally, this commit fixes an out-of-bounds write that has been present since bpf_bprintf_prepare() was introduced but does not include a Fixes: tag. Should this have:

Fixes 48cac3f4a96d ("bpf: Implement formatted output helpers with bstr_printf")

---
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/23279168532