blob: 00bbc29712bbfbb2affd830651184eee17ae053d [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
Amir Goldstein5b8fea62021-03-04 13:29:20 +020062struct ctl_table fanotify_table[] = {
63 {
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};
91#endif /* CONFIG_SYSCTL */
Eric Paris2529a0d2010-10-28 17:21:57 -040092
Heinrich Schuchardt48149e92014-06-04 16:05:44 -070093/*
94 * All flags that may be specified in parameter event_f_flags of fanotify_init.
95 *
96 * Internal and external open flags are stored together in field f_flags of
97 * struct file. Only external open flags shall be allowed in event_f_flags.
98 * Internal flags like FMODE_NONOTIFY, FMODE_EXEC, FMODE_NOCMTIME shall be
99 * excluded.
100 */
101#define FANOTIFY_INIT_ALL_EVENT_F_BITS ( \
102 O_ACCMODE | O_APPEND | O_NONBLOCK | \
103 __O_SYNC | O_DSYNC | O_CLOEXEC | \
104 O_LARGEFILE | O_NOATIME )
105
Andreas Gruenbacher33d3dff2009-12-17 21:24:29 -0500106extern const struct fsnotify_ops fanotify_fsnotify_ops;
Eric Paris11637e42009-12-17 21:24:25 -0500107
Jan Kara054c6362016-12-21 18:06:12 +0100108struct kmem_cache *fanotify_mark_cache __read_mostly;
Jan Kara7088f352020-03-24 17:04:20 +0100109struct kmem_cache *fanotify_fid_event_cachep __read_mostly;
110struct kmem_cache *fanotify_path_event_cachep __read_mostly;
Jan Karaf0834412014-04-03 14:46:33 -0700111struct kmem_cache *fanotify_perm_event_cachep __read_mostly;
Eric Paris2a3edf82009-12-17 21:24:26 -0500112
Amir Goldstein5e469c82019-01-10 19:04:35 +0200113#define FANOTIFY_EVENT_ALIGN 4
Matthew Bobrowskid3424c92021-08-08 15:25:32 +1000114#define FANOTIFY_FID_INFO_HDR_LEN \
Amir Goldstein44d705b2020-03-19 17:10:22 +0200115 (sizeof(struct fanotify_event_info_fid) + sizeof(struct file_handle))
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000116#define FANOTIFY_PIDFD_INFO_HDR_LEN \
117 sizeof(struct fanotify_event_info_pidfd)
Gabriel Krisman Bertazi130a3c72021-10-25 16:27:42 -0300118#define FANOTIFY_ERROR_INFO_LEN \
119 (sizeof(struct fanotify_event_info_error))
Amir Goldstein5e469c82019-01-10 19:04:35 +0200120
Amir Goldstein44d705b2020-03-19 17:10:22 +0200121static int fanotify_fid_info_len(int fh_len, int name_len)
Amir Goldsteind766b552020-03-19 17:10:20 +0200122{
Amir Goldstein44d705b2020-03-19 17:10:22 +0200123 int info_len = fh_len;
124
125 if (name_len)
126 info_len += name_len + 1;
127
Matthew Bobrowskid3424c92021-08-08 15:25:32 +1000128 return roundup(FANOTIFY_FID_INFO_HDR_LEN + info_len,
129 FANOTIFY_EVENT_ALIGN);
Amir Goldsteind766b552020-03-19 17:10:20 +0200130}
131
Gabriel Krisman Bertazib9928e82021-10-25 16:27:20 -0300132static size_t fanotify_event_len(unsigned int info_mode,
133 struct fanotify_event *event)
Amir Goldstein5e469c82019-01-10 19:04:35 +0200134{
Gabriel Krisman Bertazib9928e82021-10-25 16:27:20 -0300135 size_t event_len = FAN_EVENT_METADATA_LEN;
136 struct fanotify_info *info;
137 int dir_fh_len;
138 int fh_len;
Amir Goldstein929943b2020-07-16 11:42:28 +0300139 int dot_len = 0;
Amir Goldsteinf454fa62020-07-16 11:42:17 +0300140
Gabriel Krisman Bertazib9928e82021-10-25 16:27:20 -0300141 if (!info_mode)
142 return event_len;
143
Gabriel Krisman Bertazi130a3c72021-10-25 16:27:42 -0300144 if (fanotify_is_error_event(event->mask))
145 event_len += FANOTIFY_ERROR_INFO_LEN;
146
Gabriel Krisman Bertazib9928e82021-10-25 16:27:20 -0300147 info = fanotify_event_info(event);
Gabriel Krisman Bertazib9928e82021-10-25 16:27:20 -0300148
Gabriel Krisman Bertazi4bd5a5c2021-10-25 16:27:38 -0300149 if (fanotify_event_has_dir_fh(event)) {
150 dir_fh_len = fanotify_event_dir_fh_len(event);
Gabriel Krisman Bertazib9928e82021-10-25 16:27:20 -0300151 event_len += fanotify_fid_info_len(dir_fh_len, info->name_len);
Matthew Bobrowskid3424c92021-08-08 15:25:32 +1000152 } else if ((info_mode & FAN_REPORT_NAME) &&
153 (event->mask & FAN_ONDIR)) {
Amir Goldstein929943b2020-07-16 11:42:28 +0300154 /*
155 * With group flag FAN_REPORT_NAME, if name was not recorded in
156 * event on a directory, we will report the name ".".
157 */
158 dot_len = 1;
159 }
Jan Karaafc894c2020-03-24 16:55:37 +0100160
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000161 if (info_mode & FAN_REPORT_PIDFD)
Gabriel Krisman Bertazib9928e82021-10-25 16:27:20 -0300162 event_len += FANOTIFY_PIDFD_INFO_HDR_LEN;
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000163
Gabriel Krisman Bertazi4bd5a5c2021-10-25 16:27:38 -0300164 if (fanotify_event_has_object_fh(event)) {
165 fh_len = fanotify_event_object_fh_len(event);
Gabriel Krisman Bertazib9928e82021-10-25 16:27:20 -0300166 event_len += fanotify_fid_info_len(fh_len, dot_len);
Gabriel Krisman Bertazi4bd5a5c2021-10-25 16:27:38 -0300167 }
Amir Goldstein5e469c82019-01-10 19:04:35 +0200168
Gabriel Krisman Bertazib9928e82021-10-25 16:27:20 -0300169 return event_len;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200170}
171
Eric Parisa1014f12009-12-17 21:24:26 -0500172/*
Amir Goldstein94e00d22021-03-04 12:48:25 +0200173 * Remove an hashed event from merge hash table.
174 */
175static void fanotify_unhash_event(struct fsnotify_group *group,
176 struct fanotify_event *event)
177{
178 assert_spin_locked(&group->notification_lock);
179
180 pr_debug("%s: group=%p event=%p bucket=%u\n", __func__,
181 group, event, fanotify_event_hash_bucket(group, event));
182
183 if (WARN_ON_ONCE(hlist_unhashed(&event->merge_list)))
184 return;
185
186 hlist_del_init(&event->merge_list);
187}
188
189/*
Jan Kara7088f352020-03-24 17:04:20 +0100190 * Get an fanotify notification event if one exists and is small
Eric Parisa1014f12009-12-17 21:24:26 -0500191 * enough to fit in "count". Return an error pointer if the count
Jan Kara40873282019-01-08 14:02:44 +0100192 * is not large enough. When permission event is dequeued, its state is
193 * updated accordingly.
Eric Parisa1014f12009-12-17 21:24:26 -0500194 */
Jan Kara7088f352020-03-24 17:04:20 +0100195static struct fanotify_event *get_one_event(struct fsnotify_group *group,
Eric Parisa1014f12009-12-17 21:24:26 -0500196 size_t count)
197{
Gabriel Krisman Bertazib9928e82021-10-25 16:27:20 -0300198 size_t event_size;
Jan Kara7088f352020-03-24 17:04:20 +0100199 struct fanotify_event *event = NULL;
Amir Goldstein6f731712021-03-04 12:48:22 +0200200 struct fsnotify_event *fsn_event;
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000201 unsigned int info_mode = FAN_GROUP_FLAG(group, FANOTIFY_INFO_MODES);
Eric Parisa1014f12009-12-17 21:24:26 -0500202
203 pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
204
Jan Kara8c554462019-01-08 13:52:31 +0100205 spin_lock(&group->notification_lock);
Amir Goldstein6f731712021-03-04 12:48:22 +0200206 fsn_event = fsnotify_peek_first_event(group);
207 if (!fsn_event)
Jan Kara8c554462019-01-08 13:52:31 +0100208 goto out;
Eric Parisa1014f12009-12-17 21:24:26 -0500209
Amir Goldstein6f731712021-03-04 12:48:22 +0200210 event = FANOTIFY_E(fsn_event);
Gabriel Krisman Bertazib9928e82021-10-25 16:27:20 -0300211 event_size = fanotify_event_len(info_mode, event);
Amir Goldstein5e469c82019-01-10 19:04:35 +0200212
Jan Kara8c554462019-01-08 13:52:31 +0100213 if (event_size > count) {
Jan Kara7088f352020-03-24 17:04:20 +0100214 event = ERR_PTR(-EINVAL);
Jan Kara8c554462019-01-08 13:52:31 +0100215 goto out;
216 }
Amir Goldstein6f731712021-03-04 12:48:22 +0200217
218 /*
219 * Held the notification_lock the whole time, so this is the
220 * same event we peeked above.
221 */
222 fsnotify_remove_first_event(group);
Jan Kara7088f352020-03-24 17:04:20 +0100223 if (fanotify_is_perm_event(event->mask))
224 FANOTIFY_PERM(event)->state = FAN_EVENT_REPORTED;
Amir Goldstein94e00d22021-03-04 12:48:25 +0200225 if (fanotify_is_hashed_event(event->mask))
226 fanotify_unhash_event(group, event);
Jan Kara8c554462019-01-08 13:52:31 +0100227out:
228 spin_unlock(&group->notification_lock);
Jan Kara7088f352020-03-24 17:04:20 +0100229 return event;
Eric Parisa1014f12009-12-17 21:24:26 -0500230}
231
Jan Karaa741c2f2020-03-24 15:27:52 +0100232static int create_fd(struct fsnotify_group *group, struct path *path,
Jan Kara7053aee2014-01-21 15:48:14 -0800233 struct file **file)
Eric Parisa1014f12009-12-17 21:24:26 -0500234{
235 int client_fd;
Eric Parisa1014f12009-12-17 21:24:26 -0500236 struct file *new_file;
237
Yann Droneaud0b37e092014-10-09 15:24:40 -0700238 client_fd = get_unused_fd_flags(group->fanotify_data.f_flags);
Eric Parisa1014f12009-12-17 21:24:26 -0500239 if (client_fd < 0)
240 return client_fd;
241
Eric Parisa1014f12009-12-17 21:24:26 -0500242 /*
243 * we need a new file handle for the userspace program so it can read even if it was
244 * originally opened O_WRONLY.
245 */
Jan Karaa741c2f2020-03-24 15:27:52 +0100246 new_file = dentry_open(path,
247 group->fanotify_data.f_flags | FMODE_NONOTIFY,
248 current_cred());
Eric Parisa1014f12009-12-17 21:24:26 -0500249 if (IS_ERR(new_file)) {
250 /*
251 * we still send an event even if we can't open the file. this
252 * can happen when say tasks are gone and we try to open their
253 * /proc files or we try to open a WRONLY file like in sysfs
254 * we just send the errno to userspace since there isn't much
255 * else we can do.
256 */
257 put_unused_fd(client_fd);
258 client_fd = PTR_ERR(new_file);
259 } else {
Al Viro352e3b22012-08-19 12:30:45 -0400260 *file = new_file;
Eric Parisa1014f12009-12-17 21:24:26 -0500261 }
262
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -0500263 return client_fd;
Eric Parisa1014f12009-12-17 21:24:26 -0500264}
265
Jan Kara40873282019-01-08 14:02:44 +0100266/*
267 * Finish processing of permission event by setting it to ANSWERED state and
268 * drop group->notification_lock.
269 */
270static void finish_permission_event(struct fsnotify_group *group,
271 struct fanotify_perm_event *event,
272 unsigned int response)
273 __releases(&group->notification_lock)
274{
Jan Karafabf7f22019-01-08 15:18:02 +0100275 bool destroy = false;
276
Jan Kara40873282019-01-08 14:02:44 +0100277 assert_spin_locked(&group->notification_lock);
278 event->response = response;
Jan Karafabf7f22019-01-08 15:18:02 +0100279 if (event->state == FAN_EVENT_CANCELED)
280 destroy = true;
281 else
282 event->state = FAN_EVENT_ANSWERED;
Jan Kara40873282019-01-08 14:02:44 +0100283 spin_unlock(&group->notification_lock);
Jan Karafabf7f22019-01-08 15:18:02 +0100284 if (destroy)
285 fsnotify_destroy_event(group, &event->fae.fse);
Jan Kara40873282019-01-08 14:02:44 +0100286}
287
Eric Parisb2d87902009-12-17 21:24:34 -0500288static int process_access_response(struct fsnotify_group *group,
289 struct fanotify_response *response_struct)
290{
Amir Goldstein33913992019-01-10 19:04:32 +0200291 struct fanotify_perm_event *event;
Jan Karaf0834412014-04-03 14:46:33 -0700292 int fd = response_struct->fd;
293 int response = response_struct->response;
Eric Parisb2d87902009-12-17 21:24:34 -0500294
295 pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
296 fd, response);
297 /*
298 * make sure the response is valid, if invalid we do nothing and either
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300299 * userspace can send a valid response or we will clean it up after the
Eric Parisb2d87902009-12-17 21:24:34 -0500300 * timeout
301 */
Steve Grubbde8cd832017-10-02 20:21:39 -0400302 switch (response & ~FAN_AUDIT) {
Eric Parisb2d87902009-12-17 21:24:34 -0500303 case FAN_ALLOW:
304 case FAN_DENY:
305 break;
306 default:
307 return -EINVAL;
308 }
309
310 if (fd < 0)
311 return -EINVAL;
312
Amir Goldstein96a71f22018-09-21 21:20:30 +0300313 if ((response & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT))
Steve Grubbde8cd832017-10-02 20:21:39 -0400314 return -EINVAL;
315
Jan Karaaf6a5112019-01-08 13:28:18 +0100316 spin_lock(&group->notification_lock);
317 list_for_each_entry(event, &group->fanotify_data.access_list,
318 fae.fse.list) {
319 if (event->fd != fd)
320 continue;
Eric Parisb2d87902009-12-17 21:24:34 -0500321
Jan Karaaf6a5112019-01-08 13:28:18 +0100322 list_del_init(&event->fae.fse.list);
Jan Kara40873282019-01-08 14:02:44 +0100323 finish_permission_event(group, event, response);
Jan Karaaf6a5112019-01-08 13:28:18 +0100324 wake_up(&group->fanotify_data.access_waitq);
325 return 0;
326 }
327 spin_unlock(&group->notification_lock);
Eric Parisb2d87902009-12-17 21:24:34 -0500328
Jan Karaaf6a5112019-01-08 13:28:18 +0100329 return -ENOENT;
Eric Parisb2d87902009-12-17 21:24:34 -0500330}
Eric Parisb2d87902009-12-17 21:24:34 -0500331
Gabriel Krisman Bertazi130a3c72021-10-25 16:27:42 -0300332static size_t copy_error_info_to_user(struct fanotify_event *event,
333 char __user *buf, int count)
334{
335 struct fanotify_event_info_error info;
336 struct fanotify_error_event *fee = FANOTIFY_EE(event);
337
338 info.hdr.info_type = FAN_EVENT_INFO_TYPE_ERROR;
339 info.hdr.pad = 0;
340 info.hdr.len = FANOTIFY_ERROR_INFO_LEN;
341
342 if (WARN_ON(count < info.hdr.len))
343 return -EFAULT;
344
345 info.error = fee->error;
346 info.error_count = fee->err_count;
347
348 if (copy_to_user(buf, &info, sizeof(info)))
349 return -EFAULT;
350
351 return info.hdr.len;
352}
353
Matthew Bobrowskid3424c92021-08-08 15:25:32 +1000354static int copy_fid_info_to_user(__kernel_fsid_t *fsid, struct fanotify_fh *fh,
355 int info_type, const char *name,
356 size_t name_len,
357 char __user *buf, size_t count)
Amir Goldstein5e469c82019-01-10 19:04:35 +0200358{
359 struct fanotify_event_info_fid info = { };
360 struct file_handle handle = { };
Jan Karaafc894c2020-03-24 16:55:37 +0100361 unsigned char bounce[FANOTIFY_INLINE_FH_LEN], *fh_buf;
Amir Goldsteincacfb952020-03-19 17:10:21 +0200362 size_t fh_len = fh ? fh->len : 0;
Amir Goldstein44d705b2020-03-19 17:10:22 +0200363 size_t info_len = fanotify_fid_info_len(fh_len, name_len);
364 size_t len = info_len;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200365
Amir Goldstein44d705b2020-03-19 17:10:22 +0200366 pr_debug("%s: fh_len=%zu name_len=%zu, info_len=%zu, count=%zu\n",
367 __func__, fh_len, name_len, info_len, count);
368
Amir Goldstein44d705b2020-03-19 17:10:22 +0200369 if (WARN_ON_ONCE(len < sizeof(info) || len > count))
Amir Goldstein5e469c82019-01-10 19:04:35 +0200370 return -EFAULT;
371
Amir Goldstein44d705b2020-03-19 17:10:22 +0200372 /*
373 * Copy event info fid header followed by variable sized file handle
374 * and optionally followed by variable sized filename.
375 */
Amir Goldstein83b7a592020-07-16 11:42:26 +0300376 switch (info_type) {
377 case FAN_EVENT_INFO_TYPE_FID:
378 case FAN_EVENT_INFO_TYPE_DFID:
379 if (WARN_ON_ONCE(name_len))
380 return -EFAULT;
381 break;
382 case FAN_EVENT_INFO_TYPE_DFID_NAME:
383 if (WARN_ON_ONCE(!name || !name_len))
384 return -EFAULT;
385 break;
386 default:
387 return -EFAULT;
388 }
389
390 info.hdr.info_type = info_type;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200391 info.hdr.len = len;
Amir Goldsteind766b552020-03-19 17:10:20 +0200392 info.fsid = *fsid;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200393 if (copy_to_user(buf, &info, sizeof(info)))
394 return -EFAULT;
395
396 buf += sizeof(info);
397 len -= sizeof(info);
Amir Goldstein44d705b2020-03-19 17:10:22 +0200398 if (WARN_ON_ONCE(len < sizeof(handle)))
399 return -EFAULT;
400
Jan Karaafc894c2020-03-24 16:55:37 +0100401 handle.handle_type = fh->type;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200402 handle.handle_bytes = fh_len;
Gabriel Krisman Bertazi936d6a32021-10-25 16:27:41 -0300403
404 /* Mangle handle_type for bad file_handle */
405 if (!fh_len)
406 handle.handle_type = FILEID_INVALID;
407
Amir Goldstein5e469c82019-01-10 19:04:35 +0200408 if (copy_to_user(buf, &handle, sizeof(handle)))
409 return -EFAULT;
410
411 buf += sizeof(handle);
412 len -= sizeof(handle);
Amir Goldstein44d705b2020-03-19 17:10:22 +0200413 if (WARN_ON_ONCE(len < fh_len))
414 return -EFAULT;
415
Jan Karab2d22b62019-03-12 12:42:37 +0100416 /*
Amir Goldstein44d705b2020-03-19 17:10:22 +0200417 * For an inline fh and inline file name, copy through stack to exclude
418 * the copy from usercopy hardening protections.
Jan Karab2d22b62019-03-12 12:42:37 +0100419 */
Jan Karaafc894c2020-03-24 16:55:37 +0100420 fh_buf = fanotify_fh_buf(fh);
Jan Karab2d22b62019-03-12 12:42:37 +0100421 if (fh_len <= FANOTIFY_INLINE_FH_LEN) {
Jan Karaafc894c2020-03-24 16:55:37 +0100422 memcpy(bounce, fh_buf, fh_len);
423 fh_buf = bounce;
Jan Karab2d22b62019-03-12 12:42:37 +0100424 }
Jan Karaafc894c2020-03-24 16:55:37 +0100425 if (copy_to_user(buf, fh_buf, fh_len))
Amir Goldstein5e469c82019-01-10 19:04:35 +0200426 return -EFAULT;
427
Amir Goldstein5e469c82019-01-10 19:04:35 +0200428 buf += fh_len;
429 len -= fh_len;
Amir Goldstein44d705b2020-03-19 17:10:22 +0200430
431 if (name_len) {
432 /* Copy the filename with terminating null */
433 name_len++;
434 if (WARN_ON_ONCE(len < name_len))
435 return -EFAULT;
436
437 if (copy_to_user(buf, name, name_len))
438 return -EFAULT;
439
440 buf += name_len;
441 len -= name_len;
442 }
443
444 /* Pad with 0's */
Amir Goldstein5e469c82019-01-10 19:04:35 +0200445 WARN_ON_ONCE(len < 0 || len >= FANOTIFY_EVENT_ALIGN);
446 if (len > 0 && clear_user(buf, len))
447 return -EFAULT;
448
Amir Goldstein44d705b2020-03-19 17:10:22 +0200449 return info_len;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200450}
451
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000452static int copy_pidfd_info_to_user(int pidfd,
453 char __user *buf,
454 size_t count)
455{
456 struct fanotify_event_info_pidfd info = { };
457 size_t info_len = FANOTIFY_PIDFD_INFO_HDR_LEN;
458
459 if (WARN_ON_ONCE(info_len > count))
460 return -EFAULT;
461
462 info.hdr.info_type = FAN_EVENT_INFO_TYPE_PIDFD;
463 info.hdr.len = info_len;
464 info.pidfd = pidfd;
465
466 if (copy_to_user(buf, &info, info_len))
467 return -EFAULT;
468
469 return info_len;
470}
471
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000472static int copy_info_records_to_user(struct fanotify_event *event,
473 struct fanotify_info *info,
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000474 unsigned int info_mode, int pidfd,
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000475 char __user *buf, size_t count)
476{
477 int ret, total_bytes = 0, info_type = 0;
478 unsigned int fid_mode = info_mode & FANOTIFY_FID_BITS;
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000479 unsigned int pidfd_mode = info_mode & FAN_REPORT_PIDFD;
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000480
481 /*
482 * Event info records order is as follows: dir fid + name, child fid.
483 */
Gabriel Krisman Bertazi4bd5a5c2021-10-25 16:27:38 -0300484 if (fanotify_event_has_dir_fh(event)) {
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000485 info_type = info->name_len ? FAN_EVENT_INFO_TYPE_DFID_NAME :
486 FAN_EVENT_INFO_TYPE_DFID;
487 ret = copy_fid_info_to_user(fanotify_event_fsid(event),
488 fanotify_info_dir_fh(info),
489 info_type,
490 fanotify_info_name(info),
491 info->name_len, buf, count);
492 if (ret < 0)
493 return ret;
494
495 buf += ret;
496 count -= ret;
497 total_bytes += ret;
498 }
499
Gabriel Krisman Bertazi4bd5a5c2021-10-25 16:27:38 -0300500 if (fanotify_event_has_object_fh(event)) {
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000501 const char *dot = NULL;
502 int dot_len = 0;
503
504 if (fid_mode == FAN_REPORT_FID || info_type) {
505 /*
506 * With only group flag FAN_REPORT_FID only type FID is
507 * reported. Second info record type is always FID.
508 */
509 info_type = FAN_EVENT_INFO_TYPE_FID;
510 } else if ((fid_mode & FAN_REPORT_NAME) &&
511 (event->mask & FAN_ONDIR)) {
512 /*
513 * With group flag FAN_REPORT_NAME, if name was not
514 * recorded in an event on a directory, report the name
515 * "." with info type DFID_NAME.
516 */
517 info_type = FAN_EVENT_INFO_TYPE_DFID_NAME;
518 dot = ".";
519 dot_len = 1;
520 } else if ((event->mask & ALL_FSNOTIFY_DIRENT_EVENTS) ||
521 (event->mask & FAN_ONDIR)) {
522 /*
523 * With group flag FAN_REPORT_DIR_FID, a single info
524 * record has type DFID for directory entry modification
525 * event and for event on a directory.
526 */
527 info_type = FAN_EVENT_INFO_TYPE_DFID;
528 } else {
529 /*
530 * With group flags FAN_REPORT_DIR_FID|FAN_REPORT_FID,
531 * a single info record has type FID for event on a
532 * non-directory, when there is no directory to report.
533 * For example, on FAN_DELETE_SELF event.
534 */
535 info_type = FAN_EVENT_INFO_TYPE_FID;
536 }
537
538 ret = copy_fid_info_to_user(fanotify_event_fsid(event),
539 fanotify_event_object_fh(event),
540 info_type, dot, dot_len,
541 buf, count);
542 if (ret < 0)
543 return ret;
544
545 buf += ret;
546 count -= ret;
547 total_bytes += ret;
548 }
549
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000550 if (pidfd_mode) {
551 ret = copy_pidfd_info_to_user(pidfd, buf, count);
552 if (ret < 0)
553 return ret;
554
555 buf += ret;
556 count -= ret;
557 total_bytes += ret;
558 }
559
Gabriel Krisman Bertazi130a3c72021-10-25 16:27:42 -0300560 if (fanotify_is_error_event(event->mask)) {
561 ret = copy_error_info_to_user(event, buf, count);
562 if (ret < 0)
563 return ret;
564 buf += ret;
565 count -= ret;
566 total_bytes += ret;
567 }
568
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000569 return total_bytes;
570}
571
Eric Parisa1014f12009-12-17 21:24:26 -0500572static ssize_t copy_event_to_user(struct fsnotify_group *group,
Jan Kara7088f352020-03-24 17:04:20 +0100573 struct fanotify_event *event,
Kees Cook5b03a472018-12-04 15:44:46 -0800574 char __user *buf, size_t count)
Eric Parisa1014f12009-12-17 21:24:26 -0500575{
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200576 struct fanotify_event_metadata metadata;
Jan Kara7088f352020-03-24 17:04:20 +0100577 struct path *path = fanotify_event_path(event);
Amir Goldsteinf454fa62020-07-16 11:42:17 +0300578 struct fanotify_info *info = fanotify_event_info(event);
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000579 unsigned int info_mode = FAN_GROUP_FLAG(group, FANOTIFY_INFO_MODES);
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000580 unsigned int pidfd_mode = info_mode & FAN_REPORT_PIDFD;
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200581 struct file *f = NULL;
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000582 int ret, pidfd = FAN_NOPIDFD, fd = FAN_NOFD;
Eric Parisa1014f12009-12-17 21:24:26 -0500583
Jan Kara7088f352020-03-24 17:04:20 +0100584 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
Eric Parisa1014f12009-12-17 21:24:26 -0500585
Gabriel Krisman Bertazib9928e82021-10-25 16:27:20 -0300586 metadata.event_len = fanotify_event_len(info_mode, event);
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200587 metadata.metadata_len = FAN_EVENT_METADATA_LEN;
588 metadata.vers = FANOTIFY_METADATA_VERSION;
589 metadata.reserved = 0;
590 metadata.mask = event->mask & FANOTIFY_OUTGOING_EVENTS;
591 metadata.pid = pid_vnr(event->pid);
Amir Goldstein7cea2a32021-03-04 13:29:21 +0200592 /*
593 * For an unprivileged listener, event->pid can be used to identify the
594 * events generated by the listener process itself, without disclosing
595 * the pids of other processes.
596 */
Amir Goldsteina8b98c82021-05-24 16:53:21 +0300597 if (FAN_GROUP_FLAG(group, FANOTIFY_UNPRIV) &&
Amir Goldstein7cea2a32021-03-04 13:29:21 +0200598 task_tgid(current) != event->pid)
599 metadata.pid = 0;
Eric Parisa1014f12009-12-17 21:24:26 -0500600
Amir Goldsteina8b98c82021-05-24 16:53:21 +0300601 /*
602 * For now, fid mode is required for an unprivileged listener and
603 * fid mode does not report fd in events. Keep this check anyway
604 * for safety in case fid mode requirement is relaxed in the future
605 * to allow unprivileged listener to get events with no fd and no fid.
606 */
607 if (!FAN_GROUP_FLAG(group, FANOTIFY_UNPRIV) &&
608 path && path->mnt && path->dentry) {
Jan Karaafc894c2020-03-24 16:55:37 +0100609 fd = create_fd(group, path, &f);
610 if (fd < 0)
611 return fd;
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200612 }
613 metadata.fd = fd;
614
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000615 if (pidfd_mode) {
616 /*
617 * Complain if the FAN_REPORT_PIDFD and FAN_REPORT_TID mutual
618 * exclusion is ever lifted. At the time of incoporating pidfd
619 * support within fanotify, the pidfd API only supported the
620 * creation of pidfds for thread-group leaders.
621 */
622 WARN_ON_ONCE(FAN_GROUP_FLAG(group, FAN_REPORT_TID));
623
624 /*
625 * The PIDTYPE_TGID check for an event->pid is performed
626 * preemptively in an attempt to catch out cases where the event
627 * listener reads events after the event generating process has
628 * already terminated. Report FAN_NOPIDFD to the event listener
629 * in those cases, with all other pidfd creation errors being
630 * reported as FAN_EPIDFD.
631 */
632 if (metadata.pid == 0 ||
633 !pid_has_task(event->pid, PIDTYPE_TGID)) {
634 pidfd = FAN_NOPIDFD;
635 } else {
636 pidfd = pidfd_create(event->pid, 0);
637 if (pidfd < 0)
638 pidfd = FAN_EPIDFD;
639 }
640 }
641
Al Viro352e3b22012-08-19 12:30:45 -0400642 ret = -EFAULT;
Kees Cook5b03a472018-12-04 15:44:46 -0800643 /*
644 * Sanity check copy size in case get_one_event() and
Fabian Frederickc5e443c2020-05-12 20:18:36 +0200645 * event_len sizes ever get out of sync.
Kees Cook5b03a472018-12-04 15:44:46 -0800646 */
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200647 if (WARN_ON_ONCE(metadata.event_len > count))
Al Viro352e3b22012-08-19 12:30:45 -0400648 goto out_close_fd;
649
Amir Goldstein5e469c82019-01-10 19:04:35 +0200650 if (copy_to_user(buf, &metadata, FAN_EVENT_METADATA_LEN))
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200651 goto out_close_fd;
652
Amir Goldstein44d705b2020-03-19 17:10:22 +0200653 buf += FAN_EVENT_METADATA_LEN;
654 count -= FAN_EVENT_METADATA_LEN;
655
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200656 if (fanotify_is_perm_event(event->mask))
Jan Kara7088f352020-03-24 17:04:20 +0100657 FANOTIFY_PERM(event)->fd = fd;
Eric Parisb2d87902009-12-17 21:24:34 -0500658
Amir Goldstein44d705b2020-03-19 17:10:22 +0200659 if (f)
Al Viro3587b1b2012-11-18 19:19:00 +0000660 fd_install(fd, f);
Amir Goldstein44d705b2020-03-19 17:10:22 +0200661
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000662 if (info_mode) {
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000663 ret = copy_info_records_to_user(event, info, info_mode, pidfd,
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000664 buf, count);
Amir Goldstein5e469c82019-01-10 19:04:35 +0200665 if (ret < 0)
Matthew Bobrowskif644bc42021-06-11 13:32:06 +1000666 goto out_close_fd;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200667 }
668
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200669 return metadata.event_len;
Eric Parisb2d87902009-12-17 21:24:34 -0500670
Eric Parisb2d87902009-12-17 21:24:34 -0500671out_close_fd:
Al Viro352e3b22012-08-19 12:30:45 -0400672 if (fd != FAN_NOFD) {
673 put_unused_fd(fd);
674 fput(f);
675 }
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000676
677 if (pidfd >= 0)
678 close_fd(pidfd);
679
Eric Parisb2d87902009-12-17 21:24:34 -0500680 return ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500681}
682
683/* intofiy userspace file descriptor functions */
Al Viro076ccb72017-07-03 01:02:18 -0400684static __poll_t fanotify_poll(struct file *file, poll_table *wait)
Eric Parisa1014f12009-12-17 21:24:26 -0500685{
686 struct fsnotify_group *group = file->private_data;
Al Viro076ccb72017-07-03 01:02:18 -0400687 __poll_t ret = 0;
Eric Parisa1014f12009-12-17 21:24:26 -0500688
689 poll_wait(file, &group->notification_waitq, wait);
Jan Karac21dbe22016-10-07 16:56:52 -0700690 spin_lock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500691 if (!fsnotify_notify_queue_is_empty(group))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800692 ret = EPOLLIN | EPOLLRDNORM;
Jan Karac21dbe22016-10-07 16:56:52 -0700693 spin_unlock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500694
695 return ret;
696}
697
698static ssize_t fanotify_read(struct file *file, char __user *buf,
699 size_t count, loff_t *pos)
700{
701 struct fsnotify_group *group;
Jan Kara7088f352020-03-24 17:04:20 +0100702 struct fanotify_event *event;
Eric Parisa1014f12009-12-17 21:24:26 -0500703 char __user *start;
704 int ret;
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100705 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Eric Parisa1014f12009-12-17 21:24:26 -0500706
707 start = buf;
708 group = file->private_data;
709
710 pr_debug("%s: group=%p\n", __func__, group);
711
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100712 add_wait_queue(&group->notification_waitq, &wait);
Eric Parisa1014f12009-12-17 21:24:26 -0500713 while (1) {
Jan Kara47aaabd2020-07-15 14:06:21 +0200714 /*
715 * User can supply arbitrarily large buffer. Avoid softlockups
716 * in case there are lots of available events.
717 */
718 cond_resched();
Jan Kara7088f352020-03-24 17:04:20 +0100719 event = get_one_event(group, count);
720 if (IS_ERR(event)) {
721 ret = PTR_ERR(event);
Jan Karad8aaab42014-04-03 14:46:35 -0700722 break;
723 }
724
Jan Kara7088f352020-03-24 17:04:20 +0100725 if (!event) {
Jan Karad8aaab42014-04-03 14:46:35 -0700726 ret = -EAGAIN;
727 if (file->f_flags & O_NONBLOCK)
Eric Parisa1014f12009-12-17 21:24:26 -0500728 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700729
730 ret = -ERESTARTSYS;
731 if (signal_pending(current))
Eric Parisa1014f12009-12-17 21:24:26 -0500732 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700733
734 if (start != buf)
735 break;
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100736
737 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Eric Parisa1014f12009-12-17 21:24:26 -0500738 continue;
739 }
740
Jan Kara7088f352020-03-24 17:04:20 +0100741 ret = copy_event_to_user(group, event, buf, count);
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300742 if (unlikely(ret == -EOPENSTALE)) {
743 /*
744 * We cannot report events with stale fd so drop it.
745 * Setting ret to 0 will continue the event loop and
746 * do the right thing if there are no more events to
747 * read (i.e. return bytes read, -EAGAIN or wait).
748 */
749 ret = 0;
750 }
751
Jan Karad8aaab42014-04-03 14:46:35 -0700752 /*
753 * Permission events get queued to wait for response. Other
754 * events can be destroyed now.
755 */
Jan Kara7088f352020-03-24 17:04:20 +0100756 if (!fanotify_is_perm_event(event->mask)) {
757 fsnotify_destroy_event(group, &event->fse);
Jan Karad5078162014-04-03 14:46:36 -0700758 } else {
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300759 if (ret <= 0) {
Jan Kara40873282019-01-08 14:02:44 +0100760 spin_lock(&group->notification_lock);
761 finish_permission_event(group,
Jan Kara7088f352020-03-24 17:04:20 +0100762 FANOTIFY_PERM(event), FAN_DENY);
Jan Karad5078162014-04-03 14:46:36 -0700763 wake_up(&group->fanotify_data.access_waitq);
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300764 } else {
765 spin_lock(&group->notification_lock);
Jan Kara7088f352020-03-24 17:04:20 +0100766 list_add_tail(&event->fse.list,
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300767 &group->fanotify_data.access_list);
768 spin_unlock(&group->notification_lock);
Jan Karad5078162014-04-03 14:46:36 -0700769 }
Jan Karad5078162014-04-03 14:46:36 -0700770 }
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300771 if (ret < 0)
772 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700773 buf += ret;
774 count -= ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500775 }
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100776 remove_wait_queue(&group->notification_waitq, &wait);
Eric Parisa1014f12009-12-17 21:24:26 -0500777
Eric Parisa1014f12009-12-17 21:24:26 -0500778 if (start != buf && ret != -EFAULT)
779 ret = buf - start;
780 return ret;
781}
782
Eric Parisb2d87902009-12-17 21:24:34 -0500783static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
784{
Eric Parisb2d87902009-12-17 21:24:34 -0500785 struct fanotify_response response = { .fd = -1, .response = -1 };
786 struct fsnotify_group *group;
787 int ret;
788
Miklos Szeredi6685df32017-10-30 21:14:56 +0100789 if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
790 return -EINVAL;
791
Eric Parisb2d87902009-12-17 21:24:34 -0500792 group = file->private_data;
793
Fabian Frederick5e236632020-05-12 20:19:21 +0200794 if (count < sizeof(response))
795 return -EINVAL;
796
797 count = sizeof(response);
Eric Parisb2d87902009-12-17 21:24:34 -0500798
799 pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
800
801 if (copy_from_user(&response, buf, count))
802 return -EFAULT;
803
804 ret = process_access_response(group, &response);
805 if (ret < 0)
806 count = ret;
807
808 return count;
Eric Parisb2d87902009-12-17 21:24:34 -0500809}
810
Eric Paris52c923d2009-12-17 21:24:26 -0500811static int fanotify_release(struct inode *ignored, struct file *file)
812{
813 struct fsnotify_group *group = file->private_data;
Amir Goldstein6f731712021-03-04 12:48:22 +0200814 struct fsnotify_event *fsn_event;
Andrew Morton19ba54f2010-10-28 17:21:59 -0400815
Jan Kara5838d442014-08-06 16:03:28 -0700816 /*
Jan Kara96d41012016-09-19 14:44:30 -0700817 * Stop new events from arriving in the notification queue. since
818 * userspace cannot use fanotify fd anymore, no event can enter or
819 * leave access_list by now either.
820 */
821 fsnotify_group_stop_queueing(group);
822
823 /*
824 * Process all permission events on access_list and notification queue
825 * and simulate reply from userspace.
Jan Kara5838d442014-08-06 16:03:28 -0700826 */
Jan Kara073f6552016-10-07 16:56:55 -0700827 spin_lock(&group->notification_lock);
Jan Karaca6f8692019-01-09 13:21:01 +0100828 while (!list_empty(&group->fanotify_data.access_list)) {
Jan Kara7088f352020-03-24 17:04:20 +0100829 struct fanotify_perm_event *event;
830
Jan Karaca6f8692019-01-09 13:21:01 +0100831 event = list_first_entry(&group->fanotify_data.access_list,
832 struct fanotify_perm_event, fae.fse.list);
Jan Karaf0834412014-04-03 14:46:33 -0700833 list_del_init(&event->fae.fse.list);
Jan Kara40873282019-01-08 14:02:44 +0100834 finish_permission_event(group, event, FAN_ALLOW);
835 spin_lock(&group->notification_lock);
Eric Paris2eebf582010-08-18 12:25:50 -0400836 }
Eric Paris2eebf582010-08-18 12:25:50 -0400837
Jan Kara5838d442014-08-06 16:03:28 -0700838 /*
Jan Kara96d41012016-09-19 14:44:30 -0700839 * Destroy all non-permission events. For permission events just
840 * dequeue them and set the response. They will be freed once the
841 * response is consumed and fanotify_get_response() returns.
Jan Kara5838d442014-08-06 16:03:28 -0700842 */
Amir Goldstein6f731712021-03-04 12:48:22 +0200843 while ((fsn_event = fsnotify_remove_first_event(group))) {
844 struct fanotify_event *event = FANOTIFY_E(fsn_event);
Jan Kara7088f352020-03-24 17:04:20 +0100845
Jan Kara7088f352020-03-24 17:04:20 +0100846 if (!(event->mask & FANOTIFY_PERM_EVENTS)) {
Jan Karac21dbe22016-10-07 16:56:52 -0700847 spin_unlock(&group->notification_lock);
Amir Goldstein6f731712021-03-04 12:48:22 +0200848 fsnotify_destroy_event(group, fsn_event);
Miklos Szeredi6685df32017-10-30 21:14:56 +0100849 } else {
Jan Kara7088f352020-03-24 17:04:20 +0100850 finish_permission_event(group, FANOTIFY_PERM(event),
Jan Kara40873282019-01-08 14:02:44 +0100851 FAN_ALLOW);
Miklos Szeredi6685df32017-10-30 21:14:56 +0100852 }
Jan Kara40873282019-01-08 14:02:44 +0100853 spin_lock(&group->notification_lock);
Jan Kara96d41012016-09-19 14:44:30 -0700854 }
Jan Karac21dbe22016-10-07 16:56:52 -0700855 spin_unlock(&group->notification_lock);
Jan Kara96d41012016-09-19 14:44:30 -0700856
857 /* Response for all permission events it set, wakeup waiters */
Eric Paris2eebf582010-08-18 12:25:50 -0400858 wake_up(&group->fanotify_data.access_waitq);
Eric Paris0a6b6bd2011-10-14 17:43:39 -0400859
Eric Paris52c923d2009-12-17 21:24:26 -0500860 /* matches the fanotify_init->fsnotify_alloc_group */
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200861 fsnotify_destroy_group(group);
Eric Paris52c923d2009-12-17 21:24:26 -0500862
863 return 0;
864}
865
Eric Parisa1014f12009-12-17 21:24:26 -0500866static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
867{
868 struct fsnotify_group *group;
Jan Kara7053aee2014-01-21 15:48:14 -0800869 struct fsnotify_event *fsn_event;
Eric Parisa1014f12009-12-17 21:24:26 -0500870 void __user *p;
871 int ret = -ENOTTY;
872 size_t send_len = 0;
873
874 group = file->private_data;
875
876 p = (void __user *) arg;
877
878 switch (cmd) {
879 case FIONREAD:
Jan Karac21dbe22016-10-07 16:56:52 -0700880 spin_lock(&group->notification_lock);
Jan Kara7053aee2014-01-21 15:48:14 -0800881 list_for_each_entry(fsn_event, &group->notification_list, list)
Eric Parisa1014f12009-12-17 21:24:26 -0500882 send_len += FAN_EVENT_METADATA_LEN;
Jan Karac21dbe22016-10-07 16:56:52 -0700883 spin_unlock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500884 ret = put_user(send_len, (int __user *) p);
885 break;
886 }
887
888 return ret;
889}
890
Eric Paris52c923d2009-12-17 21:24:26 -0500891static const struct file_operations fanotify_fops = {
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800892 .show_fdinfo = fanotify_show_fdinfo,
Eric Parisa1014f12009-12-17 21:24:26 -0500893 .poll = fanotify_poll,
894 .read = fanotify_read,
Eric Parisb2d87902009-12-17 21:24:34 -0500895 .write = fanotify_write,
Eric Paris52c923d2009-12-17 21:24:26 -0500896 .fasync = NULL,
897 .release = fanotify_release,
Eric Parisa1014f12009-12-17 21:24:26 -0500898 .unlocked_ioctl = fanotify_ioctl,
Arnd Bergmann1832f2d2018-09-11 21:59:08 +0200899 .compat_ioctl = compat_ptr_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200900 .llseek = noop_llseek,
Eric Paris52c923d2009-12-17 21:24:26 -0500901};
902
Eric Paris2a3edf82009-12-17 21:24:26 -0500903static int fanotify_find_path(int dfd, const char __user *filename,
Aaron Goidelac5656d2019-08-12 11:20:00 -0400904 struct path *path, unsigned int flags, __u64 mask,
905 unsigned int obj_type)
Eric Paris2a3edf82009-12-17 21:24:26 -0500906{
907 int ret;
908
909 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
910 dfd, filename, flags);
911
912 if (filename == NULL) {
Al Viro2903ff02012-08-28 12:52:22 -0400913 struct fd f = fdget(dfd);
Eric Paris2a3edf82009-12-17 21:24:26 -0500914
915 ret = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -0400916 if (!f.file)
Eric Paris2a3edf82009-12-17 21:24:26 -0500917 goto out;
918
919 ret = -ENOTDIR;
920 if ((flags & FAN_MARK_ONLYDIR) &&
Al Viro496ad9a2013-01-23 17:07:38 -0500921 !(S_ISDIR(file_inode(f.file)->i_mode))) {
Al Viro2903ff02012-08-28 12:52:22 -0400922 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500923 goto out;
924 }
925
Al Viro2903ff02012-08-28 12:52:22 -0400926 *path = f.file->f_path;
Eric Paris2a3edf82009-12-17 21:24:26 -0500927 path_get(path);
Al Viro2903ff02012-08-28 12:52:22 -0400928 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500929 } else {
930 unsigned int lookup_flags = 0;
931
932 if (!(flags & FAN_MARK_DONT_FOLLOW))
933 lookup_flags |= LOOKUP_FOLLOW;
934 if (flags & FAN_MARK_ONLYDIR)
935 lookup_flags |= LOOKUP_DIRECTORY;
936
937 ret = user_path_at(dfd, filename, lookup_flags, path);
938 if (ret)
939 goto out;
940 }
941
942 /* you can only watch an inode if you have read permissions on it */
Christian Brauner02f92b32021-01-21 14:19:22 +0100943 ret = path_permission(path, MAY_READ);
Aaron Goidelac5656d2019-08-12 11:20:00 -0400944 if (ret) {
945 path_put(path);
946 goto out;
947 }
948
949 ret = security_path_notify(path, mask, obj_type);
Eric Paris2a3edf82009-12-17 21:24:26 -0500950 if (ret)
951 path_put(path);
Aaron Goidelac5656d2019-08-12 11:20:00 -0400952
Eric Paris2a3edf82009-12-17 21:24:26 -0500953out:
954 return ret;
955}
956
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500957static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
Amir Goldstein4ed68142020-07-16 11:42:14 +0300958 __u32 mask, unsigned int flags,
959 __u32 umask, int *destroy)
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500960{
Lino Sanfilippod2c18742015-02-10 14:08:24 -0800961 __u32 oldmask = 0;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500962
Amir Goldstein4ed68142020-07-16 11:42:14 +0300963 /* umask bits cannot be removed by user */
964 mask &= ~umask;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500965 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500966 if (!(flags & FAN_MARK_IGNORED_MASK)) {
967 oldmask = fsn_mark->mask;
Amir Goldsteina72fd222018-10-04 00:25:34 +0300968 fsn_mark->mask &= ~mask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500969 } else {
Amir Goldsteina72fd222018-10-04 00:25:34 +0300970 fsn_mark->ignored_mask &= ~mask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500971 }
Amir Goldstein4ed68142020-07-16 11:42:14 +0300972 /*
973 * We need to keep the mark around even if remaining mask cannot
974 * result in any events (e.g. mask == FAN_ONDIR) to support incremenal
975 * changes to the mask.
976 * Destroy mark when only umask bits remain.
977 */
978 *destroy = !((fsn_mark->mask | fsn_mark->ignored_mask) & ~umask);
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500979 spin_unlock(&fsn_mark->lock);
980
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500981 return mask & oldmask;
982}
983
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300984static int fanotify_remove_mark(struct fsnotify_group *group,
985 fsnotify_connp_t *connp, __u32 mask,
Amir Goldstein4ed68142020-07-16 11:42:14 +0300986 unsigned int flags, __u32 umask)
Eric Paris88826272009-12-17 21:24:28 -0500987{
988 struct fsnotify_mark *fsn_mark = NULL;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500989 __u32 removed;
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200990 int destroy_mark;
Eric Paris88826272009-12-17 21:24:28 -0500991
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700992 mutex_lock(&group->mark_mutex);
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300993 fsn_mark = fsnotify_find_mark(connp, group);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700994 if (!fsn_mark) {
995 mutex_unlock(&group->mark_mutex);
Eric Paris88826272009-12-17 21:24:28 -0500996 return -ENOENT;
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700997 }
Eric Paris88826272009-12-17 21:24:28 -0500998
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200999 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
Amir Goldstein4ed68142020-07-16 11:42:14 +03001000 umask, &destroy_mark);
Amir Goldstein3ac70bf2018-06-23 17:54:50 +03001001 if (removed & fsnotify_conn_mask(fsn_mark->connector))
1002 fsnotify_recalc_mask(fsn_mark->connector);
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +02001003 if (destroy_mark)
Jan Kara4712e7222015-09-04 15:43:12 -07001004 fsnotify_detach_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -07001005 mutex_unlock(&group->mark_mutex);
Jan Kara4712e7222015-09-04 15:43:12 -07001006 if (destroy_mark)
1007 fsnotify_free_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -07001008
Jan Karab1362ed2016-12-21 16:28:45 +01001009 /* matches the fsnotify_find_mark() */
Eric Paris2a3edf82009-12-17 21:24:26 -05001010 fsnotify_put_mark(fsn_mark);
Eric Paris2a3edf82009-12-17 21:24:26 -05001011 return 0;
1012}
1013
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001014static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
1015 struct vfsmount *mnt, __u32 mask,
Amir Goldstein4ed68142020-07-16 11:42:14 +03001016 unsigned int flags, __u32 umask)
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001017{
1018 return fanotify_remove_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
Amir Goldstein4ed68142020-07-16 11:42:14 +03001019 mask, flags, umask);
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001020}
1021
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001022static int fanotify_remove_sb_mark(struct fsnotify_group *group,
Amir Goldstein4ed68142020-07-16 11:42:14 +03001023 struct super_block *sb, __u32 mask,
1024 unsigned int flags, __u32 umask)
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001025{
Amir Goldstein4ed68142020-07-16 11:42:14 +03001026 return fanotify_remove_mark(group, &sb->s_fsnotify_marks, mask,
1027 flags, umask);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001028}
1029
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001030static int fanotify_remove_inode_mark(struct fsnotify_group *group,
1031 struct inode *inode, __u32 mask,
Amir Goldstein4ed68142020-07-16 11:42:14 +03001032 unsigned int flags, __u32 umask)
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001033{
1034 return fanotify_remove_mark(group, &inode->i_fsnotify_marks, mask,
Amir Goldstein4ed68142020-07-16 11:42:14 +03001035 flags, umask);
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001036}
1037
Eric Parisb9e4e3b2009-12-17 21:24:33 -05001038static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
1039 __u32 mask,
1040 unsigned int flags)
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -05001041{
Eric Paris192ca4d2010-10-28 17:21:59 -04001042 __u32 oldmask = -1;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -05001043
1044 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -05001045 if (!(flags & FAN_MARK_IGNORED_MASK)) {
1046 oldmask = fsn_mark->mask;
Amir Goldsteina72fd222018-10-04 00:25:34 +03001047 fsn_mark->mask |= mask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -05001048 } else {
Amir Goldsteina72fd222018-10-04 00:25:34 +03001049 fsn_mark->ignored_mask |= mask;
Eric Parisc9778a92009-12-17 21:24:33 -05001050 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
1051 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
Eric Parisb9e4e3b2009-12-17 21:24:33 -05001052 }
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -05001053 spin_unlock(&fsn_mark->lock);
1054
1055 return mask & ~oldmask;
1056}
1057
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001058static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
Amir Goldsteinb812a9f2018-06-23 17:54:48 +03001059 fsnotify_connp_t *connp,
Amir Goldsteinad69cd92021-11-29 22:15:27 +02001060 unsigned int obj_type,
Amir Goldstein77115222019-01-10 19:04:37 +02001061 __kernel_fsid_t *fsid)
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001062{
Amir Goldstein5b8fea62021-03-04 13:29:20 +02001063 struct ucounts *ucounts = group->fanotify_data.ucounts;
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001064 struct fsnotify_mark *mark;
1065 int ret;
1066
Amir Goldstein5b8fea62021-03-04 13:29:20 +02001067 /*
1068 * Enforce per user marks limits per user in all containing user ns.
1069 * A group with FAN_UNLIMITED_MARKS does not contribute to mark count
1070 * in the limited groups account.
1071 */
1072 if (!FAN_GROUP_FLAG(group, FAN_UNLIMITED_MARKS) &&
1073 !inc_ucount(ucounts->ns, ucounts->uid, UCOUNT_FANOTIFY_MARKS))
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001074 return ERR_PTR(-ENOSPC);
1075
1076 mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
Amir Goldstein5b8fea62021-03-04 13:29:20 +02001077 if (!mark) {
1078 ret = -ENOMEM;
1079 goto out_dec_ucounts;
1080 }
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001081
Jan Kara054c6362016-12-21 18:06:12 +01001082 fsnotify_init_mark(mark, group);
Amir Goldsteinad69cd92021-11-29 22:15:27 +02001083 ret = fsnotify_add_mark_locked(mark, connp, obj_type, 0, fsid);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001084 if (ret) {
1085 fsnotify_put_mark(mark);
Amir Goldstein5b8fea62021-03-04 13:29:20 +02001086 goto out_dec_ucounts;
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001087 }
1088
1089 return mark;
Amir Goldstein5b8fea62021-03-04 13:29:20 +02001090
1091out_dec_ucounts:
1092 if (!FAN_GROUP_FLAG(group, FAN_UNLIMITED_MARKS))
1093 dec_ucount(ucounts, UCOUNT_FANOTIFY_MARKS);
1094 return ERR_PTR(ret);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001095}
1096
Gabriel Krisman Bertazi734a1a52021-10-25 16:27:34 -03001097static int fanotify_group_init_error_pool(struct fsnotify_group *group)
1098{
1099 if (mempool_initialized(&group->fanotify_data.error_events_pool))
1100 return 0;
1101
1102 return mempool_init_kmalloc_pool(&group->fanotify_data.error_events_pool,
1103 FANOTIFY_DEFAULT_FEE_POOL_SIZE,
1104 sizeof(struct fanotify_error_event));
1105}
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001106
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001107static int fanotify_add_mark(struct fsnotify_group *group,
Amir Goldsteinad69cd92021-11-29 22:15:27 +02001108 fsnotify_connp_t *connp, unsigned int obj_type,
Amir Goldstein77115222019-01-10 19:04:37 +02001109 __u32 mask, unsigned int flags,
1110 __kernel_fsid_t *fsid)
Eric Paris2a3edf82009-12-17 21:24:26 -05001111{
1112 struct fsnotify_mark *fsn_mark;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -05001113 __u32 added;
Gabriel Krisman Bertazi734a1a52021-10-25 16:27:34 -03001114 int ret = 0;
Eric Paris2a3edf82009-12-17 21:24:26 -05001115
Lino Sanfilippo7b185272013-07-08 15:59:42 -07001116 mutex_lock(&group->mark_mutex);
Amir Goldsteinb812a9f2018-06-23 17:54:48 +03001117 fsn_mark = fsnotify_find_mark(connp, group);
Eric Paris88826272009-12-17 21:24:28 -05001118 if (!fsn_mark) {
Amir Goldsteinad69cd92021-11-29 22:15:27 +02001119 fsn_mark = fanotify_add_new_mark(group, connp, obj_type, fsid);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001120 if (IS_ERR(fsn_mark)) {
Lino Sanfilippo7b185272013-07-08 15:59:42 -07001121 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001122 return PTR_ERR(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -07001123 }
Eric Paris88826272009-12-17 21:24:28 -05001124 }
Gabriel Krisman Bertazi734a1a52021-10-25 16:27:34 -03001125
1126 /*
1127 * Error events are pre-allocated per group, only if strictly
1128 * needed (i.e. FAN_FS_ERROR was requested).
1129 */
1130 if (!(flags & FAN_MARK_IGNORED_MASK) && (mask & FAN_FS_ERROR)) {
1131 ret = fanotify_group_init_error_pool(group);
1132 if (ret)
1133 goto out;
1134 }
1135
Eric Parisb9e4e3b2009-12-17 21:24:33 -05001136 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
Amir Goldstein3ac70bf2018-06-23 17:54:50 +03001137 if (added & ~fsnotify_conn_mask(fsn_mark->connector))
1138 fsnotify_recalc_mask(fsn_mark->connector);
Gabriel Krisman Bertazi734a1a52021-10-25 16:27:34 -03001139
1140out:
Jan Karac9747642016-12-14 13:53:46 +01001141 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001142
Lino Sanfilippofa218ab2010-11-09 18:18:16 +01001143 fsnotify_put_mark(fsn_mark);
Gabriel Krisman Bertazi734a1a52021-10-25 16:27:34 -03001144 return ret;
Eric Paris88826272009-12-17 21:24:28 -05001145}
1146
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001147static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
1148 struct vfsmount *mnt, __u32 mask,
Amir Goldstein77115222019-01-10 19:04:37 +02001149 unsigned int flags, __kernel_fsid_t *fsid)
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001150{
1151 return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
Amir Goldstein77115222019-01-10 19:04:37 +02001152 FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags, fsid);
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001153}
1154
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001155static int fanotify_add_sb_mark(struct fsnotify_group *group,
Amir Goldstein77115222019-01-10 19:04:37 +02001156 struct super_block *sb, __u32 mask,
1157 unsigned int flags, __kernel_fsid_t *fsid)
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001158{
1159 return fanotify_add_mark(group, &sb->s_fsnotify_marks,
Amir Goldstein77115222019-01-10 19:04:37 +02001160 FSNOTIFY_OBJ_TYPE_SB, mask, flags, fsid);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001161}
1162
Andreas Gruenbacher52202df2009-12-17 21:24:28 -05001163static int fanotify_add_inode_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -05001164 struct inode *inode, __u32 mask,
Amir Goldstein77115222019-01-10 19:04:37 +02001165 unsigned int flags, __kernel_fsid_t *fsid)
Eric Paris88826272009-12-17 21:24:28 -05001166{
Eric Paris88826272009-12-17 21:24:28 -05001167 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
Eric Paris2a3edf82009-12-17 21:24:26 -05001168
Eric Paris5322a592010-10-28 17:21:57 -04001169 /*
1170 * If some other task has this inode open for write we should not add
1171 * an ignored mark, unless that ignored mark is supposed to survive
1172 * modification changes anyway.
1173 */
1174 if ((flags & FAN_MARK_IGNORED_MASK) &&
1175 !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
Nikolay Borisovac9498d2018-12-11 10:27:23 +02001176 inode_is_open_for_write(inode))
Eric Paris5322a592010-10-28 17:21:57 -04001177 return 0;
1178
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001179 return fanotify_add_mark(group, &inode->i_fsnotify_marks,
Amir Goldstein77115222019-01-10 19:04:37 +02001180 FSNOTIFY_OBJ_TYPE_INODE, mask, flags, fsid);
Eric Paris88826272009-12-17 21:24:28 -05001181}
Eric Paris2a3edf82009-12-17 21:24:26 -05001182
Amir Goldsteinb8a6c3a2020-07-08 14:11:42 +03001183static struct fsnotify_event *fanotify_alloc_overflow_event(void)
1184{
1185 struct fanotify_event *oevent;
1186
1187 oevent = kmalloc(sizeof(*oevent), GFP_KERNEL_ACCOUNT);
1188 if (!oevent)
1189 return NULL;
1190
1191 fanotify_init_event(oevent, 0, FS_Q_OVERFLOW);
1192 oevent->type = FANOTIFY_EVENT_TYPE_OVERFLOW;
1193
1194 return &oevent->fse;
1195}
1196
Amir Goldstein94e00d22021-03-04 12:48:25 +02001197static struct hlist_head *fanotify_alloc_merge_hash(void)
1198{
1199 struct hlist_head *hash;
1200
1201 hash = kmalloc(sizeof(struct hlist_head) << FANOTIFY_HTABLE_BITS,
1202 GFP_KERNEL_ACCOUNT);
1203 if (!hash)
1204 return NULL;
1205
1206 __hash_init(hash, FANOTIFY_HTABLE_SIZE);
1207
1208 return hash;
1209}
1210
Eric Paris52c923d2009-12-17 21:24:26 -05001211/* fanotify syscalls */
Eric Paris08ae8932010-05-27 09:41:40 -04001212SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
Eric Paris11637e42009-12-17 21:24:25 -05001213{
Eric Paris52c923d2009-12-17 21:24:26 -05001214 struct fsnotify_group *group;
1215 int f_flags, fd;
Amir Goldstein83b7a592020-07-16 11:42:26 +03001216 unsigned int fid_mode = flags & FANOTIFY_FID_BITS;
1217 unsigned int class = flags & FANOTIFY_CLASS_BITS;
Amir Goldsteina8b98c82021-05-24 16:53:21 +03001218 unsigned int internal_flags = 0;
Eric Paris52c923d2009-12-17 21:24:26 -05001219
Amir Goldstein96a71f22018-09-21 21:20:30 +03001220 pr_debug("%s: flags=%x event_f_flags=%x\n",
1221 __func__, flags, event_f_flags);
Eric Paris52c923d2009-12-17 21:24:26 -05001222
Amir Goldstein7cea2a32021-03-04 13:29:21 +02001223 if (!capable(CAP_SYS_ADMIN)) {
1224 /*
1225 * An unprivileged user can setup an fanotify group with
1226 * limited functionality - an unprivileged group is limited to
1227 * notification events with file handles and it cannot use
1228 * unlimited queue/marks.
1229 */
1230 if ((flags & FANOTIFY_ADMIN_INIT_FLAGS) || !fid_mode)
1231 return -EPERM;
Amir Goldsteina8b98c82021-05-24 16:53:21 +03001232
1233 /*
1234 * Setting the internal flag FANOTIFY_UNPRIV on the group
1235 * prevents setting mount/filesystem marks on this group and
1236 * prevents reporting pid and open fd in events.
1237 */
1238 internal_flags |= FANOTIFY_UNPRIV;
Amir Goldstein7cea2a32021-03-04 13:29:21 +02001239 }
Eric Paris52c923d2009-12-17 21:24:26 -05001240
Steve Grubbde8cd832017-10-02 20:21:39 -04001241#ifdef CONFIG_AUDITSYSCALL
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001242 if (flags & ~(FANOTIFY_INIT_FLAGS | FAN_ENABLE_AUDIT))
Steve Grubbde8cd832017-10-02 20:21:39 -04001243#else
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001244 if (flags & ~FANOTIFY_INIT_FLAGS)
Steve Grubbde8cd832017-10-02 20:21:39 -04001245#endif
Eric Paris52c923d2009-12-17 21:24:26 -05001246 return -EINVAL;
1247
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +10001248 /*
1249 * A pidfd can only be returned for a thread-group leader; thus
1250 * FAN_REPORT_PIDFD and FAN_REPORT_TID need to remain mutually
1251 * exclusive.
1252 */
1253 if ((flags & FAN_REPORT_PIDFD) && (flags & FAN_REPORT_TID))
1254 return -EINVAL;
1255
Heinrich Schuchardt48149e92014-06-04 16:05:44 -07001256 if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS)
1257 return -EINVAL;
1258
1259 switch (event_f_flags & O_ACCMODE) {
1260 case O_RDONLY:
1261 case O_RDWR:
1262 case O_WRONLY:
1263 break;
1264 default:
1265 return -EINVAL;
1266 }
1267
Amir Goldstein83b7a592020-07-16 11:42:26 +03001268 if (fid_mode && class != FAN_CLASS_NOTIF)
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001269 return -EINVAL;
1270
Amir Goldstein929943b2020-07-16 11:42:28 +03001271 /*
Amir Goldstein929943b2020-07-16 11:42:28 +03001272 * Child name is reported with parent fid so requires dir fid.
Amir Goldstein691d9762020-07-16 11:42:30 +03001273 * We can report both child fid and dir fid with or without name.
Amir Goldstein929943b2020-07-16 11:42:28 +03001274 */
Amir Goldstein691d9762020-07-16 11:42:30 +03001275 if ((fid_mode & FAN_REPORT_NAME) && !(fid_mode & FAN_REPORT_DIR_FID))
Amir Goldstein83b7a592020-07-16 11:42:26 +03001276 return -EINVAL;
Amir Goldstein83b7a592020-07-16 11:42:26 +03001277
Amir Goldsteind61fd652021-11-29 22:15:29 +02001278 /*
1279 * FAN_REPORT_TARGET_FID requires FAN_REPORT_NAME and FAN_REPORT_FID
1280 * and is used as an indication to report both dir and child fid on all
1281 * dirent events.
1282 */
1283 if ((fid_mode & FAN_REPORT_TARGET_FID) &&
1284 (!(fid_mode & FAN_REPORT_NAME) || !(fid_mode & FAN_REPORT_FID)))
1285 return -EINVAL;
1286
Eric Parisb2d87902009-12-17 21:24:34 -05001287 f_flags = O_RDWR | FMODE_NONOTIFY;
Eric Paris52c923d2009-12-17 21:24:26 -05001288 if (flags & FAN_CLOEXEC)
1289 f_flags |= O_CLOEXEC;
1290 if (flags & FAN_NONBLOCK)
1291 f_flags |= O_NONBLOCK;
1292
1293 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
Shakeel Buttac7b79f2020-12-19 20:46:08 -08001294 group = fsnotify_alloc_user_group(&fanotify_fsnotify_ops);
Eric Paris26379192010-11-23 23:48:26 -05001295 if (IS_ERR(group)) {
Eric Paris52c923d2009-12-17 21:24:26 -05001296 return PTR_ERR(group);
Eric Paris26379192010-11-23 23:48:26 -05001297 }
Eric Paris52c923d2009-12-17 21:24:26 -05001298
Amir Goldstein5b8fea62021-03-04 13:29:20 +02001299 /* Enforce groups limits per user in all containing user ns */
1300 group->fanotify_data.ucounts = inc_ucount(current_user_ns(),
1301 current_euid(),
1302 UCOUNT_FANOTIFY_GROUPS);
1303 if (!group->fanotify_data.ucounts) {
1304 fd = -EMFILE;
1305 goto out_destroy_group;
1306 }
1307
Amir Goldsteina8b98c82021-05-24 16:53:21 +03001308 group->fanotify_data.flags = flags | internal_flags;
Shakeel Buttd46eb14b2018-08-17 15:46:39 -07001309 group->memcg = get_mem_cgroup_from_mm(current->mm);
Eric Paris4afeff82010-10-28 17:21:58 -04001310
Amir Goldstein94e00d22021-03-04 12:48:25 +02001311 group->fanotify_data.merge_hash = fanotify_alloc_merge_hash();
1312 if (!group->fanotify_data.merge_hash) {
1313 fd = -ENOMEM;
1314 goto out_destroy_group;
1315 }
1316
Amir Goldsteinb8a6c3a2020-07-08 14:11:42 +03001317 group->overflow_event = fanotify_alloc_overflow_event();
1318 if (unlikely(!group->overflow_event)) {
Jan Karaff57cd52014-02-21 19:14:11 +01001319 fd = -ENOMEM;
1320 goto out_destroy_group;
1321 }
Jan Karaff57cd52014-02-21 19:14:11 +01001322
Will Woods1e2ee492014-05-06 12:50:10 -07001323 if (force_o_largefile())
1324 event_f_flags |= O_LARGEFILE;
Eric Paris80af2582010-07-28 10:18:37 -04001325 group->fanotify_data.f_flags = event_f_flags;
Eric Paris9e66e422009-12-17 21:24:34 -05001326 init_waitqueue_head(&group->fanotify_data.access_waitq);
1327 INIT_LIST_HEAD(&group->fanotify_data.access_list);
Amir Goldstein83b7a592020-07-16 11:42:26 +03001328 switch (class) {
Eric Paris4231a232010-10-28 17:21:56 -04001329 case FAN_CLASS_NOTIF:
1330 group->priority = FS_PRIO_0;
1331 break;
1332 case FAN_CLASS_CONTENT:
1333 group->priority = FS_PRIO_1;
1334 break;
1335 case FAN_CLASS_PRE_CONTENT:
1336 group->priority = FS_PRIO_2;
1337 break;
1338 default:
1339 fd = -EINVAL;
Lino Sanfilippod8153d42011-06-14 17:29:45 +02001340 goto out_destroy_group;
Eric Paris4231a232010-10-28 17:21:56 -04001341 }
Eric Pariscb2d4292009-12-17 21:24:34 -05001342
Eric Paris5dd03f52010-10-28 17:21:57 -04001343 if (flags & FAN_UNLIMITED_QUEUE) {
1344 fd = -EPERM;
1345 if (!capable(CAP_SYS_ADMIN))
Lino Sanfilippod8153d42011-06-14 17:29:45 +02001346 goto out_destroy_group;
Eric Paris5dd03f52010-10-28 17:21:57 -04001347 group->max_events = UINT_MAX;
1348 } else {
Amir Goldstein5b8fea62021-03-04 13:29:20 +02001349 group->max_events = fanotify_max_queued_events;
Eric Paris5dd03f52010-10-28 17:21:57 -04001350 }
Eric Paris2529a0d2010-10-28 17:21:57 -04001351
Eric Parisac7e22d2010-10-28 17:21:58 -04001352 if (flags & FAN_UNLIMITED_MARKS) {
1353 fd = -EPERM;
1354 if (!capable(CAP_SYS_ADMIN))
Lino Sanfilippod8153d42011-06-14 17:29:45 +02001355 goto out_destroy_group;
Eric Parisac7e22d2010-10-28 17:21:58 -04001356 }
Eric Parise7099d82010-10-28 17:21:57 -04001357
Steve Grubbde8cd832017-10-02 20:21:39 -04001358 if (flags & FAN_ENABLE_AUDIT) {
1359 fd = -EPERM;
1360 if (!capable(CAP_AUDIT_WRITE))
1361 goto out_destroy_group;
Steve Grubbde8cd832017-10-02 20:21:39 -04001362 }
1363
Eric Paris52c923d2009-12-17 21:24:26 -05001364 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
1365 if (fd < 0)
Lino Sanfilippod8153d42011-06-14 17:29:45 +02001366 goto out_destroy_group;
Eric Paris52c923d2009-12-17 21:24:26 -05001367
1368 return fd;
1369
Lino Sanfilippod8153d42011-06-14 17:29:45 +02001370out_destroy_group:
1371 fsnotify_destroy_group(group);
Eric Paris52c923d2009-12-17 21:24:26 -05001372 return fd;
Eric Paris11637e42009-12-17 21:24:25 -05001373}
Eric Parisbbaa4162009-12-17 21:24:26 -05001374
Gabriel Krisman Bertazi82992122021-10-25 16:27:21 -03001375static int fanotify_test_fsid(struct dentry *dentry, __kernel_fsid_t *fsid)
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001376{
Amir Goldstein73072282019-01-10 19:04:39 +02001377 __kernel_fsid_t root_fsid;
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001378 int err;
1379
1380 /*
Gabriel Krisman Bertazi82992122021-10-25 16:27:21 -03001381 * Make sure dentry is not of a filesystem with zero fsid (e.g. fuse).
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001382 */
Gabriel Krisman Bertazi82992122021-10-25 16:27:21 -03001383 err = vfs_get_fsid(dentry, fsid);
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001384 if (err)
1385 return err;
1386
Amir Goldstein73072282019-01-10 19:04:39 +02001387 if (!fsid->val[0] && !fsid->val[1])
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001388 return -ENODEV;
1389
1390 /*
Gabriel Krisman Bertazi82992122021-10-25 16:27:21 -03001391 * Make sure dentry is not of a filesystem subvolume (e.g. btrfs)
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001392 * which uses a different fsid than sb root.
1393 */
Gabriel Krisman Bertazi82992122021-10-25 16:27:21 -03001394 err = vfs_get_fsid(dentry->d_sb->s_root, &root_fsid);
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001395 if (err)
1396 return err;
1397
Amir Goldstein73072282019-01-10 19:04:39 +02001398 if (root_fsid.val[0] != fsid->val[0] ||
1399 root_fsid.val[1] != fsid->val[1])
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001400 return -EXDEV;
1401
Gabriel Krisman Bertazi82992122021-10-25 16:27:21 -03001402 return 0;
1403}
1404
1405/* Check if filesystem can encode a unique fid */
1406static int fanotify_test_fid(struct dentry *dentry)
1407{
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001408 /*
1409 * We need to make sure that the file system supports at least
1410 * encoding a file handle so user can use name_to_handle_at() to
1411 * compare fid returned with event to the file handle of watched
1412 * objects. However, name_to_handle_at() requires that the
1413 * filesystem also supports decoding file handles.
1414 */
Gabriel Krisman Bertazi82992122021-10-25 16:27:21 -03001415 if (!dentry->d_sb->s_export_op ||
1416 !dentry->d_sb->s_export_op->fh_to_dentry)
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001417 return -EOPNOTSUPP;
1418
1419 return 0;
1420}
1421
Jan Kara0b3b0942019-05-15 16:28:34 +02001422static int fanotify_events_supported(struct path *path, __u64 mask)
1423{
1424 /*
1425 * Some filesystems such as 'proc' acquire unusual locks when opening
1426 * files. For them fanotify permission events have high chances of
1427 * deadlocking the system - open done when reporting fanotify event
1428 * blocks on this "unusual" lock while another process holding the lock
1429 * waits for fanotify permission event to be answered. Just disallow
1430 * permission events for such filesystems.
1431 */
1432 if (mask & FANOTIFY_PERM_EVENTS &&
1433 path->mnt->mnt_sb->s_type->fs_flags & FS_DISALLOW_NOTIFY_PERM)
1434 return -EINVAL;
1435 return 0;
1436}
1437
Dominik Brodowski183caa32018-03-17 15:06:11 +01001438static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
1439 int dfd, const char __user *pathname)
Eric Parisbbaa4162009-12-17 21:24:26 -05001440{
Eric Paris0ff21db2009-12-17 21:24:29 -05001441 struct inode *inode = NULL;
1442 struct vfsmount *mnt = NULL;
Eric Paris2a3edf82009-12-17 21:24:26 -05001443 struct fsnotify_group *group;
Al Viro2903ff02012-08-28 12:52:22 -04001444 struct fd f;
Eric Paris2a3edf82009-12-17 21:24:26 -05001445 struct path path;
Amir Goldstein73072282019-01-10 19:04:39 +02001446 __kernel_fsid_t __fsid, *fsid = NULL;
Amir Goldsteinbdd5a462018-10-04 00:25:37 +03001447 u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS;
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001448 unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS;
Amir Goldstein3ef86652020-07-16 11:42:13 +03001449 bool ignored = flags & FAN_MARK_IGNORED_MASK;
Amir Goldsteind809daf2020-07-16 11:42:12 +03001450 unsigned int obj_type, fid_mode;
Amir Goldstein85af5d92020-07-16 11:42:15 +03001451 u32 umask = 0;
Al Viro2903ff02012-08-28 12:52:22 -04001452 int ret;
Eric Paris2a3edf82009-12-17 21:24:26 -05001453
1454 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
1455 __func__, fanotify_fd, flags, dfd, pathname, mask);
1456
1457 /* we only use the lower 32 bits as of right now. */
Christian Brauner22d483b2021-03-25 09:37:43 +01001458 if (upper_32_bits(mask))
Eric Paris2a3edf82009-12-17 21:24:26 -05001459 return -EINVAL;
1460
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001461 if (flags & ~FANOTIFY_MARK_FLAGS)
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -05001462 return -EINVAL;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001463
1464 switch (mark_type) {
1465 case FAN_MARK_INODE:
Aaron Goidelac5656d2019-08-12 11:20:00 -04001466 obj_type = FSNOTIFY_OBJ_TYPE_INODE;
1467 break;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001468 case FAN_MARK_MOUNT:
Aaron Goidelac5656d2019-08-12 11:20:00 -04001469 obj_type = FSNOTIFY_OBJ_TYPE_VFSMOUNT;
1470 break;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001471 case FAN_MARK_FILESYSTEM:
Aaron Goidelac5656d2019-08-12 11:20:00 -04001472 obj_type = FSNOTIFY_OBJ_TYPE_SB;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001473 break;
1474 default:
1475 return -EINVAL;
1476 }
1477
Eric Paris4d926042009-12-17 21:24:34 -05001478 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001479 case FAN_MARK_ADD:
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -05001480 case FAN_MARK_REMOVE:
Lino Sanfilippo1734dee2010-11-22 18:46:33 +01001481 if (!mask)
1482 return -EINVAL;
Heinrich Schuchardtcc299a92014-06-04 16:05:43 -07001483 break;
Eric Paris4d926042009-12-17 21:24:34 -05001484 case FAN_MARK_FLUSH:
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001485 if (flags & ~(FANOTIFY_MARK_TYPE_BITS | FAN_MARK_FLUSH))
Heinrich Schuchardtcc299a92014-06-04 16:05:43 -07001486 return -EINVAL;
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -05001487 break;
1488 default:
1489 return -EINVAL;
1490 }
Eric Paris8fcd6522010-10-28 17:21:59 -04001491
Miklos Szeredi6685df32017-10-30 21:14:56 +01001492 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001493 valid_mask |= FANOTIFY_PERM_EVENTS;
Miklos Szeredi6685df32017-10-30 21:14:56 +01001494
1495 if (mask & ~valid_mask)
Eric Paris2a3edf82009-12-17 21:24:26 -05001496 return -EINVAL;
1497
Amir Goldstein3ef86652020-07-16 11:42:13 +03001498 /* Event flags (ONDIR, ON_CHILD) are meaningless in ignored mask */
1499 if (ignored)
1500 mask &= ~FANOTIFY_EVENT_FLAGS;
1501
Al Viro2903ff02012-08-28 12:52:22 -04001502 f = fdget(fanotify_fd);
1503 if (unlikely(!f.file))
Eric Paris2a3edf82009-12-17 21:24:26 -05001504 return -EBADF;
1505
1506 /* verify that this is indeed an fanotify instance */
1507 ret = -EINVAL;
Al Viro2903ff02012-08-28 12:52:22 -04001508 if (unlikely(f.file->f_op != &fanotify_fops))
Eric Paris2a3edf82009-12-17 21:24:26 -05001509 goto fput_and_out;
Al Viro2903ff02012-08-28 12:52:22 -04001510 group = f.file->private_data;
Eric Paris4231a232010-10-28 17:21:56 -04001511
1512 /*
Amir Goldsteina8b98c82021-05-24 16:53:21 +03001513 * An unprivileged user is not allowed to setup mount nor filesystem
1514 * marks. This also includes setting up such marks by a group that
1515 * was initialized by an unprivileged user.
Amir Goldstein7cea2a32021-03-04 13:29:21 +02001516 */
1517 ret = -EPERM;
Amir Goldsteina8b98c82021-05-24 16:53:21 +03001518 if ((!capable(CAP_SYS_ADMIN) ||
1519 FAN_GROUP_FLAG(group, FANOTIFY_UNPRIV)) &&
Amir Goldstein7cea2a32021-03-04 13:29:21 +02001520 mark_type != FAN_MARK_INODE)
1521 goto fput_and_out;
1522
1523 /*
Eric Paris4231a232010-10-28 17:21:56 -04001524 * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
1525 * allowed to set permissions events.
1526 */
1527 ret = -EINVAL;
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001528 if (mask & FANOTIFY_PERM_EVENTS &&
Eric Paris4231a232010-10-28 17:21:56 -04001529 group->priority == FS_PRIO_0)
1530 goto fput_and_out;
Eric Paris2a3edf82009-12-17 21:24:26 -05001531
Gabriel Krisman Bertazi9709bd52021-10-25 16:27:43 -03001532 if (mask & FAN_FS_ERROR &&
1533 mark_type != FAN_MARK_FILESYSTEM)
1534 goto fput_and_out;
1535
Amir Goldstein235328d2019-01-10 19:04:43 +02001536 /*
Gabriel Krisman Bertazi4fe595c2021-10-25 16:27:31 -03001537 * Events that do not carry enough information to report
1538 * event->fd require a group that supports reporting fid. Those
1539 * events are not supported on a mount mark, because they do not
1540 * carry enough information (i.e. path) to be filtered by mount
1541 * point.
Amir Goldstein235328d2019-01-10 19:04:43 +02001542 */
Amir Goldsteind809daf2020-07-16 11:42:12 +03001543 fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
Gabriel Krisman Bertazi4fe595c2021-10-25 16:27:31 -03001544 if (mask & ~(FANOTIFY_FD_EVENTS|FANOTIFY_EVENT_FLAGS) &&
Amir Goldsteind809daf2020-07-16 11:42:12 +03001545 (!fid_mode || mark_type == FAN_MARK_MOUNT))
Amir Goldstein235328d2019-01-10 19:04:43 +02001546 goto fput_and_out;
1547
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -07001548 if (flags & FAN_MARK_FLUSH) {
1549 ret = 0;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001550 if (mark_type == FAN_MARK_MOUNT)
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -07001551 fsnotify_clear_vfsmount_marks_by_group(group);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001552 else if (mark_type == FAN_MARK_FILESYSTEM)
1553 fsnotify_clear_sb_marks_by_group(group);
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -07001554 else
1555 fsnotify_clear_inode_marks_by_group(group);
1556 goto fput_and_out;
1557 }
1558
Aaron Goidelac5656d2019-08-12 11:20:00 -04001559 ret = fanotify_find_path(dfd, pathname, &path, flags,
1560 (mask & ALL_FSNOTIFY_EVENTS), obj_type);
Eric Paris2a3edf82009-12-17 21:24:26 -05001561 if (ret)
1562 goto fput_and_out;
1563
Jan Kara0b3b0942019-05-15 16:28:34 +02001564 if (flags & FAN_MARK_ADD) {
1565 ret = fanotify_events_supported(&path, mask);
1566 if (ret)
1567 goto path_put_and_out;
1568 }
1569
Amir Goldsteind809daf2020-07-16 11:42:12 +03001570 if (fid_mode) {
Gabriel Krisman Bertazi82992122021-10-25 16:27:21 -03001571 ret = fanotify_test_fsid(path.dentry, &__fsid);
1572 if (ret)
1573 goto path_put_and_out;
1574
1575 ret = fanotify_test_fid(path.dentry);
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001576 if (ret)
1577 goto path_put_and_out;
Amir Goldstein77115222019-01-10 19:04:37 +02001578
Amir Goldstein73072282019-01-10 19:04:39 +02001579 fsid = &__fsid;
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001580 }
1581
Eric Paris2a3edf82009-12-17 21:24:26 -05001582 /* inode held in place by reference to path; group by fget on fd */
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001583 if (mark_type == FAN_MARK_INODE)
Eric Paris0ff21db2009-12-17 21:24:29 -05001584 inode = path.dentry->d_inode;
1585 else
1586 mnt = path.mnt;
Eric Paris2a3edf82009-12-17 21:24:26 -05001587
Amir Goldstein85af5d92020-07-16 11:42:15 +03001588 /* Mask out FAN_EVENT_ON_CHILD flag for sb/mount/non-dir marks */
1589 if (mnt || !S_ISDIR(inode->i_mode)) {
1590 mask &= ~FAN_EVENT_ON_CHILD;
1591 umask = FAN_EVENT_ON_CHILD;
Amir Goldstein51280632020-07-16 11:42:27 +03001592 /*
1593 * If group needs to report parent fid, register for getting
1594 * events with parent/name info for non-directory.
1595 */
1596 if ((fid_mode & FAN_REPORT_DIR_FID) &&
1597 (flags & FAN_MARK_ADD) && !ignored)
1598 mask |= FAN_EVENT_ON_CHILD;
Amir Goldstein85af5d92020-07-16 11:42:15 +03001599 }
1600
Eric Paris2a3edf82009-12-17 21:24:26 -05001601 /* create/update an inode mark */
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -07001602 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -05001603 case FAN_MARK_ADD:
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001604 if (mark_type == FAN_MARK_MOUNT)
Amir Goldstein77115222019-01-10 19:04:37 +02001605 ret = fanotify_add_vfsmount_mark(group, mnt, mask,
1606 flags, fsid);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001607 else if (mark_type == FAN_MARK_FILESYSTEM)
Amir Goldstein77115222019-01-10 19:04:37 +02001608 ret = fanotify_add_sb_mark(group, mnt->mnt_sb, mask,
1609 flags, fsid);
Eric Paris0ff21db2009-12-17 21:24:29 -05001610 else
Amir Goldstein77115222019-01-10 19:04:37 +02001611 ret = fanotify_add_inode_mark(group, inode, mask,
1612 flags, fsid);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -05001613 break;
1614 case FAN_MARK_REMOVE:
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001615 if (mark_type == FAN_MARK_MOUNT)
Amir Goldstein77115222019-01-10 19:04:37 +02001616 ret = fanotify_remove_vfsmount_mark(group, mnt, mask,
Amir Goldstein85af5d92020-07-16 11:42:15 +03001617 flags, umask);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001618 else if (mark_type == FAN_MARK_FILESYSTEM)
Amir Goldstein77115222019-01-10 19:04:37 +02001619 ret = fanotify_remove_sb_mark(group, mnt->mnt_sb, mask,
Amir Goldstein85af5d92020-07-16 11:42:15 +03001620 flags, umask);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -05001621 else
Amir Goldstein77115222019-01-10 19:04:37 +02001622 ret = fanotify_remove_inode_mark(group, inode, mask,
Amir Goldstein85af5d92020-07-16 11:42:15 +03001623 flags, umask);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -05001624 break;
1625 default:
1626 ret = -EINVAL;
1627 }
Eric Paris2a3edf82009-12-17 21:24:26 -05001628
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001629path_put_and_out:
Eric Paris2a3edf82009-12-17 21:24:26 -05001630 path_put(&path);
1631fput_and_out:
Al Viro2903ff02012-08-28 12:52:22 -04001632 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -05001633 return ret;
Eric Parisbbaa4162009-12-17 21:24:26 -05001634}
Eric Paris2a3edf82009-12-17 21:24:26 -05001635
Brian Gerst2ca408d2020-11-30 17:30:59 -05001636#ifndef CONFIG_ARCH_SPLIT_ARG64
Dominik Brodowski183caa32018-03-17 15:06:11 +01001637SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
1638 __u64, mask, int, dfd,
1639 const char __user *, pathname)
1640{
1641 return do_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname);
1642}
Brian Gerst2ca408d2020-11-30 17:30:59 -05001643#endif
Dominik Brodowski183caa32018-03-17 15:06:11 +01001644
Brian Gerst2ca408d2020-11-30 17:30:59 -05001645#if defined(CONFIG_ARCH_SPLIT_ARG64) || defined(CONFIG_COMPAT)
1646SYSCALL32_DEFINE6(fanotify_mark,
Al Viro91c2e0b2013-03-05 20:10:59 -05001647 int, fanotify_fd, unsigned int, flags,
Brian Gerst2ca408d2020-11-30 17:30:59 -05001648 SC_ARG64(mask), int, dfd,
Al Viro91c2e0b2013-03-05 20:10:59 -05001649 const char __user *, pathname)
1650{
Brian Gerst2ca408d2020-11-30 17:30:59 -05001651 return do_fanotify_mark(fanotify_fd, flags, SC_VAL64(__u64, mask),
1652 dfd, pathname);
Al Viro91c2e0b2013-03-05 20:10:59 -05001653}
1654#endif
1655
Eric Paris2a3edf82009-12-17 21:24:26 -05001656/*
Justin P. Mattockae0e47f2011-03-01 15:06:02 +01001657 * fanotify_user_setup - Our initialization function. Note that we cannot return
Eric Paris2a3edf82009-12-17 21:24:26 -05001658 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
1659 * must result in panic().
1660 */
1661static int __init fanotify_user_setup(void)
1662{
Amir Goldstein5b8fea62021-03-04 13:29:20 +02001663 struct sysinfo si;
1664 int max_marks;
1665
1666 si_meminfo(&si);
1667 /*
1668 * Allow up to 1% of addressable memory to be accounted for per user
1669 * marks limited to the range [8192, 1048576]. mount and sb marks are
1670 * a lot cheaper than inode marks, but there is no reason for a user
1671 * to have many of those, so calculate by the cost of inode marks.
1672 */
1673 max_marks = (((si.totalram - si.totalhigh) / 100) << PAGE_SHIFT) /
1674 INODE_MARK_COST;
1675 max_marks = clamp(max_marks, FANOTIFY_OLD_DEFAULT_MAX_MARKS,
1676 FANOTIFY_DEFAULT_MAX_USER_MARKS);
1677
Amir Goldsteina8b98c82021-05-24 16:53:21 +03001678 BUILD_BUG_ON(FANOTIFY_INIT_FLAGS & FANOTIFY_INTERNAL_GROUP_FLAGS);
Amir Goldsteind61fd652021-11-29 22:15:29 +02001679 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 12);
Amir Goldsteinbdd5a462018-10-04 00:25:37 +03001680 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9);
1681
Shakeel Buttd46eb14b2018-08-17 15:46:39 -07001682 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark,
1683 SLAB_PANIC|SLAB_ACCOUNT);
Jan Kara7088f352020-03-24 17:04:20 +01001684 fanotify_fid_event_cachep = KMEM_CACHE(fanotify_fid_event,
1685 SLAB_PANIC);
1686 fanotify_path_event_cachep = KMEM_CACHE(fanotify_path_event,
1687 SLAB_PANIC);
Miklos Szeredi6685df32017-10-30 21:14:56 +01001688 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
1689 fanotify_perm_event_cachep =
Amir Goldstein33913992019-01-10 19:04:32 +02001690 KMEM_CACHE(fanotify_perm_event, SLAB_PANIC);
Miklos Szeredi6685df32017-10-30 21:14:56 +01001691 }
Eric Paris2a3edf82009-12-17 21:24:26 -05001692
Amir Goldstein5b8fea62021-03-04 13:29:20 +02001693 fanotify_max_queued_events = FANOTIFY_DEFAULT_MAX_EVENTS;
1694 init_user_ns.ucount_max[UCOUNT_FANOTIFY_GROUPS] =
1695 FANOTIFY_DEFAULT_MAX_GROUPS;
1696 init_user_ns.ucount_max[UCOUNT_FANOTIFY_MARKS] = max_marks;
1697
Eric Paris2a3edf82009-12-17 21:24:26 -05001698 return 0;
1699}
1700device_initcall(fanotify_user_setup);