1 #include <linux/fanotify.h>
2 #include <linux/fcntl.h>
3 #include <linux/file.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>
18 #include <asm/ioctls.h>
20 #include "../../mount.h"
21 #include "../fdinfo.h"
23 #define FANOTIFY_DEFAULT_MAX_EVENTS 16384
24 #define FANOTIFY_DEFAULT_MAX_MARKS 8192
25 #define FANOTIFY_DEFAULT_MAX_LISTENERS 128
27 extern const struct fsnotify_ops fanotify_fsnotify_ops;
29 static struct kmem_cache *fanotify_mark_cache __read_mostly;
30 static struct kmem_cache *fanotify_response_event_cache __read_mostly;
32 struct fanotify_response_event {
33 struct list_head list;
35 struct fsnotify_event *event;
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.
43 * Called with the group->notification_mutex held.
45 static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
48 BUG_ON(!mutex_is_locked(&group->notification_mutex));
50 pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
52 if (fsnotify_notify_queue_is_empty(group))
55 if (FAN_EVENT_METADATA_LEN > count)
56 return ERR_PTR(-EINVAL);
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);
63 static int create_fd(struct fsnotify_group *group,
64 struct fsnotify_event *event,
68 struct file *new_file;
70 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
72 client_fd = get_unused_fd();
76 if (event->data_type != FSNOTIFY_EVENT_PATH) {
78 put_unused_fd(client_fd);
83 * we need a new file handle for the userspace program so it can read even if it was
84 * originally opened O_WRONLY.
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,
93 new_file = ERR_PTR(-EOVERFLOW);
94 if (IS_ERR(new_file)) {
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
102 put_unused_fd(client_fd);
103 client_fd = PTR_ERR(new_file);
111 static int fill_event_metadata(struct fsnotify_group *group,
112 struct fanotify_event_metadata *metadata,
113 struct fsnotify_event *event,
118 pr_debug("%s: group=%p metadata=%p event=%p\n", __func__,
119 group, metadata, event);
122 metadata->event_len = FAN_EVENT_METADATA_LEN;
123 metadata->metadata_len = FAN_EVENT_METADATA_LEN;
124 metadata->vers = FANOTIFY_METADATA_VERSION;
125 metadata->mask = event->mask & FAN_ALL_OUTGOING_EVENTS;
126 metadata->pid = pid_vnr(event->tgid);
127 if (unlikely(event->mask & FAN_Q_OVERFLOW))
128 metadata->fd = FAN_NOFD;
130 metadata->fd = create_fd(group, event, file);
131 if (metadata->fd < 0)
138 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
139 static struct fanotify_response_event *dequeue_re(struct fsnotify_group *group,
142 struct fanotify_response_event *re, *return_re = NULL;
144 mutex_lock(&group->fanotify_data.access_mutex);
145 list_for_each_entry(re, &group->fanotify_data.access_list, list) {
149 list_del_init(&re->list);
153 mutex_unlock(&group->fanotify_data.access_mutex);
155 pr_debug("%s: found return_re=%p\n", __func__, return_re);
160 static int process_access_response(struct fsnotify_group *group,
161 struct fanotify_response *response_struct)
163 struct fanotify_response_event *re;
164 __s32 fd = response_struct->fd;
165 __u32 response = response_struct->response;
167 pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
170 * make sure the response is valid, if invalid we do nothing and either
171 * userspace can send a valid response or we will clean it up after the
185 re = dequeue_re(group, fd);
189 re->event->response = response;
191 wake_up(&group->fanotify_data.access_waitq);
193 kmem_cache_free(fanotify_response_event_cache, re);
198 static int prepare_for_access_response(struct fsnotify_group *group,
199 struct fsnotify_event *event,
202 struct fanotify_response_event *re;
204 if (!(event->mask & FAN_ALL_PERM_EVENTS))
207 re = kmem_cache_alloc(fanotify_response_event_cache, GFP_KERNEL);
214 mutex_lock(&group->fanotify_data.access_mutex);
216 if (atomic_read(&group->fanotify_data.bypass_perm)) {
217 mutex_unlock(&group->fanotify_data.access_mutex);
218 kmem_cache_free(fanotify_response_event_cache, re);
219 event->response = FAN_ALLOW;
223 list_add_tail(&re->list, &group->fanotify_data.access_list);
224 mutex_unlock(&group->fanotify_data.access_mutex);
230 static int prepare_for_access_response(struct fsnotify_group *group,
231 struct fsnotify_event *event,
239 static ssize_t copy_event_to_user(struct fsnotify_group *group,
240 struct fsnotify_event *event,
243 struct fanotify_event_metadata fanotify_event_metadata;
247 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
249 ret = fill_event_metadata(group, &fanotify_event_metadata, event, &f);
253 fd = fanotify_event_metadata.fd;
255 if (copy_to_user(buf, &fanotify_event_metadata,
256 fanotify_event_metadata.event_len))
259 ret = prepare_for_access_response(group, event, fd);
265 return fanotify_event_metadata.event_len;
268 if (fd != FAN_NOFD) {
273 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
274 if (event->mask & FAN_ALL_PERM_EVENTS) {
275 event->response = FAN_DENY;
276 wake_up(&group->fanotify_data.access_waitq);
282 /* intofiy userspace file descriptor functions */
283 static unsigned int fanotify_poll(struct file *file, poll_table *wait)
285 struct fsnotify_group *group = file->private_data;
288 poll_wait(file, &group->notification_waitq, wait);
289 mutex_lock(&group->notification_mutex);
290 if (!fsnotify_notify_queue_is_empty(group))
291 ret = POLLIN | POLLRDNORM;
292 mutex_unlock(&group->notification_mutex);
297 static ssize_t fanotify_read(struct file *file, char __user *buf,
298 size_t count, loff_t *pos)
300 struct fsnotify_group *group;
301 struct fsnotify_event *kevent;
307 group = file->private_data;
309 pr_debug("%s: group=%p\n", __func__, group);
312 prepare_to_wait(&group->notification_waitq, &wait, TASK_INTERRUPTIBLE);
314 mutex_lock(&group->notification_mutex);
315 kevent = get_one_event(group, count);
316 mutex_unlock(&group->notification_mutex);
319 ret = PTR_ERR(kevent);
322 ret = copy_event_to_user(group, kevent, buf);
323 fsnotify_put_event(kevent);
332 if (file->f_flags & O_NONBLOCK)
335 if (signal_pending(current))
344 finish_wait(&group->notification_waitq, &wait);
345 if (start != buf && ret != -EFAULT)
350 static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
352 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
353 struct fanotify_response response = { .fd = -1, .response = -1 };
354 struct fsnotify_group *group;
357 group = file->private_data;
359 if (count > sizeof(response))
360 count = sizeof(response);
362 pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
364 if (copy_from_user(&response, buf, count))
367 ret = process_access_response(group, &response);
377 static int fanotify_release(struct inode *ignored, struct file *file)
379 struct fsnotify_group *group = file->private_data;
381 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
382 struct fanotify_response_event *re, *lre;
384 mutex_lock(&group->fanotify_data.access_mutex);
386 atomic_inc(&group->fanotify_data.bypass_perm);
388 list_for_each_entry_safe(re, lre, &group->fanotify_data.access_list, list) {
389 pr_debug("%s: found group=%p re=%p event=%p\n", __func__, group,
392 list_del_init(&re->list);
393 re->event->response = FAN_ALLOW;
395 kmem_cache_free(fanotify_response_event_cache, re);
397 mutex_unlock(&group->fanotify_data.access_mutex);
399 wake_up(&group->fanotify_data.access_waitq);
402 if (file->f_flags & FASYNC)
403 fsnotify_fasync(-1, file, 0);
405 /* matches the fanotify_init->fsnotify_alloc_group */
406 fsnotify_destroy_group(group);
411 static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
413 struct fsnotify_group *group;
414 struct fsnotify_event_holder *holder;
419 group = file->private_data;
421 p = (void __user *) arg;
425 mutex_lock(&group->notification_mutex);
426 list_for_each_entry(holder, &group->notification_list, event_list)
427 send_len += FAN_EVENT_METADATA_LEN;
428 mutex_unlock(&group->notification_mutex);
429 ret = put_user(send_len, (int __user *) p);
436 static const struct file_operations fanotify_fops = {
437 .show_fdinfo = fanotify_show_fdinfo,
438 .poll = fanotify_poll,
439 .read = fanotify_read,
440 .write = fanotify_write,
442 .release = fanotify_release,
443 .unlocked_ioctl = fanotify_ioctl,
444 .compat_ioctl = fanotify_ioctl,
445 .llseek = noop_llseek,
448 static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
450 kmem_cache_free(fanotify_mark_cache, fsn_mark);
453 static int fanotify_find_path(int dfd, const char __user *filename,
454 struct path *path, unsigned int flags)
458 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
459 dfd, filename, flags);
461 if (filename == NULL) {
462 struct fd f = fdget(dfd);
469 if ((flags & FAN_MARK_ONLYDIR) &&
470 !(S_ISDIR(file_inode(f.file)->i_mode))) {
475 *path = f.file->f_path;
479 unsigned int lookup_flags = 0;
481 if (!(flags & FAN_MARK_DONT_FOLLOW))
482 lookup_flags |= LOOKUP_FOLLOW;
483 if (flags & FAN_MARK_ONLYDIR)
484 lookup_flags |= LOOKUP_DIRECTORY;
486 ret = user_path_at(dfd, filename, lookup_flags, path);
491 /* you can only watch an inode if you have read permissions on it */
492 ret = inode_permission(path->dentry->d_inode, MAY_READ);
499 static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
506 spin_lock(&fsn_mark->lock);
507 if (!(flags & FAN_MARK_IGNORED_MASK)) {
508 oldmask = fsn_mark->mask;
509 fsnotify_set_mark_mask_locked(fsn_mark, (oldmask & ~mask));
511 oldmask = fsn_mark->ignored_mask;
512 fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask & ~mask));
514 spin_unlock(&fsn_mark->lock);
516 *destroy = !(oldmask & ~mask);
518 return mask & oldmask;
521 static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
522 struct vfsmount *mnt, __u32 mask,
525 struct fsnotify_mark *fsn_mark = NULL;
529 fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
533 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
536 fsnotify_destroy_mark(fsn_mark, group);
538 fsnotify_put_mark(fsn_mark);
539 if (removed & real_mount(mnt)->mnt_fsnotify_mask)
540 fsnotify_recalc_vfsmount_mask(mnt);
545 static int fanotify_remove_inode_mark(struct fsnotify_group *group,
546 struct inode *inode, __u32 mask,
549 struct fsnotify_mark *fsn_mark = NULL;
553 fsn_mark = fsnotify_find_inode_mark(group, inode);
557 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
560 fsnotify_destroy_mark(fsn_mark, group);
561 /* matches the fsnotify_find_inode_mark() */
562 fsnotify_put_mark(fsn_mark);
563 if (removed & inode->i_fsnotify_mask)
564 fsnotify_recalc_inode_mask(inode);
569 static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
575 spin_lock(&fsn_mark->lock);
576 if (!(flags & FAN_MARK_IGNORED_MASK)) {
577 oldmask = fsn_mark->mask;
578 fsnotify_set_mark_mask_locked(fsn_mark, (oldmask | mask));
580 __u32 tmask = fsn_mark->ignored_mask | mask;
581 fsnotify_set_mark_ignored_mask_locked(fsn_mark, tmask);
582 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
583 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
586 if (!(flags & FAN_MARK_ONDIR)) {
587 __u32 tmask = fsn_mark->ignored_mask | FAN_ONDIR;
588 fsnotify_set_mark_ignored_mask_locked(fsn_mark, tmask);
591 spin_unlock(&fsn_mark->lock);
593 return mask & ~oldmask;
596 static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
597 struct vfsmount *mnt, __u32 mask,
600 struct fsnotify_mark *fsn_mark;
604 fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
606 if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
609 fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
613 fsnotify_init_mark(fsn_mark, fanotify_free_mark);
614 ret = fsnotify_add_mark(fsn_mark, group, NULL, mnt, 0);
618 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
620 if (added & ~real_mount(mnt)->mnt_fsnotify_mask)
621 fsnotify_recalc_vfsmount_mask(mnt);
623 fsnotify_put_mark(fsn_mark);
627 static int fanotify_add_inode_mark(struct fsnotify_group *group,
628 struct inode *inode, __u32 mask,
631 struct fsnotify_mark *fsn_mark;
635 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
638 * If some other task has this inode open for write we should not add
639 * an ignored mark, unless that ignored mark is supposed to survive
640 * modification changes anyway.
642 if ((flags & FAN_MARK_IGNORED_MASK) &&
643 !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
644 (atomic_read(&inode->i_writecount) > 0))
647 fsn_mark = fsnotify_find_inode_mark(group, inode);
649 if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
652 fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
656 fsnotify_init_mark(fsn_mark, fanotify_free_mark);
657 ret = fsnotify_add_mark(fsn_mark, group, inode, NULL, 0);
661 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
663 if (added & ~inode->i_fsnotify_mask)
664 fsnotify_recalc_inode_mask(inode);
666 fsnotify_put_mark(fsn_mark);
670 /* fanotify syscalls */
671 SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
673 struct fsnotify_group *group;
675 struct user_struct *user;
677 pr_debug("%s: flags=%d event_f_flags=%d\n",
678 __func__, flags, event_f_flags);
680 if (!capable(CAP_SYS_ADMIN))
683 if (flags & ~FAN_ALL_INIT_FLAGS)
686 user = get_current_user();
687 if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {
692 f_flags = O_RDWR | FMODE_NONOTIFY;
693 if (flags & FAN_CLOEXEC)
694 f_flags |= O_CLOEXEC;
695 if (flags & FAN_NONBLOCK)
696 f_flags |= O_NONBLOCK;
698 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
699 group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
702 return PTR_ERR(group);
705 group->fanotify_data.user = user;
706 atomic_inc(&user->fanotify_listeners);
708 group->fanotify_data.f_flags = event_f_flags;
709 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
710 mutex_init(&group->fanotify_data.access_mutex);
711 init_waitqueue_head(&group->fanotify_data.access_waitq);
712 INIT_LIST_HEAD(&group->fanotify_data.access_list);
713 atomic_set(&group->fanotify_data.bypass_perm, 0);
715 switch (flags & FAN_ALL_CLASS_BITS) {
716 case FAN_CLASS_NOTIF:
717 group->priority = FS_PRIO_0;
719 case FAN_CLASS_CONTENT:
720 group->priority = FS_PRIO_1;
722 case FAN_CLASS_PRE_CONTENT:
723 group->priority = FS_PRIO_2;
727 goto out_destroy_group;
730 if (flags & FAN_UNLIMITED_QUEUE) {
732 if (!capable(CAP_SYS_ADMIN))
733 goto out_destroy_group;
734 group->max_events = UINT_MAX;
736 group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
739 if (flags & FAN_UNLIMITED_MARKS) {
741 if (!capable(CAP_SYS_ADMIN))
742 goto out_destroy_group;
743 group->fanotify_data.max_marks = UINT_MAX;
745 group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS;
748 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
750 goto out_destroy_group;
755 fsnotify_destroy_group(group);
759 SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
760 __u64, mask, int, dfd,
761 const char __user *, pathname)
763 struct inode *inode = NULL;
764 struct vfsmount *mnt = NULL;
765 struct fsnotify_group *group;
770 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
771 __func__, fanotify_fd, flags, dfd, pathname, mask);
773 /* we only use the lower 32 bits as of right now. */
774 if (mask & ((__u64)0xffffffff << 32))
777 if (flags & ~FAN_ALL_MARK_FLAGS)
779 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
780 case FAN_MARK_ADD: /* fallthrough */
781 case FAN_MARK_REMOVE:
790 if (mask & FAN_ONDIR) {
791 flags |= FAN_MARK_ONDIR;
795 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
796 if (mask & ~(FAN_ALL_EVENTS | FAN_ALL_PERM_EVENTS | FAN_EVENT_ON_CHILD))
798 if (mask & ~(FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD))
802 f = fdget(fanotify_fd);
803 if (unlikely(!f.file))
806 /* verify that this is indeed an fanotify instance */
808 if (unlikely(f.file->f_op != &fanotify_fops))
810 group = f.file->private_data;
813 * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
814 * allowed to set permissions events.
817 if (mask & FAN_ALL_PERM_EVENTS &&
818 group->priority == FS_PRIO_0)
821 ret = fanotify_find_path(dfd, pathname, &path, flags);
825 /* inode held in place by reference to path; group by fget on fd */
826 if (!(flags & FAN_MARK_MOUNT))
827 inode = path.dentry->d_inode;
831 /* create/update an inode mark */
832 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
834 if (flags & FAN_MARK_MOUNT)
835 ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags);
837 ret = fanotify_add_inode_mark(group, inode, mask, flags);
839 case FAN_MARK_REMOVE:
840 if (flags & FAN_MARK_MOUNT)
841 ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags);
843 ret = fanotify_remove_inode_mark(group, inode, mask, flags);
846 if (flags & FAN_MARK_MOUNT)
847 fsnotify_clear_vfsmount_marks_by_group(group);
849 fsnotify_clear_inode_marks_by_group(group);
862 COMPAT_SYSCALL_DEFINE6(fanotify_mark,
863 int, fanotify_fd, unsigned int, flags,
864 __u32, mask0, __u32, mask1, int, dfd,
865 const char __user *, pathname)
867 return sys_fanotify_mark(fanotify_fd, flags,
869 ((__u64)mask1 << 32) | mask0,
871 ((__u64)mask0 << 32) | mask1,
878 * fanotify_user_setup - Our initialization function. Note that we cannot return
879 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
880 * must result in panic().
882 static int __init fanotify_user_setup(void)
884 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
885 fanotify_response_event_cache = KMEM_CACHE(fanotify_response_event,
890 device_initcall(fanotify_user_setup);