]> www.infradead.org Git - users/hch/misc.git/commitdiff
fsnotify: disable notification by default for all pseudo files
authorAmir Goldstein <amir73il@gmail.com>
Mon, 3 Feb 2025 22:32:04 +0000 (23:32 +0100)
committerChristian Brauner <brauner@kernel.org>
Fri, 7 Feb 2025 09:27:26 +0000 (10:27 +0100)
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: 20bf82a898b6 ("mm: don't allow huge faults for files with pre content watches")
Reported-by: Alex Williamson <alex.williamson@redhat.com>
Closes: https://lore.kernel.org/linux-fsdevel/20250131121703.1e4d00a7.alex.williamson@redhat.com/
Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Link: https://lore.kernel.org/linux-fsdevel/CAHk-=wi2pThSVY=zhO=ZKxViBj5QCRX-=AS2+rVknQgJnHXDFg@mail.gmail.com/
Tested-by: Alex Williamson <alex.williamson@redhat.com>
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Link: https://lore.kernel.org/r/20250203223205.861346-3-amir73il@gmail.com
Signed-off-by: Christian Brauner <brauner@kernel.org>
fs/file_table.c
fs/open.c
fs/pipe.c
net/socket.c

index f0291a66f9db4184359a52da2f4ead308de4abfe..35b93da6c5cb1db2c6937162f2fb04faee0c0328 100644 (file)
@@ -375,7 +375,13 @@ struct file *alloc_file_pseudo(struct inode *inode, struct vfsmount *mnt,
        if (IS_ERR(file)) {
                ihold(inode);
                path_put(&path);
+               return file;
        }
+       /*
+        * Disable all fsnotify events for pseudo files by default.
+        * They may be enabled by caller with file_set_fsnotify_mode().
+        */
+       file_set_fsnotify_mode(file, FMODE_NONOTIFY);
        return file;
 }
 EXPORT_SYMBOL(alloc_file_pseudo);
@@ -400,6 +406,11 @@ struct file *alloc_file_pseudo_noaccount(struct inode *inode,
                return file;
        }
        file_init_path(file, &path, fops);
+       /*
+        * Disable all fsnotify events for pseudo files by default.
+        * They may be enabled by caller with file_set_fsnotify_mode().
+        */
+       file_set_fsnotify_mode(file, FMODE_NONOTIFY);
        return file;
 }
 EXPORT_SYMBOL_GPL(alloc_file_pseudo_noaccount);
index 3fcbfff8aede8023917f4e050e6aa2977f015607..1be20de9f283a46f7401eaa146dcd61ad92dbc48 100644 (file)
--- a/fs/open.c
+++ b/fs/open.c
@@ -936,8 +936,8 @@ static int do_dentry_open(struct file *f,
 
        /*
         * Set FMODE_NONOTIFY_* bits according to existing permission watches.
-        * If FMODE_NONOTIFY was already set for an fanotify fd, this doesn't
-        * change anything.
+        * If FMODE_NONOTIFY mode was already set for an fanotify fd or for a
+        * pseudo file, this call will not change the mode.
         */
        file_set_fsnotify_mode_from_watchers(f);
        error = fsnotify_open_perm(f);
index 94b59045ab44bced0e8a1e0179df0e90fe9df658..ce1af7592780dab38fa2ee52123703445e98906f 100644 (file)
--- a/fs/pipe.c
+++ b/fs/pipe.c
@@ -960,6 +960,12 @@ int create_pipe_files(struct file **res, int flags)
        res[1] = f;
        stream_open(inode, res[0]);
        stream_open(inode, res[1]);
+       /*
+        * Disable permission and pre-content events, but enable legacy
+        * inotify events for legacy users.
+        */
+       file_set_fsnotify_mode(res[0], FMODE_NONOTIFY_PERM);
+       file_set_fsnotify_mode(res[1], FMODE_NONOTIFY_PERM);
        return 0;
 }
 
index 262a28b59c7f0f760fd29e207f270e65150abec8..28bae5a942341b94afec01d5f2f43f710f8ed000 100644 (file)
@@ -479,6 +479,11 @@ struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
        sock->file = file;
        file->private_data = sock;
        stream_open(SOCK_INODE(sock), file);
+       /*
+        * Disable permission and pre-content events, but enable legacy
+        * inotify events for legacy users.
+        */
+       file_set_fsnotify_mode(file, FMODE_NONOTIFY_PERM);
        return file;
 }
 EXPORT_SYMBOL(sock_alloc_file);