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