git: d5cbb6229f53 - main - lindebugfs: Do not expose a node before its private data is set

From: Emmanuel Vadot <manu_at_FreeBSD.org>
Date: Fri, 20 Nov 2026 14:06:43 UTC
The branch main has been updated by manu:

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

commit d5cbb6229f53c213f1ce769c0dacd1a979ec000f
Author:     Emmanuel Vadot <manu@FreeBSD.org>
AuthorDate: 2026-11-18 08:27:18 +0000
Commit:     Emmanuel Vadot <manu@FreeBSD.org>
CommitDate: 2026-11-20 14:05:59 +0000

    lindebugfs: Do not expose a node before its private data is set
    
    pfs_create_file(), pfs_create_dir() and pfs_create_link() link the new node
    into its parent and return, and only then do the debugfs_create_*()
    functions set pn_data.  Between the two, the node can already be found by a
    lookup but pn_data is NULL, so a concurrent stat(2) or read(2) calls
    debugfs_attr(), debugfs_fill() or debugfs_fill_data() which dereference it:
    a driver creating debugfs entries while lindebugfs is mounted can panic
    whoever is looking at the tree at that moment.  The window is only a few
    instructions wide, which made it hard to hit; with a delay added on purpose
    it panics at once in debugfs_attr() called from stat(2).
    
    Changing pfs_create_*() to take the data would change the pseudofs API and
    ABI.  Use the visibility callback instead: pseudofs checks it before
    creating a vnode for a node and before getattr, read and write, so a
    node whose pn_data is not set yet is reported as not visible and lookups
    return ENOENT.  The data is published with a release store and read with an
    acquire load, so whoever sees the pointer sees the initialized dentry_meta.
    debugfs_lookup() now also returns NULL for such a node.
    
    Reviewed by:    dumbell
    Reported by:    llm-postcr
    Differential Revision:  https://reviews.freebsd.org/D61705
---
 sys/compat/lindebugfs/lindebugfs.c | 41 ++++++++++++++++++++++++++++++--------
 1 file changed, 33 insertions(+), 8 deletions(-)

diff --git a/sys/compat/lindebugfs/lindebugfs.c b/sys/compat/lindebugfs/lindebugfs.c
index b26d2b31b62f..af9e2a24b899 100644
--- a/sys/compat/lindebugfs/lindebugfs.c
+++ b/sys/compat/lindebugfs/lindebugfs.c
@@ -88,6 +88,27 @@ struct dentry_meta {
 	int dm_type;
 };
 
+/*
+ * pseudofs makes a node visible to lookups as soon as pfs_create_*() has added
+ * it to its parent, but pn_data can only be set once these functions return.
+ * Report the node as not visible until then, so that nothing can reach it
+ * (lookups fail with ENOENT) while pn_data is still NULL.  The release store
+ * makes the dentry_meta contents visible to whoever sees the pointer.
+ */
+static int
+debugfs_vis(PFS_VIS_ARGS)
+{
+
+	return (atomic_load_acq_ptr((volatile uintptr_t *)&pn->pn_data) != 0);
+}
+
+static void
+debugfs_set_data(struct pfs_node *pn, struct dentry_meta *dm)
+{
+
+	atomic_store_rel_ptr((volatile uintptr_t *)&pn->pn_data, (uintptr_t)dm);
+}
+
 static int
 debugfs_attr(PFS_ATTR_ARGS)
 {
@@ -235,12 +256,13 @@ debugfs_create_file(const char *name, umode_t mode,
 
 	flags = fops->write ? PFS_RDWR : PFS_RD;
 	pfs_create_file(pnode, &dnode->d_pfs_node, name, debugfs_fill,
-	    debugfs_attr, NULL, debugfs_destroy, flags | PFS_RAW | PFS_NOWAIT);
+	    debugfs_attr, debugfs_vis, debugfs_destroy,
+	    flags | PFS_RAW | PFS_NOWAIT);
 	if (dnode->d_pfs_node == NULL) {
 		free(dm, M_DFSINT);
 		return (NULL);
 	}
-	dnode->d_pfs_node->pn_data = dm;
+	debugfs_set_data(dnode->d_pfs_node, dm);
 
 	return (dnode);
 }
@@ -314,13 +336,13 @@ debugfs_create_dir(const char *name, struct dentry *parent)
 	else
 		pnode = debugfs_root;
 
-	pfs_create_dir(pnode, &dnode->d_pfs_node, name, debugfs_attr, NULL,
-	    debugfs_destroy, PFS_RD | PFS_NOWAIT);
+	pfs_create_dir(pnode, &dnode->d_pfs_node, name, debugfs_attr,
+	    debugfs_vis, debugfs_destroy, PFS_RD | PFS_NOWAIT);
 	if (dnode->d_pfs_node == NULL) {
 		free(dm, M_DFSINT);
 		return (NULL);
 	}
-	dnode->d_pfs_node->pn_data = dm;
+	debugfs_set_data(dnode->d_pfs_node, dm);
 	return (dnode);
 }
 
@@ -349,10 +371,10 @@ debugfs_create_symlink(const char *name, struct dentry *parent,
 		pnode = debugfs_root;
 
 	pfs_create_link(pnode, &dnode->d_pfs_node, name, &debugfs_fill_data,
-	    NULL, NULL, debugfs_destroy, PFS_NOWAIT);
+	    NULL, debugfs_vis, debugfs_destroy, PFS_NOWAIT);
 	if (dnode->d_pfs_node == NULL)
 		goto fail;
-	dnode->d_pfs_node->pn_data = dm;
+	debugfs_set_data(dnode->d_pfs_node, dm);
 	return (dnode);
  fail:
 	free(dm, M_DFSINT);
@@ -372,7 +394,10 @@ debugfs_lookup(const char *name, struct dentry *parent)
 	if (pnode == NULL)
 		return (NULL);
 
-	dm = (struct dentry_meta *)pnode->pn_data;
+	dm = (struct dentry_meta *)atomic_load_acq_ptr(
+	    (volatile uintptr_t *)&pnode->pn_data);
+	if (dm == NULL)
+		return (NULL);
 	dnode = &dm->dm_dnode;
 
 	return (dnode);