From f758aadd07ee052c9829b7f56e2921f86537d968 Mon Sep 17 00:00:00 2001
From: Konstantin Belousov <kib@FreeBSD.org>
Date: Tue, 18 Jul 2017 20:26:41 +0000
Subject: [PATCH] Convert assertion that only vmspace owner grows the stack,
 into a check blocking grow from other processes accesses.

Debugger may access stack grow area with ptrace(2).  In this case,
real state of the process is to not have the stack grown, which
provides more accurate inspection.  Technical reason to avoid the grow
is to avoid applying wrong process (debugger) stack limit.

This change also has a consequence of making aio workers accesses past
the bottom of stacks into EFAULT, arguably the situation is a
programmers mistake.

Reported by:	jhb
Discussed with:	alc, jhb
Sponsored by:	The FreeBSD Foundation
MFC after:	3 days
---
 sys/vm/vm_map.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/sys/vm/vm_map.c b/sys/vm/vm_map.c
index f6c825c27499..93ba5f21ff0c 100644
--- a/sys/vm/vm_map.c
+++ b/sys/vm/vm_map.c
@@ -3698,7 +3698,8 @@ vm_map_growstack(vm_map_t map, vm_offset_t addr, vm_map_entry_t gap_entry)
 
 	p = curproc;
 	vm = p->p_vmspace;
-	MPASS(map == &p->p_vmspace->vm_map);
+	if (map != &p->p_vmspace->vm_map)
+		return (KERN_FAILURE);
 	MPASS(!map->system_map);
 
 	guard = stack_guard_page * PAGE_SIZE;