select: fix sys_select to not leak ERESTARTNOHAND to userspace
PATCH 1/1 select: fix sys_select to not leak ERESTARTNOHAND to userspace
As it is currently written, sys_select checks its return code to convert ERESTARTNOHAND to EINTR. However, the check is within an if (tvp) clause, and so if select is called from userspace with a NULL timeval, then it is possible for the ERESTARTNOHAND errno to leak into userspace, which is incorrect. This patch moves that check outside of the conditional, and prevents the errno leak.
Thanks & Regards
Neil
select.c | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-)
fs/select.c +5 -13
@@ -415,20 +415,12 @@ asmlinkage long sys_select(int n, fd_set __user *inp, fd_set __user *outp, rtv.tv_sec = timeout;if (timeval_compare (&rtv, &tv) >=0 ) rtv = tv; -if (copy_to_user (tvp, &rtv,sizeof (rtv))) { -sticky: -/* -* If an application puts its timeval in read-only -* memory, we don't want the Linux-specific update to -* the timeval to cause a fault after the select has -* completed successfully. However, because we're not -* updating the timeval, we can't restart the system -* call. -*/ -if (ret == -ERESTARTNOHAND ) - ret = -EINTR ; - } +if (copy_to_user (tvp, &rtv,sizeof (rtv))) +return -EFAULT ; } +sticky: +if (ret == -ERESTARTNOHAND ) + ret = -EINTR ;return ret; }
Quoted 5 lines · On Tue, 16 Jan 2007 15:13:32 -0500 Neil Horman <[email protected]> wrote:
As it is currently written,
sys_selectchecks its return code to convert ERESTARTNOHAND to EINTR. However, the check is within an if (tvp) clause, and so if select is called from userspace with a NULL timeval, then it is possible for the ERESTARTNOHAND errno to leak into userspace, which is incorrect. This patch moves that check outside of the conditional, and prevents the errno leak.
the ERESTARTNOHAND thing is handled in arch specific signal code, syscalls can return -ERESTARTNOHAND as much as they want (and your change breaks the current behaviour of select()).
For example:
arch/x86_64/kernel/signal.c
/* Are we from a system call? */
if ((long)regs->orig_rax >= 0) {
/* If so, check system call restarting.. */
switch (regs->rax) {
case -ERESTART_RESTARTBLOCK:
case -ERESTARTNOHAND:
regs->rax = -EINTR;
break;Signature · 2 lines
Paolo Ornati Linux 2.6.20-rc5 on x86_64
Quoted 10 lines · Paolo Ornati
Quoted 5 lines · On Tue, 16 Jan 2007 15:13:32 -0500 Neil Horman <[email protected]> wrote:
As it is currently written,
sys_selectchecks its return code to convert ERESTARTNOHAND to EINTR. However, the check is within an if (tvp) clause, and so if select is called from userspace with a NULL timeval, then it is possible for the ERESTARTNOHAND errno to leak into userspace, which is incorrect. This patch moves that check outside of the conditional, and prevents the errno leak.the ERESTARTNOHAND thing is handled in arch specific signal code,
In the signal handling path yes.
Not always in the case of select, though. Check core_sys_select:
if (!ret) {
ret = -ERESTARTNOHAND;
if (signal_pending(current))
goto out;
ret = 0;
}...
out:
if (bits != stack_fds)
kfree(bits);
out_nofds:
return ret;Its possible for core_sys_select to return ERESTARTNOHAND to sys_select, which will in turn (as its currently written), return that value back to user space.
Quoted 3 lines
syscalls can return
-ERESTARTNOHANDas much as they want (and your change breaks the current behaviour ofselect()).
It doesn't break it, it fixes it. select isn't meant to ever return ERESTARTNOHAND to user space:
http://www.opengroup.org/onlinepubs/009695399/functions/select.html
And ENORESTARTHAND isn't defined in the userspace errno.h, so this causes all sorts of confusion for apps that don't expect it.
Neil
Quoted 16 lines
For example:
arch/x86_64/kernel/signal.c/* Are we from a system call? */ if ((long)regs->orig_rax >= 0) { /* If so, check system call restarting.. */ switch (regs->rax) { case -ERESTART_RESTARTBLOCK: case -ERESTARTNOHAND: regs->rax = -EINTR; break;Signature · 2 lines
Paolo Ornati Linux 2.6.20-rc5 on x86_64
Quoted 5 lines · Neil Horman
Quoted 2 lines · Paolo Ornati
the ERESTARTNOHAND thing is handled in arch specific signal code,
In the signal handling path yes.
Right.
Quoted 1 line
Not always in the case of select, though. Check
core_sys_select:
No, even in the case of select().
Quoted 5 lines
if (!ret) {
ret = -ERESTARTNOHAND;
if (signal_pending(current))
goto out;
ret = 0;Since we have "signal_pending(current)" being true, we _know_ that the signal handling path will be triggered, so the ERESTARTNOHAND will be changed into the appropriate error return (or restart) by the signal handling code.
Quoted 2 lines
Its possible for
core_sys_selectto return ERESTARTNOHAND tosys_select, which will in turn (as its currently written), return that value back to user space.
No. Exactly because sys_select() will always return through the system call handling path, and that will turn the ERESTARTNOHAND into something else.
NOTE! If you use "ptrace()", you may see the internal errors. But that's a ptrace-only thing, and may have fooled you into thinking that the actual _application_ sees those internal errors. It won't.
Of course, we could have some signal-handling bug here, but if so, it would affect a lot more than just select(). Have you actually seen ERESTARTNOINTR in the app (not just ptrace?)
Linus
Quoted 34 lines · Linus Torvalds
Quoted 5 lines · Neil Horman
Quoted 2 lines · Paolo Ornati
the ERESTARTNOHAND thing is handled in arch specific signal code,
In the signal handling path yes.
Right.
Quoted 1 line
Not always in the case of select, though. Check
core_sys_select:No, even in the case of
select().Quoted 5 lines
if (!ret) { ret = -ERESTARTNOHAND; if (signal_pending(current)) goto out; ret = 0;Since we have "
signal_pending(current)" being true, we _know_ that the signal handling path will be triggered, so the ERESTARTNOHAND will be changed into the appropriate error return (or restart) by the signal handling code.Quoted 2 lines
Its possible for
core_sys_selectto return ERESTARTNOHAND tosys_select, which will in turn (as its currently written), return that value back to user space.No. Exactly because
sys_select()will always return through the system call handling path, and that will turn the ERESTARTNOHAND into something else.
Quoted 8 lines
NOTE! If you use "
ptrace()", you may see the internal errors. But that's a ptrace-only thing, and may have fooled you into thinking that the actual _application_ sees those internal errors. It won't.Of course, we could have some signal-handling bug here, but if so, it would affect a lot more than just
select(). Have you actually seen ERESTARTNOINTR in the app (not just ptrace?)
The error was reported to me second hand. I'm expecting a reproducer (although to date, I'm still waiting for it, so I may have jumped the gun here). In fact, I see what your saying now, down in the assembly glue for our arches (x86 in this case) we jump to do_notify_resume since we have a pending signal, and inside do_signal from there we fix up ERESTARTNOHAND to be something sane for userspace. Ok, I withdraw this patch. I'll repost when/if I get my hands on the reproducer and see that something is actually slipping through.
Neil
Quoted 1 line
Linus
Quoted 2 lines · Neil Horman
The error was reported to me second hand. I'm expecting a reproducer (although to date, I'm still waiting for it, so I may have jumped the gun here). In fact,
I've asked for a repoducer weeks ago and nothing happened, nobody even bothered to reply - I wrote a small test-app that might exhibit the problem, but didn't manage to trigger it.
Bert
Signature · 2 lines
http://www.PowerDNS.com Open source, database driven DNS Software http://netherlabs.nl Open and Closed source services
From: Neil Horman <[email protected]>
Date: Tue, 16 Jan 2007 15:13:32 -0500
Quoted 7 lines
As it is currently written,
sys_selectchecks its return code to convert ERESTARTNOHAND to EINTR. However, the check is within an if (tvp) clause, and so if select is called from userspace with a NULL timeval, then it is possible for the ERESTARTNOHAND errno to leak into userspace, which is incorrect. This patch moves that check outside of the conditional, and prevents the errno leak.Signed-off-by Neil Horman <[email protected]>
As has been probably mentioned, this change is bogus.
core_sys_select() returns -ERESTARTNOHAND in exactly the case where that return value is legal, when a signal is pending, so that the signal dispatch code (on return from the system call) will "fixup" the -ERESTARTNOHAND return value so that userspace does not see it.
What could be happening is that, somehow, we see the signal pending in core_sys_select(), but for some reason by the time the signal dispatch code would run, the signal is cleared.
I always found this scheme a little fishy, relying on signal pending state to guarentee the fixup of the syscall restart error return values.
For example, I see nothing that prevents another thread sharing this signal state from clearing the signal between the syscall checking in the other thread and the signal dispatch check running in that other thread.
cpu 1 cpu 2 check sigpending clear signal syscall return no signal panding get -ERESTARTNOHAND
Perhaps something makes this no happen, but I don't see it :)
Quoted 32 lines · David Miller
From: Neil Horman <[email protected]>
Date: Tue, 16 Jan 2007 15:13:32 -0500Quoted 7 lines
As it is currently written,
sys_selectchecks its return code to convert ERESTARTNOHAND to EINTR. However, the check is within an if (tvp) clause, and so if select is called from userspace with a NULL timeval, then it is possible for the ERESTARTNOHAND errno to leak into userspace, which is incorrect. This patch moves that check outside of the conditional, and prevents the errno leak.Signed-off-by Neil Horman <[email protected]>As has been probably mentioned, this change is bogus.
core_sys_select()returns-ERESTARTNOHANDin exactly the case where that return value is legal, when a signal is pending, so that the signal dispatch code (on return from the system call) will "fixup" the-ERESTARTNOHANDreturn value so that userspace does not see it.What could be happening is that, somehow, we see the signal pending in
core_sys_select(), but for some reason by the time the signal dispatch code would run, the signal is cleared.I always found this scheme a little fishy, relying on signal pending state to guarentee the fixup of the syscall restart error return values.
For example, I see nothing that prevents another thread sharing this signal state from clearing the signal between the syscall checking in the other thread and the signal dispatch check running in that other thread.
Quoted 8 lines
cpu 1 cpu 2 check sigpending clear signal syscall return no signal panding get -ERESTARTNOHANDPerhaps something makes this no happen, but I don't see it :)
This is exactly the theory that I've been tossing around with the person that reported it to me. We're still witing on a reproduer, but I'm currently working on a multithreaded test case to try and trigger user space leaks of ERESTARTNOHAND to user space.
Neil