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>
17 #include <asm/ioctls.h>
19 #define FANOTIFY_DEFAULT_MAX_EVENTS 16384
21 extern const struct fsnotify_ops fanotify_fsnotify_ops;
23 static struct kmem_cache *fanotify_mark_cache __read_mostly;
24 static struct kmem_cache *fanotify_response_event_cache __read_mostly;
26 struct fanotify_response_event {
27 struct list_head list;
29 struct fsnotify_event *event;
33 * Get an fsnotify notification event if one exists and is small
34 * enough to fit in "count". Return an error pointer if the count
35 * is not large enough.
37 * Called with the group->notification_mutex held.
39 static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
42 BUG_ON(!mutex_is_locked(&group->notification_mutex));
44 pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
46 if (fsnotify_notify_queue_is_empty(group))
49 if (FAN_EVENT_METADATA_LEN > count)
50 return ERR_PTR(-EINVAL);
52 /* held the notification_mutex the whole time, so this is the
53 * same event we peeked above */
54 return fsnotify_remove_notify_event(group);
57 static int create_fd(struct fsnotify_group *group, struct fsnotify_event *event)
60 struct dentry *dentry;
62 struct file *new_file;
64 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
66 client_fd = get_unused_fd();
70 if (event->data_type != FSNOTIFY_EVENT_PATH) {
72 put_unused_fd(client_fd);
77 * we need a new file handle for the userspace program so it can read even if it was
78 * originally opened O_WRONLY.
80 dentry = dget(event->path.dentry);
81 mnt = mntget(event->path.mnt);
82 /* it's possible this event was an overflow event. in that case dentry and mnt
83 * are NULL; That's fine, just don't call dentry open */
85 new_file = dentry_open(dentry, mnt,
86 group->fanotify_data.f_flags | FMODE_NONOTIFY,
89 new_file = ERR_PTR(-EOVERFLOW);
90 if (IS_ERR(new_file)) {
92 * we still send an event even if we can't open the file. this
93 * can happen when say tasks are gone and we try to open their
94 * /proc files or we try to open a WRONLY file like in sysfs
95 * we just send the errno to userspace since there isn't much
98 put_unused_fd(client_fd);
99 client_fd = PTR_ERR(new_file);
101 fd_install(client_fd, new_file);
107 static ssize_t fill_event_metadata(struct fsnotify_group *group,
108 struct fanotify_event_metadata *metadata,
109 struct fsnotify_event *event)
111 pr_debug("%s: group=%p metadata=%p event=%p\n", __func__,
112 group, metadata, event);
114 metadata->event_len = FAN_EVENT_METADATA_LEN;
115 metadata->vers = FANOTIFY_METADATA_VERSION;
116 metadata->mask = event->mask & FAN_ALL_OUTGOING_EVENTS;
117 metadata->pid = pid_vnr(event->tgid);
118 metadata->fd = create_fd(group, event);
123 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
124 static struct fanotify_response_event *dequeue_re(struct fsnotify_group *group,
127 struct fanotify_response_event *re, *return_re = NULL;
129 mutex_lock(&group->fanotify_data.access_mutex);
130 list_for_each_entry(re, &group->fanotify_data.access_list, list) {
134 list_del_init(&re->list);
138 mutex_unlock(&group->fanotify_data.access_mutex);
140 pr_debug("%s: found return_re=%p\n", __func__, return_re);
145 static int process_access_response(struct fsnotify_group *group,
146 struct fanotify_response *response_struct)
148 struct fanotify_response_event *re;
149 __s32 fd = response_struct->fd;
150 __u32 response = response_struct->response;
152 pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
155 * make sure the response is valid, if invalid we do nothing and either
156 * userspace can send a valid responce or we will clean it up after the
170 re = dequeue_re(group, fd);
174 re->event->response = response;
176 wake_up(&group->fanotify_data.access_waitq);
178 kmem_cache_free(fanotify_response_event_cache, re);
183 static int prepare_for_access_response(struct fsnotify_group *group,
184 struct fsnotify_event *event,
187 struct fanotify_response_event *re;
189 if (!(event->mask & FAN_ALL_PERM_EVENTS))
192 re = kmem_cache_alloc(fanotify_response_event_cache, GFP_KERNEL);
199 mutex_lock(&group->fanotify_data.access_mutex);
201 if (group->fanotify_data.bypass_perm) {
202 mutex_unlock(&group->fanotify_data.access_mutex);
203 kmem_cache_free(fanotify_response_event_cache, re);
204 event->response = FAN_ALLOW;
208 list_add_tail(&re->list, &group->fanotify_data.access_list);
209 mutex_unlock(&group->fanotify_data.access_mutex);
214 static void remove_access_response(struct fsnotify_group *group,
215 struct fsnotify_event *event,
218 struct fanotify_response_event *re;
220 if (!(event->mask & FAN_ALL_PERM_EVENTS))
223 re = dequeue_re(group, fd);
227 BUG_ON(re->event != event);
229 kmem_cache_free(fanotify_response_event_cache, re);
234 static int prepare_for_access_response(struct fsnotify_group *group,
235 struct fsnotify_event *event,
241 static void remove_access_response(struct fsnotify_group *group,
242 struct fsnotify_event *event,
249 static ssize_t copy_event_to_user(struct fsnotify_group *group,
250 struct fsnotify_event *event,
253 struct fanotify_event_metadata fanotify_event_metadata;
256 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
258 fd = fill_event_metadata(group, &fanotify_event_metadata, event);
262 ret = prepare_for_access_response(group, event, fd);
267 if (copy_to_user(buf, &fanotify_event_metadata, FAN_EVENT_METADATA_LEN))
268 goto out_kill_access_response;
270 return FAN_EVENT_METADATA_LEN;
272 out_kill_access_response:
273 remove_access_response(group, event, fd);
279 /* intofiy userspace file descriptor functions */
280 static unsigned int fanotify_poll(struct file *file, poll_table *wait)
282 struct fsnotify_group *group = file->private_data;
285 poll_wait(file, &group->notification_waitq, wait);
286 mutex_lock(&group->notification_mutex);
287 if (!fsnotify_notify_queue_is_empty(group))
288 ret = POLLIN | POLLRDNORM;
289 mutex_unlock(&group->notification_mutex);
294 static ssize_t fanotify_read(struct file *file, char __user *buf,
295 size_t count, loff_t *pos)
297 struct fsnotify_group *group;
298 struct fsnotify_event *kevent;
304 group = file->private_data;
306 pr_debug("%s: group=%p\n", __func__, group);
309 prepare_to_wait(&group->notification_waitq, &wait, TASK_INTERRUPTIBLE);
311 mutex_lock(&group->notification_mutex);
312 kevent = get_one_event(group, count);
313 mutex_unlock(&group->notification_mutex);
316 ret = PTR_ERR(kevent);
319 ret = copy_event_to_user(group, kevent, buf);
320 fsnotify_put_event(kevent);
329 if (file->f_flags & O_NONBLOCK)
332 if (signal_pending(current))
341 finish_wait(&group->notification_waitq, &wait);
342 if (start != buf && ret != -EFAULT)
347 static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
349 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
350 struct fanotify_response response = { .fd = -1, .response = -1 };
351 struct fsnotify_group *group;
354 group = file->private_data;
356 if (count > sizeof(response))
357 count = sizeof(response);
359 pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
361 if (copy_from_user(&response, buf, count))
364 ret = process_access_response(group, &response);
374 static int fanotify_release(struct inode *ignored, struct file *file)
376 struct fsnotify_group *group = file->private_data;
377 struct fanotify_response_event *re, *lre;
379 pr_debug("%s: file=%p group=%p\n", __func__, file, group);
381 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
382 mutex_lock(&group->fanotify_data.access_mutex);
384 group->fanotify_data.bypass_perm = true;
386 list_for_each_entry_safe(re, lre, &group->fanotify_data.access_list, list) {
387 pr_debug("%s: found group=%p re=%p event=%p\n", __func__, group,
390 list_del_init(&re->list);
391 re->event->response = FAN_ALLOW;
393 kmem_cache_free(fanotify_response_event_cache, re);
395 mutex_unlock(&group->fanotify_data.access_mutex);
397 wake_up(&group->fanotify_data.access_waitq);
399 /* matches the fanotify_init->fsnotify_alloc_group */
400 fsnotify_put_group(group);
405 static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
407 struct fsnotify_group *group;
408 struct fsnotify_event_holder *holder;
413 group = file->private_data;
415 p = (void __user *) arg;
419 mutex_lock(&group->notification_mutex);
420 list_for_each_entry(holder, &group->notification_list, event_list)
421 send_len += FAN_EVENT_METADATA_LEN;
422 mutex_unlock(&group->notification_mutex);
423 ret = put_user(send_len, (int __user *) p);
430 static const struct file_operations fanotify_fops = {
431 .poll = fanotify_poll,
432 .read = fanotify_read,
433 .write = fanotify_write,
435 .release = fanotify_release,
436 .unlocked_ioctl = fanotify_ioctl,
437 .compat_ioctl = fanotify_ioctl,
438 .llseek = noop_llseek,
441 static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
443 kmem_cache_free(fanotify_mark_cache, fsn_mark);
446 static int fanotify_find_path(int dfd, const char __user *filename,
447 struct path *path, unsigned int flags)
451 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
452 dfd, filename, flags);
454 if (filename == NULL) {
459 file = fget_light(dfd, &fput_needed);
464 if ((flags & FAN_MARK_ONLYDIR) &&
465 !(S_ISDIR(file->f_path.dentry->d_inode->i_mode))) {
466 fput_light(file, fput_needed);
470 *path = file->f_path;
472 fput_light(file, fput_needed);
474 unsigned int lookup_flags = 0;
476 if (!(flags & FAN_MARK_DONT_FOLLOW))
477 lookup_flags |= LOOKUP_FOLLOW;
478 if (flags & FAN_MARK_ONLYDIR)
479 lookup_flags |= LOOKUP_DIRECTORY;
481 ret = user_path_at(dfd, filename, lookup_flags, path);
486 /* you can only watch an inode if you have read permissions on it */
487 ret = inode_permission(path->dentry->d_inode, MAY_READ);
494 static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
500 spin_lock(&fsn_mark->lock);
501 if (!(flags & FAN_MARK_IGNORED_MASK)) {
502 oldmask = fsn_mark->mask;
503 fsnotify_set_mark_mask_locked(fsn_mark, (oldmask & ~mask));
505 oldmask = fsn_mark->ignored_mask;
506 fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask & ~mask));
508 spin_unlock(&fsn_mark->lock);
510 if (!(oldmask & ~mask))
511 fsnotify_destroy_mark(fsn_mark);
513 return mask & oldmask;
516 static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
517 struct vfsmount *mnt, __u32 mask,
520 struct fsnotify_mark *fsn_mark = NULL;
523 fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
527 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags);
528 fsnotify_put_mark(fsn_mark);
529 if (removed & mnt->mnt_fsnotify_mask)
530 fsnotify_recalc_vfsmount_mask(mnt);
535 static int fanotify_remove_inode_mark(struct fsnotify_group *group,
536 struct inode *inode, __u32 mask,
539 struct fsnotify_mark *fsn_mark = NULL;
542 fsn_mark = fsnotify_find_inode_mark(group, inode);
546 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags);
547 /* matches the fsnotify_find_inode_mark() */
548 fsnotify_put_mark(fsn_mark);
549 if (removed & inode->i_fsnotify_mask)
550 fsnotify_recalc_inode_mask(inode);
555 static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
561 spin_lock(&fsn_mark->lock);
562 if (!(flags & FAN_MARK_IGNORED_MASK)) {
563 oldmask = fsn_mark->mask;
564 fsnotify_set_mark_mask_locked(fsn_mark, (oldmask | mask));
566 oldmask = fsn_mark->ignored_mask;
567 fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask | mask));
568 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
569 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
571 spin_unlock(&fsn_mark->lock);
573 return mask & ~oldmask;
576 static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
577 struct vfsmount *mnt, __u32 mask,
580 struct fsnotify_mark *fsn_mark;
583 fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
587 fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
591 fsnotify_init_mark(fsn_mark, fanotify_free_mark);
592 ret = fsnotify_add_mark(fsn_mark, group, NULL, mnt, 0);
594 fanotify_free_mark(fsn_mark);
598 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
599 fsnotify_put_mark(fsn_mark);
600 if (added & ~mnt->mnt_fsnotify_mask)
601 fsnotify_recalc_vfsmount_mask(mnt);
606 static int fanotify_add_inode_mark(struct fsnotify_group *group,
607 struct inode *inode, __u32 mask,
610 struct fsnotify_mark *fsn_mark;
613 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
616 * If some other task has this inode open for write we should not add
617 * an ignored mark, unless that ignored mark is supposed to survive
618 * modification changes anyway.
620 if ((flags & FAN_MARK_IGNORED_MASK) &&
621 !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
622 (atomic_read(&inode->i_writecount) > 0))
625 fsn_mark = fsnotify_find_inode_mark(group, inode);
629 fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
633 fsnotify_init_mark(fsn_mark, fanotify_free_mark);
634 ret = fsnotify_add_mark(fsn_mark, group, inode, NULL, 0);
636 fanotify_free_mark(fsn_mark);
640 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
641 fsnotify_put_mark(fsn_mark);
642 if (added & ~inode->i_fsnotify_mask)
643 fsnotify_recalc_inode_mask(inode);
647 /* fanotify syscalls */
648 SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
650 struct fsnotify_group *group;
653 pr_debug("%s: flags=%d event_f_flags=%d\n",
654 __func__, flags, event_f_flags);
656 if (!capable(CAP_SYS_ADMIN))
659 if (flags & ~FAN_ALL_INIT_FLAGS)
662 f_flags = O_RDWR | FMODE_NONOTIFY;
663 if (flags & FAN_CLOEXEC)
664 f_flags |= O_CLOEXEC;
665 if (flags & FAN_NONBLOCK)
666 f_flags |= O_NONBLOCK;
668 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
669 group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
671 return PTR_ERR(group);
673 group->fanotify_data.f_flags = event_f_flags;
674 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
675 mutex_init(&group->fanotify_data.access_mutex);
676 init_waitqueue_head(&group->fanotify_data.access_waitq);
677 INIT_LIST_HEAD(&group->fanotify_data.access_list);
679 switch (flags & FAN_ALL_CLASS_BITS) {
680 case FAN_CLASS_NOTIF:
681 group->priority = FS_PRIO_0;
683 case FAN_CLASS_CONTENT:
684 group->priority = FS_PRIO_1;
686 case FAN_CLASS_PRE_CONTENT:
687 group->priority = FS_PRIO_2;
694 if (flags & FAN_UNLIMITED_QUEUE) {
696 if (!capable(CAP_SYS_ADMIN))
698 group->max_events = UINT_MAX;
700 group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
703 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
710 fsnotify_put_group(group);
714 SYSCALL_DEFINE(fanotify_mark)(int fanotify_fd, unsigned int flags,
716 const char __user * pathname)
718 struct inode *inode = NULL;
719 struct vfsmount *mnt = NULL;
720 struct fsnotify_group *group;
723 int ret, fput_needed;
725 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
726 __func__, fanotify_fd, flags, dfd, pathname, mask);
728 /* we only use the lower 32 bits as of right now. */
729 if (mask & ((__u64)0xffffffff << 32))
732 if (flags & ~FAN_ALL_MARK_FLAGS)
734 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
736 case FAN_MARK_REMOVE:
742 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
743 if (mask & ~(FAN_ALL_EVENTS | FAN_ALL_PERM_EVENTS | FAN_EVENT_ON_CHILD))
745 if (mask & ~(FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD))
749 filp = fget_light(fanotify_fd, &fput_needed);
753 /* verify that this is indeed an fanotify instance */
755 if (unlikely(filp->f_op != &fanotify_fops))
757 group = filp->private_data;
760 * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
761 * allowed to set permissions events.
764 if (mask & FAN_ALL_PERM_EVENTS &&
765 group->priority == FS_PRIO_0)
768 ret = fanotify_find_path(dfd, pathname, &path, flags);
772 /* inode held in place by reference to path; group by fget on fd */
773 if (!(flags & FAN_MARK_MOUNT))
774 inode = path.dentry->d_inode;
778 /* create/update an inode mark */
779 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
781 if (flags & FAN_MARK_MOUNT)
782 ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags);
784 ret = fanotify_add_inode_mark(group, inode, mask, flags);
786 case FAN_MARK_REMOVE:
787 if (flags & FAN_MARK_MOUNT)
788 ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags);
790 ret = fanotify_remove_inode_mark(group, inode, mask, flags);
793 if (flags & FAN_MARK_MOUNT)
794 fsnotify_clear_vfsmount_marks_by_group(group);
796 fsnotify_clear_inode_marks_by_group(group);
804 fput_light(filp, fput_needed);
808 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
809 asmlinkage long SyS_fanotify_mark(long fanotify_fd, long flags, __u64 mask,
810 long dfd, long pathname)
812 return SYSC_fanotify_mark((int) fanotify_fd, (unsigned int) flags,
814 (const char __user *) pathname);
816 SYSCALL_ALIAS(sys_fanotify_mark, SyS_fanotify_mark);
820 * fanotify_user_setup - Our initialization function. Note that we cannnot return
821 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
822 * must result in panic().
824 static int __init fanotify_user_setup(void)
826 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
827 fanotify_response_event_cache = KMEM_CACHE(fanotify_response_event,
832 device_initcall(fanotify_user_setup);