wait_for_helper: SIGCHLD from user-space can lead to use-after-free
PATCH 1/1 wait_for_helper: SIGCHLD from user-space can lead to use-after-free
(this patch is orthogonal to other umh changes in -mm)
1. wait_for_helper() does allow_signal(SIGCHLD) to ensure the child can't autoreap itself.
However, this means that a spurious SIGCHILD from user-space can set TIF_SIGPENDING and:
- kernel_thread() or sys_wait4() can fail due to signal_pending()
- worse, wait4() can fail before ____call_usermodehelper() execs or exits. In this case the caller may kfree(subprocess_info) while the child still uses this memory.
Change the code to use SIG_DFL instead of magic "(void __user *)2" set by allow_signal(). This means that SIGCHLD won't be delivered, yet the child won't autoreap itsefl.
The problem is minor, only root can send a signal to this kthread.
2. If sys_wait4(&ret) fails it doesn't populate "ret", in this case wait_for_helper() reports a random value from uninitialized var.
With this patch sys_wait4() should never fail, but still it makes sense to initialize ret = -ECHILD so that the caller can notice the problem.
---
kernel/kmod.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
kernel/kmod.c renamed from kernel/kmod.c~4_SIGCHLD_RACE +5 -4
@@ -194,15 +194,16 @@ static int wait_for_helper(void *data)struct subprocess_info *sub_info = data;pid_t pid; -/* Install a handler: if SIGCLD isn't handled sys_wait4 won't -* populate the status, but will return -ECHILD. */ -allow_signal (SIGCHLD ); +/* If SIGCLD is ignored sys_wait4 won't populate the status. */ +spin_lock_irq (¤t->sighand->siglock); + current->sighand->action[SIGCHLD -1 ].sa.sa_handler =SIG_DFL ; +spin_unlock_irq (¤t->sighand->siglock); pid =kernel_thread (____call_usermodehelper, sub_info,SIGCHLD );if (pid <0 ) { sub_info->retval = pid; }else { -int ret; +int ret = -ECHILD ;/* * Normally it is bogus to call wait4() from in-kernel because
Quoted 20 lines · Oleg Nesterov
(this patch is orthogonal to other umh changes in -mm)
1.
wait_for_helper()doesallow_signal(SIGCHLD)to ensure the child can't autoreap itself.However, this means that a spurious SIGCHILD from user-space can set
TIF_SIGPENDINGand:-
kernel_thread()orsys_wait4()can fail due tosignal_pending()- worse,
wait4()can fail before____call_usermodehelper()execs or exits. In this case the caller maykfree(subprocess_info)while the child still uses this memory.Change the code to use
SIG_DFLinstead of magic "(void__user*)2" set byallow_signal(). This means that SIGCHLD won't be delivered, yet the child won't autoreap itsefl.The problem is minor, only root can send a signal to this kthread.
Quoted 8 lines
2. If
sys_wait4(&ret)fails it doesn't populate "ret", in this casewait_for_helper()reports a random value from uninitialized var.With this patch
sys_wait4()should never fail, but still it makes sense to initialize ret =-ECHILDso that the caller can notice the problem.Signed-off-by Oleg Nesterov <[email protected]>
Quoted 1 line
SIGCHLD being blocked doesn't affect reaping, so SIG_IGN or sa_flags & SA_NOCLDWAIT is the only thing that would do this. How does that come about here in this kthread? Is it inherited from the instigating user process? If so, then SA_NOCLDWAIT is as much a problem as SIG_IGN. Or I guess maybe it's from ignore_signals() in kthreadd()? In that case SIG_IGN is indeed all that matters. (I don't really know all the kthread/kmod/userhelper code organization.)
Perhaps it would be cleaner to do:
flush_signal_handlers(current, 1);
in wait_for_helper. That should make it redundant in ____call_usermodehelper, so it could be removed from there.
Thanks,
Roland
Quoted 8 lines · Roland McGrath
SIGCHLD being blocked doesn't affect reaping, so
SIG_IGNorsa_flags&SA_NOCLDWAITis the only thing that would do this. How does that come about here in this kthread? Is it inherited from the instigating user process? If so, thenSA_NOCLDWAITis as much a problem asSIG_IGN. Or I guess maybe it's fromignore_signals()inkthreadd()? In that caseSIG_IGNis indeed all that matters. (I don't really know all the kthread/kmod/userhelper code organization.)
Yes. kthreads run with all signal ignored, this is inherited from kthreadd() which does ignore_signal().
Quoted 5 lines
Perhaps it would be cleaner to do:
flush_signal_handlers(current, 1);in
wait_for_helper
I don't think this can work. SIG_DFL for SIGCHLD is OK because it is sig_kernel_ignore(). But, say, SIGHUP and other signals still should be ignored, otherwise we have the same problems with the unwanted signal_pending() this patch tries to avoid.
But even if we could do this,
Quoted 2 lines
That should make it redundant in
____call_usermodehelper, so it could be removed from there.
Please note that __call_usermodehelper() forks ____call_usermodehelper() too.
Oleg.
Quoted 2 lines
Yes. kthreads run with all signal ignored, this is inherited from
kthreadd()which doesignore_signal().
Ok.
Quoted 4 lines
I don't think this can work.
SIG_DFLfor SIGCHLD is OK because it issig_kernel_ignore(). But, say, SIGHUP and other signals still should be ignored, otherwise we have the same problems with the unwantedsignal_pending()this patch tries to avoid.
Yes, I see.
Quoted 6 lines
But even if we could do this,
Quoted 2 lines
That should make it redundant in
____call_usermodehelper, so it could be removed from there.Please note that
__call_usermodehelper()forks____call_usermodehelper()too.
Ok.
Thanks,
Roland