Reintroduce struct proc::p_oppid

Relying on p_opptr is not safe as there is a race between:
 - spawner giving a birth to a child process and being killed
 - spawnee accessng p_opptr and reporting TRAP_CHLD

PR kern/54786 by Andreas Gustafsson
This commit is contained in:
kamil 2020-04-06 08:20:05 +00:00
parent d26fa7dbd1
commit 848901a664
5 changed files with 17 additions and 12 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: kern_exec.c,v 1.494 2020/04/05 20:53:17 christos Exp $ */
/* $NetBSD: kern_exec.c,v 1.495 2020/04/06 08:20:05 kamil Exp $ */
/*-
* Copyright (c) 2008, 2019, 2020 The NetBSD Foundation, Inc.
@ -62,7 +62,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.494 2020/04/05 20:53:17 christos Exp $");
__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.495 2020/04/06 08:20:05 kamil Exp $");
#include "opt_exec.h"
#include "opt_execfmt.h"
@ -2660,6 +2660,7 @@ do_posix_spawn(struct lwp *l1, pid_t *pid_res, bool *child_ok, const char *path,
if ((p1->p_slflag & (PSL_TRACEPOSIX_SPAWN|PSL_TRACED)) ==
(PSL_TRACEPOSIX_SPAWN|PSL_TRACED)) {
proc_changeparent(p2, p1->p_pptr);
p2->p_oppid = p1->p_pid;
}
LIST_INSERT_AFTER(p1, p2, p_pglist);

View File

@ -1,4 +1,4 @@
/* $NetBSD: kern_fork.c,v 1.220 2020/04/05 20:53:17 christos Exp $ */
/* $NetBSD: kern_fork.c,v 1.221 2020/04/06 08:20:05 kamil Exp $ */
/*-
* Copyright (c) 1999, 2001, 2004, 2006, 2007, 2008, 2019
@ -68,7 +68,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_fork.c,v 1.220 2020/04/05 20:53:17 christos Exp $");
__KERNEL_RCSID(0, "$NetBSD: kern_fork.c,v 1.221 2020/04/06 08:20:05 kamil Exp $");
#include "opt_ktrace.h"
#include "opt_dtrace.h"
@ -511,8 +511,10 @@ fork1(struct lwp *l1, int flags, int exitsig, void *stack, size_t stacksize,
/*
* Trace fork(2) and vfork(2)-like events on demand in a debugger.
*/
if (tracefork(p1, flags) || tracevfork(p1, flags))
if (tracefork(p1, flags) || tracevfork(p1, flags)) {
proc_changeparent(p2, p1->p_pptr);
p2->p_oppid = p1->p_pid;
}
LIST_INSERT_AFTER(p1, p2, p_pglist);
LIST_INSERT_HEAD(&allproc, p2, p_list);

View File

@ -1,4 +1,4 @@
/* $NetBSD: kern_proc.c,v 1.242 2020/02/23 22:14:03 ad Exp $ */
/* $NetBSD: kern_proc.c,v 1.243 2020/04/06 08:20:05 kamil Exp $ */
/*-
* Copyright (c) 1999, 2006, 2007, 2008, 2020 The NetBSD Foundation, Inc.
@ -62,7 +62,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_proc.c,v 1.242 2020/02/23 22:14:03 ad Exp $");
__KERNEL_RCSID(0, "$NetBSD: kern_proc.c,v 1.243 2020/04/06 08:20:05 kamil Exp $");
#ifdef _KERNEL_OPT
#include "opt_kstack.h"
@ -2270,6 +2270,7 @@ fill_proc(const struct proc *psrc, struct proc *p, bool allowaddr)
COND_SET_VALUE(p->p_sigpend, psrc->p_sigpend, allowaddr);
COND_SET_VALUE(p->p_lwpctl, psrc->p_lwpctl, allowaddr);
p->p_ppid = psrc->p_ppid;
p->p_oppid = psrc->p_oppid;
COND_SET_VALUE(p->p_path, psrc->p_path, allowaddr);
COND_SET_VALUE(p->p_sigctx, psrc->p_sigctx, allowaddr);
p->p_nice = psrc->p_nice;

View File

@ -1,4 +1,4 @@
/* $NetBSD: kern_sig.c,v 1.386 2020/04/05 20:53:17 christos Exp $ */
/* $NetBSD: kern_sig.c,v 1.387 2020/04/06 08:20:05 kamil Exp $ */
/*-
* Copyright (c) 2006, 2007, 2008, 2019 The NetBSD Foundation, Inc.
@ -70,7 +70,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.386 2020/04/05 20:53:17 christos Exp $");
__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.387 2020/04/06 08:20:05 kamil Exp $");
#include "opt_ptrace.h"
#include "opt_dtrace.h"
@ -1693,12 +1693,12 @@ eventswitchchild(struct proc *p, int code, int pe_report_event)
{
mutex_enter(proc_lock);
mutex_enter(p->p_lock);
if (!(p->p_slflag & PSL_TRACED) || p->p_opptr == NULL) {
if (!(p->p_slflag & PSL_TRACED)) {
mutex_exit(p->p_lock);
mutex_exit(proc_lock);
return;
}
eventswitch(code, pe_report_event, p->p_opptr->p_pid);
eventswitch(code, pe_report_event, p->p_oppid);
}
/*

View File

@ -1,4 +1,4 @@
/* $NetBSD: proc.h,v 1.361 2020/04/05 20:53:46 christos Exp $ */
/* $NetBSD: proc.h,v 1.362 2020/04/06 08:20:05 kamil Exp $ */
/*-
* Copyright (c) 2006, 2007, 2008, 2020 The NetBSD Foundation, Inc.
@ -311,6 +311,7 @@ struct proc {
sigpend_t p_sigpend; /* p: pending signals */
struct lcproc *p_lwpctl; /* p, a: _lwp_ctl() information */
pid_t p_ppid; /* :: cached parent pid */
pid_t p_oppid; /* :: cached original parent pid */
char *p_path; /* :: full pathname of executable */
/*