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 extern const struct fsnotify_ops fanotify_fsnotify_ops
;
21 static struct kmem_cache
*fanotify_mark_cache __read_mostly
;
22 static struct kmem_cache
*fanotify_response_event_cache __read_mostly
;
24 struct fanotify_response_event
{
25 struct list_head list
;
27 struct fsnotify_event
*event
;
31 * Get an fsnotify notification event if one exists and is small
32 * enough to fit in "count". Return an error pointer if the count
33 * is not large enough.
35 * Called with the group->notification_mutex held.
37 static struct fsnotify_event
*get_one_event(struct fsnotify_group
*group
,
40 BUG_ON(!mutex_is_locked(&group
->notification_mutex
));
42 pr_debug("%s: group=%p count=%zd\n", __func__
, group
, count
);
44 if (fsnotify_notify_queue_is_empty(group
))
47 if (FAN_EVENT_METADATA_LEN
> count
)
48 return ERR_PTR(-EINVAL
);
50 /* held the notification_mutex the whole time, so this is the
51 * same event we peeked above */
52 return fsnotify_remove_notify_event(group
);
55 static int create_fd(struct fsnotify_group
*group
, struct fsnotify_event
*event
)
58 struct dentry
*dentry
;
60 struct file
*new_file
;
62 pr_debug("%s: group=%p event=%p\n", __func__
, group
, event
);
64 client_fd
= get_unused_fd();
68 if (event
->data_type
!= FSNOTIFY_EVENT_PATH
) {
70 put_unused_fd(client_fd
);
75 * we need a new file handle for the userspace program so it can read even if it was
76 * originally opened O_WRONLY.
78 dentry
= dget(event
->path
.dentry
);
79 mnt
= mntget(event
->path
.mnt
);
80 /* it's possible this event was an overflow event. in that case dentry and mnt
81 * are NULL; That's fine, just don't call dentry open */
83 new_file
= dentry_open(dentry
, mnt
,
84 group
->fanotify_data
.f_flags
| FMODE_NONOTIFY
,
87 new_file
= ERR_PTR(-EOVERFLOW
);
88 if (IS_ERR(new_file
)) {
90 * we still send an event even if we can't open the file. this
91 * can happen when say tasks are gone and we try to open their
92 * /proc files or we try to open a WRONLY file like in sysfs
93 * we just send the errno to userspace since there isn't much
96 put_unused_fd(client_fd
);
97 client_fd
= PTR_ERR(new_file
);
99 fd_install(client_fd
, new_file
);
105 static ssize_t
fill_event_metadata(struct fsnotify_group
*group
,
106 struct fanotify_event_metadata
*metadata
,
107 struct fsnotify_event
*event
)
109 pr_debug("%s: group=%p metadata=%p event=%p\n", __func__
,
110 group
, metadata
, event
);
112 metadata
->event_len
= FAN_EVENT_METADATA_LEN
;
113 metadata
->vers
= FANOTIFY_METADATA_VERSION
;
114 metadata
->mask
= event
->mask
& FAN_ALL_OUTGOING_EVENTS
;
115 metadata
->pid
= pid_vnr(event
->tgid
);
116 metadata
->fd
= create_fd(group
, event
);
121 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
122 static struct fanotify_response_event
*dequeue_re(struct fsnotify_group
*group
,
125 struct fanotify_response_event
*re
, *return_re
= NULL
;
127 mutex_lock(&group
->fanotify_data
.access_mutex
);
128 list_for_each_entry(re
, &group
->fanotify_data
.access_list
, list
) {
132 list_del_init(&re
->list
);
136 mutex_unlock(&group
->fanotify_data
.access_mutex
);
138 pr_debug("%s: found return_re=%p\n", __func__
, return_re
);
143 static int process_access_response(struct fsnotify_group
*group
,
144 struct fanotify_response
*response_struct
)
146 struct fanotify_response_event
*re
;
147 __s32 fd
= response_struct
->fd
;
148 __u32 response
= response_struct
->response
;
150 pr_debug("%s: group=%p fd=%d response=%d\n", __func__
, group
,
153 * make sure the response is valid, if invalid we do nothing and either
154 * userspace can send a valid responce or we will clean it up after the
168 re
= dequeue_re(group
, fd
);
172 re
->event
->response
= response
;
174 wake_up(&group
->fanotify_data
.access_waitq
);
176 kmem_cache_free(fanotify_response_event_cache
, re
);
181 static int prepare_for_access_response(struct fsnotify_group
*group
,
182 struct fsnotify_event
*event
,
185 struct fanotify_response_event
*re
;
187 if (!(event
->mask
& FAN_ALL_PERM_EVENTS
))
190 re
= kmem_cache_alloc(fanotify_response_event_cache
, GFP_KERNEL
);
197 mutex_lock(&group
->fanotify_data
.access_mutex
);
199 if (group
->fanotify_data
.bypass_perm
) {
200 mutex_unlock(&group
->fanotify_data
.access_mutex
);
201 kmem_cache_free(fanotify_response_event_cache
, re
);
202 event
->response
= FAN_ALLOW
;
206 list_add_tail(&re
->list
, &group
->fanotify_data
.access_list
);
207 mutex_unlock(&group
->fanotify_data
.access_mutex
);
212 static void remove_access_response(struct fsnotify_group
*group
,
213 struct fsnotify_event
*event
,
216 struct fanotify_response_event
*re
;
218 if (!(event
->mask
& FAN_ALL_PERM_EVENTS
))
221 re
= dequeue_re(group
, fd
);
225 BUG_ON(re
->event
!= event
);
227 kmem_cache_free(fanotify_response_event_cache
, re
);
232 static int prepare_for_access_response(struct fsnotify_group
*group
,
233 struct fsnotify_event
*event
,
239 static void remove_access_response(struct fsnotify_group
*group
,
240 struct fsnotify_event
*event
,
247 static ssize_t
copy_event_to_user(struct fsnotify_group
*group
,
248 struct fsnotify_event
*event
,
251 struct fanotify_event_metadata fanotify_event_metadata
;
254 pr_debug("%s: group=%p event=%p\n", __func__
, group
, event
);
256 fd
= fill_event_metadata(group
, &fanotify_event_metadata
, event
);
260 ret
= prepare_for_access_response(group
, event
, fd
);
265 if (copy_to_user(buf
, &fanotify_event_metadata
, FAN_EVENT_METADATA_LEN
))
266 goto out_kill_access_response
;
268 return FAN_EVENT_METADATA_LEN
;
270 out_kill_access_response
:
271 remove_access_response(group
, event
, fd
);
277 /* intofiy userspace file descriptor functions */
278 static unsigned int fanotify_poll(struct file
*file
, poll_table
*wait
)
280 struct fsnotify_group
*group
= file
->private_data
;
283 poll_wait(file
, &group
->notification_waitq
, wait
);
284 mutex_lock(&group
->notification_mutex
);
285 if (!fsnotify_notify_queue_is_empty(group
))
286 ret
= POLLIN
| POLLRDNORM
;
287 mutex_unlock(&group
->notification_mutex
);
292 static ssize_t
fanotify_read(struct file
*file
, char __user
*buf
,
293 size_t count
, loff_t
*pos
)
295 struct fsnotify_group
*group
;
296 struct fsnotify_event
*kevent
;
302 group
= file
->private_data
;
304 pr_debug("%s: group=%p\n", __func__
, group
);
307 prepare_to_wait(&group
->notification_waitq
, &wait
, TASK_INTERRUPTIBLE
);
309 mutex_lock(&group
->notification_mutex
);
310 kevent
= get_one_event(group
, count
);
311 mutex_unlock(&group
->notification_mutex
);
314 ret
= PTR_ERR(kevent
);
317 ret
= copy_event_to_user(group
, kevent
, buf
);
318 fsnotify_put_event(kevent
);
327 if (file
->f_flags
& O_NONBLOCK
)
330 if (signal_pending(current
))
339 finish_wait(&group
->notification_waitq
, &wait
);
340 if (start
!= buf
&& ret
!= -EFAULT
)
345 static ssize_t
fanotify_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*pos
)
347 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
348 struct fanotify_response response
= { .fd
= -1, .response
= -1 };
349 struct fsnotify_group
*group
;
352 group
= file
->private_data
;
354 if (count
> sizeof(response
))
355 count
= sizeof(response
);
357 pr_debug("%s: group=%p count=%zu\n", __func__
, group
, count
);
359 if (copy_from_user(&response
, buf
, count
))
362 ret
= process_access_response(group
, &response
);
372 static int fanotify_release(struct inode
*ignored
, struct file
*file
)
374 struct fsnotify_group
*group
= file
->private_data
;
375 struct fanotify_response_event
*re
, *lre
;
377 pr_debug("%s: file=%p group=%p\n", __func__
, file
, group
);
379 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
380 mutex_lock(&group
->fanotify_data
.access_mutex
);
382 group
->fanotify_data
.bypass_perm
= true;
384 list_for_each_entry_safe(re
, lre
, &group
->fanotify_data
.access_list
, list
) {
385 pr_debug("%s: found group=%p re=%p event=%p\n", __func__
, group
,
388 list_del_init(&re
->list
);
389 re
->event
->response
= FAN_ALLOW
;
391 kmem_cache_free(fanotify_response_event_cache
, re
);
393 mutex_unlock(&group
->fanotify_data
.access_mutex
);
395 wake_up(&group
->fanotify_data
.access_waitq
);
397 /* matches the fanotify_init->fsnotify_alloc_group */
398 fsnotify_put_group(group
);
403 static long fanotify_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
405 struct fsnotify_group
*group
;
406 struct fsnotify_event_holder
*holder
;
411 group
= file
->private_data
;
413 p
= (void __user
*) arg
;
417 mutex_lock(&group
->notification_mutex
);
418 list_for_each_entry(holder
, &group
->notification_list
, event_list
)
419 send_len
+= FAN_EVENT_METADATA_LEN
;
420 mutex_unlock(&group
->notification_mutex
);
421 ret
= put_user(send_len
, (int __user
*) p
);
428 static const struct file_operations fanotify_fops
= {
429 .poll
= fanotify_poll
,
430 .read
= fanotify_read
,
431 .write
= fanotify_write
,
433 .release
= fanotify_release
,
434 .unlocked_ioctl
= fanotify_ioctl
,
435 .compat_ioctl
= fanotify_ioctl
,
438 static void fanotify_free_mark(struct fsnotify_mark
*fsn_mark
)
440 kmem_cache_free(fanotify_mark_cache
, fsn_mark
);
443 static int fanotify_find_path(int dfd
, const char __user
*filename
,
444 struct path
*path
, unsigned int flags
)
448 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__
,
449 dfd
, filename
, flags
);
451 if (filename
== NULL
) {
456 file
= fget_light(dfd
, &fput_needed
);
461 if ((flags
& FAN_MARK_ONLYDIR
) &&
462 !(S_ISDIR(file
->f_path
.dentry
->d_inode
->i_mode
))) {
463 fput_light(file
, fput_needed
);
467 *path
= file
->f_path
;
469 fput_light(file
, fput_needed
);
471 unsigned int lookup_flags
= 0;
473 if (!(flags
& FAN_MARK_DONT_FOLLOW
))
474 lookup_flags
|= LOOKUP_FOLLOW
;
475 if (flags
& FAN_MARK_ONLYDIR
)
476 lookup_flags
|= LOOKUP_DIRECTORY
;
478 ret
= user_path_at(dfd
, filename
, lookup_flags
, path
);
483 /* you can only watch an inode if you have read permissions on it */
484 ret
= inode_permission(path
->dentry
->d_inode
, MAY_READ
);
491 static __u32
fanotify_mark_remove_from_mask(struct fsnotify_mark
*fsn_mark
,
497 spin_lock(&fsn_mark
->lock
);
498 if (!(flags
& FAN_MARK_IGNORED_MASK
)) {
499 oldmask
= fsn_mark
->mask
;
500 fsnotify_set_mark_mask_locked(fsn_mark
, (oldmask
& ~mask
));
502 oldmask
= fsn_mark
->ignored_mask
;
503 fsnotify_set_mark_ignored_mask_locked(fsn_mark
, (oldmask
& ~mask
));
505 spin_unlock(&fsn_mark
->lock
);
507 if (!(oldmask
& ~mask
))
508 fsnotify_destroy_mark(fsn_mark
);
510 return mask
& oldmask
;
513 static int fanotify_remove_vfsmount_mark(struct fsnotify_group
*group
,
514 struct vfsmount
*mnt
, __u32 mask
,
517 struct fsnotify_mark
*fsn_mark
= NULL
;
520 fsn_mark
= fsnotify_find_vfsmount_mark(group
, mnt
);
524 removed
= fanotify_mark_remove_from_mask(fsn_mark
, mask
, flags
);
525 fsnotify_put_mark(fsn_mark
);
526 if (removed
& mnt
->mnt_fsnotify_mask
)
527 fsnotify_recalc_vfsmount_mask(mnt
);
532 static int fanotify_remove_inode_mark(struct fsnotify_group
*group
,
533 struct inode
*inode
, __u32 mask
,
536 struct fsnotify_mark
*fsn_mark
= NULL
;
539 fsn_mark
= fsnotify_find_inode_mark(group
, inode
);
543 removed
= fanotify_mark_remove_from_mask(fsn_mark
, mask
, flags
);
544 /* matches the fsnotify_find_inode_mark() */
545 fsnotify_put_mark(fsn_mark
);
546 if (removed
& inode
->i_fsnotify_mask
)
547 fsnotify_recalc_inode_mask(inode
);
552 static __u32
fanotify_mark_add_to_mask(struct fsnotify_mark
*fsn_mark
,
558 spin_lock(&fsn_mark
->lock
);
559 if (!(flags
& FAN_MARK_IGNORED_MASK
)) {
560 oldmask
= fsn_mark
->mask
;
561 fsnotify_set_mark_mask_locked(fsn_mark
, (oldmask
| mask
));
563 oldmask
= fsn_mark
->ignored_mask
;
564 fsnotify_set_mark_ignored_mask_locked(fsn_mark
, (oldmask
| mask
));
565 if (flags
& FAN_MARK_IGNORED_SURV_MODIFY
)
566 fsn_mark
->flags
|= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY
;
568 spin_unlock(&fsn_mark
->lock
);
570 return mask
& ~oldmask
;
573 static int fanotify_add_vfsmount_mark(struct fsnotify_group
*group
,
574 struct vfsmount
*mnt
, __u32 mask
,
577 struct fsnotify_mark
*fsn_mark
;
580 fsn_mark
= fsnotify_find_vfsmount_mark(group
, mnt
);
584 fsn_mark
= kmem_cache_alloc(fanotify_mark_cache
, GFP_KERNEL
);
588 fsnotify_init_mark(fsn_mark
, fanotify_free_mark
);
589 ret
= fsnotify_add_mark(fsn_mark
, group
, NULL
, mnt
, 0);
591 fanotify_free_mark(fsn_mark
);
595 added
= fanotify_mark_add_to_mask(fsn_mark
, mask
, flags
);
596 fsnotify_put_mark(fsn_mark
);
597 if (added
& ~mnt
->mnt_fsnotify_mask
)
598 fsnotify_recalc_vfsmount_mask(mnt
);
603 static int fanotify_add_inode_mark(struct fsnotify_group
*group
,
604 struct inode
*inode
, __u32 mask
,
607 struct fsnotify_mark
*fsn_mark
;
610 pr_debug("%s: group=%p inode=%p\n", __func__
, group
, inode
);
612 fsn_mark
= fsnotify_find_inode_mark(group
, inode
);
616 fsn_mark
= kmem_cache_alloc(fanotify_mark_cache
, GFP_KERNEL
);
620 fsnotify_init_mark(fsn_mark
, fanotify_free_mark
);
621 ret
= fsnotify_add_mark(fsn_mark
, group
, inode
, NULL
, 0);
623 fanotify_free_mark(fsn_mark
);
627 added
= fanotify_mark_add_to_mask(fsn_mark
, mask
, flags
);
628 fsnotify_put_mark(fsn_mark
);
629 if (added
& ~inode
->i_fsnotify_mask
)
630 fsnotify_recalc_inode_mask(inode
);
634 /* fanotify syscalls */
635 SYSCALL_DEFINE2(fanotify_init
, unsigned int, flags
, unsigned int, event_f_flags
)
637 struct fsnotify_group
*group
;
640 pr_debug("%s: flags=%d event_f_flags=%d\n",
641 __func__
, flags
, event_f_flags
);
643 if (!capable(CAP_SYS_ADMIN
))
646 if (flags
& ~FAN_ALL_INIT_FLAGS
)
649 f_flags
= O_RDWR
| FMODE_NONOTIFY
;
650 if (flags
& FAN_CLOEXEC
)
651 f_flags
|= O_CLOEXEC
;
652 if (flags
& FAN_NONBLOCK
)
653 f_flags
|= O_NONBLOCK
;
655 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
656 group
= fsnotify_alloc_group(&fanotify_fsnotify_ops
);
658 return PTR_ERR(group
);
660 group
->fanotify_data
.f_flags
= event_f_flags
;
661 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
662 mutex_init(&group
->fanotify_data
.access_mutex
);
663 init_waitqueue_head(&group
->fanotify_data
.access_waitq
);
664 INIT_LIST_HEAD(&group
->fanotify_data
.access_list
);
667 fd
= anon_inode_getfd("[fanotify]", &fanotify_fops
, group
, f_flags
);
674 fsnotify_put_group(group
);
678 SYSCALL_DEFINE(fanotify_mark
)(int fanotify_fd
, unsigned int flags
,
680 const char __user
* pathname
)
682 struct inode
*inode
= NULL
;
683 struct vfsmount
*mnt
= NULL
;
684 struct fsnotify_group
*group
;
687 int ret
, fput_needed
;
689 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
690 __func__
, fanotify_fd
, flags
, dfd
, pathname
, mask
);
692 /* we only use the lower 32 bits as of right now. */
693 if (mask
& ((__u64
)0xffffffff << 32))
696 if (flags
& ~FAN_ALL_MARK_FLAGS
)
698 switch (flags
& (FAN_MARK_ADD
| FAN_MARK_REMOVE
| FAN_MARK_FLUSH
)) {
700 case FAN_MARK_REMOVE
:
706 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
707 if (mask
& ~(FAN_ALL_EVENTS
| FAN_ALL_PERM_EVENTS
| FAN_EVENT_ON_CHILD
))
709 if (mask
& ~(FAN_ALL_EVENTS
| FAN_EVENT_ON_CHILD
))
713 filp
= fget_light(fanotify_fd
, &fput_needed
);
717 /* verify that this is indeed an fanotify instance */
719 if (unlikely(filp
->f_op
!= &fanotify_fops
))
722 ret
= fanotify_find_path(dfd
, pathname
, &path
, flags
);
726 /* inode held in place by reference to path; group by fget on fd */
727 if (!(flags
& FAN_MARK_MOUNT
))
728 inode
= path
.dentry
->d_inode
;
731 group
= filp
->private_data
;
733 /* create/update an inode mark */
734 switch (flags
& (FAN_MARK_ADD
| FAN_MARK_REMOVE
| FAN_MARK_FLUSH
)) {
736 if (flags
& FAN_MARK_MOUNT
)
737 ret
= fanotify_add_vfsmount_mark(group
, mnt
, mask
, flags
);
739 ret
= fanotify_add_inode_mark(group
, inode
, mask
, flags
);
741 case FAN_MARK_REMOVE
:
742 if (flags
& FAN_MARK_MOUNT
)
743 ret
= fanotify_remove_vfsmount_mark(group
, mnt
, mask
, flags
);
745 ret
= fanotify_remove_inode_mark(group
, inode
, mask
, flags
);
748 if (flags
& FAN_MARK_MOUNT
)
749 fsnotify_clear_vfsmount_marks_by_group(group
);
751 fsnotify_clear_inode_marks_by_group(group
);
759 fput_light(filp
, fput_needed
);
763 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
764 asmlinkage
long SyS_fanotify_mark(long fanotify_fd
, long flags
, __u64 mask
,
765 long dfd
, long pathname
)
767 return SYSC_fanotify_mark((int) fanotify_fd
, (unsigned int) flags
,
769 (const char __user
*) pathname
);
771 SYSCALL_ALIAS(sys_fanotify_mark
, SyS_fanotify_mark
);
775 * fanotify_user_setup - Our initialization function. Note that we cannnot return
776 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
777 * must result in panic().
779 static int __init
fanotify_user_setup(void)
781 fanotify_mark_cache
= KMEM_CACHE(fsnotify_mark
, SLAB_PANIC
);
782 fanotify_response_event_cache
= KMEM_CACHE(fanotify_response_event
,
787 device_initcall(fanotify_user_setup
);