Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Andreas Gruenbacher | 33d3dff | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 2 | #include <linux/fanotify.h> |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 3 | #include <linux/fcntl.h> |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 4 | #include <linux/file.h> |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 5 | #include <linux/fs.h> |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 6 | #include <linux/anon_inodes.h> |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 7 | #include <linux/fsnotify_backend.h> |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 8 | #include <linux/init.h> |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 9 | #include <linux/mount.h> |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 10 | #include <linux/namei.h> |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 11 | #include <linux/poll.h> |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 12 | #include <linux/security.h> |
| 13 | #include <linux/syscalls.h> |
Tejun Heo | e4e047a | 2010-05-20 01:36:28 +1000 | [diff] [blame] | 14 | #include <linux/slab.h> |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 15 | #include <linux/types.h> |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 16 | #include <linux/uaccess.h> |
Al Viro | 91c2e0b | 2013-03-05 20:10:59 -0500 | [diff] [blame] | 17 | #include <linux/compat.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 18 | #include <linux/sched/signal.h> |
Shakeel Butt | d46eb14b | 2018-08-17 15:46:39 -0700 | [diff] [blame] | 19 | #include <linux/memcontrol.h> |
Amir Goldstein | a8b13aa | 2019-01-10 19:04:36 +0200 | [diff] [blame] | 20 | #include <linux/statfs.h> |
| 21 | #include <linux/exportfs.h> |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 22 | |
| 23 | #include <asm/ioctls.h> |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 24 | |
Al Viro | c63181e | 2011-11-25 02:35:16 -0500 | [diff] [blame] | 25 | #include "../../mount.h" |
Cyrill Gorcunov | be77196 | 2012-12-17 16:05:12 -0800 | [diff] [blame] | 26 | #include "../fdinfo.h" |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 27 | #include "fanotify.h" |
Al Viro | c63181e | 2011-11-25 02:35:16 -0500 | [diff] [blame] | 28 | |
Eric Paris | 2529a0d | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 29 | #define FANOTIFY_DEFAULT_MAX_EVENTS 16384 |
Amir Goldstein | 5b8fea6 | 2021-03-04 13:29:20 +0200 | [diff] [blame] | 30 | #define FANOTIFY_OLD_DEFAULT_MAX_MARKS 8192 |
| 31 | #define FANOTIFY_DEFAULT_MAX_GROUPS 128 |
| 32 | |
| 33 | /* |
| 34 | * Legacy fanotify marks limits (8192) is per group and we introduced a tunable |
| 35 | * limit of marks per user, similar to inotify. Effectively, the legacy limit |
| 36 | * of fanotify marks per user is <max marks per group> * <max groups per user>. |
| 37 | * This default limit (1M) also happens to match the increased limit of inotify |
| 38 | * max_user_watches since v5.10. |
| 39 | */ |
| 40 | #define FANOTIFY_DEFAULT_MAX_USER_MARKS \ |
| 41 | (FANOTIFY_OLD_DEFAULT_MAX_MARKS * FANOTIFY_DEFAULT_MAX_GROUPS) |
| 42 | |
| 43 | /* |
| 44 | * Most of the memory cost of adding an inode mark is pinning the marked inode. |
| 45 | * The size of the filesystem inode struct is not uniform across filesystems, |
| 46 | * so double the size of a VFS inode is used as a conservative approximation. |
| 47 | */ |
| 48 | #define INODE_MARK_COST (2 * sizeof(struct inode)) |
| 49 | |
| 50 | /* configurable via /proc/sys/fs/fanotify/ */ |
| 51 | static int fanotify_max_queued_events __read_mostly; |
| 52 | |
| 53 | #ifdef CONFIG_SYSCTL |
| 54 | |
| 55 | #include <linux/sysctl.h> |
| 56 | |
| 57 | struct ctl_table fanotify_table[] = { |
| 58 | { |
| 59 | .procname = "max_user_groups", |
| 60 | .data = &init_user_ns.ucount_max[UCOUNT_FANOTIFY_GROUPS], |
| 61 | .maxlen = sizeof(int), |
| 62 | .mode = 0644, |
| 63 | .proc_handler = proc_dointvec_minmax, |
| 64 | .extra1 = SYSCTL_ZERO, |
| 65 | }, |
| 66 | { |
| 67 | .procname = "max_user_marks", |
| 68 | .data = &init_user_ns.ucount_max[UCOUNT_FANOTIFY_MARKS], |
| 69 | .maxlen = sizeof(int), |
| 70 | .mode = 0644, |
| 71 | .proc_handler = proc_dointvec_minmax, |
| 72 | .extra1 = SYSCTL_ZERO, |
| 73 | }, |
| 74 | { |
| 75 | .procname = "max_queued_events", |
| 76 | .data = &fanotify_max_queued_events, |
| 77 | .maxlen = sizeof(int), |
| 78 | .mode = 0644, |
| 79 | .proc_handler = proc_dointvec_minmax, |
| 80 | .extra1 = SYSCTL_ZERO |
| 81 | }, |
| 82 | { } |
| 83 | }; |
| 84 | #endif /* CONFIG_SYSCTL */ |
Eric Paris | 2529a0d | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 85 | |
Heinrich Schuchardt | 48149e9 | 2014-06-04 16:05:44 -0700 | [diff] [blame] | 86 | /* |
| 87 | * All flags that may be specified in parameter event_f_flags of fanotify_init. |
| 88 | * |
| 89 | * Internal and external open flags are stored together in field f_flags of |
| 90 | * struct file. Only external open flags shall be allowed in event_f_flags. |
| 91 | * Internal flags like FMODE_NONOTIFY, FMODE_EXEC, FMODE_NOCMTIME shall be |
| 92 | * excluded. |
| 93 | */ |
| 94 | #define FANOTIFY_INIT_ALL_EVENT_F_BITS ( \ |
| 95 | O_ACCMODE | O_APPEND | O_NONBLOCK | \ |
| 96 | __O_SYNC | O_DSYNC | O_CLOEXEC | \ |
| 97 | O_LARGEFILE | O_NOATIME ) |
| 98 | |
Andreas Gruenbacher | 33d3dff | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 99 | extern const struct fsnotify_ops fanotify_fsnotify_ops; |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 100 | |
Jan Kara | 054c636 | 2016-12-21 18:06:12 +0100 | [diff] [blame] | 101 | struct kmem_cache *fanotify_mark_cache __read_mostly; |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 102 | struct kmem_cache *fanotify_fid_event_cachep __read_mostly; |
| 103 | struct kmem_cache *fanotify_path_event_cachep __read_mostly; |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 104 | struct kmem_cache *fanotify_perm_event_cachep __read_mostly; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 105 | |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 106 | #define FANOTIFY_EVENT_ALIGN 4 |
Matthew Bobrowski | d3424c9 | 2021-08-08 15:25:32 +1000 | [diff] [blame^] | 107 | #define FANOTIFY_FID_INFO_HDR_LEN \ |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 108 | (sizeof(struct fanotify_event_info_fid) + sizeof(struct file_handle)) |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 109 | |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 110 | static int fanotify_fid_info_len(int fh_len, int name_len) |
Amir Goldstein | d766b55 | 2020-03-19 17:10:20 +0200 | [diff] [blame] | 111 | { |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 112 | int info_len = fh_len; |
| 113 | |
| 114 | if (name_len) |
| 115 | info_len += name_len + 1; |
| 116 | |
Matthew Bobrowski | d3424c9 | 2021-08-08 15:25:32 +1000 | [diff] [blame^] | 117 | return roundup(FANOTIFY_FID_INFO_HDR_LEN + info_len, |
| 118 | FANOTIFY_EVENT_ALIGN); |
Amir Goldstein | d766b55 | 2020-03-19 17:10:20 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Matthew Bobrowski | d3424c9 | 2021-08-08 15:25:32 +1000 | [diff] [blame^] | 121 | static int fanotify_event_info_len(unsigned int info_mode, |
Amir Goldstein | 929943b | 2020-07-16 11:42:28 +0300 | [diff] [blame] | 122 | struct fanotify_event *event) |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 123 | { |
Amir Goldstein | f454fa6 | 2020-07-16 11:42:17 +0300 | [diff] [blame] | 124 | struct fanotify_info *info = fanotify_event_info(event); |
| 125 | int dir_fh_len = fanotify_event_dir_fh_len(event); |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 126 | int fh_len = fanotify_event_object_fh_len(event); |
Amir Goldstein | f454fa6 | 2020-07-16 11:42:17 +0300 | [diff] [blame] | 127 | int info_len = 0; |
Amir Goldstein | 929943b | 2020-07-16 11:42:28 +0300 | [diff] [blame] | 128 | int dot_len = 0; |
Amir Goldstein | f454fa6 | 2020-07-16 11:42:17 +0300 | [diff] [blame] | 129 | |
Amir Goldstein | 929943b | 2020-07-16 11:42:28 +0300 | [diff] [blame] | 130 | if (dir_fh_len) { |
Amir Goldstein | f454fa6 | 2020-07-16 11:42:17 +0300 | [diff] [blame] | 131 | info_len += fanotify_fid_info_len(dir_fh_len, info->name_len); |
Matthew Bobrowski | d3424c9 | 2021-08-08 15:25:32 +1000 | [diff] [blame^] | 132 | } else if ((info_mode & FAN_REPORT_NAME) && |
| 133 | (event->mask & FAN_ONDIR)) { |
Amir Goldstein | 929943b | 2020-07-16 11:42:28 +0300 | [diff] [blame] | 134 | /* |
| 135 | * With group flag FAN_REPORT_NAME, if name was not recorded in |
| 136 | * event on a directory, we will report the name ".". |
| 137 | */ |
| 138 | dot_len = 1; |
| 139 | } |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 140 | |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 141 | if (fh_len) |
Amir Goldstein | 929943b | 2020-07-16 11:42:28 +0300 | [diff] [blame] | 142 | info_len += fanotify_fid_info_len(fh_len, dot_len); |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 143 | |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 144 | return info_len; |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 145 | } |
| 146 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 147 | /* |
Amir Goldstein | 94e00d2 | 2021-03-04 12:48:25 +0200 | [diff] [blame] | 148 | * Remove an hashed event from merge hash table. |
| 149 | */ |
| 150 | static void fanotify_unhash_event(struct fsnotify_group *group, |
| 151 | struct fanotify_event *event) |
| 152 | { |
| 153 | assert_spin_locked(&group->notification_lock); |
| 154 | |
| 155 | pr_debug("%s: group=%p event=%p bucket=%u\n", __func__, |
| 156 | group, event, fanotify_event_hash_bucket(group, event)); |
| 157 | |
| 158 | if (WARN_ON_ONCE(hlist_unhashed(&event->merge_list))) |
| 159 | return; |
| 160 | |
| 161 | hlist_del_init(&event->merge_list); |
| 162 | } |
| 163 | |
| 164 | /* |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 165 | * Get an fanotify notification event if one exists and is small |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 166 | * enough to fit in "count". Return an error pointer if the count |
Jan Kara | 4087328 | 2019-01-08 14:02:44 +0100 | [diff] [blame] | 167 | * is not large enough. When permission event is dequeued, its state is |
| 168 | * updated accordingly. |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 169 | */ |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 170 | static struct fanotify_event *get_one_event(struct fsnotify_group *group, |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 171 | size_t count) |
| 172 | { |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 173 | size_t event_size = FAN_EVENT_METADATA_LEN; |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 174 | struct fanotify_event *event = NULL; |
Amir Goldstein | 6f73171 | 2021-03-04 12:48:22 +0200 | [diff] [blame] | 175 | struct fsnotify_event *fsn_event; |
Amir Goldstein | 929943b | 2020-07-16 11:42:28 +0300 | [diff] [blame] | 176 | unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 177 | |
| 178 | pr_debug("%s: group=%p count=%zd\n", __func__, group, count); |
| 179 | |
Jan Kara | 8c55446 | 2019-01-08 13:52:31 +0100 | [diff] [blame] | 180 | spin_lock(&group->notification_lock); |
Amir Goldstein | 6f73171 | 2021-03-04 12:48:22 +0200 | [diff] [blame] | 181 | fsn_event = fsnotify_peek_first_event(group); |
| 182 | if (!fsn_event) |
Jan Kara | 8c55446 | 2019-01-08 13:52:31 +0100 | [diff] [blame] | 183 | goto out; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 184 | |
Amir Goldstein | 6f73171 | 2021-03-04 12:48:22 +0200 | [diff] [blame] | 185 | event = FANOTIFY_E(fsn_event); |
| 186 | if (fid_mode) |
| 187 | event_size += fanotify_event_info_len(fid_mode, event); |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 188 | |
Jan Kara | 8c55446 | 2019-01-08 13:52:31 +0100 | [diff] [blame] | 189 | if (event_size > count) { |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 190 | event = ERR_PTR(-EINVAL); |
Jan Kara | 8c55446 | 2019-01-08 13:52:31 +0100 | [diff] [blame] | 191 | goto out; |
| 192 | } |
Amir Goldstein | 6f73171 | 2021-03-04 12:48:22 +0200 | [diff] [blame] | 193 | |
| 194 | /* |
| 195 | * Held the notification_lock the whole time, so this is the |
| 196 | * same event we peeked above. |
| 197 | */ |
| 198 | fsnotify_remove_first_event(group); |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 199 | if (fanotify_is_perm_event(event->mask)) |
| 200 | FANOTIFY_PERM(event)->state = FAN_EVENT_REPORTED; |
Amir Goldstein | 94e00d2 | 2021-03-04 12:48:25 +0200 | [diff] [blame] | 201 | if (fanotify_is_hashed_event(event->mask)) |
| 202 | fanotify_unhash_event(group, event); |
Jan Kara | 8c55446 | 2019-01-08 13:52:31 +0100 | [diff] [blame] | 203 | out: |
| 204 | spin_unlock(&group->notification_lock); |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 205 | return event; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 206 | } |
| 207 | |
Jan Kara | a741c2f | 2020-03-24 15:27:52 +0100 | [diff] [blame] | 208 | static int create_fd(struct fsnotify_group *group, struct path *path, |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 209 | struct file **file) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 210 | { |
| 211 | int client_fd; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 212 | struct file *new_file; |
| 213 | |
Yann Droneaud | 0b37e09 | 2014-10-09 15:24:40 -0700 | [diff] [blame] | 214 | client_fd = get_unused_fd_flags(group->fanotify_data.f_flags); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 215 | if (client_fd < 0) |
| 216 | return client_fd; |
| 217 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 218 | /* |
| 219 | * we need a new file handle for the userspace program so it can read even if it was |
| 220 | * originally opened O_WRONLY. |
| 221 | */ |
Jan Kara | a741c2f | 2020-03-24 15:27:52 +0100 | [diff] [blame] | 222 | new_file = dentry_open(path, |
| 223 | group->fanotify_data.f_flags | FMODE_NONOTIFY, |
| 224 | current_cred()); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 225 | if (IS_ERR(new_file)) { |
| 226 | /* |
| 227 | * we still send an event even if we can't open the file. this |
| 228 | * can happen when say tasks are gone and we try to open their |
| 229 | * /proc files or we try to open a WRONLY file like in sysfs |
| 230 | * we just send the errno to userspace since there isn't much |
| 231 | * else we can do. |
| 232 | */ |
| 233 | put_unused_fd(client_fd); |
| 234 | client_fd = PTR_ERR(new_file); |
| 235 | } else { |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 236 | *file = new_file; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 237 | } |
| 238 | |
Andreas Gruenbacher | 22aa425 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 239 | return client_fd; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 240 | } |
| 241 | |
Jan Kara | 4087328 | 2019-01-08 14:02:44 +0100 | [diff] [blame] | 242 | /* |
| 243 | * Finish processing of permission event by setting it to ANSWERED state and |
| 244 | * drop group->notification_lock. |
| 245 | */ |
| 246 | static void finish_permission_event(struct fsnotify_group *group, |
| 247 | struct fanotify_perm_event *event, |
| 248 | unsigned int response) |
| 249 | __releases(&group->notification_lock) |
| 250 | { |
Jan Kara | fabf7f2 | 2019-01-08 15:18:02 +0100 | [diff] [blame] | 251 | bool destroy = false; |
| 252 | |
Jan Kara | 4087328 | 2019-01-08 14:02:44 +0100 | [diff] [blame] | 253 | assert_spin_locked(&group->notification_lock); |
| 254 | event->response = response; |
Jan Kara | fabf7f2 | 2019-01-08 15:18:02 +0100 | [diff] [blame] | 255 | if (event->state == FAN_EVENT_CANCELED) |
| 256 | destroy = true; |
| 257 | else |
| 258 | event->state = FAN_EVENT_ANSWERED; |
Jan Kara | 4087328 | 2019-01-08 14:02:44 +0100 | [diff] [blame] | 259 | spin_unlock(&group->notification_lock); |
Jan Kara | fabf7f2 | 2019-01-08 15:18:02 +0100 | [diff] [blame] | 260 | if (destroy) |
| 261 | fsnotify_destroy_event(group, &event->fae.fse); |
Jan Kara | 4087328 | 2019-01-08 14:02:44 +0100 | [diff] [blame] | 262 | } |
| 263 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 264 | static int process_access_response(struct fsnotify_group *group, |
| 265 | struct fanotify_response *response_struct) |
| 266 | { |
Amir Goldstein | 3391399 | 2019-01-10 19:04:32 +0200 | [diff] [blame] | 267 | struct fanotify_perm_event *event; |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 268 | int fd = response_struct->fd; |
| 269 | int response = response_struct->response; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 270 | |
| 271 | pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group, |
| 272 | fd, response); |
| 273 | /* |
| 274 | * make sure the response is valid, if invalid we do nothing and either |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 275 | * userspace can send a valid response or we will clean it up after the |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 276 | * timeout |
| 277 | */ |
Steve Grubb | de8cd83 | 2017-10-02 20:21:39 -0400 | [diff] [blame] | 278 | switch (response & ~FAN_AUDIT) { |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 279 | case FAN_ALLOW: |
| 280 | case FAN_DENY: |
| 281 | break; |
| 282 | default: |
| 283 | return -EINVAL; |
| 284 | } |
| 285 | |
| 286 | if (fd < 0) |
| 287 | return -EINVAL; |
| 288 | |
Amir Goldstein | 96a71f2 | 2018-09-21 21:20:30 +0300 | [diff] [blame] | 289 | if ((response & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT)) |
Steve Grubb | de8cd83 | 2017-10-02 20:21:39 -0400 | [diff] [blame] | 290 | return -EINVAL; |
| 291 | |
Jan Kara | af6a511 | 2019-01-08 13:28:18 +0100 | [diff] [blame] | 292 | spin_lock(&group->notification_lock); |
| 293 | list_for_each_entry(event, &group->fanotify_data.access_list, |
| 294 | fae.fse.list) { |
| 295 | if (event->fd != fd) |
| 296 | continue; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 297 | |
Jan Kara | af6a511 | 2019-01-08 13:28:18 +0100 | [diff] [blame] | 298 | list_del_init(&event->fae.fse.list); |
Jan Kara | 4087328 | 2019-01-08 14:02:44 +0100 | [diff] [blame] | 299 | finish_permission_event(group, event, response); |
Jan Kara | af6a511 | 2019-01-08 13:28:18 +0100 | [diff] [blame] | 300 | wake_up(&group->fanotify_data.access_waitq); |
| 301 | return 0; |
| 302 | } |
| 303 | spin_unlock(&group->notification_lock); |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 304 | |
Jan Kara | af6a511 | 2019-01-08 13:28:18 +0100 | [diff] [blame] | 305 | return -ENOENT; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 306 | } |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 307 | |
Matthew Bobrowski | d3424c9 | 2021-08-08 15:25:32 +1000 | [diff] [blame^] | 308 | static int copy_fid_info_to_user(__kernel_fsid_t *fsid, struct fanotify_fh *fh, |
| 309 | int info_type, const char *name, |
| 310 | size_t name_len, |
| 311 | char __user *buf, size_t count) |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 312 | { |
| 313 | struct fanotify_event_info_fid info = { }; |
| 314 | struct file_handle handle = { }; |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 315 | unsigned char bounce[FANOTIFY_INLINE_FH_LEN], *fh_buf; |
Amir Goldstein | cacfb95 | 2020-03-19 17:10:21 +0200 | [diff] [blame] | 316 | size_t fh_len = fh ? fh->len : 0; |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 317 | size_t info_len = fanotify_fid_info_len(fh_len, name_len); |
| 318 | size_t len = info_len; |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 319 | |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 320 | pr_debug("%s: fh_len=%zu name_len=%zu, info_len=%zu, count=%zu\n", |
| 321 | __func__, fh_len, name_len, info_len, count); |
| 322 | |
Amir Goldstein | 83b7a59 | 2020-07-16 11:42:26 +0300 | [diff] [blame] | 323 | if (!fh_len) |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 324 | return 0; |
| 325 | |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 326 | if (WARN_ON_ONCE(len < sizeof(info) || len > count)) |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 327 | return -EFAULT; |
| 328 | |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 329 | /* |
| 330 | * Copy event info fid header followed by variable sized file handle |
| 331 | * and optionally followed by variable sized filename. |
| 332 | */ |
Amir Goldstein | 83b7a59 | 2020-07-16 11:42:26 +0300 | [diff] [blame] | 333 | switch (info_type) { |
| 334 | case FAN_EVENT_INFO_TYPE_FID: |
| 335 | case FAN_EVENT_INFO_TYPE_DFID: |
| 336 | if (WARN_ON_ONCE(name_len)) |
| 337 | return -EFAULT; |
| 338 | break; |
| 339 | case FAN_EVENT_INFO_TYPE_DFID_NAME: |
| 340 | if (WARN_ON_ONCE(!name || !name_len)) |
| 341 | return -EFAULT; |
| 342 | break; |
| 343 | default: |
| 344 | return -EFAULT; |
| 345 | } |
| 346 | |
| 347 | info.hdr.info_type = info_type; |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 348 | info.hdr.len = len; |
Amir Goldstein | d766b55 | 2020-03-19 17:10:20 +0200 | [diff] [blame] | 349 | info.fsid = *fsid; |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 350 | if (copy_to_user(buf, &info, sizeof(info))) |
| 351 | return -EFAULT; |
| 352 | |
| 353 | buf += sizeof(info); |
| 354 | len -= sizeof(info); |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 355 | if (WARN_ON_ONCE(len < sizeof(handle))) |
| 356 | return -EFAULT; |
| 357 | |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 358 | handle.handle_type = fh->type; |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 359 | handle.handle_bytes = fh_len; |
| 360 | if (copy_to_user(buf, &handle, sizeof(handle))) |
| 361 | return -EFAULT; |
| 362 | |
| 363 | buf += sizeof(handle); |
| 364 | len -= sizeof(handle); |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 365 | if (WARN_ON_ONCE(len < fh_len)) |
| 366 | return -EFAULT; |
| 367 | |
Jan Kara | b2d22b6 | 2019-03-12 12:42:37 +0100 | [diff] [blame] | 368 | /* |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 369 | * For an inline fh and inline file name, copy through stack to exclude |
| 370 | * the copy from usercopy hardening protections. |
Jan Kara | b2d22b6 | 2019-03-12 12:42:37 +0100 | [diff] [blame] | 371 | */ |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 372 | fh_buf = fanotify_fh_buf(fh); |
Jan Kara | b2d22b6 | 2019-03-12 12:42:37 +0100 | [diff] [blame] | 373 | if (fh_len <= FANOTIFY_INLINE_FH_LEN) { |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 374 | memcpy(bounce, fh_buf, fh_len); |
| 375 | fh_buf = bounce; |
Jan Kara | b2d22b6 | 2019-03-12 12:42:37 +0100 | [diff] [blame] | 376 | } |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 377 | if (copy_to_user(buf, fh_buf, fh_len)) |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 378 | return -EFAULT; |
| 379 | |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 380 | buf += fh_len; |
| 381 | len -= fh_len; |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 382 | |
| 383 | if (name_len) { |
| 384 | /* Copy the filename with terminating null */ |
| 385 | name_len++; |
| 386 | if (WARN_ON_ONCE(len < name_len)) |
| 387 | return -EFAULT; |
| 388 | |
| 389 | if (copy_to_user(buf, name, name_len)) |
| 390 | return -EFAULT; |
| 391 | |
| 392 | buf += name_len; |
| 393 | len -= name_len; |
| 394 | } |
| 395 | |
| 396 | /* Pad with 0's */ |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 397 | WARN_ON_ONCE(len < 0 || len >= FANOTIFY_EVENT_ALIGN); |
| 398 | if (len > 0 && clear_user(buf, len)) |
| 399 | return -EFAULT; |
| 400 | |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 401 | return info_len; |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 402 | } |
| 403 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 404 | static ssize_t copy_event_to_user(struct fsnotify_group *group, |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 405 | struct fanotify_event *event, |
Kees Cook | 5b03a47 | 2018-12-04 15:44:46 -0800 | [diff] [blame] | 406 | char __user *buf, size_t count) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 407 | { |
Amir Goldstein | bb2f7b4 | 2019-01-10 19:04:33 +0200 | [diff] [blame] | 408 | struct fanotify_event_metadata metadata; |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 409 | struct path *path = fanotify_event_path(event); |
Amir Goldstein | f454fa6 | 2020-07-16 11:42:17 +0300 | [diff] [blame] | 410 | struct fanotify_info *info = fanotify_event_info(event); |
Amir Goldstein | 83b7a59 | 2020-07-16 11:42:26 +0300 | [diff] [blame] | 411 | unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS); |
Amir Goldstein | bb2f7b4 | 2019-01-10 19:04:33 +0200 | [diff] [blame] | 412 | struct file *f = NULL; |
Amir Goldstein | e9e0c89 | 2019-01-10 19:04:34 +0200 | [diff] [blame] | 413 | int ret, fd = FAN_NOFD; |
Amir Goldstein | 83b7a59 | 2020-07-16 11:42:26 +0300 | [diff] [blame] | 414 | int info_type = 0; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 415 | |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 416 | pr_debug("%s: group=%p event=%p\n", __func__, group, event); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 417 | |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 418 | metadata.event_len = FAN_EVENT_METADATA_LEN + |
Amir Goldstein | 929943b | 2020-07-16 11:42:28 +0300 | [diff] [blame] | 419 | fanotify_event_info_len(fid_mode, event); |
Amir Goldstein | bb2f7b4 | 2019-01-10 19:04:33 +0200 | [diff] [blame] | 420 | metadata.metadata_len = FAN_EVENT_METADATA_LEN; |
| 421 | metadata.vers = FANOTIFY_METADATA_VERSION; |
| 422 | metadata.reserved = 0; |
| 423 | metadata.mask = event->mask & FANOTIFY_OUTGOING_EVENTS; |
| 424 | metadata.pid = pid_vnr(event->pid); |
Amir Goldstein | 7cea2a3 | 2021-03-04 13:29:21 +0200 | [diff] [blame] | 425 | /* |
| 426 | * For an unprivileged listener, event->pid can be used to identify the |
| 427 | * events generated by the listener process itself, without disclosing |
| 428 | * the pids of other processes. |
| 429 | */ |
Amir Goldstein | a8b98c8 | 2021-05-24 16:53:21 +0300 | [diff] [blame] | 430 | if (FAN_GROUP_FLAG(group, FANOTIFY_UNPRIV) && |
Amir Goldstein | 7cea2a3 | 2021-03-04 13:29:21 +0200 | [diff] [blame] | 431 | task_tgid(current) != event->pid) |
| 432 | metadata.pid = 0; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 433 | |
Amir Goldstein | a8b98c8 | 2021-05-24 16:53:21 +0300 | [diff] [blame] | 434 | /* |
| 435 | * For now, fid mode is required for an unprivileged listener and |
| 436 | * fid mode does not report fd in events. Keep this check anyway |
| 437 | * for safety in case fid mode requirement is relaxed in the future |
| 438 | * to allow unprivileged listener to get events with no fd and no fid. |
| 439 | */ |
| 440 | if (!FAN_GROUP_FLAG(group, FANOTIFY_UNPRIV) && |
| 441 | path && path->mnt && path->dentry) { |
Jan Kara | afc894c | 2020-03-24 16:55:37 +0100 | [diff] [blame] | 442 | fd = create_fd(group, path, &f); |
| 443 | if (fd < 0) |
| 444 | return fd; |
Amir Goldstein | bb2f7b4 | 2019-01-10 19:04:33 +0200 | [diff] [blame] | 445 | } |
| 446 | metadata.fd = fd; |
| 447 | |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 448 | ret = -EFAULT; |
Kees Cook | 5b03a47 | 2018-12-04 15:44:46 -0800 | [diff] [blame] | 449 | /* |
| 450 | * Sanity check copy size in case get_one_event() and |
Fabian Frederick | c5e443c | 2020-05-12 20:18:36 +0200 | [diff] [blame] | 451 | * event_len sizes ever get out of sync. |
Kees Cook | 5b03a47 | 2018-12-04 15:44:46 -0800 | [diff] [blame] | 452 | */ |
Amir Goldstein | bb2f7b4 | 2019-01-10 19:04:33 +0200 | [diff] [blame] | 453 | if (WARN_ON_ONCE(metadata.event_len > count)) |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 454 | goto out_close_fd; |
| 455 | |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 456 | if (copy_to_user(buf, &metadata, FAN_EVENT_METADATA_LEN)) |
Amir Goldstein | bb2f7b4 | 2019-01-10 19:04:33 +0200 | [diff] [blame] | 457 | goto out_close_fd; |
| 458 | |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 459 | buf += FAN_EVENT_METADATA_LEN; |
| 460 | count -= FAN_EVENT_METADATA_LEN; |
| 461 | |
Amir Goldstein | bb2f7b4 | 2019-01-10 19:04:33 +0200 | [diff] [blame] | 462 | if (fanotify_is_perm_event(event->mask)) |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 463 | FANOTIFY_PERM(event)->fd = fd; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 464 | |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 465 | if (f) |
Al Viro | 3587b1b | 2012-11-18 19:19:00 +0000 | [diff] [blame] | 466 | fd_install(fd, f); |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 467 | |
| 468 | /* Event info records order is: dir fid + name, child fid */ |
Amir Goldstein | f454fa6 | 2020-07-16 11:42:17 +0300 | [diff] [blame] | 469 | if (fanotify_event_dir_fh_len(event)) { |
Amir Goldstein | 691d976 | 2020-07-16 11:42:30 +0300 | [diff] [blame] | 470 | info_type = info->name_len ? FAN_EVENT_INFO_TYPE_DFID_NAME : |
| 471 | FAN_EVENT_INFO_TYPE_DFID; |
Matthew Bobrowski | d3424c9 | 2021-08-08 15:25:32 +1000 | [diff] [blame^] | 472 | ret = copy_fid_info_to_user(fanotify_event_fsid(event), |
| 473 | fanotify_info_dir_fh(info), |
| 474 | info_type, |
| 475 | fanotify_info_name(info), |
| 476 | info->name_len, buf, count); |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 477 | if (ret < 0) |
Matthew Bobrowski | f644bc4 | 2021-06-11 13:32:06 +1000 | [diff] [blame] | 478 | goto out_close_fd; |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 479 | |
| 480 | buf += ret; |
| 481 | count -= ret; |
| 482 | } |
| 483 | |
| 484 | if (fanotify_event_object_fh_len(event)) { |
Amir Goldstein | 929943b | 2020-07-16 11:42:28 +0300 | [diff] [blame] | 485 | const char *dot = NULL; |
| 486 | int dot_len = 0; |
| 487 | |
Amir Goldstein | 83b7a59 | 2020-07-16 11:42:26 +0300 | [diff] [blame] | 488 | if (fid_mode == FAN_REPORT_FID || info_type) { |
| 489 | /* |
| 490 | * With only group flag FAN_REPORT_FID only type FID is |
| 491 | * reported. Second info record type is always FID. |
| 492 | */ |
| 493 | info_type = FAN_EVENT_INFO_TYPE_FID; |
Amir Goldstein | 929943b | 2020-07-16 11:42:28 +0300 | [diff] [blame] | 494 | } else if ((fid_mode & FAN_REPORT_NAME) && |
| 495 | (event->mask & FAN_ONDIR)) { |
| 496 | /* |
| 497 | * With group flag FAN_REPORT_NAME, if name was not |
| 498 | * recorded in an event on a directory, report the |
| 499 | * name "." with info type DFID_NAME. |
| 500 | */ |
| 501 | info_type = FAN_EVENT_INFO_TYPE_DFID_NAME; |
| 502 | dot = "."; |
| 503 | dot_len = 1; |
Amir Goldstein | 83b7a59 | 2020-07-16 11:42:26 +0300 | [diff] [blame] | 504 | } else if ((event->mask & ALL_FSNOTIFY_DIRENT_EVENTS) || |
| 505 | (event->mask & FAN_ONDIR)) { |
| 506 | /* |
| 507 | * With group flag FAN_REPORT_DIR_FID, a single info |
| 508 | * record has type DFID for directory entry modification |
| 509 | * event and for event on a directory. |
| 510 | */ |
| 511 | info_type = FAN_EVENT_INFO_TYPE_DFID; |
| 512 | } else { |
| 513 | /* |
| 514 | * With group flags FAN_REPORT_DIR_FID|FAN_REPORT_FID, |
| 515 | * a single info record has type FID for event on a |
| 516 | * non-directory, when there is no directory to report. |
| 517 | * For example, on FAN_DELETE_SELF event. |
| 518 | */ |
| 519 | info_type = FAN_EVENT_INFO_TYPE_FID; |
| 520 | } |
| 521 | |
Matthew Bobrowski | d3424c9 | 2021-08-08 15:25:32 +1000 | [diff] [blame^] | 522 | ret = copy_fid_info_to_user(fanotify_event_fsid(event), |
| 523 | fanotify_event_object_fh(event), |
| 524 | info_type, dot, dot_len, |
| 525 | buf, count); |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 526 | if (ret < 0) |
Matthew Bobrowski | f644bc4 | 2021-06-11 13:32:06 +1000 | [diff] [blame] | 527 | goto out_close_fd; |
Amir Goldstein | 44d705b | 2020-03-19 17:10:22 +0200 | [diff] [blame] | 528 | |
| 529 | buf += ret; |
| 530 | count -= ret; |
Amir Goldstein | 5e469c8 | 2019-01-10 19:04:35 +0200 | [diff] [blame] | 531 | } |
| 532 | |
Amir Goldstein | bb2f7b4 | 2019-01-10 19:04:33 +0200 | [diff] [blame] | 533 | return metadata.event_len; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 534 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 535 | out_close_fd: |
Al Viro | 352e3b2 | 2012-08-19 12:30:45 -0400 | [diff] [blame] | 536 | if (fd != FAN_NOFD) { |
| 537 | put_unused_fd(fd); |
| 538 | fput(f); |
| 539 | } |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 540 | return ret; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | /* intofiy userspace file descriptor functions */ |
Al Viro | 076ccb7 | 2017-07-03 01:02:18 -0400 | [diff] [blame] | 544 | static __poll_t fanotify_poll(struct file *file, poll_table *wait) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 545 | { |
| 546 | struct fsnotify_group *group = file->private_data; |
Al Viro | 076ccb7 | 2017-07-03 01:02:18 -0400 | [diff] [blame] | 547 | __poll_t ret = 0; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 548 | |
| 549 | poll_wait(file, &group->notification_waitq, wait); |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame] | 550 | spin_lock(&group->notification_lock); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 551 | if (!fsnotify_notify_queue_is_empty(group)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 552 | ret = EPOLLIN | EPOLLRDNORM; |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame] | 553 | spin_unlock(&group->notification_lock); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 554 | |
| 555 | return ret; |
| 556 | } |
| 557 | |
| 558 | static ssize_t fanotify_read(struct file *file, char __user *buf, |
| 559 | size_t count, loff_t *pos) |
| 560 | { |
| 561 | struct fsnotify_group *group; |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 562 | struct fanotify_event *event; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 563 | char __user *start; |
| 564 | int ret; |
Peter Zijlstra | 536ebe9ca | 2014-12-16 16:28:38 +0100 | [diff] [blame] | 565 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 566 | |
| 567 | start = buf; |
| 568 | group = file->private_data; |
| 569 | |
| 570 | pr_debug("%s: group=%p\n", __func__, group); |
| 571 | |
Peter Zijlstra | 536ebe9ca | 2014-12-16 16:28:38 +0100 | [diff] [blame] | 572 | add_wait_queue(&group->notification_waitq, &wait); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 573 | while (1) { |
Jan Kara | 47aaabd | 2020-07-15 14:06:21 +0200 | [diff] [blame] | 574 | /* |
| 575 | * User can supply arbitrarily large buffer. Avoid softlockups |
| 576 | * in case there are lots of available events. |
| 577 | */ |
| 578 | cond_resched(); |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 579 | event = get_one_event(group, count); |
| 580 | if (IS_ERR(event)) { |
| 581 | ret = PTR_ERR(event); |
Jan Kara | d8aaab4 | 2014-04-03 14:46:35 -0700 | [diff] [blame] | 582 | break; |
| 583 | } |
| 584 | |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 585 | if (!event) { |
Jan Kara | d8aaab4 | 2014-04-03 14:46:35 -0700 | [diff] [blame] | 586 | ret = -EAGAIN; |
| 587 | if (file->f_flags & O_NONBLOCK) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 588 | break; |
Jan Kara | d8aaab4 | 2014-04-03 14:46:35 -0700 | [diff] [blame] | 589 | |
| 590 | ret = -ERESTARTSYS; |
| 591 | if (signal_pending(current)) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 592 | break; |
Jan Kara | d8aaab4 | 2014-04-03 14:46:35 -0700 | [diff] [blame] | 593 | |
| 594 | if (start != buf) |
| 595 | break; |
Peter Zijlstra | 536ebe9ca | 2014-12-16 16:28:38 +0100 | [diff] [blame] | 596 | |
| 597 | wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 598 | continue; |
| 599 | } |
| 600 | |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 601 | ret = copy_event_to_user(group, event, buf, count); |
Amir Goldstein | 4ff33aa | 2017-04-25 14:29:35 +0300 | [diff] [blame] | 602 | if (unlikely(ret == -EOPENSTALE)) { |
| 603 | /* |
| 604 | * We cannot report events with stale fd so drop it. |
| 605 | * Setting ret to 0 will continue the event loop and |
| 606 | * do the right thing if there are no more events to |
| 607 | * read (i.e. return bytes read, -EAGAIN or wait). |
| 608 | */ |
| 609 | ret = 0; |
| 610 | } |
| 611 | |
Jan Kara | d8aaab4 | 2014-04-03 14:46:35 -0700 | [diff] [blame] | 612 | /* |
| 613 | * Permission events get queued to wait for response. Other |
| 614 | * events can be destroyed now. |
| 615 | */ |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 616 | if (!fanotify_is_perm_event(event->mask)) { |
| 617 | fsnotify_destroy_event(group, &event->fse); |
Jan Kara | d507816 | 2014-04-03 14:46:36 -0700 | [diff] [blame] | 618 | } else { |
Amir Goldstein | 4ff33aa | 2017-04-25 14:29:35 +0300 | [diff] [blame] | 619 | if (ret <= 0) { |
Jan Kara | 4087328 | 2019-01-08 14:02:44 +0100 | [diff] [blame] | 620 | spin_lock(&group->notification_lock); |
| 621 | finish_permission_event(group, |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 622 | FANOTIFY_PERM(event), FAN_DENY); |
Jan Kara | d507816 | 2014-04-03 14:46:36 -0700 | [diff] [blame] | 623 | wake_up(&group->fanotify_data.access_waitq); |
Amir Goldstein | 4ff33aa | 2017-04-25 14:29:35 +0300 | [diff] [blame] | 624 | } else { |
| 625 | spin_lock(&group->notification_lock); |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 626 | list_add_tail(&event->fse.list, |
Amir Goldstein | 4ff33aa | 2017-04-25 14:29:35 +0300 | [diff] [blame] | 627 | &group->fanotify_data.access_list); |
| 628 | spin_unlock(&group->notification_lock); |
Jan Kara | d507816 | 2014-04-03 14:46:36 -0700 | [diff] [blame] | 629 | } |
Jan Kara | d507816 | 2014-04-03 14:46:36 -0700 | [diff] [blame] | 630 | } |
Amir Goldstein | 4ff33aa | 2017-04-25 14:29:35 +0300 | [diff] [blame] | 631 | if (ret < 0) |
| 632 | break; |
Jan Kara | d8aaab4 | 2014-04-03 14:46:35 -0700 | [diff] [blame] | 633 | buf += ret; |
| 634 | count -= ret; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 635 | } |
Peter Zijlstra | 536ebe9ca | 2014-12-16 16:28:38 +0100 | [diff] [blame] | 636 | remove_wait_queue(&group->notification_waitq, &wait); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 637 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 638 | if (start != buf && ret != -EFAULT) |
| 639 | ret = buf - start; |
| 640 | return ret; |
| 641 | } |
| 642 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 643 | static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos) |
| 644 | { |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 645 | struct fanotify_response response = { .fd = -1, .response = -1 }; |
| 646 | struct fsnotify_group *group; |
| 647 | int ret; |
| 648 | |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 649 | if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) |
| 650 | return -EINVAL; |
| 651 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 652 | group = file->private_data; |
| 653 | |
Fabian Frederick | 5e23663 | 2020-05-12 20:19:21 +0200 | [diff] [blame] | 654 | if (count < sizeof(response)) |
| 655 | return -EINVAL; |
| 656 | |
| 657 | count = sizeof(response); |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 658 | |
| 659 | pr_debug("%s: group=%p count=%zu\n", __func__, group, count); |
| 660 | |
| 661 | if (copy_from_user(&response, buf, count)) |
| 662 | return -EFAULT; |
| 663 | |
| 664 | ret = process_access_response(group, &response); |
| 665 | if (ret < 0) |
| 666 | count = ret; |
| 667 | |
| 668 | return count; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 669 | } |
| 670 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 671 | static int fanotify_release(struct inode *ignored, struct file *file) |
| 672 | { |
| 673 | struct fsnotify_group *group = file->private_data; |
Amir Goldstein | 6f73171 | 2021-03-04 12:48:22 +0200 | [diff] [blame] | 674 | struct fsnotify_event *fsn_event; |
Andrew Morton | 19ba54f | 2010-10-28 17:21:59 -0400 | [diff] [blame] | 675 | |
Jan Kara | 5838d44 | 2014-08-06 16:03:28 -0700 | [diff] [blame] | 676 | /* |
Jan Kara | 96d4101 | 2016-09-19 14:44:30 -0700 | [diff] [blame] | 677 | * Stop new events from arriving in the notification queue. since |
| 678 | * userspace cannot use fanotify fd anymore, no event can enter or |
| 679 | * leave access_list by now either. |
| 680 | */ |
| 681 | fsnotify_group_stop_queueing(group); |
| 682 | |
| 683 | /* |
| 684 | * Process all permission events on access_list and notification queue |
| 685 | * and simulate reply from userspace. |
Jan Kara | 5838d44 | 2014-08-06 16:03:28 -0700 | [diff] [blame] | 686 | */ |
Jan Kara | 073f655 | 2016-10-07 16:56:55 -0700 | [diff] [blame] | 687 | spin_lock(&group->notification_lock); |
Jan Kara | ca6f869 | 2019-01-09 13:21:01 +0100 | [diff] [blame] | 688 | while (!list_empty(&group->fanotify_data.access_list)) { |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 689 | struct fanotify_perm_event *event; |
| 690 | |
Jan Kara | ca6f869 | 2019-01-09 13:21:01 +0100 | [diff] [blame] | 691 | event = list_first_entry(&group->fanotify_data.access_list, |
| 692 | struct fanotify_perm_event, fae.fse.list); |
Jan Kara | f083441 | 2014-04-03 14:46:33 -0700 | [diff] [blame] | 693 | list_del_init(&event->fae.fse.list); |
Jan Kara | 4087328 | 2019-01-08 14:02:44 +0100 | [diff] [blame] | 694 | finish_permission_event(group, event, FAN_ALLOW); |
| 695 | spin_lock(&group->notification_lock); |
Eric Paris | 2eebf58 | 2010-08-18 12:25:50 -0400 | [diff] [blame] | 696 | } |
Eric Paris | 2eebf58 | 2010-08-18 12:25:50 -0400 | [diff] [blame] | 697 | |
Jan Kara | 5838d44 | 2014-08-06 16:03:28 -0700 | [diff] [blame] | 698 | /* |
Jan Kara | 96d4101 | 2016-09-19 14:44:30 -0700 | [diff] [blame] | 699 | * Destroy all non-permission events. For permission events just |
| 700 | * dequeue them and set the response. They will be freed once the |
| 701 | * response is consumed and fanotify_get_response() returns. |
Jan Kara | 5838d44 | 2014-08-06 16:03:28 -0700 | [diff] [blame] | 702 | */ |
Amir Goldstein | 6f73171 | 2021-03-04 12:48:22 +0200 | [diff] [blame] | 703 | while ((fsn_event = fsnotify_remove_first_event(group))) { |
| 704 | struct fanotify_event *event = FANOTIFY_E(fsn_event); |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 705 | |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 706 | if (!(event->mask & FANOTIFY_PERM_EVENTS)) { |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame] | 707 | spin_unlock(&group->notification_lock); |
Amir Goldstein | 6f73171 | 2021-03-04 12:48:22 +0200 | [diff] [blame] | 708 | fsnotify_destroy_event(group, fsn_event); |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 709 | } else { |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 710 | finish_permission_event(group, FANOTIFY_PERM(event), |
Jan Kara | 4087328 | 2019-01-08 14:02:44 +0100 | [diff] [blame] | 711 | FAN_ALLOW); |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 712 | } |
Jan Kara | 4087328 | 2019-01-08 14:02:44 +0100 | [diff] [blame] | 713 | spin_lock(&group->notification_lock); |
Jan Kara | 96d4101 | 2016-09-19 14:44:30 -0700 | [diff] [blame] | 714 | } |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame] | 715 | spin_unlock(&group->notification_lock); |
Jan Kara | 96d4101 | 2016-09-19 14:44:30 -0700 | [diff] [blame] | 716 | |
| 717 | /* Response for all permission events it set, wakeup waiters */ |
Eric Paris | 2eebf58 | 2010-08-18 12:25:50 -0400 | [diff] [blame] | 718 | wake_up(&group->fanotify_data.access_waitq); |
Eric Paris | 0a6b6bd | 2011-10-14 17:43:39 -0400 | [diff] [blame] | 719 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 720 | /* matches the fanotify_init->fsnotify_alloc_group */ |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 721 | fsnotify_destroy_group(group); |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 722 | |
| 723 | return 0; |
| 724 | } |
| 725 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 726 | static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 727 | { |
| 728 | struct fsnotify_group *group; |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 729 | struct fsnotify_event *fsn_event; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 730 | void __user *p; |
| 731 | int ret = -ENOTTY; |
| 732 | size_t send_len = 0; |
| 733 | |
| 734 | group = file->private_data; |
| 735 | |
| 736 | p = (void __user *) arg; |
| 737 | |
| 738 | switch (cmd) { |
| 739 | case FIONREAD: |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame] | 740 | spin_lock(&group->notification_lock); |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 741 | list_for_each_entry(fsn_event, &group->notification_list, list) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 742 | send_len += FAN_EVENT_METADATA_LEN; |
Jan Kara | c21dbe2 | 2016-10-07 16:56:52 -0700 | [diff] [blame] | 743 | spin_unlock(&group->notification_lock); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 744 | ret = put_user(send_len, (int __user *) p); |
| 745 | break; |
| 746 | } |
| 747 | |
| 748 | return ret; |
| 749 | } |
| 750 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 751 | static const struct file_operations fanotify_fops = { |
Cyrill Gorcunov | be77196 | 2012-12-17 16:05:12 -0800 | [diff] [blame] | 752 | .show_fdinfo = fanotify_show_fdinfo, |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 753 | .poll = fanotify_poll, |
| 754 | .read = fanotify_read, |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 755 | .write = fanotify_write, |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 756 | .fasync = NULL, |
| 757 | .release = fanotify_release, |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 758 | .unlocked_ioctl = fanotify_ioctl, |
Arnd Bergmann | 1832f2d | 2018-09-11 21:59:08 +0200 | [diff] [blame] | 759 | .compat_ioctl = compat_ptr_ioctl, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 760 | .llseek = noop_llseek, |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 761 | }; |
| 762 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 763 | static int fanotify_find_path(int dfd, const char __user *filename, |
Aaron Goidel | ac5656d | 2019-08-12 11:20:00 -0400 | [diff] [blame] | 764 | struct path *path, unsigned int flags, __u64 mask, |
| 765 | unsigned int obj_type) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 766 | { |
| 767 | int ret; |
| 768 | |
| 769 | pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__, |
| 770 | dfd, filename, flags); |
| 771 | |
| 772 | if (filename == NULL) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 773 | struct fd f = fdget(dfd); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 774 | |
| 775 | ret = -EBADF; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 776 | if (!f.file) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 777 | goto out; |
| 778 | |
| 779 | ret = -ENOTDIR; |
| 780 | if ((flags & FAN_MARK_ONLYDIR) && |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 781 | !(S_ISDIR(file_inode(f.file)->i_mode))) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 782 | fdput(f); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 783 | goto out; |
| 784 | } |
| 785 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 786 | *path = f.file->f_path; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 787 | path_get(path); |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 788 | fdput(f); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 789 | } else { |
| 790 | unsigned int lookup_flags = 0; |
| 791 | |
| 792 | if (!(flags & FAN_MARK_DONT_FOLLOW)) |
| 793 | lookup_flags |= LOOKUP_FOLLOW; |
| 794 | if (flags & FAN_MARK_ONLYDIR) |
| 795 | lookup_flags |= LOOKUP_DIRECTORY; |
| 796 | |
| 797 | ret = user_path_at(dfd, filename, lookup_flags, path); |
| 798 | if (ret) |
| 799 | goto out; |
| 800 | } |
| 801 | |
| 802 | /* you can only watch an inode if you have read permissions on it */ |
Christian Brauner | 02f92b3 | 2021-01-21 14:19:22 +0100 | [diff] [blame] | 803 | ret = path_permission(path, MAY_READ); |
Aaron Goidel | ac5656d | 2019-08-12 11:20:00 -0400 | [diff] [blame] | 804 | if (ret) { |
| 805 | path_put(path); |
| 806 | goto out; |
| 807 | } |
| 808 | |
| 809 | ret = security_path_notify(path, mask, obj_type); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 810 | if (ret) |
| 811 | path_put(path); |
Aaron Goidel | ac5656d | 2019-08-12 11:20:00 -0400 | [diff] [blame] | 812 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 813 | out: |
| 814 | return ret; |
| 815 | } |
| 816 | |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 817 | static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark, |
Amir Goldstein | 4ed6814 | 2020-07-16 11:42:14 +0300 | [diff] [blame] | 818 | __u32 mask, unsigned int flags, |
| 819 | __u32 umask, int *destroy) |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 820 | { |
Lino Sanfilippo | d2c1874 | 2015-02-10 14:08:24 -0800 | [diff] [blame] | 821 | __u32 oldmask = 0; |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 822 | |
Amir Goldstein | 4ed6814 | 2020-07-16 11:42:14 +0300 | [diff] [blame] | 823 | /* umask bits cannot be removed by user */ |
| 824 | mask &= ~umask; |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 825 | spin_lock(&fsn_mark->lock); |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 826 | if (!(flags & FAN_MARK_IGNORED_MASK)) { |
| 827 | oldmask = fsn_mark->mask; |
Amir Goldstein | a72fd22 | 2018-10-04 00:25:34 +0300 | [diff] [blame] | 828 | fsn_mark->mask &= ~mask; |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 829 | } else { |
Amir Goldstein | a72fd22 | 2018-10-04 00:25:34 +0300 | [diff] [blame] | 830 | fsn_mark->ignored_mask &= ~mask; |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 831 | } |
Amir Goldstein | 4ed6814 | 2020-07-16 11:42:14 +0300 | [diff] [blame] | 832 | /* |
| 833 | * We need to keep the mark around even if remaining mask cannot |
| 834 | * result in any events (e.g. mask == FAN_ONDIR) to support incremenal |
| 835 | * changes to the mask. |
| 836 | * Destroy mark when only umask bits remain. |
| 837 | */ |
| 838 | *destroy = !((fsn_mark->mask | fsn_mark->ignored_mask) & ~umask); |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 839 | spin_unlock(&fsn_mark->lock); |
| 840 | |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 841 | return mask & oldmask; |
| 842 | } |
| 843 | |
Amir Goldstein | eaa2c6b | 2018-06-23 17:54:51 +0300 | [diff] [blame] | 844 | static int fanotify_remove_mark(struct fsnotify_group *group, |
| 845 | fsnotify_connp_t *connp, __u32 mask, |
Amir Goldstein | 4ed6814 | 2020-07-16 11:42:14 +0300 | [diff] [blame] | 846 | unsigned int flags, __u32 umask) |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 847 | { |
| 848 | struct fsnotify_mark *fsn_mark = NULL; |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 849 | __u32 removed; |
Lino Sanfilippo | 6dfbd14 | 2011-06-14 17:29:49 +0200 | [diff] [blame] | 850 | int destroy_mark; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 851 | |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 852 | mutex_lock(&group->mark_mutex); |
Amir Goldstein | eaa2c6b | 2018-06-23 17:54:51 +0300 | [diff] [blame] | 853 | fsn_mark = fsnotify_find_mark(connp, group); |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 854 | if (!fsn_mark) { |
| 855 | mutex_unlock(&group->mark_mutex); |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 856 | return -ENOENT; |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 857 | } |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 858 | |
Lino Sanfilippo | 6dfbd14 | 2011-06-14 17:29:49 +0200 | [diff] [blame] | 859 | removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags, |
Amir Goldstein | 4ed6814 | 2020-07-16 11:42:14 +0300 | [diff] [blame] | 860 | umask, &destroy_mark); |
Amir Goldstein | 3ac70bf | 2018-06-23 17:54:50 +0300 | [diff] [blame] | 861 | if (removed & fsnotify_conn_mask(fsn_mark->connector)) |
| 862 | fsnotify_recalc_mask(fsn_mark->connector); |
Lino Sanfilippo | 6dfbd14 | 2011-06-14 17:29:49 +0200 | [diff] [blame] | 863 | if (destroy_mark) |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 864 | fsnotify_detach_mark(fsn_mark); |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 865 | mutex_unlock(&group->mark_mutex); |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 866 | if (destroy_mark) |
| 867 | fsnotify_free_mark(fsn_mark); |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 868 | |
Jan Kara | b1362ed | 2016-12-21 16:28:45 +0100 | [diff] [blame] | 869 | /* matches the fsnotify_find_mark() */ |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 870 | fsnotify_put_mark(fsn_mark); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 871 | return 0; |
| 872 | } |
| 873 | |
Amir Goldstein | eaa2c6b | 2018-06-23 17:54:51 +0300 | [diff] [blame] | 874 | static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group, |
| 875 | struct vfsmount *mnt, __u32 mask, |
Amir Goldstein | 4ed6814 | 2020-07-16 11:42:14 +0300 | [diff] [blame] | 876 | unsigned int flags, __u32 umask) |
Amir Goldstein | eaa2c6b | 2018-06-23 17:54:51 +0300 | [diff] [blame] | 877 | { |
| 878 | return fanotify_remove_mark(group, &real_mount(mnt)->mnt_fsnotify_marks, |
Amir Goldstein | 4ed6814 | 2020-07-16 11:42:14 +0300 | [diff] [blame] | 879 | mask, flags, umask); |
Amir Goldstein | eaa2c6b | 2018-06-23 17:54:51 +0300 | [diff] [blame] | 880 | } |
| 881 | |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 882 | static int fanotify_remove_sb_mark(struct fsnotify_group *group, |
Amir Goldstein | 4ed6814 | 2020-07-16 11:42:14 +0300 | [diff] [blame] | 883 | struct super_block *sb, __u32 mask, |
| 884 | unsigned int flags, __u32 umask) |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 885 | { |
Amir Goldstein | 4ed6814 | 2020-07-16 11:42:14 +0300 | [diff] [blame] | 886 | return fanotify_remove_mark(group, &sb->s_fsnotify_marks, mask, |
| 887 | flags, umask); |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 888 | } |
| 889 | |
Amir Goldstein | eaa2c6b | 2018-06-23 17:54:51 +0300 | [diff] [blame] | 890 | static int fanotify_remove_inode_mark(struct fsnotify_group *group, |
| 891 | struct inode *inode, __u32 mask, |
Amir Goldstein | 4ed6814 | 2020-07-16 11:42:14 +0300 | [diff] [blame] | 892 | unsigned int flags, __u32 umask) |
Amir Goldstein | eaa2c6b | 2018-06-23 17:54:51 +0300 | [diff] [blame] | 893 | { |
| 894 | return fanotify_remove_mark(group, &inode->i_fsnotify_marks, mask, |
Amir Goldstein | 4ed6814 | 2020-07-16 11:42:14 +0300 | [diff] [blame] | 895 | flags, umask); |
Amir Goldstein | eaa2c6b | 2018-06-23 17:54:51 +0300 | [diff] [blame] | 896 | } |
| 897 | |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 898 | static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark, |
| 899 | __u32 mask, |
| 900 | unsigned int flags) |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 901 | { |
Eric Paris | 192ca4d | 2010-10-28 17:21:59 -0400 | [diff] [blame] | 902 | __u32 oldmask = -1; |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 903 | |
| 904 | spin_lock(&fsn_mark->lock); |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 905 | if (!(flags & FAN_MARK_IGNORED_MASK)) { |
| 906 | oldmask = fsn_mark->mask; |
Amir Goldstein | a72fd22 | 2018-10-04 00:25:34 +0300 | [diff] [blame] | 907 | fsn_mark->mask |= mask; |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 908 | } else { |
Amir Goldstein | a72fd22 | 2018-10-04 00:25:34 +0300 | [diff] [blame] | 909 | fsn_mark->ignored_mask |= mask; |
Eric Paris | c9778a9 | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 910 | if (flags & FAN_MARK_IGNORED_SURV_MODIFY) |
| 911 | fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY; |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 912 | } |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 913 | spin_unlock(&fsn_mark->lock); |
| 914 | |
| 915 | return mask & ~oldmask; |
| 916 | } |
| 917 | |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 918 | static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group, |
Amir Goldstein | b812a9f | 2018-06-23 17:54:48 +0300 | [diff] [blame] | 919 | fsnotify_connp_t *connp, |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 920 | unsigned int type, |
| 921 | __kernel_fsid_t *fsid) |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 922 | { |
Amir Goldstein | 5b8fea6 | 2021-03-04 13:29:20 +0200 | [diff] [blame] | 923 | struct ucounts *ucounts = group->fanotify_data.ucounts; |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 924 | struct fsnotify_mark *mark; |
| 925 | int ret; |
| 926 | |
Amir Goldstein | 5b8fea6 | 2021-03-04 13:29:20 +0200 | [diff] [blame] | 927 | /* |
| 928 | * Enforce per user marks limits per user in all containing user ns. |
| 929 | * A group with FAN_UNLIMITED_MARKS does not contribute to mark count |
| 930 | * in the limited groups account. |
| 931 | */ |
| 932 | if (!FAN_GROUP_FLAG(group, FAN_UNLIMITED_MARKS) && |
| 933 | !inc_ucount(ucounts->ns, ucounts->uid, UCOUNT_FANOTIFY_MARKS)) |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 934 | return ERR_PTR(-ENOSPC); |
| 935 | |
| 936 | mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL); |
Amir Goldstein | 5b8fea6 | 2021-03-04 13:29:20 +0200 | [diff] [blame] | 937 | if (!mark) { |
| 938 | ret = -ENOMEM; |
| 939 | goto out_dec_ucounts; |
| 940 | } |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 941 | |
Jan Kara | 054c636 | 2016-12-21 18:06:12 +0100 | [diff] [blame] | 942 | fsnotify_init_mark(mark, group); |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 943 | ret = fsnotify_add_mark_locked(mark, connp, type, 0, fsid); |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 944 | if (ret) { |
| 945 | fsnotify_put_mark(mark); |
Amir Goldstein | 5b8fea6 | 2021-03-04 13:29:20 +0200 | [diff] [blame] | 946 | goto out_dec_ucounts; |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | return mark; |
Amir Goldstein | 5b8fea6 | 2021-03-04 13:29:20 +0200 | [diff] [blame] | 950 | |
| 951 | out_dec_ucounts: |
| 952 | if (!FAN_GROUP_FLAG(group, FAN_UNLIMITED_MARKS)) |
| 953 | dec_ucount(ucounts, UCOUNT_FANOTIFY_MARKS); |
| 954 | return ERR_PTR(ret); |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | |
Amir Goldstein | eaa2c6b | 2018-06-23 17:54:51 +0300 | [diff] [blame] | 958 | static int fanotify_add_mark(struct fsnotify_group *group, |
| 959 | fsnotify_connp_t *connp, unsigned int type, |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 960 | __u32 mask, unsigned int flags, |
| 961 | __kernel_fsid_t *fsid) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 962 | { |
| 963 | struct fsnotify_mark *fsn_mark; |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 964 | __u32 added; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 965 | |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 966 | mutex_lock(&group->mark_mutex); |
Amir Goldstein | b812a9f | 2018-06-23 17:54:48 +0300 | [diff] [blame] | 967 | fsn_mark = fsnotify_find_mark(connp, group); |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 968 | if (!fsn_mark) { |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 969 | fsn_mark = fanotify_add_new_mark(group, connp, type, fsid); |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 970 | if (IS_ERR(fsn_mark)) { |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 971 | mutex_unlock(&group->mark_mutex); |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 972 | return PTR_ERR(fsn_mark); |
Lino Sanfilippo | 7b18527 | 2013-07-08 15:59:42 -0700 | [diff] [blame] | 973 | } |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 974 | } |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 975 | added = fanotify_mark_add_to_mask(fsn_mark, mask, flags); |
Amir Goldstein | 3ac70bf | 2018-06-23 17:54:50 +0300 | [diff] [blame] | 976 | if (added & ~fsnotify_conn_mask(fsn_mark->connector)) |
| 977 | fsnotify_recalc_mask(fsn_mark->connector); |
Jan Kara | c974764 | 2016-12-14 13:53:46 +0100 | [diff] [blame] | 978 | mutex_unlock(&group->mark_mutex); |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 979 | |
Lino Sanfilippo | fa218ab | 2010-11-09 18:18:16 +0100 | [diff] [blame] | 980 | fsnotify_put_mark(fsn_mark); |
Lino Sanfilippo | 5e9c070c | 2013-07-08 15:59:43 -0700 | [diff] [blame] | 981 | return 0; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 982 | } |
| 983 | |
Amir Goldstein | eaa2c6b | 2018-06-23 17:54:51 +0300 | [diff] [blame] | 984 | static int fanotify_add_vfsmount_mark(struct fsnotify_group *group, |
| 985 | struct vfsmount *mnt, __u32 mask, |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 986 | unsigned int flags, __kernel_fsid_t *fsid) |
Amir Goldstein | eaa2c6b | 2018-06-23 17:54:51 +0300 | [diff] [blame] | 987 | { |
| 988 | return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks, |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 989 | FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags, fsid); |
Amir Goldstein | eaa2c6b | 2018-06-23 17:54:51 +0300 | [diff] [blame] | 990 | } |
| 991 | |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 992 | static int fanotify_add_sb_mark(struct fsnotify_group *group, |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 993 | struct super_block *sb, __u32 mask, |
| 994 | unsigned int flags, __kernel_fsid_t *fsid) |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 995 | { |
| 996 | return fanotify_add_mark(group, &sb->s_fsnotify_marks, |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 997 | FSNOTIFY_OBJ_TYPE_SB, mask, flags, fsid); |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 998 | } |
| 999 | |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 1000 | static int fanotify_add_inode_mark(struct fsnotify_group *group, |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 1001 | struct inode *inode, __u32 mask, |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 1002 | unsigned int flags, __kernel_fsid_t *fsid) |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 1003 | { |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 1004 | pr_debug("%s: group=%p inode=%p\n", __func__, group, inode); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1005 | |
Eric Paris | 5322a59 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 1006 | /* |
| 1007 | * If some other task has this inode open for write we should not add |
| 1008 | * an ignored mark, unless that ignored mark is supposed to survive |
| 1009 | * modification changes anyway. |
| 1010 | */ |
| 1011 | if ((flags & FAN_MARK_IGNORED_MASK) && |
| 1012 | !(flags & FAN_MARK_IGNORED_SURV_MODIFY) && |
Nikolay Borisov | ac9498d | 2018-12-11 10:27:23 +0200 | [diff] [blame] | 1013 | inode_is_open_for_write(inode)) |
Eric Paris | 5322a59 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 1014 | return 0; |
| 1015 | |
Amir Goldstein | eaa2c6b | 2018-06-23 17:54:51 +0300 | [diff] [blame] | 1016 | return fanotify_add_mark(group, &inode->i_fsnotify_marks, |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 1017 | FSNOTIFY_OBJ_TYPE_INODE, mask, flags, fsid); |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 1018 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1019 | |
Amir Goldstein | b8a6c3a | 2020-07-08 14:11:42 +0300 | [diff] [blame] | 1020 | static struct fsnotify_event *fanotify_alloc_overflow_event(void) |
| 1021 | { |
| 1022 | struct fanotify_event *oevent; |
| 1023 | |
| 1024 | oevent = kmalloc(sizeof(*oevent), GFP_KERNEL_ACCOUNT); |
| 1025 | if (!oevent) |
| 1026 | return NULL; |
| 1027 | |
| 1028 | fanotify_init_event(oevent, 0, FS_Q_OVERFLOW); |
| 1029 | oevent->type = FANOTIFY_EVENT_TYPE_OVERFLOW; |
| 1030 | |
| 1031 | return &oevent->fse; |
| 1032 | } |
| 1033 | |
Amir Goldstein | 94e00d2 | 2021-03-04 12:48:25 +0200 | [diff] [blame] | 1034 | static struct hlist_head *fanotify_alloc_merge_hash(void) |
| 1035 | { |
| 1036 | struct hlist_head *hash; |
| 1037 | |
| 1038 | hash = kmalloc(sizeof(struct hlist_head) << FANOTIFY_HTABLE_BITS, |
| 1039 | GFP_KERNEL_ACCOUNT); |
| 1040 | if (!hash) |
| 1041 | return NULL; |
| 1042 | |
| 1043 | __hash_init(hash, FANOTIFY_HTABLE_SIZE); |
| 1044 | |
| 1045 | return hash; |
| 1046 | } |
| 1047 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1048 | /* fanotify syscalls */ |
Eric Paris | 08ae893 | 2010-05-27 09:41:40 -0400 | [diff] [blame] | 1049 | SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 1050 | { |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1051 | struct fsnotify_group *group; |
| 1052 | int f_flags, fd; |
Amir Goldstein | 83b7a59 | 2020-07-16 11:42:26 +0300 | [diff] [blame] | 1053 | unsigned int fid_mode = flags & FANOTIFY_FID_BITS; |
| 1054 | unsigned int class = flags & FANOTIFY_CLASS_BITS; |
Amir Goldstein | a8b98c8 | 2021-05-24 16:53:21 +0300 | [diff] [blame] | 1055 | unsigned int internal_flags = 0; |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1056 | |
Amir Goldstein | 96a71f2 | 2018-09-21 21:20:30 +0300 | [diff] [blame] | 1057 | pr_debug("%s: flags=%x event_f_flags=%x\n", |
| 1058 | __func__, flags, event_f_flags); |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1059 | |
Amir Goldstein | 7cea2a3 | 2021-03-04 13:29:21 +0200 | [diff] [blame] | 1060 | if (!capable(CAP_SYS_ADMIN)) { |
| 1061 | /* |
| 1062 | * An unprivileged user can setup an fanotify group with |
| 1063 | * limited functionality - an unprivileged group is limited to |
| 1064 | * notification events with file handles and it cannot use |
| 1065 | * unlimited queue/marks. |
| 1066 | */ |
| 1067 | if ((flags & FANOTIFY_ADMIN_INIT_FLAGS) || !fid_mode) |
| 1068 | return -EPERM; |
Amir Goldstein | a8b98c8 | 2021-05-24 16:53:21 +0300 | [diff] [blame] | 1069 | |
| 1070 | /* |
| 1071 | * Setting the internal flag FANOTIFY_UNPRIV on the group |
| 1072 | * prevents setting mount/filesystem marks on this group and |
| 1073 | * prevents reporting pid and open fd in events. |
| 1074 | */ |
| 1075 | internal_flags |= FANOTIFY_UNPRIV; |
Amir Goldstein | 7cea2a3 | 2021-03-04 13:29:21 +0200 | [diff] [blame] | 1076 | } |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1077 | |
Steve Grubb | de8cd83 | 2017-10-02 20:21:39 -0400 | [diff] [blame] | 1078 | #ifdef CONFIG_AUDITSYSCALL |
Amir Goldstein | 23c9dee | 2018-10-04 00:25:35 +0300 | [diff] [blame] | 1079 | if (flags & ~(FANOTIFY_INIT_FLAGS | FAN_ENABLE_AUDIT)) |
Steve Grubb | de8cd83 | 2017-10-02 20:21:39 -0400 | [diff] [blame] | 1080 | #else |
Amir Goldstein | 23c9dee | 2018-10-04 00:25:35 +0300 | [diff] [blame] | 1081 | if (flags & ~FANOTIFY_INIT_FLAGS) |
Steve Grubb | de8cd83 | 2017-10-02 20:21:39 -0400 | [diff] [blame] | 1082 | #endif |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1083 | return -EINVAL; |
| 1084 | |
Heinrich Schuchardt | 48149e9 | 2014-06-04 16:05:44 -0700 | [diff] [blame] | 1085 | if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS) |
| 1086 | return -EINVAL; |
| 1087 | |
| 1088 | switch (event_f_flags & O_ACCMODE) { |
| 1089 | case O_RDONLY: |
| 1090 | case O_RDWR: |
| 1091 | case O_WRONLY: |
| 1092 | break; |
| 1093 | default: |
| 1094 | return -EINVAL; |
| 1095 | } |
| 1096 | |
Amir Goldstein | 83b7a59 | 2020-07-16 11:42:26 +0300 | [diff] [blame] | 1097 | if (fid_mode && class != FAN_CLASS_NOTIF) |
Amir Goldstein | a8b13aa | 2019-01-10 19:04:36 +0200 | [diff] [blame] | 1098 | return -EINVAL; |
| 1099 | |
Amir Goldstein | 929943b | 2020-07-16 11:42:28 +0300 | [diff] [blame] | 1100 | /* |
Amir Goldstein | 929943b | 2020-07-16 11:42:28 +0300 | [diff] [blame] | 1101 | * Child name is reported with parent fid so requires dir fid. |
Amir Goldstein | 691d976 | 2020-07-16 11:42:30 +0300 | [diff] [blame] | 1102 | * We can report both child fid and dir fid with or without name. |
Amir Goldstein | 929943b | 2020-07-16 11:42:28 +0300 | [diff] [blame] | 1103 | */ |
Amir Goldstein | 691d976 | 2020-07-16 11:42:30 +0300 | [diff] [blame] | 1104 | if ((fid_mode & FAN_REPORT_NAME) && !(fid_mode & FAN_REPORT_DIR_FID)) |
Amir Goldstein | 83b7a59 | 2020-07-16 11:42:26 +0300 | [diff] [blame] | 1105 | return -EINVAL; |
Amir Goldstein | 83b7a59 | 2020-07-16 11:42:26 +0300 | [diff] [blame] | 1106 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 1107 | f_flags = O_RDWR | FMODE_NONOTIFY; |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1108 | if (flags & FAN_CLOEXEC) |
| 1109 | f_flags |= O_CLOEXEC; |
| 1110 | if (flags & FAN_NONBLOCK) |
| 1111 | f_flags |= O_NONBLOCK; |
| 1112 | |
| 1113 | /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */ |
Shakeel Butt | ac7b79f | 2020-12-19 20:46:08 -0800 | [diff] [blame] | 1114 | group = fsnotify_alloc_user_group(&fanotify_fsnotify_ops); |
Eric Paris | 2637919 | 2010-11-23 23:48:26 -0500 | [diff] [blame] | 1115 | if (IS_ERR(group)) { |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1116 | return PTR_ERR(group); |
Eric Paris | 2637919 | 2010-11-23 23:48:26 -0500 | [diff] [blame] | 1117 | } |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1118 | |
Amir Goldstein | 5b8fea6 | 2021-03-04 13:29:20 +0200 | [diff] [blame] | 1119 | /* Enforce groups limits per user in all containing user ns */ |
| 1120 | group->fanotify_data.ucounts = inc_ucount(current_user_ns(), |
| 1121 | current_euid(), |
| 1122 | UCOUNT_FANOTIFY_GROUPS); |
| 1123 | if (!group->fanotify_data.ucounts) { |
| 1124 | fd = -EMFILE; |
| 1125 | goto out_destroy_group; |
| 1126 | } |
| 1127 | |
Amir Goldstein | a8b98c8 | 2021-05-24 16:53:21 +0300 | [diff] [blame] | 1128 | group->fanotify_data.flags = flags | internal_flags; |
Shakeel Butt | d46eb14b | 2018-08-17 15:46:39 -0700 | [diff] [blame] | 1129 | group->memcg = get_mem_cgroup_from_mm(current->mm); |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 1130 | |
Amir Goldstein | 94e00d2 | 2021-03-04 12:48:25 +0200 | [diff] [blame] | 1131 | group->fanotify_data.merge_hash = fanotify_alloc_merge_hash(); |
| 1132 | if (!group->fanotify_data.merge_hash) { |
| 1133 | fd = -ENOMEM; |
| 1134 | goto out_destroy_group; |
| 1135 | } |
| 1136 | |
Amir Goldstein | b8a6c3a | 2020-07-08 14:11:42 +0300 | [diff] [blame] | 1137 | group->overflow_event = fanotify_alloc_overflow_event(); |
| 1138 | if (unlikely(!group->overflow_event)) { |
Jan Kara | ff57cd5 | 2014-02-21 19:14:11 +0100 | [diff] [blame] | 1139 | fd = -ENOMEM; |
| 1140 | goto out_destroy_group; |
| 1141 | } |
Jan Kara | ff57cd5 | 2014-02-21 19:14:11 +0100 | [diff] [blame] | 1142 | |
Will Woods | 1e2ee49 | 2014-05-06 12:50:10 -0700 | [diff] [blame] | 1143 | if (force_o_largefile()) |
| 1144 | event_f_flags |= O_LARGEFILE; |
Eric Paris | 80af258 | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 1145 | group->fanotify_data.f_flags = event_f_flags; |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 1146 | init_waitqueue_head(&group->fanotify_data.access_waitq); |
| 1147 | INIT_LIST_HEAD(&group->fanotify_data.access_list); |
Amir Goldstein | 83b7a59 | 2020-07-16 11:42:26 +0300 | [diff] [blame] | 1148 | switch (class) { |
Eric Paris | 4231a23 | 2010-10-28 17:21:56 -0400 | [diff] [blame] | 1149 | case FAN_CLASS_NOTIF: |
| 1150 | group->priority = FS_PRIO_0; |
| 1151 | break; |
| 1152 | case FAN_CLASS_CONTENT: |
| 1153 | group->priority = FS_PRIO_1; |
| 1154 | break; |
| 1155 | case FAN_CLASS_PRE_CONTENT: |
| 1156 | group->priority = FS_PRIO_2; |
| 1157 | break; |
| 1158 | default: |
| 1159 | fd = -EINVAL; |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 1160 | goto out_destroy_group; |
Eric Paris | 4231a23 | 2010-10-28 17:21:56 -0400 | [diff] [blame] | 1161 | } |
Eric Paris | cb2d429 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 1162 | |
Eric Paris | 5dd03f5 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 1163 | if (flags & FAN_UNLIMITED_QUEUE) { |
| 1164 | fd = -EPERM; |
| 1165 | if (!capable(CAP_SYS_ADMIN)) |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 1166 | goto out_destroy_group; |
Eric Paris | 5dd03f5 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 1167 | group->max_events = UINT_MAX; |
| 1168 | } else { |
Amir Goldstein | 5b8fea6 | 2021-03-04 13:29:20 +0200 | [diff] [blame] | 1169 | group->max_events = fanotify_max_queued_events; |
Eric Paris | 5dd03f5 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 1170 | } |
Eric Paris | 2529a0d | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 1171 | |
Eric Paris | ac7e22d | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 1172 | if (flags & FAN_UNLIMITED_MARKS) { |
| 1173 | fd = -EPERM; |
| 1174 | if (!capable(CAP_SYS_ADMIN)) |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 1175 | goto out_destroy_group; |
Eric Paris | ac7e22d | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 1176 | } |
Eric Paris | e7099d8 | 2010-10-28 17:21:57 -0400 | [diff] [blame] | 1177 | |
Steve Grubb | de8cd83 | 2017-10-02 20:21:39 -0400 | [diff] [blame] | 1178 | if (flags & FAN_ENABLE_AUDIT) { |
| 1179 | fd = -EPERM; |
| 1180 | if (!capable(CAP_AUDIT_WRITE)) |
| 1181 | goto out_destroy_group; |
Steve Grubb | de8cd83 | 2017-10-02 20:21:39 -0400 | [diff] [blame] | 1182 | } |
| 1183 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1184 | fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags); |
| 1185 | if (fd < 0) |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 1186 | goto out_destroy_group; |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1187 | |
| 1188 | return fd; |
| 1189 | |
Lino Sanfilippo | d8153d4 | 2011-06-14 17:29:45 +0200 | [diff] [blame] | 1190 | out_destroy_group: |
| 1191 | fsnotify_destroy_group(group); |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1192 | return fd; |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 1193 | } |
Eric Paris | bbaa416 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1194 | |
Amir Goldstein | a8b13aa | 2019-01-10 19:04:36 +0200 | [diff] [blame] | 1195 | /* Check if filesystem can encode a unique fid */ |
Amir Goldstein | 7307228 | 2019-01-10 19:04:39 +0200 | [diff] [blame] | 1196 | static int fanotify_test_fid(struct path *path, __kernel_fsid_t *fsid) |
Amir Goldstein | a8b13aa | 2019-01-10 19:04:36 +0200 | [diff] [blame] | 1197 | { |
Amir Goldstein | 7307228 | 2019-01-10 19:04:39 +0200 | [diff] [blame] | 1198 | __kernel_fsid_t root_fsid; |
Amir Goldstein | a8b13aa | 2019-01-10 19:04:36 +0200 | [diff] [blame] | 1199 | int err; |
| 1200 | |
| 1201 | /* |
| 1202 | * Make sure path is not in filesystem with zero fsid (e.g. tmpfs). |
| 1203 | */ |
Amir Goldstein | 7307228 | 2019-01-10 19:04:39 +0200 | [diff] [blame] | 1204 | err = vfs_get_fsid(path->dentry, fsid); |
Amir Goldstein | a8b13aa | 2019-01-10 19:04:36 +0200 | [diff] [blame] | 1205 | if (err) |
| 1206 | return err; |
| 1207 | |
Amir Goldstein | 7307228 | 2019-01-10 19:04:39 +0200 | [diff] [blame] | 1208 | if (!fsid->val[0] && !fsid->val[1]) |
Amir Goldstein | a8b13aa | 2019-01-10 19:04:36 +0200 | [diff] [blame] | 1209 | return -ENODEV; |
| 1210 | |
| 1211 | /* |
| 1212 | * Make sure path is not inside a filesystem subvolume (e.g. btrfs) |
| 1213 | * which uses a different fsid than sb root. |
| 1214 | */ |
Amir Goldstein | 7307228 | 2019-01-10 19:04:39 +0200 | [diff] [blame] | 1215 | err = vfs_get_fsid(path->dentry->d_sb->s_root, &root_fsid); |
Amir Goldstein | a8b13aa | 2019-01-10 19:04:36 +0200 | [diff] [blame] | 1216 | if (err) |
| 1217 | return err; |
| 1218 | |
Amir Goldstein | 7307228 | 2019-01-10 19:04:39 +0200 | [diff] [blame] | 1219 | if (root_fsid.val[0] != fsid->val[0] || |
| 1220 | root_fsid.val[1] != fsid->val[1]) |
Amir Goldstein | a8b13aa | 2019-01-10 19:04:36 +0200 | [diff] [blame] | 1221 | return -EXDEV; |
| 1222 | |
| 1223 | /* |
| 1224 | * We need to make sure that the file system supports at least |
| 1225 | * encoding a file handle so user can use name_to_handle_at() to |
| 1226 | * compare fid returned with event to the file handle of watched |
| 1227 | * objects. However, name_to_handle_at() requires that the |
| 1228 | * filesystem also supports decoding file handles. |
| 1229 | */ |
| 1230 | if (!path->dentry->d_sb->s_export_op || |
| 1231 | !path->dentry->d_sb->s_export_op->fh_to_dentry) |
| 1232 | return -EOPNOTSUPP; |
| 1233 | |
| 1234 | return 0; |
| 1235 | } |
| 1236 | |
Jan Kara | 0b3b094 | 2019-05-15 16:28:34 +0200 | [diff] [blame] | 1237 | static int fanotify_events_supported(struct path *path, __u64 mask) |
| 1238 | { |
| 1239 | /* |
| 1240 | * Some filesystems such as 'proc' acquire unusual locks when opening |
| 1241 | * files. For them fanotify permission events have high chances of |
| 1242 | * deadlocking the system - open done when reporting fanotify event |
| 1243 | * blocks on this "unusual" lock while another process holding the lock |
| 1244 | * waits for fanotify permission event to be answered. Just disallow |
| 1245 | * permission events for such filesystems. |
| 1246 | */ |
| 1247 | if (mask & FANOTIFY_PERM_EVENTS && |
| 1248 | path->mnt->mnt_sb->s_type->fs_flags & FS_DISALLOW_NOTIFY_PERM) |
| 1249 | return -EINVAL; |
| 1250 | return 0; |
| 1251 | } |
| 1252 | |
Dominik Brodowski | 183caa3 | 2018-03-17 15:06:11 +0100 | [diff] [blame] | 1253 | static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, |
| 1254 | int dfd, const char __user *pathname) |
Eric Paris | bbaa416 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1255 | { |
Eric Paris | 0ff21db | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 1256 | struct inode *inode = NULL; |
| 1257 | struct vfsmount *mnt = NULL; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1258 | struct fsnotify_group *group; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1259 | struct fd f; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1260 | struct path path; |
Amir Goldstein | 7307228 | 2019-01-10 19:04:39 +0200 | [diff] [blame] | 1261 | __kernel_fsid_t __fsid, *fsid = NULL; |
Amir Goldstein | bdd5a46 | 2018-10-04 00:25:37 +0300 | [diff] [blame] | 1262 | u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS; |
Amir Goldstein | 23c9dee | 2018-10-04 00:25:35 +0300 | [diff] [blame] | 1263 | unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS; |
Amir Goldstein | 3ef8665 | 2020-07-16 11:42:13 +0300 | [diff] [blame] | 1264 | bool ignored = flags & FAN_MARK_IGNORED_MASK; |
Amir Goldstein | d809daf | 2020-07-16 11:42:12 +0300 | [diff] [blame] | 1265 | unsigned int obj_type, fid_mode; |
Amir Goldstein | 85af5d9 | 2020-07-16 11:42:15 +0300 | [diff] [blame] | 1266 | u32 umask = 0; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1267 | int ret; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1268 | |
| 1269 | pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n", |
| 1270 | __func__, fanotify_fd, flags, dfd, pathname, mask); |
| 1271 | |
| 1272 | /* we only use the lower 32 bits as of right now. */ |
Christian Brauner | 22d483b | 2021-03-25 09:37:43 +0100 | [diff] [blame] | 1273 | if (upper_32_bits(mask)) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1274 | return -EINVAL; |
| 1275 | |
Amir Goldstein | 23c9dee | 2018-10-04 00:25:35 +0300 | [diff] [blame] | 1276 | if (flags & ~FANOTIFY_MARK_FLAGS) |
Andreas Gruenbacher | 88380fe | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 1277 | return -EINVAL; |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 1278 | |
| 1279 | switch (mark_type) { |
| 1280 | case FAN_MARK_INODE: |
Aaron Goidel | ac5656d | 2019-08-12 11:20:00 -0400 | [diff] [blame] | 1281 | obj_type = FSNOTIFY_OBJ_TYPE_INODE; |
| 1282 | break; |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 1283 | case FAN_MARK_MOUNT: |
Aaron Goidel | ac5656d | 2019-08-12 11:20:00 -0400 | [diff] [blame] | 1284 | obj_type = FSNOTIFY_OBJ_TYPE_VFSMOUNT; |
| 1285 | break; |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 1286 | case FAN_MARK_FILESYSTEM: |
Aaron Goidel | ac5656d | 2019-08-12 11:20:00 -0400 | [diff] [blame] | 1287 | obj_type = FSNOTIFY_OBJ_TYPE_SB; |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 1288 | break; |
| 1289 | default: |
| 1290 | return -EINVAL; |
| 1291 | } |
| 1292 | |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 1293 | switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) { |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 1294 | case FAN_MARK_ADD: |
Andreas Gruenbacher | 88380fe | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 1295 | case FAN_MARK_REMOVE: |
Lino Sanfilippo | 1734dee | 2010-11-22 18:46:33 +0100 | [diff] [blame] | 1296 | if (!mask) |
| 1297 | return -EINVAL; |
Heinrich Schuchardt | cc299a9 | 2014-06-04 16:05:43 -0700 | [diff] [blame] | 1298 | break; |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 1299 | case FAN_MARK_FLUSH: |
Amir Goldstein | 23c9dee | 2018-10-04 00:25:35 +0300 | [diff] [blame] | 1300 | if (flags & ~(FANOTIFY_MARK_TYPE_BITS | FAN_MARK_FLUSH)) |
Heinrich Schuchardt | cc299a9 | 2014-06-04 16:05:43 -0700 | [diff] [blame] | 1301 | return -EINVAL; |
Andreas Gruenbacher | 88380fe | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 1302 | break; |
| 1303 | default: |
| 1304 | return -EINVAL; |
| 1305 | } |
Eric Paris | 8fcd652 | 2010-10-28 17:21:59 -0400 | [diff] [blame] | 1306 | |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 1307 | if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) |
Amir Goldstein | 23c9dee | 2018-10-04 00:25:35 +0300 | [diff] [blame] | 1308 | valid_mask |= FANOTIFY_PERM_EVENTS; |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 1309 | |
| 1310 | if (mask & ~valid_mask) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1311 | return -EINVAL; |
| 1312 | |
Amir Goldstein | 3ef8665 | 2020-07-16 11:42:13 +0300 | [diff] [blame] | 1313 | /* Event flags (ONDIR, ON_CHILD) are meaningless in ignored mask */ |
| 1314 | if (ignored) |
| 1315 | mask &= ~FANOTIFY_EVENT_FLAGS; |
| 1316 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1317 | f = fdget(fanotify_fd); |
| 1318 | if (unlikely(!f.file)) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1319 | return -EBADF; |
| 1320 | |
| 1321 | /* verify that this is indeed an fanotify instance */ |
| 1322 | ret = -EINVAL; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1323 | if (unlikely(f.file->f_op != &fanotify_fops)) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1324 | goto fput_and_out; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1325 | group = f.file->private_data; |
Eric Paris | 4231a23 | 2010-10-28 17:21:56 -0400 | [diff] [blame] | 1326 | |
| 1327 | /* |
Amir Goldstein | a8b98c8 | 2021-05-24 16:53:21 +0300 | [diff] [blame] | 1328 | * An unprivileged user is not allowed to setup mount nor filesystem |
| 1329 | * marks. This also includes setting up such marks by a group that |
| 1330 | * was initialized by an unprivileged user. |
Amir Goldstein | 7cea2a3 | 2021-03-04 13:29:21 +0200 | [diff] [blame] | 1331 | */ |
| 1332 | ret = -EPERM; |
Amir Goldstein | a8b98c8 | 2021-05-24 16:53:21 +0300 | [diff] [blame] | 1333 | if ((!capable(CAP_SYS_ADMIN) || |
| 1334 | FAN_GROUP_FLAG(group, FANOTIFY_UNPRIV)) && |
Amir Goldstein | 7cea2a3 | 2021-03-04 13:29:21 +0200 | [diff] [blame] | 1335 | mark_type != FAN_MARK_INODE) |
| 1336 | goto fput_and_out; |
| 1337 | |
| 1338 | /* |
Eric Paris | 4231a23 | 2010-10-28 17:21:56 -0400 | [diff] [blame] | 1339 | * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not |
| 1340 | * allowed to set permissions events. |
| 1341 | */ |
| 1342 | ret = -EINVAL; |
Amir Goldstein | 23c9dee | 2018-10-04 00:25:35 +0300 | [diff] [blame] | 1343 | if (mask & FANOTIFY_PERM_EVENTS && |
Eric Paris | 4231a23 | 2010-10-28 17:21:56 -0400 | [diff] [blame] | 1344 | group->priority == FS_PRIO_0) |
| 1345 | goto fput_and_out; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1346 | |
Amir Goldstein | 235328d | 2019-01-10 19:04:43 +0200 | [diff] [blame] | 1347 | /* |
| 1348 | * Events with data type inode do not carry enough information to report |
| 1349 | * event->fd, so we do not allow setting a mask for inode events unless |
| 1350 | * group supports reporting fid. |
| 1351 | * inode events are not supported on a mount mark, because they do not |
| 1352 | * carry enough information (i.e. path) to be filtered by mount point. |
| 1353 | */ |
Amir Goldstein | d809daf | 2020-07-16 11:42:12 +0300 | [diff] [blame] | 1354 | fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS); |
Amir Goldstein | 235328d | 2019-01-10 19:04:43 +0200 | [diff] [blame] | 1355 | if (mask & FANOTIFY_INODE_EVENTS && |
Amir Goldstein | d809daf | 2020-07-16 11:42:12 +0300 | [diff] [blame] | 1356 | (!fid_mode || mark_type == FAN_MARK_MOUNT)) |
Amir Goldstein | 235328d | 2019-01-10 19:04:43 +0200 | [diff] [blame] | 1357 | goto fput_and_out; |
| 1358 | |
Heinrich Schuchardt | 0a8dd2d | 2014-06-04 16:05:40 -0700 | [diff] [blame] | 1359 | if (flags & FAN_MARK_FLUSH) { |
| 1360 | ret = 0; |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 1361 | if (mark_type == FAN_MARK_MOUNT) |
Heinrich Schuchardt | 0a8dd2d | 2014-06-04 16:05:40 -0700 | [diff] [blame] | 1362 | fsnotify_clear_vfsmount_marks_by_group(group); |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 1363 | else if (mark_type == FAN_MARK_FILESYSTEM) |
| 1364 | fsnotify_clear_sb_marks_by_group(group); |
Heinrich Schuchardt | 0a8dd2d | 2014-06-04 16:05:40 -0700 | [diff] [blame] | 1365 | else |
| 1366 | fsnotify_clear_inode_marks_by_group(group); |
| 1367 | goto fput_and_out; |
| 1368 | } |
| 1369 | |
Aaron Goidel | ac5656d | 2019-08-12 11:20:00 -0400 | [diff] [blame] | 1370 | ret = fanotify_find_path(dfd, pathname, &path, flags, |
| 1371 | (mask & ALL_FSNOTIFY_EVENTS), obj_type); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1372 | if (ret) |
| 1373 | goto fput_and_out; |
| 1374 | |
Jan Kara | 0b3b094 | 2019-05-15 16:28:34 +0200 | [diff] [blame] | 1375 | if (flags & FAN_MARK_ADD) { |
| 1376 | ret = fanotify_events_supported(&path, mask); |
| 1377 | if (ret) |
| 1378 | goto path_put_and_out; |
| 1379 | } |
| 1380 | |
Amir Goldstein | d809daf | 2020-07-16 11:42:12 +0300 | [diff] [blame] | 1381 | if (fid_mode) { |
Amir Goldstein | 7307228 | 2019-01-10 19:04:39 +0200 | [diff] [blame] | 1382 | ret = fanotify_test_fid(&path, &__fsid); |
Amir Goldstein | a8b13aa | 2019-01-10 19:04:36 +0200 | [diff] [blame] | 1383 | if (ret) |
| 1384 | goto path_put_and_out; |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 1385 | |
Amir Goldstein | 7307228 | 2019-01-10 19:04:39 +0200 | [diff] [blame] | 1386 | fsid = &__fsid; |
Amir Goldstein | a8b13aa | 2019-01-10 19:04:36 +0200 | [diff] [blame] | 1387 | } |
| 1388 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1389 | /* inode held in place by reference to path; group by fget on fd */ |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 1390 | if (mark_type == FAN_MARK_INODE) |
Eric Paris | 0ff21db | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 1391 | inode = path.dentry->d_inode; |
| 1392 | else |
| 1393 | mnt = path.mnt; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1394 | |
Amir Goldstein | 85af5d9 | 2020-07-16 11:42:15 +0300 | [diff] [blame] | 1395 | /* Mask out FAN_EVENT_ON_CHILD flag for sb/mount/non-dir marks */ |
| 1396 | if (mnt || !S_ISDIR(inode->i_mode)) { |
| 1397 | mask &= ~FAN_EVENT_ON_CHILD; |
| 1398 | umask = FAN_EVENT_ON_CHILD; |
Amir Goldstein | 5128063 | 2020-07-16 11:42:27 +0300 | [diff] [blame] | 1399 | /* |
| 1400 | * If group needs to report parent fid, register for getting |
| 1401 | * events with parent/name info for non-directory. |
| 1402 | */ |
| 1403 | if ((fid_mode & FAN_REPORT_DIR_FID) && |
| 1404 | (flags & FAN_MARK_ADD) && !ignored) |
| 1405 | mask |= FAN_EVENT_ON_CHILD; |
Amir Goldstein | 85af5d9 | 2020-07-16 11:42:15 +0300 | [diff] [blame] | 1406 | } |
| 1407 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1408 | /* create/update an inode mark */ |
Heinrich Schuchardt | 0a8dd2d | 2014-06-04 16:05:40 -0700 | [diff] [blame] | 1409 | switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) { |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 1410 | case FAN_MARK_ADD: |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 1411 | if (mark_type == FAN_MARK_MOUNT) |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 1412 | ret = fanotify_add_vfsmount_mark(group, mnt, mask, |
| 1413 | flags, fsid); |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 1414 | else if (mark_type == FAN_MARK_FILESYSTEM) |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 1415 | ret = fanotify_add_sb_mark(group, mnt->mnt_sb, mask, |
| 1416 | flags, fsid); |
Eric Paris | 0ff21db | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 1417 | else |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 1418 | ret = fanotify_add_inode_mark(group, inode, mask, |
| 1419 | flags, fsid); |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 1420 | break; |
| 1421 | case FAN_MARK_REMOVE: |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 1422 | if (mark_type == FAN_MARK_MOUNT) |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 1423 | ret = fanotify_remove_vfsmount_mark(group, mnt, mask, |
Amir Goldstein | 85af5d9 | 2020-07-16 11:42:15 +0300 | [diff] [blame] | 1424 | flags, umask); |
Amir Goldstein | d54f4fb | 2018-09-01 10:41:13 +0300 | [diff] [blame] | 1425 | else if (mark_type == FAN_MARK_FILESYSTEM) |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 1426 | ret = fanotify_remove_sb_mark(group, mnt->mnt_sb, mask, |
Amir Goldstein | 85af5d9 | 2020-07-16 11:42:15 +0300 | [diff] [blame] | 1427 | flags, umask); |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 1428 | else |
Amir Goldstein | 7711522 | 2019-01-10 19:04:37 +0200 | [diff] [blame] | 1429 | ret = fanotify_remove_inode_mark(group, inode, mask, |
Amir Goldstein | 85af5d9 | 2020-07-16 11:42:15 +0300 | [diff] [blame] | 1430 | flags, umask); |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 1431 | break; |
| 1432 | default: |
| 1433 | ret = -EINVAL; |
| 1434 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1435 | |
Amir Goldstein | a8b13aa | 2019-01-10 19:04:36 +0200 | [diff] [blame] | 1436 | path_put_and_out: |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1437 | path_put(&path); |
| 1438 | fput_and_out: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1439 | fdput(f); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1440 | return ret; |
Eric Paris | bbaa416 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1441 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1442 | |
Brian Gerst | 2ca408d | 2020-11-30 17:30:59 -0500 | [diff] [blame] | 1443 | #ifndef CONFIG_ARCH_SPLIT_ARG64 |
Dominik Brodowski | 183caa3 | 2018-03-17 15:06:11 +0100 | [diff] [blame] | 1444 | SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags, |
| 1445 | __u64, mask, int, dfd, |
| 1446 | const char __user *, pathname) |
| 1447 | { |
| 1448 | return do_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname); |
| 1449 | } |
Brian Gerst | 2ca408d | 2020-11-30 17:30:59 -0500 | [diff] [blame] | 1450 | #endif |
Dominik Brodowski | 183caa3 | 2018-03-17 15:06:11 +0100 | [diff] [blame] | 1451 | |
Brian Gerst | 2ca408d | 2020-11-30 17:30:59 -0500 | [diff] [blame] | 1452 | #if defined(CONFIG_ARCH_SPLIT_ARG64) || defined(CONFIG_COMPAT) |
| 1453 | SYSCALL32_DEFINE6(fanotify_mark, |
Al Viro | 91c2e0b | 2013-03-05 20:10:59 -0500 | [diff] [blame] | 1454 | int, fanotify_fd, unsigned int, flags, |
Brian Gerst | 2ca408d | 2020-11-30 17:30:59 -0500 | [diff] [blame] | 1455 | SC_ARG64(mask), int, dfd, |
Al Viro | 91c2e0b | 2013-03-05 20:10:59 -0500 | [diff] [blame] | 1456 | const char __user *, pathname) |
| 1457 | { |
Brian Gerst | 2ca408d | 2020-11-30 17:30:59 -0500 | [diff] [blame] | 1458 | return do_fanotify_mark(fanotify_fd, flags, SC_VAL64(__u64, mask), |
| 1459 | dfd, pathname); |
Al Viro | 91c2e0b | 2013-03-05 20:10:59 -0500 | [diff] [blame] | 1460 | } |
| 1461 | #endif |
| 1462 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1463 | /* |
Justin P. Mattock | ae0e47f | 2011-03-01 15:06:02 +0100 | [diff] [blame] | 1464 | * fanotify_user_setup - Our initialization function. Note that we cannot return |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1465 | * error because we have compiled-in VFS hooks. So an (unlikely) failure here |
| 1466 | * must result in panic(). |
| 1467 | */ |
| 1468 | static int __init fanotify_user_setup(void) |
| 1469 | { |
Amir Goldstein | 5b8fea6 | 2021-03-04 13:29:20 +0200 | [diff] [blame] | 1470 | struct sysinfo si; |
| 1471 | int max_marks; |
| 1472 | |
| 1473 | si_meminfo(&si); |
| 1474 | /* |
| 1475 | * Allow up to 1% of addressable memory to be accounted for per user |
| 1476 | * marks limited to the range [8192, 1048576]. mount and sb marks are |
| 1477 | * a lot cheaper than inode marks, but there is no reason for a user |
| 1478 | * to have many of those, so calculate by the cost of inode marks. |
| 1479 | */ |
| 1480 | max_marks = (((si.totalram - si.totalhigh) / 100) << PAGE_SHIFT) / |
| 1481 | INODE_MARK_COST; |
| 1482 | max_marks = clamp(max_marks, FANOTIFY_OLD_DEFAULT_MAX_MARKS, |
| 1483 | FANOTIFY_DEFAULT_MAX_USER_MARKS); |
| 1484 | |
Amir Goldstein | a8b98c8 | 2021-05-24 16:53:21 +0300 | [diff] [blame] | 1485 | BUILD_BUG_ON(FANOTIFY_INIT_FLAGS & FANOTIFY_INTERNAL_GROUP_FLAGS); |
Amir Goldstein | 929943b | 2020-07-16 11:42:28 +0300 | [diff] [blame] | 1486 | BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 10); |
Amir Goldstein | bdd5a46 | 2018-10-04 00:25:37 +0300 | [diff] [blame] | 1487 | BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9); |
| 1488 | |
Shakeel Butt | d46eb14b | 2018-08-17 15:46:39 -0700 | [diff] [blame] | 1489 | fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, |
| 1490 | SLAB_PANIC|SLAB_ACCOUNT); |
Jan Kara | 7088f35 | 2020-03-24 17:04:20 +0100 | [diff] [blame] | 1491 | fanotify_fid_event_cachep = KMEM_CACHE(fanotify_fid_event, |
| 1492 | SLAB_PANIC); |
| 1493 | fanotify_path_event_cachep = KMEM_CACHE(fanotify_path_event, |
| 1494 | SLAB_PANIC); |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 1495 | if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) { |
| 1496 | fanotify_perm_event_cachep = |
Amir Goldstein | 3391399 | 2019-01-10 19:04:32 +0200 | [diff] [blame] | 1497 | KMEM_CACHE(fanotify_perm_event, SLAB_PANIC); |
Miklos Szeredi | 6685df3 | 2017-10-30 21:14:56 +0100 | [diff] [blame] | 1498 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1499 | |
Amir Goldstein | 5b8fea6 | 2021-03-04 13:29:20 +0200 | [diff] [blame] | 1500 | fanotify_max_queued_events = FANOTIFY_DEFAULT_MAX_EVENTS; |
| 1501 | init_user_ns.ucount_max[UCOUNT_FANOTIFY_GROUPS] = |
| 1502 | FANOTIFY_DEFAULT_MAX_GROUPS; |
| 1503 | init_user_ns.ucount_max[UCOUNT_FANOTIFY_MARKS] = max_marks; |
| 1504 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 1505 | return 0; |
| 1506 | } |
| 1507 | device_initcall(fanotify_user_setup); |