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

From: Kumar Kartikeya Dwivedi

Date: Fri Nov 27 2026 - 21:53:34 EST


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@xxxxxxxxx>
---
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