Skip to contentSkip to conversation

namei: free new_dentry late

PATCH In review Pan Bian 3 participants 5 messages Nov 25, 2018 – Nov 27, 2018 VFS

lore
Share & tools
lore

Pan Bian

author
#0 lore raw

PATCH v2 1/1 namei: free new_dentry late

After calling dput(new_dentry), new_dentry is passed to fsnotify_move. This may result in a use-after-free bug. This patch moves the put operation late.

Fixes da1ce0670c14("vfs: add cross-rename")

---
V2: correct the fixes commit information

---
 fs/namei.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/namei.c +1 -1
index 0cab649..8b104d9 100644
@@ -4498,7 +4498,6 @@ int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
 		unlock_two_nondirectories(source, target);
 	else if (target)
 		inode_unlock(target);
-	dput(new_dentry);
 	if (!error) {
 		fsnotify_move(old_dir, new_dir, old_name.name, is_dir,
 			      !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
@@ -4507,6 +4506,7 @@ int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
 				      new_is_dir, NULL, new_dentry);
 		}
 	}
+	dput(new_dentry);
 	release_dentry_name_snapshot(&old_name);
 
 	return error;
Signature · 1 line
2.7.4
reviewer
#1 lore raw
Quoted 6 lines · Pan Bian

After calling dput(new_dentry), new_dentry is passed to fsnotify_move. This may result in a use-after-free bug. This patch moves the put operation late.

Fixes da1ce0670c14("vfs: add cross-rename")

The code is actually fine AFAICT. One new_dentry reference is passed into vfs_rename() and another one is acquired directly inside vfs_rename(). That is the one dropped early but there's still the original reference passed in protecting new_dentry from freeing. Am I missing something?

Honza

Quoted 30 lines

---
V2: correct the fixes commit information

---
 fs/namei.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/namei.c +1 -1
index 0cab649..8b104d9 100644
@@ -4498,7 +4498,6 @@ int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
 		unlock_two_nondirectories(source, target);
 	else if (target)
 		inode_unlock(target);
-	dput(new_dentry);
 	if (!error) {
 		fsnotify_move(old_dir, new_dir, old_name.name, is_dir,
 			      !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
@@ -4507,6 +4506,7 @@ int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
 				      new_is_dir, NULL, new_dentry);
 		}
 	}
+	dput(new_dentry);
 	release_dentry_name_snapshot(&old_name);
 
 	return error;
Signature · 1 line
2.7.4
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author
#2 lore raw
Quoted 5 lines · Jan Kara
Quoted 4 lines · Pan Bian

After calling dput(new_dentry), new_dentry is passed to fsnotify_move. This may result in a use-after-free bug. This patch moves the put operation late.

Quoted 7 lines
Quoted 2 lines
Fixes da1ce0670c14("vfs: add cross-rename")

The code is actually fine AFAICT. One new_dentry reference is passed into vfs_rename() and another one is acquired directly inside vfs_rename(). That is the one dropped early but there's still the original reference passed in protecting new_dentry from freeing. Am I missing something?

I am not quite sure about the actual execution logic. But I guess new_dentry reference may be dropped outside vfs_rename in cocurrent executions. Otherwise, there is no need to acquire & drop new_dentry reference as it is always alive along vfs_rename.

Best regards,
Pan

Quoted 36 lines

Honza

Quoted 30 lines

---
V2: correct the fixes commit information

---
 fs/namei.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/namei.c +1 -1
index 0cab649..8b104d9 100644
@@ -4498,7 +4498,6 @@ int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
 		unlock_two_nondirectories(source, target);
 	else if (target)
 		inode_unlock(target);
-	dput(new_dentry);
 	if (!error) {
 		fsnotify_move(old_dir, new_dir, old_name.name, is_dir,
 			      !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
@@ -4507,6 +4506,7 @@ int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
 				      new_is_dir, NULL, new_dentry);
 		}
 	}
+	dput(new_dentry);
 	release_dentry_name_snapshot(&old_name);
 
 	return error;
Signature · 1 line
2.7.4
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
reviewer
#3 lore raw
Quoted 14 lines · PanBian
Quoted 5 lines · Jan Kara
Quoted 4 lines · Pan Bian

After calling dput(new_dentry), new_dentry is passed to fsnotify_move. This may result in a use-after-free bug. This patch moves the put operation late.

Quoted 7 lines
Quoted 2 lines
Fixes da1ce0670c14("vfs: add cross-rename")

The code is actually fine AFAICT. One new_dentry reference is passed into vfs_rename() and another one is acquired directly inside vfs_rename(). That is the one dropped early but there's still the original reference passed in protecting new_dentry from freeing. Am I missing something?

Quoted 4 lines

I am not quite sure about the actual execution logic. But I guess new_dentry reference may be dropped outside vfs_rename in cocurrent executions. Otherwise, there is no need to acquire & drop new_dentry reference as it is always alive along vfs_rename.

I don't think that's the case. The dget() - dput() pair just looks superfluous to me in vfs_rename(). Am I missing something Miklos?

Honza

Quoted 28 lines
Quoted 27 lines
Quoted 24 lines
fs/namei.c +1 -1
index 0cab649..8b104d9 100644
@@ -4498,7 +4498,6 @@ int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
 		unlock_two_nondirectories(source, target);
 	else if (target)
 		inode_unlock(target);
-	dput(new_dentry);
 	if (!error) {
 		fsnotify_move(old_dir, new_dir, old_name.name, is_dir,
 			      !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
@@ -4507,6 +4506,7 @@ int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
 				      new_is_dir, NULL, new_dentry);
 		}
 	}
+	dput(new_dentry);
 	release_dentry_name_snapshot(&old_name);
 
 	return error;
Signature · 1 line
2.7.4
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#4 lore raw
Quoted 10 lines · Jan Kara
Quoted 4 lines · PanBian

I am not quite sure about the actual execution logic. But I guess new_dentry reference may be dropped outside vfs_rename in cocurrent executions. Otherwise, there is no need to acquire & drop new_dentry reference as it is always alive along vfs_rename.

I don't think that's the case. The dget() - dput() pair just looks superfluous to me in vfs_rename(). Am I missing something Miklos?

I think those are to protect against d_delete() called from fs to reset d_inode. The caller indeed has to hold one ref anyway.

So not superfluous, but only needed due to d_delete() and not to protect against freeing of dentry.

Thanks,
Miklos