mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Kumar Kartikeya Dwivedi <memxor@gmail.com>
To: bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Emil Tsalapatis <emil@etsalapatis.com>,
	Ihor Solodrai <ihor.solodrai@linux.dev>,
	kkd@meta.com, kernel-team@meta.com,
	Dave Hansen <dave.hansen@linux.intel.com>,
	Andy Lutomirski <luto@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
	Borislav Petkov <bp@alien8.de>,
	Puranjay Mohan <puranjay@kernel.org>,
	x86@kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH bpf-next v1 2/3] bpf: Treat __iomem pointers as scalars
Date: Sat, 28 Nov 2026 03:53:16 +0100	[thread overview]
Message-ID: <52249df7401e63679e1ec5c416ef9b12d3ad318c.1795834380.git.memxor@gmail.com> (raw)
In-Reply-To: <cover.1795834380.git.memxor@gmail.com>

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


  parent reply	other threads:[~2026-11-28  2:53 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2026-11-28  2:53 ` [PATCH bpf-next v1 3/3] selftests/bpf: Test loads through __iomem pointers Kumar Kartikeya Dwivedi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=52249df7401e63679e1ec5c416ef9b12d3ad318c.1795834380.git.memxor@gmail.com \
    --to=memxor@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bp@alien8.de \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=dave.hansen@linux.intel.com \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=kernel-team@meta.com \
    --cc=kkd@meta.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=puranjay@kernel.org \
    --cc=tglx@kernel.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®