git: 92adb5d4cd22 - main - ntsync(4): fix double free and leak of the per-open priv
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Mon, 23 Nov 2026 18:30:09 UTC
The branch main has been updated by kib:
URL: https://cgit.FreeBSD.org/src/commit/?id=92adb5d4cd22e93e1f6539994e30eca9fdcdb250
commit 92adb5d4cd22e93e1f6539994e30eca9fdcdb250
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): fix double free and leak of the per-open priv
ntsync_free_priv() took the priv lock again to test closed and
objs_cnt, after its caller had updated them and dropped the lock.
Once the priv was closed with no objects left, every caller that got
there saw that state. Two threads closing the last two objects, or a
thread closing the last object while another closed the device file,
could both free the priv, or one could lock it after the other had
freed it. Make the decision in the same critical section that
changes closed or objs_cnt, so that only the thread making the
transition frees the priv.
Also, closed was only set by d_close. The cdevsw does not specify
D_TRACKCLOSE, so devfs calls d_close only on the last close of the
device. When the device was opened more than once, every priv except
the one closed last was never marked closed and leaked. Set closed
from the cdevpriv destructor instead, which runs once for each open
file, when the file is closed or the device is destroyed. Remove
d_close, which is no longer needed.
Reported by: Ermenson Junior <ermenson.sec@gmail.com>
Fixes: 03ca6dbdb80d ("ntsync(4)")
Sponsored by: The FreeBSD Foundation
MFC after: 1 week
---
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",
};