1 #include <linux/fanotify.h>
2 #include <linux/fcntl.h>
10 #include <linux/poll.h>
13 #include <linux/slab.h>
14 #include <linux/types.h>
17 #include <asm/ioctls.h>
19 #include "../../mount.h"
21 #define FANOTIFY_DEFAULT_MAX_EVENTS 16384
22 #define FANOTIFY_DEFAULT_MAX_MARKS 8192
23 #define FANOTIFY_DEFAULT_MAX_LISTENERS 128
43 static struct fsnotify_event *get_one_event(
struct fsnotify_group *
group,
46 BUG_ON(!mutex_is_locked(&group->notification_mutex));
48 pr_debug(
"%s: group=%p count=%zd\n", __func__, group, count);
61 static int create_fd(
struct fsnotify_group *
group,
62 struct fsnotify_event *
event,
66 struct file *new_file;
68 pr_debug(
"%s: group=%p event=%p\n", __func__, group, event);
74 if (event->data_type != FSNOTIFY_EVENT_PATH) {
86 if (event->path.dentry && event->path.mnt)
92 if (IS_ERR(new_file)) {
101 client_fd = PTR_ERR(new_file);
109 static int fill_event_metadata(
struct fsnotify_group *group,
111 struct fsnotify_event *event,
116 pr_debug(
"%s: group=%p metadata=%p event=%p\n", __func__,
117 group, metadata, event);
128 metadata->
fd = create_fd(group, event, file);
129 if (metadata->
fd < 0)
136 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
142 mutex_lock(&group->fanotify_data.access_mutex);
147 list_del_init(&re->
list);
153 pr_debug(
"%s: found return_re=%p\n", __func__, return_re);
158 static int process_access_response(
struct fsnotify_group *group,
162 __s32 fd = response_struct->
fd;
165 pr_debug(
"%s: group=%p fd=%d response=%d\n", __func__, group,
183 re = dequeue_re(group, fd);
189 wake_up(&group->fanotify_data.access_waitq);
196 static int prepare_for_access_response(
struct fsnotify_group *group,
197 struct fsnotify_event *event,
212 mutex_lock(&group->fanotify_data.access_mutex);
214 if (
atomic_read(&group->fanotify_data.bypass_perm)) {
228 static int prepare_for_access_response(
struct fsnotify_group *group,
229 struct fsnotify_event *event,
237 static ssize_t copy_event_to_user(
struct fsnotify_group *group,
238 struct fsnotify_event *event,
245 pr_debug(
"%s: group=%p event=%p\n", __func__, group, event);
257 ret = prepare_for_access_response(group, event, fd);
271 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
274 wake_up(&group->fanotify_data.access_waitq);
281 static unsigned int fanotify_poll(
struct file *file,
poll_table *
wait)
286 poll_wait(file, &group->notification_waitq, wait);
295 static ssize_t fanotify_read(
struct file *file,
char __user *buf,
298 struct fsnotify_group *
group;
299 struct fsnotify_event *kevent;
307 pr_debug(
"%s: group=%p\n", __func__, group);
313 kevent = get_one_event(group, count);
317 ret = PTR_ERR(kevent);
320 ret = copy_event_to_user(group, kevent, buf);
343 if (start != buf && ret != -
EFAULT)
348 static ssize_t fanotify_write(
struct file *file,
const char __user *buf,
size_t count, loff_t *pos)
350 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
352 struct fsnotify_group *
group;
357 if (count >
sizeof(response))
360 pr_debug(
"%s: group=%p count=%zu\n", __func__, group, count);
365 ret = process_access_response(group, &response);
375 static int fanotify_release(
struct inode *
ignored,
struct file *file)
379 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
382 mutex_lock(&group->fanotify_data.access_mutex);
384 atomic_inc(&group->fanotify_data.bypass_perm);
387 pr_debug(
"%s: found group=%p re=%p event=%p\n", __func__, group,
390 list_del_init(&re->
list);
397 wake_up(&group->fanotify_data.access_waitq);
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;
423 ret =
put_user(send_len, (
int __user *) p);
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,
441 static void fanotify_free_mark(
struct fsnotify_mark *fsn_mark)
446 static int fanotify_find_path(
int dfd,
const char __user *
filename,
451 pr_debug(
"%s: dfd=%d filename=%p flags=%x\n", __func__,
452 dfd, filename, flags);
454 if (filename ==
NULL) {
455 struct fd f = fdget(dfd);
463 !(
S_ISDIR(f.
file->f_path.dentry->d_inode->i_mode))) {
468 *path = f.
file->f_path;
472 unsigned int lookup_flags = 0;
476 if (flags & FAN_MARK_ONLYDIR)
492 static __u32 fanotify_mark_remove_from_mask(
struct fsnotify_mark *fsn_mark,
498 spin_lock(&fsn_mark->lock);
500 oldmask = fsn_mark->mask;
503 oldmask = fsn_mark->ignored_mask;
506 spin_unlock(&fsn_mark->lock);
508 if (!(oldmask & ~mask))
511 return mask & oldmask;
514 static int fanotify_remove_vfsmount_mark(
struct fsnotify_group *group,
518 struct fsnotify_mark *fsn_mark =
NULL;
525 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags);
527 if (removed & real_mount(mnt)->mnt_fsnotify_mask)
533 static int fanotify_remove_inode_mark(
struct fsnotify_group *group,
537 struct fsnotify_mark *fsn_mark =
NULL;
544 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags);
547 if (removed & inode->i_fsnotify_mask)
553 static __u32 fanotify_mark_add_to_mask(
struct fsnotify_mark *fsn_mark,
559 spin_lock(&fsn_mark->lock);
560 if (!(flags & FAN_MARK_IGNORED_MASK)) {
561 oldmask = fsn_mark->mask;
564 __u32 tmask = fsn_mark->ignored_mask |
mask;
567 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
575 spin_unlock(&fsn_mark->lock);
577 return mask & ~oldmask;
580 static int fanotify_add_vfsmount_mark(
struct fsnotify_group *group,
584 struct fsnotify_mark *fsn_mark;
590 if (
atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
602 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
604 if (added & ~real_mount(mnt)->mnt_fsnotify_mask)
611 static int fanotify_add_inode_mark(
struct fsnotify_group *group,
612 struct inode *inode,
__u32 mask,
615 struct fsnotify_mark *fsn_mark;
619 pr_debug(
"%s: group=%p inode=%p\n", __func__, group, inode);
626 if ((flags & FAN_MARK_IGNORED_MASK) &&
633 if (
atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
645 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
647 if (added & ~inode->i_fsnotify_mask)
657 struct fsnotify_group *
group;
661 pr_debug(
"%s: flags=%d event_f_flags=%d\n",
662 __func__, flags, event_f_flags);
686 return PTR_ERR(group);
689 group->fanotify_data.user =
user;
692 group->fanotify_data.f_flags = event_f_flags;
693 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
694 mutex_init(&group->fanotify_data.access_mutex);
696 INIT_LIST_HEAD(&group->fanotify_data.access_list);
697 atomic_set(&group->fanotify_data.bypass_perm, 0);
701 group->priority = FS_PRIO_0;
704 group->priority = FS_PRIO_1;
707 group->priority = FS_PRIO_2;
727 group->fanotify_data.max_marks =
UINT_MAX;
747 struct inode *inode =
NULL;
749 struct fsnotify_group *
group;
754 pr_debug(
"%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
755 __func__, fanotify_fd, flags, dfd,
pathname, mask);
758 if (mask & ((
__u64)0xffffffff << 32))
779 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
786 f = fdget(fanotify_fd);
794 group = f.
file->private_data;
802 group->priority == FS_PRIO_0)
805 ret = fanotify_find_path(dfd,
pathname, &path, flags);
811 inode = path.
dentry->d_inode;
818 if (flags & FAN_MARK_MOUNT)
819 ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags);
821 ret = fanotify_add_inode_mark(group, inode, mask, flags);
824 if (flags & FAN_MARK_MOUNT)
825 ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags);
827 ret = fanotify_remove_inode_mark(group, inode, mask, flags);
830 if (flags & FAN_MARK_MOUNT)
845 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
846 asmlinkage long SyS_fanotify_mark(
long fanotify_fd,
long flags,
__u64 mask,
849 return SYSC_fanotify_mark((
int) fanotify_fd, (
unsigned int) flags,
851 (
const char __user *) pathname);
861 static int __init fanotify_user_setup(
void)