Partition the fanotify watchers by type
Amir Goldstein
reviewerv3 cover Partition the fanotify watchers by type
Jan,
As discussed on the "namespace monitoring" patch set [1], the proposed namespace events do not need to be mixed on the same group with legacy fanotify filesystem events.
The mount events are already not mixable with filesystem events, but we want to be able to mix them with the new namespace events.
Therefore, introduce the concept of "watcher type" where all the legacy events belong to the filesystem watcher type and the new mount events belong to a new namespace watcher type.
The new watcher type is free to reuse most of the 32bit mask space that we have internally in fsnotify for event masks.
The prep patch set only does the partitioning by watcher type. Followup namepsace events work in available for review in my tree [2].
If you are fine with this plan and have time to review the patches, perhaps it is not too late to stage this prep series for next cycle?
Otherwise, we could aim for merging this together with the namespace events for the following cycle.
Thanks,
Amir.
[1] https://lore.kernel.org/linux-fsdevel/[email protected]/
[2] https://github.com/amir73il/linux/commits/fan_mark_userns
Amir Goldstein (14):
fsnotify: rename fsnotify group flag macros
fsnotify: introduce fsnotify group types
fsnotify: separate the events bitmask macros by group type
fanotify: test event->type instead of event mask when possible
fsnotify: do not report mount events with fsnotify()
fanotify: gate filesystem event classification by group type
fanotify: define event mask constants as 64bit
fanotify: define new FAN_NS_MNT_* constants
fanotify: gate event mask in fanotify_mark() by group type
fanotify: introduce FAN_REPORT_MNTID
fanotify: introduce FAN_EVENT_MATCH() macro selftests/filesystems: create fanotify test dir selftests/fanotify: update the uapi tools header copy selftests/fanotify: test the FAN_REPORT_MNTID mode
fs/notify/fanotify/fanotify.c | 186 +++++++++-----
fs/notify/fanotify/fanotify.h | 38 ++-
fs/notify/fanotify/fanotify_user.c | 230 ++++++++++++------
fs/notify/fsnotify.c | 106 +++++---
fs/notify/group.c | 15 +-
fs/notify/inotify/inotify_user.c | 2 +-
fs/notify/mark.c | 2 +-
include/linux/fanotify.h | 45 +++-
include/linux/fsnotify.h | 6 +-
include/linux/fsnotify_backend.h | 83 +++++--
include/trace/misc/fsnotify.h | 4 +-
include/uapi/linux/fanotify.h | 100 +++++---
kernel/audit_fsnotify.c | 2 +-
tools/include/uapi/linux/fanotify.h | 100 +++++---
tools/testing/selftests/Makefile | 2 +-
.../{mount-notify => fanotify}/.gitignore | 0
.../{mount-notify => fanotify}/Makefile | 0
.../mount-notify_test.c | 51 +++-
.../mount-notify_test_ns.c | 54 +++-
19 files changed, 707 insertions(+), 319 deletions(-)
rename tools/testing/selftests/filesystems/{mount-notify => fanotify}/.gitignore (100%)
rename tools/testing/selftests/filesystems/{mount-notify => fanotify}/Makefile (100%)
rename tools/testing/selftests/filesystems/{mount-notify => fanotify}/mount-notify_test.c (90%)
rename tools/testing/selftests/filesystems/{mount-notify => fanotify}/mount-notify_test_ns.c (90%)Signature · 1 line
2.55.0
Amir Goldstein
reviewerPATCH v3 1/14 fsnotify: rename fsnotify group flag macros
Use more explicit FSNOTIFY_GROUP_FLAG_ prefix.
--- fs/notify/fanotify/fanotify_user.c | 2 +- fs/notify/group.c | 4 ++-- fs/notify/inotify/inotify_user.c | 2 +- fs/notify/mark.c | 2 +- include/linux/fsnotify_backend.h | 4 ++-- kernel/audit_fsnotify.c | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-)
fs/notify/fanotify/fanotify_user.c +1 -1
@@ -1670,7 +1670,7 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) f_flags |=O_NONBLOCK ;CLASS (fsnotify_group, group)(&fanotify_fsnotify_ops, -FSNOTIFY_GROUP_USER ); +FSNOTIFY_GROUP_FLAG_USER );/* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */ if (IS_ERR (group))return PTR_ERR (group);
fs/notify/group.c +2 -2
@@ -145,8 +145,8 @@ static struct fsnotify_group *__fsnotify_alloc_group(struct fsnotify_group *fsnotify_alloc_group (const struct fsnotify_ops *ops,int flags) { -gfp_t gfp = (flags &FSNOTIFY_GROUP_USER ) ?GFP_KERNEL_ACCOUNT : -GFP_KERNEL ; +gfp_t gfp = (flags &FSNOTIFY_GROUP_FLAG_USER ) ? +GFP_KERNEL_ACCOUNT :GFP_KERNEL ;return __fsnotify_alloc_group (ops, flags, gfp); }
fs/notify/inotify/inotify_user.c +1 -1
@@ -644,7 +644,7 @@ static struct fsnotify_group *inotify_new_group(unsigned int max_events)struct inotify_event_info *oevent; group =fsnotify_alloc_group (&inotify_fsnotify_ops, -FSNOTIFY_GROUP_USER ); +FSNOTIFY_GROUP_FLAG_USER );if (IS_ERR (group))return group;
fs/notify/mark.c +1 -1
@@ -937,7 +937,7 @@ static int fsnotify_add_mark_list(struct fsnotify_mark *mark, void *obj,if ((lmark->group == mark->group) && (lmark->flags &FSNOTIFY_MARK_FLAG_ATTACHED ) && - !(mark->group->flags &FSNOTIFY_GROUP_DUPS )) { + !(mark->group->flags &FSNOTIFY_GROUP_FLAG_DUPS )) { err = -EEXIST ; goto out_err; }
include/linux/fsnotify_backend.h +2 -2
@@ -232,8 +232,8 @@ struct fsnotify_group {enum fsnotify_group_prio priority;/* priority for sending events */ bool shutdown;/* group is being shut down, don't queue more events */ -#define FSNOTIFY_GROUP_USER 0x01/* user allocated group */ -#define FSNOTIFY_GROUP_DUPS 0x02/* allow multiple marks per object */ +#define FSNOTIFY_GROUP_FLAG_USER 0x01/* user allocated group */ +#define FSNOTIFY_GROUP_FLAG_DUPS 0x02/* allow multiple marks per object */ int flags;unsigned int owner_flags;/* stored flags of mark_mutex owner */
kernel/audit_fsnotify.c +1 -1
@@ -194,7 +194,7 @@ static const struct fsnotify_ops audit_mark_fsnotify_ops = {static int __init audit_fsnotify_init (void ) { audit_fsnotify_group =fsnotify_alloc_group (&audit_mark_fsnotify_ops, -FSNOTIFY_GROUP_DUPS ); +FSNOTIFY_GROUP_FLAG_DUPS );if (IS_ERR (audit_fsnotify_group)) { audit_fsnotify_group =NULL ;audit_panic ("cannot create audit fsnotify group" );
Signature · 1 line
2.55.0
Amir Goldstein
reviewerPATCH v3 2/14 fsnotify: introduce fsnotify group types
In reply to Amir GoldsteinCurrently an fanotify group can subscribe to events FAN_MNT_ATTACH/DETACH only on mark type FAN_MARK_MNTNS and IFF the group was initialized with FAN_REPORT_MNT.
Hence, mount events can not be mixed on the same group with filesystem events and mntns marks cannot be mixed with filesystem type marks.
Define a new property for an fsnotify group called fsnotify_group_type which describes the category of events and mark types and use a distinct category FSNOTIFY_GROUP_TYPE_NAMESPACE for the mount event groups.
Create helpers fsnotify_is_{filesystem,namespace}_watcher() and the macro FANOTIFY_NAMESPACE_INIT_FLAGS and use them to generalize some code that was checking the FAN_REPORT_MNT flag. FAN_REPORT_MNT is both a namespace group type selector and an information mode that controls the event metadata.
--- fs/notify/fanotify/fanotify.c | 4 +++- fs/notify/fanotify/fanotify_user.c | 37 ++++++++++++++++++++++-------- fs/notify/group.c | 11 +++++---- include/linux/fanotify.h | 10 ++++++-- include/linux/fsnotify_backend.h | 26 +++++++++++++++++++++ 5 files changed, 72 insertions(+), 16 deletions(-)
fs/notify/fanotify/fanotify.c +3 -1
@@ -312,9 +312,11 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group,pr_debug ("%s: report_mask=%x mask=%x data=%p data_type=%d\n" , __func__, iter_info->report_mask, event_mask, data, data_type); -if (FAN_GROUP_FLAG (group,FAN_REPORT_MNT )) { +if (fsnotify_is_namespace_watcher (group)) {if (data_type !=FSNOTIFY_EVENT_MNT )return 0 ; + }else if (WARN_ON_ONCE (!fsnotify_is_filesystem_watcher (group))) { +return 0 ; }else if (!fid_mode) {/* Do we have path to open a file descriptor? */ if (!path)
fs/notify/fanotify/fanotify_user.c +28 -9
@@ -1582,8 +1582,10 @@ static struct hlist_head *fanotify_alloc_merge_hash(void)DEFINE_CLASS (fsnotify_group, struct fsnotify_group *, if (!IS_ERR_OR_NULL(_T)) fsnotify_destroy_group(_T), -fsnotify_alloc_group (ops, flags), - const struct fsnotify_ops *ops, int flags) +__fsnotify_alloc_group (ops, type, flags, gfp), + const struct fsnotify_ops *ops, + enum fsnotify_group_type type, + int flags, gfp_t gfp) /* fanotify syscalls */ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
@@ -1593,6 +1595,10 @@ 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 ; +/* A group is either for watching filesystems or namespaces */ +enum fsnotify_group_type type = + (flags &FANOTIFY_NAMESPACE_INIT_FLAGS ) ? +FSNOTIFY_GROUP_TYPE_NAMESPACE :FSNOTIFY_GROUP_TYPE_FILESYSTEM ;pr_debug ("%s: flags=%x event_f_flags=%x\n" , __func__, flags, event_f_flags);
@@ -1604,7 +1610,7 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)* queue/marks. */ if (((flags &FANOTIFY_ADMIN_INIT_FLAGS ) || - !(flags & (FANOTIFY_FID_BITS |FAN_REPORT_MNT ))) && + !(flags & (FANOTIFY_FID_BITS |FANOTIFY_NAMESPACE_INIT_FLAGS ))) && !capable (CAP_SYS_ADMIN ))return -EPERM ;
@@ -1624,8 +1630,11 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)#endif return -EINVAL ; -/* Don't allow mixing mnt events with inode events for now */ -if (flags &FAN_REPORT_MNT ) { +/* +* Namespace watchers do not support priority classes and do not +* support reporting file info event extensions. +*/ +if (type ==FSNOTIFY_GROUP_TYPE_NAMESPACE ) {if (class !=FAN_CLASS_NOTIF )return -EINVAL ;if (flags & (FANOTIFY_FID_BITS |FAN_REPORT_FD_ERROR ))
@@ -1669,8 +1678,9 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)if (flags &FAN_NONBLOCK ) f_flags |=O_NONBLOCK ; -CLASS (fsnotify_group, group)(&fanotify_fsnotify_ops, -FSNOTIFY_GROUP_FLAG_USER ); +CLASS (fsnotify_group, group)(&fanotify_fsnotify_ops, type, +FSNOTIFY_GROUP_FLAG_USER , +GFP_KERNEL_ACCOUNT );/* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */ if (IS_ERR (group))return PTR_ERR (group);
@@ -1873,6 +1883,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,unsigned int mark_type = flags &FANOTIFY_MARK_TYPE_BITS ;unsigned int mark_cmd = flags &FANOTIFY_MARK_CMD_BITS ;unsigned int ignore = flags &FANOTIFY_MARK_IGNORE_BITS ; +enum fsnotify_group_type mark_group_type;unsigned int obj_type, fid_mode;void *obj =NULL ;u32 umask =0 ;
@@ -1891,15 +1902,19 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,switch (mark_type) {case FAN_MARK_INODE : obj_type =FSNOTIFY_OBJ_TYPE_INODE ; + mark_group_type =FSNOTIFY_GROUP_TYPE_FILESYSTEM ;break ;case FAN_MARK_MOUNT : obj_type =FSNOTIFY_OBJ_TYPE_VFSMOUNT ; + mark_group_type =FSNOTIFY_GROUP_TYPE_FILESYSTEM ;break ;case FAN_MARK_FILESYSTEM : obj_type =FSNOTIFY_OBJ_TYPE_SB ; + mark_group_type =FSNOTIFY_GROUP_TYPE_FILESYSTEM ;break ;case FAN_MARK_MNTNS : obj_type =FSNOTIFY_OBJ_TYPE_MNTNS ; + mark_group_type =FSNOTIFY_GROUP_TYPE_NAMESPACE ;break ;default :return -EINVAL ;
@@ -1948,6 +1963,9 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,return -EINVAL ; group =fd_file (f)->private_data; +if (group->type != mark_group_type) +return -EINVAL ; +/* Only report mount events on mnt namespace */ if (FAN_GROUP_FLAG (group,FAN_REPORT_MNT )) {if (mask & ~FANOTIFY_MOUNT_EVENTS )
@@ -1993,14 +2011,15 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,return -EINVAL ;/* -* Events that do not carry enough information to report +* Filesystem events that do not carry enough information to report * event->fd require a group that supports reporting fid. Those * events are not supported on a mount mark, because they do not * carry enough information (i.e. path) to be filtered by mount * point. */ fid_mode =FAN_GROUP_FLAG (group,FANOTIFY_FID_BITS ); -if (mask & ~(FANOTIFY_FD_EVENTS |FANOTIFY_MOUNT_EVENTS |FANOTIFY_EVENT_FLAGS ) && +if (fsnotify_is_filesystem_watcher (group) && + mask & ~(FANOTIFY_FD_EVENTS |FANOTIFY_EVENT_FLAGS ) && (!fid_mode || mark_type ==FAN_MARK_MOUNT ))return -EINVAL ;
fs/notify/group.c +7 -4
@@ -111,9 +111,9 @@ void fsnotify_put_group(struct fsnotify_group *group) }EXPORT_SYMBOL_GPL (fsnotify_put_group); -static struct fsnotify_group *__fsnotify_alloc_group ( -const struct fsnotify_ops *ops, -int flags,gfp_t gfp) +struct fsnotify_group *__fsnotify_alloc_group (const struct fsnotify_ops *ops, +enum fsnotify_group_type type, +int flags,gfp_t gfp) {struct fsnotify_group *group;
@@ -135,9 +135,11 @@ static struct fsnotify_group *__fsnotify_alloc_group( group->ops = ops; group->flags = flags; + group->type = type;return group; } +EXPORT_SYMBOL_GPL (__fsnotify_alloc_group);/* * Create a new fsnotify_group and hold a reference for the group returned.
@@ -148,7 +150,8 @@ struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops,gfp_t gfp = (flags &FSNOTIFY_GROUP_FLAG_USER ) ?GFP_KERNEL_ACCOUNT :GFP_KERNEL ; -return __fsnotify_alloc_group (ops, flags, gfp); +return __fsnotify_alloc_group (ops,FSNOTIFY_GROUP_TYPE_FILESYSTEM , + flags, gfp); }EXPORT_SYMBOL_GPL (fsnotify_alloc_group);
include/linux/fanotify.h +8 -2
@@ -25,7 +25,12 @@#define FANOTIFY_FID_BITS (FAN_REPORT_DFID_NAME_TARGET) -#define FANOTIFY_INFO_MODES (FANOTIFY_FID_BITS | FAN_REPORT_PIDFD | FAN_REPORT_MNT) +/* fanotify_init() flags related to information reported with events */ +#define FANOTIFY_INFO_MODES (FANOTIFY_FID_BITS | FAN_REPORT_PIDFD | \ + FAN_REPORT_MNT) + +/* fanotify_init() flags that create a namespace watcher */ +#define FANOTIFY_NAMESPACE_INIT_FLAGS (FAN_REPORT_MNT)/* * fanotify_init() flags that require CAP_SYS_ADMIN.
@@ -47,7 +52,8 @@* so one of the flags for reporting file handles is required. */ #define FANOTIFY_USER_INIT_FLAGS (FAN_CLASS_NOTIF | \ - FANOTIFY_FID_BITS | FAN_REPORT_MNT | \ + FANOTIFY_FID_BITS | \ + FANOTIFY_NAMESPACE_INIT_FLAGS | \ FAN_CLOEXEC | FAN_NONBLOCK)#define FANOTIFY_INIT_FLAGS (FANOTIFY_ADMIN_INIT_FLAGS | \
include/linux/fsnotify_backend.h +26 -0
@@ -204,6 +204,15 @@ enum fsnotify_group_prio { __FSNOTIFY_PRIO_NUM }; +/* +* Category of kernel objects watched by this group. +* Every category has its own mark types and event types. +*/ +enum fsnotify_group_type { +FSNOTIFY_GROUP_TYPE_FILESYSTEM =0 , +FSNOTIFY_GROUP_TYPE_NAMESPACE , +}; +/* * A group is a "thing" that wants to receive notification about filesystem * events. The mask holds the subset of event types this group cares about.
@@ -230,6 +239,7 @@ struct fsnotify_group {unsigned int q_len;/* events on the queue */ unsigned int max_events;/* maximum events allowed on the list */ enum fsnotify_group_prio priority;/* priority for sending events */ +enum fsnotify_group_type type;/* type of watched objects */ bool shutdown;/* group is being shut down, don't queue more events */ #define FSNOTIFY_GROUP_FLAG_USER 0x01/* user allocated group */
@@ -280,6 +290,18 @@ struct fsnotify_group { }; }; +static inline bool fsnotify_is_filesystem_watcher ( +const struct fsnotify_group *group) +{ +return group->type ==FSNOTIFY_GROUP_TYPE_FILESYSTEM ; +} + +static inline bool fsnotify_is_namespace_watcher ( +const struct fsnotify_group *group) +{ +return group->type ==FSNOTIFY_GROUP_TYPE_NAMESPACE ; +} +/* * These helpers are used to prevent deadlock when reclaiming inodes with * evictable marks of the same group that is allocating a new mark.
@@ -727,6 +749,10 @@ static inline void fsnotify_update_flags(struct dentry *dentry)/* called from fsnotify listeners, such as fanotify or dnotify */ /* create a new group */ +extern struct fsnotify_group *__fsnotify_alloc_group ( +const struct fsnotify_ops *ops, +enum fsnotify_group_type type, +int flags,gfp_t gfp);extern struct fsnotify_group *fsnotify_alloc_group (const struct fsnotify_ops *ops,int flags);
Signature · 1 line
2.55.0
Amir Goldstein
reviewerPATCH v3 3/14 fsnotify: separate the events bitmask macros by group type
In reply to Amir GoldsteinFork the macros ALL_FSNOTIFY_EVENTS and FANOTIFY_EVENTS to *_EVENTS_ON_{FILESYSTEM,NAMESPACE} and use them to determine the valid mask for fanotify_mark() and the outgoing mask by the group type.
Use fanotify_group_outgoing_events() for both the group event mask and the mask copied to userspace. Rename fsnotify_object_watched() to fsnotify_filesystem_object_watched(), because its callers only deal with filesystem events.
--- fs/notify/fanotify/fanotify.c | 7 ++-- fs/notify/fanotify/fanotify.h | 14 ++++++++ fs/notify/fanotify/fanotify_user.c | 56 ++++++++++++++++++------------ fs/notify/fsnotify.c | 14 ++++---- include/linux/fanotify.h | 28 +++++++++------ include/linux/fsnotify_backend.h | 48 ++++++++++++++++--------- include/uapi/linux/fanotify.h | 24 +++++++++---- 7 files changed, 124 insertions(+), 67 deletions(-)
fs/notify/fanotify/fanotify.c +4 -3
@@ -300,9 +300,10 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group, const void *data, int data_type, struct inode *dir) { - __u32 marks_mask = 0, marks_ignore_mask = 0; -__u32 test_mask, user_mask =FANOTIFY_OUTGOING_EVENTS | -FANOTIFY_EVENT_FLAGS ; + u32 marks_mask = 0, marks_ignore_mask = 0; +u32 test_mask, user_mask = +fanotify_group_outgoing_events (group,ALL_FSNOTIFY_BITS ) | +FANOTIFY_EVENT_FLAGS ;const struct path *path =fsnotify_data_path (data, data_type);unsigned int fid_mode =FAN_GROUP_FLAG (group,FANOTIFY_FID_BITS );struct fsnotify_mark *mark;
fs/notify/fanotify/fanotify.h +14 -0
@@ -558,3 +558,17 @@ static inline u32 fanotify_get_response_errno(int res) {return (res >>FAN_ERRNO_SHIFT ) &FAN_ERRNO_MASK ; } + +/* Bits of @mask that this that this group can report to userspace */ +static inline u32 fanotify_group_outgoing_events (struct fsnotify_group *group, +u32 mask) +{ +switch (group->type) { +case FSNOTIFY_GROUP_TYPE_FILESYSTEM : +return mask &FANOTIFY_OUTGOING_FILESYSTEM_EVENTS ; +case FSNOTIFY_GROUP_TYPE_NAMESPACE : +return mask &FANOTIFY_OUTGOING_NAMESPACE_EVENTS ; +default : +return 0 ; + } +}
fs/notify/fanotify/fanotify_user.c +33 -23
88 lines not on this page · raw message
fs/notify/fsnotify.c +7 -7
@@ -146,13 +146,13 @@ static bool fsnotify_event_needs_parent(struct inode *inode, __u32 mnt_mask, }/* Are there any inode/mount/sb objects that watch for these events? */ -static inline __u32 fsnotify_object_watched (struct inode *inode,__u32 mnt_mask, -__u32 mask) +static inline u32 fsnotify_filesystem_object_watched (struct inode *inode, +u32 mnt_mask,u32 mask) {__u32 marks_mask =READ_ONCE (inode->i_fsnotify_mask) | mnt_mask |READ_ONCE (inode->i_sb->s_fsnotify_mask); -return mask & marks_mask &ALL_FSNOTIFY_EVENTS ; +return mask & marks_mask &FSNOTIFY_EVENTS_ON_FILESYSTEM ; }/* Report pre-content event with optional range info */
@@ -199,7 +199,7 @@ int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data,/* Optimize the likely case of nobody watching this path */ if (likely (!parent_watched && - !fsnotify_object_watched (inode, mnt_mask, mask))) + !fsnotify_filesystem_object_watched (inode, mnt_mask, mask)))return 0 ; parent =NULL ;
@@ -222,7 +222,7 @@ int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data,* events can provide an undesirable side-channel for information * exfiltration. */ - parent_interested = mask & p_mask &ALL_FSNOTIFY_EVENTS && + parent_interested = mask & p_mask &FSNOTIFY_EVENTS_ON_FILESYSTEM && !(data_type ==FSNOTIFY_EVENT_PATH &&d_is_special (dentry) && (mask & (FS_ACCESS |FS_MODIFY )));
@@ -269,7 +269,7 @@ static int fsnotify_handle_inode_event(struct fsnotify_group *group,return 0 ;/* Check interest of this mark in case event was sent with two marks */ -if (!(mask & inode_mark->mask &ALL_FSNOTIFY_EVENTS )) +if (!(mask & inode_mark->mask &FSNOTIFY_EVENTS_ON_FILESYSTEM ))return 0 ;return ops->handle_inode_event (inode_mark, mask, inode, dir, name, cookie);
@@ -643,7 +643,7 @@ int fsnotify_open_perm_and_set_mode(struct file *file)* watching for permission events on *this* file. */ mnt_mask =READ_ONCE (real_mount (file->f_path.mnt)->mnt_fsnotify_mask); - p_mask =fsnotify_object_watched (d_inode (dentry), mnt_mask, + p_mask =fsnotify_filesystem_object_watched (d_inode (dentry), mnt_mask,ALL_FSNOTIFY_PERM_EVENTS );if (dentry->d_flags &DCACHE_FSNOTIFY_PARENT_WATCHED ) { parent =dget_parent (dentry);
include/linux/fanotify.h +18 -10
@@ -115,27 +115,35 @@/* Events that can only be reported with data type FSNOTIFY_EVENT_ERROR */ #define FANOTIFY_ERROR_EVENTS (FAN_FS_ERROR) +/* Events that user can request to be notified on filesystem watchers */ +#define FANOTIFY_EVENTS_ON_FILESYSTEM (FANOTIFY_PATH_EVENTS | \ + FANOTIFY_PERM_EVENTS | \ + FANOTIFY_INODE_EVENTS | \ + FANOTIFY_ERROR_EVENTS) + +/* Mount tree monitoring events */ #define FANOTIFY_MOUNT_EVENTS (FAN_MNT_ATTACH | FAN_MNT_DETACH) -/* Events that user can request to be notified on */ -#define FANOTIFY_EVENTS (FANOTIFY_PATH_EVENTS | \ - FANOTIFY_INODE_EVENTS | \ - FANOTIFY_ERROR_EVENTS | \ - FANOTIFY_MOUNT_EVENTS) +/* Events that user can request to be notified on namespace watchers */ +#define FANOTIFY_EVENTS_ON_NAMESPACE (FANOTIFY_MOUNT_EVENTS)/* Extra flags that may be reported with event or control handling of events */ #define FANOTIFY_EVENT_FLAGS (FAN_EVENT_ON_CHILD | FAN_ONDIR) -/* Events that may be reported to user */ -#define FANOTIFY_OUTGOING_EVENTS (FANOTIFY_EVENTS | \ - FANOTIFY_PERM_EVENTS | \ - FAN_Q_OVERFLOW | FAN_ONDIR) +/* Events that may be reported to user on filesystem watchers */ +#define FANOTIFY_OUTGOING_FILESYSTEM_EVENTS (FANOTIFY_EVENTS_ON_FILESYSTEM | \ + FAN_Q_OVERFLOW | FAN_ONDIR) + +/* Events that may be reported to user on namespace watchers */ +#define FANOTIFY_OUTGOING_NAMESPACE_EVENTS (FANOTIFY_EVENTS_ON_NAMESPACE | \ + FAN_Q_OVERFLOW)/* Events and flags relevant only for directories */ #define FANOTIFY_DIRONLY_EVENT_BITS (FANOTIFY_DIRENT_EVENTS | \ FAN_EVENT_ON_CHILD | FAN_ONDIR) -#define ALL_FANOTIFY_EVENT_BITS (FANOTIFY_OUTGOING_EVENTS | \ +#define ALL_FANOTIFY_EVENT_BITS (FANOTIFY_OUTGOING_FILESYSTEM_EVENTS | \ + FANOTIFY_OUTGOING_NAMESPACE_EVENTS | \ FANOTIFY_EVENT_FLAGS)/* These masks check for invalid bits in permission responses. */
include/linux/fsnotify_backend.h +31 -17
75 lines not on this page · raw message
include/uapi/linux/fanotify.h +17 -7
@@ -4,7 +4,9 @@#include <linux/types.h> -/* the following events that user-space can register for */ +/* +* Events that user-space can request when watching filesystems +*/ #define FAN_ACCESS 0x00000001/* File was accessed */ #define FAN_MODIFY 0x00000002/* File was modified */ #define FAN_ATTRIB 0x00000004/* Metadata changed */
@@ -28,19 +30,27 @@/* #define FAN_DIR_MODIFY 0x00080000 */ /* Deprecated (reserved) */ #define FAN_PRE_ACCESS 0x00100000/* Pre-content access hook */ -#define FAN_MNT_ATTACH 0x01000000/* Mount was attached */ -#define FAN_MNT_DETACH 0x02000000/* Mount was detached */ - -#define FAN_EVENT_ON_CHILD 0x08000000/* Interested in child events */ #define FAN_RENAME 0x10000000/* File was renamed */ -#define FAN_ONDIR 0x40000000/* Event occurred against dir */ -/* helper events */ #define FAN_CLOSE (FAN_CLOSE_WRITE | FAN_CLOSE_NOWRITE)/* close */ #define FAN_MOVE (FAN_MOVED_FROM | FAN_MOVED_TO)/* moves */ +/* +* Filter flags for watching filesystems +*/ +#define FAN_EVENT_ON_CHILD 0x08000000/* Interested in child events */ +#define FAN_ONDIR 0x40000000/* Event occurred against dir */ + +/* +* Events that user-space can request when watching namespaces +* +* NOTE: These values may overload filesystem events, but not event flags +*/ +#define FAN_MNT_ATTACH 0x01000000/* Mount was attached */ +#define FAN_MNT_DETACH 0x02000000/* Mount was detached */ +/* flags used for fanotify_init() */ #define FAN_CLOEXEC 0x00000001#define FAN_NONBLOCK 0x00000002
Signature · 1 line
2.55.0
Amir Goldstein
reviewerPATCH v3 4/14 fanotify: test event->type instead of event mask when possible
In reply to Amir GoldsteinThe fanotify_event already has a type field, so tests like fanotify_is_mnt_event() should check the type and not the mask.
In fanotify_alloc_event() determine the mnt and fs_error event types by data_type, which uniquely identifies those events.
Permission events are recognized by event->type once the event exists. Before that, a permission event is a filesystem watcher whose mask contains FANOTIFY_PERM_EVENTS, so those bits can be reused by namespace events.
--- fs/notify/fanotify/fanotify.c | 33 ++++++++++++++++-------------- fs/notify/fanotify/fanotify.h | 24 +++++++++++++--------- fs/notify/fanotify/fanotify_user.c | 18 ++++++++-------- include/linux/fsnotify_backend.h | 5 ----- 4 files changed, 41 insertions(+), 39 deletions(-)
fs/notify/fanotify/fanotify.c +18 -15
@@ -193,7 +193,7 @@ static int fanotify_merge(struct fsnotify_group *group,* the event structure we have created in fanotify_handle_event() is the * one we should check for permission response. */ -if (fanotify_is_perm_event (new->mask)) +if (fanotify_is_perm_event (new))return 0 ;hlist_for_each_entry (old, hlist, merge_list) {
@@ -202,7 +202,7 @@ static int fanotify_merge(struct fsnotify_group *group,if (fanotify_should_merge (old, new)) { old->mask |= new->mask; -if (fanotify_is_error_event (old->mask)) +if (fanotify_is_error_event (old))FANOTIFY_EE (old)->err_count++;return 1 ;
@@ -712,11 +712,9 @@ static struct fanotify_event *fanotify_alloc_name_event(struct inode *dir,static struct fanotify_event *fanotify_alloc_error_event (struct fsnotify_group *group,__kernel_fsid_t *fsid, -const void *data,int data_type, +struct fs_error_report *report,unsigned int *hash) { -struct fs_error_report *report = -fsnotify_data_error_report (data, data_type);struct inode *inode;struct fanotify_error_event *fee;int fh_len;
@@ -760,6 +758,8 @@ static struct fanotify_event *fanotify_alloc_event( fid_mode);struct inode *dirid =fanotify_dfid_inode (mask, data, data_type, dir);const struct path *path =fsnotify_data_path (data, data_type); +struct fs_error_report *fs_error = +fsnotify_data_error_report (data, data_type);u64 mnt_id =fsnotify_data_mnt_id (data, data_type);struct mem_cgroup *old_memcg;struct dentry *moved =NULL ;
@@ -855,11 +855,12 @@ static struct fanotify_event *fanotify_alloc_event( pidfs_register_pid_gfp(pid, gfp)) goto out; -if (fanotify_is_perm_event (mask)) { +if (IS_ENABLED (CONFIG_FANOTIFY_ACCESS_PERMISSIONS ) && + mask &FANOTIFY_PERM_EVENTS ) { 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); + }else if (fs_error) { + event =fanotify_alloc_error_event (group, fsid, fs_error, + &hash); }else if (name_event && (file_name || moved || child)) { event =fanotify_alloc_name_event (dirid, fsid, file_name, child, moved, &hash, gfp);
@@ -921,7 +922,7 @@ static void fanotify_insert_event(struct fsnotify_group *group,assert_spin_locked (&group->notification_lock); -if (!fanotify_is_hashed_event (event->mask)) +if (!fanotify_is_hashed_event (event))return ; pr_debug("%s: group=%p event=%p bucket=%u\n", __func__,
@@ -941,6 +942,8 @@ static int fanotify_handle_event(struct fsnotify_group *group, u32 mask,struct fsnotify_event *fsn_event;__kernel_fsid_t fsid = {};u32 match_mask =0 ; +bool is_perm_event =fsnotify_is_filesystem_watcher (group) && + (mask &FANOTIFY_PERM_EVENTS );BUILD_BUG_ON (FAN_ACCESS !=FS_ACCESS );BUILD_BUG_ON (FAN_MODIFY !=FS_MODIFY );
@@ -975,7 +978,7 @@ static int fanotify_handle_event(struct fsnotify_group *group, u32 mask,pr_debug ("%s: group=%p mask=%x report_mask=%x\n" , __func__, group, mask, match_mask); -if (fanotify_is_perm_event (mask)) { +if (is_perm_event) {/* * fsnotify_prepare_user_wait() fails if we race with mark * deletion. Just let the operation pass in that case.
@@ -995,7 +998,7 @@ static int fanotify_handle_event(struct fsnotify_group *group, u32 mask,* We don't queue overflow events for permission events as * there the access is denied and so no event is in fact lost. */ -if (!fanotify_is_perm_event (mask)) +if (!is_perm_event)fsnotify_queue_overflow (group); goto finish; }
@@ -1005,17 +1008,17 @@ static int fanotify_handle_event(struct fsnotify_group *group, u32 mask, fanotify_insert_event);if (ret) {/* Permission events shouldn't be merged */ -BUG_ON (ret ==1 && mask &FANOTIFY_PERM_EVENTS ); +WARN_ON (ret ==1 && is_perm_event);/* Our event wasn't used in the end. Free it. */ fsnotify_destroy_event (group, fsn_event); ret =0 ; - }else if (fanotify_is_perm_event (mask)) { + }else if (is_perm_event) { ret =fanotify_get_response (group,FANOTIFY_PERM (event), iter_info); } finish: -if (fanotify_is_perm_event (mask)) +if (is_perm_event)fsnotify_finish_user_wait (iter_info);return ret;
fs/notify/fanotify/fanotify.h +14 -10
@@ -462,10 +462,14 @@ FANOTIFY_PERM(struct fanotify_event *event) return container_of(event, struct fanotify_perm_event, fae); } -static inline bool fanotify_is_perm_event (u32 mask) +static inline bool fanotify_is_perm_event (struct fanotify_event *event) { -return IS_ENABLED (CONFIG_FANOTIFY_ACCESS_PERMISSIONS ) && - mask &FANOTIFY_PERM_EVENTS ; +return event->type ==FANOTIFY_EVENT_TYPE_PATH_PERM ; +} + +static inline bool fsnotify_is_overflow_event (struct fanotify_event *event) +{ +return event->type ==FANOTIFY_EVENT_TYPE_OVERFLOW ; }static inline bool fanotify_event_has_access_range (struct fanotify_event *event)
@@ -481,14 +485,14 @@ static inline struct fanotify_event *FANOTIFY_E(struct fsnotify_event *fse) return container_of(fse, struct fanotify_event, fse); } -static inline bool fanotify_is_error_event (u32 mask) +static inline bool fanotify_is_error_event (struct fanotify_event *event) { -return mask &FAN_FS_ERROR ; +return event->type ==FANOTIFY_EVENT_TYPE_FS_ERROR ; } -static inline bool fanotify_is_mnt_event (u32 mask) +static inline bool fanotify_is_mnt_event (struct fanotify_event *event) { -return mask & (FAN_MNT_ATTACH |FAN_MNT_DETACH ); +return event->type ==FANOTIFY_EVENT_TYPE_MNT ; }static inline const struct path *fanotify_event_path (struct fanotify_event *event)
@@ -511,10 +515,10 @@ static inline const struct path *fanotify_event_path(struct fanotify_event *even/* * Permission events and overflow event do not get merged - don't hash them. */ -static inline bool fanotify_is_hashed_event (u32 mask) +static inline bool fanotify_is_hashed_event (struct fanotify_event *event) { -return !(fanotify_is_perm_event (mask) || -fsnotify_is_overflow_event (mask)); +return !(fanotify_is_perm_event (event) || +fsnotify_is_overflow_event (event)); } static inline unsigned int fanotify_event_hash_bucket(
fs/notify/fanotify/fanotify_user.c +9 -9
@@ -263,7 +263,7 @@ static size_t fanotify_event_len(unsigned int info_mode,int fh_len;int dot_len =0 ; -if (fanotify_is_error_event (event->mask)) +if (fanotify_is_error_event (event)) event_len +=FANOTIFY_ERROR_INFO_LEN ;if (fanotify_event_has_any_dir_fh (event)) {
@@ -281,7 +281,7 @@ static size_t fanotify_event_len(unsigned int info_mode, fh_len =fanotify_event_object_fh_len (event); event_len +=fanotify_fid_info_len (fh_len, dot_len); } -if (fanotify_is_mnt_event (event->mask)) +if (fanotify_is_mnt_event (event)) event_len +=FANOTIFY_MNT_INFO_LEN ;if (info_mode &FAN_REPORT_PIDFD )
@@ -344,9 +344,9 @@ static struct fanotify_event *get_one_event(struct fsnotify_group *group,* same event we peeked above. */ fsnotify_remove_first_event (group); -if (fanotify_is_perm_event (event->mask)) +if (fanotify_is_perm_event (event))FANOTIFY_PERM (event)->state =FAN_EVENT_REPORTED ; -if (fanotify_is_hashed_event (event->mask)) +if (fanotify_is_hashed_event (event))fanotify_unhash_event (group, event); out:spin_unlock (&group->notification_lock);
@@ -803,7 +803,7 @@ static int copy_info_records_to_user(struct fanotify_event *event, total_bytes += ret; } -if (fanotify_is_error_event (event->mask)) { +if (fanotify_is_error_event (event)) { ret =copy_error_info_to_user (event, buf, count);if (ret <0 )return ret;
@@ -821,7 +821,7 @@ static int copy_info_records_to_user(struct fanotify_event *event, total_bytes += ret; } -if (fanotify_is_mnt_event (event->mask)) { +if (fanotify_is_mnt_event (event)) { ret =copy_mnt_info_to_user (event, buf, count);if (ret <0 )return ret;
@@ -943,7 +943,7 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group,if (pidfd_file)fd_install (pidfd, pidfd_file); -if (fanotify_is_perm_event (event->mask)) +if (fanotify_is_perm_event (event))FANOTIFY_PERM (event)->fd = fd;return metadata.event_len;
@@ -1026,7 +1026,7 @@ static ssize_t fanotify_read(struct file *file, char __user *buf,* Permission events get queued to wait for response. Other * events can be destroyed now. */ -if (!fanotify_is_perm_event (event->mask)) { +if (!fanotify_is_perm_event (event)) {fsnotify_destroy_event (group, &event->fse); }else {if (ret <=0 ||FANOTIFY_PERM (event)->fd <0 ) {
@@ -1123,7 +1123,7 @@ static int fanotify_release(struct inode *ignored, struct file *file)while ((fsn_event =fsnotify_remove_first_event (group))) {struct fanotify_event *event =FANOTIFY_E (fsn_event); -if (!(event->mask &FANOTIFY_PERM_EVENTS )) { +if (!fanotify_is_perm_event (event)) {spin_unlock (&group->notification_lock);fsnotify_destroy_event (group, fsn_event); }else {
include/linux/fsnotify_backend.h +0 -5
@@ -805,11 +805,6 @@ static inline void fsnotify_queue_overflow(struct fsnotify_group *group)fsnotify_add_event (group, group->overflow_event,NULL ); } -static inline bool fsnotify_is_overflow_event (u32 mask) -{ -return mask &FS_Q_OVERFLOW ; -} -static inline bool fsnotify_notify_queue_is_empty (struct fsnotify_group *group) {assert_spin_locked (&group->notification_lock);
Signature · 1 line
2.55.0
Amir Goldstein
reviewerPATCH v3 5/14 fsnotify: do not report mount events with fsnotify()
In reply to Amir Goldsteinfsnotify() is now used to report events to both filesystem watchers and namespace watchers, but those two distinct event categories can never be sent to the same group.
Split out send_to_namespace_groups(), which only looks for interested mntns object marks, from fsnotify(), which only looks for interested filesystem object marks, and let fsnotify_mnt() call it.
The intention is that send_to_namespace_groups() will also be used to report namespace events to watchers of a userns object.
Gate the common code that is checking filesystem events with a filesystem group type check.
--- fs/notify/fsnotify.c | 92 +++++++++++++++++++++++++++++++------------- 1 file changed, 66 insertions(+), 26 deletions(-)
fs/notify/fsnotify.c +66 -26
@@ -333,7 +333,8 @@ static int fsnotify_handle_event(struct fsnotify_group *group, __u32 mask,static int send_to_group (__u32 mask,const void *data,int data_type,struct inode *dir,const struct qstr *file_name, -u32 cookie,struct fsnotify_iter_info *iter_info) +u32 cookie,struct fsnotify_iter_info *iter_info, +enum fsnotify_group_type group_type) {struct fsnotify_group *group =NULL ;__u32 test_mask = (mask &ALL_FSNOTIFY_EVENTS );
@@ -347,7 +348,7 @@ static int send_to_group(__u32 mask, const void *data, int data_type,return 0 ;/* clear ignored on inode modification */ -if (mask &FS_MODIFY ) { +if (group_type ==FSNOTIFY_GROUP_TYPE_FILESYSTEM && mask &FS_MODIFY ) { fsnotify_foreach_iter_mark_type(iter_info, mark, type) {if (!(mark->flags &FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY ))
@@ -363,9 +364,12 @@ static int send_to_group(__u32 mask, const void *data, int data_type,fsnotify_effective_ignore_mask (mark, is_dir, type); } -pr_debug ("%s: group=%p mask=%x marks_mask=%x marks_ignore_mask=%x data=%p data_type=%d dir=%p cookie=%d\n" , - __func__, group, mask, marks_mask, marks_ignore_mask, - data, data_type, dir, cookie); +pr_debug ("%s: group=%p type=%d mask=%x marks_mask=%x marks_ignore_mask=%x data=%p data_type=%d dir=%p cookie=%d\n" , + __func__, group, group_type, mask, marks_mask, + marks_ignore_mask, data, data_type, dir, cookie); + +if (WARN_ON_ONCE (group->type != group_type)) +return 0 ;if (!(test_mask & marks_mask & ~marks_ignore_mask))return 0 ;
@@ -472,6 +476,26 @@ static void fsnotify_iter_next(struct fsnotify_iter_info *iter_info) } } +static int send_to_groups (__u32 mask,const void *data,int data_type, +struct inode *dir,const struct qstr *file_name, +u32 cookie,struct fsnotify_iter_info *iter_info, +enum fsnotify_group_type group_type) +{ +int ret; + +while (fsnotify_iter_select_report_types (iter_info)) { + ret =send_to_group (mask, data, data_type, dir, file_name, + cookie, iter_info, group_type); + +if (ret && group_type ==FSNOTIFY_GROUP_TYPE_FILESYSTEM && + (mask &ALL_FSNOTIFY_PERM_EVENTS )) +return ret; + +fsnotify_iter_next (iter_info); + } +return 0 ; +} +/* * fsnotify - This is the main call to fsnotify. *
@@ -497,7 +521,6 @@ int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir, {const struct path *path =fsnotify_data_path (data, data_type);struct super_block *sb =fsnotify_data_sb (data, data_type); -const struct fsnotify_mnt *mnt_data =fsnotify_data_mnt (data, data_type);struct fsnotify_sb_info *sbinfo = sb ?fsnotify_sb_info (sb) :NULL ;struct fsnotify_iter_info iter_info = {};struct mount *mnt =NULL ;
@@ -540,8 +563,7 @@ int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir,if ((!sbinfo || !sbinfo->sb_marks) && (!mnt || !mnt->mnt_fsnotify_marks) && (!inode || !inode->i_fsnotify_marks) && - (!inode2 || !inode2->i_fsnotify_marks) && - (!mnt_data || !mnt_data->ns->n_fsnotify_marks)) + (!inode2 || !inode2->i_fsnotify_marks))return 0 ;if (sb)
@@ -552,8 +574,6 @@ int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir, marks_mask |=READ_ONCE (inode->i_fsnotify_mask);if (inode2) marks_mask |=READ_ONCE (inode2->i_fsnotify_mask); -if (mnt_data) - marks_mask |=READ_ONCE (mnt_data->ns->n_fsnotify_mask);/* * If this is a modify event we may need to clear some ignore masks.
@@ -561,7 +581,7 @@ int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir,* event in its mask. * Otherwise, return if none of the marks care about this type of event. */ - test_mask = (mask &ALL_FSNOTIFY_EVENTS ); + test_mask = (mask &FSNOTIFY_EVENTS_ON_FILESYSTEM );if (!(test_mask & marks_mask))return 0 ;
@@ -583,27 +603,16 @@ int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir, iter_info.marks[inode2_type] =fsnotify_first_mark (&inode2->i_fsnotify_marks); } -if (mnt_data) { - iter_info.marks[FSNOTIFY_ITER_TYPE_MNTNS ] = -fsnotify_first_mark (&mnt_data->ns->n_fsnotify_marks); - }/* * We need to merge inode/vfsmount/sb mark lists so that e.g. inode mark * ignore masks are properly reflected for mount/sb mark notifications. * That's why this traversal is so complicated... */ -while (fsnotify_iter_select_report_types (&iter_info)) { - ret =send_to_group (mask, data, data_type, dir, file_name, - cookie, &iter_info); - -if (ret && (mask &ALL_FSNOTIFY_PERM_EVENTS )) - goto out; + ret =send_to_groups (mask, data, data_type, dir, file_name, + cookie, &iter_info, +FSNOTIFY_GROUP_TYPE_FILESYSTEM ); -fsnotify_iter_next (&iter_info); - } - ret =0 ; -out:srcu_read_unlock (&fsnotify_mark_srcu, iter_info.srcu_idx);return ret;
@@ -696,6 +705,37 @@ int fsnotify_open_perm_and_set_mode(struct file *file) }#endif +static int send_to_namespace_groups (__u32 mask,const void *data, +int data_type) +{ +const struct fsnotify_mnt *mnt_data = +fsnotify_data_mnt (data, data_type); +struct fsnotify_iter_info iter_info = {}; +__u32 test_mask, marks_mask =0 ; +int ret; + +if (mnt_data) + marks_mask |=READ_ONCE (mnt_data->ns->n_fsnotify_mask); + + test_mask = mask &FSNOTIFY_EVENTS_ON_NAMESPACE ; +if (!(test_mask & marks_mask)) +return 0 ; + + iter_info.srcu_idx =srcu_read_lock (&fsnotify_mark_srcu); + +if (mnt_data) { + iter_info.marks[FSNOTIFY_ITER_TYPE_MNTNS ] = +fsnotify_first_mark (&mnt_data->ns->n_fsnotify_marks); + } + + ret =send_to_groups (mask, data, data_type,NULL ,NULL ,0 , &iter_info, +FSNOTIFY_GROUP_TYPE_NAMESPACE ); + +srcu_read_unlock (&fsnotify_mark_srcu, iter_info.srcu_idx); + +return ret; +} +void fsnotify_mnt (__u32 mask,struct mnt_namespace *ns,struct vfsmount *mnt) {struct fsnotify_mnt data = {
@@ -713,7 +753,7 @@ void fsnotify_mnt(__u32 mask, struct mnt_namespace *ns, struct vfsmount *mnt)if (!ns->n_fsnotify_marks)return ; -fsnotify (mask, &data,FSNOTIFY_EVENT_MNT ,NULL ,NULL ,NULL ,0 ); +send_to_namespace_groups (mask, &data,FSNOTIFY_EVENT_MNT ); }static __init int fsnotify_init (void )
Signature · 1 line
2.55.0
Amir Goldstein
reviewerPATCH v3 6/14 fanotify: gate filesystem event classification by group type
In reply to Amir Goldsteinfanotify decides on how to allocate events based on the event mask. The event mask bits that correspond to filesystem watchers are only meaningful in the context of filesystem group type.
Hence, before checking if event is a specific filesystem event (e.g. FS_RENAME) need to first check the group type is filesystem.
Separate fanotify_alloc_event() into an allocator per group type, so that all the filesystem event checks inside fanotify_alloc_filesystem_watcher_event() are already properly gated.
--- fs/notify/fanotify/fanotify.c | 88 ++++++++++++++++++++++++----------- 1 file changed, 60 insertions(+), 28 deletions(-)
fs/notify/fanotify/fanotify.c +60 -28
@@ -745,14 +745,14 @@ static struct fanotify_event *fanotify_alloc_error_event(return &fee->fae; } -static struct fanotify_event *fanotify_alloc_event ( +static struct fanotify_event *fanotify_alloc_filesystem_watcher_event (struct fsnotify_group *group,u32 mask,const void *data,int data_type,struct inode *dir,const struct qstr *file_name, -__kernel_fsid_t *fsid,u32 match_mask) +__kernel_fsid_t *fsid,u32 match_mask, +unsigned int *hash,gfp_t gfp) {struct fanotify_event *event =NULL ; -gfp_t gfp =GFP_KERNEL_ACCOUNT ;unsigned int fid_mode =FAN_GROUP_FLAG (group,FANOTIFY_FID_BITS );struct inode *id =fanotify_fid_inode (mask, data, data_type, dir, fid_mode);
@@ -760,14 +760,10 @@ static struct fanotify_event *fanotify_alloc_event(const struct path *path =fsnotify_data_path (data, data_type);struct fs_error_report *fs_error =fsnotify_data_error_report (data, data_type); -u64 mnt_id =fsnotify_data_mnt_id (data, data_type); -struct mem_cgroup *old_memcg;struct dentry *moved =NULL ;struct inode *child =NULL ;bool name_event = false; -unsigned int hash =0 ;bool ondir = mask &FAN_ONDIR ; -struct pid *pid;if ((fid_mode &FAN_REPORT_DIR_FID ) && dirid) {/*
@@ -832,6 +828,54 @@ static struct fanotify_event *fanotify_alloc_event( } } +if (IS_ENABLED (CONFIG_FANOTIFY_ACCESS_PERMISSIONS ) && + mask &FANOTIFY_PERM_EVENTS ) { + event =fanotify_alloc_perm_event (data, data_type, gfp); + }else if (fs_error) { + event =fanotify_alloc_error_event (group, fsid, fs_error, + hash); + }else if (name_event && (file_name || moved || child)) { + event =fanotify_alloc_name_event (dirid, fsid, file_name, child, + moved, hash, gfp); + }else if (fid_mode) { + event =fanotify_alloc_fid_event (id, fsid, hash, gfp); + }else if (path) { + event =fanotify_alloc_path_event (path, hash, gfp); + }else { +WARN_ON_ONCE (1 ); + } + +return event; +} + +static struct fanotify_event *fanotify_alloc_namespace_watcher_event ( +struct fsnotify_group *group,u64 mask, +const void *data,int data_type,gfp_t gfp) +{ +u64 mnt_id =fsnotify_data_mnt_id (data, data_type); +struct fanotify_event *event =NULL ; + +if (mnt_id) + event =fanotify_alloc_mnt_event (mnt_id, gfp); +else +WARN_ON_ONCE (1 ); + +return event; +} + +static struct fanotify_event *fanotify_alloc_event ( +struct fsnotify_group *group,u64 mask, +const void *data,int data_type, +struct inode *dir,const struct qstr *name, +__kernel_fsid_t *fsid,u32 match_mask) +{ +struct fanotify_event *event =NULL ; +gfp_t gfp =GFP_KERNEL_ACCOUNT ; +struct mem_cgroup *old_memcg; +unsigned int hash =0 ; +bool ondir = mask &FAN_ONDIR ; +struct pid *pid; +/* * For queues with unlimited length lost events are not expected and * can possibly have security implications. Avoid losing events when
@@ -855,25 +899,13 @@ static struct fanotify_event *fanotify_alloc_event( pidfs_register_pid_gfp(pid, gfp)) goto out; -if (IS_ENABLED (CONFIG_FANOTIFY_ACCESS_PERMISSIONS ) && - mask &FANOTIFY_PERM_EVENTS ) { - event =fanotify_alloc_perm_event (data, data_type, gfp); - }else if (fs_error) { - event =fanotify_alloc_error_event (group, fsid, fs_error, - &hash); - }else if (name_event && (file_name || moved || child)) { - event =fanotify_alloc_name_event (dirid, fsid, file_name, child, - moved, &hash, gfp); - }else if (fid_mode) { - event =fanotify_alloc_fid_event (id, fsid, &hash, gfp); - }else if (path) { - event =fanotify_alloc_path_event (path, &hash, gfp); - }else if (mnt_id) { - event =fanotify_alloc_mnt_event (mnt_id, gfp); - }else { -WARN_ON_ONCE (1 ); - } - +if (fsnotify_is_namespace_watcher (group)) + event =fanotify_alloc_namespace_watcher_event (group, mask, + data, data_type, gfp); +else + event =fanotify_alloc_filesystem_watcher_event (group, mask, + data, data_type, dir, name, + fsid, match_mask, &hash, gfp);if (!event) goto out;
@@ -975,8 +1007,8 @@ static int fanotify_handle_event(struct fsnotify_group *group, u32 mask,if (!mask)return 0 ; -pr_debug ("%s: group=%p mask=%x report_mask=%x\n" , __func__, - group, mask, match_mask); +pr_debug ("%s: group=%p type=%d mask=%x report_mask=%x\n" , __func__, + group, group->type, mask, match_mask);if (is_perm_event) {/*
Signature · 1 line
2.55.0
Amir Goldstein
reviewerPATCH v3 7/14 fanotify: define event mask constants as 64bit
In reply to Amir GoldsteinThe fanotoify_mark() syscall takes a u64 mask argument, but all the constants are defined as int values.
Define the constants unsigned long and use bits 32..29 to define their target group type.
All of the legacy event constant values are unchanged because they are defined with the filesystem watcher group type (0).
The FAN_MNT_* constants need special attention - they belong to the namespace watcher group type, but the type bits do not match. This will be handled by a followup patch.
--- fs/notify/fanotify/fanotify.c | 53 ++++++++++++++------------- include/uapi/linux/fanotify.h | 68 ++++++++++++++++++++--------------- 2 files changed, 68 insertions(+), 53 deletions(-)
fs/notify/fanotify/fanotify.c +29 -24
@@ -963,6 +963,10 @@ static void fanotify_insert_event(struct fsnotify_group *group,hlist_add_head (&event->merge_list, hlist); } +#define ASSERT_FS_EVENT (fan_mask, fsn_mask) \ + BUILD_BUG_ON((u32)(fan_mask) != (fsn_mask) || \ + FAN_EVENT_TYPE(fan_mask) != FSNOTIFY_GROUP_TYPE_FILESYSTEM) + static int fanotify_handle_event(struct fsnotify_group *group, u32 mask, const void *data, int data_type, struct inode *dir,
@@ -977,30 +981,31 @@ static int fanotify_handle_event(struct fsnotify_group *group, u32 mask,bool is_perm_event =fsnotify_is_filesystem_watcher (group) && (mask &FANOTIFY_PERM_EVENTS ); -BUILD_BUG_ON (FAN_ACCESS !=FS_ACCESS ); -BUILD_BUG_ON (FAN_MODIFY !=FS_MODIFY ); -BUILD_BUG_ON (FAN_ATTRIB !=FS_ATTRIB ); -BUILD_BUG_ON (FAN_CLOSE_NOWRITE !=FS_CLOSE_NOWRITE ); -BUILD_BUG_ON (FAN_CLOSE_WRITE !=FS_CLOSE_WRITE ); -BUILD_BUG_ON (FAN_OPEN !=FS_OPEN ); -BUILD_BUG_ON (FAN_MOVED_TO !=FS_MOVED_TO ); -BUILD_BUG_ON (FAN_MOVED_FROM !=FS_MOVED_FROM ); -BUILD_BUG_ON (FAN_CREATE !=FS_CREATE ); -BUILD_BUG_ON (FAN_DELETE !=FS_DELETE ); -BUILD_BUG_ON (FAN_DELETE_SELF !=FS_DELETE_SELF ); -BUILD_BUG_ON (FAN_MOVE_SELF !=FS_MOVE_SELF ); -BUILD_BUG_ON (FAN_EVENT_ON_CHILD !=FS_EVENT_ON_CHILD ); -BUILD_BUG_ON (FAN_Q_OVERFLOW !=FS_Q_OVERFLOW ); -BUILD_BUG_ON (FAN_OPEN_PERM !=FS_OPEN_PERM ); -BUILD_BUG_ON (FAN_ACCESS_PERM !=FS_ACCESS_PERM ); -BUILD_BUG_ON (FAN_ONDIR !=FS_ISDIR ); -BUILD_BUG_ON (FAN_OPEN_EXEC !=FS_OPEN_EXEC ); -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 ) !=24 ); +/* Assert "strong typed" events for filesystem watcher */ +ASSERT_FS_EVENT (FAN_ACCESS ,FS_ACCESS ); +ASSERT_FS_EVENT (FAN_MODIFY ,FS_MODIFY ); +ASSERT_FS_EVENT (FAN_ATTRIB ,FS_ATTRIB ); +ASSERT_FS_EVENT (FAN_CLOSE_NOWRITE ,FS_CLOSE_NOWRITE ); +ASSERT_FS_EVENT (FAN_CLOSE_WRITE ,FS_CLOSE_WRITE ); +ASSERT_FS_EVENT (FAN_OPEN ,FS_OPEN ); +ASSERT_FS_EVENT (FAN_MOVED_TO ,FS_MOVED_TO ); +ASSERT_FS_EVENT (FAN_MOVED_FROM ,FS_MOVED_FROM ); +ASSERT_FS_EVENT (FAN_CREATE ,FS_CREATE ); +ASSERT_FS_EVENT (FAN_DELETE ,FS_DELETE ); +ASSERT_FS_EVENT (FAN_DELETE_SELF ,FS_DELETE_SELF ); +ASSERT_FS_EVENT (FAN_MOVE_SELF ,FS_MOVE_SELF ); +ASSERT_FS_EVENT (FAN_EVENT_ON_CHILD ,FS_EVENT_ON_CHILD ); +ASSERT_FS_EVENT (FAN_Q_OVERFLOW ,FS_Q_OVERFLOW ); +ASSERT_FS_EVENT (FAN_OPEN_PERM ,FS_OPEN_PERM ); +ASSERT_FS_EVENT (FAN_ACCESS_PERM ,FS_ACCESS_PERM ); +ASSERT_FS_EVENT (FAN_ONDIR ,FS_ISDIR ); +ASSERT_FS_EVENT (FAN_OPEN_EXEC ,FS_OPEN_EXEC ); +ASSERT_FS_EVENT (FAN_OPEN_EXEC_PERM ,FS_OPEN_EXEC_PERM ); +ASSERT_FS_EVENT (FAN_FS_ERROR ,FS_ERROR ); +ASSERT_FS_EVENT (FAN_RENAME ,FS_RENAME ); +ASSERT_FS_EVENT (FAN_PRE_ACCESS ,FS_PRE_ACCESS ); + +BUILD_BUG_ON (HWEIGHT64 (ALL_FANOTIFY_EVENT_BITS ) !=24 ); mask =fanotify_group_event_mask (group, iter_info, &match_mask, mask, data, data_type, dir);
include/uapi/linux/fanotify.h +39 -29
89 lines not on this page · raw message
Signature · 1 line
2.55.0
Amir Goldstein
reviewerPATCH v3 8/14 fanotify: define new FAN_NS_MNT_* constants
In reply to Amir GoldsteinThe FAN_MNT_* event constants belong to the namespace watcher group type, but the type bits do not match.
We preserve these legacy FAN_MNT_* constants - they can never be mixed with any other event in the mask in upstream kernel anyway, so there is no real risk of confusion for applications that only watch mount events.
We define a new set of FAN_NS_MNT_* constants, which are defined with the namespace watcher group type. These could be allowed together with other namespace events in future kernels, so they are intended to be used by new applications that are going to watch namespace trees and mount trees with the same fanotify group.
At the fsnotify u32 mask level, there is no difference between the two uapi flavors. Rename the fsnotify event constants to FSN_MNT_*, to clarify that they are not filesystem watcher event types.
--- fs/notify/fanotify/fanotify.c | 13 +++++++++++-- fs/notify/fanotify/fanotify_user.c | 2 +- include/linux/fanotify.h | 2 +- include/linux/fsnotify.h | 6 +++--- include/linux/fsnotify_backend.h | 8 ++++---- include/trace/misc/fsnotify.h | 4 ++-- include/uapi/linux/fanotify.h | 6 +++++- 7 files changed, 27 insertions(+), 14 deletions(-)
fs/notify/fanotify/fanotify.c +11 -2
@@ -966,6 +966,9 @@ static void fanotify_insert_event(struct fsnotify_group *group,#define ASSERT_FS_EVENT (fan_mask, fsn_mask) \ BUILD_BUG_ON((u32)(fan_mask) != (fsn_mask) || \ FAN_EVENT_TYPE(fan_mask) != FSNOTIFY_GROUP_TYPE_FILESYSTEM) +#define ASSERT_NAMESPACE_EVENT (fan_mask, fsn_mask) \ + BUILD_BUG_ON((u32)(fan_mask) != (fsn_mask) || \ + FAN_EVENT_TYPE(fan_mask) != FSNOTIFY_GROUP_TYPE_NAMESPACE) static int fanotify_handle_event(struct fsnotify_group *group, u32 mask, const void *data, int data_type,
@@ -1004,8 +1007,14 @@ static int fanotify_handle_event(struct fsnotify_group *group, u32 mask,ASSERT_FS_EVENT (FAN_FS_ERROR ,FS_ERROR );ASSERT_FS_EVENT (FAN_RENAME ,FS_RENAME );ASSERT_FS_EVENT (FAN_PRE_ACCESS ,FS_PRE_ACCESS ); - -BUILD_BUG_ON (HWEIGHT64 (ALL_FANOTIFY_EVENT_BITS ) !=24 ); +/* Assert backward compat mount events */ +ASSERT_FS_EVENT (FAN_MNT_ATTACH ,FSN_MNT_ATTACH ); +ASSERT_FS_EVENT (FAN_MNT_DETACH ,FSN_MNT_DETACH ); +/* Assert "strong typed" events for namespace watcher */ +ASSERT_NAMESPACE_EVENT (FAN_NS_MNT_ATTACH ,FSN_MNT_ATTACH ); +ASSERT_NAMESPACE_EVENT (FAN_NS_MNT_DETACH ,FSN_MNT_DETACH ); + +BUILD_BUG_ON (HWEIGHT64 (ALL_FANOTIFY_EVENT_BITS ) !=25 ); mask =fanotify_group_event_mask (group, iter_info, &match_mask, mask, data, data_type, dir);
fs/notify/fanotify/fanotify_user.c +1 -1
@@ -1873,7 +1873,7 @@ static int fanotify_events_supported(struct fsnotify_group *group,static bool fanotify_is_valid_mask (struct fsnotify_group *group,int mark_type,u64 mask) { -u32 valid_mask =0 ; +u64 valid_mask =0 ;/* * Event bits for different group type may be overloaded but event
include/linux/fanotify.h +1 -1
@@ -122,7 +122,7 @@ FANOTIFY_ERROR_EVENTS)/* Mount tree monitoring events */ -#define FANOTIFY_MOUNT_EVENTS (FAN_MNT_ATTACH | FAN_MNT_DETACH) +#define FANOTIFY_MOUNT_EVENTS (FAN_NS_MNT_ATTACH | FAN_NS_MNT_DETACH)/* Events that user can request to be notified on namespace watchers */ #define FANOTIFY_EVENTS_ON_NAMESPACE (FANOTIFY_MOUNT_EVENTS)
include/linux/fsnotify.h +3 -3
@@ -501,17 +501,17 @@ static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)static inline void fsnotify_mnt_attach (struct mnt_namespace *ns,struct vfsmount *mnt) { -fsnotify_mnt (FS_MNT_ATTACH , ns, mnt); +fsnotify_mnt (FSN_MNT_ATTACH , ns, mnt); }static inline void fsnotify_mnt_detach (struct mnt_namespace *ns,struct vfsmount *mnt) { -fsnotify_mnt (FS_MNT_DETACH , ns, mnt); +fsnotify_mnt (FSN_MNT_DETACH , ns, mnt); }static inline void fsnotify_mnt_move (struct mnt_namespace *ns,struct vfsmount *mnt) { -fsnotify_mnt (FS_MNT_MOVE , ns, mnt); +fsnotify_mnt (FSN_MNT_MOVE , ns, mnt); }#endif /* _LINUX_FS_NOTIFY_H */
include/linux/fsnotify_backend.h +4 -4
@@ -79,9 +79,9 @@* * NOTE: These values may overload filesystem events, but not event flags */ -#define FS_MNT_ATTACH 0x01000000/* Mount was attached */ -#define FS_MNT_DETACH 0x02000000/* Mount was detached */ -#define FS_MNT_MOVE (FS_MNT_ATTACH | FS_MNT_DETACH) +#define FSN_MNT_ATTACH 0x01000000/* Mount was attached */ +#define FSN_MNT_DETACH 0x02000000/* Mount was detached */ +#define FSN_MNT_MOVE (FSN_MNT_ATTACH | FSN_MNT_DETACH)/*
@@ -126,7 +126,7 @@ FS_IN_IGNORED | FS_ERROR)/* Mount tree monitoring events */ -#define FSNOTIFY_MNT_EVENTS (FS_MNT_ATTACH | FS_MNT_DETACH) +#define FSNOTIFY_MNT_EVENTS (FSN_MNT_ATTACH | FSN_MNT_DETACH)/* Events that can be reported to backends on namespace watchers */ #define FSNOTIFY_EVENTS_ON_NAMESPACE (FSNOTIFY_MNT_EVENTS | FS_Q_OVERFLOW)
include/trace/misc/fsnotify.h +2 -2
@@ -27,8 +27,8 @@ { FS_ACCESS_PERM, "ACCESS_PERM" }, \ { FS_OPEN_EXEC_PERM, "OPEN_EXEC_PERM" }, \ { FS_PRE_ACCESS, "PRE_ACCESS" }, \ - { FS_MNT_ATTACH, "MNT_ATTACH" }, \ - { FS_MNT_DETACH, "MNT_DETACH" }, \ + { FSN_MNT_ATTACH, "MNT_ATTACH" }, \ + { FSN_MNT_DETACH, "MNT_DETACH" }, \ { FS_EVENT_ON_CHILD, "EVENT_ON_CHILD" }, \ { FS_RENAME, "RENAME" }, \ { FS_DN_MULTISHOT, "DN_MULTISHOT" }, \
include/uapi/linux/fanotify.h +5 -1
@@ -56,10 +56,14 @@/* * Events that user-space can request when watching namespaces * -* NOTE: These values may overload filesystem events, but not event flags +* NOTE: Lower 32bit values may overload filesystem events, but not event flags */ +/* Backward compat mount events - cannot be mixed with other events */ #define FAN_MNT_ATTACH 0x0001000000ULL/* Mount was attached */ #define FAN_MNT_DETACH 0x0002000000ULL/* Mount was detached */ +/* "Strong typed" mount events - may be mixed with other namespace events */ +#define FAN_NS_MNT_ATTACH 0x0101000000ULL/* Mount was attached */ +#define FAN_NS_MNT_DETACH 0x0102000000ULL/* Mount was detached */ /* flags used for fanotify_init() */ #define FAN_CLOEXEC 0x00000001
Signature · 1 line
2.55.0
Amir Goldstein
reviewerPATCH v3 9/14 fanotify: gate event mask in fanotify_mark() by group type
In reply to Amir GoldsteinBits 32..39 of the event mask encode the expected group watcher type. Let fanotify_mark() accept a mask only when that type matches the group watcher type.
FAN_MNT_* events carry no type in the mask, so special case them to pass this gate.
The type is dropped when the 32bit mask is stored in the mark, so for now the new FAN_NS_MNT_* events cannot be requested in fanotify_mark().
to prepare for overload the lower event bits, change all the bitmask checks to use a helper fanotify_mask_match() which also compares the type bits.
--- fs/notify/fanotify/fanotify_user.c | 118 ++++++++++++++++++++--------- 1 file changed, 81 insertions(+), 37 deletions(-)
fs/notify/fanotify/fanotify_user.c +81 -37
@@ -1462,8 +1462,36 @@ static int fanotify_group_init_error_pool(struct fsnotify_group *group) sizeof(struct fanotify_error_event)); } +/* +* The low 32 event bits of a fanotify mask and the watcher type encoded +* in its high byte. +*/ +struct fanotify_mask { +u32 mask; +u8 type; +}; + +/* Match @fan_mask to any event in @event_set of the same watcher type */ +static bool fanotify_mask_match (struct fanotify_mask fan_mask, +__u64 event_set) +{ +return fan_mask.type ==FAN_EVENT_TYPE (event_set) && + (fan_mask.mask & event_set); +} + +static int fanotify_mask_from_user (__u64 mask,struct fanotify_mask *fan_mask) +{ +if (mask & ~(FAN_EVENT_TYPE_MASK |ALL_FANOTIFY_EVENT_BITS )) +return -EINVAL ; + + fan_mask->mask = mask; + fan_mask->type =FAN_EVENT_TYPE (mask); +return 0 ; +} +static int fanotify_may_update_existing_mark (struct fsnotify_mark *fsn_mark, -__u32 mask,unsigned int fan_flags) +struct fanotify_mask fan_mask, +unsigned int fan_flags) {/* * Non evictable mark cannot be downgraded to evictable mark.
@@ -1491,8 +1519,9 @@ static int fanotify_may_update_existing_mark(struct fsnotify_mark *fsn_mark,return -EEXIST ;/* For now pre-content events are not generated for directories */ - mask |= fsn_mark->mask; -if (mask &FANOTIFY_PRE_CONTENT_EVENTS && mask &FAN_ONDIR ) + fan_mask.mask |= fsn_mark->mask; +if ((fan_mask.mask &FAN_ONDIR ) && +fanotify_mask_match (fan_mask,FANOTIFY_PRE_CONTENT_EVENTS ))return -EEXIST ;return 0 ;
@@ -1500,7 +1529,8 @@ static int fanotify_may_update_existing_mark(struct fsnotify_mark *fsn_mark,static int fanotify_add_mark (struct fsnotify_group *group,void *obj,unsigned int obj_type, -__u32 mask,unsigned int fan_flags, +struct fanotify_mask fan_mask, +unsigned int fan_flags,struct fan_fsid *fsid) {struct fsnotify_mark *fsn_mark;
@@ -1521,7 +1551,7 @@ static int fanotify_add_mark(struct fsnotify_group *group,/* * Check if requested mark flags conflict with an existing mark flags. */ - ret =fanotify_may_update_existing_mark (fsn_mark, mask, fan_flags); + ret =fanotify_may_update_existing_mark (fsn_mark, fan_mask, fan_flags);if (ret) goto out;
@@ -1530,13 +1560,13 @@ static int fanotify_add_mark(struct fsnotify_group *group,* needed (i.e. FAN_FS_ERROR was requested). */ if (!(fan_flags &FANOTIFY_MARK_IGNORE_BITS ) && - (mask &FAN_FS_ERROR )) { +fanotify_mask_match (fan_mask,FAN_FS_ERROR )) { ret =fanotify_group_init_error_pool (group);if (ret) goto out; } - recalc =fanotify_mark_add_to_mask (fsn_mark, mask, fan_flags); + recalc =fanotify_mark_add_to_mask (fsn_mark, fan_mask.mask, fan_flags);if (recalc)fsnotify_recalc_mask (fsn_mark->connector);
@@ -1545,7 +1575,7 @@ static int fanotify_add_mark(struct fsnotify_group *group,fsnotify_put_mark (fsn_mark); -if (!ret && (mask &FANOTIFY_PERM_EVENTS )) +if (!ret &&fanotify_mask_match (fan_mask,FANOTIFY_PERM_EVENTS ))fanotify_perm_watchdog_group_add (group);return ret;
@@ -1811,21 +1841,22 @@ static int fanotify_test_fid(struct dentry *dentry, unsigned int flags) }static int fanotify_events_supported (struct fsnotify_group *group, -const struct path *path,__u64 mask, +const struct path *path, +struct fanotify_mask fan_mask,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 ) || +fanotify_mask_match (fan_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 (fanotify_mask_match (fan_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))
@@ -1840,7 +1871,7 @@ static int fanotify_events_supported(struct fsnotify_group *group,* waits for fanotify permission event to be answered. Just disallow * permission events for such filesystems. */ -if (mask &FANOTIFY_PERM_EVENTS && +if (fanotify_mask_match (fan_mask,FANOTIFY_PERM_EVENTS ) && path->mnt->mnt_sb->s_type->fs_flags &FS_DISALLOW_NOTIFY_PERM )return -EINVAL ;
@@ -1863,15 +1894,15 @@ static int fanotify_events_supported(struct fsnotify_group *group,* flags FAN_ONDIR and FAN_EVENT_ON_CHILD in mask of non-dir inode, * but because we always allowed it, error only when using new APIs. */ -if (strict_dir_events && mark_type ==FAN_MARK_INODE && - !is_dir && (mask &FANOTIFY_DIRONLY_EVENT_BITS )) +if (strict_dir_events && mark_type ==FAN_MARK_INODE && !is_dir && +fanotify_mask_match (fan_mask,FANOTIFY_DIRONLY_EVENT_BITS ))return -ENOTDIR ;return 0 ; }static bool fanotify_is_valid_mask (struct fsnotify_group *group,int mark_type, -u64 mask) +struct fanotify_mask fan_mask) {u64 valid_mask =0 ;
@@ -1894,14 +1925,20 @@ static bool fanotify_is_valid_mask(struct fsnotify_group *group, int mark_type,if (mark_type ==FAN_MARK_MNTNS &&FAN_GROUP_FLAG (group,FAN_REPORT_MNT )) valid_mask =FANOTIFY_MOUNT_EVENTS ; +/* Classify the legacy mount events as namespace events */ +if (fan_mask.mask & (FAN_MNT_ATTACH |FAN_MNT_DETACH )) + fan_mask.type =FSNOTIFY_GROUP_TYPE_NAMESPACE ;break ; } -return !(mask & ~valid_mask); +if (fan_mask.type != group->type) +return false; + +return !(fan_mask.mask & ~valid_mask); } -static int do_fanotify_mark (int fanotify_fd,unsigned int flags,__u64 mask, -int dfd,const char __user *pathname) +static int do_fanotify_mark (int fanotify_fd,unsigned int flags,__u64 __mask, +int dfd,const char __user *pathname) {struct inode *inode =NULL ;struct fsnotify_group *group;
@@ -1909,6 +1946,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,struct fan_fsid __fsid, *fsid =NULL ;struct user_namespace *user_ns =NULL ;struct mnt_namespace *mntns; +struct fanotify_mask fan_mask;unsigned int mark_type = flags &FANOTIFY_MARK_TYPE_BITS ;unsigned int mark_cmd = flags &FANOTIFY_MARK_CMD_BITS ;unsigned int ignore = flags &FANOTIFY_MARK_IGNORE_BITS ;
@@ -1919,10 +1957,14 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,int ret;pr_debug ("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n" , - __func__, fanotify_fd, flags, dfd, pathname, mask); + __func__, fanotify_fd, flags, dfd, pathname, __mask); + + ret =fanotify_mask_from_user (__mask, &fan_mask); +if (ret) +return ret;/* we only use the lower 32 bits as of right now. */ -if (upper_32_bits (mask)) +if (fan_mask.type)return -EINVAL ;if (flags & ~FANOTIFY_MARK_FLAGS )
@@ -1952,7 +1994,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,switch (mark_cmd) {case FAN_MARK_ADD :case FAN_MARK_REMOVE : -if (!mask) +if (!fan_mask.mask)return -EINVAL ;break ;case FAN_MARK_FLUSH :
@@ -1972,7 +2014,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,* FAN_MARK_IGNORED_MASK. */ if (ignore ==FAN_MARK_IGNORED_MASK ) { - mask &= ~FANOTIFY_EVENT_FLAGS ; + fan_mask.mask &= ~FANOTIFY_EVENT_FLAGS ; umask =FANOTIFY_EVENT_FLAGS ; }
@@ -1986,7 +2028,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, group =fd_file (f)->private_data;if (group->type != mark_group_type || - !fanotify_is_valid_mask (group, mark_type, mask)) + !fanotify_is_valid_mask (group, mark_type, fan_mask))return -EINVAL ;/*
@@ -2001,14 +2043,14 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,* Permission events are not allowed for FAN_CLASS_NOTIF. * Pre-content permission events are not allowed for FAN_CLASS_CONTENT. */ -if (mask &FANOTIFY_PERM_EVENTS && +if (fanotify_mask_match (fan_mask,FANOTIFY_PERM_EVENTS ) && group->priority ==FSNOTIFY_PRIO_NORMAL )return -EINVAL ; -else if (mask &FANOTIFY_PRE_CONTENT_EVENTS && +else if (fanotify_mask_match (fan_mask,FANOTIFY_PRE_CONTENT_EVENTS ) && group->priority ==FSNOTIFY_PRIO_CONTENT )return -EINVAL ; -if (mask &FAN_FS_ERROR && +if (fanotify_mask_match (fan_mask,FAN_FS_ERROR ) && mark_type !=FAN_MARK_FILESYSTEM )return -EINVAL ;
@@ -2029,7 +2071,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,*/ fid_mode =FAN_GROUP_FLAG (group,FANOTIFY_FID_BITS );if (fsnotify_is_filesystem_watcher (group) && - mask & ~(FANOTIFY_FD_EVENTS |FANOTIFY_EVENT_FLAGS ) && + fan_mask.mask & ~(FANOTIFY_FD_EVENTS |FANOTIFY_EVENT_FLAGS ) && (!fid_mode || mark_type ==FAN_MARK_MOUNT ))return -EINVAL ;
@@ -2038,11 +2080,13 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,* new parent+name. Reporting only old and new parent id is less * useful and was not implemented. */ -if (mask &FAN_RENAME && !(fid_mode &FAN_REPORT_NAME )) +if (fanotify_mask_match (fan_mask,FAN_RENAME ) && + !(fid_mode &FAN_REPORT_NAME ))return -EINVAL ;/* Pre-content events are not currently generated for directories. */ -if (mask &FANOTIFY_PRE_CONTENT_EVENTS && mask &FAN_ONDIR ) +if ((fan_mask.mask &FAN_ONDIR ) && +fanotify_mask_match (fan_mask,FANOTIFY_PRE_CONTENT_EVENTS ))return -EINVAL ;if (mark_cmd ==FAN_MARK_FLUSH ) {
@@ -2051,12 +2095,12 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, } ret =fanotify_find_path (dfd, pathname, &path, flags, - (mask &ALL_FSNOTIFY_EVENTS ), obj_type); + (fan_mask.mask &ALL_FSNOTIFY_EVENTS ), obj_type);if (ret)return ret;if (mark_cmd ==FAN_MARK_ADD ) { - ret =fanotify_events_supported (group, &path, mask, flags); + ret =fanotify_events_supported (group, &path, fan_mask, flags);if (ret) goto path_put_and_out; }
@@ -2066,7 +2110,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,if (ret) goto path_put_and_out; - ret =security_path_notify (&path, mask, obj_type); + ret =security_path_notify (&path, fan_mask.mask, obj_type);if (ret) goto path_put_and_out;
@@ -2134,7 +2178,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,/* Mask out FAN_EVENT_ON_CHILD flag for sb/mount/non-dir marks */ if (!inode || !S_ISDIR (inode->i_mode)) { - mask &= ~FAN_EVENT_ON_CHILD ; + fan_mask.mask &= ~FAN_EVENT_ON_CHILD ; umask =FAN_EVENT_ON_CHILD ;/* * If group needs to report parent fid, register for getting
@@ -2142,18 +2186,18 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,*/ if ((fid_mode &FAN_REPORT_DIR_FID ) && (flags &FAN_MARK_ADD ) && !ignore) - mask |=FAN_EVENT_ON_CHILD ; + fan_mask.mask |=FAN_EVENT_ON_CHILD ; }/* create/update an inode mark */ switch (mark_cmd) {case FAN_MARK_ADD : - ret =fanotify_add_mark (group, obj, obj_type, mask, flags, + ret =fanotify_add_mark (group, obj, obj_type, fan_mask, flags, fsid);break ;case FAN_MARK_REMOVE : - ret =fanotify_remove_mark (group, obj, obj_type, mask, flags, - umask); + ret =fanotify_remove_mark (group, obj, obj_type, fan_mask.mask, + flags, umask);break ;default : ret = -EINVAL ;
Signature · 1 line
2.55.0
Amir Goldstein
reviewerRe: [PATCH v3 09/14] fanotify: gate event mask in fanotify_mark() by group type
Quoted 176 lines · Amir Goldstein
Bits 32..39 of the event mask encode the expected group watcher type. Let
fanotify_mark()accept a mask only when that type matches the group watcher type.FAN_MNT_* events carry no type in the mask, so special case them to pass this gate.
The type is dropped when the 32bit mask is stored in the mark, so for now the new FAN_NS_MNT_* events cannot be requested in
fanotify_mark().to prepare for overload the lower event bits, change all the bitmask checks to use a helper
fanotify_mask_match()which also compares the type bits.Signed-off-by Amir Goldstein <[email protected]>--- fs/notify/fanotify/fanotify_user.c | 118 ++++++++++++++++++++--------- 1 file changed, 81 insertions(+), 37 deletions(-)fs/notify/fanotify/fanotify_user.c +46
-15@@ -1462,8 +1462,36 @@ static int fanotify_group_init_error_pool(struct fsnotify_group *group) sizeof(struct fanotify_error_event)); } +/* +* The low 32 event bits of a fanotify mask and the watcher type encoded +* in its high byte. +*/ +struct fanotify_mask { +u32 mask; +u8 type; +}; + +/* Match @fan_mask to any event in @event_set of the same watcher type */ +static bool fanotify_mask_match (struct fanotify_mask fan_mask, +__u64 event_set) +{ +return fan_mask.type ==FAN_EVENT_TYPE (event_set) && + (fan_mask.mask & event_set); +} + +static int fanotify_mask_from_user (__u64 mask,struct fanotify_mask *fan_mask) +{ +if (mask & ~(FAN_EVENT_TYPE_MASK |ALL_FANOTIFY_EVENT_BITS )) +return -EINVAL ; + + fan_mask->mask = mask; + fan_mask->type =FAN_EVENT_TYPE (mask); +return 0 ; +} +static int fanotify_may_update_existing_mark (struct fsnotify_mark *fsn_mark, -__u32 mask,unsigned int fan_flags) +struct fanotify_mask fan_mask, +unsigned int fan_flags) {/* * Non evictable mark cannot be downgraded to evictable mark. @@ -1491,8 +1519,9 @@ static int fanotify_may_update_existing_mark(struct fsnotify_mark *fsn_mark,return -EEXIST ;/* For now pre-content events are not generated for directories */ - mask |= fsn_mark->mask; -if (mask &FANOTIFY_PRE_CONTENT_EVENTS && mask &FAN_ONDIR ) + fan_mask.mask |= fsn_mark->mask; +if ((fan_mask.mask &FAN_ONDIR ) && +fanotify_mask_match (fan_mask,FANOTIFY_PRE_CONTENT_EVENTS ))return -EEXIST ;return 0 ;@@ -1500,7 +1529,8 @@ static int fanotify_may_update_existing_mark(struct fsnotify_mark *fsn_mark,static int fanotify_add_mark (struct fsnotify_group *group,void *obj,unsigned int obj_type, -__u32 mask,unsigned int fan_flags, +struct fanotify_mask fan_mask, +unsigned int fan_flags,struct fan_fsid *fsid) {struct fsnotify_mark *fsn_mark;@@ -1521,7 +1551,7 @@ static int fanotify_add_mark(struct fsnotify_group *group,/* * Check if requested mark flags conflict with an existing mark flags. */ - ret =fanotify_may_update_existing_mark (fsn_mark, mask, fan_flags); + ret =fanotify_may_update_existing_mark (fsn_mark, fan_mask, fan_flags);if (ret) goto out;@@ -1530,13 +1560,13 @@ static int fanotify_add_mark(struct fsnotify_group *group,* needed (i.e. FAN_FS_ERROR was requested). */ if (!(fan_flags &FANOTIFY_MARK_IGNORE_BITS ) && - (mask &FAN_FS_ERROR )) { +fanotify_mask_match (fan_mask,FAN_FS_ERROR )) { ret =fanotify_group_init_error_pool (group);if (ret) goto out; } - recalc =fanotify_mark_add_to_mask (fsn_mark, mask, fan_flags); + recalc =fanotify_mark_add_to_mask (fsn_mark, fan_mask.mask, fan_flags);if (recalc)fsnotify_recalc_mask (fsn_mark->connector);@@ -1545,7 +1575,7 @@ static int fanotify_add_mark(struct fsnotify_group *group,fsnotify_put_mark (fsn_mark); -if (!ret && (mask &FANOTIFY_PERM_EVENTS )) +if (!ret &&fanotify_mask_match (fan_mask,FANOTIFY_PERM_EVENTS ))fanotify_perm_watchdog_group_add (group);return ret;@@ -1811,21 +1841,22 @@ static int fanotify_test_fid(struct dentry *dentry, unsigned int flags) }static int fanotify_events_supported (struct fsnotify_group *group, -const struct path *path,__u64 mask, +const struct path *path, +struct fanotify_mask fan_mask,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 ) || +fanotify_mask_match (fan_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 (fanotify_mask_match (fan_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))@@ -1840,7 +1871,7 @@ static int fanotify_events_supported(struct fsnotify_group *group,* waits for fanotify permission event to be answered. Just disallow * permission events for such filesystems. */ -if (mask &FANOTIFY_PERM_EVENTS && +if (fanotify_mask_match (fan_mask,FANOTIFY_PERM_EVENTS ) && path->mnt->mnt_sb->s_type->fs_flags &FS_DISALLOW_NOTIFY_PERM )return -EINVAL ;@@ -1863,15 +1894,15 @@ static int fanotify_events_supported(struct fsnotify_group *group,* flags FAN_ONDIR and FAN_EVENT_ON_CHILD in mask of non-dir inode, * but because we always allowed it, error only when using new APIs. */ -if (strict_dir_events && mark_type ==FAN_MARK_INODE && - !is_dir && (mask &FANOTIFY_DIRONLY_EVENT_BITS )) +if (strict_dir_events && mark_type ==FAN_MARK_INODE && !is_dir && +fanotify_mask_match (fan_mask,FANOTIFY_DIRONLY_EVENT_BITS ))return -ENOTDIR ;return 0 ; }static bool fanotify_is_valid_mask (struct fsnotify_group *group,int mark_type, -u64 mask) +struct fanotify_mask fan_mask) {u64 valid_mask =0 ;@@ -1894,14 +1925,20 @@ static bool fanotify_is_valid_mask(struct fsnotify_group *group, int mark_type,if (mark_type ==FAN_MARK_MNTNS &&FAN_GROUP_FLAG (group,FAN_REPORT_MNT )) valid_mask =FANOTIFY_MOUNT_EVENTS ;
fs/notify/fanotify/fanotify_user.c static bool fanotify_is_valid_mask(struct fsnotify_group *group, int mark_type, · line 1936
Quoted 10 lines
fs/notify/fanotify/fanotify_user.c +6
-1+/* Classify the legacy mount events as namespace events */ +if (fan_mask.mask & (FAN_MNT_ATTACH |FAN_MNT_DETACH )) + fan_mask.type =FSNOTIFY_GROUP_TYPE_NAMESPACE ;break ; } -return !(mask & ~valid_mask); +if (fan_mask.type != group->type) +return false; +
This overlooks the case of zero mask for FAN_MARK_FLUSH and non zero group type (FAN_MARK_MNTNS).
Fixed on branch fsnotify_group_type in my github and added this test case to the sefltest.
Thanks,
Amir.
Quoted 164 lines
fs/notify/fanotify/fanotify_user.c +29
-22+return !(fan_mask.mask & ~valid_mask); } -static int do_fanotify_mark (int fanotify_fd,unsigned int flags,__u64 mask, -int dfd,const char __user *pathname) +static int do_fanotify_mark (int fanotify_fd,unsigned int flags,__u64 __mask, +int dfd,const char __user *pathname) {struct inode *inode =NULL ;struct fsnotify_group *group;@@ -1909,6 +1946,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,struct fan_fsid __fsid, *fsid =NULL ;struct user_namespace *user_ns =NULL ;struct mnt_namespace *mntns; +struct fanotify_mask fan_mask;unsigned int mark_type = flags &FANOTIFY_MARK_TYPE_BITS ;unsigned int mark_cmd = flags &FANOTIFY_MARK_CMD_BITS ;unsigned int ignore = flags &FANOTIFY_MARK_IGNORE_BITS ;@@ -1919,10 +1957,14 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,int ret;pr_debug ("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n" , - __func__, fanotify_fd, flags, dfd, pathname, mask); + __func__, fanotify_fd, flags, dfd, pathname, __mask); + + ret =fanotify_mask_from_user (__mask, &fan_mask); +if (ret) +return ret;/* we only use the lower 32 bits as of right now. */ -if (upper_32_bits (mask)) +if (fan_mask.type)return -EINVAL ;if (flags & ~FANOTIFY_MARK_FLAGS )@@ -1952,7 +1994,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,switch (mark_cmd) {case FAN_MARK_ADD :case FAN_MARK_REMOVE : -if (!mask) +if (!fan_mask.mask)return -EINVAL ;break ;case FAN_MARK_FLUSH :@@ -1972,7 +2014,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,* FAN_MARK_IGNORED_MASK. */ if (ignore ==FAN_MARK_IGNORED_MASK ) { - mask &= ~FANOTIFY_EVENT_FLAGS ; + fan_mask.mask &= ~FANOTIFY_EVENT_FLAGS ; umask =FANOTIFY_EVENT_FLAGS ; }@@ -1986,7 +2028,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, group =fd_file (f)->private_data;if (group->type != mark_group_type || - !fanotify_is_valid_mask (group, mark_type, mask)) + !fanotify_is_valid_mask (group, mark_type, fan_mask))return -EINVAL ;/* @@ -2001,14 +2043,14 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,* Permission events are not allowed for FAN_CLASS_NOTIF. * Pre-content permission events are not allowed for FAN_CLASS_CONTENT. */ -if (mask &FANOTIFY_PERM_EVENTS && +if (fanotify_mask_match (fan_mask,FANOTIFY_PERM_EVENTS ) && group->priority ==FSNOTIFY_PRIO_NORMAL )return -EINVAL ; -else if (mask &FANOTIFY_PRE_CONTENT_EVENTS && +else if (fanotify_mask_match (fan_mask,FANOTIFY_PRE_CONTENT_EVENTS ) && group->priority ==FSNOTIFY_PRIO_CONTENT )return -EINVAL ; -if (mask &FAN_FS_ERROR && +if (fanotify_mask_match (fan_mask,FAN_FS_ERROR ) && mark_type !=FAN_MARK_FILESYSTEM )return -EINVAL ;@@ -2029,7 +2071,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,*/ fid_mode =FAN_GROUP_FLAG (group,FANOTIFY_FID_BITS );if (fsnotify_is_filesystem_watcher (group) && - mask & ~(FANOTIFY_FD_EVENTS |FANOTIFY_EVENT_FLAGS ) && + fan_mask.mask & ~(FANOTIFY_FD_EVENTS |FANOTIFY_EVENT_FLAGS ) && (!fid_mode || mark_type ==FAN_MARK_MOUNT ))return -EINVAL ;@@ -2038,11 +2080,13 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,* new parent+name. Reporting only old and new parent id is less * useful and was not implemented. */ -if (mask &FAN_RENAME && !(fid_mode &FAN_REPORT_NAME )) +if (fanotify_mask_match (fan_mask,FAN_RENAME ) && + !(fid_mode &FAN_REPORT_NAME ))return -EINVAL ;/* Pre-content events are not currently generated for directories. */ -if (mask &FANOTIFY_PRE_CONTENT_EVENTS && mask &FAN_ONDIR ) +if ((fan_mask.mask &FAN_ONDIR ) && +fanotify_mask_match (fan_mask,FANOTIFY_PRE_CONTENT_EVENTS ))return -EINVAL ;if (mark_cmd ==FAN_MARK_FLUSH ) {@@ -2051,12 +2095,12 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, } ret =fanotify_find_path (dfd, pathname, &path, flags, - (mask &ALL_FSNOTIFY_EVENTS ), obj_type); + (fan_mask.mask &ALL_FSNOTIFY_EVENTS ), obj_type);if (ret)return ret;if (mark_cmd ==FAN_MARK_ADD ) { - ret =fanotify_events_supported (group, &path, mask, flags); + ret =fanotify_events_supported (group, &path, fan_mask, flags);if (ret) goto path_put_and_out; }@@ -2066,7 +2110,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,if (ret) goto path_put_and_out; - ret =security_path_notify (&path, mask, obj_type); + ret =security_path_notify (&path, fan_mask.mask, obj_type);if (ret) goto path_put_and_out;@@ -2134,7 +2178,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,/* Mask out FAN_EVENT_ON_CHILD flag for sb/mount/non-dir marks */ if (!inode || !S_ISDIR (inode->i_mode)) { - mask &= ~FAN_EVENT_ON_CHILD ; + fan_mask.mask &= ~FAN_EVENT_ON_CHILD ; umask =FAN_EVENT_ON_CHILD ;/* * If group needs to report parent fid, register for getting @@ -2142,18 +2186,18 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,*/ if ((fid_mode &FAN_REPORT_DIR_FID ) && (flags &FAN_MARK_ADD ) && !ignore) - mask |=FAN_EVENT_ON_CHILD ; + fan_mask.mask |=FAN_EVENT_ON_CHILD ; }/* create/update an inode mark */ switch (mark_cmd) {case FAN_MARK_ADD : - ret =fanotify_add_mark (group, obj, obj_type, mask, flags, + ret =fanotify_add_mark (group, obj, obj_type, fan_mask, flags, fsid);break ;case FAN_MARK_REMOVE : - ret =fanotify_remove_mark (group, obj, obj_type, mask, flags, - umask); + ret =fanotify_remove_mark (group, obj, obj_type, fan_mask.mask, + flags, umask);break ;default : ret = -EINVAL ; --2.55.0
Amir Goldstein
reviewerPATCH v3 10/14 fanotify: introduce FAN_REPORT_MNTID
In reply to Amir GoldsteinThis fanotify_init() flag is similar to FAN_REPORT_MNT, but instead of enabling the legacy FAN_MNT_* events, it enabled the new FAN_NS_MNT_* events which also report the watcher type bits in event->mask.
FAN_REPORT_MNT and FAN_REPORT_MNTID are mutually exclusive and therefore the group accepts either the legacy FAN_MNT_* events or the new FAN_NS_MNT_* events in fanotify_mark().
--- fs/notify/fanotify/fanotify_user.c | 21 ++++++++++++++------- include/linux/fanotify.h | 9 +++++++-- include/uapi/linux/fanotify.h | 3 ++- 3 files changed, 23 insertions(+), 10 deletions(-)
fs/notify/fanotify/fanotify_user.c +14 -7
@@ -852,6 +852,9 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group, metadata.vers =FANOTIFY_METADATA_VERSION ; metadata.reserved =0 ; metadata.mask =fanotify_group_outgoing_events (group, event->mask); +/* Report watcher type part of the event mask */ +if (FAN_GROUP_FLAG (group,FANOTIFY_REPORT_TYPE )) + metadata.mask |= (u64 )group->type <<FAN_EVENT_TYPE_SHIFT ; metadata.pid =pid_vnr (event->pid);/* * For an unprivileged listener, event->pid can be used to identify the
@@ -1669,6 +1672,9 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)return -EINVAL ;if (flags & (FANOTIFY_FID_BITS |FAN_REPORT_FD_ERROR ))return -EINVAL ; +/* The old and new mount event modes are mutually exclusive */ +if ((flags &FAN_REPORT_MNT ) && (flags &FAN_REPORT_MNTID )) +return -EINVAL ; }if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS )
@@ -1913,6 +1919,10 @@ static bool fanotify_is_valid_mask(struct fsnotify_group *group, int mark_type,BUILD_BUG_ON (FANOTIFY_EVENTS_ON_FILESYSTEM &FANOTIFY_EVENT_FLAGS );BUILD_BUG_ON (FANOTIFY_EVENTS_ON_NAMESPACE &FANOTIFY_EVENT_FLAGS ); +/* Only allow high bits in mask when reporting them in event->mask */ +if (!FAN_GROUP_FLAG (group,FANOTIFY_REPORT_TYPE ) && fan_mask.type) +return false; +switch (group->type) {case FSNOTIFY_GROUP_TYPE_FILESYSTEM : valid_mask = FANOTIFY_EVENTS_ON_FILESYSTEM |
@@ -1923,10 +1933,11 @@ static bool fanotify_is_valid_mask(struct fsnotify_group *group, int mark_type,case FSNOTIFY_GROUP_TYPE_NAMESPACE :/* Only report mount events on mntns mark */ if (mark_type ==FAN_MARK_MNTNS && -FAN_GROUP_FLAG (group,FAN_REPORT_MNT )) +FAN_GROUP_FLAG (group,FANOTIFY_MNT_MODES )) valid_mask =FANOTIFY_MOUNT_EVENTS ;/* Classify the legacy mount events as namespace events */ -if (fan_mask.mask & (FAN_MNT_ATTACH |FAN_MNT_DETACH )) +if (fan_mask.mask & (FAN_MNT_ATTACH |FAN_MNT_DETACH ) && +FAN_GROUP_FLAG (group,FAN_REPORT_MNT )) fan_mask.type =FSNOTIFY_GROUP_TYPE_NAMESPACE ;break ; }
@@ -1963,10 +1974,6 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 __mask,if (ret)return ret; -/* we only use the lower 32 bits as of right now. */ -if (fan_mask.type) -return -EINVAL ; -if (flags & ~FANOTIFY_MARK_FLAGS )return -EINVAL ;
@@ -2251,7 +2258,7 @@ static int __init fanotify_user_setup(void) FANOTIFY_DEFAULT_MAX_USER_MARKS);BUILD_BUG_ON (FANOTIFY_INIT_FLAGS &FANOTIFY_INTERNAL_GROUP_FLAGS ); -BUILD_BUG_ON (HWEIGHT32 (FANOTIFY_INIT_FLAGS ) !=14 ); +BUILD_BUG_ON (HWEIGHT32 (FANOTIFY_INIT_FLAGS ) !=15 );BUILD_BUG_ON (HWEIGHT32 (FANOTIFY_MARK_FLAGS ) !=11 ); fanotify_mark_cache = KMEM_CACHE(fanotify_mark,
include/linux/fanotify.h +7 -2
@@ -25,12 +25,17 @@#define FANOTIFY_FID_BITS (FAN_REPORT_DFID_NAME_TARGET) +#define FANOTIFY_MNT_MODES (FAN_REPORT_MNT | FAN_REPORT_MNTID) + +/* fanotify_init() flags that report watcher type in event mask */ +#define FANOTIFY_REPORT_TYPE (FAN_REPORT_MNTID) +/* fanotify_init() flags related to information reported with events */ #define FANOTIFY_INFO_MODES (FANOTIFY_FID_BITS | FAN_REPORT_PIDFD | \ - FAN_REPORT_MNT) + FANOTIFY_MNT_MODES)/* fanotify_init() flags that create a namespace watcher */ -#define FANOTIFY_NAMESPACE_INIT_FLAGS (FAN_REPORT_MNT) +#define FANOTIFY_NAMESPACE_INIT_FLAGS (FANOTIFY_MNT_MODES)/* * fanotify_init() flags that require CAP_SYS_ADMIN.
include/uapi/linux/fanotify.h +2 -1
@@ -90,7 +90,8 @@#define FAN_REPORT_NAME 0x00000800/* Report events with name */ #define FAN_REPORT_TARGET_FID 0x00001000/* Report dirent target id */ #define FAN_REPORT_FD_ERROR 0x00002000/* event->fd can report error */ -#define FAN_REPORT_MNT 0x00004000/* Report mount events */ +#define FAN_REPORT_MNT 0x00004000/* Report FAN_MNT_ events */ +#define FAN_REPORT_MNTID 0x00008000/* Report FAN_NS_MNT_ events */ /* Convenience macro - FAN_REPORT_NAME requires FAN_REPORT_DIR_FID */ #define FAN_REPORT_DFID_NAME (FAN_REPORT_DIR_FID | FAN_REPORT_NAME)
Signature · 1 line
2.55.0
Amir Goldstein
reviewerRe: [PATCH v3 10/14] fanotify: introduce FAN_REPORT_MNTID
Quoted 9 lines · Amir Goldstein
This
fanotify_init()flag is similar toFAN_REPORT_MNT, but instead of enabling the legacy FAN_MNT_* events, it enabled the new FAN_NS_MNT_* events which also report the watcher type bits inevent->mask.
FAN_REPORT_MNTandFAN_REPORT_MNTIDare mutually exclusive and therefore the group accepts either the legacy FAN_MNT_* events or the new FAN_NS_MNT_* events infanotify_mark().
FYI, I used the opportunity of this FAN_REPORT_MNT V2 to fix something that I found to be ugly in fdinfo:
} else if (mark->connector->type == FSNOTIFY_OBJ_TYPE_MNTNS) {
struct mnt_namespace *mnt_ns =fsnotify_conn_mntns(mark->connector);
- seq_printf(m, "fanotify mnt_ns:%u mflags:%x mask:%x
ignored_mask:%x\n",
- mnt_ns->ns.inum, mflags, mark->mask,mark->ignore_mask);
+ if (FAN_GROUP_FLAG(mark->group, FAN_REPORT_MNTID))
+ seq_printf(m, "fanotify mnt_ns_id:%llu ",
+ mnt_ns->ns.ns_id);
+ else
+ seq_printf(m, "fanotify mnt_ns:%u ", mnt_ns->ns.inum);
+ seq_printf(m, "mflags:%x mask:%x ignored_mask:%x\n",
+ mflags, mark->mask, mark->ignore_mask);
}pushed a new patch to fsnotify_group_type branch on my github.
Thanks,
Amir.
Amir Goldstein
reviewerPATCH v3 11/14 fanotify: introduce FAN_EVENT_MATCH() macro
In reply to Amir GoldsteinWith "strong typed" events a correctly written program needs to use the new macros FAN_EVENT_MATCH() FAN_EVENT_MASK_MATCH() to match the reported event mask in the scope of the watcher type.
--- include/uapi/linux/fanotify.h | 9 +++++++++ 1 file changed, 9 insertions(+)
include/uapi/linux/fanotify.h +9 -0
@@ -14,6 +14,11 @@#define FAN_EVENT_TYPE (mask) \ (((mask) & FAN_EVENT_TYPE_MASK) >> FAN_EVENT_TYPE_SHIFT) +/* Match event mask to any event in @event_set of the same watcher type */ +#define FAN_EVENT_MASK_MATCH (mask, event_set) \ + (FAN_EVENT_TYPE(mask) == FAN_EVENT_TYPE(event_set) && \ + ((mask) & (event_set) & ~FAN_EVENT_TYPE_MASK)) +/* * Events that user-space can request when watching filesystems */
@@ -296,4 +301,8 @@ struct fanotify_response_info_audit_rule { (long)(meta)->event_len >= (long)FAN_EVENT_METADATA_LEN && \ (long)(meta)->event_len <= (long)(len)) +/* Was one of the events in @event_set reported in the event? */ +#define FAN_EVENT_MATCH (meta, event_set) \ + FAN_EVENT_MASK_MATCH((meta)->mask, event_set) +#endif /* _UAPI_LINUX_FANOTIFY_H */
Signature · 1 line
2.55.0
Amir Goldstein
reviewerRe: [PATCH v3 11/14] fanotify: introduce FAN_EVENT_MATCH() macro
Quoted 27 lines · Amir Goldstein
With "strong typed" events a correctly written program needs to use the new macros
FAN_EVENT_MATCH()FAN_EVENT_MASK_MATCH()to match the reported event mask in the scope of the watcher type.Signed-off-by Amir Goldstein <[email protected]>--- include/uapi/linux/fanotify.h | 9 +++++++++ 1 file changed, 9 insertions(+)include/uapi/linux/fanotify.h +5
-0@@ -14,6 +14,11 @@#define FAN_EVENT_TYPE (mask) \ (((mask) & FAN_EVENT_TYPE_MASK) >> FAN_EVENT_TYPE_SHIFT) +/* Match event mask to any event in @event_set of the same watcher type */ +#define FAN_EVENT_MASK_MATCH (mask, event_set) \ + (FAN_EVENT_TYPE(mask) == FAN_EVENT_TYPE(event_set) && \ + ((mask) & (event_set) & ~FAN_EVENT_TYPE_MASK)) +/* * Events that user-space can request when watching filesystems */
include/uapi/linux/fanotify.h struct fanotify_response_info_audit_rule { · line 307
Quoted 7 lines
include/uapi/linux/fanotify.h +4
-0@@ -296,4 +301,8 @@ struct fanotify_response_info_audit_rule { (long)(meta)->event_len >= (long)FAN_EVENT_METADATA_LEN && \ (long)(meta)->event_len <= (long)(len)) +/* Was one of the events in @event_set reported in the event? */ +#define FAN_EVENT_MATCH (meta, event_set) \ + FAN_EVENT_MASK_MATCH((meta)->mask, event_set) +
FYI, I realized that a strong typed overflow event is wrong, so I added:
#define FAN_EVENT_OVERFLOW(meta) ((meta)->mask == FAN_Q_OVERFLOW)
And made sure that:
An event queue overflow is reported to groups of all watcher types without the watcher type bits, so the FAN_EVENT_OVERFLOW() macro can check for it on any group.
metadata.mask = fanotify_group_outgoing_events(group, event->mask);
+ /* Report watcher type of events that are not common to all types */
+ if (FAN_GROUP_FLAG(group, FANOTIFY_REPORT_TYPE) &&
+ metadata.mask & ~FANOTIFY_COMMON_EVENT_BITS)
+ metadata.mask |= (u64)group->type << FAN_EVENT_TYPE_SHIFT;
Pushed this to my github branch fsnotify_group_type
Thanks,
Amir.
Amir Goldstein
reviewerPATCH v3 12/14 selftests/filesystems: create fanotify test dir
In reply to Amir GoldsteinRename the dir mount-notify with two fanotify mount notify tests to fanotify before adding more fanotify tests.
---
tools/testing/selftests/Makefile | 2 +-
.../selftests/filesystems/{mount-notify => fanotify}/.gitignore | 0
.../selftests/filesystems/{mount-notify => fanotify}/Makefile | 0
.../filesystems/{mount-notify => fanotify}/mount-notify_test.c | 0
.../{mount-notify => fanotify}/mount-notify_test_ns.c | 0
5 files changed, 1 insertion(+), 1 deletion(-)
rename tools/testing/selftests/filesystems/{mount-notify => fanotify}/.gitignore (100%)
rename tools/testing/selftests/filesystems/{mount-notify => fanotify}/Makefile (100%)
rename tools/testing/selftests/filesystems/{mount-notify => fanotify}/mount-notify_test.c (100%)
rename tools/testing/selftests/filesystems/{mount-notify => fanotify}/mount-notify_test_ns.c (100%)tools/testing/selftests/Makefile +1 -1
@@ -40,7 +40,7 @@ TARGETS += filesystems/fat TARGETS += filesystems/file_stressor TARGETS += filesystems/overlayfs TARGETS += filesystems/statmount -TARGETS += filesystems/mount-notify +TARGETS += filesystems/fanotify TARGETS += filesystems/nsfs TARGETS += filesystems/fuse TARGETS += filesystems/move_mount
tools/testing/selftests/filesystems/fanotify/.gitignore renamed from tools/testing/selftests/filesystems/mount-notify/.gitignore +0 -0
tools/testing/selftests/filesystems/fanotify/Makefile renamed from tools/testing/selftests/filesystems/mount-notify/Makefile +0 -0
tools/testing/selftests/filesystems/fanotify/mount-notify_test.c renamed from tools/testing/selftests/filesystems/mount-notify/mount-notify_test.c +0 -0
tools/testing/selftests/filesystems/fanotify/mount-notify_test_ns.c renamed from tools/testing/selftests/filesystems/mount-notify/mount-notify_test_ns.c +0 -0
Signature · 1 line
2.55.0
Amir Goldstein
reviewerPATCH v3 13/14 selftests/fanotify: update the uapi tools header copy
In reply to Amir GoldsteinSync the "strong typed" event definitions.
--- tools/include/uapi/linux/fanotify.h | 100 +++++++++++++++++++--------- 1 file changed, 67 insertions(+), 33 deletions(-)
tools/include/uapi/linux/fanotify.h +67 -33
@@ -4,43 +4,72 @@#include <linux/types.h> -/* the following events that user-space can register for */ -#define FAN_ACCESS 0x00000001/* File was accessed */ -#define FAN_MODIFY 0x00000002/* File was modified */ -#define FAN_ATTRIB 0x00000004/* Metadata changed */ -#define FAN_CLOSE_WRITE 0x00000008/* Writable file closed */ -#define FAN_CLOSE_NOWRITE 0x00000010/* Unwritable file closed */ -#define FAN_OPEN 0x00000020/* File was opened */ -#define FAN_MOVED_FROM 0x00000040/* File was moved from X */ -#define FAN_MOVED_TO 0x00000080/* File was moved to Y */ -#define FAN_CREATE 0x00000100/* Subfile was created */ -#define FAN_DELETE 0x00000200/* Subfile was deleted */ -#define FAN_DELETE_SELF 0x00000400/* Self was deleted */ -#define FAN_MOVE_SELF 0x00000800/* Self was moved */ -#define FAN_OPEN_EXEC 0x00001000/* File was opened for exec */ - -#define FAN_Q_OVERFLOW 0x00004000/* Event queued overflowed */ -#define FAN_FS_ERROR 0x00008000/* Filesystem error */ - -#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_PRE_ACCESS 0x00100000/* Pre-content access hook */ -#define FAN_MNT_ATTACH 0x01000000/* Mount was attached */ -#define FAN_MNT_DETACH 0x02000000/* Mount was detached */ - -#define FAN_EVENT_ON_CHILD 0x08000000/* Interested in child events */ - -#define FAN_RENAME 0x10000000/* File was renamed */ - -#define FAN_ONDIR 0x40000000/* Event occurred against dir */ +/* +* Bits 32..39 of the 64-bit event mask encodes the watcher type. +* Filesystem events have type 0, so legacy event constants are +* filesystem events. +*/ +#define FAN_EVENT_TYPE_SHIFT 32 +#define FAN_EVENT_TYPE_MASK (0xffULL << FAN_EVENT_TYPE_SHIFT) +#define FAN_EVENT_TYPE (mask) \ + (((mask) & FAN_EVENT_TYPE_MASK) >> FAN_EVENT_TYPE_SHIFT) + +/* Match event mask to any event in @event_set of the same watcher type */ +#define FAN_EVENT_MASK_MATCH (mask, event_set) \ + (FAN_EVENT_TYPE(mask) == FAN_EVENT_TYPE(event_set) && \ + ((mask) & (event_set) & ~FAN_EVENT_TYPE_MASK)) + +/* +* Events that user-space can request when watching filesystems +*/ +#define FAN_ACCESS 0x0000000001UL/* File was accessed */ +#define FAN_MODIFY 0x0000000002UL/* File was modified */ +#define FAN_ATTRIB 0x0000000004UL/* Metadata changed */ +#define FAN_CLOSE_WRITE 0x0000000008UL/* Writable file closed */ +#define FAN_CLOSE_NOWRITE 0x0000000010UL/* Unwritable file closed */ +#define FAN_OPEN 0x0000000020UL/* File was opened */ +#define FAN_MOVED_FROM 0x0000000040UL/* File was moved from X */ +#define FAN_MOVED_TO 0x0000000080UL/* File was moved to Y */ +#define FAN_CREATE 0x0000000100UL/* Subfile was created */ +#define FAN_DELETE 0x0000000200UL/* Subfile was deleted */ +#define FAN_DELETE_SELF 0x0000000400UL/* Self was deleted */ +#define FAN_MOVE_SELF 0x0000000800UL/* Self was moved */ +#define FAN_OPEN_EXEC 0x0000001000UL/* File was opened for exec */ + +#define FAN_Q_OVERFLOW 0x0000004000UL/* Event queued overflowed */ +#define FAN_FS_ERROR 0x0000008000UL/* Filesystem error */ + +#define FAN_OPEN_PERM 0x0000010000UL/* File open in perm check */ +#define FAN_ACCESS_PERM 0x0000020000UL/* File accessed in perm check */ +#define FAN_OPEN_EXEC_PERM 0x0000040000UL/* File open/exec in perm check */ +/* #define FAN_DIR_MODIFY 0x0000080000UL *//* Deprecated (reserved) */ + +#define FAN_PRE_ACCESS 0x0000100000UL/* Pre-content access hook */ + +#define FAN_RENAME 0x0010000000UL/* File was renamed */ /* helper events */ #define FAN_CLOSE (FAN_CLOSE_WRITE | FAN_CLOSE_NOWRITE)/* close */ #define FAN_MOVE (FAN_MOVED_FROM | FAN_MOVED_TO)/* moves */ +/* +* Filter flags for watching filesystems +*/ +#define FAN_EVENT_ON_CHILD 0x0008000000UL/* Interested in child events */ +#define FAN_ONDIR 0x0040000000UL/* Event occurred against dir */ + +/* +* Events that user-space can request when watching namespaces +* +* NOTE: Lower 32bit values may overload filesystem events, but not event flags +*/ +/* Backward compat mount events - cannot be mixed with other events */ +#define FAN_MNT_ATTACH 0x0001000000UL/* Mount was attached */ +#define FAN_MNT_DETACH 0x0002000000UL/* Mount was detached */ +/* "Strong typed" mount events - may be mixed with other namespace events */ +#define FAN_NS_MNT_ATTACH 0x0101000000UL/* Mount was attached */ +#define FAN_NS_MNT_DETACH 0x0102000000UL/* Mount was detached */ +/* flags used for fanotify_init() */ #define FAN_CLOEXEC 0x00000001#define FAN_NONBLOCK 0x00000002
@@ -66,7 +95,8 @@#define FAN_REPORT_NAME 0x00000800/* Report events with name */ #define FAN_REPORT_TARGET_FID 0x00001000/* Report dirent target id */ #define FAN_REPORT_FD_ERROR 0x00002000/* event->fd can report error */ -#define FAN_REPORT_MNT 0x00004000/* Report mount events */ +#define FAN_REPORT_MNT 0x00004000/* Report FAN_MNT_ events */ +#define FAN_REPORT_MNTID 0x00008000/* Report FAN_NS_MNT_ events */ /* Convenience macro - FAN_REPORT_NAME requires FAN_REPORT_DIR_FID */ #define FAN_REPORT_DFID_NAME (FAN_REPORT_DIR_FID | FAN_REPORT_NAME)
@@ -271,4 +301,8 @@ struct fanotify_response_info_audit_rule { (long)(meta)->event_len >= (long)FAN_EVENT_METADATA_LEN && \ (long)(meta)->event_len <= (long)(len)) +/* Was one of the events in @event_set reported in the event? */ +#define FAN_EVENT_MATCH (meta, event_set) \ + FAN_EVENT_MASK_MATCH((meta)->mask, event_set) +#endif /* _UAPI_LINUX_FANOTIFY_H */
Signature · 1 line
2.55.0
Amir Goldstein
reviewerPATCH v3 14/14 selftests/fanotify: test the FAN_REPORT_MNTID mode
In reply to Amir GoldsteinThis mode accepts the FAN_NS_MNT_* events in mask and reports these events in event->mask.
Verify that FAN_REPORT_MNTID mode rejects legacy FAN_MNT_* events and verify that FAN_REPORT_MNT mode rejects FAN_NS_MNT_* events.
--- .../filesystems/fanotify/mount-notify_test.c | 51 +++++++++++++++--- .../fanotify/mount-notify_test_ns.c | 54 +++++++++++++++---- 2 files changed, 89 insertions(+), 16 deletions(-)
tools/testing/selftests/filesystems/fanotify/mount-notify_test.c +44 -7
@@ -42,12 +42,36 @@ FIXTURE(fanotify) {int orig_root;int ns_fd;uint64_t root_id; +uint64_t events_mask; +}; + +FIXTURE_VARIANT (fanotify) { +uint64_t events_mask; +}; + +FIXTURE_VARIANT_ADD(fanotify, mnt) { + .events_mask =FAN_MNT_ATTACH |FAN_MNT_DETACH , +}; + +FIXTURE_VARIANT_ADD(fanotify, mntid) { + .events_mask = FAN_NS_MNT_ATTACH | FAN_NS_MNT_DETACH, };FIXTURE_SETUP (fanotify) {int i, ret; +if (variant->events_mask &FAN_EVENT_TYPE_MASK ) { +int fd; + + fd =fanotify_init (FAN_REPORT_MNTID |FAN_NONBLOCK ,0 ); +if (fd <0 ) { +ASSERT_EQ (errno,EINVAL ); +SKIP (return,"FAN_REPORT_MNTID not supported" ); + } +close (fd); + } +ASSERT_EQ (unshare (CLONE_NEWNS ),0 ); self->ns_fd =open ("/proc/self/ns/mnt" ,O_RDONLY );
@@ -74,22 +98,33 @@ FIXTURE_SETUP(fanotify) self->root_id =get_unique_mnt_id ("/" );ASSERT_NE (self->root_id,0 ); +uint64_t ns_event_type =FAN_NS_MNT_ATTACH &FAN_EVENT_TYPE_MASK ; + + self->events_mask = variant->events_mask; +for (i =0 ; i <NUM_FAN_FDS ; i++) { - self->fan_fd[i] =fanotify_init (FAN_REPORT_MNT |FAN_NONBLOCK , -0 ); + self->fan_fd[i] =fanotify_init (FAN_NONBLOCK | + (self->events_mask & +FAN_EVENT_TYPE_MASK ? +FAN_REPORT_MNTID : +FAN_REPORT_MNT ),0 );ASSERT_GE (self->fan_fd[i],0 ); ret =fanotify_mark (self->fan_fd[i],FAN_MARK_ADD | -FAN_MARK_MNTNS , -FAN_MNT_ATTACH |FAN_MNT_DETACH , +FAN_MARK_MNTNS , self->events_mask, self->ns_fd,NULL );ASSERT_EQ (ret,0 );// On fd[0] we do an extra ADD that changes nothing. // On fd[1]/fd[2] we REMOVE/FLUSH which removes the mark. ret =fanotify_mark (self->fan_fd[i], mark_cmds[i] | -FAN_MARK_MNTNS , -FAN_MNT_ATTACH |FAN_MNT_DETACH , +FAN_MARK_MNTNS , self->events_mask, self->ns_fd,NULL );ASSERT_EQ (ret,0 ); +/* The other watcher type is not allowed on this group */ + ret =fanotify_mark (self->fan_fd[i],FAN_MARK_ADD | +FAN_MARK_MNTNS , + self->events_mask ^ ns_event_type, + self->ns_fd,NULL ); +ASSERT_NE (ret,0 ); } self->rem =0 ;
@@ -149,7 +184,9 @@ static uint64_t expect_notify(struct __test_metadata *const _metadata, self->rem -= thislen; self->next += thislen; - *mask = meta->mask; +ASSERT_EQ (meta->mask &FAN_EVENT_TYPE_MASK , + self->events_mask &FAN_EVENT_TYPE_MASK ); + *mask = meta->mask & ~FAN_EVENT_TYPE_MASK ; thislen -=sizeof (*meta); mnt = ((void *) meta) + meta->event_len - thislen;
tools/testing/selftests/filesystems/fanotify/mount-notify_test_ns.c +45 -9
98 lines not on this page · raw message
Signature · 1 line
2.55.0
Amir Goldstein
reviewerQuoted 17 lines · Amir Goldstein
Jan,
As discussed on the "namespace monitoring" patch set [1], the proposed namespace events do not need to be mixed on the same group with legacy fanotify filesystem events.
The mount events are already not mixable with filesystem events, but we want to be able to mix them with the new namespace events.
Therefore, introduce the concept of "watcher type" where all the legacy events belong to the filesystem watcher type and the new mount events belong to a new namespace watcher type.
The new watcher type is free to reuse most of the 32bit mask space that we have internally in fsnotify for event masks.
Quoted 9 lines
The prep patch set only does the partitioning by watcher type. Followup namepsace events work in available for review in my tree [2].
If you are fine with this plan and have time to review the patches, perhaps it is not too late to stage this prep series for next cycle?
Otherwise, we could aim for merging this together with the namespace events for the following cycle.
Hi Jan,
I suppose with the firehose of bugs you had not had a chance to look at this yet.
Just making sure it didn't fall between the cracks.
Thanks,
Amir.
Quoted 48 lines
[1] https://lore.kernel.org/linux-fsdevel/[email protected]/
[2] https://github.com/amir73il/linux/commits/fan_mark_usernsAmir Goldstein (14):
fsnotify: rename fsnotify group flag macros
fsnotify: introduce fsnotify group types
fsnotify: separate the events bitmask macros by group type
fanotify: testevent->typeinstead of event mask when possible
fsnotify: do not report mount events withfsnotify()
fanotify: gate filesystem event classification by group type
fanotify: define event mask constants as 64bit
fanotify: define new FAN_NS_MNT_* constants
fanotify: gate event mask infanotify_mark()by group type
fanotify: introduceFAN_REPORT_MNTID
fanotify: introduceFAN_EVENT_MATCH()macro selftests/filesystems: create fanotify test dir selftests/fanotify: update the uapi tools header copy selftests/fanotify: test theFAN_REPORT_MNTIDmodefs/notify/fanotify/fanotify.c | 186 +++++++++----- fs/notify/fanotify/fanotify.h | 38 ++- fs/notify/fanotify/fanotify_user.c | 230 ++++++++++++------ fs/notify/fsnotify.c | 106 +++++--- fs/notify/group.c | 15 +- fs/notify/inotify/inotify_user.c | 2 +- fs/notify/mark.c | 2 +- include/linux/fanotify.h | 45 +++- include/linux/fsnotify.h | 6 +- include/linux/fsnotify_backend.h | 83 +++++-- include/trace/misc/fsnotify.h | 4 +- include/uapi/linux/fanotify.h | 100 +++++--- kernel/audit_fsnotify.c | 2 +- tools/include/uapi/linux/fanotify.h | 100 +++++--- tools/testing/selftests/Makefile | 2 +- .../{mount-notify => fanotify}/.gitignore | 0 .../{mount-notify => fanotify}/Makefile | 0 .../mount-notify_test.c | 51 +++- .../mount-notify_test_ns.c | 54 +++- 19 files changed, 707 insertions(+), 319 deletions(-) rename tools/testing/selftests/filesystems/{mount-notify => fanotify}/.gitignore (100%) rename tools/testing/selftests/filesystems/{mount-notify => fanotify}/Makefile (100%) rename tools/testing/selftests/filesystems/{mount-notify => fanotify}/mount-notify_test.c (90%) rename tools/testing/selftests/filesystems/{mount-notify => fanotify}/mount-notify_test_ns.c (90%)--
2.55.0
Amir Goldstein
reviewerQuoted 29 lines · Amir Goldstein
Quoted 17 lines · Amir Goldstein
Jan,
As discussed on the "namespace monitoring" patch set [1], the proposed namespace events do not need to be mixed on the same group with legacy fanotify filesystem events.
The mount events are already not mixable with filesystem events, but we want to be able to mix them with the new namespace events.
Therefore, introduce the concept of "watcher type" where all the legacy events belong to the filesystem watcher type and the new mount events belong to a new namespace watcher type.
The new watcher type is free to reuse most of the 32bit mask space that we have internally in fsnotify for event masks.
Quoted 9 lines
The prep patch set only does the partitioning by watcher type. Followup namepsace events work in available for review in my tree [2].
If you are fine with this plan and have time to review the patches, perhaps it is not too late to stage this prep series for next cycle?
Otherwise, we could aim for merging this together with the namespace events for the following cycle.
Quoted 6 lines
Hi Jan,
I suppose with the firehose of bugs you had not had a chance to look at this yet.
Just making sure it didn't fall between the cracks.
Anyway, I addressed some review comments from AI tools. Besides some issues that I already mentioned on specific patches, fixed sparse warnings reported by the kbuild robot and some other minor cleanups and pushed to fsnotify_group_type branch on github.
I won't send v4 for those minor changes because I first want to know that you are ok with the shape that this took since your v2 review.
Thanks,
Amir.
Jan Kara
maintainerQuoted 37 lines · Amir Goldstein
Quoted 29 lines · Amir Goldstein
Quoted 17 lines · Amir Goldstein
Jan,
As discussed on the "namespace monitoring" patch set [1], the proposed namespace events do not need to be mixed on the same group with legacy fanotify filesystem events.
The mount events are already not mixable with filesystem events, but we want to be able to mix them with the new namespace events.
Therefore, introduce the concept of "watcher type" where all the legacy events belong to the filesystem watcher type and the new mount events belong to a new namespace watcher type.
The new watcher type is free to reuse most of the 32bit mask space that we have internally in fsnotify for event masks.
Quoted 9 lines
The prep patch set only does the partitioning by watcher type. Followup namepsace events work in available for review in my tree [2].
If you are fine with this plan and have time to review the patches, perhaps it is not too late to stage this prep series for next cycle?
Otherwise, we could aim for merging this together with the namespace events for the following cycle.
Quoted 6 lines
Hi Jan,
I suppose with the firehose of bugs you had not had a chance to look at this yet.
Just making sure it didn't fall between the cracks.
Quoted 7 lines
Anyway, I addressed some review comments from AI tools. Besides some issues that I already mentioned on specific patches, fixed sparse warnings reported by the kbuild robot and some other minor cleanups and pushed to
fsnotify_group_typebranch on github.I won't send v4 for those minor changes because I first want to know that you are ok with the shape that this took since your v2 review.
Yes, I'm aware of this patch set and I'm sorry for the delay but as you guessed this is getting pushed out by other stuff that keeps popping up. But I'm hopeful this week I'll have a look.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR