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