glibc/sysdeps/htl
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
include
dl-support.c
dl-thread_gscope_wait.c
flockfile.c
ftrylockfile.c
funlockfile.c
futex-internal.h
Implies
libc-lock.h
libc-lockP.h
Makeconfig
Makefile
pt-attr-destroy.c
pt-attr-getdetachstate.c hurd: Fix missing pthread_ compat symbol in libc 2024-08-01 23:58:51 +02:00
pt-attr-getguardsize.c
pt-attr-getinheritsched.c hurd: Fix missing pthread_ compat symbol in libc 2024-08-01 23:58:51 +02:00
pt-attr-getschedparam.c hurd: Fix missing pthread_ compat symbol in libc 2024-08-01 23:58:51 +02:00
pt-attr-getschedpolicy.c hurd: Fix missing pthread_ compat symbol in libc 2024-08-01 23:58:51 +02:00
pt-attr-getscope.c
pt-attr-getstack.c
pt-attr-getstackaddr.c
pt-attr-getstacksize.c
pt-attr-init.c
pt-attr-setdetachstate.c hurd: Fix missing pthread_ compat symbol in libc 2024-08-01 23:58:51 +02:00
pt-attr-setguardsize.c
pt-attr-setinheritsched.c hurd: Fix missing pthread_ compat symbol in libc 2024-08-01 23:58:51 +02:00
pt-attr-setschedparam.c
pt-attr-setschedpolicy.c hurd: Fix missing pthread_ compat symbol in libc 2024-08-01 23:58:51 +02:00
pt-attr-setscope.c
pt-attr-setstack.c
pt-attr-setstackaddr.c
pt-attr-setstacksize.c
pt-attr.c
pt-barrier-destroy.c
pt-barrier-init.c
pt-barrier-wait.c
pt-barrier.c
pt-barrierattr-destroy.c
pt-barrierattr-getpshared.c
pt-barrierattr-init.c
pt-barrierattr-setpshared.c
pt-cond-brdcast.c
pt-cond-destroy.c
pt-cond-init.c
pt-cond-signal.c
pt-cond-timedwait.c
pt-cond-wait.c
pt-cond.c
pt-condattr-destroy.c
pt-condattr-getclock.c
pt-condattr-getpshared.c
pt-condattr-init.c
pt-condattr-setclock.c
pt-condattr-setpshared.c
pt-destroy-specific.c
pt-equal.c hurd: Fix missing pthread_ compat symbol in libc 2024-08-01 23:58:51 +02:00
pt-getconcurrency.c
pt-getcpuclockid.c
pt-getschedparam.c hurd: Fix missing pthread_ compat symbol in libc 2024-08-01 23:58:51 +02:00
pt-getspecific.c
pt-init-specific.c
pt-key-create.c
pt-key-delete.c
pt-key.h
pt-once.c
pt-rwlock-attr.c
pt-rwlock-destroy.c
pt-rwlock-init.c
pt-rwlock-rdlock.c
pt-rwlock-timedrdlock.c
pt-rwlock-timedwrlock.c
pt-rwlock-tryrdlock.c
pt-rwlock-trywrlock.c
pt-rwlock-unlock.c
pt-rwlock-wrlock.c
pt-rwlockattr-destroy.c
pt-rwlockattr-getpshared.c
pt-rwlockattr-init.c
pt-rwlockattr-setpshared.c
pt-setconcurrency.c
pt-setschedparam.c hurd: Fix missing pthread_ compat symbol in libc 2024-08-01 23:58:51 +02:00
pt-setschedprio.c
pt-setspecific.c
pt-startup.c
pthread_atfork_compat.h
pthread-functions.h
pthread.h Add pthread_getname_np and pthread_setname_np for Hurd 2024-07-16 09:21:52 +02:00
pthreadP.h stdlib: Make abort/_Exit AS-safe (BZ 26275) 2024-10-08 14:40:12 -03:00
raise.c
sem-destroy.c
sem-getvalue.c
sem-init.c
sem-post.c
sem-timedwait.c
sem-trywait.c
sem-wait.c
sem-waitfast.c
stdio-lock.h
Subdirs
thrd_current.c
timer_routines.c
timer_routines.h
Versions