blob: dcab112e1f0012073456c56b52b1ce4ac261d9a4 [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>
Eric Paris2a3edf82009-12-17 21:24:26 -05004#include <linux/file.h>
Eric Paris11637e42009-12-17 21:24:25 -05005#include <linux/fs.h>
Eric Paris52c923d2009-12-17 21:24:26 -05006#include <linux/anon_inodes.h>
Eric Paris11637e42009-12-17 21:24:25 -05007#include <linux/fsnotify_backend.h>
Eric Paris2a3edf82009-12-17 21:24:26 -05008#include <linux/init.h>
Eric Parisa1014f12009-12-17 21:24:26 -05009#include <linux/mount.h>
Eric Paris2a3edf82009-12-17 21:24:26 -050010#include <linux/namei.h>
Eric Parisa1014f12009-12-17 21:24:26 -050011#include <linux/poll.h>
Eric Paris11637e42009-12-17 21:24:25 -050012#include <linux/security.h>
13#include <linux/syscalls.h>
Tejun Heoe4e047a2010-05-20 01:36:28 +100014#include <linux/slab.h>
Eric Paris2a3edf82009-12-17 21:24:26 -050015#include <linux/types.h>
Eric Parisa1014f12009-12-17 21:24:26 -050016#include <linux/uaccess.h>
Al Viro91c2e0b2013-03-05 20:10:59 -050017#include <linux/compat.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010018#include <linux/sched/signal.h>
Shakeel Buttd46eb14b2018-08-17 15:46:39 -070019#include <linux/memcontrol.h>
Amir Goldsteina8b13aa2019-01-10 19:04:36 +020020#include <linux/statfs.h>
21#include <linux/exportfs.h>
Eric Parisa1014f12009-12-17 21:24:26 -050022
23#include <asm/ioctls.h>
Eric Paris11637e42009-12-17 21:24:25 -050024
Al Viroc63181e2011-11-25 02:35:16 -050025#include "../../mount.h"
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080026#include "../fdinfo.h"
Jan Kara7053aee2014-01-21 15:48:14 -080027#include "fanotify.h"
Al Viroc63181e2011-11-25 02:35:16 -050028
Eric Paris2529a0d2010-10-28 17:21:57 -040029#define FANOTIFY_DEFAULT_MAX_EVENTS 16384
Eric Parise7099d82010-10-28 17:21:57 -040030#define FANOTIFY_DEFAULT_MAX_MARKS 8192
Eric Paris4afeff82010-10-28 17:21:58 -040031#define FANOTIFY_DEFAULT_MAX_LISTENERS 128
Eric Paris2529a0d2010-10-28 17:21:57 -040032
Heinrich Schuchardt48149e92014-06-04 16:05:44 -070033/*
34 * All flags that may be specified in parameter event_f_flags of fanotify_init.
35 *
36 * Internal and external open flags are stored together in field f_flags of
37 * struct file. Only external open flags shall be allowed in event_f_flags.
38 * Internal flags like FMODE_NONOTIFY, FMODE_EXEC, FMODE_NOCMTIME shall be
39 * excluded.
40 */
41#define FANOTIFY_INIT_ALL_EVENT_F_BITS ( \
42 O_ACCMODE | O_APPEND | O_NONBLOCK | \
43 __O_SYNC | O_DSYNC | O_CLOEXEC | \
44 O_LARGEFILE | O_NOATIME )
45
Andreas Gruenbacher33d3dff2009-12-17 21:24:29 -050046extern const struct fsnotify_ops fanotify_fsnotify_ops;
Eric Paris11637e42009-12-17 21:24:25 -050047
Jan Kara054c6362016-12-21 18:06:12 +010048struct kmem_cache *fanotify_mark_cache __read_mostly;
Jan Kara7088f352020-03-24 17:04:20 +010049struct kmem_cache *fanotify_fid_event_cachep __read_mostly;
50struct kmem_cache *fanotify_path_event_cachep __read_mostly;
Jan Karaf0834412014-04-03 14:46:33 -070051struct kmem_cache *fanotify_perm_event_cachep __read_mostly;
Eric Paris2a3edf82009-12-17 21:24:26 -050052
Amir Goldstein5e469c82019-01-10 19:04:35 +020053#define FANOTIFY_EVENT_ALIGN 4
Amir Goldstein44d705b2020-03-19 17:10:22 +020054#define FANOTIFY_INFO_HDR_LEN \
55 (sizeof(struct fanotify_event_info_fid) + sizeof(struct file_handle))
Amir Goldstein5e469c82019-01-10 19:04:35 +020056
Amir Goldstein44d705b2020-03-19 17:10:22 +020057static int fanotify_fid_info_len(int fh_len, int name_len)
Amir Goldsteind766b552020-03-19 17:10:20 +020058{
Amir Goldstein44d705b2020-03-19 17:10:22 +020059 int info_len = fh_len;
60
61 if (name_len)
62 info_len += name_len + 1;
63
64 return roundup(FANOTIFY_INFO_HDR_LEN + info_len, FANOTIFY_EVENT_ALIGN);
Amir Goldsteind766b552020-03-19 17:10:20 +020065}
66
Amir Goldstein929943b2020-07-16 11:42:28 +030067static int fanotify_event_info_len(unsigned int fid_mode,
68 struct fanotify_event *event)
Amir Goldstein5e469c82019-01-10 19:04:35 +020069{
Amir Goldsteinf454fa62020-07-16 11:42:17 +030070 struct fanotify_info *info = fanotify_event_info(event);
71 int dir_fh_len = fanotify_event_dir_fh_len(event);
Jan Karaafc894c2020-03-24 16:55:37 +010072 int fh_len = fanotify_event_object_fh_len(event);
Amir Goldsteinf454fa62020-07-16 11:42:17 +030073 int info_len = 0;
Amir Goldstein929943b2020-07-16 11:42:28 +030074 int dot_len = 0;
Amir Goldsteinf454fa62020-07-16 11:42:17 +030075
Amir Goldstein929943b2020-07-16 11:42:28 +030076 if (dir_fh_len) {
Amir Goldsteinf454fa62020-07-16 11:42:17 +030077 info_len += fanotify_fid_info_len(dir_fh_len, info->name_len);
Amir Goldstein929943b2020-07-16 11:42:28 +030078 } else if ((fid_mode & FAN_REPORT_NAME) && (event->mask & FAN_ONDIR)) {
79 /*
80 * With group flag FAN_REPORT_NAME, if name was not recorded in
81 * event on a directory, we will report the name ".".
82 */
83 dot_len = 1;
84 }
Jan Karaafc894c2020-03-24 16:55:37 +010085
Amir Goldstein44d705b2020-03-19 17:10:22 +020086 if (fh_len)
Amir Goldstein929943b2020-07-16 11:42:28 +030087 info_len += fanotify_fid_info_len(fh_len, dot_len);
Amir Goldstein5e469c82019-01-10 19:04:35 +020088
Amir Goldstein44d705b2020-03-19 17:10:22 +020089 return info_len;
Amir Goldstein5e469c82019-01-10 19:04:35 +020090}
91
Eric Parisa1014f12009-12-17 21:24:26 -050092/*
Jan Kara7088f352020-03-24 17:04:20 +010093 * Get an fanotify notification event if one exists and is small
Eric Parisa1014f12009-12-17 21:24:26 -050094 * enough to fit in "count". Return an error pointer if the count
Jan Kara40873282019-01-08 14:02:44 +010095 * is not large enough. When permission event is dequeued, its state is
96 * updated accordingly.
Eric Parisa1014f12009-12-17 21:24:26 -050097 */
Jan Kara7088f352020-03-24 17:04:20 +010098static struct fanotify_event *get_one_event(struct fsnotify_group *group,
Eric Parisa1014f12009-12-17 21:24:26 -050099 size_t count)
100{
Amir Goldstein5e469c82019-01-10 19:04:35 +0200101 size_t event_size = FAN_EVENT_METADATA_LEN;
Jan Kara7088f352020-03-24 17:04:20 +0100102 struct fanotify_event *event = NULL;
Amir Goldstein929943b2020-07-16 11:42:28 +0300103 unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
Eric Parisa1014f12009-12-17 21:24:26 -0500104
105 pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
106
Jan Kara8c554462019-01-08 13:52:31 +0100107 spin_lock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500108 if (fsnotify_notify_queue_is_empty(group))
Jan Kara8c554462019-01-08 13:52:31 +0100109 goto out;
Eric Parisa1014f12009-12-17 21:24:26 -0500110
Amir Goldstein929943b2020-07-16 11:42:28 +0300111 if (fid_mode) {
112 event_size += fanotify_event_info_len(fid_mode,
Jan Kara8c554462019-01-08 13:52:31 +0100113 FANOTIFY_E(fsnotify_peek_first_event(group)));
Amir Goldstein5e469c82019-01-10 19:04:35 +0200114 }
115
Jan Kara8c554462019-01-08 13:52:31 +0100116 if (event_size > count) {
Jan Kara7088f352020-03-24 17:04:20 +0100117 event = ERR_PTR(-EINVAL);
Jan Kara8c554462019-01-08 13:52:31 +0100118 goto out;
119 }
Jan Kara7088f352020-03-24 17:04:20 +0100120 event = FANOTIFY_E(fsnotify_remove_first_event(group));
121 if (fanotify_is_perm_event(event->mask))
122 FANOTIFY_PERM(event)->state = FAN_EVENT_REPORTED;
Jan Kara8c554462019-01-08 13:52:31 +0100123out:
124 spin_unlock(&group->notification_lock);
Jan Kara7088f352020-03-24 17:04:20 +0100125 return event;
Eric Parisa1014f12009-12-17 21:24:26 -0500126}
127
Jan Karaa741c2f2020-03-24 15:27:52 +0100128static int create_fd(struct fsnotify_group *group, struct path *path,
Jan Kara7053aee2014-01-21 15:48:14 -0800129 struct file **file)
Eric Parisa1014f12009-12-17 21:24:26 -0500130{
131 int client_fd;
Eric Parisa1014f12009-12-17 21:24:26 -0500132 struct file *new_file;
133
Yann Droneaud0b37e092014-10-09 15:24:40 -0700134 client_fd = get_unused_fd_flags(group->fanotify_data.f_flags);
Eric Parisa1014f12009-12-17 21:24:26 -0500135 if (client_fd < 0)
136 return client_fd;
137
Eric Parisa1014f12009-12-17 21:24:26 -0500138 /*
139 * we need a new file handle for the userspace program so it can read even if it was
140 * originally opened O_WRONLY.
141 */
Jan Karaa741c2f2020-03-24 15:27:52 +0100142 new_file = dentry_open(path,
143 group->fanotify_data.f_flags | FMODE_NONOTIFY,
144 current_cred());
Eric Parisa1014f12009-12-17 21:24:26 -0500145 if (IS_ERR(new_file)) {
146 /*
147 * we still send an event even if we can't open the file. this
148 * can happen when say tasks are gone and we try to open their
149 * /proc files or we try to open a WRONLY file like in sysfs
150 * we just send the errno to userspace since there isn't much
151 * else we can do.
152 */
153 put_unused_fd(client_fd);
154 client_fd = PTR_ERR(new_file);
155 } else {
Al Viro352e3b22012-08-19 12:30:45 -0400156 *file = new_file;
Eric Parisa1014f12009-12-17 21:24:26 -0500157 }
158
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -0500159 return client_fd;
Eric Parisa1014f12009-12-17 21:24:26 -0500160}
161
Jan Kara40873282019-01-08 14:02:44 +0100162/*
163 * Finish processing of permission event by setting it to ANSWERED state and
164 * drop group->notification_lock.
165 */
166static void finish_permission_event(struct fsnotify_group *group,
167 struct fanotify_perm_event *event,
168 unsigned int response)
169 __releases(&group->notification_lock)
170{
Jan Karafabf7f22019-01-08 15:18:02 +0100171 bool destroy = false;
172
Jan Kara40873282019-01-08 14:02:44 +0100173 assert_spin_locked(&group->notification_lock);
174 event->response = response;
Jan Karafabf7f22019-01-08 15:18:02 +0100175 if (event->state == FAN_EVENT_CANCELED)
176 destroy = true;
177 else
178 event->state = FAN_EVENT_ANSWERED;
Jan Kara40873282019-01-08 14:02:44 +0100179 spin_unlock(&group->notification_lock);
Jan Karafabf7f22019-01-08 15:18:02 +0100180 if (destroy)
181 fsnotify_destroy_event(group, &event->fae.fse);
Jan Kara40873282019-01-08 14:02:44 +0100182}
183
Eric Parisb2d87902009-12-17 21:24:34 -0500184static int process_access_response(struct fsnotify_group *group,
185 struct fanotify_response *response_struct)
186{
Amir Goldstein33913992019-01-10 19:04:32 +0200187 struct fanotify_perm_event *event;
Jan Karaf0834412014-04-03 14:46:33 -0700188 int fd = response_struct->fd;
189 int response = response_struct->response;
Eric Parisb2d87902009-12-17 21:24:34 -0500190
191 pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
192 fd, response);
193 /*
194 * make sure the response is valid, if invalid we do nothing and either
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300195 * userspace can send a valid response or we will clean it up after the
Eric Parisb2d87902009-12-17 21:24:34 -0500196 * timeout
197 */
Steve Grubbde8cd832017-10-02 20:21:39 -0400198 switch (response & ~FAN_AUDIT) {
Eric Parisb2d87902009-12-17 21:24:34 -0500199 case FAN_ALLOW:
200 case FAN_DENY:
201 break;
202 default:
203 return -EINVAL;
204 }
205
206 if (fd < 0)
207 return -EINVAL;
208
Amir Goldstein96a71f22018-09-21 21:20:30 +0300209 if ((response & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT))
Steve Grubbde8cd832017-10-02 20:21:39 -0400210 return -EINVAL;
211
Jan Karaaf6a5112019-01-08 13:28:18 +0100212 spin_lock(&group->notification_lock);
213 list_for_each_entry(event, &group->fanotify_data.access_list,
214 fae.fse.list) {
215 if (event->fd != fd)
216 continue;
Eric Parisb2d87902009-12-17 21:24:34 -0500217
Jan Karaaf6a5112019-01-08 13:28:18 +0100218 list_del_init(&event->fae.fse.list);
Jan Kara40873282019-01-08 14:02:44 +0100219 finish_permission_event(group, event, response);
Jan Karaaf6a5112019-01-08 13:28:18 +0100220 wake_up(&group->fanotify_data.access_waitq);
221 return 0;
222 }
223 spin_unlock(&group->notification_lock);
Eric Parisb2d87902009-12-17 21:24:34 -0500224
Jan Karaaf6a5112019-01-08 13:28:18 +0100225 return -ENOENT;
Eric Parisb2d87902009-12-17 21:24:34 -0500226}
Eric Parisb2d87902009-12-17 21:24:34 -0500227
Amir Goldstein44d705b2020-03-19 17:10:22 +0200228static int copy_info_to_user(__kernel_fsid_t *fsid, struct fanotify_fh *fh,
Amir Goldstein83b7a592020-07-16 11:42:26 +0300229 int info_type, const char *name, size_t name_len,
Amir Goldstein44d705b2020-03-19 17:10:22 +0200230 char __user *buf, size_t count)
Amir Goldstein5e469c82019-01-10 19:04:35 +0200231{
232 struct fanotify_event_info_fid info = { };
233 struct file_handle handle = { };
Jan Karaafc894c2020-03-24 16:55:37 +0100234 unsigned char bounce[FANOTIFY_INLINE_FH_LEN], *fh_buf;
Amir Goldsteincacfb952020-03-19 17:10:21 +0200235 size_t fh_len = fh ? fh->len : 0;
Amir Goldstein44d705b2020-03-19 17:10:22 +0200236 size_t info_len = fanotify_fid_info_len(fh_len, name_len);
237 size_t len = info_len;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200238
Amir Goldstein44d705b2020-03-19 17:10:22 +0200239 pr_debug("%s: fh_len=%zu name_len=%zu, info_len=%zu, count=%zu\n",
240 __func__, fh_len, name_len, info_len, count);
241
Amir Goldstein83b7a592020-07-16 11:42:26 +0300242 if (!fh_len)
Amir Goldstein5e469c82019-01-10 19:04:35 +0200243 return 0;
244
Amir Goldstein44d705b2020-03-19 17:10:22 +0200245 if (WARN_ON_ONCE(len < sizeof(info) || len > count))
Amir Goldstein5e469c82019-01-10 19:04:35 +0200246 return -EFAULT;
247
Amir Goldstein44d705b2020-03-19 17:10:22 +0200248 /*
249 * Copy event info fid header followed by variable sized file handle
250 * and optionally followed by variable sized filename.
251 */
Amir Goldstein83b7a592020-07-16 11:42:26 +0300252 switch (info_type) {
253 case FAN_EVENT_INFO_TYPE_FID:
254 case FAN_EVENT_INFO_TYPE_DFID:
255 if (WARN_ON_ONCE(name_len))
256 return -EFAULT;
257 break;
258 case FAN_EVENT_INFO_TYPE_DFID_NAME:
259 if (WARN_ON_ONCE(!name || !name_len))
260 return -EFAULT;
261 break;
262 default:
263 return -EFAULT;
264 }
265
266 info.hdr.info_type = info_type;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200267 info.hdr.len = len;
Amir Goldsteind766b552020-03-19 17:10:20 +0200268 info.fsid = *fsid;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200269 if (copy_to_user(buf, &info, sizeof(info)))
270 return -EFAULT;
271
272 buf += sizeof(info);
273 len -= sizeof(info);
Amir Goldstein44d705b2020-03-19 17:10:22 +0200274 if (WARN_ON_ONCE(len < sizeof(handle)))
275 return -EFAULT;
276
Jan Karaafc894c2020-03-24 16:55:37 +0100277 handle.handle_type = fh->type;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200278 handle.handle_bytes = fh_len;
279 if (copy_to_user(buf, &handle, sizeof(handle)))
280 return -EFAULT;
281
282 buf += sizeof(handle);
283 len -= sizeof(handle);
Amir Goldstein44d705b2020-03-19 17:10:22 +0200284 if (WARN_ON_ONCE(len < fh_len))
285 return -EFAULT;
286
Jan Karab2d22b62019-03-12 12:42:37 +0100287 /*
Amir Goldstein44d705b2020-03-19 17:10:22 +0200288 * For an inline fh and inline file name, copy through stack to exclude
289 * the copy from usercopy hardening protections.
Jan Karab2d22b62019-03-12 12:42:37 +0100290 */
Jan Karaafc894c2020-03-24 16:55:37 +0100291 fh_buf = fanotify_fh_buf(fh);
Jan Karab2d22b62019-03-12 12:42:37 +0100292 if (fh_len <= FANOTIFY_INLINE_FH_LEN) {
Jan Karaafc894c2020-03-24 16:55:37 +0100293 memcpy(bounce, fh_buf, fh_len);
294 fh_buf = bounce;
Jan Karab2d22b62019-03-12 12:42:37 +0100295 }
Jan Karaafc894c2020-03-24 16:55:37 +0100296 if (copy_to_user(buf, fh_buf, fh_len))
Amir Goldstein5e469c82019-01-10 19:04:35 +0200297 return -EFAULT;
298
Amir Goldstein5e469c82019-01-10 19:04:35 +0200299 buf += fh_len;
300 len -= fh_len;
Amir Goldstein44d705b2020-03-19 17:10:22 +0200301
302 if (name_len) {
303 /* Copy the filename with terminating null */
304 name_len++;
305 if (WARN_ON_ONCE(len < name_len))
306 return -EFAULT;
307
308 if (copy_to_user(buf, name, name_len))
309 return -EFAULT;
310
311 buf += name_len;
312 len -= name_len;
313 }
314
315 /* Pad with 0's */
Amir Goldstein5e469c82019-01-10 19:04:35 +0200316 WARN_ON_ONCE(len < 0 || len >= FANOTIFY_EVENT_ALIGN);
317 if (len > 0 && clear_user(buf, len))
318 return -EFAULT;
319
Amir Goldstein44d705b2020-03-19 17:10:22 +0200320 return info_len;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200321}
322
Eric Parisa1014f12009-12-17 21:24:26 -0500323static ssize_t copy_event_to_user(struct fsnotify_group *group,
Jan Kara7088f352020-03-24 17:04:20 +0100324 struct fanotify_event *event,
Kees Cook5b03a472018-12-04 15:44:46 -0800325 char __user *buf, size_t count)
Eric Parisa1014f12009-12-17 21:24:26 -0500326{
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200327 struct fanotify_event_metadata metadata;
Jan Kara7088f352020-03-24 17:04:20 +0100328 struct path *path = fanotify_event_path(event);
Amir Goldsteinf454fa62020-07-16 11:42:17 +0300329 struct fanotify_info *info = fanotify_event_info(event);
Amir Goldstein83b7a592020-07-16 11:42:26 +0300330 unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200331 struct file *f = NULL;
Amir Goldsteine9e0c892019-01-10 19:04:34 +0200332 int ret, fd = FAN_NOFD;
Amir Goldstein83b7a592020-07-16 11:42:26 +0300333 int info_type = 0;
Eric Parisa1014f12009-12-17 21:24:26 -0500334
Jan Kara7088f352020-03-24 17:04:20 +0100335 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
Eric Parisa1014f12009-12-17 21:24:26 -0500336
Amir Goldstein44d705b2020-03-19 17:10:22 +0200337 metadata.event_len = FAN_EVENT_METADATA_LEN +
Amir Goldstein929943b2020-07-16 11:42:28 +0300338 fanotify_event_info_len(fid_mode, event);
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200339 metadata.metadata_len = FAN_EVENT_METADATA_LEN;
340 metadata.vers = FANOTIFY_METADATA_VERSION;
341 metadata.reserved = 0;
342 metadata.mask = event->mask & FANOTIFY_OUTGOING_EVENTS;
343 metadata.pid = pid_vnr(event->pid);
Eric Parisa1014f12009-12-17 21:24:26 -0500344
Amir Goldstein44d705b2020-03-19 17:10:22 +0200345 if (path && path->mnt && path->dentry) {
Jan Karaafc894c2020-03-24 16:55:37 +0100346 fd = create_fd(group, path, &f);
347 if (fd < 0)
348 return fd;
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200349 }
350 metadata.fd = fd;
351
Al Viro352e3b22012-08-19 12:30:45 -0400352 ret = -EFAULT;
Kees Cook5b03a472018-12-04 15:44:46 -0800353 /*
354 * Sanity check copy size in case get_one_event() and
Fabian Frederickc5e443c2020-05-12 20:18:36 +0200355 * event_len sizes ever get out of sync.
Kees Cook5b03a472018-12-04 15:44:46 -0800356 */
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200357 if (WARN_ON_ONCE(metadata.event_len > count))
Al Viro352e3b22012-08-19 12:30:45 -0400358 goto out_close_fd;
359
Amir Goldstein5e469c82019-01-10 19:04:35 +0200360 if (copy_to_user(buf, &metadata, FAN_EVENT_METADATA_LEN))
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200361 goto out_close_fd;
362
Amir Goldstein44d705b2020-03-19 17:10:22 +0200363 buf += FAN_EVENT_METADATA_LEN;
364 count -= FAN_EVENT_METADATA_LEN;
365
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200366 if (fanotify_is_perm_event(event->mask))
Jan Kara7088f352020-03-24 17:04:20 +0100367 FANOTIFY_PERM(event)->fd = fd;
Eric Parisb2d87902009-12-17 21:24:34 -0500368
Amir Goldstein44d705b2020-03-19 17:10:22 +0200369 if (f)
Al Viro3587b1b2012-11-18 19:19:00 +0000370 fd_install(fd, f);
Amir Goldstein44d705b2020-03-19 17:10:22 +0200371
372 /* Event info records order is: dir fid + name, child fid */
Amir Goldsteinf454fa62020-07-16 11:42:17 +0300373 if (fanotify_event_dir_fh_len(event)) {
Amir Goldstein691d9762020-07-16 11:42:30 +0300374 info_type = info->name_len ? FAN_EVENT_INFO_TYPE_DFID_NAME :
375 FAN_EVENT_INFO_TYPE_DFID;
Amir Goldstein44d705b2020-03-19 17:10:22 +0200376 ret = copy_info_to_user(fanotify_event_fsid(event),
Amir Goldsteinf454fa62020-07-16 11:42:17 +0300377 fanotify_info_dir_fh(info),
Amir Goldstein83b7a592020-07-16 11:42:26 +0300378 info_type, fanotify_info_name(info),
Amir Goldsteinf454fa62020-07-16 11:42:17 +0300379 info->name_len, buf, count);
Amir Goldstein5e469c82019-01-10 19:04:35 +0200380 if (ret < 0)
381 return ret;
Amir Goldstein44d705b2020-03-19 17:10:22 +0200382
383 buf += ret;
384 count -= ret;
385 }
386
387 if (fanotify_event_object_fh_len(event)) {
Amir Goldstein929943b2020-07-16 11:42:28 +0300388 const char *dot = NULL;
389 int dot_len = 0;
390
Amir Goldstein83b7a592020-07-16 11:42:26 +0300391 if (fid_mode == FAN_REPORT_FID || info_type) {
392 /*
393 * With only group flag FAN_REPORT_FID only type FID is
394 * reported. Second info record type is always FID.
395 */
396 info_type = FAN_EVENT_INFO_TYPE_FID;
Amir Goldstein929943b2020-07-16 11:42:28 +0300397 } else if ((fid_mode & FAN_REPORT_NAME) &&
398 (event->mask & FAN_ONDIR)) {
399 /*
400 * With group flag FAN_REPORT_NAME, if name was not
401 * recorded in an event on a directory, report the
402 * name "." with info type DFID_NAME.
403 */
404 info_type = FAN_EVENT_INFO_TYPE_DFID_NAME;
405 dot = ".";
406 dot_len = 1;
Amir Goldstein83b7a592020-07-16 11:42:26 +0300407 } else if ((event->mask & ALL_FSNOTIFY_DIRENT_EVENTS) ||
408 (event->mask & FAN_ONDIR)) {
409 /*
410 * With group flag FAN_REPORT_DIR_FID, a single info
411 * record has type DFID for directory entry modification
412 * event and for event on a directory.
413 */
414 info_type = FAN_EVENT_INFO_TYPE_DFID;
415 } else {
416 /*
417 * With group flags FAN_REPORT_DIR_FID|FAN_REPORT_FID,
418 * a single info record has type FID for event on a
419 * non-directory, when there is no directory to report.
420 * For example, on FAN_DELETE_SELF event.
421 */
422 info_type = FAN_EVENT_INFO_TYPE_FID;
423 }
424
Amir Goldstein44d705b2020-03-19 17:10:22 +0200425 ret = copy_info_to_user(fanotify_event_fsid(event),
426 fanotify_event_object_fh(event),
Amir Goldstein929943b2020-07-16 11:42:28 +0300427 info_type, dot, dot_len, buf, count);
Amir Goldstein44d705b2020-03-19 17:10:22 +0200428 if (ret < 0)
429 return ret;
430
431 buf += ret;
432 count -= ret;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200433 }
434
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200435 return metadata.event_len;
Eric Parisb2d87902009-12-17 21:24:34 -0500436
Eric Parisb2d87902009-12-17 21:24:34 -0500437out_close_fd:
Al Viro352e3b22012-08-19 12:30:45 -0400438 if (fd != FAN_NOFD) {
439 put_unused_fd(fd);
440 fput(f);
441 }
Eric Parisb2d87902009-12-17 21:24:34 -0500442 return ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500443}
444
445/* intofiy userspace file descriptor functions */
Al Viro076ccb72017-07-03 01:02:18 -0400446static __poll_t fanotify_poll(struct file *file, poll_table *wait)
Eric Parisa1014f12009-12-17 21:24:26 -0500447{
448 struct fsnotify_group *group = file->private_data;
Al Viro076ccb72017-07-03 01:02:18 -0400449 __poll_t ret = 0;
Eric Parisa1014f12009-12-17 21:24:26 -0500450
451 poll_wait(file, &group->notification_waitq, wait);
Jan Karac21dbe22016-10-07 16:56:52 -0700452 spin_lock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500453 if (!fsnotify_notify_queue_is_empty(group))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800454 ret = EPOLLIN | EPOLLRDNORM;
Jan Karac21dbe22016-10-07 16:56:52 -0700455 spin_unlock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500456
457 return ret;
458}
459
460static ssize_t fanotify_read(struct file *file, char __user *buf,
461 size_t count, loff_t *pos)
462{
463 struct fsnotify_group *group;
Jan Kara7088f352020-03-24 17:04:20 +0100464 struct fanotify_event *event;
Eric Parisa1014f12009-12-17 21:24:26 -0500465 char __user *start;
466 int ret;
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100467 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Eric Parisa1014f12009-12-17 21:24:26 -0500468
469 start = buf;
470 group = file->private_data;
471
472 pr_debug("%s: group=%p\n", __func__, group);
473
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100474 add_wait_queue(&group->notification_waitq, &wait);
Eric Parisa1014f12009-12-17 21:24:26 -0500475 while (1) {
Jan Kara47aaabd2020-07-15 14:06:21 +0200476 /*
477 * User can supply arbitrarily large buffer. Avoid softlockups
478 * in case there are lots of available events.
479 */
480 cond_resched();
Jan Kara7088f352020-03-24 17:04:20 +0100481 event = get_one_event(group, count);
482 if (IS_ERR(event)) {
483 ret = PTR_ERR(event);
Jan Karad8aaab42014-04-03 14:46:35 -0700484 break;
485 }
486
Jan Kara7088f352020-03-24 17:04:20 +0100487 if (!event) {
Jan Karad8aaab42014-04-03 14:46:35 -0700488 ret = -EAGAIN;
489 if (file->f_flags & O_NONBLOCK)
Eric Parisa1014f12009-12-17 21:24:26 -0500490 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700491
492 ret = -ERESTARTSYS;
493 if (signal_pending(current))
Eric Parisa1014f12009-12-17 21:24:26 -0500494 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700495
496 if (start != buf)
497 break;
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100498
499 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Eric Parisa1014f12009-12-17 21:24:26 -0500500 continue;
501 }
502
Jan Kara7088f352020-03-24 17:04:20 +0100503 ret = copy_event_to_user(group, event, buf, count);
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300504 if (unlikely(ret == -EOPENSTALE)) {
505 /*
506 * We cannot report events with stale fd so drop it.
507 * Setting ret to 0 will continue the event loop and
508 * do the right thing if there are no more events to
509 * read (i.e. return bytes read, -EAGAIN or wait).
510 */
511 ret = 0;
512 }
513
Jan Karad8aaab42014-04-03 14:46:35 -0700514 /*
515 * Permission events get queued to wait for response. Other
516 * events can be destroyed now.
517 */
Jan Kara7088f352020-03-24 17:04:20 +0100518 if (!fanotify_is_perm_event(event->mask)) {
519 fsnotify_destroy_event(group, &event->fse);
Jan Karad5078162014-04-03 14:46:36 -0700520 } else {
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300521 if (ret <= 0) {
Jan Kara40873282019-01-08 14:02:44 +0100522 spin_lock(&group->notification_lock);
523 finish_permission_event(group,
Jan Kara7088f352020-03-24 17:04:20 +0100524 FANOTIFY_PERM(event), FAN_DENY);
Jan Karad5078162014-04-03 14:46:36 -0700525 wake_up(&group->fanotify_data.access_waitq);
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300526 } else {
527 spin_lock(&group->notification_lock);
Jan Kara7088f352020-03-24 17:04:20 +0100528 list_add_tail(&event->fse.list,
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300529 &group->fanotify_data.access_list);
530 spin_unlock(&group->notification_lock);
Jan Karad5078162014-04-03 14:46:36 -0700531 }
Jan Karad5078162014-04-03 14:46:36 -0700532 }
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300533 if (ret < 0)
534 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700535 buf += ret;
536 count -= ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500537 }
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100538 remove_wait_queue(&group->notification_waitq, &wait);
Eric Parisa1014f12009-12-17 21:24:26 -0500539
Eric Parisa1014f12009-12-17 21:24:26 -0500540 if (start != buf && ret != -EFAULT)
541 ret = buf - start;
542 return ret;
543}
544
Eric Parisb2d87902009-12-17 21:24:34 -0500545static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
546{
Eric Parisb2d87902009-12-17 21:24:34 -0500547 struct fanotify_response response = { .fd = -1, .response = -1 };
548 struct fsnotify_group *group;
549 int ret;
550
Miklos Szeredi6685df32017-10-30 21:14:56 +0100551 if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
552 return -EINVAL;
553
Eric Parisb2d87902009-12-17 21:24:34 -0500554 group = file->private_data;
555
Fabian Frederick5e236632020-05-12 20:19:21 +0200556 if (count < sizeof(response))
557 return -EINVAL;
558
559 count = sizeof(response);
Eric Parisb2d87902009-12-17 21:24:34 -0500560
561 pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
562
563 if (copy_from_user(&response, buf, count))
564 return -EFAULT;
565
566 ret = process_access_response(group, &response);
567 if (ret < 0)
568 count = ret;
569
570 return count;
Eric Parisb2d87902009-12-17 21:24:34 -0500571}
572
Eric Paris52c923d2009-12-17 21:24:26 -0500573static int fanotify_release(struct inode *ignored, struct file *file)
574{
575 struct fsnotify_group *group = file->private_data;
Andrew Morton19ba54f2010-10-28 17:21:59 -0400576
Jan Kara5838d442014-08-06 16:03:28 -0700577 /*
Jan Kara96d41012016-09-19 14:44:30 -0700578 * Stop new events from arriving in the notification queue. since
579 * userspace cannot use fanotify fd anymore, no event can enter or
580 * leave access_list by now either.
581 */
582 fsnotify_group_stop_queueing(group);
583
584 /*
585 * Process all permission events on access_list and notification queue
586 * and simulate reply from userspace.
Jan Kara5838d442014-08-06 16:03:28 -0700587 */
Jan Kara073f6552016-10-07 16:56:55 -0700588 spin_lock(&group->notification_lock);
Jan Karaca6f8692019-01-09 13:21:01 +0100589 while (!list_empty(&group->fanotify_data.access_list)) {
Jan Kara7088f352020-03-24 17:04:20 +0100590 struct fanotify_perm_event *event;
591
Jan Karaca6f8692019-01-09 13:21:01 +0100592 event = list_first_entry(&group->fanotify_data.access_list,
593 struct fanotify_perm_event, fae.fse.list);
Jan Karaf0834412014-04-03 14:46:33 -0700594 list_del_init(&event->fae.fse.list);
Jan Kara40873282019-01-08 14:02:44 +0100595 finish_permission_event(group, event, FAN_ALLOW);
596 spin_lock(&group->notification_lock);
Eric Paris2eebf582010-08-18 12:25:50 -0400597 }
Eric Paris2eebf582010-08-18 12:25:50 -0400598
Jan Kara5838d442014-08-06 16:03:28 -0700599 /*
Jan Kara96d41012016-09-19 14:44:30 -0700600 * Destroy all non-permission events. For permission events just
601 * dequeue them and set the response. They will be freed once the
602 * response is consumed and fanotify_get_response() returns.
Jan Kara5838d442014-08-06 16:03:28 -0700603 */
Jan Kara96d41012016-09-19 14:44:30 -0700604 while (!fsnotify_notify_queue_is_empty(group)) {
Jan Kara7088f352020-03-24 17:04:20 +0100605 struct fanotify_event *event;
606
607 event = FANOTIFY_E(fsnotify_remove_first_event(group));
608 if (!(event->mask & FANOTIFY_PERM_EVENTS)) {
Jan Karac21dbe22016-10-07 16:56:52 -0700609 spin_unlock(&group->notification_lock);
Jan Kara7088f352020-03-24 17:04:20 +0100610 fsnotify_destroy_event(group, &event->fse);
Miklos Szeredi6685df32017-10-30 21:14:56 +0100611 } else {
Jan Kara7088f352020-03-24 17:04:20 +0100612 finish_permission_event(group, FANOTIFY_PERM(event),
Jan Kara40873282019-01-08 14:02:44 +0100613 FAN_ALLOW);
Miklos Szeredi6685df32017-10-30 21:14:56 +0100614 }
Jan Kara40873282019-01-08 14:02:44 +0100615 spin_lock(&group->notification_lock);
Jan Kara96d41012016-09-19 14:44:30 -0700616 }
Jan Karac21dbe22016-10-07 16:56:52 -0700617 spin_unlock(&group->notification_lock);
Jan Kara96d41012016-09-19 14:44:30 -0700618
619 /* Response for all permission events it set, wakeup waiters */
Eric Paris2eebf582010-08-18 12:25:50 -0400620 wake_up(&group->fanotify_data.access_waitq);
Eric Paris0a6b6bd2011-10-14 17:43:39 -0400621
Eric Paris52c923d2009-12-17 21:24:26 -0500622 /* matches the fanotify_init->fsnotify_alloc_group */
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200623 fsnotify_destroy_group(group);
Eric Paris52c923d2009-12-17 21:24:26 -0500624
625 return 0;
626}
627
Eric Parisa1014f12009-12-17 21:24:26 -0500628static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
629{
630 struct fsnotify_group *group;
Jan Kara7053aee2014-01-21 15:48:14 -0800631 struct fsnotify_event *fsn_event;
Eric Parisa1014f12009-12-17 21:24:26 -0500632 void __user *p;
633 int ret = -ENOTTY;
634 size_t send_len = 0;
635
636 group = file->private_data;
637
638 p = (void __user *) arg;
639
640 switch (cmd) {
641 case FIONREAD:
Jan Karac21dbe22016-10-07 16:56:52 -0700642 spin_lock(&group->notification_lock);
Jan Kara7053aee2014-01-21 15:48:14 -0800643 list_for_each_entry(fsn_event, &group->notification_list, list)
Eric Parisa1014f12009-12-17 21:24:26 -0500644 send_len += FAN_EVENT_METADATA_LEN;
Jan Karac21dbe22016-10-07 16:56:52 -0700645 spin_unlock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500646 ret = put_user(send_len, (int __user *) p);
647 break;
648 }
649
650 return ret;
651}
652
Eric Paris52c923d2009-12-17 21:24:26 -0500653static const struct file_operations fanotify_fops = {
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800654 .show_fdinfo = fanotify_show_fdinfo,
Eric Parisa1014f12009-12-17 21:24:26 -0500655 .poll = fanotify_poll,
656 .read = fanotify_read,
Eric Parisb2d87902009-12-17 21:24:34 -0500657 .write = fanotify_write,
Eric Paris52c923d2009-12-17 21:24:26 -0500658 .fasync = NULL,
659 .release = fanotify_release,
Eric Parisa1014f12009-12-17 21:24:26 -0500660 .unlocked_ioctl = fanotify_ioctl,
Arnd Bergmann1832f2d2018-09-11 21:59:08 +0200661 .compat_ioctl = compat_ptr_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200662 .llseek = noop_llseek,
Eric Paris52c923d2009-12-17 21:24:26 -0500663};
664
Eric Paris2a3edf82009-12-17 21:24:26 -0500665static int fanotify_find_path(int dfd, const char __user *filename,
Aaron Goidelac5656d2019-08-12 11:20:00 -0400666 struct path *path, unsigned int flags, __u64 mask,
667 unsigned int obj_type)
Eric Paris2a3edf82009-12-17 21:24:26 -0500668{
669 int ret;
670
671 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
672 dfd, filename, flags);
673
674 if (filename == NULL) {
Al Viro2903ff02012-08-28 12:52:22 -0400675 struct fd f = fdget(dfd);
Eric Paris2a3edf82009-12-17 21:24:26 -0500676
677 ret = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -0400678 if (!f.file)
Eric Paris2a3edf82009-12-17 21:24:26 -0500679 goto out;
680
681 ret = -ENOTDIR;
682 if ((flags & FAN_MARK_ONLYDIR) &&
Al Viro496ad9a2013-01-23 17:07:38 -0500683 !(S_ISDIR(file_inode(f.file)->i_mode))) {
Al Viro2903ff02012-08-28 12:52:22 -0400684 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500685 goto out;
686 }
687
Al Viro2903ff02012-08-28 12:52:22 -0400688 *path = f.file->f_path;
Eric Paris2a3edf82009-12-17 21:24:26 -0500689 path_get(path);
Al Viro2903ff02012-08-28 12:52:22 -0400690 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500691 } else {
692 unsigned int lookup_flags = 0;
693
694 if (!(flags & FAN_MARK_DONT_FOLLOW))
695 lookup_flags |= LOOKUP_FOLLOW;
696 if (flags & FAN_MARK_ONLYDIR)
697 lookup_flags |= LOOKUP_DIRECTORY;
698
699 ret = user_path_at(dfd, filename, lookup_flags, path);
700 if (ret)
701 goto out;
702 }
703
704 /* you can only watch an inode if you have read permissions on it */
705 ret = inode_permission(path->dentry->d_inode, MAY_READ);
Aaron Goidelac5656d2019-08-12 11:20:00 -0400706 if (ret) {
707 path_put(path);
708 goto out;
709 }
710
711 ret = security_path_notify(path, mask, obj_type);
Eric Paris2a3edf82009-12-17 21:24:26 -0500712 if (ret)
713 path_put(path);
Aaron Goidelac5656d2019-08-12 11:20:00 -0400714
Eric Paris2a3edf82009-12-17 21:24:26 -0500715out:
716 return ret;
717}
718
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500719static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
Amir Goldstein4ed68142020-07-16 11:42:14 +0300720 __u32 mask, unsigned int flags,
721 __u32 umask, int *destroy)
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500722{
Lino Sanfilippod2c18742015-02-10 14:08:24 -0800723 __u32 oldmask = 0;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500724
Amir Goldstein4ed68142020-07-16 11:42:14 +0300725 /* umask bits cannot be removed by user */
726 mask &= ~umask;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500727 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500728 if (!(flags & FAN_MARK_IGNORED_MASK)) {
729 oldmask = fsn_mark->mask;
Amir Goldsteina72fd222018-10-04 00:25:34 +0300730 fsn_mark->mask &= ~mask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500731 } else {
Amir Goldsteina72fd222018-10-04 00:25:34 +0300732 fsn_mark->ignored_mask &= ~mask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500733 }
Amir Goldstein4ed68142020-07-16 11:42:14 +0300734 /*
735 * We need to keep the mark around even if remaining mask cannot
736 * result in any events (e.g. mask == FAN_ONDIR) to support incremenal
737 * changes to the mask.
738 * Destroy mark when only umask bits remain.
739 */
740 *destroy = !((fsn_mark->mask | fsn_mark->ignored_mask) & ~umask);
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500741 spin_unlock(&fsn_mark->lock);
742
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500743 return mask & oldmask;
744}
745
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300746static int fanotify_remove_mark(struct fsnotify_group *group,
747 fsnotify_connp_t *connp, __u32 mask,
Amir Goldstein4ed68142020-07-16 11:42:14 +0300748 unsigned int flags, __u32 umask)
Eric Paris88826272009-12-17 21:24:28 -0500749{
750 struct fsnotify_mark *fsn_mark = NULL;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500751 __u32 removed;
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200752 int destroy_mark;
Eric Paris88826272009-12-17 21:24:28 -0500753
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700754 mutex_lock(&group->mark_mutex);
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300755 fsn_mark = fsnotify_find_mark(connp, group);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700756 if (!fsn_mark) {
757 mutex_unlock(&group->mark_mutex);
Eric Paris88826272009-12-17 21:24:28 -0500758 return -ENOENT;
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700759 }
Eric Paris88826272009-12-17 21:24:28 -0500760
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200761 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
Amir Goldstein4ed68142020-07-16 11:42:14 +0300762 umask, &destroy_mark);
Amir Goldstein3ac70bf2018-06-23 17:54:50 +0300763 if (removed & fsnotify_conn_mask(fsn_mark->connector))
764 fsnotify_recalc_mask(fsn_mark->connector);
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200765 if (destroy_mark)
Jan Kara4712e7222015-09-04 15:43:12 -0700766 fsnotify_detach_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700767 mutex_unlock(&group->mark_mutex);
Jan Kara4712e7222015-09-04 15:43:12 -0700768 if (destroy_mark)
769 fsnotify_free_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700770
Jan Karab1362ed2016-12-21 16:28:45 +0100771 /* matches the fsnotify_find_mark() */
Eric Paris2a3edf82009-12-17 21:24:26 -0500772 fsnotify_put_mark(fsn_mark);
Eric Paris2a3edf82009-12-17 21:24:26 -0500773 return 0;
774}
775
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300776static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
777 struct vfsmount *mnt, __u32 mask,
Amir Goldstein4ed68142020-07-16 11:42:14 +0300778 unsigned int flags, __u32 umask)
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300779{
780 return fanotify_remove_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
Amir Goldstein4ed68142020-07-16 11:42:14 +0300781 mask, flags, umask);
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300782}
783
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300784static int fanotify_remove_sb_mark(struct fsnotify_group *group,
Amir Goldstein4ed68142020-07-16 11:42:14 +0300785 struct super_block *sb, __u32 mask,
786 unsigned int flags, __u32 umask)
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300787{
Amir Goldstein4ed68142020-07-16 11:42:14 +0300788 return fanotify_remove_mark(group, &sb->s_fsnotify_marks, mask,
789 flags, umask);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300790}
791
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300792static int fanotify_remove_inode_mark(struct fsnotify_group *group,
793 struct inode *inode, __u32 mask,
Amir Goldstein4ed68142020-07-16 11:42:14 +0300794 unsigned int flags, __u32 umask)
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300795{
796 return fanotify_remove_mark(group, &inode->i_fsnotify_marks, mask,
Amir Goldstein4ed68142020-07-16 11:42:14 +0300797 flags, umask);
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300798}
799
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500800static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
801 __u32 mask,
802 unsigned int flags)
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500803{
Eric Paris192ca4d2010-10-28 17:21:59 -0400804 __u32 oldmask = -1;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500805
806 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500807 if (!(flags & FAN_MARK_IGNORED_MASK)) {
808 oldmask = fsn_mark->mask;
Amir Goldsteina72fd222018-10-04 00:25:34 +0300809 fsn_mark->mask |= mask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500810 } else {
Amir Goldsteina72fd222018-10-04 00:25:34 +0300811 fsn_mark->ignored_mask |= mask;
Eric Parisc9778a92009-12-17 21:24:33 -0500812 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
813 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500814 }
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500815 spin_unlock(&fsn_mark->lock);
816
817 return mask & ~oldmask;
818}
819
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700820static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
Amir Goldsteinb812a9f2018-06-23 17:54:48 +0300821 fsnotify_connp_t *connp,
Amir Goldstein77115222019-01-10 19:04:37 +0200822 unsigned int type,
823 __kernel_fsid_t *fsid)
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700824{
825 struct fsnotify_mark *mark;
826 int ret;
827
828 if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
829 return ERR_PTR(-ENOSPC);
830
831 mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
832 if (!mark)
833 return ERR_PTR(-ENOMEM);
834
Jan Kara054c6362016-12-21 18:06:12 +0100835 fsnotify_init_mark(mark, group);
Amir Goldstein77115222019-01-10 19:04:37 +0200836 ret = fsnotify_add_mark_locked(mark, connp, type, 0, fsid);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700837 if (ret) {
838 fsnotify_put_mark(mark);
839 return ERR_PTR(ret);
840 }
841
842 return mark;
843}
844
845
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300846static int fanotify_add_mark(struct fsnotify_group *group,
847 fsnotify_connp_t *connp, unsigned int type,
Amir Goldstein77115222019-01-10 19:04:37 +0200848 __u32 mask, unsigned int flags,
849 __kernel_fsid_t *fsid)
Eric Paris2a3edf82009-12-17 21:24:26 -0500850{
851 struct fsnotify_mark *fsn_mark;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500852 __u32 added;
Eric Paris2a3edf82009-12-17 21:24:26 -0500853
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700854 mutex_lock(&group->mark_mutex);
Amir Goldsteinb812a9f2018-06-23 17:54:48 +0300855 fsn_mark = fsnotify_find_mark(connp, group);
Eric Paris88826272009-12-17 21:24:28 -0500856 if (!fsn_mark) {
Amir Goldstein77115222019-01-10 19:04:37 +0200857 fsn_mark = fanotify_add_new_mark(group, connp, type, fsid);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700858 if (IS_ERR(fsn_mark)) {
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700859 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700860 return PTR_ERR(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700861 }
Eric Paris88826272009-12-17 21:24:28 -0500862 }
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500863 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
Amir Goldstein3ac70bf2018-06-23 17:54:50 +0300864 if (added & ~fsnotify_conn_mask(fsn_mark->connector))
865 fsnotify_recalc_mask(fsn_mark->connector);
Jan Karac9747642016-12-14 13:53:46 +0100866 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700867
Lino Sanfilippofa218ab2010-11-09 18:18:16 +0100868 fsnotify_put_mark(fsn_mark);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700869 return 0;
Eric Paris88826272009-12-17 21:24:28 -0500870}
871
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300872static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
873 struct vfsmount *mnt, __u32 mask,
Amir Goldstein77115222019-01-10 19:04:37 +0200874 unsigned int flags, __kernel_fsid_t *fsid)
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300875{
876 return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
Amir Goldstein77115222019-01-10 19:04:37 +0200877 FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags, fsid);
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300878}
879
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300880static int fanotify_add_sb_mark(struct fsnotify_group *group,
Amir Goldstein77115222019-01-10 19:04:37 +0200881 struct super_block *sb, __u32 mask,
882 unsigned int flags, __kernel_fsid_t *fsid)
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300883{
884 return fanotify_add_mark(group, &sb->s_fsnotify_marks,
Amir Goldstein77115222019-01-10 19:04:37 +0200885 FSNOTIFY_OBJ_TYPE_SB, mask, flags, fsid);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300886}
887
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500888static int fanotify_add_inode_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500889 struct inode *inode, __u32 mask,
Amir Goldstein77115222019-01-10 19:04:37 +0200890 unsigned int flags, __kernel_fsid_t *fsid)
Eric Paris88826272009-12-17 21:24:28 -0500891{
Eric Paris88826272009-12-17 21:24:28 -0500892 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
Eric Paris2a3edf82009-12-17 21:24:26 -0500893
Eric Paris5322a592010-10-28 17:21:57 -0400894 /*
895 * If some other task has this inode open for write we should not add
896 * an ignored mark, unless that ignored mark is supposed to survive
897 * modification changes anyway.
898 */
899 if ((flags & FAN_MARK_IGNORED_MASK) &&
900 !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
Nikolay Borisovac9498d2018-12-11 10:27:23 +0200901 inode_is_open_for_write(inode))
Eric Paris5322a592010-10-28 17:21:57 -0400902 return 0;
903
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300904 return fanotify_add_mark(group, &inode->i_fsnotify_marks,
Amir Goldstein77115222019-01-10 19:04:37 +0200905 FSNOTIFY_OBJ_TYPE_INODE, mask, flags, fsid);
Eric Paris88826272009-12-17 21:24:28 -0500906}
Eric Paris2a3edf82009-12-17 21:24:26 -0500907
Amir Goldsteinb8a6c3a2020-07-08 14:11:42 +0300908static struct fsnotify_event *fanotify_alloc_overflow_event(void)
909{
910 struct fanotify_event *oevent;
911
912 oevent = kmalloc(sizeof(*oevent), GFP_KERNEL_ACCOUNT);
913 if (!oevent)
914 return NULL;
915
916 fanotify_init_event(oevent, 0, FS_Q_OVERFLOW);
917 oevent->type = FANOTIFY_EVENT_TYPE_OVERFLOW;
918
919 return &oevent->fse;
920}
921
Eric Paris52c923d2009-12-17 21:24:26 -0500922/* fanotify syscalls */
Eric Paris08ae8932010-05-27 09:41:40 -0400923SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
Eric Paris11637e42009-12-17 21:24:25 -0500924{
Eric Paris52c923d2009-12-17 21:24:26 -0500925 struct fsnotify_group *group;
926 int f_flags, fd;
Eric Paris4afeff82010-10-28 17:21:58 -0400927 struct user_struct *user;
Amir Goldstein83b7a592020-07-16 11:42:26 +0300928 unsigned int fid_mode = flags & FANOTIFY_FID_BITS;
929 unsigned int class = flags & FANOTIFY_CLASS_BITS;
Eric Paris52c923d2009-12-17 21:24:26 -0500930
Amir Goldstein96a71f22018-09-21 21:20:30 +0300931 pr_debug("%s: flags=%x event_f_flags=%x\n",
932 __func__, flags, event_f_flags);
Eric Paris52c923d2009-12-17 21:24:26 -0500933
Eric Paris52c923d2009-12-17 21:24:26 -0500934 if (!capable(CAP_SYS_ADMIN))
Andreas Gruenbachera2f13ad2010-08-24 12:58:54 +0200935 return -EPERM;
Eric Paris52c923d2009-12-17 21:24:26 -0500936
Steve Grubbde8cd832017-10-02 20:21:39 -0400937#ifdef CONFIG_AUDITSYSCALL
Amir Goldstein23c9dee2018-10-04 00:25:35 +0300938 if (flags & ~(FANOTIFY_INIT_FLAGS | FAN_ENABLE_AUDIT))
Steve Grubbde8cd832017-10-02 20:21:39 -0400939#else
Amir Goldstein23c9dee2018-10-04 00:25:35 +0300940 if (flags & ~FANOTIFY_INIT_FLAGS)
Steve Grubbde8cd832017-10-02 20:21:39 -0400941#endif
Eric Paris52c923d2009-12-17 21:24:26 -0500942 return -EINVAL;
943
Heinrich Schuchardt48149e92014-06-04 16:05:44 -0700944 if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS)
945 return -EINVAL;
946
947 switch (event_f_flags & O_ACCMODE) {
948 case O_RDONLY:
949 case O_RDWR:
950 case O_WRONLY:
951 break;
952 default:
953 return -EINVAL;
954 }
955
Amir Goldstein83b7a592020-07-16 11:42:26 +0300956 if (fid_mode && class != FAN_CLASS_NOTIF)
Amir Goldsteina8b13aa2019-01-10 19:04:36 +0200957 return -EINVAL;
958
Amir Goldstein929943b2020-07-16 11:42:28 +0300959 /*
Amir Goldstein929943b2020-07-16 11:42:28 +0300960 * Child name is reported with parent fid so requires dir fid.
Amir Goldstein691d9762020-07-16 11:42:30 +0300961 * We can report both child fid and dir fid with or without name.
Amir Goldstein929943b2020-07-16 11:42:28 +0300962 */
Amir Goldstein691d9762020-07-16 11:42:30 +0300963 if ((fid_mode & FAN_REPORT_NAME) && !(fid_mode & FAN_REPORT_DIR_FID))
Amir Goldstein83b7a592020-07-16 11:42:26 +0300964 return -EINVAL;
Amir Goldstein83b7a592020-07-16 11:42:26 +0300965
Eric Paris4afeff82010-10-28 17:21:58 -0400966 user = get_current_user();
967 if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {
968 free_uid(user);
969 return -EMFILE;
970 }
971
Eric Parisb2d87902009-12-17 21:24:34 -0500972 f_flags = O_RDWR | FMODE_NONOTIFY;
Eric Paris52c923d2009-12-17 21:24:26 -0500973 if (flags & FAN_CLOEXEC)
974 f_flags |= O_CLOEXEC;
975 if (flags & FAN_NONBLOCK)
976 f_flags |= O_NONBLOCK;
977
978 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
979 group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
Eric Paris26379192010-11-23 23:48:26 -0500980 if (IS_ERR(group)) {
981 free_uid(user);
Eric Paris52c923d2009-12-17 21:24:26 -0500982 return PTR_ERR(group);
Eric Paris26379192010-11-23 23:48:26 -0500983 }
Eric Paris52c923d2009-12-17 21:24:26 -0500984
Eric Paris4afeff82010-10-28 17:21:58 -0400985 group->fanotify_data.user = user;
Amir Goldstein96a71f22018-09-21 21:20:30 +0300986 group->fanotify_data.flags = flags;
Eric Paris4afeff82010-10-28 17:21:58 -0400987 atomic_inc(&user->fanotify_listeners);
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700988 group->memcg = get_mem_cgroup_from_mm(current->mm);
Eric Paris4afeff82010-10-28 17:21:58 -0400989
Amir Goldsteinb8a6c3a2020-07-08 14:11:42 +0300990 group->overflow_event = fanotify_alloc_overflow_event();
991 if (unlikely(!group->overflow_event)) {
Jan Karaff57cd52014-02-21 19:14:11 +0100992 fd = -ENOMEM;
993 goto out_destroy_group;
994 }
Jan Karaff57cd52014-02-21 19:14:11 +0100995
Will Woods1e2ee492014-05-06 12:50:10 -0700996 if (force_o_largefile())
997 event_f_flags |= O_LARGEFILE;
Eric Paris80af2582010-07-28 10:18:37 -0400998 group->fanotify_data.f_flags = event_f_flags;
Eric Paris9e66e422009-12-17 21:24:34 -0500999 init_waitqueue_head(&group->fanotify_data.access_waitq);
1000 INIT_LIST_HEAD(&group->fanotify_data.access_list);
Amir Goldstein83b7a592020-07-16 11:42:26 +03001001 switch (class) {
Eric Paris4231a232010-10-28 17:21:56 -04001002 case FAN_CLASS_NOTIF:
1003 group->priority = FS_PRIO_0;
1004 break;
1005 case FAN_CLASS_CONTENT:
1006 group->priority = FS_PRIO_1;
1007 break;
1008 case FAN_CLASS_PRE_CONTENT:
1009 group->priority = FS_PRIO_2;
1010 break;
1011 default:
1012 fd = -EINVAL;
Lino Sanfilippod8153d42011-06-14 17:29:45 +02001013 goto out_destroy_group;
Eric Paris4231a232010-10-28 17:21:56 -04001014 }
Eric Pariscb2d4292009-12-17 21:24:34 -05001015
Eric Paris5dd03f52010-10-28 17:21:57 -04001016 if (flags & FAN_UNLIMITED_QUEUE) {
1017 fd = -EPERM;
1018 if (!capable(CAP_SYS_ADMIN))
Lino Sanfilippod8153d42011-06-14 17:29:45 +02001019 goto out_destroy_group;
Eric Paris5dd03f52010-10-28 17:21:57 -04001020 group->max_events = UINT_MAX;
1021 } else {
1022 group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
1023 }
Eric Paris2529a0d2010-10-28 17:21:57 -04001024
Eric Parisac7e22d2010-10-28 17:21:58 -04001025 if (flags & FAN_UNLIMITED_MARKS) {
1026 fd = -EPERM;
1027 if (!capable(CAP_SYS_ADMIN))
Lino Sanfilippod8153d42011-06-14 17:29:45 +02001028 goto out_destroy_group;
Eric Parisac7e22d2010-10-28 17:21:58 -04001029 group->fanotify_data.max_marks = UINT_MAX;
1030 } else {
1031 group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS;
1032 }
Eric Parise7099d82010-10-28 17:21:57 -04001033
Steve Grubbde8cd832017-10-02 20:21:39 -04001034 if (flags & FAN_ENABLE_AUDIT) {
1035 fd = -EPERM;
1036 if (!capable(CAP_AUDIT_WRITE))
1037 goto out_destroy_group;
Steve Grubbde8cd832017-10-02 20:21:39 -04001038 }
1039
Eric Paris52c923d2009-12-17 21:24:26 -05001040 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
1041 if (fd < 0)
Lino Sanfilippod8153d42011-06-14 17:29:45 +02001042 goto out_destroy_group;
Eric Paris52c923d2009-12-17 21:24:26 -05001043
1044 return fd;
1045
Lino Sanfilippod8153d42011-06-14 17:29:45 +02001046out_destroy_group:
1047 fsnotify_destroy_group(group);
Eric Paris52c923d2009-12-17 21:24:26 -05001048 return fd;
Eric Paris11637e42009-12-17 21:24:25 -05001049}
Eric Parisbbaa4162009-12-17 21:24:26 -05001050
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001051/* Check if filesystem can encode a unique fid */
Amir Goldstein73072282019-01-10 19:04:39 +02001052static int fanotify_test_fid(struct path *path, __kernel_fsid_t *fsid)
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001053{
Amir Goldstein73072282019-01-10 19:04:39 +02001054 __kernel_fsid_t root_fsid;
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001055 int err;
1056
1057 /*
1058 * Make sure path is not in filesystem with zero fsid (e.g. tmpfs).
1059 */
Amir Goldstein73072282019-01-10 19:04:39 +02001060 err = vfs_get_fsid(path->dentry, fsid);
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001061 if (err)
1062 return err;
1063
Amir Goldstein73072282019-01-10 19:04:39 +02001064 if (!fsid->val[0] && !fsid->val[1])
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001065 return -ENODEV;
1066
1067 /*
1068 * Make sure path is not inside a filesystem subvolume (e.g. btrfs)
1069 * which uses a different fsid than sb root.
1070 */
Amir Goldstein73072282019-01-10 19:04:39 +02001071 err = vfs_get_fsid(path->dentry->d_sb->s_root, &root_fsid);
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001072 if (err)
1073 return err;
1074
Amir Goldstein73072282019-01-10 19:04:39 +02001075 if (root_fsid.val[0] != fsid->val[0] ||
1076 root_fsid.val[1] != fsid->val[1])
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001077 return -EXDEV;
1078
1079 /*
1080 * We need to make sure that the file system supports at least
1081 * encoding a file handle so user can use name_to_handle_at() to
1082 * compare fid returned with event to the file handle of watched
1083 * objects. However, name_to_handle_at() requires that the
1084 * filesystem also supports decoding file handles.
1085 */
1086 if (!path->dentry->d_sb->s_export_op ||
1087 !path->dentry->d_sb->s_export_op->fh_to_dentry)
1088 return -EOPNOTSUPP;
1089
1090 return 0;
1091}
1092
Jan Kara0b3b0942019-05-15 16:28:34 +02001093static int fanotify_events_supported(struct path *path, __u64 mask)
1094{
1095 /*
1096 * Some filesystems such as 'proc' acquire unusual locks when opening
1097 * files. For them fanotify permission events have high chances of
1098 * deadlocking the system - open done when reporting fanotify event
1099 * blocks on this "unusual" lock while another process holding the lock
1100 * waits for fanotify permission event to be answered. Just disallow
1101 * permission events for such filesystems.
1102 */
1103 if (mask & FANOTIFY_PERM_EVENTS &&
1104 path->mnt->mnt_sb->s_type->fs_flags & FS_DISALLOW_NOTIFY_PERM)
1105 return -EINVAL;
1106 return 0;
1107}
1108
Dominik Brodowski183caa32018-03-17 15:06:11 +01001109static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
1110 int dfd, const char __user *pathname)
Eric Parisbbaa4162009-12-17 21:24:26 -05001111{
Eric Paris0ff21db2009-12-17 21:24:29 -05001112 struct inode *inode = NULL;
1113 struct vfsmount *mnt = NULL;
Eric Paris2a3edf82009-12-17 21:24:26 -05001114 struct fsnotify_group *group;
Al Viro2903ff02012-08-28 12:52:22 -04001115 struct fd f;
Eric Paris2a3edf82009-12-17 21:24:26 -05001116 struct path path;
Amir Goldstein73072282019-01-10 19:04:39 +02001117 __kernel_fsid_t __fsid, *fsid = NULL;
Amir Goldsteinbdd5a462018-10-04 00:25:37 +03001118 u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS;
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001119 unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS;
Amir Goldstein3ef86652020-07-16 11:42:13 +03001120 bool ignored = flags & FAN_MARK_IGNORED_MASK;
Amir Goldsteind809daf2020-07-16 11:42:12 +03001121 unsigned int obj_type, fid_mode;
Amir Goldstein85af5d92020-07-16 11:42:15 +03001122 u32 umask = 0;
Al Viro2903ff02012-08-28 12:52:22 -04001123 int ret;
Eric Paris2a3edf82009-12-17 21:24:26 -05001124
1125 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
1126 __func__, fanotify_fd, flags, dfd, pathname, mask);
1127
1128 /* we only use the lower 32 bits as of right now. */
1129 if (mask & ((__u64)0xffffffff << 32))
1130 return -EINVAL;
1131
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001132 if (flags & ~FANOTIFY_MARK_FLAGS)
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -05001133 return -EINVAL;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001134
1135 switch (mark_type) {
1136 case FAN_MARK_INODE:
Aaron Goidelac5656d2019-08-12 11:20:00 -04001137 obj_type = FSNOTIFY_OBJ_TYPE_INODE;
1138 break;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001139 case FAN_MARK_MOUNT:
Aaron Goidelac5656d2019-08-12 11:20:00 -04001140 obj_type = FSNOTIFY_OBJ_TYPE_VFSMOUNT;
1141 break;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001142 case FAN_MARK_FILESYSTEM:
Aaron Goidelac5656d2019-08-12 11:20:00 -04001143 obj_type = FSNOTIFY_OBJ_TYPE_SB;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001144 break;
1145 default:
1146 return -EINVAL;
1147 }
1148
Eric Paris4d926042009-12-17 21:24:34 -05001149 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001150 case FAN_MARK_ADD:
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -05001151 case FAN_MARK_REMOVE:
Lino Sanfilippo1734dee2010-11-22 18:46:33 +01001152 if (!mask)
1153 return -EINVAL;
Heinrich Schuchardtcc299a92014-06-04 16:05:43 -07001154 break;
Eric Paris4d926042009-12-17 21:24:34 -05001155 case FAN_MARK_FLUSH:
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001156 if (flags & ~(FANOTIFY_MARK_TYPE_BITS | FAN_MARK_FLUSH))
Heinrich Schuchardtcc299a92014-06-04 16:05:43 -07001157 return -EINVAL;
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -05001158 break;
1159 default:
1160 return -EINVAL;
1161 }
Eric Paris8fcd6522010-10-28 17:21:59 -04001162
Miklos Szeredi6685df32017-10-30 21:14:56 +01001163 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001164 valid_mask |= FANOTIFY_PERM_EVENTS;
Miklos Szeredi6685df32017-10-30 21:14:56 +01001165
1166 if (mask & ~valid_mask)
Eric Paris2a3edf82009-12-17 21:24:26 -05001167 return -EINVAL;
1168
Amir Goldstein3ef86652020-07-16 11:42:13 +03001169 /* Event flags (ONDIR, ON_CHILD) are meaningless in ignored mask */
1170 if (ignored)
1171 mask &= ~FANOTIFY_EVENT_FLAGS;
1172
Al Viro2903ff02012-08-28 12:52:22 -04001173 f = fdget(fanotify_fd);
1174 if (unlikely(!f.file))
Eric Paris2a3edf82009-12-17 21:24:26 -05001175 return -EBADF;
1176
1177 /* verify that this is indeed an fanotify instance */
1178 ret = -EINVAL;
Al Viro2903ff02012-08-28 12:52:22 -04001179 if (unlikely(f.file->f_op != &fanotify_fops))
Eric Paris2a3edf82009-12-17 21:24:26 -05001180 goto fput_and_out;
Al Viro2903ff02012-08-28 12:52:22 -04001181 group = f.file->private_data;
Eric Paris4231a232010-10-28 17:21:56 -04001182
1183 /*
1184 * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
1185 * allowed to set permissions events.
1186 */
1187 ret = -EINVAL;
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001188 if (mask & FANOTIFY_PERM_EVENTS &&
Eric Paris4231a232010-10-28 17:21:56 -04001189 group->priority == FS_PRIO_0)
1190 goto fput_and_out;
Eric Paris2a3edf82009-12-17 21:24:26 -05001191
Amir Goldstein235328d2019-01-10 19:04:43 +02001192 /*
1193 * Events with data type inode do not carry enough information to report
1194 * event->fd, so we do not allow setting a mask for inode events unless
1195 * group supports reporting fid.
1196 * inode events are not supported on a mount mark, because they do not
1197 * carry enough information (i.e. path) to be filtered by mount point.
1198 */
Amir Goldsteind809daf2020-07-16 11:42:12 +03001199 fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
Amir Goldstein235328d2019-01-10 19:04:43 +02001200 if (mask & FANOTIFY_INODE_EVENTS &&
Amir Goldsteind809daf2020-07-16 11:42:12 +03001201 (!fid_mode || mark_type == FAN_MARK_MOUNT))
Amir Goldstein235328d2019-01-10 19:04:43 +02001202 goto fput_and_out;
1203
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -07001204 if (flags & FAN_MARK_FLUSH) {
1205 ret = 0;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001206 if (mark_type == FAN_MARK_MOUNT)
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -07001207 fsnotify_clear_vfsmount_marks_by_group(group);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001208 else if (mark_type == FAN_MARK_FILESYSTEM)
1209 fsnotify_clear_sb_marks_by_group(group);
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -07001210 else
1211 fsnotify_clear_inode_marks_by_group(group);
1212 goto fput_and_out;
1213 }
1214
Aaron Goidelac5656d2019-08-12 11:20:00 -04001215 ret = fanotify_find_path(dfd, pathname, &path, flags,
1216 (mask & ALL_FSNOTIFY_EVENTS), obj_type);
Eric Paris2a3edf82009-12-17 21:24:26 -05001217 if (ret)
1218 goto fput_and_out;
1219
Jan Kara0b3b0942019-05-15 16:28:34 +02001220 if (flags & FAN_MARK_ADD) {
1221 ret = fanotify_events_supported(&path, mask);
1222 if (ret)
1223 goto path_put_and_out;
1224 }
1225
Amir Goldsteind809daf2020-07-16 11:42:12 +03001226 if (fid_mode) {
Amir Goldstein73072282019-01-10 19:04:39 +02001227 ret = fanotify_test_fid(&path, &__fsid);
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001228 if (ret)
1229 goto path_put_and_out;
Amir Goldstein77115222019-01-10 19:04:37 +02001230
Amir Goldstein73072282019-01-10 19:04:39 +02001231 fsid = &__fsid;
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001232 }
1233
Eric Paris2a3edf82009-12-17 21:24:26 -05001234 /* inode held in place by reference to path; group by fget on fd */
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001235 if (mark_type == FAN_MARK_INODE)
Eric Paris0ff21db2009-12-17 21:24:29 -05001236 inode = path.dentry->d_inode;
1237 else
1238 mnt = path.mnt;
Eric Paris2a3edf82009-12-17 21:24:26 -05001239
Amir Goldstein85af5d92020-07-16 11:42:15 +03001240 /* Mask out FAN_EVENT_ON_CHILD flag for sb/mount/non-dir marks */
1241 if (mnt || !S_ISDIR(inode->i_mode)) {
1242 mask &= ~FAN_EVENT_ON_CHILD;
1243 umask = FAN_EVENT_ON_CHILD;
Amir Goldstein51280632020-07-16 11:42:27 +03001244 /*
1245 * If group needs to report parent fid, register for getting
1246 * events with parent/name info for non-directory.
1247 */
1248 if ((fid_mode & FAN_REPORT_DIR_FID) &&
1249 (flags & FAN_MARK_ADD) && !ignored)
1250 mask |= FAN_EVENT_ON_CHILD;
Amir Goldstein85af5d92020-07-16 11:42:15 +03001251 }
1252
Eric Paris2a3edf82009-12-17 21:24:26 -05001253 /* create/update an inode mark */
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -07001254 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -05001255 case FAN_MARK_ADD:
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001256 if (mark_type == FAN_MARK_MOUNT)
Amir Goldstein77115222019-01-10 19:04:37 +02001257 ret = fanotify_add_vfsmount_mark(group, mnt, mask,
1258 flags, fsid);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001259 else if (mark_type == FAN_MARK_FILESYSTEM)
Amir Goldstein77115222019-01-10 19:04:37 +02001260 ret = fanotify_add_sb_mark(group, mnt->mnt_sb, mask,
1261 flags, fsid);
Eric Paris0ff21db2009-12-17 21:24:29 -05001262 else
Amir Goldstein77115222019-01-10 19:04:37 +02001263 ret = fanotify_add_inode_mark(group, inode, mask,
1264 flags, fsid);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -05001265 break;
1266 case FAN_MARK_REMOVE:
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001267 if (mark_type == FAN_MARK_MOUNT)
Amir Goldstein77115222019-01-10 19:04:37 +02001268 ret = fanotify_remove_vfsmount_mark(group, mnt, mask,
Amir Goldstein85af5d92020-07-16 11:42:15 +03001269 flags, umask);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001270 else if (mark_type == FAN_MARK_FILESYSTEM)
Amir Goldstein77115222019-01-10 19:04:37 +02001271 ret = fanotify_remove_sb_mark(group, mnt->mnt_sb, mask,
Amir Goldstein85af5d92020-07-16 11:42:15 +03001272 flags, umask);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -05001273 else
Amir Goldstein77115222019-01-10 19:04:37 +02001274 ret = fanotify_remove_inode_mark(group, inode, mask,
Amir Goldstein85af5d92020-07-16 11:42:15 +03001275 flags, umask);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -05001276 break;
1277 default:
1278 ret = -EINVAL;
1279 }
Eric Paris2a3edf82009-12-17 21:24:26 -05001280
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001281path_put_and_out:
Eric Paris2a3edf82009-12-17 21:24:26 -05001282 path_put(&path);
1283fput_and_out:
Al Viro2903ff02012-08-28 12:52:22 -04001284 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -05001285 return ret;
Eric Parisbbaa4162009-12-17 21:24:26 -05001286}
Eric Paris2a3edf82009-12-17 21:24:26 -05001287
Brian Gerst2ca408d2020-11-30 17:30:59 -05001288#ifndef CONFIG_ARCH_SPLIT_ARG64
Dominik Brodowski183caa32018-03-17 15:06:11 +01001289SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
1290 __u64, mask, int, dfd,
1291 const char __user *, pathname)
1292{
1293 return do_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname);
1294}
Brian Gerst2ca408d2020-11-30 17:30:59 -05001295#endif
Dominik Brodowski183caa32018-03-17 15:06:11 +01001296
Brian Gerst2ca408d2020-11-30 17:30:59 -05001297#if defined(CONFIG_ARCH_SPLIT_ARG64) || defined(CONFIG_COMPAT)
1298SYSCALL32_DEFINE6(fanotify_mark,
Al Viro91c2e0b2013-03-05 20:10:59 -05001299 int, fanotify_fd, unsigned int, flags,
Brian Gerst2ca408d2020-11-30 17:30:59 -05001300 SC_ARG64(mask), int, dfd,
Al Viro91c2e0b2013-03-05 20:10:59 -05001301 const char __user *, pathname)
1302{
Brian Gerst2ca408d2020-11-30 17:30:59 -05001303 return do_fanotify_mark(fanotify_fd, flags, SC_VAL64(__u64, mask),
1304 dfd, pathname);
Al Viro91c2e0b2013-03-05 20:10:59 -05001305}
1306#endif
1307
Eric Paris2a3edf82009-12-17 21:24:26 -05001308/*
Justin P. Mattockae0e47f2011-03-01 15:06:02 +01001309 * fanotify_user_setup - Our initialization function. Note that we cannot return
Eric Paris2a3edf82009-12-17 21:24:26 -05001310 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
1311 * must result in panic().
1312 */
1313static int __init fanotify_user_setup(void)
1314{
Amir Goldstein929943b2020-07-16 11:42:28 +03001315 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 10);
Amir Goldsteinbdd5a462018-10-04 00:25:37 +03001316 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9);
1317
Shakeel Buttd46eb14b2018-08-17 15:46:39 -07001318 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark,
1319 SLAB_PANIC|SLAB_ACCOUNT);
Jan Kara7088f352020-03-24 17:04:20 +01001320 fanotify_fid_event_cachep = KMEM_CACHE(fanotify_fid_event,
1321 SLAB_PANIC);
1322 fanotify_path_event_cachep = KMEM_CACHE(fanotify_path_event,
1323 SLAB_PANIC);
Miklos Szeredi6685df32017-10-30 21:14:56 +01001324 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
1325 fanotify_perm_event_cachep =
Amir Goldstein33913992019-01-10 19:04:32 +02001326 KMEM_CACHE(fanotify_perm_event, SLAB_PANIC);
Miklos Szeredi6685df32017-10-30 21:14:56 +01001327 }
Eric Paris2a3edf82009-12-17 21:24:26 -05001328
1329 return 0;
1330}
1331device_initcall(fanotify_user_setup);