Skip to contentSkip to conversation

RFC PATCH In review Michal Hocko 7 participants 22 messages Nov 16, 2017 – Nov 27, 2017

Subsystems (12)
Memory management VFS PowerPC MIPS +8 more subsystems
lore
Share & tools
lore
#0 lore raw

Hi,
this has started as a follow up discussion [1][2] resulting in the runtime failure caused by hardening patch [3] which removes MAP_FIXED from the elf loader because MAP_FIXED is inherently dangerous as it might silently clobber and existing underlying mapping (e.g. stack). The reason for the failure is that some architectures enforce an alignment for the given address hint without MAP_FIXED used (e.g. for shared or file backed mappings).

One way around this would be excluding those archs which do alignment tricks from the hardening [4]. The patch is really trivial but it has been objected, rightfully so, that this screams for a more generic solution. We basically want a non-destructive MAP_FIXED.

The first patch introduced MAP_FIXED_SAFE which enforces the given address but unlike MAP_FIXED it fails with ENOMEM if the given range conflicts with an existing one. The flag is introduced as a completely new flag rather than a MAP_FIXED extension because of the backward compatibility. We really want a never-clobber semantic even on older kernels which do not recognize the flag. Unfortunately mmap sucks wrt. flags evaluation because we do not EINVAL on unknown flags. On those kernels we would simply use the traditional hint based semantic so the caller can still get a different address (which sucks) but at least not silently corrupt an existing mapping. I do not see a good way around that. Except we won't export expose the new semantic to the userspace at all. It seems there are users who would like to have something like that [5], though. Atomic address range probing in the multithreaded programs sounds like an interesting thing to me as well, although I do not have any specific usecase in mind.

The second patch simply replaces MAP_FIXED use in elf loader by MAP_FIXED_SAFE. I believe other places which rely on MAP_FIXED should follow. Actually real MAP_FIXED usages should be docummented properly and they should be more of an exception.

Does anybody see any fundamental reasons why this is a wrong approach?

Diffstat says

 arch/alpha/include/uapi/asm/mman.h   |  2 ++
 arch/metag/kernel/process.c          |  6 +++++-
 arch/mips/include/uapi/asm/mman.h    |  2 ++
 arch/parisc/include/uapi/asm/mman.h  |  2 ++
 arch/powerpc/include/uapi/asm/mman.h |  1 +
 arch/sparc/include/uapi/asm/mman.h   |  1 +
 arch/tile/include/uapi/asm/mman.h    |  1 +
 arch/xtensa/include/uapi/asm/mman.h  |  2 ++
 fs/binfmt_elf.c                      | 12 ++++++++----
 include/uapi/asm-generic/mman.h      |  1 +
 mm/mmap.c                            | 11 +++++++++++
 11 files changed, 36 insertions(+), 5 deletions(-)

[1] http://lkml.kernel.org/r/[email protected]
[2] http://lkml.kernel.org/r/[email protected]
[3] http://lkml.kernel.org/r/[email protected]
[4] http://lkml.kernel.org/r/[email protected]
[5] http://lkml.kernel.org/r/[email protected]

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#1 lore raw

RFC PATCH 1/2 mm: introduce MAP_FIXED_SAFE

From: Michal Hocko <[email protected]>

MAP_FIXED is used quite often to enforce mapping at the particular range. The main problem of this flag is, however, that it is inherently dangerous because it unmaps existing mappings covered by the requested range. This can cause silent memory corruptions. Some of them even with serious security implications. While the current semantic might be really desiderable in many cases there are others which would want to enforce the given range but rather see a failure than a silent memory corruption on a clashing range. Please note that there is no guarantee that a given range is obeyed by the mmap even when it is free - e.g. arch specific code is allowed to apply an alignment.

Introduce a new MAP_FIXED_SAFE flag for mmap to achieve this behavior. It has the same semantic as MAP_FIXED wrt. the given address request with a single exception that it fails with ENOMEM if the requested address is already covered by an existing mapping. We still do rely on get_unmaped_area to handle all the arch specific MAP_FIXED treatment and check for a conflicting vma after it returns.

[set MAP_FIXED before round_hint_to_min as per Khalid Aziz]
---
 arch/alpha/include/uapi/asm/mman.h   |  2 ++
 arch/mips/include/uapi/asm/mman.h    |  2 ++
 arch/parisc/include/uapi/asm/mman.h  |  2 ++
 arch/powerpc/include/uapi/asm/mman.h |  1 +
 arch/sparc/include/uapi/asm/mman.h   |  1 +
 arch/tile/include/uapi/asm/mman.h    |  1 +
 arch/xtensa/include/uapi/asm/mman.h  |  2 ++
 include/uapi/asm-generic/mman.h      |  1 +
 mm/mmap.c                            | 11 +++++++++++
 9 files changed, 23 insertions(+)
arch/alpha/include/uapi/asm/mman.h +2 -0
index 3b26cc62dadb..0e5724e4b4ad 100644
@@ -31,6 +31,8 @@
 #define MAP_STACK	0x80000		/* give out an address that is best suited for process/thread stacks */
 #define MAP_HUGETLB	0x100000	/* create a huge page mapping */
 
+#define MAP_FIXED_SAFE	0x200000	/* MAP_FIXED which doesn't unmap underlying mapping */
+
 #define MS_ASYNC	1		/* sync memory asynchronously */
 #define MS_SYNC		2		/* synchronous memory sync */
 #define MS_INVALIDATE	4		/* invalidate the caches */
arch/mips/include/uapi/asm/mman.h +2 -0
index da3216007fe0..fc5e61ef9fd4 100644
@@ -49,6 +49,8 @@
 #define MAP_STACK	0x40000		/* give out an address that is best suited for process/thread stacks */
 #define MAP_HUGETLB	0x80000		/* create a huge page mapping */
 
+#define MAP_FIXED_SAFE	0x100000	/* MAP_FIXED which doesn't unmap underlying mapping */
+
 /*
  * Flags for msync
  */
arch/parisc/include/uapi/asm/mman.h +2 -0
index cc9ba1d34779..c926487472fa 100644
@@ -25,6 +25,8 @@
 #define MAP_STACK	0x40000		/* give out an address that is best suited for process/thread stacks */
 #define MAP_HUGETLB	0x80000		/* create a huge page mapping */
 
+#define MAP_FIXED_SAFE	0x100000	/* MAP_FIXED which doesn't unmap underlying mapping */
+
 #define MS_SYNC		1		/* synchronous memory sync */
 #define MS_ASYNC	2		/* sync memory asynchronously */
 #define MS_INVALIDATE	4		/* invalidate the caches */
arch/powerpc/include/uapi/asm/mman.h +1 -0
index 03c06ba7464f..d97342ca25b1 100644
@@ -28,5 +28,6 @@
 #define MAP_NONBLOCK	0x10000		/* do not block on IO */
 #define MAP_STACK	0x20000		/* give out an address that is best suited for process/thread stacks */
 #define MAP_HUGETLB	0x40000		/* create a huge page mapping */
+#define MAP_FIXED_SAFE	0x800000	/* MAP_FIXED which doesn't unmap underlying mapping */
 
 #endif /* _UAPI_ASM_POWERPC_MMAN_H */
arch/sparc/include/uapi/asm/mman.h +1 -0
index 9765896ecb2c..7b00477a7f9a 100644
@@ -23,6 +23,7 @@
 #define MAP_NONBLOCK	0x10000		/* do not block on IO */
 #define MAP_STACK	0x20000		/* give out an address that is best suited for process/thread stacks */
 #define MAP_HUGETLB	0x40000		/* create a huge page mapping */
+#define MAP_FIXED_SAFE	0x80000		/* MAP_FIXED which doesn't unmap underlying mapping */
 
 
 #endif /* _UAPI__SPARC_MMAN_H__ */
arch/tile/include/uapi/asm/mman.h +1 -0
index 63ee13faf17d..d5d58d2dc95e 100644
@@ -29,6 +29,7 @@
 #define MAP_DENYWRITE	0x0800		/* ETXTBSY */
 #define MAP_EXECUTABLE	0x1000		/* mark it as an executable */
 #define MAP_HUGETLB	0x4000		/* create a huge page mapping */
+#define MAP_FIXED_SAFE	0x8000		/* MAP_FIXED which doesn't unmap underlying mapping */
 
 
 /*
arch/xtensa/include/uapi/asm/mman.h +2 -0
index b15b278aa314..d665bd8b7cbd 100644
@@ -55,6 +55,7 @@
 #define MAP_NONBLOCK	0x20000		/* do not block on IO */
 #define MAP_STACK	0x40000		/* give out an address that is best suited for process/thread stacks */
 #define MAP_HUGETLB	0x80000		/* create a huge page mapping */
+#define MAP_FIXED_SAFE	0x100000	/* MAP_FIXED which doesn't unmap underlying mapping */
 #ifdef CONFIG_MMAP_ALLOW_UNINITIALIZED
 # define MAP_UNINITIALIZED 0x4000000	/* For anonymous mmap, memory could be
 					 * uninitialized */
@@ -62,6 +63,7 @@
 # define MAP_UNINITIALIZED 0x0		/* Don't support this flag */
 #endif
 
+
 /*
  * Flags for msync
  */
include/uapi/asm-generic/mman.h +1 -0
index 7162cd4cca73..64c46047fbd3 100644
@@ -12,6 +12,7 @@
 #define MAP_NONBLOCK	0x10000		/* do not block on IO */
 #define MAP_STACK	0x20000		/* give out an address that is best suited for process/thread stacks */
 #define MAP_HUGETLB	0x40000		/* create a huge page mapping */
+#define MAP_FIXED_SAFE	0x80000		/* MAP_FIXED which doesn't unmap underlying mapping */
 
 /* Bits [26:31] are reserved, see mman-common.h for MAP_HUGETLB usage */
 
mm/mmap.c +11 -0
index 680506faceae..89af0b5839a5 100644
@@ -1342,6 +1342,10 @@ unsigned long do_mmap(struct file *file, unsigned long addr,
 		if (!(file && path_noexec(&file->f_path)))
 			prot |= PROT_EXEC;
 
+	/* force arch specific MAP_FIXED handling in get_unmapped_area */
+	if (flags & MAP_FIXED_SAFE)
+		flags |= MAP_FIXED;
+
 	if (!(flags & MAP_FIXED))
 		addr = round_hint_to_min(addr);
 
@@ -1365,6 +1369,13 @@ unsigned long do_mmap(struct file *file, unsigned long addr,
 	if (offset_in_page(addr))
 		return addr;
 
+	if (flags & MAP_FIXED_SAFE) {
+		struct vm_area_struct *vma = find_vma(mm, addr);
+
+		if (vma && vma->vm_start <= addr)
+			return -ENOMEM;
+	}
+
 	if (prot == PROT_EXEC) {
 		pkey = execute_only_pkey(mm);
 		if (pkey < 0)
Signature · 1 line
2.15.0
#12 lore raw

PATCH 2/2 fs, elf: drop MAP_FIXED usage from elf_map

In reply to Michal Hocko

From: Michal Hocko <[email protected]>

Both load_elf_interp and load_elf_binary rely on elf_map to map segments on a controlled address and they use MAP_FIXED to enforce that. This is however dangerous thing prone to silent data corruption which can be even exploitable. Let's take CVE-2017-1000253 as an example. At the time (before eab09532d400 ("binfmt_elf: use ELF_ET_DYN_BASE only for PIE")) ELF_ET_DYN_BASE was at TASK_SIZE / 3 * 2 which is not that far away from the stack top on 32b (legacy) memory layout (only 1GB away). Therefore we could end up mapping over the existing stack with some luck.

The issue has been fixed since then (a87938b2e246 ("fs/binfmt_elf.c:
fix bug in loading of PIE binaries")), ELF_ET_DYN_BASE moved moved much further from the stack (eab09532d400 and later by c715b72c1ba4 ("mm:
revert x86_64 and arm64 ELF_ET_DYN_BASE base changes")) and excessive stack consumption early during execve fully stopped by da029c11e6b1 ("exec: Limit arg stack to at most 75% of _STK_LIM"). So we should be safe and any attack should be impractical. On the other hand this is just too subtle assumption so it can break quite easily and hard to spot.

I believe that the MAP_FIXED usage in load_elf_binary (et. al) is still fundamentally dangerous. Moreover it shouldn't be even needed. We are at the early process stage and so there shouldn't be unrelated mappings (except for stack and loader) existing so mmap for a given address should succeed even without MAP_FIXED. Something is terribly wrong if this is not the case and we should rather fail than silently corrupt the underlying mapping.

Address this issue by changing MAP_FIXED to the newly added MAP_FIXED_SAFE. This will mean that mmap will fail if there is an existing mapping clashing with the requested one without clobbering it.

Cc Abdul Haleem <[email protected]>
Cc Joel Stanley <[email protected]>
Cc Kees Cook <[email protected]>
---
 arch/metag/kernel/process.c |  6 +++++-
 fs/binfmt_elf.c             | 12 ++++++++----
 2 files changed, 13 insertions(+), 5 deletions(-)
arch/metag/kernel/process.c +5 -1
index c4606ce743d2..2286140e54e0 100644
@@ -398,7 +398,7 @@ unsigned long __metag_elf_map(struct file *filep, unsigned long addr,
 	tcm_tag = tcm_lookup_tag(addr);
 
 	if (tcm_tag != TCM_INVALID_TAG)
-		type &= ~MAP_FIXED;
+		type &= ~(MAP_FIXED | MAP_FIXED_SAFE);
 
 	/*
 	* total_size is the size of the ELF (interpreter) image.
@@ -416,6 +416,10 @@ unsigned long __metag_elf_map(struct file *filep, unsigned long addr,
 	} else
 		map_addr = vm_mmap(filep, addr, size, prot, type, off);
 
+	if ((type & MAP_FIXED_SAFE) && BAD_ADDR(map_addr))
+		pr_info("%d (%s): Uhuuh, elf segement at %p requested but the memory is mapped already\n",
+				task_pid_nr(current), tsk->comm, (void*)addr);
+
 	if (!BAD_ADDR(map_addr) && tcm_tag != TCM_INVALID_TAG) {
 		struct tcm_allocation *tcm;
 		unsigned long tcm_addr;
fs/binfmt_elf.c +8 -4
index 6466153f2bf0..12b21942ccde 100644
@@ -372,6 +372,10 @@ static unsigned long elf_map(struct file *filep, unsigned long addr,
 	} else
 		map_addr = vm_mmap(filep, addr, size, prot, type, off);
 
+	if ((type & MAP_FIXED_SAFE) && BAD_ADDR(map_addr))
+		pr_info("%d (%s): Uhuuh, elf segement at %p requested but the memory is mapped already\n",
+				task_pid_nr(current), current->comm, (void*)addr);
+
 	return(map_addr);
 }
 
@@ -569,7 +573,7 @@ static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex,
 				elf_prot |= PROT_EXEC;
 			vaddr = eppnt->p_vaddr;
 			if (interp_elf_ex->e_type == ET_EXEC || load_addr_set)
-				elf_type |= MAP_FIXED;
+				elf_type |= MAP_FIXED_SAFE;
 			else if (no_base && interp_elf_ex->e_type == ET_DYN)
 				load_addr = -vaddr;
 
@@ -929,7 +933,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
 		 * the ET_DYN load_addr calculations, proceed normally.
 		 */
 		if (loc->elf_ex.e_type == ET_EXEC || load_addr_set) {
-			elf_flags |= MAP_FIXED;
+			elf_flags |= MAP_FIXED_SAFE;
 		} else if (loc->elf_ex.e_type == ET_DYN) {
 			/*
 			 * This logic is run once for the first LOAD Program
@@ -965,7 +969,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
 				load_bias = ELF_ET_DYN_BASE;
 				if (current->flags & PF_RANDOMIZE)
 					load_bias += arch_mmap_rnd();
-				elf_flags |= MAP_FIXED;
+				elf_flags |= MAP_FIXED_SAFE;
 			} else
 				load_bias = 0;
 
@@ -1220,7 +1224,7 @@ static int load_elf_library(struct file *file)
 			(eppnt->p_filesz +
 			 ELF_PAGEOFFSET(eppnt->p_vaddr)),
 			PROT_READ | PROT_WRITE | PROT_EXEC,
-			MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
+			MAP_FIXED_SAFE | MAP_PRIVATE | MAP_DENYWRITE,
 			(eppnt->p_offset -
 			 ELF_PAGEOFFSET(eppnt->p_vaddr)));
 	if (error != ELF_PAGESTART(eppnt->p_vaddr))
Signature · 7 lines
2.15.0

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#14 lore raw

cover mm: introduce MAP_FIXED_SAFE

In reply to Michal Hocko

[Ups, managed to screw the subject - fix it]

Quoted 63 lines · Michal Hocko

Hi,
this has started as a follow up discussion [1][2] resulting in the runtime failure caused by hardening patch [3] which removes MAP_FIXED from the elf loader because MAP_FIXED is inherently dangerous as it might silently clobber and existing underlying mapping (e.g. stack). The reason for the failure is that some architectures enforce an alignment for the given address hint without MAP_FIXED used (e.g. for shared or file backed mappings).

One way around this would be excluding those archs which do alignment tricks from the hardening [4]. The patch is really trivial but it has been objected, rightfully so, that this screams for a more generic solution. We basically want a non-destructive MAP_FIXED.

The first patch introduced MAP_FIXED_SAFE which enforces the given address but unlike MAP_FIXED it fails with ENOMEM if the given range conflicts with an existing one. The flag is introduced as a completely new flag rather than a MAP_FIXED extension because of the backward compatibility. We really want a never-clobber semantic even on older kernels which do not recognize the flag. Unfortunately mmap sucks wrt. flags evaluation because we do not EINVAL on unknown flags. On those kernels we would simply use the traditional hint based semantic so the caller can still get a different address (which sucks) but at least not silently corrupt an existing mapping. I do not see a good way around that. Except we won't export expose the new semantic to the userspace at all. It seems there are users who would like to have something like that [5], though. Atomic address range probing in the multithreaded programs sounds like an interesting thing to me as well, although I do not have any specific usecase in mind.

The second patch simply replaces MAP_FIXED use in elf loader by MAP_FIXED_SAFE. I believe other places which rely on MAP_FIXED should follow. Actually real MAP_FIXED usages should be docummented properly and they should be more of an exception.

Does anybody see any fundamental reasons why this is a wrong approach?

Diffstat says

 arch/alpha/include/uapi/asm/mman.h   |  2 ++
 arch/metag/kernel/process.c          |  6 +++++-
 arch/mips/include/uapi/asm/mman.h    |  2 ++
 arch/parisc/include/uapi/asm/mman.h  |  2 ++
 arch/powerpc/include/uapi/asm/mman.h |  1 +
 arch/sparc/include/uapi/asm/mman.h   |  1 +
 arch/tile/include/uapi/asm/mman.h    |  1 +
 arch/xtensa/include/uapi/asm/mman.h  |  2 ++
 fs/binfmt_elf.c                      | 12 ++++++++----
 include/uapi/asm-generic/mman.h      |  1 +
 mm/mmap.c                            | 11 +++++++++++
 11 files changed, 36 insertions(+), 5 deletions(-)

[1] http://lkml.kernel.org/r/[email protected]
[2] http://lkml.kernel.org/r/[email protected]
[3] http://lkml.kernel.org/r/20171023082608.6167-1-mhocko-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org
[4] http://lkml.kernel.org/r/20171113094203.aofz2e7kueitk55y-2MMpYkNvuYDjFM9bn6wA6Q@public.gmane.org
[5] http://lkml.kernel.org/r/87efp1w7vy.fsf-W0DJWXSxmBNbyGPkN3NxC2scP1bn1w/[email protected]

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected] For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Signature · 2 lines
Michal Hocko
SUSE Labs
#2 lore raw

Re: [RFC PATCH 1/2] mm: introduce MAP_FIXED_SAFE

In reply to Michal Hocko
Quoted 13 lines · Michal Hocko

From: Michal Hocko <[email protected]>

MAP_FIXED is used quite often to enforce mapping at the particular range. The main problem of this flag is, however, that it is inherently dangerous because it unmaps existing mappings covered by the requested range. This can cause silent memory corruptions. Some of them even with serious security implications. While the current semantic might be really desiderable in many cases there are others which would want to enforce the given range but rather see a failure than a silent memory corruption on a clashing range. Please note that there is no guarantee that a given range is obeyed by the mmap even when it is free - e.g. arch specific code is allowed to apply an alignment.

Quoted 6 lines

Introduce a new MAP_FIXED_SAFE flag for mmap to achieve this behavior. It has the same semantic as MAP_FIXED wrt. the given address request with a single exception that it fails with ENOMEM if the requested address is already covered by an existing mapping. We still do rely on get_unmaped_area to handle all the arch specific MAP_FIXED treatment and check for a conflicting vma after it returns.

I like this much more than special-casing the ELF loader. It is an unusual property that MAP_FIXED does _two_ things, so I like having this split out.

Bikeshedding: maybe call this MAP_NO_CLOBBER? It's a modifier to MAP_FIXED, really...

At the end of the day, I don't really care about the name, but "SAFE" isn't very descriptive for what the flag changes about FIXED.

-Kees

Quoted 153 lines
[set MAP_FIXED before round_hint_to_min as per Khalid Aziz]
---
 arch/alpha/include/uapi/asm/mman.h   |  2 ++
 arch/mips/include/uapi/asm/mman.h    |  2 ++
 arch/parisc/include/uapi/asm/mman.h  |  2 ++
 arch/powerpc/include/uapi/asm/mman.h |  1 +
 arch/sparc/include/uapi/asm/mman.h   |  1 +
 arch/tile/include/uapi/asm/mman.h    |  1 +
 arch/xtensa/include/uapi/asm/mman.h  |  2 ++
 include/uapi/asm-generic/mman.h      |  1 +
 mm/mmap.c                            | 11 +++++++++++
 9 files changed, 23 insertions(+)
arch/alpha/include/uapi/asm/mman.h +2 -0
index 3b26cc62dadb..0e5724e4b4ad 100644
@@ -31,6 +31,8 @@
 #define MAP_STACK      0x80000         /* give out an address that is best suited for process/thread stacks */
 #define MAP_HUGETLB    0x100000        /* create a huge page mapping */
 
+#define MAP_FIXED_SAFE 0x200000        /* MAP_FIXED which doesn't unmap underlying mapping */
+
 #define MS_ASYNC       1               /* sync memory asynchronously */
 #define MS_SYNC                2               /* synchronous memory sync */
 #define MS_INVALIDATE  4               /* invalidate the caches */
arch/mips/include/uapi/asm/mman.h +2 -0
index da3216007fe0..fc5e61ef9fd4 100644
@@ -49,6 +49,8 @@
 #define MAP_STACK      0x40000         /* give out an address that is best suited for process/thread stacks */
 #define MAP_HUGETLB    0x80000         /* create a huge page mapping */
 
+#define MAP_FIXED_SAFE 0x100000        /* MAP_FIXED which doesn't unmap underlying mapping */
+
 /*
  * Flags for msync
  */
arch/parisc/include/uapi/asm/mman.h +2 -0
index cc9ba1d34779..c926487472fa 100644
@@ -25,6 +25,8 @@
 #define MAP_STACK      0x40000         /* give out an address that is best suited for process/thread stacks */
 #define MAP_HUGETLB    0x80000         /* create a huge page mapping */
 
+#define MAP_FIXED_SAFE 0x100000        /* MAP_FIXED which doesn't unmap underlying mapping */
+
 #define MS_SYNC                1               /* synchronous memory sync */
 #define MS_ASYNC       2               /* sync memory asynchronously */
 #define MS_INVALIDATE  4               /* invalidate the caches */
arch/powerpc/include/uapi/asm/mman.h +1 -0
index 03c06ba7464f..d97342ca25b1 100644
@@ -28,5 +28,6 @@
 #define MAP_NONBLOCK   0x10000         /* do not block on IO */
 #define MAP_STACK      0x20000         /* give out an address that is best suited for process/thread stacks */
 #define MAP_HUGETLB    0x40000         /* create a huge page mapping */
+#define MAP_FIXED_SAFE 0x800000        /* MAP_FIXED which doesn't unmap underlying mapping */
 
 #endif /* _UAPI_ASM_POWERPC_MMAN_H */
arch/sparc/include/uapi/asm/mman.h +1 -0
index 9765896ecb2c..7b00477a7f9a 100644
@@ -23,6 +23,7 @@
 #define MAP_NONBLOCK   0x10000         /* do not block on IO */
 #define MAP_STACK      0x20000         /* give out an address that is best suited for process/thread stacks */
 #define MAP_HUGETLB    0x40000         /* create a huge page mapping */
+#define MAP_FIXED_SAFE 0x80000         /* MAP_FIXED which doesn't unmap underlying mapping */
 
 
 #endif /* _UAPI__SPARC_MMAN_H__ */
arch/tile/include/uapi/asm/mman.h +1 -0
index 63ee13faf17d..d5d58d2dc95e 100644
@@ -29,6 +29,7 @@
 #define MAP_DENYWRITE  0x0800          /* ETXTBSY */
 #define MAP_EXECUTABLE 0x1000          /* mark it as an executable */
 #define MAP_HUGETLB    0x4000          /* create a huge page mapping */
+#define MAP_FIXED_SAFE 0x8000          /* MAP_FIXED which doesn't unmap underlying mapping */
 
 
 /*
arch/xtensa/include/uapi/asm/mman.h +2 -0
index b15b278aa314..d665bd8b7cbd 100644
@@ -55,6 +55,7 @@
 #define MAP_NONBLOCK   0x20000         /* do not block on IO */
 #define MAP_STACK      0x40000         /* give out an address that is best suited for process/thread stacks */
 #define MAP_HUGETLB    0x80000         /* create a huge page mapping */
+#define MAP_FIXED_SAFE 0x100000        /* MAP_FIXED which doesn't unmap underlying mapping */
 #ifdef CONFIG_MMAP_ALLOW_UNINITIALIZED
 # define MAP_UNINITIALIZED 0x4000000   /* For anonymous mmap, memory could be
                                         * uninitialized */
@@ -62,6 +63,7 @@
 # define MAP_UNINITIALIZED 0x0         /* Don't support this flag */
 #endif
 
+
 /*
  * Flags for msync
  */
include/uapi/asm-generic/mman.h +1 -0
index 7162cd4cca73..64c46047fbd3 100644
@@ -12,6 +12,7 @@
 #define MAP_NONBLOCK   0x10000         /* do not block on IO */
 #define MAP_STACK      0x20000         /* give out an address that is best suited for process/thread stacks */
 #define MAP_HUGETLB    0x40000         /* create a huge page mapping */
+#define MAP_FIXED_SAFE 0x80000         /* MAP_FIXED which doesn't unmap underlying mapping */
 
 /* Bits [26:31] are reserved, see mman-common.h for MAP_HUGETLB usage */
 
mm/mmap.c +11 -1
index 680506faceae..89af0b5839a5 100644
@@ -1342,6 +1342,10 @@ unsigned long do_mmap(struct file *file, unsigned long addr,
                if (!(file && path_noexec(&file->f_path)))
                        prot |= PROT_EXEC;
 
+       /* force arch specific MAP_FIXED handling in get_unmapped_area */
+       if (flags & MAP_FIXED_SAFE)
+               flags |= MAP_FIXED;
+
        if (!(flags & MAP_FIXED))
                addr = round_hint_to_min(addr);
 
@@ -1365,6 +1369,13 @@ unsigned long do_mmap(struct file *file, unsigned long addr,
        if (offset_in_page(addr))
                return addr;
 
+       if (flags & MAP_FIXED_SAFE) {
+               struct vm_area_struct *vma = find_vma(mm, addr);
+
+               if (vma && vma->vm_start <= addr)
+                       return -ENOMEM;
+       }
+
        if (prot == PROT_EXEC) {
                pkey = execute_only_pkey(mm);
                if (pkey < 0)
--

2.15.0

Signature · 8 lines
Kees Cook
Pixel Security

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Acked-by on v1 2/2
#13 lore raw

Re: [PATCH 2/2] fs, elf: drop MAP_FIXED usage from elf_map

In reply to Michal Hocko
Quoted 29 lines · Michal Hocko

From: Michal Hocko <[email protected]>

Both load_elf_interp and load_elf_binary rely on elf_map to map segments on a controlled address and they use MAP_FIXED to enforce that. This is however dangerous thing prone to silent data corruption which can be even exploitable. Let's take CVE-2017-1000253 as an example. At the time (before eab09532d400 ("binfmt_elf: use ELF_ET_DYN_BASE only for PIE")) ELF_ET_DYN_BASE was at TASK_SIZE / 3 * 2 which is not that far away from the stack top on 32b (legacy) memory layout (only 1GB away). Therefore we could end up mapping over the existing stack with some luck.

The issue has been fixed since then (a87938b2e246 ("fs/binfmt_elf.c:
fix bug in loading of PIE binaries")), ELF_ET_DYN_BASE moved moved much further from the stack (eab09532d400 and later by c715b72c1ba4 ("mm:
revert x86_64 and arm64 ELF_ET_DYN_BASE base changes")) and excessive stack consumption early during execve fully stopped by da029c11e6b1 ("exec: Limit arg stack to at most 75% of _STK_LIM"). So we should be safe and any attack should be impractical. On the other hand this is just too subtle assumption so it can break quite easily and hard to spot.

I believe that the MAP_FIXED usage in load_elf_binary (et. al) is still fundamentally dangerous. Moreover it shouldn't be even needed. We are at the early process stage and so there shouldn't be unrelated mappings (except for stack and loader) existing so mmap for a given address should succeed even without MAP_FIXED. Something is terribly wrong if this is not the case and we should rather fail than silently corrupt the underlying mapping.

Quoted 8 lines

Address this issue by changing MAP_FIXED to the newly added MAP_FIXED_SAFE. This will mean that mmap will fail if there is an existing mapping clashing with the requested one without clobbering it.

Cc Abdul Haleem <[email protected]>
Cc Joel Stanley <[email protected]>
Cc Kees Cook <[email protected]>

Once (if?) the name gets settled, this looks good to me:

-Kees

Quoted 83 lines
---
 arch/metag/kernel/process.c |  6 +++++-
 fs/binfmt_elf.c             | 12 ++++++++----
 2 files changed, 13 insertions(+), 5 deletions(-)
arch/metag/kernel/process.c +5 -1
index c4606ce743d2..2286140e54e0 100644
@@ -398,7 +398,7 @@ unsigned long __metag_elf_map(struct file *filep, unsigned long addr,
        tcm_tag = tcm_lookup_tag(addr);
 
        if (tcm_tag != TCM_INVALID_TAG)
-               type &= ~MAP_FIXED;
+               type &= ~(MAP_FIXED | MAP_FIXED_SAFE);
 
        /*
        * total_size is the size of the ELF (interpreter) image.
@@ -416,6 +416,10 @@ unsigned long __metag_elf_map(struct file *filep, unsigned long addr,
        } else
                map_addr = vm_mmap(filep, addr, size, prot, type, off);
 
+       if ((type & MAP_FIXED_SAFE) && BAD_ADDR(map_addr))
+               pr_info("%d (%s): Uhuuh, elf segement at %p requested but the memory is mapped already\n",
+                               task_pid_nr(current), tsk->comm, (void*)addr);
+
        if (!BAD_ADDR(map_addr) && tcm_tag != TCM_INVALID_TAG) {
                struct tcm_allocation *tcm;
                unsigned long tcm_addr;
fs/binfmt_elf.c +8 -5
index 6466153f2bf0..12b21942ccde 100644
@@ -372,6 +372,10 @@ static unsigned long elf_map(struct file *filep, unsigned long addr,
        } else
                map_addr = vm_mmap(filep, addr, size, prot, type, off);
 
+       if ((type & MAP_FIXED_SAFE) && BAD_ADDR(map_addr))
+               pr_info("%d (%s): Uhuuh, elf segement at %p requested but the memory is mapped already\n",
+                               task_pid_nr(current), current->comm, (void*)addr);
+
        return(map_addr);
 }
 
@@ -569,7 +573,7 @@ static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex,
                                elf_prot |= PROT_EXEC;
                        vaddr = eppnt->p_vaddr;
                        if (interp_elf_ex->e_type == ET_EXEC || load_addr_set)
-                               elf_type |= MAP_FIXED;
+                               elf_type |= MAP_FIXED_SAFE;
                        else if (no_base && interp_elf_ex->e_type == ET_DYN)
                                load_addr = -vaddr;
 
@@ -929,7 +933,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
                 * the ET_DYN load_addr calculations, proceed normally.
                 */
                if (loc->elf_ex.e_type == ET_EXEC || load_addr_set) {
-                       elf_flags |= MAP_FIXED;
+                       elf_flags |= MAP_FIXED_SAFE;
                } else if (loc->elf_ex.e_type == ET_DYN) {
                        /*
                         * This logic is run once for the first LOAD Program
@@ -965,7 +969,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
                                load_bias = ELF_ET_DYN_BASE;
                                if (current->flags & PF_RANDOMIZE)
                                        load_bias += arch_mmap_rnd();
-                               elf_flags |= MAP_FIXED;
+                               elf_flags |= MAP_FIXED_SAFE;
                        } else
                                load_bias = 0;
 
@@ -1220,7 +1224,7 @@ static int load_elf_library(struct file *file)
                        (eppnt->p_filesz +
                         ELF_PAGEOFFSET(eppnt->p_vaddr)),
                        PROT_READ | PROT_WRITE | PROT_EXEC,
-                       MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
+                       MAP_FIXED_SAFE | MAP_PRIVATE | MAP_DENYWRITE,
                        (eppnt->p_offset -
                         ELF_PAGEOFFSET(eppnt->p_vaddr)));
        if (error != ELF_PAGESTART(eppnt->p_vaddr))
--

2.15.0

Signature · 8 lines
Kees Cook
Pixel Security

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#5 lore raw

Re: [RFC PATCH 1/2] mm: introduce MAP_FIXED_SAFE

In reply to Michal Hocko

mm/mmap.c unsigned long do_mmap(struct file *file, unsigned long addr, · line 1377

Quoted 6 lines · Michal Hocko
mm/mmap.c +6 -0
+	if (flags & MAP_FIXED_SAFE) {
+		struct vm_area_struct *vma = find_vma(mm, addr);
+
+		if (vma && vma->vm_start <= addr)
+			return -ENOMEM;
+	}

Could you pick a different error code which cannot also be caused by a an unrelated, possibly temporary condition? Maybe EBUSY or EEXIST?

This would definitely help with application-based randomization of mappings, and there, actual ENOMEM and this error would have to be handled differently.

Thanks,
Florian

#10 lore raw

Re: [RFC PATCH 1/2] mm: introduce MAP_FIXED_SAFE

In reply to Michal Hocko
Quoted 78 lines · Michal Hocko

From: Michal Hocko <[email protected]>

MAP_FIXED is used quite often to enforce mapping at the particular range. The main problem of this flag is, however, that it is inherently dangerous because it unmaps existing mappings covered by the requested range. This can cause silent memory corruptions. Some of them even with serious security implications. While the current semantic might be really desiderable in many cases there are others which would want to enforce the given range but rather see a failure than a silent memory corruption on a clashing range. Please note that there is no guarantee that a given range is obeyed by the mmap even when it is free - e.g. arch specific code is allowed to apply an alignment.

Introduce a new MAP_FIXED_SAFE flag for mmap to achieve this behavior. It has the same semantic as MAP_FIXED wrt. the given address request with a single exception that it fails with ENOMEM if the requested address is already covered by an existing mapping. We still do rely on get_unmaped_area to handle all the arch specific MAP_FIXED treatment and check for a conflicting vma after it returns.

[set MAP_FIXED before round_hint_to_min as per Khalid Aziz]
---
 arch/alpha/include/uapi/asm/mman.h   |  2 ++
 arch/mips/include/uapi/asm/mman.h    |  2 ++
 arch/parisc/include/uapi/asm/mman.h  |  2 ++
 arch/powerpc/include/uapi/asm/mman.h |  1 +
 arch/sparc/include/uapi/asm/mman.h   |  1 +
 arch/tile/include/uapi/asm/mman.h    |  1 +
 arch/xtensa/include/uapi/asm/mman.h  |  2 ++
 include/uapi/asm-generic/mman.h      |  1 +
 mm/mmap.c                            | 11 +++++++++++
 9 files changed, 23 insertions(+)
arch/alpha/include/uapi/asm/mman.h +2 -0
index 3b26cc62dadb..0e5724e4b4ad 100644
@@ -31,6 +31,8 @@
 #define MAP_STACK	0x80000		/* give out an address that is best suited for process/thread stacks */
 #define MAP_HUGETLB	0x100000	/* create a huge page mapping */
 
+#define MAP_FIXED_SAFE	0x200000	/* MAP_FIXED which doesn't unmap underlying mapping */
+
 #define MS_ASYNC	1		/* sync memory asynchronously */
 #define MS_SYNC		2		/* synchronous memory sync */
 #define MS_INVALIDATE	4		/* invalidate the caches */
arch/mips/include/uapi/asm/mman.h +2 -0
index da3216007fe0..fc5e61ef9fd4 100644
@@ -49,6 +49,8 @@
 #define MAP_STACK	0x40000		/* give out an address that is best suited for process/thread stacks */
 #define MAP_HUGETLB	0x80000		/* create a huge page mapping */
 
+#define MAP_FIXED_SAFE	0x100000	/* MAP_FIXED which doesn't unmap underlying mapping */
+
 /*
  * Flags for msync
  */
arch/parisc/include/uapi/asm/mman.h +2 -0
index cc9ba1d34779..c926487472fa 100644
@@ -25,6 +25,8 @@
 #define MAP_STACK	0x40000		/* give out an address that is best suited for process/thread stacks */
 #define MAP_HUGETLB	0x80000		/* create a huge page mapping */
 
+#define MAP_FIXED_SAFE	0x100000	/* MAP_FIXED which doesn't unmap underlying mapping */
+
 #define MS_SYNC		1		/* synchronous memory sync */
 #define MS_ASYNC	2		/* sync memory asynchronously */
 #define MS_INVALIDATE	4		/* invalidate the caches */
arch/powerpc/include/uapi/asm/mman.h +0 -0
index 03c06ba7464f..d97342ca25b1 100644

arch/powerpc/include/uapi/asm/mman.h line 31

Quoted 4 lines
arch/powerpc/include/uapi/asm/mman.h +1 -0
@@ -28,5 +28,6 @@
 #define MAP_NONBLOCK	0x10000		/* do not block on IO */
 #define MAP_STACK	0x20000		/* give out an address that is best suited for process/thread stacks */
 #define MAP_HUGETLB	0x40000		/* create a huge page mapping */
+#define MAP_FIXED_SAFE	0x800000	/* MAP_FIXED which doesn't unmap underlying mapping */

Hi Michal,

1. The powerpc change, above, has one too many zeroes. It should be 0x80000, not 0x800000.

2. For the one-line comments, if you phrase them like this:

/* Like MAP_FIXED, except that it doesn't unmap pre-existing mappings */

...I think that would be a little clearer.

more below:

Quoted 62 lines
arch/powerpc/include/uapi/asm/mman.h +0 -0
 #endif /* _UAPI_ASM_POWERPC_MMAN_H */
arch/sparc/include/uapi/asm/mman.h +1 -0
index 9765896ecb2c..7b00477a7f9a 100644
@@ -23,6 +23,7 @@
 #define MAP_NONBLOCK	0x10000		/* do not block on IO */
 #define MAP_STACK	0x20000		/* give out an address that is best suited for process/thread stacks */
 #define MAP_HUGETLB	0x40000		/* create a huge page mapping */
+#define MAP_FIXED_SAFE	0x80000		/* MAP_FIXED which doesn't unmap underlying mapping */
 
 
 #endif /* _UAPI__SPARC_MMAN_H__ */
arch/tile/include/uapi/asm/mman.h +1 -0
index 63ee13faf17d..d5d58d2dc95e 100644
@@ -29,6 +29,7 @@
 #define MAP_DENYWRITE	0x0800		/* ETXTBSY */
 #define MAP_EXECUTABLE	0x1000		/* mark it as an executable */
 #define MAP_HUGETLB	0x4000		/* create a huge page mapping */
+#define MAP_FIXED_SAFE	0x8000		/* MAP_FIXED which doesn't unmap underlying mapping */
 
 
 /*
arch/xtensa/include/uapi/asm/mman.h +2 -0
index b15b278aa314..d665bd8b7cbd 100644
@@ -55,6 +55,7 @@
 #define MAP_NONBLOCK	0x20000		/* do not block on IO */
 #define MAP_STACK	0x40000		/* give out an address that is best suited for process/thread stacks */
 #define MAP_HUGETLB	0x80000		/* create a huge page mapping */
+#define MAP_FIXED_SAFE	0x100000	/* MAP_FIXED which doesn't unmap underlying mapping */
 #ifdef CONFIG_MMAP_ALLOW_UNINITIALIZED
 # define MAP_UNINITIALIZED 0x4000000	/* For anonymous mmap, memory could be
 					 * uninitialized */
@@ -62,6 +63,7 @@
 # define MAP_UNINITIALIZED 0x0		/* Don't support this flag */
 #endif
 
+
 /*
  * Flags for msync
  */
include/uapi/asm-generic/mman.h +1 -0
index 7162cd4cca73..64c46047fbd3 100644
@@ -12,6 +12,7 @@
 #define MAP_NONBLOCK	0x10000		/* do not block on IO */
 #define MAP_STACK	0x20000		/* give out an address that is best suited for process/thread stacks */
 #define MAP_HUGETLB	0x40000		/* create a huge page mapping */
+#define MAP_FIXED_SAFE	0x80000		/* MAP_FIXED which doesn't unmap underlying mapping */
 
 /* Bits [26:31] are reserved, see mman-common.h for MAP_HUGETLB usage */
 
mm/mmap.c +0 -0
index 680506faceae..89af0b5839a5 100644

mm/mmap.c unsigned long do_mmap(struct file *file, unsigned long addr, · line 1347

Quoted 6 lines
mm/mmap.c +3 -0
@@ -1342,6 +1342,10 @@ unsigned long do_mmap(struct file *file, unsigned long addr,
 		if (!(file && path_noexec(&file->f_path)))
 			prot |= PROT_EXEC;
 
+	/* force arch specific MAP_FIXED handling in get_unmapped_area */
+	if (flags & MAP_FIXED_SAFE)
+		flags |= MAP_FIXED;

Hooking in at this point is a nice way to solve the problem. :)

For the naming and implementation, I see a couple of things that might improve it slightly:

a) Change MAP_FIXED_SAFE to MAP_NO_CLOBBER (as per Kees' idea), but keep the new flag independent, by omitting the above two lines. Instead of forcing MAP_FIXED as a result of the new flag, you could simply fail to take any action on MAP_NO_CLOBBER *unless* MAP_FIXED is set.

This is a bit easier to explain and reason about, as compared to a flag that auto-sets another flag. I like this approach best.

or

b) Change MAP_FIXED_SAFE to MAP_FIXED_NO_CLOBBER (also a variation on Kees' name idea, but a little longer, a bit uglier, and clearer), and leave the implementation the same.

thanks,
John Hubbard

Quoted 19 lines
mm/mmap.c +8 -0
+
 	if (!(flags & MAP_FIXED))
 		addr = round_hint_to_min(addr);
 
@@ -1365,6 +1369,13 @@ unsigned long do_mmap(struct file *file, unsigned long addr,
 	if (offset_in_page(addr))
 		return addr;
 
+	if (flags & MAP_FIXED_SAFE) {
+		struct vm_area_struct *vma = find_vma(mm, addr);
+
+		if (vma && vma->vm_start <= addr)
+			return -ENOMEM;
+	}
+
 	if (prot == PROT_EXEC) {
 		pkey = execute_only_pkey(mm);
 		if (pkey < 0)
#15 lore raw

Re: [RFC PATCH 0/2] mm: introduce MAP_FIXED_SAFE

In reply to Michal Hocko
Quoted 32 lines · Michal Hocko

[Ups, managed to screw the subject - fix it]

Quoted 29 lines · Michal Hocko

Hi,
this has started as a follow up discussion [1][2] resulting in the runtime failure caused by hardening patch [3] which removes MAP_FIXED from the elf loader because MAP_FIXED is inherently dangerous as it might silently clobber and existing underlying mapping (e.g. stack). The reason for the failure is that some architectures enforce an alignment for the given address hint without MAP_FIXED used (e.g. for shared or file backed mappings).

One way around this would be excluding those archs which do alignment tricks from the hardening [4]. The patch is really trivial but it has been objected, rightfully so, that this screams for a more generic solution. We basically want a non-destructive MAP_FIXED.

The first patch introduced MAP_FIXED_SAFE which enforces the given address but unlike MAP_FIXED it fails with ENOMEM if the given range conflicts with an existing one. The flag is introduced as a completely new flag rather than a MAP_FIXED extension because of the backward compatibility. We really want a never-clobber semantic even on older kernels which do not recognize the flag. Unfortunately mmap sucks wrt. flags evaluation because we do not EINVAL on unknown flags. On those kernels we would simply use the traditional hint based semantic so the caller can still get a different address (which sucks) but at least not silently corrupt an existing mapping. I do not see a good way around that. Except we won't export expose the new semantic to the userspace at all. It seems there are users who would like to have something like that [5], though. Atomic address range probing in the multithreaded programs sounds like an interesting thing to me as well, although I do not have any specific usecase in mind.

Hi Michal,

Quoted 1 line

From looking at the patchset, it seems to me that the new MAP_FIXED_SAFE

(or whatever it ends up being named) *would* be passed through from user space. When you say that "we won't export expose the new semantic to the userspace at all", do you mean that glibc won't add it? Or is there something I'm missing, that prevents that flag from getting from the syscall, to do_mmap()?

On the usage: there are cases in user space that could probably make good use of a no-clobber hint to MAP_FIXED. The user space code that surrounds HMM (speaking loosely there--it's really any user space code that manages a unified memory address space, across devices) often ends up using MAP_FIXED, but MAP_FIXED crams several features into one flag: an exact address, an "atomic" switch to the new mapping, and unmapping the old mappings. That's pretty overloaded, so being able to split it up a bit, by removing one of those features, seems useful.

thanks,
John Hubbard

Quoted 35 lines
Quoted 34 lines

The second patch simply replaces MAP_FIXED use in elf loader by MAP_FIXED_SAFE. I believe other places which rely on MAP_FIXED should follow. Actually real MAP_FIXED usages should be docummented properly and they should be more of an exception.

Does anybody see any fundamental reasons why this is a wrong approach?

Diffstat says

 arch/alpha/include/uapi/asm/mman.h   |  2 ++
 arch/metag/kernel/process.c          |  6 +++++-
 arch/mips/include/uapi/asm/mman.h    |  2 ++
 arch/parisc/include/uapi/asm/mman.h  |  2 ++
 arch/powerpc/include/uapi/asm/mman.h |  1 +
 arch/sparc/include/uapi/asm/mman.h   |  1 +
 arch/tile/include/uapi/asm/mman.h    |  1 +
 arch/xtensa/include/uapi/asm/mman.h  |  2 ++
 fs/binfmt_elf.c                      | 12 ++++++++----
 include/uapi/asm-generic/mman.h      |  1 +
 mm/mmap.c                            | 11 +++++++++++
 11 files changed, 36 insertions(+), 5 deletions(-)

[1] http://lkml.kernel.org/r/[email protected]
[2] http://lkml.kernel.org/r/[email protected]
[3] http://lkml.kernel.org/r/20171023082608.6167-1-mhocko-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org
[4] http://lkml.kernel.org/r/20171113094203.aofz2e7kueitk55y-2MMpYkNvuYDjFM9bn6wA6Q@public.gmane.org
[5] http://lkml.kernel.org/r/87efp1w7vy.fsf-W0DJWXSxmBNbyGPkN3NxC2scP1bn1w/[email protected]

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected] For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#3 lore raw

Re: [RFC PATCH 1/2] mm: introduce MAP_FIXED_SAFE

In reply to Kees Cook
Quoted 21 lines · Kees Cook
Quoted 13 lines · Michal Hocko

From: Michal Hocko <mhocko-IBi9RG/[email protected]>

MAP_FIXED is used quite often to enforce mapping at the particular range. The main problem of this flag is, however, that it is inherently dangerous because it unmaps existing mappings covered by the requested range. This can cause silent memory corruptions. Some of them even with serious security implications. While the current semantic might be really desiderable in many cases there are others which would want to enforce the given range but rather see a failure than a silent memory corruption on a clashing range. Please note that there is no guarantee that a given range is obeyed by the mmap even when it is free - e.g. arch specific code is allowed to apply an alignment.

Quoted 6 lines

Introduce a new MAP_FIXED_SAFE flag for mmap to achieve this behavior. It has the same semantic as MAP_FIXED wrt. the given address request with a single exception that it fails with ENOMEM if the requested address is already covered by an existing mapping. We still do rely on get_unmaped_area to handle all the arch specific MAP_FIXED treatment and check for a conflicting vma after it returns.

Quoted 6 lines

I like this much more than special-casing the ELF loader. It is an unusual property that MAP_FIXED does _two_ things, so I like having this split out.

Bikeshedding: maybe call this MAP_NO_CLOBBER? It's a modifier to MAP_FIXED, really...

Way back when, I proposed a new flag called MAP_FIXED_WEAK. I was dissuaded from it when userspace people said it was just as easy for them to provide the address hint, then run fixups on their data if the address they were assigned wasn't the one they asked for.

The real problem is that MAP_FIXED should have been called MAP_FORCE.

So ... do we really have users that want failure instead of success at a different address? And if so, is it really a hardship for them to make a call to unmap on success-at-the-wrong-address?

#4 lore raw

Re: [RFC PATCH 1/2] mm: introduce MAP_FIXED_SAFE

Quoted 22 lines · Matthew Wilcox
Quoted 21 lines · Kees Cook
Quoted 19 lines · Michal Hocko

From: Michal Hocko <[email protected]>

MAP_FIXED is used quite often to enforce mapping at the particular range. The main problem of this flag is, however, that it is inherently dangerous because it unmaps existing mappings covered by the requested range. This can cause silent memory corruptions. Some of them even with serious security implications. While the current semantic might be really desiderable in many cases there are others which would want to enforce the given range but rather see a failure than a silent memory corruption on a clashing range. Please note that there is no guarantee that a given range is obeyed by the mmap even when it is free - e.g. arch specific code is allowed to apply an alignment.

Introduce a new MAP_FIXED_SAFE flag for mmap to achieve this behavior. It has the same semantic as MAP_FIXED wrt. the given address request with a single exception that it fails with ENOMEM if the requested address is already covered by an existing mapping. We still do rely on get_unmaped_area to handle all the arch specific MAP_FIXED treatment and check for a conflicting vma after it returns.

Quoted 6 lines
Quoted 6 lines

I like this much more than special-casing the ELF loader. It is an unusual property that MAP_FIXED does _two_ things, so I like having this split out.

Bikeshedding: maybe call this MAP_NO_CLOBBER? It's a modifier to MAP_FIXED, really...

Unfortunatelly doing this as an extension wouldn't work due to backward compatibility issues. See [1]

Quoted 9 lines

Way back when, I proposed a new flag called MAP_FIXED_WEAK. I was dissuaded from it when userspace people said it was just as easy for them to provide the address hint, then run fixups on their data if the address they were assigned wasn't the one they asked for.

The real problem is that MAP_FIXED should have been called MAP_FORCE.

So ... do we really have users that want failure instead of success at a different address?

I am not really sure but Michael has pointed out to jemalloc [2] which could probably use it.

Quoted 2 lines

And if so, is it really a hardship for them to make a call to unmap on success-at-the-wrong-address?

How do you do something like that safely in a multithreaded environment? You do not have any safe way to do atomic probe of a memory range.

As I've said, I do not insist on exporting this functionality to the userspace. I can make it an internal flag (outside of the map type) and use it solely in the kernel but considering how MAP_FIXED is tricky I wouldn't be surprised if the userspace can find a use for this. The main question is, are there any downsides to do so?

[1] http://lkml.kernel.org/r/[email protected]
[2] http://lkml.kernel.org/r/[email protected]

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#6 lore raw

Re: [RFC PATCH 1/2] mm: introduce MAP_FIXED_SAFE

In reply to Florian Weimer
Quoted 10 lines · Florian Weimer

mm/mmap.c unsigned long do_mmap(struct file *file, unsigned long addr, · line 1377

Quoted 6 lines · Michal Hocko
mm/mmap.c +6 -0
+	if (flags & MAP_FIXED_SAFE) {
+		struct vm_area_struct *vma = find_vma(mm, addr);
+
+		if (vma && vma->vm_start <= addr)
+			return -ENOMEM;
+	}

Could you pick a different error code which cannot also be caused by a an unrelated, possibly temporary condition? Maybe EBUSY or EEXIST?

Hmm, none of those are described in the man page. I am usually very careful to not add new and potentially unexpected error codes but it is true that a new flag should warrant a new error code. I am not sure which one is more appropriate though. EBUSY suggests that retrying might help which is true only if some other party unmaps the range. So EEXIST would sound more natural.

Quoted 3 lines

This would definitely help with application-based randomization of mappings, and there, actual ENOMEM and this error would have to be handled differently.

I see. Could you be more specific about the usecase you have in mind? I would incorporate it into the patch description.

Signature · 2 lines
Michal Hocko
SUSE Labs
#11 lore raw

Re: [RFC PATCH 1/2] mm: introduce MAP_FIXED_SAFE

In reply to John Hubbard
Quoted 1 line · John Hubbard

On 11/16/2017 02:18 AM, Michal Hocko wrote:

[...]

Quoted 13 lines

arch/powerpc/include/uapi/asm/mman.h line 31

Quoted 9 lines
arch/powerpc/include/uapi/asm/mman.h +1 -0
index 03c06ba7464f..d97342ca25b1 100644
@@ -28,5 +28,6 @@
 #define MAP_NONBLOCK	0x10000		/* do not block on IO */
 #define MAP_STACK	0x20000		/* give out an address that is best suited for process/thread stacks */
 #define MAP_HUGETLB	0x40000		/* create a huge page mapping */
+#define MAP_FIXED_SAFE	0x800000	/* MAP_FIXED which doesn't unmap underlying mapping */

Hi Michal,

Quoted 2 lines

1. The powerpc change, above, has one too many zeroes. It should be 0x80000, not 0x800000.

OK, I will fix it. It shouldn't matter much, because we only care about non-clashing address but I agree that we should consume them from bottom bits.

Quoted 5 lines

2. For the one-line comments, if you phrase them like this:

/* Like MAP_FIXED, except that it doesn't unmap pre-existing mappings */

...I think that would be a little clearer.

I do not have any strong preference here.
[...]

Quoted 17 lines

mm/mmap.c unsigned long do_mmap(struct file *file, unsigned long addr, · line 1347

Quoted 11 lines
mm/mmap.c +3 -0
index 680506faceae..89af0b5839a5 100644
@@ -1342,6 +1342,10 @@ unsigned long do_mmap(struct file *file, unsigned long addr,
 		if (!(file && path_noexec(&file->f_path)))
 			prot |= PROT_EXEC;
 
+	/* force arch specific MAP_FIXED handling in get_unmapped_area */
+	if (flags & MAP_FIXED_SAFE)
+		flags |= MAP_FIXED;

Hooking in at this point is a nice way to solve the problem. :)

For the naming and implementation, I see a couple of things that might improve it slightly:

Quoted 7 lines

a) Change MAP_FIXED_SAFE to MAP_NO_CLOBBER (as per Kees' idea), but keep the new flag independent, by omitting the above two lines. Instead of forcing MAP_FIXED as a result of the new flag, you could simply fail to take any action on MAP_NO_CLOBBER *unless* MAP_FIXED is set.

This is a bit easier to explain and reason about, as compared to a flag that auto-sets another flag. I like this approach best.

As I've exaplained in other email I do not think we can make this a modifier.

Quoted 5 lines

or

b) Change MAP_FIXED_SAFE to MAP_FIXED_NO_CLOBBER (also a variation on Kees' name idea, but a little longer, a bit uglier, and clearer), and leave the implementation the same.

I do not have a _strong_ preference on the name itself. But I think that _SAFE reflects the behavior slightly better because _NO_CLOBBER is not very specific _when_ and _what_ we do not clobber while _SAFE is clear on that it doesn't perform any unsafe operations.

But if the majority think that _NO_CLOBBER is better i will change it.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#16 lore raw

Re: [RFC PATCH 0/2] mm: introduce MAP_FIXED_SAFE

In reply to John Hubbard
Quoted 34 lines · John Hubbard
Quoted 32 lines · Michal Hocko

[Ups, managed to screw the subject - fix it]

Quoted 29 lines · Michal Hocko

Hi,
this has started as a follow up discussion [1][2] resulting in the runtime failure caused by hardening patch [3] which removes MAP_FIXED from the elf loader because MAP_FIXED is inherently dangerous as it might silently clobber and existing underlying mapping (e.g. stack). The reason for the failure is that some architectures enforce an alignment for the given address hint without MAP_FIXED used (e.g. for shared or file backed mappings).

One way around this would be excluding those archs which do alignment tricks from the hardening [4]. The patch is really trivial but it has been objected, rightfully so, that this screams for a more generic solution. We basically want a non-destructive MAP_FIXED.

The first patch introduced MAP_FIXED_SAFE which enforces the given address but unlike MAP_FIXED it fails with ENOMEM if the given range conflicts with an existing one. The flag is introduced as a completely new flag rather than a MAP_FIXED extension because of the backward compatibility. We really want a never-clobber semantic even on older kernels which do not recognize the flag. Unfortunately mmap sucks wrt. flags evaluation because we do not EINVAL on unknown flags. On those kernels we would simply use the traditional hint based semantic so the caller can still get a different address (which sucks) but at least not silently corrupt an existing mapping. I do not see a good way around that. Except we won't export expose the new semantic to the userspace at all. It seems there are users who would like to have something like that [5], though. Atomic address range probing in the multithreaded programs sounds like an interesting thing to me as well, although I do not have any specific usecase in mind.

Quoted 8 lines

Hi Michal,

From looking at the patchset, it seems to me that the new MAP_FIXED_SAFE (or whatever it ends up being named) *would* be passed through from user space. When you say that "we won't export expose the new semantic to the userspace at all", do you mean that glibc won't add it? Or is there something I'm missing, that prevents that flag from getting from the syscall, to do_mmap()?

I meant that I could make it an internal flag outside of the map_type space. So the userspace will not be able to use it.

Quoted 9 lines

On the usage: there are cases in user space that could probably make good use of a no-clobber hint to MAP_FIXED. The user space code that surrounds HMM (speaking loosely there--it's really any user space code that manages a unified memory address space, across devices) often ends up using MAP_FIXED, but MAP_FIXED crams several features into one flag: an exact address, an "atomic" switch to the new mapping, and unmapping the old mappings. That's pretty overloaded, so being able to split it up a bit, by removing one of those features, seems useful.

Yes, atomic address range probing sounds useful. I cannot comment on HMM usage but if you have any more specific I would welcome any links to add them to the changelog.

Signature · 2 lines
Michal Hocko
SUSE Labs
#7 lore raw

Re: [RFC PATCH 1/2] mm: introduce MAP_FIXED_SAFE

In reply to Michal Hocko
Quoted 12 lines · Michal Hocko
Quoted 10 lines · Florian Weimer

mm/mmap.c unsigned long do_mmap(struct file *file, unsigned long addr, · line 1377

Quoted 6 lines · Michal Hocko
mm/mmap.c +6 -0
+	if (flags & MAP_FIXED_SAFE) {
+		struct vm_area_struct *vma = find_vma(mm, addr);
+
+		if (vma && vma->vm_start <= addr)
+			return -ENOMEM;
+	}

Could you pick a different error code which cannot also be caused by a an unrelated, possibly temporary condition? Maybe EBUSY or EEXIST?

Quoted 2 lines

Hmm, none of those are described in the man page. I am usually very careful to not add new and potentially unexpected error codes but it is

I think this is a bad idea. It leads to bizarre behavior, like open failing with EOVERFLOW with certain namespace configurations (which have nothing to do with file sizes).

Most of the manual pages are incomplete regarding error codes, and with seccomp filters and security modules, what error codes you actually get is anyone's guess.

Quoted 4 lines

true that a new flag should warrant a new error code. I am not sure which one is more appropriate though. EBUSY suggests that retrying might help which is true only if some other party unmaps the range. So EEXIST would sound more natural.

Sure, EEXIST is completely fine.

Quoted 6 lines
Quoted 3 lines

This would definitely help with application-based randomization of mappings, and there, actual ENOMEM and this error would have to be handled differently.

I see. Could you be more specific about the usecase you have in mind? I would incorporate it into the patch description.

glibc ld.so currently maps DSOs without hints. This means that the kernel will map right next to each other, and the offsets between them a completely predictable. We would like to change that and supply a random address in a window of the address space. If there is a conflict, we do not want the kernel to pick a non-random address. Instead, we would try again with a random address.

Thanks,
Florian

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#8 lore raw

Re: [RFC PATCH 1/2] mm: introduce MAP_FIXED_SAFE

Quoted 13 lines · Florian Weimer
Quoted 12 lines · Michal Hocko
Quoted 10 lines · Florian Weimer

mm/mmap.c unsigned long do_mmap(struct file *file, unsigned long addr, · line 1377

Quoted 6 lines · Michal Hocko
mm/mmap.c +6 -0
+	if (flags & MAP_FIXED_SAFE) {
+		struct vm_area_struct *vma = find_vma(mm, addr);
+
+		if (vma && vma->vm_start <= addr)
+			return -ENOMEM;
+	}

Could you pick a different error code which cannot also be caused by a an unrelated, possibly temporary condition? Maybe EBUSY or EEXIST?

Quoted 6 lines
Quoted 2 lines

Hmm, none of those are described in the man page. I am usually very careful to not add new and potentially unexpected error codes but it is

I think this is a bad idea. It leads to bizarre behavior, like open failing with EOVERFLOW with certain namespace configurations (which have nothing to do with file sizes).

Ohh, I agree but breaking userspace is, you know, no-no. And an unexpected error codes can break things terribly.

Quoted 10 lines

Most of the manual pages are incomplete regarding error codes, and with seccomp filters and security modules, what error codes you actually get is anyone's guess.

Quoted 4 lines

true that a new flag should warrant a new error code. I am not sure which one is more appropriate though. EBUSY suggests that retrying might help which is true only if some other party unmaps the range. So EEXIST would sound more natural.

Sure, EEXIST is completely fine.

OK, I will use it.

Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

This would definitely help with application-based randomization of mappings, and there, actual ENOMEM and this error would have to be handled differently.

I see. Could you be more specific about the usecase you have in mind? I would incorporate it into the patch description.

Quoted 6 lines

glibc ld.so currently maps DSOs without hints. This means that the kernel will map right next to each other, and the offsets between them a completely predictable. We would like to change that and supply a random address in a window of the address space. If there is a conflict, we do not want the kernel to pick a non-random address. Instead, we would try again with a random address.

This makes sense to me. Thanks, I will add it to the cover letter.

Signature · 2 lines
Michal Hocko
SUSE Labs
#9 lore raw

Re: [RFC PATCH 1/2] mm: introduce MAP_FIXED_SAFE

Quoted 17 lines · Michal Hocko
Quoted 16 lines · Florian Weimer
Quoted 14 lines · Michal Hocko
Quoted 10 lines · Florian Weimer

mm/mmap.c unsigned long do_mmap(struct file *file, unsigned long addr, · line 1377

Quoted 6 lines · Michal Hocko
mm/mmap.c +6 -0
+	if (flags & MAP_FIXED_SAFE) {
+		struct vm_area_struct *vma = find_vma(mm, addr);
+
+		if (vma && vma->vm_start <= addr)
+			return -ENOMEM;
+	}

Could you pick a different error code which cannot also be caused by a an unrelated, possibly temporary condition? Maybe EBUSY or EEXIST?

Hmm, none of those are described in the man page. I am usually very careful to not add new and potentially unexpected error codes but it is

Quoted 6 lines
Quoted 3 lines

I think this is a bad idea. It leads to bizarre behavior, like open failing with EOVERFLOW with certain namespace configurations (which have nothing to do with file sizes).

Ohh, I agree but breaking userspace is, you know, no-no. And an unexpected error codes can break things terribly.

On the glibc side, we see a lot of changes in error codes depending on kernel version, build and run-time configuration. It never occurred to me that you guys think the precise error code is part of the userspace ABI. Personally, I even assume that failure itself can disappear at any time (evidence: the f* functions which accept O_PATH in their non-*at variants).

Thanks,
Florian

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#17 lore raw

Re: [RFC PATCH 0/2] mm: introduce MAP_FIXED_SAFE

In reply to Michal Hocko
Quoted 57 lines · Michal Hocko
Quoted 34 lines · John Hubbard
Quoted 32 lines · Michal Hocko

[Ups, managed to screw the subject - fix it]

Quoted 29 lines · Michal Hocko

Hi,
this has started as a follow up discussion [1][2] resulting in the runtime failure caused by hardening patch [3] which removes MAP_FIXED from the elf loader because MAP_FIXED is inherently dangerous as it might silently clobber and existing underlying mapping (e.g. stack). The reason for the failure is that some architectures enforce an alignment for the given address hint without MAP_FIXED used (e.g. for shared or file backed mappings).

One way around this would be excluding those archs which do alignment tricks from the hardening [4]. The patch is really trivial but it has been objected, rightfully so, that this screams for a more generic solution. We basically want a non-destructive MAP_FIXED.

The first patch introduced MAP_FIXED_SAFE which enforces the given address but unlike MAP_FIXED it fails with ENOMEM if the given range conflicts with an existing one. The flag is introduced as a completely new flag rather than a MAP_FIXED extension because of the backward compatibility. We really want a never-clobber semantic even on older kernels which do not recognize the flag. Unfortunately mmap sucks wrt. flags evaluation because we do not EINVAL on unknown flags. On those kernels we would simply use the traditional hint based semantic so the caller can still get a different address (which sucks) but at least not silently corrupt an existing mapping. I do not see a good way around that. Except we won't export expose the new semantic to the userspace at all. It seems there are users who would like to have something like that [5], though. Atomic address range probing in the multithreaded programs sounds like an interesting thing to me as well, although I do not have any specific usecase in mind.

Quoted 8 lines

Hi Michal,

From looking at the patchset, it seems to me that the new MAP_FIXED_SAFE (or whatever it ends up being named) *would* be passed through from user space. When you say that "we won't export expose the new semantic to the userspace at all", do you mean that glibc won't add it? Or is there something I'm missing, that prevents that flag from getting from the syscall, to do_mmap()?

I meant that I could make it an internal flag outside of the map_type space. So the userspace will not be able to use it.

Quoted 9 lines

On the usage: there are cases in user space that could probably make good use of a no-clobber hint to MAP_FIXED. The user space code that surrounds HMM (speaking loosely there--it's really any user space code that manages a unified memory address space, across devices) often ends up using MAP_FIXED, but MAP_FIXED crams several features into one flag: an exact address, an "atomic" switch to the new mapping, and unmapping the old mappings. That's pretty overloaded, so being able to split it up a bit, by removing one of those features, seems useful.

Quoted 4 lines

Yes, atomic address range probing sounds useful. I cannot comment on HMM usage but if you have any more specific I would welcome any links to add them to the changelog.

Hi Michal,

Yes, it really is useful for user space. I'll use CUDA as an example, but I think anything that enforces a uniform virtual addressing scheme across CPUs and devices, probably has to do something eerily similar. CUDA does this:

a) Searches /proc/<pid>/maps for a "suitable" region of available VA space. "Suitable" generally means it has to have a base address within a certain limited range (a particular device model might have odd limitations, for example), it has to be large enough, and alignment has to be large enough (again, various devices may have constraints that lead us to do this).

This is of course subject to races with other threads in the process.

Let's say it finds a region starting at va.

b) Next it does:

    p = mmap(va, ...) 

*without* setting MAP_FIXED, of course (so va is just a hint), to attempt to safely reserve that region. If p != va, then in most cases, this is a failure (almost certainly due to another thread getting a mapping from that region before we did), and so this layer now has to call munmap(), before returning a "failure: retry" to upper layers.

IMPROVEMENT: --> if instead, we could call this:

p = mmap(va, ... MAP_FIXED_NO_CLOBBER ...)

, then we could skip the munmap() call upon failure. This is a small thing, but it is useful here. (Thanks to Piotr Jaroszynski and Mark Hairgrove for helping me get that detail exactly right, btw.)

c) After that, CUDA suballocates from p, via:

     q = mmap(sub_region_start, ... MAP_FIXED ...)

Interestingly enough, "freeing" is also done via MAP_FIXED, and setting PROT_NONE to the subregion. Anyway, I just included (c) for general interest.

I expect that as we continue working on the open source compute software stack, this new capability will be useful there, too.

Oh, and on the naming, when I described how your implementation worked (without naming it) to Piotr, he said, "oh, something like map-fixed-no-clobber?". So I think my miniature sociology naming data point here can bolster the case ever so slightly for calling it MAP_FIXED_NO_CLOBBER. haha. :)

thanks,
John Hubbard

#18 lore raw

Re: [RFC PATCH 0/2] mm: introduce MAP_FIXED_SAFE

On Tue 21-11-17 17:48:31, John Hubbard wrote:
[...]

Quoted 33 lines

Hi Michal,

Yes, it really is useful for user space. I'll use CUDA as an example, but I think anything that enforces a uniform virtual addressing scheme across CPUs and devices, probably has to do something eerily similar. CUDA does this:

a) Searches /proc/<pid>/maps for a "suitable" region of available VA space. "Suitable" generally means it has to have a base address within a certain limited range (a particular device model might have odd limitations, for example), it has to be large enough, and alignment has to be large enough (again, various devices may have constraints that lead us to do this).

This is of course subject to races with other threads in the process.

Let's say it finds a region starting at va.

b) Next it does:

    p = mmap(va, ...) 

*without* setting MAP_FIXED, of course (so va is just a hint), to attempt to safely reserve that region. If p != va, then in most cases, this is a failure (almost certainly due to another thread getting a mapping from that region before we did), and so this layer now has to call munmap(), before returning a "failure: retry" to upper layers.

IMPROVEMENT: --> if instead, we could call this:

p = mmap(va, ... MAP_FIXED_NO_CLOBBER ...)

, then we could skip the munmap() call upon failure. This is a small thing, but it is useful here. (Thanks to Piotr Jaroszynski and Mark Hairgrove for helping me get that detail exactly right, btw.)

Quoted 6 lines

c) After that, CUDA suballocates from p, via:

     q = mmap(sub_region_start, ... MAP_FIXED ...)

Interestingly enough, "freeing" is also done via MAP_FIXED, and setting PROT_NONE to the subregion. Anyway, I just included (c) for general interest.

OK, I will add this to the changelog. This is basically the "Atomic address range probing in the multithreaded programs" I've had in the cover letter.

Quoted 7 lines

I expect that as we continue working on the open source compute software stack, this new capability will be useful there, too.

Oh, and on the naming, when I described how your implementation worked (without naming it) to Piotr, he said, "oh, something like map-fixed-no-clobber?". So I think my miniature sociology naming data point here can bolster the case ever so slightly for calling it MAP_FIXED_NO_CLOBBER. haha. :)

I will be probably stubborn and go with a shorter name I have currently. I am not very fond-of-very-long-names.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#19 lore raw

Re: [RFC PATCH 0/2] mm: introduce MAP_FIXED_SAFE

Quoted 2 lines · Michal Hocko

I will be probably stubborn and go with a shorter name I have currently. I am not very fond-of-very-long-names.

The short synonym for the last word is "German"

SCNR :P

#20 lore raw

Re: [RFC PATCH 0/2] mm: introduce MAP_FIXED_SAFE

In reply to Michal Hocko

Are there any more concerns? So far the biggest one was the name. The other which suggests a flag as a modifier has been sorted out hopefully. Is there anymore more before we can consider this for merging? Well except for man page update which I will prepare of course. Can we target this to 4.16?

Quoted 70 lines · Michal Hocko

[Ups, managed to screw the subject - fix it]

Quoted 63 lines · Michal Hocko

Hi,
this has started as a follow up discussion [1][2] resulting in the runtime failure caused by hardening patch [3] which removes MAP_FIXED from the elf loader because MAP_FIXED is inherently dangerous as it might silently clobber and existing underlying mapping (e.g. stack). The reason for the failure is that some architectures enforce an alignment for the given address hint without MAP_FIXED used (e.g. for shared or file backed mappings).

One way around this would be excluding those archs which do alignment tricks from the hardening [4]. The patch is really trivial but it has been objected, rightfully so, that this screams for a more generic solution. We basically want a non-destructive MAP_FIXED.

The first patch introduced MAP_FIXED_SAFE which enforces the given address but unlike MAP_FIXED it fails with ENOMEM if the given range conflicts with an existing one. The flag is introduced as a completely new flag rather than a MAP_FIXED extension because of the backward compatibility. We really want a never-clobber semantic even on older kernels which do not recognize the flag. Unfortunately mmap sucks wrt. flags evaluation because we do not EINVAL on unknown flags. On those kernels we would simply use the traditional hint based semantic so the caller can still get a different address (which sucks) but at least not silently corrupt an existing mapping. I do not see a good way around that. Except we won't export expose the new semantic to the userspace at all. It seems there are users who would like to have something like that [5], though. Atomic address range probing in the multithreaded programs sounds like an interesting thing to me as well, although I do not have any specific usecase in mind.

The second patch simply replaces MAP_FIXED use in elf loader by MAP_FIXED_SAFE. I believe other places which rely on MAP_FIXED should follow. Actually real MAP_FIXED usages should be docummented properly and they should be more of an exception.

Does anybody see any fundamental reasons why this is a wrong approach?

Diffstat says

 arch/alpha/include/uapi/asm/mman.h   |  2 ++
 arch/metag/kernel/process.c          |  6 +++++-
 arch/mips/include/uapi/asm/mman.h    |  2 ++
 arch/parisc/include/uapi/asm/mman.h  |  2 ++
 arch/powerpc/include/uapi/asm/mman.h |  1 +
 arch/sparc/include/uapi/asm/mman.h   |  1 +
 arch/tile/include/uapi/asm/mman.h    |  1 +
 arch/xtensa/include/uapi/asm/mman.h  |  2 ++
 fs/binfmt_elf.c                      | 12 ++++++++----
 include/uapi/asm-generic/mman.h      |  1 +
 mm/mmap.c                            | 11 +++++++++++
 11 files changed, 36 insertions(+), 5 deletions(-)

[1] http://lkml.kernel.org/r/[email protected]
[2] http://lkml.kernel.org/r/[email protected]
[3] http://lkml.kernel.org/r/[email protected]
[4] http://lkml.kernel.org/r/[email protected]
[5] http://lkml.kernel.org/r/[email protected]

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Signature · 2 lines
Michal Hocko
SUSE Labs
Signature · 2 lines
Michal Hocko
SUSE Labs
Reviewed-by
#21 lore raw

Re: [RFC PATCH 0/2] mm: introduce MAP_FIXED_SAFE

Quoted 5 lines · Michal Hocko

Are there any more concerns? So far the biggest one was the name. The other which suggests a flag as a modifier has been sorted out hopefully. Is there anymore more before we can consider this for merging? Well except for man page update which I will prepare of course. Can we target this to 4.16?

I do not have concerns with this approach. I prefer a new flag as opposed to a modifier and the name is ok by me.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>