NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: kern/60729: incosistent st_ino from the first stat(2) on a socket



Try attached?
# HG changeset patch
# User Taylor R Campbell <riastradh%NetBSD.org@localhost>
# Date 1789490537 0
#      Tue Sep 15 16:42:17 2026 +0000
# Branch trunk
# Node ID 3fe1ba99ddf50e054ee728621b78ed85d138cc5d
# Parent  4fbd288ac1da9e7d9592ddecf04f7513e244152f
# EXP-Topic riastradh-pr60729-unixsockino
uipc_usrreq: Fix unix-domain socket inode assignment.

1. `if (unp->unp_ino == 0) unp->unp_ino = unp_ino++' would assign
   zero the first time around by mistake, and then assign nonzero
   after that.  `if (unp->unp_ino == 0) unp->unp_ino = ++unp_ino'
   avoids this silliness.

2. Make the ++unp_ino part atomic.

PR kern/60729: incosistent st_ino from the first stat(2) on a socket

diff -r 4fbd288ac1da -r 3fe1ba99ddf5 sys/kern/uipc_usrreq.c
--- a/sys/kern/uipc_usrreq.c	Mon Aug 10 13:36:10 2026 +0000
+++ b/sys/kern/uipc_usrreq.c	Tue Sep 15 16:42:17 2026 +0000
@@ -179,7 +179,16 @@ const struct sockaddr_un sun_noname = {
 	.sun_len = offsetof(struct sockaddr_un, sun_path),
 	.sun_family = AF_LOCAL,
 };
-ino_t	unp_ino;			/* prototype for fake inode numbers */
+
+/* prototype for fake inode numbers */
+#ifdef __HAVE_ATOMIC64_OPS
+volatile uint64_t	unp_ino __cacheline_aligned;
+#else
+struct {
+	uint64_t	num;
+	kmutex_t	lock;
+} unp_ino __cacheline_aligned;
+#endif
 
 static struct mbuf * unp_addsockcred(struct lwp *, struct mbuf *);
 static void   unp_discard_later(file_t *);
@@ -233,6 +242,10 @@ uipc_init(void)
 	    NULL, &unp_thread_lwp, "unpgc");
 	if (error != 0)
 		panic("uipc_init %d", error);
+
+#ifndef __HAVE_ATOMIC64_OPS
+	mutex_init(&unp_ino.lock, MUTEX_DEFAULT, IPL_NONE);
+#endif
 }
 
 static void
@@ -906,8 +919,15 @@ unp_stat(struct socket *so, struct stat 
 		break;
 	}
 	ub->st_dev = NODEV;
-	if (unp->unp_ino == 0)
-		unp->unp_ino = unp_ino++;
+	if (unp->unp_ino == 0) {
+#ifdef __HAVE_ATOMIC64_OPS
+		unp->unp_ino = atomic_inc_64_nv(&unp_ino);
+#else
+		mutex_enter(&unp_ino.lock);
+		unp->unp_ino = ++unp_ino.num;
+		mutex_exit(&unp_ino.lock);
+#endif
+	}
 	ub->st_atimespec = ub->st_mtimespec = ub->st_ctimespec = unp->unp_ctime;
 	ub->st_ino = unp->unp_ino;
 	ub->st_uid = so->so_uidinfo->ui_uid;


Home | Main Index | Thread Index | Old Index