mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH bpf-next v1 0/3] bpf: Do not follow __iomem pointers
@ 2026-11-28  2:53 Kumar Kartikeya Dwivedi
  2026-11-28  2:53 ` [PATCH bpf-next v1 1/3] compiler_types: Emit a BTF type tag for __iomem Kumar Kartikeya Dwivedi
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Kumar Kartikeya Dwivedi @ 2026-11-28  2:53 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Eduard Zingerman, Emil Tsalapatis, Ihor Solodrai, kkd,
	kernel-team, Dave Hansen, Andy Lutomirski, Peter Zijlstra,
	Thomas Gleixner, Ingo Molnar, Borislav Petkov, Puranjay Mohan,
	x86, linux-kernel

A BPF tracing program can read a device register with a plain field walk,
for example mdev->common->device_status through a
struct virtio_pci_common_cfg __iomem *. The load is a PROBE_MEM load of a
mapped address, so it does not fault and reads the register. Such a read
can have side effects. This came up in the discussion of [1].

Patch 1 gives __iomem a BTF type tag, as __user, __percpu and __rcu have.
Patch 2 makes the verifier hand out a scalar for a struct member, function
argument or return value with that tag, so a program cannot load through
it. Patch 3 adds selftests.

  [1]: https://lore.kernel.org/bpf/DN6GS9V7A3HS.1COD0JUQQDHQ@gmail.com/

Kumar Kartikeya Dwivedi (3):
  compiler_types: Emit a BTF type tag for __iomem
  bpf: Treat __iomem pointers as scalars
  selftests/bpf: Test loads through __iomem pointers

 include/linux/compiler_types.h                |  2 +-
 kernel/bpf/btf.c                              | 15 +++++
 .../selftests/bpf/prog_tests/btf_tag.c        | 55 +++++++++++++++++++
 .../selftests/bpf/progs/btf_type_tag_iomem.c  | 49 +++++++++++++++++
 .../selftests/bpf/test_kmods/bpf_testmod.c    | 15 +++++
 5 files changed, 135 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/progs/btf_type_tag_iomem.c


base-commit: 71ad211eb086d0f8fc7ae595a37943bed58cad6f
-- 
2.53.1-Meta


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH bpf-next v1 1/3] compiler_types: Emit a BTF type tag for __iomem
  2026-11-28  2:53 [PATCH bpf-next v1 0/3] bpf: Do not follow __iomem pointers Kumar Kartikeya Dwivedi
@ 2026-11-28  2:53 ` Kumar Kartikeya Dwivedi
  2026-11-28  2:53 ` [PATCH bpf-next v1 2/3] bpf: Treat __iomem pointers as scalars Kumar Kartikeya Dwivedi
  2026-11-28  2:53 ` [PATCH bpf-next v1 3/3] selftests/bpf: Test loads through __iomem pointers Kumar Kartikeya Dwivedi
  2 siblings, 0 replies; 4+ messages in thread
From: Kumar Kartikeya Dwivedi @ 2026-11-28  2:53 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Eduard Zingerman, Emil Tsalapatis, Ihor Solodrai, kkd,
	kernel-team, Dave Hansen, Andy Lutomirski, Peter Zijlstra,
	Thomas Gleixner, Ingo Molnar, Borislav Petkov, Puranjay Mohan,
	x86, linux-kernel

__user, __percpu and __rcu pointers carry a BTF type tag, which tells the
BPF verifier what they point to. __iomem pointers do not: outside of sparse
the annotation expands to nothing, and in BTF such a pointer looks like any
other.

Define __iomem as BTF_TYPE_TAG(iomem), so that BTF records which pointers
hold the address of a device register. The next patch uses it.

As for the other three, the tag is only emitted with CONFIG_DEBUG_INFO_BTF,
a pahole that supports type tags and a compiler that has the btf_type_tag
attribute. The attribute does not take part in type checking. With the
config of the BPF selftests, vmlinux BTF grows by 892 bytes for 27 tagged
types.

Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
 include/linux/compiler_types.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h
index c5921f139007..8cfa555664e9 100644
--- a/include/linux/compiler_types.h
+++ b/include/linux/compiler_types.h
@@ -67,7 +67,7 @@ static inline void __chk_io_ptr(const volatile void __iomem *ptr) { }
 # else
 #  define __user	BTF_TYPE_TAG(user)
 # endif
-# define __iomem
+# define __iomem	BTF_TYPE_TAG(iomem)
 # define __percpu	__percpu_qual BTF_TYPE_TAG(percpu)
 # define __rcu		BTF_TYPE_TAG(rcu)
 
-- 
2.53.1-Meta


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH bpf-next v1 2/3] bpf: Treat __iomem pointers as scalars
  2026-11-28  2:53 [PATCH bpf-next v1 0/3] bpf: Do not follow __iomem pointers Kumar Kartikeya Dwivedi
  2026-11-28  2:53 ` [PATCH bpf-next v1 1/3] compiler_types: Emit a BTF type tag for __iomem Kumar Kartikeya Dwivedi
@ 2026-11-28  2:53 ` Kumar Kartikeya Dwivedi
  2026-11-28  2:53 ` [PATCH bpf-next v1 3/3] selftests/bpf: Test loads through __iomem pointers Kumar Kartikeya Dwivedi
  2 siblings, 0 replies; 4+ messages in thread
From: Kumar Kartikeya Dwivedi @ 2026-11-28  2:53 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Eduard Zingerman, Emil Tsalapatis, Ihor Solodrai, kkd,
	kernel-team, Dave Hansen, Andy Lutomirski, Peter Zijlstra,
	Thomas Gleixner, Ingo Molnar, Borislav Petkov, Puranjay Mohan,
	x86, linux-kernel

A BPF program can read a device register with a plain field walk. Given

  struct virtio_pci_modern_device {
          struct virtio_pci_common_cfg __iomem *common;
          void __iomem *device;
          ...
          u8 __iomem *isr;

all of mdev->common->device_status, *mdev->isr and
*(u8 *)(mdev->device + 4) load today. The verifier turns the first pointer
into a pointer to the struct and the other two into untrusted memory, and
each load becomes a PROBE_MEM load. The address is mapped, so the load does
not fault and reads the register. Such a read can have side effects: the
virtio ISR above is cleared by reading it.

These pointers now have the "iomem" BTF type tag. When a struct member, a
function argument or a return value has it, give the program a scalar. A
scalar cannot be followed, so the three loads above are rejected with
"R1 invalid mem access 'scalar'". The value of the pointer can still be
read.

Programs that load through such a pointer today stop loading.

bpf_probe_read_kernel() is not changed. It calls copy_from_kernel_nofault()
and should inherit any check that gets for I/O memory.

Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
 kernel/bpf/btf.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index c4386a9a1ad4..c78b37b813e2 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -7632,6 +7632,13 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
 	if (!btf_type_is_struct_ptr(btf, t))
 		return true;
 
+	/*
+	 * An __iomem pointer holds the address of a device register. A load
+	 * through it would read the device, so treat it as a scalar too.
+	 */
+	if (btf_ptr_has_type_tag(btf, t, "iomem"))
+		return true;
+
 	/* this is a pointer to another type */
 	for (i = 0; i < prog->aux->ctx_arg_info_size; i++) {
 		const struct bpf_ctx_arg_aux *ctx_arg_info = &prog->aux->ctx_arg_info[i];
@@ -8018,6 +8025,14 @@ static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf,
 				return -EACCES;
 			}
 
+			/*
+			 * An __iomem pointer holds the address of a device
+			 * register. A load through it would read the device, so
+			 * hand out a scalar, which cannot be followed.
+			 */
+			if (btf_ptr_has_type_tag(btf, mtype, "iomem"))
+				return WALK_SCALAR;
+
 			err = btf_type_tag_walk(btf, &ctx, walk_type_tags,
 						ARRAY_SIZE(walk_type_tags));
 			if (err) {
-- 
2.53.1-Meta


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH bpf-next v1 3/3] selftests/bpf: Test loads through __iomem pointers
  2026-11-28  2:53 [PATCH bpf-next v1 0/3] bpf: Do not follow __iomem pointers Kumar Kartikeya Dwivedi
  2026-11-28  2:53 ` [PATCH bpf-next v1 1/3] compiler_types: Emit a BTF type tag for __iomem Kumar Kartikeya Dwivedi
  2026-11-28  2:53 ` [PATCH bpf-next v1 2/3] bpf: Treat __iomem pointers as scalars Kumar Kartikeya Dwivedi
@ 2026-11-28  2:53 ` Kumar Kartikeya Dwivedi
  2 siblings, 0 replies; 4+ messages in thread
From: Kumar Kartikeya Dwivedi @ 2026-11-28  2:53 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Eduard Zingerman, Emil Tsalapatis, Ihor Solodrai, kkd,
	kernel-team, Dave Hansen, Andy Lutomirski, Peter Zijlstra,
	Thomas Gleixner, Ingo Molnar, Borislav Petkov, Puranjay Mohan,
	x86, linux-kernel

Give bpf_testmod a function with an __iomem struct pointer argument, and a
struct with two __iomem members, one pointing to a struct and one to an
int. Check that a program cannot load through any of the three, and that
it can still read the value of the pointers.

The subtests are skipped when the BTF of bpf_testmod has no "iomem" type
tag.

Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
 .../selftests/bpf/prog_tests/btf_tag.c        | 55 +++++++++++++++++++
 .../selftests/bpf/progs/btf_type_tag_iomem.c  | 49 +++++++++++++++++
 .../selftests/bpf/test_kmods/bpf_testmod.c    | 15 +++++
 3 files changed, 119 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/btf_type_tag_iomem.c

diff --git a/tools/testing/selftests/bpf/prog_tests/btf_tag.c b/tools/testing/selftests/bpf/prog_tests/btf_tag.c
index 071430cd54de..164359841381 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf_tag.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf_tag.c
@@ -11,6 +11,7 @@ struct btf_type_tag_test {
 #include "btf_type_tag.skel.h"
 #include "btf_type_tag_user.skel.h"
 #include "btf_type_tag_percpu.skel.h"
+#include "btf_type_tag_iomem.skel.h"
 
 static void test_btf_decl_tag(void)
 {
@@ -224,6 +225,51 @@ static void test_btf_type_tag_vmlinux_percpu(bool load_test)
 	btf__free(vmlinux_btf);
 }
 
+/* Load only @name. A load through an __iomem pointer has to fail. */
+static void test_btf_type_tag_mod_iomem(const char *name, bool fails)
+{
+	struct btf *vmlinux_btf = NULL, *module_btf = NULL;
+	struct btf_type_tag_iomem *skel = NULL;
+	struct bpf_program *prog;
+	char log[4096] = {};
+	int err;
+
+	if (load_btfs(&vmlinux_btf, &module_btf, /*needs_vmlinux_tag=*/false))
+		return;
+
+	/* skip the test if the module does not have __iomem tags */
+	if (btf__find_by_name_kind(module_btf, "iomem", BTF_KIND_TYPE_TAG) <= 0) {
+		printf("%s:SKIP: no __iomem btf_type_tag in bpf_testmod", __func__);
+		test__skip();
+		goto cleanup;
+	}
+
+	skel = btf_type_tag_iomem__open();
+	if (!ASSERT_OK_PTR(skel, "btf_type_tag_iomem"))
+		goto cleanup;
+
+	bpf_object__for_each_program(prog, skel->obj)
+		bpf_program__set_autoload(prog, !strcmp(bpf_program__name(prog), name));
+
+	prog = bpf_object__find_program_by_name(skel->obj, name);
+	if (!ASSERT_OK_PTR(prog, "find_program"))
+		goto cleanup;
+	bpf_program__set_log_buf(prog, log, sizeof(log));
+
+	err = btf_type_tag_iomem__load(skel);
+	if (fails) {
+		ASSERT_ERR(err, name);
+		ASSERT_HAS_SUBSTR(log, "invalid mem access 'scalar'", "log");
+	} else {
+		ASSERT_OK(err, name);
+	}
+
+cleanup:
+	btf_type_tag_iomem__destroy(skel);
+	btf__free(module_btf);
+	btf__free(vmlinux_btf);
+}
+
 void test_btf_tag(void)
 {
 	if (test__start_subtest("btf_decl_tag"))
@@ -246,4 +292,13 @@ void test_btf_tag(void)
 		test_btf_type_tag_vmlinux_percpu(true);
 	if (test__start_subtest("btf_type_tag_percpu_vmlinux_helper"))
 		test_btf_type_tag_vmlinux_percpu(false);
+
+	if (test__start_subtest("btf_type_tag_iomem_arg"))
+		test_btf_type_tag_mod_iomem("test_iomem_arg", true);
+	if (test__start_subtest("btf_type_tag_iomem_struct"))
+		test_btf_type_tag_mod_iomem("test_iomem_struct", true);
+	if (test__start_subtest("btf_type_tag_iomem_int"))
+		test_btf_type_tag_mod_iomem("test_iomem_int", true);
+	if (test__start_subtest("btf_type_tag_iomem_value"))
+		test_btf_type_tag_mod_iomem("test_iomem_value", false);
 }
diff --git a/tools/testing/selftests/bpf/progs/btf_type_tag_iomem.c b/tools/testing/selftests/bpf/progs/btf_type_tag_iomem.c
new file mode 100644
index 000000000000..29f1faa318e8
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/btf_type_tag_iomem.c
@@ -0,0 +1,49 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+struct bpf_testmod_btf_type_tag_1 {
+	int a;
+};
+
+struct bpf_testmod_btf_type_tag_4 {
+	struct bpf_testmod_btf_type_tag_1 *p;
+	int *q;
+};
+
+long g;
+
+/* The argument is an __iomem pointer to a struct. */
+SEC("fentry/bpf_testmod_test_btf_type_tag_iomem_1")
+int BPF_PROG(test_iomem_arg, struct bpf_testmod_btf_type_tag_1 *arg)
+{
+	g = arg->a;
+	return 0;
+}
+
+/* The member is an __iomem pointer to a struct. */
+SEC("fentry/bpf_testmod_test_btf_type_tag_iomem_2")
+int BPF_PROG(test_iomem_struct, struct bpf_testmod_btf_type_tag_4 *arg)
+{
+	g = arg->p->a;
+	return 0;
+}
+
+/* The member is an __iomem pointer to an int. */
+SEC("fentry/bpf_testmod_test_btf_type_tag_iomem_2")
+int BPF_PROG(test_iomem_int, struct bpf_testmod_btf_type_tag_4 *arg)
+{
+	g = *arg->q;
+	return 0;
+}
+
+/* The value of the pointers can still be read. */
+SEC("fentry/bpf_testmod_test_btf_type_tag_iomem_2")
+int BPF_PROG(test_iomem_value, struct bpf_testmod_btf_type_tag_4 *arg)
+{
+	g = (long)arg->p + (long)arg->q;
+	return 0;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
index 1fbc0061cc24..debcc58418e7 100644
--- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
+++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
@@ -650,6 +650,11 @@ struct bpf_testmod_btf_type_tag_3 {
 	struct bpf_testmod_btf_type_tag_1 __percpu *p;
 };
 
+struct bpf_testmod_btf_type_tag_4 {
+	struct bpf_testmod_btf_type_tag_1 __iomem *p;
+	int __iomem *q;
+};
+
 noinline int
 bpf_testmod_test_btf_type_tag_user_1(struct bpf_testmod_btf_type_tag_1 __user *arg) {
 	BTF_TYPE_EMIT(func_proto_typedef);
@@ -673,6 +678,16 @@ bpf_testmod_test_btf_type_tag_percpu_2(struct bpf_testmod_btf_type_tag_3 *arg) {
 	return arg->p->a;
 }
 
+noinline int
+bpf_testmod_test_btf_type_tag_iomem_1(struct bpf_testmod_btf_type_tag_1 __iomem *arg) {
+	return !!arg;
+}
+
+noinline int
+bpf_testmod_test_btf_type_tag_iomem_2(struct bpf_testmod_btf_type_tag_4 *arg) {
+	return arg->p && arg->q;
+}
+
 noinline int bpf_testmod_loop_test(int n)
 {
 	/* Make sum volatile, so smart compilers, such as clang, will not
-- 
2.53.1-Meta


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-28  2:53 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-28  2:53 [PATCH bpf-next v1 0/3] bpf: Do not follow __iomem pointers Kumar Kartikeya Dwivedi
2026-11-28  2:53 ` [PATCH bpf-next v1 1/3] compiler_types: Emit a BTF type tag for __iomem Kumar Kartikeya Dwivedi
2026-11-28  2:53 ` [PATCH bpf-next v1 2/3] bpf: Treat __iomem pointers as scalars Kumar Kartikeya Dwivedi
2026-11-28  2:53 ` [PATCH bpf-next v1 3/3] selftests/bpf: Test loads through __iomem pointers Kumar Kartikeya Dwivedi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®