linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] mm/mmap: check mapping limits more strictly
@ 2020-03-23 13:29 agordeev
  2020-03-23 13:29 ` [PATCH 1/2] mm/mmap.c: add more sanity checks to get_unmapped_area() agordeev
  2020-03-23 13:29 ` [PATCH 2/2] mm/mmap.c: do not allow mappings outside of allowed limits agordeev
  0 siblings, 2 replies; 5+ messages in thread
From: agordeev @ 2020-03-23 13:29 UTC (permalink / raw)
  To: linux-kernel; +Cc: linux-mm, Alexander Gordeev

From: Alexander Gordeev <agordeev@linux.ibm.com>

This series is against v5.6-rc7

Alexander Gordeev (2):
  mm/mmap.c: add more sanity checks to get_unmapped_area()
  mm/mmap.c: do not allow mappings outside of allowed limits

 mm/mmap.c | 29 +++++++++++++++++------------
 1 file changed, 17 insertions(+), 12 deletions(-)

-- 
2.23.0



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

* [PATCH 1/2] mm/mmap.c: add more sanity checks to get_unmapped_area()
  2020-03-23 13:29 [PATCH 0/2] mm/mmap: check mapping limits more strictly agordeev
@ 2020-03-23 13:29 ` agordeev
  2020-03-23 13:29 ` [PATCH 2/2] mm/mmap.c: do not allow mappings outside of allowed limits agordeev
  1 sibling, 0 replies; 5+ messages in thread
From: agordeev @ 2020-03-23 13:29 UTC (permalink / raw)
  To: linux-kernel; +Cc: linux-mm, Alexander Gordeev

From: Alexander Gordeev <agordeev@linux.ibm.com>

Generic get_unmapped_area() function does sanity checks
of address and length of the area to be mapped. Yet, it
lacks checking against mmap_min_addr and mmap_end limits.

At the same time the default implementation of functions
arch_get_unmapped_area[_topdown]() and some architecture
callbacks do mmap_min_addr and mmap_end checks on its own.

Put additional checks into the generic code and do not let
architecture callbacks to get away with a possible area
outside of the allowed limits.

That could also relieve arch_get_unmapped_area[_topdown]()
callbacks of own address and length sanity checks.

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

diff --git a/mm/mmap.c b/mm/mmap.c
index d681a20eb4ea..a0fcb5ca0e06 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -2168,12 +2168,13 @@ get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
 	unsigned long (*get_area)(struct file *, unsigned long,
 				  unsigned long, unsigned long, unsigned long);
 
+	const unsigned long mmap_end = arch_get_mmap_end(addr);
 	unsigned long error = arch_mmap_check(addr, len, flags);
 	if (error)
 		return error;
 
 	/* Careful about overflows.. */
-	if (len > TASK_SIZE)
+	if (len > mmap_end - mmap_min_addr)
 		return -ENOMEM;
 
 	get_area = current->mm->get_unmapped_area;
@@ -2194,7 +2195,7 @@ get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
 	if (IS_ERR_VALUE(addr))
 		return addr;
 
-	if (addr > TASK_SIZE - len)
+	if ((addr < mmap_min_addr) || (addr > mmap_end - len))
 		return -ENOMEM;
 	if (offset_in_page(addr))
 		return -EINVAL;
-- 
2.23.0



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

* [PATCH 2/2] mm/mmap.c: do not allow mappings outside of allowed limits
  2020-03-23 13:29 [PATCH 0/2] mm/mmap: check mapping limits more strictly agordeev
  2020-03-23 13:29 ` [PATCH 1/2] mm/mmap.c: add more sanity checks to get_unmapped_area() agordeev
@ 2020-03-23 13:29 ` agordeev
  2020-04-21  2:59   ` Andrew Morton
  1 sibling, 1 reply; 5+ messages in thread
From: agordeev @ 2020-03-23 13:29 UTC (permalink / raw)
  To: linux-kernel; +Cc: linux-mm, Alexander Gordeev

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(-)

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.
  *
-- 
2.23.0



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

* Re: [PATCH 2/2] mm/mmap.c: do not allow mappings outside of allowed limits
  2020-03-23 13:29 ` [PATCH 2/2] mm/mmap.c: do not allow mappings outside of allowed limits agordeev
@ 2020-04-21  2:59   ` Andrew Morton
  2020-04-21  5:29     ` Alexander Gordeev
  0 siblings, 1 reply; 5+ messages in thread
From: Andrew Morton @ 2020-04-21  2:59 UTC (permalink / raw)
  To: agordeev; +Cc: linux-kernel, linux-mm

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?



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

* Re: [PATCH 2/2] mm/mmap.c: do not allow mappings outside of allowed limits
  2020-04-21  2:59   ` Andrew Morton
@ 2020-04-21  5:29     ` Alexander Gordeev
  0 siblings, 0 replies; 5+ messages in thread
From: Alexander Gordeev @ 2020-04-21  5:29 UTC (permalink / raw)
  To: Andrew Morton; +Cc: linux-kernel, linux-mm

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!



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

end of thread, other threads:[~2020-04-21  5:29 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-23 13:29 [PATCH 0/2] mm/mmap: check mapping limits more strictly agordeev
2020-03-23 13:29 ` [PATCH 1/2] mm/mmap.c: add more sanity checks to get_unmapped_area() agordeev
2020-03-23 13:29 ` [PATCH 2/2] mm/mmap.c: do not allow mappings outside of allowed limits agordeev
2020-04-21  2:59   ` Andrew Morton
2020-04-21  5:29     ` Alexander Gordeev

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).