git: 916b63aeb648 - stable/14 - pam_exec: fix the stderr close test and avoid a double close

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Fri, 13 Nov 2026 22:07:49 UTC
The branch stable/14 has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=916b63aeb6485af3cf3f46b61ccce138e04800b7

commit 916b63aeb6485af3cf3f46b61ccce138e04800b7
Author:     Osvaldo Janeri Filho <ojanerif@amd.com>
AuthorDate: 2026-10-20 22:04:17 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-11-13 22:07:31 +0000

    pam_exec: fix the stderr close test and avoid a double close
    
    The cleanup tested cherr[0] when closing cherr[1].  The parent also closed
    chin[0]/chout[1]/cherr[1] after pdfork() without clearing them, so the out:
    cleanup path closed them again.  Clear each fd after closing and test the right
    one.
    
    Reviewed by:    des, markj
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D60924
    
    (cherry picked from commit 06abe08de45d251410949eac1bbb025367275260)
---
 lib/libpam/modules/pam_exec/pam_exec.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/lib/libpam/modules/pam_exec/pam_exec.c b/lib/libpam/modules/pam_exec/pam_exec.c
index c249a654c8be..053f970c3808 100644
--- a/lib/libpam/modules/pam_exec/pam_exec.c
+++ b/lib/libpam/modules/pam_exec/pam_exec.c
@@ -336,12 +336,18 @@ _pam_exec(pam_handle_t *pamh,
 		OUT(PAM_SYSTEM_ERR);
 	}
 	/* use poll() to watch the process and stdin / stdout / stderr */
-	if (chin[0] >= 0)
+	if (chin[0] >= 0) {
 		close(chin[0]);
-	if (chout[1] >= 0)
+		chin[0] = -1;
+	}
+	if (chout[1] >= 0) {
 		close(chout[1]);
-	if (cherr[1] >= 0)
+		chout[1] = -1;
+	}
+	if (cherr[1] >= 0) {
 		close(cherr[1]);
+		cherr[1] = -1;
+	}
 	memset(pfd, 0, sizeof pfd);
 	pfd[0].fd = pd;
 	pfd[0].events = POLLHUP;
@@ -452,7 +458,7 @@ out:
 		close(chout[1]);
 	if (cherr[0] >= 0)
 		close(cherr[0]);
-	if (cherr[0] >= 0)
+	if (cherr[1] >= 0)
 		close(cherr[1]);
 	if (envlist != NULL)
 		openpam_free_envlist(envlist);