char: mem: reject wrapped physical ranges
PATCH 1/1 char: mem: reject wrapped physical ranges
The generic /dev/mem range check validates addr + count directly. A large count can wrap the physical address calculation and pass the check against high_memory.
Use subtraction-based range validation so read_mem() and write_mem() reject ranges that either start past the limit or extend beyond it.
--- drivers/char/mem.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/char/mem.c +3 -1
@@ -47,7 +47,9 @@ static inline unsigned long size_inside_page(unsigned long start,#ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE static inline int valid_phys_addr_range (phys_addr_t addr,size_t count) { -return addr + count <=__pa (high_memory); +phys_addr_t end =__pa (high_memory); + +return addr <= end && count <= end - addr; }static inline int valid_mmap_phys_addr_range (unsigned long pfn,size_t size)
Signature · 1 line
2.54.0
Arnd Bergmann
maintainerdrivers/char/mem.c static inline unsigned long size_inside_page(unsigned · line 54
Quoted 13 lines · Yousef Alhouseen
index
63253d1de..9e22b18f7100644drivers/char/mem.c +3
-1@@ -47,7 +47,9 @@ static inline unsigned long size_inside_page(unsigned long start,#ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE static inline int valid_phys_addr_range (phys_addr_t addr,size_t count) { -return addr + count <=__pa (high_memory); +phys_addr_t end =__pa (high_memory); + +return addr <= end && count <= end - addr; }
The change looks correct, but the same thing still happens in the arch/arm and arch/sh versions of the same function.
Please keep them in sync.
Ideally we'd generalize this function to check the lower bound against ARCH_PFN_OFFSET and remove the arm and sh versions. I see that half the architectures allow having RAM start at a non-zero address but don't have this check, e.g. mips, powerpc, and riscv.
Arnd
Hi Arnd,
Thanks, I kept the ARM and SH implementations in sync and sent a follow-up patch:
[PATCH] char: mem: keep arch range checks overflow-safe
I left the broader lower-bound/generalization cleanup for separate work.
Thanks,
Yousef
Quoted 27 lines · Arnd Bergmann
drivers/char/mem.c static inline unsigned long size_inside_page(unsigned · line 54
Quoted 13 lines · Yousef Alhouseen
index
63253d1de..9e22b18f7100644drivers/char/mem.c +3
-1@@ -47,7 +47,9 @@ static inline unsigned long size_inside_page(unsigned long start,#ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE static inline int valid_phys_addr_range (phys_addr_t addr,size_t count) { -return addr + count <=__pa (high_memory); +phys_addr_t end =__pa (high_memory); + +return addr <= end && count <= end - addr; }The change looks correct, but the same thing still happens in the
arch/armandarch/shversions of the same function.Please keep them in sync.
Ideally we'd generalize this function to check the lower bound against
ARCH_PFN_OFFSETand remove the arm and sh versions. I see that half the architectures allow having RAM start at a non-zero address but don't have this check, e.g. mips, powerpc, and riscv.Arnd