[PATCH] riscv: mm: Fix /dev/mem read/write range validation

From: Zhao Gongyi

Date: Sat Nov 14 2026 - 03:30:25 EST


RISC-V uses the generic valid_phys_addr_range(), which checks only the
upper limit. A pwrite() to physical address 0 on QEMU virt can therefore
reach write_mem() -> xlate_dev_mem_ptr() -> __va(), triggering a
hardened usercopy BUG ("vmalloc 'no area'"). UART MMIO with iomem=relaxed
triggers the same fault.

Limit read/write to ranges fully covered by System RAM with valid PFNs
at or above the physical start of the linear map. This rejects holes
and MMIO before the __va() access. Keep the mmap() range policy unchanged.

MMIO read/write now returns -EFAULT even with iomem=relaxed. With
CONFIG_STRICT_DEVMEM=y, the existing RAM policy blocks the remaining
non-empty read/write ranges.

Signed-off-by: Zhao Gongyi <zhaogongyi@xxxxxxxxxxxxx>
---
RV64 and RV32 Image builds passed. RV64 QEMU covered the faulting
physical-0 and UART-MMIO writes, RAM/hole boundaries, and MMIO mmap.

arch/riscv/include/asm/io.h | 4 ++
arch/riscv/mm/Makefile | 2 +-
arch/riscv/mm/mmap.c | 93 +++++++++++++++++++++++++++++++++++++
3 files changed, 98 insertions(+), 1 deletion(-)
create mode 100644 arch/riscv/mm/mmap.c

diff --git a/arch/riscv/include/asm/io.h b/arch/riscv/include/asm/io.h
index 92d5f831f..ef305ede2 100644
--- a/arch/riscv/include/asm/io.h
+++ b/arch/riscv/include/asm/io.h
@@ -32,6 +32,10 @@
#define ioremap_wc(addr, size) \
ioremap_prot((addr), (size), __pgprot(_PAGE_KERNEL_NC))

+#define ARCH_HAS_VALID_PHYS_ADDR_RANGE
+int valid_phys_addr_range(phys_addr_t addr, size_t size);
+int valid_mmap_phys_addr_range(unsigned long pfn, size_t size);
+
#endif /* CONFIG_MMU */

/*
diff --git a/arch/riscv/mm/Makefile b/arch/riscv/mm/Makefile
index b916a68d3..e3461bec3 100644
--- a/arch/riscv/mm/Makefile
+++ b/arch/riscv/mm/Makefile
@@ -13,7 +13,7 @@ endif
KCOV_INSTRUMENT_init.o := n

obj-y += init.o
-obj-$(CONFIG_MMU) += extable.o fault.o pageattr.o pgtable.o tlbflush.o
+obj-$(CONFIG_MMU) += extable.o fault.o mmap.o pageattr.o pgtable.o tlbflush.o
obj-y += cacheflush.o
obj-y += context.o
obj-y += pmem.o
diff --git a/arch/riscv/mm/mmap.c b/arch/riscv/mm/mmap.c
new file mode 100644
index 000000000..45a0ac740
--- /dev/null
+++ b/arch/riscv/mm/mmap.c
@@ -0,0 +1,93 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/ioport.h>
+#include <linux/mm.h>
+#include <linux/pfn.h>
+#include <asm/io.h>
+#include <asm/page.h>
+
+struct devmem_ram_range {
+ unsigned long next_pfn;
+ unsigned long end_pfn;
+ bool complete;
+};
+
+static int devmem_ram_range_check(unsigned long pfn, unsigned long nr_pages,
+ void *arg)
+{
+ struct devmem_ram_range *range = arg;
+ unsigned long last_pfn;
+
+ if (pfn != range->next_pfn)
+ return 1;
+
+ last_pfn = pfn + nr_pages - 1;
+ if (last_pfn < pfn)
+ return 1;
+ if (last_pfn > range->end_pfn)
+ last_pfn = range->end_pfn;
+
+ for (;;) {
+ if (!pfn_valid(pfn))
+ return 1;
+ if (pfn == last_pfn)
+ break;
+ pfn++;
+ }
+
+ if (last_pfn == range->end_pfn) {
+ range->complete = true;
+ return 0;
+ }
+
+ range->next_pfn = last_pfn + 1;
+ return 0;
+}
+
+/*
+ * You really should not be using read() or write() on /dev/mem. Restrict
+ * these operations to ranges that are completely covered by System RAM with
+ * valid struct page backing. Device access should use proper I/O accessors,
+ * and /dev/mem mmap() keeps its separate policy below.
+ */
+int valid_phys_addr_range(phys_addr_t addr, size_t size)
+{
+ struct devmem_ram_range range;
+ phys_addr_t end;
+ phys_addr_t linear_start;
+ unsigned long start_pfn;
+ unsigned long nr_pages;
+
+ if (!size)
+ return 1;
+
+ end = addr + size - 1;
+ if (end < addr)
+ return 0;
+
+ linear_start = __pa(PAGE_OFFSET);
+ if (addr < linear_start)
+ return 0;
+
+ start_pfn = PHYS_PFN(addr);
+ range.end_pfn = PHYS_PFN(end);
+ if (range.end_pfn < start_pfn)
+ return 0;
+
+ nr_pages = range.end_pfn - start_pfn + 1;
+ range.next_pfn = start_pfn;
+ range.complete = false;
+
+ return !walk_system_ram_range(start_pfn, nr_pages, &range,
+ devmem_ram_range_check) &&
+ range.complete;
+}
+
+/*
+ * Preserve the existing /dev/mem mmap() range policy. Access filtering for
+ * mmap() is still handled by range_is_allowed().
+ */
+int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
+{
+ return 1;
+}

base-commit: 79fc9d9467a113bb93f4539c11fa8c937982ff89
--
2.39.5 (Apple Git-154)