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.
This commit is contained in:
Siddhesh Poyarekar 2012-05-26 09:48:25 +05:30
parent a82392228a
commit 9c6ea9facb
5 changed files with 111 additions and 16 deletions

View File

@ -1,3 +1,10 @@
2012-05-26 Siddhesh Poyarekar <siddhesh@redhat.com>
[BZ #12416]
* elf/tst-execstack.c: Include stackinfo.h.
(do_test): Adjust test case to ensure that pthread_getattr_np
behaviour remains the same after marking stack executable.
2012-05-25 Joseph Myers <joseph@codesourcery.com>
* sysdeps/unix/sysv/linux/kernel-features.h

26
NEWS
View File

@ -16,19 +16,19 @@ Version 2.16
6884, 6890, 6894, 6895, 6907, 6911, 6959, 7064, 9739, 9902, 10110, 10135,
10140, 10153, 10210, 10254, 10346, 10375, 10545, 10716, 10846, 11174,
11322, 11365, 11451, 11494, 11521, 11677, 11837, 11959, 12047, 12097,
12193, 12297, 12298, 12301, 12340, 12354, 12495, 13058, 13361, 13525,
13526, 13527, 13528, 13529, 13530, 13531, 13532, 13533, 13547, 13551,
13552, 13553, 13555, 13556, 13559, 13563, 13566, 13576, 13583, 13592,
13594, 13613, 13618, 13637, 13656, 13658, 13673, 13691, 13695, 13704,
13705, 13706, 13726, 13738, 13739, 13750, 13758, 13760, 13761, 13775,
13786, 13787, 13792, 13806, 13824, 13840, 13841, 13844, 13846, 13851,
13852, 13854, 13871, 13872, 13873, 13879, 13883, 13884, 13885, 13886,
13892, 13895, 13908, 13910, 13911, 13912, 13913, 13914, 13915, 13916,
13917, 13918, 13919, 13920, 13921, 13922, 13923, 13924, 13926, 13927,
13928, 13938, 13941, 13942, 13954, 13955, 13956, 13963, 13967, 13968,
13970, 13973, 13979, 13983, 13986, 14012, 14027, 14033, 14034, 14036,
14040, 14043, 14044, 14049, 14053, 14055, 14059, 14064, 14080, 14083,
14103, 14104, 14109, 14122, 14123, 14153
12193, 12297, 12298, 12301, 12340, 12354, 12416, 12495, 13058, 13361,
13525, 13526, 13527, 13528, 13529, 13530, 13531, 13532, 13533, 13547,
13551, 13552, 13553, 13555, 13556, 13559, 13563, 13566, 13576, 13583,
13592, 13594, 13613, 13618, 13637, 13656, 13658, 13673, 13691, 13695,
13704, 13705, 13706, 13726, 13738, 13739, 13750, 13758, 13760, 13761,
13775, 13786, 13787, 13792, 13806, 13824, 13840, 13841, 13844, 13846,
13851, 13852, 13854, 13871, 13872, 13873, 13879, 13883, 13884, 13885,
13886, 13892, 13895, 13908, 13910, 13911, 13912, 13913, 13914, 13915,
13916, 13917, 13918, 13919, 13920, 13921, 13922, 13923, 13924, 13926,
13927, 13928, 13938, 13941, 13942, 13954, 13955, 13956, 13963, 13967,
13968, 13970, 13973, 13979, 13983, 13986, 14012, 14027, 14033, 14034,
14036, 14040, 14043, 14044, 14049, 14053, 14055, 14059, 14064, 14080,
14083, 14103, 14104, 14109, 14122, 14123, 14153
* ISO C11 support:

View File

@ -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);

View File

@ -1,3 +1,10 @@
2012-05-26 Siddhesh Poyarekar <siddhesh@redhat.com>
[BZ #12416]
* nptl/pthread_getattr_np.c (pthread_getattr_np): Use
__libc_stack_end rounded to the end of containing page as the
real stack end.
2012-05-25 Rayson Ho <rho@redhat.com>
* sysdeps/unix/sysv/linux/i386/lowlevellock.h: Low-level SystemTap

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2002-2004, 2006, 2007, 2011 Free Software Foundation, Inc.
/* Copyright (C) 2002-2012 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
@ -84,6 +84,18 @@ pthread_getattr_np (thread_id, attr)
ret = errno;
else
{
/* We consider the main process stack to have ended with
the page containing __libc_stack_end. There is stuff below
it in the stack too, like the program arguments, environment
variables and auxv info, but we ignore those pages when
returning size so that the output is consistent when the
stack is marked executable due to a loaded DSO requiring
it. */
void *stack_end = (void *) ((uintptr_t) __libc_stack_end
& -(uintptr_t) GLRO(dl_pagesize));
#if _STACK_GROWS_DOWN
stack_end += GLRO(dl_pagesize);
#endif
/* We need no locking. */
__fsetlocking (fp, FSETLOCKING_BYCALLER);
@ -109,7 +121,7 @@ pthread_getattr_np (thread_id, attr)
{
/* Found the entry. Now we have the info we need. */
iattr->stacksize = rl.rlim_cur;
iattr->stackaddr = (void *) to;
iattr->stackaddr = stack_end;
/* The limit might be too high. */
if ((size_t) iattr->stacksize