]> git.ipfire.org Git - people/ms/linux.git/blame - fs/notify/fanotify/fanotify_user.c
fanotify: Simplify cleaning of access_list
[people/ms/linux.git] / fs / notify / fanotify / fanotify_user.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
33d3dfff 2#include <linux/fanotify.h>
11637e4b 3#include <linux/fcntl.h>
2a3edf86 4#include <linux/file.h>
11637e4b 5#include <linux/fs.h>
52c923dd 6#include <linux/anon_inodes.h>
11637e4b 7#include <linux/fsnotify_backend.h>
2a3edf86 8#include <linux/init.h>
a1014f10 9#include <linux/mount.h>
2a3edf86 10#include <linux/namei.h>
a1014f10 11#include <linux/poll.h>
11637e4b
EP
12#include <linux/security.h>
13#include <linux/syscalls.h>
e4e047a2 14#include <linux/slab.h>
2a3edf86 15#include <linux/types.h>
a1014f10 16#include <linux/uaccess.h>
91c2e0bc 17#include <linux/compat.h>
174cd4b1 18#include <linux/sched/signal.h>
d46eb14b 19#include <linux/memcontrol.h>
a8b13aa2
AG
20#include <linux/statfs.h>
21#include <linux/exportfs.h>
a1014f10
EP
22
23#include <asm/ioctls.h>
11637e4b 24
c63181e6 25#include "../../mount.h"
be77196b 26#include "../fdinfo.h"
7053aee2 27#include "fanotify.h"
c63181e6 28
2529a0df 29#define FANOTIFY_DEFAULT_MAX_EVENTS 16384
e7099d8a 30#define FANOTIFY_DEFAULT_MAX_MARKS 8192
4afeff85 31#define FANOTIFY_DEFAULT_MAX_LISTENERS 128
2529a0df 32
48149e9d
HS
33/*
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
33d3dfff 46extern const struct fsnotify_ops fanotify_fsnotify_ops;
11637e4b 47
054c636e 48struct kmem_cache *fanotify_mark_cache __read_mostly;
7053aee2 49struct kmem_cache *fanotify_event_cachep __read_mostly;
f083441b 50struct kmem_cache *fanotify_perm_event_cachep __read_mostly;
2a3edf86 51
5e469c83
AG
52#define FANOTIFY_EVENT_ALIGN 4
53
54static int fanotify_event_info_len(struct fanotify_event *event)
55{
56 if (!fanotify_event_has_fid(event))
57 return 0;
58
59 return roundup(sizeof(struct fanotify_event_info_fid) +
60 sizeof(struct file_handle) + event->fh_len,
61 FANOTIFY_EVENT_ALIGN);
62}
63
a1014f10
EP
64/*
65 * Get an fsnotify notification event if one exists and is small
66 * enough to fit in "count". Return an error pointer if the count
67 * is not large enough.
a1014f10
EP
68 */
69static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
70 size_t count)
71{
5e469c83 72 size_t event_size = FAN_EVENT_METADATA_LEN;
8c554466 73 struct fsnotify_event *fsn_event = NULL;
a1014f10
EP
74
75 pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
76
8c554466 77 spin_lock(&group->notification_lock);
a1014f10 78 if (fsnotify_notify_queue_is_empty(group))
8c554466 79 goto out;
a1014f10 80
5e469c83 81 if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) {
8c554466
JK
82 event_size += fanotify_event_info_len(
83 FANOTIFY_E(fsnotify_peek_first_event(group)));
5e469c83
AG
84 }
85
8c554466
JK
86 if (event_size > count) {
87 fsn_event = ERR_PTR(-EINVAL);
88 goto out;
89 }
90 fsn_event = fsnotify_remove_first_event(group);
91out:
92 spin_unlock(&group->notification_lock);
93 return fsn_event;
a1014f10
EP
94}
95
352e3b24 96static int create_fd(struct fsnotify_group *group,
33913997 97 struct fanotify_event *event,
7053aee2 98 struct file **file)
a1014f10
EP
99{
100 int client_fd;
a1014f10
EP
101 struct file *new_file;
102
22aa425d 103 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
a1014f10 104
0b37e097 105 client_fd = get_unused_fd_flags(group->fanotify_data.f_flags);
a1014f10
EP
106 if (client_fd < 0)
107 return client_fd;
108
a1014f10
EP
109 /*
110 * we need a new file handle for the userspace program so it can read even if it was
111 * originally opened O_WRONLY.
112 */
a1014f10
EP
113 /* it's possible this event was an overflow event. in that case dentry and mnt
114 * are NULL; That's fine, just don't call dentry open */
765927b2
AV
115 if (event->path.dentry && event->path.mnt)
116 new_file = dentry_open(&event->path,
80af2588 117 group->fanotify_data.f_flags | FMODE_NONOTIFY,
a1014f10
EP
118 current_cred());
119 else
120 new_file = ERR_PTR(-EOVERFLOW);
121 if (IS_ERR(new_file)) {
122 /*
123 * we still send an event even if we can't open the file. this
124 * can happen when say tasks are gone and we try to open their
125 * /proc files or we try to open a WRONLY file like in sysfs
126 * we just send the errno to userspace since there isn't much
127 * else we can do.
128 */
129 put_unused_fd(client_fd);
130 client_fd = PTR_ERR(new_file);
131 } else {
352e3b24 132 *file = new_file;
a1014f10
EP
133 }
134
22aa425d 135 return client_fd;
a1014f10
EP
136}
137
b2d87909
EP
138static int process_access_response(struct fsnotify_group *group,
139 struct fanotify_response *response_struct)
140{
33913997 141 struct fanotify_perm_event *event;
f083441b
JK
142 int fd = response_struct->fd;
143 int response = response_struct->response;
b2d87909
EP
144
145 pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
146 fd, response);
147 /*
148 * make sure the response is valid, if invalid we do nothing and either
25985edc 149 * userspace can send a valid response or we will clean it up after the
b2d87909
EP
150 * timeout
151 */
de8cd83e 152 switch (response & ~FAN_AUDIT) {
b2d87909
EP
153 case FAN_ALLOW:
154 case FAN_DENY:
155 break;
156 default:
157 return -EINVAL;
158 }
159
160 if (fd < 0)
161 return -EINVAL;
162
96a71f21 163 if ((response & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT))
de8cd83e
SG
164 return -EINVAL;
165
af6a5113
JK
166 spin_lock(&group->notification_lock);
167 list_for_each_entry(event, &group->fanotify_data.access_list,
168 fae.fse.list) {
169 if (event->fd != fd)
170 continue;
b2d87909 171
af6a5113
JK
172 list_del_init(&event->fae.fse.list);
173 event->response = response;
174 spin_unlock(&group->notification_lock);
175 wake_up(&group->fanotify_data.access_waitq);
176 return 0;
177 }
178 spin_unlock(&group->notification_lock);
b2d87909 179
af6a5113 180 return -ENOENT;
b2d87909 181}
b2d87909 182
5e469c83
AG
183static int copy_fid_to_user(struct fanotify_event *event, char __user *buf)
184{
185 struct fanotify_event_info_fid info = { };
186 struct file_handle handle = { };
187 size_t fh_len = event->fh_len;
188 size_t len = fanotify_event_info_len(event);
189
190 if (!len)
191 return 0;
192
193 if (WARN_ON_ONCE(len < sizeof(info) + sizeof(handle) + fh_len))
194 return -EFAULT;
195
196 /* Copy event info fid header followed by vaiable sized file handle */
197 info.hdr.info_type = FAN_EVENT_INFO_TYPE_FID;
198 info.hdr.len = len;
199 info.fsid = event->fid.fsid;
200 if (copy_to_user(buf, &info, sizeof(info)))
201 return -EFAULT;
202
203 buf += sizeof(info);
204 len -= sizeof(info);
205 handle.handle_type = event->fh_type;
206 handle.handle_bytes = fh_len;
207 if (copy_to_user(buf, &handle, sizeof(handle)))
208 return -EFAULT;
209
210 buf += sizeof(handle);
211 len -= sizeof(handle);
212 if (copy_to_user(buf, fanotify_event_fh(event), fh_len))
213 return -EFAULT;
214
215 /* Pad with 0's */
216 buf += fh_len;
217 len -= fh_len;
218 WARN_ON_ONCE(len < 0 || len >= FANOTIFY_EVENT_ALIGN);
219 if (len > 0 && clear_user(buf, len))
220 return -EFAULT;
221
222 return 0;
223}
224
a1014f10 225static ssize_t copy_event_to_user(struct fsnotify_group *group,
bb2f7b45 226 struct fsnotify_event *fsn_event,
5b03a472 227 char __user *buf, size_t count)
a1014f10 228{
bb2f7b45
AG
229 struct fanotify_event_metadata metadata;
230 struct fanotify_event *event;
231 struct file *f = NULL;
e9e0c890 232 int ret, fd = FAN_NOFD;
a1014f10 233
bb2f7b45 234 pr_debug("%s: group=%p event=%p\n", __func__, group, fsn_event);
a1014f10 235
bb2f7b45
AG
236 event = container_of(fsn_event, struct fanotify_event, fse);
237 metadata.event_len = FAN_EVENT_METADATA_LEN;
238 metadata.metadata_len = FAN_EVENT_METADATA_LEN;
239 metadata.vers = FANOTIFY_METADATA_VERSION;
240 metadata.reserved = 0;
241 metadata.mask = event->mask & FANOTIFY_OUTGOING_EVENTS;
242 metadata.pid = pid_vnr(event->pid);
243
e9e0c890 244 if (fanotify_event_has_path(event)) {
bb2f7b45
AG
245 fd = create_fd(group, event, &f);
246 if (fd < 0)
247 return fd;
5e469c83
AG
248 } else if (fanotify_event_has_fid(event)) {
249 metadata.event_len += fanotify_event_info_len(event);
bb2f7b45
AG
250 }
251 metadata.fd = fd;
b2d87909 252
b2d87909 253 ret = -EFAULT;
5b03a472
KC
254 /*
255 * Sanity check copy size in case get_one_event() and
256 * fill_event_metadata() event_len sizes ever get out of sync.
257 */
bb2f7b45 258 if (WARN_ON_ONCE(metadata.event_len > count))
5b03a472 259 goto out_close_fd;
bb2f7b45 260
5e469c83 261 if (copy_to_user(buf, &metadata, FAN_EVENT_METADATA_LEN))
352e3b24
AV
262 goto out_close_fd;
263
bb2f7b45
AG
264 if (fanotify_is_perm_event(event->mask))
265 FANOTIFY_PE(fsn_event)->fd = fd;
a1014f10 266
5e469c83 267 if (fanotify_event_has_path(event)) {
3587b1b0 268 fd_install(fd, f);
5e469c83
AG
269 } else if (fanotify_event_has_fid(event)) {
270 ret = copy_fid_to_user(event, buf + FAN_EVENT_METADATA_LEN);
271 if (ret < 0)
272 return ret;
273 }
274
bb2f7b45 275 return metadata.event_len;
b2d87909 276
b2d87909 277out_close_fd:
352e3b24
AV
278 if (fd != FAN_NOFD) {
279 put_unused_fd(fd);
280 fput(f);
281 }
b2d87909 282 return ret;
a1014f10
EP
283}
284
285/* intofiy userspace file descriptor functions */
076ccb76 286static __poll_t fanotify_poll(struct file *file, poll_table *wait)
a1014f10
EP
287{
288 struct fsnotify_group *group = file->private_data;
076ccb76 289 __poll_t ret = 0;
a1014f10
EP
290
291 poll_wait(file, &group->notification_waitq, wait);
c21dbe20 292 spin_lock(&group->notification_lock);
a1014f10 293 if (!fsnotify_notify_queue_is_empty(group))
a9a08845 294 ret = EPOLLIN | EPOLLRDNORM;
c21dbe20 295 spin_unlock(&group->notification_lock);
a1014f10
EP
296
297 return ret;
298}
299
300static ssize_t fanotify_read(struct file *file, char __user *buf,
301 size_t count, loff_t *pos)
302{
303 struct fsnotify_group *group;
304 struct fsnotify_event *kevent;
305 char __user *start;
306 int ret;
536ebe9c 307 DEFINE_WAIT_FUNC(wait, woken_wake_function);
a1014f10
EP
308
309 start = buf;
310 group = file->private_data;
311
312 pr_debug("%s: group=%p\n", __func__, group);
313
536ebe9c 314 add_wait_queue(&group->notification_waitq, &wait);
a1014f10 315 while (1) {
a1014f10 316 kevent = get_one_event(group, count);
d8aaab4f 317 if (IS_ERR(kevent)) {
a1014f10 318 ret = PTR_ERR(kevent);
d8aaab4f
JK
319 break;
320 }
321
322 if (!kevent) {
323 ret = -EAGAIN;
324 if (file->f_flags & O_NONBLOCK)
a1014f10 325 break;
d8aaab4f
JK
326
327 ret = -ERESTARTSYS;
328 if (signal_pending(current))
329 break;
330
331 if (start != buf)
a1014f10 332 break;
536ebe9c
PZ
333
334 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
a1014f10
EP
335 continue;
336 }
337
5b03a472 338 ret = copy_event_to_user(group, kevent, buf, count);
4ff33aaf
AG
339 if (unlikely(ret == -EOPENSTALE)) {
340 /*
341 * We cannot report events with stale fd so drop it.
342 * Setting ret to 0 will continue the event loop and
343 * do the right thing if there are no more events to
344 * read (i.e. return bytes read, -EAGAIN or wait).
345 */
346 ret = 0;
347 }
348
d8aaab4f
JK
349 /*
350 * Permission events get queued to wait for response. Other
351 * events can be destroyed now.
352 */
a0a92d26 353 if (!fanotify_is_perm_event(FANOTIFY_E(kevent)->mask)) {
d8aaab4f 354 fsnotify_destroy_event(group, kevent);
d507816b 355 } else {
4ff33aaf 356 if (ret <= 0) {
d507816b
JK
357 FANOTIFY_PE(kevent)->response = FAN_DENY;
358 wake_up(&group->fanotify_data.access_waitq);
4ff33aaf
AG
359 } else {
360 spin_lock(&group->notification_lock);
361 list_add_tail(&kevent->list,
362 &group->fanotify_data.access_list);
363 spin_unlock(&group->notification_lock);
d507816b 364 }
d507816b 365 }
4ff33aaf
AG
366 if (ret < 0)
367 break;
d8aaab4f
JK
368 buf += ret;
369 count -= ret;
a1014f10 370 }
536ebe9c 371 remove_wait_queue(&group->notification_waitq, &wait);
a1014f10 372
a1014f10
EP
373 if (start != buf && ret != -EFAULT)
374 ret = buf - start;
375 return ret;
376}
377
b2d87909
EP
378static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
379{
b2d87909
EP
380 struct fanotify_response response = { .fd = -1, .response = -1 };
381 struct fsnotify_group *group;
382 int ret;
383
6685df31
MS
384 if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
385 return -EINVAL;
386
b2d87909
EP
387 group = file->private_data;
388
389 if (count > sizeof(response))
390 count = sizeof(response);
391
392 pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
393
394 if (copy_from_user(&response, buf, count))
395 return -EFAULT;
396
397 ret = process_access_response(group, &response);
398 if (ret < 0)
399 count = ret;
400
401 return count;
b2d87909
EP
402}
403
52c923dd
EP
404static int fanotify_release(struct inode *ignored, struct file *file)
405{
406 struct fsnotify_group *group = file->private_data;
ca6f8699 407 struct fanotify_perm_event *event;
96d41019 408 struct fsnotify_event *fsn_event;
19ba54f4 409
5838d444 410 /*
96d41019
JK
411 * Stop new events from arriving in the notification queue. since
412 * userspace cannot use fanotify fd anymore, no event can enter or
413 * leave access_list by now either.
5838d444 414 */
96d41019 415 fsnotify_group_stop_queueing(group);
2eebf582 416
96d41019
JK
417 /*
418 * Process all permission events on access_list and notification queue
419 * and simulate reply from userspace.
420 */
073f6552 421 spin_lock(&group->notification_lock);
ca6f8699
JK
422 while (!list_empty(&group->fanotify_data.access_list)) {
423 event = list_first_entry(&group->fanotify_data.access_list,
424 struct fanotify_perm_event, fae.fse.list);
f083441b
JK
425 list_del_init(&event->fae.fse.list);
426 event->response = FAN_ALLOW;
2eebf582 427 }
2eebf582 428
5838d444 429 /*
96d41019
JK
430 * Destroy all non-permission events. For permission events just
431 * dequeue them and set the response. They will be freed once the
432 * response is consumed and fanotify_get_response() returns.
5838d444 433 */
96d41019
JK
434 while (!fsnotify_notify_queue_is_empty(group)) {
435 fsn_event = fsnotify_remove_first_event(group);
a0a92d26 436 if (!(FANOTIFY_E(fsn_event)->mask & FANOTIFY_PERM_EVENTS)) {
c21dbe20 437 spin_unlock(&group->notification_lock);
96d41019 438 fsnotify_destroy_event(group, fsn_event);
c21dbe20 439 spin_lock(&group->notification_lock);
6685df31 440 } else {
96d41019 441 FANOTIFY_PE(fsn_event)->response = FAN_ALLOW;
6685df31 442 }
96d41019 443 }
c21dbe20 444 spin_unlock(&group->notification_lock);
96d41019
JK
445
446 /* Response for all permission events it set, wakeup waiters */
2eebf582 447 wake_up(&group->fanotify_data.access_waitq);
0a6b6bd5 448
52c923dd 449 /* matches the fanotify_init->fsnotify_alloc_group */
d8153d4d 450 fsnotify_destroy_group(group);
52c923dd
EP
451
452 return 0;
453}
454
a1014f10
EP
455static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
456{
457 struct fsnotify_group *group;
7053aee2 458 struct fsnotify_event *fsn_event;
a1014f10
EP
459 void __user *p;
460 int ret = -ENOTTY;
461 size_t send_len = 0;
462
463 group = file->private_data;
464
465 p = (void __user *) arg;
466
467 switch (cmd) {
468 case FIONREAD:
c21dbe20 469 spin_lock(&group->notification_lock);
7053aee2 470 list_for_each_entry(fsn_event, &group->notification_list, list)
a1014f10 471 send_len += FAN_EVENT_METADATA_LEN;
c21dbe20 472 spin_unlock(&group->notification_lock);
a1014f10
EP
473 ret = put_user(send_len, (int __user *) p);
474 break;
475 }
476
477 return ret;
478}
479
52c923dd 480static const struct file_operations fanotify_fops = {
be77196b 481 .show_fdinfo = fanotify_show_fdinfo,
a1014f10
EP
482 .poll = fanotify_poll,
483 .read = fanotify_read,
b2d87909 484 .write = fanotify_write,
52c923dd
EP
485 .fasync = NULL,
486 .release = fanotify_release,
a1014f10
EP
487 .unlocked_ioctl = fanotify_ioctl,
488 .compat_ioctl = fanotify_ioctl,
6038f373 489 .llseek = noop_llseek,
52c923dd
EP
490};
491
2a3edf86
EP
492static int fanotify_find_path(int dfd, const char __user *filename,
493 struct path *path, unsigned int flags)
494{
495 int ret;
496
497 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
498 dfd, filename, flags);
499
500 if (filename == NULL) {
2903ff01 501 struct fd f = fdget(dfd);
2a3edf86
EP
502
503 ret = -EBADF;
2903ff01 504 if (!f.file)
2a3edf86
EP
505 goto out;
506
507 ret = -ENOTDIR;
508 if ((flags & FAN_MARK_ONLYDIR) &&
496ad9aa 509 !(S_ISDIR(file_inode(f.file)->i_mode))) {
2903ff01 510 fdput(f);
2a3edf86
EP
511 goto out;
512 }
513
2903ff01 514 *path = f.file->f_path;
2a3edf86 515 path_get(path);
2903ff01 516 fdput(f);
2a3edf86
EP
517 } else {
518 unsigned int lookup_flags = 0;
519
520 if (!(flags & FAN_MARK_DONT_FOLLOW))
521 lookup_flags |= LOOKUP_FOLLOW;
522 if (flags & FAN_MARK_ONLYDIR)
523 lookup_flags |= LOOKUP_DIRECTORY;
524
525 ret = user_path_at(dfd, filename, lookup_flags, path);
526 if (ret)
527 goto out;
528 }
529
530 /* you can only watch an inode if you have read permissions on it */
531 ret = inode_permission(path->dentry->d_inode, MAY_READ);
532 if (ret)
533 path_put(path);
534out:
535 return ret;
536}
537
b9e4e3bd
EP
538static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
539 __u32 mask,
6dfbd149
LS
540 unsigned int flags,
541 int *destroy)
088b09b0 542{
d2c1874c 543 __u32 oldmask = 0;
088b09b0
AG
544
545 spin_lock(&fsn_mark->lock);
b9e4e3bd
EP
546 if (!(flags & FAN_MARK_IGNORED_MASK)) {
547 oldmask = fsn_mark->mask;
a72fd224 548 fsn_mark->mask &= ~mask;
b9e4e3bd 549 } else {
a72fd224 550 fsn_mark->ignored_mask &= ~mask;
b9e4e3bd 551 }
a118449a 552 *destroy = !(fsn_mark->mask | fsn_mark->ignored_mask);
088b09b0
AG
553 spin_unlock(&fsn_mark->lock);
554
088b09b0
AG
555 return mask & oldmask;
556}
557
eaa2c6b0
AG
558static int fanotify_remove_mark(struct fsnotify_group *group,
559 fsnotify_connp_t *connp, __u32 mask,
560 unsigned int flags)
88826276
EP
561{
562 struct fsnotify_mark *fsn_mark = NULL;
088b09b0 563 __u32 removed;
6dfbd149 564 int destroy_mark;
88826276 565
7b18527c 566 mutex_lock(&group->mark_mutex);
eaa2c6b0 567 fsn_mark = fsnotify_find_mark(connp, group);
7b18527c
LS
568 if (!fsn_mark) {
569 mutex_unlock(&group->mark_mutex);
f3640192 570 return -ENOENT;
7b18527c 571 }
88826276 572
6dfbd149
LS
573 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
574 &destroy_mark);
3ac70bfc
AG
575 if (removed & fsnotify_conn_mask(fsn_mark->connector))
576 fsnotify_recalc_mask(fsn_mark->connector);
6dfbd149 577 if (destroy_mark)
4712e722 578 fsnotify_detach_mark(fsn_mark);
7b18527c 579 mutex_unlock(&group->mark_mutex);
4712e722
JK
580 if (destroy_mark)
581 fsnotify_free_mark(fsn_mark);
6dfbd149 582
eaa2c6b0 583 /* matches the fsnotify_find_mark() */
f3640192 584 fsnotify_put_mark(fsn_mark);
f3640192
AG
585 return 0;
586}
2a3edf86 587
eaa2c6b0
AG
588static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
589 struct vfsmount *mnt, __u32 mask,
590 unsigned int flags)
591{
592 return fanotify_remove_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
593 mask, flags);
594}
595
d54f4fba
AG
596static int fanotify_remove_sb_mark(struct fsnotify_group *group,
597 struct super_block *sb, __u32 mask,
598 unsigned int flags)
599{
600 return fanotify_remove_mark(group, &sb->s_fsnotify_marks, mask, flags);
601}
602
f3640192 603static int fanotify_remove_inode_mark(struct fsnotify_group *group,
b9e4e3bd
EP
604 struct inode *inode, __u32 mask,
605 unsigned int flags)
f3640192 606{
eaa2c6b0
AG
607 return fanotify_remove_mark(group, &inode->i_fsnotify_marks, mask,
608 flags);
2a3edf86
EP
609}
610
b9e4e3bd
EP
611static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
612 __u32 mask,
613 unsigned int flags)
912ee394 614{
192ca4d1 615 __u32 oldmask = -1;
912ee394
AG
616
617 spin_lock(&fsn_mark->lock);
b9e4e3bd
EP
618 if (!(flags & FAN_MARK_IGNORED_MASK)) {
619 oldmask = fsn_mark->mask;
a72fd224 620 fsn_mark->mask |= mask;
b9e4e3bd 621 } else {
a72fd224 622 fsn_mark->ignored_mask |= mask;
c9778a98
EP
623 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
624 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
b9e4e3bd 625 }
912ee394
AG
626 spin_unlock(&fsn_mark->lock);
627
628 return mask & ~oldmask;
629}
630
5e9c070c 631static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
b812a9f5 632 fsnotify_connp_t *connp,
77115225
AG
633 unsigned int type,
634 __kernel_fsid_t *fsid)
5e9c070c
LS
635{
636 struct fsnotify_mark *mark;
637 int ret;
638
639 if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
640 return ERR_PTR(-ENOSPC);
641
642 mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
643 if (!mark)
644 return ERR_PTR(-ENOMEM);
645
054c636e 646 fsnotify_init_mark(mark, group);
77115225 647 ret = fsnotify_add_mark_locked(mark, connp, type, 0, fsid);
5e9c070c
LS
648 if (ret) {
649 fsnotify_put_mark(mark);
650 return ERR_PTR(ret);
651 }
652
653 return mark;
654}
655
656
eaa2c6b0
AG
657static int fanotify_add_mark(struct fsnotify_group *group,
658 fsnotify_connp_t *connp, unsigned int type,
77115225
AG
659 __u32 mask, unsigned int flags,
660 __kernel_fsid_t *fsid)
2a3edf86
EP
661{
662 struct fsnotify_mark *fsn_mark;
912ee394 663 __u32 added;
2a3edf86 664
7b18527c 665 mutex_lock(&group->mark_mutex);
b812a9f5 666 fsn_mark = fsnotify_find_mark(connp, group);
88826276 667 if (!fsn_mark) {
77115225 668 fsn_mark = fanotify_add_new_mark(group, connp, type, fsid);
5e9c070c 669 if (IS_ERR(fsn_mark)) {
7b18527c 670 mutex_unlock(&group->mark_mutex);
5e9c070c 671 return PTR_ERR(fsn_mark);
7b18527c 672 }
88826276 673 }
b9e4e3bd 674 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
3ac70bfc
AG
675 if (added & ~fsnotify_conn_mask(fsn_mark->connector))
676 fsnotify_recalc_mask(fsn_mark->connector);
c9747640 677 mutex_unlock(&group->mark_mutex);
5e9c070c 678
fa218ab9 679 fsnotify_put_mark(fsn_mark);
5e9c070c 680 return 0;
88826276
EP
681}
682
eaa2c6b0
AG
683static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
684 struct vfsmount *mnt, __u32 mask,
77115225 685 unsigned int flags, __kernel_fsid_t *fsid)
eaa2c6b0
AG
686{
687 return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
77115225 688 FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags, fsid);
eaa2c6b0
AG
689}
690
d54f4fba 691static int fanotify_add_sb_mark(struct fsnotify_group *group,
77115225
AG
692 struct super_block *sb, __u32 mask,
693 unsigned int flags, __kernel_fsid_t *fsid)
d54f4fba
AG
694{
695 return fanotify_add_mark(group, &sb->s_fsnotify_marks,
77115225 696 FSNOTIFY_OBJ_TYPE_SB, mask, flags, fsid);
d54f4fba
AG
697}
698
52202dfb 699static int fanotify_add_inode_mark(struct fsnotify_group *group,
b9e4e3bd 700 struct inode *inode, __u32 mask,
77115225 701 unsigned int flags, __kernel_fsid_t *fsid)
88826276 702{
88826276 703 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
2a3edf86 704
5322a59f
EP
705 /*
706 * If some other task has this inode open for write we should not add
707 * an ignored mark, unless that ignored mark is supposed to survive
708 * modification changes anyway.
709 */
710 if ((flags & FAN_MARK_IGNORED_MASK) &&
711 !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
ac9498d6 712 inode_is_open_for_write(inode))
5322a59f
EP
713 return 0;
714
eaa2c6b0 715 return fanotify_add_mark(group, &inode->i_fsnotify_marks,
77115225 716 FSNOTIFY_OBJ_TYPE_INODE, mask, flags, fsid);
88826276 717}
2a3edf86 718
52c923dd 719/* fanotify syscalls */
08ae8938 720SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
11637e4b 721{
52c923dd
EP
722 struct fsnotify_group *group;
723 int f_flags, fd;
4afeff85 724 struct user_struct *user;
33913997 725 struct fanotify_event *oevent;
52c923dd 726
96a71f21
AG
727 pr_debug("%s: flags=%x event_f_flags=%x\n",
728 __func__, flags, event_f_flags);
52c923dd 729
52c923dd 730 if (!capable(CAP_SYS_ADMIN))
a2f13ad0 731 return -EPERM;
52c923dd 732
de8cd83e 733#ifdef CONFIG_AUDITSYSCALL
23c9deeb 734 if (flags & ~(FANOTIFY_INIT_FLAGS | FAN_ENABLE_AUDIT))
de8cd83e 735#else
23c9deeb 736 if (flags & ~FANOTIFY_INIT_FLAGS)
de8cd83e 737#endif
52c923dd
EP
738 return -EINVAL;
739
48149e9d
HS
740 if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS)
741 return -EINVAL;
742
743 switch (event_f_flags & O_ACCMODE) {
744 case O_RDONLY:
745 case O_RDWR:
746 case O_WRONLY:
747 break;
748 default:
749 return -EINVAL;
750 }
751
a8b13aa2
AG
752 if ((flags & FAN_REPORT_FID) &&
753 (flags & FANOTIFY_CLASS_BITS) != FAN_CLASS_NOTIF)
754 return -EINVAL;
755
4afeff85
EP
756 user = get_current_user();
757 if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {
758 free_uid(user);
759 return -EMFILE;
760 }
761
b2d87909 762 f_flags = O_RDWR | FMODE_NONOTIFY;
52c923dd
EP
763 if (flags & FAN_CLOEXEC)
764 f_flags |= O_CLOEXEC;
765 if (flags & FAN_NONBLOCK)
766 f_flags |= O_NONBLOCK;
767
768 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
769 group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
26379198
EP
770 if (IS_ERR(group)) {
771 free_uid(user);
52c923dd 772 return PTR_ERR(group);
26379198 773 }
52c923dd 774
4afeff85 775 group->fanotify_data.user = user;
96a71f21 776 group->fanotify_data.flags = flags;
4afeff85 777 atomic_inc(&user->fanotify_listeners);
d46eb14b 778 group->memcg = get_mem_cgroup_from_mm(current->mm);
4afeff85 779
83b535d2
AG
780 oevent = fanotify_alloc_event(group, NULL, FS_Q_OVERFLOW, NULL,
781 FSNOTIFY_EVENT_NONE, NULL);
ff57cd58
JK
782 if (unlikely(!oevent)) {
783 fd = -ENOMEM;
784 goto out_destroy_group;
785 }
786 group->overflow_event = &oevent->fse;
ff57cd58 787
1e2ee49f
WW
788 if (force_o_largefile())
789 event_f_flags |= O_LARGEFILE;
80af2588 790 group->fanotify_data.f_flags = event_f_flags;
9e66e423
EP
791 init_waitqueue_head(&group->fanotify_data.access_waitq);
792 INIT_LIST_HEAD(&group->fanotify_data.access_list);
23c9deeb 793 switch (flags & FANOTIFY_CLASS_BITS) {
4231a235
EP
794 case FAN_CLASS_NOTIF:
795 group->priority = FS_PRIO_0;
796 break;
797 case FAN_CLASS_CONTENT:
798 group->priority = FS_PRIO_1;
799 break;
800 case FAN_CLASS_PRE_CONTENT:
801 group->priority = FS_PRIO_2;
802 break;
803 default:
804 fd = -EINVAL;
d8153d4d 805 goto out_destroy_group;
4231a235 806 }
cb2d429f 807
5dd03f55
EP
808 if (flags & FAN_UNLIMITED_QUEUE) {
809 fd = -EPERM;
810 if (!capable(CAP_SYS_ADMIN))
d8153d4d 811 goto out_destroy_group;
5dd03f55
EP
812 group->max_events = UINT_MAX;
813 } else {
814 group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
815 }
2529a0df 816
ac7e22dc
EP
817 if (flags & FAN_UNLIMITED_MARKS) {
818 fd = -EPERM;
819 if (!capable(CAP_SYS_ADMIN))
d8153d4d 820 goto out_destroy_group;
ac7e22dc
EP
821 group->fanotify_data.max_marks = UINT_MAX;
822 } else {
823 group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS;
824 }
e7099d8a 825
de8cd83e
SG
826 if (flags & FAN_ENABLE_AUDIT) {
827 fd = -EPERM;
828 if (!capable(CAP_AUDIT_WRITE))
829 goto out_destroy_group;
de8cd83e
SG
830 }
831
52c923dd
EP
832 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
833 if (fd < 0)
d8153d4d 834 goto out_destroy_group;
52c923dd
EP
835
836 return fd;
837
d8153d4d
LS
838out_destroy_group:
839 fsnotify_destroy_group(group);
52c923dd 840 return fd;
11637e4b 841}
bbaa4168 842
a8b13aa2 843/* Check if filesystem can encode a unique fid */
73072283 844static int fanotify_test_fid(struct path *path, __kernel_fsid_t *fsid)
a8b13aa2 845{
73072283 846 __kernel_fsid_t root_fsid;
a8b13aa2
AG
847 int err;
848
849 /*
850 * Make sure path is not in filesystem with zero fsid (e.g. tmpfs).
851 */
73072283 852 err = vfs_get_fsid(path->dentry, fsid);
a8b13aa2
AG
853 if (err)
854 return err;
855
73072283 856 if (!fsid->val[0] && !fsid->val[1])
a8b13aa2
AG
857 return -ENODEV;
858
859 /*
860 * Make sure path is not inside a filesystem subvolume (e.g. btrfs)
861 * which uses a different fsid than sb root.
862 */
73072283 863 err = vfs_get_fsid(path->dentry->d_sb->s_root, &root_fsid);
a8b13aa2
AG
864 if (err)
865 return err;
866
73072283
AG
867 if (root_fsid.val[0] != fsid->val[0] ||
868 root_fsid.val[1] != fsid->val[1])
a8b13aa2
AG
869 return -EXDEV;
870
871 /*
872 * We need to make sure that the file system supports at least
873 * encoding a file handle so user can use name_to_handle_at() to
874 * compare fid returned with event to the file handle of watched
875 * objects. However, name_to_handle_at() requires that the
876 * filesystem also supports decoding file handles.
877 */
878 if (!path->dentry->d_sb->s_export_op ||
879 !path->dentry->d_sb->s_export_op->fh_to_dentry)
880 return -EOPNOTSUPP;
881
882 return 0;
883}
884
183caa3c
DB
885static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
886 int dfd, const char __user *pathname)
bbaa4168 887{
0ff21db9
EP
888 struct inode *inode = NULL;
889 struct vfsmount *mnt = NULL;
2a3edf86 890 struct fsnotify_group *group;
2903ff01 891 struct fd f;
2a3edf86 892 struct path path;
73072283 893 __kernel_fsid_t __fsid, *fsid = NULL;
bdd5a46f 894 u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS;
23c9deeb 895 unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS;
2903ff01 896 int ret;
2a3edf86
EP
897
898 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
899 __func__, fanotify_fd, flags, dfd, pathname, mask);
900
901 /* we only use the lower 32 bits as of right now. */
902 if (mask & ((__u64)0xffffffff << 32))
903 return -EINVAL;
904
23c9deeb 905 if (flags & ~FANOTIFY_MARK_FLAGS)
88380fe6 906 return -EINVAL;
d54f4fba
AG
907
908 switch (mark_type) {
909 case FAN_MARK_INODE:
910 case FAN_MARK_MOUNT:
911 case FAN_MARK_FILESYSTEM:
912 break;
913 default:
914 return -EINVAL;
915 }
916
4d92604c 917 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
1734dee4 918 case FAN_MARK_ADD: /* fallthrough */
88380fe6 919 case FAN_MARK_REMOVE:
1734dee4
LS
920 if (!mask)
921 return -EINVAL;
cc299a98 922 break;
4d92604c 923 case FAN_MARK_FLUSH:
23c9deeb 924 if (flags & ~(FANOTIFY_MARK_TYPE_BITS | FAN_MARK_FLUSH))
cc299a98 925 return -EINVAL;
88380fe6
AG
926 break;
927 default:
928 return -EINVAL;
929 }
8fcd6528 930
6685df31 931 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
23c9deeb 932 valid_mask |= FANOTIFY_PERM_EVENTS;
6685df31
MS
933
934 if (mask & ~valid_mask)
2a3edf86
EP
935 return -EINVAL;
936
2903ff01
AV
937 f = fdget(fanotify_fd);
938 if (unlikely(!f.file))
2a3edf86
EP
939 return -EBADF;
940
941 /* verify that this is indeed an fanotify instance */
942 ret = -EINVAL;
2903ff01 943 if (unlikely(f.file->f_op != &fanotify_fops))
2a3edf86 944 goto fput_and_out;
2903ff01 945 group = f.file->private_data;
4231a235
EP
946
947 /*
948 * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
949 * allowed to set permissions events.
950 */
951 ret = -EINVAL;
23c9deeb 952 if (mask & FANOTIFY_PERM_EVENTS &&
4231a235
EP
953 group->priority == FS_PRIO_0)
954 goto fput_and_out;
2a3edf86 955
235328d1
AG
956 /*
957 * Events with data type inode do not carry enough information to report
958 * event->fd, so we do not allow setting a mask for inode events unless
959 * group supports reporting fid.
960 * inode events are not supported on a mount mark, because they do not
961 * carry enough information (i.e. path) to be filtered by mount point.
962 */
963 if (mask & FANOTIFY_INODE_EVENTS &&
964 (!FAN_GROUP_FLAG(group, FAN_REPORT_FID) ||
965 mark_type == FAN_MARK_MOUNT))
966 goto fput_and_out;
967
0a8dd2db
HS
968 if (flags & FAN_MARK_FLUSH) {
969 ret = 0;
d54f4fba 970 if (mark_type == FAN_MARK_MOUNT)
0a8dd2db 971 fsnotify_clear_vfsmount_marks_by_group(group);
d54f4fba
AG
972 else if (mark_type == FAN_MARK_FILESYSTEM)
973 fsnotify_clear_sb_marks_by_group(group);
0a8dd2db
HS
974 else
975 fsnotify_clear_inode_marks_by_group(group);
976 goto fput_and_out;
977 }
978
2a3edf86
EP
979 ret = fanotify_find_path(dfd, pathname, &path, flags);
980 if (ret)
981 goto fput_and_out;
982
a8b13aa2 983 if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) {
73072283 984 ret = fanotify_test_fid(&path, &__fsid);
a8b13aa2
AG
985 if (ret)
986 goto path_put_and_out;
77115225 987
73072283 988 fsid = &__fsid;
a8b13aa2
AG
989 }
990
2a3edf86 991 /* inode held in place by reference to path; group by fget on fd */
d54f4fba 992 if (mark_type == FAN_MARK_INODE)
0ff21db9
EP
993 inode = path.dentry->d_inode;
994 else
995 mnt = path.mnt;
2a3edf86
EP
996
997 /* create/update an inode mark */
0a8dd2db 998 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
c6223f46 999 case FAN_MARK_ADD:
d54f4fba 1000 if (mark_type == FAN_MARK_MOUNT)
77115225
AG
1001 ret = fanotify_add_vfsmount_mark(group, mnt, mask,
1002 flags, fsid);
d54f4fba 1003 else if (mark_type == FAN_MARK_FILESYSTEM)
77115225
AG
1004 ret = fanotify_add_sb_mark(group, mnt->mnt_sb, mask,
1005 flags, fsid);
0ff21db9 1006 else
77115225
AG
1007 ret = fanotify_add_inode_mark(group, inode, mask,
1008 flags, fsid);
c6223f46
AG
1009 break;
1010 case FAN_MARK_REMOVE:
d54f4fba 1011 if (mark_type == FAN_MARK_MOUNT)
77115225
AG
1012 ret = fanotify_remove_vfsmount_mark(group, mnt, mask,
1013 flags);
d54f4fba 1014 else if (mark_type == FAN_MARK_FILESYSTEM)
77115225
AG
1015 ret = fanotify_remove_sb_mark(group, mnt->mnt_sb, mask,
1016 flags);
f3640192 1017 else
77115225
AG
1018 ret = fanotify_remove_inode_mark(group, inode, mask,
1019 flags);
c6223f46
AG
1020 break;
1021 default:
1022 ret = -EINVAL;
1023 }
2a3edf86 1024
a8b13aa2 1025path_put_and_out:
2a3edf86
EP
1026 path_put(&path);
1027fput_and_out:
2903ff01 1028 fdput(f);
2a3edf86
EP
1029 return ret;
1030}
1031
183caa3c
DB
1032SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
1033 __u64, mask, int, dfd,
1034 const char __user *, pathname)
1035{
1036 return do_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname);
1037}
1038
91c2e0bc
AV
1039#ifdef CONFIG_COMPAT
1040COMPAT_SYSCALL_DEFINE6(fanotify_mark,
1041 int, fanotify_fd, unsigned int, flags,
1042 __u32, mask0, __u32, mask1, int, dfd,
1043 const char __user *, pathname)
1044{
183caa3c 1045 return do_fanotify_mark(fanotify_fd, flags,
91c2e0bc 1046#ifdef __BIG_ENDIAN
91c2e0bc 1047 ((__u64)mask0 << 32) | mask1,
592f6b84
HC
1048#else
1049 ((__u64)mask1 << 32) | mask0,
91c2e0bc
AV
1050#endif
1051 dfd, pathname);
1052}
1053#endif
1054
2a3edf86 1055/*
ae0e47f0 1056 * fanotify_user_setup - Our initialization function. Note that we cannot return
2a3edf86
EP
1057 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
1058 * must result in panic().
1059 */
1060static int __init fanotify_user_setup(void)
1061{
a8b13aa2 1062 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 8);
bdd5a46f
AG
1063 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9);
1064
d46eb14b
SB
1065 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark,
1066 SLAB_PANIC|SLAB_ACCOUNT);
33913997 1067 fanotify_event_cachep = KMEM_CACHE(fanotify_event, SLAB_PANIC);
6685df31
MS
1068 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
1069 fanotify_perm_event_cachep =
33913997 1070 KMEM_CACHE(fanotify_perm_event, SLAB_PANIC);
6685df31 1071 }
2a3edf86
EP
1072
1073 return 0;
bbaa4168 1074}
2a3edf86 1075device_initcall(fanotify_user_setup);