Skip to content

Commit 0b4e824

Browse files
amir73ilgregkh
authored andcommitted
fsnotify: invalidate dcache before IN_DELETE event
commit a37d9a1 upstream. Apparently, there are some applications that use IN_DELETE event as an invalidation mechanism and expect that if they try to open a file with the name reported with the delete event, that it should not contain the content of the deleted file. Commit 4924646 ("fsnotify: move fsnotify_nameremove() hook out of d_delete()") moved the fsnotify delete hook before d_delete() so fsnotify will have access to a positive dentry. This allowed a race where opening the deleted file via cached dentry is now possible after receiving the IN_DELETE event. To fix the regression, create a new hook fsnotify_delete() that takes the unlinked inode as an argument and use a helper d_delete_notify() to pin the inode, so we can pass it to fsnotify_delete() after d_delete(). Backporting hint: this regression is from v5.3. Although patch will apply with only trivial conflicts to v5.4 and v5.10, it won't build, because fsnotify_delete() implementation is different in each of those versions (see fsnotify_link()). A follow up patch will fix the fsnotify_unlink/rmdir() calls in pseudo filesystem that do not need to call d_delete(). Link: https://lore.kernel.org/r/20220120215305.282577-1-amir73il@gmail.com Reported-by: Ivan Delalande <colona@arista.com> Link: https://lore.kernel.org/linux-fsdevel/YeNyzoDM5hP5LtGW@visor/ Fixes: 4924646 ("fsnotify: move fsnotify_nameremove() hook out of d_delete()") Cc: stable@vger.kernel.org # v5.3+ Signed-off-by: Amir Goldstein <amir73il@gmail.com> Signed-off-by: Jan Kara <jack@suse.cz> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 8bae6db commit 0b4e824

3 files changed

Lines changed: 49 additions & 15 deletions

File tree

‎fs/btrfs/ioctl.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3103,10 +3103,8 @@ static noinline int btrfs_ioctl_snap_destroy(struct file *file,
31033103
inode_lock(inode);
31043104
err = btrfs_delete_subvolume(dir, dentry);
31053105
inode_unlock(inode);
3106-
if (!err) {
3107-
fsnotify_rmdir(dir, dentry);
3108-
d_delete(dentry);
3109-
}
3106+
if (!err)
3107+
d_delete_notify(dir, dentry);
31103108

31113109
out_dput:
31123110
dput(dentry);

‎fs/namei.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3709,13 +3709,12 @@ int vfs_rmdir(struct inode *dir, struct dentry *dentry)
37093709
dentry->d_inode->i_flags |= S_DEAD;
37103710
dont_mount(dentry);
37113711
detach_mounts(dentry);
3712-
fsnotify_rmdir(dir, dentry);
37133712

37143713
out:
37153714
inode_unlock(dentry->d_inode);
37163715
dput(dentry);
37173716
if (!error)
3718-
d_delete(dentry);
3717+
d_delete_notify(dir, dentry);
37193718
return error;
37203719
}
37213720
EXPORT_SYMBOL(vfs_rmdir);
@@ -3825,17 +3824,18 @@ int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegate
38253824
if (!error) {
38263825
dont_mount(dentry);
38273826
detach_mounts(dentry);
3828-
fsnotify_unlink(dir, dentry);
38293827
}
38303828
}
38313829
}
38323830
out:
38333831
inode_unlock(target);
38343832

38353833
/* We don't d_delete() NFS sillyrenamed files--they still exist. */
3836-
if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
3834+
if (!error && dentry->d_flags & DCACHE_NFSFS_RENAMED) {
3835+
fsnotify_unlink(dir, dentry);
3836+
} else if (!error) {
38373837
fsnotify_link_count(target);
3838-
d_delete(dentry);
3838+
d_delete_notify(dir, dentry);
38393839
}
38403840

38413841
return error;

‎include/linux/fsnotify.h‎

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,17 +203,53 @@ static inline void fsnotify_link(struct inode *dir, struct inode *inode,
203203
fsnotify_name(dir, FS_CREATE, inode, &new_dentry->d_name, 0);
204204
}
205205

206+
/*
207+
* fsnotify_delete - @dentry was unlinked and unhashed
208+
*
209+
* Caller must make sure that dentry->d_name is stable.
210+
*
211+
* Note: unlike fsnotify_unlink(), we have to pass also the unlinked inode
212+
* as this may be called after d_delete() and old_dentry may be negative.
213+
*/
214+
static inline void fsnotify_delete(struct inode *dir, struct inode *inode,
215+
struct dentry *dentry)
216+
{
217+
__u32 mask = FS_DELETE;
218+
219+
if (S_ISDIR(inode->i_mode))
220+
mask |= FS_ISDIR;
221+
222+
fsnotify_name(dir, mask, inode, &dentry->d_name, 0);
223+
}
224+
225+
/**
226+
* d_delete_notify - delete a dentry and call fsnotify_delete()
227+
* @dentry: The dentry to delete
228+
*
229+
* This helper is used to guaranty that the unlinked inode cannot be found
230+
* by lookup of this name after fsnotify_delete() event has been delivered.
231+
*/
232+
static inline void d_delete_notify(struct inode *dir, struct dentry *dentry)
233+
{
234+
struct inode *inode = d_inode(dentry);
235+
236+
ihold(inode);
237+
d_delete(dentry);
238+
fsnotify_delete(dir, inode, dentry);
239+
iput(inode);
240+
}
241+
206242
/*
207243
* fsnotify_unlink - 'name' was unlinked
208244
*
209245
* Caller must make sure that dentry->d_name is stable.
210246
*/
211247
static inline void fsnotify_unlink(struct inode *dir, struct dentry *dentry)
212248
{
213-
/* Expected to be called before d_delete() */
214-
WARN_ON_ONCE(d_is_negative(dentry));
249+
if (WARN_ON_ONCE(d_is_negative(dentry)))
250+
return;
215251

216-
fsnotify_dirent(dir, dentry, FS_DELETE);
252+
fsnotify_delete(dir, d_inode(dentry), dentry);
217253
}
218254

219255
/*
@@ -233,10 +269,10 @@ static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
233269
*/
234270
static inline void fsnotify_rmdir(struct inode *dir, struct dentry *dentry)
235271
{
236-
/* Expected to be called before d_delete() */
237-
WARN_ON_ONCE(d_is_negative(dentry));
272+
if (WARN_ON_ONCE(d_is_negative(dentry)))
273+
return;
238274

239-
fsnotify_dirent(dir, dentry, FS_DELETE | FS_ISDIR);
275+
fsnotify_delete(dir, d_inode(dentry), dentry);
240276
}
241277

242278
/*

0 commit comments

Comments
 (0)