2022-04-21 12:41:59 +00:00
|
|
|
/* Internal sigset_t definition.
|
2025-01-01 18:14:45 +00:00
|
|
|
Copyright (C) 2022-2025 Free Software Foundation, Inc.
|
2022-04-21 12:41:59 +00:00
|
|
|
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 _INTERNAL_SIGSET_H
|
|
|
|
#define _INTERNAL_SIGSET_H
|
|
|
|
|
|
|
|
#include <sigsetops.h>
|
|
|
|
|
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-03 18:41:10 +00:00
|
|
|
typedef struct _internal_sigset_t
|
2022-04-21 12:41:59 +00:00
|
|
|
{
|
|
|
|
unsigned long int __val[__NSIG_WORDS];
|
|
|
|
} internal_sigset_t;
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
internal_sigset_from_sigset (internal_sigset_t *iset, const sigset_t *set)
|
|
|
|
{
|
|
|
|
int cnt = __NSIG_WORDS;
|
|
|
|
while (--cnt >= 0)
|
|
|
|
iset->__val[cnt] = set->__val[cnt];
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
internal_sigemptyset (internal_sigset_t *set)
|
|
|
|
{
|
|
|
|
int cnt = __NSIG_WORDS;
|
|
|
|
while (--cnt >= 0)
|
|
|
|
set->__val[cnt] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
internal_sigfillset (internal_sigset_t *set)
|
|
|
|
{
|
|
|
|
int cnt = __NSIG_WORDS;
|
|
|
|
while (--cnt >= 0)
|
|
|
|
set->__val[cnt] = ~0UL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
internal_sigisemptyset (const internal_sigset_t *set)
|
|
|
|
{
|
|
|
|
int cnt = __NSIG_WORDS;
|
2024-03-11 14:13:09 +00:00
|
|
|
unsigned long int ret = set->__val[--cnt];
|
2022-04-21 12:41:59 +00:00
|
|
|
while (ret == 0 && --cnt >= 0)
|
|
|
|
ret = set->__val[cnt];
|
|
|
|
return ret == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
internal_sigandset (internal_sigset_t *dest, const internal_sigset_t *left,
|
|
|
|
const internal_sigset_t *right)
|
|
|
|
{
|
|
|
|
int cnt = __NSIG_WORDS;
|
|
|
|
while (--cnt >= 0)
|
|
|
|
dest->__val[cnt] = left->__val[cnt] & right->__val[cnt];
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
internal_sigorset (internal_sigset_t *dest, const internal_sigset_t *left,
|
|
|
|
const internal_sigset_t *right)
|
|
|
|
{
|
|
|
|
int cnt = __NSIG_WORDS;
|
|
|
|
while (--cnt >= 0)
|
|
|
|
dest->__val[cnt] = left->__val[cnt] | right->__val[cnt];
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
internal_sigismember (const internal_sigset_t *set, int sig)
|
|
|
|
{
|
|
|
|
unsigned long int mask = __sigmask (sig);
|
2024-03-11 14:13:09 +00:00
|
|
|
int word = __sigword (sig);
|
2022-04-21 12:41:59 +00:00
|
|
|
return set->__val[word] & mask ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
internal_sigaddset (internal_sigset_t *set, int sig)
|
|
|
|
{
|
|
|
|
unsigned long int mask = __sigmask (sig);
|
2024-03-11 14:13:09 +00:00
|
|
|
int word = __sigword (sig);
|
2022-04-21 12:41:59 +00:00
|
|
|
set->__val[word] |= mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
internal_sigdelset (internal_sigset_t *set, int sig)
|
|
|
|
{
|
|
|
|
unsigned long int mask = __sigmask (sig);
|
2024-03-11 14:13:09 +00:00
|
|
|
int word = __sigword (sig);
|
2022-04-21 12:41:59 +00:00
|
|
|
set->__val[word] &= ~mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* _INTERNAL_SIGSET_H */
|