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