blob: 16162207e8865a3a8b67126a075a31bf04bab747 [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 Goldstein6f731712021-03-04 12:48:22 +0200103 struct fsnotify_event *fsn_event;
Amir Goldstein929943b2020-07-16 11:42:28 +0300104 unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
Eric Parisa1014f12009-12-17 21:24:26 -0500105
106 pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
107
Jan Kara8c554462019-01-08 13:52:31 +0100108 spin_lock(&group->notification_lock);
Amir Goldstein6f731712021-03-04 12:48:22 +0200109 fsn_event = fsnotify_peek_first_event(group);
110 if (!fsn_event)
Jan Kara8c554462019-01-08 13:52:31 +0100111 goto out;
Eric Parisa1014f12009-12-17 21:24:26 -0500112
Amir Goldstein6f731712021-03-04 12:48:22 +0200113 event = FANOTIFY_E(fsn_event);
114 if (fid_mode)
115 event_size += fanotify_event_info_len(fid_mode, event);
Amir Goldstein5e469c82019-01-10 19:04:35 +0200116
Jan Kara8c554462019-01-08 13:52:31 +0100117 if (event_size > count) {
Jan Kara7088f352020-03-24 17:04:20 +0100118 event = ERR_PTR(-EINVAL);
Jan Kara8c554462019-01-08 13:52:31 +0100119 goto out;
120 }
Amir Goldstein6f731712021-03-04 12:48:22 +0200121
122 /*
123 * Held the notification_lock the whole time, so this is the
124 * same event we peeked above.
125 */
126 fsnotify_remove_first_event(group);
Jan Kara7088f352020-03-24 17:04:20 +0100127 if (fanotify_is_perm_event(event->mask))
128 FANOTIFY_PERM(event)->state = FAN_EVENT_REPORTED;
Jan Kara8c554462019-01-08 13:52:31 +0100129out:
130 spin_unlock(&group->notification_lock);
Jan Kara7088f352020-03-24 17:04:20 +0100131 return event;
Eric Parisa1014f12009-12-17 21:24:26 -0500132}
133
Jan Karaa741c2f2020-03-24 15:27:52 +0100134static int create_fd(struct fsnotify_group *group, struct path *path,
Jan Kara7053aee2014-01-21 15:48:14 -0800135 struct file **file)
Eric Parisa1014f12009-12-17 21:24:26 -0500136{
137 int client_fd;
Eric Parisa1014f12009-12-17 21:24:26 -0500138 struct file *new_file;
139
Yann Droneaud0b37e092014-10-09 15:24:40 -0700140 client_fd = get_unused_fd_flags(group->fanotify_data.f_flags);
Eric Parisa1014f12009-12-17 21:24:26 -0500141 if (client_fd < 0)
142 return client_fd;
143
Eric Parisa1014f12009-12-17 21:24:26 -0500144 /*
145 * we need a new file handle for the userspace program so it can read even if it was
146 * originally opened O_WRONLY.
147 */
Jan Karaa741c2f2020-03-24 15:27:52 +0100148 new_file = dentry_open(path,
149 group->fanotify_data.f_flags | FMODE_NONOTIFY,
150 current_cred());
Eric Parisa1014f12009-12-17 21:24:26 -0500151 if (IS_ERR(new_file)) {
152 /*
153 * we still send an event even if we can't open the file. this
154 * can happen when say tasks are gone and we try to open their
155 * /proc files or we try to open a WRONLY file like in sysfs
156 * we just send the errno to userspace since there isn't much
157 * else we can do.
158 */
159 put_unused_fd(client_fd);
160 client_fd = PTR_ERR(new_file);
161 } else {
Al Viro352e3b22012-08-19 12:30:45 -0400162 *file = new_file;
Eric Parisa1014f12009-12-17 21:24:26 -0500163 }
164
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -0500165 return client_fd;
Eric Parisa1014f12009-12-17 21:24:26 -0500166}
167
Jan Kara40873282019-01-08 14:02:44 +0100168/*
169 * Finish processing of permission event by setting it to ANSWERED state and
170 * drop group->notification_lock.
171 */
172static void finish_permission_event(struct fsnotify_group *group,
173 struct fanotify_perm_event *event,
174 unsigned int response)
175 __releases(&group->notification_lock)
176{
Jan Karafabf7f22019-01-08 15:18:02 +0100177 bool destroy = false;
178
Jan Kara40873282019-01-08 14:02:44 +0100179 assert_spin_locked(&group->notification_lock);
180 event->response = response;
Jan Karafabf7f22019-01-08 15:18:02 +0100181 if (event->state == FAN_EVENT_CANCELED)
182 destroy = true;
183 else
184 event->state = FAN_EVENT_ANSWERED;
Jan Kara40873282019-01-08 14:02:44 +0100185 spin_unlock(&group->notification_lock);
Jan Karafabf7f22019-01-08 15:18:02 +0100186 if (destroy)
187 fsnotify_destroy_event(group, &event->fae.fse);
Jan Kara40873282019-01-08 14:02:44 +0100188}
189
Eric Parisb2d87902009-12-17 21:24:34 -0500190static int process_access_response(struct fsnotify_group *group,
191 struct fanotify_response *response_struct)
192{
Amir Goldstein33913992019-01-10 19:04:32 +0200193 struct fanotify_perm_event *event;
Jan Karaf0834412014-04-03 14:46:33 -0700194 int fd = response_struct->fd;
195 int response = response_struct->response;
Eric Parisb2d87902009-12-17 21:24:34 -0500196
197 pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
198 fd, response);
199 /*
200 * make sure the response is valid, if invalid we do nothing and either
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300201 * userspace can send a valid response or we will clean it up after the
Eric Parisb2d87902009-12-17 21:24:34 -0500202 * timeout
203 */
Steve Grubbde8cd832017-10-02 20:21:39 -0400204 switch (response & ~FAN_AUDIT) {
Eric Parisb2d87902009-12-17 21:24:34 -0500205 case FAN_ALLOW:
206 case FAN_DENY:
207 break;
208 default:
209 return -EINVAL;
210 }
211
212 if (fd < 0)
213 return -EINVAL;
214
Amir Goldstein96a71f22018-09-21 21:20:30 +0300215 if ((response & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT))
Steve Grubbde8cd832017-10-02 20:21:39 -0400216 return -EINVAL;
217
Jan Karaaf6a5112019-01-08 13:28:18 +0100218 spin_lock(&group->notification_lock);
219 list_for_each_entry(event, &group->fanotify_data.access_list,
220 fae.fse.list) {
221 if (event->fd != fd)
222 continue;
Eric Parisb2d87902009-12-17 21:24:34 -0500223
Jan Karaaf6a5112019-01-08 13:28:18 +0100224 list_del_init(&event->fae.fse.list);
Jan Kara40873282019-01-08 14:02:44 +0100225 finish_permission_event(group, event, response);
Jan Karaaf6a5112019-01-08 13:28:18 +0100226 wake_up(&group->fanotify_data.access_waitq);
227 return 0;
228 }
229 spin_unlock(&group->notification_lock);
Eric Parisb2d87902009-12-17 21:24:34 -0500230
Jan Karaaf6a5112019-01-08 13:28:18 +0100231 return -ENOENT;
Eric Parisb2d87902009-12-17 21:24:34 -0500232}
Eric Parisb2d87902009-12-17 21:24:34 -0500233
Amir Goldstein44d705b2020-03-19 17:10:22 +0200234static int copy_info_to_user(__kernel_fsid_t *fsid, struct fanotify_fh *fh,
Amir Goldstein83b7a592020-07-16 11:42:26 +0300235 int info_type, const char *name, size_t name_len,
Amir Goldstein44d705b2020-03-19 17:10:22 +0200236 char __user *buf, size_t count)
Amir Goldstein5e469c82019-01-10 19:04:35 +0200237{
238 struct fanotify_event_info_fid info = { };
239 struct file_handle handle = { };
Jan Karaafc894c2020-03-24 16:55:37 +0100240 unsigned char bounce[FANOTIFY_INLINE_FH_LEN], *fh_buf;
Amir Goldsteincacfb952020-03-19 17:10:21 +0200241 size_t fh_len = fh ? fh->len : 0;
Amir Goldstein44d705b2020-03-19 17:10:22 +0200242 size_t info_len = fanotify_fid_info_len(fh_len, name_len);
243 size_t len = info_len;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200244
Amir Goldstein44d705b2020-03-19 17:10:22 +0200245 pr_debug("%s: fh_len=%zu name_len=%zu, info_len=%zu, count=%zu\n",
246 __func__, fh_len, name_len, info_len, count);
247
Amir Goldstein83b7a592020-07-16 11:42:26 +0300248 if (!fh_len)
Amir Goldstein5e469c82019-01-10 19:04:35 +0200249 return 0;
250
Amir Goldstein44d705b2020-03-19 17:10:22 +0200251 if (WARN_ON_ONCE(len < sizeof(info) || len > count))
Amir Goldstein5e469c82019-01-10 19:04:35 +0200252 return -EFAULT;
253
Amir Goldstein44d705b2020-03-19 17:10:22 +0200254 /*
255 * Copy event info fid header followed by variable sized file handle
256 * and optionally followed by variable sized filename.
257 */
Amir Goldstein83b7a592020-07-16 11:42:26 +0300258 switch (info_type) {
259 case FAN_EVENT_INFO_TYPE_FID:
260 case FAN_EVENT_INFO_TYPE_DFID:
261 if (WARN_ON_ONCE(name_len))
262 return -EFAULT;
263 break;
264 case FAN_EVENT_INFO_TYPE_DFID_NAME:
265 if (WARN_ON_ONCE(!name || !name_len))
266 return -EFAULT;
267 break;
268 default:
269 return -EFAULT;
270 }
271
272 info.hdr.info_type = info_type;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200273 info.hdr.len = len;
Amir Goldsteind766b552020-03-19 17:10:20 +0200274 info.fsid = *fsid;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200275 if (copy_to_user(buf, &info, sizeof(info)))
276 return -EFAULT;
277
278 buf += sizeof(info);
279 len -= sizeof(info);
Amir Goldstein44d705b2020-03-19 17:10:22 +0200280 if (WARN_ON_ONCE(len < sizeof(handle)))
281 return -EFAULT;
282
Jan Karaafc894c2020-03-24 16:55:37 +0100283 handle.handle_type = fh->type;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200284 handle.handle_bytes = fh_len;
285 if (copy_to_user(buf, &handle, sizeof(handle)))
286 return -EFAULT;
287
288 buf += sizeof(handle);
289 len -= sizeof(handle);
Amir Goldstein44d705b2020-03-19 17:10:22 +0200290 if (WARN_ON_ONCE(len < fh_len))
291 return -EFAULT;
292
Jan Karab2d22b62019-03-12 12:42:37 +0100293 /*
Amir Goldstein44d705b2020-03-19 17:10:22 +0200294 * For an inline fh and inline file name, copy through stack to exclude
295 * the copy from usercopy hardening protections.
Jan Karab2d22b62019-03-12 12:42:37 +0100296 */
Jan Karaafc894c2020-03-24 16:55:37 +0100297 fh_buf = fanotify_fh_buf(fh);
Jan Karab2d22b62019-03-12 12:42:37 +0100298 if (fh_len <= FANOTIFY_INLINE_FH_LEN) {
Jan Karaafc894c2020-03-24 16:55:37 +0100299 memcpy(bounce, fh_buf, fh_len);
300 fh_buf = bounce;
Jan Karab2d22b62019-03-12 12:42:37 +0100301 }
Jan Karaafc894c2020-03-24 16:55:37 +0100302 if (copy_to_user(buf, fh_buf, fh_len))
Amir Goldstein5e469c82019-01-10 19:04:35 +0200303 return -EFAULT;
304
Amir Goldstein5e469c82019-01-10 19:04:35 +0200305 buf += fh_len;
306 len -= fh_len;
Amir Goldstein44d705b2020-03-19 17:10:22 +0200307
308 if (name_len) {
309 /* Copy the filename with terminating null */
310 name_len++;
311 if (WARN_ON_ONCE(len < name_len))
312 return -EFAULT;
313
314 if (copy_to_user(buf, name, name_len))
315 return -EFAULT;
316
317 buf += name_len;
318 len -= name_len;
319 }
320
321 /* Pad with 0's */
Amir Goldstein5e469c82019-01-10 19:04:35 +0200322 WARN_ON_ONCE(len < 0 || len >= FANOTIFY_EVENT_ALIGN);
323 if (len > 0 && clear_user(buf, len))
324 return -EFAULT;
325
Amir Goldstein44d705b2020-03-19 17:10:22 +0200326 return info_len;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200327}
328
Eric Parisa1014f12009-12-17 21:24:26 -0500329static ssize_t copy_event_to_user(struct fsnotify_group *group,
Jan Kara7088f352020-03-24 17:04:20 +0100330 struct fanotify_event *event,
Kees Cook5b03a472018-12-04 15:44:46 -0800331 char __user *buf, size_t count)
Eric Parisa1014f12009-12-17 21:24:26 -0500332{
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200333 struct fanotify_event_metadata metadata;
Jan Kara7088f352020-03-24 17:04:20 +0100334 struct path *path = fanotify_event_path(event);
Amir Goldsteinf454fa62020-07-16 11:42:17 +0300335 struct fanotify_info *info = fanotify_event_info(event);
Amir Goldstein83b7a592020-07-16 11:42:26 +0300336 unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200337 struct file *f = NULL;
Amir Goldsteine9e0c892019-01-10 19:04:34 +0200338 int ret, fd = FAN_NOFD;
Amir Goldstein83b7a592020-07-16 11:42:26 +0300339 int info_type = 0;
Eric Parisa1014f12009-12-17 21:24:26 -0500340
Jan Kara7088f352020-03-24 17:04:20 +0100341 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
Eric Parisa1014f12009-12-17 21:24:26 -0500342
Amir Goldstein44d705b2020-03-19 17:10:22 +0200343 metadata.event_len = FAN_EVENT_METADATA_LEN +
Amir Goldstein929943b2020-07-16 11:42:28 +0300344 fanotify_event_info_len(fid_mode, event);
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200345 metadata.metadata_len = FAN_EVENT_METADATA_LEN;
346 metadata.vers = FANOTIFY_METADATA_VERSION;
347 metadata.reserved = 0;
348 metadata.mask = event->mask & FANOTIFY_OUTGOING_EVENTS;
349 metadata.pid = pid_vnr(event->pid);
Eric Parisa1014f12009-12-17 21:24:26 -0500350
Amir Goldstein44d705b2020-03-19 17:10:22 +0200351 if (path && path->mnt && path->dentry) {
Jan Karaafc894c2020-03-24 16:55:37 +0100352 fd = create_fd(group, path, &f);
353 if (fd < 0)
354 return fd;
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200355 }
356 metadata.fd = fd;
357
Al Viro352e3b22012-08-19 12:30:45 -0400358 ret = -EFAULT;
Kees Cook5b03a472018-12-04 15:44:46 -0800359 /*
360 * Sanity check copy size in case get_one_event() and
Fabian Frederickc5e443c2020-05-12 20:18:36 +0200361 * event_len sizes ever get out of sync.
Kees Cook5b03a472018-12-04 15:44:46 -0800362 */
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200363 if (WARN_ON_ONCE(metadata.event_len > count))
Al Viro352e3b22012-08-19 12:30:45 -0400364 goto out_close_fd;
365
Amir Goldstein5e469c82019-01-10 19:04:35 +0200366 if (copy_to_user(buf, &metadata, FAN_EVENT_METADATA_LEN))
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200367 goto out_close_fd;
368
Amir Goldstein44d705b2020-03-19 17:10:22 +0200369 buf += FAN_EVENT_METADATA_LEN;
370 count -= FAN_EVENT_METADATA_LEN;
371
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200372 if (fanotify_is_perm_event(event->mask))
Jan Kara7088f352020-03-24 17:04:20 +0100373 FANOTIFY_PERM(event)->fd = fd;
Eric Parisb2d87902009-12-17 21:24:34 -0500374
Amir Goldstein44d705b2020-03-19 17:10:22 +0200375 if (f)
Al Viro3587b1b2012-11-18 19:19:00 +0000376 fd_install(fd, f);
Amir Goldstein44d705b2020-03-19 17:10:22 +0200377
378 /* Event info records order is: dir fid + name, child fid */
Amir Goldsteinf454fa62020-07-16 11:42:17 +0300379 if (fanotify_event_dir_fh_len(event)) {
Amir Goldstein691d9762020-07-16 11:42:30 +0300380 info_type = info->name_len ? FAN_EVENT_INFO_TYPE_DFID_NAME :
381 FAN_EVENT_INFO_TYPE_DFID;
Amir Goldstein44d705b2020-03-19 17:10:22 +0200382 ret = copy_info_to_user(fanotify_event_fsid(event),
Amir Goldsteinf454fa62020-07-16 11:42:17 +0300383 fanotify_info_dir_fh(info),
Amir Goldstein83b7a592020-07-16 11:42:26 +0300384 info_type, fanotify_info_name(info),
Amir Goldsteinf454fa62020-07-16 11:42:17 +0300385 info->name_len, buf, count);
Amir Goldstein5e469c82019-01-10 19:04:35 +0200386 if (ret < 0)
387 return ret;
Amir Goldstein44d705b2020-03-19 17:10:22 +0200388
389 buf += ret;
390 count -= ret;
391 }
392
393 if (fanotify_event_object_fh_len(event)) {
Amir Goldstein929943b2020-07-16 11:42:28 +0300394 const char *dot = NULL;
395 int dot_len = 0;
396
Amir Goldstein83b7a592020-07-16 11:42:26 +0300397 if (fid_mode == FAN_REPORT_FID || info_type) {
398 /*
399 * With only group flag FAN_REPORT_FID only type FID is
400 * reported. Second info record type is always FID.
401 */
402 info_type = FAN_EVENT_INFO_TYPE_FID;
Amir Goldstein929943b2020-07-16 11:42:28 +0300403 } else if ((fid_mode & FAN_REPORT_NAME) &&
404 (event->mask & FAN_ONDIR)) {
405 /*
406 * With group flag FAN_REPORT_NAME, if name was not
407 * recorded in an event on a directory, report the
408 * name "." with info type DFID_NAME.
409 */
410 info_type = FAN_EVENT_INFO_TYPE_DFID_NAME;
411 dot = ".";
412 dot_len = 1;
Amir Goldstein83b7a592020-07-16 11:42:26 +0300413 } else if ((event->mask & ALL_FSNOTIFY_DIRENT_EVENTS) ||
414 (event->mask & FAN_ONDIR)) {
415 /*
416 * With group flag FAN_REPORT_DIR_FID, a single info
417 * record has type DFID for directory entry modification
418 * event and for event on a directory.
419 */
420 info_type = FAN_EVENT_INFO_TYPE_DFID;
421 } else {
422 /*
423 * With group flags FAN_REPORT_DIR_FID|FAN_REPORT_FID,
424 * a single info record has type FID for event on a
425 * non-directory, when there is no directory to report.
426 * For example, on FAN_DELETE_SELF event.
427 */
428 info_type = FAN_EVENT_INFO_TYPE_FID;
429 }
430
Amir Goldstein44d705b2020-03-19 17:10:22 +0200431 ret = copy_info_to_user(fanotify_event_fsid(event),
432 fanotify_event_object_fh(event),
Amir Goldstein929943b2020-07-16 11:42:28 +0300433 info_type, dot, dot_len, buf, count);
Amir Goldstein44d705b2020-03-19 17:10:22 +0200434 if (ret < 0)
435 return ret;
436
437 buf += ret;
438 count -= ret;
Amir Goldstein5e469c82019-01-10 19:04:35 +0200439 }
440
Amir Goldsteinbb2f7b42019-01-10 19:04:33 +0200441 return metadata.event_len;
Eric Parisb2d87902009-12-17 21:24:34 -0500442
Eric Parisb2d87902009-12-17 21:24:34 -0500443out_close_fd:
Al Viro352e3b22012-08-19 12:30:45 -0400444 if (fd != FAN_NOFD) {
445 put_unused_fd(fd);
446 fput(f);
447 }
Eric Parisb2d87902009-12-17 21:24:34 -0500448 return ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500449}
450
451/* intofiy userspace file descriptor functions */
Al Viro076ccb72017-07-03 01:02:18 -0400452static __poll_t fanotify_poll(struct file *file, poll_table *wait)
Eric Parisa1014f12009-12-17 21:24:26 -0500453{
454 struct fsnotify_group *group = file->private_data;
Al Viro076ccb72017-07-03 01:02:18 -0400455 __poll_t ret = 0;
Eric Parisa1014f12009-12-17 21:24:26 -0500456
457 poll_wait(file, &group->notification_waitq, wait);
Jan Karac21dbe22016-10-07 16:56:52 -0700458 spin_lock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500459 if (!fsnotify_notify_queue_is_empty(group))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800460 ret = EPOLLIN | EPOLLRDNORM;
Jan Karac21dbe22016-10-07 16:56:52 -0700461 spin_unlock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500462
463 return ret;
464}
465
466static ssize_t fanotify_read(struct file *file, char __user *buf,
467 size_t count, loff_t *pos)
468{
469 struct fsnotify_group *group;
Jan Kara7088f352020-03-24 17:04:20 +0100470 struct fanotify_event *event;
Eric Parisa1014f12009-12-17 21:24:26 -0500471 char __user *start;
472 int ret;
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100473 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Eric Parisa1014f12009-12-17 21:24:26 -0500474
475 start = buf;
476 group = file->private_data;
477
478 pr_debug("%s: group=%p\n", __func__, group);
479
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100480 add_wait_queue(&group->notification_waitq, &wait);
Eric Parisa1014f12009-12-17 21:24:26 -0500481 while (1) {
Jan Kara47aaabd2020-07-15 14:06:21 +0200482 /*
483 * User can supply arbitrarily large buffer. Avoid softlockups
484 * in case there are lots of available events.
485 */
486 cond_resched();
Jan Kara7088f352020-03-24 17:04:20 +0100487 event = get_one_event(group, count);
488 if (IS_ERR(event)) {
489 ret = PTR_ERR(event);
Jan Karad8aaab42014-04-03 14:46:35 -0700490 break;
491 }
492
Jan Kara7088f352020-03-24 17:04:20 +0100493 if (!event) {
Jan Karad8aaab42014-04-03 14:46:35 -0700494 ret = -EAGAIN;
495 if (file->f_flags & O_NONBLOCK)
Eric Parisa1014f12009-12-17 21:24:26 -0500496 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700497
498 ret = -ERESTARTSYS;
499 if (signal_pending(current))
Eric Parisa1014f12009-12-17 21:24:26 -0500500 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700501
502 if (start != buf)
503 break;
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100504
505 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Eric Parisa1014f12009-12-17 21:24:26 -0500506 continue;
507 }
508
Jan Kara7088f352020-03-24 17:04:20 +0100509 ret = copy_event_to_user(group, event, buf, count);
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300510 if (unlikely(ret == -EOPENSTALE)) {
511 /*
512 * We cannot report events with stale fd so drop it.
513 * Setting ret to 0 will continue the event loop and
514 * do the right thing if there are no more events to
515 * read (i.e. return bytes read, -EAGAIN or wait).
516 */
517 ret = 0;
518 }
519
Jan Karad8aaab42014-04-03 14:46:35 -0700520 /*
521 * Permission events get queued to wait for response. Other
522 * events can be destroyed now.
523 */
Jan Kara7088f352020-03-24 17:04:20 +0100524 if (!fanotify_is_perm_event(event->mask)) {
525 fsnotify_destroy_event(group, &event->fse);
Jan Karad5078162014-04-03 14:46:36 -0700526 } else {
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300527 if (ret <= 0) {
Jan Kara40873282019-01-08 14:02:44 +0100528 spin_lock(&group->notification_lock);
529 finish_permission_event(group,
Jan Kara7088f352020-03-24 17:04:20 +0100530 FANOTIFY_PERM(event), FAN_DENY);
Jan Karad5078162014-04-03 14:46:36 -0700531 wake_up(&group->fanotify_data.access_waitq);
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300532 } else {
533 spin_lock(&group->notification_lock);
Jan Kara7088f352020-03-24 17:04:20 +0100534 list_add_tail(&event->fse.list,
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300535 &group->fanotify_data.access_list);
536 spin_unlock(&group->notification_lock);
Jan Karad5078162014-04-03 14:46:36 -0700537 }
Jan Karad5078162014-04-03 14:46:36 -0700538 }
Amir Goldstein4ff33aa2017-04-25 14:29:35 +0300539 if (ret < 0)
540 break;
Jan Karad8aaab42014-04-03 14:46:35 -0700541 buf += ret;
542 count -= ret;
Eric Parisa1014f12009-12-17 21:24:26 -0500543 }
Peter Zijlstra536ebe9ca2014-12-16 16:28:38 +0100544 remove_wait_queue(&group->notification_waitq, &wait);
Eric Parisa1014f12009-12-17 21:24:26 -0500545
Eric Parisa1014f12009-12-17 21:24:26 -0500546 if (start != buf && ret != -EFAULT)
547 ret = buf - start;
548 return ret;
549}
550
Eric Parisb2d87902009-12-17 21:24:34 -0500551static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
552{
Eric Parisb2d87902009-12-17 21:24:34 -0500553 struct fanotify_response response = { .fd = -1, .response = -1 };
554 struct fsnotify_group *group;
555 int ret;
556
Miklos Szeredi6685df32017-10-30 21:14:56 +0100557 if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
558 return -EINVAL;
559
Eric Parisb2d87902009-12-17 21:24:34 -0500560 group = file->private_data;
561
Fabian Frederick5e236632020-05-12 20:19:21 +0200562 if (count < sizeof(response))
563 return -EINVAL;
564
565 count = sizeof(response);
Eric Parisb2d87902009-12-17 21:24:34 -0500566
567 pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
568
569 if (copy_from_user(&response, buf, count))
570 return -EFAULT;
571
572 ret = process_access_response(group, &response);
573 if (ret < 0)
574 count = ret;
575
576 return count;
Eric Parisb2d87902009-12-17 21:24:34 -0500577}
578
Eric Paris52c923d2009-12-17 21:24:26 -0500579static int fanotify_release(struct inode *ignored, struct file *file)
580{
581 struct fsnotify_group *group = file->private_data;
Amir Goldstein6f731712021-03-04 12:48:22 +0200582 struct fsnotify_event *fsn_event;
Andrew Morton19ba54f2010-10-28 17:21:59 -0400583
Jan Kara5838d442014-08-06 16:03:28 -0700584 /*
Jan Kara96d41012016-09-19 14:44:30 -0700585 * Stop new events from arriving in the notification queue. since
586 * userspace cannot use fanotify fd anymore, no event can enter or
587 * leave access_list by now either.
588 */
589 fsnotify_group_stop_queueing(group);
590
591 /*
592 * Process all permission events on access_list and notification queue
593 * and simulate reply from userspace.
Jan Kara5838d442014-08-06 16:03:28 -0700594 */
Jan Kara073f6552016-10-07 16:56:55 -0700595 spin_lock(&group->notification_lock);
Jan Karaca6f8692019-01-09 13:21:01 +0100596 while (!list_empty(&group->fanotify_data.access_list)) {
Jan Kara7088f352020-03-24 17:04:20 +0100597 struct fanotify_perm_event *event;
598
Jan Karaca6f8692019-01-09 13:21:01 +0100599 event = list_first_entry(&group->fanotify_data.access_list,
600 struct fanotify_perm_event, fae.fse.list);
Jan Karaf0834412014-04-03 14:46:33 -0700601 list_del_init(&event->fae.fse.list);
Jan Kara40873282019-01-08 14:02:44 +0100602 finish_permission_event(group, event, FAN_ALLOW);
603 spin_lock(&group->notification_lock);
Eric Paris2eebf582010-08-18 12:25:50 -0400604 }
Eric Paris2eebf582010-08-18 12:25:50 -0400605
Jan Kara5838d442014-08-06 16:03:28 -0700606 /*
Jan Kara96d41012016-09-19 14:44:30 -0700607 * Destroy all non-permission events. For permission events just
608 * dequeue them and set the response. They will be freed once the
609 * response is consumed and fanotify_get_response() returns.
Jan Kara5838d442014-08-06 16:03:28 -0700610 */
Amir Goldstein6f731712021-03-04 12:48:22 +0200611 while ((fsn_event = fsnotify_remove_first_event(group))) {
612 struct fanotify_event *event = FANOTIFY_E(fsn_event);
Jan Kara7088f352020-03-24 17:04:20 +0100613
Jan Kara7088f352020-03-24 17:04:20 +0100614 if (!(event->mask & FANOTIFY_PERM_EVENTS)) {
Jan Karac21dbe22016-10-07 16:56:52 -0700615 spin_unlock(&group->notification_lock);
Amir Goldstein6f731712021-03-04 12:48:22 +0200616 fsnotify_destroy_event(group, fsn_event);
Miklos Szeredi6685df32017-10-30 21:14:56 +0100617 } else {
Jan Kara7088f352020-03-24 17:04:20 +0100618 finish_permission_event(group, FANOTIFY_PERM(event),
Jan Kara40873282019-01-08 14:02:44 +0100619 FAN_ALLOW);
Miklos Szeredi6685df32017-10-30 21:14:56 +0100620 }
Jan Kara40873282019-01-08 14:02:44 +0100621 spin_lock(&group->notification_lock);
Jan Kara96d41012016-09-19 14:44:30 -0700622 }
Jan Karac21dbe22016-10-07 16:56:52 -0700623 spin_unlock(&group->notification_lock);
Jan Kara96d41012016-09-19 14:44:30 -0700624
625 /* Response for all permission events it set, wakeup waiters */
Eric Paris2eebf582010-08-18 12:25:50 -0400626 wake_up(&group->fanotify_data.access_waitq);
Eric Paris0a6b6bd2011-10-14 17:43:39 -0400627
Eric Paris52c923d2009-12-17 21:24:26 -0500628 /* matches the fanotify_init->fsnotify_alloc_group */
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200629 fsnotify_destroy_group(group);
Eric Paris52c923d2009-12-17 21:24:26 -0500630
631 return 0;
632}
633
Eric Parisa1014f12009-12-17 21:24:26 -0500634static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
635{
636 struct fsnotify_group *group;
Jan Kara7053aee2014-01-21 15:48:14 -0800637 struct fsnotify_event *fsn_event;
Eric Parisa1014f12009-12-17 21:24:26 -0500638 void __user *p;
639 int ret = -ENOTTY;
640 size_t send_len = 0;
641
642 group = file->private_data;
643
644 p = (void __user *) arg;
645
646 switch (cmd) {
647 case FIONREAD:
Jan Karac21dbe22016-10-07 16:56:52 -0700648 spin_lock(&group->notification_lock);
Jan Kara7053aee2014-01-21 15:48:14 -0800649 list_for_each_entry(fsn_event, &group->notification_list, list)
Eric Parisa1014f12009-12-17 21:24:26 -0500650 send_len += FAN_EVENT_METADATA_LEN;
Jan Karac21dbe22016-10-07 16:56:52 -0700651 spin_unlock(&group->notification_lock);
Eric Parisa1014f12009-12-17 21:24:26 -0500652 ret = put_user(send_len, (int __user *) p);
653 break;
654 }
655
656 return ret;
657}
658
Eric Paris52c923d2009-12-17 21:24:26 -0500659static const struct file_operations fanotify_fops = {
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800660 .show_fdinfo = fanotify_show_fdinfo,
Eric Parisa1014f12009-12-17 21:24:26 -0500661 .poll = fanotify_poll,
662 .read = fanotify_read,
Eric Parisb2d87902009-12-17 21:24:34 -0500663 .write = fanotify_write,
Eric Paris52c923d2009-12-17 21:24:26 -0500664 .fasync = NULL,
665 .release = fanotify_release,
Eric Parisa1014f12009-12-17 21:24:26 -0500666 .unlocked_ioctl = fanotify_ioctl,
Arnd Bergmann1832f2d2018-09-11 21:59:08 +0200667 .compat_ioctl = compat_ptr_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200668 .llseek = noop_llseek,
Eric Paris52c923d2009-12-17 21:24:26 -0500669};
670
Eric Paris2a3edf82009-12-17 21:24:26 -0500671static int fanotify_find_path(int dfd, const char __user *filename,
Aaron Goidelac5656d2019-08-12 11:20:00 -0400672 struct path *path, unsigned int flags, __u64 mask,
673 unsigned int obj_type)
Eric Paris2a3edf82009-12-17 21:24:26 -0500674{
675 int ret;
676
677 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
678 dfd, filename, flags);
679
680 if (filename == NULL) {
Al Viro2903ff02012-08-28 12:52:22 -0400681 struct fd f = fdget(dfd);
Eric Paris2a3edf82009-12-17 21:24:26 -0500682
683 ret = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -0400684 if (!f.file)
Eric Paris2a3edf82009-12-17 21:24:26 -0500685 goto out;
686
687 ret = -ENOTDIR;
688 if ((flags & FAN_MARK_ONLYDIR) &&
Al Viro496ad9a2013-01-23 17:07:38 -0500689 !(S_ISDIR(file_inode(f.file)->i_mode))) {
Al Viro2903ff02012-08-28 12:52:22 -0400690 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500691 goto out;
692 }
693
Al Viro2903ff02012-08-28 12:52:22 -0400694 *path = f.file->f_path;
Eric Paris2a3edf82009-12-17 21:24:26 -0500695 path_get(path);
Al Viro2903ff02012-08-28 12:52:22 -0400696 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -0500697 } else {
698 unsigned int lookup_flags = 0;
699
700 if (!(flags & FAN_MARK_DONT_FOLLOW))
701 lookup_flags |= LOOKUP_FOLLOW;
702 if (flags & FAN_MARK_ONLYDIR)
703 lookup_flags |= LOOKUP_DIRECTORY;
704
705 ret = user_path_at(dfd, filename, lookup_flags, path);
706 if (ret)
707 goto out;
708 }
709
710 /* you can only watch an inode if you have read permissions on it */
Christian Brauner02f92b32021-01-21 14:19:22 +0100711 ret = path_permission(path, MAY_READ);
Aaron Goidelac5656d2019-08-12 11:20:00 -0400712 if (ret) {
713 path_put(path);
714 goto out;
715 }
716
717 ret = security_path_notify(path, mask, obj_type);
Eric Paris2a3edf82009-12-17 21:24:26 -0500718 if (ret)
719 path_put(path);
Aaron Goidelac5656d2019-08-12 11:20:00 -0400720
Eric Paris2a3edf82009-12-17 21:24:26 -0500721out:
722 return ret;
723}
724
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500725static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
Amir Goldstein4ed68142020-07-16 11:42:14 +0300726 __u32 mask, unsigned int flags,
727 __u32 umask, int *destroy)
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500728{
Lino Sanfilippod2c18742015-02-10 14:08:24 -0800729 __u32 oldmask = 0;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500730
Amir Goldstein4ed68142020-07-16 11:42:14 +0300731 /* umask bits cannot be removed by user */
732 mask &= ~umask;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500733 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500734 if (!(flags & FAN_MARK_IGNORED_MASK)) {
735 oldmask = fsn_mark->mask;
Amir Goldsteina72fd222018-10-04 00:25:34 +0300736 fsn_mark->mask &= ~mask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500737 } else {
Amir Goldsteina72fd222018-10-04 00:25:34 +0300738 fsn_mark->ignored_mask &= ~mask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500739 }
Amir Goldstein4ed68142020-07-16 11:42:14 +0300740 /*
741 * We need to keep the mark around even if remaining mask cannot
742 * result in any events (e.g. mask == FAN_ONDIR) to support incremenal
743 * changes to the mask.
744 * Destroy mark when only umask bits remain.
745 */
746 *destroy = !((fsn_mark->mask | fsn_mark->ignored_mask) & ~umask);
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500747 spin_unlock(&fsn_mark->lock);
748
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500749 return mask & oldmask;
750}
751
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300752static int fanotify_remove_mark(struct fsnotify_group *group,
753 fsnotify_connp_t *connp, __u32 mask,
Amir Goldstein4ed68142020-07-16 11:42:14 +0300754 unsigned int flags, __u32 umask)
Eric Paris88826272009-12-17 21:24:28 -0500755{
756 struct fsnotify_mark *fsn_mark = NULL;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500757 __u32 removed;
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200758 int destroy_mark;
Eric Paris88826272009-12-17 21:24:28 -0500759
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700760 mutex_lock(&group->mark_mutex);
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300761 fsn_mark = fsnotify_find_mark(connp, group);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700762 if (!fsn_mark) {
763 mutex_unlock(&group->mark_mutex);
Eric Paris88826272009-12-17 21:24:28 -0500764 return -ENOENT;
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700765 }
Eric Paris88826272009-12-17 21:24:28 -0500766
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200767 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
Amir Goldstein4ed68142020-07-16 11:42:14 +0300768 umask, &destroy_mark);
Amir Goldstein3ac70bf2018-06-23 17:54:50 +0300769 if (removed & fsnotify_conn_mask(fsn_mark->connector))
770 fsnotify_recalc_mask(fsn_mark->connector);
Lino Sanfilippo6dfbd142011-06-14 17:29:49 +0200771 if (destroy_mark)
Jan Kara4712e7222015-09-04 15:43:12 -0700772 fsnotify_detach_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700773 mutex_unlock(&group->mark_mutex);
Jan Kara4712e7222015-09-04 15:43:12 -0700774 if (destroy_mark)
775 fsnotify_free_mark(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700776
Jan Karab1362ed2016-12-21 16:28:45 +0100777 /* matches the fsnotify_find_mark() */
Eric Paris2a3edf82009-12-17 21:24:26 -0500778 fsnotify_put_mark(fsn_mark);
Eric Paris2a3edf82009-12-17 21:24:26 -0500779 return 0;
780}
781
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300782static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
783 struct vfsmount *mnt, __u32 mask,
Amir Goldstein4ed68142020-07-16 11:42:14 +0300784 unsigned int flags, __u32 umask)
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300785{
786 return fanotify_remove_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
Amir Goldstein4ed68142020-07-16 11:42:14 +0300787 mask, flags, umask);
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300788}
789
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300790static int fanotify_remove_sb_mark(struct fsnotify_group *group,
Amir Goldstein4ed68142020-07-16 11:42:14 +0300791 struct super_block *sb, __u32 mask,
792 unsigned int flags, __u32 umask)
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300793{
Amir Goldstein4ed68142020-07-16 11:42:14 +0300794 return fanotify_remove_mark(group, &sb->s_fsnotify_marks, mask,
795 flags, umask);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300796}
797
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300798static int fanotify_remove_inode_mark(struct fsnotify_group *group,
799 struct inode *inode, __u32 mask,
Amir Goldstein4ed68142020-07-16 11:42:14 +0300800 unsigned int flags, __u32 umask)
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300801{
802 return fanotify_remove_mark(group, &inode->i_fsnotify_marks, mask,
Amir Goldstein4ed68142020-07-16 11:42:14 +0300803 flags, umask);
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300804}
805
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500806static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
807 __u32 mask,
808 unsigned int flags)
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500809{
Eric Paris192ca4d2010-10-28 17:21:59 -0400810 __u32 oldmask = -1;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500811
812 spin_lock(&fsn_mark->lock);
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500813 if (!(flags & FAN_MARK_IGNORED_MASK)) {
814 oldmask = fsn_mark->mask;
Amir Goldsteina72fd222018-10-04 00:25:34 +0300815 fsn_mark->mask |= mask;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500816 } else {
Amir Goldsteina72fd222018-10-04 00:25:34 +0300817 fsn_mark->ignored_mask |= mask;
Eric Parisc9778a92009-12-17 21:24:33 -0500818 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
819 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500820 }
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500821 spin_unlock(&fsn_mark->lock);
822
823 return mask & ~oldmask;
824}
825
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700826static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
Amir Goldsteinb812a9f2018-06-23 17:54:48 +0300827 fsnotify_connp_t *connp,
Amir Goldstein77115222019-01-10 19:04:37 +0200828 unsigned int type,
829 __kernel_fsid_t *fsid)
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700830{
831 struct fsnotify_mark *mark;
832 int ret;
833
834 if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
835 return ERR_PTR(-ENOSPC);
836
837 mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
838 if (!mark)
839 return ERR_PTR(-ENOMEM);
840
Jan Kara054c6362016-12-21 18:06:12 +0100841 fsnotify_init_mark(mark, group);
Amir Goldstein77115222019-01-10 19:04:37 +0200842 ret = fsnotify_add_mark_locked(mark, connp, type, 0, fsid);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700843 if (ret) {
844 fsnotify_put_mark(mark);
845 return ERR_PTR(ret);
846 }
847
848 return mark;
849}
850
851
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300852static int fanotify_add_mark(struct fsnotify_group *group,
853 fsnotify_connp_t *connp, unsigned int type,
Amir Goldstein77115222019-01-10 19:04:37 +0200854 __u32 mask, unsigned int flags,
855 __kernel_fsid_t *fsid)
Eric Paris2a3edf82009-12-17 21:24:26 -0500856{
857 struct fsnotify_mark *fsn_mark;
Andreas Gruenbacher912ee39462009-12-17 21:24:28 -0500858 __u32 added;
Eric Paris2a3edf82009-12-17 21:24:26 -0500859
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700860 mutex_lock(&group->mark_mutex);
Amir Goldsteinb812a9f2018-06-23 17:54:48 +0300861 fsn_mark = fsnotify_find_mark(connp, group);
Eric Paris88826272009-12-17 21:24:28 -0500862 if (!fsn_mark) {
Amir Goldstein77115222019-01-10 19:04:37 +0200863 fsn_mark = fanotify_add_new_mark(group, connp, type, fsid);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700864 if (IS_ERR(fsn_mark)) {
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700865 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700866 return PTR_ERR(fsn_mark);
Lino Sanfilippo7b185272013-07-08 15:59:42 -0700867 }
Eric Paris88826272009-12-17 21:24:28 -0500868 }
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500869 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
Amir Goldstein3ac70bf2018-06-23 17:54:50 +0300870 if (added & ~fsnotify_conn_mask(fsn_mark->connector))
871 fsnotify_recalc_mask(fsn_mark->connector);
Jan Karac9747642016-12-14 13:53:46 +0100872 mutex_unlock(&group->mark_mutex);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700873
Lino Sanfilippofa218ab2010-11-09 18:18:16 +0100874 fsnotify_put_mark(fsn_mark);
Lino Sanfilippo5e9c070c2013-07-08 15:59:43 -0700875 return 0;
Eric Paris88826272009-12-17 21:24:28 -0500876}
877
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300878static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
879 struct vfsmount *mnt, __u32 mask,
Amir Goldstein77115222019-01-10 19:04:37 +0200880 unsigned int flags, __kernel_fsid_t *fsid)
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300881{
882 return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
Amir Goldstein77115222019-01-10 19:04:37 +0200883 FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags, fsid);
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300884}
885
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300886static int fanotify_add_sb_mark(struct fsnotify_group *group,
Amir Goldstein77115222019-01-10 19:04:37 +0200887 struct super_block *sb, __u32 mask,
888 unsigned int flags, __kernel_fsid_t *fsid)
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300889{
890 return fanotify_add_mark(group, &sb->s_fsnotify_marks,
Amir Goldstein77115222019-01-10 19:04:37 +0200891 FSNOTIFY_OBJ_TYPE_SB, mask, flags, fsid);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +0300892}
893
Andreas Gruenbacher52202df2009-12-17 21:24:28 -0500894static int fanotify_add_inode_mark(struct fsnotify_group *group,
Eric Parisb9e4e3b2009-12-17 21:24:33 -0500895 struct inode *inode, __u32 mask,
Amir Goldstein77115222019-01-10 19:04:37 +0200896 unsigned int flags, __kernel_fsid_t *fsid)
Eric Paris88826272009-12-17 21:24:28 -0500897{
Eric Paris88826272009-12-17 21:24:28 -0500898 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
Eric Paris2a3edf82009-12-17 21:24:26 -0500899
Eric Paris5322a592010-10-28 17:21:57 -0400900 /*
901 * If some other task has this inode open for write we should not add
902 * an ignored mark, unless that ignored mark is supposed to survive
903 * modification changes anyway.
904 */
905 if ((flags & FAN_MARK_IGNORED_MASK) &&
906 !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
Nikolay Borisovac9498d2018-12-11 10:27:23 +0200907 inode_is_open_for_write(inode))
Eric Paris5322a592010-10-28 17:21:57 -0400908 return 0;
909
Amir Goldsteineaa2c6b2018-06-23 17:54:51 +0300910 return fanotify_add_mark(group, &inode->i_fsnotify_marks,
Amir Goldstein77115222019-01-10 19:04:37 +0200911 FSNOTIFY_OBJ_TYPE_INODE, mask, flags, fsid);
Eric Paris88826272009-12-17 21:24:28 -0500912}
Eric Paris2a3edf82009-12-17 21:24:26 -0500913
Amir Goldsteinb8a6c3a2020-07-08 14:11:42 +0300914static struct fsnotify_event *fanotify_alloc_overflow_event(void)
915{
916 struct fanotify_event *oevent;
917
918 oevent = kmalloc(sizeof(*oevent), GFP_KERNEL_ACCOUNT);
919 if (!oevent)
920 return NULL;
921
922 fanotify_init_event(oevent, 0, FS_Q_OVERFLOW);
923 oevent->type = FANOTIFY_EVENT_TYPE_OVERFLOW;
924
925 return &oevent->fse;
926}
927
Eric Paris52c923d2009-12-17 21:24:26 -0500928/* fanotify syscalls */
Eric Paris08ae8932010-05-27 09:41:40 -0400929SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
Eric Paris11637e42009-12-17 21:24:25 -0500930{
Eric Paris52c923d2009-12-17 21:24:26 -0500931 struct fsnotify_group *group;
932 int f_flags, fd;
Eric Paris4afeff82010-10-28 17:21:58 -0400933 struct user_struct *user;
Amir Goldstein83b7a592020-07-16 11:42:26 +0300934 unsigned int fid_mode = flags & FANOTIFY_FID_BITS;
935 unsigned int class = flags & FANOTIFY_CLASS_BITS;
Eric Paris52c923d2009-12-17 21:24:26 -0500936
Amir Goldstein96a71f22018-09-21 21:20:30 +0300937 pr_debug("%s: flags=%x event_f_flags=%x\n",
938 __func__, flags, event_f_flags);
Eric Paris52c923d2009-12-17 21:24:26 -0500939
Eric Paris52c923d2009-12-17 21:24:26 -0500940 if (!capable(CAP_SYS_ADMIN))
Andreas Gruenbachera2f13ad2010-08-24 12:58:54 +0200941 return -EPERM;
Eric Paris52c923d2009-12-17 21:24:26 -0500942
Steve Grubbde8cd832017-10-02 20:21:39 -0400943#ifdef CONFIG_AUDITSYSCALL
Amir Goldstein23c9dee2018-10-04 00:25:35 +0300944 if (flags & ~(FANOTIFY_INIT_FLAGS | FAN_ENABLE_AUDIT))
Steve Grubbde8cd832017-10-02 20:21:39 -0400945#else
Amir Goldstein23c9dee2018-10-04 00:25:35 +0300946 if (flags & ~FANOTIFY_INIT_FLAGS)
Steve Grubbde8cd832017-10-02 20:21:39 -0400947#endif
Eric Paris52c923d2009-12-17 21:24:26 -0500948 return -EINVAL;
949
Heinrich Schuchardt48149e92014-06-04 16:05:44 -0700950 if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS)
951 return -EINVAL;
952
953 switch (event_f_flags & O_ACCMODE) {
954 case O_RDONLY:
955 case O_RDWR:
956 case O_WRONLY:
957 break;
958 default:
959 return -EINVAL;
960 }
961
Amir Goldstein83b7a592020-07-16 11:42:26 +0300962 if (fid_mode && class != FAN_CLASS_NOTIF)
Amir Goldsteina8b13aa2019-01-10 19:04:36 +0200963 return -EINVAL;
964
Amir Goldstein929943b2020-07-16 11:42:28 +0300965 /*
Amir Goldstein929943b2020-07-16 11:42:28 +0300966 * Child name is reported with parent fid so requires dir fid.
Amir Goldstein691d9762020-07-16 11:42:30 +0300967 * We can report both child fid and dir fid with or without name.
Amir Goldstein929943b2020-07-16 11:42:28 +0300968 */
Amir Goldstein691d9762020-07-16 11:42:30 +0300969 if ((fid_mode & FAN_REPORT_NAME) && !(fid_mode & FAN_REPORT_DIR_FID))
Amir Goldstein83b7a592020-07-16 11:42:26 +0300970 return -EINVAL;
Amir Goldstein83b7a592020-07-16 11:42:26 +0300971
Eric Paris4afeff82010-10-28 17:21:58 -0400972 user = get_current_user();
973 if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {
974 free_uid(user);
975 return -EMFILE;
976 }
977
Eric Parisb2d87902009-12-17 21:24:34 -0500978 f_flags = O_RDWR | FMODE_NONOTIFY;
Eric Paris52c923d2009-12-17 21:24:26 -0500979 if (flags & FAN_CLOEXEC)
980 f_flags |= O_CLOEXEC;
981 if (flags & FAN_NONBLOCK)
982 f_flags |= O_NONBLOCK;
983
984 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
Shakeel Buttac7b79f2020-12-19 20:46:08 -0800985 group = fsnotify_alloc_user_group(&fanotify_fsnotify_ops);
Eric Paris26379192010-11-23 23:48:26 -0500986 if (IS_ERR(group)) {
987 free_uid(user);
Eric Paris52c923d2009-12-17 21:24:26 -0500988 return PTR_ERR(group);
Eric Paris26379192010-11-23 23:48:26 -0500989 }
Eric Paris52c923d2009-12-17 21:24:26 -0500990
Eric Paris4afeff82010-10-28 17:21:58 -0400991 group->fanotify_data.user = user;
Amir Goldstein96a71f22018-09-21 21:20:30 +0300992 group->fanotify_data.flags = flags;
Eric Paris4afeff82010-10-28 17:21:58 -0400993 atomic_inc(&user->fanotify_listeners);
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700994 group->memcg = get_mem_cgroup_from_mm(current->mm);
Eric Paris4afeff82010-10-28 17:21:58 -0400995
Amir Goldsteinb8a6c3a2020-07-08 14:11:42 +0300996 group->overflow_event = fanotify_alloc_overflow_event();
997 if (unlikely(!group->overflow_event)) {
Jan Karaff57cd52014-02-21 19:14:11 +0100998 fd = -ENOMEM;
999 goto out_destroy_group;
1000 }
Jan Karaff57cd52014-02-21 19:14:11 +01001001
Will Woods1e2ee492014-05-06 12:50:10 -07001002 if (force_o_largefile())
1003 event_f_flags |= O_LARGEFILE;
Eric Paris80af2582010-07-28 10:18:37 -04001004 group->fanotify_data.f_flags = event_f_flags;
Eric Paris9e66e422009-12-17 21:24:34 -05001005 init_waitqueue_head(&group->fanotify_data.access_waitq);
1006 INIT_LIST_HEAD(&group->fanotify_data.access_list);
Amir Goldstein83b7a592020-07-16 11:42:26 +03001007 switch (class) {
Eric Paris4231a232010-10-28 17:21:56 -04001008 case FAN_CLASS_NOTIF:
1009 group->priority = FS_PRIO_0;
1010 break;
1011 case FAN_CLASS_CONTENT:
1012 group->priority = FS_PRIO_1;
1013 break;
1014 case FAN_CLASS_PRE_CONTENT:
1015 group->priority = FS_PRIO_2;
1016 break;
1017 default:
1018 fd = -EINVAL;
Lino Sanfilippod8153d42011-06-14 17:29:45 +02001019 goto out_destroy_group;
Eric Paris4231a232010-10-28 17:21:56 -04001020 }
Eric Pariscb2d4292009-12-17 21:24:34 -05001021
Eric Paris5dd03f52010-10-28 17:21:57 -04001022 if (flags & FAN_UNLIMITED_QUEUE) {
1023 fd = -EPERM;
1024 if (!capable(CAP_SYS_ADMIN))
Lino Sanfilippod8153d42011-06-14 17:29:45 +02001025 goto out_destroy_group;
Eric Paris5dd03f52010-10-28 17:21:57 -04001026 group->max_events = UINT_MAX;
1027 } else {
1028 group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
1029 }
Eric Paris2529a0d2010-10-28 17:21:57 -04001030
Eric Parisac7e22d2010-10-28 17:21:58 -04001031 if (flags & FAN_UNLIMITED_MARKS) {
1032 fd = -EPERM;
1033 if (!capable(CAP_SYS_ADMIN))
Lino Sanfilippod8153d42011-06-14 17:29:45 +02001034 goto out_destroy_group;
Eric Parisac7e22d2010-10-28 17:21:58 -04001035 group->fanotify_data.max_marks = UINT_MAX;
1036 } else {
1037 group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS;
1038 }
Eric Parise7099d82010-10-28 17:21:57 -04001039
Steve Grubbde8cd832017-10-02 20:21:39 -04001040 if (flags & FAN_ENABLE_AUDIT) {
1041 fd = -EPERM;
1042 if (!capable(CAP_AUDIT_WRITE))
1043 goto out_destroy_group;
Steve Grubbde8cd832017-10-02 20:21:39 -04001044 }
1045
Eric Paris52c923d2009-12-17 21:24:26 -05001046 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
1047 if (fd < 0)
Lino Sanfilippod8153d42011-06-14 17:29:45 +02001048 goto out_destroy_group;
Eric Paris52c923d2009-12-17 21:24:26 -05001049
1050 return fd;
1051
Lino Sanfilippod8153d42011-06-14 17:29:45 +02001052out_destroy_group:
1053 fsnotify_destroy_group(group);
Eric Paris52c923d2009-12-17 21:24:26 -05001054 return fd;
Eric Paris11637e42009-12-17 21:24:25 -05001055}
Eric Parisbbaa4162009-12-17 21:24:26 -05001056
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001057/* Check if filesystem can encode a unique fid */
Amir Goldstein73072282019-01-10 19:04:39 +02001058static int fanotify_test_fid(struct path *path, __kernel_fsid_t *fsid)
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001059{
Amir Goldstein73072282019-01-10 19:04:39 +02001060 __kernel_fsid_t root_fsid;
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001061 int err;
1062
1063 /*
1064 * Make sure path is not in filesystem with zero fsid (e.g. tmpfs).
1065 */
Amir Goldstein73072282019-01-10 19:04:39 +02001066 err = vfs_get_fsid(path->dentry, fsid);
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001067 if (err)
1068 return err;
1069
Amir Goldstein73072282019-01-10 19:04:39 +02001070 if (!fsid->val[0] && !fsid->val[1])
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001071 return -ENODEV;
1072
1073 /*
1074 * Make sure path is not inside a filesystem subvolume (e.g. btrfs)
1075 * which uses a different fsid than sb root.
1076 */
Amir Goldstein73072282019-01-10 19:04:39 +02001077 err = vfs_get_fsid(path->dentry->d_sb->s_root, &root_fsid);
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001078 if (err)
1079 return err;
1080
Amir Goldstein73072282019-01-10 19:04:39 +02001081 if (root_fsid.val[0] != fsid->val[0] ||
1082 root_fsid.val[1] != fsid->val[1])
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001083 return -EXDEV;
1084
1085 /*
1086 * We need to make sure that the file system supports at least
1087 * encoding a file handle so user can use name_to_handle_at() to
1088 * compare fid returned with event to the file handle of watched
1089 * objects. However, name_to_handle_at() requires that the
1090 * filesystem also supports decoding file handles.
1091 */
1092 if (!path->dentry->d_sb->s_export_op ||
1093 !path->dentry->d_sb->s_export_op->fh_to_dentry)
1094 return -EOPNOTSUPP;
1095
1096 return 0;
1097}
1098
Jan Kara0b3b0942019-05-15 16:28:34 +02001099static int fanotify_events_supported(struct path *path, __u64 mask)
1100{
1101 /*
1102 * Some filesystems such as 'proc' acquire unusual locks when opening
1103 * files. For them fanotify permission events have high chances of
1104 * deadlocking the system - open done when reporting fanotify event
1105 * blocks on this "unusual" lock while another process holding the lock
1106 * waits for fanotify permission event to be answered. Just disallow
1107 * permission events for such filesystems.
1108 */
1109 if (mask & FANOTIFY_PERM_EVENTS &&
1110 path->mnt->mnt_sb->s_type->fs_flags & FS_DISALLOW_NOTIFY_PERM)
1111 return -EINVAL;
1112 return 0;
1113}
1114
Dominik Brodowski183caa32018-03-17 15:06:11 +01001115static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
1116 int dfd, const char __user *pathname)
Eric Parisbbaa4162009-12-17 21:24:26 -05001117{
Eric Paris0ff21db2009-12-17 21:24:29 -05001118 struct inode *inode = NULL;
1119 struct vfsmount *mnt = NULL;
Eric Paris2a3edf82009-12-17 21:24:26 -05001120 struct fsnotify_group *group;
Al Viro2903ff02012-08-28 12:52:22 -04001121 struct fd f;
Eric Paris2a3edf82009-12-17 21:24:26 -05001122 struct path path;
Amir Goldstein73072282019-01-10 19:04:39 +02001123 __kernel_fsid_t __fsid, *fsid = NULL;
Amir Goldsteinbdd5a462018-10-04 00:25:37 +03001124 u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS;
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001125 unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS;
Amir Goldstein3ef86652020-07-16 11:42:13 +03001126 bool ignored = flags & FAN_MARK_IGNORED_MASK;
Amir Goldsteind809daf2020-07-16 11:42:12 +03001127 unsigned int obj_type, fid_mode;
Amir Goldstein85af5d92020-07-16 11:42:15 +03001128 u32 umask = 0;
Al Viro2903ff02012-08-28 12:52:22 -04001129 int ret;
Eric Paris2a3edf82009-12-17 21:24:26 -05001130
1131 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
1132 __func__, fanotify_fd, flags, dfd, pathname, mask);
1133
1134 /* we only use the lower 32 bits as of right now. */
1135 if (mask & ((__u64)0xffffffff << 32))
1136 return -EINVAL;
1137
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001138 if (flags & ~FANOTIFY_MARK_FLAGS)
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -05001139 return -EINVAL;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001140
1141 switch (mark_type) {
1142 case FAN_MARK_INODE:
Aaron Goidelac5656d2019-08-12 11:20:00 -04001143 obj_type = FSNOTIFY_OBJ_TYPE_INODE;
1144 break;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001145 case FAN_MARK_MOUNT:
Aaron Goidelac5656d2019-08-12 11:20:00 -04001146 obj_type = FSNOTIFY_OBJ_TYPE_VFSMOUNT;
1147 break;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001148 case FAN_MARK_FILESYSTEM:
Aaron Goidelac5656d2019-08-12 11:20:00 -04001149 obj_type = FSNOTIFY_OBJ_TYPE_SB;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001150 break;
1151 default:
1152 return -EINVAL;
1153 }
1154
Eric Paris4d926042009-12-17 21:24:34 -05001155 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001156 case FAN_MARK_ADD:
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -05001157 case FAN_MARK_REMOVE:
Lino Sanfilippo1734dee2010-11-22 18:46:33 +01001158 if (!mask)
1159 return -EINVAL;
Heinrich Schuchardtcc299a92014-06-04 16:05:43 -07001160 break;
Eric Paris4d926042009-12-17 21:24:34 -05001161 case FAN_MARK_FLUSH:
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001162 if (flags & ~(FANOTIFY_MARK_TYPE_BITS | FAN_MARK_FLUSH))
Heinrich Schuchardtcc299a92014-06-04 16:05:43 -07001163 return -EINVAL;
Andreas Gruenbacher88380fe2009-12-17 21:24:29 -05001164 break;
1165 default:
1166 return -EINVAL;
1167 }
Eric Paris8fcd6522010-10-28 17:21:59 -04001168
Miklos Szeredi6685df32017-10-30 21:14:56 +01001169 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001170 valid_mask |= FANOTIFY_PERM_EVENTS;
Miklos Szeredi6685df32017-10-30 21:14:56 +01001171
1172 if (mask & ~valid_mask)
Eric Paris2a3edf82009-12-17 21:24:26 -05001173 return -EINVAL;
1174
Amir Goldstein3ef86652020-07-16 11:42:13 +03001175 /* Event flags (ONDIR, ON_CHILD) are meaningless in ignored mask */
1176 if (ignored)
1177 mask &= ~FANOTIFY_EVENT_FLAGS;
1178
Al Viro2903ff02012-08-28 12:52:22 -04001179 f = fdget(fanotify_fd);
1180 if (unlikely(!f.file))
Eric Paris2a3edf82009-12-17 21:24:26 -05001181 return -EBADF;
1182
1183 /* verify that this is indeed an fanotify instance */
1184 ret = -EINVAL;
Al Viro2903ff02012-08-28 12:52:22 -04001185 if (unlikely(f.file->f_op != &fanotify_fops))
Eric Paris2a3edf82009-12-17 21:24:26 -05001186 goto fput_and_out;
Al Viro2903ff02012-08-28 12:52:22 -04001187 group = f.file->private_data;
Eric Paris4231a232010-10-28 17:21:56 -04001188
1189 /*
1190 * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
1191 * allowed to set permissions events.
1192 */
1193 ret = -EINVAL;
Amir Goldstein23c9dee2018-10-04 00:25:35 +03001194 if (mask & FANOTIFY_PERM_EVENTS &&
Eric Paris4231a232010-10-28 17:21:56 -04001195 group->priority == FS_PRIO_0)
1196 goto fput_and_out;
Eric Paris2a3edf82009-12-17 21:24:26 -05001197
Amir Goldstein235328d2019-01-10 19:04:43 +02001198 /*
1199 * Events with data type inode do not carry enough information to report
1200 * event->fd, so we do not allow setting a mask for inode events unless
1201 * group supports reporting fid.
1202 * inode events are not supported on a mount mark, because they do not
1203 * carry enough information (i.e. path) to be filtered by mount point.
1204 */
Amir Goldsteind809daf2020-07-16 11:42:12 +03001205 fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
Amir Goldstein235328d2019-01-10 19:04:43 +02001206 if (mask & FANOTIFY_INODE_EVENTS &&
Amir Goldsteind809daf2020-07-16 11:42:12 +03001207 (!fid_mode || mark_type == FAN_MARK_MOUNT))
Amir Goldstein235328d2019-01-10 19:04:43 +02001208 goto fput_and_out;
1209
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -07001210 if (flags & FAN_MARK_FLUSH) {
1211 ret = 0;
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001212 if (mark_type == FAN_MARK_MOUNT)
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -07001213 fsnotify_clear_vfsmount_marks_by_group(group);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001214 else if (mark_type == FAN_MARK_FILESYSTEM)
1215 fsnotify_clear_sb_marks_by_group(group);
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -07001216 else
1217 fsnotify_clear_inode_marks_by_group(group);
1218 goto fput_and_out;
1219 }
1220
Aaron Goidelac5656d2019-08-12 11:20:00 -04001221 ret = fanotify_find_path(dfd, pathname, &path, flags,
1222 (mask & ALL_FSNOTIFY_EVENTS), obj_type);
Eric Paris2a3edf82009-12-17 21:24:26 -05001223 if (ret)
1224 goto fput_and_out;
1225
Jan Kara0b3b0942019-05-15 16:28:34 +02001226 if (flags & FAN_MARK_ADD) {
1227 ret = fanotify_events_supported(&path, mask);
1228 if (ret)
1229 goto path_put_and_out;
1230 }
1231
Amir Goldsteind809daf2020-07-16 11:42:12 +03001232 if (fid_mode) {
Amir Goldstein73072282019-01-10 19:04:39 +02001233 ret = fanotify_test_fid(&path, &__fsid);
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001234 if (ret)
1235 goto path_put_and_out;
Amir Goldstein77115222019-01-10 19:04:37 +02001236
Amir Goldstein73072282019-01-10 19:04:39 +02001237 fsid = &__fsid;
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001238 }
1239
Eric Paris2a3edf82009-12-17 21:24:26 -05001240 /* inode held in place by reference to path; group by fget on fd */
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001241 if (mark_type == FAN_MARK_INODE)
Eric Paris0ff21db2009-12-17 21:24:29 -05001242 inode = path.dentry->d_inode;
1243 else
1244 mnt = path.mnt;
Eric Paris2a3edf82009-12-17 21:24:26 -05001245
Amir Goldstein85af5d92020-07-16 11:42:15 +03001246 /* Mask out FAN_EVENT_ON_CHILD flag for sb/mount/non-dir marks */
1247 if (mnt || !S_ISDIR(inode->i_mode)) {
1248 mask &= ~FAN_EVENT_ON_CHILD;
1249 umask = FAN_EVENT_ON_CHILD;
Amir Goldstein51280632020-07-16 11:42:27 +03001250 /*
1251 * If group needs to report parent fid, register for getting
1252 * events with parent/name info for non-directory.
1253 */
1254 if ((fid_mode & FAN_REPORT_DIR_FID) &&
1255 (flags & FAN_MARK_ADD) && !ignored)
1256 mask |= FAN_EVENT_ON_CHILD;
Amir Goldstein85af5d92020-07-16 11:42:15 +03001257 }
1258
Eric Paris2a3edf82009-12-17 21:24:26 -05001259 /* create/update an inode mark */
Heinrich Schuchardt0a8dd2d2014-06-04 16:05:40 -07001260 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -05001261 case FAN_MARK_ADD:
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001262 if (mark_type == FAN_MARK_MOUNT)
Amir Goldstein77115222019-01-10 19:04:37 +02001263 ret = fanotify_add_vfsmount_mark(group, mnt, mask,
1264 flags, fsid);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001265 else if (mark_type == FAN_MARK_FILESYSTEM)
Amir Goldstein77115222019-01-10 19:04:37 +02001266 ret = fanotify_add_sb_mark(group, mnt->mnt_sb, mask,
1267 flags, fsid);
Eric Paris0ff21db2009-12-17 21:24:29 -05001268 else
Amir Goldstein77115222019-01-10 19:04:37 +02001269 ret = fanotify_add_inode_mark(group, inode, mask,
1270 flags, fsid);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -05001271 break;
1272 case FAN_MARK_REMOVE:
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001273 if (mark_type == FAN_MARK_MOUNT)
Amir Goldstein77115222019-01-10 19:04:37 +02001274 ret = fanotify_remove_vfsmount_mark(group, mnt, mask,
Amir Goldstein85af5d92020-07-16 11:42:15 +03001275 flags, umask);
Amir Goldsteind54f4fb2018-09-01 10:41:13 +03001276 else if (mark_type == FAN_MARK_FILESYSTEM)
Amir Goldstein77115222019-01-10 19:04:37 +02001277 ret = fanotify_remove_sb_mark(group, mnt->mnt_sb, mask,
Amir Goldstein85af5d92020-07-16 11:42:15 +03001278 flags, umask);
Andreas Gruenbacherf3640192009-12-17 21:24:29 -05001279 else
Amir Goldstein77115222019-01-10 19:04:37 +02001280 ret = fanotify_remove_inode_mark(group, inode, mask,
Amir Goldstein85af5d92020-07-16 11:42:15 +03001281 flags, umask);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -05001282 break;
1283 default:
1284 ret = -EINVAL;
1285 }
Eric Paris2a3edf82009-12-17 21:24:26 -05001286
Amir Goldsteina8b13aa2019-01-10 19:04:36 +02001287path_put_and_out:
Eric Paris2a3edf82009-12-17 21:24:26 -05001288 path_put(&path);
1289fput_and_out:
Al Viro2903ff02012-08-28 12:52:22 -04001290 fdput(f);
Eric Paris2a3edf82009-12-17 21:24:26 -05001291 return ret;
Eric Parisbbaa4162009-12-17 21:24:26 -05001292}
Eric Paris2a3edf82009-12-17 21:24:26 -05001293
Brian Gerst2ca408d2020-11-30 17:30:59 -05001294#ifndef CONFIG_ARCH_SPLIT_ARG64
Dominik Brodowski183caa32018-03-17 15:06:11 +01001295SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
1296 __u64, mask, int, dfd,
1297 const char __user *, pathname)
1298{
1299 return do_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname);
1300}
Brian Gerst2ca408d2020-11-30 17:30:59 -05001301#endif
Dominik Brodowski183caa32018-03-17 15:06:11 +01001302
Brian Gerst2ca408d2020-11-30 17:30:59 -05001303#if defined(CONFIG_ARCH_SPLIT_ARG64) || defined(CONFIG_COMPAT)
1304SYSCALL32_DEFINE6(fanotify_mark,
Al Viro91c2e0b2013-03-05 20:10:59 -05001305 int, fanotify_fd, unsigned int, flags,
Brian Gerst2ca408d2020-11-30 17:30:59 -05001306 SC_ARG64(mask), int, dfd,
Al Viro91c2e0b2013-03-05 20:10:59 -05001307 const char __user *, pathname)
1308{
Brian Gerst2ca408d2020-11-30 17:30:59 -05001309 return do_fanotify_mark(fanotify_fd, flags, SC_VAL64(__u64, mask),
1310 dfd, pathname);
Al Viro91c2e0b2013-03-05 20:10:59 -05001311}
1312#endif
1313
Eric Paris2a3edf82009-12-17 21:24:26 -05001314/*
Justin P. Mattockae0e47f2011-03-01 15:06:02 +01001315 * fanotify_user_setup - Our initialization function. Note that we cannot return
Eric Paris2a3edf82009-12-17 21:24:26 -05001316 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
1317 * must result in panic().
1318 */
1319static int __init fanotify_user_setup(void)
1320{
Amir Goldstein929943b2020-07-16 11:42:28 +03001321 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 10);
Amir Goldsteinbdd5a462018-10-04 00:25:37 +03001322 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9);
1323
Shakeel Buttd46eb14b2018-08-17 15:46:39 -07001324 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark,
1325 SLAB_PANIC|SLAB_ACCOUNT);
Jan Kara7088f352020-03-24 17:04:20 +01001326 fanotify_fid_event_cachep = KMEM_CACHE(fanotify_fid_event,
1327 SLAB_PANIC);
1328 fanotify_path_event_cachep = KMEM_CACHE(fanotify_path_event,
1329 SLAB_PANIC);
Miklos Szeredi6685df32017-10-30 21:14:56 +01001330 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
1331 fanotify_perm_event_cachep =
Amir Goldstein33913992019-01-10 19:04:32 +02001332 KMEM_CACHE(fanotify_perm_event, SLAB_PANIC);
Miklos Szeredi6685df32017-10-30 21:14:56 +01001333 }
Eric Paris2a3edf82009-12-17 21:24:26 -05001334
1335 return 0;
1336}
1337device_initcall(fanotify_user_setup);