fanotify namespace monitoring
Subsystems (7)
Amir Goldstein
reviewerv2 cover fanotify namespace monitoring
Jan,
Following your feedback from v1 [1] review, I've made the changes to clear the way for reusing the fs watcher event bits for ns watcher event bits.
The terminology of "ns watcher" vs. "ns events" is a little confusing:
- "ns watcher" group can place mark on ns objects with mntns/userns marks
- The events that can be requested by ns watcher are
mount (tree monitoring) and ns (tree monitoring) events
- We could imagine requesting all mount events of all mntns owned by a specific userns, but this was not implemented
- "fs watcher" group can place mark on fs objects with inode/mnt/sb marks
- The events that can be requested by fs watcher are
fs (monitoring, permission and pre-content) events
To simplify the implementation, the event flags (ON_CHILD, ISDIR) live in a shared space that cannot be overloaded by neither group types. This is not because ISDIR makes sense for ns watcher, just to reduce the number of gates in common code. ON_CHILD flag might be usable for ns watchers, not sure.
Thanks,
Amir.
Changes since v1:
- Introduce group type and gates
- FAN_NS_CREATE/FAN_NS_DELETE overload FAN_CREATE/FAN_DELETE in uapi instead of using high 32bit
[1] https://lore.kernel.org/linux-fsdevel/[email protected]/
Amir Goldstein (10):
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 fs event classification by group type
fanotify: gate fs events checks in fanotify_mark() by group type
fanotify: add support for watching the namespaces tree selftests/filesystems: create fanotify test dir selftests/filesystems: add fanotify namespace notifications test
fs/notify/fanotify/fanotify.c | 141 ++++++--
fs/notify/fanotify/fanotify.h | 62 +++-
fs/notify/fanotify/fanotify_user.c | 218 +++++++++---
fs/notify/fdinfo.c | 9 +-
fs/notify/fsnotify.c | 123 +++++--
fs/notify/fsnotify.h | 12 +
fs/notify/group.c | 14 +-
fs/notify/inotify/inotify_user.c | 2 +-
fs/notify/mark.c | 9 +-
fs/nsfs.c | 21 ++
include/linux/fanotify.h | 40 ++-
include/linux/fsnotify.h | 5 +
include/linux/fsnotify_backend.h | 108 ++++--
include/linux/proc_fs.h | 2 +
include/linux/user_namespace.h | 6 +
include/uapi/linux/fanotify.h | 37 +-
kernel/audit_fsnotify.c | 2 +-
kernel/nscommon.c | 47 +++
kernel/user_namespace.c | 2 +
tools/include/uapi/linux/fanotify.h | 37 +-
tools/testing/selftests/Makefile | 2 +-
.../{mount-notify => fanotify}/.gitignore | 0
.../{mount-notify => fanotify}/Makefile | 3 +-
.../mount-notify_test.c | 0
.../mount-notify_test_ns.c | 0
.../filesystems/fanotify/ns-notify_test.c | 330 ++++++++++++++++++
26 files changed, 1045 insertions(+), 187 deletions(-)
rename tools/testing/selftests/filesystems/{mount-notify => fanotify}/.gitignore (100%)
rename tools/testing/selftests/filesystems/{mount-notify => fanotify}/Makefile (67%)
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%)
create mode 100644 tools/testing/selftests/filesystems/fanotify/ns-notify_test.cSignature · 1 line
2.54.0
Amir Goldstein
reviewerPATCH v2 1/10 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
@@ -1682,7 +1682,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
@@ -657,7 +657,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
@@ -908,7 +908,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
@@ -184,7 +184,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.54.0
Amir Goldstein
reviewerPATCH v2 2/10 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_NS for the mount event groups.
Create helpers fsnotify_is_{fs,ns}_watcher() and the macro FANOTIFY_NS_INIT_FLAGS and use them to generalize some code that was checking the FAN_REPORT_MNT flag.
--- fs/notify/fanotify/fanotify.c | 4 ++- fs/notify/fanotify/fanotify_user.c | 40 ++++++++++++++++++++++-------- fs/notify/group.c | 10 +++++--- include/linux/fanotify.h | 8 ++++-- include/linux/fsnotify_backend.h | 24 ++++++++++++++++++ 5 files changed, 68 insertions(+), 18 deletions(-)
fs/notify/fanotify/fanotify.c +3 -1
@@ -314,9 +314,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_ns_watcher (group)) {if (data_type !=FSNOTIFY_EVENT_MNT )return 0 ; + }else if (WARN_ON_ONCE (!fsnotify_is_fs_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 +29 -11
@@ -1586,8 +1586,11 @@ 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, +FSNOTIFY_GROUP_FLAG_USER , +GFP_KERNEL_ACCOUNT ), + const struct fsnotify_ops *ops, + enum fsnotify_group_type type) /* fanotify syscalls */ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
@@ -1597,18 +1600,22 @@ 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_NS_INIT_FLAGS ) ? +FSNOTIFY_GROUP_TYPE_NS : +FSNOTIFY_GROUP_TYPE_FS ;pr_debug ("%s: flags=%x event_f_flags=%x\n" , __func__, flags, event_f_flags);/* * An unprivileged user can setup an fanotify group with limited -* functionality - an unprivileged group is limited to notification -* events with file handles or mount ids and it cannot use unlimited +* functionality - an unprivileged group cannot receive filesystem +* notification events with file descriptors and cannot use unlimited * queue/marks. */ if (((flags &FANOTIFY_ADMIN_INIT_FLAGS ) || - !(flags & (FANOTIFY_FID_BITS |FAN_REPORT_MNT ))) && + !(flags & (FANOTIFY_FID_BITS |FANOTIFY_NS_INIT_FLAGS ))) && !capable (CAP_SYS_ADMIN ))return -EPERM ;
@@ -1636,8 +1643,11 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)if ((flags &FAN_REPORT_PIDFD ) && (flags &FAN_REPORT_TID ))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_NS ) {if (class !=FAN_CLASS_NOTIF )return -EINVAL ;if (flags & (FANOTIFY_FID_BITS |FAN_REPORT_FD_ERROR ))
@@ -1681,8 +1691,7 @@ 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_alloc_group takes a ref. Dropped in fanotify_release */ if (IS_ERR (group))return PTR_ERR (group);
@@ -1885,6 +1894,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 group_type;unsigned int obj_type, fid_mode;void *obj =NULL ;u32 umask =0 ;
@@ -1903,15 +1913,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 ; + group_type =FSNOTIFY_GROUP_TYPE_FS ;break ;case FAN_MARK_MOUNT : obj_type =FSNOTIFY_OBJ_TYPE_VFSMOUNT ; + group_type =FSNOTIFY_GROUP_TYPE_FS ;break ;case FAN_MARK_FILESYSTEM : obj_type =FSNOTIFY_OBJ_TYPE_SB ; + group_type =FSNOTIFY_GROUP_TYPE_FS ;break ;case FAN_MARK_MNTNS : obj_type =FSNOTIFY_OBJ_TYPE_MNTNS ; + group_type =FSNOTIFY_GROUP_TYPE_NS ;break ;default :return -EINVAL ;
@@ -1960,6 +1974,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 != group_type) +return -EINVAL ; +/* Only report mount events on mnt namespace */ if (FAN_GROUP_FLAG (group,FAN_REPORT_MNT )) {if (mask & ~FANOTIFY_MOUNT_EVENTS )
@@ -2005,14 +2022,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_fs_watcher (group) && + mask & ~(FANOTIFY_FD_EVENTS |FANOTIFY_EVENT_FLAGS ) && (!fid_mode || mark_type ==FAN_MARK_MOUNT ))return -EINVAL ;
fs/notify/group.c +6 -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,7 @@ 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_FS , flags, gfp); }EXPORT_SYMBOL_GPL (fsnotify_alloc_group);
include/linux/fanotify.h +6 -2
@@ -25,7 +25,10 @@#define FANOTIFY_FID_BITS (FAN_REPORT_DFID_NAME_TARGET) -#define FANOTIFY_INFO_MODES (FANOTIFY_FID_BITS | FAN_REPORT_PIDFD | FAN_REPORT_MNT) +#define FANOTIFY_INFO_MODES (FANOTIFY_FID_BITS | FAN_REPORT_PIDFD) + +/* fanotify_init() flags to create a namepsace event watcher */ +#define FANOTIFY_NS_INIT_FLAGS (FAN_REPORT_MNT)/* * fanotify_init() flags that require CAP_SYS_ADMIN.
@@ -47,7 +50,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_NS_INIT_FLAGS | \ FAN_CLOEXEC | FAN_NONBLOCK)#define FANOTIFY_INIT_FLAGS (FANOTIFY_ADMIN_INIT_FLAGS | \
include/linux/fsnotify_backend.h +24 -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_FS =0 , +FSNOTIFY_GROUP_TYPE_NS , +}; +/* * 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;/* category 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,16 @@ struct fsnotify_group { }; }; +static inline bool fsnotify_is_fs_watcher (const struct fsnotify_group *group) +{ +return group->type ==FSNOTIFY_GROUP_TYPE_FS ; +} + +static inline bool fsnotify_is_ns_watcher (const struct fsnotify_group *group) +{ +return group->type ==FSNOTIFY_GROUP_TYPE_NS ; +} +/* * These helpers are used to prevent deadlock when reclaiming inodes with * evictable marks of the same group that is allocating a new mark.
@@ -707,6 +727,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.54.0
Jan Kara
maintainerRe: [PATCH v2 02/10] fsnotify: introduce fsnotify group types
Quoted 16 lines · Amir Goldstein
Currently an fanotify group can subscribe to events FAN_MNT_ATTACH/DETACH only on mark type
FAN_MARK_MNTNSand IFF the group was initialized withFAN_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_typewhich describes the category of events and mark types and use a distinct categoryFSNOTIFY_GROUP_TYPE_NSfor the mount event groups.Create helpers fsnotify_is_{fs,ns}_watcher() and the macro
FANOTIFY_NS_INIT_FLAGSand use them to generalize some code that was checking theFAN_REPORT_MNTflag.Signed-off-by Amir Goldstein <[email protected]>
...
Quoted 5 lines
fs/notify/fanotify/fanotify.c +0
-0
fs/notify/fanotify/fanotify.c static u32 fanotify_group_event_mask(struct fsnotify_group *group, · line 321
Quoted 9 lines
fs/notify/fanotify/fanotify.c +3
-1@@ -314,9 +314,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_ns_watcher (group)) {if (data_type !=FSNOTIFY_EVENT_MNT )return 0 ; + }else if (WARN_ON_ONCE (!fsnotify_is_fs_watcher (group))) { +return 0 ;
ns_watcher and fs_watcher names are really very similar. I was staring at that for a while trying to understand what you're doing here before I noticed that single letter difference. I'm struggling to find better names. Maybe fsnotify_namespace_watcher() and fsnotify_filesystem_watcher() and correspondingly FSNOTIFY_GROUP_TYPE_FILESYSTEM / NAMESPACE?
Quoted 5 lines
fs/notify/fanotify/fanotify_user.c +0
-0
fs/notify/fanotify/fanotify_user.c static struct hlist_head *fanotify_alloc_merge_hash(void) · line 1593
Quoted 10 lines
fs/notify/fanotify/fanotify_user.c +5
-2@@ -1586,8 +1586,11 @@ 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, +FSNOTIFY_GROUP_FLAG_USER , +GFP_KERNEL_ACCOUNT ), + const struct fsnotify_ops *ops, + enum fsnotify_group_type type)
I don't like this much. It's a bit of a catch that FSNOTIFY_GROUP_FLAG_USER is now always set (although I see that that's what the only user is doing). I'd keep this as is.
Quoted 7 lines
fs/notify/fanotify/fanotify_user.c +1
-1@@ -2005,14 +2022,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
fs/notify/fanotify/fanotify_user.c static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, · line 2035
Quoted 10 lines
fs/notify/fanotify/fanotify_user.c +2
-1* 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_fs_watcher (group) && + mask & ~(FANOTIFY_FD_EVENTS |FANOTIFY_EVENT_FLAGS ) && (!fid_mode || mark_type ==FAN_MARK_MOUNT ))return -EINVAL ;
I guess it would look more obvious here to switch on mark_type and validate that the set event bits match the mark_type (and fid_mode).
fs/notify/fanotify/fanotify_user.c static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
Quoted 7 lines
fs/notify/fanotify/fanotify_user.c +3
-3-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) {
Can't we just derive the group type from flags in this function? I don't see a huge reason to duplicate that info in flags argument.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Amir Goldstein
reviewerRe: [PATCH v2 02/10] fsnotify: introduce fsnotify group types
Quoted 29 lines · Jan Kara
Quoted 16 lines · Amir Goldstein
Currently an fanotify group can subscribe to events FAN_MNT_ATTACH/DETACH only on mark type
FAN_MARK_MNTNSand IFF the group was initialized withFAN_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_typewhich describes the category of events and mark types and use a distinct categoryFSNOTIFY_GROUP_TYPE_NSfor the mount event groups.Create helpers fsnotify_is_{fs,ns}_watcher() and the macro
FANOTIFY_NS_INIT_FLAGSand use them to generalize some code that was checking theFAN_REPORT_MNTflag.Signed-off-by Amir Goldstein <[email protected]>...
Quoted 8 lines
fs/notify/fanotify/fanotify.c +0
-0@@ -314,9 +314,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);
Quoted 12 lines
fs/notify/fanotify/fanotify.c static u32 fanotify_group_event_mask(struct fsnotify_group *group, · line 321
Quoted 6 lines
fs/notify/fanotify/fanotify.c +3
-1-if (FAN_GROUP_FLAG (group,FAN_REPORT_MNT )) { +if (fsnotify_is_ns_watcher (group)) {if (data_type !=FSNOTIFY_EVENT_MNT )return 0 ; + }else if (WARN_ON_ONCE (!fsnotify_is_fs_watcher (group))) { +return 0 ;
ns_watcherandfs_watchernames are really very similar. I was staring at that for a while trying to understand what you're doing here before I noticed that single letter difference. I'm struggling to find better names. Maybefsnotify_namespace_watcher()andfsnotify_filesystem_watcher()and correspondinglyFSNOTIFY_GROUP_TYPE_FILESYSTEM/ NAMESPACE?
Yes, I agree. This is less confusing.
Quoted 12 lines
Quoted 12 lines
fs/notify/fanotify/fanotify_user.c +1
-2@@ -1586,8 +1586,11 @@ 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,
Quoted 9 lines
fs/notify/fanotify/fanotify_user.c static struct hlist_head *fanotify_alloc_merge_hash(void) · line 1593
Quoted 4 lines
fs/notify/fanotify/fanotify_user.c +4
-0+ FSNOTIFY_GROUP_FLAG_USER, + GFP_KERNEL_ACCOUNT), +const struct fsnotify_ops *ops, + enum fsnotify_group_type type)I don't like this much. It's a bit of a catch that
FSNOTIFY_GROUP_FLAG_USERis now always set (although I see that that's what the only user is doing). I'd keep this as is.
Sorry, you mean keep it as a constant flag? or pass the flag as an argument of CLASS fsnotify_group?
We can also make it DEFINE_CLASS(fsnotify_user_group,
and then the constant flag will not look as awkward?
Quoted 12 lines
Quoted 12 lines
fs/notify/fanotify/fanotify_user.c +1
-1@@ -2005,14 +2022,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 );
Quoted 9 lines
fs/notify/fanotify/fanotify_user.c static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, · line 2035
Quoted 5 lines
fs/notify/fanotify/fanotify_user.c +2
-1-if (mask & ~(FANOTIFY_FD_EVENTS |FANOTIFY_MOUNT_EVENTS |FANOTIFY_EVENT_FLAGS ) && +if (fsnotify_is_fs_watcher (group) && + mask & ~(FANOTIFY_FD_EVENTS |FANOTIFY_EVENT_FLAGS ) && (!fid_mode || mark_type ==FAN_MARK_MOUNT ))return -EINVAL ;I guess it would look more obvious here to switch on
mark_typeand validate that the set event bits match themark_type(andfid_mode).
I will try that.
Quoted 10 lines
fs/notify/fanotify/fanotify_user.c static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
Quoted 7 lines
fs/notify/fanotify/fanotify_user.c +3
-3-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) {Can't we just derive the group type from flags in this function? I don't see a huge reason to duplicate that info in flags argument.
I am not sure I am following.
These are FSNOTIFY_GROUP_FLAG_* flags not the fanotify_init() flags. Did you mean to add a temporary flag FSNOTIFY_GROUP_FLAG_NAMESPACE just in order to save the extra type argument?
I did not want to encode fanotify logic in fsnotify generic code if that is what you meant.
Thanks,
Amir.
Jan Kara
maintainerRe: [PATCH v2 02/10] fsnotify: introduce fsnotify group types
Quoted 21 lines · Amir Goldstein
Quoted 11 lines · Jan Kara
Quoted 11 lines
fs/notify/fanotify/fanotify_user.c +1
-2@@ -1586,8 +1586,11 @@ 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,Quoted 8 lines
fs/notify/fanotify/fanotify_user.c static struct hlist_head *fanotify_alloc_merge_hash(void) · line 1593
Quoted 4 lines
fs/notify/fanotify/fanotify_user.c +4
-0+ FSNOTIFY_GROUP_FLAG_USER, + GFP_KERNEL_ACCOUNT), +const struct fsnotify_ops *ops, + enum fsnotify_group_type type)I don't like this much. It's a bit of a catch that
FSNOTIFY_GROUP_FLAG_USERis now always set (although I see that that's what the only user is doing). I'd keep this as is.
Quoted 2 lines
Sorry, you mean keep it as a constant flag? or pass the flag as an argument of CLASS
fsnotify_group?
I meant keep passing the flag FSNOTIFY_GROUP_USER as explicit argument when declaring fsnotify_group.
Quoted 2 lines
We can also make it DEFINE_CLASS(fsnotify_user_group, and then the constant flag will not look as awkward?
Yeah, possible as well, although I don't quite see the point with the single declaration site we currently have. But at least with this name it will be less surprising.
Quoted 11 lines
Quoted 10 lines
Quoted 7 lines
fs/notify/fanotify/fanotify_user.c +3
-3-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){
Can't we just derive the group type from flags in this function? I don't see a huge reason to duplicate that info in flags argument.
Quoted 4 lines
I am not sure I am following.
These are FSNOTIFY_GROUP_FLAG_* flags not thefanotify_init()flags. Did you mean to add a temporary flagFSNOTIFY_GROUP_FLAG_NAMESPACEjust in order to save the extra type argument?
No, I was just confused. Passing group type as a new argument to __fsnotify_alloc_group() as you do makes sense. Sorry for the noise.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Jan Kara
maintainerRe: [PATCH v2 02/10] fsnotify: introduce fsnotify group types
In reply to Amir GoldsteinQuoted 16 lines · Amir Goldstein
Currently an fanotify group can subscribe to events FAN_MNT_ATTACH/DETACH only on mark type
FAN_MARK_MNTNSand IFF the group was initialized withFAN_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_typewhich describes the category of events and mark types and use a distinct categoryFSNOTIFY_GROUP_TYPE_NSfor the mount event groups.Create helpers fsnotify_is_{fs,ns}_watcher() and the macro
FANOTIFY_NS_INIT_FLAGSand use them to generalize some code that was checking theFAN_REPORT_MNTflag.Signed-off-by Amir Goldstein <[email protected]>
...
include/linux/fanotify.h line 31
Quoted 5 lines
include/linux/fanotify.h +4
-1-#define FANOTIFY_INFO_MODES (FANOTIFY_FID_BITS | FAN_REPORT_PIDFD | FAN_REPORT_MNT) +#define FANOTIFY_INFO_MODES (FANOTIFY_FID_BITS | FAN_REPORT_PIDFD) + +/* fanotify_init() flags to create a namepsace event watcher */ +#define FANOTIFY_NS_INIT_FLAGS (FAN_REPORT_MNT)
Seeing this and where FANOTIFY_INFO_MODES is used - FAN_REPORT_MNT does influence the set of supplemental information that's provided with the event. So IMHO it should stay in FANOTIFY_INFO_MODES. We should just have bitmasks (currently two of them) through which we decide the type of the notification group we are creating.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Amir Goldstein
reviewerRe: [PATCH v2 02/10] fsnotify: introduce fsnotify group types
Quoted 21 lines · Jan Kara
Quoted 16 lines · Amir Goldstein
Currently an fanotify group can subscribe to events FAN_MNT_ATTACH/DETACH only on mark type
FAN_MARK_MNTNSand IFF the group was initialized withFAN_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_typewhich describes the category of events and mark types and use a distinct categoryFSNOTIFY_GROUP_TYPE_NSfor the mount event groups.Create helpers fsnotify_is_{fs,ns}_watcher() and the macro
FANOTIFY_NS_INIT_FLAGSand use them to generalize some code that was checking theFAN_REPORT_MNTflag.Signed-off-by Amir Goldstein <[email protected]>...
Quoted 11 lines
include/linux/fanotify.h line 31
Quoted 5 lines
include/linux/fanotify.h +4
-1-#define FANOTIFY_INFO_MODES (FANOTIFY_FID_BITS | FAN_REPORT_PIDFD | FAN_REPORT_MNT) +#define FANOTIFY_INFO_MODES (FANOTIFY_FID_BITS | FAN_REPORT_PIDFD) + +/* fanotify_init() flags to create a namepsace event watcher */ +#define FANOTIFY_NS_INIT_FLAGS (FAN_REPORT_MNT)Seeing this and where
FANOTIFY_INFO_MODESis used -FAN_REPORT_MNTdoes influence the set of supplemental information that's provided with the event. So IMHO it should stay inFANOTIFY_INFO_MODES. We should just have bitmasks (currently two of them) through which we decide the type of the notification group we are creating.
Makes sense.
I might have had a reason to do that, but if I did, it needs to be better justified.
Thanks,
Amir.
Amir Goldstein
reviewerPATCH v2 3/10 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_{FS,NS} and use them to determine the valid mask for fanotify_mark() and the outgoing mask by the group type.
Use the fanotify_is_valid_mask() helepr to simplifies the check for reporting mount events IFF group was initialized with FAN_REPORT_MNT and IFF watching an mntns object.
--- fs/notify/fanotify/fanotify.c | 3 +- fs/notify/fanotify/fanotify_user.c | 68 ++++++++++++++++++++---------- fs/notify/fsnotify.c | 6 +-- include/linux/fanotify.h | 26 ++++++++---- include/linux/fsnotify_backend.h | 42 ++++++++++++------ include/uapi/linux/fanotify.h | 25 ++++++++--- 6 files changed, 113 insertions(+), 57 deletions(-)
fs/notify/fanotify/fanotify.c +2 -1
@@ -303,7 +303,7 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group, struct inode *dir) {__u32 marks_mask =0 , marks_ignore_mask =0 ; -__u32 test_mask, user_mask =FANOTIFY_OUTGOING_EVENTS | +__u32 test_mask, user_mask =FANOTIFY_OUTGOING_FS_EVENTS |FANOTIFY_EVENT_FLAGS ;const struct path *path =fsnotify_data_path (data, data_type);unsigned int fid_mode =FAN_GROUP_FLAG (group,FANOTIFY_FID_BITS );
@@ -315,6 +315,7 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group, __func__, iter_info->report_mask, event_mask, data, data_type);if (fsnotify_is_ns_watcher (group)) { + user_mask =FANOTIFY_OUTGOING_NS_EVENTS ;if (data_type !=FSNOTIFY_EVENT_MNT )return 0 ; }else if (WARN_ON_ONCE (!fsnotify_is_fs_watcher (group))) {
fs/notify/fanotify/fanotify_user.c +45 -23
106 lines not on this page · raw message
fs/notify/fsnotify.c +3 -3
@@ -149,7 +149,7 @@ static inline __u32 fsnotify_object_watched(struct inode *inode, __u32 mnt_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_FS ; }/* Report pre-content event with optional range info */
@@ -219,7 +219,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_FS && !(data_type ==FSNOTIFY_EVENT_PATH &&d_is_special (dentry) && (mask & (FS_ACCESS |FS_MODIFY )));
@@ -266,7 +266,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_FS ))return 0 ;return ops->handle_inode_event (inode_mark, mask, inode, dir, name, cookie);
include/linux/fanotify.h +17 -9
@@ -113,27 +113,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_FS (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 namepsace watchers */ +#define FANOTIFY_EVENTS_ON_NS (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 | \ +/* Events that may be reported to user on filesystem watchers */ +#define FANOTIFY_OUTGOING_FS_EVENTS (FANOTIFY_EVENTS_ON_FS | \ FAN_Q_OVERFLOW | FAN_ONDIR) +/* Events that may be reported to user on namepsace watchers */ +#define FANOTIFY_OUTGOING_NS_EVENTS (FANOTIFY_EVENTS_ON_NS | \ + 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_FS_EVENTS | \ + FANOTIFY_OUTGOING_NS_EVENTS | \ FANOTIFY_EVENT_FLAGS)/* These masks check for invalid bits in permission responses. */
include/linux/fsnotify_backend.h +28 -14
73 lines not on this page · raw message
include/uapi/linux/fanotify.h +18 -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,28 @@/* #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.54.0
Jan Kara
maintainerRe: [PATCH v2 03/10] fsnotify: separate the events bitmask macros by group type
Quoted 23 lines · Amir Goldstein
Fork the macros
ALL_FSNOTIFY_EVENTSandFANOTIFY_EVENTSto *_EVENTS_ON_{FS,NS} and use them to determine the valid mask forfanotify_mark()and the outgoing mask by the group type.Use the
fanotify_is_valid_mask()helepr to simplifies the check for reporting mount events IFF group was initialized withFAN_REPORT_MNTand IFF watching an mntns object.Signed-off-by Amir Goldstein <[email protected]>fs/notify/fanotify/fanotify.c +1
-1@@ -303,7 +303,7 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group, struct inode *dir) {__u32 marks_mask =0 , marks_ignore_mask =0 ; -__u32 test_mask, user_mask =FANOTIFY_OUTGOING_EVENTS | +__u32 test_mask, user_mask =FANOTIFY_OUTGOING_FS_EVENTS |FANOTIFY_EVENT_FLAGS ;const struct path *path =fsnotify_data_path (data, data_type);unsigned int fid_mode =FAN_GROUP_FLAG (group,FANOTIFY_FID_BITS );
fs/notify/fanotify/fanotify.c static u32 fanotify_group_event_mask(struct fsnotify_group *group, · line 318
Quoted 4 lines
fs/notify/fanotify/fanotify.c +1
-0@@ -315,6 +315,7 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group, __func__, iter_info->report_mask, event_mask, data, data_type);if (fsnotify_is_ns_watcher (group)) { + user_mask =FANOTIFY_OUTGOING_NS_EVENTS ;
I'd use the helper function you define in fanotify_user.c for this so that we have the logic in one place.
fs/notify/fanotify/fanotify_user.c static int copy_info_records_to_user(struct fanotify_event *event, · line 834
Quoted 10 lines
fs/notify/fanotify/fanotify_user.c +2
-0@@ -830,6 +830,19 @@ static int copy_info_records_to_user(struct fanotify_event *event,return total_bytes; } +static __u64 fanotify_event_mask (struct fsnotify_group *group, +struct fanotify_event *event)
I'd call this fanotify_group_outgoing_events(). Your name is too similar to fanotify_group_event_mask() - it's unclear what's what from the name. Also I'd let it just take the mask as an argument instead of event.
fs/notify/fanotify/fanotify_user.c static int copy_info_records_to_user(struct fanotify_event *event, · line 840
Quoted 6 lines
fs/notify/fanotify/fanotify_user.c +6
-0+{ +switch (group->type) { +case FSNOTIFY_GROUP_TYPE_FS : +return event->mask &FANOTIFY_OUTGOING_FS_EVENTS ; +case FSNOTIFY_GROUP_TYPE_NS : +return event->mask &FANOTIFY_OUTGOING_NS_EVENTS ;
Here I'd also go for FILESYSTEM / NAMESPACE in constant names to increase distinction.
Quoted 22 lines
fs/notify/fanotify/fanotify_user.c +7
-1+ } + +return 0 ; +} +static ssize_t copy_event_to_user (struct fsnotify_group *group,struct fanotify_event *event,char __user *buf,size_t count)@@ -848,7 +861,7 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group, metadata.metadata_len =FAN_EVENT_METADATA_LEN ; metadata.vers =FANOTIFY_METADATA_VERSION ; metadata.reserved =0 ; - metadata.mask = event->mask &FANOTIFY_OUTGOING_EVENTS ; + metadata.mask =fanotify_event_mask (group, event); metadata.pid =pid_vnr (event->pid);/* * For an unprivileged listener, event->pid can be used to identify the @@ -1881,6 +1894,35 @@ static int fanotify_events_supported(struct fsnotify_group *group,return 0 ; } +static bool fanotify_is_valid_mask(struct fsnotify_group *group, int mark_type,
fs/notify/fanotify/fanotify_user.c static int fanotify_events_supported(struct fsnotify_group *group, · line 1907
Quoted 10 lines
fs/notify/fanotify/fanotify_user.c +10
-0+ __u64 mask) +{ +u32 valid_mask =0 ; + +/* +* Event bits for different group type may be overloaded but event +* flags are common to all group types. +*/ +BUILD_BUG_ON (FANOTIFY_EVENTS_ON_FS &FANOTIFY_EVENT_FLAGS ); +BUILD_BUG_ON (FANOTIFY_EVENTS_ON_NS &FANOTIFY_EVENT_FLAGS );
I'm a bit curious what's your rationale for this? It isn't like EVENT_FLAGS can be sensibly used with namespace notifications, they are kind of fs specific as well...
Quoted 4 lines
fs/notify/fanotify/fanotify_user.c +4
-0+ +switch (group->type) { +case FSNOTIFY_GROUP_TYPE_FS : + valid_mask =FANOTIFY_EVENTS_ON_FS |FANOTIFY_EVENT_FLAGS ;
fs/notify/fanotify/fanotify_user.c static int fanotify_events_supported(struct fsnotify_group *group, · line 1921
Quoted 10 lines
fs/notify/fanotify/fanotify_user.c +10
-0+if (!IS_ENABLED (CONFIG_FANOTIFY_ACCESS_PERMISSIONS )) + valid_mask &= ~FANOTIFY_PERM_EVENTS ; +break ; +case FSNOTIFY_GROUP_TYPE_NS : +/* Only report mount events on mntns mark */ +if (mark_type ==FAN_MARK_MNTNS && +FAN_GROUP_FLAG (group,FAN_REPORT_MNT )) + valid_mask =FANOTIFY_MOUNT_EVENTS ; +break ; + }
Shouldn't this also check that the mark type matches the group type? The condition you replace with this call did that...
Quoted 59 lines
fs/notify/fanotify/fanotify_user.c +6
-22+ +return !(mask & ~valid_mask); +} +static int do_fanotify_mark (int fanotify_fd,unsigned int flags,__u64 mask,int dfd,const char __user *pathname) {@@ -1890,7 +1932,6 @@ 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; -u32 valid_mask =FANOTIFY_EVENTS |FANOTIFY_EVENT_FLAGS ;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 ;@@ -1945,13 +1986,6 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,return -EINVAL ; } -if (IS_ENABLED (CONFIG_FANOTIFY_ACCESS_PERMISSIONS )) - valid_mask |=FANOTIFY_PERM_EVENTS ; - -if (mask & ~valid_mask) -return -EINVAL ; - -/* We don't allow FAN_MARK_IGNORE & FAN_MARK_IGNORED_MASK together */ if (ignore == (FAN_MARK_IGNORE |FAN_MARK_IGNORED_MASK ))return -EINVAL ;@@ -1974,22 +2008,10 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,return -EINVAL ; group =fd_file (f)->private_data; -if (group->type != group_type) +if (group->type != group_type || + !fanotify_is_valid_mask (group, mark_type, mask))return -EINVAL ; -/* Only report mount events on mnt namespace */ -if (FAN_GROUP_FLAG (group,FAN_REPORT_MNT )) { -if (mask & ~FANOTIFY_MOUNT_EVENTS ) -return -EINVAL ; -if (mark_type !=FAN_MARK_MNTNS ) -return -EINVAL ; - }else { -if (mask &FANOTIFY_MOUNT_EVENTS ) -return -EINVAL ; -if (mark_type ==FAN_MARK_MNTNS ) -return -EINVAL ; - } -/* * A user is allowed to setup sb/mount/mntns marks only if it is * capable in the user ns where the group was created. fs/notify/fsnotify.c +0
-0
fs/notify/fsnotify.c static inline __u32 fsnotify_object_watched(struct inode *inode, __u32 mnt_mask, · line 153
Quoted 6 lines
fs/notify/fsnotify.c +1
-1@@ -149,7 +149,7 @@ static inline __u32 fsnotify_object_watched(struct inode *inode, __u32 mnt_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_FS ; }
With this change it becomes kind of obvious that fsnotify_object_watched() is a misnomer after mntns events were added. And it is ok only because the callers of this are really specific to fs events. Can you please fix the name - perhaps to fsnotify_fs_object_watched()?
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Amir Goldstein
reviewerRe: [PATCH v2 03/10] fsnotify: separate the events bitmask macros by group type
Quoted 27 lines · Jan Kara
Quoted 25 lines · Amir Goldstein
Fork the macros
ALL_FSNOTIFY_EVENTSandFANOTIFY_EVENTSto *_EVENTS_ON_{FS,NS} and use them to determine the valid mask forfanotify_mark()and the outgoing mask by the group type.Use the
fanotify_is_valid_mask()helepr to simplifies the check for reporting mount events IFF group was initialized withFAN_REPORT_MNTand IFF watching an mntns object.Signed-off-by Amir Goldstein <[email protected]>fs/notify/fanotify/fanotify.c +1
-1@@ -303,7 +303,7 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group, struct inode *dir) {__u32 marks_mask =0 , marks_ignore_mask =0 ; -__u32 test_mask, user_mask =FANOTIFY_OUTGOING_EVENTS | +__u32 test_mask, user_mask =FANOTIFY_OUTGOING_FS_EVENTS |FANOTIFY_EVENT_FLAGS ;const struct path *path =fsnotify_data_path (data, data_type);unsigned int fid_mode =FAN_GROUP_FLAG (group,FANOTIFY_FID_BITS );@@ -315,6 +315,7 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group, __func__, iter_info->report_mask, event_mask, data, data_type);
Quoted 5 lines
fs/notify/fanotify/fanotify.c static u32 fanotify_group_event_mask(struct fsnotify_group *group, · line 318
Quoted 2 lines
fs/notify/fanotify/fanotify.c +1
-0if (fsnotify_is_ns_watcher (group)) { + user_mask =FANOTIFY_OUTGOING_NS_EVENTS ;I'd use the helper function you define in
fanotify_user.cfor this so that we have the logic in one place.
ok
Quoted 9 lines
Quoted 9 lines
fs/notify/fanotify/fanotify_user.c +0
-0@@ -830,6 +830,19 @@ static int copy_info_records_to_user(struct fanotify_event *event,return total_bytes; }
Quoted 6 lines
fs/notify/fanotify/fanotify_user.c static int copy_info_records_to_user(struct fanotify_event *event, · line 834
Quoted 2 lines
fs/notify/fanotify/fanotify_user.c +2
-0+static __u64 fanotify_event_mask (struct fsnotify_group *group, +struct fanotify_event *event)I'd call this
fanotify_group_outgoing_events(). Your name is too similar tofanotify_group_event_mask()- it's unclear what's what from the name. Also I'd let it just take the mask as an argument instead of event.
ok
Quoted 10 lines
fs/notify/fanotify/fanotify_user.c static int copy_info_records_to_user(struct fanotify_event *event, · line 840
Quoted 7 lines
fs/notify/fanotify/fanotify_user.c +6
-0+{ +switch (group->type) { +case FSNOTIFY_GROUP_TYPE_FS : +return event->mask &FANOTIFY_OUTGOING_FS_EVENTS ; +case FSNOTIFY_GROUP_TYPE_NS : +return event->mask &FANOTIFY_OUTGOING_NS_EVENTS ;Here I'd also go for FILESYSTEM / NAMESPACE in constant names to increase distinction.
ok
Quoted 22 lines
Quoted 22 lines
fs/notify/fanotify/fanotify_user.c +6
-1+ } + +return 0 ; +} +static ssize_t copy_event_to_user (struct fsnotify_group *group,struct fanotify_event *event,char __user *buf,size_t count)@@ -848,7 +861,7 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group, metadata.metadata_len =FAN_EVENT_METADATA_LEN ; metadata.vers =FANOTIFY_METADATA_VERSION ; metadata.reserved =0 ; - metadata.mask = event->mask &FANOTIFY_OUTGOING_EVENTS ; + metadata.mask =fanotify_event_mask (group, event); metadata.pid =pid_vnr (event->pid);/* * For an unprivileged listener, event->pid can be used to identify the @@ -1881,6 +1894,35 @@ static int fanotify_events_supported(struct fsnotify_group *group,return 0 ; }
Quoted 16 lines
fs/notify/fanotify/fanotify_user.c static int fanotify_events_supported(struct fsnotify_group *group, · line 1907
Quoted 11 lines
fs/notify/fanotify/fanotify_user.c +11
-0+static bool fanotify_is_valid_mask (struct fsnotify_group *group,int mark_type, +__u64 mask) +{ +u32 valid_mask =0 ; + +/* +* Event bits for different group type may be overloaded but event +* flags are common to all group types. +*/ +BUILD_BUG_ON (FANOTIFY_EVENTS_ON_FS &FANOTIFY_EVENT_FLAGS ); +BUILD_BUG_ON (FANOTIFY_EVENTS_ON_NS &FANOTIFY_EVENT_FLAGS );I'm a bit curious what's your rationale for this? It isn't like
EVENT_FLAGScan be sensibly used with namespace notifications, they are kind of fs specific as well...
The rationale is written in the cover letter.
I should have mentioned it here:
"To simplify the implementation, the event flags (ON_CHILD, ISDIR) live in a shared space that cannot be overloaded by neither group types. This is not because ISDIR makes sense for ns watcher, just to reduce the number of gates in common code. ON_CHILD flag might be usable for ns watchers, not sure."
My rationale was not having to take care of gating or making sure that they are already gated with is_filesystem_watcher() in all the places in common fsnotify code that test those flags:
fsnotify_ignore_mask(), fsnotify_mask_applicable(), fsnotify_iter_select_report_types(), send_to_group()
Just a lot of pain and hard to maintain subtle code saving for giving up two bits - one of those bits may be usable to namespaces, which sounded like a good enough deal to me.
The reason that FAN_EVENT_ON_CHILD could be applicable to namepsace watchers is that namespaces are a tree so userns mark could be watching events on children namespaces and pidns mark could also be required to watch children pid namespaces or children processes (Christian has some ideas there).
Quoted 15 lines
Quoted 4 lines
fs/notify/fanotify/fanotify_user.c +4
-0+ +switch (group->type) { +case FSNOTIFY_GROUP_TYPE_FS : + valid_mask =FANOTIFY_EVENTS_ON_FS |FANOTIFY_EVENT_FLAGS ;fs/notify/fanotify/fanotify_user.c static int fanotify_events_supported(struct fsnotify_group *group, · line 1921
Quoted 10 lines
fs/notify/fanotify/fanotify_user.c +10
-0+if (!IS_ENABLED (CONFIG_FANOTIFY_ACCESS_PERMISSIONS )) + valid_mask &= ~FANOTIFY_PERM_EVENTS ; +break ; +case FSNOTIFY_GROUP_TYPE_NS : +/* Only report mount events on mntns mark */ +if (mark_type ==FAN_MARK_MNTNS && +FAN_GROUP_FLAG (group,FAN_REPORT_MNT )) + valid_mask =FANOTIFY_MOUNT_EVENTS ; +break ; + }
Quoted 2 lines
Shouldn't this also check that the mark type matches the group type? The condition you replace with this call did that...
Hmm not sure.
See this check:
if (group->type != group_type ||
!fanotify_is_valid_mask(group, mark_type, mask))
return -EINVAL;group_type is determined earlier in the switch (mark_type) block.
Because every mark type maps to one single group type, this makes more sense, but maybe a better choice of variable name for group_type is in order. mark_group_type?
Quoted 63 lines
Quoted 63 lines
fs/notify/fanotify/fanotify_user.c +6
-22+ +return !(mask & ~valid_mask); +} +static int do_fanotify_mark (int fanotify_fd,unsigned int flags,__u64 mask,int dfd,const char __user *pathname) {@@ -1890,7 +1932,6 @@ 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; -u32 valid_mask =FANOTIFY_EVENTS |FANOTIFY_EVENT_FLAGS ;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 ;@@ -1945,13 +1986,6 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,return -EINVAL ; } -if (IS_ENABLED (CONFIG_FANOTIFY_ACCESS_PERMISSIONS )) - valid_mask |=FANOTIFY_PERM_EVENTS ; - -if (mask & ~valid_mask) -return -EINVAL ; - -/* We don't allow FAN_MARK_IGNORE & FAN_MARK_IGNORED_MASK together */ if (ignore == (FAN_MARK_IGNORE |FAN_MARK_IGNORED_MASK ))return -EINVAL ;@@ -1974,22 +2008,10 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,return -EINVAL ; group =fd_file (f)->private_data; -if (group->type != group_type) +if (group->type != group_type || + !fanotify_is_valid_mask (group, mark_type, mask))return -EINVAL ; -/* Only report mount events on mnt namespace */ -if (FAN_GROUP_FLAG (group,FAN_REPORT_MNT )) { -if (mask & ~FANOTIFY_MOUNT_EVENTS ) -return -EINVAL ; -if (mark_type !=FAN_MARK_MNTNS ) -return -EINVAL ; - }else { -if (mask &FANOTIFY_MOUNT_EVENTS ) -return -EINVAL ; -if (mark_type ==FAN_MARK_MNTNS ) -return -EINVAL ; - } -/* * A user is allowed to setup sb/mount/mntns marks only if it is * capable in the user ns where the group was created. fs/notify/fsnotify.c +0
-0@@ -149,7 +149,7 @@ static inline __u32 fsnotify_object_watched(struct inode *inode, __u32 mnt_mask,__u32 marks_mask =READ_ONCE (inode->i_fsnotify_mask) | mnt_mask |READ_ONCE (inode->i_sb->s_fsnotify_mask);
Quoted 8 lines
fs/notify/fsnotify.c static inline __u32 fsnotify_object_watched(struct inode *inode, __u32 mnt_mask, · line 153
Quoted 3 lines
fs/notify/fsnotify.c +1
-1-return mask & marks_mask &ALL_FSNOTIFY_EVENTS ; +return mask & marks_mask &FSNOTIFY_EVENTS_ON_FS ; }With this change it becomes kind of obvious that
fsnotify_object_watched()is a misnomer after mntns events were added. And it is ok only because the callers of this are really specific to fs events. Can you please fix the name - perhaps tofsnotify_fs_object_watched()?
Sure, are you sure that you don't want it to befsnotify_filesystem_object_watched() ;)
Thanks,
Amir.
Jan Kara
maintainerRe: [PATCH v2 03/10] fsnotify: separate the events bitmask macros by group type
Quoted 31 lines · Amir Goldstein
Quoted 13 lines · Jan Kara
Quoted 13 lines
fs/notify/fanotify/fanotify_user.c +1
-1@@ -848,7 +861,7 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group, metadata.metadata_len =FAN_EVENT_METADATA_LEN ; metadata.vers =FANOTIFY_METADATA_VERSION ; metadata.reserved =0 ; - metadata.mask = event->mask &FANOTIFY_OUTGOING_EVENTS ; + metadata.mask =fanotify_event_mask (group, event); metadata.pid =pid_vnr (event->pid);/* * For an unprivileged listener, event->pid can be used to identify the @@ -1881,6 +1894,35 @@ static int fanotify_events_supported(struct fsnotify_group *group,return 0 ; }Quoted 16 lines
fs/notify/fanotify/fanotify_user.c static int fanotify_events_supported(struct fsnotify_group *group, · line 1907
Quoted 11 lines
fs/notify/fanotify/fanotify_user.c +11
-0+static bool fanotify_is_valid_mask (struct fsnotify_group *group,int mark_type, +__u64 mask) +{ +u32 valid_mask =0 ; + +/* +* Event bits for different group type may be overloaded but event +* flags are common to all group types. +*/ +BUILD_BUG_ON (FANOTIFY_EVENTS_ON_FS &FANOTIFY_EVENT_FLAGS ); +BUILD_BUG_ON (FANOTIFY_EVENTS_ON_NS &FANOTIFY_EVENT_FLAGS );I'm a bit curious what's your rationale for this? It isn't like
EVENT_FLAGScan be sensibly used with namespace notifications, they are kind of fs specific as well...
Quoted 8 lines
The rationale is written in the cover letter.
I should have mentioned it here:"To simplify the implementation, the event flags (
ON_CHILD, ISDIR) live in a shared space that cannot be overloaded by neither group types. This is not because ISDIR makes sense for ns watcher, just to reduce the number of gates in common code.ON_CHILDflag might be usable for ns watchers, not sure."
Sorry, I should have noticed that. I was reviewing the patches on-and-off on a conference and some things were falling out of my context :).
Quoted 11 lines
My rationale was not having to take care of gating or making sure that they are already gated with
is_filesystem_watcher()in all the places in common fsnotify code that test those flags:
fsnotify_ignore_mask(),fsnotify_mask_applicable(),fsnotify_iter_select_report_types(),send_to_group()Just a lot of pain and hard to maintain subtle code saving for giving up two bits - one of those bits may be usable to namespaces, which sounded like a good enough deal to me.
Quoted 5 lines
The reason that
FAN_EVENT_ON_CHILDcould be applicable to namepsace watchers is that namespaces are a tree so userns mark could be watching events on children namespaces and pidns mark could also be required to watch children pid namespaces or children processes (Christian has some ideas there).
I see. I agree the group type checks are a bit annoying and I'm still thinking whether we cannot somehow make them nice :)
Quoted 17 lines
Quoted 14 lines
fs/notify/fanotify/fanotify_user.c static int fanotify_events_supported(struct fsnotify_group *group, · line 1921
Quoted 13 lines
fs/notify/fanotify/fanotify_user.c +13
-0+switch (group->type) { +case FSNOTIFY_GROUP_TYPE_FS : + valid_mask =FANOTIFY_EVENTS_ON_FS |FANOTIFY_EVENT_FLAGS ; +if (!IS_ENABLED (CONFIG_FANOTIFY_ACCESS_PERMISSIONS )) + valid_mask &= ~FANOTIFY_PERM_EVENTS ; +break ; +case FSNOTIFY_GROUP_TYPE_NS : +/* Only report mount events on mntns mark */ +if (mark_type ==FAN_MARK_MNTNS && +FAN_GROUP_FLAG (group,FAN_REPORT_MNT )) + valid_mask =FANOTIFY_MOUNT_EVENTS ; +break ; + }Quoted 2 lines
Shouldn't this also check that the mark type matches the group type? The condition you replace with this call did that...
Quoted 7 lines
Hmm not sure.
See this check:if (group->type != group_type || !fanotify_is_valid_mask(group, mark_type, mask)) return -EINVAL;
group_typeis determined earlier in the switch (mark_type) block.
Aha, right. Sorry for the noise.
Quoted 3 lines
Because every mark type maps to one single group type, this makes more sense, but maybe a better choice of variable name for
group_typeis in order.mark_group_type?
Yeah, that might be a bit more understandable.
Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
fs/notify/fsnotify.c +0
-0@@ -149,7 +149,7 @@ static inline __u32 fsnotify_object_watched(struct inode *inode, __u32 mnt_mask,__u32 marks_mask =READ_ONCE (inode->i_fsnotify_mask) | mnt_mask |READ_ONCE (inode->i_sb->s_fsnotify_mask);Quoted 8 lines
fs/notify/fsnotify.c static inline __u32 fsnotify_object_watched(struct inode *inode, __u32 mnt_mask, · line 153
Quoted 3 lines
fs/notify/fsnotify.c +1
-1-return mask & marks_mask &ALL_FSNOTIFY_EVENTS ; +return mask & marks_mask &FSNOTIFY_EVENTS_ON_FS ; }With this change it becomes kind of obvious that
fsnotify_object_watched()is a misnomer after mntns events were added. And it is ok only because the callers of this are really specific to fs events. Can you please fix the name - perhaps tofsnotify_fs_object_watched()?
Quoted 2 lines
Sure, are you sure that you don't want it to be
fsnotify_filesystem_object_watched();)
Maybe for consistency let's do that :)
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Amir Goldstein
reviewerPATCH v2 4/10 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.
The helper for classifying an fs permission event is renamed to fanotify_is_fs_perm_event() and takes the group argument to verify that this is a filesystem group type.
--- fs/notify/fanotify/fanotify.c | 30 +++++++++++++++--------------- fs/notify/fanotify/fanotify.h | 29 +++++++++++++++++++++-------- fs/notify/fanotify/fanotify_user.c | 18 +++++++++--------- include/linux/fsnotify_backend.h | 5 ----- 4 files changed, 45 insertions(+), 37 deletions(-)
fs/notify/fanotify/fanotify.c +15 -15
@@ -195,7 +195,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) {
@@ -204,7 +204,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 ;
@@ -711,11 +711,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;
@@ -759,6 +757,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 ;
@@ -845,11 +845,10 @@ static struct fanotify_event *fanotify_alloc_event(/* Whoever is interested in the event, pays for the allocation. */ old_memcg =set_active_memcg (group->memcg); -if (fanotify_is_perm_event (mask)) { +if (fanotify_is_fs_perm_event (group, mask)) { 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);
@@ -916,7 +915,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__,
@@ -970,7 +969,8 @@ 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)) { +bool is_perm =fanotify_is_fs_perm_event (group, mask); +if (is_perm) {/* * fsnotify_prepare_user_wait() fails if we race with mark * deletion. Just let the operation pass in that case.
@@ -990,7 +990,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)fsnotify_queue_overflow (group); goto finish; }
@@ -1000,17 +1000,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);/* 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) { ret =fanotify_get_response (group,FANOTIFY_PERM (event), iter_info); } finish: -if (fanotify_is_perm_event (mask)) +if (is_perm)fsnotify_finish_user_wait (iter_info);return ret;
fs/notify/fanotify/fanotify.h +21 -8
@@ -265,6 +265,7 @@ struct fanotify_event {struct pid *pid; }; +static inline void fanotify_init_event (struct fanotify_event *event,unsigned int hash,u32 mask) {
@@ -457,12 +458,24 @@ 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_fs_perm_event (struct fsnotify_group *group, +u32 mask) {return IS_ENABLED (CONFIG_FANOTIFY_ACCESS_PERMISSIONS ) && +fsnotify_is_fs_watcher (group) && mask &FANOTIFY_PERM_EVENTS ; } +static inline bool fanotify_is_perm_event (struct fanotify_event *event) +{ +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) {if (!(event->mask &FANOTIFY_PRE_CONTENT_EVENTS ))
@@ -476,14 +489,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)
@@ -506,10 +519,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
@@ -257,7 +257,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)) {
@@ -275,7 +275,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 )
@@ -338,9 +338,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);
@@ -800,7 +800,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;
@@ -818,7 +818,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;
@@ -965,7 +965,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;
@@ -1048,7 +1048,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 ) {
@@ -1145,7 +1145,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
@@ -783,11 +783,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.54.0
Jan Kara
maintainerRe: [PATCH v2 04/10] fanotify: test event->type instead of event mask when possible
Quoted 11 lines · Amir Goldstein
The
fanotify_eventalready has a type field, so tests likefanotify_is_mnt_event()should check the type and not the mask.In
fanotify_alloc_event()determine the mnt andfs_errorevent types bydata_type, which uniquely identifies those events.The helper for classifying an fs permission event is renamed to
fanotify_is_fs_perm_event()and takes the group argument to verify that this is a filesystem group type.Signed-off-by Amir Goldstein <[email protected]>
...
fs/notify/fanotify/fanotify.c static struct fanotify_event *fanotify_alloc_event( · line 848
Quoted 6 lines
fs/notify/fanotify/fanotify.c +1
-1@@ -845,11 +845,10 @@ static struct fanotify_event *fanotify_alloc_event(/* Whoever is interested in the event, pays for the allocation. */ old_memcg =set_active_memcg (group->memcg); -if (fanotify_is_perm_event (mask)) { +if (fanotify_is_fs_perm_event (group, mask)) {
Instead of creating this fanotify_is_fs_perm_event() helper func along with similar fanotify_is_perm_event(), I'd just opencode the mask checks in the two places where we don't have the event yet. Things will be IMHO more obvious that way. BTW I don't think we need anything besides the mask check. fanotify_group_event_mask() check should have taken care of the rest...
fs/notify/fanotify/fanotify.h struct fanotify_event { · line 268
Quoted 5 lines
fs/notify/fanotify/fanotify.h +1
-0@@ -265,6 +265,7 @@ struct fanotify_event {struct pid *pid; }; +
Stray empty line.
fs/notify/fanotify/fanotify.h struct fanotify_event { · line 271
Quoted 3 lines
fs/notify/fanotify/fanotify.h +0
-0static inline void fanotify_init_event (struct fanotify_event *event,unsigned int hash,u32 mask) {
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Amir Goldstein
reviewerRe: [PATCH v2 04/10] fanotify: test event->type instead of event mask when possible
Quoted 18 lines · Jan Kara
Quoted 11 lines · Amir Goldstein
The
fanotify_eventalready has a type field, so tests likefanotify_is_mnt_event()should check the type and not the mask.In
fanotify_alloc_event()determine the mnt andfs_errorevent types bydata_type, which uniquely identifies those events.The helper for classifying an fs permission event is renamed to
fanotify_is_fs_perm_event()and takes the group argument to verify that this is a filesystem group type.Signed-off-by Amir Goldstein <[email protected]>...
Quoted 4 lines
fs/notify/fanotify/fanotify.c +0
-0@@ -845,11 +845,10 @@ static struct fanotify_event *fanotify_alloc_event(/* Whoever is interested in the event, pays for the allocation. */ old_memcg =set_active_memcg (group->memcg);
Quoted 10 lines
fs/notify/fanotify/fanotify.c static struct fanotify_event *fanotify_alloc_event( · line 848
Quoted 2 lines
fs/notify/fanotify/fanotify.c +1
-1-if (fanotify_is_perm_event (mask)) { +if (fanotify_is_fs_perm_event (group, mask)) {Instead of creating this
fanotify_is_fs_perm_event()helper func along with similarfanotify_is_perm_event(), I'd just opencode the mask checks in the two places where we don't have the event yet. Things will be IMHO more obvious that way. BTW I don't think we need anything besides the mask check.fanotify_group_event_mask()check should have taken care of the rest...
Not sure about this.
They have to be gated with fsnotify_is_fs_watcher(), but anyway a later patch introduces fanotify_test_fs_watcher_event() helper and then it will be easy to drop fanotify_is_fs_perm_event() and open code it as
fanotify_test_fs_watcher_event(group, mask,
FANOTIFY_PERM_EVENTS);
Not sure if that end results addresses your concern.
Quoted 8 lines
fs/notify/fanotify/fanotify.h struct fanotify_event { · line 268
Quoted 5 lines
fs/notify/fanotify/fanotify.h +1
-0@@ -265,6 +265,7 @@ struct fanotify_event {struct pid *pid; }; +Stray empty line.
Thanks,
Amir.
Jan Kara
maintainerRe: [PATCH v2 04/10] fanotify: test event->type instead of event mask when possible
Quoted 30 lines · Amir Goldstein
Quoted 18 lines · Jan Kara
Quoted 11 lines · Amir Goldstein
The
fanotify_eventalready has a type field, so tests likefanotify_is_mnt_event()should check the type and not the mask.In
fanotify_alloc_event()determine the mnt andfs_errorevent types bydata_type, which uniquely identifies those events.The helper for classifying an fs permission event is renamed to
fanotify_is_fs_perm_event()and takes the group argument to verify that this is a filesystem group type.Signed-off-by Amir Goldstein <[email protected]>...
Quoted 4 lines
fs/notify/fanotify/fanotify.c +0
-0@@ -845,11 +845,10 @@ static struct fanotify_event *fanotify_alloc_event(/* Whoever is interested in the event, pays for the allocation. */ old_memcg =set_active_memcg (group->memcg);Quoted 10 lines
fs/notify/fanotify/fanotify.c static struct fanotify_event *fanotify_alloc_event( · line 848
Quoted 2 lines
fs/notify/fanotify/fanotify.c +1
-1-if (fanotify_is_perm_event (mask)) { +if (fanotify_is_fs_perm_event (group, mask)) {Instead of creating this
fanotify_is_fs_perm_event()helper func along with similarfanotify_is_perm_event(), I'd just opencode the mask checks in the two places where we don't have the event yet. Things will be IMHO more obvious that way. BTW I don't think we need anything besides the mask check.fanotify_group_event_mask()check should have taken care of the rest...
Quoted 2 lines
Not sure about this.
They have to be gated withfsnotify_is_fs_watcher(), but anyway
Well, the two places where the mask checks happen are already sure we have a filesystem event watching group (i.e., they have effectively checked fsnotify_is_fs_watcher() earlier).
Quoted 7 lines
a later patch introduces
fanotify_test_fs_watcher_event()helper and then it will be easy to dropfanotify_is_fs_perm_event()and open code it as
fanotify_test_fs_watcher_event(group, mask,FANOTIFY_PERM_EVENTS);Not sure if that end results addresses your concern.
But yes, if we can get rid of this helper later, that addresses my concern as well.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Amir Goldstein
reviewerPATCH v2 5/10 fsnotify: do not report mount events with fsnotify()
In reply to Amir Goldsteinfsnotify() is now used to report events to both filesystem watchers and (mnt) namepsace watchers, but those two distinct event categories can never be sent to the same group.
Split out send_to_ns_groups() which only looks for interested mntns object marks from fsnotify() which only looks for interested filesystem object marks and let fsnotify_mnt() call send_to_ns_groups().
The intention is that send_to_ns_groups() will also be used to report namespace events to watchers of userns object.
Gate the common code that is checking fs events with filesystem group type check.
--- fs/notify/fsnotify.c | 87 +++++++++++++++++++++++++++++++------------- 1 file changed, 62 insertions(+), 25 deletions(-)
fs/notify/fsnotify.c +62 -25
@@ -330,7 +330,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 );
@@ -344,7 +345,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_FS && mask &FS_MODIFY ) { fsnotify_foreach_iter_mark_type(iter_info, mark, type) {if (!(mark->flags &FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY ))
@@ -360,10 +361,13 @@ 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, +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 ;
@@ -469,6 +473,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_FS && + (mask &ALL_FSNOTIFY_PERM_EVENTS )) +return ret; + +fsnotify_iter_next (iter_info); + } +return 0 ; +} +/* * fsnotify - This is the main call to fsnotify. *
@@ -494,7 +518,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 ;
@@ -535,8 +558,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)
@@ -547,8 +569,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.
@@ -556,7 +576,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_FS );if (!(test_mask & marks_mask))return 0 ;
@@ -578,27 +598,15 @@ 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); + ret =send_to_groups (mask, data, data_type, dir, file_name, + cookie, &iter_info,FSNOTIFY_GROUP_TYPE_FS ); -if (ret && (mask &ALL_FSNOTIFY_PERM_EVENTS )) - goto out; - -fsnotify_iter_next (&iter_info); - } - ret =0 ; -out:srcu_read_unlock (&fsnotify_mark_srcu, iter_info.srcu_idx);return ret;
@@ -691,6 +699,35 @@ int fsnotify_open_perm_and_set_mode(struct file *file) }#endif +static int send_to_ns_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_NS ; +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_NS ); + +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 = {
@@ -708,7 +745,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_ns_groups (mask, &data,FSNOTIFY_EVENT_MNT ); }static __init int fsnotify_init (void )
Signature · 1 line
2.54.0
Amir Goldstein
reviewerPATCH v2 6/10 fanotify: gate fs 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 fs 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 fs events check inside fanotify_alloc_fs_watcher_event() are already properly gated.
--- fs/notify/fanotify/fanotify.c | 73 ++++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 15 deletions(-)
fs/notify/fanotify/fanotify.c +58 -15
@@ -744,11 +744,12 @@ static struct fanotify_event *fanotify_alloc_error_event(return &fee->fae; } -static struct fanotify_event *fanotify_alloc_event ( +static struct fanotify_event *fanotify_alloc_fs_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) {struct fanotify_event *event =NULL ;gfp_t gfp =GFP_KERNEL_ACCOUNT ;
@@ -759,14 +760,11 @@ 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) {/*
@@ -848,22 +846,69 @@ static struct fanotify_event *fanotify_alloc_event(if (fanotify_is_fs_perm_event (group, mask)) { event =fanotify_alloc_perm_event (data, data_type, gfp); }else if (fs_error) { - event =fanotify_alloc_error_event (group, fsid, fs_error, &hash); + 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); + moved, hash, gfp); }else if (fid_mode) { - event =fanotify_alloc_fid_event (id, fsid, &hash, gfp); + 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_path_event (path, hash, gfp); + }else { +WARN_ON_ONCE (1 ); + } + +set_active_memcg (old_memcg); +return event; +} + +static struct fanotify_event *fanotify_alloc_ns_watcher_event ( +struct fsnotify_group *group,u64 mask, +const void *data,int data_type) +{ +u64 mnt_id =fsnotify_data_mnt_id (data, data_type); +struct mem_cgroup *old_memcg; +struct fanotify_event *event =NULL ; +gfp_t gfp =GFP_KERNEL_ACCOUNT ; + +if (group->max_events ==UINT_MAX ) + gfp |= __GFP_NOFAIL; +else + gfp |= __GFP_RETRY_MAYFAIL; + + old_memcg =set_active_memcg (group->memcg); + +if (mnt_id) { event =fanotify_alloc_mnt_event (mnt_id, gfp); }else {WARN_ON_ONCE (1 ); } +set_active_memcg (old_memcg); +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; +unsigned int hash =0 ; +bool ondir = mask &FAN_ONDIR ; +struct pid *pid; + +if (fsnotify_is_ns_watcher (group)) { + event =fanotify_alloc_ns_watcher_event (group, mask, data, + data_type); + }else { + event =fanotify_alloc_fs_watcher_event (group, mask, data, + data_type, dir, name, fsid, + match_mask, &hash); + }if (!event) - goto out; +return NULL ;if (FAN_GROUP_FLAG (group,FAN_REPORT_TID )) pid =get_pid (task_pid (current));
@@ -875,8 +920,6 @@ static struct fanotify_event *fanotify_alloc_event(fanotify_init_event (event, hash, mask); event->pid = pid; -out: -set_active_memcg (old_memcg);return event; }
@@ -966,8 +1009,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);bool is_perm =fanotify_is_fs_perm_event (group, mask);if (is_perm) {
Signature · 1 line
2.54.0
Jan Kara
maintainerRe: [PATCH v2 06/10] fanotify: gate fs event classification by group type
Quoted 12 lines · Amir Goldstein
fanotify 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 fs 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 fs events check insidefanotify_alloc_fs_watcher_event()are already properly gated.Signed-off-by Amir Goldstein <[email protected]>
...
Quoted 26 lines
fs/notify/fanotify/fanotify.c +20
-1+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; +unsigned int hash =0 ; +bool ondir = mask &FAN_ONDIR ; +struct pid *pid; + +if (fsnotify_is_ns_watcher (group)) { + event =fanotify_alloc_ns_watcher_event (group, mask, data, + data_type); + }else { + event =fanotify_alloc_fs_watcher_event (group, mask, data, + data_type, dir, name, fsid, + match_mask, &hash); + }if (!event) - goto out; +return NULL ;if (FAN_GROUP_FLAG (group,FAN_REPORT_TID )) pid =get_pid (task_pid (current));
fs/notify/fanotify/fanotify.c static struct fanotify_event *fanotify_alloc_event( · line 924
Quoted 7 lines
fs/notify/fanotify/fanotify.c +0
-2@@ -875,8 +920,6 @@ static struct fanotify_event *fanotify_alloc_event(fanotify_init_event (event, hash, mask); event->pid = pid; -out: -set_active_memcg (old_memcg);return event; }
Looks good. Just I'd keep the memcg & gfp dance in the shared code in fanotify_alloc_event() and just pass the gfp mask into the group-type-specific allocation functions.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Amir Goldstein
reviewerPATCH v2 7/10 fanotify: gate fs events checks in fanotify_mark() by group type
In reply to Amir Goldsteinfanotify_mark() has plenty of checks 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 event (e.g. FAN_FS_ERROR) need to check that the group type as well (e.g. filesystem).
Add helpers fanotify_test_{fs,ns}_watcher_event() and use them instead of checking the event mask directly.
--- fs/notify/fanotify/fanotify.h | 16 ++++++++++-- fs/notify/fanotify/fanotify_user.c | 39 ++++++++++++++++++------------ 2 files changed, 38 insertions(+), 17 deletions(-)
fs/notify/fanotify/fanotify.h +14 -2
@@ -458,12 +458,24 @@ FANOTIFY_PERM(struct fanotify_event *event) return container_of(event, struct fanotify_perm_event, fae); } +static inline bool fanotify_test_fs_watcher_event (struct fsnotify_group *group, +u32 mask,u32 test_mask) +{ +return fsnotify_is_fs_watcher (group) && (mask & test_mask); +} + +static inline bool fanotify_test_ns_watcher_event (struct fsnotify_group *group, +u32 mask,u32 test_mask) +{ +return fsnotify_is_ns_watcher (group) && (mask & test_mask); +} +static inline bool fanotify_is_fs_perm_event (struct fsnotify_group *group,u32 mask) {return IS_ENABLED (CONFIG_FANOTIFY_ACCESS_PERMISSIONS ) && -fsnotify_is_fs_watcher (group) && - mask &FANOTIFY_PERM_EVENTS ; +fanotify_test_fs_watcher_event (group, mask, +FANOTIFY_PERM_EVENTS ); }static inline bool fanotify_is_perm_event (struct fanotify_event *event)
fs/notify/fanotify/fanotify_user.c +24 -15
@@ -1509,7 +1509,9 @@ static int fanotify_may_update_existing_mark(struct fsnotify_mark *fsn_mark,/* For now pre-content events are not generated for directories */ mask |= fsn_mark->mask; -if (mask &FANOTIFY_PRE_CONTENT_EVENTS && mask &FAN_ONDIR ) +if (mask &FAN_ONDIR && +fanotify_test_fs_watcher_event (fsn_mark->group, mask, +FANOTIFY_PRE_CONTENT_EVENTS ))return -EEXIST ;return 0 ;
@@ -1546,8 +1548,8 @@ static int fanotify_add_mark(struct fsnotify_group *group,* Error events are pre-allocated per group, only if strictly * needed (i.e. FAN_FS_ERROR was requested). */ -if (!(fan_flags &FANOTIFY_MARK_IGNORE_BITS ) && - (mask &FAN_FS_ERROR )) { +if (fanotify_test_fs_watcher_event (group, mask,FAN_FS_ERROR ) && + !(fan_flags &FANOTIFY_MARK_IGNORE_BITS )) { ret =fanotify_group_init_error_pool (group);if (ret) goto out;
@@ -1562,7 +1564,8 @@ static int fanotify_add_mark(struct fsnotify_group *group,fsnotify_put_mark (fsn_mark); -if (!ret && (mask &FANOTIFY_PERM_EVENTS )) +if (!ret &&fanotify_test_fs_watcher_event (group, mask, +FANOTIFY_PERM_EVENTS ))fanotify_perm_watchdog_group_add (group);return ret;
@@ -1842,14 +1845,15 @@ static int fanotify_events_supported(struct fsnotify_group *group,bool is_dir =d_is_dir (path->dentry);/* Strict validation of events in non-dir inode mask with v5.17+ APIs */ bool strict_dir_events =FAN_GROUP_FLAG (group,FAN_REPORT_TARGET_FID ) || - (mask &FAN_RENAME ) || - (flags &FAN_MARK_IGNORE ); +fanotify_test_fs_watcher_event (group, 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_test_fs_watcher_event (group, 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))
@@ -1864,7 +1868,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_test_fs_watcher_event (group, mask,FANOTIFY_PERM_EVENTS ) && path->mnt->mnt_sb->s_type->fs_flags &FS_DISALLOW_NOTIFY_PERM )return -EINVAL ;
@@ -1887,8 +1891,9 @@ 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_test_fs_watcher_event (group, mask, +FANOTIFY_DIRONLY_EVENT_BITS ))return -ENOTDIR ;return 0 ;
@@ -2024,14 +2029,15 @@ 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_test_fs_watcher_event (group, mask,FANOTIFY_PERM_EVENTS ) && group->priority ==FSNOTIFY_PRIO_NORMAL )return -EINVAL ; -else if (mask &FANOTIFY_PRE_CONTENT_EVENTS && +else if (fanotify_test_fs_watcher_event (group, mask, +FANOTIFY_PRE_CONTENT_EVENTS ) && group->priority ==FSNOTIFY_PRIO_CONTENT )return -EINVAL ; -if (mask &FAN_FS_ERROR && +if (fanotify_test_fs_watcher_event (group, mask,FAN_FS_ERROR ) && mark_type !=FAN_MARK_FILESYSTEM )return -EINVAL ;
@@ -2061,11 +2067,14 @@ 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_test_fs_watcher_event (group, 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 (mask &FAN_ONDIR && +fanotify_test_fs_watcher_event (group, mask, +FANOTIFY_PRE_CONTENT_EVENTS ))return -EINVAL ;if (mark_cmd ==FAN_MARK_FLUSH ) {
Signature · 1 line
2.54.0
Jan Kara
maintainerRe: [PATCH v2 07/10] fanotify: gate fs events checks in fanotify_mark() by group type
Quoted 11 lines · Amir Goldstein
fanotify_mark()has plenty of checks 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 event (e.g.
FAN_FS_ERROR) need to check that the group type as well (e.g. filesystem).Add helpers fanotify_test_{fs,ns}_watcher_event() and use them instead of checking the event mask directly.
Signed-off-by Amir Goldstein <[email protected]>
I was looking into this and thinking about it for some time and I have one brainstorming suggestion:
fs/notify/fanotify/fanotify.h FANOTIFY_PERM(struct fanotify_event *event) · line 471
Quoted 11 lines
fs/notify/fanotify/fanotify.h +11
-0+static inline bool fanotify_test_fs_watcher_event (struct fsnotify_group *group, +u32 mask,u32 test_mask) +{ +return fsnotify_is_fs_watcher (group) && (mask & test_mask); +} + +static inline bool fanotify_test_ns_watcher_event (struct fsnotify_group *group, +u32 mask,u32 test_mask) +{ +return fsnotify_is_ns_watcher (group) && (mask & test_mask); +}
These helpers look a bit cumbersome (having to manually select the right one, there's no safety if you accidentally pick the wrong helper, you need to pass the group as well, ...). So I was thinking about the following: In fanotify uAPI the mask is actually 64-bit. What if we carve out some bits from the upper word (8?) and store the group type there? Either we can do this type-encoding only internaly once we receive the mask from userspace or we could even have the encoding in uAPI constants as well - which would provide additional uAPI constant type safety (we'd have to special-case FAN_MNT_ATTACH & FAN_MNT_DETACH in that case but that should be doable, since they are not widely used yet we could maybe even eventually deprecate the old values).
Anyway, once we have the type encoded in the 64-bit mask we can easily compare, mask, combine, ... these with some helper functions without having to check the group all the time and having to select different helper function based on event type. We are not very consistent with passing 64-bit mask everywhere since the fsnotify core has only 32-bit mask and so far we use only the lower 32-bits in fanotify as well so that would need to be fixed. Also once we store the mask into the mark, it would be squashed down to 32 bits but I think that's fine.
What do you think about it? Do you find this worth it? What's your opinion on having the type encoded already in uAPI constants?
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Amir Goldstein
reviewerRe: [PATCH v2 07/10] fanotify: gate fs events checks in fanotify_mark() by group type
Quoted 50 lines · Jan Kara
Quoted 11 lines · Amir Goldstein
fanotify_mark()has plenty of checks 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 event (e.g.
FAN_FS_ERROR) need to check that the group type as well (e.g. filesystem).Add helpers fanotify_test_{fs,ns}_watcher_event() and use them instead of checking the event mask directly.
Signed-off-by Amir Goldstein <[email protected]>I was looking into this and thinking about it for some time and I have one brainstorming suggestion:
Review
Quoted 11 lines
(quoted diff) +11
-0+static inline bool fanotify_test_fs_watcher_event(struct fsnotify_group *group, + u32 mask, u32 test_mask) +{ + return fsnotify_is_fs_watcher(group) && (mask & test_mask); +} + +static inline bool fanotify_test_ns_watcher_event(struct fsnotify_group *group, + u32 mask, u32 test_mask) +{ + return fsnotify_is_ns_watcher(group) && (mask & test_mask); +}These helpers look a bit cumbersome (having to manually select the right one, there's no safety if you accidentally pick the wrong helper, you need to pass the group as well, ...). So I was thinking about the following: In fanotify uAPI the mask is actually 64-bit. What if we carve out some bits from the upper word (8?) and store the group type there? Either we can do this type-encoding only internaly once we receive the mask from userspace or we could even have the encoding in uAPI constants as well - which would provide additional uAPI constant type safety (we'd have to special-case
FAN_MNT_ATTACH&FAN_MNT_DETACHin that case but that should be doable, since they are not widely used yet we could maybe even eventually deprecate the old values).Anyway, once we have the type encoded in the 64-bit mask we can easily compare, mask, combine, ... these with some helper functions without having to check the group all the time and having to select different helper function based on event type. We are not very consistent with passing 64-bit mask everywhere since the fsnotify core has only 32-bit mask and so far we use only the lower 32-bits in fanotify as well so that would need to be fixed. Also once we store the mask into the mark, it would be squashed down to 32 bits but I think that's fine.
Quoted 2 lines
What do you think about it? Do you find this worth it? What's your opinion on having the type encoded already in uAPI constants?
My opinion is that I had already started playing with that, see:
https://github.com/amir73il/linux/commits/fanotify_event_type-wip
include/linux/fsnotify_backend.h +3 -1
@@ -78,8 +78,10 @@/* 64bit kernel internal event hint flags */ #define FS_EVENT_IS_PERM BIT_ULL(33)/* Wait for user response */ +#define FS_EVENT_ON_NS BIT_ULL(34)/* For namespace watchers */ #define FS_PERM_EVENT (mask) (FS_EVENT_IS_PERM | (mask)) +#define FS_NS_EVENT (mask) (FS_EVENT_ON_NS | (mask))/* * Directory entry modification events - reported only to directory
@@ -136,7 +138,7 @@#define ALL_FSNOTIFY_FLAGS (FS_ISDIR | FS_EVENT_ON_CHILD | FS_DN_MULTISHOT)/* Kernel internal 64bit flags */ -#define ALL_FSNOTIFY_KERN_FLAGS (FS_EVENT_IS_PERM) +#define ALL_FSNOTIFY_KERN_FLAGS (FS_EVENT_IS_PERM | FS_EVENT_ON_NS)#define ALL_FSNOTIFY_BITS (ALL_FSNOTIFY_KERN_FLAGS | \ ALL_FSNOTIFY_EVENTS | \
---
TBH, I don't remember whether I was discouraged for some reason or if I had started to question the value of this
or if I thought it was better to send the simpler version first.
Anyway, I am fond of this approach so I will give it a try.
I went back and forth about exporting this explicitly in uAPI and require that all fanotify_mark() calls on namespace objects (add,remove,flush) always carry the explicit FS_EVENT_ON_NS flag and/or in the outgoing mask.
or that they are defined as:
#define FAN_NS_CREATE (__FAN_NS_CREATE | FS_EVENT_ON_NS)
Also if we do that, we need to think of the least ugly way to handle FAN_MNT_ATTACH & FAN_MNT_DETACH.
Your thoughts on this?
Thanks,
Amir.
Jan Kara
maintainerRe: [PATCH v2 07/10] fanotify: gate fs events checks in fanotify_mark() by group type
Quoted 51 lines · Amir Goldstein
Quoted 50 lines · Jan Kara
Quoted 11 lines · Amir Goldstein
fanotify_mark()has plenty of checks 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 event (e.g.
FAN_FS_ERROR) need to check that the group type as well (e.g. filesystem).Add helpers fanotify_test_{fs,ns}_watcher_event() and use them instead of checking the event mask directly.
Signed-off-by Amir Goldstein <[email protected]>I was looking into this and thinking about it for some time and I have one brainstorming suggestion:
Review
Quoted 11 lines
(quoted diff) +11
-0+static inline bool fanotify_test_fs_watcher_event(struct fsnotify_group *group, + u32 mask, u32 test_mask) +{ + return fsnotify_is_fs_watcher(group) && (mask & test_mask); +} + +static inline bool fanotify_test_ns_watcher_event(struct fsnotify_group *group, + u32 mask, u32 test_mask) +{ + return fsnotify_is_ns_watcher(group) && (mask & test_mask); +}These helpers look a bit cumbersome (having to manually select the right one, there's no safety if you accidentally pick the wrong helper, you need to pass the group as well, ...). So I was thinking about the following: In fanotify uAPI the mask is actually 64-bit. What if we carve out some bits from the upper word (8?) and store the group type there? Either we can do this type-encoding only internaly once we receive the mask from userspace or we could even have the encoding in uAPI constants as well - which would provide additional uAPI constant type safety (we'd have to special-case
FAN_MNT_ATTACH&FAN_MNT_DETACHin that case but that should be doable, since they are not widely used yet we could maybe even eventually deprecate the old values).Anyway, once we have the type encoded in the 64-bit mask we can easily compare, mask, combine, ... these with some helper functions without having to check the group all the time and having to select different helper function based on event type. We are not very consistent with passing 64-bit mask everywhere since the fsnotify core has only 32-bit mask and so far we use only the lower 32-bits in fanotify as well so that would need to be fixed. Also once we store the mask into the mark, it would be squashed down to 32 bits but I think that's fine.
Quoted 6 lines
Quoted 2 lines
What do you think about it? Do you find this worth it? What's your opinion on having the type encoded already in uAPI constants?
My opinion is that I had already started playing with that, see:
https://github.com/amir73il/linux/commits/fanotify_event_type-wip
Cool :)
include/linux/fsnotify_backend.h line 84
Quoted 10 lines
include/linux/fsnotify_backend.h +2
-0@@ -78,8 +78,10 @@/* 64bit kernel internal event hint flags */ #define FS_EVENT_IS_PERM BIT_ULL(33)/* Wait for user response */ +#define FS_EVENT_ON_NS BIT_ULL(34)/* For namespace watchers */ #define FS_PERM_EVENT (mask) (FS_EVENT_IS_PERM | (mask)) +#define FS_NS_EVENT (mask) (FS_EVENT_ON_NS | (mask))
I'd prefer we have couple bits reserved like:
#define FSNOTIFY_EVENT_TYPE_BITS 8
#define FSNOTIFY_EVENT_TYPE_MASK (~0ULL << (64 - FSNOTIFY_EVENT_TYPE_BITS))
And then allocate different types (FILESYSTEM == 0, NAMESPACE == 1, ...) from this in an enum fashion. This is much more efficient on bit space than just event flags.
Quoted 12 lines
include/linux/fsnotify_backend.h +1
-2@@ -136,7 +138,7 @@#define ALL_FSNOTIFY_FLAGS (FS_ISDIR | FS_EVENT_ON_CHILD | FS_DN_MULTISHOT)/* Kernel internal 64bit flags */ -#define ALL_FSNOTIFY_KERN_FLAGS (FS_EVENT_IS_PERM) +#define ALL_FSNOTIFY_KERN_FLAGS (FS_EVENT_IS_PERM | FS_EVENT_ON_NS)#define ALL_FSNOTIFY_BITS (ALL_FSNOTIFY_KERN_FLAGS | \ ALL_FSNOTIFY_EVENTS | \ ---
Quoted 5 lines
TBH, I don't remember whether I was discouraged for some reason or if I had started to question the value of this
or if I thought it was better to send the simpler version first.Anyway, I am fond of this approach so I will give it a try.
Thanks!
Quoted 10 lines
I went back and forth about exporting this explicitly in uAPI and require that all
fanotify_mark()calls on namespace objects (add,remove,flush) always carry the explicitFS_EVENT_ON_NSflag and/or in the outgoing mask.or that they are defined as:
#defineFAN_NS_CREATE(__FAN_NS_CREATE|FS_EVENT_ON_NS)Also if we do that, we need to think of the least ugly way to handle
FAN_MNT_ATTACH&FAN_MNT_DETACH.
These are good questions. After thinking about this I think the most convenient would be if:
* Type was already included in the defined event constants
* Type was included in the reported outgoing events
That way you can combine events constants like EVENT1 | EVENT2 | EVENT3 and get a valid input for fanotify_mark() or a valid mask. For checking you'd have to do received_mask & ~FSNOTIFY_EVENT_TYPE_MASK & (EVENT1 | EVENT2) to check whether any of these events was received which is bearable, mask == EVENT1 would keep working.
I've checked and FAN_MNT_ATTACH / FAN_MNT_DETACH already got used in a couple of places (stress-ng, util-linux, ...) so the only realistic option I see at this point is to define FAN_MNTNS_ATTACH, FAN_MNTNS_DETACH with proper type info included and automatically morph FAN_MNT_ATTACH and FAN_MNT_DETACH into FAN_MNTNS_ATTACH and FAN_MNTNS_DETACH, respectively in fanotify_mark(). When morphing we'd have to set some group flag to disable reporting of type in outgoing events as well and make sure FAN_MNT_ATTACH and FAN_MNT_DETACH cannot be mixed with any other properly typed events in one group.
I also considered an option to explicitly define a group flag that userspace can set to enable verification of types in fanotify_mark() and reporting them in outgoing event mask. This would be an option as well but so far it seemed somewhat more confusing than what I suggested above (where I expect people to transition to properly typed events faster because otherwise their functionality will be restricted).
Of course there's still the question whether we want to expose types in the uAPI at all but I'm slowly leaning towards yes to make it harder to use the API wrongly.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Amir Goldstein
reviewerRe: [PATCH v2 07/10] fanotify: gate fs events checks in fanotify_mark() by group type
Quoted 75 lines · Jan Kara
Quoted 51 lines · Amir Goldstein
Quoted 50 lines · Jan Kara
Quoted 11 lines · Amir Goldstein
fanotify_mark()has plenty of checks 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 event (e.g.
FAN_FS_ERROR) need to check that the group type as well (e.g. filesystem).Add helpers fanotify_test_{fs,ns}_watcher_event() and use them instead of checking the event mask directly.
Signed-off-by Amir Goldstein <[email protected]>I was looking into this and thinking about it for some time and I have one brainstorming suggestion:
Review
Quoted 11 lines
(quoted diff) +11
-0+static inline bool fanotify_test_fs_watcher_event(struct fsnotify_group *group, + u32 mask, u32 test_mask) +{ + return fsnotify_is_fs_watcher(group) && (mask & test_mask); +} + +static inline bool fanotify_test_ns_watcher_event(struct fsnotify_group *group, + u32 mask, u32 test_mask) +{ + return fsnotify_is_ns_watcher(group) && (mask & test_mask); +}These helpers look a bit cumbersome (having to manually select the right one, there's no safety if you accidentally pick the wrong helper, you need to pass the group as well, ...). So I was thinking about the following: In fanotify uAPI the mask is actually 64-bit. What if we carve out some bits from the upper word (8?) and store the group type there? Either we can do this type-encoding only internaly once we receive the mask from userspace or we could even have the encoding in uAPI constants as well - which would provide additional uAPI constant type safety (we'd have to special-case
FAN_MNT_ATTACH&FAN_MNT_DETACHin that case but that should be doable, since they are not widely used yet we could maybe even eventually deprecate the old values).Anyway, once we have the type encoded in the 64-bit mask we can easily compare, mask, combine, ... these with some helper functions without having to check the group all the time and having to select different helper function based on event type. We are not very consistent with passing 64-bit mask everywhere since the fsnotify core has only 32-bit mask and so far we use only the lower 32-bits in fanotify as well so that would need to be fixed. Also once we store the mask into the mark, it would be squashed down to 32 bits but I think that's fine.
Quoted 6 lines
Quoted 2 lines
What do you think about it? Do you find this worth it? What's your opinion on having the type encoded already in uAPI constants?
My opinion is that I had already started playing with that, see:
https://github.com/amir73il/linux/commits/fanotify_event_type-wip
Cool :)
include/linux/fsnotify_backend.h line 84
Quoted 10 lines
include/linux/fsnotify_backend.h +2
-0@@ -78,8 +78,10 @@/* 64bit kernel internal event hint flags */ #define FS_EVENT_IS_PERM BIT_ULL(33)/* Wait for user response */ +#define FS_EVENT_ON_NS BIT_ULL(34)/* For namespace watchers */ #define FS_PERM_EVENT (mask) (FS_EVENT_IS_PERM | (mask)) +#define FS_NS_EVENT (mask) (FS_EVENT_ON_NS | (mask))I'd prefer we have couple bits reserved like:
Quoted 6 lines
#define
FSNOTIFY_EVENT_TYPE_BITS8
#defineFSNOTIFY_EVENT_TYPE_MASK(~0ULL << (64 -FSNOTIFY_EVENT_TYPE_BITS))And then allocate different types (FILESYSTEM == 0, NAMESPACE == 1, ...) from this in an enum fashion. This is much more efficient on bit space than just event flags.
Wow, 256 types. What a rich future ;)
Note that I also considered flags (FS_EVENT_IS_PERM) as part of the event definition - those would control things that are common to all types.
Quoted 35 lines
Quoted 13 lines
include/linux/fsnotify_backend.h +1
-2@@ -136,7 +138,7 @@#define ALL_FSNOTIFY_FLAGS (FS_ISDIR | FS_EVENT_ON_CHILD | FS_DN_MULTISHOT)/* Kernel internal 64bit flags */ -#define ALL_FSNOTIFY_KERN_FLAGS (FS_EVENT_IS_PERM) +#define ALL_FSNOTIFY_KERN_FLAGS (FS_EVENT_IS_PERM | FS_EVENT_ON_NS)#define ALL_FSNOTIFY_BITS (ALL_FSNOTIFY_KERN_FLAGS | \ ALL_FSNOTIFY_EVENTS | \ ---Quoted 5 lines
TBH, I don't remember whether I was discouraged for some reason or if I had started to question the value of this
or if I thought it was better to send the simpler version first.Anyway, I am fond of this approach so I will give it a try.
Thanks!
Quoted 10 lines
I went back and forth about exporting this explicitly in uAPI and require that all
fanotify_mark()calls on namespace objects (add,remove,flush) always carry the explicitFS_EVENT_ON_NSflag and/or in the outgoing mask.or that they are defined as:
#defineFAN_NS_CREATE(__FAN_NS_CREATE|FS_EVENT_ON_NS)Also if we do that, we need to think of the least ugly way to handle
FAN_MNT_ATTACH&FAN_MNT_DETACH.These are good questions. After thinking about this I think the most convenient would be if:
Quoted 8 lines
* Type was already included in the defined event constants
* Type was included in the reported outgoing eventsThat way you can combine events constants like EVENT1 | EVENT2 | EVENT3 and get a valid input for
fanotify_mark()or a valid mask. For checking you'd have to doreceived_mask& ~FSNOTIFY_EVENT_TYPE_MASK & (EVENT1 | EVENT2) to check whether any of these events was received which is bearable, mask == EVENT1 would keep working.
When you write "you'd have to do..." meaning all the applications need to be educated about this and we need to expose FAN_EVENT_TYPE_MASK
in uAPI.
I guess we should provide
#define FAN_EVENT_MATCH(event_meta, event_set)
The risk of a programmer doing if (event->mask & EVENT1) exists, but not much more we can do about it.
Quoted 10 lines
I've checked and
FAN_MNT_ATTACH/FAN_MNT_DETACHalready got used in a couple of places (stress-ng, util-linux, ...) so the only realistic option I see at this point is to defineFAN_MNTNS_ATTACH,FAN_MNTNS_DETACHwith proper type info included and automatically morphFAN_MNT_ATTACHandFAN_MNT_DETACHintoFAN_MNTNS_ATTACHandFAN_MNTNS_DETACH, respectively infanotify_mark(). When morphing we'd have to set some group flag to disable reporting of type in outgoing events as well and make sureFAN_MNT_ATTACHandFAN_MNT_DETACHcannot be mixed with any other properly typed events in one group.
Seems manageable.
Quoted 11 lines
I also considered an option to explicitly define a group flag that userspace can set to enable verification of types in
fanotify_mark()and reporting them in outgoing event mask. This would be an option as well but so far it seemed somewhat more confusing than what I suggested above (where I expect people to transition to properly typed events faster because otherwise their functionality will be restricted).Of course there's still the question whether we want to expose types in the uAPI at all but I'm slowly leaning towards yes to make it harder to use the API wrongly.
I see no value in opt-in for verification of types in fanotify_mark(). opt-in or -out for the outgoing mask - maybe, especially since we need this internal flag anyway for FAN_MNT_*, but I am also leaning against it.
Anyway, with no end users to nag me and summer vacation getting close it may take me some time to post v3, so if you think of any new ideas there is still time to bring them up.
Thanks,
Amir.
Jan Kara
maintainerRe: [PATCH v2 07/10] fanotify: gate fs events checks in fanotify_mark() by group type
Quoted 74 lines · Amir Goldstein
Quoted 73 lines · Jan Kara
Quoted 50 lines · Amir Goldstein
Quoted 49 lines · Jan Kara
Quoted 11 lines · Amir Goldstein
fanotify_mark()has plenty of checks 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 event (e.g.
FAN_FS_ERROR) need to check that the group type as well (e.g. filesystem).Add helpers fanotify_test_{fs,ns}_watcher_event() and use them instead of checking the event mask directly.
Signed-off-by Amir Goldstein <[email protected]>I was looking into this and thinking about it for some time and I have one brainstorming suggestion:
Review
Quoted 11 lines
(quoted diff) +11
-0+static inline bool fanotify_test_fs_watcher_event(struct fsnotify_group *group, + u32 mask, u32 test_mask) +{ + return fsnotify_is_fs_watcher(group) && (mask & test_mask); +} + +static inline bool fanotify_test_ns_watcher_event(struct fsnotify_group *group, + u32 mask, u32 test_mask) +{ + return fsnotify_is_ns_watcher(group) && (mask & test_mask); +}These helpers look a bit cumbersome (having to manually select the right one, there's no safety if you accidentally pick the wrong helper, you need to pass the group as well, ...). So I was thinking about the following: In fanotify uAPI the mask is actually 64-bit. What if we carve out some bits from the upper word (8?) and store the group type there? Either we can do this type-encoding only internaly once we receive the mask from userspace or we could even have the encoding in uAPI constants as well - which would provide additional uAPI constant type safety (we'd have to special-case
FAN_MNT_ATTACH&FAN_MNT_DETACHin that case but that should be doable, since they are not widely used yet we could maybe even eventually deprecate the old values).Anyway, once we have the type encoded in the 64-bit mask we can easily compare, mask, combine, ... these with some helper functions without having to check the group all the time and having to select different helper function based on event type. We are not very consistent with passing 64-bit mask everywhere since the fsnotify core has only 32-bit mask and so far we use only the lower 32-bits in fanotify as well so that would need to be fixed. Also once we store the mask into the mark, it would be squashed down to 32 bits but I think that's fine.
Quoted 6 lines
Quoted 2 lines
What do you think about it? Do you find this worth it? What's your opinion on having the type encoded already in uAPI constants?
My opinion is that I had already started playing with that, see:
https://github.com/amir73il/linux/commits/fanotify_event_type-wip
Cool :)
include/linux/fsnotify_backend.h line 84
Quoted 10 lines
include/linux/fsnotify_backend.h +2
-0@@ -78,8 +78,10 @@/* 64bit kernel internal event hint flags */ #define FS_EVENT_IS_PERM BIT_ULL(33)/* Wait for user response */ +#define FS_EVENT_ON_NS BIT_ULL(34)/* For namespace watchers */ #define FS_PERM_EVENT (mask) (FS_EVENT_IS_PERM | (mask)) +#define FS_NS_EVENT (mask) (FS_EVENT_ON_NS | (mask))I'd prefer we have couple bits reserved like:
Quoted 8 lines
Quoted 6 lines
#define
FSNOTIFY_EVENT_TYPE_BITS8
#defineFSNOTIFY_EVENT_TYPE_MASK(~0ULL << (64 -FSNOTIFY_EVENT_TYPE_BITS))And then allocate different types (FILESYSTEM == 0, NAMESPACE == 1, ...) from this in an enum fashion. This is much more efficient on bit space than just event flags.
Wow, 256 types. What a rich future ;)
:) It may be a bit too much but OTOH in those 64 bits I think we can spare 8 bits...
Quoted 2 lines
Note that I also considered flags (
FS_EVENT_IS_PERM) as part of the event definition - those would control things that are common to all types.
Hum, but FS_EVENT_IS_PERM is kernel internal flag and I really want it to stay that way. If needed we can move it out of the mask completely...
Quoted 43 lines
Quoted 34 lines
Quoted 12 lines
include/linux/fsnotify_backend.h +1
-2@@ -136,7 +138,7 @@#define ALL_FSNOTIFY_FLAGS (FS_ISDIR | FS_EVENT_ON_CHILD | FS_DN_MULTISHOT)/* Kernel internal 64bit flags */ -#define ALL_FSNOTIFY_KERN_FLAGS (FS_EVENT_IS_PERM) +#define ALL_FSNOTIFY_KERN_FLAGS (FS_EVENT_IS_PERM | FS_EVENT_ON_NS)#define ALL_FSNOTIFY_BITS (ALL_FSNOTIFY_KERN_FLAGS | \ ALL_FSNOTIFY_EVENTS | \ ---Quoted 5 lines
TBH, I don't remember whether I was discouraged for some reason or if I had started to question the value of this
or if I thought it was better to send the simpler version first.Anyway, I am fond of this approach so I will give it a try.
Thanks!
Quoted 10 lines
I went back and forth about exporting this explicitly in uAPI and require that all
fanotify_mark()calls on namespace objects (add,remove,flush) always carry the explicitFS_EVENT_ON_NSflag and/or in the outgoing mask.or that they are defined as:
#defineFAN_NS_CREATE(__FAN_NS_CREATE|FS_EVENT_ON_NS)Also if we do that, we need to think of the least ugly way to handle
FAN_MNT_ATTACH&FAN_MNT_DETACH.These are good questions. After thinking about this I think the most convenient would be if:
Quoted 8 lines
* Type was already included in the defined event constants
* Type was included in the reported outgoing eventsThat way you can combine events constants like EVENT1 | EVENT2 | EVENT3 and get a valid input for
fanotify_mark()or a valid mask. For checking you'd have to doreceived_mask& ~FSNOTIFY_EVENT_TYPE_MASK & (EVENT1 | EVENT2) to check whether any of these events was received which is bearable, mask == EVENT1 would keep working.
Quoted 3 lines
When you write "you'd have to do..." meaning all the applications need to be educated about this and we need to expose
FAN_EVENT_TYPE_MASK
in uAPI.
Yes, all apps wanting to use other than the standard filesystem events with type == 0 will need to be using this new way of checking.
Quoted 3 lines
I guess we should provide
#define
FAN_EVENT_MATCH(event_meta, event_set)
Yes, we could do that I guess.
Quoted 2 lines
The risk of a programmer doing if (
event->mask& EVENT1) exists, but not much more we can do about it.
Yep + documentation in the manpage obviously.
Quoted 19 lines
Quoted 9 lines
I've checked and
FAN_MNT_ATTACH/FAN_MNT_DETACHalready got used in a couple of places (stress-ng, util-linux, ...) so the only realistic option I see at this point is to defineFAN_MNTNS_ATTACH,FAN_MNTNS_DETACHwith proper type info included and automatically morphFAN_MNT_ATTACHandFAN_MNT_DETACHintoFAN_MNTNS_ATTACHandFAN_MNTNS_DETACH, respectively infanotify_mark(). When morphing we'd have to set some group flag to disable reporting of type in outgoing events as well and make sureFAN_MNT_ATTACHandFAN_MNT_DETACHcannot be mixed with any other properly typed events in one group.Seems manageable.
Quoted 7 lines
I also considered an option to explicitly define a group flag that userspace can set to enable verification of types in
fanotify_mark()and reporting them in outgoing event mask. This would be an option as well but so far it seemed somewhat more confusing than what I suggested above (where I expect people to transition to properly typed events faster because otherwise their functionality will be restricted).
Quoted 7 lines
Quoted 3 lines
Of course there's still the question whether we want to expose types in the uAPI at all but I'm slowly leaning towards yes to make it harder to use the API wrongly.
I see no value in opt-in for verification of types in
fanotify_mark(). opt-in or -out for the outgoing mask - maybe, especially since we need this internal flag anyway for FAN_MNT_*, but I am also leaning against it.
OK, good.
Quoted 3 lines
Anyway, with no end users to nag me and summer vacation getting close it may take me some time to post v3, so if you think of any new ideas there is still time to bring them up.
Sure :) Thanks for working on this!
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Amir Goldstein
reviewerRe: [PATCH v2 07/10] fanotify: gate fs events checks in fanotify_mark() by group type
Quoted 79 lines · Jan Kara
Quoted 77 lines · Amir Goldstein
Quoted 76 lines · Jan Kara
Quoted 50 lines · Amir Goldstein
Quoted 49 lines · Jan Kara
Quoted 11 lines · Amir Goldstein
fanotify_mark()has plenty of checks 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 event (e.g.
FAN_FS_ERROR) need to check that the group type as well (e.g. filesystem).Add helpers fanotify_test_{fs,ns}_watcher_event() and use them instead of checking the event mask directly.
Signed-off-by Amir Goldstein <[email protected]>I was looking into this and thinking about it for some time and I have one brainstorming suggestion:
Review
Quoted 11 lines
(quoted diff) +11
-0+static inline bool fanotify_test_fs_watcher_event(struct fsnotify_group *group, + u32 mask, u32 test_mask) +{ + return fsnotify_is_fs_watcher(group) && (mask & test_mask); +} + +static inline bool fanotify_test_ns_watcher_event(struct fsnotify_group *group, + u32 mask, u32 test_mask) +{ + return fsnotify_is_ns_watcher(group) && (mask & test_mask); +}These helpers look a bit cumbersome (having to manually select the right one, there's no safety if you accidentally pick the wrong helper, you need to pass the group as well, ...). So I was thinking about the following: In fanotify uAPI the mask is actually 64-bit. What if we carve out some bits from the upper word (8?) and store the group type there? Either we can do this type-encoding only internaly once we receive the mask from userspace or we could even have the encoding in uAPI constants as well - which would provide additional uAPI constant type safety (we'd have to special-case
FAN_MNT_ATTACH&FAN_MNT_DETACHin that case but that should be doable, since they are not widely used yet we could maybe even eventually deprecate the old values).Anyway, once we have the type encoded in the 64-bit mask we can easily compare, mask, combine, ... these with some helper functions without having to check the group all the time and having to select different helper function based on event type. We are not very consistent with passing 64-bit mask everywhere since the fsnotify core has only 32-bit mask and so far we use only the lower 32-bits in fanotify as well so that would need to be fixed. Also once we store the mask into the mark, it would be squashed down to 32 bits but I think that's fine.
Quoted 6 lines
Quoted 2 lines
What do you think about it? Do you find this worth it? What's your opinion on having the type encoded already in uAPI constants?
My opinion is that I had already started playing with that, see:
https://github.com/amir73il/linux/commits/fanotify_event_type-wip
Cool :)
include/linux/fsnotify_backend.h line 84
Quoted 10 lines
include/linux/fsnotify_backend.h +2
-0@@ -78,8 +78,10 @@/* 64bit kernel internal event hint flags */ #define FS_EVENT_IS_PERM BIT_ULL(33)/* Wait for user response */ +#define FS_EVENT_ON_NS BIT_ULL(34)/* For namespace watchers */ #define FS_PERM_EVENT (mask) (FS_EVENT_IS_PERM | (mask)) +#define FS_NS_EVENT (mask) (FS_EVENT_ON_NS | (mask))I'd prefer we have couple bits reserved like:
#define
FSNOTIFY_EVENT_TYPE_BITS8
#defineFSNOTIFY_EVENT_TYPE_MASK(~0ULL << (64 -FSNOTIFY_EVENT_TYPE_BITS))
Quoted 9 lines
Quoted 5 lines
Quoted 3 lines
And then allocate different types (FILESYSTEM == 0, NAMESPACE == 1, ...) from this in an enum fashion. This is much more efficient on bit space than just event flags.
Wow, 256 types. What a rich future ;)
:) It may be a bit too much but OTOH in those 64 bits I think we can spare 8 bits...
I went with bits 32..39 so that event macros can be written explicitly as:
/*
* 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)
...
/* 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 */
...
/* Internal fsnotify_mnt() events */
#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)
...
#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)
...
ASSERT_FS_EVENT(FAN_RENAME, FS_RENAME);
ASSERT_FS_EVENT(FAN_PRE_ACCESS, FS_PRE_ACCESS);
/* 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);---
Quoted 6 lines
Quoted 2 lines
Note that I also considered flags (
FS_EVENT_IS_PERM) as part of the event definition - those would control things that are common to all types.Hum, but
FS_EVENT_IS_PERMis kernel internal flag and I really want it to stay that way. If needed we can move it out of the mask completely...
I left these out for now.
Quoted 47 lines
Quoted 43 lines
Quoted 34 lines
Quoted 12 lines
include/linux/fsnotify_backend.h +1
-2@@ -136,7 +138,7 @@#define ALL_FSNOTIFY_FLAGS (FS_ISDIR | FS_EVENT_ON_CHILD | FS_DN_MULTISHOT)/* Kernel internal 64bit flags */ -#define ALL_FSNOTIFY_KERN_FLAGS (FS_EVENT_IS_PERM) +#define ALL_FSNOTIFY_KERN_FLAGS (FS_EVENT_IS_PERM | FS_EVENT_ON_NS)#define ALL_FSNOTIFY_BITS (ALL_FSNOTIFY_KERN_FLAGS | \ ALL_FSNOTIFY_EVENTS | \ ---Quoted 5 lines
TBH, I don't remember whether I was discouraged for some reason or if I had started to question the value of this
or if I thought it was better to send the simpler version first.Anyway, I am fond of this approach so I will give it a try.
Thanks!
Quoted 10 lines
I went back and forth about exporting this explicitly in uAPI and require that all
fanotify_mark()calls on namespace objects (add,remove,flush) always carry the explicitFS_EVENT_ON_NSflag and/or in the outgoing mask.or that they are defined as:
#defineFAN_NS_CREATE(__FAN_NS_CREATE|FS_EVENT_ON_NS)Also if we do that, we need to think of the least ugly way to handle
FAN_MNT_ATTACH&FAN_MNT_DETACH.These are good questions. After thinking about this I think the most convenient would be if:
Quoted 8 lines
* Type was already included in the defined event constants
* Type was included in the reported outgoing eventsThat way you can combine events constants like EVENT1 | EVENT2 | EVENT3 and get a valid input for
fanotify_mark()or a valid mask. For checking you'd have to doreceived_mask& ~FSNOTIFY_EVENT_TYPE_MASK & (EVENT1 | EVENT2) to check whether any of these events was received which is bearable, mask == EVENT1 would keep working.Quoted 3 lines
When you write "you'd have to do..." meaning all the applications need to be educated about this and we need to expose
FAN_EVENT_TYPE_MASK
in uAPI.
Quoted 8 lines
Yes, all apps wanting to use other than the standard filesystem events with type == 0 will need to be using this new way of checking.
Quoted 3 lines
I guess we should provide
#define
FAN_EVENT_MATCH(event_meta, event_set)Yes, we could do that I guess.
Created two macros:
fanotify: introduce FAN_EVENT_MATCH() macro
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.
---
/* 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))/* 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)---
Quoted 17 lines
Quoted 3 lines
The risk of a programmer doing if (
event->mask& EVENT1) exists, but not much more we can do about it.Yep + documentation in the manpage obviously.
Quoted 11 lines
Quoted 9 lines
I've checked and
FAN_MNT_ATTACH/FAN_MNT_DETACHalready got used in a couple of places (stress-ng, util-linux, ...) so the only realistic option I see at this point is to defineFAN_MNTNS_ATTACH,FAN_MNTNS_DETACHwith proper type info included and automatically morphFAN_MNT_ATTACHandFAN_MNT_DETACHintoFAN_MNTNS_ATTACHandFAN_MNTNS_DETACH, respectively infanotify_mark(). When morphing we'd have to set some group flag to disable reporting of type in outgoing events as well and make sureFAN_MNT_ATTACHandFAN_MNT_DETACHcannot be mixed with any other properly typed events in one group.Seems manageable.
I examined some options here and uapi seemed problematic.
For starters, FAN_MNTNS_ATTACH sounds like an mntns object is being attached and it does not conform to the language in other event macos.
What I settled on at the moment is the following semantics:
fanotify: define new FAN_NS_MNT_* constants
The 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.
---
/* 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 */
---
fanotify: introduce FAN_REPORT_MNTID
This 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().
---
#define FAN_REPORT_MNT 0x00004000 /* Report FAN_MNT_ events */ #define FAN_REPORT_MNTID 0x00008000 /* Report FAN_NS_MNT_ events */
---
opting-in to this mode at init time greatly simplifies the semantics compared to guessing what user meant later on and this is quite easy to document in man page, as per the short commit message above.
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
I also considered an option to explicitly define a group flag that userspace can set to enable verification of types in
fanotify_mark()and reporting them in outgoing event mask. This would be an option as well but so far it seemed somewhat more confusing than what I suggested above (where I expect people to transition to properly typed events faster because otherwise their functionality will be restricted).Of course there's still the question whether we want to expose types in the uAPI at all but I'm slowly leaning towards yes to make it harder to use the API wrongly.
Quoted 6 lines
Quoted 3 lines
I see no value in opt-in for verification of types in
fanotify_mark(). opt-in or -out for the outgoing mask - maybe, especially since we need this internal flag anyway for FAN_MNT_*, but I am also leaning against it.OK, good.
I was swayed the other way ;)
I now have opt-in for both input verification and outgoing mask:
#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)
...
static bool fanotify_is_valid_mask(struct fsnotify_group *group, int mark_type,
struct fanotify_mask fan_mask)
{
u64 valid_mask = 0;
...
/* 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 |
FANOTIFY_EVENT_FLAGS;
if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
valid_mask &= ~FANOTIFY_PERM_EVENTS;
break;
case FSNOTIFY_GROUP_TYPE_NAMESPACE:
/* Only report mount events on mntns mark */
if (mark_type == FAN_MARK_MNTNS &&
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) &&
FAN_GROUP_FLAG(group, FAN_REPORT_MNT))
fan_mask.type = FSNOTIFY_GROUP_TYPE_NAMESPACE;
break;
}
if (fan_mask.type != group->type)
return false;
return !(fan_mask.mask & ~valid_mask);
}Later on this opt-in is going to grow to:
#define FANOTIFY_REPORT_TYPE (FAN_REPORT_MNTID | FAN_REPORT_NSID)
Quoted 5 lines
Quoted 3 lines
Anyway, with no end users to nag me and summer vacation getting close it may take me some time to post v3, so if you think of any new ideas there is still time to bring them up.
Sure :) Thanks for working on this!
I pushed the tested poc to github [1] so you can take a look when you have time. I will post the patches for proper review, but first I wanted to know if you are fine
with the semantics I came up with.
Thanks,
Amir.
[1] https://github.com/amir73il/linux/commits/fsnotify_group_type
Jan Kara
maintainerRe: [PATCH v2 07/10] fanotify: gate fs events checks in fanotify_mark() by group type
Quoted 10 lines · Amir Goldstein
Quoted 5 lines
Quoted 3 lines
Anyway, with no end users to nag me and summer vacation getting close it may take me some time to post v3, so if you think of any new ideas there is still time to bring them up.
Sure :) Thanks for working on this!
I pushed the tested poc to github [1] so you can take a look when you have time. I will post the patches for proper review, but first I wanted to know if you are fine
with the semantics I came up with.
I've glanced through the series in github and it looks good to me. I didn't find any problem with the design. So feel free to post it :).
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Amir Goldstein
reviewerRe: [PATCH v2 07/10] fanotify: gate fs events checks in fanotify_mark() by group type
Quoted 8 lines · Jan Kara
Quoted 6 lines · Amir Goldstein
Quoted 5 lines
Quoted 3 lines
Anyway, with no end users to nag me and summer vacation getting close it may take me some time to post v3, so if you think of any new ideas there is still time to bring them up.
Sure :) Thanks for working on this!
Quoted 7 lines
Quoted 4 lines
I pushed the tested poc to github [1] so you can take a look when you have time. I will post the patches for proper review, but first I wanted to know if you are fine
with the semantics I came up with.I've glanced through the series in github and it looks good to me. I didn't find any problem with the design. So feel free to post it :).
Ok posted.
Btw, I fixed the ULL uapi constants in fanotify.h, but forgot to fix patch "[13/14] selftests/fanotify: update the uapi tools header copy" which still carries the old UL constants before posting.
This is fixed now on my github branch.
Thanks,
Amir.
Amir Goldstein
reviewerPATCH v2 8/10 fanotify: add support for watching the namespaces tree
In reply to Amir GoldsteinIntroduce FAN_MARK_USERNS type to mark a user namespace object from nsfs path.
Support two events FAN_NS_CREATE and FAN_NS_DELETE to report creation and tear down of namespaces owned by the marked userns.
Introduce FAN_REPORT_NSID to report the self and owner nsid of the created or torn down namespace.
An fanotify group initialized with flags FAN_REPORT_MNT and FAN_REPORT_NSID, may add marks on both userns and mntns objects to mix mount and namespace events, but the same group cannot also request filesystem events.
--- fs/notify/fanotify/fanotify.c | 33 +++++++++++++++- fs/notify/fanotify/fanotify.h | 19 ++++++++++ fs/notify/fanotify/fanotify_user.c | 61 ++++++++++++++++++++++++++++-- fs/notify/fdinfo.c | 9 ++++- fs/notify/fsnotify.c | 30 +++++++++++++++ fs/notify/fsnotify.h | 12 ++++++ fs/notify/mark.c | 7 ++++ fs/nsfs.c | 21 ++++++++++ include/linux/fanotify.h | 10 +++-- include/linux/fsnotify.h | 5 +++ include/linux/fsnotify_backend.h | 33 ++++++++++++++++ include/linux/proc_fs.h | 2 + include/linux/user_namespace.h | 6 +++ include/uapi/linux/fanotify.h | 12 ++++++ kernel/nscommon.c | 47 +++++++++++++++++++++++ kernel/user_namespace.c | 2 + 16 files changed, 299 insertions(+), 10 deletions(-)
fs/notify/fanotify/fanotify.c +32 -1
@@ -168,6 +168,8 @@ static bool fanotify_should_merge(struct fanotify_event *old, FANOTIFY_EE(new));case FANOTIFY_EVENT_TYPE_MNT :return false; +case FANOTIFY_EVENT_TYPE_NS : +return false;default :WARN_ON_ONCE (1 ); }
@@ -316,7 +318,8 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group,if (fsnotify_is_ns_watcher (group)) { user_mask =FANOTIFY_OUTGOING_NS_EVENTS ; -if (data_type !=FSNOTIFY_EVENT_MNT ) +if (data_type !=FSNOTIFY_EVENT_MNT && + data_type !=FSNOTIFY_EVENT_NS )return 0 ; }else if (WARN_ON_ONCE (!fsnotify_is_fs_watcher (group))) {return 0 ;
@@ -585,6 +588,23 @@ static struct fanotify_event *fanotify_alloc_mnt_event(u64 mnt_id, gfp_t gfp)return &pevent->fae; } +static struct fanotify_event *fanotify_alloc_userns_event ( +const struct fsnotify_ns *ns_data, +gfp_t gfp) +{ +struct fanotify_ns_event *pevent; + + pevent =kmem_cache_alloc (fanotify_ns_event_cachep, gfp); +if (!pevent) +return NULL ; + + pevent->fae.type =FANOTIFY_EVENT_TYPE_NS ; + pevent->self_nsid = ns_data->self_nsid; + pevent->owner_nsid = ns_data->owner_nsid; + +return &pevent->fae; +} +static struct fanotify_event *fanotify_alloc_perm_event (const void *data,int data_type,gfp_t gfp)
@@ -866,6 +886,7 @@ static struct fanotify_event *fanotify_alloc_ns_watcher_event( struct fsnotify_group *group, u64 mask, const void *data, int data_type) { + const struct fsnotify_ns *ns_data = fsnotify_data_ns(data, data_type);u64 mnt_id =fsnotify_data_mnt_id (data, data_type);struct mem_cgroup *old_memcg;struct fanotify_event *event =NULL ;
@@ -880,6 +901,8 @@ static struct fanotify_event *fanotify_alloc_ns_watcher_event(if (mnt_id) { event =fanotify_alloc_mnt_event (mnt_id, gfp); + }else if (ns_data) { + event =fanotify_alloc_userns_event (ns_data, gfp); }else {WARN_ON_ONCE (1 ); }
@@ -1110,6 +1133,11 @@ static void fanotify_free_mnt_event(struct fanotify_event *event)kmem_cache_free (fanotify_mnt_event_cachep,FANOTIFY_ME (event)); } +static void fanotify_free_ns_event (struct fanotify_event *event) +{ +kmem_cache_free (fanotify_ns_event_cachep,FANOTIFY_NSE (event)); +} +static void fanotify_free_event (struct fsnotify_group *group,struct fsnotify_event *fsn_event) {
@@ -1139,6 +1167,9 @@ static void fanotify_free_event(struct fsnotify_group *group,case FANOTIFY_EVENT_TYPE_MNT :fanotify_free_mnt_event (event);break ; +case FANOTIFY_EVENT_TYPE_NS : +fanotify_free_ns_event (event); +break ;default :WARN_ON_ONCE (1 ); }
fs/notify/fanotify/fanotify.h +19 -0
@@ -11,6 +11,7 @@ extern struct kmem_cache *fanotify_fid_event_cachep;extern struct kmem_cache *fanotify_path_event_cachep;extern struct kmem_cache *fanotify_perm_event_cachep;extern struct kmem_cache *fanotify_mnt_event_cachep; +extern struct kmem_cache *fanotify_ns_event_cachep;/* Possible states of the permission event */ enum {
@@ -246,6 +247,7 @@ enum fanotify_event_type {FANOTIFY_EVENT_TYPE_OVERFLOW ,/* struct fanotify_event */ FANOTIFY_EVENT_TYPE_FS_ERROR ,/* struct fanotify_error_event */ FANOTIFY_EVENT_TYPE_MNT , +FANOTIFY_EVENT_TYPE_NS , __FANOTIFY_EVENT_TYPE_NUM };
@@ -417,6 +419,12 @@ struct fanotify_mnt_event {u64 mnt_id; }; +struct fanotify_ns_event { +struct fanotify_event fae; +u64 self_nsid; +u64 owner_nsid; +}; +static inline struct fanotify_path_event *FANOTIFY_PE (struct fanotify_event *event) {
@@ -429,6 +437,12 @@ FANOTIFY_ME(struct fanotify_event *event) return container_of(event, struct fanotify_mnt_event, fae); } +static inline struct fanotify_ns_event * +FANOTIFY_NSE (struct fanotify_event *event) +{ + return container_of(event, struct fanotify_ns_event, fae); +} +/* * Structure for permission fanotify events. It gets allocated and freed in * fanotify_handle_event() since we wait there for user response. When the
@@ -511,6 +525,11 @@ static inline bool fanotify_is_mnt_event(struct fanotify_event *event)return event->type ==FANOTIFY_EVENT_TYPE_MNT ; } +static inline bool fanotify_is_ns_event (const struct fanotify_event *event) +{ +return event->type ==FANOTIFY_EVENT_TYPE_NS ; +} +static inline const struct path *fanotify_event_path (struct fanotify_event *event) {if (event->type ==FANOTIFY_EVENT_TYPE_PATH )
fs/notify/fanotify/fanotify_user.c +57 -4
129 lines not on this page · raw message
fs/notify/fdinfo.c +7 -2
@@ -130,8 +130,13 @@ static void fanotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark) }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); +seq_printf (m,"fanotify mnt_ns_id:%llu mflags:%x mask:%x ignored_mask:%x\n" , + mnt_ns->ns.ns_id, mflags, mark->mask, mark->ignore_mask); + }else if (mark->connector->type ==FSNOTIFY_OBJ_TYPE_USERNS ) { +struct user_namespace *userns =fsnotify_conn_userns (mark->connector); + +seq_printf (m,"fanotify user_ns_id:%llu mflags:%x mask:%x ignored_mask:%x\n" , + userns->ns.ns_id, mflags, mark->mask, mark->ignore_mask); } }
fs/notify/fsnotify.c +30 -0
@@ -33,6 +33,11 @@ void __fsnotify_mntns_delete(struct mnt_namespace *mntns)fsnotify_clear_marks_by_mntns (mntns); } +void __fsnotify_userns_delete (struct user_namespace *userns) +{ +fsnotify_clear_marks_by_userns (userns); +} +void fsnotify_sb_delete (struct super_block *sb) {struct fsnotify_sb_info *sbinfo =fsnotify_sb_info (sb);
@@ -702,12 +707,15 @@ int fsnotify_open_perm_and_set_mode(struct file *file)static int send_to_ns_groups (__u32 mask,const void *data,int data_type) {const struct fsnotify_mnt *mnt_data =fsnotify_data_mnt (data, data_type); +const struct fsnotify_ns *ns_data =fsnotify_data_ns (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); +if (ns_data) + marks_mask |=READ_ONCE (ns_data->userns->n_fsnotify_mask); test_mask = mask &FSNOTIFY_EVENTS_ON_NS ;if (!(test_mask & marks_mask))
@@ -719,6 +727,10 @@ static int send_to_ns_groups(__u32 mask, const void *data, int data_type) iter_info.marks[FSNOTIFY_ITER_TYPE_MNTNS ] =fsnotify_first_mark (&mnt_data->ns->n_fsnotify_marks); } +if (ns_data) { + iter_info.marks[FSNOTIFY_ITER_TYPE_USERNS ] = +fsnotify_first_mark (&ns_data->userns->n_fsnotify_marks); + } ret =send_to_groups (mask, data, data_type,NULL ,NULL ,0 , &iter_info,FSNOTIFY_GROUP_TYPE_NS );
@@ -748,6 +760,24 @@ void fsnotify_mnt(__u32 mask, struct mnt_namespace *ns, struct vfsmount *mnt)send_to_ns_groups (mask, &data,FSNOTIFY_EVENT_MNT ); } +void fsnotify_ns (__u32 mask,struct user_namespace *userns, +u64 self_nsid,u64 owner_nsid) +{ +struct fsnotify_ns data = { + .userns = userns, + .self_nsid = self_nsid, + .owner_nsid = owner_nsid, + }; + +if (WARN_ON_ONCE (!userns)) +return ; + +if (!READ_ONCE (userns->n_fsnotify_marks)) +return ; + +send_to_ns_groups (mask, &data,FSNOTIFY_EVENT_NS ); +} +static __init int fsnotify_init (void ) {int ret;
fs/notify/fsnotify.h +12 -0
@@ -6,6 +6,7 @@#include <linux/fsnotify.h> #include <linux/srcu.h> #include <linux/types.h> +#include <linux/user_namespace.h> #include "../mount.h"
@@ -39,6 +40,12 @@ static inline struct mnt_namespace *fsnotify_conn_mntns(return conn->obj; } +static inline struct user_namespace *fsnotify_conn_userns ( +struct fsnotify_mark_connector *conn) +{ +return conn->obj; +} +static inline struct super_block *fsnotify_object_sb (void *obj,enum fsnotify_obj_type obj_type) {
@@ -103,6 +110,11 @@ static inline void fsnotify_clear_marks_by_mntns(struct mnt_namespace *mntns)fsnotify_destroy_marks (&mntns->n_fsnotify_marks); } +static inline void fsnotify_clear_marks_by_userns (struct user_namespace *userns) +{ +fsnotify_destroy_marks (&userns->n_fsnotify_marks); +} +/* * update the dentry->d_flags of all of inode's children to indicate if inode cares * about events that happen to its children.
fs/notify/mark.c +7 -0
@@ -74,6 +74,7 @@#include <linux/atomic.h> #include <linux/fsnotify_backend.h> +#include <linux/user_namespace.h> #include "fsnotify.h" #define FSNOTIFY_REAPER_DELAY (1)/* 1 jiffy */
@@ -110,6 +111,8 @@ static fsnotify_connp_t *fsnotify_object_connp(void *obj,return fsnotify_sb_marks (obj);case FSNOTIFY_OBJ_TYPE_MNTNS :return &((struct mnt_namespace *)obj)->n_fsnotify_marks; +case FSNOTIFY_OBJ_TYPE_USERNS : +return &((struct user_namespace *)obj)->n_fsnotify_marks;default :return NULL ; }
@@ -125,6 +128,8 @@ static __u32 *fsnotify_conn_mask_p(struct fsnotify_mark_connector *conn)return &fsnotify_conn_sb (conn)->s_fsnotify_mask; else if (conn->type == FSNOTIFY_OBJ_TYPE_MNTNS) return &fsnotify_conn_mntns(conn)->n_fsnotify_mask; + else if (conn->type == FSNOTIFY_OBJ_TYPE_USERNS) + return &fsnotify_conn_userns(conn)->n_fsnotify_mask;return NULL ; }
@@ -389,6 +394,8 @@ static void *fsnotify_detach_connector_from_object(fsnotify_conn_sb (conn)->s_fsnotify_mask =0 ; }else if (conn->type ==FSNOTIFY_OBJ_TYPE_MNTNS ) {fsnotify_conn_mntns (conn)->n_fsnotify_mask =0 ; + }else if (conn->type ==FSNOTIFY_OBJ_TYPE_USERNS ) { +fsnotify_conn_userns (conn)->n_fsnotify_mask =0 ; }rcu_assign_pointer (*connp,NULL );
fs/nsfs.c +21 -0
@@ -387,6 +387,27 @@ bool proc_ns_file(const struct file *file)return file->f_op == &ns_file_operations; } +/** +* userns_from_dentry() - Return the user_namespace referenced by an nsfs dentry. +* @dentry: dentry of an open nsfs file +* +* Returns the user_namespace if @dentry is an nsfs file for a user namespace, +* NULL otherwise. The caller is responsible for ensuring the returned pointer +* remains valid (e.g. by holding a reference to the dentry). +*/ +struct user_namespace *userns_from_dentry (struct dentry *dentry) +{ +struct inode *inode =d_inode (dentry); +struct ns_common *ns; + +if (!inode || inode->i_sb->s_magic !=NSFS_MAGIC ) +return NULL ; + ns =get_proc_ns (inode); +if (!ns || ns->ns_type !=CLONE_NEWUSER ) +return NULL ; +return to_user_ns (ns); +} +/** * ns_match() - Returns true if current namespace matches dev/ino provided. * @ns: current namespace
include/linux/fanotify.h +7 -3
@@ -28,7 +28,7 @@#define FANOTIFY_INFO_MODES (FANOTIFY_FID_BITS | FAN_REPORT_PIDFD)/* fanotify_init() flags to create a namepsace event watcher */ -#define FANOTIFY_NS_INIT_FLAGS (FAN_REPORT_MNT) +#define FANOTIFY_NS_INIT_FLAGS (FAN_REPORT_MNT | FAN_REPORT_NSID)/* * fanotify_init() flags that require CAP_SYS_ADMIN.
@@ -62,7 +62,8 @@#define FANOTIFY_INTERNAL_GROUP_FLAGS (FANOTIFY_UNPRIV)#define FANOTIFY_MARK_TYPE_BITS (FAN_MARK_INODE | FAN_MARK_MOUNT | \ - FAN_MARK_FILESYSTEM | FAN_MARK_MNTNS) + FAN_MARK_FILESYSTEM | FAN_MARK_MNTNS | \ + FAN_MARK_USERNS)#define FANOTIFY_MARK_CMD_BITS (FAN_MARK_ADD | FAN_MARK_REMOVE | \ FAN_MARK_FLUSH)
@@ -122,8 +123,11 @@/* Mount tree monitoring events */ #define FANOTIFY_MOUNT_EVENTS (FAN_MNT_ATTACH | FAN_MNT_DETACH) +/* Namespace tree monitoring events */ +#define FANOTIFY_NS_EVENTS (FAN_NS_CREATE | FAN_NS_DELETE) +/* Events that user can request to be notified on namepsace watchers */ -#define FANOTIFY_EVENTS_ON_NS (FANOTIFY_MOUNT_EVENTS) +#define FANOTIFY_EVENTS_ON_NS (FANOTIFY_MOUNT_EVENTS | FANOTIFY_NS_EVENTS)/* Extra flags that may be reported with event or control handling of events */ #define FANOTIFY_EVENT_FLAGS (FAN_EVENT_ON_CHILD | FAN_ONDIR)
include/linux/fsnotify.h +5 -0
@@ -300,6 +300,11 @@ static inline void fsnotify_mntns_delete(struct mnt_namespace *mntns)__fsnotify_mntns_delete (mntns); } +static inline void fsnotify_userns_delete (struct user_namespace *userns) +{ +__fsnotify_userns_delete (userns); +} +/* * fsnotify_inoderemove - an inode is going away */
include/linux/fsnotify_backend.h +33 -0
@@ -79,6 +79,9 @@* * NOTE: These values may overload filesystem events, but not event flags */ +#define FS_NS_CREATE 0x00000100/* Sub namespace was created */ +#define FS_NS_DELETE 0x00000200/* Sub namespace was deleted */ +#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)
@@ -128,8 +131,12 @@/* Mount tree monitoring events */ #define FSNOTIFY_MNT_EVENTS (FS_MNT_ATTACH | FS_MNT_DETACH) +/* Namespace tree monitoring events */ +#define FSNOTIFY_NS_EVENTS (FS_NS_CREATE | FS_NS_DELETE) +/* Events that can be reported to backends on namepsace watchers */ #define FSNOTIFY_EVENTS_ON_NS (FSNOTIFY_MNT_EVENTS | \ + FSNOTIFY_NS_EVENTS | \ FS_Q_OVERFLOW)/* Events that can be reported to backends */
@@ -344,6 +351,7 @@ enum fsnotify_data_type {FSNOTIFY_EVENT_INODE ,FSNOTIFY_EVENT_DENTRY ,FSNOTIFY_EVENT_MNT , +FSNOTIFY_EVENT_NS , FSNOTIFY_EVENT_ERROR, };
@@ -369,6 +377,12 @@ struct fsnotify_mnt {u64 mnt_id; }; +struct fsnotify_ns { +const struct user_namespace *userns; +u64 self_nsid; +u64 owner_nsid; +}; +static inline struct inode *fsnotify_data_inode (const void *data,int data_type) {switch (data_type) {
@@ -445,6 +459,17 @@ static inline const struct fsnotify_mnt *fsnotify_data_mnt(const void *data, } } +static inline const struct fsnotify_ns *fsnotify_data_ns (const void *data, +int data_type) +{ +switch (data_type) { +case FSNOTIFY_EVENT_NS : +return data; +default : +return NULL ; + } +} +static inline u64 fsnotify_data_mnt_id (const void *data,int data_type) {const struct fsnotify_mnt *mnt_data =fsnotify_data_mnt (data, data_type);
@@ -490,6 +515,7 @@ enum fsnotify_iter_type {FSNOTIFY_ITER_TYPE_PARENT ,FSNOTIFY_ITER_TYPE_INODE2 ,FSNOTIFY_ITER_TYPE_MNTNS , +FSNOTIFY_ITER_TYPE_USERNS , FSNOTIFY_ITER_TYPE_COUNT };
@@ -500,6 +526,7 @@ enum fsnotify_obj_type {FSNOTIFY_OBJ_TYPE_VFSMOUNT ,FSNOTIFY_OBJ_TYPE_SB ,FSNOTIFY_OBJ_TYPE_MNTNS , +FSNOTIFY_OBJ_TYPE_USERNS ,FSNOTIFY_OBJ_TYPE_COUNT , FSNOTIFY_OBJ_TYPE_DETACHED = FSNOTIFY_OBJ_TYPE_COUNT };
@@ -688,9 +715,12 @@ extern void __fsnotify_inode_delete(struct inode *inode);extern void __fsnotify_vfsmount_delete (struct vfsmount *mnt);extern void fsnotify_sb_delete (struct super_block *sb);extern void __fsnotify_mntns_delete (struct mnt_namespace *mntns); +extern void __fsnotify_userns_delete (struct user_namespace *userns);extern void fsnotify_sb_free (struct super_block *sb);extern u32 fsnotify_get_cookie (void );extern void fsnotify_mnt (__u32 mask,struct mnt_namespace *ns,struct vfsmount *mnt); +extern void fsnotify_ns (__u32 mask,struct user_namespace *userns, +u64 self_nsid,u64 owner_nsid);static inline __u32 fsnotify_parent_needed_mask (__u32 mask) {
@@ -992,6 +1022,9 @@ static inline void fsnotify_sb_delete(struct super_block *sb)static inline void __fsnotify_mntns_delete (struct mnt_namespace *mntns) {} +static inline void __fsnotify_userns_delete (struct user_namespace *userns) +{} +static inline void fsnotify_sb_free (struct super_block *sb) {}
include/linux/proc_fs.h +2 -0
@@ -248,4 +248,6 @@ static inline struct pid_namespace *proc_pid_ns(struct super_block *sb)bool proc_ns_file (const struct file *file); +struct user_namespace *userns_from_dentry (struct dentry *dentry); +#endif /* _LINUX_PROC_FS_H */
include/linux/user_namespace.h +6 -0
@@ -13,6 +13,8 @@#include <linux/sysctl.h> #include <linux/err.h> +struct fsnotify_mark_connector ; +#define UID_GID_MAP_MAX_BASE_EXTENTS 5#define UID_GID_MAP_MAX_EXTENTS 340
@@ -86,6 +88,10 @@ struct user_namespace {/* parent_could_setfcap: true if the creator if this ns had CAP_SETFCAP * in its effective capability set at the child ns creation time. */ bool parent_could_setfcap; +#ifdef CONFIG_FSNOTIFY +__u32 n_fsnotify_mask; +struct fsnotify_mark_connector __rcu *n_fsnotify_marks; +#endif #ifdef CONFIG_KEYS /* List of joinable keyrings in this namespace. Modification access of
include/uapi/linux/fanotify.h +12 -0
@@ -48,6 +48,9 @@* * NOTE: These values may overload filesystem events, but not event flags */ +#define FAN_NS_CREATE 0x00000100/* Sub namespace was created */ +#define FAN_NS_DELETE 0x00000200/* Sub namespace was deleted */ +#define FAN_MNT_ATTACH 0x01000000/* Mount was attached */ #define FAN_MNT_DETACH 0x02000000/* Mount was detached */
@@ -78,6 +81,7 @@#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_NSID 0x00008000/* Report namespace events */ /* Convenience macro - FAN_REPORT_NAME requires FAN_REPORT_DIR_FID */ #define FAN_REPORT_DFID_NAME (FAN_REPORT_DIR_FID | FAN_REPORT_NAME)
@@ -109,6 +113,7 @@#define FAN_MARK_MOUNT 0x00000010#define FAN_MARK_FILESYSTEM 0x00000100#define FAN_MARK_MNTNS 0x00000110 +#define FAN_MARK_USERNS 0x00001000/* * Convenience macro - FAN_MARK_IGNORE requires FAN_MARK_IGNORED_SURV_MODIFY
@@ -163,6 +168,7 @@ struct fanotify_event_metadata {#define FAN_EVENT_INFO_TYPE_ERROR 5#define FAN_EVENT_INFO_TYPE_RANGE 6#define FAN_EVENT_INFO_TYPE_MNT 7 +#define FAN_EVENT_INFO_TYPE_NS 8/* Special info types for FAN_RENAME */ #define FAN_EVENT_INFO_TYPE_OLD_DFID_NAME 10
@@ -221,6 +227,12 @@ struct fanotify_event_info_mnt {__u64 mnt_id; }; +struct fanotify_event_info_ns { +struct fanotify_event_info_header hdr; +__u64 self_nsid;/* ns_id of the namespace */ +__u64 owner_nsid;/* ns_id of its owning user namespace */ +}; +/* * User space may need to record additional information about its decision. * The extra information type records what kind of information is included.
kernel/nscommon.c +47 -0
83 lines not on this page · raw message
kernel/user_namespace.c +2 -0
@@ -22,6 +22,7 @@#include <linux/bsearch.h> #include <linux/sort.h> #include <linux/nstree.h> +#include <linux/fsnotify.h> static struct kmem_cache *user_ns_cachep__ro_after_init ;static DEFINE_MUTEX (userns_state_mutex);
@@ -221,6 +222,7 @@ static void free_user_ns(struct work_struct *work)retire_userns_sysctls (ns);key_free_user_ns (ns);ns_common_free (ns); +fsnotify_userns_delete (ns);/* Concurrent nstree traversal depends on a grace period. */ kfree_rcu (ns, ns.ns_rcu);dec_user_namespaces (ucounts);
Signature · 1 line
2.54.0
Amir Goldstein
reviewerPATCH v2 9/10 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
@@ -36,7 +36,7 @@ TARGETS += filesystems/epoll TARGETS += filesystems/fat TARGETS += filesystems/overlayfs TARGETS += filesystems/statmount -TARGETS += filesystems/mount-notify +TARGETS += filesystems/fanotify TARGETS += filesystems/fuse TARGETS += filesystems/move_mount TARGETS += filesystems/empty_mntns
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.54.0
Amir Goldstein
reviewerPATCH v2 10/10 selftests/filesystems: add fanotify namespace notifications test
In reply to Amir GoldsteinTest create and delete events for nsfs:
- For init userns and child userns
- Verify delete event is created regardless of vfs inode access
- Verify required ns capabilities
--- tools/include/uapi/linux/fanotify.h | 37 +- .../selftests/filesystems/fanotify/Makefile | 3 +- .../filesystems/fanotify/ns-notify_test.c | 330 ++++++++++++++++++ 3 files changed, 362 insertions(+), 8 deletions(-) create mode 100644 tools/testing/selftests/filesystems/fanotify/ns-notify_test.c
tools/include/uapi/linux/fanotify.h +30 -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,31 @@/* #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_NS_CREATE 0x00000100/* Sub namespace was created */ +#define FAN_NS_DELETE 0x00000200/* Sub namespace was deleted */ + +#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
@@ -67,6 +81,7 @@#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_NSID 0x00008000/* Report namespace events */ /* Convenience macro - FAN_REPORT_NAME requires FAN_REPORT_DIR_FID */ #define FAN_REPORT_DFID_NAME (FAN_REPORT_DIR_FID | FAN_REPORT_NAME)
@@ -98,6 +113,7 @@#define FAN_MARK_MOUNT 0x00000010#define FAN_MARK_FILESYSTEM 0x00000100#define FAN_MARK_MNTNS 0x00000110 +#define FAN_MARK_USERNS 0x00001000/* * Convenience macro - FAN_MARK_IGNORE requires FAN_MARK_IGNORED_SURV_MODIFY
@@ -152,6 +168,7 @@ struct fanotify_event_metadata {#define FAN_EVENT_INFO_TYPE_ERROR 5#define FAN_EVENT_INFO_TYPE_RANGE 6#define FAN_EVENT_INFO_TYPE_MNT 7 +#define FAN_EVENT_INFO_TYPE_NS 8/* Special info types for FAN_RENAME */ #define FAN_EVENT_INFO_TYPE_OLD_DFID_NAME 10
@@ -210,6 +227,12 @@ struct fanotify_event_info_mnt {__u64 mnt_id; }; +struct fanotify_event_info_ns { +struct fanotify_event_info_header hdr; +__u64 self_nsid;/* ns_id of the namespace */ +__u64 owner_nsid;/* ns_id of its owning user namespace */ +}; +/* * User space may need to record additional information about its decision. * The extra information type records what kind of information is included.
tools/testing/selftests/filesystems/fanotify/Makefile +2 -1
@@ -3,9 +3,10 @@ CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES) LDLIBS += -lcap -TEST_GEN_PROGS := mount-notify_test mount-notify_test_ns +TEST_GEN_PROGS := mount-notify_test mount-notify_test_ns ns-notify_test include ../../lib.mk $(OUTPUT)/mount-notify_test: ../utils.c $(OUTPUT)/mount-notify_test_ns: ../utils.c +$(OUTPUT)/ns-notify_test: ../utils.c
tools/testing/selftests/filesystems/fanotify/ns-notify_test.c new file +330 -0
330 lines not on this page · raw message
Signature · 1 line
2.54.0