about summary refs log tree commit diff
path: root/elf/tst-execstack.c
diff options
context:
space:
mode:
authorSiddhesh Poyarekar <siddhesh@redhat.com>2012-05-26 09:48:25 +0530
committerSiddhesh Poyarekar <siddhesh@redhat.com>2012-05-26 09:48:26 +0530
commit9c6ea9facbba4d430807bd21fa82892d713b1ecd (patch)
treefc116e5e30c8ea5cada22f53d6914fbc33eb586e /elf/tst-execstack.c
parenta82392228a31bb94b8dc4c35a37b04611d19891f (diff)
downloadglibc-9c6ea9facbba4d430807bd21fa82892d713b1ecd.tar.gz
glibc-9c6ea9facbba4d430807bd21fa82892d713b1ecd.tar.xz
glibc-9c6ea9facbba4d430807bd21fa82892d713b1ecd.zip
Fix stack size and address inconsistency due to executable stack
When a stack is marked executable due to loading a DSO that requires
an executable stack, the logic tends to leave out a portion of stack
after the first frame, thus causing a difference in the value returned
by pthread_getattr_np before and after the stack is marked
executable. It ought to be possible to fix this by marking the rest of
the stack as executable too, but in the interest of marking as less of
the stack as executable as possible, the path this fix takes is to
make pthread_getattr_np also look at the first frame as the underflow
end of the stack and compute size and stack top accordingly.

The above happens only for the main process stack. NPTL thread stacks
are not affected by this change.
Diffstat (limited to 'elf/tst-execstack.c')
-rw-r--r--elf/tst-execstack.c71
1 files changed, 70 insertions, 1 deletions
diff --git a/elf/tst-execstack.c b/elf/tst-execstack.c
index 6632e53367..02cc270d80 100644
--- a/elf/tst-execstack.c
+++ b/elf/tst-execstack.c
@@ -7,6 +7,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <error.h>
+#include <stackinfo.h>
 
 static void
 print_maps (void)
@@ -46,7 +47,6 @@ waiter_thread (void *arg)
 }
 #endif
 
-
 static bool allow_execstack = true;
 
 
@@ -107,6 +107,35 @@ do_test (void)
 
   print_maps ();
 
+#if USE_PTHREADS
+  void *old_stack_addr, *new_stack_addr;
+  size_t stack_size;
+  pthread_t me = pthread_self ();
+  pthread_attr_t attr;
+  int ret = 0;
+
+  ret = pthread_getattr_np (me, &attr);
+  if (ret)
+    {
+      printf ("before execstack: pthread_getattr_np returned error: %s\n",
+	      strerror (ret));
+      return 1;
+    }
+
+  ret = pthread_attr_getstack (&attr, &old_stack_addr, &stack_size);
+  if (ret)
+    {
+      printf ("before execstack: pthread_attr_getstack returned error: %s\n",
+	      strerror (ret));
+      return 1;
+    }
+# if _STACK_GROWS_DOWN
+    old_stack_addr += stack_size;
+# else
+    old_stack_addr -= stack_size;
+# endif
+#endif
+
   /* Loading this module should force stacks to become executable.  */
   void *h = dlopen ("tst-execstack-mod.so", RTLD_LAZY);
   if (h == NULL)
@@ -129,6 +158,46 @@ do_test (void)
 
   print_maps ();
 
+#if USE_PTHREADS
+  ret = pthread_getattr_np (me, &attr);
+  if (ret)
+    {
+      printf ("after execstack: pthread_getattr_np returned error: %s\n",
+	      strerror (ret));
+      return 1;
+    }
+
+  ret = pthread_attr_getstack (&attr, &new_stack_addr, &stack_size);
+  if (ret)
+    {
+      printf ("after execstack: pthread_attr_getstack returned error: %s\n",
+	      strerror (ret));
+      return 1;
+    }
+
+# if _STACK_GROWS_DOWN
+    new_stack_addr += stack_size;
+# else
+    new_stack_addr -= stack_size;
+# endif
+
+  /* It is possible that the dlopen'd module may have been mmapped just below
+     the stack.  The stack size is taken as MIN(stack rlimit size, end of last
+     vma) in pthread_getattr_np.  If rlimit is set high enough, it is possible
+     that the size may have changed.  A subsequent call to
+     pthread_attr_getstack returns the size and (bottom - size) as the
+     stacksize and stackaddr respectively.  If the size changes due to the
+     above, then both stacksize and stackaddr can change, but the stack bottom
+     should remain the same, which is computed as stackaddr + stacksize.  */
+  if (old_stack_addr != new_stack_addr)
+    {
+      printf ("Stack end changed, old: %p, new: %p\n",
+	      old_stack_addr, new_stack_addr);
+      return 1;
+    }
+  printf ("Stack address remains the same: %p\n", old_stack_addr);
+#endif
+
   /* Test that growing the stack region gets new executable pages too.  */
   deeper ((void (*) (void)) f);