Skip to contentSkip to conversation

selftests/bpf: add test for nullable PTR_TO_BUF access

PATCH Applied Qi Tang 2 participants 2 messages Apr 7

Subsystems (3)
lore
Share & tools
lore

Qi Tang

author
#0 lore raw

PATCH 1/1 selftests/bpf: add test for nullable PTR_TO_BUF access

Add iter_buf_null_fail with two tests and a test runner:
- iter_buf_null_deref: verifier must reject direct dereference of

    ctx->key (PTR_TO_BUF | PTR_MAYBE_NULL) without a null check

- iter_buf_null_check_ok: verifier must accept dereference after an explicit null check

---
 .../bpf/prog_tests/iter_buf_null_fail.c       |  9 +++++
 .../selftests/bpf/progs/iter_buf_null_fail.c  | 39 +++++++++++++++++++
 2 files changed, 48 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/iter_buf_null_fail.c
 create mode 100644 tools/testing/selftests/bpf/progs/iter_buf_null_fail.c
tools/testing/selftests/bpf/prog_tests/iter_buf_null_fail.c new file +9 -0
new file mode 100644
index 000000000000..ea97787b870d
@@ -0,0 +1,9 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <test_progs.h>
+#include "iter_buf_null_fail.skel.h"
+
+void test_iter_buf_null_fail(void)
+{
+	RUN_TESTS(iter_buf_null_fail);
+}
tools/testing/selftests/bpf/progs/iter_buf_null_fail.c new file +39 -0
new file mode 100644
index 000000000000..3daad40515e6
@@ -0,0 +1,39 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Qi Tang */
+
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+/* Verify that the verifier rejects direct access to nullable PTR_TO_BUF. */
+SEC("iter/bpf_map_elem")
+__failure __msg("invalid mem access")
+int iter_buf_null_deref(struct bpf_iter__bpf_map_elem *ctx)
+{
+	/*
+	 * ctx->key is PTR_TO_BUF | PTR_MAYBE_NULL | MEM_RDONLY.
+	 * Direct access without null check must be rejected.
+	 */
+	volatile __u32 v = *(__u32 *)ctx->key;
+
+	(void)v;
+	return 0;
+}
+
+/* Verify that access after a null check is still accepted. */
+SEC("iter/bpf_map_elem")
+__success
+int iter_buf_null_check_ok(struct bpf_iter__bpf_map_elem *ctx)
+{
+	__u32 *key = ctx->key;
+
+	if (!key)
+		return 0;
+
+	volatile __u32 v = *key;
+
+	(void)v;
+	return 0;
+}
Signature · 1 line
2.43.0
#1 lore raw
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 11 lines · On Tue, 7 Apr 2026 22:54:21 +0800 you wrote:

Add iter_buf_null_fail with two tests and a test runner:
- iter_buf_null_deref: verifier must reject direct dereference of

    ctx->key (PTR_TO_BUF | PTR_MAYBE_NULL) without a null check

- iter_buf_null_check_ok: verifier must accept dereference after an explicit null check

[...]

Here is the summary with links:
- [bpf-next] selftests/bpf: add test for nullable PTR_TO_BUF access

    https://git.kernel.org/bpf/bpf-next/c/a4985a1755ec

You are awesome, thank you!

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