[v2] Fix stack memory protection on targets where the stack grows upward

Message ID 6F4B764B-DEC3-4A85-91FD-809EF7A4C0D0@bell.net
State New, archived
Headers

Commit Message

John David Anglin April 17, 2017, 12:13 p.m. UTC
  On 2017-04-16, at 4:06 PM, Andreas Schwab wrote:

> On Apr 16 2017, John David Anglin <dave.anglin@bell.net> wrote:
> 
>> +	  char *new_guard = (char *)(((uintptr_t) pd - guardsize) & ~pagesize_m1);
>> +	  char *old_guard = (char *)(((uintptr_t) pd - pd->guardsize) & ~pagesize_m1);
> 
> The lines are too long.

The long lines are fixed in this version.  Otherwise, unchanged.

Dave
--
John David Anglin	dave.anglin@bell.net
2017-04-17  John David Anglin  <danglin@gcc.gnu.org>

	* nptl/allocatestack.c (allocate_stack): Align old and new guard
	addresses to page boundaries when the stack grows up.
  

Patch

diff --git a/nptl/allocatestack.c b/nptl/allocatestack.c
index e5c5f79a82..595a858861 100644
--- a/nptl/allocatestack.c
+++ b/nptl/allocatestack.c
@@ -647,8 +647,14 @@  allocate_stack (const struct pthread_attr *attr, struct pthread **pdp,
 			prot) != 0)
 	    goto mprot_error;
 #elif _STACK_GROWS_UP
-	  if (mprotect ((char *) pd - pd->guardsize,
-			pd->guardsize - guardsize, prot) != 0)
+	  char *new_guard = (char *)(((uintptr_t) pd - guardsize)
+			             & ~pagesize_m1);
+	  char *old_guard = (char *)(((uintptr_t) pd - pd->guardsize)
+			             & ~pagesize_m1);
+	  /* The guard size difference might be > 0, but once rounded
+	     to the nearest page the size difference might be zero.  */
+	  if (new_guard > old_guard
+	      && mprotect (old_guard, new_guard - old_guard, prot) != 0)
 	    goto mprot_error;
 #endif