Page MenuHomeFreeBSD

D62051.1795707525.diff
No OneTemporary

Authored By
Unknown
Size
9 KB
Referenced Files
None
Subscribers
None

D62051.1795707525.diff

diff --git a/sys/kern/kern_exit.c b/sys/kern/kern_exit.c
--- a/sys/kern/kern_exit.c
+++ b/sys/kern/kern_exit.c
@@ -582,7 +582,13 @@
* Traced processes are killed by default
* since their existence means someone is
* screwing up.
+ *
+ * Clear P_TRACED before reparenting q, as
+ * PT_DETACH does. Otherwise proc_reparent()
+ * links q onto our orphan list, and does not
+ * re-enable waitpid(2) for it.
*/
+ q->p_flag &= ~P_TRACED;
t = proc_realparent(q);
if (t == p) {
proc_reparent(q, q->p_reaper, true);
@@ -591,14 +597,6 @@
proc_reparent(q, t, true);
PROC_UNLOCK(t);
}
- /*
- * Since q was found on our children list, the
- * proc_reparent() call moved q to the orphan
- * list due to present P_TRACED flag. Clear
- * orphan link for q now while q is locked.
- */
- proc_clear_orphan(q);
- q->p_flag &= ~P_TRACED;
q->p_flag2 &= ~P2_PTRACE_FSTP;
q->p_ptevents = 0;
p->p_xthread = NULL;
@@ -1012,6 +1010,7 @@
int zombieref)
{
struct proc *q, *t;
+ bool debugger;
sx_assert(&proctree_lock, SA_XLOCKED);
PROC_LOCK_ASSERT(p, MA_OWNED);
@@ -1033,11 +1032,20 @@
return;
}
- p->p_zombieref &= ~zombieref;
- if ((p->p_zombieref & PZOMBIEREF_REFMASK) != 0) {
- sx_xunlock(&proctree_lock);
- PROC_UNLOCK(p);
- return;
+ /*
+ * A debugger waiting for a child it got via a ptrace 'attach'
+ * holds none of the child's zombie references. Do not drop
+ * any, and give the child back to the old parent below.
+ */
+ debugger = zombieref == PZOMBIEREF_PARENT && p->p_pptr == q &&
+ proc_realparent(p) != q;
+ if (!debugger) {
+ p->p_zombieref &= ~zombieref;
+ if ((p->p_zombieref & PZOMBIEREF_REFMASK) != 0) {
+ sx_xunlock(&proctree_lock);
+ PROC_UNLOCK(p);
+ return;
+ }
}
PROC_LOCK(q);
@@ -1211,6 +1219,23 @@
calccru(p, &rup->ru_utime, &rup->ru_stime);
}
+/*
+ * Is the waiter the debugger of p? exit1() clears P_TRACED of the
+ * exiting process, so for a zombie, rely on the debugger not being
+ * the real parent.
+ */
+static bool
+proc_waiter_is_debugger(struct thread *td, struct proc *p)
+{
+ sx_assert(&proctree_lock, SA_XLOCKED);
+ PROC_LOCK_ASSERT(p, MA_OWNED);
+ if (p->p_pptr != td->td_proc)
+ return (false);
+ if ((p->p_flag & P_TRACED) != 0)
+ return (true);
+ return (p->p_state == PRS_ZOMBIE && proc_realparent(p) != td->td_proc);
+}
+
static int
proc_to_reap(struct thread *td, struct proc *p, idtype_t idtype, id_t id,
int *status, int options, struct __wrusage *wrusage, siginfo_t *siginfo,
@@ -1223,7 +1248,7 @@
switch (idtype) {
case P_ALL:
if ((p->p_zombieref & PZOMBIEREF_PARENT) != 0 ||
- (p->p_pptr == td->td_proc && (p->p_flag & P_TRACED) != 0)) {
+ proc_waiter_is_debugger(td, p)) {
break;
}
@@ -1279,7 +1304,7 @@
((options & WEXITED) == 0 && p->p_state == PRS_ZOMBIE) ||
/* waitpid() is disabled and waiter is not the debugger */
((p->p_zombieref & PZOMBIEREF_PARENT) == 0 &&
- (p->p_pptr != td->td_proc || (p->p_flag & P_TRACED) == 0))) {
+ !proc_waiter_is_debugger(td, p))) {
PROC_UNLOCK(p);
return (0);
}
diff --git a/tests/sys/kern/procdesc.c b/tests/sys/kern/procdesc.c
--- a/tests/sys/kern/procdesc.c
+++ b/tests/sys/kern/procdesc.c
@@ -951,6 +951,131 @@
ATF_REQUIRE_EQ(WTERMSIG(status), SIGKILL);
}
+/*
+ * Child traced by its parent, which is not its reaper. The parent exits
+ * with the descriptor still open, so its exit closes the descriptor and
+ * then gives the child to the reaper, which must be able to collect it.
+ */
+ATF_TC_WITHOUT_HEAD(reaper_grandchild_close_traced_exit);
+ATF_TC_BODY(reaper_grandchild_close_traced_exit, tc)
+{
+ pid_t child, helper;
+ int fd, pip[2], rdy[2], status;
+ char c;
+
+ /* Become the reaper of the helper's child. */
+ ATF_REQUIRE_MSG(procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL) == 0,
+ "PROC_REAP_ACQUIRE: %s", strerror(errno));
+ ATF_REQUIRE_EQ(pipe(pip), 0);
+
+ helper = fork();
+ ATF_REQUIRE_MSG(helper >= 0, "fork: %s", strerror(errno));
+ if (helper == 0) {
+ close(pip[0]);
+ if (pipe(rdy) != 0)
+ _exit(1);
+ child = pdfork(&fd, PD_DAEMON | PD_NOWAITPID);
+ if (child < 0)
+ _exit(2);
+ if (child == 0) {
+ close(rdy[0]);
+ traceme_and_stop(fd, rdy[1]);
+ }
+ close(rdy[1]);
+
+ /* The child is invisible to waitpid(2) until traced. */
+ if (read(rdy[0], &c, 1) != 1)
+ _exit(3);
+ if (waitpid(child, &status, 0) != child || !WIFSTOPPED(status))
+ _exit(4);
+ if (write(pip[1], &child, sizeof(child)) != sizeof(child))
+ _exit(5);
+ /* Exit with the descriptor open, still tracing the child. */
+ _exit(0);
+ }
+ ATF_REQUIRE(close(pip[1]) == 0);
+
+ ATF_REQUIRE_EQ(helper, waitpid(helper, &status, WEXITED));
+ ATF_REQUIRE_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 0,
+ "helper failed, status %#x", status);
+ ATF_REQUIRE_EQ(read(pip[0], &child, sizeof(child)),
+ (ssize_t)sizeof(child));
+
+ /*
+ * The debugger's exit killed the child, unless
+ * kern.kill_on_debugger_exit is turned off.
+ */
+ (void)kill(child, SIGKILL);
+ ATF_REQUIRE_EQ_MSG(child, waitpid(child, &status, WEXITED),
+ "waitpid: %s", strerror(errno));
+ ATF_REQUIRE(WIFSIGNALED(status));
+ ATF_REQUIRE_EQ(WTERMSIG(status), SIGKILL);
+}
+
+/*
+ * Child traced by its parent, which is not its reaper. The parent closes
+ * the descriptor and kills the child. As the debugger, it must be able to
+ * wait for the child, which then goes to the reaper.
+ */
+ATF_TC_WITHOUT_HEAD(reaper_grandchild_close_traced_wait);
+ATF_TC_BODY(reaper_grandchild_close_traced_wait, tc)
+{
+ pid_t child, helper;
+ int fd, pip[2], rdy[2], status;
+ char c;
+
+ /* Become the reaper of the helper's child. */
+ ATF_REQUIRE_MSG(procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL) == 0,
+ "PROC_REAP_ACQUIRE: %s", strerror(errno));
+ ATF_REQUIRE_EQ(pipe(pip), 0);
+
+ helper = fork();
+ ATF_REQUIRE_MSG(helper >= 0, "fork: %s", strerror(errno));
+ if (helper == 0) {
+ close(pip[0]);
+ if (pipe(rdy) != 0)
+ _exit(1);
+ child = pdfork(&fd, PD_DAEMON | PD_NOWAITPID);
+ if (child < 0)
+ _exit(2);
+ if (child == 0) {
+ close(rdy[0]);
+ traceme_and_stop(fd, rdy[1]);
+ }
+ close(rdy[1]);
+
+ /* The child is invisible to waitpid(2) until traced. */
+ if (read(rdy[0], &c, 1) != 1)
+ _exit(3);
+ if (waitpid(child, &status, 0) != child || !WIFSTOPPED(status))
+ _exit(4);
+ if (close(fd) != 0)
+ _exit(5);
+ /* A stopped tracee does not act on kill(2). */
+ if (ptrace(PT_KILL, child, NULL, 0) != 0)
+ _exit(6);
+ if (waitpid(child, &status, 0) != child ||
+ !WIFSIGNALED(status) || WTERMSIG(status) != SIGKILL)
+ _exit(7);
+ if (write(pip[1], &child, sizeof(child)) != sizeof(child))
+ _exit(8);
+ _exit(0);
+ }
+ ATF_REQUIRE(close(pip[1]) == 0);
+
+ ATF_REQUIRE_EQ(helper, waitpid(helper, &status, WEXITED));
+ ATF_REQUIRE_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 0,
+ "helper failed, status %#x", status);
+ ATF_REQUIRE_EQ(read(pip[0], &child, sizeof(child)),
+ (ssize_t)sizeof(child));
+
+ /* The debugger's wait handed the zombie over to us. */
+ ATF_REQUIRE_EQ_MSG(child, waitpid(child, &status, WEXITED),
+ "waitpid: %s", strerror(errno));
+ ATF_REQUIRE(WIFSIGNALED(status));
+ ATF_REQUIRE_EQ(WTERMSIG(status), SIGKILL);
+}
+
ATF_TP_ADD_TCS(tp)
{
ATF_TP_ADD_TC(tp, pid_recycle);
@@ -978,6 +1103,8 @@
ATF_TP_ADD_TC(tp, reaper_child_close);
ATF_TP_ADD_TC(tp, reaper_child_close_traced);
ATF_TP_ADD_TC(tp, reaper_child_close_traced_exit);
+ ATF_TP_ADD_TC(tp, reaper_grandchild_close_traced_exit);
+ ATF_TP_ADD_TC(tp, reaper_grandchild_close_traced_wait);
return (atf_no_error());
}
diff --git a/tests/sys/kern/ptrace_test.c b/tests/sys/kern/ptrace_test.c
--- a/tests/sys/kern/ptrace_test.c
+++ b/tests/sys/kern/ptrace_test.c
@@ -4317,8 +4317,8 @@
}
/*
- * Ensure that traced processes created with pdfork(2) are not visible
- * after returning to parent - waitid(P_ALL).
+ * Ensure that traced processes created with pdfork(PD_NOWAITPID) are not
+ * visible after returning to parent - waitid(P_ALL).
*/
ATF_TC_WITHOUT_HEAD(ptrace__procdesc_reparent_wait_child);
ATF_TC_BODY(ptrace__procdesc_reparent_wait_child, tc)
@@ -4326,7 +4326,7 @@
pid_t traced, debuger, wpid;
int pd, status;
- traced = pdfork(&pd, 0);
+ traced = pdfork(&pd, PD_NOWAITPID);
ATF_REQUIRE(traced >= 0);
if (traced == 0) {
raise(SIGSTOP);
@@ -4334,8 +4334,11 @@
}
ATF_REQUIRE(pd >= 0);
- /* Wait until the child process has stopped before fork()ing again. */
- REQUIRE_EQ(traced, waitpid(traced, &status, WSTOPPED));
+ /*
+ * Wait until the child process has stopped before fork()ing again.
+ * The child is invisible to waitpid(2), use its process descriptor.
+ */
+ REQUIRE_EQ(pdwait(pd, &status, WSTOPPED, NULL, NULL), 0);
debuger = fork();
ATF_REQUIRE(debuger >= 0);
if (debuger == 0) {
@@ -4363,8 +4366,8 @@
REQUIRE_EQ(WEXITSTATUS(status), 0);
/*
- * We have a child but it has a process descriptori
- * so we should not be able to collect it process.
+ * We have a child, but it was created with PD_NOWAITPID,
+ * so we should not be able to collect it.
*/
wpid = wait(&status);
REQUIRE_EQ(wpid, -1);

File Metadata

Mime Type
text/plain
Expires
Thu, Nov 26, 3:38 PM (9 h, 44 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42013217
Default Alt Text
D62051.1795707525.diff (9 KB)

Event Timeline