Commit 8afd7215 authored by Amir Goldstein's avatar Amir Goldstein Committed by Jan Kara

fanotify: cleanups for fanotify_mark() input validations

Create helper fanotify_may_update_existing_mark() for checking for
conflicts between existing mark flags and fanotify_mark() flags.

Use variable mark_cmd to make the checks for mark command bits
cleaner.

Link: https://lore.kernel.org/r/20220629144210.2983229-3-amir73il@gmail.comSigned-off-by: default avatarAmir Goldstein <amir73il@gmail.com>
Signed-off-by: default avatarJan Kara <jack@suse.cz>
parent 31a371e4
...@@ -1187,6 +1187,19 @@ static int fanotify_group_init_error_pool(struct fsnotify_group *group) ...@@ -1187,6 +1187,19 @@ static int fanotify_group_init_error_pool(struct fsnotify_group *group)
sizeof(struct fanotify_error_event)); sizeof(struct fanotify_error_event));
} }
static int fanotify_may_update_existing_mark(struct fsnotify_mark *fsn_mark,
unsigned int fan_flags)
{
/*
* Non evictable mark cannot be downgraded to evictable mark.
*/
if (fan_flags & FAN_MARK_EVICTABLE &&
!(fsn_mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF))
return -EEXIST;
return 0;
}
static int fanotify_add_mark(struct fsnotify_group *group, static int fanotify_add_mark(struct fsnotify_group *group,
fsnotify_connp_t *connp, unsigned int obj_type, fsnotify_connp_t *connp, unsigned int obj_type,
__u32 mask, unsigned int fan_flags, __u32 mask, unsigned int fan_flags,
...@@ -1208,13 +1221,11 @@ static int fanotify_add_mark(struct fsnotify_group *group, ...@@ -1208,13 +1221,11 @@ static int fanotify_add_mark(struct fsnotify_group *group,
} }
/* /*
* Non evictable mark cannot be downgraded to evictable mark. * Check if requested mark flags conflict with an existing mark flags.
*/ */
if (fan_flags & FAN_MARK_EVICTABLE && ret = fanotify_may_update_existing_mark(fsn_mark, fan_flags);
!(fsn_mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF)) { if (ret)
ret = -EEXIST;
goto out; goto out;
}
/* /*
* Error events are pre-allocated per group, only if strictly * Error events are pre-allocated per group, only if strictly
...@@ -1557,6 +1568,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, ...@@ -1557,6 +1568,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
__kernel_fsid_t __fsid, *fsid = NULL; __kernel_fsid_t __fsid, *fsid = NULL;
u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS; u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS;
unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS; unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS;
unsigned int mark_cmd = flags & FANOTIFY_MARK_CMD_BITS;
bool ignore = flags & FAN_MARK_IGNORED_MASK; bool ignore = flags & FAN_MARK_IGNORED_MASK;
unsigned int obj_type, fid_mode; unsigned int obj_type, fid_mode;
u32 umask = 0; u32 umask = 0;
...@@ -1586,7 +1598,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, ...@@ -1586,7 +1598,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
return -EINVAL; return -EINVAL;
} }
switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) { switch (mark_cmd) {
case FAN_MARK_ADD: case FAN_MARK_ADD:
case FAN_MARK_REMOVE: case FAN_MARK_REMOVE:
if (!mask) if (!mask)
...@@ -1675,7 +1687,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, ...@@ -1675,7 +1687,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
if (mask & FAN_RENAME && !(fid_mode & FAN_REPORT_NAME)) if (mask & FAN_RENAME && !(fid_mode & FAN_REPORT_NAME))
goto fput_and_out; goto fput_and_out;
if (flags & FAN_MARK_FLUSH) { if (mark_cmd == FAN_MARK_FLUSH) {
ret = 0; ret = 0;
if (mark_type == FAN_MARK_MOUNT) if (mark_type == FAN_MARK_MOUNT)
fsnotify_clear_vfsmount_marks_by_group(group); fsnotify_clear_vfsmount_marks_by_group(group);
...@@ -1691,7 +1703,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, ...@@ -1691,7 +1703,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
if (ret) if (ret)
goto fput_and_out; goto fput_and_out;
if (flags & FAN_MARK_ADD) { if (mark_cmd == FAN_MARK_ADD) {
ret = fanotify_events_supported(group, &path, mask, flags); ret = fanotify_events_supported(group, &path, mask, flags);
if (ret) if (ret)
goto path_put_and_out; goto path_put_and_out;
...@@ -1729,7 +1741,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, ...@@ -1729,7 +1741,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
} }
/* create/update an inode mark */ /* create/update an inode mark */
switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) { switch (mark_cmd) {
case FAN_MARK_ADD: case FAN_MARK_ADD:
if (mark_type == FAN_MARK_MOUNT) if (mark_type == FAN_MARK_MOUNT)
ret = fanotify_add_vfsmount_mark(group, mnt, mask, ret = fanotify_add_vfsmount_mark(group, mnt, mask,
......
...@@ -59,15 +59,16 @@ ...@@ -59,15 +59,16 @@
#define FANOTIFY_MARK_TYPE_BITS (FAN_MARK_INODE | FAN_MARK_MOUNT | \ #define FANOTIFY_MARK_TYPE_BITS (FAN_MARK_INODE | FAN_MARK_MOUNT | \
FAN_MARK_FILESYSTEM) FAN_MARK_FILESYSTEM)
#define FANOTIFY_MARK_CMD_BITS (FAN_MARK_ADD | FAN_MARK_REMOVE | \
FAN_MARK_FLUSH)
#define FANOTIFY_MARK_FLAGS (FANOTIFY_MARK_TYPE_BITS | \ #define FANOTIFY_MARK_FLAGS (FANOTIFY_MARK_TYPE_BITS | \
FAN_MARK_ADD | \ FANOTIFY_MARK_CMD_BITS | \
FAN_MARK_REMOVE | \
FAN_MARK_DONT_FOLLOW | \ FAN_MARK_DONT_FOLLOW | \
FAN_MARK_ONLYDIR | \ FAN_MARK_ONLYDIR | \
FAN_MARK_IGNORED_MASK | \ FAN_MARK_IGNORED_MASK | \
FAN_MARK_IGNORED_SURV_MODIFY | \ FAN_MARK_IGNORED_SURV_MODIFY | \
FAN_MARK_EVICTABLE | \ FAN_MARK_EVICTABLE)
FAN_MARK_FLUSH)
/* /*
* Events that can be reported with data type FSNOTIFY_EVENT_PATH. * Events that can be reported with data type FSNOTIFY_EVENT_PATH.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment