1
0
mirror of https://sourceware.org/git/glibc.git synced 2025-07-28 00:21:52 +03:00

Block all signals on timer_create thread (BZ#10815)

The behavior of the signal mask on threads created by timer_create
for SIGEV_THREAD timers are implementation-defined and glibc explicit
unblocks all signals before calling the user-defined function.

This behavior, although not incorrect standard-wise, opens a race if a
program using a blocked rt-signal plus sigwaitinfo (and without an
installed signal handler for the rt-signal) receives a signal while
executing the used-defined function for SIGEV_THREAD.

A better alternative discussed in bug report is to rather block all
signals (besides the internal ones not available to application
usage).

This patch fixes this issue by only unblocking SIGSETXID (used on
set*uid function) and SIGCANCEL (used for thread cancellation).

Checked on x86_64-linux-gnu and i686-linux-gnu.
This commit is contained in:
Adhemerval Zanella
2019-12-09 14:33:33 -03:00
parent 8d42bf859a
commit 27d83441a2
6 changed files with 221 additions and 63 deletions

View File

@ -42,15 +42,9 @@ struct thread_start_data
static void *
timer_sigev_thread (void *arg)
{
/* The parent thread has all signals blocked. This is a bit
surprising for user code, although valid. We unblock all
signals. */
sigset_t ss;
sigemptyset (&ss);
INTERNAL_SYSCALL_CALL (rt_sigprocmask, SIG_SETMASK, &ss, NULL, _NSIG / 8);
__libc_signal_unblock_sigtimer (NULL);
struct thread_start_data *td = (struct thread_start_data *) arg;
void (*thrfunc) (sigval_t) = td->thrfunc;
sigval_t sival = td->sival;
@ -68,65 +62,49 @@ timer_sigev_thread (void *arg)
static void *
timer_helper_thread (void *arg)
{
/* Wait for the SIGTIMER signal, allowing the setXid signal, and
none else. */
sigset_t ss;
sigemptyset (&ss);
__sigaddset (&ss, SIGTIMER);
/* Endless loop of waiting for signals. The loop is only ended when
the thread is canceled. */
while (1)
{
siginfo_t si;
/* sigwaitinfo cannot be used here, since it deletes
SIGCANCEL == SIGTIMER from the set. */
/* XXX The size argument hopefully will have to be changed to the
real size of the user-level sigset_t. */
int result = SYSCALL_CANCEL (rt_sigtimedwait, &ss, &si, NULL, _NSIG / 8);
if (result > 0)
while (sigwaitinfo (&sigtimer_set, &si) < 0);
if (si.si_code == SI_TIMER)
{
if (si.si_code == SI_TIMER)
struct timer *tk = (struct timer *) si.si_ptr;
/* Check the timer is still used and will not go away
while we are reading the values here. */
pthread_mutex_lock (&__active_timer_sigev_thread_lock);
struct timer *runp = __active_timer_sigev_thread;
while (runp != NULL)
if (runp == tk)
break;
else
runp = runp->next;
if (runp != NULL)
{
struct timer *tk = (struct timer *) si.si_ptr;
struct thread_start_data *td = malloc (sizeof (*td));
/* Check the timer is still used and will not go away
while we are reading the values here. */
pthread_mutex_lock (&__active_timer_sigev_thread_lock);
struct timer *runp = __active_timer_sigev_thread;
while (runp != NULL)
if (runp == tk)
break;
else
runp = runp->next;
if (runp != NULL)
/* There is not much we can do if the allocation fails. */
if (td != NULL)
{
struct thread_start_data *td = malloc (sizeof (*td));
/* This is the signal we are waiting for. */
td->thrfunc = tk->thrfunc;
td->sival = tk->sival;
/* There is not much we can do if the allocation fails. */
if (td != NULL)
{
/* This is the signal we are waiting for. */
td->thrfunc = tk->thrfunc;
td->sival = tk->sival;
pthread_t th;
(void) pthread_create (&th, &tk->attr,
timer_sigev_thread, td);
}
pthread_t th;
pthread_create (&th, &tk->attr, timer_sigev_thread, td);
}
pthread_mutex_unlock (&__active_timer_sigev_thread_lock);
}
else if (si.si_code == SI_TKILL)
/* The thread is canceled. */
pthread_exit (NULL);
pthread_mutex_unlock (&__active_timer_sigev_thread_lock);
}
else if (si.si_code == SI_TKILL)
/* The thread is canceled. */
pthread_exit (NULL);
}
}
@ -160,14 +138,10 @@ __start_helper_thread (void)
/* Block all signals in the helper thread but SIGSETXID. To do this
thoroughly we temporarily have to block all signals here. The
helper can lose wakeups if SIGCANCEL is not blocked throughout,
but sigfillset omits it SIGSETXID. So, we add SIGCANCEL back
explicitly here. */
helper can lose wakeups if SIGTIMER is not blocked throughout. */
sigset_t ss;
sigset_t oss;
sigfillset (&ss);
__sigaddset (&ss, SIGCANCEL);
INTERNAL_SYSCALL_CALL (rt_sigprocmask, SIG_SETMASK, &ss, &oss, _NSIG / 8);
__libc_signal_block_app (&ss);
__libc_signal_block_sigtimer (NULL);
/* Create the helper thread for this timer. */
pthread_t th;
@ -177,7 +151,7 @@ __start_helper_thread (void)
__helper_tid = ((struct pthread *) th)->tid;
/* Restore the signal mask. */
INTERNAL_SYSCALL_CALL (rt_sigprocmask, SIG_SETMASK, &oss, NULL, _NSIG / 8);
__libc_signal_restore_set (&ss);
/* No need for the attribute anymore. */
(void) pthread_attr_destroy (&attr);