Skip to contentSkip to conversation

bpf: Do not follow __iomem pointers

PATCH In review Kumar Kartikeya Dwivedi 2 participants 5 messages Nov 28 – Dec 1

Subsystems (4)
lore
Share & tools
lore

Kumar Kartikeya Dwivedi

author maintainer
#0 lore raw

cover bpf: Do not follow __iomem pointers

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/[email protected]/

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

Signature · 1 line
2.53.1-Meta
author maintainer
#1 lore raw

PATCH 1/3 compiler_types: Emit a BTF type tag for __iomem

__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.

---
 include/linux/compiler_types.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/compiler_types.h +1 -1
index c5921f139007..8cfa555664e9 100644
@@ -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)
 
Signature · 1 line
2.53.1-Meta
author maintainer
#2 lore raw

PATCH 2/3 bpf: Treat __iomem pointers as scalars

In reply to Kumar Kartikeya Dwivedi

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.

---
 kernel/bpf/btf.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
kernel/bpf/btf.c +15 -0
index c4386a9a1ad4..c78b37b813e2 100644
@@ -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) {
Signature · 1 line
2.53.1-Meta
#3 lore raw

Re: [PATCH bpf-next v1 2/3] bpf: Treat __iomem pointers as scalars

Quoted 15 lines · Kumar Kartikeya Dwivedi

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.

Quoted 7 lines

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.

This looks entirely reasonable, with the caveat that it should reject a reasonably large class of problematic programs but does not reject *all* problematic programs. I don't think it can be the basis of a sound static is-a-pointer-in-bounds verification.

--Andy

author maintainer
#4 lore raw

PATCH 3/3 selftests/bpf: Test loads through __iomem pointers

In reply to Kumar Kartikeya Dwivedi

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.

---
 .../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
tools/testing/selftests/bpf/prog_tests/btf_tag.c +55 -0
index 071430cd54de..164359841381 100644
@@ -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);
 }
tools/testing/selftests/bpf/progs/btf_type_tag_iomem.c new file +49 -0

49 lines not on this page · raw message

tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +15 -0
index 1fbc0061cc24..debcc58418e7 100644
@@ -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
Signature · 1 line
2.53.1-Meta