Skip to contentSkip to conversation

af_unix: read UNIX_DIAG_VFS data under unix_state_lock

PATCH Superseded Ren Wei 5 participants 7 messages Apr 6 – Apr 10 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Ren Wei

author
#1 lore raw

PATCH 1/1 af_unix: read UNIX_DIAG_VFS data under unix_state_lock

From: Jiexun Wang <[email protected]>

Exact UNIX diag lookups hold a reference to the socket, but not to u->path. Meanwhile, unix_release_sock() clears u->path under unix_state_lock() and drops the path reference after unlocking.

Read the inode and device numbers for UNIX_DIAG_VFS while holding unix_state_lock(), then emit the netlink attribute after dropping the lock.

This keeps the VFS data stable while the reply is being built.

Fixes 5f7b0569460b ("unix_diag: Unix inode info NLA")
---
 net/unix/diag.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
net/unix/diag.c +13 -8
index ca3473026151..76b2c48137dd 100644
@@ -28,18 +28,23 @@ static int sk_diag_dump_name(struct sock *sk, struct sk_buff *nlskb)
 
 static int sk_diag_dump_vfs(struct sock *sk, struct sk_buff *nlskb)
 {
-	struct dentry *dentry = unix_sk(sk)->path.dentry;
+	struct dentry *dentry;
+	struct unix_diag_vfs uv;
+	bool have_vfs = false;
 
+	unix_state_lock(sk);
+	dentry = unix_sk(sk)->path.dentry;
 	if (dentry) {
-		struct unix_diag_vfs uv = {
-			.udiag_vfs_ino = d_backing_inode(dentry)->i_ino,
-			.udiag_vfs_dev = dentry->d_sb->s_dev,
-		};
-
-		return nla_put(nlskb, UNIX_DIAG_VFS, sizeof(uv), &uv);
+		uv.udiag_vfs_ino = d_backing_inode(dentry)->i_ino;
+		uv.udiag_vfs_dev = dentry->d_sb->s_dev;
+		have_vfs = true;
 	}
+	unix_state_unlock(sk);
 
-	return 0;
+	if (!have_vfs)
+		return 0;
+
+	return nla_put(nlskb, UNIX_DIAG_VFS, sizeof(uv), &uv);
 }
 
 static int sk_diag_dump_peer(struct sock *sk, struct sk_buff *nlskb)
Signature · 1 line
2.34.1
#2 lore raw
Quoted 31 lines · Ren Wei

From: Jiexun Wang <[email protected]>

Exact UNIX diag lookups hold a reference to the socket, but not to u->path. Meanwhile, unix_release_sock() clears u->path under unix_state_lock() and drops the path reference after unlocking.

Read the inode and device numbers for UNIX_DIAG_VFS while holding unix_state_lock(), then emit the netlink attribute after dropping the lock.

This keeps the VFS data stable while the reply is being built.

Fixes 5f7b0569460b ("unix_diag: Unix inode info NLA")
---
 net/unix/diag.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
net/unix/diag.c +0 -0
index ca3473026151..76b2c48137dd 100644

net/unix/diag.c static int sk_diag_dump_name(struct sock *sk, struct sk_buff *nlskb) · line 33

Quoted 7 lines
net/unix/diag.c +3 -1
@@ -28,18 +28,23 @@ static int sk_diag_dump_name(struct sock *sk, struct sk_buff *nlskb)
 
 static int sk_diag_dump_vfs(struct sock *sk, struct sk_buff *nlskb)
 {
-       struct dentry *dentry = unix_sk(sk)->path.dentry;
+       struct dentry *dentry;
+       struct unix_diag_vfs uv;
+       bool have_vfs = false;

nit: Please keep the reverse xmas order.
https://www.kernel.org/doc/html/v6.6/process/maintainer-netdev.html#local-variable-ordering-reverse-xmas-tree-rcs

Quoted 27 lines
net/unix/diag.c +10 -8
+       unix_state_lock(sk);
+       dentry = unix_sk(sk)->path.dentry;
        if (dentry) {
-               struct unix_diag_vfs uv = {
-                       .udiag_vfs_ino = d_backing_inode(dentry)->i_ino,
-                       .udiag_vfs_dev = dentry->d_sb->s_dev,
-               };
-
-               return nla_put(nlskb, UNIX_DIAG_VFS, sizeof(uv), &uv);
+               uv.udiag_vfs_ino = d_backing_inode(dentry)->i_ino;
+               uv.udiag_vfs_dev = dentry->d_sb->s_dev;
+               have_vfs = true;
        }
+       unix_state_unlock(sk);
 
-       return 0;
+       if (!have_vfs)
+               return 0;
+
+       return nla_put(nlskb, UNIX_DIAG_VFS, sizeof(uv), &uv);
 }
 
 static int sk_diag_dump_peer(struct sock *sk, struct sk_buff *nlskb)
--

2.34.1

author
#3 lore raw

PATCH v2 1/1 af_unix: read UNIX_DIAG_VFS data under unix_state_lock

In reply to a message not in the archive

From: Jiexun Wang <[email protected]>

Exact UNIX diag lookups hold a reference to the socket, but not to u->path. Meanwhile, unix_release_sock() clears u->path under unix_state_lock() and drops the path reference after unlocking.

Read the inode and device numbers for UNIX_DIAG_VFS while holding unix_state_lock(), then emit the netlink attribute after dropping the lock.

This keeps the VFS data stable while the reply is being built.

Fixes 5f7b0569460b ("unix_diag: Unix inode info NLA")

---
Changes in v2:
- reorder local variables in reverse xmas tree order

 net/unix/diag.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
net/unix/diag.c +13 -8
index ca3473026151..c9c1e51c4419 100644
@@ -28,18 +28,23 @@ static int sk_diag_dump_name(struct sock *sk, struct sk_buff *nlskb)
 
 static int sk_diag_dump_vfs(struct sock *sk, struct sk_buff *nlskb)
 {
-	struct dentry *dentry = unix_sk(sk)->path.dentry;
+	struct unix_diag_vfs uv;
+	struct dentry *dentry;
+	bool have_vfs = false;
 
+	unix_state_lock(sk);
+	dentry = unix_sk(sk)->path.dentry;
 	if (dentry) {
-		struct unix_diag_vfs uv = {
-			.udiag_vfs_ino = d_backing_inode(dentry)->i_ino,
-			.udiag_vfs_dev = dentry->d_sb->s_dev,
-		};
-
-		return nla_put(nlskb, UNIX_DIAG_VFS, sizeof(uv), &uv);
+		uv.udiag_vfs_ino = d_backing_inode(dentry)->i_ino;
+		uv.udiag_vfs_dev = dentry->d_sb->s_dev;
+		have_vfs = true;
 	}
+	unix_state_unlock(sk);
 
-	return 0;
+	if (!have_vfs)
+		return 0;
+
+	return nla_put(nlskb, UNIX_DIAG_VFS, sizeof(uv), &uv);
 }
 
 static int sk_diag_dump_peer(struct sock *sk, struct sk_buff *nlskb)
Signature · 1 line
2.34.1
#4 lore raw
Quoted 13 lines · Ren Wei

From: Jiexun Wang <[email protected]>

Exact UNIX diag lookups hold a reference to the socket, but not to u->path. Meanwhile, unix_release_sock() clears u->path under unix_state_lock() and drops the path reference after unlocking.

Read the inode and device numbers for UNIX_DIAG_VFS while holding unix_state_lock(), then emit the netlink attribute after dropping the lock.

This keeps the VFS data stable while the reply is being built.

Quoted 9 lines
Fixes 5f7b0569460b ("unix_diag: Unix inode info NLA")

Next time, please, you should obey the submission rules. One of them is to at least wait ~24 hours before posting a new version, which makes reviewers from all over the world have a chance to take a look at your patch.

Thanks,
Jason

maintainer Reviewed-by on v2 1/1
#5 lore raw
In reply to Ren Wei
Quoted 13 lines · Ren Wei

From: Jiexun Wang <[email protected]>

Exact UNIX diag lookups hold a reference to the socket, but not to u->path. Meanwhile, unix_release_sock() clears u->path under unix_state_lock() and drops the path reference after unlocking.

Read the inode and device numbers for UNIX_DIAG_VFS while holding unix_state_lock(), then emit the netlink attribute after dropping the lock.

This keeps the VFS data stable while the reply is being built.

Quoted 9 lines
Fixes 5f7b0569460b ("unix_diag: Unix inode info NLA")

Looks good, thanks !

Next time your team post patches, please make sure to follow this guideline:

Documentation/process/maintainer-netdev.rst

(e.g. post a next revision as a separate thread, etc)

Quoted 46 lines

---
Changes in v2:
- reorder local variables in reverse xmas tree order

 net/unix/diag.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
net/unix/diag.c +13 -9
index ca3473026151..c9c1e51c4419 100644
@@ -28,18 +28,23 @@ static int sk_diag_dump_name(struct sock *sk, struct sk_buff *nlskb)
 
 static int sk_diag_dump_vfs(struct sock *sk, struct sk_buff *nlskb)
 {
-       struct dentry *dentry = unix_sk(sk)->path.dentry;
+       struct unix_diag_vfs uv;
+       struct dentry *dentry;
+       bool have_vfs = false;
 
+       unix_state_lock(sk);
+       dentry = unix_sk(sk)->path.dentry;
        if (dentry) {
-               struct unix_diag_vfs uv = {
-                       .udiag_vfs_ino = d_backing_inode(dentry)->i_ino,
-                       .udiag_vfs_dev = dentry->d_sb->s_dev,
-               };
-
-               return nla_put(nlskb, UNIX_DIAG_VFS, sizeof(uv), &uv);
+               uv.udiag_vfs_ino = d_backing_inode(dentry)->i_ino;
+               uv.udiag_vfs_dev = dentry->d_sb->s_dev;
+               have_vfs = true;
        }
+       unix_state_unlock(sk);
 
-       return 0;
+       if (!have_vfs)
+               return 0;
+
+       return nla_put(nlskb, UNIX_DIAG_VFS, sizeof(uv), &uv);
 }
 
 static int sk_diag_dump_peer(struct sock *sk, struct sk_buff *nlskb)
--

2.34.1

#6 lore raw
In reply to Ren Wei
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 11 lines · On Tue, 7 Apr 2026 16:00:14 +0800 you wrote:

From: Jiexun Wang <[email protected]>

Exact UNIX diag lookups hold a reference to the socket, but not to u->path. Meanwhile, unix_release_sock() clears u->path under unix_state_lock() and drops the path reference after unlocking.

Read the inode and device numbers for UNIX_DIAG_VFS while holding unix_state_lock(), then emit the netlink attribute after dropping the lock.

[...]

Here is the summary with links:
- [net,v2,1/1] af_unix: read UNIX_DIAG_VFS data under unix_state_lock

    https://git.kernel.org/netdev/net/c/39897df38637

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
Reviewed-by on v2 1/1
#7 lore raw
In reply to Ren Wei
Quoted 21 lines · Ren Wei

From: Jiexun Wang <[email protected]>

Exact UNIX diag lookups hold a reference to the socket, but not to u->path. Meanwhile, unix_release_sock() clears u->path under unix_state_lock() and drops the path reference after unlocking.

Read the inode and device numbers for UNIX_DIAG_VFS while holding unix_state_lock(), then emit the netlink attribute after dropping the lock.

This keeps the VFS data stable while the reply is being built.

Fixes 5f7b0569460b ("unix_diag: Unix inode info NLA")
Quoted 1 line

---

Seems good, thanks.