blob: e4a11f56782d52fb14fa7bf80dab23500f7435b8 [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{
Amir Goldstein3cf984e2021-11-29 22:15:33 +0200335 struct fanotify_event_info_error info = { };
Gabriel Krisman Bertazi130a3c72021-10-25 16:27:42 -0300336 struct fanotify_error_event *fee = FANOTIFY_EE(event);
337
338 info.hdr.info_type = FAN_EVENT_INFO_TYPE_ERROR;
Gabriel Krisman Bertazi130a3c72021-10-25 16:27:42 -0300339 info.hdr.len = FANOTIFY_ERROR_INFO_LEN;
340
341 if (WARN_ON(count < info.hdr.len))
342 return -EFAULT;
343
344 info.error = fee->error;
345 info.error_count = fee->err_count;
346
347 if (copy_to_user(buf, &info, sizeof(info)))
348 return -EFAULT;
349
350 return info.hdr.len;
351}
352
Matthew Bobrowskid3424c92021-08-08 15:25:32 +1000353static int copy_fid_info_to_user(__kernel_fsid_t *fsid, struct fanotify_fh *fh,
354 int info_type, const char *name,
355 size_t name_len,
356 char __user *buf, size_t count)
Amir Goldstein5e469c82019-01-10 19:04:35 +0200357{
358 struct fanotify_event_info_fid info = { };
359 struct file_handle handle = { };
Jan Karaafc894c2020-03-24 16:55:37 +0100360 unsigned char bounce[FANOTIFY_INLINE_FH_LEN], *fh_buf;
Amir Goldsteincacfb952020-03-19 17:10:21 +0200361 size_t fh_len = fh ? fh->len : 0;
Amir Goldstein44d705b2020-03-19 17:10:22 +0200362 size_t info_len = fanotify_fid_info_len(fh_len, name_len);
363 size_t len = info_len;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200364
Amir Goldstein44d705b2020-03-19 17:10:22 +0200365 pr_debug("%s: fh_len=%zu name_len=%zu, info_len=%zu, count=%zu\n",
366 __func__, fh_len, name_len, info_len, count);
367
Amir Goldstein44d705b2020-03-19 17:10:22 +0200368 if (WARN_ON_ONCE(len < sizeof(info) || len > count))
Amir Goldstein5e469c82019-01-10 19:04:35 +0200369 return -EFAULT;
370
Amir Goldstein44d705b2020-03-19 17:10:22 +0200371 /*
372 * Copy event info fid header followed by variable sized file handle
373 * and optionally followed by variable sized filename.
374 */
Amir Goldstein83b7a592020-07-16 11:42:26 +0300375 switch (info_type) {
376 case FAN_EVENT_INFO_TYPE_FID:
377 case FAN_EVENT_INFO_TYPE_DFID:
378 if (WARN_ON_ONCE(name_len))
379 return -EFAULT;
380 break;
381 case FAN_EVENT_INFO_TYPE_DFID_NAME:
382 if (WARN_ON_ONCE(!name || !name_len))
383 return -EFAULT;
384 break;
385 default:
386 return -EFAULT;
387 }
388
389 info.hdr.info_type = info_type;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200390 info.hdr.len = len;
Amir Goldsteind766b552020-03-19 17:10:20 +0200391 info.fsid = *fsid;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200392 if (copy_to_user(buf, &info, sizeof(info)))
393 return -EFAULT;
394
395 buf += sizeof(info);
396 len -= sizeof(info);
Amir Goldstein44d705b2020-03-19 17:10:22 +0200397 if (WARN_ON_ONCE(len < sizeof(handle)))
398 return -EFAULT;
399
Jan Karaafc894c2020-03-24 16:55:37 +0100400 handle.handle_type = fh->type;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200401 handle.handle_bytes = fh_len;
Gabriel Krisman Bertazi936d6a32021-10-25 16:27:41 -0300402
403 /* Mangle handle_type for bad file_handle */
404 if (!fh_len)
405 handle.handle_type = FILEID_INVALID;
406
Amir Goldstein5e469c82019-01-10 19:04:35 +0200407 if (copy_to_user(buf, &handle, sizeof(handle)))
408 return -EFAULT;
409
410 buf += sizeof(handle);
411 len -= sizeof(handle);
Amir Goldstein44d705b2020-03-19 17:10:22 +0200412 if (WARN_ON_ONCE(len < fh_len))
413 return -EFAULT;
414
Jan Karab2d22b62019-03-12 12:42:37 +0100415 /*
Amir Goldstein44d705b2020-03-19 17:10:22 +0200416 * For an inline fh and inline file name, copy through stack to exclude
417 * the copy from usercopy hardening protections.
Jan Karab2d22b62019-03-12 12:42:37 +0100418 */
Jan Karaafc894c2020-03-24 16:55:37 +0100419 fh_buf = fanotify_fh_buf(fh);
Jan Karab2d22b62019-03-12 12:42:37 +0100420 if (fh_len <= FANOTIFY_INLINE_FH_LEN) {
Jan Karaafc894c2020-03-24 16:55:37 +0100421 memcpy(bounce, fh_buf, fh_len);
422 fh_buf = bounce;
Jan Karab2d22b62019-03-12 12:42:37 +0100423 }
Jan Karaafc894c2020-03-24 16:55:37 +0100424 if (copy_to_user(buf, fh_buf, fh_len))
Amir Goldstein5e469c82019-01-10 19:04:35 +0200425 return -EFAULT;
426
Amir Goldstein5e469c82019-01-10 19:04:35 +0200427 buf += fh_len;
428 len -= fh_len;
Amir Goldstein44d705b2020-03-19 17:10:22 +0200429
430 if (name_len) {
431 /* Copy the filename with terminating null */
432 name_len++;
433 if (WARN_ON_ONCE(len < name_len))
434 return -EFAULT;
435
436 if (copy_to_user(buf, name, name_len))
437 return -EFAULT;
438
439 buf += name_len;
440 len -= name_len;
441 }
442
443 /* Pad with 0's */
Amir Goldstein5e469c82019-01-10 19:04:35 +0200444 WARN_ON_ONCE(len < 0 || len >= FANOTIFY_EVENT_ALIGN);
445 if (len > 0 && clear_user(buf, len))
446 return -EFAULT;
447
Amir Goldstein44d705b2020-03-19 17:10:22 +0200448 return info_len;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200449}
450
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000451static int copy_pidfd_info_to_user(int pidfd,
452 char __user *buf,
453 size_t count)
454{
455 struct fanotify_event_info_pidfd info = { };
456 size_t info_len = FANOTIFY_PIDFD_INFO_HDR_LEN;
457
458 if (WARN_ON_ONCE(info_len > count))
459 return -EFAULT;
460
461 info.hdr.info_type = FAN_EVENT_INFO_TYPE_PIDFD;
462 info.hdr.len = info_len;
463 info.pidfd = pidfd;
464
465 if (copy_to_user(buf, &info, info_len))
466 return -EFAULT;
467
468 return info_len;
469}
470
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000471static int copy_info_records_to_user(struct fanotify_event *event,
472 struct fanotify_info *info,
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000473 unsigned int info_mode, int pidfd,
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000474 char __user *buf, size_t count)
475{
476 int ret, total_bytes = 0, info_type = 0;
477 unsigned int fid_mode = info_mode & FANOTIFY_FID_BITS;
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000478 unsigned int pidfd_mode = info_mode & FAN_REPORT_PIDFD;
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000479
480 /*
481 * Event info records order is as follows: dir fid + name, child fid.
482 */
Gabriel Krisman Bertazi4bd5a5c2021-10-25 16:27:38 -0300483 if (fanotify_event_has_dir_fh(event)) {
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000484 info_type = info->name_len ? FAN_EVENT_INFO_TYPE_DFID_NAME :
485 FAN_EVENT_INFO_TYPE_DFID;
486 ret = copy_fid_info_to_user(fanotify_event_fsid(event),
487 fanotify_info_dir_fh(info),
488 info_type,
489 fanotify_info_name(info),
490 info->name_len, buf, count);
491 if (ret < 0)
492 return ret;
493
494 buf += ret;
495 count -= ret;
496 total_bytes += ret;
497 }
498
Gabriel Krisman Bertazi4bd5a5c2021-10-25 16:27:38 -0300499 if (fanotify_event_has_object_fh(event)) {
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000500 const char *dot = NULL;
501 int dot_len = 0;
502
503 if (fid_mode == FAN_REPORT_FID || info_type) {
504 /*
505 * With only group flag FAN_REPORT_FID only type FID is
506 * reported. Second info record type is always FID.
507 */
508 info_type = FAN_EVENT_INFO_TYPE_FID;
509 } else if ((fid_mode & FAN_REPORT_NAME) &&
510 (event->mask & FAN_ONDIR)) {
511 /*
512 * With group flag FAN_REPORT_NAME, if name was not
513 * recorded in an event on a directory, report the name
514 * "." with info type DFID_NAME.
515 */
516 info_type = FAN_EVENT_INFO_TYPE_DFID_NAME;
517 dot = ".";
518 dot_len = 1;
519 } else if ((event->mask & ALL_FSNOTIFY_DIRENT_EVENTS) ||
520 (event->mask & FAN_ONDIR)) {
521 /*
522 * With group flag FAN_REPORT_DIR_FID, a single info
523 * record has type DFID for directory entry modification
524 * event and for event on a directory.
525 */
526 info_type = FAN_EVENT_INFO_TYPE_DFID;
527 } else {
528 /*
529 * With group flags FAN_REPORT_DIR_FID|FAN_REPORT_FID,
530 * a single info record has type FID for event on a
531 * non-directory, when there is no directory to report.
532 * For example, on FAN_DELETE_SELF event.
533 */
534 info_type = FAN_EVENT_INFO_TYPE_FID;
535 }
536
537 ret = copy_fid_info_to_user(fanotify_event_fsid(event),
538 fanotify_event_object_fh(event),
539 info_type, dot, dot_len,
540 buf, count);
541 if (ret < 0)
542 return ret;
543
544 buf += ret;
545 count -= ret;
546 total_bytes += ret;
547 }
548
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000549 if (pidfd_mode) {
550 ret = copy_pidfd_info_to_user(pidfd, buf, count);
551 if (ret < 0)
552 return ret;
553
554 buf += ret;
555 count -= ret;
556 total_bytes += ret;
557 }
558
Gabriel Krisman Bertazi130a3c72021-10-25 16:27:42 -0300559 if (fanotify_is_error_event(event->mask)) {
560 ret = copy_error_info_to_user(event, buf, count);
561 if (ret < 0)
562 return ret;
563 buf += ret;
564 count -= ret;
565 total_bytes += ret;
566 }
567
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000568 return total_bytes;
569}
570
Eric Parisa1014f12009-12-17 21:24:26 -0500571static ssize_t copy_event_to_user(struct fsnotify_group *group,
Jan Kara7088f352020-03-24 17:04:20 +0100572 struct fanotify_event *event,
Kees Cook5b03a472018-12-04 15:44:46 -0800573 char __user *buf, size_t count)
Eric Parisa1014f12009-12-17 21:24:26 -0500574{
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200575 struct fanotify_event_metadata metadata;
Jan Kara7088f352020-03-24 17:04:20 +0100576 struct path *path = fanotify_event_path(event);
Amir Goldsteinf454fa62020-07-16 11:42:17 +0300577 struct fanotify_info *info = fanotify_event_info(event);
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000578 unsigned int info_mode = FAN_GROUP_FLAG(group, FANOTIFY_INFO_MODES);
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000579 unsigned int pidfd_mode = info_mode & FAN_REPORT_PIDFD;
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200580 struct file *f = NULL;
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000581 int ret, pidfd = FAN_NOPIDFD, fd = FAN_NOFD;
Eric Parisa1014f12009-12-17 21:24:26 -0500582
Jan Kara7088f352020-03-24 17:04:20 +0100583 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
Eric Parisa1014f12009-12-17 21:24:26 -0500584
Gabriel Krisman Bertazib9928e82021-10-25 16:27:20 -0300585 metadata.event_len = fanotify_event_len(info_mode, event);
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200586 metadata.metadata_len = FAN_EVENT_METADATA_LEN;
587 metadata.vers = FANOTIFY_METADATA_VERSION;
588 metadata.reserved = 0;
589 metadata.mask = event->mask & FANOTIFY_OUTGOING_EVENTS;
590 metadata.pid = pid_vnr(event->pid);
Amir Goldstein7cea2a32021-03-04 13:29:21 +0200591 /*
592 * For an unprivileged listener, event->pid can be used to identify the
593 * events generated by the listener process itself, without disclosing
594 * the pids of other processes.
595 */
Amir Goldsteina8b98c82021-05-24 16:53:21 +0300596 if (FAN_GROUP_FLAG(group, FANOTIFY_UNPRIV) &&
Amir Goldstein7cea2a32021-03-04 13:29:21 +0200597 task_tgid(current) != event->pid)
598 metadata.pid = 0;
Eric Parisa1014f12009-12-17 21:24:26 -0500599
Amir Goldsteina8b98c82021-05-24 16:53:21 +0300600 /*
601 * For now, fid mode is required for an unprivileged listener and
602 * fid mode does not report fd in events. Keep this check anyway
603 * for safety in case fid mode requirement is relaxed in the future
604 * to allow unprivileged listener to get events with no fd and no fid.
605 */
606 if (!FAN_GROUP_FLAG(group, FANOTIFY_UNPRIV) &&
607 path && path->mnt && path->dentry) {
Jan Karaafc894c2020-03-24 16:55:37 +0100608 fd = create_fd(group, path, &f);
609 if (fd < 0)
610 return fd;
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200611 }
612 metadata.fd = fd;
613
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000614 if (pidfd_mode) {
615 /*
616 * Complain if the FAN_REPORT_PIDFD and FAN_REPORT_TID mutual
617 * exclusion is ever lifted. At the time of incoporating pidfd
618 * support within fanotify, the pidfd API only supported the
619 * creation of pidfds for thread-group leaders.
620 */
621 WARN_ON_ONCE(FAN_GROUP_FLAG(group, FAN_REPORT_TID));
622
623 /*
624 * The PIDTYPE_TGID check for an event->pid is performed
625 * preemptively in an attempt to catch out cases where the event
626 * listener reads events after the event generating process has
627 * already terminated. Report FAN_NOPIDFD to the event listener
628 * in those cases, with all other pidfd creation errors being
629 * reported as FAN_EPIDFD.
630 */
631 if (metadata.pid == 0 ||
632 !pid_has_task(event->pid, PIDTYPE_TGID)) {
633 pidfd = FAN_NOPIDFD;
634 } else {
635 pidfd = pidfd_create(event->pid, 0);
636 if (pidfd < 0)
637 pidfd = FAN_EPIDFD;
638 }
639 }
640
Al Viro352e3b22012-08-19 12:30:45 -0400641 ret = -EFAULT;
Kees Cook5b03a472018-12-04 15:44:46 -0800642 /*
643 * Sanity check copy size in case get_one_event() and
Fabian Frederickc5e443c2020-05-12 20:18:36 +0200644 * event_len sizes ever get out of sync.
Kees Cook5b03a472018-12-04 15:44:46 -0800645 */
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200646 if (WARN_ON_ONCE(metadata.event_len > count))
Al Viro352e3b22012-08-19 12:30:45 -0400647 goto out_close_fd;
648
Amir Goldstein5e469c82019-01-10 19:04:35 +0200649 if (copy_to_user(buf, &metadata, FAN_EVENT_METADATA_LEN))
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200650 goto out_close_fd;
651
Amir Goldstein44d705b2020-03-19 17:10:22 +0200652 buf += FAN_EVENT_METADATA_LEN;
653 count -= FAN_EVENT_METADATA_LEN;
654
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200655 if (fanotify_is_perm_event(event->mask))
Jan Kara7088f352020-03-24 17:04:20 +0100656 FANOTIFY_PERM(event)->fd = fd;
Eric Parisb2d87902009-12-17 21:24:34 -0500657
Amir Goldstein44d705b2020-03-19 17:10:22 +0200658 if (f)
Al Viro3587b1b2012-11-18 19:19:00 +0000659 fd_install(fd, f);
Amir Goldstein44d705b2020-03-19 17:10:22 +0200660
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000661 if (info_mode) {
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000662 ret = copy_info_records_to_user(event, info, info_mode, pidfd,
Matthew Bobrowski0aca67b2021-08-08 15:25:58 +1000663 buf, count);
Amir Goldstein5e469c82019-01-10 19:04:35 +0200664 if (ret < 0)
Matthew Bobrowskif644bc42021-06-11 13:32:06 +1000665 goto out_close_fd;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200666 }
667
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200668 return metadata.event_len;
Eric Parisb2d87902009-12-17 21:24:34 -0500669
Eric Parisb2d87902009-12-17 21:24:34 -0500670out_close_fd:
Al Viro352e3b22012-08-19 12:30:45 -0400671 if (fd != FAN_NOFD) {
672 put_unused_fd(fd);
673 fput(f);
674 }
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +1000675
676 if (pidfd >= 0)
677 close_fd(pidfd);
678
Eric Parisb2d87902009-12-17 21:24:34 -0500679 return ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500680}
681
682/* intofiy userspace file descriptor functions */
Al Viro076ccb72017-07-03 01:02:18 -0400683static __poll_t fanotify_poll(struct file *file, poll_table *wait)
Eric Parisa1014f12009-12-17 21:24:26 -0500684{
685 struct fsnotify_group *group = file->private_data;
Al Viro076ccb72017-07-03 01:02:18 -0400686 __poll_t ret = 0;
Eric Parisa1014f12009-12-17 21:24:26 -0500687
688 poll_wait(file, &group->notification_waitq, wait);
Jan Karac21dbe22016-10-07 16:56:52 -0700689 spin_lock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500690 if (!fsnotify_notify_queue_is_empty(group))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800691 ret = EPOLLIN | EPOLLRDNORM;
Jan Karac21dbe22016-10-07 16:56:52 -0700692 spin_unlock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500693
694 return ret;
695}
696
697static ssize_t fanotify_read(struct file *file, char __user *buf,
698 size_t count, loff_t *pos)
699{
700 struct fsnotify_group *group;
Jan Kara7088f352020-03-24 17:04:20 +0100701 struct fanotify_event *event;
Eric Parisa1014f12009-12-17 21:24:26 -0500702 char __user *start;
703 int ret;
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100704 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Eric Parisa1014f12009-12-17 21:24:26 -0500705
706 start = buf;
707 group = file->private_data;
708
709 pr_debug("%s: group=%p\n", __func__, group);
710
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100711 add_wait_queue(&group->notification_waitq, &wait);
Eric Parisa1014f12009-12-17 21:24:26 -0500712 while (1) {
Jan Kara47aaabd2020-07-15 14:06:21 +0200713 /*
714 * User can supply arbitrarily large buffer. Avoid softlockups
715 * in case there are lots of available events.
716 */
717 cond_resched();
Jan Kara7088f352020-03-24 17:04:20 +0100718 event = get_one_event(group, count);
719 if (IS_ERR(event)) {
720 ret = PTR_ERR(event);
Jan Karad8aaab42014-04-03 14:46:35 -0700721 break;
722 }
723
Jan Kara7088f352020-03-24 17:04:20 +0100724 if (!event) {
Jan Karad8aaab42014-04-03 14:46:35 -0700725 ret = -EAGAIN;
726 if (file->f_flags & O_NONBLOCK)
Eric Parisa1014f12009-12-17 21:24:26 -0500727 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700728
729 ret = -ERESTARTSYS;
730 if (signal_pending(current))
Eric Parisa1014f12009-12-17 21:24:26 -0500731 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700732
733 if (start != buf)
734 break;
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100735
736 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Eric Parisa1014f12009-12-17 21:24:26 -0500737 continue;
738 }
739
Jan Kara7088f352020-03-24 17:04:20 +0100740 ret = copy_event_to_user(group, event, buf, count);
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300741 if (unlikely(ret == -EOPENSTALE)) {
742 /*
743 * We cannot report events with stale fd so drop it.
744 * Setting ret to 0 will continue the event loop and
745 * do the right thing if there are no more events to
746 * read (i.e. return bytes read, -EAGAIN or wait).
747 */
748 ret = 0;
749 }
750
Jan Karad8aaab42014-04-03 14:46:35 -0700751 /*
752 * Permission events get queued to wait for response. Other
753 * events can be destroyed now.
754 */
Jan Kara7088f352020-03-24 17:04:20 +0100755 if (!fanotify_is_perm_event(event->mask)) {
756 fsnotify_destroy_event(group, &event->fse);
Jan Karad5078162014-04-03 14:46:36 -0700757 } else {
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300758 if (ret <= 0) {
Jan Kara40873282019-01-08 14:02:44 +0100759 spin_lock(&group->notification_lock);
760 finish_permission_event(group,
Jan Kara7088f352020-03-24 17:04:20 +0100761 FANOTIFY_PERM(event), FAN_DENY);
Jan Karad5078162014-04-03 14:46:36 -0700762 wake_up(&group->fanotify_data.access_waitq);
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300763 } else {
764 spin_lock(&group->notification_lock);
Jan Kara7088f352020-03-24 17:04:20 +0100765 list_add_tail(&event->fse.list,
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300766 &group->fanotify_data.access_list);
767 spin_unlock(&group->notification_lock);
Jan Karad5078162014-04-03 14:46:36 -0700768 }
Jan Karad5078162014-04-03 14:46:36 -0700769 }
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300770 if (ret < 0)
771 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700772 buf += ret;
773 count -= ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500774 }
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100775 remove_wait_queue(&group->notification_waitq, &wait);
Eric Parisa1014f12009-12-17 21:24:26 -0500776
Eric Parisa1014f12009-12-17 21:24:26 -0500777 if (start != buf && ret != -EFAULT)
778 ret = buf - start;
779 return ret;
780}
781
Eric Parisb2d87902009-12-17 21:24:34 -0500782static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
783{
Eric Parisb2d87902009-12-17 21:24:34 -0500784 struct fanotify_response response = { .fd = -1, .response = -1 };
785 struct fsnotify_group *group;
786 int ret;
787
Miklos Szeredi6685df32017-10-30 21:14:56 +0100788 if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
789 return -EINVAL;
790
Eric Parisb2d87902009-12-17 21:24:34 -0500791 group = file->private_data;
792
Fabian Frederick5e236632020-05-12 20:19:21 +0200793 if (count < sizeof(response))
794 return -EINVAL;
795
796 count = sizeof(response);
Eric Parisb2d87902009-12-17 21:24:34 -0500797
798 pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
799
800 if (copy_from_user(&response, buf, count))
801 return -EFAULT;
802
803 ret = process_access_response(group, &response);
804 if (ret < 0)
805 count = ret;
806
807 return count;
Eric Parisb2d87902009-12-17 21:24:34 -0500808}
809
Eric Paris52c923d2009-12-17 21:24:26 -0500810static int fanotify_release(struct inode *ignored, struct file *file)
811{
812 struct fsnotify_group *group = file->private_data;
Amir Goldstein6f731712021-03-04 12:48:22 +0200813 struct fsnotify_event *fsn_event;
Andrew Morton19ba54f2010-10-28 17:21:59 -0400814
Jan Kara5838d442014-08-06 16:03:28 -0700815 /*
Jan Kara96d41012016-09-19 14:44:30 -0700816 * Stop new events from arriving in the notification queue. since
817 * userspace cannot use fanotify fd anymore, no event can enter or
818 * leave access_list by now either.
819 */
820 fsnotify_group_stop_queueing(group);
821
822 /*
823 * Process all permission events on access_list and notification queue
824 * and simulate reply from userspace.
Jan Kara5838d442014-08-06 16:03:28 -0700825 */
Jan Kara073f6552016-10-07 16:56:55 -0700826 spin_lock(&group->notification_lock);
Jan Karaca6f8692019-01-09 13:21:01 +0100827 while (!list_empty(&group->fanotify_data.access_list)) {
Jan Kara7088f352020-03-24 17:04:20 +0100828 struct fanotify_perm_event *event;
829
Jan Karaca6f8692019-01-09 13:21:01 +0100830 event = list_first_entry(&group->fanotify_data.access_list,
831 struct fanotify_perm_event, fae.fse.list);
Jan Karaf0834412014-04-03 14:46:33 -0700832 list_del_init(&event->fae.fse.list);
Jan Kara40873282019-01-08 14:02:44 +0100833 finish_permission_event(group, event, FAN_ALLOW);
834 spin_lock(&group->notification_lock);
Eric Paris2eebf582010-08-18 12:25:50 -0400835 }
Eric Paris2eebf582010-08-18 12:25:50 -0400836
Jan Kara5838d442014-08-06 16:03:28 -0700837 /*
Jan Kara96d41012016-09-19 14:44:30 -0700838 * Destroy all non-permission events. For permission events just
839 * dequeue them and set the response. They will be freed once the
840 * response is consumed and fanotify_get_response() returns.
Jan Kara5838d442014-08-06 16:03:28 -0700841 */
Amir Goldstein6f731712021-03-04 12:48:22 +0200842 while ((fsn_event = fsnotify_remove_first_event(group))) {
843 struct fanotify_event *event = FANOTIFY_E(fsn_event);
Jan Kara7088f352020-03-24 17:04:20 +0100844
Jan Kara7088f352020-03-24 17:04:20 +0100845 if (!(event->mask & FANOTIFY_PERM_EVENTS)) {
Jan Karac21dbe22016-10-07 16:56:52 -0700846 spin_unlock(&group->notification_lock);
Amir Goldstein6f731712021-03-04 12:48:22 +0200847 fsnotify_destroy_event(group, fsn_event);
Miklos Szeredi6685df32017-10-30 21:14:56 +0100848 } else {
Jan Kara7088f352020-03-24 17:04:20 +0100849 finish_permission_event(group, FANOTIFY_PERM(event),
Jan Kara40873282019-01-08 14:02:44 +0100850 FAN_ALLOW);
Miklos Szeredi6685df32017-10-30 21:14:56 +0100851 }
Jan Kara40873282019-01-08 14:02:44 +0100852 spin_lock(&group->notification_lock);
Jan Kara96d41012016-09-19 14:44:30 -0700853 }
Jan Karac21dbe22016-10-07 16:56:52 -0700854 spin_unlock(&group->notification_lock);
Jan Kara96d41012016-09-19 14:44:30 -0700855
856 /* Response for all permission events it set, wakeup waiters */
Eric Paris2eebf582010-08-18 12:25:50 -0400857 wake_up(&group->fanotify_data.access_waitq);
Eric Paris0a6b6bd2011-10-14 17:43:39 -0400858
Eric Paris52c923d2009-12-17 21:24:26 -0500859 /* matches the fanotify_init->fsnotify_alloc_group */
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200860 fsnotify_destroy_group(group);
Eric Paris52c923d2009-12-17 21:24:26 -0500861
862 return 0;
863}
864
Eric Parisa1014f12009-12-17 21:24:26 -0500865static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
866{
867 struct fsnotify_group *group;
Jan Kara7053aee2014-01-21 15:48:14 -0800868 struct fsnotify_event *fsn_event;
Eric Parisa1014f12009-12-17 21:24:26 -0500869 void __user *p;
870 int ret = -ENOTTY;
871 size_t send_len = 0;
872
873 group = file->private_data;
874
875 p = (void __user *) arg;
876
877 switch (cmd) {
878 case FIONREAD:
Jan Karac21dbe22016-10-07 16:56:52 -0700879 spin_lock(&group->notification_lock);
Jan Kara7053aee2014-01-21 15:48:14 -0800880 list_for_each_entry(fsn_event, &group->notification_list, list)
Eric Parisa1014f12009-12-17 21:24:26 -0500881 send_len += FAN_EVENT_METADATA_LEN;
Jan Karac21dbe22016-10-07 16:56:52 -0700882 spin_unlock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500883 ret = put_user(send_len, (int __user *) p);
884 break;
885 }
886
887 return ret;
888}
889
Eric Paris52c923d2009-12-17 21:24:26 -0500890static const struct file_operations fanotify_fops = {
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800891 .show_fdinfo = fanotify_show_fdinfo,
Eric Parisa1014f12009-12-17 21:24:26 -0500892 .poll = fanotify_poll,
893 .read = fanotify_read,
Eric Parisb2d87902009-12-17 21:24:34 -0500894 .write = fanotify_write,
Eric Paris52c923d2009-12-17 21:24:26 -0500895 .fasync = NULL,
896 .release = fanotify_release,
Eric Parisa1014f12009-12-17 21:24:26 -0500897 .unlocked_ioctl = fanotify_ioctl,
Arnd Bergmann1832f2d2018-09-11 21:59:08 +0200898 .compat_ioctl = compat_ptr_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200899 .llseek = noop_llseek,
Eric Paris52c923d2009-12-17 21:24:26 -0500900};
901
Eric Paris2a3edf82009-12-17 21:24:26 -0500902static int fanotify_find_path(int dfd, const char __user *filename,
Aaron Goidelac5656d2019-08-12 11:20:00 -0400903 struct path *path, unsigned int flags, __u64 mask,
904 unsigned int obj_type)
Eric Paris2a3edf82009-12-17 21:24:26 -0500905{
906 int ret;
907
908 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
909 dfd, filename, flags);
910
911 if (filename == NULL) {
Al Viro2903ff02012-08-28 12:52:22 -0400912 struct fd f = fdget(dfd);
Eric Paris2a3edf82009-12-17 21:24:26 -0500913
914 ret = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -0400915 if (!f.file)
Eric Paris2a3edf82009-12-17 21:24:26 -0500916 goto out;
917
918 ret = -ENOTDIR;
919 if ((flags & FAN_MARK_ONLYDIR) &&
Al Viro496ad9a2013-01-23 17:07:38 -0500920 !(S_ISDIR(file_inode(f.file)->i_mode))) {
Al Viro2903ff02012-08-28 12:52:22 -0400921 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500922 goto out;
923 }
924
Al Viro2903ff02012-08-28 12:52:22 -0400925 *path = f.file->f_path;
Eric Paris2a3edf82009-12-17 21:24:26 -0500926 path_get(path);
Al Viro2903ff02012-08-28 12:52:22 -0400927 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500928 } else {
929 unsigned int lookup_flags = 0;
930
931 if (!(flags & FAN_MARK_DONT_FOLLOW))
932 lookup_flags |= LOOKUP_FOLLOW;
933 if (flags & FAN_MARK_ONLYDIR)
934 lookup_flags |= LOOKUP_DIRECTORY;
935
936 ret = user_path_at(dfd, filename, lookup_flags, path);
937 if (ret)
938 goto out;
939 }
940
941 /* you can only watch an inode if you have read permissions on it */
Christian Brauner02f92b32021-01-21 14:19:22 +0100942 ret = path_permission(path, MAY_READ);
Aaron Goidelac5656d2019-08-12 11:20:00 -0400943 if (ret) {
944 path_put(path);
945 goto out;
946 }
947
948 ret = security_path_notify(path, mask, obj_type);
Eric Paris2a3edf82009-12-17 21:24:26 -0500949 if (ret)
950 path_put(path);
Aaron Goidelac5656d2019-08-12 11:20:00 -0400951
Eric Paris2a3edf82009-12-17 21:24:26 -0500952out:
953 return ret;
954}
955
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500956static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
Amir Goldstein4ed68142020-07-16 11:42:14 +0300957 __u32 mask, unsigned int flags,
958 __u32 umask, int *destroy)
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500959{
Lino Sanfilippod2c18742015-02-10 14:08:24 -0800960 __u32 oldmask = 0;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500961
Amir Goldstein4ed68142020-07-16 11:42:14 +0300962 /* umask bits cannot be removed by user */
963 mask &= ~umask;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500964 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500965 if (!(flags & FAN_MARK_IGNORED_MASK)) {
966 oldmask = fsn_mark->mask;
Amir Goldsteina72fd222018-10-04 00:25:34 +0300967 fsn_mark->mask &= ~mask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500968 } else {
Amir Goldsteina72fd222018-10-04 00:25:34 +0300969 fsn_mark->ignored_mask &= ~mask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500970 }
Amir Goldstein4ed68142020-07-16 11:42:14 +0300971 /*
972 * We need to keep the mark around even if remaining mask cannot
973 * result in any events (e.g. mask == FAN_ONDIR) to support incremenal
974 * changes to the mask.
975 * Destroy mark when only umask bits remain.
976 */
977 *destroy = !((fsn_mark->mask | fsn_mark->ignored_mask) & ~umask);
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500978 spin_unlock(&fsn_mark->lock);
979
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500980 return mask & oldmask;
981}
982
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300983static int fanotify_remove_mark(struct fsnotify_group *group,
984 fsnotify_connp_t *connp, __u32 mask,
Amir Goldstein4ed68142020-07-16 11:42:14 +0300985 unsigned int flags, __u32 umask)
Eric Paris88826272009-12-17 21:24:28 -0500986{
987 struct fsnotify_mark *fsn_mark = NULL;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500988 __u32 removed;
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200989 int destroy_mark;
Eric Paris88826272009-12-17 21:24:28 -0500990
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700991 mutex_lock(&group->mark_mutex);
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300992 fsn_mark = fsnotify_find_mark(connp, group);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700993 if (!fsn_mark) {
994 mutex_unlock(&group->mark_mutex);
Eric Paris88826272009-12-17 21:24:28 -0500995 return -ENOENT;
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700996 }
Eric Paris88826272009-12-17 21:24:28 -0500997
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200998 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
Amir Goldstein4ed68142020-07-16 11:42:14 +0300999 umask, &destroy_mark);
Amir Goldstein3ac70bf2018-06-23 17:54:50 +03001000 if (removed & fsnotify_conn_mask(fsn_mark->connector))
1001 fsnotify_recalc_mask(fsn_mark->connector);
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +02001002 if (destroy_mark)
Jan Kara4712e7222015-09-04 15:43:12 -07001003 fsnotify_detach_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -07001004 mutex_unlock(&group->mark_mutex);
Jan Kara4712e7222015-09-04 15:43:12 -07001005 if (destroy_mark)
1006 fsnotify_free_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -07001007
Jan Karab1362ed2016-12-21 16:28:45 +01001008 /* matches the fsnotify_find_mark() */
Eric Paris2a3edf82009-12-17 21:24:26 -05001009 fsnotify_put_mark(fsn_mark);
Eric Paris2a3edf82009-12-17 21:24:26 -05001010 return 0;
1011}
1012
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001013static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
1014 struct vfsmount *mnt, __u32 mask,
Amir Goldstein4ed68142020-07-16 11:42:14 +03001015 unsigned int flags, __u32 umask)
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001016{
1017 return fanotify_remove_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
Amir Goldstein4ed68142020-07-16 11:42:14 +03001018 mask, flags, umask);
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001019}
1020
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001021static int fanotify_remove_sb_mark(struct fsnotify_group *group,
Amir Goldstein4ed68142020-07-16 11:42:14 +03001022 struct super_block *sb, __u32 mask,
1023 unsigned int flags, __u32 umask)
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001024{
Amir Goldstein4ed68142020-07-16 11:42:14 +03001025 return fanotify_remove_mark(group, &sb->s_fsnotify_marks, mask,
1026 flags, umask);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001027}
1028
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001029static int fanotify_remove_inode_mark(struct fsnotify_group *group,
1030 struct inode *inode, __u32 mask,
Amir Goldstein4ed68142020-07-16 11:42:14 +03001031 unsigned int flags, __u32 umask)
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001032{
1033 return fanotify_remove_mark(group, &inode->i_fsnotify_marks, mask,
Amir Goldstein4ed68142020-07-16 11:42:14 +03001034 flags, umask);
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001035}
1036
Eric Parisb9e4e3b2009-12-17 21:24:33 -05001037static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
1038 __u32 mask,
1039 unsigned int flags)
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -05001040{
Eric Paris192ca4d2010-10-28 17:21:59 -04001041 __u32 oldmask = -1;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -05001042
1043 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -05001044 if (!(flags & FAN_MARK_IGNORED_MASK)) {
1045 oldmask = fsn_mark->mask;
Amir Goldsteina72fd222018-10-04 00:25:34 +03001046 fsn_mark->mask |= mask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -05001047 } else {
Amir Goldsteina72fd222018-10-04 00:25:34 +03001048 fsn_mark->ignored_mask |= mask;
Eric Parisc9778a92009-12-17 21:24:33 -05001049 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
1050 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
Eric Parisb9e4e3b2009-12-17 21:24:33 -05001051 }
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -05001052 spin_unlock(&fsn_mark->lock);
1053
1054 return mask & ~oldmask;
1055}
1056
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001057static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
Amir Goldsteinb812a9f2018-06-23 17:54:48 +03001058 fsnotify_connp_t *connp,
Amir Goldsteinad69cd92021-11-29 22:15:27 +02001059 unsigned int obj_type,
Amir Goldstein77115222019-01-10 19:04:37 +02001060 __kernel_fsid_t *fsid)
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001061{
Amir Goldstein5b8fea62021-03-04 13:29:20 +02001062 struct ucounts *ucounts = group->fanotify_data.ucounts;
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001063 struct fsnotify_mark *mark;
1064 int ret;
1065
Amir Goldstein5b8fea62021-03-04 13:29:20 +02001066 /*
1067 * Enforce per user marks limits per user in all containing user ns.
1068 * A group with FAN_UNLIMITED_MARKS does not contribute to mark count
1069 * in the limited groups account.
1070 */
1071 if (!FAN_GROUP_FLAG(group, FAN_UNLIMITED_MARKS) &&
1072 !inc_ucount(ucounts->ns, ucounts->uid, UCOUNT_FANOTIFY_MARKS))
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001073 return ERR_PTR(-ENOSPC);
1074
1075 mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
Amir Goldstein5b8fea62021-03-04 13:29:20 +02001076 if (!mark) {
1077 ret = -ENOMEM;
1078 goto out_dec_ucounts;
1079 }
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001080
Jan Kara054c6362016-12-21 18:06:12 +01001081 fsnotify_init_mark(mark, group);
Amir Goldsteinad69cd92021-11-29 22:15:27 +02001082 ret = fsnotify_add_mark_locked(mark, connp, obj_type, 0, fsid);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001083 if (ret) {
1084 fsnotify_put_mark(mark);
Amir Goldstein5b8fea62021-03-04 13:29:20 +02001085 goto out_dec_ucounts;
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001086 }
1087
1088 return mark;
Amir Goldstein5b8fea62021-03-04 13:29:20 +02001089
1090out_dec_ucounts:
1091 if (!FAN_GROUP_FLAG(group, FAN_UNLIMITED_MARKS))
1092 dec_ucount(ucounts, UCOUNT_FANOTIFY_MARKS);
1093 return ERR_PTR(ret);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001094}
1095
Gabriel Krisman Bertazi734a1a52021-10-25 16:27:34 -03001096static int fanotify_group_init_error_pool(struct fsnotify_group *group)
1097{
1098 if (mempool_initialized(&group->fanotify_data.error_events_pool))
1099 return 0;
1100
1101 return mempool_init_kmalloc_pool(&group->fanotify_data.error_events_pool,
1102 FANOTIFY_DEFAULT_FEE_POOL_SIZE,
1103 sizeof(struct fanotify_error_event));
1104}
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001105
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001106static int fanotify_add_mark(struct fsnotify_group *group,
Amir Goldsteinad69cd92021-11-29 22:15:27 +02001107 fsnotify_connp_t *connp, unsigned int obj_type,
Amir Goldstein77115222019-01-10 19:04:37 +02001108 __u32 mask, unsigned int flags,
1109 __kernel_fsid_t *fsid)
Eric Paris2a3edf82009-12-17 21:24:26 -05001110{
1111 struct fsnotify_mark *fsn_mark;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -05001112 __u32 added;
Gabriel Krisman Bertazi734a1a52021-10-25 16:27:34 -03001113 int ret = 0;
Eric Paris2a3edf82009-12-17 21:24:26 -05001114
Lino Sanfilippo7b185272013-07-08 15:59:42 -07001115 mutex_lock(&group->mark_mutex);
Amir Goldsteinb812a9f2018-06-23 17:54:48 +03001116 fsn_mark = fsnotify_find_mark(connp, group);
Eric Paris88826272009-12-17 21:24:28 -05001117 if (!fsn_mark) {
Amir Goldsteinad69cd92021-11-29 22:15:27 +02001118 fsn_mark = fanotify_add_new_mark(group, connp, obj_type, fsid);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001119 if (IS_ERR(fsn_mark)) {
Lino Sanfilippo7b185272013-07-08 15:59:42 -07001120 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001121 return PTR_ERR(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -07001122 }
Eric Paris88826272009-12-17 21:24:28 -05001123 }
Gabriel Krisman Bertazi734a1a52021-10-25 16:27:34 -03001124
1125 /*
1126 * Error events are pre-allocated per group, only if strictly
1127 * needed (i.e. FAN_FS_ERROR was requested).
1128 */
1129 if (!(flags & FAN_MARK_IGNORED_MASK) && (mask & FAN_FS_ERROR)) {
1130 ret = fanotify_group_init_error_pool(group);
1131 if (ret)
1132 goto out;
1133 }
1134
Eric Parisb9e4e3b2009-12-17 21:24:33 -05001135 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
Amir Goldstein3ac70bf2018-06-23 17:54:50 +03001136 if (added & ~fsnotify_conn_mask(fsn_mark->connector))
1137 fsnotify_recalc_mask(fsn_mark->connector);
Gabriel Krisman Bertazi734a1a52021-10-25 16:27:34 -03001138
1139out:
Jan Karac9747642016-12-14 13:53:46 +01001140 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -07001141
Lino Sanfilippofa218ab2010-11-09 18:18:16 +01001142 fsnotify_put_mark(fsn_mark);
Gabriel Krisman Bertazi734a1a52021-10-25 16:27:34 -03001143 return ret;
Eric Paris88826272009-12-17 21:24:28 -05001144}
1145
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001146static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
1147 struct vfsmount *mnt, __u32 mask,
Amir Goldstein77115222019-01-10 19:04:37 +02001148 unsigned int flags, __kernel_fsid_t *fsid)
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001149{
1150 return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
Amir Goldstein77115222019-01-10 19:04:37 +02001151 FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags, fsid);
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001152}
1153
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001154static int fanotify_add_sb_mark(struct fsnotify_group *group,
Amir Goldstein77115222019-01-10 19:04:37 +02001155 struct super_block *sb, __u32 mask,
1156 unsigned int flags, __kernel_fsid_t *fsid)
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001157{
1158 return fanotify_add_mark(group, &sb->s_fsnotify_marks,
Amir Goldstein77115222019-01-10 19:04:37 +02001159 FSNOTIFY_OBJ_TYPE_SB, mask, flags, fsid);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001160}
1161
Andreas Gruenbacher52202df2009-12-17 21:24:28 -05001162static int fanotify_add_inode_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -05001163 struct inode *inode, __u32 mask,
Amir Goldstein77115222019-01-10 19:04:37 +02001164 unsigned int flags, __kernel_fsid_t *fsid)
Eric Paris88826272009-12-17 21:24:28 -05001165{
Eric Paris88826272009-12-17 21:24:28 -05001166 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
Eric Paris2a3edf82009-12-17 21:24:26 -05001167
Eric Paris5322a592010-10-28 17:21:57 -04001168 /*
1169 * If some other task has this inode open for write we should not add
1170 * an ignored mark, unless that ignored mark is supposed to survive
1171 * modification changes anyway.
1172 */
1173 if ((flags & FAN_MARK_IGNORED_MASK) &&
1174 !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
Nikolay Borisovac9498d2018-12-11 10:27:23 +02001175 inode_is_open_for_write(inode))
Eric Paris5322a592010-10-28 17:21:57 -04001176 return 0;
1177
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +03001178 return fanotify_add_mark(group, &inode->i_fsnotify_marks,
Amir Goldstein77115222019-01-10 19:04:37 +02001179 FSNOTIFY_OBJ_TYPE_INODE, mask, flags, fsid);
Eric Paris88826272009-12-17 21:24:28 -05001180}
Eric Paris2a3edf82009-12-17 21:24:26 -05001181
Amir Goldsteinb8a6c3a2020-07-08 14:11:42 +03001182static struct fsnotify_event *fanotify_alloc_overflow_event(void)
1183{
1184 struct fanotify_event *oevent;
1185
1186 oevent = kmalloc(sizeof(*oevent), GFP_KERNEL_ACCOUNT);
1187 if (!oevent)
1188 return NULL;
1189
1190 fanotify_init_event(oevent, 0, FS_Q_OVERFLOW);
1191 oevent->type = FANOTIFY_EVENT_TYPE_OVERFLOW;
1192
1193 return &oevent->fse;
1194}
1195
Amir Goldstein94e00d22021-03-04 12:48:25 +02001196static struct hlist_head *fanotify_alloc_merge_hash(void)
1197{
1198 struct hlist_head *hash;
1199
1200 hash = kmalloc(sizeof(struct hlist_head) << FANOTIFY_HTABLE_BITS,
1201 GFP_KERNEL_ACCOUNT);
1202 if (!hash)
1203 return NULL;
1204
1205 __hash_init(hash, FANOTIFY_HTABLE_SIZE);
1206
1207 return hash;
1208}
1209
Eric Paris52c923d2009-12-17 21:24:26 -05001210/* fanotify syscalls */
Eric Paris08ae8932010-05-27 09:41:40 -04001211SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
Eric Paris11637e42009-12-17 21:24:25 -05001212{
Eric Paris52c923d2009-12-17 21:24:26 -05001213 struct fsnotify_group *group;
1214 int f_flags, fd;
Amir Goldstein83b7a592020-07-16 11:42:26 +03001215 unsigned int fid_mode = flags & FANOTIFY_FID_BITS;
1216 unsigned int class = flags & FANOTIFY_CLASS_BITS;
Amir Goldsteina8b98c82021-05-24 16:53:21 +03001217 unsigned int internal_flags = 0;
Eric Paris52c923d2009-12-17 21:24:26 -05001218
Amir Goldstein96a71f22018-09-21 21:20:30 +03001219 pr_debug("%s: flags=%x event_f_flags=%x\n",
1220 __func__, flags, event_f_flags);
Eric Paris52c923d2009-12-17 21:24:26 -05001221
Amir Goldstein7cea2a32021-03-04 13:29:21 +02001222 if (!capable(CAP_SYS_ADMIN)) {
1223 /*
1224 * An unprivileged user can setup an fanotify group with
1225 * limited functionality - an unprivileged group is limited to
1226 * notification events with file handles and it cannot use
1227 * unlimited queue/marks.
1228 */
1229 if ((flags & FANOTIFY_ADMIN_INIT_FLAGS) || !fid_mode)
1230 return -EPERM;
Amir Goldsteina8b98c82021-05-24 16:53:21 +03001231
1232 /*
1233 * Setting the internal flag FANOTIFY_UNPRIV on the group
1234 * prevents setting mount/filesystem marks on this group and
1235 * prevents reporting pid and open fd in events.
1236 */
1237 internal_flags |= FANOTIFY_UNPRIV;
Amir Goldstein7cea2a32021-03-04 13:29:21 +02001238 }
Eric Paris52c923d2009-12-17 21:24:26 -05001239
Steve Grubbde8cd832017-10-02 20:21:39 -04001240#ifdef CONFIG_AUDITSYSCALL
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001241 if (flags & ~(FANOTIFY_INIT_FLAGS | FAN_ENABLE_AUDIT))
Steve Grubbde8cd832017-10-02 20:21:39 -04001242#else
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001243 if (flags & ~FANOTIFY_INIT_FLAGS)
Steve Grubbde8cd832017-10-02 20:21:39 -04001244#endif
Eric Paris52c923d2009-12-17 21:24:26 -05001245 return -EINVAL;
1246
Matthew Bobrowskiaf579be2021-08-08 15:26:25 +10001247 /*
1248 * A pidfd can only be returned for a thread-group leader; thus
1249 * FAN_REPORT_PIDFD and FAN_REPORT_TID need to remain mutually
1250 * exclusive.
1251 */
1252 if ((flags & FAN_REPORT_PIDFD) && (flags & FAN_REPORT_TID))
1253 return -EINVAL;
1254
Heinrich Schuchardt48149e92014-06-04 16:05:44 -07001255 if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS)
1256 return -EINVAL;
1257
1258 switch (event_f_flags & O_ACCMODE) {
1259 case O_RDONLY:
1260 case O_RDWR:
1261 case O_WRONLY:
1262 break;
1263 default:
1264 return -EINVAL;
1265 }
1266
Amir Goldstein83b7a592020-07-16 11:42:26 +03001267 if (fid_mode && class != FAN_CLASS_NOTIF)
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001268 return -EINVAL;
1269
Amir Goldstein929943b2020-07-16 11:42:28 +03001270 /*
Amir Goldstein929943b2020-07-16 11:42:28 +03001271 * Child name is reported with parent fid so requires dir fid.
Amir Goldstein691d9762020-07-16 11:42:30 +03001272 * We can report both child fid and dir fid with or without name.
Amir Goldstein929943b2020-07-16 11:42:28 +03001273 */
Amir Goldstein691d9762020-07-16 11:42:30 +03001274 if ((fid_mode & FAN_REPORT_NAME) && !(fid_mode & FAN_REPORT_DIR_FID))
Amir Goldstein83b7a592020-07-16 11:42:26 +03001275 return -EINVAL;
Amir Goldstein83b7a592020-07-16 11:42:26 +03001276
Amir Goldsteind61fd652021-11-29 22:15:29 +02001277 /*
1278 * FAN_REPORT_TARGET_FID requires FAN_REPORT_NAME and FAN_REPORT_FID
1279 * and is used as an indication to report both dir and child fid on all
1280 * dirent events.
1281 */
1282 if ((fid_mode & FAN_REPORT_TARGET_FID) &&
1283 (!(fid_mode & FAN_REPORT_NAME) || !(fid_mode & FAN_REPORT_FID)))
1284 return -EINVAL;
1285
Eric Parisb2d87902009-12-17 21:24:34 -05001286 f_flags = O_RDWR | FMODE_NONOTIFY;
Eric Paris52c923d2009-12-17 21:24:26 -05001287 if (flags & FAN_CLOEXEC)
1288 f_flags |= O_CLOEXEC;
1289 if (flags & FAN_NONBLOCK)
1290 f_flags |= O_NONBLOCK;
1291
1292 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
Shakeel Buttac7b79f2020-12-19 20:46:08 -08001293 group = fsnotify_alloc_user_group(&fanotify_fsnotify_ops);
Eric Paris26379192010-11-23 23:48:26 -05001294 if (IS_ERR(group)) {
Eric Paris52c923d2009-12-17 21:24:26 -05001295 return PTR_ERR(group);
Eric Paris26379192010-11-23 23:48:26 -05001296 }
Eric Paris52c923d2009-12-17 21:24:26 -05001297
Amir Goldstein5b8fea62021-03-04 13:29:20 +02001298 /* Enforce groups limits per user in all containing user ns */
1299 group->fanotify_data.ucounts = inc_ucount(current_user_ns(),
1300 current_euid(),
1301 UCOUNT_FANOTIFY_GROUPS);
1302 if (!group->fanotify_data.ucounts) {
1303 fd = -EMFILE;
1304 goto out_destroy_group;
1305 }
1306
Amir Goldsteina8b98c82021-05-24 16:53:21 +03001307 group->fanotify_data.flags = flags | internal_flags;
Shakeel Buttd46eb14b2018-08-17 15:46:39 -07001308 group->memcg = get_mem_cgroup_from_mm(current->mm);
Eric Paris4afeff82010-10-28 17:21:58 -04001309
Amir Goldstein94e00d22021-03-04 12:48:25 +02001310 group->fanotify_data.merge_hash = fanotify_alloc_merge_hash();
1311 if (!group->fanotify_data.merge_hash) {
1312 fd = -ENOMEM;
1313 goto out_destroy_group;
1314 }
1315
Amir Goldsteinb8a6c3a2020-07-08 14:11:42 +03001316 group->overflow_event = fanotify_alloc_overflow_event();
1317 if (unlikely(!group->overflow_event)) {
Jan Karaff57cd52014-02-21 19:14:11 +01001318 fd = -ENOMEM;
1319 goto out_destroy_group;
1320 }
Jan Karaff57cd52014-02-21 19:14:11 +01001321
Will Woods1e2ee492014-05-06 12:50:10 -07001322 if (force_o_largefile())
1323 event_f_flags |= O_LARGEFILE;
Eric Paris80af2582010-07-28 10:18:37 -04001324 group->fanotify_data.f_flags = event_f_flags;
Eric Paris9e66e422009-12-17 21:24:34 -05001325 init_waitqueue_head(&group->fanotify_data.access_waitq);
1326 INIT_LIST_HEAD(&group->fanotify_data.access_list);
Amir Goldstein83b7a592020-07-16 11:42:26 +03001327 switch (class) {
Eric Paris4231a232010-10-28 17:21:56 -04001328 case FAN_CLASS_NOTIF:
1329 group->priority = FS_PRIO_0;
1330 break;
1331 case FAN_CLASS_CONTENT:
1332 group->priority = FS_PRIO_1;
1333 break;
1334 case FAN_CLASS_PRE_CONTENT:
1335 group->priority = FS_PRIO_2;
1336 break;
1337 default:
1338 fd = -EINVAL;
Lino Sanfilippod8153d42011-06-14 17:29:45 +02001339 goto out_destroy_group;
Eric Paris4231a232010-10-28 17:21:56 -04001340 }
Eric Pariscb2d4292009-12-17 21:24:34 -05001341
Eric Paris5dd03f52010-10-28 17:21:57 -04001342 if (flags & FAN_UNLIMITED_QUEUE) {
1343 fd = -EPERM;
1344 if (!capable(CAP_SYS_ADMIN))
Lino Sanfilippod8153d42011-06-14 17:29:45 +02001345 goto out_destroy_group;
Eric Paris5dd03f52010-10-28 17:21:57 -04001346 group->max_events = UINT_MAX;
1347 } else {
Amir Goldstein5b8fea62021-03-04 13:29:20 +02001348 group->max_events = fanotify_max_queued_events;
Eric Paris5dd03f52010-10-28 17:21:57 -04001349 }
Eric Paris2529a0d2010-10-28 17:21:57 -04001350
Eric Parisac7e22d2010-10-28 17:21:58 -04001351 if (flags & FAN_UNLIMITED_MARKS) {
1352 fd = -EPERM;
1353 if (!capable(CAP_SYS_ADMIN))
Lino Sanfilippod8153d42011-06-14 17:29:45 +02001354 goto out_destroy_group;
Eric Parisac7e22d2010-10-28 17:21:58 -04001355 }
Eric Parise7099d82010-10-28 17:21:57 -04001356
Steve Grubbde8cd832017-10-02 20:21:39 -04001357 if (flags & FAN_ENABLE_AUDIT) {
1358 fd = -EPERM;
1359 if (!capable(CAP_AUDIT_WRITE))
1360 goto out_destroy_group;
Steve Grubbde8cd832017-10-02 20:21:39 -04001361 }
1362
Eric Paris52c923d2009-12-17 21:24:26 -05001363 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
1364 if (fd < 0)
Lino Sanfilippod8153d42011-06-14 17:29:45 +02001365 goto out_destroy_group;
Eric Paris52c923d2009-12-17 21:24:26 -05001366
1367 return fd;
1368
Lino Sanfilippod8153d42011-06-14 17:29:45 +02001369out_destroy_group:
1370 fsnotify_destroy_group(group);
Eric Paris52c923d2009-12-17 21:24:26 -05001371 return fd;
Eric Paris11637e42009-12-17 21:24:25 -05001372}
Eric Parisbbaa4162009-12-17 21:24:26 -05001373
Gabriel Krisman Bertazi82992122021-10-25 16:27:21 -03001374static int fanotify_test_fsid(struct dentry *dentry, __kernel_fsid_t *fsid)
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001375{
Amir Goldstein73072282019-01-10 19:04:39 +02001376 __kernel_fsid_t root_fsid;
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001377 int err;
1378
1379 /*
Gabriel Krisman Bertazi82992122021-10-25 16:27:21 -03001380 * Make sure dentry is not of a filesystem with zero fsid (e.g. fuse).
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001381 */
Gabriel Krisman Bertazi82992122021-10-25 16:27:21 -03001382 err = vfs_get_fsid(dentry, fsid);
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001383 if (err)
1384 return err;
1385
Amir Goldstein73072282019-01-10 19:04:39 +02001386 if (!fsid->val[0] && !fsid->val[1])
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001387 return -ENODEV;
1388
1389 /*
Gabriel Krisman Bertazi82992122021-10-25 16:27:21 -03001390 * Make sure dentry is not of a filesystem subvolume (e.g. btrfs)
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001391 * which uses a different fsid than sb root.
1392 */
Gabriel Krisman Bertazi82992122021-10-25 16:27:21 -03001393 err = vfs_get_fsid(dentry->d_sb->s_root, &root_fsid);
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001394 if (err)
1395 return err;
1396
Amir Goldstein73072282019-01-10 19:04:39 +02001397 if (root_fsid.val[0] != fsid->val[0] ||
1398 root_fsid.val[1] != fsid->val[1])
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001399 return -EXDEV;
1400
Gabriel Krisman Bertazi82992122021-10-25 16:27:21 -03001401 return 0;
1402}
1403
1404/* Check if filesystem can encode a unique fid */
1405static int fanotify_test_fid(struct dentry *dentry)
1406{
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001407 /*
1408 * We need to make sure that the file system supports at least
1409 * encoding a file handle so user can use name_to_handle_at() to
1410 * compare fid returned with event to the file handle of watched
1411 * objects. However, name_to_handle_at() requires that the
1412 * filesystem also supports decoding file handles.
1413 */
Gabriel Krisman Bertazi82992122021-10-25 16:27:21 -03001414 if (!dentry->d_sb->s_export_op ||
1415 !dentry->d_sb->s_export_op->fh_to_dentry)
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001416 return -EOPNOTSUPP;
1417
1418 return 0;
1419}
1420
Jan Kara0b3b0942019-05-15 16:28:34 +02001421static int fanotify_events_supported(struct path *path, __u64 mask)
1422{
1423 /*
1424 * Some filesystems such as 'proc' acquire unusual locks when opening
1425 * files. For them fanotify permission events have high chances of
1426 * deadlocking the system - open done when reporting fanotify event
1427 * blocks on this "unusual" lock while another process holding the lock
1428 * waits for fanotify permission event to be answered. Just disallow
1429 * permission events for such filesystems.
1430 */
1431 if (mask & FANOTIFY_PERM_EVENTS &&
1432 path->mnt->mnt_sb->s_type->fs_flags & FS_DISALLOW_NOTIFY_PERM)
1433 return -EINVAL;
1434 return 0;
1435}
1436
Dominik Brodowski183caa32018-03-17 15:06:11 +01001437static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
1438 int dfd, const char __user *pathname)
Eric Parisbbaa4162009-12-17 21:24:26 -05001439{
Eric Paris0ff21db2009-12-17 21:24:29 -05001440 struct inode *inode = NULL;
1441 struct vfsmount *mnt = NULL;
Eric Paris2a3edf82009-12-17 21:24:26 -05001442 struct fsnotify_group *group;
Al Viro2903ff02012-08-28 12:52:22 -04001443 struct fd f;
Eric Paris2a3edf82009-12-17 21:24:26 -05001444 struct path path;
Amir Goldstein73072282019-01-10 19:04:39 +02001445 __kernel_fsid_t __fsid, *fsid = NULL;
Amir Goldsteinbdd5a462018-10-04 00:25:37 +03001446 u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS;
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001447 unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS;
Amir Goldstein3ef86652020-07-16 11:42:13 +03001448 bool ignored = flags & FAN_MARK_IGNORED_MASK;
Amir Goldsteind809daf2020-07-16 11:42:12 +03001449 unsigned int obj_type, fid_mode;
Amir Goldstein85af5d92020-07-16 11:42:15 +03001450 u32 umask = 0;
Al Viro2903ff02012-08-28 12:52:22 -04001451 int ret;
Eric Paris2a3edf82009-12-17 21:24:26 -05001452
1453 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
1454 __func__, fanotify_fd, flags, dfd, pathname, mask);
1455
1456 /* we only use the lower 32 bits as of right now. */
Christian Brauner22d483b2021-03-25 09:37:43 +01001457 if (upper_32_bits(mask))
Eric Paris2a3edf82009-12-17 21:24:26 -05001458 return -EINVAL;
1459
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001460 if (flags & ~FANOTIFY_MARK_FLAGS)
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -05001461 return -EINVAL;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001462
1463 switch (mark_type) {
1464 case FAN_MARK_INODE:
Aaron Goidelac5656d2019-08-12 11:20:00 -04001465 obj_type = FSNOTIFY_OBJ_TYPE_INODE;
1466 break;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001467 case FAN_MARK_MOUNT:
Aaron Goidelac5656d2019-08-12 11:20:00 -04001468 obj_type = FSNOTIFY_OBJ_TYPE_VFSMOUNT;
1469 break;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001470 case FAN_MARK_FILESYSTEM:
Aaron Goidelac5656d2019-08-12 11:20:00 -04001471 obj_type = FSNOTIFY_OBJ_TYPE_SB;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001472 break;
1473 default:
1474 return -EINVAL;
1475 }
1476
Eric Paris4d926042009-12-17 21:24:34 -05001477 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001478 case FAN_MARK_ADD:
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -05001479 case FAN_MARK_REMOVE:
Lino Sanfilippo1734dee2010-11-22 18:46:33 +01001480 if (!mask)
1481 return -EINVAL;
Heinrich Schuchardtcc299a92014-06-04 16:05:43 -07001482 break;
Eric Paris4d926042009-12-17 21:24:34 -05001483 case FAN_MARK_FLUSH:
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001484 if (flags & ~(FANOTIFY_MARK_TYPE_BITS | FAN_MARK_FLUSH))
Heinrich Schuchardtcc299a92014-06-04 16:05:43 -07001485 return -EINVAL;
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -05001486 break;
1487 default:
1488 return -EINVAL;
1489 }
Eric Paris8fcd6522010-10-28 17:21:59 -04001490
Miklos Szeredi6685df32017-10-30 21:14:56 +01001491 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001492 valid_mask |= FANOTIFY_PERM_EVENTS;
Miklos Szeredi6685df32017-10-30 21:14:56 +01001493
1494 if (mask & ~valid_mask)
Eric Paris2a3edf82009-12-17 21:24:26 -05001495 return -EINVAL;
1496
Amir Goldstein3ef86652020-07-16 11:42:13 +03001497 /* Event flags (ONDIR, ON_CHILD) are meaningless in ignored mask */
1498 if (ignored)
1499 mask &= ~FANOTIFY_EVENT_FLAGS;
1500
Al Viro2903ff02012-08-28 12:52:22 -04001501 f = fdget(fanotify_fd);
1502 if (unlikely(!f.file))
Eric Paris2a3edf82009-12-17 21:24:26 -05001503 return -EBADF;
1504
1505 /* verify that this is indeed an fanotify instance */
1506 ret = -EINVAL;
Al Viro2903ff02012-08-28 12:52:22 -04001507 if (unlikely(f.file->f_op != &fanotify_fops))
Eric Paris2a3edf82009-12-17 21:24:26 -05001508 goto fput_and_out;
Al Viro2903ff02012-08-28 12:52:22 -04001509 group = f.file->private_data;
Eric Paris4231a232010-10-28 17:21:56 -04001510
1511 /*
Amir Goldsteina8b98c82021-05-24 16:53:21 +03001512 * An unprivileged user is not allowed to setup mount nor filesystem
1513 * marks. This also includes setting up such marks by a group that
1514 * was initialized by an unprivileged user.
Amir Goldstein7cea2a32021-03-04 13:29:21 +02001515 */
1516 ret = -EPERM;
Amir Goldsteina8b98c82021-05-24 16:53:21 +03001517 if ((!capable(CAP_SYS_ADMIN) ||
1518 FAN_GROUP_FLAG(group, FANOTIFY_UNPRIV)) &&
Amir Goldstein7cea2a32021-03-04 13:29:21 +02001519 mark_type != FAN_MARK_INODE)
1520 goto fput_and_out;
1521
1522 /*
Eric Paris4231a232010-10-28 17:21:56 -04001523 * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
1524 * allowed to set permissions events.
1525 */
1526 ret = -EINVAL;
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001527 if (mask & FANOTIFY_PERM_EVENTS &&
Eric Paris4231a232010-10-28 17:21:56 -04001528 group->priority == FS_PRIO_0)
1529 goto fput_and_out;
Eric Paris2a3edf82009-12-17 21:24:26 -05001530
Gabriel Krisman Bertazi9709bd52021-10-25 16:27:43 -03001531 if (mask & FAN_FS_ERROR &&
1532 mark_type != FAN_MARK_FILESYSTEM)
1533 goto fput_and_out;
1534
Amir Goldstein235328d2019-01-10 19:04:43 +02001535 /*
Gabriel Krisman Bertazi4fe595c2021-10-25 16:27:31 -03001536 * Events that do not carry enough information to report
1537 * event->fd require a group that supports reporting fid. Those
1538 * events are not supported on a mount mark, because they do not
1539 * carry enough information (i.e. path) to be filtered by mount
1540 * point.
Amir Goldstein235328d2019-01-10 19:04:43 +02001541 */
Amir Goldsteind809daf2020-07-16 11:42:12 +03001542 fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
Gabriel Krisman Bertazi4fe595c2021-10-25 16:27:31 -03001543 if (mask & ~(FANOTIFY_FD_EVENTS|FANOTIFY_EVENT_FLAGS) &&
Amir Goldsteind809daf2020-07-16 11:42:12 +03001544 (!fid_mode || mark_type == FAN_MARK_MOUNT))
Amir Goldstein235328d2019-01-10 19:04:43 +02001545 goto fput_and_out;
1546
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -07001547 if (flags & FAN_MARK_FLUSH) {
1548 ret = 0;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001549 if (mark_type == FAN_MARK_MOUNT)
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -07001550 fsnotify_clear_vfsmount_marks_by_group(group);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001551 else if (mark_type == FAN_MARK_FILESYSTEM)
1552 fsnotify_clear_sb_marks_by_group(group);
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -07001553 else
1554 fsnotify_clear_inode_marks_by_group(group);
1555 goto fput_and_out;
1556 }
1557
Aaron Goidelac5656d2019-08-12 11:20:00 -04001558 ret = fanotify_find_path(dfd, pathname, &path, flags,
1559 (mask & ALL_FSNOTIFY_EVENTS), obj_type);
Eric Paris2a3edf82009-12-17 21:24:26 -05001560 if (ret)
1561 goto fput_and_out;
1562
Jan Kara0b3b0942019-05-15 16:28:34 +02001563 if (flags & FAN_MARK_ADD) {
1564 ret = fanotify_events_supported(&path, mask);
1565 if (ret)
1566 goto path_put_and_out;
1567 }
1568
Amir Goldsteind809daf2020-07-16 11:42:12 +03001569 if (fid_mode) {
Gabriel Krisman Bertazi82992122021-10-25 16:27:21 -03001570 ret = fanotify_test_fsid(path.dentry, &__fsid);
1571 if (ret)
1572 goto path_put_and_out;
1573
1574 ret = fanotify_test_fid(path.dentry);
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001575 if (ret)
1576 goto path_put_and_out;
Amir Goldstein77115222019-01-10 19:04:37 +02001577
Amir Goldstein73072282019-01-10 19:04:39 +02001578 fsid = &__fsid;
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001579 }
1580
Eric Paris2a3edf82009-12-17 21:24:26 -05001581 /* inode held in place by reference to path; group by fget on fd */
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001582 if (mark_type == FAN_MARK_INODE)
Eric Paris0ff21db2009-12-17 21:24:29 -05001583 inode = path.dentry->d_inode;
1584 else
1585 mnt = path.mnt;
Eric Paris2a3edf82009-12-17 21:24:26 -05001586
Amir Goldstein85af5d92020-07-16 11:42:15 +03001587 /* Mask out FAN_EVENT_ON_CHILD flag for sb/mount/non-dir marks */
1588 if (mnt || !S_ISDIR(inode->i_mode)) {
1589 mask &= ~FAN_EVENT_ON_CHILD;
1590 umask = FAN_EVENT_ON_CHILD;
Amir Goldstein51280632020-07-16 11:42:27 +03001591 /*
1592 * If group needs to report parent fid, register for getting
1593 * events with parent/name info for non-directory.
1594 */
1595 if ((fid_mode & FAN_REPORT_DIR_FID) &&
1596 (flags & FAN_MARK_ADD) && !ignored)
1597 mask |= FAN_EVENT_ON_CHILD;
Amir Goldstein85af5d92020-07-16 11:42:15 +03001598 }
1599
Eric Paris2a3edf82009-12-17 21:24:26 -05001600 /* create/update an inode mark */
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -07001601 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -05001602 case FAN_MARK_ADD:
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001603 if (mark_type == FAN_MARK_MOUNT)
Amir Goldstein77115222019-01-10 19:04:37 +02001604 ret = fanotify_add_vfsmount_mark(group, mnt, mask,
1605 flags, fsid);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001606 else if (mark_type == FAN_MARK_FILESYSTEM)
Amir Goldstein77115222019-01-10 19:04:37 +02001607 ret = fanotify_add_sb_mark(group, mnt->mnt_sb, mask,
1608 flags, fsid);
Eric Paris0ff21db2009-12-17 21:24:29 -05001609 else
Amir Goldstein77115222019-01-10 19:04:37 +02001610 ret = fanotify_add_inode_mark(group, inode, mask,
1611 flags, fsid);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -05001612 break;
1613 case FAN_MARK_REMOVE:
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001614 if (mark_type == FAN_MARK_MOUNT)
Amir Goldstein77115222019-01-10 19:04:37 +02001615 ret = fanotify_remove_vfsmount_mark(group, mnt, mask,
Amir Goldstein85af5d92020-07-16 11:42:15 +03001616 flags, umask);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001617 else if (mark_type == FAN_MARK_FILESYSTEM)
Amir Goldstein77115222019-01-10 19:04:37 +02001618 ret = fanotify_remove_sb_mark(group, mnt->mnt_sb, mask,
Amir Goldstein85af5d92020-07-16 11:42:15 +03001619 flags, umask);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -05001620 else
Amir Goldstein77115222019-01-10 19:04:37 +02001621 ret = fanotify_remove_inode_mark(group, inode, mask,
Amir Goldstein85af5d92020-07-16 11:42:15 +03001622 flags, umask);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -05001623 break;
1624 default:
1625 ret = -EINVAL;
1626 }
Eric Paris2a3edf82009-12-17 21:24:26 -05001627
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001628path_put_and_out:
Eric Paris2a3edf82009-12-17 21:24:26 -05001629 path_put(&path);
1630fput_and_out:
Al Viro2903ff02012-08-28 12:52:22 -04001631 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -05001632 return ret;
Eric Parisbbaa4162009-12-17 21:24:26 -05001633}
Eric Paris2a3edf82009-12-17 21:24:26 -05001634
Brian Gerst2ca408d2020-11-30 17:30:59 -05001635#ifndef CONFIG_ARCH_SPLIT_ARG64
Dominik Brodowski183caa32018-03-17 15:06:11 +01001636SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
1637 __u64, mask, int, dfd,
1638 const char __user *, pathname)
1639{
1640 return do_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname);
1641}
Brian Gerst2ca408d2020-11-30 17:30:59 -05001642#endif
Dominik Brodowski183caa32018-03-17 15:06:11 +01001643
Brian Gerst2ca408d2020-11-30 17:30:59 -05001644#if defined(CONFIG_ARCH_SPLIT_ARG64) || defined(CONFIG_COMPAT)
1645SYSCALL32_DEFINE6(fanotify_mark,
Al Viro91c2e0b2013-03-05 20:10:59 -05001646 int, fanotify_fd, unsigned int, flags,
Brian Gerst2ca408d2020-11-30 17:30:59 -05001647 SC_ARG64(mask), int, dfd,
Al Viro91c2e0b2013-03-05 20:10:59 -05001648 const char __user *, pathname)
1649{
Brian Gerst2ca408d2020-11-30 17:30:59 -05001650 return do_fanotify_mark(fanotify_fd, flags, SC_VAL64(__u64, mask),
1651 dfd, pathname);
Al Viro91c2e0b2013-03-05 20:10:59 -05001652}
1653#endif
1654
Eric Paris2a3edf82009-12-17 21:24:26 -05001655/*
Justin P. Mattockae0e47f2011-03-01 15:06:02 +01001656 * fanotify_user_setup - Our initialization function. Note that we cannot return
Eric Paris2a3edf82009-12-17 21:24:26 -05001657 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
1658 * must result in panic().
1659 */
1660static int __init fanotify_user_setup(void)
1661{
Amir Goldstein5b8fea62021-03-04 13:29:20 +02001662 struct sysinfo si;
1663 int max_marks;
1664
1665 si_meminfo(&si);
1666 /*
1667 * Allow up to 1% of addressable memory to be accounted for per user
1668 * marks limited to the range [8192, 1048576]. mount and sb marks are
1669 * a lot cheaper than inode marks, but there is no reason for a user
1670 * to have many of those, so calculate by the cost of inode marks.
1671 */
1672 max_marks = (((si.totalram - si.totalhigh) / 100) << PAGE_SHIFT) /
1673 INODE_MARK_COST;
1674 max_marks = clamp(max_marks, FANOTIFY_OLD_DEFAULT_MAX_MARKS,
1675 FANOTIFY_DEFAULT_MAX_USER_MARKS);
1676
Amir Goldsteina8b98c82021-05-24 16:53:21 +03001677 BUILD_BUG_ON(FANOTIFY_INIT_FLAGS & FANOTIFY_INTERNAL_GROUP_FLAGS);
Amir Goldsteind61fd652021-11-29 22:15:29 +02001678 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 12);
Amir Goldsteinbdd5a462018-10-04 00:25:37 +03001679 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9);
1680
Shakeel Buttd46eb14b2018-08-17 15:46:39 -07001681 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark,
1682 SLAB_PANIC|SLAB_ACCOUNT);
Jan Kara7088f352020-03-24 17:04:20 +01001683 fanotify_fid_event_cachep = KMEM_CACHE(fanotify_fid_event,
1684 SLAB_PANIC);
1685 fanotify_path_event_cachep = KMEM_CACHE(fanotify_path_event,
1686 SLAB_PANIC);
Miklos Szeredi6685df32017-10-30 21:14:56 +01001687 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
1688 fanotify_perm_event_cachep =
Amir Goldstein33913992019-01-10 19:04:32 +02001689 KMEM_CACHE(fanotify_perm_event, SLAB_PANIC);
Miklos Szeredi6685df32017-10-30 21:14:56 +01001690 }
Eric Paris2a3edf82009-12-17 21:24:26 -05001691
Amir Goldstein5b8fea62021-03-04 13:29:20 +02001692 fanotify_max_queued_events = FANOTIFY_DEFAULT_MAX_EVENTS;
1693 init_user_ns.ucount_max[UCOUNT_FANOTIFY_GROUPS] =
1694 FANOTIFY_DEFAULT_MAX_GROUPS;
1695 init_user_ns.ucount_max[UCOUNT_FANOTIFY_MARKS] = max_marks;
1696
Eric Paris2a3edf82009-12-17 21:24:26 -05001697 return 0;
1698}
1699device_initcall(fanotify_user_setup);