Skip to content

Commit 2a42754

Browse files
amir73ilbrauner
authored andcommitted
fsnotify: disable notification by default for all pseudo files
Most pseudo files are not applicable for fsnotify events at all, let alone to the new pre-content events. Disable notifications to all files allocated with alloc_file_pseudo() and enable legacy inotify events for the specific cases of pipe and socket, which have known users of inotify events. Pre-content events are also kept disabled for sockets and pipes. Fixes: 20bf82a ("mm: don't allow huge faults for files with pre content watches") Reported-by: Alex Williamson <[email protected]> Closes: https://lore.kernel.org/linux-fsdevel/[email protected]/ Suggested-by: Linus Torvalds <[email protected]> Link: https://lore.kernel.org/linux-fsdevel/CAHk-=wi2pThSVY=zhO=ZKxViBj5QCRX-=AS2+rVknQgJnHXDFg@mail.gmail.com/ Tested-by: Alex Williamson <[email protected]> Signed-off-by: Amir Goldstein <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Christian Brauner <[email protected]>
1 parent 9510140 commit 2a42754

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

fs/file_table.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,13 @@ struct file *alloc_file_pseudo(struct inode *inode, struct vfsmount *mnt,
375375
if (IS_ERR(file)) {
376376
ihold(inode);
377377
path_put(&path);
378+
return file;
378379
}
380+
/*
381+
* Disable all fsnotify events for pseudo files by default.
382+
* They may be enabled by caller with file_set_fsnotify_mode().
383+
*/
384+
file_set_fsnotify_mode(file, FMODE_NONOTIFY);
379385
return file;
380386
}
381387
EXPORT_SYMBOL(alloc_file_pseudo);
@@ -400,6 +406,11 @@ struct file *alloc_file_pseudo_noaccount(struct inode *inode,
400406
return file;
401407
}
402408
file_init_path(file, &path, fops);
409+
/*
410+
* Disable all fsnotify events for pseudo files by default.
411+
* They may be enabled by caller with file_set_fsnotify_mode().
412+
*/
413+
file_set_fsnotify_mode(file, FMODE_NONOTIFY);
403414
return file;
404415
}
405416
EXPORT_SYMBOL_GPL(alloc_file_pseudo_noaccount);

fs/open.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -936,8 +936,8 @@ static int do_dentry_open(struct file *f,
936936

937937
/*
938938
* Set FMODE_NONOTIFY_* bits according to existing permission watches.
939-
* If FMODE_NONOTIFY was already set for an fanotify fd, this doesn't
940-
* change anything.
939+
* If FMODE_NONOTIFY mode was already set for an fanotify fd or for a
940+
* pseudo file, this call will not change the mode.
941941
*/
942942
file_set_fsnotify_mode_from_watchers(f);
943943
error = fsnotify_open_perm(f);

fs/pipe.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,12 @@ int create_pipe_files(struct file **res, int flags)
960960
res[1] = f;
961961
stream_open(inode, res[0]);
962962
stream_open(inode, res[1]);
963+
/*
964+
* Disable permission and pre-content events, but enable legacy
965+
* inotify events for legacy users.
966+
*/
967+
file_set_fsnotify_mode(res[0], FMODE_NONOTIFY_PERM);
968+
file_set_fsnotify_mode(res[1], FMODE_NONOTIFY_PERM);
963969
return 0;
964970
}
965971

net/socket.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,11 @@ struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
479479
sock->file = file;
480480
file->private_data = sock;
481481
stream_open(SOCK_INODE(sock), file);
482+
/*
483+
* Disable permission and pre-content events, but enable legacy
484+
* inotify events for legacy users.
485+
*/
486+
file_set_fsnotify_mode(file, FMODE_NONOTIFY_PERM);
482487
return file;
483488
}
484489
EXPORT_SYMBOL(sock_alloc_file);

0 commit comments

Comments
 (0)