1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/fanotify.h>
3 #include <linux/fcntl.h>
4 #include <linux/file.h>
6 #include <linux/anon_inodes.h>
7 #include <linux/fsnotify_backend.h>
8 #include <linux/init.h>
9 #include <linux/mount.h>
10 #include <linux/namei.h>
11 #include <linux/poll.h>
12 #include <linux/security.h>
13 #include <linux/syscalls.h>
14 #include <linux/slab.h>
15 #include <linux/types.h>
16 #include <linux/uaccess.h>
17 #include <linux/compat.h>
18 #include <linux/sched/signal.h>
19 #include <linux/memcontrol.h>
20 #include <linux/statfs.h>
21 #include <linux/exportfs.h>
23 #include <asm/ioctls.h>
25 #include "../../mount.h"
26 #include "../fdinfo.h"
29 #define FANOTIFY_DEFAULT_MAX_EVENTS 16384
30 #define FANOTIFY_DEFAULT_MAX_MARKS 8192
31 #define FANOTIFY_DEFAULT_MAX_LISTENERS 128
34 * All flags that may be specified in parameter event_f_flags of fanotify_init.
36 * Internal and external open flags are stored together in field f_flags of
37 * struct file. Only external open flags shall be allowed in event_f_flags.
38 * Internal flags like FMODE_NONOTIFY, FMODE_EXEC, FMODE_NOCMTIME shall be
41 #define FANOTIFY_INIT_ALL_EVENT_F_BITS ( \
42 O_ACCMODE | O_APPEND | O_NONBLOCK | \
43 __O_SYNC | O_DSYNC | O_CLOEXEC | \
44 O_LARGEFILE | O_NOATIME )
46 extern const struct fsnotify_ops fanotify_fsnotify_ops;
48 struct kmem_cache *fanotify_mark_cache __read_mostly;
49 struct kmem_cache *fanotify_fid_event_cachep __read_mostly;
50 struct kmem_cache *fanotify_path_event_cachep __read_mostly;
51 struct kmem_cache *fanotify_perm_event_cachep __read_mostly;
53 #define FANOTIFY_EVENT_ALIGN 4
54 #define FANOTIFY_INFO_HDR_LEN \
55 (sizeof(struct fanotify_event_info_fid) + sizeof(struct file_handle))
57 static int fanotify_fid_info_len(int fh_len, int name_len)
59 int info_len = fh_len;
62 info_len += name_len + 1;
64 return roundup(FANOTIFY_INFO_HDR_LEN + info_len, FANOTIFY_EVENT_ALIGN);
67 static int fanotify_event_info_len(struct fanotify_event *event)
70 int fh_len = fanotify_event_object_fh_len(event);
73 info_len += fanotify_fid_info_len(fh_len, 0);
75 if (fanotify_event_name_len(event)) {
76 struct fanotify_name_event *fne = FANOTIFY_NE(event);
78 info_len += fanotify_fid_info_len(fne->dir_fh.len,
86 * Get an fanotify notification event if one exists and is small
87 * enough to fit in "count". Return an error pointer if the count
88 * is not large enough. When permission event is dequeued, its state is
89 * updated accordingly.
91 static struct fanotify_event *get_one_event(struct fsnotify_group *group,
94 size_t event_size = FAN_EVENT_METADATA_LEN;
95 struct fanotify_event *event = NULL;
97 pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
99 spin_lock(&group->notification_lock);
100 if (fsnotify_notify_queue_is_empty(group))
103 if (FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS)) {
104 event_size += fanotify_event_info_len(
105 FANOTIFY_E(fsnotify_peek_first_event(group)));
108 if (event_size > count) {
109 event = ERR_PTR(-EINVAL);
112 event = FANOTIFY_E(fsnotify_remove_first_event(group));
113 if (fanotify_is_perm_event(event->mask))
114 FANOTIFY_PERM(event)->state = FAN_EVENT_REPORTED;
116 spin_unlock(&group->notification_lock);
120 static int create_fd(struct fsnotify_group *group, struct path *path,
124 struct file *new_file;
126 client_fd = get_unused_fd_flags(group->fanotify_data.f_flags);
131 * we need a new file handle for the userspace program so it can read even if it was
132 * originally opened O_WRONLY.
134 new_file = dentry_open(path,
135 group->fanotify_data.f_flags | FMODE_NONOTIFY,
137 if (IS_ERR(new_file)) {
139 * we still send an event even if we can't open the file. this
140 * can happen when say tasks are gone and we try to open their
141 * /proc files or we try to open a WRONLY file like in sysfs
142 * we just send the errno to userspace since there isn't much
145 put_unused_fd(client_fd);
146 client_fd = PTR_ERR(new_file);
155 * Finish processing of permission event by setting it to ANSWERED state and
156 * drop group->notification_lock.
158 static void finish_permission_event(struct fsnotify_group *group,
159 struct fanotify_perm_event *event,
160 unsigned int response)
161 __releases(&group->notification_lock)
163 bool destroy = false;
165 assert_spin_locked(&group->notification_lock);
166 event->response = response;
167 if (event->state == FAN_EVENT_CANCELED)
170 event->state = FAN_EVENT_ANSWERED;
171 spin_unlock(&group->notification_lock);
173 fsnotify_destroy_event(group, &event->fae.fse);
176 static int process_access_response(struct fsnotify_group *group,
177 struct fanotify_response *response_struct)
179 struct fanotify_perm_event *event;
180 int fd = response_struct->fd;
181 int response = response_struct->response;
183 pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
186 * make sure the response is valid, if invalid we do nothing and either
187 * userspace can send a valid response or we will clean it up after the
190 switch (response & ~FAN_AUDIT) {
201 if ((response & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT))
204 spin_lock(&group->notification_lock);
205 list_for_each_entry(event, &group->fanotify_data.access_list,
210 list_del_init(&event->fae.fse.list);
211 finish_permission_event(group, event, response);
212 wake_up(&group->fanotify_data.access_waitq);
215 spin_unlock(&group->notification_lock);
220 static int copy_info_to_user(__kernel_fsid_t *fsid, struct fanotify_fh *fh,
221 const char *name, size_t name_len,
222 char __user *buf, size_t count)
224 struct fanotify_event_info_fid info = { };
225 struct file_handle handle = { };
226 unsigned char bounce[FANOTIFY_INLINE_FH_LEN], *fh_buf;
227 size_t fh_len = fh ? fh->len : 0;
228 size_t info_len = fanotify_fid_info_len(fh_len, name_len);
229 size_t len = info_len;
231 pr_debug("%s: fh_len=%zu name_len=%zu, info_len=%zu, count=%zu\n",
232 __func__, fh_len, name_len, info_len, count);
234 if (!fh_len || (name && !name_len))
237 if (WARN_ON_ONCE(len < sizeof(info) || len > count))
241 * Copy event info fid header followed by variable sized file handle
242 * and optionally followed by variable sized filename.
244 info.hdr.info_type = name_len ? FAN_EVENT_INFO_TYPE_DFID_NAME :
245 FAN_EVENT_INFO_TYPE_FID;
248 if (copy_to_user(buf, &info, sizeof(info)))
253 if (WARN_ON_ONCE(len < sizeof(handle)))
256 handle.handle_type = fh->type;
257 handle.handle_bytes = fh_len;
258 if (copy_to_user(buf, &handle, sizeof(handle)))
261 buf += sizeof(handle);
262 len -= sizeof(handle);
263 if (WARN_ON_ONCE(len < fh_len))
267 * For an inline fh and inline file name, copy through stack to exclude
268 * the copy from usercopy hardening protections.
270 fh_buf = fanotify_fh_buf(fh);
271 if (fh_len <= FANOTIFY_INLINE_FH_LEN) {
272 memcpy(bounce, fh_buf, fh_len);
275 if (copy_to_user(buf, fh_buf, fh_len))
282 /* Copy the filename with terminating null */
284 if (WARN_ON_ONCE(len < name_len))
287 if (copy_to_user(buf, name, name_len))
295 WARN_ON_ONCE(len < 0 || len >= FANOTIFY_EVENT_ALIGN);
296 if (len > 0 && clear_user(buf, len))
302 static ssize_t copy_event_to_user(struct fsnotify_group *group,
303 struct fanotify_event *event,
304 char __user *buf, size_t count)
306 struct fanotify_event_metadata metadata;
307 struct path *path = fanotify_event_path(event);
308 struct file *f = NULL;
309 int ret, fd = FAN_NOFD;
311 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
313 metadata.event_len = FAN_EVENT_METADATA_LEN +
314 fanotify_event_info_len(event);
315 metadata.metadata_len = FAN_EVENT_METADATA_LEN;
316 metadata.vers = FANOTIFY_METADATA_VERSION;
317 metadata.reserved = 0;
318 metadata.mask = event->mask & FANOTIFY_OUTGOING_EVENTS;
319 metadata.pid = pid_vnr(event->pid);
321 if (path && path->mnt && path->dentry) {
322 fd = create_fd(group, path, &f);
330 * Sanity check copy size in case get_one_event() and
331 * event_len sizes ever get out of sync.
333 if (WARN_ON_ONCE(metadata.event_len > count))
336 if (copy_to_user(buf, &metadata, FAN_EVENT_METADATA_LEN))
339 buf += FAN_EVENT_METADATA_LEN;
340 count -= FAN_EVENT_METADATA_LEN;
342 if (fanotify_is_perm_event(event->mask))
343 FANOTIFY_PERM(event)->fd = fd;
348 /* Event info records order is: dir fid + name, child fid */
349 if (fanotify_event_name_len(event)) {
350 struct fanotify_name_event *fne = FANOTIFY_NE(event);
352 ret = copy_info_to_user(fanotify_event_fsid(event),
353 fanotify_event_dir_fh(event),
354 fne->name, fne->name_len,
363 if (fanotify_event_object_fh_len(event)) {
364 ret = copy_info_to_user(fanotify_event_fsid(event),
365 fanotify_event_object_fh(event),
366 NULL, 0, buf, count);
374 return metadata.event_len;
377 if (fd != FAN_NOFD) {
384 /* intofiy userspace file descriptor functions */
385 static __poll_t fanotify_poll(struct file *file, poll_table *wait)
387 struct fsnotify_group *group = file->private_data;
390 poll_wait(file, &group->notification_waitq, wait);
391 spin_lock(&group->notification_lock);
392 if (!fsnotify_notify_queue_is_empty(group))
393 ret = EPOLLIN | EPOLLRDNORM;
394 spin_unlock(&group->notification_lock);
399 static ssize_t fanotify_read(struct file *file, char __user *buf,
400 size_t count, loff_t *pos)
402 struct fsnotify_group *group;
403 struct fanotify_event *event;
406 DEFINE_WAIT_FUNC(wait, woken_wake_function);
409 group = file->private_data;
411 pr_debug("%s: group=%p\n", __func__, group);
413 add_wait_queue(&group->notification_waitq, &wait);
416 * User can supply arbitrarily large buffer. Avoid softlockups
417 * in case there are lots of available events.
420 event = get_one_event(group, count);
422 ret = PTR_ERR(event);
428 if (file->f_flags & O_NONBLOCK)
432 if (signal_pending(current))
438 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
442 ret = copy_event_to_user(group, event, buf, count);
443 if (unlikely(ret == -EOPENSTALE)) {
445 * We cannot report events with stale fd so drop it.
446 * Setting ret to 0 will continue the event loop and
447 * do the right thing if there are no more events to
448 * read (i.e. return bytes read, -EAGAIN or wait).
454 * Permission events get queued to wait for response. Other
455 * events can be destroyed now.
457 if (!fanotify_is_perm_event(event->mask)) {
458 fsnotify_destroy_event(group, &event->fse);
461 spin_lock(&group->notification_lock);
462 finish_permission_event(group,
463 FANOTIFY_PERM(event), FAN_DENY);
464 wake_up(&group->fanotify_data.access_waitq);
466 spin_lock(&group->notification_lock);
467 list_add_tail(&event->fse.list,
468 &group->fanotify_data.access_list);
469 spin_unlock(&group->notification_lock);
477 remove_wait_queue(&group->notification_waitq, &wait);
479 if (start != buf && ret != -EFAULT)
484 static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
486 struct fanotify_response response = { .fd = -1, .response = -1 };
487 struct fsnotify_group *group;
490 if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
493 group = file->private_data;
495 if (count < sizeof(response))
498 count = sizeof(response);
500 pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
502 if (copy_from_user(&response, buf, count))
505 ret = process_access_response(group, &response);
512 static int fanotify_release(struct inode *ignored, struct file *file)
514 struct fsnotify_group *group = file->private_data;
517 * Stop new events from arriving in the notification queue. since
518 * userspace cannot use fanotify fd anymore, no event can enter or
519 * leave access_list by now either.
521 fsnotify_group_stop_queueing(group);
524 * Process all permission events on access_list and notification queue
525 * and simulate reply from userspace.
527 spin_lock(&group->notification_lock);
528 while (!list_empty(&group->fanotify_data.access_list)) {
529 struct fanotify_perm_event *event;
531 event = list_first_entry(&group->fanotify_data.access_list,
532 struct fanotify_perm_event, fae.fse.list);
533 list_del_init(&event->fae.fse.list);
534 finish_permission_event(group, event, FAN_ALLOW);
535 spin_lock(&group->notification_lock);
539 * Destroy all non-permission events. For permission events just
540 * dequeue them and set the response. They will be freed once the
541 * response is consumed and fanotify_get_response() returns.
543 while (!fsnotify_notify_queue_is_empty(group)) {
544 struct fanotify_event *event;
546 event = FANOTIFY_E(fsnotify_remove_first_event(group));
547 if (!(event->mask & FANOTIFY_PERM_EVENTS)) {
548 spin_unlock(&group->notification_lock);
549 fsnotify_destroy_event(group, &event->fse);
551 finish_permission_event(group, FANOTIFY_PERM(event),
554 spin_lock(&group->notification_lock);
556 spin_unlock(&group->notification_lock);
558 /* Response for all permission events it set, wakeup waiters */
559 wake_up(&group->fanotify_data.access_waitq);
561 /* matches the fanotify_init->fsnotify_alloc_group */
562 fsnotify_destroy_group(group);
567 static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
569 struct fsnotify_group *group;
570 struct fsnotify_event *fsn_event;
575 group = file->private_data;
577 p = (void __user *) arg;
581 spin_lock(&group->notification_lock);
582 list_for_each_entry(fsn_event, &group->notification_list, list)
583 send_len += FAN_EVENT_METADATA_LEN;
584 spin_unlock(&group->notification_lock);
585 ret = put_user(send_len, (int __user *) p);
592 static const struct file_operations fanotify_fops = {
593 .show_fdinfo = fanotify_show_fdinfo,
594 .poll = fanotify_poll,
595 .read = fanotify_read,
596 .write = fanotify_write,
598 .release = fanotify_release,
599 .unlocked_ioctl = fanotify_ioctl,
600 .compat_ioctl = compat_ptr_ioctl,
601 .llseek = noop_llseek,
604 static int fanotify_find_path(int dfd, const char __user *filename,
605 struct path *path, unsigned int flags, __u64 mask,
606 unsigned int obj_type)
610 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
611 dfd, filename, flags);
613 if (filename == NULL) {
614 struct fd f = fdget(dfd);
621 if ((flags & FAN_MARK_ONLYDIR) &&
622 !(S_ISDIR(file_inode(f.file)->i_mode))) {
627 *path = f.file->f_path;
631 unsigned int lookup_flags = 0;
633 if (!(flags & FAN_MARK_DONT_FOLLOW))
634 lookup_flags |= LOOKUP_FOLLOW;
635 if (flags & FAN_MARK_ONLYDIR)
636 lookup_flags |= LOOKUP_DIRECTORY;
638 ret = user_path_at(dfd, filename, lookup_flags, path);
643 /* you can only watch an inode if you have read permissions on it */
644 ret = inode_permission(path->dentry->d_inode, MAY_READ);
650 ret = security_path_notify(path, mask, obj_type);
658 static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
659 __u32 mask, unsigned int flags,
660 __u32 umask, int *destroy)
664 /* umask bits cannot be removed by user */
666 spin_lock(&fsn_mark->lock);
667 if (!(flags & FAN_MARK_IGNORED_MASK)) {
668 oldmask = fsn_mark->mask;
669 fsn_mark->mask &= ~mask;
671 fsn_mark->ignored_mask &= ~mask;
674 * We need to keep the mark around even if remaining mask cannot
675 * result in any events (e.g. mask == FAN_ONDIR) to support incremenal
676 * changes to the mask.
677 * Destroy mark when only umask bits remain.
679 *destroy = !((fsn_mark->mask | fsn_mark->ignored_mask) & ~umask);
680 spin_unlock(&fsn_mark->lock);
682 return mask & oldmask;
685 static int fanotify_remove_mark(struct fsnotify_group *group,
686 fsnotify_connp_t *connp, __u32 mask,
687 unsigned int flags, __u32 umask)
689 struct fsnotify_mark *fsn_mark = NULL;
693 mutex_lock(&group->mark_mutex);
694 fsn_mark = fsnotify_find_mark(connp, group);
696 mutex_unlock(&group->mark_mutex);
700 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
701 umask, &destroy_mark);
702 if (removed & fsnotify_conn_mask(fsn_mark->connector))
703 fsnotify_recalc_mask(fsn_mark->connector);
705 fsnotify_detach_mark(fsn_mark);
706 mutex_unlock(&group->mark_mutex);
708 fsnotify_free_mark(fsn_mark);
710 /* matches the fsnotify_find_mark() */
711 fsnotify_put_mark(fsn_mark);
715 static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
716 struct vfsmount *mnt, __u32 mask,
717 unsigned int flags, __u32 umask)
719 return fanotify_remove_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
723 static int fanotify_remove_sb_mark(struct fsnotify_group *group,
724 struct super_block *sb, __u32 mask,
725 unsigned int flags, __u32 umask)
727 return fanotify_remove_mark(group, &sb->s_fsnotify_marks, mask,
731 static int fanotify_remove_inode_mark(struct fsnotify_group *group,
732 struct inode *inode, __u32 mask,
733 unsigned int flags, __u32 umask)
735 return fanotify_remove_mark(group, &inode->i_fsnotify_marks, mask,
739 static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
745 spin_lock(&fsn_mark->lock);
746 if (!(flags & FAN_MARK_IGNORED_MASK)) {
747 oldmask = fsn_mark->mask;
748 fsn_mark->mask |= mask;
750 fsn_mark->ignored_mask |= mask;
751 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
752 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
754 spin_unlock(&fsn_mark->lock);
756 return mask & ~oldmask;
759 static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
760 fsnotify_connp_t *connp,
762 __kernel_fsid_t *fsid)
764 struct fsnotify_mark *mark;
767 if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
768 return ERR_PTR(-ENOSPC);
770 mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
772 return ERR_PTR(-ENOMEM);
774 fsnotify_init_mark(mark, group);
775 ret = fsnotify_add_mark_locked(mark, connp, type, 0, fsid);
777 fsnotify_put_mark(mark);
785 static int fanotify_add_mark(struct fsnotify_group *group,
786 fsnotify_connp_t *connp, unsigned int type,
787 __u32 mask, unsigned int flags,
788 __kernel_fsid_t *fsid)
790 struct fsnotify_mark *fsn_mark;
793 mutex_lock(&group->mark_mutex);
794 fsn_mark = fsnotify_find_mark(connp, group);
796 fsn_mark = fanotify_add_new_mark(group, connp, type, fsid);
797 if (IS_ERR(fsn_mark)) {
798 mutex_unlock(&group->mark_mutex);
799 return PTR_ERR(fsn_mark);
802 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
803 if (added & ~fsnotify_conn_mask(fsn_mark->connector))
804 fsnotify_recalc_mask(fsn_mark->connector);
805 mutex_unlock(&group->mark_mutex);
807 fsnotify_put_mark(fsn_mark);
811 static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
812 struct vfsmount *mnt, __u32 mask,
813 unsigned int flags, __kernel_fsid_t *fsid)
815 return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
816 FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags, fsid);
819 static int fanotify_add_sb_mark(struct fsnotify_group *group,
820 struct super_block *sb, __u32 mask,
821 unsigned int flags, __kernel_fsid_t *fsid)
823 return fanotify_add_mark(group, &sb->s_fsnotify_marks,
824 FSNOTIFY_OBJ_TYPE_SB, mask, flags, fsid);
827 static int fanotify_add_inode_mark(struct fsnotify_group *group,
828 struct inode *inode, __u32 mask,
829 unsigned int flags, __kernel_fsid_t *fsid)
831 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
834 * If some other task has this inode open for write we should not add
835 * an ignored mark, unless that ignored mark is supposed to survive
836 * modification changes anyway.
838 if ((flags & FAN_MARK_IGNORED_MASK) &&
839 !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
840 inode_is_open_for_write(inode))
843 return fanotify_add_mark(group, &inode->i_fsnotify_marks,
844 FSNOTIFY_OBJ_TYPE_INODE, mask, flags, fsid);
847 static struct fsnotify_event *fanotify_alloc_overflow_event(void)
849 struct fanotify_event *oevent;
851 oevent = kmalloc(sizeof(*oevent), GFP_KERNEL_ACCOUNT);
855 fanotify_init_event(oevent, 0, FS_Q_OVERFLOW);
856 oevent->type = FANOTIFY_EVENT_TYPE_OVERFLOW;
861 /* fanotify syscalls */
862 SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
864 struct fsnotify_group *group;
866 struct user_struct *user;
868 pr_debug("%s: flags=%x event_f_flags=%x\n",
869 __func__, flags, event_f_flags);
871 if (!capable(CAP_SYS_ADMIN))
874 #ifdef CONFIG_AUDITSYSCALL
875 if (flags & ~(FANOTIFY_INIT_FLAGS | FAN_ENABLE_AUDIT))
877 if (flags & ~FANOTIFY_INIT_FLAGS)
881 if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS)
884 switch (event_f_flags & O_ACCMODE) {
893 if ((flags & FANOTIFY_FID_BITS) &&
894 (flags & FANOTIFY_CLASS_BITS) != FAN_CLASS_NOTIF)
897 user = get_current_user();
898 if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {
903 f_flags = O_RDWR | FMODE_NONOTIFY;
904 if (flags & FAN_CLOEXEC)
905 f_flags |= O_CLOEXEC;
906 if (flags & FAN_NONBLOCK)
907 f_flags |= O_NONBLOCK;
909 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
910 group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
913 return PTR_ERR(group);
916 group->fanotify_data.user = user;
917 group->fanotify_data.flags = flags;
918 atomic_inc(&user->fanotify_listeners);
919 group->memcg = get_mem_cgroup_from_mm(current->mm);
921 group->overflow_event = fanotify_alloc_overflow_event();
922 if (unlikely(!group->overflow_event)) {
924 goto out_destroy_group;
927 if (force_o_largefile())
928 event_f_flags |= O_LARGEFILE;
929 group->fanotify_data.f_flags = event_f_flags;
930 init_waitqueue_head(&group->fanotify_data.access_waitq);
931 INIT_LIST_HEAD(&group->fanotify_data.access_list);
932 switch (flags & FANOTIFY_CLASS_BITS) {
933 case FAN_CLASS_NOTIF:
934 group->priority = FS_PRIO_0;
936 case FAN_CLASS_CONTENT:
937 group->priority = FS_PRIO_1;
939 case FAN_CLASS_PRE_CONTENT:
940 group->priority = FS_PRIO_2;
944 goto out_destroy_group;
947 if (flags & FAN_UNLIMITED_QUEUE) {
949 if (!capable(CAP_SYS_ADMIN))
950 goto out_destroy_group;
951 group->max_events = UINT_MAX;
953 group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
956 if (flags & FAN_UNLIMITED_MARKS) {
958 if (!capable(CAP_SYS_ADMIN))
959 goto out_destroy_group;
960 group->fanotify_data.max_marks = UINT_MAX;
962 group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS;
965 if (flags & FAN_ENABLE_AUDIT) {
967 if (!capable(CAP_AUDIT_WRITE))
968 goto out_destroy_group;
971 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
973 goto out_destroy_group;
978 fsnotify_destroy_group(group);
982 /* Check if filesystem can encode a unique fid */
983 static int fanotify_test_fid(struct path *path, __kernel_fsid_t *fsid)
985 __kernel_fsid_t root_fsid;
989 * Make sure path is not in filesystem with zero fsid (e.g. tmpfs).
991 err = vfs_get_fsid(path->dentry, fsid);
995 if (!fsid->val[0] && !fsid->val[1])
999 * Make sure path is not inside a filesystem subvolume (e.g. btrfs)
1000 * which uses a different fsid than sb root.
1002 err = vfs_get_fsid(path->dentry->d_sb->s_root, &root_fsid);
1006 if (root_fsid.val[0] != fsid->val[0] ||
1007 root_fsid.val[1] != fsid->val[1])
1011 * We need to make sure that the file system supports at least
1012 * encoding a file handle so user can use name_to_handle_at() to
1013 * compare fid returned with event to the file handle of watched
1014 * objects. However, name_to_handle_at() requires that the
1015 * filesystem also supports decoding file handles.
1017 if (!path->dentry->d_sb->s_export_op ||
1018 !path->dentry->d_sb->s_export_op->fh_to_dentry)
1024 static int fanotify_events_supported(struct path *path, __u64 mask)
1027 * Some filesystems such as 'proc' acquire unusual locks when opening
1028 * files. For them fanotify permission events have high chances of
1029 * deadlocking the system - open done when reporting fanotify event
1030 * blocks on this "unusual" lock while another process holding the lock
1031 * waits for fanotify permission event to be answered. Just disallow
1032 * permission events for such filesystems.
1034 if (mask & FANOTIFY_PERM_EVENTS &&
1035 path->mnt->mnt_sb->s_type->fs_flags & FS_DISALLOW_NOTIFY_PERM)
1040 static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
1041 int dfd, const char __user *pathname)
1043 struct inode *inode = NULL;
1044 struct vfsmount *mnt = NULL;
1045 struct fsnotify_group *group;
1048 __kernel_fsid_t __fsid, *fsid = NULL;
1049 u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS;
1050 unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS;
1051 bool ignored = flags & FAN_MARK_IGNORED_MASK;
1052 unsigned int obj_type, fid_mode;
1055 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
1056 __func__, fanotify_fd, flags, dfd, pathname, mask);
1058 /* we only use the lower 32 bits as of right now. */
1059 if (mask & ((__u64)0xffffffff << 32))
1062 if (flags & ~FANOTIFY_MARK_FLAGS)
1065 switch (mark_type) {
1066 case FAN_MARK_INODE:
1067 obj_type = FSNOTIFY_OBJ_TYPE_INODE;
1069 case FAN_MARK_MOUNT:
1070 obj_type = FSNOTIFY_OBJ_TYPE_VFSMOUNT;
1072 case FAN_MARK_FILESYSTEM:
1073 obj_type = FSNOTIFY_OBJ_TYPE_SB;
1079 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
1080 case FAN_MARK_ADD: /* fallthrough */
1081 case FAN_MARK_REMOVE:
1085 case FAN_MARK_FLUSH:
1086 if (flags & ~(FANOTIFY_MARK_TYPE_BITS | FAN_MARK_FLUSH))
1093 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
1094 valid_mask |= FANOTIFY_PERM_EVENTS;
1096 if (mask & ~valid_mask)
1099 /* Event flags (ONDIR, ON_CHILD) are meaningless in ignored mask */
1101 mask &= ~FANOTIFY_EVENT_FLAGS;
1103 f = fdget(fanotify_fd);
1104 if (unlikely(!f.file))
1107 /* verify that this is indeed an fanotify instance */
1109 if (unlikely(f.file->f_op != &fanotify_fops))
1111 group = f.file->private_data;
1114 * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
1115 * allowed to set permissions events.
1118 if (mask & FANOTIFY_PERM_EVENTS &&
1119 group->priority == FS_PRIO_0)
1123 * Events with data type inode do not carry enough information to report
1124 * event->fd, so we do not allow setting a mask for inode events unless
1125 * group supports reporting fid.
1126 * inode events are not supported on a mount mark, because they do not
1127 * carry enough information (i.e. path) to be filtered by mount point.
1129 fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
1130 if (mask & FANOTIFY_INODE_EVENTS &&
1131 (!fid_mode || mark_type == FAN_MARK_MOUNT))
1134 if (flags & FAN_MARK_FLUSH) {
1136 if (mark_type == FAN_MARK_MOUNT)
1137 fsnotify_clear_vfsmount_marks_by_group(group);
1138 else if (mark_type == FAN_MARK_FILESYSTEM)
1139 fsnotify_clear_sb_marks_by_group(group);
1141 fsnotify_clear_inode_marks_by_group(group);
1145 ret = fanotify_find_path(dfd, pathname, &path, flags,
1146 (mask & ALL_FSNOTIFY_EVENTS), obj_type);
1150 if (flags & FAN_MARK_ADD) {
1151 ret = fanotify_events_supported(&path, mask);
1153 goto path_put_and_out;
1157 ret = fanotify_test_fid(&path, &__fsid);
1159 goto path_put_and_out;
1164 /* inode held in place by reference to path; group by fget on fd */
1165 if (mark_type == FAN_MARK_INODE)
1166 inode = path.dentry->d_inode;
1170 /* create/update an inode mark */
1171 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
1173 if (mark_type == FAN_MARK_MOUNT)
1174 ret = fanotify_add_vfsmount_mark(group, mnt, mask,
1176 else if (mark_type == FAN_MARK_FILESYSTEM)
1177 ret = fanotify_add_sb_mark(group, mnt->mnt_sb, mask,
1180 ret = fanotify_add_inode_mark(group, inode, mask,
1183 case FAN_MARK_REMOVE:
1184 if (mark_type == FAN_MARK_MOUNT)
1185 ret = fanotify_remove_vfsmount_mark(group, mnt, mask,
1187 else if (mark_type == FAN_MARK_FILESYSTEM)
1188 ret = fanotify_remove_sb_mark(group, mnt->mnt_sb, mask,
1191 ret = fanotify_remove_inode_mark(group, inode, mask,
1205 SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
1206 __u64, mask, int, dfd,
1207 const char __user *, pathname)
1209 return do_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname);
1212 #ifdef CONFIG_COMPAT
1213 COMPAT_SYSCALL_DEFINE6(fanotify_mark,
1214 int, fanotify_fd, unsigned int, flags,
1215 __u32, mask0, __u32, mask1, int, dfd,
1216 const char __user *, pathname)
1218 return do_fanotify_mark(fanotify_fd, flags,
1220 ((__u64)mask0 << 32) | mask1,
1222 ((__u64)mask1 << 32) | mask0,
1229 * fanotify_user_setup - Our initialization function. Note that we cannot return
1230 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
1231 * must result in panic().
1233 static int __init fanotify_user_setup(void)
1235 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 8);
1236 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9);
1238 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark,
1239 SLAB_PANIC|SLAB_ACCOUNT);
1240 fanotify_fid_event_cachep = KMEM_CACHE(fanotify_fid_event,
1242 fanotify_path_event_cachep = KMEM_CACHE(fanotify_path_event,
1244 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
1245 fanotify_perm_event_cachep =
1246 KMEM_CACHE(fanotify_perm_event, SLAB_PANIC);
1251 device_initcall(fanotify_user_setup);