git: d3bfdc5f0487 - main - ntsync(4): do not leak the priv when devfs_set_cdevpriv() fails

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

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

commit d3bfdc5f048773d530e1873f9d4fe7d0481c7324
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-11-23 16:44:06 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-11-23 18:30:02 +0000

    ntsync(4): do not leak the priv when devfs_set_cdevpriv() fails
    
    ntsync_open() ignored the error from devfs_set_cdevpriv().  The call
    fails when the device is opened without a file, for instance by
    ktrace(2), which opens the trace file with vn_open() and a NULL fp,
    and only then checks the vnode type.  The priv was attached to
    nothing, so its destructor never ran and it leaked.  The device node
    is mode 0666, so any user could leak a priv with each ktrace(2) call.
    
    Free the priv and return the error.
    
    Fixes:          03ca6dbdb80d ("ntsync(4)")
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
---
 sys/dev/ntsync/ntsync.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/sys/dev/ntsync/ntsync.c b/sys/dev/ntsync/ntsync.c
index 49e670de7e59..984a2460a436 100644
--- a/sys/dev/ntsync/ntsync.c
+++ b/sys/dev/ntsync/ntsync.c
@@ -1174,13 +1174,16 @@ static int
 ntsync_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
 {
 	struct ntsync_priv *priv;
+	int error;
 
 	priv = malloc(sizeof(*priv), M_NTSYNC, M_WAITOK);
 	priv->closed = false;
 	priv->objs_cnt = 0;
 	mtx_init(&priv->lock, "ntsync", "ntsync", MTX_DEF | MTX_NEW);
-	devfs_set_cdevpriv(priv, ntsync_priv_dtr);
-	return (0);
+	error = devfs_set_cdevpriv(priv, ntsync_priv_dtr);
+	if (error != 0)
+		ntsync_free_priv(priv);
+	return (error);
 }
 
 static int