glibc/sysdeps/nptl
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
..
bits Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
sys Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
_Fork.c stdlib: Make abort/_Exit AS-safe (BZ 26275) 2024-10-08 14:40:12 -03:00
aio_misc.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
cancellation-pc-check.h nptl: Fix Race conditions in pthread cancellation [BZ#12683] 2024-08-23 14:27:43 -03:00
dl-mutex.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
dl-thread_gscope_wait.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
dl-tls_init_tp.c Linux: Make __rseq_size useful for feature detection (bug 31965) 2024-07-09 19:33:37 +02:00
dl-tunables.list Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
fork.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
futex-internal.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
gai_misc.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
Implies
internaltypes.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
jmp-unwind.c Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
libc_start_call_main.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
libc-lock.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
libc-lockP.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
lowlevellock-futex.h nptl: Fix Race conditions in pthread cancellation [BZ#12683] 2024-08-23 14:27:43 -03:00
lowlevellock.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
Makeconfig Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
Makefile Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
malloc-machine.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
proc_service.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
pthread_atfork_compat.h Consolidate pthread_atfork 2021-06-24 10:04:41 -03:00
pthread_early_init.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
pthread_mutex_backoff.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
pthread_mutex_conf.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
pthread-offsets.h nptl: update default pthread-offsets.h 2020-02-10 17:01:21 +01:00
pthread.h Always define __USE_TIME_BITS64 when 64 bit time_t is used 2024-04-02 15:28:36 -03:00
pthreadP.h stdlib: Make abort/_Exit AS-safe (BZ 26275) 2024-10-08 14:40:12 -03:00
setxid.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
stdio-lock.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
Subdirs
tcb-access.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
tcb-offsets.h
thread_db.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
thread_pointer.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
timer_routines.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
tst-mqueue8x.c