blob: 73b1615f9d962d9adf56b061c5d37c08e4ad155d [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Andreas Gruenbacher33d3dff2009-12-17 21:24:29 -05002#include <linux/fanotify.h>
Eric Paris11637e42009-12-17 21:24:25 -05003#include <linux/fcntl.h>
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +10004#include <linux/fdtable.h>
Eric Paris2a3edf82009-12-17 21:24:26 -05005#include <linux/file.h>
Eric Paris11637e42009-12-17 21:24:25 -05006#include <linux/fs.h>
Eric Paris52c923d2009-12-17 21:24:26 -05007#include <linux/anon_inodes.h>
Eric Paris11637e42009-12-17 21:24:25 -05008#include <linux/fsnotify_backend.h>
Eric Paris2a3edf82009-12-17 21:24:26 -05009#include <linux/init.h>
Eric Parisa1014f12009-12-17 21:24:26 -050010#include <linux/mount.h>
Eric Paris2a3edf82009-12-17 21:24:26 -050011#include <linux/namei.h>
Eric Parisa1014f12009-12-17 21:24:26 -050012#include <linux/poll.h>
Eric Paris11637e42009-12-17 21:24:25 -050013#include <linux/security.h>
14#include <linux/syscalls.h>
Tejun Heoe4e047a2010-05-20 01:36:28 +100015#include <linux/slab.h>
Eric Paris2a3edf82009-12-17 21:24:26 -050016#include <linux/types.h>
Eric Parisa1014f12009-12-17 21:24:26 -050017#include <linux/uaccess.h>
Al Viro91c2e0b2013-03-05 20:10:59 -050018#include <linux/compat.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010019#include <linux/sched/signal.h>
Shakeel Buttd46eb14b2018-08-17 15:46:39 -070020#include <linux/memcontrol.h>
Amir Goldsteina8b13aa2019-01-10 19:04:36 +020021#include <linux/statfs.h>
22#include <linux/exportfs.h>
Eric Parisa1014f12009-12-17 21:24:26 -050023
24#include <asm/ioctls.h>
Eric Paris11637e42009-12-17 21:24:25 -050025
Al Viroc63181e2011-11-25 02:35:16 -050026#include "../../mount.h"
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080027#include "../fdinfo.h"
Jan Kara7053aee2014-01-21 15:48:14 -080028#include "fanotify.h"
Al Viroc63181e2011-11-25 02:35:16 -050029
Eric Paris2529a0d2010-10-28 17:21:57 -040030#define FANOTIFY_DEFAULT_MAX_EVENTS 16384
Amir Goldstein5b8fea62021-03-04 13:29:20 +020031#define FANOTIFY_OLD_DEFAULT_MAX_MARKS 8192
32#define FANOTIFY_DEFAULT_MAX_GROUPS 128
Gabriel Krisman Bertazi734a1a52021-10-25 16:27:34 -030033#define FANOTIFY_DEFAULT_FEE_POOL_SIZE 32
Amir Goldstein5b8fea62021-03-04 13:29:20 +020034
35/*
36 * Legacy fanotify marks limits (8192) is per group and we introduced a tunable
37 * limit of marks per user, similar to inotify. Effectively, the legacy limit
38 * of fanotify marks per user is <max marks per group> * <max groups per user>.
39 * This default limit (1M) also happens to match the increased limit of inotify
40 * max_user_watches since v5.10.
41 */
42#define FANOTIFY_DEFAULT_MAX_USER_MARKS \
43 (FANOTIFY_OLD_DEFAULT_MAX_MARKS * FANOTIFY_DEFAULT_MAX_GROUPS)
44
45/*
46 * Most of the memory cost of adding an inode mark is pinning the marked inode.
47 * The size of the filesystem inode struct is not uniform across filesystems,
48 * so double the size of a VFS inode is used as a conservative approximation.
49 */
50#define INODE_MARK_COST (2 * sizeof(struct inode))
51
52/* configurable via /proc/sys/fs/fanotify/ */
53static int fanotify_max_queued_events __read_mostly;
54
55#ifdef CONFIG_SYSCTL
56
57#include <linux/sysctl.h>
58
Sven Schnellef153c222021-07-30 08:28:54 +020059static long ft_zero = 0;
60static long ft_int_max = INT_MAX;
61
Xiaoming Ni7b9ad122022-01-21 22:11:59 -080062static struct ctl_table fanotify_table[] = {
Amir Goldstein5b8fea62021-03-04 13:29:20 +020063 {
64 .procname = "max_user_groups",
65 .data = &init_user_ns.ucount_max[UCOUNT_FANOTIFY_GROUPS],
Sven Schnellef153c222021-07-30 08:28:54 +020066 .maxlen = sizeof(long),
Amir Goldstein5b8fea62021-03-04 13:29:20 +020067 .mode = 0644,
Sven Schnellef153c222021-07-30 08:28:54 +020068 .proc_handler = proc_doulongvec_minmax,
69 .extra1 = &ft_zero,
70 .extra2 = &ft_int_max,
Amir Goldstein5b8fea62021-03-04 13:29:20 +020071 },
72 {
73 .procname = "max_user_marks",
74 .data = &init_user_ns.ucount_max[UCOUNT_FANOTIFY_MARKS],
Sven Schnellef153c222021-07-30 08:28:54 +020075 .maxlen = sizeof(long),
Amir Goldstein5b8fea62021-03-04 13:29:20 +020076 .mode = 0644,
Sven Schnellef153c222021-07-30 08:28:54 +020077 .proc_handler = proc_doulongvec_minmax,
78 .extra1 = &ft_zero,
79 .extra2 = &ft_int_max,
Amir Goldstein5b8fea62021-03-04 13:29:20 +020080 },
81 {
82 .procname = "max_queued_events",
83 .data = &fanotify_max_queued_events,
84 .maxlen = sizeof(int),
85 .mode = 0644,
86 .proc_handler = proc_dointvec_minmax,
87 .extra1 = SYSCTL_ZERO
88 },
89 { }
90};
Xiaoming Ni7b9ad122022-01-21 22:11:59 -080091
92static void __init fanotify_sysctls_init(void)
93{
94 register_sysctl("fs/fanotify", fanotify_table);
95}
96#else
97#define fanotify_sysctls_init() do { } while (0)
Amir Goldstein5b8fea62021-03-04 13:29:20 +020098#endif /* CONFIG_SYSCTL */
Eric Paris2529a0d2010-10-28 17:21:57 -040099
Heinrich Schuchardt48149e92014-06-04 16:05:44 -0700100/*
101 * All flags that may be specified in parameter event_f_flags of fanotify_init.
102 *
103 * Internal and external open flags are stored together in field f_flags of
104 * struct file. Only external open flags shall be allowed in event_f_flags.
105 * Internal flags like FMODE_NONOTIFY, FMODE_EXEC, FMODE_NOCMTIME shall be
106 * excluded.
107 */
108#define FANOTIFY_INIT_ALL_EVENT_F_BITS ( \
109 O_ACCMODE | O_APPEND | O_NONBLOCK | \
110 __O_SYNC | O_DSYNC | O_CLOEXEC | \
111 O_LARGEFILE | O_NOATIME )
112
Andreas Gruenbacher33d3dff2009-12-17 21:24:29 -0500113extern const struct fsnotify_ops fanotify_fsnotify_ops;
Eric Paris11637e42009-12-17 21:24:25 -0500114
Jan Kara054c6362016-12-21 18:06:12 +0100115struct kmem_cache *fanotify_mark_cache __read_mostly;
Jan Kara7088f352020-03-24 17:04:20 +0100116struct kmem_cache *fanotify_fid_event_cachep __read_mostly;
117struct kmem_cache *fanotify_path_event_cachep __read_mostly;
Jan Karaf0834412014-04-03 14:46:33 -0700118struct kmem_cache *fanotify_perm_event_cachep __read_mostly;
Eric Paris2a3edf82009-12-17 21:24:26 -0500119
Amir Goldstein5e469c82019-01-10 19:04:35 +0200120#define FANOTIFY_EVENT_ALIGN 4
Matthew Bobrowskid3424c92021-08-08 15:25:32 +1000121#define FANOTIFY_FID_INFO_HDR_LEN \
Amir Goldstein44d705b2020-03-19 17:10:22 +0200122 (sizeof(struct fanotify_event_info_fid) + sizeof(struct file_handle))
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000123#define FANOTIFY_PIDFD_INFO_HDR_LEN \
124 sizeof(struct fanotify_event_info_pidfd)
Gabriel Krisman Bertazi130a3c72021-10-25 16:27:42 -0300125#define FANOTIFY_ERROR_INFO_LEN \
126 (sizeof(struct fanotify_event_info_error))
Amir Goldstein5e469c82019-01-10 19:04:35 +0200127
Amir Goldstein44d705b2020-03-19 17:10:22 +0200128static int fanotify_fid_info_len(int fh_len, int name_len)
Amir Goldsteind766b552020-03-19 17:10:20 +0200129{
Amir Goldstein44d705b2020-03-19 17:10:22 +0200130 int info_len = fh_len;
131
132 if (name_len)
133 info_len += name_len + 1;
134
Matthew Bobrowskid3424c92021-08-08 15:25:32 +1000135 return roundup(FANOTIFY_FID_INFO_HDR_LEN + info_len,
136 FANOTIFY_EVENT_ALIGN);
Amir Goldsteind766b552020-03-19 17:10:20 +0200137}
138
Amir Goldstein7326e3822021-11-29 22:15:36 +0200139/* FAN_RENAME may have one or two dir+name info records */
140static int fanotify_dir_name_info_len(struct fanotify_event *event)
141{
142 struct fanotify_info *info = fanotify_event_info(event);
143 int dir_fh_len = fanotify_event_dir_fh_len(event);
144 int dir2_fh_len = fanotify_event_dir2_fh_len(event);
145 int info_len = 0;
146
147 if (dir_fh_len)
148 info_len += fanotify_fid_info_len(dir_fh_len,
149 info->name_len);
150 if (dir2_fh_len)
151 info_len += fanotify_fid_info_len(dir2_fh_len,
152 info->name2_len);
153
154 return info_len;
155}
156
Gabriel Krisman Bertazib9928e82021-10-25 16:27:20 -0300157static size_t fanotify_event_len(unsigned int info_mode,
158 struct fanotify_event *event)
Amir Goldstein5e469c82019-01-10 19:04:35 +0200159{
Gabriel Krisman Bertazib9928e82021-10-25 16:27:20 -0300160 size_t event_len = FAN_EVENT_METADATA_LEN;
161 struct fanotify_info *info;
Gabriel Krisman Bertazib9928e82021-10-25 16:27:20 -0300162 int fh_len;
Amir Goldstein929943b2020-07-16 11:42:28 +0300163 int dot_len = 0;
Amir Goldsteinf454fa62020-07-16 11:42:17 +0300164
Gabriel Krisman Bertazib9928e82021-10-25 16:27:20 -0300165 if (!info_mode)
166 return event_len;
167
Gabriel Krisman Bertazi130a3c72021-10-25 16:27:42 -0300168 if (fanotify_is_error_event(event->mask))
169 event_len += FANOTIFY_ERROR_INFO_LEN;
170
Gabriel Krisman Bertazib9928e82021-10-25 16:27:20 -0300171 info = fanotify_event_info(event);
Gabriel Krisman Bertazib9928e82021-10-25 16:27:20 -0300172
Amir Goldstein7326e3822021-11-29 22:15:36 +0200173 if (fanotify_event_has_any_dir_fh(event)) {
174 event_len += fanotify_dir_name_info_len(event);
Matthew Bobrowskid3424c92021-08-08 15:25:32 +1000175 } else if ((info_mode & FAN_REPORT_NAME) &&
176 (event->mask & FAN_ONDIR)) {
Amir Goldstein929943b2020-07-16 11:42:28 +0300177 /*
178 * With group flag FAN_REPORT_NAME, if name was not recorded in
179 * event on a directory, we will report the name ".".
180 */
181 dot_len = 1;
182 }
Jan Karaafc894c2020-03-24 16:55:37 +0100183
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000184 if (info_mode & FAN_REPORT_PIDFD)
Gabriel Krisman Bertazib9928e82021-10-25 16:27:20 -0300185 event_len += FANOTIFY_PIDFD_INFO_HDR_LEN;
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000186
Gabriel Krisman Bertazi4bd5a5c2021-10-25 16:27:38 -0300187 if (fanotify_event_has_object_fh(event)) {
188 fh_len = fanotify_event_object_fh_len(event);
Gabriel Krisman Bertazib9928e82021-10-25 16:27:20 -0300189 event_len += fanotify_fid_info_len(fh_len, dot_len);
Gabriel Krisman Bertazi4bd5a5c2021-10-25 16:27:38 -0300190 }
Amir Goldstein5e469c82019-01-10 19:04:35 +0200191
Gabriel Krisman Bertazib9928e82021-10-25 16:27:20 -0300192 return event_len;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200193}
194
Eric Parisa1014f12009-12-17 21:24:26 -0500195/*
Amir Goldstein94e00d22021-03-04 12:48:25 +0200196 * Remove an hashed event from merge hash table.
197 */
198static void fanotify_unhash_event(struct fsnotify_group *group,
199 struct fanotify_event *event)
200{
201 assert_spin_locked(&group->notification_lock);
202
203 pr_debug("%s: group=%p event=%p bucket=%u\n", __func__,
204 group, event, fanotify_event_hash_bucket(group, event));
205
206 if (WARN_ON_ONCE(hlist_unhashed(&event->merge_list)))
207 return;
208
209 hlist_del_init(&event->merge_list);
210}
211
212/*
Jan Kara7088f352020-03-24 17:04:20 +0100213 * Get an fanotify notification event if one exists and is small
Eric Parisa1014f12009-12-17 21:24:26 -0500214 * enough to fit in "count". Return an error pointer if the count
Jan Kara40873282019-01-08 14:02:44 +0100215 * is not large enough. When permission event is dequeued, its state is
216 * updated accordingly.
Eric Parisa1014f12009-12-17 21:24:26 -0500217 */
Jan Kara7088f352020-03-24 17:04:20 +0100218static struct fanotify_event *get_one_event(struct fsnotify_group *group,
Eric Parisa1014f12009-12-17 21:24:26 -0500219 size_t count)
220{
Gabriel Krisman Bertazib9928e82021-10-25 16:27:20 -0300221 size_t event_size;
Jan Kara7088f352020-03-24 17:04:20 +0100222 struct fanotify_event *event = NULL;
Amir Goldstein6f731712021-03-04 12:48:22 +0200223 struct fsnotify_event *fsn_event;
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000224 unsigned int info_mode = FAN_GROUP_FLAG(group, FANOTIFY_INFO_MODES);
Eric Parisa1014f12009-12-17 21:24:26 -0500225
226 pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
227
Jan Kara8c554462019-01-08 13:52:31 +0100228 spin_lock(&group->notification_lock);
Amir Goldstein6f731712021-03-04 12:48:22 +0200229 fsn_event = fsnotify_peek_first_event(group);
230 if (!fsn_event)
Jan Kara8c554462019-01-08 13:52:31 +0100231 goto out;
Eric Parisa1014f12009-12-17 21:24:26 -0500232
Amir Goldstein6f731712021-03-04 12:48:22 +0200233 event = FANOTIFY_E(fsn_event);
Gabriel Krisman Bertazib9928e82021-10-25 16:27:20 -0300234 event_size = fanotify_event_len(info_mode, event);
Amir Goldstein5e469c82019-01-10 19:04:35 +0200235
Jan Kara8c554462019-01-08 13:52:31 +0100236 if (event_size > count) {
Jan Kara7088f352020-03-24 17:04:20 +0100237 event = ERR_PTR(-EINVAL);
Jan Kara8c554462019-01-08 13:52:31 +0100238 goto out;
239 }
Amir Goldstein6f731712021-03-04 12:48:22 +0200240
241 /*
242 * Held the notification_lock the whole time, so this is the
243 * same event we peeked above.
244 */
245 fsnotify_remove_first_event(group);
Jan Kara7088f352020-03-24 17:04:20 +0100246 if (fanotify_is_perm_event(event->mask))
247 FANOTIFY_PERM(event)->state = FAN_EVENT_REPORTED;
Amir Goldstein94e00d22021-03-04 12:48:25 +0200248 if (fanotify_is_hashed_event(event->mask))
249 fanotify_unhash_event(group, event);
Jan Kara8c554462019-01-08 13:52:31 +0100250out:
251 spin_unlock(&group->notification_lock);
Jan Kara7088f352020-03-24 17:04:20 +0100252 return event;
Eric Parisa1014f12009-12-17 21:24:26 -0500253}
254
Jan Karaa741c2f2020-03-24 15:27:52 +0100255static int create_fd(struct fsnotify_group *group, struct path *path,
Jan Kara7053aee2014-01-21 15:48:14 -0800256 struct file **file)
Eric Parisa1014f12009-12-17 21:24:26 -0500257{
258 int client_fd;
Eric Parisa1014f12009-12-17 21:24:26 -0500259 struct file *new_file;
260
Yann Droneaud0b37e092014-10-09 15:24:40 -0700261 client_fd = get_unused_fd_flags(group->fanotify_data.f_flags);
Eric Parisa1014f12009-12-17 21:24:26 -0500262 if (client_fd < 0)
263 return client_fd;
264
Eric Parisa1014f12009-12-17 21:24:26 -0500265 /*
266 * we need a new file handle for the userspace program so it can read even if it was
267 * originally opened O_WRONLY.
268 */
Jan Karaa741c2f2020-03-24 15:27:52 +0100269 new_file = dentry_open(path,
270 group->fanotify_data.f_flags | FMODE_NONOTIFY,
271 current_cred());
Eric Parisa1014f12009-12-17 21:24:26 -0500272 if (IS_ERR(new_file)) {
273 /*
274 * we still send an event even if we can't open the file. this
275 * can happen when say tasks are gone and we try to open their
276 * /proc files or we try to open a WRONLY file like in sysfs
277 * we just send the errno to userspace since there isn't much
278 * else we can do.
279 */
280 put_unused_fd(client_fd);
281 client_fd = PTR_ERR(new_file);
282 } else {
Al Viro352e3b22012-08-19 12:30:45 -0400283 *file = new_file;
Eric Parisa1014f12009-12-17 21:24:26 -0500284 }
285
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -0500286 return client_fd;
Eric Parisa1014f12009-12-17 21:24:26 -0500287}
288
Jan Kara40873282019-01-08 14:02:44 +0100289/*
290 * Finish processing of permission event by setting it to ANSWERED state and
291 * drop group->notification_lock.
292 */
293static void finish_permission_event(struct fsnotify_group *group,
294 struct fanotify_perm_event *event,
295 unsigned int response)
296 __releases(&group->notification_lock)
297{
Jan Karafabf7f22019-01-08 15:18:02 +0100298 bool destroy = false;
299
Jan Kara40873282019-01-08 14:02:44 +0100300 assert_spin_locked(&group->notification_lock);
301 event->response = response;
Jan Karafabf7f22019-01-08 15:18:02 +0100302 if (event->state == FAN_EVENT_CANCELED)
303 destroy = true;
304 else
305 event->state = FAN_EVENT_ANSWERED;
Jan Kara40873282019-01-08 14:02:44 +0100306 spin_unlock(&group->notification_lock);
Jan Karafabf7f22019-01-08 15:18:02 +0100307 if (destroy)
308 fsnotify_destroy_event(group, &event->fae.fse);
Jan Kara40873282019-01-08 14:02:44 +0100309}
310
Eric Parisb2d87902009-12-17 21:24:34 -0500311static int process_access_response(struct fsnotify_group *group,
312 struct fanotify_response *response_struct)
313{
Amir Goldstein33913992019-01-10 19:04:32 +0200314 struct fanotify_perm_event *event;
Jan Karaf0834412014-04-03 14:46:33 -0700315 int fd = response_struct->fd;
316 int response = response_struct->response;
Eric Parisb2d87902009-12-17 21:24:34 -0500317
318 pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
319 fd, response);
320 /*
321 * make sure the response is valid, if invalid we do nothing and either
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300322 * userspace can send a valid response or we will clean it up after the
Eric Parisb2d87902009-12-17 21:24:34 -0500323 * timeout
324 */
Steve Grubbde8cd832017-10-02 20:21:39 -0400325 switch (response & ~FAN_AUDIT) {
Eric Parisb2d87902009-12-17 21:24:34 -0500326 case FAN_ALLOW:
327 case FAN_DENY:
328 break;
329 default:
330 return -EINVAL;
331 }
332
333 if (fd < 0)
334 return -EINVAL;
335
Amir Goldstein96a71f22018-09-21 21:20:30 +0300336 if ((response & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT))
Steve Grubbde8cd832017-10-02 20:21:39 -0400337 return -EINVAL;
338
Jan Karaaf6a5112019-01-08 13:28:18 +0100339 spin_lock(&group->notification_lock);
340 list_for_each_entry(event, &group->fanotify_data.access_list,
341 fae.fse.list) {
342 if (event->fd != fd)
343 continue;
Eric Parisb2d87902009-12-17 21:24:34 -0500344
Jan Karaaf6a5112019-01-08 13:28:18 +0100345 list_del_init(&event->fae.fse.list);
Jan Kara40873282019-01-08 14:02:44 +0100346 finish_permission_event(group, event, response);
Jan Karaaf6a5112019-01-08 13:28:18 +0100347 wake_up(&group->fanotify_data.access_waitq);
348 return 0;
349 }
350 spin_unlock(&group->notification_lock);
Eric Parisb2d87902009-12-17 21:24:34 -0500351
Jan Karaaf6a5112019-01-08 13:28:18 +0100352 return -ENOENT;
Eric Parisb2d87902009-12-17 21:24:34 -0500353}
Eric Parisb2d87902009-12-17 21:24:34 -0500354
Gabriel Krisman Bertazi130a3c72021-10-25 16:27:42 -0300355static size_t copy_error_info_to_user(struct fanotify_event *event,
356 char __user *buf, int count)
357{
Amir Goldstein3cf984e2021-11-29 22:15:33 +0200358 struct fanotify_event_info_error info = { };
Gabriel Krisman Bertazi130a3c72021-10-25 16:27:42 -0300359 struct fanotify_error_event *fee = FANOTIFY_EE(event);
360
361 info.hdr.info_type = FAN_EVENT_INFO_TYPE_ERROR;
Gabriel Krisman Bertazi130a3c72021-10-25 16:27:42 -0300362 info.hdr.len = FANOTIFY_ERROR_INFO_LEN;
363
364 if (WARN_ON(count < info.hdr.len))
365 return -EFAULT;
366
367 info.error = fee->error;
368 info.error_count = fee->err_count;
369
370 if (copy_to_user(buf, &info, sizeof(info)))
371 return -EFAULT;
372
373 return info.hdr.len;
374}
375
Matthew Bobrowskid3424c92021-08-08 15:25:32 +1000376static int copy_fid_info_to_user(__kernel_fsid_t *fsid, struct fanotify_fh *fh,
377 int info_type, const char *name,
378 size_t name_len,
379 char __user *buf, size_t count)
Amir Goldstein5e469c82019-01-10 19:04:35 +0200380{
381 struct fanotify_event_info_fid info = { };
382 struct file_handle handle = { };
Jan Karaafc894c2020-03-24 16:55:37 +0100383 unsigned char bounce[FANOTIFY_INLINE_FH_LEN], *fh_buf;
Amir Goldsteincacfb952020-03-19 17:10:21 +0200384 size_t fh_len = fh ? fh->len : 0;
Amir Goldstein44d705b2020-03-19 17:10:22 +0200385 size_t info_len = fanotify_fid_info_len(fh_len, name_len);
386 size_t len = info_len;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200387
Amir Goldstein44d705b2020-03-19 17:10:22 +0200388 pr_debug("%s: fh_len=%zu name_len=%zu, info_len=%zu, count=%zu\n",
389 __func__, fh_len, name_len, info_len, count);
390
Amir Goldstein44d705b2020-03-19 17:10:22 +0200391 if (WARN_ON_ONCE(len < sizeof(info) || len > count))
Amir Goldstein5e469c82019-01-10 19:04:35 +0200392 return -EFAULT;
393
Amir Goldstein44d705b2020-03-19 17:10:22 +0200394 /*
395 * Copy event info fid header followed by variable sized file handle
396 * and optionally followed by variable sized filename.
397 */
Amir Goldstein83b7a592020-07-16 11:42:26 +0300398 switch (info_type) {
399 case FAN_EVENT_INFO_TYPE_FID:
400 case FAN_EVENT_INFO_TYPE_DFID:
401 if (WARN_ON_ONCE(name_len))
402 return -EFAULT;
403 break;
404 case FAN_EVENT_INFO_TYPE_DFID_NAME:
Amir Goldstein7326e3822021-11-29 22:15:36 +0200405 case FAN_EVENT_INFO_TYPE_OLD_DFID_NAME:
406 case FAN_EVENT_INFO_TYPE_NEW_DFID_NAME:
Amir Goldstein83b7a592020-07-16 11:42:26 +0300407 if (WARN_ON_ONCE(!name || !name_len))
408 return -EFAULT;
409 break;
410 default:
411 return -EFAULT;
412 }
413
414 info.hdr.info_type = info_type;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200415 info.hdr.len = len;
Amir Goldsteind766b552020-03-19 17:10:20 +0200416 info.fsid = *fsid;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200417 if (copy_to_user(buf, &info, sizeof(info)))
418 return -EFAULT;
419
420 buf += sizeof(info);
421 len -= sizeof(info);
Amir Goldstein44d705b2020-03-19 17:10:22 +0200422 if (WARN_ON_ONCE(len < sizeof(handle)))
423 return -EFAULT;
424
Jan Karaafc894c2020-03-24 16:55:37 +0100425 handle.handle_type = fh->type;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200426 handle.handle_bytes = fh_len;
Gabriel Krisman Bertazi936d6a32021-10-25 16:27:41 -0300427
428 /* Mangle handle_type for bad file_handle */
429 if (!fh_len)
430 handle.handle_type = FILEID_INVALID;
431
Amir Goldstein5e469c82019-01-10 19:04:35 +0200432 if (copy_to_user(buf, &handle, sizeof(handle)))
433 return -EFAULT;
434
435 buf += sizeof(handle);
436 len -= sizeof(handle);
Amir Goldstein44d705b2020-03-19 17:10:22 +0200437 if (WARN_ON_ONCE(len < fh_len))
438 return -EFAULT;
439
Jan Karab2d22b62019-03-12 12:42:37 +0100440 /*
Amir Goldstein44d705b2020-03-19 17:10:22 +0200441 * For an inline fh and inline file name, copy through stack to exclude
442 * the copy from usercopy hardening protections.
Jan Karab2d22b62019-03-12 12:42:37 +0100443 */
Jan Karaafc894c2020-03-24 16:55:37 +0100444 fh_buf = fanotify_fh_buf(fh);
Jan Karab2d22b62019-03-12 12:42:37 +0100445 if (fh_len <= FANOTIFY_INLINE_FH_LEN) {
Jan Karaafc894c2020-03-24 16:55:37 +0100446 memcpy(bounce, fh_buf, fh_len);
447 fh_buf = bounce;
Jan Karab2d22b62019-03-12 12:42:37 +0100448 }
Jan Karaafc894c2020-03-24 16:55:37 +0100449 if (copy_to_user(buf, fh_buf, fh_len))
Amir Goldstein5e469c82019-01-10 19:04:35 +0200450 return -EFAULT;
451
Amir Goldstein5e469c82019-01-10 19:04:35 +0200452 buf += fh_len;
453 len -= fh_len;
Amir Goldstein44d705b2020-03-19 17:10:22 +0200454
455 if (name_len) {
456 /* Copy the filename with terminating null */
457 name_len++;
458 if (WARN_ON_ONCE(len < name_len))
459 return -EFAULT;
460
461 if (copy_to_user(buf, name, name_len))
462 return -EFAULT;
463
464 buf += name_len;
465 len -= name_len;
466 }
467
468 /* Pad with 0's */
Amir Goldstein5e469c82019-01-10 19:04:35 +0200469 WARN_ON_ONCE(len < 0 || len >= FANOTIFY_EVENT_ALIGN);
470 if (len > 0 && clear_user(buf, len))
471 return -EFAULT;
472
Amir Goldstein44d705b2020-03-19 17:10:22 +0200473 return info_len;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200474}
475
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000476static int copy_pidfd_info_to_user(int pidfd,
477 char __user *buf,
478 size_t count)
479{
480 struct fanotify_event_info_pidfd info = { };
481 size_t info_len = FANOTIFY_PIDFD_INFO_HDR_LEN;
482
483 if (WARN_ON_ONCE(info_len > count))
484 return -EFAULT;
485
486 info.hdr.info_type = FAN_EVENT_INFO_TYPE_PIDFD;
487 info.hdr.len = info_len;
488 info.pidfd = pidfd;
489
490 if (copy_to_user(buf, &info, info_len))
491 return -EFAULT;
492
493 return info_len;
494}
495
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000496static int copy_info_records_to_user(struct fanotify_event *event,
497 struct fanotify_info *info,
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000498 unsigned int info_mode, int pidfd,
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000499 char __user *buf, size_t count)
500{
501 int ret, total_bytes = 0, info_type = 0;
502 unsigned int fid_mode = info_mode & FANOTIFY_FID_BITS;
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000503 unsigned int pidfd_mode = info_mode & FAN_REPORT_PIDFD;
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000504
505 /*
Amir Goldstein7326e3822021-11-29 22:15:36 +0200506 * Event info records order is as follows:
507 * 1. dir fid + name
508 * 2. (optional) new dir fid + new name
509 * 3. (optional) child fid
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000510 */
Gabriel Krisman Bertazi4bd5a5c2021-10-25 16:27:38 -0300511 if (fanotify_event_has_dir_fh(event)) {
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000512 info_type = info->name_len ? FAN_EVENT_INFO_TYPE_DFID_NAME :
513 FAN_EVENT_INFO_TYPE_DFID;
Amir Goldstein7326e3822021-11-29 22:15:36 +0200514
515 /* FAN_RENAME uses special info types */
516 if (event->mask & FAN_RENAME)
517 info_type = FAN_EVENT_INFO_TYPE_OLD_DFID_NAME;
518
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000519 ret = copy_fid_info_to_user(fanotify_event_fsid(event),
520 fanotify_info_dir_fh(info),
521 info_type,
522 fanotify_info_name(info),
523 info->name_len, buf, count);
524 if (ret < 0)
525 return ret;
526
527 buf += ret;
528 count -= ret;
529 total_bytes += ret;
530 }
531
Amir Goldstein7326e3822021-11-29 22:15:36 +0200532 /* New dir fid+name may be reported in addition to old dir fid+name */
533 if (fanotify_event_has_dir2_fh(event)) {
534 info_type = FAN_EVENT_INFO_TYPE_NEW_DFID_NAME;
535 ret = copy_fid_info_to_user(fanotify_event_fsid(event),
536 fanotify_info_dir2_fh(info),
537 info_type,
538 fanotify_info_name2(info),
539 info->name2_len, buf, count);
540 if (ret < 0)
541 return ret;
542
543 buf += ret;
544 count -= ret;
545 total_bytes += ret;
546 }
547
Gabriel Krisman Bertazi4bd5a5c2021-10-25 16:27:38 -0300548 if (fanotify_event_has_object_fh(event)) {
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000549 const char *dot = NULL;
550 int dot_len = 0;
551
552 if (fid_mode == FAN_REPORT_FID || info_type) {
553 /*
554 * With only group flag FAN_REPORT_FID only type FID is
555 * reported. Second info record type is always FID.
556 */
557 info_type = FAN_EVENT_INFO_TYPE_FID;
558 } else if ((fid_mode & FAN_REPORT_NAME) &&
559 (event->mask & FAN_ONDIR)) {
560 /*
561 * With group flag FAN_REPORT_NAME, if name was not
562 * recorded in an event on a directory, report the name
563 * "." with info type DFID_NAME.
564 */
565 info_type = FAN_EVENT_INFO_TYPE_DFID_NAME;
566 dot = ".";
567 dot_len = 1;
568 } else if ((event->mask & ALL_FSNOTIFY_DIRENT_EVENTS) ||
569 (event->mask & FAN_ONDIR)) {
570 /*
571 * With group flag FAN_REPORT_DIR_FID, a single info
572 * record has type DFID for directory entry modification
573 * event and for event on a directory.
574 */
575 info_type = FAN_EVENT_INFO_TYPE_DFID;
576 } else {
577 /*
578 * With group flags FAN_REPORT_DIR_FID|FAN_REPORT_FID,
579 * a single info record has type FID for event on a
580 * non-directory, when there is no directory to report.
581 * For example, on FAN_DELETE_SELF event.
582 */
583 info_type = FAN_EVENT_INFO_TYPE_FID;
584 }
585
586 ret = copy_fid_info_to_user(fanotify_event_fsid(event),
587 fanotify_event_object_fh(event),
588 info_type, dot, dot_len,
589 buf, count);
590 if (ret < 0)
591 return ret;
592
593 buf += ret;
594 count -= ret;
595 total_bytes += ret;
596 }
597
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000598 if (pidfd_mode) {
599 ret = copy_pidfd_info_to_user(pidfd, buf, count);
600 if (ret < 0)
601 return ret;
602
603 buf += ret;
604 count -= ret;
605 total_bytes += ret;
606 }
607
Gabriel Krisman Bertazi130a3c72021-10-25 16:27:42 -0300608 if (fanotify_is_error_event(event->mask)) {
609 ret = copy_error_info_to_user(event, buf, count);
610 if (ret < 0)
611 return ret;
612 buf += ret;
613 count -= ret;
614 total_bytes += ret;
615 }
616
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000617 return total_bytes;
618}
619
Eric Parisa1014f12009-12-17 21:24:26 -0500620static ssize_t copy_event_to_user(struct fsnotify_group *group,
Jan Kara7088f352020-03-24 17:04:20 +0100621 struct fanotify_event *event,
Kees Cook5b03a472018-12-04 15:44:46 -0800622 char __user *buf, size_t count)
Eric Parisa1014f12009-12-17 21:24:26 -0500623{
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200624 struct fanotify_event_metadata metadata;
Jan Kara7088f352020-03-24 17:04:20 +0100625 struct path *path = fanotify_event_path(event);
Amir Goldsteinf454fa62020-07-16 11:42:17 +0300626 struct fanotify_info *info = fanotify_event_info(event);
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000627 unsigned int info_mode = FAN_GROUP_FLAG(group, FANOTIFY_INFO_MODES);
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000628 unsigned int pidfd_mode = info_mode & FAN_REPORT_PIDFD;
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200629 struct file *f = NULL;
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000630 int ret, pidfd = FAN_NOPIDFD, fd = FAN_NOFD;
Eric Parisa1014f12009-12-17 21:24:26 -0500631
Jan Kara7088f352020-03-24 17:04:20 +0100632 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
Eric Parisa1014f12009-12-17 21:24:26 -0500633
Gabriel Krisman Bertazib9928e82021-10-25 16:27:20 -0300634 metadata.event_len = fanotify_event_len(info_mode, event);
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200635 metadata.metadata_len = FAN_EVENT_METADATA_LEN;
636 metadata.vers = FANOTIFY_METADATA_VERSION;
637 metadata.reserved = 0;
638 metadata.mask = event->mask & FANOTIFY_OUTGOING_EVENTS;
639 metadata.pid = pid_vnr(event->pid);
Amir Goldstein7cea2a32021-03-04 13:29:21 +0200640 /*
641 * For an unprivileged listener, event->pid can be used to identify the
642 * events generated by the listener process itself, without disclosing
643 * the pids of other processes.
644 */
Amir Goldsteina8b98c82021-05-24 16:53:21 +0300645 if (FAN_GROUP_FLAG(group, FANOTIFY_UNPRIV) &&
Amir Goldstein7cea2a32021-03-04 13:29:21 +0200646 task_tgid(current) != event->pid)
647 metadata.pid = 0;
Eric Parisa1014f12009-12-17 21:24:26 -0500648
Amir Goldsteina8b98c82021-05-24 16:53:21 +0300649 /*
650 * For now, fid mode is required for an unprivileged listener and
651 * fid mode does not report fd in events. Keep this check anyway
652 * for safety in case fid mode requirement is relaxed in the future
653 * to allow unprivileged listener to get events with no fd and no fid.
654 */
655 if (!FAN_GROUP_FLAG(group, FANOTIFY_UNPRIV) &&
656 path && path->mnt && path->dentry) {
Jan Karaafc894c2020-03-24 16:55:37 +0100657 fd = create_fd(group, path, &f);
658 if (fd < 0)
659 return fd;
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200660 }
661 metadata.fd = fd;
662
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000663 if (pidfd_mode) {
664 /*
665 * Complain if the FAN_REPORT_PIDFD and FAN_REPORT_TID mutual
666 * exclusion is ever lifted. At the time of incoporating pidfd
667 * support within fanotify, the pidfd API only supported the
668 * creation of pidfds for thread-group leaders.
669 */
670 WARN_ON_ONCE(FAN_GROUP_FLAG(group, FAN_REPORT_TID));
671
672 /*
673 * The PIDTYPE_TGID check for an event->pid is performed
674 * preemptively in an attempt to catch out cases where the event
675 * listener reads events after the event generating process has
676 * already terminated. Report FAN_NOPIDFD to the event listener
677 * in those cases, with all other pidfd creation errors being
678 * reported as FAN_EPIDFD.
679 */
680 if (metadata.pid == 0 ||
681 !pid_has_task(event->pid, PIDTYPE_TGID)) {
682 pidfd = FAN_NOPIDFD;
683 } else {
684 pidfd = pidfd_create(event->pid, 0);
685 if (pidfd < 0)
686 pidfd = FAN_EPIDFD;
687 }
688 }
689
Al Viro352e3b22012-08-19 12:30:45 -0400690 ret = -EFAULT;
Kees Cook5b03a472018-12-04 15:44:46 -0800691 /*
692 * Sanity check copy size in case get_one_event() and
Fabian Frederickc5e443c2020-05-12 20:18:36 +0200693 * event_len sizes ever get out of sync.
Kees Cook5b03a472018-12-04 15:44:46 -0800694 */
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200695 if (WARN_ON_ONCE(metadata.event_len > count))
Al Viro352e3b22012-08-19 12:30:45 -0400696 goto out_close_fd;
697
Amir Goldstein5e469c82019-01-10 19:04:35 +0200698 if (copy_to_user(buf, &metadata, FAN_EVENT_METADATA_LEN))
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200699 goto out_close_fd;
700
Amir Goldstein44d705b2020-03-19 17:10:22 +0200701 buf += FAN_EVENT_METADATA_LEN;
702 count -= FAN_EVENT_METADATA_LEN;
703
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200704 if (fanotify_is_perm_event(event->mask))
Jan Kara7088f352020-03-24 17:04:20 +0100705 FANOTIFY_PERM(event)->fd = fd;
Eric Parisb2d87902009-12-17 21:24:34 -0500706
Amir Goldstein44d705b2020-03-19 17:10:22 +0200707 if (f)
Al Viro3587b1b2012-11-18 19:19:00 +0000708 fd_install(fd, f);
Amir Goldstein44d705b2020-03-19 17:10:22 +0200709
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000710 if (info_mode) {
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000711 ret = copy_info_records_to_user(event, info, info_mode, pidfd,
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000712 buf, count);
Amir Goldstein5e469c82019-01-10 19:04:35 +0200713 if (ret < 0)
Matthew Bobrowskif644bc42021-06-11 13:32:06 +1000714 goto out_close_fd;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200715 }
716
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200717 return metadata.event_len;
Eric Parisb2d87902009-12-17 21:24:34 -0500718
Eric Parisb2d87902009-12-17 21:24:34 -0500719out_close_fd:
Al Viro352e3b22012-08-19 12:30:45 -0400720 if (fd != FAN_NOFD) {
721 put_unused_fd(fd);
722 fput(f);
723 }
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000724
725 if (pidfd >= 0)
726 close_fd(pidfd);
727
Eric Parisb2d87902009-12-17 21:24:34 -0500728 return ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500729}
730
731/* intofiy userspace file descriptor functions */
Al Viro076ccb72017-07-03 01:02:18 -0400732static __poll_t fanotify_poll(struct file *file, poll_table *wait)
Eric Parisa1014f12009-12-17 21:24:26 -0500733{
734 struct fsnotify_group *group = file->private_data;
Al Viro076ccb72017-07-03 01:02:18 -0400735 __poll_t ret = 0;
Eric Parisa1014f12009-12-17 21:24:26 -0500736
737 poll_wait(file, &group->notification_waitq, wait);
Jan Karac21dbe22016-10-07 16:56:52 -0700738 spin_lock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500739 if (!fsnotify_notify_queue_is_empty(group))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800740 ret = EPOLLIN | EPOLLRDNORM;
Jan Karac21dbe22016-10-07 16:56:52 -0700741 spin_unlock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500742
743 return ret;
744}
745
746static ssize_t fanotify_read(struct file *file, char __user *buf,
747 size_t count, loff_t *pos)
748{
749 struct fsnotify_group *group;
Jan Kara7088f352020-03-24 17:04:20 +0100750 struct fanotify_event *event;
Eric Parisa1014f12009-12-17 21:24:26 -0500751 char __user *start;
752 int ret;
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100753 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Eric Parisa1014f12009-12-17 21:24:26 -0500754
755 start = buf;
756 group = file->private_data;
757
758 pr_debug("%s: group=%p\n", __func__, group);
759
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100760 add_wait_queue(&group->notification_waitq, &wait);
Eric Parisa1014f12009-12-17 21:24:26 -0500761 while (1) {
Jan Kara47aaabd2020-07-15 14:06:21 +0200762 /*
763 * User can supply arbitrarily large buffer. Avoid softlockups
764 * in case there are lots of available events.
765 */
766 cond_resched();
Jan Kara7088f352020-03-24 17:04:20 +0100767 event = get_one_event(group, count);
768 if (IS_ERR(event)) {
769 ret = PTR_ERR(event);
Jan Karad8aaab42014-04-03 14:46:35 -0700770 break;
771 }
772
Jan Kara7088f352020-03-24 17:04:20 +0100773 if (!event) {
Jan Karad8aaab42014-04-03 14:46:35 -0700774 ret = -EAGAIN;
775 if (file->f_flags & O_NONBLOCK)
Eric Parisa1014f12009-12-17 21:24:26 -0500776 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700777
778 ret = -ERESTARTSYS;
779 if (signal_pending(current))
Eric Parisa1014f12009-12-17 21:24:26 -0500780 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700781
782 if (start != buf)
783 break;
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100784
785 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Eric Parisa1014f12009-12-17 21:24:26 -0500786 continue;
787 }
788
Jan Kara7088f352020-03-24 17:04:20 +0100789 ret = copy_event_to_user(group, event, buf, count);
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300790 if (unlikely(ret == -EOPENSTALE)) {
791 /*
792 * We cannot report events with stale fd so drop it.
793 * Setting ret to 0 will continue the event loop and
794 * do the right thing if there are no more events to
795 * read (i.e. return bytes read, -EAGAIN or wait).
796 */
797 ret = 0;
798 }
799
Jan Karad8aaab42014-04-03 14:46:35 -0700800 /*
801 * Permission events get queued to wait for response. Other
802 * events can be destroyed now.
803 */
Jan Kara7088f352020-03-24 17:04:20 +0100804 if (!fanotify_is_perm_event(event->mask)) {
805 fsnotify_destroy_event(group, &event->fse);
Jan Karad5078162014-04-03 14:46:36 -0700806 } else {
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300807 if (ret <= 0) {
Jan Kara40873282019-01-08 14:02:44 +0100808 spin_lock(&group->notification_lock);
809 finish_permission_event(group,
Jan Kara7088f352020-03-24 17:04:20 +0100810 FANOTIFY_PERM(event), FAN_DENY);
Jan Karad5078162014-04-03 14:46:36 -0700811 wake_up(&group->fanotify_data.access_waitq);
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300812 } else {
813 spin_lock(&group->notification_lock);
Jan Kara7088f352020-03-24 17:04:20 +0100814 list_add_tail(&event->fse.list,
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300815 &group->fanotify_data.access_list);
816 spin_unlock(&group->notification_lock);
Jan Karad5078162014-04-03 14:46:36 -0700817 }
Jan Karad5078162014-04-03 14:46:36 -0700818 }
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300819 if (ret < 0)
820 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700821 buf += ret;
822 count -= ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500823 }
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100824 remove_wait_queue(&group->notification_waitq, &wait);
Eric Parisa1014f12009-12-17 21:24:26 -0500825
Eric Parisa1014f12009-12-17 21:24:26 -0500826 if (start != buf && ret != -EFAULT)
827 ret = buf - start;
828 return ret;
829}
830
Eric Parisb2d87902009-12-17 21:24:34 -0500831static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
832{
Eric Parisb2d87902009-12-17 21:24:34 -0500833 struct fanotify_response response = { .fd = -1, .response = -1 };
834 struct fsnotify_group *group;
835 int ret;
836
Miklos Szeredi6685df32017-10-30 21:14:56 +0100837 if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
838 return -EINVAL;
839
Eric Parisb2d87902009-12-17 21:24:34 -0500840 group = file->private_data;
841
Fabian Frederick5e236632020-05-12 20:19:21 +0200842 if (count < sizeof(response))
843 return -EINVAL;
844
845 count = sizeof(response);
Eric Parisb2d87902009-12-17 21:24:34 -0500846
847 pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
848
849 if (copy_from_user(&response, buf, count))
850 return -EFAULT;
851
852 ret = process_access_response(group, &response);
853 if (ret < 0)
854 count = ret;
855
856 return count;
Eric Parisb2d87902009-12-17 21:24:34 -0500857}
858
Eric Paris52c923d2009-12-17 21:24:26 -0500859static int fanotify_release(struct inode *ignored, struct file *file)
860{
861 struct fsnotify_group *group = file->private_data;
Amir Goldstein6f731712021-03-04 12:48:22 +0200862 struct fsnotify_event *fsn_event;
Andrew Morton19ba54f2010-10-28 17:21:59 -0400863
Jan Kara5838d442014-08-06 16:03:28 -0700864 /*
Jan Kara96d41012016-09-19 14:44:30 -0700865 * Stop new events from arriving in the notification queue. since
866 * userspace cannot use fanotify fd anymore, no event can enter or
867 * leave access_list by now either.
868 */
869 fsnotify_group_stop_queueing(group);
870
871 /*
872 * Process all permission events on access_list and notification queue
873 * and simulate reply from userspace.
Jan Kara5838d442014-08-06 16:03:28 -0700874 */
Jan Kara073f6552016-10-07 16:56:55 -0700875 spin_lock(&group->notification_lock);
Jan Karaca6f8692019-01-09 13:21:01 +0100876 while (!list_empty(&group->fanotify_data.access_list)) {
Jan Kara7088f352020-03-24 17:04:20 +0100877 struct fanotify_perm_event *event;
878
Jan Karaca6f8692019-01-09 13:21:01 +0100879 event = list_first_entry(&group->fanotify_data.access_list,
880 struct fanotify_perm_event, fae.fse.list);
Jan Karaf0834412014-04-03 14:46:33 -0700881 list_del_init(&event->fae.fse.list);
Jan Kara40873282019-01-08 14:02:44 +0100882 finish_permission_event(group, event, FAN_ALLOW);
883 spin_lock(&group->notification_lock);
Eric Paris2eebf582010-08-18 12:25:50 -0400884 }
Eric Paris2eebf582010-08-18 12:25:50 -0400885
Jan Kara5838d442014-08-06 16:03:28 -0700886 /*
Jan Kara96d41012016-09-19 14:44:30 -0700887 * Destroy all non-permission events. For permission events just
888 * dequeue them and set the response. They will be freed once the
889 * response is consumed and fanotify_get_response() returns.
Jan Kara5838d442014-08-06 16:03:28 -0700890 */
Amir Goldstein6f731712021-03-04 12:48:22 +0200891 while ((fsn_event = fsnotify_remove_first_event(group))) {
892 struct fanotify_event *event = FANOTIFY_E(fsn_event);
Jan Kara7088f352020-03-24 17:04:20 +0100893
Jan Kara7088f352020-03-24 17:04:20 +0100894 if (!(event->mask & FANOTIFY_PERM_EVENTS)) {
Jan Karac21dbe22016-10-07 16:56:52 -0700895 spin_unlock(&group->notification_lock);
Amir Goldstein6f731712021-03-04 12:48:22 +0200896 fsnotify_destroy_event(group, fsn_event);
Miklos Szeredi6685df32017-10-30 21:14:56 +0100897 } else {
Jan Kara7088f352020-03-24 17:04:20 +0100898 finish_permission_event(group, FANOTIFY_PERM(event),
Jan Kara40873282019-01-08 14:02:44 +0100899 FAN_ALLOW);
Miklos Szeredi6685df32017-10-30 21:14:56 +0100900 }
Jan Kara40873282019-01-08 14:02:44 +0100901 spin_lock(&group->notification_lock);
Jan Kara96d41012016-09-19 14:44:30 -0700902 }
Jan Karac21dbe22016-10-07 16:56:52 -0700903 spin_unlock(&group->notification_lock);
Jan Kara96d41012016-09-19 14:44:30 -0700904
905 /* Response for all permission events it set, wakeup waiters */
Eric Paris2eebf582010-08-18 12:25:50 -0400906 wake_up(&group->fanotify_data.access_waitq);
Eric Paris0a6b6bd2011-10-14 17:43:39 -0400907
Eric Paris52c923d2009-12-17 21:24:26 -0500908 /* matches the fanotify_init->fsnotify_alloc_group */
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200909 fsnotify_destroy_group(group);
Eric Paris52c923d2009-12-17 21:24:26 -0500910
911 return 0;
912}
913
Eric Parisa1014f12009-12-17 21:24:26 -0500914static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
915{
916 struct fsnotify_group *group;
Jan Kara7053aee2014-01-21 15:48:14 -0800917 struct fsnotify_event *fsn_event;
Eric Parisa1014f12009-12-17 21:24:26 -0500918 void __user *p;
919 int ret = -ENOTTY;
920 size_t send_len = 0;
921
922 group = file->private_data;
923
924 p = (void __user *) arg;
925
926 switch (cmd) {
927 case FIONREAD:
Jan Karac21dbe22016-10-07 16:56:52 -0700928 spin_lock(&group->notification_lock);
Jan Kara7053aee2014-01-21 15:48:14 -0800929 list_for_each_entry(fsn_event, &group->notification_list, list)
Eric Parisa1014f12009-12-17 21:24:26 -0500930 send_len += FAN_EVENT_METADATA_LEN;
Jan Karac21dbe22016-10-07 16:56:52 -0700931 spin_unlock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500932 ret = put_user(send_len, (int __user *) p);
933 break;
934 }
935
936 return ret;
937}
938
Eric Paris52c923d2009-12-17 21:24:26 -0500939static const struct file_operations fanotify_fops = {
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800940 .show_fdinfo = fanotify_show_fdinfo,
Eric Parisa1014f12009-12-17 21:24:26 -0500941 .poll = fanotify_poll,
942 .read = fanotify_read,
Eric Parisb2d87902009-12-17 21:24:34 -0500943 .write = fanotify_write,
Eric Paris52c923d2009-12-17 21:24:26 -0500944 .fasync = NULL,
945 .release = fanotify_release,
Eric Parisa1014f12009-12-17 21:24:26 -0500946 .unlocked_ioctl = fanotify_ioctl,
Arnd Bergmann1832f2d2018-09-11 21:59:08 +0200947 .compat_ioctl = compat_ptr_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200948 .llseek = noop_llseek,
Eric Paris52c923d2009-12-17 21:24:26 -0500949};
950
Eric Paris2a3edf82009-12-17 21:24:26 -0500951static int fanotify_find_path(int dfd, const char __user *filename,
Aaron Goidelac5656d2019-08-12 11:20:00 -0400952 struct path *path, unsigned int flags, __u64 mask,
953 unsigned int obj_type)
Eric Paris2a3edf82009-12-17 21:24:26 -0500954{
955 int ret;
956
957 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
958 dfd, filename, flags);
959
960 if (filename == NULL) {
Al Viro2903ff02012-08-28 12:52:22 -0400961 struct fd f = fdget(dfd);
Eric Paris2a3edf82009-12-17 21:24:26 -0500962
963 ret = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -0400964 if (!f.file)
Eric Paris2a3edf82009-12-17 21:24:26 -0500965 goto out;
966
967 ret = -ENOTDIR;
968 if ((flags & FAN_MARK_ONLYDIR) &&
Al Viro496ad9a2013-01-23 17:07:38 -0500969 !(S_ISDIR(file_inode(f.file)->i_mode))) {
Al Viro2903ff02012-08-28 12:52:22 -0400970 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500971 goto out;
972 }
973
Al Viro2903ff02012-08-28 12:52:22 -0400974 *path = f.file->f_path;
Eric Paris2a3edf82009-12-17 21:24:26 -0500975 path_get(path);
Al Viro2903ff02012-08-28 12:52:22 -0400976 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500977 } else {
978 unsigned int lookup_flags = 0;
979
980 if (!(flags & FAN_MARK_DONT_FOLLOW))
981 lookup_flags |= LOOKUP_FOLLOW;
982 if (flags & FAN_MARK_ONLYDIR)
983 lookup_flags |= LOOKUP_DIRECTORY;
984
985 ret = user_path_at(dfd, filename, lookup_flags, path);
986 if (ret)
987 goto out;
988 }
989
990 /* you can only watch an inode if you have read permissions on it */
Christian Brauner02f92b32021-01-21 14:19:22 +0100991 ret = path_permission(path, MAY_READ);
Aaron Goidelac5656d2019-08-12 11:20:00 -0400992 if (ret) {
993 path_put(path);
994 goto out;
995 }
996
997 ret = security_path_notify(path, mask, obj_type);
Eric Paris2a3edf82009-12-17 21:24:26 -0500998 if (ret)
999 path_put(path);
Aaron Goidelac5656d2019-08-12 11:20:00 -04001000
Eric Paris2a3edf82009-12-17 21:24:26 -05001001out:
1002 return ret;
1003}
1004
Eric Parisb9e4e3b2009-12-17 21:24:33 -05001005static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
Amir Goldstein4ed68142020-07-16 11:42:14 +03001006 __u32 mask, unsigned int flags,
1007 __u32 umask, int *destroy)
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -05001008{
Lino Sanfilippod2c18742015-02-10 14:08:24 -08001009 __u32 oldmask = 0;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -05001010
Amir Goldstein4ed68142020-07-16 11:42:14 +03001011 /* umask bits cannot be removed by user */
1012 mask &= ~umask;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -05001013 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -05001014 if (!(flags & FAN_MARK_IGNORED_MASK)) {
1015 oldmask = fsn_mark->mask;
Amir Goldsteina72fd222018-10-04 00:25:34 +03001016 fsn_mark->mask &= ~mask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -05001017 } else {
Amir Goldsteina72fd222018-10-04 00:25:34 +03001018 fsn_mark->ignored_mask &= ~mask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -05001019 }
Amir Goldstein4ed68142020-07-16 11:42:14 +03001020 /*
1021 * We need to keep the mark around even if remaining mask cannot
1022 * result in any events (e.g. mask == FAN_ONDIR) to support incremenal
1023 * changes to the mask.
1024 * Destroy mark when only umask bits remain.
1025 */
1026 *destroy = !((fsn_mark->mask | fsn_mark->ignored_mask) & ~umask);
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -05001027 spin_unlock(&fsn_mark->lock);
1028
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -05001029 return mask & oldmask;
1030}
1031
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001032static int fanotify_remove_mark(struct fsnotify_group *group,
1033 fsnotify_connp_t *connp, __u32 mask,
Amir Goldstein4ed68142020-07-16 11:42:14 +03001034 unsigned int flags, __u32 umask)
Eric Paris88826272009-12-17 21:24:28 -05001035{
1036 struct fsnotify_mark *fsn_mark = NULL;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -05001037 __u32 removed;
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +02001038 int destroy_mark;
Eric Paris88826272009-12-17 21:24:28 -05001039
Lino Sanfilippo7b185272013-07-08 15:59:42 -07001040 mutex_lock(&group->mark_mutex);
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001041 fsn_mark = fsnotify_find_mark(connp, group);
Lino Sanfilippo7b185272013-07-08 15:59:42 -07001042 if (!fsn_mark) {
1043 mutex_unlock(&group->mark_mutex);
Eric Paris88826272009-12-17 21:24:28 -05001044 return -ENOENT;
Lino Sanfilippo7b185272013-07-08 15:59:42 -07001045 }
Eric Paris88826272009-12-17 21:24:28 -05001046
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +02001047 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
Amir Goldstein4ed68142020-07-16 11:42:14 +03001048 umask, &destroy_mark);
Amir Goldstein3ac70bf2018-06-23 17:54:50 +03001049 if (removed & fsnotify_conn_mask(fsn_mark->connector))
1050 fsnotify_recalc_mask(fsn_mark->connector);
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +02001051 if (destroy_mark)
Jan Kara4712e7222015-09-04 15:43:12 -07001052 fsnotify_detach_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -07001053 mutex_unlock(&group->mark_mutex);
Jan Kara4712e7222015-09-04 15:43:12 -07001054 if (destroy_mark)
1055 fsnotify_free_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -07001056
Jan Karab1362ed2016-12-21 16:28:45 +01001057 /* matches the fsnotify_find_mark() */
Eric Paris2a3edf82009-12-17 21:24:26 -05001058 fsnotify_put_mark(fsn_mark);
Eric Paris2a3edf82009-12-17 21:24:26 -05001059 return 0;
1060}
1061
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001062static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
1063 struct vfsmount *mnt, __u32 mask,
Amir Goldstein4ed68142020-07-16 11:42:14 +03001064 unsigned int flags, __u32 umask)
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001065{
1066 return fanotify_remove_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
Amir Goldstein4ed68142020-07-16 11:42:14 +03001067 mask, flags, umask);
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001068}
1069
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001070static int fanotify_remove_sb_mark(struct fsnotify_group *group,
Amir Goldstein4ed68142020-07-16 11:42:14 +03001071 struct super_block *sb, __u32 mask,
1072 unsigned int flags, __u32 umask)
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001073{
Amir Goldstein4ed68142020-07-16 11:42:14 +03001074 return fanotify_remove_mark(group, &sb->s_fsnotify_marks, mask,
1075 flags, umask);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001076}
1077
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001078static int fanotify_remove_inode_mark(struct fsnotify_group *group,
1079 struct inode *inode, __u32 mask,
Amir Goldstein4ed68142020-07-16 11:42:14 +03001080 unsigned int flags, __u32 umask)
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001081{
1082 return fanotify_remove_mark(group, &inode->i_fsnotify_marks, mask,
Amir Goldstein4ed68142020-07-16 11:42:14 +03001083 flags, umask);
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001084}
1085
Eric Parisb9e4e3b2009-12-17 21:24:33 -05001086static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
1087 __u32 mask,
1088 unsigned int flags)
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -05001089{
Eric Paris192ca4d2010-10-28 17:21:59 -04001090 __u32 oldmask = -1;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -05001091
1092 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -05001093 if (!(flags & FAN_MARK_IGNORED_MASK)) {
1094 oldmask = fsn_mark->mask;
Amir Goldsteina72fd222018-10-04 00:25:34 +03001095 fsn_mark->mask |= mask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -05001096 } else {
Amir Goldsteina72fd222018-10-04 00:25:34 +03001097 fsn_mark->ignored_mask |= mask;
Eric Parisc9778a92009-12-17 21:24:33 -05001098 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
1099 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
Eric Parisb9e4e3b2009-12-17 21:24:33 -05001100 }
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -05001101 spin_unlock(&fsn_mark->lock);
1102
1103 return mask & ~oldmask;
1104}
1105
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001106static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
Amir Goldsteinb812a9f2018-06-23 17:54:48 +03001107 fsnotify_connp_t *connp,
Amir Goldsteinad69cd92021-11-29 22:15:27 +02001108 unsigned int obj_type,
Amir Goldstein77115222019-01-10 19:04:37 +02001109 __kernel_fsid_t *fsid)
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001110{
Amir Goldstein5b8fea62021-03-04 13:29:20 +02001111 struct ucounts *ucounts = group->fanotify_data.ucounts;
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001112 struct fsnotify_mark *mark;
1113 int ret;
1114
Amir Goldstein5b8fea62021-03-04 13:29:20 +02001115 /*
1116 * Enforce per user marks limits per user in all containing user ns.
1117 * A group with FAN_UNLIMITED_MARKS does not contribute to mark count
1118 * in the limited groups account.
1119 */
1120 if (!FAN_GROUP_FLAG(group, FAN_UNLIMITED_MARKS) &&
1121 !inc_ucount(ucounts->ns, ucounts->uid, UCOUNT_FANOTIFY_MARKS))
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001122 return ERR_PTR(-ENOSPC);
1123
1124 mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
Amir Goldstein5b8fea62021-03-04 13:29:20 +02001125 if (!mark) {
1126 ret = -ENOMEM;
1127 goto out_dec_ucounts;
1128 }
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001129
Jan Kara054c6362016-12-21 18:06:12 +01001130 fsnotify_init_mark(mark, group);
Amir Goldsteinad69cd92021-11-29 22:15:27 +02001131 ret = fsnotify_add_mark_locked(mark, connp, obj_type, 0, fsid);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001132 if (ret) {
1133 fsnotify_put_mark(mark);
Amir Goldstein5b8fea62021-03-04 13:29:20 +02001134 goto out_dec_ucounts;
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001135 }
1136
1137 return mark;
Amir Goldstein5b8fea62021-03-04 13:29:20 +02001138
1139out_dec_ucounts:
1140 if (!FAN_GROUP_FLAG(group, FAN_UNLIMITED_MARKS))
1141 dec_ucount(ucounts, UCOUNT_FANOTIFY_MARKS);
1142 return ERR_PTR(ret);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001143}
1144
Gabriel Krisman Bertazi734a1a52021-10-25 16:27:34 -03001145static int fanotify_group_init_error_pool(struct fsnotify_group *group)
1146{
1147 if (mempool_initialized(&group->fanotify_data.error_events_pool))
1148 return 0;
1149
1150 return mempool_init_kmalloc_pool(&group->fanotify_data.error_events_pool,
1151 FANOTIFY_DEFAULT_FEE_POOL_SIZE,
1152 sizeof(struct fanotify_error_event));
1153}
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001154
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001155static int fanotify_add_mark(struct fsnotify_group *group,
Amir Goldsteinad69cd92021-11-29 22:15:27 +02001156 fsnotify_connp_t *connp, unsigned int obj_type,
Amir Goldstein77115222019-01-10 19:04:37 +02001157 __u32 mask, unsigned int flags,
1158 __kernel_fsid_t *fsid)
Eric Paris2a3edf82009-12-17 21:24:26 -05001159{
1160 struct fsnotify_mark *fsn_mark;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -05001161 __u32 added;
Gabriel Krisman Bertazi734a1a52021-10-25 16:27:34 -03001162 int ret = 0;
Eric Paris2a3edf82009-12-17 21:24:26 -05001163
Lino Sanfilippo7b185272013-07-08 15:59:42 -07001164 mutex_lock(&group->mark_mutex);
Amir Goldsteinb812a9f2018-06-23 17:54:48 +03001165 fsn_mark = fsnotify_find_mark(connp, group);
Eric Paris88826272009-12-17 21:24:28 -05001166 if (!fsn_mark) {
Amir Goldsteinad69cd92021-11-29 22:15:27 +02001167 fsn_mark = fanotify_add_new_mark(group, connp, obj_type, fsid);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001168 if (IS_ERR(fsn_mark)) {
Lino Sanfilippo7b185272013-07-08 15:59:42 -07001169 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001170 return PTR_ERR(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -07001171 }
Eric Paris88826272009-12-17 21:24:28 -05001172 }
Gabriel Krisman Bertazi734a1a52021-10-25 16:27:34 -03001173
1174 /*
1175 * Error events are pre-allocated per group, only if strictly
1176 * needed (i.e. FAN_FS_ERROR was requested).
1177 */
1178 if (!(flags & FAN_MARK_IGNORED_MASK) && (mask & FAN_FS_ERROR)) {
1179 ret = fanotify_group_init_error_pool(group);
1180 if (ret)
1181 goto out;
1182 }
1183
Eric Parisb9e4e3b2009-12-17 21:24:33 -05001184 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
Amir Goldstein3ac70bf2018-06-23 17:54:50 +03001185 if (added & ~fsnotify_conn_mask(fsn_mark->connector))
1186 fsnotify_recalc_mask(fsn_mark->connector);
Gabriel Krisman Bertazi734a1a52021-10-25 16:27:34 -03001187
1188out:
Jan Karac9747642016-12-14 13:53:46 +01001189 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001190
Lino Sanfilippofa218ab2010-11-09 18:18:16 +01001191 fsnotify_put_mark(fsn_mark);
Gabriel Krisman Bertazi734a1a52021-10-25 16:27:34 -03001192 return ret;
Eric Paris88826272009-12-17 21:24:28 -05001193}
1194
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001195static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
1196 struct vfsmount *mnt, __u32 mask,
Amir Goldstein77115222019-01-10 19:04:37 +02001197 unsigned int flags, __kernel_fsid_t *fsid)
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001198{
1199 return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
Amir Goldstein77115222019-01-10 19:04:37 +02001200 FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags, fsid);
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001201}
1202
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001203static int fanotify_add_sb_mark(struct fsnotify_group *group,
Amir Goldstein77115222019-01-10 19:04:37 +02001204 struct super_block *sb, __u32 mask,
1205 unsigned int flags, __kernel_fsid_t *fsid)
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001206{
1207 return fanotify_add_mark(group, &sb->s_fsnotify_marks,
Amir Goldstein77115222019-01-10 19:04:37 +02001208 FSNOTIFY_OBJ_TYPE_SB, mask, flags, fsid);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001209}
1210
Andreas Gruenbacher52202df2009-12-17 21:24:28 -05001211static int fanotify_add_inode_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -05001212 struct inode *inode, __u32 mask,
Amir Goldstein77115222019-01-10 19:04:37 +02001213 unsigned int flags, __kernel_fsid_t *fsid)
Eric Paris88826272009-12-17 21:24:28 -05001214{
Eric Paris88826272009-12-17 21:24:28 -05001215 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
Eric Paris2a3edf82009-12-17 21:24:26 -05001216
Eric Paris5322a592010-10-28 17:21:57 -04001217 /*
1218 * If some other task has this inode open for write we should not add
1219 * an ignored mark, unless that ignored mark is supposed to survive
1220 * modification changes anyway.
1221 */
1222 if ((flags & FAN_MARK_IGNORED_MASK) &&
1223 !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
Nikolay Borisovac9498d2018-12-11 10:27:23 +02001224 inode_is_open_for_write(inode))
Eric Paris5322a592010-10-28 17:21:57 -04001225 return 0;
1226
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001227 return fanotify_add_mark(group, &inode->i_fsnotify_marks,
Amir Goldstein77115222019-01-10 19:04:37 +02001228 FSNOTIFY_OBJ_TYPE_INODE, mask, flags, fsid);
Eric Paris88826272009-12-17 21:24:28 -05001229}
Eric Paris2a3edf82009-12-17 21:24:26 -05001230
Amir Goldsteinb8a6c3a2020-07-08 14:11:42 +03001231static struct fsnotify_event *fanotify_alloc_overflow_event(void)
1232{
1233 struct fanotify_event *oevent;
1234
1235 oevent = kmalloc(sizeof(*oevent), GFP_KERNEL_ACCOUNT);
1236 if (!oevent)
1237 return NULL;
1238
1239 fanotify_init_event(oevent, 0, FS_Q_OVERFLOW);
1240 oevent->type = FANOTIFY_EVENT_TYPE_OVERFLOW;
1241
1242 return &oevent->fse;
1243}
1244
Amir Goldstein94e00d22021-03-04 12:48:25 +02001245static struct hlist_head *fanotify_alloc_merge_hash(void)
1246{
1247 struct hlist_head *hash;
1248
1249 hash = kmalloc(sizeof(struct hlist_head) << FANOTIFY_HTABLE_BITS,
1250 GFP_KERNEL_ACCOUNT);
1251 if (!hash)
1252 return NULL;
1253
1254 __hash_init(hash, FANOTIFY_HTABLE_SIZE);
1255
1256 return hash;
1257}
1258
Eric Paris52c923d2009-12-17 21:24:26 -05001259/* fanotify syscalls */
Eric Paris08ae8932010-05-27 09:41:40 -04001260SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
Eric Paris11637e42009-12-17 21:24:25 -05001261{
Eric Paris52c923d2009-12-17 21:24:26 -05001262 struct fsnotify_group *group;
1263 int f_flags, fd;
Amir Goldstein83b7a592020-07-16 11:42:26 +03001264 unsigned int fid_mode = flags & FANOTIFY_FID_BITS;
1265 unsigned int class = flags & FANOTIFY_CLASS_BITS;
Amir Goldsteina8b98c82021-05-24 16:53:21 +03001266 unsigned int internal_flags = 0;
Eric Paris52c923d2009-12-17 21:24:26 -05001267
Amir Goldstein96a71f22018-09-21 21:20:30 +03001268 pr_debug("%s: flags=%x event_f_flags=%x\n",
1269 __func__, flags, event_f_flags);
Eric Paris52c923d2009-12-17 21:24:26 -05001270
Amir Goldstein7cea2a32021-03-04 13:29:21 +02001271 if (!capable(CAP_SYS_ADMIN)) {
1272 /*
1273 * An unprivileged user can setup an fanotify group with
1274 * limited functionality - an unprivileged group is limited to
1275 * notification events with file handles and it cannot use
1276 * unlimited queue/marks.
1277 */
1278 if ((flags & FANOTIFY_ADMIN_INIT_FLAGS) || !fid_mode)
1279 return -EPERM;
Amir Goldsteina8b98c82021-05-24 16:53:21 +03001280
1281 /*
1282 * Setting the internal flag FANOTIFY_UNPRIV on the group
1283 * prevents setting mount/filesystem marks on this group and
1284 * prevents reporting pid and open fd in events.
1285 */
1286 internal_flags |= FANOTIFY_UNPRIV;
Amir Goldstein7cea2a32021-03-04 13:29:21 +02001287 }
Eric Paris52c923d2009-12-17 21:24:26 -05001288
Steve Grubbde8cd832017-10-02 20:21:39 -04001289#ifdef CONFIG_AUDITSYSCALL
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001290 if (flags & ~(FANOTIFY_INIT_FLAGS | FAN_ENABLE_AUDIT))
Steve Grubbde8cd832017-10-02 20:21:39 -04001291#else
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001292 if (flags & ~FANOTIFY_INIT_FLAGS)
Steve Grubbde8cd832017-10-02 20:21:39 -04001293#endif
Eric Paris52c923d2009-12-17 21:24:26 -05001294 return -EINVAL;
1295
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +10001296 /*
1297 * A pidfd can only be returned for a thread-group leader; thus
1298 * FAN_REPORT_PIDFD and FAN_REPORT_TID need to remain mutually
1299 * exclusive.
1300 */
1301 if ((flags & FAN_REPORT_PIDFD) && (flags & FAN_REPORT_TID))
1302 return -EINVAL;
1303
Heinrich Schuchardt48149e92014-06-04 16:05:44 -07001304 if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS)
1305 return -EINVAL;
1306
1307 switch (event_f_flags & O_ACCMODE) {
1308 case O_RDONLY:
1309 case O_RDWR:
1310 case O_WRONLY:
1311 break;
1312 default:
1313 return -EINVAL;
1314 }
1315
Amir Goldstein83b7a592020-07-16 11:42:26 +03001316 if (fid_mode && class != FAN_CLASS_NOTIF)
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001317 return -EINVAL;
1318
Amir Goldstein929943b2020-07-16 11:42:28 +03001319 /*
Amir Goldstein929943b2020-07-16 11:42:28 +03001320 * Child name is reported with parent fid so requires dir fid.
Amir Goldstein691d9762020-07-16 11:42:30 +03001321 * We can report both child fid and dir fid with or without name.
Amir Goldstein929943b2020-07-16 11:42:28 +03001322 */
Amir Goldstein691d9762020-07-16 11:42:30 +03001323 if ((fid_mode & FAN_REPORT_NAME) && !(fid_mode & FAN_REPORT_DIR_FID))
Amir Goldstein83b7a592020-07-16 11:42:26 +03001324 return -EINVAL;
Amir Goldstein83b7a592020-07-16 11:42:26 +03001325
Amir Goldsteind61fd652021-11-29 22:15:29 +02001326 /*
1327 * FAN_REPORT_TARGET_FID requires FAN_REPORT_NAME and FAN_REPORT_FID
1328 * and is used as an indication to report both dir and child fid on all
1329 * dirent events.
1330 */
1331 if ((fid_mode & FAN_REPORT_TARGET_FID) &&
1332 (!(fid_mode & FAN_REPORT_NAME) || !(fid_mode & FAN_REPORT_FID)))
1333 return -EINVAL;
1334
Eric Parisb2d87902009-12-17 21:24:34 -05001335 f_flags = O_RDWR | FMODE_NONOTIFY;
Eric Paris52c923d2009-12-17 21:24:26 -05001336 if (flags & FAN_CLOEXEC)
1337 f_flags |= O_CLOEXEC;
1338 if (flags & FAN_NONBLOCK)
1339 f_flags |= O_NONBLOCK;
1340
1341 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
Shakeel Buttac7b79f2020-12-19 20:46:08 -08001342 group = fsnotify_alloc_user_group(&fanotify_fsnotify_ops);
Eric Paris26379192010-11-23 23:48:26 -05001343 if (IS_ERR(group)) {
Eric Paris52c923d2009-12-17 21:24:26 -05001344 return PTR_ERR(group);
Eric Paris26379192010-11-23 23:48:26 -05001345 }
Eric Paris52c923d2009-12-17 21:24:26 -05001346
Amir Goldstein5b8fea62021-03-04 13:29:20 +02001347 /* Enforce groups limits per user in all containing user ns */
1348 group->fanotify_data.ucounts = inc_ucount(current_user_ns(),
1349 current_euid(),
1350 UCOUNT_FANOTIFY_GROUPS);
1351 if (!group->fanotify_data.ucounts) {
1352 fd = -EMFILE;
1353 goto out_destroy_group;
1354 }
1355
Amir Goldsteina8b98c82021-05-24 16:53:21 +03001356 group->fanotify_data.flags = flags | internal_flags;
Shakeel Buttd46eb14b2018-08-17 15:46:39 -07001357 group->memcg = get_mem_cgroup_from_mm(current->mm);
Eric Paris4afeff82010-10-28 17:21:58 -04001358
Amir Goldstein94e00d22021-03-04 12:48:25 +02001359 group->fanotify_data.merge_hash = fanotify_alloc_merge_hash();
1360 if (!group->fanotify_data.merge_hash) {
1361 fd = -ENOMEM;
1362 goto out_destroy_group;
1363 }
1364
Amir Goldsteinb8a6c3a2020-07-08 14:11:42 +03001365 group->overflow_event = fanotify_alloc_overflow_event();
1366 if (unlikely(!group->overflow_event)) {
Jan Karaff57cd52014-02-21 19:14:11 +01001367 fd = -ENOMEM;
1368 goto out_destroy_group;
1369 }
Jan Karaff57cd52014-02-21 19:14:11 +01001370
Will Woods1e2ee492014-05-06 12:50:10 -07001371 if (force_o_largefile())
1372 event_f_flags |= O_LARGEFILE;
Eric Paris80af2582010-07-28 10:18:37 -04001373 group->fanotify_data.f_flags = event_f_flags;
Eric Paris9e66e422009-12-17 21:24:34 -05001374 init_waitqueue_head(&group->fanotify_data.access_waitq);
1375 INIT_LIST_HEAD(&group->fanotify_data.access_list);
Amir Goldstein83b7a592020-07-16 11:42:26 +03001376 switch (class) {
Eric Paris4231a232010-10-28 17:21:56 -04001377 case FAN_CLASS_NOTIF:
1378 group->priority = FS_PRIO_0;
1379 break;
1380 case FAN_CLASS_CONTENT:
1381 group->priority = FS_PRIO_1;
1382 break;
1383 case FAN_CLASS_PRE_CONTENT:
1384 group->priority = FS_PRIO_2;
1385 break;
1386 default:
1387 fd = -EINVAL;
Lino Sanfilippod8153d42011-06-14 17:29:45 +02001388 goto out_destroy_group;
Eric Paris4231a232010-10-28 17:21:56 -04001389 }
Eric Pariscb2d4292009-12-17 21:24:34 -05001390
Eric Paris5dd03f52010-10-28 17:21:57 -04001391 if (flags & FAN_UNLIMITED_QUEUE) {
1392 fd = -EPERM;
1393 if (!capable(CAP_SYS_ADMIN))
Lino Sanfilippod8153d42011-06-14 17:29:45 +02001394 goto out_destroy_group;
Eric Paris5dd03f52010-10-28 17:21:57 -04001395 group->max_events = UINT_MAX;
1396 } else {
Amir Goldstein5b8fea62021-03-04 13:29:20 +02001397 group->max_events = fanotify_max_queued_events;
Eric Paris5dd03f52010-10-28 17:21:57 -04001398 }
Eric Paris2529a0d2010-10-28 17:21:57 -04001399
Eric Parisac7e22d2010-10-28 17:21:58 -04001400 if (flags & FAN_UNLIMITED_MARKS) {
1401 fd = -EPERM;
1402 if (!capable(CAP_SYS_ADMIN))
Lino Sanfilippod8153d42011-06-14 17:29:45 +02001403 goto out_destroy_group;
Eric Parisac7e22d2010-10-28 17:21:58 -04001404 }
Eric Parise7099d82010-10-28 17:21:57 -04001405
Steve Grubbde8cd832017-10-02 20:21:39 -04001406 if (flags & FAN_ENABLE_AUDIT) {
1407 fd = -EPERM;
1408 if (!capable(CAP_AUDIT_WRITE))
1409 goto out_destroy_group;
Steve Grubbde8cd832017-10-02 20:21:39 -04001410 }
1411
Eric Paris52c923d2009-12-17 21:24:26 -05001412 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
1413 if (fd < 0)
Lino Sanfilippod8153d42011-06-14 17:29:45 +02001414 goto out_destroy_group;
Eric Paris52c923d2009-12-17 21:24:26 -05001415
1416 return fd;
1417
Lino Sanfilippod8153d42011-06-14 17:29:45 +02001418out_destroy_group:
1419 fsnotify_destroy_group(group);
Eric Paris52c923d2009-12-17 21:24:26 -05001420 return fd;
Eric Paris11637e42009-12-17 21:24:25 -05001421}
Eric Parisbbaa4162009-12-17 21:24:26 -05001422
Gabriel Krisman Bertazi82992122021-10-25 16:27:21 -03001423static int fanotify_test_fsid(struct dentry *dentry, __kernel_fsid_t *fsid)
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001424{
Amir Goldstein73072282019-01-10 19:04:39 +02001425 __kernel_fsid_t root_fsid;
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001426 int err;
1427
1428 /*
Gabriel Krisman Bertazi82992122021-10-25 16:27:21 -03001429 * Make sure dentry is not of a filesystem with zero fsid (e.g. fuse).
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001430 */
Gabriel Krisman Bertazi82992122021-10-25 16:27:21 -03001431 err = vfs_get_fsid(dentry, fsid);
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001432 if (err)
1433 return err;
1434
Amir Goldstein73072282019-01-10 19:04:39 +02001435 if (!fsid->val[0] && !fsid->val[1])
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001436 return -ENODEV;
1437
1438 /*
Gabriel Krisman Bertazi82992122021-10-25 16:27:21 -03001439 * Make sure dentry is not of a filesystem subvolume (e.g. btrfs)
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001440 * which uses a different fsid than sb root.
1441 */
Gabriel Krisman Bertazi82992122021-10-25 16:27:21 -03001442 err = vfs_get_fsid(dentry->d_sb->s_root, &root_fsid);
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001443 if (err)
1444 return err;
1445
Amir Goldstein73072282019-01-10 19:04:39 +02001446 if (root_fsid.val[0] != fsid->val[0] ||
1447 root_fsid.val[1] != fsid->val[1])
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001448 return -EXDEV;
1449
Gabriel Krisman Bertazi82992122021-10-25 16:27:21 -03001450 return 0;
1451}
1452
1453/* Check if filesystem can encode a unique fid */
1454static int fanotify_test_fid(struct dentry *dentry)
1455{
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001456 /*
1457 * We need to make sure that the file system supports at least
1458 * encoding a file handle so user can use name_to_handle_at() to
1459 * compare fid returned with event to the file handle of watched
1460 * objects. However, name_to_handle_at() requires that the
1461 * filesystem also supports decoding file handles.
1462 */
Gabriel Krisman Bertazi82992122021-10-25 16:27:21 -03001463 if (!dentry->d_sb->s_export_op ||
1464 !dentry->d_sb->s_export_op->fh_to_dentry)
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001465 return -EOPNOTSUPP;
1466
1467 return 0;
1468}
1469
Jan Kara0b3b0942019-05-15 16:28:34 +02001470static int fanotify_events_supported(struct path *path, __u64 mask)
1471{
1472 /*
1473 * Some filesystems such as 'proc' acquire unusual locks when opening
1474 * files. For them fanotify permission events have high chances of
1475 * deadlocking the system - open done when reporting fanotify event
1476 * blocks on this "unusual" lock while another process holding the lock
1477 * waits for fanotify permission event to be answered. Just disallow
1478 * permission events for such filesystems.
1479 */
1480 if (mask & FANOTIFY_PERM_EVENTS &&
1481 path->mnt->mnt_sb->s_type->fs_flags & FS_DISALLOW_NOTIFY_PERM)
1482 return -EINVAL;
1483 return 0;
1484}
1485
Dominik Brodowski183caa32018-03-17 15:06:11 +01001486static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
1487 int dfd, const char __user *pathname)
Eric Parisbbaa4162009-12-17 21:24:26 -05001488{
Eric Paris0ff21db2009-12-17 21:24:29 -05001489 struct inode *inode = NULL;
1490 struct vfsmount *mnt = NULL;
Eric Paris2a3edf82009-12-17 21:24:26 -05001491 struct fsnotify_group *group;
Al Viro2903ff02012-08-28 12:52:22 -04001492 struct fd f;
Eric Paris2a3edf82009-12-17 21:24:26 -05001493 struct path path;
Amir Goldstein73072282019-01-10 19:04:39 +02001494 __kernel_fsid_t __fsid, *fsid = NULL;
Amir Goldsteinbdd5a462018-10-04 00:25:37 +03001495 u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS;
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001496 unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS;
Amir Goldstein3ef86652020-07-16 11:42:13 +03001497 bool ignored = flags & FAN_MARK_IGNORED_MASK;
Amir Goldsteind809daf2020-07-16 11:42:12 +03001498 unsigned int obj_type, fid_mode;
Amir Goldstein85af5d92020-07-16 11:42:15 +03001499 u32 umask = 0;
Al Viro2903ff02012-08-28 12:52:22 -04001500 int ret;
Eric Paris2a3edf82009-12-17 21:24:26 -05001501
1502 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
1503 __func__, fanotify_fd, flags, dfd, pathname, mask);
1504
1505 /* we only use the lower 32 bits as of right now. */
Christian Brauner22d483b2021-03-25 09:37:43 +01001506 if (upper_32_bits(mask))
Eric Paris2a3edf82009-12-17 21:24:26 -05001507 return -EINVAL;
1508
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001509 if (flags & ~FANOTIFY_MARK_FLAGS)
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -05001510 return -EINVAL;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001511
1512 switch (mark_type) {
1513 case FAN_MARK_INODE:
Aaron Goidelac5656d2019-08-12 11:20:00 -04001514 obj_type = FSNOTIFY_OBJ_TYPE_INODE;
1515 break;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001516 case FAN_MARK_MOUNT:
Aaron Goidelac5656d2019-08-12 11:20:00 -04001517 obj_type = FSNOTIFY_OBJ_TYPE_VFSMOUNT;
1518 break;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001519 case FAN_MARK_FILESYSTEM:
Aaron Goidelac5656d2019-08-12 11:20:00 -04001520 obj_type = FSNOTIFY_OBJ_TYPE_SB;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001521 break;
1522 default:
1523 return -EINVAL;
1524 }
1525
Eric Paris4d926042009-12-17 21:24:34 -05001526 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001527 case FAN_MARK_ADD:
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -05001528 case FAN_MARK_REMOVE:
Lino Sanfilippo1734dee2010-11-22 18:46:33 +01001529 if (!mask)
1530 return -EINVAL;
Heinrich Schuchardtcc299a92014-06-04 16:05:43 -07001531 break;
Eric Paris4d926042009-12-17 21:24:34 -05001532 case FAN_MARK_FLUSH:
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001533 if (flags & ~(FANOTIFY_MARK_TYPE_BITS | FAN_MARK_FLUSH))
Heinrich Schuchardtcc299a92014-06-04 16:05:43 -07001534 return -EINVAL;
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -05001535 break;
1536 default:
1537 return -EINVAL;
1538 }
Eric Paris8fcd6522010-10-28 17:21:59 -04001539
Miklos Szeredi6685df32017-10-30 21:14:56 +01001540 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001541 valid_mask |= FANOTIFY_PERM_EVENTS;
Miklos Szeredi6685df32017-10-30 21:14:56 +01001542
1543 if (mask & ~valid_mask)
Eric Paris2a3edf82009-12-17 21:24:26 -05001544 return -EINVAL;
1545
Amir Goldstein3ef86652020-07-16 11:42:13 +03001546 /* Event flags (ONDIR, ON_CHILD) are meaningless in ignored mask */
1547 if (ignored)
1548 mask &= ~FANOTIFY_EVENT_FLAGS;
1549
Al Viro2903ff02012-08-28 12:52:22 -04001550 f = fdget(fanotify_fd);
1551 if (unlikely(!f.file))
Eric Paris2a3edf82009-12-17 21:24:26 -05001552 return -EBADF;
1553
1554 /* verify that this is indeed an fanotify instance */
1555 ret = -EINVAL;
Al Viro2903ff02012-08-28 12:52:22 -04001556 if (unlikely(f.file->f_op != &fanotify_fops))
Eric Paris2a3edf82009-12-17 21:24:26 -05001557 goto fput_and_out;
Al Viro2903ff02012-08-28 12:52:22 -04001558 group = f.file->private_data;
Eric Paris4231a232010-10-28 17:21:56 -04001559
1560 /*
Amir Goldsteina8b98c82021-05-24 16:53:21 +03001561 * An unprivileged user is not allowed to setup mount nor filesystem
1562 * marks. This also includes setting up such marks by a group that
1563 * was initialized by an unprivileged user.
Amir Goldstein7cea2a32021-03-04 13:29:21 +02001564 */
1565 ret = -EPERM;
Amir Goldsteina8b98c82021-05-24 16:53:21 +03001566 if ((!capable(CAP_SYS_ADMIN) ||
1567 FAN_GROUP_FLAG(group, FANOTIFY_UNPRIV)) &&
Amir Goldstein7cea2a32021-03-04 13:29:21 +02001568 mark_type != FAN_MARK_INODE)
1569 goto fput_and_out;
1570
1571 /*
Eric Paris4231a232010-10-28 17:21:56 -04001572 * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
1573 * allowed to set permissions events.
1574 */
1575 ret = -EINVAL;
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001576 if (mask & FANOTIFY_PERM_EVENTS &&
Eric Paris4231a232010-10-28 17:21:56 -04001577 group->priority == FS_PRIO_0)
1578 goto fput_and_out;
Eric Paris2a3edf82009-12-17 21:24:26 -05001579
Gabriel Krisman Bertazi9709bd52021-10-25 16:27:43 -03001580 if (mask & FAN_FS_ERROR &&
1581 mark_type != FAN_MARK_FILESYSTEM)
1582 goto fput_and_out;
1583
Amir Goldstein235328d2019-01-10 19:04:43 +02001584 /*
Gabriel Krisman Bertazi4fe595c2021-10-25 16:27:31 -03001585 * Events that do not carry enough information to report
1586 * event->fd require a group that supports reporting fid. Those
1587 * events are not supported on a mount mark, because they do not
1588 * carry enough information (i.e. path) to be filtered by mount
1589 * point.
Amir Goldstein235328d2019-01-10 19:04:43 +02001590 */
Amir Goldsteind809daf2020-07-16 11:42:12 +03001591 fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
Gabriel Krisman Bertazi4fe595c2021-10-25 16:27:31 -03001592 if (mask & ~(FANOTIFY_FD_EVENTS|FANOTIFY_EVENT_FLAGS) &&
Amir Goldsteind809daf2020-07-16 11:42:12 +03001593 (!fid_mode || mark_type == FAN_MARK_MOUNT))
Amir Goldstein235328d2019-01-10 19:04:43 +02001594 goto fput_and_out;
1595
Amir Goldstein8cc3b1c2021-11-29 22:15:37 +02001596 /*
1597 * FAN_RENAME uses special info type records to report the old and
1598 * new parent+name. Reporting only old and new parent id is less
1599 * useful and was not implemented.
1600 */
1601 if (mask & FAN_RENAME && !(fid_mode & FAN_REPORT_NAME))
1602 goto fput_and_out;
1603
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -07001604 if (flags & FAN_MARK_FLUSH) {
1605 ret = 0;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001606 if (mark_type == FAN_MARK_MOUNT)
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -07001607 fsnotify_clear_vfsmount_marks_by_group(group);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001608 else if (mark_type == FAN_MARK_FILESYSTEM)
1609 fsnotify_clear_sb_marks_by_group(group);
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -07001610 else
1611 fsnotify_clear_inode_marks_by_group(group);
1612 goto fput_and_out;
1613 }
1614
Aaron Goidelac5656d2019-08-12 11:20:00 -04001615 ret = fanotify_find_path(dfd, pathname, &path, flags,
1616 (mask & ALL_FSNOTIFY_EVENTS), obj_type);
Eric Paris2a3edf82009-12-17 21:24:26 -05001617 if (ret)
1618 goto fput_and_out;
1619
Jan Kara0b3b0942019-05-15 16:28:34 +02001620 if (flags & FAN_MARK_ADD) {
1621 ret = fanotify_events_supported(&path, mask);
1622 if (ret)
1623 goto path_put_and_out;
1624 }
1625
Amir Goldsteind809daf2020-07-16 11:42:12 +03001626 if (fid_mode) {
Gabriel Krisman Bertazi82992122021-10-25 16:27:21 -03001627 ret = fanotify_test_fsid(path.dentry, &__fsid);
1628 if (ret)
1629 goto path_put_and_out;
1630
1631 ret = fanotify_test_fid(path.dentry);
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001632 if (ret)
1633 goto path_put_and_out;
Amir Goldstein77115222019-01-10 19:04:37 +02001634
Amir Goldstein73072282019-01-10 19:04:39 +02001635 fsid = &__fsid;
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001636 }
1637
Eric Paris2a3edf82009-12-17 21:24:26 -05001638 /* inode held in place by reference to path; group by fget on fd */
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001639 if (mark_type == FAN_MARK_INODE)
Eric Paris0ff21db2009-12-17 21:24:29 -05001640 inode = path.dentry->d_inode;
1641 else
1642 mnt = path.mnt;
Eric Paris2a3edf82009-12-17 21:24:26 -05001643
Amir Goldstein85af5d92020-07-16 11:42:15 +03001644 /* Mask out FAN_EVENT_ON_CHILD flag for sb/mount/non-dir marks */
1645 if (mnt || !S_ISDIR(inode->i_mode)) {
1646 mask &= ~FAN_EVENT_ON_CHILD;
1647 umask = FAN_EVENT_ON_CHILD;
Amir Goldstein51280632020-07-16 11:42:27 +03001648 /*
1649 * If group needs to report parent fid, register for getting
1650 * events with parent/name info for non-directory.
1651 */
1652 if ((fid_mode & FAN_REPORT_DIR_FID) &&
1653 (flags & FAN_MARK_ADD) && !ignored)
1654 mask |= FAN_EVENT_ON_CHILD;
Amir Goldstein85af5d92020-07-16 11:42:15 +03001655 }
1656
Eric Paris2a3edf82009-12-17 21:24:26 -05001657 /* create/update an inode mark */
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -07001658 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -05001659 case FAN_MARK_ADD:
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001660 if (mark_type == FAN_MARK_MOUNT)
Amir Goldstein77115222019-01-10 19:04:37 +02001661 ret = fanotify_add_vfsmount_mark(group, mnt, mask,
1662 flags, fsid);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001663 else if (mark_type == FAN_MARK_FILESYSTEM)
Amir Goldstein77115222019-01-10 19:04:37 +02001664 ret = fanotify_add_sb_mark(group, mnt->mnt_sb, mask,
1665 flags, fsid);
Eric Paris0ff21db2009-12-17 21:24:29 -05001666 else
Amir Goldstein77115222019-01-10 19:04:37 +02001667 ret = fanotify_add_inode_mark(group, inode, mask,
1668 flags, fsid);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -05001669 break;
1670 case FAN_MARK_REMOVE:
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001671 if (mark_type == FAN_MARK_MOUNT)
Amir Goldstein77115222019-01-10 19:04:37 +02001672 ret = fanotify_remove_vfsmount_mark(group, mnt, mask,
Amir Goldstein85af5d92020-07-16 11:42:15 +03001673 flags, umask);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001674 else if (mark_type == FAN_MARK_FILESYSTEM)
Amir Goldstein77115222019-01-10 19:04:37 +02001675 ret = fanotify_remove_sb_mark(group, mnt->mnt_sb, mask,
Amir Goldstein85af5d92020-07-16 11:42:15 +03001676 flags, umask);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -05001677 else
Amir Goldstein77115222019-01-10 19:04:37 +02001678 ret = fanotify_remove_inode_mark(group, inode, mask,
Amir Goldstein85af5d92020-07-16 11:42:15 +03001679 flags, umask);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -05001680 break;
1681 default:
1682 ret = -EINVAL;
1683 }
Eric Paris2a3edf82009-12-17 21:24:26 -05001684
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001685path_put_and_out:
Eric Paris2a3edf82009-12-17 21:24:26 -05001686 path_put(&path);
1687fput_and_out:
Al Viro2903ff02012-08-28 12:52:22 -04001688 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -05001689 return ret;
Eric Parisbbaa4162009-12-17 21:24:26 -05001690}
Eric Paris2a3edf82009-12-17 21:24:26 -05001691
Brian Gerst2ca408d2020-11-30 17:30:59 -05001692#ifndef CONFIG_ARCH_SPLIT_ARG64
Dominik Brodowski183caa32018-03-17 15:06:11 +01001693SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
1694 __u64, mask, int, dfd,
1695 const char __user *, pathname)
1696{
1697 return do_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname);
1698}
Brian Gerst2ca408d2020-11-30 17:30:59 -05001699#endif
Dominik Brodowski183caa32018-03-17 15:06:11 +01001700
Brian Gerst2ca408d2020-11-30 17:30:59 -05001701#if defined(CONFIG_ARCH_SPLIT_ARG64) || defined(CONFIG_COMPAT)
1702SYSCALL32_DEFINE6(fanotify_mark,
Al Viro91c2e0b2013-03-05 20:10:59 -05001703 int, fanotify_fd, unsigned int, flags,
Brian Gerst2ca408d2020-11-30 17:30:59 -05001704 SC_ARG64(mask), int, dfd,
Al Viro91c2e0b2013-03-05 20:10:59 -05001705 const char __user *, pathname)
1706{
Brian Gerst2ca408d2020-11-30 17:30:59 -05001707 return do_fanotify_mark(fanotify_fd, flags, SC_VAL64(__u64, mask),
1708 dfd, pathname);
Al Viro91c2e0b2013-03-05 20:10:59 -05001709}
1710#endif
1711
Eric Paris2a3edf82009-12-17 21:24:26 -05001712/*
Justin P. Mattockae0e47f2011-03-01 15:06:02 +01001713 * fanotify_user_setup - Our initialization function. Note that we cannot return
Eric Paris2a3edf82009-12-17 21:24:26 -05001714 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
1715 * must result in panic().
1716 */
1717static int __init fanotify_user_setup(void)
1718{
Amir Goldstein5b8fea62021-03-04 13:29:20 +02001719 struct sysinfo si;
1720 int max_marks;
1721
1722 si_meminfo(&si);
1723 /*
1724 * Allow up to 1% of addressable memory to be accounted for per user
1725 * marks limited to the range [8192, 1048576]. mount and sb marks are
1726 * a lot cheaper than inode marks, but there is no reason for a user
1727 * to have many of those, so calculate by the cost of inode marks.
1728 */
1729 max_marks = (((si.totalram - si.totalhigh) / 100) << PAGE_SHIFT) /
1730 INODE_MARK_COST;
1731 max_marks = clamp(max_marks, FANOTIFY_OLD_DEFAULT_MAX_MARKS,
1732 FANOTIFY_DEFAULT_MAX_USER_MARKS);
1733
Amir Goldsteina8b98c82021-05-24 16:53:21 +03001734 BUILD_BUG_ON(FANOTIFY_INIT_FLAGS & FANOTIFY_INTERNAL_GROUP_FLAGS);
Amir Goldsteind61fd652021-11-29 22:15:29 +02001735 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 12);
Amir Goldsteinbdd5a462018-10-04 00:25:37 +03001736 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9);
1737
Shakeel Buttd46eb14b2018-08-17 15:46:39 -07001738 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark,
1739 SLAB_PANIC|SLAB_ACCOUNT);
Jan Kara7088f352020-03-24 17:04:20 +01001740 fanotify_fid_event_cachep = KMEM_CACHE(fanotify_fid_event,
1741 SLAB_PANIC);
1742 fanotify_path_event_cachep = KMEM_CACHE(fanotify_path_event,
1743 SLAB_PANIC);
Miklos Szeredi6685df32017-10-30 21:14:56 +01001744 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
1745 fanotify_perm_event_cachep =
Amir Goldstein33913992019-01-10 19:04:32 +02001746 KMEM_CACHE(fanotify_perm_event, SLAB_PANIC);
Miklos Szeredi6685df32017-10-30 21:14:56 +01001747 }
Eric Paris2a3edf82009-12-17 21:24:26 -05001748
Amir Goldstein5b8fea62021-03-04 13:29:20 +02001749 fanotify_max_queued_events = FANOTIFY_DEFAULT_MAX_EVENTS;
1750 init_user_ns.ucount_max[UCOUNT_FANOTIFY_GROUPS] =
1751 FANOTIFY_DEFAULT_MAX_GROUPS;
1752 init_user_ns.ucount_max[UCOUNT_FANOTIFY_MARKS] = max_marks;
Xiaoming Ni7b9ad122022-01-21 22:11:59 -08001753 fanotify_sysctls_init();
Amir Goldstein5b8fea62021-03-04 13:29:20 +02001754
Eric Paris2a3edf82009-12-17 21:24:26 -05001755 return 0;
1756}
1757device_initcall(fanotify_user_setup);