]> Git Repo - linux.git/commitdiff
x86/ioremap: Improve iounmap() address range checks
authorMax Ramanouski <[email protected]>
Sat, 24 Aug 2024 22:01:11 +0000 (01:01 +0300)
committerDave Hansen <[email protected]>
Mon, 26 Aug 2024 17:19:55 +0000 (10:19 -0700)
Allowing iounmap() on memory that was not ioremap()'d in the first
place is obviously a bad idea.  There is currently a feeble attempt to
avoid errant iounmap()s by checking to see if the address is below
"high_memory".  But that's imprecise at best because there are plenty
of high addresses that are also invalid to call iounmap() on.

Thankfully, there is a more precise helper: is_ioremap_addr().  x86
just does not use it in iounmap().

Restrict iounmap() to addresses in the ioremap region, by using
is_ioremap_addr(). This aligns x86 closer to the generic iounmap()
implementation.

Additionally, add a warning in case there is an attempt to iounmap()
invalid memory.  This replaces an existing silent return and will
help alert folks to any incorrect usage of iounmap().

Due to VMALLOC_START on i386 not being present in asm/pgtable.h,
include for asm/vmalloc.h had to be added to include/linux/ioremap.h.

[ dhansen: tweak subject and changelog ]

Signed-off-by: Max Ramanouski <[email protected]>
Signed-off-by: Dave Hansen <[email protected]>
Reviewed-by: Christoph Hellwig <[email protected]>
Reviewed-by: Alistair Popple <[email protected]>
Link: https://lore.kernel.org/all/20240824220111.84441-1-max8rr8%40gmail.com
arch/x86/mm/ioremap.c
include/linux/ioremap.h

index aa7d279321ea0cca935374b16b96fa349cdaacd2..70b02fc61d935c0854cba4e51098c12210355978 100644 (file)
@@ -11,6 +11,7 @@
 #include <linux/init.h>
 #include <linux/io.h>
 #include <linux/ioport.h>
+#include <linux/ioremap.h>
 #include <linux/slab.h>
 #include <linux/vmalloc.h>
 #include <linux/mmiotrace.h>
@@ -457,7 +458,7 @@ void iounmap(volatile void __iomem *addr)
 {
        struct vm_struct *p, *o;
 
-       if ((void __force *)addr <= high_memory)
+       if (WARN_ON_ONCE(!is_ioremap_addr((void __force *)addr)))
                return;
 
        /*
index f0e99fc7dd8b23038e05fe9d37b5495cac29d199..2bd1661fe9ad607ada4ecbe4f8d091337706b742 100644 (file)
@@ -4,6 +4,7 @@
 
 #include <linux/kasan.h>
 #include <asm/pgtable.h>
+#include <asm/vmalloc.h>
 
 #if defined(CONFIG_HAS_IOMEM) || defined(CONFIG_GENERIC_IOREMAP)
 /*
This page took 0.054989 seconds and 4 git commands to generate.