mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-09-15 15:15:47 +00:00
ext4: fix race between ext4_sync_parent() and rename()
'igrab(d_inode(dentry->d_parent))' without holding dentry->d_lock is
broken because without d_lock, d_parent can be concurrently changed due
to a rename(). Then if the old directory is immediately deleted, old
d_parent->inode can be NULL. That causes a NULL dereference in igrab().
To fix this, use dget_parent() to safely grab a reference to the parent
dentry, which pins the inode. This also eliminates the need to use
d_find_any_alias() other than for the initial inode, as we no longer
throw away the dentry at each step.
This is an extremely hard race to hit, but it is possible. Adding a
udelay() in between the reads of ->d_parent and its ->d_inode makes it
reproducible on a no-journal filesystem using the following program:
#include <fcntl.h>
#include <unistd.h>
int main()
{
if (fork()) {
for (;;) {
mkdir("dir1", 0700);
int fd = open("dir1/file", O_RDWR|O_CREAT|O_SYNC);
write(fd, "X", 1);
close(fd);
}
} else {
mkdir("dir2", 0700);
for (;;) {
rename("dir1/file", "dir2/file");
rmdir("dir1");
}
}
}
Fixes: d59729f4e7
("ext4: fix races in ext4_sync_parent()")
Cc: stable@vger.kernel.org
Signed-off-by: Eric Biggers <ebiggers@google.com>
Link: https://lore.kernel.org/r/20200506183140.541194-1-ebiggers@kernel.org
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
This commit is contained in:
parent
80dd4978dd
commit
08adf452e6
1 changed files with 13 additions and 15 deletions
|
@ -44,30 +44,28 @@
|
||||||
*/
|
*/
|
||||||
static int ext4_sync_parent(struct inode *inode)
|
static int ext4_sync_parent(struct inode *inode)
|
||||||
{
|
{
|
||||||
struct dentry *dentry = NULL;
|
struct dentry *dentry, *next;
|
||||||
struct inode *next;
|
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
if (!ext4_test_inode_state(inode, EXT4_STATE_NEWENTRY))
|
if (!ext4_test_inode_state(inode, EXT4_STATE_NEWENTRY))
|
||||||
return 0;
|
return 0;
|
||||||
inode = igrab(inode);
|
|
||||||
while (ext4_test_inode_state(inode, EXT4_STATE_NEWENTRY)) {
|
|
||||||
ext4_clear_inode_state(inode, EXT4_STATE_NEWENTRY);
|
|
||||||
dentry = d_find_any_alias(inode);
|
dentry = d_find_any_alias(inode);
|
||||||
if (!dentry)
|
if (!dentry)
|
||||||
break;
|
return 0;
|
||||||
next = igrab(d_inode(dentry->d_parent));
|
while (ext4_test_inode_state(inode, EXT4_STATE_NEWENTRY)) {
|
||||||
|
ext4_clear_inode_state(inode, EXT4_STATE_NEWENTRY);
|
||||||
|
|
||||||
|
next = dget_parent(dentry);
|
||||||
dput(dentry);
|
dput(dentry);
|
||||||
if (!next)
|
dentry = next;
|
||||||
break;
|
inode = dentry->d_inode;
|
||||||
iput(inode);
|
|
||||||
inode = next;
|
|
||||||
/*
|
/*
|
||||||
* The directory inode may have gone through rmdir by now. But
|
* The directory inode may have gone through rmdir by now. But
|
||||||
* the inode itself and its blocks are still allocated (we hold
|
* the inode itself and its blocks are still allocated (we hold
|
||||||
* a reference to the inode so it didn't go through
|
* a reference to the inode via its dentry), so it didn't go
|
||||||
* ext4_evict_inode()) and so we are safe to flush metadata
|
* through ext4_evict_inode()) and so we are safe to flush
|
||||||
* blocks and the inode.
|
* metadata blocks and the inode.
|
||||||
*/
|
*/
|
||||||
ret = sync_mapping_buffers(inode->i_mapping);
|
ret = sync_mapping_buffers(inode->i_mapping);
|
||||||
if (ret)
|
if (ret)
|
||||||
|
@ -76,7 +74,7 @@ static int ext4_sync_parent(struct inode *inode)
|
||||||
if (ret)
|
if (ret)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
iput(inode);
|
dput(dentry);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue