[openssh-commits] [openssh] 02/03: upstream: don't let the monitor kill a stale unpriv child PID

git+noreply at mindrot.org git+noreply at mindrot.org
Fri Nov 27 09:34:11 AEDT 2026


This is an automated email from the git hooks/post-receive script.

djm pushed a commit to branch master
in repository openssh.

commit dca5a910317eb8d9c287755e131d68cad9637c8b
Author: djm at openbsd.org <djm at openbsd.org>
AuthorDate: Thu Nov 26 22:16:57 2026 +0000

    upstream: don't let the monitor kill a stale unpriv child PID
    
    Anthropic ANT-2026-6V17471X
    
    OpenBSD-Commit-ID: 147b5c13afc322cb5ec6568d74dc767e26a34379
---
 monitor.c      |  7 ++++++-
 monitor.h      |  3 ++-
 monitor_wrap.c | 12 +++++++++++-
 sshd-session.c |  7 ++++---
 4 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/monitor.c b/monitor.c
index 749e00815..9eadc80be 100644
--- a/monitor.c
+++ b/monitor.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: monitor.c,v 1.264 2026/11/25 20:42:32 djm Exp $ */
+/* $OpenBSD: monitor.c,v 1.265 2026/11/26 22:16:57 djm Exp $ */
 /*
  * Copyright 2002 Niels Provos <provos at citi.umich.edu>
  * Copyright 2002 Markus Friedl <markus at openbsd.org>
@@ -408,6 +408,7 @@ monitor_child_preauth(struct ssh *ssh, struct monitor *pmonitor)
 			continue;
 		fatal_f("waitpid: %s", strerror(errno));
 	}
+	pmonitor->m_child_reaped = 1;
 	if (WIFEXITED(status)) {
 		if (WEXITSTATUS(status) != 0)
 			fatal_f("preauth child %ld exited with status %d",
@@ -1803,6 +1804,10 @@ mm_answer_term(struct ssh *ssh, int sock, struct sshbuf *req)
 		sshpam_cleanup();
 #endif
 
+	/* Don't forward signals to the child's PID once it has been reaped */
+	ssh_signal(SIGHUP, SIG_IGN);
+	ssh_signal(SIGTERM, SIG_IGN);
+	ssh_signal(SIGINT, SIG_IGN);
 	while (waitpid(pmonitor->m_pid, &status, 0) == -1)
 		if (errno != EINTR)
 			exit(1);
diff --git a/monitor.h b/monitor.h
index d5a1c5d24..7d0a0307f 100644
--- a/monitor.h
+++ b/monitor.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: monitor.h,v 1.28 2026/03/02 02:40:15 djm Exp $ */
+/* $OpenBSD: monitor.h,v 1.29 2026/11/26 22:16:57 djm Exp $ */
 
 /*
  * Copyright 2002 Niels Provos <provos at citi.umich.edu>
@@ -76,6 +76,7 @@ struct monitor {
 	int			 m_log_sendfd;
 	struct kex		**m_pkex;
 	pid_t			 m_pid;
+	int			 m_child_reaped;
 };
 
 struct monitor *monitor_init(void);
diff --git a/monitor_wrap.c b/monitor_wrap.c
index 3df3121ed..216337be8 100644
--- a/monitor_wrap.c
+++ b/monitor_wrap.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: monitor_wrap.c,v 1.153 2026/10/30 05:38:16 djm Exp $ */
+/* $OpenBSD: monitor_wrap.c,v 1.154 2026/11/26 22:16:57 djm Exp $ */
 /*
  * Copyright 2002 Niels Provos <provos at citi.umich.edu>
  * Copyright 2002 Markus Friedl <markus at openbsd.org>
@@ -123,12 +123,22 @@ mm_reap(void)
 
 	if (!mm_is_monitor())
 		return;
+	/*
+	 * Once the child has been reaped its PID may be reused by an
+	 * unrelated process, so stop forwarding signals to it and record
+	 * that it is gone for cleanup_exit(). m_pid must remain set as
+	 * mm_is_monitor() depends on it.
+	 */
+	ssh_signal(SIGHUP, SIG_IGN);
+	ssh_signal(SIGTERM, SIG_IGN);
+	ssh_signal(SIGINT, SIG_IGN);
 	while (waitpid(pmonitor->m_pid, &status, 0) == -1) {
 		if (errno == EINTR)
 			continue;
 		pmonitor->m_pid = -1;
 		fatal_f("waitpid: %s", strerror(errno));
 	}
+	pmonitor->m_child_reaped = 1;
 	if (WIFEXITED(status)) {
 		if (WEXITSTATUS(status) != 0) {
 			debug_f("child exited with status %d",
diff --git a/sshd-session.c b/sshd-session.c
index e8f29980d..1d2485a30 100644
--- a/sshd-session.c
+++ b/sshd-session.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: sshd-session.c,v 1.28 2026/11/25 20:42:32 djm Exp $ */
+/* $OpenBSD: sshd-session.c,v 1.29 2026/11/26 22:16:57 djm Exp $ */
 /*
  * SSH2 implementation:
  * Privilege Separation:
@@ -383,6 +383,7 @@ privsep_postauth(struct ssh *ssh, Authctxt *authctxt)
 	/* New socket pair */
 	monitor_reinit(pmonitor);
 
+	pmonitor->m_child_reaped = 0;
 	pmonitor->m_pid = fork();
 	if (pmonitor->m_pid == -1)
 		fatal("fork of unprivileged child failed");
@@ -1350,8 +1351,8 @@ cleanup_exit(int i)
 {
 	if (the_active_state != NULL && the_authctxt != NULL) {
 		do_cleanup(the_active_state, the_authctxt);
-		if (privsep_is_preauth &&
-		    pmonitor != NULL && pmonitor->m_pid > 1) {
+		if (privsep_is_preauth && pmonitor != NULL &&
+		    pmonitor->m_pid > 1 && !pmonitor->m_child_reaped) {
 			debug("Killing privsep child %d", pmonitor->m_pid);
 			if (kill(pmonitor->m_pid, SIGKILL) != 0 &&
 			    errno != ESRCH) {

-- 
To stop receiving notification emails like this one, please contact
djm at mindrot.org.


More information about the openssh-commits mailing list