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