Add a paranoid racy lock check in child_return()

In theory a child could be detached for some reason or another during
the time window between checking for PSL_TRACED and acquiring proc_lock.

Acquire the proc_lock mutex and recheck for PSL_TRACED before emitting
SIGTRAP. sigswitch() must acquite it internally anyway so this does not
have a negative impact and adds an extra sanity check.

For !PSL_TRACED case there is no impact.
This commit is contained in:
kamil 2019-04-07 14:50:41 +00:00
parent d340589347
commit 3b9e60c4b5
1 changed files with 11 additions and 3 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: kern_fork.c,v 1.208 2019/04/06 11:54:21 kamil Exp $ */
/* $NetBSD: kern_fork.c,v 1.209 2019/04/07 14:50:41 kamil Exp $ */
/*-
* Copyright (c) 1999, 2001, 2004, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@ -67,7 +67,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_fork.c,v 1.208 2019/04/06 11:54:21 kamil Exp $");
__KERNEL_RCSID(0, "$NetBSD: kern_fork.c,v 1.209 2019/04/07 14:50:41 kamil Exp $");
#include "opt_ktrace.h"
#include "opt_dtrace.h"
@ -619,16 +619,24 @@ child_return(void *arg)
struct proc *p = l->l_proc;
if (p->p_slflag & PSL_TRACED) {
/* Paranoid check */
mutex_enter(proc_lock);
if (!(p->p_slflag & PSL_TRACED)) {
mutex_exit(proc_lock);
goto my_tracer_is_gone;
}
mutex_enter(p->p_lock);
p->p_xsig = SIGTRAP;
p->p_sigctx.ps_faked = true; // XXX
p->p_sigctx.ps_info._signo = p->p_xsig;
p->p_sigctx.ps_info._code = TRAP_CHLD;
sigswitch(0, SIGTRAP, true);
sigswitch(0, SIGTRAP, false);
// XXX ktrpoint(KTR_PSIG)
mutex_exit(p->p_lock);
}
my_tracer_is_gone:
md_child_return(l);
/*