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/types.h>
14 #include <linux/uaccess.h>
16 #include <asm/ioctls.h>
18 extern const struct fsnotify_ops fanotify_fsnotify_ops;
20 static struct kmem_cache *fanotify_mark_cache __read_mostly;
21 static struct kmem_cache *fanotify_response_event_cache __read_mostly;
23 struct fanotify_response_event {
24 struct list_head list;
26 struct fsnotify_event *event;
30 * Get an fsnotify notification event if one exists and is small
31 * enough to fit in "count". Return an error pointer if the count
32 * is not large enough.
34 * Called with the group->notification_mutex held.
36 static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
39 BUG_ON(!mutex_is_locked(&group->notification_mutex));
41 pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
43 if (fsnotify_notify_queue_is_empty(group))
46 if (FAN_EVENT_METADATA_LEN > count)
47 return ERR_PTR(-EINVAL);
49 /* held the notification_mutex the whole time, so this is the
50 * same event we peeked above */
51 return fsnotify_remove_notify_event(group);
54 static int create_fd(struct fsnotify_group *group, struct fsnotify_event *event)
57 struct dentry *dentry;
59 struct file *new_file;
61 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
63 client_fd = get_unused_fd();
67 if (event->data_type != FSNOTIFY_EVENT_PATH) {
69 put_unused_fd(client_fd);
74 * we need a new file handle for the userspace program so it can read even if it was
75 * originally opened O_WRONLY.
77 dentry = dget(event->path.dentry);
78 mnt = mntget(event->path.mnt);
79 /* it's possible this event was an overflow event. in that case dentry and mnt
80 * are NULL; That's fine, just don't call dentry open */
82 new_file = dentry_open(dentry, mnt,
83 O_RDONLY | O_LARGEFILE | FMODE_NONOTIFY,
86 new_file = ERR_PTR(-EOVERFLOW);
87 if (IS_ERR(new_file)) {
89 * we still send an event even if we can't open the file. this
90 * can happen when say tasks are gone and we try to open their
91 * /proc files or we try to open a WRONLY file like in sysfs
92 * we just send the errno to userspace since there isn't much
95 put_unused_fd(client_fd);
96 client_fd = PTR_ERR(new_file);
98 fd_install(client_fd, new_file);
104 static ssize_t fill_event_metadata(struct fsnotify_group *group,
105 struct fanotify_event_metadata *metadata,
106 struct fsnotify_event *event)
108 pr_debug("%s: group=%p metadata=%p event=%p\n", __func__,
109 group, metadata, event);
111 metadata->event_len = FAN_EVENT_METADATA_LEN;
112 metadata->vers = FANOTIFY_METADATA_VERSION;
113 metadata->mask = event->mask & FAN_ALL_OUTGOING_EVENTS;
114 metadata->pid = pid_vnr(event->tgid);
115 metadata->fd = create_fd(group, event);
120 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
121 static struct fanotify_response_event *dequeue_re(struct fsnotify_group *group,
124 struct fanotify_response_event *re, *return_re = NULL;
126 mutex_lock(&group->fanotify_data.access_mutex);
127 list_for_each_entry(re, &group->fanotify_data.access_list, list) {
131 list_del_init(&re->list);
135 mutex_unlock(&group->fanotify_data.access_mutex);
137 pr_debug("%s: found return_re=%p\n", __func__, return_re);
142 static int process_access_response(struct fsnotify_group *group,
143 struct fanotify_response *response_struct)
145 struct fanotify_response_event *re;
146 __s32 fd = response_struct->fd;
147 __u32 response = response_struct->response;
149 pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
152 * make sure the response is valid, if invalid we do nothing and either
153 * userspace can send a valid responce or we will clean it up after the
167 re = dequeue_re(group, fd);
171 re->event->response = response;
173 wake_up(&group->fanotify_data.access_waitq);
175 kmem_cache_free(fanotify_response_event_cache, re);
180 static int prepare_for_access_response(struct fsnotify_group *group,
181 struct fsnotify_event *event,
184 struct fanotify_response_event *re;
186 if (!(event->mask & FAN_ALL_PERM_EVENTS))
189 re = kmem_cache_alloc(fanotify_response_event_cache, GFP_KERNEL);
196 mutex_lock(&group->fanotify_data.access_mutex);
197 list_add_tail(&re->list, &group->fanotify_data.access_list);
198 mutex_unlock(&group->fanotify_data.access_mutex);
203 static void remove_access_response(struct fsnotify_group *group,
204 struct fsnotify_event *event,
207 struct fanotify_response_event *re;
209 if (!(event->mask & FAN_ALL_PERM_EVENTS))
212 re = dequeue_re(group, fd);
216 BUG_ON(re->event != event);
218 kmem_cache_free(fanotify_response_event_cache, re);
223 static int prepare_for_access_response(struct fsnotify_group *group,
224 struct fsnotify_event *event,
230 static void remove_access_response(struct fsnotify_group *group,
231 struct fsnotify_event *event,
238 static ssize_t copy_event_to_user(struct fsnotify_group *group,
239 struct fsnotify_event *event,
242 struct fanotify_event_metadata fanotify_event_metadata;
245 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
247 fd = fill_event_metadata(group, &fanotify_event_metadata, event);
251 ret = prepare_for_access_response(group, event, fd);
256 if (copy_to_user(buf, &fanotify_event_metadata, FAN_EVENT_METADATA_LEN))
257 goto out_kill_access_response;
259 return FAN_EVENT_METADATA_LEN;
261 out_kill_access_response:
262 remove_access_response(group, event, fd);
268 /* intofiy userspace file descriptor functions */
269 static unsigned int fanotify_poll(struct file *file, poll_table *wait)
271 struct fsnotify_group *group = file->private_data;
274 poll_wait(file, &group->notification_waitq, wait);
275 mutex_lock(&group->notification_mutex);
276 if (!fsnotify_notify_queue_is_empty(group))
277 ret = POLLIN | POLLRDNORM;
278 mutex_unlock(&group->notification_mutex);
283 static ssize_t fanotify_read(struct file *file, char __user *buf,
284 size_t count, loff_t *pos)
286 struct fsnotify_group *group;
287 struct fsnotify_event *kevent;
293 group = file->private_data;
295 pr_debug("%s: group=%p\n", __func__, group);
298 prepare_to_wait(&group->notification_waitq, &wait, TASK_INTERRUPTIBLE);
300 mutex_lock(&group->notification_mutex);
301 kevent = get_one_event(group, count);
302 mutex_unlock(&group->notification_mutex);
305 ret = PTR_ERR(kevent);
308 ret = copy_event_to_user(group, kevent, buf);
309 fsnotify_put_event(kevent);
318 if (file->f_flags & O_NONBLOCK)
321 if (signal_pending(current))
330 finish_wait(&group->notification_waitq, &wait);
331 if (start != buf && ret != -EFAULT)
336 static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
338 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
339 struct fanotify_response response = { .fd = -1, .response = -1 };
340 struct fsnotify_group *group;
343 group = file->private_data;
345 if (count > sizeof(response))
346 count = sizeof(response);
348 pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
350 if (copy_from_user(&response, buf, count))
353 ret = process_access_response(group, &response);
363 static int fanotify_release(struct inode *ignored, struct file *file)
365 struct fsnotify_group *group = file->private_data;
367 pr_debug("%s: file=%p group=%p\n", __func__, file, group);
369 /* matches the fanotify_init->fsnotify_alloc_group */
370 fsnotify_put_group(group);
375 static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
377 struct fsnotify_group *group;
378 struct fsnotify_event_holder *holder;
383 group = file->private_data;
385 p = (void __user *) arg;
389 mutex_lock(&group->notification_mutex);
390 list_for_each_entry(holder, &group->notification_list, event_list)
391 send_len += FAN_EVENT_METADATA_LEN;
392 mutex_unlock(&group->notification_mutex);
393 ret = put_user(send_len, (int __user *) p);
400 static const struct file_operations fanotify_fops = {
401 .poll = fanotify_poll,
402 .read = fanotify_read,
403 .write = fanotify_write,
405 .release = fanotify_release,
406 .unlocked_ioctl = fanotify_ioctl,
407 .compat_ioctl = fanotify_ioctl,
410 static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
412 kmem_cache_free(fanotify_mark_cache, fsn_mark);
415 static int fanotify_find_path(int dfd, const char __user *filename,
416 struct path *path, unsigned int flags)
420 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
421 dfd, filename, flags);
423 if (filename == NULL) {
428 file = fget_light(dfd, &fput_needed);
433 if ((flags & FAN_MARK_ONLYDIR) &&
434 !(S_ISDIR(file->f_path.dentry->d_inode->i_mode))) {
435 fput_light(file, fput_needed);
439 *path = file->f_path;
441 fput_light(file, fput_needed);
443 unsigned int lookup_flags = 0;
445 if (!(flags & FAN_MARK_DONT_FOLLOW))
446 lookup_flags |= LOOKUP_FOLLOW;
447 if (flags & FAN_MARK_ONLYDIR)
448 lookup_flags |= LOOKUP_DIRECTORY;
450 ret = user_path_at(dfd, filename, lookup_flags, path);
455 /* you can only watch an inode if you have read permissions on it */
456 ret = inode_permission(path->dentry->d_inode, MAY_READ);
463 static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
469 spin_lock(&fsn_mark->lock);
470 if (!(flags & FAN_MARK_IGNORED_MASK)) {
471 oldmask = fsn_mark->mask;
472 fsnotify_set_mark_mask_locked(fsn_mark, (oldmask & ~mask));
474 oldmask = fsn_mark->ignored_mask;
475 fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask & ~mask));
477 spin_unlock(&fsn_mark->lock);
479 if (!(oldmask & ~mask))
480 fsnotify_destroy_mark(fsn_mark);
482 return mask & oldmask;
485 static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
486 struct vfsmount *mnt, __u32 mask,
489 struct fsnotify_mark *fsn_mark = NULL;
492 fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
496 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags);
497 fsnotify_put_mark(fsn_mark);
498 if (removed & group->mask)
499 fsnotify_recalc_group_mask(group);
500 if (removed & mnt->mnt_fsnotify_mask)
501 fsnotify_recalc_vfsmount_mask(mnt);
506 static int fanotify_remove_inode_mark(struct fsnotify_group *group,
507 struct inode *inode, __u32 mask,
510 struct fsnotify_mark *fsn_mark = NULL;
513 fsn_mark = fsnotify_find_inode_mark(group, inode);
517 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags);
518 /* matches the fsnotify_find_inode_mark() */
519 fsnotify_put_mark(fsn_mark);
521 if (removed & group->mask)
522 fsnotify_recalc_group_mask(group);
523 if (removed & inode->i_fsnotify_mask)
524 fsnotify_recalc_inode_mask(inode);
529 static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
535 spin_lock(&fsn_mark->lock);
536 if (!(flags & FAN_MARK_IGNORED_MASK)) {
537 oldmask = fsn_mark->mask;
538 fsnotify_set_mark_mask_locked(fsn_mark, (oldmask | mask));
540 oldmask = fsn_mark->ignored_mask;
541 fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask | mask));
542 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
543 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
545 spin_unlock(&fsn_mark->lock);
547 return mask & ~oldmask;
550 static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
551 struct vfsmount *mnt, __u32 mask,
554 struct fsnotify_mark *fsn_mark;
557 fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
561 fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
565 fsnotify_init_mark(fsn_mark, fanotify_free_mark);
566 ret = fsnotify_add_mark(fsn_mark, group, NULL, mnt, 0);
568 fanotify_free_mark(fsn_mark);
572 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
573 fsnotify_put_mark(fsn_mark);
575 if (added & ~group->mask)
576 fsnotify_recalc_group_mask(group);
577 if (added & ~mnt->mnt_fsnotify_mask)
578 fsnotify_recalc_vfsmount_mask(mnt);
583 static int fanotify_add_inode_mark(struct fsnotify_group *group,
584 struct inode *inode, __u32 mask,
587 struct fsnotify_mark *fsn_mark;
590 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
592 fsn_mark = fsnotify_find_inode_mark(group, inode);
596 fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
600 fsnotify_init_mark(fsn_mark, fanotify_free_mark);
601 ret = fsnotify_add_mark(fsn_mark, group, inode, NULL, 0);
603 fanotify_free_mark(fsn_mark);
607 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
608 fsnotify_put_mark(fsn_mark);
610 if (added & ~group->mask)
611 fsnotify_recalc_group_mask(group);
612 if (added & ~inode->i_fsnotify_mask)
613 fsnotify_recalc_inode_mask(inode);
618 /* fanotify syscalls */
619 SYSCALL_DEFINE3(fanotify_init, unsigned int, flags, unsigned int, event_f_flags,
620 unsigned int, priority)
622 struct fsnotify_group *group;
625 pr_debug("%s: flags=%d event_f_flags=%d priority=%d\n",
626 __func__, flags, event_f_flags, priority);
631 if (!capable(CAP_SYS_ADMIN))
634 if (flags & ~FAN_ALL_INIT_FLAGS)
637 f_flags = O_RDWR | FMODE_NONOTIFY;
638 if (flags & FAN_CLOEXEC)
639 f_flags |= O_CLOEXEC;
640 if (flags & FAN_NONBLOCK)
641 f_flags |= O_NONBLOCK;
643 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
644 group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
646 return PTR_ERR(group);
648 group->priority = priority;
649 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
650 mutex_init(&group->fanotify_data.access_mutex);
651 init_waitqueue_head(&group->fanotify_data.access_waitq);
652 INIT_LIST_HEAD(&group->fanotify_data.access_list);
655 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
662 fsnotify_put_group(group);
666 SYSCALL_DEFINE(fanotify_mark)(int fanotify_fd, unsigned int flags,
668 const char __user * pathname)
670 struct inode *inode = NULL;
671 struct vfsmount *mnt = NULL;
672 struct fsnotify_group *group;
675 int ret, fput_needed;
677 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
678 __func__, fanotify_fd, flags, dfd, pathname, mask);
680 /* we only use the lower 32 bits as of right now. */
681 if (mask & ((__u64)0xffffffff << 32))
684 if (flags & ~FAN_ALL_MARK_FLAGS)
686 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
688 case FAN_MARK_REMOVE:
694 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
695 if (mask & ~(FAN_ALL_EVENTS | FAN_ALL_PERM_EVENTS | FAN_EVENT_ON_CHILD))
697 if (mask & ~(FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD))
701 filp = fget_light(fanotify_fd, &fput_needed);
705 /* verify that this is indeed an fanotify instance */
707 if (unlikely(filp->f_op != &fanotify_fops))
710 ret = fanotify_find_path(dfd, pathname, &path, flags);
714 /* inode held in place by reference to path; group by fget on fd */
715 if (!(flags & FAN_MARK_MOUNT))
716 inode = path.dentry->d_inode;
719 group = filp->private_data;
721 /* create/update an inode mark */
722 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
724 if (flags & FAN_MARK_MOUNT)
725 ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags);
727 ret = fanotify_add_inode_mark(group, inode, mask, flags);
729 case FAN_MARK_REMOVE:
730 if (flags & FAN_MARK_MOUNT)
731 ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags);
733 ret = fanotify_remove_inode_mark(group, inode, mask, flags);
736 if (flags & FAN_MARK_MOUNT)
737 fsnotify_clear_vfsmount_marks_by_group(group);
739 fsnotify_clear_inode_marks_by_group(group);
740 fsnotify_recalc_group_mask(group);
748 fput_light(filp, fput_needed);
752 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
753 asmlinkage long SyS_fanotify_mark(long fanotify_fd, long flags, __u64 mask,
754 long dfd, long pathname)
756 return SYSC_fanotify_mark((int) fanotify_fd, (unsigned int) flags,
758 (const char __user *) pathname);
760 SYSCALL_ALIAS(sys_fanotify_mark, SyS_fanotify_mark);
764 * fanotify_user_setup - Our initialization function. Note that we cannnot return
765 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
766 * must result in panic().
768 static int __init fanotify_user_setup(void)
770 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
771 fanotify_response_event_cache = KMEM_CACHE(fanotify_response_event,
776 device_initcall(fanotify_user_setup);