git: 7df20069d886 - releng/15.2 - ntsync(4): fix double free and leak of the per-open priv

From: Colin Percival <cperciva_at_FreeBSD.org>
Date: Wed, 25 Nov 2026 22:11:10 UTC
The branch releng/15.2 has been updated by cperciva:

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

commit 7df20069d886fa842a12dbe2a6c828d8f9ec22e4
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-11-23 16:44:06 +0000
Commit:     Colin Percival <cperciva@FreeBSD.org>
CommitDate: 2026-11-25 22:08:09 +0000

    ntsync(4): fix double free and leak of the per-open priv
    
    Approved by:    re (cperciva)
    
    (cherry picked from commit 92adb5d4cd22e93e1f6539994e30eca9fdcdb250)
    (cherry picked from commit 74756b1905237f3de6ca8059bc47fc56d579a77c)
---
 sys/dev/ntsync/ntsync.c | 53 +++++++++++++++++++++----------------------------
 1 file changed, 23 insertions(+), 30 deletions(-)

diff --git a/sys/dev/ntsync/ntsync.c b/sys/dev/ntsync/ntsync.c
index c1c3ab6a3d6c..49e670de7e59 100644
--- a/sys/dev/ntsync/ntsync.c
+++ b/sys/dev/ntsync/ntsync.c
@@ -313,14 +313,17 @@ static void
 ntsync_close_obj(struct ntsync_obj *obj, struct thread *td)
 {
 	struct ntsync_priv *priv;
+	bool do_free;
 
 	priv = obj->owner;
 	NTSYNC_PRIV_LOCK(priv);
 	MPASS(priv->objs_cnt > 0);
 	MPASS(TAILQ_EMPTY(&obj->waiters));
 	priv->objs_cnt--;
+	do_free = priv->closed && priv->objs_cnt == 0;
 	NTSYNC_PRIV_UNLOCK(priv);
-	ntsync_free_priv(priv);
+	if (do_free)
+		ntsync_free_priv(priv);
 }
 
 static bool
@@ -1141,21 +1144,30 @@ ntsync_create_event(struct ntsync_event_args *args, struct ntsync_priv *priv,
 static void
 ntsync_free_priv(struct ntsync_priv *priv)
 {
-	bool do_free;
-
-	NTSYNC_PRIV_LOCK(priv);
-	do_free = priv->closed && priv->objs_cnt == 0;
-	NTSYNC_PRIV_UNLOCK(priv);
-	if (do_free) {
-		mtx_destroy(&priv->lock);
-		free(priv, M_NTSYNC);
-	}
+	mtx_destroy(&priv->lock);
+	free(priv, M_NTSYNC);
 }
 
+/*
+ * Called once for each open of the device, when the file is closed
+ * or the device is destroyed.  Objects created through the file keep
+ * the priv alive.  The free is decided in the same critical section
+ * that updates closed or objs_cnt, so exactly one thread does it.
+ */
 static void
 ntsync_priv_dtr(void *data)
 {
-	ntsync_free_priv(data);
+	struct ntsync_priv *priv;
+	bool do_free;
+
+	priv = data;
+	NTSYNC_PRIV_LOCK(priv);
+	MPASS(!priv->closed);
+	priv->closed = true;
+	do_free = priv->objs_cnt == 0;
+	NTSYNC_PRIV_UNLOCK(priv);
+	if (do_free)
+		ntsync_free_priv(priv);
 }
 
 static int
@@ -1171,24 +1183,6 @@ ntsync_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
 	return (0);
 }
 
-static int
-ntsync_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
-{
-	struct ntsync_priv *priv;
-	void *a;
-	int error;
-
-	error = devfs_get_cdevpriv(&a);
-	if (error == 0) {
-		priv = a;
-		NTSYNC_PRIV_LOCK(priv);
-		priv->closed = true;
-		NTSYNC_PRIV_UNLOCK(priv);
-	}
-	devfs_clear_cdevpriv();
-	return (0);
-}
-
 static int
 ntsync_wait_state_get(struct ntsync_wait_args *nwa, u_long cmd,
     struct ntsync_priv *owner, struct ntsync_wait_state **statep,
@@ -1384,7 +1378,6 @@ struct cdevsw ntsync_cdevsw = {
 	.d_version =	D_VERSION,
 	.d_flags =	0,
 	.d_open =	ntsync_open,
-	.d_close =	ntsync_close,
 	.d_ioctl =	ntsync_ioctl,
 	.d_name =	"ntsync",
 };