fanotify: add pre-content hooks
Subsystems (13)
Jan Kara
maintainerv9 cover fanotify: add pre-content hooks
I'm posting here the series I'm currently carrying in my tree [1]. The changes from v8 Josef posted are not huge but big enough that I think it's worth a repost. Unless somebody speaks up, the plan is to merge into fsnotify branch after the merge window closes.
[1] https://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs.git/log/?h=fsnotify_hsm
v8:
https://lore.kernel.org/all/[email protected]
v7:
https://lore.kernel.org/linux-fsdevel/[email protected]/
v6:
https://lore.kernel.org/linux-fsdevel/[email protected]/
v5:
https://lore.kernel.org/linux-fsdevel/[email protected]/
v4:
https://lore.kernel.org/linux-fsdevel/[email protected]/
v3:
https://lore.kernel.org/linux-fsdevel/[email protected]/
v2:
https://lore.kernel.org/linux-fsdevel/[email protected]/
v1:
https://lore.kernel.org/linux-fsdevel/[email protected]/
v8->v9:
- fix DAX fault handling for ext4 & xfs
- rework FMODE_ constants a bit to keep FMODE_NONOTIFY a single bit
- move file_set_fsnotify_mode() out of line as it's quite big
- fold fsnotify_file_object_watched() into the single caller
- use explicit f_mode checks instead of fsnotify_file_has_pre_content_watches()
- fix compilation breakage with CONFIG_NOMMU
- fixed up some changelogs
v7->v8:
- A bunch of work from Amir to cleanup the fast path for the common case of no watches, which cascades through the rest of th series to update the helpers and the hooks to use the new helpers.
- A patch from Al to get rid of the __FMODE_NONOTIFY flag and cleanup the usage there, thanks Al!
v6->v7:
- As per Linus's suggestion, Amir added the file flag FMODE_NOTIFY_PERM that will be set at open time if the file has permission related watches (this is the original malware style permission watches and the new precontent watches). All of the VFS hooks and the page fault hooks use this flag to determine if they should generate a notification to allow for a much cheaper check in the common case.
v5->v6:
- Linus had problems with this and rejected Jan's PR
(https://lore.kernel.org/linux-fsdevel/20240923110348.tbwihs42dxxltabc@quack3/
),
so I'm respinning this series to address his concerns. Hopefully this is more acceptable.
- Change the page fault hooks to happen only in the case where we have to add a page, not where there exists pages already.
- Amir added a hook to truncate.
- We made the flag per SB instead of per fstype, Amir wanted this because of some potential issues with other file system specific work he's doing.
- Dropped the bcachefs patch, there were some concerns that we were doing something wrong, and it's not a huge deal to not have this feature for now.
- Unfortunately the xfs write fault path still has to do the page fault hook before we know if we have a page or not, this is because of the locking that's done before we get to the part where we know if we have a page already or not, so that's the path that is still the same from last iteration.
- I've re-validated this series with btrfs, xfs, and ext4 to make sure I didn't break anything.
v4->v5:
- Cleaned up the various "I'll fix it on commit" notes that Jan made since I had to respin the series anyway.
- Renamed the filemap pagefault helper for fsnotify per Christians suggestion.
- Added a FS_ALLOW_HSM flag per Jan's comments, based on Amir's rough sketch.
- Added a patch to disable btrfs defrag on pre-content watched files.
- Added a patch to turn on FS_ALLOW_HSM for all the file systems that I tested.
- Added two fstests (which will be posted separately) to validate everything, re-validated the series with btrfs, xfs, ext4, and bcachefs to make sure I didn't break anything.
v3->v4:
- Trying to send a final verson Friday at 5pm before you go on vacation is a recipe for silly mistakes, fixed the xfs handling yet again, per Christoph's review.
- Reworked the file system helper so it's handling of fpin was a little less silly, per Chinner's suggestion.
- Updated the return values to not or in VM_FAULT_RETRY, as we have a comment in filemap_fault that says if VM_FAULT_ERROR is set we won't have VM_FAULT_RETRY set.
v2->v3:
- Fix the pagefault path to do MAY_ACCESS instead, updated the perm handler to emit PRE_ACCESS in this case, so we can avoid the extraneous perm event as per Amir's suggestion.
- Reworked the exported helper so the per-filesystem changes are much smaller, per Amir's suggestion.
- Fixed the screwup for DAX writes per Chinner's suggestion.
- Added Christian's reviewed-by's where appropriate.
v1->v2:
- reworked the page fault logic based on Jan's suggestion and turned it into a helper.
- Added 3 patches per-fs where we need to call the fsnotify helper from their ->fault handlers.
- Disabled readahead in the case that there's a pre-content watch in place.
- Disabled huge faults when there's a pre-content watch in place (entirely because it's untested, theoretically it should be straightforward to do).
- Updated the command numbers.
- Addressed the random spelling/grammer mistakes that Jan pointed out.
- Addressed the other random nits from Jan.
--- Original email ---
Hello,
These are the patches for the bare bones pre-content fanotify support. The majority of this work is Amir's, my contribution to this has solely been around adding the page fault hooks, testing and validating everything. I'm sending it because Amir is traveling a bunch, and I touched it last so I'm going to take all the hate and he can take all the credit.
There is a PoC that I've been using to validate this work, you can find the git repo here
https://github.com/josefbacik/remote-fetch
This consists of 3 different tools.
1. populate. This just creates all the stub files in the directory from the source directory. Just run ./populate ~/linux ~/hsm-linux and it'll recursively create all of the stub files and directories.
2. remote-fetch. This is the actual PoC, you just point it at the source and destination directory and then you can do whatever. ./remote-fetch ~/linux ~/hsm-linux.
3. mmap-validate. This was to validate the pagefault thing, this is likely what will be turned into the selftest with remote-fetch. It creates a file and then you can validate the file matches the right pattern with both normal reads and mmap. Normally I do something like
./mmap-validate create ~/src/foo
./populate ~/src ~/dst
./rmeote-fetch ~/src ~/dst
./mmap-validate validate ~/dst/foo
I did a bunch of testing, I also got some performance numbers. I copied a kernel tree, and then did remote-fetch, and then make -j4
Normal
real 9m49.709s user 28m11.372s sys 4m57.304s
HSM
real 10m6.454s user 29m10.517s sys 5m2.617s
So ~17 seconds more to build with HSM. I then did a make mrproper on both trees to see the size
[root@fedora ~]# du -hs /src/linux
1.6G /src/linux
[root@fedora ~]# du -hs dst
125M dst
This mirrors the sort of savings we've seen in production.
Meta has had these patches (minus the page fault patch) deployed in production for almost a year with our own utility for doing on-demand package fetching. The savings from this has been pretty significant.
The page-fault hooks are necessary for the last thing we need, which is on-demand range fetching of executables. Some of our binaries are several gigs large, having the ability to remote fetch them on demand is a huge win for us not only with space savings, but with startup time of containers.
There will be tests for this going into LTP once we're satisfied with the patches and they're on their way upstream. Thanks,
Josef
Al Viro (1):
fs: get rid of __FMODE_NONOTIFY kludge
Amir Goldstein (11):
fsnotify: opt-in for permission events at file open time
fsnotify: check if file is actually being watched for pre-content events on open
fanotify: don't skip extra event info if no info_mode is set
fanotify: rename a misnamed constant
fanotify: reserve event bit of deprecated FAN_DIR_MODIFY
fsnotify: introduce pre-content permission events
fsnotify: pass optional file access range in pre-content event
fsnotify: generate pre-content permission event on truncate
fanotify: introduce FAN_PRE_ACCESS permission event
fanotify: report file range info with pre-content events
fanotify: allow to set errno in FAN_DENY permission response
Jan Kara (1):
ext4: add pre-content fsnotify hook for DAX faults
Josef Bacik (6):
fanotify: disable readahead if we have pre-content watches
mm: don't allow huge faults for files with pre content watches
fsnotify: generate pre-content permission event on page fault
xfs: add pre-content fsnotify hook for DAX faults
btrfs: disable defrag on pre-content watched files
fs: enable pre-content events on supported file systems
fs/btrfs/ioctl.c | 9 +++ fs/btrfs/super.c | 2 +- fs/ext4/file.c | 3 + fs/ext4/super.c | 3 + fs/fcntl.c | 4 +- fs/notify/fanotify/fanotify.c | 31 +++++++-- fs/notify/fanotify/fanotify.h | 15 ++++ fs/notify/fanotify/fanotify_user.c | 137 +++++++++++++++++++++++++++++-------- fs/notify/fsnotify.c | 83 +++++++++++++++++++++- fs/open.c | 62 +++++++++++++---- fs/xfs/xfs_file.c | 14 ++++ fs/xfs/xfs_super.c | 2 +- include/linux/fanotify.h | 18 +++-- include/linux/fs.h | 44 +++++++++--- include/linux/fsnotify.h | 78 ++++++++++++++++----- include/linux/fsnotify_backend.h | 53 +++++++++++++- include/linux/mm.h | 1 + include/uapi/asm-generic/fcntl.h | 1 - include/uapi/linux/fanotify.h | 18 +++++ mm/filemap.c | 90 ++++++++++++++++++++++++ mm/memory.c | 22 ++++++ mm/nommu.c | 7 ++ mm/readahead.c | 14 ++++ security/selinux/hooks.c | 3 +- 24 files changed, 624 insertions(+), 90 deletions(-)
Jan Kara
maintainerPATCH v9 1/19 fs: get rid of __FMODE_NONOTIFY kludge
From: Al Viro <[email protected]>
All it takes to get rid of the __FMODE_NONOTIFY kludge is switching fanotify from anon_inode_getfd() to anon_inode_getfile_fmode() and adding a dentry_open_nonotify() helper to be used by fanotify on the other path. That's it - no more weird shit in OPEN_FMODE(), etc.
--- fs/fcntl.c | 4 ++-- fs/notify/fanotify/fanotify_user.c | 25 ++++++++++++++++--------- fs/open.c | 23 +++++++++++++++++++---- include/linux/fs.h | 6 +++--- include/uapi/asm-generic/fcntl.h | 1 - 5 files changed, 40 insertions(+), 19 deletions(-)
fs/fcntl.c +2 -2
@@ -1161,10 +1161,10 @@ static int __init fcntl_init(void)* Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY * is defined as O_NONBLOCK on some platforms and not on others. */ -BUILD_BUG_ON (21 -1 /* for O_RDONLY being 0 */ != +BUILD_BUG_ON (20 -1 /* for O_RDONLY being 0 */ !=HWEIGHT32 ( (VALID_OPEN_FLAGS & ~(O_NONBLOCK |O_NDELAY )) | - __FMODE_EXEC | __FMODE_NONOTIFY)); + __FMODE_EXEC)); fasync_cache = kmem_cache_create("fasync_cache", sizeof(struct fasync_struct), 0,
fs/notify/fanotify/fanotify_user.c +16 -9
@@ -101,8 +101,7 @@ static void __init fanotify_sysctls_init(void)* * Internal and external open flags are stored together in field f_flags of * struct file. Only external open flags shall be allowed in event_f_flags. -* Internal flags like FMODE_NONOTIFY, FMODE_EXEC, FMODE_NOCMTIME shall be -* excluded. +* Internal flags like FMODE_EXEC shall be excluded. */ #define FANOTIFY_INIT_ALL_EVENT_F_BITS ( \ O_ACCMODE | O_APPEND | O_NONBLOCK | \
@@ -259,12 +258,11 @@ static int create_fd(struct fsnotify_group *group, const struct path *path,return client_fd;/* -* we need a new file handle for the userspace program so it can read even if it was -* originally opened O_WRONLY. +* We provide an fd for the userspace program, so it could access the +* file without generating fanotify events itself. */ - new_file =dentry_open (path, - group->fanotify_data.f_flags | __FMODE_NONOTIFY, -current_cred ()); + new_file =dentry_open_nonotify (path, group->fanotify_data.f_flags, +current_cred ());if (IS_ERR (new_file)) {put_unused_fd (client_fd); client_fd =PTR_ERR (new_file);
@@ -1415,6 +1413,7 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)unsigned int fid_mode = flags &FANOTIFY_FID_BITS ;unsigned int class = flags &FANOTIFY_CLASS_BITS ;unsigned int internal_flags =0 ; +struct file *file;pr_debug ("%s: flags=%x event_f_flags=%x\n" , __func__, flags, event_f_flags);
@@ -1483,7 +1482,7 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) (!(fid_mode & FAN_REPORT_NAME) || !(fid_mode & FAN_REPORT_FID)))return -EINVAL ; - f_flags =O_RDWR | __FMODE_NONOTIFY; + f_flags =O_RDWR ;if (flags &FAN_CLOEXEC ) f_flags |=O_CLOEXEC ;if (flags &FAN_NONBLOCK )
@@ -1561,10 +1560,18 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) goto out_destroy_group; } - fd =anon_inode_getfd ("[fanotify]" , &fanotify_fops, group, f_flags); + fd =get_unused_fd_flags (f_flags);if (fd <0 ) goto out_destroy_group; + file =anon_inode_getfile_fmode ("[fanotify]" , &fanotify_fops, group, + f_flags,FMODE_NONOTIFY ); +if (IS_ERR (file)) { + fd =PTR_ERR (file); +put_unused_fd (fd); + goto out_destroy_group; + } +fd_install (fd, file);return fd; out_destroy_group:
fs/open.c +19 -4
@@ -1122,6 +1122,23 @@ struct file *dentry_open(const struct path *path, int flags, }EXPORT_SYMBOL (dentry_open); +struct file *dentry_open_nonotify (const struct path *path,int flags, +const struct cred *cred) +{ +struct file *f =alloc_empty_file (flags, cred); +if (!IS_ERR (f)) { +int error; + + f->f_mode |=FMODE_NONOTIFY ; + error =vfs_open (path, f); +if (error) { +fput (f); + f =ERR_PTR (error); + } + } +return f; +} +/** * dentry_create - Create and open a file * @path: path to create
@@ -1219,7 +1236,7 @@ inline struct open_how build_open_how(int flags, umode_t mode)inline int build_open_flags (const struct open_how *how,struct open_flags *op) {u64 flags = how->flags; -u64 strip = __FMODE_NONOTIFY |O_CLOEXEC ; +u64 strip =O_CLOEXEC ;int lookup_flags =0 ;int acc_mode =ACC_MODE (flags);
@@ -1227,9 +1244,7 @@ inline int build_open_flags(const struct open_how *how, struct open_flags *op) "struct open_flags doesn't yet handle flags > 32 bits");/* -* Strip flags that either shouldn't be set by userspace like -* FMODE_NONOTIFY or that aren't relevant in determining struct -* open_flags like O_CLOEXEC. +* Strip flags that aren't relevant in determining struct open_flags. */ flags &= ~strip;
include/linux/fs.h +3 -3
@@ -2728,6 +2728,8 @@ static inline struct file *file_open_root_mnt(struct vfsmount *mnt, }struct file *dentry_open (const struct path *path,int flags,const struct cred *creds); +struct file *dentry_open_nonotify (const struct path *path,int flags, +const struct cred *cred);struct file *dentry_create (const struct path *path,int flags,umode_t mode,const struct cred *cred);struct path *backing_file_user_path (struct file *f);
@@ -3625,11 +3627,9 @@ struct ctl_table;int __init list_bdev_fs_names (char *buf,size_t size);#define __FMODE_EXEC ((__force int) FMODE_EXEC) -#define __FMODE_NONOTIFY ((__force int) FMODE_NONOTIFY)#define ACC_MODE (x) ("\004\002\006\006"[(x)&O_ACCMODE]) -#define OPEN_FMODE (flag) ((__force fmode_t)(((flag + 1) & O_ACCMODE) | \ - (flag & __FMODE_NONOTIFY))) +#define OPEN_FMODE (flag) ((__force fmode_t)((flag + 1) & O_ACCMODE))static inline bool is_sxid (umode_t mode) {
include/uapi/asm-generic/fcntl.h +0 -1
@@ -6,7 +6,6 @@/* * FMODE_EXEC is 0x20 -* FMODE_NONOTIFY is 0x4000000 * These cannot be used by userspace O_* until internal and external open * flags are split. * -Eric Paris
Signature · 1 line
2.35.3
Jan Kara
maintainerPATCH v9 2/19 fsnotify: opt-in for permission events at file open time
In reply to Jan KaraFrom: Amir Goldstein <[email protected]>
Legacy inotify/fanotify listeners can add watches for events on inode, parent or mount and expect to get events (e.g. FS_MODIFY) on files that were already open at the time of setting up the watches.
fanotify permission events are typically used by Anti-malware sofware, that is watching the entire mount and it is not common to have more that one Anti-malware engine installed on a system.
To reduce the overhead of the fsnotify_file_perm() hooks on every file access, relax the semantics of the legacy FAN_ACCESS_PERM event to generate events only if there were *any* permission event listeners on the filesystem at the time that the file was opened.
The new semantic is implemented by extending the FMODE_NONOTIFY bit into two FMODE_NONOTIFY_* bits, that are used to store a mode for which of the events types to report.
This is going to apply to the new fanotify pre-content events in order to reduce the cost of the new pre-content event vfs hooks.
--- fs/notify/fsnotify.c | 38 ++++++++++++++++++++++++++++++++++++++ fs/open.c | 8 +++++++- include/linux/fs.h | 37 ++++++++++++++++++++++++++++++++----- include/linux/fsnotify.h | 39 +++++++++++++++++++++++---------------- 4 files changed, 100 insertions(+), 22 deletions(-)
fs/notify/fsnotify.c +38 -0
@@ -623,6 +623,44 @@ int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir, }EXPORT_SYMBOL_GPL (fsnotify); +#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS +/* +* At open time we check fsnotify_sb_has_priority_watchers() and set the +* FMODE_NONOTIFY_ mode bits accordignly. +* Later, fsnotify permission hooks do not check if there are permission event +* watches, but that there were permission event watches at open time. +*/ +void file_set_fsnotify_mode (struct file *file) +{ +struct super_block *sb = file->f_path.dentry->d_sb; + +/* Is it a file opened by fanotify? */ +if (FMODE_FSNOTIFY_NONE (file->f_mode)) +return ; + +/* +* Permission events is a super set of pre-content events, so if there +* are no permission event watchers, there are also no pre-content event +* watchers and this is implied from the single FMODE_NONOTIFY_PERM bit. +*/ +if (likely (!fsnotify_sb_has_priority_watchers (sb, +FSNOTIFY_PRIO_CONTENT ))) { + file->f_mode |=FMODE_NONOTIFY_PERM ; +return ; + } + +/* +* If there are permission event watchers but no pre-content event +* watchers, set FMODE_NONOTIFY | FMODE_NONOTIFY_PERM to indicate that. +*/ +if (likely (!fsnotify_sb_has_priority_watchers (sb, +FSNOTIFY_PRIO_PRE_CONTENT ))) { + file->f_mode |=FMODE_NONOTIFY |FMODE_NONOTIFY_PERM ; +return ; + } +} +#endif +static __init int fsnotify_init (void ) {int ret;
fs/open.c +7 -1
@@ -918,7 +918,7 @@ static int do_dentry_open(struct file *f, f->f_sb_err =file_sample_sb_err (f);if (unlikely (f->f_flags &O_PATH )) { - f->f_mode =FMODE_PATH |FMODE_OPENED ; + f->f_mode =FMODE_PATH |FMODE_OPENED |FMODE_NONOTIFY ; f->f_op = &empty_fops;return 0 ; }
@@ -946,6 +946,12 @@ static int do_dentry_open(struct file *f,if (error) goto cleanup_all; +/* +* Set FMODE_NONOTIFY_* bits according to existing permission watches. +* If FMODE_NONOTIFY was already set for an fanotify fd, this doesn't +* change anything. +*/ +file_set_fsnotify_mode (f); error =fsnotify_open_perm (f);if (error) goto cleanup_all;
include/linux/fs.h +32 -5
@@ -171,13 +171,20 @@ typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset,#define FMODE_NOREUSE ((__force fmode_t)(1 << 23)) -/* FMODE_* bit 24 */ -/* File is embedded in backing_file object */ -#define FMODE_BACKING ((__force fmode_t)(1 << 25)) +#define FMODE_BACKING ((__force fmode_t)(1 << 24)) + +/* +* Together with FMODE_NONOTIFY_PERM defines which fsnotify events shouldn't be +* generated (see below) +*/ +#define FMODE_NONOTIFY ((__force fmode_t)(1 << 25)) -/* File was opened by fanotify and shouldn't generate fanotify events */ -#define FMODE_NONOTIFY ((__force fmode_t)(1 << 26)) +/* +* Together with FMODE_NONOTIFY defines which fsnotify events shouldn't be +* generated (see below) +*/ +#define FMODE_NONOTIFY_PERM ((__force fmode_t)(1 << 26))/* File is capable of returning -EAGAIN if I/O will block */ #define FMODE_NOWAIT ((__force fmode_t)(1 << 27))
@@ -188,6 +195,26 @@ typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset,/* File does not contribute to nr_files count */ #define FMODE_NOACCOUNT ((__force fmode_t)(1 << 29)) +/* +* The two FMODE_NONOTIFY* define which fsnotify events should not be generated +* for a file. These are the possible values of (f->f_mode & +* FMODE_FSNOTIFY_MASK) and their meaning: +* +* FMODE_NONOTIFY - suppress all (incl. non-permission) events. +* FMODE_NONOTIFY_PERM - suppress permission (incl. pre-content) events. +* FMODE_NONOTIFY | FMODE_NONOTIFY_PERM - suppress only pre-content events. +*/ +#define FMODE_FSNOTIFY_MASK \ + (FMODE_NONOTIFY | FMODE_NONOTIFY_PERM) + +#define FMODE_FSNOTIFY_NONE (mode) \ + ((mode & FMODE_FSNOTIFY_MASK) == FMODE_NONOTIFY) +#define FMODE_FSNOTIFY_PERM (mode) \ + ((mode & FMODE_FSNOTIFY_MASK) == 0 || \ + (mode & FMODE_FSNOTIFY_MASK) == (FMODE_NONOTIFY | FMODE_NONOTIFY_PERM)) +#define FMODE_FSNOTIFY_HSM (mode) \ + ((mode & FMODE_FSNOTIFY_MASK) == 0) +/* * Attribute flags. These should be or-ed together to figure out what * has been changed!
include/linux/fsnotify.h +23 -16
@@ -108,38 +108,35 @@ static inline void fsnotify_dentry(struct dentry *dentry, __u32 mask)fsnotify_parent (dentry, mask, dentry,FSNOTIFY_EVENT_DENTRY ); } -static inline int fsnotify_file (struct file *file,__u32 mask) +static inline int fsnotify_path (const struct path *path,__u32 mask) { -const struct path *path; +return fsnotify_parent (path->dentry, mask, path,FSNOTIFY_EVENT_PATH ); +} +static inline int fsnotify_file (struct file *file,__u32 mask) +{/* * FMODE_NONOTIFY are fds generated by fanotify itself which should not * generate new events. We also don't want to generate events for * FMODE_PATH fds (involves open & close events) as they are just * handle creation / destruction events and not "real" file events. */ -if (file->f_mode & (FMODE_NONOTIFY |FMODE_PATH )) +if (FMODE_FSNOTIFY_NONE (file->f_mode))return 0 ; - path = &file->f_path; -/* Permission events require group prio >= FSNOTIFY_PRIO_CONTENT */ -if (mask &ALL_FSNOTIFY_PERM_EVENTS && - !fsnotify_sb_has_priority_watchers (path->dentry->d_sb, -FSNOTIFY_PRIO_CONTENT )) -return 0 ; - -return fsnotify_parent (path->dentry, mask, path,FSNOTIFY_EVENT_PATH ); +return fsnotify_path (&file->f_path, mask); }#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS + +void file_set_fsnotify_mode (struct file *file); +/* * fsnotify_file_area_perm - permission hook before access to file range */ static inline int fsnotify_file_area_perm (struct file *file,int perm_mask,const loff_t *ppos,size_t count) { -__u32 fsnotify_mask =FS_ACCESS_PERM ; -/* * filesystem may be modified in the context of permission events * (e.g. by HSM filling a file on access), so sb freeze protection
@@ -150,7 +147,10 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask,if (!(perm_mask &MAY_READ ))return 0 ; -return fsnotify_file (file, fsnotify_mask); +if (likely (!FMODE_FSNOTIFY_PERM (file->f_mode))) +return 0 ; + +return fsnotify_path (&file->f_path,FS_ACCESS_PERM ); }/*
@@ -168,16 +168,23 @@ static inline int fsnotify_open_perm(struct file *file) {int ret; +if (likely (!FMODE_FSNOTIFY_PERM (file->f_mode))) +return 0 ; +if (file->f_flags & __FMODE_EXEC) { - ret =fsnotify_file (file,FS_OPEN_EXEC_PERM ); + ret =fsnotify_path (&file->f_path,FS_OPEN_EXEC_PERM );if (ret)return ret; } -return fsnotify_file (file,FS_OPEN_PERM ); +return fsnotify_path (&file->f_path,FS_OPEN_PERM ); }#else +static inline void file_set_fsnotify_mode (struct file *file) +{ +} +static inline int fsnotify_file_area_perm (struct file *file,int perm_mask,const loff_t *ppos,size_t count) {
Signature · 1 line
2.35.3
Jan Kara
maintainerPATCH v9 3/19 fsnotify: check if file is actually being watched for pre-content events on open
In reply to Jan KaraFrom: Amir Goldstein <[email protected]>
So far, we set FMODE_NONOTIFY_ flags at open time if we know that there are no permission event watchers at all on the filesystem, but lack of FMODE_NONOTIFY_ flags does not mean that the file is actually watched.
For pre-content events, it is possible to optimize things so that we don't bother trying to send pre-content events if file was not watched (through sb, mnt, parent or inode itself) on open. Set FMODE_NONOTIFY_ flags according to that.
--- fs/notify/fsnotify.c | 27 +++++++++++++++++++++++++-- include/linux/fsnotify_backend.h | 3 +++ 2 files changed, 28 insertions(+), 2 deletions(-)
fs/notify/fsnotify.c +25 -2
@@ -193,7 +193,7 @@ static bool fsnotify_event_needs_parent(struct inode *inode, __u32 mnt_mask,return mask & marks_mask; } -/* Are there any inode/mount/sb objects that are interested in this event? */ +/* Are there any inode/mount/sb objects that watch for these events? */ static inline bool fsnotify_object_watched (struct inode *inode,__u32 mnt_mask,__u32 mask) {
@@ -632,7 +632,9 @@ EXPORT_SYMBOL_GPL(fsnotify);*/ void file_set_fsnotify_mode (struct file *file) { -struct super_block *sb = file->f_path.dentry->d_sb; +struct dentry *dentry = file->f_path.dentry, *parent; +struct super_block *sb = dentry->d_sb; +__u32 mnt_mask, p_mask;/* Is it a file opened by fanotify? */ if (FMODE_FSNOTIFY_NONE (file->f_mode))
@@ -658,6 +660,27 @@ void file_set_fsnotify_mode(struct file *file) file->f_mode |=FMODE_NONOTIFY |FMODE_NONOTIFY_PERM ; return; } + +/* +* OK, there are some pre-content watchers. Check if anybody can be +* watching for pre-content events on *this* file. +*/ + mnt_mask =READ_ONCE (real_mount (file->f_path.mnt)->mnt_fsnotify_mask); +if (likely (!(dentry->d_flags &DCACHE_FSNOTIFY_PARENT_WATCHED ) && + !fsnotify_object_watched (d_inode (dentry), mnt_mask, +FSNOTIFY_PRE_CONTENT_EVENTS ))) { + file->f_mode |=FMODE_NONOTIFY |FMODE_NONOTIFY_PERM ; +return ; + } + +/* Even parent is not watching for pre-content events on this file? */ + parent =dget_parent (dentry); + p_mask =fsnotify_inode_watches_children (d_inode (parent)); +dput (parent); +if (!(p_mask &FSNOTIFY_PRE_CONTENT_EVENTS )) { + file->f_mode |=FMODE_NONOTIFY |FMODE_NONOTIFY_PERM ; +return ; + } }#endif
include/linux/fsnotify_backend.h +3 -0
@@ -77,6 +77,9 @@*/ #define ALL_FSNOTIFY_DIRENT_EVENTS (FS_CREATE | FS_DELETE | FS_MOVE | FS_RENAME) +/* Pre-content events can be used to fill file content */ +#define FSNOTIFY_PRE_CONTENT_EVENTS 0 +#define ALL_FSNOTIFY_PERM_EVENTS (FS_OPEN_PERM | FS_ACCESS_PERM | \ FS_OPEN_EXEC_PERM)
Signature · 1 line
2.35.3
Amir Goldstein
reviewerRe: [PATCH 03/19] fsnotify: check if file is actually being watched for pre-content events on open
Quoted 45 lines · Jan Kara
From: Amir Goldstein <[email protected]>
So far, we set
FMODE_NONOTIFY_flags at open time if we know that there are no permission event watchers at all on the filesystem, but lack ofFMODE_NONOTIFY_flags does not mean that the file is actually watched.For pre-content events, it is possible to optimize things so that we don't bother trying to send pre-content events if file was not watched (through sb, mnt, parent or inode itself) on open. Set
FMODE_NONOTIFY_flags according to that.Signed-off-by Amir Goldstein <[email protected]>Signed-off-by Jan Kara <[email protected]>--- fs/notify/fsnotify.c | 27 +++++++++++++++++++++++++-- include/linux/fsnotify_backend.h | 3 +++ 2 files changed, 28 insertions(+), 2 deletions(-)fs/notify/fsnotify.c +4
-2@@ -193,7 +193,7 @@ static bool fsnotify_event_needs_parent(struct inode *inode, __u32 mnt_mask,return mask & marks_mask; } -/* Are there any inode/mount/sb objects that are interested in this event? */ +/* Are there any inode/mount/sb objects that watch for these events? */ static inline bool fsnotify_object_watched (struct inode *inode,__u32 mnt_mask,__u32 mask) {@@ -632,7 +632,9 @@ EXPORT_SYMBOL_GPL(fsnotify);*/ void file_set_fsnotify_mode (struct file *file) { -struct super_block *sb = file->f_path.dentry->d_sb; +struct dentry *dentry = file->f_path.dentry, *parent; +struct super_block *sb = dentry->d_sb; +__u32 mnt_mask, p_mask;/* Is it a file opened by fanotify? */ if (FMODE_FSNOTIFY_NONE (file->f_mode))
fs/notify/fsnotify.c void file_set_fsnotify_mode(struct file *file) · line 662
Quoted 3 lines
fs/notify/fsnotify.c +0
-0@@ -658,6 +660,27 @@ void file_set_fsnotify_mode(struct file *file) file->f_mode |=FMODE_NONOTIFY |FMODE_NONOTIFY_PERM ;return ; }
This was lost in translation:
@@ -672,8 +672,10 @@ void file_set_fsnotify_mode(struct file *file)
/*
* If there are permission event watchers but no pre-content event
* watchers, set FMODE_NONOTIFY | FMODE_NONOTIFY_PERM to indicate that.
+ * Pre-content events are only reported for regular files and dirs.
*/
- if (likely(!fsnotify_sb_has_priority_watchers(sb,
+ if ((!d_is_dir(dentry) && !d_is_reg(dentry)) ||
+ likely(!fsnotify_sb_has_priority_watchers(sb,
FSNOTIFY_PRIO_PRE_CONTENT))) {
file->f_mode |= FMODE_NONOTIFY | FMODE_NONOTIFY_PERM;
return;Quoted 11 lines
fs/notify/fsnotify.c +11
-0+ +/* +* OK, there are some pre-content watchers. Check if anybody can be +* watching for pre-content events on *this* file. +*/ + mnt_mask =READ_ONCE (real_mount (file->f_path.mnt)->mnt_fsnotify_mask); +if (likely (!(dentry->d_flags &DCACHE_FSNOTIFY_PARENT_WATCHED ) && + !fsnotify_object_watched (d_inode (dentry), mnt_mask, +FSNOTIFY_PRE_CONTENT_EVENTS ))) { + file->f_mode |=FMODE_NONOTIFY |FMODE_NONOTIFY_PERM ; +return ;
fs/notify/fsnotify.c void file_set_fsnotify_mode(struct file *file) · line 683
Quoted 10 lines
fs/notify/fsnotify.c +10
-0+ } + +/* Even parent is not watching for pre-content events on this file? */ + parent =dget_parent (dentry); + p_mask =fsnotify_inode_watches_children (d_inode (parent)); +dput (parent); +if (!(p_mask &FSNOTIFY_PRE_CONTENT_EVENTS )) { + file->f_mode |=FMODE_NONOTIFY |FMODE_NONOTIFY_PERM ; +return ; + }
inlining broke the logic and your branch fails the new PRE_ACCESS test cases of fanotify03 LTP test (now pushed to branch fan_hsm).
Specifically in the test case that fails, parent is not watching and inode is watching pre-content and your code gets to the p_mask test and marks this file as no-pre-content watchers.
This passes the test:
@@ -684,17 +686,18 @@ void file_set_fsnotify_mode(struct file *file)
* watching for pre-content events on *this* file.
*/
mnt_mask = READ_ONCE(real_mount(file->f_path.mnt)->mnt_fsnotify_mask);- if (likely(!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED) &&
- !fsnotify_object_watched(d_inode(dentry), mnt_mask,
- FSNOTIFY_PRE_CONTENT_EVENTS))) {
- file->f_mode |= FMODE_NONOTIFY | FMODE_NONOTIFY_PERM;
+ if (unlikely(fsnotify_object_watched(d_inode(dentry), mnt_mask,
+ FSNOTIFY_PRE_CONTENT_EVENTS))) {
return;
}
/* Even parent is not watching for pre-content events on this file? */
- parent = dget_parent(dentry);
- p_mask = fsnotify_inode_watches_children(d_inode(parent));
- dput(parent);
+ p_mask = 0;
+ if (unlikely(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED)) {
+ parent = dget_parent(dentry);
+ p_mask = fsnotify_inode_watches_children(d_inode(parent));
+ dput(parent);
+ }
if (!(p_mask & FSNOTIFY_PRE_CONTENT_EVENTS)) {
file->f_mode |= FMODE_NONOTIFY | FMODE_NONOTIFY_PERM;
return;Thanks,
Amir.
Jan Kara
maintainerRe: [PATCH 03/19] fsnotify: check if file is actually being watched for pre-content events on open
Quoted 54 lines · Amir Goldstein
Quoted 45 lines · Jan Kara
From: Amir Goldstein <[email protected]>
So far, we set
FMODE_NONOTIFY_flags at open time if we know that there are no permission event watchers at all on the filesystem, but lack ofFMODE_NONOTIFY_flags does not mean that the file is actually watched.For pre-content events, it is possible to optimize things so that we don't bother trying to send pre-content events if file was not watched (through sb, mnt, parent or inode itself) on open. Set
FMODE_NONOTIFY_flags according to that.Signed-off-by Amir Goldstein <[email protected]>Signed-off-by Jan Kara <[email protected]>--- fs/notify/fsnotify.c | 27 +++++++++++++++++++++++++-- include/linux/fsnotify_backend.h | 3 +++ 2 files changed, 28 insertions(+), 2 deletions(-)fs/notify/fsnotify.c +4
-2@@ -193,7 +193,7 @@ static bool fsnotify_event_needs_parent(struct inode *inode, __u32 mnt_mask,return mask & marks_mask; } -/* Are there any inode/mount/sb objects that are interested in this event? */ +/* Are there any inode/mount/sb objects that watch for these events? */ static inline bool fsnotify_object_watched (struct inode *inode,__u32 mnt_mask,__u32 mask) {@@ -632,7 +632,9 @@ EXPORT_SYMBOL_GPL(fsnotify);*/ void file_set_fsnotify_mode (struct file *file) { -struct super_block *sb = file->f_path.dentry->d_sb; +struct dentry *dentry = file->f_path.dentry, *parent; +struct super_block *sb = dentry->d_sb; +__u32 mnt_mask, p_mask;/* Is it a file opened by fanotify? */ if (FMODE_FSNOTIFY_NONE (file->f_mode))fs/notify/fsnotify.c void file_set_fsnotify_mode(struct file *file) · line 662
Quoted 3 lines
fs/notify/fsnotify.c +0
-0@@ -658,6 +660,27 @@ void file_set_fsnotify_mode(struct file *file) file->f_mode |=FMODE_NONOTIFY |FMODE_NONOTIFY_PERM ;return ; }This was lost in translation:
(quoted diff) +0
-0@@ -672,8 +672,10 @@ void file_set_fsnotify_mode(struct file *file) /*
Review void file_set_fsnotify_mode(struct file *file) · line 681
Quoted 10 lines
(quoted diff) +3
-1* If there are permission event watchers but no pre-content event * watchers, set FMODE_NONOTIFY | FMODE_NONOTIFY_PERM to indicate that. + * Pre-content events are only reported for regular files and dirs. */ - if (likely(!fsnotify_sb_has_priority_watchers(sb, + if ((!d_is_dir(dentry) && !d_is_reg(dentry)) || + likely(!fsnotify_sb_has_priority_watchers(sb, FSNOTIFY_PRIO_PRE_CONTENT))) { file->f_mode |= FMODE_NONOTIFY | FMODE_NONOTIFY_PERM; return;
Right. Specifically the (!d_is_dir(dentry) && !d_is_reg(dentry)) got lost, likely when I moved stuff between different commits and then was resolving patch conflicts. I'll add it back. Thanks for finding this!
Quoted 47 lines
Quoted 11 lines
fs/notify/fsnotify.c +11
-0+ +/* +* OK, there are some pre-content watchers. Check if anybody can be +* watching for pre-content events on *this* file. +*/ + mnt_mask =READ_ONCE (real_mount (file->f_path.mnt)->mnt_fsnotify_mask); +if (likely (!(dentry->d_flags &DCACHE_FSNOTIFY_PARENT_WATCHED ) && + !fsnotify_object_watched (d_inode (dentry), mnt_mask, +FSNOTIFY_PRE_CONTENT_EVENTS ))) { + file->f_mode |=FMODE_NONOTIFY |FMODE_NONOTIFY_PERM ; +return ;fs/notify/fsnotify.c void file_set_fsnotify_mode(struct file *file) · line 683
Quoted 10 lines
fs/notify/fsnotify.c +10
-0+ } + +/* Even parent is not watching for pre-content events on this file? */ + parent =dget_parent (dentry); + p_mask =fsnotify_inode_watches_children (d_inode (parent)); +dput (parent); +if (!(p_mask &FSNOTIFY_PRE_CONTENT_EVENTS )) { + file->f_mode |=FMODE_NONOTIFY |FMODE_NONOTIFY_PERM ; +return ; + }inlining broke the logic and your branch fails the new
PRE_ACCESStest cases of fanotify03 LTP test (now pushed to branchfan_hsm).Specifically in the test case that fails, parent is not watching and inode is watching pre-content and your code gets to the
p_masktest and marks this file as no-pre-content watchers.This passes the test:
(quoted diff) +2
-6@@ -684,17 +686,18 @@ void file_set_fsnotify_mode(struct file *file) * watching for pre-content events on *this* file. */ mnt_mask = READ_ONCE(real_mount(file->f_path.mnt)->mnt_fsnotify_mask); - if (likely(!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED) && - !fsnotify_object_watched(d_inode(dentry), mnt_mask, - FSNOTIFY_PRE_CONTENT_EVENTS))) { - file->f_mode |= FMODE_NONOTIFY | FMODE_NONOTIFY_PERM; + if (unlikely(fsnotify_object_watched(d_inode(dentry), mnt_mask, + FSNOTIFY_PRE_CONTENT_EVENTS))) { return; } /* Even parent is not watching for pre-content events on this file? */ - parent = dget_parent(dentry); - p_mask = fsnotify_inode_watches_children(d_inode(parent));
Review void file_set_fsnotify_mode(struct file *file) · line 703
Quoted 10 lines
(quoted diff) +6
-1- dput(parent); + p_mask = 0; + if (unlikely(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED)) { + parent = dget_parent(dentry); + p_mask = fsnotify_inode_watches_children(d_inode(parent)); + dput(parent); + } if (!(p_mask & FSNOTIFY_PRE_CONTENT_EVENTS)) { file->f_mode |= FMODE_NONOTIFY | FMODE_NONOTIFY_PERM; return;
Right. I've fixed that up. Thanks!
LTP from your fan_hsm branch is now passing so I've re-pushed the current state to my fsnotify_hsm branch.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Jan Kara
maintainerPATCH v9 4/19 fanotify: don't skip extra event info if no info_mode is set
In reply to Jan KaraFrom: Amir Goldstein <[email protected]>
Previously we would only include optional information if you requested it via an FAN_ flag at fanotify_init time (FAN_REPORT_FID for example). However this isn't necessary as the event length is encoded in the metadata, and if the user doesn't want to consume the information they don't have to. With the PRE_ACCESS events we will always generate range information, so drop this check in order to allow this extra information to be exported without needing to have another flag.
--- fs/notify/fanotify/fanotify_user.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)
fs/notify/fanotify/fanotify_user.c +4 -9
@@ -159,9 +159,6 @@ static size_t fanotify_event_len(unsigned int info_mode,int fh_len;int dot_len =0 ; -if (!info_mode) -return event_len; -if (fanotify_is_error_event (event->mask)) event_len +=FANOTIFY_ERROR_INFO_LEN ;
@@ -755,12 +752,10 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group, buf +=FAN_EVENT_METADATA_LEN ; count -=FAN_EVENT_METADATA_LEN ; -if (info_mode) { - ret =copy_info_records_to_user (event, info, info_mode, pidfd, - buf, count); -if (ret <0 ) - goto out_close_fd; - } + ret =copy_info_records_to_user (event, info, info_mode, pidfd, + buf, count); +if (ret <0 ) + goto out_close_fd;if (f)fd_install (fd, f);
Signature · 1 line
2.35.3
Jan Kara
maintainerPATCH v9 5/19 fanotify: rename a misnamed constant
In reply to Jan KaraFrom: Amir Goldstein <[email protected]>
FANOTIFY_PIDFD_INFO_HDR_LEN is not the length of the header.
--- fs/notify/fanotify/fanotify_user.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
fs/notify/fanotify/fanotify_user.c +5 -5
@@ -118,7 +118,7 @@ struct kmem_cache *fanotify_perm_event_cachep __ro_after_init;#define FANOTIFY_EVENT_ALIGN 4#define FANOTIFY_FID_INFO_HDR_LEN \ (sizeof(struct fanotify_event_info_fid) + sizeof(struct file_handle)) -#define FANOTIFY_PIDFD_INFO_HDR_LEN \ +#define FANOTIFY_PIDFD_INFO_LEN \ sizeof(struct fanotify_event_info_pidfd)#define FANOTIFY_ERROR_INFO_LEN \ (sizeof(struct fanotify_event_info_error))
@@ -173,14 +173,14 @@ static size_t fanotify_event_len(unsigned int info_mode, dot_len =1 ; } -if (info_mode &FAN_REPORT_PIDFD ) - event_len +=FANOTIFY_PIDFD_INFO_HDR_LEN ; -if (fanotify_event_has_object_fh (event)) { fh_len =fanotify_event_object_fh_len (event); event_len +=fanotify_fid_info_len (fh_len, dot_len); } +if (info_mode &FAN_REPORT_PIDFD ) + event_len +=FANOTIFY_PIDFD_INFO_LEN ; +return event_len; }
@@ -502,7 +502,7 @@ static int copy_pidfd_info_to_user(int pidfd, size_t count) {struct fanotify_event_info_pidfd info = { }; -size_t info_len =FANOTIFY_PIDFD_INFO_HDR_LEN ; +size_t info_len =FANOTIFY_PIDFD_INFO_LEN ;if (WARN_ON_ONCE (info_len > count))return -EFAULT ;
Signature · 1 line
2.35.3
Jan Kara
maintainerPATCH v9 6/19 fanotify: reserve event bit of deprecated FAN_DIR_MODIFY
In reply to Jan KaraFrom: Amir Goldstein <[email protected]>
Avoid reusing it, because we would like to reserve it for future FAN_PATH_MODIFY pre-content event.
--- include/linux/fsnotify_backend.h | 1 + include/uapi/linux/fanotify.h | 1 + 2 files changed, 2 insertions(+)
include/linux/fsnotify_backend.h +1 -0
@@ -55,6 +55,7 @@#define FS_OPEN_PERM 0x00010000/* open event in an permission hook */ #define FS_ACCESS_PERM 0x00020000/* access event in a permissions hook */ #define FS_OPEN_EXEC_PERM 0x00040000/* open/exec event in a permission hook */ +/* #define FS_DIR_MODIFY 0x00080000 */ /* Deprecated (reserved) */ /* * Set on inode mark that cares about things that happen to its children.
include/uapi/linux/fanotify.h +1 -0
@@ -25,6 +25,7 @@#define FAN_OPEN_PERM 0x00010000/* File open in perm check */ #define FAN_ACCESS_PERM 0x00020000/* File accessed in perm check */ #define FAN_OPEN_EXEC_PERM 0x00040000/* File open/exec in perm check */ +/* #define FAN_DIR_MODIFY 0x00080000 */ /* Deprecated (reserved) */ #define FAN_EVENT_ON_CHILD 0x08000000/* Interested in child events */
Signature · 1 line
2.35.3
Jan Kara
maintainerPATCH v9 7/19 fsnotify: introduce pre-content permission events
In reply to Jan KaraFrom: Amir Goldstein <[email protected]>
The new FS_PRE_ACCESS permission event is similar to FS_ACCESS_PERM, but it meant for a different use case of filling file content before access to a file range, so it has slightly different semantics.
Generate FS_PRE_ACCESS/FS_ACCESS_PERM as two seperate events, so content scanners could inspect the content filled by pre-content event handler.
Unlike FS_ACCESS_PERM, FS_PRE_ACCESS is also called before a file is modified by syscalls as write() and fallocate().
FS_ACCESS_PERM is reported also on blockdev and pipes, but the new pre-content events are only reported for regular files and dirs.
The pre-content events are meant to be used by hierarchical storage managers that want to fill the content of files on first access.
There are some specific requirements from filesystems that could be used with pre-content events, so add a flag for fs to opt-in for pre-content events explicitly before they can be used.
--- fs/notify/fsnotify.c | 2 +- include/linux/fs.h | 1 + include/linux/fsnotify.h | 19 ++++++++++++++++++- include/linux/fsnotify_backend.h | 11 ++++++++--- security/selinux/hooks.c | 3 ++- 5 files changed, 30 insertions(+), 6 deletions(-)
fs/notify/fsnotify.c +1 -1
@@ -688,7 +688,7 @@ static __init int fsnotify_init(void) {int ret; -BUILD_BUG_ON (HWEIGHT32 (ALL_FSNOTIFY_BITS ) !=23 ); +BUILD_BUG_ON (HWEIGHT32 (ALL_FSNOTIFY_BITS ) !=24 ); ret =init_srcu_struct (&fsnotify_mark_srcu);if (ret)
include/linux/fs.h +1 -0
@@ -1257,6 +1257,7 @@ extern int send_sigurg(struct file *file);#define SB_I_RETIRED 0x00000800/* superblock shouldn't be reused */ #define SB_I_NOUMASK 0x00001000/* VFS does not apply umask */ #define SB_I_NOIDMAP 0x00002000/* No idmapped mounts on this superblock */ +#define SB_I_ALLOW_HSM 0x00004000/* Allow HSM events on this superblock */ /* Possible states of 'frozen' field */ enum {
include/linux/fsnotify.h +18 -1
@@ -144,12 +144,29 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask,*/ lockdep_assert_once (file_write_not_started (file)); -if (!(perm_mask &MAY_READ )) +if (!(perm_mask & (MAY_READ |MAY_WRITE |MAY_ACCESS )))return 0 ;if (likely (!FMODE_FSNOTIFY_PERM (file->f_mode)))return 0 ; +/* +* read()/write() and other types of access generate pre-content events. +*/ +if (unlikely (FMODE_FSNOTIFY_HSM (file->f_mode))) { +int ret =fsnotify_path (&file->f_path,FS_PRE_ACCESS ); + +if (ret) +return ret; + } + +if (!(perm_mask &MAY_READ )) +return 0 ; + +/* +* read() also generates the legacy FS_ACCESS_PERM event, so content +* scanners can inspect the content filled by pre-content event. +*/ return fsnotify_path (&file->f_path,FS_ACCESS_PERM ); }
include/linux/fsnotify_backend.h +8 -3
@@ -57,6 +57,8 @@#define FS_OPEN_EXEC_PERM 0x00040000/* open/exec event in a permission hook */ /* #define FS_DIR_MODIFY 0x00080000 */ /* Deprecated (reserved) */ +#define FS_PRE_ACCESS 0x00100000/* Pre-content access hook */ +/* * Set on inode mark that cares about things that happen to its children. * Always set for dnotify and inotify.
@@ -78,11 +80,14 @@*/ #define ALL_FSNOTIFY_DIRENT_EVENTS (FS_CREATE | FS_DELETE | FS_MOVE | FS_RENAME) +/* Content events can be used to inspect file content */ +#define FSNOTIFY_CONTENT_PERM_EVENTS (FS_OPEN_PERM | FS_OPEN_EXEC_PERM | \ + FS_ACCESS_PERM)/* Pre-content events can be used to fill file content */ -#define FSNOTIFY_PRE_CONTENT_EVENTS 0 +#define FSNOTIFY_PRE_CONTENT_EVENTS (FS_PRE_ACCESS) -#define ALL_FSNOTIFY_PERM_EVENTS (FS_OPEN_PERM | FS_ACCESS_PERM | \ - FS_OPEN_EXEC_PERM) +#define ALL_FSNOTIFY_PERM_EVENTS (FSNOTIFY_CONTENT_PERM_EVENTS | \ + FSNOTIFY_PRE_CONTENT_EVENTS)/* * This is a list of all events that may get sent to a parent that is watching
security/selinux/hooks.c +2 -1
@@ -3404,7 +3404,8 @@ static int selinux_path_notify(const struct path *path, u64 mask, perm |=FILE__WATCH_WITH_PERM ;/* watches on read-like events need the file:watch_reads permission */ -if (mask & (FS_ACCESS |FS_ACCESS_PERM |FS_CLOSE_NOWRITE )) +if (mask & (FS_ACCESS |FS_ACCESS_PERM |FS_PRE_ACCESS | +FS_CLOSE_NOWRITE )) perm |=FILE__WATCH_READS ;return path_has_perm (current_cred (), path, perm);
Signature · 1 line
2.35.3
Jan Kara
maintainerPATCH v9 8/19 fsnotify: pass optional file access range in pre-content event
In reply to Jan KaraFrom: Amir Goldstein <[email protected]>
We would like to add file range information to pre-content events.
Pass a struct file_range with offset and length to event handler along with pre-content permission event.
The offset and length are aligned to page size, but we may need to align them to minimum folio size for filesystems with large block size.
--- fs/notify/fanotify/fanotify.c | 11 +++++++-- fs/notify/fanotify/fanotify.h | 2 ++ fs/notify/fsnotify.c | 18 ++++++++++++++ include/linux/fsnotify.h | 4 ++-- include/linux/fsnotify_backend.h | 40 ++++++++++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 4 deletions(-)
fs/notify/fanotify/fanotify.c +9 -2
@@ -549,9 +549,13 @@ static struct fanotify_event *fanotify_alloc_path_event(const struct path *path,return &pevent->fae; } -static struct fanotify_event *fanotify_alloc_perm_event (const struct path *path, +static struct fanotify_event *fanotify_alloc_perm_event (const void *data, +int data_type,gfp_t gfp) { +const struct path *path =fsnotify_data_path (data, data_type); +const struct file_range *range = +fsnotify_data_file_range (data, data_type);struct fanotify_perm_event *pevent; pevent =kmem_cache_alloc (fanotify_perm_event_cachep, gfp);
@@ -565,6 +569,9 @@ static struct fanotify_event *fanotify_alloc_perm_event(const struct path *path, pevent->hdr.len =0 ; pevent->state =FAN_EVENT_INIT ; pevent->path = *path; +/* NULL ppos means no range info */ + pevent->ppos = range ? &range->pos :NULL ; + pevent->count = range ? range->count :0 ;path_get (path);return &pevent->fae;
@@ -802,7 +809,7 @@ static struct fanotify_event *fanotify_alloc_event( old_memcg =set_active_memcg (group->memcg);if (fanotify_is_perm_event (mask)) { - event =fanotify_alloc_perm_event (path, gfp); + event =fanotify_alloc_perm_event (data, data_type, gfp); }else if (fanotify_is_error_event (mask)) { event =fanotify_alloc_error_event (group, fsid, data, data_type, &hash);
fs/notify/fanotify/fanotify.h +2 -0
@@ -425,6 +425,8 @@ FANOTIFY_PE(struct fanotify_event *event)struct fanotify_perm_event {struct fanotify_event fae;struct path path; +const loff_t *ppos;/* optional file range info */ +size_t count;u32 response;/* userspace answer to the event */ unsigned short state;/* state of the event */ int fd;/* fd we passed to userspace for this event */
fs/notify/fsnotify.c +18 -0
@@ -203,6 +203,24 @@ static inline bool fsnotify_object_watched(struct inode *inode, __u32 mnt_mask,return mask & marks_mask &ALL_FSNOTIFY_EVENTS ; } +/* Report pre-content event with optional range info */ +int fsnotify_pre_content (const struct path *path,const loff_t *ppos, +size_t count) +{ +struct file_range range; + +/* Report page aligned range only when pos is known */ +if (!ppos) +return fsnotify_path (path,FS_PRE_ACCESS ); + + range.path = path; + range.pos =PAGE_ALIGN_DOWN (*ppos); + range.count =PAGE_ALIGN (*ppos + count) - range.pos; + +return fsnotify_parent (path->dentry,FS_PRE_ACCESS , &range, +FSNOTIFY_EVENT_FILE_RANGE ); +} +/* * Notify this dentry's parent about a child's events with child name info * if parent is watching or if inode/sb/mount are interested in events with
include/linux/fsnotify.h +2 -2
@@ -154,7 +154,7 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask,* read()/write() and other types of access generate pre-content events. */ if (unlikely (FMODE_FSNOTIFY_HSM (file->f_mode))) { -int ret =fsnotify_path (&file->f_path,FS_PRE_ACCESS ); +int ret =fsnotify_pre_content (&file->f_path, ppos, count);if (ret)return ret;
@@ -171,7 +171,7 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask, }/* -* fsnotify_file_perm - permission hook before file access +* fsnotify_file_perm - permission hook before file access (unknown range) */ static inline int fsnotify_file_perm (struct file *file,int perm_mask) {
include/linux/fsnotify_backend.h +40 -0
@@ -294,6 +294,7 @@ static inline void fsnotify_group_assert_locked(struct fsnotify_group *group)/* When calling fsnotify tell it if the data is a path or inode */ enum fsnotify_data_type {FSNOTIFY_EVENT_NONE , +FSNOTIFY_EVENT_FILE_RANGE ,FSNOTIFY_EVENT_PATH ,FSNOTIFY_EVENT_INODE ,FSNOTIFY_EVENT_DENTRY ,
@@ -306,6 +307,17 @@ struct fs_error_report {struct super_block *sb; }; +struct file_range { +const struct path *path; +loff_t pos; +size_t count; +}; + +static inline const struct path *file_range_path (const struct file_range *range) +{ +return range->path; +} +static inline struct inode *fsnotify_data_inode (const void *data,int data_type) {switch (data_type) {
@@ -315,6 +327,8 @@ static inline struct inode *fsnotify_data_inode(const void *data, int data_type)return d_inode (data);case FSNOTIFY_EVENT_PATH :return d_inode (((const struct path *)data)->dentry); +case FSNOTIFY_EVENT_FILE_RANGE : +return d_inode (file_range_path (data)->dentry);case FSNOTIFY_EVENT_ERROR :return ((struct fs_error_report *)data)->inode;default :
@@ -330,6 +344,8 @@ static inline struct dentry *fsnotify_data_dentry(const void *data, int data_typreturn (struct dentry *)data;case FSNOTIFY_EVENT_PATH :return ((const struct path *)data)->dentry; +case FSNOTIFY_EVENT_FILE_RANGE : +return file_range_path (data)->dentry;default :return NULL ; }
@@ -341,6 +357,8 @@ static inline const struct path *fsnotify_data_path(const void *data,switch (data_type) {case FSNOTIFY_EVENT_PATH :return data; +case FSNOTIFY_EVENT_FILE_RANGE : +return file_range_path (data);default :return NULL ; }
@@ -356,6 +374,8 @@ static inline struct super_block *fsnotify_data_sb(const void *data,return ((struct dentry *)data)->d_sb;case FSNOTIFY_EVENT_PATH :return ((const struct path *)data)->dentry->d_sb; +case FSNOTIFY_EVENT_FILE_RANGE : +return file_range_path (data)->dentry->d_sb;case FSNOTIFY_EVENT_ERROR :return ((struct fs_error_report *) data)->sb;default :
@@ -375,6 +395,18 @@ static inline struct fs_error_report *fsnotify_data_error_report( } } +static inline const struct file_range *fsnotify_data_file_range ( +const void *data, +int data_type) +{ +switch (data_type) { +case FSNOTIFY_EVENT_FILE_RANGE : +return (struct file_range *)data; +default : +return NULL ; + } +} +/* * Index to merged marks iterator array that correlates to a type of watch. * The type of watched object can be deduced from the iterator type, but not
@@ -863,9 +895,17 @@ static inline void fsnotify_init_event(struct fsnotify_event *event) {INIT_LIST_HEAD (&event->list); } +int fsnotify_pre_content (const struct path *path,const loff_t *ppos, +size_t count);#else +static inline int fsnotify_pre_content (const struct path *path, +const loff_t *ppos,size_t count) +{ +return 0 ; +} +static inline int fsnotify (__u32 mask,const void *data,int data_type,struct inode *dir,const struct qstr *name,struct inode *inode,u32 cookie)
Signature · 1 line
2.35.3
Jan Kara
maintainerPATCH v9 9/19 fsnotify: generate pre-content permission event on truncate
In reply to Jan KaraFrom: Amir Goldstein <[email protected]>
Generate FS_PRE_ACCESS event before truncate, without sb_writers held.
Move the security hooks also before sb_start_write() to conform with other security hooks (e.g. in write, fallocate).
The event will have a range info of the page surrounding the new size to provide an opportunity to fill the conetnt at the end of file before truncating to non-page aligned size.
--- fs/open.c | 31 +++++++++++++++++++++---------- include/linux/fsnotify.h | 20 ++++++++++++++++++++ 2 files changed, 41 insertions(+), 10 deletions(-)
fs/open.c +21 -10
@@ -81,14 +81,18 @@ long vfs_truncate(const struct path *path, loff_t length)if (!S_ISREG (inode->i_mode))return -EINVAL ; - error =mnt_want_write (path->mnt); -if (error) - goto out; - idmap =mnt_idmap (path->mnt); error =inode_permission (idmap, inode,MAY_WRITE );if (error) - goto mnt_drop_write_and_out; +return error; + + error =fsnotify_truncate_perm (path, length); +if (error) +return error; + + error =mnt_want_write (path->mnt); +if (error) +return error; error = -EPERM ;if (IS_APPEND (inode))
@@ -114,7 +118,7 @@ long vfs_truncate(const struct path *path, loff_t length)put_write_access (inode); mnt_drop_write_and_out:mnt_drop_write (path->mnt); -out: +return error; }EXPORT_SYMBOL_GPL (vfs_truncate);
@@ -175,11 +179,18 @@ long do_ftruncate(struct file *file, loff_t length, int small)/* Check IS_APPEND on real upper inode */ if (IS_APPEND (file_inode (file)))return -EPERM ; -sb_start_write (inode->i_sb); + error =security_file_truncate (file); -if (!error) - error =do_truncate (file_mnt_idmap (file), dentry, length, -ATTR_MTIME |ATTR_CTIME , file); +if (error) +return error; + + error =fsnotify_truncate_perm (&file->f_path, length); +if (error) +return error; + +sb_start_write (inode->i_sb); + error =do_truncate (file_mnt_idmap (file), dentry, length, +ATTR_MTIME |ATTR_CTIME , file);sb_end_write (inode->i_sb);return error;
include/linux/fsnotify.h +20 -0
@@ -170,6 +170,21 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask,return fsnotify_path (&file->f_path,FS_ACCESS_PERM ); } +/* +* fsnotify_truncate_perm - permission hook before file truncate +*/ +static inline int fsnotify_truncate_perm (const struct path *path,loff_t length) +{ +struct inode *inode =d_inode (path->dentry); + +if (!(inode->i_sb->s_iflags &SB_I_ALLOW_HSM ) || + !fsnotify_sb_has_priority_watchers (inode->i_sb, +FSNOTIFY_PRIO_PRE_CONTENT )) +return 0 ; + +return fsnotify_pre_content (path, &length,0 ); +} +/* * fsnotify_file_perm - permission hook before file access (unknown range) */
@@ -208,6 +223,11 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask,return 0 ; } +static inline int fsnotify_truncate_perm (const struct path *path,loff_t length) +{ +return 0 ; +} +static inline int fsnotify_file_perm (struct file *file,int perm_mask) {return 0 ;
Signature · 1 line
2.35.3
Jan Kara
maintainerPATCH v9 10/19 fanotify: introduce FAN_PRE_ACCESS permission event
In reply to Jan KaraFrom: Amir Goldstein <[email protected]>
Similar to FAN_ACCESS_PERM permission event, but it is only allowed with class FAN_CLASS_PRE_CONTENT and only allowed on regular files and dirs.
Unlike FAN_ACCESS_PERM, it is safe to write to the file being accessed in the context of the event handler.
This pre-content event is meant to be used by hierarchical storage managers that want to fill the content of files on first read access.
--- fs/notify/fanotify/fanotify.c | 3 ++- fs/notify/fanotify/fanotify_user.c | 22 +++++++++++++++++++--- include/linux/fanotify.h | 14 ++++++++++---- include/uapi/linux/fanotify.h | 2 ++ 4 files changed, 33 insertions(+), 8 deletions(-)
fs/notify/fanotify/fanotify.c +2 -1
@@ -917,8 +917,9 @@ static int fanotify_handle_event(struct fsnotify_group *group, u32 mask,BUILD_BUG_ON (FAN_OPEN_EXEC_PERM !=FS_OPEN_EXEC_PERM );BUILD_BUG_ON (FAN_FS_ERROR !=FS_ERROR );BUILD_BUG_ON (FAN_RENAME !=FS_RENAME ); +BUILD_BUG_ON (FAN_PRE_ACCESS !=FS_PRE_ACCESS ); -BUILD_BUG_ON (HWEIGHT32 (ALL_FANOTIFY_EVENT_BITS ) !=21 ); +BUILD_BUG_ON (HWEIGHT32 (ALL_FANOTIFY_EVENT_BITS ) !=22 ); mask =fanotify_group_event_mask (group, iter_info, &match_mask, mask, data, data_type, dir);
fs/notify/fanotify/fanotify_user.c +19 -3
@@ -1646,11 +1646,23 @@ static int fanotify_events_supported(struct fsnotify_group *group, unsigned int flags) {unsigned int mark_type = flags &FANOTIFY_MARK_TYPE_BITS ; +bool is_dir =d_is_dir (path->dentry);/* Strict validation of events in non-dir inode mask with v5.17+ APIs */ bool strict_dir_events =FAN_GROUP_FLAG (group,FAN_REPORT_TARGET_FID ) || (mask &FAN_RENAME ) || (flags &FAN_MARK_IGNORE ); +/* +* Filesystems need to opt-into pre-content evnets (a.k.a HSM) +* and they are only supported on regular files and directories. +*/ +if (mask &FANOTIFY_PRE_CONTENT_EVENTS ) { +if (!(path->mnt->mnt_sb->s_iflags &SB_I_ALLOW_HSM )) +return -EOPNOTSUPP ; +if (!is_dir && !d_is_reg (path->dentry)) +return -EINVAL ; + } +/* * Some filesystems such as 'proc' acquire unusual locks when opening * files. For them fanotify permission events have high chances of
@@ -1683,7 +1695,7 @@ static int fanotify_events_supported(struct fsnotify_group *group,* but because we always allowed it, error only when using new APIs. */ if (strict_dir_events && mark_type ==FAN_MARK_INODE && - !d_is_dir (path->dentry) && (mask &FANOTIFY_DIRONLY_EVENT_BITS )) + !is_dir && (mask &FANOTIFY_DIRONLY_EVENT_BITS ))return -ENOTDIR ;return 0 ;
@@ -1787,11 +1799,15 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, goto fput_and_out;/* -* Permission events require minimum priority FAN_CLASS_CONTENT. +* Permission events are not allowed for FAN_CLASS_NOTIF. +* Pre-content permission events are not allowed for FAN_CLASS_CONTENT. */ ret = -EINVAL ;if (mask &FANOTIFY_PERM_EVENTS && - group->priority <FSNOTIFY_PRIO_CONTENT ) + group->priority ==FSNOTIFY_PRIO_NORMAL ) + goto fput_and_out; +else if (mask &FANOTIFY_PRE_CONTENT_EVENTS && + group->priority ==FSNOTIFY_PRIO_CONTENT ) goto fput_and_out; if (mask & FAN_FS_ERROR &&
include/linux/fanotify.h +10 -4
@@ -89,6 +89,16 @@#define FANOTIFY_DIRENT_EVENTS (FAN_MOVE | FAN_CREATE | FAN_DELETE | \ FAN_RENAME) +/* Content events can be used to inspect file content */ +#define FANOTIFY_CONTENT_PERM_EVENTS (FAN_OPEN_PERM | FAN_OPEN_EXEC_PERM | \ + FAN_ACCESS_PERM) +/* Pre-content events can be used to fill file content */ +#define FANOTIFY_PRE_CONTENT_EVENTS (FAN_PRE_ACCESS) + +/* Events that require a permission response from user */ +#define FANOTIFY_PERM_EVENTS (FANOTIFY_CONTENT_PERM_EVENTS | \ + FANOTIFY_PRE_CONTENT_EVENTS) +/* Events that can be reported with event->fd */ #define FANOTIFY_FD_EVENTS (FANOTIFY_PATH_EVENTS | FANOTIFY_PERM_EVENTS)
@@ -104,10 +114,6 @@ FANOTIFY_INODE_EVENTS | \ FANOTIFY_ERROR_EVENTS) -/* Events that require a permission response from user */ -#define FANOTIFY_PERM_EVENTS (FAN_OPEN_PERM | FAN_ACCESS_PERM | \ - FAN_OPEN_EXEC_PERM) -/* Extra flags that may be reported with event or control handling of events */ #define FANOTIFY_EVENT_FLAGS (FAN_EVENT_ON_CHILD | FAN_ONDIR)
include/uapi/linux/fanotify.h +2 -0
@@ -27,6 +27,8 @@#define FAN_OPEN_EXEC_PERM 0x00040000/* File open/exec in perm check */ /* #define FAN_DIR_MODIFY 0x00080000 */ /* Deprecated (reserved) */ +#define FAN_PRE_ACCESS 0x00100000/* Pre-content access hook */ +#define FAN_EVENT_ON_CHILD 0x08000000/* Interested in child events */ #define FAN_RENAME 0x10000000/* File was renamed */
Signature · 1 line
2.35.3
Jan Kara
maintainerPATCH v9 11/19 fanotify: report file range info with pre-content events
In reply to Jan KaraFrom: Amir Goldstein <[email protected]>
With group class FAN_CLASS_PRE_CONTENT, report offset and length info along with FAN_PRE_ACCESS pre-content events.
This information is meant to be used by hierarchical storage managers that want to fill partial content of files on first access to range.
--- fs/notify/fanotify/fanotify.h | 8 +++++++ fs/notify/fanotify/fanotify_user.c | 38 ++++++++++++++++++++++++++++++ include/uapi/linux/fanotify.h | 8 +++++++ 3 files changed, 54 insertions(+)
fs/notify/fanotify/fanotify.h +8 -0
@@ -448,6 +448,14 @@ static inline bool fanotify_is_perm_event(u32 mask) mask &FANOTIFY_PERM_EVENTS ; } +static inline bool fanotify_event_has_access_range (struct fanotify_event *event) +{ +if (!(event->mask &FANOTIFY_PRE_CONTENT_EVENTS )) +return false; + +return FANOTIFY_PERM (event)->ppos; +} +static inline struct fanotify_event *FANOTIFY_E (struct fsnotify_event *fse) { return container_of(fse, struct fanotify_event, fse);
fs/notify/fanotify/fanotify_user.c +38 -0
@@ -122,6 +122,8 @@ struct kmem_cache *fanotify_perm_event_cachep __ro_after_init; sizeof(struct fanotify_event_info_pidfd)#define FANOTIFY_ERROR_INFO_LEN \ (sizeof(struct fanotify_event_info_error)) +#define FANOTIFY_RANGE_INFO_LEN \ + (sizeof(struct fanotify_event_info_range))static int fanotify_fid_info_len (int fh_len,int name_len) {
@@ -181,6 +183,9 @@ static size_t fanotify_event_len(unsigned int info_mode,if (info_mode &FAN_REPORT_PIDFD ) event_len +=FANOTIFY_PIDFD_INFO_LEN ; +if (fanotify_event_has_access_range (event)) + event_len +=FANOTIFY_RANGE_INFO_LEN ; +return event_len; }
@@ -517,6 +522,30 @@ static int copy_pidfd_info_to_user(int pidfd,return info_len; } +static size_t copy_range_info_to_user (struct fanotify_event *event, +char __user *buf,int count) +{ +struct fanotify_perm_event *pevent =FANOTIFY_PERM (event); +struct fanotify_event_info_range info = { }; +size_t info_len =FANOTIFY_RANGE_INFO_LEN ; + +if (WARN_ON_ONCE (info_len > count)) +return -EFAULT ; + +if (WARN_ON_ONCE (!pevent->ppos)) +return -EINVAL ; + + info.hdr.info_type =FAN_EVENT_INFO_TYPE_RANGE ; + info.hdr.len = info_len; + info.offset = *(pevent->ppos); + info.count = pevent->count; + +if (copy_to_user (buf, &info, info_len)) +return -EFAULT ; + +return info_len; +} + static int copy_info_records_to_user(struct fanotify_event *event, struct fanotify_info *info, unsigned int info_mode, int pidfd,
@@ -638,6 +667,15 @@ static int copy_info_records_to_user(struct fanotify_event *event, total_bytes += ret; } +if (fanotify_event_has_access_range (event)) { + ret =copy_range_info_to_user (event, buf, count); +if (ret <0 ) +return ret; + buf += ret; + count -= ret; + total_bytes += ret; + } +return total_bytes; }
include/uapi/linux/fanotify.h +8 -0
@@ -146,6 +146,7 @@ struct fanotify_event_metadata {#define FAN_EVENT_INFO_TYPE_DFID 3#define FAN_EVENT_INFO_TYPE_PIDFD 4#define FAN_EVENT_INFO_TYPE_ERROR 5 +#define FAN_EVENT_INFO_TYPE_RANGE 6/* Special info types for FAN_RENAME */ #define FAN_EVENT_INFO_TYPE_OLD_DFID_NAME 10
@@ -192,6 +193,13 @@ struct fanotify_event_info_error {__u32 error_count; }; +struct fanotify_event_info_range { +struct fanotify_event_info_header hdr; +__u32 pad; +__u64 offset; +__u64 count; +}; +/* * User space may need to record additional information about its decision. * The extra information type records what kind of information is included.
Signature · 1 line
2.35.3
Jan Kara
maintainerPATCH v9 12/19 fanotify: allow to set errno in FAN_DENY permission response
In reply to Jan KaraFrom: Amir Goldstein <[email protected]>
With FAN_DENY response, user trying to perform the filesystem operation gets an error with errno set to EPERM.
It is useful for hierarchical storage management (HSM) service to be able to deny access for reasons more diverse than EPERM, for example EAGAIN, if HSM could retry the operation later.
Allow fanotify groups with priority FAN_CLASSS_PRE_CONTENT to responsd to permission events with the response value FAN_DENY_ERRNO(errno), instead of FAN_DENY to return a custom error.
Limit custom error values to errors expected on read(2)/write(2) and open(2) of regular files. This list could be extended in the future. Userspace can test for legitimate values of FAN_DENY_ERRNO(errno) by writing a response to an fanotify group fd with a value of FAN_NOFD in the fd field of the response.
The change in fanotify_response is backward compatible, because errno is written in the high 8 bits of the 32bit response field and old kernels reject respose value with high bits set.
--- fs/notify/fanotify/fanotify.c | 17 +++++++++++++---- fs/notify/fanotify/fanotify.h | 5 +++++ fs/notify/fanotify/fanotify_user.c | 29 +++++++++++++++++++++++++++-- include/linux/fanotify.h | 4 +++- include/uapi/linux/fanotify.h | 7 +++++++ 5 files changed, 55 insertions(+), 7 deletions(-)
fs/notify/fanotify/fanotify.c +13 -4
@@ -224,7 +224,7 @@ static int fanotify_get_response(struct fsnotify_group *group,struct fanotify_perm_event *event, struct fsnotify_iter_info *iter_info) { - int ret; + int ret, errno;pr_debug ("%s: group=%p event=%p\n" , __func__, group, event);
@@ -263,14 +263,23 @@ static int fanotify_get_response(struct fsnotify_group *group, ret =0 ;break ;case FAN_DENY : +/* Check custom errno from pre-content events */ + errno =fanotify_get_response_errno (event->response); +if (errno) { + ret = -errno; +break ; + } + fallthrough;default : ret = -EPERM ; }/* Check if the response should be audited */ -if (event->response &FAN_AUDIT ) -audit_fanotify (event->response & ~FAN_AUDIT , - &event->audit_rule); +if (event->response &FAN_AUDIT ) { +u32 response = event->response & + (FANOTIFY_RESPONSE_ACCESS |FANOTIFY_RESPONSE_FLAGS ); +audit_fanotify (response & ~FAN_AUDIT , &event->audit_rule); + }pr_debug ("%s: group=%p event=%p about to return ret=%d\n" , __func__, group, event, ret);
fs/notify/fanotify/fanotify.h +5 -0
@@ -528,3 +528,8 @@ static inline unsigned int fanotify_mark_user_flags(struct fsnotify_mark *mark)return mflags; } + +static inline u32 fanotify_get_response_errno (int res) +{ +return (res >>FAN_ERRNO_SHIFT ) &FAN_ERRNO_MASK ; +}
fs/notify/fanotify/fanotify_user.c +27 -2
@@ -328,11 +328,12 @@ static int process_access_response(struct fsnotify_group *group,struct fanotify_perm_event *event;int fd = response_struct->fd;u32 response = response_struct->response; +int errno =fanotify_get_response_errno (response);int ret = info_len;struct fanotify_response_info_audit_rule friar; -pr_debug ("%s: group=%p fd=%d response=%u buf=%p size=%zu\n" , __func__, - group, fd, response, info, info_len); +pr_debug ("%s: group=%p fd=%d response=%x errno=%d buf=%p size=%zu\n" , + __func__, group, fd, response, errno, info, info_len);/* * make sure the response is valid, if invalid we do nothing and either * userspace can send a valid response or we will clean it up after the
@@ -343,7 +344,31 @@ static int process_access_response(struct fsnotify_group *group,switch (response &FANOTIFY_RESPONSE_ACCESS ) {case FAN_ALLOW : +if (errno) +return -EINVAL ; +break ;case FAN_DENY : +/* Custom errno is supported only for pre-content groups */ +if (errno && group->priority !=FSNOTIFY_PRIO_PRE_CONTENT ) +return -EINVAL ; + +/* +* Limit errno to values expected on open(2)/read(2)/write(2) +* of regular files. +*/ +switch (errno) { +case 0 : +case EIO : +case EPERM : +case EBUSY : +case ETXTBSY : +case EAGAIN : +case ENOSPC : +case EDQUOT : +break ; +default : +return -EINVAL ; + }break ;default :return -EINVAL ;
include/linux/fanotify.h +3 -1
@@ -132,7 +132,9 @@/* These masks check for invalid bits in permission responses. */ #define FANOTIFY_RESPONSE_ACCESS (FAN_ALLOW | FAN_DENY)#define FANOTIFY_RESPONSE_FLAGS (FAN_AUDIT | FAN_INFO) -#define FANOTIFY_RESPONSE_VALID_MASK (FANOTIFY_RESPONSE_ACCESS | FANOTIFY_RESPONSE_FLAGS) +#define FANOTIFY_RESPONSE_VALID_MASK \ + (FANOTIFY_RESPONSE_ACCESS | FANOTIFY_RESPONSE_FLAGS | \ + (FAN_ERRNO_MASK << FAN_ERRNO_SHIFT))/* Do not use these old uapi constants internally */ #undef FAN_ALL_CLASS_BITS
include/uapi/linux/fanotify.h +7 -0
@@ -235,6 +235,13 @@ struct fanotify_response_info_audit_rule {/* Legit userspace responses to a _PERM event */ #define FAN_ALLOW 0x01#define FAN_DENY 0x02 +/* errno other than EPERM can specified in upper byte of deny response */ +#define FAN_ERRNO_BITS 8 +#define FAN_ERRNO_SHIFT (32 - FAN_ERRNO_BITS) +#define FAN_ERRNO_MASK ((1 << FAN_ERRNO_BITS) - 1) +#define FAN_DENY_ERRNO (err) \ + (FAN_DENY | ((((__u32)(err)) & FAN_ERRNO_MASK) << FAN_ERRNO_SHIFT)) +#define FAN_AUDIT 0x10/* Bitmask to create audit record for result */ #define FAN_INFO 0x20/* Bitmask to indicate additional information */
Signature · 1 line
2.35.3
Jan Kara
maintainerPATCH v9 13/19 fanotify: disable readahead if we have pre-content watches
In reply to Jan KaraFrom: Josef Bacik <[email protected]>
With page faults we can trigger readahead on the file, and then subsequent faults can find these pages and insert them into the file without emitting an fanotify event. To avoid this case, disable readahead if we have pre-content watches on the file. This way we are guaranteed to get an event for every range we attempt to access on a pre-content watched file.
--- mm/filemap.c | 12 ++++++++++++ mm/readahead.c | 14 ++++++++++++++ 2 files changed, 26 insertions(+)
mm/filemap.c +12 -0
@@ -3149,6 +3149,14 @@ static struct file *do_sync_mmap_readahead(struct vm_fault *vmf)unsigned long vm_flags = vmf->vma->vm_flags;unsigned int mmap_miss; +/* +* If we have pre-content watches we need to disable readahead to make +* sure that we don't populate our mapping with 0 filled pages that we +* never emitted an event for. +*/ +if (unlikely (FMODE_FSNOTIFY_HSM (file->f_mode))) +return fpin; +#ifdef CONFIG_TRANSPARENT_HUGEPAGE /* Use the readahead code, even if readahead is disabled */ if ((vm_flags &VM_HUGEPAGE ) &&HPAGE_PMD_ORDER <=MAX_PAGECACHE_ORDER ) {
@@ -3217,6 +3225,10 @@ static struct file *do_async_mmap_readahead(struct vm_fault *vmf,struct file *fpin =NULL ;unsigned int mmap_miss; +/* See comment in do_sync_mmap_readahead. */ +if (unlikely (FMODE_FSNOTIFY_HSM (file->f_mode))) +return fpin; +/* If we don't want any read-ahead, don't bother */ if (vmf->vma->vm_flags &VM_RAND_READ || !ra->ra_pages)return fpin;
mm/readahead.c +14 -0
@@ -128,6 +128,7 @@#include <linux/blk-cgroup.h> #include <linux/fadvise.h> #include <linux/sched/mm.h> +#include <linux/fsnotify.h> #include "internal.h"
@@ -544,6 +545,15 @@ void page_cache_sync_ra(struct readahead_control *ractl,unsigned long max_pages, contig_count;pgoff_t prev_index, miss; +/* +* If we have pre-content watches we need to disable readahead to make +* sure that we don't find 0 filled pages in cache that we never emitted +* events for. Filesystems supporting HSM must make sure to not call +* this function with ractl->file unset for files handled by HSM. +*/ +if (ractl->file &&unlikely (FMODE_FSNOTIFY_HSM (ractl->file->f_mode))) +return ; +/* * Even if readahead is disabled, issue this request as readahead * as we'll need it to satisfy the requested range. The forced
@@ -622,6 +632,10 @@ void page_cache_async_ra(struct readahead_control *ractl,if (!ra->ra_pages)return ; +/* See the comment in page_cache_sync_ra. */ +if (ractl->file &&unlikely (FMODE_FSNOTIFY_HSM (ractl->file->f_mode))) +return ; +/* * Same bit is used for PG_readahead and PG_reclaim. */
Signature · 1 line
2.35.3
Jan Kara
maintainerPATCH v9 14/19 mm: don't allow huge faults for files with pre content watches
In reply to Jan KaraFrom: Josef Bacik <[email protected]>
There's nothing stopping us from supporting this, we could simply pass the order into the helper and emit the proper length. However currently there's no tests to validate this works properly, so disable it until there's a desire to support this along with the appropriate tests.
--- mm/memory.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
mm/memory.c +22 -0
@@ -78,6 +78,7 @@#include <linux/ptrace.h> #include <linux/vmalloc.h> #include <linux/sched/sysctl.h> +#include <linux/fsnotify.h> #include <trace/events/kmem.h>
@@ -5622,8 +5623,17 @@ static vm_fault_t do_numa_page(struct vm_fault *vmf)static inline vm_fault_t create_huge_pmd (struct vm_fault *vmf) {struct vm_area_struct *vma = vmf->vma; +struct file *file = vma->vm_file;if (vma_is_anonymous (vma))return do_huge_pmd_anonymous_page (vmf); +/* +* Currently we just emit PAGE_SIZE for our fault events, so don't allow +* a huge fault if we have a pre content watch on this file. This would +* be trivial to support, but there would need to be tests to ensure +* this works properly and those don't exist currently. +*/ +if (unlikely (FMODE_FSNOTIFY_HSM (file->f_mode))) +return VM_FAULT_FALLBACK ;if (vma->vm_ops->huge_fault)return vma->vm_ops->huge_fault (vmf,PMD_ORDER );return VM_FAULT_FALLBACK ;
@@ -5633,6 +5643,7 @@ static inline vm_fault_t create_huge_pmd(struct vm_fault *vmf)static inline vm_fault_t wp_huge_pmd (struct vm_fault *vmf) {struct vm_area_struct *vma = vmf->vma; +struct file *file = vma->vm_file;const bool unshare = vmf->flags &FAULT_FLAG_UNSHARE ;vm_fault_t ret;
@@ -5647,6 +5658,9 @@ static inline vm_fault_t wp_huge_pmd(struct vm_fault *vmf) }if (vma->vm_flags & (VM_SHARED |VM_MAYSHARE )) { +/* See comment in create_huge_pmd. */ +if (unlikely (FMODE_FSNOTIFY_HSM (file->f_mode))) + goto split;if (vma->vm_ops->huge_fault) { ret = vma->vm_ops->huge_fault (vmf,PMD_ORDER );if (!(ret &VM_FAULT_FALLBACK ))
@@ -5666,9 +5680,13 @@ static vm_fault_t create_huge_pud(struct vm_fault *vmf)#if defined(CONFIG_TRANSPARENT_HUGEPAGE ) && \ defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD )struct vm_area_struct *vma = vmf->vma; +struct file *file = vma->vm_file;/* No support for anonymous transparent PUD pages yet */ if (vma_is_anonymous (vma))return VM_FAULT_FALLBACK ; +/* See comment in create_huge_pmd. */ +if (unlikely (FMODE_FSNOTIFY_HSM (file->f_mode))) +return VM_FAULT_FALLBACK ;if (vma->vm_ops->huge_fault)return vma->vm_ops->huge_fault (vmf,PUD_ORDER );#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
@@ -5680,12 +5698,16 @@ static vm_fault_t wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud)#if defined(CONFIG_TRANSPARENT_HUGEPAGE ) && \ defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD )struct vm_area_struct *vma = vmf->vma; +struct file *file = vma->vm_file;vm_fault_t ret;/* No support for anonymous transparent PUD pages yet */ if (vma_is_anonymous (vma)) goto split;if (vma->vm_flags & (VM_SHARED |VM_MAYSHARE )) { +/* See comment in create_huge_pmd. */ +if (unlikely (FMODE_FSNOTIFY_HSM (file->f_mode))) + goto split;if (vma->vm_ops->huge_fault) { ret = vma->vm_ops->huge_fault (vmf,PUD_ORDER );if (!(ret &VM_FAULT_FALLBACK ))
Signature · 1 line
2.35.3
Jan Kara
maintainerPATCH v9 15/19 fsnotify: generate pre-content permission event on page fault
In reply to Jan KaraFrom: Josef Bacik <[email protected]>
FS_PRE_ACCESS will be generated on page fault depending on the faulting method. This pre-content event is meant to be used by hierarchical storage managers that want to fill in the file content on first read access.
Export a simple helper that file systems that have their own ->fault() will use, and have a more complicated helper to be do fancy things in filemap_fault.
--- include/linux/mm.h | 1 + mm/filemap.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++ mm/nommu.c | 7 +++++ 3 files changed, 86 insertions(+)
include/linux/mm.h +1 -0
@@ -3405,6 +3405,7 @@ extern vm_fault_t filemap_fault(struct vm_fault *vmf);extern vm_fault_t filemap_map_pages (struct vm_fault *vmf,pgoff_t start_pgoff,pgoff_t end_pgoff);extern vm_fault_t filemap_page_mkwrite (struct vm_fault *vmf); +extern vm_fault_t filemap_fsnotify_fault (struct vm_fault *vmf);extern unsigned long stack_guard_gap;/* Generic expand stack which grows the stack according to GROWS{UP,DOWN} */
mm/filemap.c +78 -0
96 lines not on this page · raw message
mm/nommu.c +7 -0
@@ -1614,6 +1614,13 @@ int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, }EXPORT_SYMBOL (remap_vmalloc_range); +vm_fault_t filemap_fsnotify_fault (struct vm_fault *vmf) +{ +BUG (); +return 0 ; +} +EXPORT_SYMBOL_GPL (filemap_fsnotify_fault); +vm_fault_t filemap_fault (struct vm_fault *vmf) {BUG ();
Signature · 1 line
2.35.3
Jan Kara
maintainerPATCH v9 16/19 xfs: add pre-content fsnotify hook for DAX faults
In reply to Jan KaraFrom: Josef Bacik <[email protected]>
xfs has it's own handling for DAX faults, so we need to add the pre-content fsnotify hook for this case. Other faults go through filemap_fault so they're handled properly there.
--- fs/xfs/xfs_file.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
fs/xfs/xfs_file.c +14 -0
@@ -1395,6 +1395,9 @@ xfs_dax_read_fault(struct xfs_inode *ip =XFS_I (file_inode (vmf->vma->vm_file));vm_fault_t ret; + ret =filemap_fsnotify_fault (vmf); +if (unlikely (ret)) +return ret;xfs_ilock (ip,XFS_MMAPLOCK_SHARED ); ret =xfs_dax_fault_locked (vmf, order, false);xfs_iunlock (ip,XFS_MMAPLOCK_SHARED );
@@ -1412,6 +1415,17 @@ xfs_write_fault(unsigned int lock_mode =XFS_MMAPLOCK_SHARED ;vm_fault_t ret; +/* +* Usually we get here from ->page_mkwrite callback but in case of DAX +* we will get here also for ordinary write fault. Handle HSM +* notifications for that case. +*/ +if (IS_DAX (inode)) { + ret =filemap_fsnotify_fault (vmf); +if (unlikely (ret)) +return ret; + } +sb_start_pagefault (inode->i_sb);file_update_time (vmf->vma->vm_file);
Signature · 1 line
2.35.3
Jan Kara
maintainerPATCH v9 17/19 btrfs: disable defrag on pre-content watched files
In reply to Jan KaraFrom: Josef Bacik <[email protected]>
We queue up inodes to be defrag'ed asynchronously, which means we do not have their original file for readahead. This means that the code to skip readahead on pre-content watched files will not run, and we could potentially read in empty pages.
Handle this corner case by disabling defrag on files that are currently being watched for pre-content events.
--- fs/btrfs/ioctl.c | 9 +++++++++ 1 file changed, 9 insertions(+)
fs/btrfs/ioctl.c +9 -0
@@ -2638,6 +2638,15 @@ static int btrfs_ioctl_defrag(struct file *file, void __user *argp) goto out; } +/* +* Don't allow defrag on pre-content watched files, as it could +* populate the page cache with 0's via readahead. +*/ +if (unlikely (FMODE_FSNOTIFY_HSM (file->f_mode))) { + ret = -EINVAL ; + goto out; + } +if (argp) {if (copy_from_user (&range, argp,sizeof (range))) { ret = -EFAULT ;
Signature · 1 line
2.35.3
Jan Kara
maintainerPATCH v9 18/19 ext4: add pre-content fsnotify hook for DAX faults
In reply to Jan Karaext4 has its own handling for DAX faults. Add the pre-content fsnotify hook for this case.
--- fs/ext4/file.c | 3 +++ 1 file changed, 3 insertions(+)
fs/ext4/file.c +3 -0
@@ -737,6 +737,9 @@ static vm_fault_t ext4_dax_huge_fault(struct vm_fault *vmf, unsigned int order)return VM_FAULT_SIGBUS ; } }else { + result =filemap_fsnotify_fault (vmf); +if (unlikely (result)) +return result;filemap_invalidate_lock_shared (mapping); } result =dax_iomap_fault (vmf, order, &pfn, &error, &ext4_iomap_ops);
Signature · 1 line
2.35.3
Jan Kara
maintainerPATCH v9 19/19 fs: enable pre-content events on supported file systems
In reply to Jan KaraFrom: Josef Bacik <[email protected]>
Now that all the code has been added for pre-content events, and the various file systems that need the page fault hooks for fsnotify have been updated, add SB_I_ALLOW_HSM to the supported file systems.
--- fs/btrfs/super.c | 2 +- fs/ext4/super.c | 3 +++ fs/xfs/xfs_super.c | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-)
fs/btrfs/super.c +1 -1
@@ -954,7 +954,7 @@ static int btrfs_fill_super(struct super_block *sb,#endif sb->s_xattr = btrfs_xattr_handlers; sb->s_time_gran =1 ; - sb->s_iflags |=SB_I_CGROUPWB ; + sb->s_iflags |=SB_I_CGROUPWB |SB_I_ALLOW_HSM ; err =super_setup_bdi (sb);if (err) {
fs/ext4/super.c +3 -0
@@ -5266,6 +5266,9 @@ static int __ext4_fill_super(struct fs_context *fc, struct super_block *sb)/* i_version is always enabled now */ sb->s_flags |=SB_I_VERSION ; +/* HSM events are allowed by default. */ + sb->s_iflags |=SB_I_ALLOW_HSM ; + err =ext4_check_feature_compatibility (sb, es, silent);if (err) goto failed_mount;
fs/xfs/xfs_super.c +1 -1
@@ -1713,7 +1713,7 @@ xfs_fs_fill_super( sb->s_time_max =XFS_LEGACY_TIME_MAX ; }trace_xfs_inode_timestamp_range (mp, sb->s_time_min, sb->s_time_max); - sb->s_iflags |=SB_I_CGROUPWB ; + sb->s_iflags |=SB_I_CGROUPWB |SB_I_ALLOW_HSM ;set_posix_acl_flag (sb);
Signature · 1 line
2.35.3
Amir Goldstein
reviewerQuoted 25 lines · Jan Kara
I'm posting here the series I'm currently carrying in my tree [1]. The changes from v8 Josef posted are not huge but big enough that I think it's worth a repost. Unless somebody speaks up, the plan is to merge into fsnotify branch after the merge window closes.
[1] https://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs.git/log/?h=fsnotify_hsm
v8:
https://lore.kernel.org/all/[email protected]
v7:
https://lore.kernel.org/linux-fsdevel/[email protected]/
v6:
https://lore.kernel.org/linux-fsdevel/[email protected]/
v5:
https://lore.kernel.org/linux-fsdevel/[email protected]/
v4:
https://lore.kernel.org/linux-fsdevel/[email protected]/
v3:
https://lore.kernel.org/linux-fsdevel/[email protected]/
v2:
https://lore.kernel.org/linux-fsdevel/[email protected]/
v1:
https://lore.kernel.org/linux-fsdevel/[email protected]/
Quoted 5 lines
v8->v9:
- fix DAX fault handling for ext4 & xfs
- reworkFMODE_constants a bit to keepFMODE_NONOTIFYa single bit
- movefile_set_fsnotify_mode()out of line as it's quite big
- foldfsnotify_file_object_watched()into the single caller
I tested this with my new test cases on LTP branch fan_hsm. One test broke, I posted a suggested fix for patch 3/19.
Quoted 4 lines
- use explicit
f_modechecks instead offsnotify_file_has_pre_content_watches()
- fix compilation breakage withCONFIG_NOMMU
- fixed up some changelogs
Other than patch 3, all looks good to me.
Thanks,
Amir.