glibc/sysdeps/posix
Adhemerval Zanella d40ac01cbb stdlib: Make abort/_Exit AS-safe (BZ 26275)
The recursive lock used on abort does not synchronize with a new process
creation (either by fork-like interfaces or posix_spawn ones), nor it
is reinitialized after fork().

Also, the SIGABRT unblock before raise() shows another race condition,
where a fork or posix_spawn() call by another thread, just after the
recursive lock release and before the SIGABRT signal, might create
programs with a non-expected signal mask.  With the default option
(without POSIX_SPAWN_SETSIGDEF), the process can see SIG_DFL for
SIGABRT, where it should be SIG_IGN.

To fix the AS-safe, raise() does not change the process signal mask,
and an AS-safe lock is used if a SIGABRT is installed or the process
is blocked or ignored.  With the signal mask change removal,
there is no need to use a recursive loc.  The lock is also taken on
both _Fork() and posix_spawn(), to avoid the spawn process to see the
abort handler as SIG_DFL.

A read-write lock is used to avoid serialize _Fork and posix_spawn
execution.  Both sigaction (SIGABRT) and abort() requires to lock
as writer (since both change the disposition).

The fallback is also simplified: there is no need to use a loop of
ABORT_INSTRUCTION after _exit() (if the syscall does not terminate the
process, the system is broken).

The proposed fix changes how setjmp works on a SIGABRT handler, where
glibc does not save the signal mask.  So usage like the below will now
always abort.

  static volatile int chk_fail_ok;
  static jmp_buf chk_fail_buf;

  static void
  handler (int sig)
  {
    if (chk_fail_ok)
      {
        chk_fail_ok = 0;
        longjmp (chk_fail_buf, 1);
      }
    else
      _exit (127);
  }
  [...]
  signal (SIGABRT, handler);
  [....]
  chk_fail_ok = 1;
  if (! setjmp (chk_fail_buf))
    {
      // Something that can calls abort, like a failed fortify function.
      chk_fail_ok = 0;
      printf ("FAIL\n");
    }

Such cases will need to use sigsetjmp instead.

The _dl_start_profile calls sigaction through _profil, and to avoid
pulling abort() on loader the call is replaced with __libc_sigaction.

Checked on x86_64-linux-gnu and aarch64-linux-gnu.

Reviewed-by: DJ Delorie <dj@redhat.com>
2024-10-08 14:40:12 -03:00
..
alarm.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
clock_getres.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
clock.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
ctermid.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
cuserid.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
dl-fileid.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
dup2.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
dup.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
euidaccess.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
flock.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
fpathconf.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
gai_strerror-strs.h posix: Handle success in gai_strerror() 2023-06-13 20:54:49 +02:00
gai_strerror.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
getcwd.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
getdtsz.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
gethostname.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
getpagesize.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
isatty.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
isfdtype.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
killpg.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
libc_fatal.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
mkfifo.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
mkfifoat.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
nice.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
open64.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
pathconf.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
pause.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
posix_fallocate64.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
posix_fallocate.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
pread64.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
pread.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
preadv2.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
preadv64.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
preadv64v2.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
preadv_common.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
preadv.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
profil.c stdlib: Make abort/_Exit AS-safe (BZ 26275) 2024-10-08 14:40:12 -03:00
pwrite64.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
pwrite.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
pwritev2.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
pwritev64.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
pwritev64v2.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
pwritev_common.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
pwritev.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
raise.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
readv.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
remove.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
rename.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
sigblock.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
sigignore.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
sigintr.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
signal.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
sigpause.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
sigset.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
sigsetmask.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
sigsuspend.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
sigwait.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
sleep.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
sprofil.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
Subdirs
sysconf-pthread_stack_min.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
sysconf.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
system.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
sysv_signal.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
tempname.c Fix missing randomness in __gen_tempname (bug 32214) 2024-09-26 11:45:44 +02:00
truncate.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
ttyname_r.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
ttyname.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
ulimit.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
usleep.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
utime.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
utimes.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
writev_nocancel.c hurd: Fix __writev_nocancel_nostatus 2020-06-14 17:45:04 +00:00
writev.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00