Skip to contentSkip to conversation

fs: Fix bug in lock_rename_child that can cause deadlock

PATCH In review Li Dong 2 participants 2 messages Jun 19, 2023 – Jun 20, 2023 VFS

lore
Share & tools
lore

Li Dong

author
#0 lore raw

PATCH 1/1 fs: Fix bug in lock_rename_child that can cause deadlock

Function xx causes a deadlock,because s_vfs_rename_mutex was not released when return

---
 fs/namei.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/namei.c +3 -1
index 91171da719c5..63b3fd05fef2 100644
@@ -3076,8 +3076,10 @@ struct dentry *lock_rename_child(struct dentry *c1, struct dentry *p2)
 	/*
 	 * nobody can move out of any directories on this fs.
 	 */
-	if (likely(c1->d_parent != p2))
+	if (likely(c1->d_parent != p2)) {
+		mutex_unlock(&c1->d_sb->s_vfs_rename_mutex);
 		return lock_two_directories(c1->d_parent, p2);
+	}
 
 	/*
 	 * c1 got moved into p2 while we were taking locks;
Signature · 1 line
2.31.1.windows.1
#1 lore raw
Quoted 4 lines · Li Dong

Function xx causes a deadlock,because s_vfs_rename_mutex was not released when return

---

This is a cross-directory rename which requires s_vfs_rename_mutex to be held. You're suggesting to drop it, violating basic locking assumptions with dire consequences:

lock_rename_child()

{
        c1->d_parent != p2
        -> acquire s_vfs_rename_mutex
}

pairs with

old_parent = c1->d_parent;
unlock_rename(old_parent, p2)

{
        if (old_parent != p2)
        -> release s_vfs_rename_mutex
}

See the usage in ksmbd:

trap = lock_rename_child(old_child, new_path.dentry); old_parent = dget(old_child->d_parent); unlock_rename(old_parent, new_path.dentry);