Skip to contentSkip to conversation

mm: mm, mmap: do not blow on PROT_NONE MAP_FIXED holes in the stack

PATCH In review Michal Hocko 4 participants 11 messages Jul 5, 2017 – Jul 6, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: mm, mmap: do not blow on PROT_NONE MAP_FIXED holes in the stack

From: Michal Hocko <[email protected]>

"mm: enlarge stack guard gap" has introduced a regression in some rust and Java environments which are trying to implement their own stack guard page. They are punching a new MAP_FIXED mapping inside the existing stack Vma.

This will confuse expand_{downwards,upwards} into thinking that the stack expansion would in fact get us too close to an existing non-stack vma which is a correct behavior wrt. safety. It is a real regression on the other hand. Let's work around the problem by considering PROT_NONE mapping as a part of the stack. This is a gros hack but overflowing to such a mapping would trap anyway an we only can hope that usespace knows what it is doing and handle it propely.

Fixes d4d2d35e6ef9 ("mm: larger stack guard gap, between vmas")
Cc stable

---
Hi,
the original thread [1] has grown quite large and also a bit confusing. At least the rust part should be fixed by this patch. 32b java will probably need something more on top of this. Btw. JNI environments rely on MAP_FIXED PROT_NONE as well they were just lucky to not hit the issue yet I guess.

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

 mm/mmap.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
mm/mmap.c +4 -2
index f60a8bc2869c..2e996cbf4ff3 100644
@@ -2244,7 +2244,8 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address)
 		gap_addr = TASK_SIZE;
 
 	next = vma->vm_next;
-	if (next && next->vm_start < gap_addr) {
+	if (next && next->vm_start < gap_addr &&
+			(next->vm_flags & (VM_WRITE|VM_READ|VM_EXEC))) {
 		if (!(next->vm_flags & VM_GROWSUP))
 			return -ENOMEM;
 		/* Check that both stack segments have the same anon_vma? */
@@ -2325,7 +2326,8 @@ int expand_downwards(struct vm_area_struct *vma,
 	/* Enforce stack_guard_gap */
 	prev = vma->vm_prev;
 	/* Check that both stack segments have the same anon_vma? */
-	if (prev && !(prev->vm_flags & VM_GROWSDOWN)) {
+	if (prev && !(prev->vm_flags & VM_GROWSDOWN) &&
+			(prev->vm_flags & (VM_WRITE|VM_READ|VM_EXEC))) {
 		if (address - prev->vm_end < stack_guard_gap)
 			return -ENOMEM;
 	}
Signature · 7 lines
2.11.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>
#1 lore raw
Quoted 5 lines · Michal Hocko

"mm: enlarge stack guard gap" has introduced a regression in some rust and Java environments which are trying to implement their own stack guard page. They are punching a new MAP_FIXED mapping inside the existing stack Vma.

Hmm. What version is this patch against? It doesn't seem to match my 4.12 tree.

Linus

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

#2 lore raw
Quoted 8 lines · Linus Torvalds
Quoted 5 lines · Michal Hocko

"mm: enlarge stack guard gap" has introduced a regression in some rust and Java environments which are trying to implement their own stack guard page. They are punching a new MAP_FIXED mapping inside the existing stack Vma.

Hmm. What version is this patch against? It doesn't seem to match my 4.12 tree.

Dohh, that was on mmotm which has a clean up by Oleg which reorganizes the code a bit. This is on top of the current master
---

#3 lore raw
Quoted 3 lines · Michal Hocko

Dohh, that was on mmotm which has a clean up by Oleg which reorganizes the code a bit. This is on top of the current master

Oh, ok. I think I know which patch from Oleg you're talking about.

Since I do want that patch too, and since I'd hate to cause unnecessary merge conflicts in this area, how about we just plan on letting your original patch (on top of Oleg's) go through Andrew and the -mm tree? I'll get it that way, and it's not like this is timing-critical.

Having to fix up conflicts in this area would just be annoying, and would be nasty for back-porting too.

Linus

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

#4 lore raw
Quoted 5 lines · Linus Torvalds
Quoted 3 lines · Michal Hocko

Dohh, that was on mmotm which has a clean up by Oleg which reorganizes the code a bit. This is on top of the current master

Quoted 7 lines

Oh, ok. I think I know which patch from Oleg you're talking about.

Since I do want that patch too, and since I'd hate to cause unnecessary merge conflicts in this area, how about we just plan on letting your original patch (on top of Oleg's) go through Andrew and the -mm tree? I'll get it that way, and it's not like this is timing-critical.

That would lead to conflicts when backporting to stable trees though which is quite annoying as well and arguably slightly more annoying than resolving this in mmotm. I can help to rebase Oleg's patch on top of mine which is not a stable material.

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>
author Acked-by
#5 lore raw
Quoted 14 lines · Michal Hocko
Quoted 5 lines · Linus Torvalds
Quoted 3 lines · Michal Hocko

Dohh, that was on mmotm which has a clean up by Oleg which reorganizes the code a bit. This is on top of the current master

Quoted 7 lines

Oh, ok. I think I know which patch from Oleg you're talking about.

Since I do want that patch too, and since I'd hate to cause unnecessary merge conflicts in this area, how about we just plan on letting your original patch (on top of Oleg's) go through Andrew and the -mm tree? I'll get it that way, and it's not like this is timing-critical.

Quoted 4 lines

That would lead to conflicts when backporting to stable trees though which is quite annoying as well and arguably slightly more annoying than resolving this in mmotm. I can help to rebase Oleg's patch on top of mine which is not a stable material.

Here is the rebase of Oleg's patch.
---

#6 lore raw
In reply to Michal Hocko
Quoted 5 lines · Michal Hocko

That would lead to conflicts when backporting to stable trees though which is quite annoying as well and arguably slightly more annoying than resolving this in mmotm. I can help to rebase Oleg's patch on top of mine which is not a stable material.

Ok, fair enough - I was actually expecting that Oleg's patch would just be marked for stable too just to keep differences minimal.

But yes, putting your patch in first and then Oleg's on top means that it works regardless.

Any opinions from others?

Linus

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

#7 lore raw
Quoted 7 lines · Linus Torvalds
Quoted 5 lines · Michal Hocko

That would lead to conflicts when backporting to stable trees though which is quite annoying as well and arguably slightly more annoying than resolving this in mmotm. I can help to rebase Oleg's patch on top of mine which is not a stable material.

Quoted 7 lines

Ok, fair enough - I was actually expecting that Oleg's patch would just be marked for stable too just to keep differences minimal.

But yes, putting your patch in first and then Oleg's on top means that it works regardless.

Any opinions from others?

No pb here, and this one is reasonably easy to backport anyway as the test is easy to locate.

Willy

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

maintainer
#8 lore raw
In reply to Michal Hocko
Quoted 13 lines · On Wed, 5 Jul 2017 20:28:49 +0200 Michal Hocko <[email protected]> wrote:

"mm: enlarge stack guard gap" has introduced a regression in some rust and Java environments which are trying to implement their own stack guard page. They are punching a new MAP_FIXED mapping inside the existing stack Vma.

This will confuse expand_{downwards,upwards} into thinking that the stack expansion would in fact get us too close to an existing non-stack vma which is a correct behavior wrt. safety. It is a real regression on the other hand. Let's work around the problem by considering PROT_NONE mapping as a part of the stack. This is a gros hack but overflowing to such a mapping would trap anyway an we only can hope that usespace knows what it is doing and handle it propely.

Quoted 1 line
Fixes d4d2d35e6ef9 ("mm: larger stack guard gap, between vmas")

That should be 1be7107fbe18, yes?

Quoted 2 lines
#9 lore raw
Quoted 5 lines · Andrew Morton
Quoted 2 lines · On Wed, 5 Jul 2017 20:28:49 +0200 Michal Hocko <[email protected]> wrote:
Quoted 1 line
Fixes d4d2d35e6ef9 ("mm: larger stack guard gap, between vmas")

That should be 1be7107fbe18, yes?

Good catch. I assume the d4d2d35e6ef9 is one of the stable backport commits..

Linus

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

#10 lore raw
In reply to Andrew Morton
Quoted 15 lines · Andrew Morton
Quoted 13 lines · On Wed, 5 Jul 2017 20:28:49 +0200 Michal Hocko <[email protected]> wrote:

"mm: enlarge stack guard gap" has introduced a regression in some rust and Java environments which are trying to implement their own stack guard page. They are punching a new MAP_FIXED mapping inside the existing stack Vma.

This will confuse expand_{downwards,upwards} into thinking that the stack expansion would in fact get us too close to an existing non-stack vma which is a correct behavior wrt. safety. It is a real regression on the other hand. Let's work around the problem by considering PROT_NONE mapping as a part of the stack. This is a gros hack but overflowing to such a mapping would trap anyway an we only can hope that usespace knows what it is doing and handle it propely.

Quoted 3 lines
Quoted 1 line
Fixes d4d2d35e6ef9 ("mm: larger stack guard gap, between vmas")

That should be 1be7107fbe18, yes?

yes. d4d2d35e6ef9 was a cherry-pick into the mmotm git tree. Sorry about that.

Quoted 2 lines
Quoted 2 lines
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>