Skip to content

Commit e6fa16a

Browse files
committed
signal: sigprocmask() should do retarget_shared_pending()
In short, almost every changing of current->blocked is wrong, or at least can lead to the unexpected results. For example. Two threads T1 and T2, T1 sleeps in sigtimedwait/pause/etc. kill(tgid, SIG) can pick T2 for TIF_SIGPENDING. If T2 calls sigprocmask() and blocks SIG before it notices the pending signal, nobody else can handle this pending shared signal. I am not sure this is bug, but at least this looks strange imho. T1 should not sleep forever, there is a signal which should wake it up. This patch moves the code which actually changes ->blocked into the new helper, set_current_blocked() and changes this code to call retarget_shared_pending() as exit_signals() does. We should only care about the signals we just blocked, we use "newset & ~current->blocked" as a mask. We do not check !sigisemptyset(newblocked), retarget_shared_pending() is cheap unless mask & shared_pending. Note: for this particular case we could simply change sigprocmask() to return -EINTR if signal_pending(), but then we should change other callers and, more importantly, if we need this fix then set_current_blocked() will have more callers and some of them can't restart. See the next patch as a random example. Signed-off-by: Oleg Nesterov <oleg@redhat.com> Reviewed-by: Matt Fleming <matt.fleming@linux.intel.com> Acked-by: Tejun Heo <tj@kernel.org>
1 parent 73ef4ae commit e6fa16a

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

include/linux/signal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ extern long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig,
243243
siginfo_t *info);
244244
extern long do_sigpending(void __user *, unsigned long);
245245
extern int sigprocmask(int, sigset_t *, sigset_t *);
246+
extern void set_current_blocked(const sigset_t *);
246247
extern int show_unhandled_signals;
247248

248249
struct pt_regs;

kernel/signal.c

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,6 +2299,29 @@ long do_no_restart_syscall(struct restart_block *param)
22992299
return -EINTR;
23002300
}
23012301

2302+
/**
2303+
* set_current_blocked - change current->blocked mask
2304+
* @newset: new mask
2305+
*
2306+
* It is wrong to change ->blocked directly, this helper should be used
2307+
* to ensure the process can't miss a shared signal we are going to block.
2308+
*/
2309+
void set_current_blocked(const sigset_t *newset)
2310+
{
2311+
struct task_struct *tsk = current;
2312+
2313+
spin_lock_irq(&tsk->sighand->siglock);
2314+
if (signal_pending(tsk) && !thread_group_empty(tsk)) {
2315+
sigset_t newblocked;
2316+
/* A set of now blocked but previously unblocked signals. */
2317+
signandsets(&newblocked, newset, &current->blocked);
2318+
retarget_shared_pending(tsk, &newblocked);
2319+
}
2320+
tsk->blocked = *newset;
2321+
recalc_sigpending();
2322+
spin_unlock_irq(&tsk->sighand->siglock);
2323+
}
2324+
23022325
/*
23032326
* This is also useful for kernel threads that want to temporarily
23042327
* (or permanently) block certain signals.
@@ -2330,11 +2353,7 @@ int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
23302353
return -EINVAL;
23312354
}
23322355

2333-
spin_lock_irq(&tsk->sighand->siglock);
2334-
tsk->blocked = newset;
2335-
recalc_sigpending();
2336-
spin_unlock_irq(&tsk->sighand->siglock);
2337-
2356+
set_current_blocked(&newset);
23382357
return 0;
23392358
}
23402359

0 commit comments

Comments
 (0)