diff mbox series

[2/2] mm/mmap.c: do not allow mappings outside of allowed limits

Message ID d6da1319114a331095052638f0ffa3ccb0be58f1.1584958099.git.agordeev@linux.ibm.com (mailing list archive)
State New, archived
Headers show
Series mm/mmap: check mapping limits more strictly | expand

Commit Message

Alexander Gordeev March 23, 2020, 1:29 p.m. UTC
From: Alexander Gordeev <agordeev@linux.ibm.com>

It is possible to request a fixed mapping address below
mmap_min_addr and succeed. This update adds early checks
of mmap_min_addr and mmap_end boundaries and fixes the
above issue.

Signed-off-by: Alexander Gordeev <agordeev@linux.ibm.com>
---
 mm/mmap.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)

Comments

Andrew Morton April 21, 2020, 2:59 a.m. UTC | #1
On Mon, 23 Mar 2020 14:29:29 +0100 agordeev@linux.ibm.com wrote:

> It is possible to request a fixed mapping address below
> mmap_min_addr and succeed. This update adds early checks
> of mmap_min_addr and mmap_end boundaries and fixes the
> above issue.

Does this solve any known problems?  If not, what is the motivation for
the change?
Alexander Gordeev April 21, 2020, 5:29 a.m. UTC | #2
On Mon, Apr 20, 2020 at 07:59:03PM -0700, Andrew Morton wrote:
> On Mon, 23 Mar 2020 14:29:29 +0100 agordeev@linux.ibm.com wrote:
> 
> > It is possible to request a fixed mapping address below
> > mmap_min_addr and succeed. This update adds early checks
> > of mmap_min_addr and mmap_end boundaries and fixes the
> > above issue.
> 
> Does this solve any known problems?  If not, what is the motivation for
> the change?

One can set a lowest possible address in /proc/sys/vm/mmap_min_addr 
and mmap below that bound nevertheless. Apart from it is wrong I am
not aware of any existing issue.

Thanks!
diff mbox series

Patch

diff --git a/mm/mmap.c b/mm/mmap.c
index a0fcb5ca0e06..bd673a01b1d0 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -59,6 +59,14 @@ 
 #define arch_mmap_check(addr, len, flags)	(0)
 #endif
 
+#ifndef arch_get_mmap_end
+#define arch_get_mmap_end(addr)	(TASK_SIZE)
+#endif
+
+#ifndef arch_get_mmap_base
+#define arch_get_mmap_base(addr, base) (base)
+#endif
+
 #ifdef CONFIG_HAVE_ARCH_MMAP_RND_BITS
 const int mmap_rnd_bits_min = CONFIG_ARCH_MMAP_RND_BITS_MIN;
 const int mmap_rnd_bits_max = CONFIG_ARCH_MMAP_RND_BITS_MAX;
@@ -1366,6 +1374,7 @@  unsigned long do_mmap(struct file *file, unsigned long addr,
 			unsigned long pgoff, unsigned long *populate,
 			struct list_head *uf)
 {
+	const unsigned long mmap_end = arch_get_mmap_end(addr);
 	struct mm_struct *mm = current->mm;
 	int pkey = 0;
 
@@ -1388,8 +1397,12 @@  unsigned long do_mmap(struct file *file, unsigned long addr,
 	if (flags & MAP_FIXED_NOREPLACE)
 		flags |= MAP_FIXED;
 
-	if (!(flags & MAP_FIXED))
+	if (flags & MAP_FIXED) {
+		if ((addr < mmap_min_addr) || (addr > mmap_end))
+			return -ENOMEM;
+	} else {
 		addr = round_hint_to_min(addr);
+	}
 
 	/* Careful about overflows.. */
 	len = PAGE_ALIGN(len);
@@ -2050,15 +2063,6 @@  unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info)
 	return gap_end;
 }
 
-
-#ifndef arch_get_mmap_end
-#define arch_get_mmap_end(addr)	(TASK_SIZE)
-#endif
-
-#ifndef arch_get_mmap_base
-#define arch_get_mmap_base(addr, base) (base)
-#endif
-
 /* Get an address range which is currently unmapped.
  * For shmat() with addr=0.
  *