Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] char: mem: keep arch range checks overflow-safe
@ 2026-06-25  8:58 Yousef Alhouseen
  2026-07-17 14:17 ` Greg Kroah-Hartman
  0 siblings, 1 reply; 2+ messages in thread
From: Yousef Alhouseen @ 2026-06-25  8:58 UTC (permalink / raw)
  To: Arnd Bergmann, Greg Kroah-Hartman
  Cc: Russell King, Yoshinori Sato, Rich Felker,
	John Paul Adrian Glaubitz, linux-kernel, linux-arm-kernel,
	linux-sh, Yousef Alhouseen

The generic /dev/mem physical range check now avoids validating
addr + size directly, but ARM and SH provide their own
valid_phys_addr_range() implementations with the same wrapped-addition
pattern.

Use subtraction-based upper-bound checks in those overrides as well. Also
make the ARM mmap pfn check avoid overflowing the pfn plus page-count
expression.

Suggested-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Yousef Alhouseen <alhouseenyousef@gmail.com>
---
 arch/arm/mm/mmap.c | 9 +++++++--
 arch/sh/mm/mmap.c  | 4 +++-
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/arch/arm/mm/mmap.c b/arch/arm/mm/mmap.c
index 3dbb383c2..7467ce8cd 100644
--- a/arch/arm/mm/mmap.c
+++ b/arch/arm/mm/mmap.c
@@ -150,9 +150,11 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  */
 int valid_phys_addr_range(phys_addr_t addr, size_t size)
 {
+	phys_addr_t end = __pa(high_memory - 1) + 1;
+
 	if (addr < PHYS_OFFSET)
 		return 0;
-	if (addr + size > __pa(high_memory - 1) + 1)
+	if (addr > end || size > end - addr)
 		return 0;
 
 	return 1;
@@ -163,5 +165,8 @@ int valid_phys_addr_range(phys_addr_t addr, size_t size)
  */
 int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
 {
-	return (pfn + (size >> PAGE_SHIFT)) <= (1 + (PHYS_MASK >> PAGE_SHIFT));
+	unsigned long max_pfn = 1 + (PHYS_MASK >> PAGE_SHIFT);
+	unsigned long pages = size >> PAGE_SHIFT;
+
+	return pfn <= max_pfn && pages <= max_pfn - pfn;
 }
diff --git a/arch/sh/mm/mmap.c b/arch/sh/mm/mmap.c
index c442734d9..ba7aade46 100644
--- a/arch/sh/mm/mmap.c
+++ b/arch/sh/mm/mmap.c
@@ -170,9 +170,11 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  */
 int valid_phys_addr_range(phys_addr_t addr, size_t count)
 {
+	phys_addr_t end = __pa(high_memory);
+
 	if (addr < __MEMORY_START)
 		return 0;
-	if (addr + count > __pa(high_memory))
+	if (addr > end || count > end - addr)
 		return 0;
 
 	return 1;
-- 
2.54.0



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] char: mem: keep arch range checks overflow-safe
  2026-06-25  8:58 [PATCH] char: mem: keep arch range checks overflow-safe Yousef Alhouseen
@ 2026-07-17 14:17 ` Greg Kroah-Hartman
  0 siblings, 0 replies; 2+ messages in thread
From: Greg Kroah-Hartman @ 2026-07-17 14:17 UTC (permalink / raw)
  To: Yousef Alhouseen
  Cc: Arnd Bergmann, Russell King, Yoshinori Sato, Rich Felker,
	John Paul Adrian Glaubitz, linux-kernel, linux-arm-kernel,
	linux-sh

On Thu, Jun 25, 2026 at 10:58:00AM +0200, Yousef Alhouseen wrote:
> The generic /dev/mem physical range check now avoids validating
> addr + size directly, but ARM and SH provide their own
> valid_phys_addr_range() implementations with the same wrapped-addition
> pattern.
> 
> Use subtraction-based upper-bound checks in those overrides as well. Also
> make the ARM mmap pfn check avoid overflowing the pfn plus page-count
> expression.
> 
> Suggested-by: Arnd Bergmann <arnd@arndb.de>
> Signed-off-by: Yousef Alhouseen <alhouseenyousef@gmail.com>
> ---
>  arch/arm/mm/mmap.c | 9 +++++++--
>  arch/sh/mm/mmap.c  | 4 +++-
>  2 files changed, 10 insertions(+), 3 deletions(-)

This should be part of the char/mem patch, right?

thanks,

greg k-h


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-07-17 14:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-25  8:58 [PATCH] char: mem: keep arch range checks overflow-safe Yousef Alhouseen
2026-07-17 14:17 ` Greg Kroah-Hartman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox