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