signals: work around random wakeups in sigsuspend()
PATCH 1/1 signals: work around random wakeups in sigsuspend()
A random wakeup can get us out of sigsuspend() without TIF_SIGPENDING being set.
Avoid that by making sure we were signaled, like sys_pause() does.
--- kernel/signal.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/signal.c +4 -2
@@ -3528,8 +3528,10 @@ static int sigsuspend(sigset_t *set) current->saved_sigmask = current->blocked;set_current_blocked (set); -__set_current_state (TASK_INTERRUPTIBLE ); -schedule (); +while (!signal_pending (current)) { +__set_current_state (TASK_INTERRUPTIBLE ); +schedule (); + }set_restore_sigmask ();return -ERESTARTNOHAND ; }
Signature · 1 line
1.7.10.4
Quoted 3 lines · Sasha Levin
A random wakeup can get us out of
sigsuspend()withoutTIF_SIGPENDINGbeing set.
and TIF_RESTORE_SIGMASK is just wrong in this case. I'd say this is the bugfix, not work-around ;)
Quoted 3 lines
Avoid that by making sure we were signaled, like
sys_pause()does.Signed-off-by Sasha Levin <[email protected]>
Thanks Sasha.
Quoted 24 lines
--- kernel/signal.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)kernel/signal.c +4
-2@@ -3528,8 +3528,10 @@ static int sigsuspend(sigset_t *set) current->saved_sigmask = current->blocked;set_current_blocked (set); -__set_current_state (TASK_INTERRUPTIBLE ); -schedule (); +while (!signal_pending (current)) { +__set_current_state (TASK_INTERRUPTIBLE ); +schedule (); + }set_restore_sigmask ();return -ERESTARTNOHAND ; }Signature · 1 line
1.7.10.4
Peter Zijlstra
Acked-byQuoted 7 lines · Oleg Nesterov
Quoted 3 lines · Sasha Levin
A random wakeup can get us out of
sigsuspend()withoutTIF_SIGPENDINGbeing set.and
TIF_RESTORE_SIGMASKis just wrong in this case. I'd say this is the bugfix, not work-around ;)
Agreed!
Quoted 5 lines
Quoted 3 lines
Avoid that by making sure we were signaled, like
sys_pause()does.Signed-off-by Sasha Levin <[email protected]>Acked-by Oleg Nesterov <[email protected]>
Quoted 8 lines · Oleg Nesterov
Quoted 3 lines · Sasha Levin
A random wakeup can get us out of
sigsuspend()withoutTIF_SIGPENDINGbeing set.and
TIF_RESTORE_SIGMASKis just wrong in this case. I'd say this is the bugfix, not work-around ;)
Quoted 7 lines
Quoted 3 lines
Avoid that by making sure we were signaled, like
sys_pause()does.Signed-off-by Sasha Levin <[email protected]>Acked-by Oleg Nesterov <[email protected]>Thanks Sasha.
Out of curiousity - where did that stray wakeup come from? PTRACE_KILL used to trigger those, but that got fixed. How does one trigger that kind of bugs on the current kernels?
Quoted 13 lines · Al Viro
Quoted 12 lines · Oleg Nesterov
Quoted 3 lines · Sasha Levin
A random wakeup can get us out of
sigsuspend()withoutTIF_SIGPENDINGbeing set.and
TIF_RESTORE_SIGMASKis just wrong in this case. I'd say this is the bugfix, not work-around ;)Quoted 3 lines
Avoid that by making sure we were signaled, like
sys_pause()does.Signed-off-by Sasha Levin <[email protected]>
Quoted 7 lines
Quoted 3 lines
Acked-by Oleg Nesterov <[email protected]>Thanks Sasha.
Out of curiousity - where did that stray wakeup come from?
PTRACE_KILLused to trigger those, but that got fixed. How does one trigger that kind of bugs on the current kernels?
Its a regular TASK_INTERRUPTIBLE sleep, for those spurious wakeups are not a bug, they're pretty fundamentally allowed.
See: lkml.kernel.org/r/CA+55aFwHkOo+YGWKYROmce1-H_uG3KfEUmCkJUerTj=ojY2H6Q@mail.gmail.com
But they became a lot more common because we explicitly used the delayed wakeup pattern described there with wake_q. See commits:
7675104990ed ("sched: Implement lockless wake-queues") 1d0dcb3ad9d3 ("futex: Implement lockless wakeups")
Quoted 6 lines · Peter Zijlstra
Quoted 3 lines
Out of curiousity - where did that stray wakeup come from?
PTRACE_KILLused to trigger those, but that got fixed. How does one trigger that kind of bugs on the current kernels?Its a regular
TASK_INTERRUPTIBLEsleep, for those spurious wakeups are not a bug, they're pretty fundamentally allowed.
They are, which makes any code that doesn't expect them in such situations buggy.
Quoted 1 line
See: lkml.kernel.org/r/CA+55aFwHkOo+YGWKYROmce1-H_uG3KfEUmCkJUerTj=ojY2H6Q@mail.gmail.com
I know. The question is not whether the code must take them into account (it must; it's a bug not to), it's what's a good way to trigger such bugs. IOW, how to stress-test for such bugs?
PTRACE_KILL used to be a convenient way to arrange for a wakeup delivered to victim engaged in something we want to stress; it doesn't do blind wake_up_process() anymore, so that trick is gone. Is there anything similar?
Suppose I have a dodgy waitqueue code (pardon the redundancy) in some filesystem. I have some idea how to maneuver a process into such-and-such part of that code; is there any convenient way to turn that into "... OK, now let's add bombing it with stray wakeups"?
Quoted 4 lines · On Mon, 25 Jan 2016 10:21:46 -0500 Sasha Levin <[email protected]> wrote:
A random wakeup can get us out of
sigsuspend()withoutTIF_SIGPENDINGbeing set.Avoid that by making sure we were signaled, like
sys_pause()does.
What we're lacking here is any description of the end-user-visible effects of the bug. Enough for people to be able to decide (and to recognize!) whether their kernel needs this patch.</stdrefrain>
Quoted 10 lines · Andrew Morton
Quoted 4 lines · On Mon, 25 Jan 2016 10:21:46 -0500 Sasha Levin <[email protected]> wrote:
A random wakeup can get us out of
sigsuspend()withoutTIF_SIGPENDINGbeing set.Avoid that by making sure we were signaled, like
sys_pause()does.What we're lacking here is any description of the end-user-visible effects of the bug.
The warning in dmesg and -ERESTARTNOHAND which we should never return to user space, although I bet nobody checks the error code returned by sigsuspend().
Plus, of course, sys_sigsuspend() can return while it should not.
Quoted 2 lines
Enough for people to be able to decide (and to recognize!) whether their kernel needs this patch.</stdrefrain>
I don't think this problem is really serious, plus it is very unlikely. The spurious return from sigsuspend() should not really hurt.
And, ironically, there is another more serious "reverse" problem ;) sigsuspend() orany other user of -ERESTARTNOHAND can "miss" the signal, in a sense that the kernel can wrongly restart this syscall after return from signal handler. This is not trivial to fix..
Oleg.
Quoted 4 lines · Oleg Nesterov
And, ironically, there is another more serious "reverse" problem ;)
sigsuspend()orany other user of-ERESTARTNOHANDcan "miss" the signal, in a sense that the kernel can wrongly restart this syscall after return from signal handler. This is not trivial to fix..
So I'm not entirely sure I get what you mean there. But it did get me to look at the patch again:
+ while (!signal_pending(current)) {
+ __set_current_state(TASK_INTERRUPTIBLE);
+ schedule();
+ }
That should very much be:
for (;;) {
set_current_state(TASK_INTERRUPTIBLE);
if (signal_pending(current))
break;
schedule();
}
__set_current_state(TASK_RUNNING);Quoted 9 lines · Peter Zijlstra
Quoted 4 lines · Oleg Nesterov
And, ironically, there is another more serious "reverse" problem ;)
sigsuspend()orany other user of-ERESTARTNOHANDcan "miss" the signal, in a sense that the kernel can wrongly restart this syscall after return from signal handler. This is not trivial to fix..So I'm not entirely sure I get what you mean there.
Yes, sorry. When I re-read my email it really looks like "I know the secret but I won't tell you" ;)
The problem is simple, the fix is not. -ERESTARTNOHAND means that we should restart if do_signal()->get_signal() returns zero. This can happen when, say, another thread has already dequeued the group-wide signal which was the reason for TIF_SIGPENDING. Or signal_pending() was true because of debuger, or another reason, doesn't matter.
In this case do_signal() does
regs->ax = regs->orig_ax; regs->ip -= 2;
and we return to user space. Note that after that the kernel can't know that the task is going to restart the syscall which should be interrupted by signals.
Now suppose that another signal (with the handler) comes before this task executes the "syscall" insn. In this case sigsuspend() will restart after the task runs the handler.
Quoted 10 lines
But it did get me to
look at the patch again:kernel/signal.c +4
-0+while (!signal_pending (current)) { +__set_current_state (TASK_INTERRUPTIBLE ); +schedule (); + }That should very much be:
Quoted 7 lines
for (;;) {
set_current_state(TASK_INTERRUPTIBLE);
if (signal_pending(current))
break;
schedule();
}
__set_current_state(TASK_RUNNING);Why? It should work either way. Yes, signal_wakeup() can come right before __set_current_state(TASK_INTERRUPTIBLE) but this is fine, __schedule() must not sleep if signal_pending() == T, that is why it checks signal_pending_state(). See also the comment above smp_mb__before_spinlock() in schedule().
IOW, signal_pending() is the "special" condition, you do not need to serialize this check with task->state setting, exactly because schedule() knows about the signals.
Oleg.
Quoted 3 lines · Oleg Nesterov
Why? It should work either way. Yes,
signal_wakeup()can come right before__set_current_state(TASK_INTERRUPTIBLE)but this is fine,__schedule()must not sleep ifsignal_pending()== T,
Urgh yes, I always forget this :/
Quoted 18 lines · On Wed, 27 Jan 2016 17:41:54 +0100 Oleg Nesterov <[email protected]> wrote:
Quoted 10 lines
But it did get me to
look at the patch again:kernel/signal.c +4
-0+while (!signal_pending (current)) { +__set_current_state (TASK_INTERRUPTIBLE ); +schedule (); + }That should very much be:
Quoted 7 lines
for (;;) { set_current_state(TASK_INTERRUPTIBLE); if (signal_pending(current)) break; schedule(); } __set_current_state(TASK_RUNNING);
Quoted 8 lines
Why? It should work either way. Yes,
signal_wakeup()can come right before__set_current_state(TASK_INTERRUPTIBLE)but this is fine,__schedule()must not sleep ifsignal_pending()== T, that is why it checkssignal_pending_state(). See also the comment abovesmp_mb__before_spinlock()inschedule().IOW,
signal_pending()is the "special" condition, you do not need to serialize this check withtask->statesetting, exactly becauseschedule()knows about the signals.
So it's non-buggy because signal_pending() is special. But it *looks* buggy! And there's no comment there explaining why it looks buggy but isn't, so someone may later come along and "fix" it for us.
Quoted 10 lines · Andrew Morton
Quoted 3 lines · On Wed, 27 Jan 2016 17:41:54 +0100 Oleg Nesterov <[email protected]> wrote:
IOW,
signal_pending()is the "special" condition, you do not need to serialize this check withtask->statesetting, exactly becauseschedule()knows about the signals.So it's non-buggy because
signal_pending()is special. But it *looks* buggy! And there's no comment there explaining why it looks buggy but isn't, so someone may later come along and "fix" it for us.
perhaps we can add a comment somewhere in sched.h to explain that a task can never sleep with task->state == STATE if signal_pending_state(STATE) is true.
Every user of signal_pending() in the wait-event-like loop relies on this well- known fact. Say, wait_event_interruptible() or __mutex_lock_common().
This is actually more about task->state, not about TIF_SIGPENDING imo.
Oleg.
Quoted 17 lines · Peter Zijlstra
Quoted 4 lines · Oleg Nesterov
And, ironically, there is another more serious "reverse" problem ;)
sigsuspend()orany other user of-ERESTARTNOHANDcan "miss" the signal, in a sense that the kernel can wrongly restart this syscall after return from signal handler. This is not trivial to fix..So I'm not entirely sure I get what you mean there. But it did get me to look at the patch again:
kernel/signal.c +4
-0+while (!signal_pending (current)) { +__set_current_state (TASK_INTERRUPTIBLE ); +schedule (); + }That should very much be:
Quoted 7 lines
for (;;) {
set_current_state(TASK_INTERRUPTIBLE);
if (signal_pending(current))
break;
schedule();
}
__set_current_state(TASK_RUNNING);Should that be the case for sys_pause() too?
Thanks,
Sasha
Quoted 17 lines · Sasha Levin
A random wakeup can get us out of
sigsuspend()withoutTIF_SIGPENDINGbeing set.Avoid that by making sure we were signaled, like
sys_pause()does.Signed-off-by Sasha Levin <[email protected]>--- kernel/signal.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)kernel/signal.c +0
-0@@ -3528,8 +3528,10 @@ static int sigsuspend(sigset_t *set) current->saved_sigmask = current->blocked;set_current_blocked (set);
kernel/signal.c static int sigsuspend(sigset_t *set) · line 3537
Quoted 10 lines
kernel/signal.c +4
-2-__set_current_state (TASK_INTERRUPTIBLE ); -schedule (); +while (!signal_pending (current)) { +__set_current_state (TASK_INTERRUPTIBLE ); +schedule (); + }set_restore_sigmask ();return -ERESTARTNOHAND ; }
So this does not appear to be anything new, right?
I agree with the fix, but I'm somewhat worried about the potential ABI impact:
does anything exist out there that has learned to rely on spurious returns from SyS_sigsuspend() or SyS_rt_sigsuspend() system calls? These are one of the most frequently used system calls in signal based event loops.
Thanks,
Ingo
Quoted 33 lines · Ingo Molnar
Quoted 17 lines · Sasha Levin
A random wakeup can get us out of
sigsuspend()withoutTIF_SIGPENDINGbeing set.Avoid that by making sure we were signaled, like
sys_pause()does.Signed-off-by Sasha Levin <[email protected]>--- kernel/signal.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)kernel/signal.c +0
-0@@ -3528,8 +3528,10 @@ static int sigsuspend(sigset_t *set) current->saved_sigmask = current->blocked;set_current_blocked (set);kernel/signal.c static int sigsuspend(sigset_t *set) · line 3537
Quoted 10 lines
kernel/signal.c +4
-2-__set_current_state (TASK_INTERRUPTIBLE ); -schedule (); +while (!signal_pending (current)) { +__set_current_state (TASK_INTERRUPTIBLE ); +schedule (); + }set_restore_sigmask ();return -ERESTARTNOHAND ; }So this does not appear to be anything new, right?
Quoted 3 lines
I agree with the fix, but I'm somewhat worried about the potential ABI impact:
does anything exist out there that has learned to rely on spurious returns fromSyS_sigsuspend()orSyS_rt_sigsuspend()system calls?
Unlikely. We can even forget about set_restore_sigmask/TIF_RESTORE_SIGMASK and WARN_ON(). We are going to return -ERESTARTNOHAND, this assumes that TIF_SIGPENDING must be set and thus do_signal() will be called, userspace should never see this error code. This is even documented in errno.h.
Oleg.