^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/fanotify.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/anon_inodes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/fsnotify_backend.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/syscalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/memcontrol.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/statfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/exportfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <asm/ioctls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include "../../mount.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include "../fdinfo.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include "fanotify.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define FANOTIFY_DEFAULT_MAX_EVENTS 16384
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define FANOTIFY_DEFAULT_MAX_MARKS 8192
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define FANOTIFY_DEFAULT_MAX_LISTENERS 128
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * All flags that may be specified in parameter event_f_flags of fanotify_init.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * Internal and external open flags are stored together in field f_flags of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * struct file. Only external open flags shall be allowed in event_f_flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * Internal flags like FMODE_NONOTIFY, FMODE_EXEC, FMODE_NOCMTIME shall be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * excluded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define FANOTIFY_INIT_ALL_EVENT_F_BITS ( \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) O_ACCMODE | O_APPEND | O_NONBLOCK | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) __O_SYNC | O_DSYNC | O_CLOEXEC | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) O_LARGEFILE | O_NOATIME )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) extern const struct fsnotify_ops fanotify_fsnotify_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct kmem_cache *fanotify_mark_cache __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct kmem_cache *fanotify_fid_event_cachep __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct kmem_cache *fanotify_path_event_cachep __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct kmem_cache *fanotify_perm_event_cachep __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #define FANOTIFY_EVENT_ALIGN 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #define FANOTIFY_INFO_HDR_LEN \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) (sizeof(struct fanotify_event_info_fid) + sizeof(struct file_handle))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static int fanotify_fid_info_len(int fh_len, int name_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) int info_len = fh_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (name_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) info_len += name_len + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return roundup(FANOTIFY_INFO_HDR_LEN + info_len, FANOTIFY_EVENT_ALIGN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static int fanotify_event_info_len(unsigned int fid_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct fanotify_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct fanotify_info *info = fanotify_event_info(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int dir_fh_len = fanotify_event_dir_fh_len(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) int fh_len = fanotify_event_object_fh_len(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) int info_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) int dot_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (dir_fh_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) info_len += fanotify_fid_info_len(dir_fh_len, info->name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) } else if ((fid_mode & FAN_REPORT_NAME) && (event->mask & FAN_ONDIR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * With group flag FAN_REPORT_NAME, if name was not recorded in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * event on a directory, we will report the name ".".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) dot_len = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (fh_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) info_len += fanotify_fid_info_len(fh_len, dot_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return info_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * Get an fanotify notification event if one exists and is small
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * enough to fit in "count". Return an error pointer if the count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * is not large enough. When permission event is dequeued, its state is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * updated accordingly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static struct fanotify_event *get_one_event(struct fsnotify_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) size_t event_size = FAN_EVENT_METADATA_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct fanotify_event *event = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) spin_lock(&group->notification_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (fsnotify_notify_queue_is_empty(group))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (fid_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) event_size += fanotify_event_info_len(fid_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) FANOTIFY_E(fsnotify_peek_first_event(group)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (event_size > count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) event = ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) event = FANOTIFY_E(fsnotify_remove_first_event(group));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (fanotify_is_perm_event(event->mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) FANOTIFY_PERM(event)->state = FAN_EVENT_REPORTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) spin_unlock(&group->notification_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static int create_fd(struct fsnotify_group *group, struct path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct file **file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) int client_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) struct file *new_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) client_fd = get_unused_fd_flags(group->fanotify_data.f_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (client_fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return client_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * we need a new file handle for the userspace program so it can read even if it was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * originally opened O_WRONLY.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) new_file = dentry_open(path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) group->fanotify_data.f_flags | FMODE_NONOTIFY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) current_cred());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (IS_ERR(new_file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * we still send an event even if we can't open the file. this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * can happen when say tasks are gone and we try to open their
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * /proc files or we try to open a WRONLY file like in sysfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * we just send the errno to userspace since there isn't much
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * else we can do.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) put_unused_fd(client_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) client_fd = PTR_ERR(new_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) *file = new_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return client_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * Finish processing of permission event by setting it to ANSWERED state and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * drop group->notification_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static void finish_permission_event(struct fsnotify_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct fanotify_perm_event *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) unsigned int response)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) __releases(&group->notification_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) bool destroy = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) assert_spin_locked(&group->notification_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) event->response = response;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (event->state == FAN_EVENT_CANCELED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) destroy = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) event->state = FAN_EVENT_ANSWERED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) spin_unlock(&group->notification_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (destroy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) fsnotify_destroy_event(group, &event->fae.fse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static int process_access_response(struct fsnotify_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) struct fanotify_response *response_struct)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) struct fanotify_perm_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) int fd = response_struct->fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) int response = response_struct->response;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) fd, response);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * make sure the response is valid, if invalid we do nothing and either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * userspace can send a valid response or we will clean it up after the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * timeout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) switch (response & ~FAN_AUDIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) case FAN_ALLOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) case FAN_DENY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if ((response & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) spin_lock(&group->notification_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) list_for_each_entry(event, &group->fanotify_data.access_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) fae.fse.list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (event->fd != fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) list_del_init(&event->fae.fse.list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) finish_permission_event(group, event, response);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) wake_up(&group->fanotify_data.access_waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) spin_unlock(&group->notification_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static int copy_info_to_user(__kernel_fsid_t *fsid, struct fanotify_fh *fh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) int info_type, const char *name, size_t name_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) char __user *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) struct fanotify_event_info_fid info = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) struct file_handle handle = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) unsigned char bounce[FANOTIFY_INLINE_FH_LEN], *fh_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) size_t fh_len = fh ? fh->len : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) size_t info_len = fanotify_fid_info_len(fh_len, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) size_t len = info_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) pr_debug("%s: fh_len=%zu name_len=%zu, info_len=%zu, count=%zu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) __func__, fh_len, name_len, info_len, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (!fh_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (WARN_ON_ONCE(len < sizeof(info) || len > count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * Copy event info fid header followed by variable sized file handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * and optionally followed by variable sized filename.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) switch (info_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) case FAN_EVENT_INFO_TYPE_FID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) case FAN_EVENT_INFO_TYPE_DFID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) if (WARN_ON_ONCE(name_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) case FAN_EVENT_INFO_TYPE_DFID_NAME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (WARN_ON_ONCE(!name || !name_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) info.hdr.info_type = info_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) info.hdr.len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) info.fsid = *fsid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (copy_to_user(buf, &info, sizeof(info)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) buf += sizeof(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) len -= sizeof(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (WARN_ON_ONCE(len < sizeof(handle)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) handle.handle_type = fh->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) handle.handle_bytes = fh_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (copy_to_user(buf, &handle, sizeof(handle)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) buf += sizeof(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) len -= sizeof(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (WARN_ON_ONCE(len < fh_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * For an inline fh and inline file name, copy through stack to exclude
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * the copy from usercopy hardening protections.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) fh_buf = fanotify_fh_buf(fh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (fh_len <= FANOTIFY_INLINE_FH_LEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) memcpy(bounce, fh_buf, fh_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) fh_buf = bounce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (copy_to_user(buf, fh_buf, fh_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) buf += fh_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) len -= fh_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (name_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) /* Copy the filename with terminating null */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) name_len++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (WARN_ON_ONCE(len < name_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (copy_to_user(buf, name, name_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) buf += name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) len -= name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) /* Pad with 0's */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) WARN_ON_ONCE(len < 0 || len >= FANOTIFY_EVENT_ALIGN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) if (len > 0 && clear_user(buf, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) return info_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) static ssize_t copy_event_to_user(struct fsnotify_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) struct fanotify_event *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) char __user *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) struct fanotify_event_metadata metadata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) struct path *path = fanotify_event_path(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) struct fanotify_info *info = fanotify_event_info(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) struct file *f = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) int ret, fd = FAN_NOFD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) int info_type = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) pr_debug("%s: group=%p event=%p\n", __func__, group, event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) metadata.event_len = FAN_EVENT_METADATA_LEN +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) fanotify_event_info_len(fid_mode, event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) metadata.metadata_len = FAN_EVENT_METADATA_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) metadata.vers = FANOTIFY_METADATA_VERSION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) metadata.reserved = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) metadata.mask = event->mask & FANOTIFY_OUTGOING_EVENTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) metadata.pid = pid_vnr(event->pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (path && path->mnt && path->dentry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) fd = create_fd(group, path, &f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) return fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) metadata.fd = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * Sanity check copy size in case get_one_event() and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) * event_len sizes ever get out of sync.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) if (WARN_ON_ONCE(metadata.event_len > count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) goto out_close_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) if (copy_to_user(buf, &metadata, FAN_EVENT_METADATA_LEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) goto out_close_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) buf += FAN_EVENT_METADATA_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) count -= FAN_EVENT_METADATA_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (fanotify_is_perm_event(event->mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) FANOTIFY_PERM(event)->fd = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) /* Event info records order is: dir fid + name, child fid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (fanotify_event_dir_fh_len(event)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) info_type = info->name_len ? FAN_EVENT_INFO_TYPE_DFID_NAME :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) FAN_EVENT_INFO_TYPE_DFID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) ret = copy_info_to_user(fanotify_event_fsid(event),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) fanotify_info_dir_fh(info),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) info_type, fanotify_info_name(info),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) info->name_len, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) goto out_close_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) buf += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) count -= ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) if (fanotify_event_object_fh_len(event)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) const char *dot = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) int dot_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) if (fid_mode == FAN_REPORT_FID || info_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) * With only group flag FAN_REPORT_FID only type FID is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) * reported. Second info record type is always FID.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) info_type = FAN_EVENT_INFO_TYPE_FID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) } else if ((fid_mode & FAN_REPORT_NAME) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) (event->mask & FAN_ONDIR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) * With group flag FAN_REPORT_NAME, if name was not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) * recorded in an event on a directory, report the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) * name "." with info type DFID_NAME.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) info_type = FAN_EVENT_INFO_TYPE_DFID_NAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) dot = ".";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) dot_len = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) } else if ((event->mask & ALL_FSNOTIFY_DIRENT_EVENTS) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) (event->mask & FAN_ONDIR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) * With group flag FAN_REPORT_DIR_FID, a single info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) * record has type DFID for directory entry modification
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) * event and for event on a directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) info_type = FAN_EVENT_INFO_TYPE_DFID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) * With group flags FAN_REPORT_DIR_FID|FAN_REPORT_FID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) * a single info record has type FID for event on a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) * non-directory, when there is no directory to report.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) * For example, on FAN_DELETE_SELF event.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) info_type = FAN_EVENT_INFO_TYPE_FID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) ret = copy_info_to_user(fanotify_event_fsid(event),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) fanotify_event_object_fh(event),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) info_type, dot, dot_len, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) goto out_close_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) buf += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) count -= ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) if (f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) fd_install(fd, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) return metadata.event_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) out_close_fd:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) if (fd != FAN_NOFD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) put_unused_fd(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) fput(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) /* intofiy userspace file descriptor functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) static __poll_t fanotify_poll(struct file *file, poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) struct fsnotify_group *group = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) __poll_t ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) poll_wait(file, &group->notification_waitq, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) spin_lock(&group->notification_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) if (!fsnotify_notify_queue_is_empty(group))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) ret = EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) spin_unlock(&group->notification_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) static ssize_t fanotify_read(struct file *file, char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) size_t count, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) struct fsnotify_group *group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) struct fanotify_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) char __user *start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) DEFINE_WAIT_FUNC(wait, woken_wake_function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) start = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) group = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) pr_debug("%s: group=%p\n", __func__, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) add_wait_queue(&group->notification_waitq, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) * User can supply arbitrarily large buffer. Avoid softlockups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) * in case there are lots of available events.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) event = get_one_event(group, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) if (IS_ERR(event)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) ret = PTR_ERR(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) if (!event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) if (file->f_flags & O_NONBLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) ret = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) if (signal_pending(current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) if (start != buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) ret = copy_event_to_user(group, event, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) if (unlikely(ret == -EOPENSTALE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) * We cannot report events with stale fd so drop it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) * Setting ret to 0 will continue the event loop and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) * do the right thing if there are no more events to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) * read (i.e. return bytes read, -EAGAIN or wait).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) * Permission events get queued to wait for response. Other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) * events can be destroyed now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) if (!fanotify_is_perm_event(event->mask)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) fsnotify_destroy_event(group, &event->fse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) if (ret <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) spin_lock(&group->notification_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) finish_permission_event(group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) FANOTIFY_PERM(event), FAN_DENY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) wake_up(&group->fanotify_data.access_waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) spin_lock(&group->notification_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) list_add_tail(&event->fse.list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) &group->fanotify_data.access_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) spin_unlock(&group->notification_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) buf += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) count -= ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) remove_wait_queue(&group->notification_waitq, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) if (start != buf && ret != -EFAULT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) ret = buf - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) struct fanotify_response response = { .fd = -1, .response = -1 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) struct fsnotify_group *group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) group = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) if (count < sizeof(response))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) count = sizeof(response);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) if (copy_from_user(&response, buf, count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) ret = process_access_response(group, &response);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) count = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) static int fanotify_release(struct inode *ignored, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) struct fsnotify_group *group = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) * Stop new events from arriving in the notification queue. since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) * userspace cannot use fanotify fd anymore, no event can enter or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) * leave access_list by now either.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) fsnotify_group_stop_queueing(group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) * Process all permission events on access_list and notification queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) * and simulate reply from userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) spin_lock(&group->notification_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) while (!list_empty(&group->fanotify_data.access_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) struct fanotify_perm_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) event = list_first_entry(&group->fanotify_data.access_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) struct fanotify_perm_event, fae.fse.list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) list_del_init(&event->fae.fse.list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) finish_permission_event(group, event, FAN_ALLOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) spin_lock(&group->notification_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) * Destroy all non-permission events. For permission events just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) * dequeue them and set the response. They will be freed once the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) * response is consumed and fanotify_get_response() returns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) while (!fsnotify_notify_queue_is_empty(group)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) struct fanotify_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) event = FANOTIFY_E(fsnotify_remove_first_event(group));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) if (!(event->mask & FANOTIFY_PERM_EVENTS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) spin_unlock(&group->notification_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) fsnotify_destroy_event(group, &event->fse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) finish_permission_event(group, FANOTIFY_PERM(event),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) FAN_ALLOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) spin_lock(&group->notification_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) spin_unlock(&group->notification_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) /* Response for all permission events it set, wakeup waiters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) wake_up(&group->fanotify_data.access_waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) /* matches the fanotify_init->fsnotify_alloc_group */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) fsnotify_destroy_group(group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) struct fsnotify_group *group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) struct fsnotify_event *fsn_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) void __user *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) int ret = -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) size_t send_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) group = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) p = (void __user *) arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) case FIONREAD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) spin_lock(&group->notification_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) list_for_each_entry(fsn_event, &group->notification_list, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) send_len += FAN_EVENT_METADATA_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) spin_unlock(&group->notification_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) ret = put_user(send_len, (int __user *) p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) static const struct file_operations fanotify_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) .show_fdinfo = fanotify_show_fdinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) .poll = fanotify_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) .read = fanotify_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) .write = fanotify_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) .fasync = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) .release = fanotify_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) .unlocked_ioctl = fanotify_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) .compat_ioctl = compat_ptr_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) .llseek = noop_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) static int fanotify_find_path(int dfd, const char __user *filename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) struct path *path, unsigned int flags, __u64 mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) unsigned int obj_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) dfd, filename, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) if (filename == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) struct fd f = fdget(dfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) ret = -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) if (!f.file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) ret = -ENOTDIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) if ((flags & FAN_MARK_ONLYDIR) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) !(S_ISDIR(file_inode(f.file)->i_mode))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) fdput(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) *path = f.file->f_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) path_get(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) fdput(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) unsigned int lookup_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) if (!(flags & FAN_MARK_DONT_FOLLOW))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) lookup_flags |= LOOKUP_FOLLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) if (flags & FAN_MARK_ONLYDIR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) lookup_flags |= LOOKUP_DIRECTORY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) ret = user_path_at(dfd, filename, lookup_flags, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) /* you can only watch an inode if you have read permissions on it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) ret = inode_permission(path->dentry->d_inode, MAY_READ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) path_put(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) ret = security_path_notify(path, mask, obj_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) path_put(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) __u32 mask, unsigned int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) __u32 umask, int *destroy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) __u32 oldmask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) /* umask bits cannot be removed by user */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) mask &= ~umask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) spin_lock(&fsn_mark->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) if (!(flags & FAN_MARK_IGNORED_MASK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) oldmask = fsn_mark->mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) fsn_mark->mask &= ~mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) fsn_mark->ignored_mask &= ~mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) * We need to keep the mark around even if remaining mask cannot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) * result in any events (e.g. mask == FAN_ONDIR) to support incremenal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) * changes to the mask.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) * Destroy mark when only umask bits remain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) *destroy = !((fsn_mark->mask | fsn_mark->ignored_mask) & ~umask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) spin_unlock(&fsn_mark->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) return mask & oldmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) static int fanotify_remove_mark(struct fsnotify_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) fsnotify_connp_t *connp, __u32 mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) unsigned int flags, __u32 umask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) struct fsnotify_mark *fsn_mark = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) __u32 removed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) int destroy_mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) mutex_lock(&group->mark_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) fsn_mark = fsnotify_find_mark(connp, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) if (!fsn_mark) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) mutex_unlock(&group->mark_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) umask, &destroy_mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) if (removed & fsnotify_conn_mask(fsn_mark->connector))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) fsnotify_recalc_mask(fsn_mark->connector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) if (destroy_mark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) fsnotify_detach_mark(fsn_mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) mutex_unlock(&group->mark_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) if (destroy_mark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) fsnotify_free_mark(fsn_mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) /* matches the fsnotify_find_mark() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) fsnotify_put_mark(fsn_mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) struct vfsmount *mnt, __u32 mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) unsigned int flags, __u32 umask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) return fanotify_remove_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) mask, flags, umask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) static int fanotify_remove_sb_mark(struct fsnotify_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) struct super_block *sb, __u32 mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) unsigned int flags, __u32 umask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) return fanotify_remove_mark(group, &sb->s_fsnotify_marks, mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) flags, umask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) static int fanotify_remove_inode_mark(struct fsnotify_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) struct inode *inode, __u32 mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) unsigned int flags, __u32 umask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) return fanotify_remove_mark(group, &inode->i_fsnotify_marks, mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) flags, umask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) __u32 mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) __u32 oldmask = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) spin_lock(&fsn_mark->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) if (!(flags & FAN_MARK_IGNORED_MASK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) oldmask = fsn_mark->mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) fsn_mark->mask |= mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) fsn_mark->ignored_mask |= mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) spin_unlock(&fsn_mark->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) return mask & ~oldmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) fsnotify_connp_t *connp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) unsigned int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) __kernel_fsid_t *fsid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) struct fsnotify_mark *mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) return ERR_PTR(-ENOSPC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) if (!mark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) fsnotify_init_mark(mark, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) ret = fsnotify_add_mark_locked(mark, connp, type, 0, fsid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) fsnotify_put_mark(mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) return mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) static int fanotify_add_mark(struct fsnotify_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) fsnotify_connp_t *connp, unsigned int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) __u32 mask, unsigned int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) __kernel_fsid_t *fsid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) struct fsnotify_mark *fsn_mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) __u32 added;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) mutex_lock(&group->mark_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) fsn_mark = fsnotify_find_mark(connp, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) if (!fsn_mark) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) fsn_mark = fanotify_add_new_mark(group, connp, type, fsid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) if (IS_ERR(fsn_mark)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) mutex_unlock(&group->mark_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) return PTR_ERR(fsn_mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) if (added & ~fsnotify_conn_mask(fsn_mark->connector))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) fsnotify_recalc_mask(fsn_mark->connector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) mutex_unlock(&group->mark_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) fsnotify_put_mark(fsn_mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) struct vfsmount *mnt, __u32 mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) unsigned int flags, __kernel_fsid_t *fsid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags, fsid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) static int fanotify_add_sb_mark(struct fsnotify_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) struct super_block *sb, __u32 mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) unsigned int flags, __kernel_fsid_t *fsid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) return fanotify_add_mark(group, &sb->s_fsnotify_marks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) FSNOTIFY_OBJ_TYPE_SB, mask, flags, fsid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) static int fanotify_add_inode_mark(struct fsnotify_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) struct inode *inode, __u32 mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) unsigned int flags, __kernel_fsid_t *fsid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) * If some other task has this inode open for write we should not add
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) * an ignored mark, unless that ignored mark is supposed to survive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) * modification changes anyway.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) if ((flags & FAN_MARK_IGNORED_MASK) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) inode_is_open_for_write(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) return fanotify_add_mark(group, &inode->i_fsnotify_marks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) FSNOTIFY_OBJ_TYPE_INODE, mask, flags, fsid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) static struct fsnotify_event *fanotify_alloc_overflow_event(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) struct fanotify_event *oevent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) oevent = kmalloc(sizeof(*oevent), GFP_KERNEL_ACCOUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) if (!oevent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) fanotify_init_event(oevent, 0, FS_Q_OVERFLOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) oevent->type = FANOTIFY_EVENT_TYPE_OVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) return &oevent->fse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) /* fanotify syscalls */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) struct fsnotify_group *group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) int f_flags, fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) struct user_struct *user;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) unsigned int fid_mode = flags & FANOTIFY_FID_BITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) unsigned int class = flags & FANOTIFY_CLASS_BITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) pr_debug("%s: flags=%x event_f_flags=%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) __func__, flags, event_f_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) #ifdef CONFIG_AUDITSYSCALL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) if (flags & ~(FANOTIFY_INIT_FLAGS | FAN_ENABLE_AUDIT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) if (flags & ~FANOTIFY_INIT_FLAGS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) switch (event_f_flags & O_ACCMODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) case O_RDONLY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) case O_RDWR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) case O_WRONLY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) if (fid_mode && class != FAN_CLASS_NOTIF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) * Child name is reported with parent fid so requires dir fid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) * We can report both child fid and dir fid with or without name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) if ((fid_mode & FAN_REPORT_NAME) && !(fid_mode & FAN_REPORT_DIR_FID))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) user = get_current_user();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) free_uid(user);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) return -EMFILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) f_flags = O_RDWR | FMODE_NONOTIFY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) if (flags & FAN_CLOEXEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) f_flags |= O_CLOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) if (flags & FAN_NONBLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) f_flags |= O_NONBLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) if (IS_ERR(group)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) free_uid(user);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) return PTR_ERR(group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) group->fanotify_data.user = user;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) group->fanotify_data.flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) atomic_inc(&user->fanotify_listeners);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) group->memcg = get_mem_cgroup_from_mm(current->mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) group->overflow_event = fanotify_alloc_overflow_event();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) if (unlikely(!group->overflow_event)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) fd = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) goto out_destroy_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) if (force_o_largefile())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) event_f_flags |= O_LARGEFILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) group->fanotify_data.f_flags = event_f_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) init_waitqueue_head(&group->fanotify_data.access_waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) INIT_LIST_HEAD(&group->fanotify_data.access_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) switch (class) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) case FAN_CLASS_NOTIF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) group->priority = FS_PRIO_0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) case FAN_CLASS_CONTENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) group->priority = FS_PRIO_1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) case FAN_CLASS_PRE_CONTENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) group->priority = FS_PRIO_2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) fd = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) goto out_destroy_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) if (flags & FAN_UNLIMITED_QUEUE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) fd = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) goto out_destroy_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) group->max_events = UINT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) if (flags & FAN_UNLIMITED_MARKS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) fd = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) goto out_destroy_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) group->fanotify_data.max_marks = UINT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) if (flags & FAN_ENABLE_AUDIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) fd = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) if (!capable(CAP_AUDIT_WRITE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) goto out_destroy_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) goto out_destroy_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) return fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) out_destroy_group:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) fsnotify_destroy_group(group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) return fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) /* Check if filesystem can encode a unique fid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) static int fanotify_test_fid(struct path *path, __kernel_fsid_t *fsid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) __kernel_fsid_t root_fsid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) * Make sure path is not in filesystem with zero fsid (e.g. tmpfs).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) err = vfs_get_fsid(path->dentry, fsid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) if (!fsid->val[0] && !fsid->val[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) * Make sure path is not inside a filesystem subvolume (e.g. btrfs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) * which uses a different fsid than sb root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) err = vfs_get_fsid(path->dentry->d_sb->s_root, &root_fsid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) if (root_fsid.val[0] != fsid->val[0] ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) root_fsid.val[1] != fsid->val[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) return -EXDEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) * We need to make sure that the file system supports at least
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) * encoding a file handle so user can use name_to_handle_at() to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) * compare fid returned with event to the file handle of watched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) * objects. However, name_to_handle_at() requires that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) * filesystem also supports decoding file handles.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) if (!path->dentry->d_sb->s_export_op ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) !path->dentry->d_sb->s_export_op->fh_to_dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) static int fanotify_events_supported(struct path *path, __u64 mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) * Some filesystems such as 'proc' acquire unusual locks when opening
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) * files. For them fanotify permission events have high chances of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) * deadlocking the system - open done when reporting fanotify event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) * blocks on this "unusual" lock while another process holding the lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) * waits for fanotify permission event to be answered. Just disallow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) * permission events for such filesystems.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) if (mask & FANOTIFY_PERM_EVENTS &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) path->mnt->mnt_sb->s_type->fs_flags & FS_DISALLOW_NOTIFY_PERM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) int dfd, const char __user *pathname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) struct inode *inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) struct vfsmount *mnt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) struct fsnotify_group *group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) struct fd f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) struct path path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) __kernel_fsid_t __fsid, *fsid = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) bool ignored = flags & FAN_MARK_IGNORED_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) unsigned int obj_type, fid_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) u32 umask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) __func__, fanotify_fd, flags, dfd, pathname, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) /* we only use the lower 32 bits as of right now. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) if (mask & ((__u64)0xffffffff << 32))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) if (flags & ~FANOTIFY_MARK_FLAGS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) switch (mark_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) case FAN_MARK_INODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) obj_type = FSNOTIFY_OBJ_TYPE_INODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) case FAN_MARK_MOUNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) obj_type = FSNOTIFY_OBJ_TYPE_VFSMOUNT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) case FAN_MARK_FILESYSTEM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) obj_type = FSNOTIFY_OBJ_TYPE_SB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) case FAN_MARK_ADD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) case FAN_MARK_REMOVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) if (!mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) case FAN_MARK_FLUSH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) if (flags & ~(FANOTIFY_MARK_TYPE_BITS | FAN_MARK_FLUSH))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) valid_mask |= FANOTIFY_PERM_EVENTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) if (mask & ~valid_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) /* Event flags (ONDIR, ON_CHILD) are meaningless in ignored mask */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) if (ignored)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) mask &= ~FANOTIFY_EVENT_FLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) f = fdget(fanotify_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) if (unlikely(!f.file))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) /* verify that this is indeed an fanotify instance */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) if (unlikely(f.file->f_op != &fanotify_fops))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) goto fput_and_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) group = f.file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) * allowed to set permissions events.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) if (mask & FANOTIFY_PERM_EVENTS &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) group->priority == FS_PRIO_0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) goto fput_and_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) * Events with data type inode do not carry enough information to report
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) * event->fd, so we do not allow setting a mask for inode events unless
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) * group supports reporting fid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) * inode events are not supported on a mount mark, because they do not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) * carry enough information (i.e. path) to be filtered by mount point.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) if (mask & FANOTIFY_INODE_EVENTS &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) (!fid_mode || mark_type == FAN_MARK_MOUNT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) goto fput_and_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) if (flags & FAN_MARK_FLUSH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) if (mark_type == FAN_MARK_MOUNT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) fsnotify_clear_vfsmount_marks_by_group(group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) else if (mark_type == FAN_MARK_FILESYSTEM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) fsnotify_clear_sb_marks_by_group(group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) fsnotify_clear_inode_marks_by_group(group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) goto fput_and_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) ret = fanotify_find_path(dfd, pathname, &path, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) (mask & ALL_FSNOTIFY_EVENTS), obj_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) goto fput_and_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) if (flags & FAN_MARK_ADD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) ret = fanotify_events_supported(&path, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) goto path_put_and_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) if (fid_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) ret = fanotify_test_fid(&path, &__fsid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) goto path_put_and_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) fsid = &__fsid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) /* inode held in place by reference to path; group by fget on fd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) if (mark_type == FAN_MARK_INODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) inode = path.dentry->d_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) mnt = path.mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) /* Mask out FAN_EVENT_ON_CHILD flag for sb/mount/non-dir marks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) if (mnt || !S_ISDIR(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) mask &= ~FAN_EVENT_ON_CHILD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) umask = FAN_EVENT_ON_CHILD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) * If group needs to report parent fid, register for getting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) * events with parent/name info for non-directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) if ((fid_mode & FAN_REPORT_DIR_FID) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) (flags & FAN_MARK_ADD) && !ignored)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) mask |= FAN_EVENT_ON_CHILD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) /* create/update an inode mark */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) case FAN_MARK_ADD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) if (mark_type == FAN_MARK_MOUNT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) ret = fanotify_add_vfsmount_mark(group, mnt, mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) flags, fsid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) else if (mark_type == FAN_MARK_FILESYSTEM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) ret = fanotify_add_sb_mark(group, mnt->mnt_sb, mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) flags, fsid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) ret = fanotify_add_inode_mark(group, inode, mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) flags, fsid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) case FAN_MARK_REMOVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) if (mark_type == FAN_MARK_MOUNT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) ret = fanotify_remove_vfsmount_mark(group, mnt, mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) flags, umask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) else if (mark_type == FAN_MARK_FILESYSTEM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) ret = fanotify_remove_sb_mark(group, mnt->mnt_sb, mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) flags, umask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) ret = fanotify_remove_inode_mark(group, inode, mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) flags, umask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) path_put_and_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) path_put(&path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) fput_and_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) fdput(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) #ifndef CONFIG_ARCH_SPLIT_ARG64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) __u64, mask, int, dfd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) const char __user *, pathname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) return do_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) #if defined(CONFIG_ARCH_SPLIT_ARG64) || defined(CONFIG_COMPAT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) SYSCALL32_DEFINE6(fanotify_mark,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) int, fanotify_fd, unsigned int, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) SC_ARG64(mask), int, dfd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) const char __user *, pathname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) return do_fanotify_mark(fanotify_fd, flags, SC_VAL64(__u64, mask),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) dfd, pathname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) * fanotify_user_setup - Our initialization function. Note that we cannot return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) * error because we have compiled-in VFS hooks. So an (unlikely) failure here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) * must result in panic().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) static int __init fanotify_user_setup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) fanotify_mark_cache = KMEM_CACHE(fsnotify_mark,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) SLAB_PANIC|SLAB_ACCOUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) fanotify_fid_event_cachep = KMEM_CACHE(fanotify_fid_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) SLAB_PANIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) fanotify_path_event_cachep = KMEM_CACHE(fanotify_path_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) SLAB_PANIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) fanotify_perm_event_cachep =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) KMEM_CACHE(fanotify_perm_event, SLAB_PANIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) device_initcall(fanotify_user_setup);