Skip to content

Commit

Permalink
sysconf: Add _SC_MINSIGSTKSZ/_SC_SIGSTKSZ [BZ #20305]
Browse files Browse the repository at this point in the history
Add _SC_MINSIGSTKSZ for the minimum signal stack size derived from
AT_MINSIGSTKSZ, which is the minimum number of bytes of free stack
space required in order to gurantee successful, non-nested handling
of a single signal whose handler is an empty function, and _SC_SIGSTKSZ
which is the suggested minimum number of bytes of stack space required
for a signal stack.

If AT_MINSIGSTKSZ isn't available, sysconf (_SC_MINSIGSTKSZ) returns
MINSIGSTKSZ.  On Linux/x86 with XSAVE, the signal frame used by kernel
is composed of the following areas and laid out as:

 ------------------------------
 | alignment padding          |
 ------------------------------
 | xsave buffer               |
 ------------------------------
 | fsave header (32-bit only) |
 ------------------------------
 | siginfo + ucontext         |
 ------------------------------

Compute AT_MINSIGSTKSZ value as size of xsave buffer + size of fsave
header (32-bit only) + size of siginfo and ucontext + alignment padding.

If _SC_SIGSTKSZ_SOURCE or _GNU_SOURCE are defined, MINSIGSTKSZ and SIGSTKSZ
are redefined as

/* Default stack size for a signal handler: sysconf (SC_SIGSTKSZ).  */
 # undef SIGSTKSZ
 # define SIGSTKSZ sysconf (_SC_SIGSTKSZ)

/* Minimum stack size for a signal handler: SIGSTKSZ.  */
 # undef MINSIGSTKSZ
 # define MINSIGSTKSZ SIGSTKSZ

Compilation will fail if the source assumes constant MINSIGSTKSZ or
SIGSTKSZ.

The reason for not simply increasing the kernel's MINSIGSTKSZ #define
(apart from the fact that it is rarely used, due to glibc's shadowing
definitions) was that userspace binaries will have baked in the old
value of the constant and may be making assumptions about it.

For example, the type (char [MINSIGSTKSZ]) changes if this #define
changes.  This could be a problem if an newly built library tries to
memcpy() or dump such an object defined by and old binary.
Bounds-checking and the stack sizes passed to things like sigaltstack()
and makecontext() could similarly go wrong.
  • Loading branch information
hjl-tools committed Feb 1, 2021
1 parent 36231be commit 6c57d32
Show file tree
Hide file tree
Showing 23 changed files with 416 additions and 3 deletions.
5 changes: 5 additions & 0 deletions NEWS
Expand Up @@ -33,6 +33,11 @@ Version 2.33

Major new features:

* Add _SC_MINSIGSTKSZ and _SC_SIGSTKSZ. When _SC_SIGSTKSZ_SOURCE or
_GNU_SOURCE are defined, MINSIGSTKSZ and SIGSTKSZ are no longer
constant on Linux. MINSIGSTKSZ is redefined to sysconf(_SC_MINSIGSTKSZ)
and SIGSTKSZ is redefined to sysconf (_SC_SIGSTKSZ).

* The dynamic linker accepts the --list-tunables argument which prints
all the supported tunables. This option is disable if glibc is
configured with tunables disabled (--enable-tunables=no).
Expand Down
8 changes: 7 additions & 1 deletion bits/confname.h
Expand Up @@ -525,8 +525,14 @@ enum

_SC_THREAD_ROBUST_PRIO_INHERIT,
#define _SC_THREAD_ROBUST_PRIO_INHERIT _SC_THREAD_ROBUST_PRIO_INHERIT
_SC_THREAD_ROBUST_PRIO_PROTECT
_SC_THREAD_ROBUST_PRIO_PROTECT,
#define _SC_THREAD_ROBUST_PRIO_PROTECT _SC_THREAD_ROBUST_PRIO_PROTECT

_SC_MINSIGSTKSZ,
#define _SC_MINSIGSTKSZ _SC_MINSIGSTKSZ

_SC_SIGSTKSZ
#define _SC_SIGSTKSZ _SC_SIGSTKSZ
};

/* Values for the NAME argument to `confstr'. */
Expand Down
21 changes: 21 additions & 0 deletions bits/sigstksz.h
@@ -0,0 +1,21 @@
/* Definition of MINSIGSTKSZ. Generic version.
Copyright (C) 2020 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */

#ifndef _SIGNAL_H
# error "Never include <bits/sigstksz.h> directly; use <signal.h> instead."
#endif
5 changes: 5 additions & 0 deletions elf/dl-support.c
Expand Up @@ -142,6 +142,8 @@ void (*_dl_init_static_tls) (struct link_map *) = &_dl_nothread_init_static_tls;

size_t _dl_pagesize = EXEC_PAGESIZE;

size_t _dl_minsigstacksize = CONSTANT_MINSIGSTKSZ;

int _dl_inhibit_cache;

unsigned int _dl_osversion;
Expand Down Expand Up @@ -307,6 +309,9 @@ _dl_aux_init (ElfW(auxv_t) *av)
case AT_RANDOM:
_dl_random = (void *) av->a_un.a_val;
break;
case AT_MINSIGSTKSZ:
_dl_minsigstacksize = av->a_un.a_val;
break;
DL_PLATFORM_AUXV
}
if (seen == 0xf)
Expand Down
9 changes: 9 additions & 0 deletions elf/dl-sysdep.c
Expand Up @@ -115,6 +115,11 @@ _dl_sysdep_start (void **start_argptr,
user_entry = (ElfW(Addr)) ENTRY_POINT;
GLRO(dl_platform) = NULL; /* Default to nothing known about the platform. */

/* NB: Default to a constant CONSTANT_MINSIGSTKSZ. */
_Static_assert (__builtin_constant_p (CONSTANT_MINSIGSTKSZ),
"CONSTANT_MINSIGSTKSZ is constant");
GLRO(dl_minsigstacksize) = CONSTANT_MINSIGSTKSZ;

for (av = GLRO(dl_auxv); av->a_type != AT_NULL; set_seen (av++))
switch (av->a_type)
{
Expand Down Expand Up @@ -179,6 +184,9 @@ _dl_sysdep_start (void **start_argptr,
case AT_RANDOM:
_dl_random = (void *) av->a_un.a_val;
break;
case AT_MINSIGSTKSZ:
GLRO(dl_minsigstacksize) = av->a_un.a_val;
break;
DL_PLATFORM_AUXV
}

Expand Down Expand Up @@ -306,6 +314,7 @@ _dl_show_auxv (void)
[AT_SYSINFO_EHDR - 2] = { "SYSINFO_EHDR: 0x", hex },
[AT_RANDOM - 2] = { "RANDOM: 0x", hex },
[AT_HWCAP2 - 2] = { "HWCAP2: 0x", hex },
[AT_MINSIGSTKSZ - 2] = { "MINSIGSTKSZ ", dec },
[AT_L1I_CACHESIZE - 2] = { "L1I_CACHESIZE: ", dec },
[AT_L1I_CACHEGEOMETRY - 2] = { "L1I_CACHEGEOMETRY: 0x", hex },
[AT_L1D_CACHESIZE - 2] = { "L1D_CACHESIZE: ", dec },
Expand Down
5 changes: 5 additions & 0 deletions include/bits/sigstack.h
@@ -0,0 +1,5 @@
#include_next <bits/sigstack.h>

#if !defined _ISOMAC && !defined CONSTANT_MINSIGSTKSZ
# define CONSTANT_MINSIGSTKSZ MINSIGSTKSZ
#endif
7 changes: 7 additions & 0 deletions include/bits/sigstksz.h
@@ -0,0 +1,7 @@
/* NB: Don't define MINSIGSTKSZ nor SIGSTKSZ to sysconf (SC_SIGSTKSZ) for
glibc build. IS_IN can only be used when _ISOMAC isn't defined. */
#ifdef _ISOMAC
# include_next <bits/sigstksz.h>
#elif IS_IN (libsupport)
# include_next <bits/sigstksz.h>
#endif
11 changes: 11 additions & 0 deletions include/features.h
Expand Up @@ -48,6 +48,8 @@
_LARGEFILE64_SOURCE Additional functionality from LFS for large files.
_FILE_OFFSET_BITS=N Select default filesystem interface.
_ATFILE_SOURCE Additional *at interfaces.
_SC_SIGSTKSZ_SOURCE Select correct (but non compile-time constant)
MINSIGSTKSZ and SIGSTKSZ.
_GNU_SOURCE All of the above, plus GNU extensions.
_DEFAULT_SOURCE The default set of features (taking precedence over
__STRICT_ANSI__).
Expand Down Expand Up @@ -94,6 +96,8 @@
__USE_FILE_OFFSET64 Define 64bit interface as default.
__USE_MISC Define things from 4.3BSD or System V Unix.
__USE_ATFILE Define *at interfaces and AT_* constants for them.
__USE_SC_SIGSTKSZ Define correct (but non compile-time constant)
MINSIGSTKSZ and SIGSTKSZ.
__USE_GNU Define GNU extensions.
__USE_FORTIFY_LEVEL Additional security measures used, according to level.
Expand Down Expand Up @@ -137,6 +141,7 @@
#undef __USE_FILE_OFFSET64
#undef __USE_MISC
#undef __USE_ATFILE
#undef __USE_SC_SIGSTKSZ
#undef __USE_GNU
#undef __USE_FORTIFY_LEVEL
#undef __KERNEL_STRICT_NAMES
Expand Down Expand Up @@ -213,6 +218,8 @@
# define _DEFAULT_SOURCE 1
# undef _ATFILE_SOURCE
# define _ATFILE_SOURCE 1
# undef _SC_SIGSTKSZ_SOURCE
# define _SC_SIGSTKSZ_SOURCE 1
#endif

/* If nothing (other than _GNU_SOURCE and _DEFAULT_SOURCE) is defined,
Expand Down Expand Up @@ -388,6 +395,10 @@
# define __USE_ATFILE 1
#endif

#ifdef _SC_SIGSTKSZ_SOURCE
# define __USE_SC_SIGSTKSZ 1
#endif

#ifdef _GNU_SOURCE
# define __USE_GNU 1
#endif
Expand Down
21 changes: 21 additions & 0 deletions manual/conf.texi
Expand Up @@ -913,6 +913,27 @@ Inquire about the parameter corresponding to @code{NL_SETMAX}.
@item _SC_NL_TEXTMAX
@standards{X/Open, unistd.h}
Inquire about the parameter corresponding to @code{NL_TEXTMAX}.

@item _SC_MINSIGSTKSZ
@standards{GNU, unistd.h}
Inquire about the minimum number of bytes of free stack space required
in order to guarantee successful, non-nested handling of a single signal
whose handler is an empty function.

@item _SC_SIGSTKSZ
@standards{GNU, unistd.h}
Inquire about the suggested minimum number of bytes of stack space
required for a signal stack.

This is not guaranteed to be enough for any specific purpose other than
the invocation of a single, non-nested, empty handler, but nonetheless
should be enough for basic scenarios involving simple signal handlers
and very low levels of signal nesting (say, 2 or 3 levels at the very
most).

This value is provided for developer convenience and to ease migration
from the legacy @code{SIGSTKSZ} constant. Programs requiring stronger
guarantees should avoid using it if at all possible.
@end vtable

@node Examples of Sysconf
Expand Down
6 changes: 6 additions & 0 deletions manual/creature.texi
Expand Up @@ -258,6 +258,12 @@ checks are applied. If defined to @math{3}, @theglibc{} may also use
checks that may have an additional performance overhead.
@end defvr

@defvr Macro _SC_SIGSTKSZ_SOURCE
@standards{GNU, (none)}
If this macro is defined, correct (but non compile-time constant)
MINSIGSTKSZ and SIGSTKSZ are defined.
@end defvr

@defvr Macro _REENTRANT
@defvrx Macro _THREAD_SAFE
@standards{Obsolete, (none)}
Expand Down
3 changes: 3 additions & 0 deletions posix/sysconf.c
Expand Up @@ -266,6 +266,9 @@ __sysconf (int name)
case _SC_XOPEN_REALTIME:
case _SC_XOPEN_REALTIME_THREADS:

case _SC_MINSIGSTKSZ:
case _SC_SIGSTKSZ:

break;
}

Expand Down
5 changes: 3 additions & 2 deletions signal/Makefile
Expand Up @@ -31,7 +31,8 @@ headers := signal.h sys/signal.h \
bits/types/sigevent_t.h bits/types/siginfo_t.h \
bits/types/sigset_t.h bits/types/sigval_t.h \
bits/types/stack_t.h bits/types/struct_sigstack.h \
bits/types/__sigval_t.h bits/signal_ext.h
bits/types/__sigval_t.h bits/signal_ext.h \
bits/sigstksz.h

routines := signal raise killpg \
sigaction sigprocmask kill \
Expand All @@ -48,7 +49,7 @@ routines := signal raise killpg \
tests := tst-signal tst-sigset tst-sigsimple tst-raise tst-sigset2 \
tst-sigwait-eintr tst-sigaction \
tst-minsigstksz-1 tst-minsigstksz-2 tst-minsigstksz-3 \
tst-minsigstksz-3a tst-minsigstksz-4 \
tst-minsigstksz-3a tst-minsigstksz-4 tst-minsigstksz-5 \
tst-sigisemptyset

include ../Rules
Expand Down
1 change: 1 addition & 0 deletions signal/signal.h
Expand Up @@ -312,6 +312,7 @@ extern int siginterrupt (int __sig, int __interrupt) __THROW
__attribute_deprecated_msg__ ("Use sigaction with SA_RESTART instead");

# include <bits/sigstack.h>
# include <bits/sigstksz.h>
# include <bits/ss_flags.h>

/* Alternate signal handler stack interface.
Expand Down
84 changes: 84 additions & 0 deletions signal/tst-minsigstksz-5.c
@@ -0,0 +1,84 @@
/* Test of signal delivery on an alternate stack with MINSIGSTKSZ size.
Copyright (C) 2020 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */

#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <support/check.h>
#include <support/support.h>

static volatile sig_atomic_t handler_run;

static void
handler (int signo)
{
/* Clear a bit of on-stack memory. */
volatile char buffer[256];
for (size_t i = 0; i < sizeof (buffer); ++i)
buffer[i] = 0;
handler_run = 1;
}

int
do_test (void)
{
size_t stack_buffer_size = 64 * 1024 * 1024;
void *stack_buffer = xmalloc (stack_buffer_size);
void *stack_end = stack_buffer + stack_buffer_size;
memset (stack_buffer, 0xCC, stack_buffer_size);

void *stack_bottom = stack_buffer + (stack_buffer_size + MINSIGSTKSZ) / 2;
void *stack_top = stack_bottom + MINSIGSTKSZ;
stack_t stack =
{
.ss_sp = stack_bottom,
.ss_size = MINSIGSTKSZ,
};
if (sigaltstack (&stack, NULL) < 0)
FAIL_RET ("sigaltstack: %m\n");

struct sigaction act =
{
.sa_handler = handler,
.sa_flags = SA_ONSTACK,
};
if (sigaction (SIGUSR1, &act, NULL) < 0)
FAIL_RET ("sigaction: %m\n");

if (kill (getpid (), SIGUSR1) < 0)
FAIL_RET ("kill: %m\n");

if (handler_run != 1)
FAIL_RET ("handler did not run\n");

for (void *p = stack_buffer; p < stack_bottom; ++p)
if (*(unsigned char *) p != 0xCC)
FAIL_RET ("changed byte %ld bytes below configured stack\n",
(long) (stack_bottom - p));
for (void *p = stack_top; p < stack_end; ++p)
if (*(unsigned char *) p != 0xCC)
FAIL_RET ("changed byte %ld bytes above configured stack\n",
(long) (p - stack_top));

free (stack_buffer);

return 0;
}

#include <support/test-driver.c>
3 changes: 3 additions & 0 deletions sysdeps/generic/ldsodefs.h
Expand Up @@ -536,6 +536,9 @@ struct rtld_global_ro
/* Cached value of `getpagesize ()'. */
EXTERN size_t _dl_pagesize;

/* Cached value of `sysconf (_SC_MINSIGSTKSZ)'. */
EXTERN size_t _dl_minsigstacksize;

/* Do we read from ld.so.cache? */
EXTERN int _dl_inhibit_cache;

Expand Down
33 changes: 33 additions & 0 deletions sysdeps/unix/sysv/linux/bits/sigstksz.h
@@ -0,0 +1,33 @@
/* Definition of MINSIGSTKSZ and SIGSTKSZ. Linux version.
Copyright (C) 2020 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */

#ifndef _SIGNAL_H
# error "Never include <bits/sigstksz.h> directly; use <signal.h> instead."
#endif

#if defined __USE_SC_SIGSTKSZ && __USE_SC_SIGSTKSZ
# include <unistd.h>

/* Default stack size for a signal handler: sysconf (SC_SIGSTKSZ). */
# undef SIGSTKSZ
# define SIGSTKSZ sysconf (_SC_SIGSTKSZ)

/* Minimum stack size for a signal handler: SIGSTKSZ. */
# undef MINSIGSTKSZ
# define MINSIGSTKSZ SIGSTKSZ
#endif
27 changes: 27 additions & 0 deletions sysdeps/unix/sysv/linux/ia64/sysconf-sigstksz.h
@@ -0,0 +1,27 @@
/* sysconf_sigstksz (). Linux/ia64 version.
Copyright (C) 2020 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */

/* Return sysconf (_SC_SIGSTKSZ). */

static long int
sysconf_sigstksz (void)
{
_Static_assert (__builtin_constant_p (SIGSTKSZ),
"SIGSTKSZ is constant");
return SIGSTKSZ;
}

0 comments on commit 6c57d32

Please sign in to comment.