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