DragonFlyBSD Kernel Audit
DF-0911 / fix.diff
← back to finding ↓ download raw
diff --git a/sys/vfs/procfs/procfs_ctl.c b/sys/vfs/procfs/procfs_ctl.c
--- a/sys/vfs/procfs/procfs_ctl.c
+++ b/sys/vfs/procfs/procfs_ctl.c
@@ -150,10 +150,16 @@
 		 */
 		p->p_flags |= P_TRACED;
 		p->p_xstat = 0;		/* XXX ? */
-		if (p->p_pptr != curp) {
-			p->p_oppid = p->p_pptr->p_pid;
+		/*
+		 * Always save the original parent pid so DETACH can restore
+		 * the parenting correctly even when the tracer is already the
+		 * real parent of the target (otherwise p_oppid stays 0 and
+		 * DETACH hands the child to proc0, orphaning it).  This mirrors
+		 * ptrace(2) PT_ATTACH in kern/sys_process.c.
+		 */
+		p->p_oppid = p->p_pptr->p_pid;
+		if (p->p_pptr != curp)
 			proc_reparent(p, curp);
-		}
 		proc_stop(p, SSTOP);
 		return (0);
 	}