public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] wait_for_helper: SIGCHLD from user-space can lead to use-after-free
@ 2010-03-10 17:16 Oleg Nesterov
  2010-03-10 18:07 ` Neil Horman
  2010-03-10 20:32 ` Roland McGrath
  0 siblings, 2 replies; 5+ messages in thread
From: Oleg Nesterov @ 2010-03-10 17:16 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Andi Kleen, David Howells, Neil Horman, Roland McGrath,
	Rusty Russell, linux-kernel

(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.

Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---

 kernel/kmod.c |    9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

--- mm/kernel/kmod.c~4_SIGCHLD_RACE	2010-03-09 22:23:37.000000000 +0100
+++ mm/kernel/kmod.c	2010-03-10 17:36:37.000000000 +0100
@@ -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(&current->sighand->siglock);
+	current->sighand->action[SIGCHLD-1].sa.sa_handler = SIG_DFL;
+	spin_unlock_irq(&current->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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] wait_for_helper: SIGCHLD from user-space can lead to use-after-free
  2010-03-10 17:16 [PATCH] wait_for_helper: SIGCHLD from user-space can lead to use-after-free Oleg Nesterov
@ 2010-03-10 18:07 ` Neil Horman
  2010-03-10 20:32 ` Roland McGrath
  1 sibling, 0 replies; 5+ messages in thread
From: Neil Horman @ 2010-03-10 18:07 UTC (permalink / raw)
  To: Oleg Nesterov
  Cc: Andrew Morton, Andi Kleen, David Howells, Roland McGrath,
	Rusty Russell, linux-kernel

On Wed, Mar 10, 2010 at 06:16:34PM +0100, Oleg Nesterov wrote:
> (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.
> 
> Signed-off-by: Oleg Nesterov <oleg@redhat.com>
Acked-by: Neil Horman <nhorman@tuxdriver.com>

> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] wait_for_helper: SIGCHLD from user-space can lead to use-after-free
  2010-03-10 17:16 [PATCH] wait_for_helper: SIGCHLD from user-space can lead to use-after-free Oleg Nesterov
  2010-03-10 18:07 ` Neil Horman
@ 2010-03-10 20:32 ` Roland McGrath
  2010-03-10 21:19   ` Oleg Nesterov
  1 sibling, 1 reply; 5+ messages in thread
From: Roland McGrath @ 2010-03-10 20:32 UTC (permalink / raw)
  To: Oleg Nesterov
  Cc: Andrew Morton, Andi Kleen, David Howells, Neil Horman,
	Rusty Russell, linux-kernel

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] wait_for_helper: SIGCHLD from user-space can lead to use-after-free
  2010-03-10 20:32 ` Roland McGrath
@ 2010-03-10 21:19   ` Oleg Nesterov
  2010-03-10 21:27     ` Roland McGrath
  0 siblings, 1 reply; 5+ messages in thread
From: Oleg Nesterov @ 2010-03-10 21:19 UTC (permalink / raw)
  To: Roland McGrath
  Cc: Andrew Morton, Andi Kleen, David Howells, Neil Horman,
	Rusty Russell, linux-kernel

On 03/10, Roland McGrath wrote:
>
> 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.)

Yes. kthreads run with all signal ignored, this is inherited from
kthreadd() which does ignore_signal().

> 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,

> 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.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] wait_for_helper: SIGCHLD from user-space can lead to use-after-free
  2010-03-10 21:19   ` Oleg Nesterov
@ 2010-03-10 21:27     ` Roland McGrath
  0 siblings, 0 replies; 5+ messages in thread
From: Roland McGrath @ 2010-03-10 21:27 UTC (permalink / raw)
  To: Oleg Nesterov
  Cc: Andrew Morton, Andi Kleen, David Howells, Neil Horman,
	Rusty Russell, linux-kernel

> Yes. kthreads run with all signal ignored, this is inherited from
> kthreadd() which does ignore_signal().

Ok.

> 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.

Yes, I see.

> But even if we could do this,
> 
> > 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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2010-03-10 21:28 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-03-10 17:16 [PATCH] wait_for_helper: SIGCHLD from user-space can lead to use-after-free Oleg Nesterov
2010-03-10 18:07 ` Neil Horman
2010-03-10 20:32 ` Roland McGrath
2010-03-10 21:19   ` Oleg Nesterov
2010-03-10 21:27     ` Roland McGrath

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox