git: ddc7069ba5ad - main - kern/kern_descrip.c: do not leak ctty/textvp/tracevp use counts

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Mon, 23 Nov 2026 00:16:05 UTC
The branch main has been updated by kib:

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

commit ddc7069ba5adf6e8f1536b3206030c12fa8eadd1
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-11-21 04:29:44 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-11-23 00:10:24 +0000

    kern/kern_descrip.c: do not leak ctty/textvp/tracevp use counts
    
    If the export of one of the special process vnodes failed, the
    references on the remaining ones leaked, since export_vnode_to_sb() was
    not called for them.
    
    Each reference is now taken just before its export, with the process
    held in between.
    
    Read the session's ctty vnode and take its reference under the session lock.
    
    Reviewed by:    markj
    Fixes:  36bd49ac4db8 ("fd: Avoid truncating output buffers for KERN_PROC_{CWD,FILEDESC}")
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D61859
---
 sys/kern/kern_descrip.c | 45 +++++++++++++++++++++++++++++----------------
 1 file changed, 29 insertions(+), 16 deletions(-)

diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c
index ebd91146ef06..c36fc16ef19b 100644
--- a/sys/kern/kern_descrip.c
+++ b/sys/kern/kern_descrip.c
@@ -5024,24 +5024,13 @@ kern_proc_filedesc_out(struct proc *p,  struct sbuf *sb, ssize_t maxlen,
 	struct export_fd_buf *efbuf;
 	struct vnode *cttyvp, *textvp, *tracevp;
 	struct pwd *pwd;
+	struct session *sess;
 	int error, i;
 	cap_rights_t rights;
 
 	PROC_LOCK_ASSERT(p, MA_OWNED);
+	_PHOLD(p);
 
-	/* ktrace vnode */
-	tracevp = ktr_get_tracevp(p, true);
-	/* text vnode */
-	textvp = p->p_textvp;
-	if (textvp != NULL)
-		vrefact(textvp);
-	/* Controlling tty. */
-	cttyvp = NULL;
-	if (p->p_pgrp != NULL && p->p_pgrp->pg_session != NULL) {
-		cttyvp = p->p_pgrp->pg_session->s_ttyvp;
-		if (cttyvp != NULL)
-			vrefact(cttyvp);
-	}
 	fdp = fdhold(p);
 	pdp = pdhold(p);
 	PROC_UNLOCK(p);
@@ -5054,15 +5043,39 @@ kern_proc_filedesc_out(struct proc *p,  struct sbuf *sb, ssize_t maxlen,
 	efbuf->flags = flags;
 
 	error = 0;
-	if (tracevp != NULL)
+	PROC_LOCK(p);
+	tracevp = ktr_get_tracevp(p, true);
+	if (tracevp != NULL) {
+		PROC_UNLOCK(p);
 		error = export_vnode_to_sb(tracevp, KF_FD_TYPE_TRACE,
 		    FREAD | FWRITE, efbuf);
-	if (error == 0 && textvp != NULL)
+		PROC_LOCK(p);
+	}
+	if (error == 0 && (textvp = p->p_textvp) != NULL) {
+		vrefact(textvp);
+		PROC_UNLOCK(p);
 		error = export_vnode_to_sb(textvp, KF_FD_TYPE_TEXT, FREAD,
 		    efbuf);
-	if (error == 0 && cttyvp != NULL)
+		PROC_LOCK(p);
+	}
+	while (error == 0) {
+		if (p->p_pgrp == NULL || (sess = p->p_session) == NULL)
+			break;
+		SESS_LOCK(sess);
+		if ((cttyvp = sess->s_ttyvp) == NULL) {
+			SESS_UNLOCK(sess);
+			break;
+		}
+		vrefact(cttyvp);
+		SESS_UNLOCK(sess);
+		PROC_UNLOCK(p);
 		error = export_vnode_to_sb(cttyvp, KF_FD_TYPE_CTTY,
 		    FREAD | FWRITE, efbuf);
+		PROC_LOCK(p);
+		break;
+	}
+	_PRELE(p);
+	PROC_UNLOCK(p);
 	if (error != 0 || pdp == NULL || fdp == NULL)
 		goto fail;
 	efbuf->fdp = fdp;