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(unsigned int fid_mode,
68 struct fanotify_event *event)
70 struct fanotify_info *info = fanotify_event_info(event);
71 int dir_fh_len = fanotify_event_dir_fh_len(event);
72 int fh_len = fanotify_event_object_fh_len(event);
77 info_len += fanotify_fid_info_len(dir_fh_len, info->name_len);
78 } else if ((fid_mode & FAN_REPORT_NAME) && (event->mask & FAN_ONDIR)) {
80 * With group flag FAN_REPORT_NAME, if name was not recorded in
81 * event on a directory, we will report the name ".".
87 info_len += fanotify_fid_info_len(fh_len, dot_len);
93 * Get an fanotify notification event if one exists and is small
94 * enough to fit in "count". Return an error pointer if the count
95 * is not large enough. When permission event is dequeued, its state is
96 * updated accordingly.
98 static struct fanotify_event *get_one_event(struct fsnotify_group *group,
101 size_t event_size = FAN_EVENT_METADATA_LEN;
102 struct fanotify_event *event = NULL;
103 struct fsnotify_event *fsn_event;
104 unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
106 pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
108 spin_lock(&group->notification_lock);
109 fsn_event = fsnotify_peek_first_event(group);
113 event = FANOTIFY_E(fsn_event);
115 event_size += fanotify_event_info_len(fid_mode, event);
117 if (event_size > count) {
118 event = ERR_PTR(-EINVAL);
123 * Held the notification_lock the whole time, so this is the
124 * same event we peeked above.
126 fsnotify_remove_first_event(group);
127 if (fanotify_is_perm_event(event->mask))
128 FANOTIFY_PERM(event)->state = FAN_EVENT_REPORTED;
130 spin_unlock(&group->notification_lock);
134 static int create_fd(struct fsnotify_group *group, struct path *path,
138 struct file *new_file;
140 client_fd = get_unused_fd_flags(group->fanotify_data.f_flags);
145 * we need a new file handle for the userspace program so it can read even if it was
146 * originally opened O_WRONLY.
148 new_file = dentry_open(path,
149 group->fanotify_data.f_flags | FMODE_NONOTIFY,
151 if (IS_ERR(new_file)) {
153 * we still send an event even if we can't open the file. this
154 * can happen when say tasks are gone and we try to open their
155 * /proc files or we try to open a WRONLY file like in sysfs
156 * we just send the errno to userspace since there isn't much
159 put_unused_fd(client_fd);
160 client_fd = PTR_ERR(new_file);
169 * Finish processing of permission event by setting it to ANSWERED state and
170 * drop group->notification_lock.
172 static void finish_permission_event(struct fsnotify_group *group,
173 struct fanotify_perm_event *event,
174 unsigned int response)
175 __releases(&group->notification_lock)
177 bool destroy = false;
179 assert_spin_locked(&group->notification_lock);
180 event->response = response;
181 if (event->state == FAN_EVENT_CANCELED)
184 event->state = FAN_EVENT_ANSWERED;
185 spin_unlock(&group->notification_lock);
187 fsnotify_destroy_event(group, &event->fae.fse);
190 static int process_access_response(struct fsnotify_group *group,
191 struct fanotify_response *response_struct)
193 struct fanotify_perm_event *event;
194 int fd = response_struct->fd;
195 int response = response_struct->response;
197 pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
200 * make sure the response is valid, if invalid we do nothing and either
201 * userspace can send a valid response or we will clean it up after the
204 switch (response & ~FAN_AUDIT) {
215 if ((response & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT))
218 spin_lock(&group->notification_lock);
219 list_for_each_entry(event, &group->fanotify_data.access_list,
224 list_del_init(&event->fae.fse.list);
225 finish_permission_event(group, event, response);
226 wake_up(&group->fanotify_data.access_waitq);
229 spin_unlock(&group->notification_lock);
234 static int copy_info_to_user(__kernel_fsid_t *fsid, struct fanotify_fh *fh,
235 int info_type, const char *name, size_t name_len,
236 char __user *buf, size_t count)
238 struct fanotify_event_info_fid info = { };
239 struct file_handle handle = { };
240 unsigned char bounce[FANOTIFY_INLINE_FH_LEN], *fh_buf;
241 size_t fh_len = fh ? fh->len : 0;
242 size_t info_len = fanotify_fid_info_len(fh_len, name_len);
243 size_t len = info_len;
245 pr_debug("%s: fh_len=%zu name_len=%zu, info_len=%zu, count=%zu\n",
246 __func__, fh_len, name_len, info_len, count);
251 if (WARN_ON_ONCE(len < sizeof(info) || len > count))
255 * Copy event info fid header followed by variable sized file handle
256 * and optionally followed by variable sized filename.
259 case FAN_EVENT_INFO_TYPE_FID:
260 case FAN_EVENT_INFO_TYPE_DFID:
261 if (WARN_ON_ONCE(name_len))
264 case FAN_EVENT_INFO_TYPE_DFID_NAME:
265 if (WARN_ON_ONCE(!name || !name_len))
272 info.hdr.info_type = info_type;
275 if (copy_to_user(buf, &info, sizeof(info)))
280 if (WARN_ON_ONCE(len < sizeof(handle)))
283 handle.handle_type = fh->type;
284 handle.handle_bytes = fh_len;
285 if (copy_to_user(buf, &handle, sizeof(handle)))
288 buf += sizeof(handle);
289 len -= sizeof(handle);
290 if (WARN_ON_ONCE(len < fh_len))
294 * For an inline fh and inline file name, copy through stack to exclude
295 * the copy from usercopy hardening protections.
297 fh_buf = fanotify_fh_buf(fh);
298 if (fh_len <= FANOTIFY_INLINE_FH_LEN) {
299 memcpy(bounce, fh_buf, fh_len);
302 if (copy_to_user(buf, fh_buf, fh_len))
309 /* Copy the filename with terminating null */
311 if (WARN_ON_ONCE(len < name_len))
314 if (copy_to_user(buf, name, name_len))
322 WARN_ON_ONCE(len < 0 || len >= FANOTIFY_EVENT_ALIGN);
323 if (len > 0 && clear_user(buf, len))
329 static ssize_t copy_event_to_user(struct fsnotify_group *group,
330 struct fanotify_event *event,
331 char __user *buf, size_t count)
333 struct fanotify_event_metadata metadata;
334 struct path *path = fanotify_event_path(event);
335 struct fanotify_info *info = fanotify_event_info(event);
336 unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
337 struct file *f = NULL;
338 int ret, fd = FAN_NOFD;
341 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
343 metadata.event_len = FAN_EVENT_METADATA_LEN +
344 fanotify_event_info_len(fid_mode, event);
345 metadata.metadata_len = FAN_EVENT_METADATA_LEN;
346 metadata.vers = FANOTIFY_METADATA_VERSION;
347 metadata.reserved = 0;
348 metadata.mask = event->mask & FANOTIFY_OUTGOING_EVENTS;
349 metadata.pid = pid_vnr(event->pid);
351 if (path && path->mnt && path->dentry) {
352 fd = create_fd(group, path, &f);
360 * Sanity check copy size in case get_one_event() and
361 * event_len sizes ever get out of sync.
363 if (WARN_ON_ONCE(metadata.event_len > count))
366 if (copy_to_user(buf, &metadata, FAN_EVENT_METADATA_LEN))
369 buf += FAN_EVENT_METADATA_LEN;
370 count -= FAN_EVENT_METADATA_LEN;
372 if (fanotify_is_perm_event(event->mask))
373 FANOTIFY_PERM(event)->fd = fd;
378 /* Event info records order is: dir fid + name, child fid */
379 if (fanotify_event_dir_fh_len(event)) {
380 info_type = info->name_len ? FAN_EVENT_INFO_TYPE_DFID_NAME :
381 FAN_EVENT_INFO_TYPE_DFID;
382 ret = copy_info_to_user(fanotify_event_fsid(event),
383 fanotify_info_dir_fh(info),
384 info_type, fanotify_info_name(info),
385 info->name_len, buf, count);
393 if (fanotify_event_object_fh_len(event)) {
394 const char *dot = NULL;
397 if (fid_mode == FAN_REPORT_FID || info_type) {
399 * With only group flag FAN_REPORT_FID only type FID is
400 * reported. Second info record type is always FID.
402 info_type = FAN_EVENT_INFO_TYPE_FID;
403 } else if ((fid_mode & FAN_REPORT_NAME) &&
404 (event->mask & FAN_ONDIR)) {
406 * With group flag FAN_REPORT_NAME, if name was not
407 * recorded in an event on a directory, report the
408 * name "." with info type DFID_NAME.
410 info_type = FAN_EVENT_INFO_TYPE_DFID_NAME;
413 } else if ((event->mask & ALL_FSNOTIFY_DIRENT_EVENTS) ||
414 (event->mask & FAN_ONDIR)) {
416 * With group flag FAN_REPORT_DIR_FID, a single info
417 * record has type DFID for directory entry modification
418 * event and for event on a directory.
420 info_type = FAN_EVENT_INFO_TYPE_DFID;
423 * With group flags FAN_REPORT_DIR_FID|FAN_REPORT_FID,
424 * a single info record has type FID for event on a
425 * non-directory, when there is no directory to report.
426 * For example, on FAN_DELETE_SELF event.
428 info_type = FAN_EVENT_INFO_TYPE_FID;
431 ret = copy_info_to_user(fanotify_event_fsid(event),
432 fanotify_event_object_fh(event),
433 info_type, dot, dot_len, buf, count);
441 return metadata.event_len;
444 if (fd != FAN_NOFD) {
451 /* intofiy userspace file descriptor functions */
452 static __poll_t fanotify_poll(struct file *file, poll_table *wait)
454 struct fsnotify_group *group = file->private_data;
457 poll_wait(file, &group->notification_waitq, wait);
458 spin_lock(&group->notification_lock);
459 if (!fsnotify_notify_queue_is_empty(group))
460 ret = EPOLLIN | EPOLLRDNORM;
461 spin_unlock(&group->notification_lock);
466 static ssize_t fanotify_read(struct file *file, char __user *buf,
467 size_t count, loff_t *pos)
469 struct fsnotify_group *group;
470 struct fanotify_event *event;
473 DEFINE_WAIT_FUNC(wait, woken_wake_function);
476 group = file->private_data;
478 pr_debug("%s: group=%p\n", __func__, group);
480 add_wait_queue(&group->notification_waitq, &wait);
483 * User can supply arbitrarily large buffer. Avoid softlockups
484 * in case there are lots of available events.
487 event = get_one_event(group, count);
489 ret = PTR_ERR(event);
495 if (file->f_flags & O_NONBLOCK)
499 if (signal_pending(current))
505 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
509 ret = copy_event_to_user(group, event, buf, count);
510 if (unlikely(ret == -EOPENSTALE)) {
512 * We cannot report events with stale fd so drop it.
513 * Setting ret to 0 will continue the event loop and
514 * do the right thing if there are no more events to
515 * read (i.e. return bytes read, -EAGAIN or wait).
521 * Permission events get queued to wait for response. Other
522 * events can be destroyed now.
524 if (!fanotify_is_perm_event(event->mask)) {
525 fsnotify_destroy_event(group, &event->fse);
528 spin_lock(&group->notification_lock);
529 finish_permission_event(group,
530 FANOTIFY_PERM(event), FAN_DENY);
531 wake_up(&group->fanotify_data.access_waitq);
533 spin_lock(&group->notification_lock);
534 list_add_tail(&event->fse.list,
535 &group->fanotify_data.access_list);
536 spin_unlock(&group->notification_lock);
544 remove_wait_queue(&group->notification_waitq, &wait);
546 if (start != buf && ret != -EFAULT)
551 static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
553 struct fanotify_response response = { .fd = -1, .response = -1 };
554 struct fsnotify_group *group;
557 if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
560 group = file->private_data;
562 if (count < sizeof(response))
565 count = sizeof(response);
567 pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
569 if (copy_from_user(&response, buf, count))
572 ret = process_access_response(group, &response);
579 static int fanotify_release(struct inode *ignored, struct file *file)
581 struct fsnotify_group *group = file->private_data;
582 struct fsnotify_event *fsn_event;
585 * Stop new events from arriving in the notification queue. since
586 * userspace cannot use fanotify fd anymore, no event can enter or
587 * leave access_list by now either.
589 fsnotify_group_stop_queueing(group);
592 * Process all permission events on access_list and notification queue
593 * and simulate reply from userspace.
595 spin_lock(&group->notification_lock);
596 while (!list_empty(&group->fanotify_data.access_list)) {
597 struct fanotify_perm_event *event;
599 event = list_first_entry(&group->fanotify_data.access_list,
600 struct fanotify_perm_event, fae.fse.list);
601 list_del_init(&event->fae.fse.list);
602 finish_permission_event(group, event, FAN_ALLOW);
603 spin_lock(&group->notification_lock);
607 * Destroy all non-permission events. For permission events just
608 * dequeue them and set the response. They will be freed once the
609 * response is consumed and fanotify_get_response() returns.
611 while ((fsn_event = fsnotify_remove_first_event(group))) {
612 struct fanotify_event *event = FANOTIFY_E(fsn_event);
614 if (!(event->mask & FANOTIFY_PERM_EVENTS)) {
615 spin_unlock(&group->notification_lock);
616 fsnotify_destroy_event(group, fsn_event);
618 finish_permission_event(group, FANOTIFY_PERM(event),
621 spin_lock(&group->notification_lock);
623 spin_unlock(&group->notification_lock);
625 /* Response for all permission events it set, wakeup waiters */
626 wake_up(&group->fanotify_data.access_waitq);
628 /* matches the fanotify_init->fsnotify_alloc_group */
629 fsnotify_destroy_group(group);
634 static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
636 struct fsnotify_group *group;
637 struct fsnotify_event *fsn_event;
642 group = file->private_data;
644 p = (void __user *) arg;
648 spin_lock(&group->notification_lock);
649 list_for_each_entry(fsn_event, &group->notification_list, list)
650 send_len += FAN_EVENT_METADATA_LEN;
651 spin_unlock(&group->notification_lock);
652 ret = put_user(send_len, (int __user *) p);
659 static const struct file_operations fanotify_fops = {
660 .show_fdinfo = fanotify_show_fdinfo,
661 .poll = fanotify_poll,
662 .read = fanotify_read,
663 .write = fanotify_write,
665 .release = fanotify_release,
666 .unlocked_ioctl = fanotify_ioctl,
667 .compat_ioctl = compat_ptr_ioctl,
668 .llseek = noop_llseek,
671 static int fanotify_find_path(int dfd, const char __user *filename,
672 struct path *path, unsigned int flags, __u64 mask,
673 unsigned int obj_type)
677 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
678 dfd, filename, flags);
680 if (filename == NULL) {
681 struct fd f = fdget(dfd);
688 if ((flags & FAN_MARK_ONLYDIR) &&
689 !(S_ISDIR(file_inode(f.file)->i_mode))) {
694 *path = f.file->f_path;
698 unsigned int lookup_flags = 0;
700 if (!(flags & FAN_MARK_DONT_FOLLOW))
701 lookup_flags |= LOOKUP_FOLLOW;
702 if (flags & FAN_MARK_ONLYDIR)
703 lookup_flags |= LOOKUP_DIRECTORY;
705 ret = user_path_at(dfd, filename, lookup_flags, path);
710 /* you can only watch an inode if you have read permissions on it */
711 ret = path_permission(path, MAY_READ);
717 ret = security_path_notify(path, mask, obj_type);
725 static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
726 __u32 mask, unsigned int flags,
727 __u32 umask, int *destroy)
731 /* umask bits cannot be removed by user */
733 spin_lock(&fsn_mark->lock);
734 if (!(flags & FAN_MARK_IGNORED_MASK)) {
735 oldmask = fsn_mark->mask;
736 fsn_mark->mask &= ~mask;
738 fsn_mark->ignored_mask &= ~mask;
741 * We need to keep the mark around even if remaining mask cannot
742 * result in any events (e.g. mask == FAN_ONDIR) to support incremenal
743 * changes to the mask.
744 * Destroy mark when only umask bits remain.
746 *destroy = !((fsn_mark->mask | fsn_mark->ignored_mask) & ~umask);
747 spin_unlock(&fsn_mark->lock);
749 return mask & oldmask;
752 static int fanotify_remove_mark(struct fsnotify_group *group,
753 fsnotify_connp_t *connp, __u32 mask,
754 unsigned int flags, __u32 umask)
756 struct fsnotify_mark *fsn_mark = NULL;
760 mutex_lock(&group->mark_mutex);
761 fsn_mark = fsnotify_find_mark(connp, group);
763 mutex_unlock(&group->mark_mutex);
767 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
768 umask, &destroy_mark);
769 if (removed & fsnotify_conn_mask(fsn_mark->connector))
770 fsnotify_recalc_mask(fsn_mark->connector);
772 fsnotify_detach_mark(fsn_mark);
773 mutex_unlock(&group->mark_mutex);
775 fsnotify_free_mark(fsn_mark);
777 /* matches the fsnotify_find_mark() */
778 fsnotify_put_mark(fsn_mark);
782 static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
783 struct vfsmount *mnt, __u32 mask,
784 unsigned int flags, __u32 umask)
786 return fanotify_remove_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
790 static int fanotify_remove_sb_mark(struct fsnotify_group *group,
791 struct super_block *sb, __u32 mask,
792 unsigned int flags, __u32 umask)
794 return fanotify_remove_mark(group, &sb->s_fsnotify_marks, mask,
798 static int fanotify_remove_inode_mark(struct fsnotify_group *group,
799 struct inode *inode, __u32 mask,
800 unsigned int flags, __u32 umask)
802 return fanotify_remove_mark(group, &inode->i_fsnotify_marks, mask,
806 static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
812 spin_lock(&fsn_mark->lock);
813 if (!(flags & FAN_MARK_IGNORED_MASK)) {
814 oldmask = fsn_mark->mask;
815 fsn_mark->mask |= mask;
817 fsn_mark->ignored_mask |= mask;
818 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
819 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
821 spin_unlock(&fsn_mark->lock);
823 return mask & ~oldmask;
826 static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
827 fsnotify_connp_t *connp,
829 __kernel_fsid_t *fsid)
831 struct fsnotify_mark *mark;
834 if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
835 return ERR_PTR(-ENOSPC);
837 mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
839 return ERR_PTR(-ENOMEM);
841 fsnotify_init_mark(mark, group);
842 ret = fsnotify_add_mark_locked(mark, connp, type, 0, fsid);
844 fsnotify_put_mark(mark);
852 static int fanotify_add_mark(struct fsnotify_group *group,
853 fsnotify_connp_t *connp, unsigned int type,
854 __u32 mask, unsigned int flags,
855 __kernel_fsid_t *fsid)
857 struct fsnotify_mark *fsn_mark;
860 mutex_lock(&group->mark_mutex);
861 fsn_mark = fsnotify_find_mark(connp, group);
863 fsn_mark = fanotify_add_new_mark(group, connp, type, fsid);
864 if (IS_ERR(fsn_mark)) {
865 mutex_unlock(&group->mark_mutex);
866 return PTR_ERR(fsn_mark);
869 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
870 if (added & ~fsnotify_conn_mask(fsn_mark->connector))
871 fsnotify_recalc_mask(fsn_mark->connector);
872 mutex_unlock(&group->mark_mutex);
874 fsnotify_put_mark(fsn_mark);
878 static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
879 struct vfsmount *mnt, __u32 mask,
880 unsigned int flags, __kernel_fsid_t *fsid)
882 return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
883 FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags, fsid);
886 static int fanotify_add_sb_mark(struct fsnotify_group *group,
887 struct super_block *sb, __u32 mask,
888 unsigned int flags, __kernel_fsid_t *fsid)
890 return fanotify_add_mark(group, &sb->s_fsnotify_marks,
891 FSNOTIFY_OBJ_TYPE_SB, mask, flags, fsid);
894 static int fanotify_add_inode_mark(struct fsnotify_group *group,
895 struct inode *inode, __u32 mask,
896 unsigned int flags, __kernel_fsid_t *fsid)
898 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
901 * If some other task has this inode open for write we should not add
902 * an ignored mark, unless that ignored mark is supposed to survive
903 * modification changes anyway.
905 if ((flags & FAN_MARK_IGNORED_MASK) &&
906 !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
907 inode_is_open_for_write(inode))
910 return fanotify_add_mark(group, &inode->i_fsnotify_marks,
911 FSNOTIFY_OBJ_TYPE_INODE, mask, flags, fsid);
914 static struct fsnotify_event *fanotify_alloc_overflow_event(void)
916 struct fanotify_event *oevent;
918 oevent = kmalloc(sizeof(*oevent), GFP_KERNEL_ACCOUNT);
922 fanotify_init_event(oevent, 0, FS_Q_OVERFLOW);
923 oevent->type = FANOTIFY_EVENT_TYPE_OVERFLOW;
928 /* fanotify syscalls */
929 SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
931 struct fsnotify_group *group;
933 struct user_struct *user;
934 unsigned int fid_mode = flags & FANOTIFY_FID_BITS;
935 unsigned int class = flags & FANOTIFY_CLASS_BITS;
937 pr_debug("%s: flags=%x event_f_flags=%x\n",
938 __func__, flags, event_f_flags);
940 if (!capable(CAP_SYS_ADMIN))
943 #ifdef CONFIG_AUDITSYSCALL
944 if (flags & ~(FANOTIFY_INIT_FLAGS | FAN_ENABLE_AUDIT))
946 if (flags & ~FANOTIFY_INIT_FLAGS)
950 if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS)
953 switch (event_f_flags & O_ACCMODE) {
962 if (fid_mode && class != FAN_CLASS_NOTIF)
966 * Child name is reported with parent fid so requires dir fid.
967 * We can report both child fid and dir fid with or without name.
969 if ((fid_mode & FAN_REPORT_NAME) && !(fid_mode & FAN_REPORT_DIR_FID))
972 user = get_current_user();
973 if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {
978 f_flags = O_RDWR | FMODE_NONOTIFY;
979 if (flags & FAN_CLOEXEC)
980 f_flags |= O_CLOEXEC;
981 if (flags & FAN_NONBLOCK)
982 f_flags |= O_NONBLOCK;
984 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
985 group = fsnotify_alloc_user_group(&fanotify_fsnotify_ops);
988 return PTR_ERR(group);
991 group->fanotify_data.user = user;
992 group->fanotify_data.flags = flags;
993 atomic_inc(&user->fanotify_listeners);
994 group->memcg = get_mem_cgroup_from_mm(current->mm);
996 group->overflow_event = fanotify_alloc_overflow_event();
997 if (unlikely(!group->overflow_event)) {
999 goto out_destroy_group;
1002 if (force_o_largefile())
1003 event_f_flags |= O_LARGEFILE;
1004 group->fanotify_data.f_flags = event_f_flags;
1005 init_waitqueue_head(&group->fanotify_data.access_waitq);
1006 INIT_LIST_HEAD(&group->fanotify_data.access_list);
1008 case FAN_CLASS_NOTIF:
1009 group->priority = FS_PRIO_0;
1011 case FAN_CLASS_CONTENT:
1012 group->priority = FS_PRIO_1;
1014 case FAN_CLASS_PRE_CONTENT:
1015 group->priority = FS_PRIO_2;
1019 goto out_destroy_group;
1022 if (flags & FAN_UNLIMITED_QUEUE) {
1024 if (!capable(CAP_SYS_ADMIN))
1025 goto out_destroy_group;
1026 group->max_events = UINT_MAX;
1028 group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
1031 if (flags & FAN_UNLIMITED_MARKS) {
1033 if (!capable(CAP_SYS_ADMIN))
1034 goto out_destroy_group;
1035 group->fanotify_data.max_marks = UINT_MAX;
1037 group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS;
1040 if (flags & FAN_ENABLE_AUDIT) {
1042 if (!capable(CAP_AUDIT_WRITE))
1043 goto out_destroy_group;
1046 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
1048 goto out_destroy_group;
1053 fsnotify_destroy_group(group);
1057 /* Check if filesystem can encode a unique fid */
1058 static int fanotify_test_fid(struct path *path, __kernel_fsid_t *fsid)
1060 __kernel_fsid_t root_fsid;
1064 * Make sure path is not in filesystem with zero fsid (e.g. tmpfs).
1066 err = vfs_get_fsid(path->dentry, fsid);
1070 if (!fsid->val[0] && !fsid->val[1])
1074 * Make sure path is not inside a filesystem subvolume (e.g. btrfs)
1075 * which uses a different fsid than sb root.
1077 err = vfs_get_fsid(path->dentry->d_sb->s_root, &root_fsid);
1081 if (root_fsid.val[0] != fsid->val[0] ||
1082 root_fsid.val[1] != fsid->val[1])
1086 * We need to make sure that the file system supports at least
1087 * encoding a file handle so user can use name_to_handle_at() to
1088 * compare fid returned with event to the file handle of watched
1089 * objects. However, name_to_handle_at() requires that the
1090 * filesystem also supports decoding file handles.
1092 if (!path->dentry->d_sb->s_export_op ||
1093 !path->dentry->d_sb->s_export_op->fh_to_dentry)
1099 static int fanotify_events_supported(struct path *path, __u64 mask)
1102 * Some filesystems such as 'proc' acquire unusual locks when opening
1103 * files. For them fanotify permission events have high chances of
1104 * deadlocking the system - open done when reporting fanotify event
1105 * blocks on this "unusual" lock while another process holding the lock
1106 * waits for fanotify permission event to be answered. Just disallow
1107 * permission events for such filesystems.
1109 if (mask & FANOTIFY_PERM_EVENTS &&
1110 path->mnt->mnt_sb->s_type->fs_flags & FS_DISALLOW_NOTIFY_PERM)
1115 static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
1116 int dfd, const char __user *pathname)
1118 struct inode *inode = NULL;
1119 struct vfsmount *mnt = NULL;
1120 struct fsnotify_group *group;
1123 __kernel_fsid_t __fsid, *fsid = NULL;
1124 u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS;
1125 unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS;
1126 bool ignored = flags & FAN_MARK_IGNORED_MASK;
1127 unsigned int obj_type, fid_mode;
1131 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
1132 __func__, fanotify_fd, flags, dfd, pathname, mask);
1134 /* we only use the lower 32 bits as of right now. */
1135 if (mask & ((__u64)0xffffffff << 32))
1138 if (flags & ~FANOTIFY_MARK_FLAGS)
1141 switch (mark_type) {
1142 case FAN_MARK_INODE:
1143 obj_type = FSNOTIFY_OBJ_TYPE_INODE;
1145 case FAN_MARK_MOUNT:
1146 obj_type = FSNOTIFY_OBJ_TYPE_VFSMOUNT;
1148 case FAN_MARK_FILESYSTEM:
1149 obj_type = FSNOTIFY_OBJ_TYPE_SB;
1155 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
1157 case FAN_MARK_REMOVE:
1161 case FAN_MARK_FLUSH:
1162 if (flags & ~(FANOTIFY_MARK_TYPE_BITS | FAN_MARK_FLUSH))
1169 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
1170 valid_mask |= FANOTIFY_PERM_EVENTS;
1172 if (mask & ~valid_mask)
1175 /* Event flags (ONDIR, ON_CHILD) are meaningless in ignored mask */
1177 mask &= ~FANOTIFY_EVENT_FLAGS;
1179 f = fdget(fanotify_fd);
1180 if (unlikely(!f.file))
1183 /* verify that this is indeed an fanotify instance */
1185 if (unlikely(f.file->f_op != &fanotify_fops))
1187 group = f.file->private_data;
1190 * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
1191 * allowed to set permissions events.
1194 if (mask & FANOTIFY_PERM_EVENTS &&
1195 group->priority == FS_PRIO_0)
1199 * Events with data type inode do not carry enough information to report
1200 * event->fd, so we do not allow setting a mask for inode events unless
1201 * group supports reporting fid.
1202 * inode events are not supported on a mount mark, because they do not
1203 * carry enough information (i.e. path) to be filtered by mount point.
1205 fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
1206 if (mask & FANOTIFY_INODE_EVENTS &&
1207 (!fid_mode || mark_type == FAN_MARK_MOUNT))
1210 if (flags & FAN_MARK_FLUSH) {
1212 if (mark_type == FAN_MARK_MOUNT)
1213 fsnotify_clear_vfsmount_marks_by_group(group);
1214 else if (mark_type == FAN_MARK_FILESYSTEM)
1215 fsnotify_clear_sb_marks_by_group(group);
1217 fsnotify_clear_inode_marks_by_group(group);
1221 ret = fanotify_find_path(dfd, pathname, &path, flags,
1222 (mask & ALL_FSNOTIFY_EVENTS), obj_type);
1226 if (flags & FAN_MARK_ADD) {
1227 ret = fanotify_events_supported(&path, mask);
1229 goto path_put_and_out;
1233 ret = fanotify_test_fid(&path, &__fsid);
1235 goto path_put_and_out;
1240 /* inode held in place by reference to path; group by fget on fd */
1241 if (mark_type == FAN_MARK_INODE)
1242 inode = path.dentry->d_inode;
1246 /* Mask out FAN_EVENT_ON_CHILD flag for sb/mount/non-dir marks */
1247 if (mnt || !S_ISDIR(inode->i_mode)) {
1248 mask &= ~FAN_EVENT_ON_CHILD;
1249 umask = FAN_EVENT_ON_CHILD;
1251 * If group needs to report parent fid, register for getting
1252 * events with parent/name info for non-directory.
1254 if ((fid_mode & FAN_REPORT_DIR_FID) &&
1255 (flags & FAN_MARK_ADD) && !ignored)
1256 mask |= FAN_EVENT_ON_CHILD;
1259 /* create/update an inode mark */
1260 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
1262 if (mark_type == FAN_MARK_MOUNT)
1263 ret = fanotify_add_vfsmount_mark(group, mnt, mask,
1265 else if (mark_type == FAN_MARK_FILESYSTEM)
1266 ret = fanotify_add_sb_mark(group, mnt->mnt_sb, mask,
1269 ret = fanotify_add_inode_mark(group, inode, mask,
1272 case FAN_MARK_REMOVE:
1273 if (mark_type == FAN_MARK_MOUNT)
1274 ret = fanotify_remove_vfsmount_mark(group, mnt, mask,
1276 else if (mark_type == FAN_MARK_FILESYSTEM)
1277 ret = fanotify_remove_sb_mark(group, mnt->mnt_sb, mask,
1280 ret = fanotify_remove_inode_mark(group, inode, mask,
1294 #ifndef CONFIG_ARCH_SPLIT_ARG64
1295 SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
1296 __u64, mask, int, dfd,
1297 const char __user *, pathname)
1299 return do_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname);
1303 #if defined(CONFIG_ARCH_SPLIT_ARG64) || defined(CONFIG_COMPAT)
1304 SYSCALL32_DEFINE6(fanotify_mark,
1305 int, fanotify_fd, unsigned int, flags,
1306 SC_ARG64(mask), int, dfd,
1307 const char __user *, pathname)
1309 return do_fanotify_mark(fanotify_fd, flags, SC_VAL64(__u64, mask),
1315 * fanotify_user_setup - Our initialization function. Note that we cannot return
1316 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
1317 * must result in panic().
1319 static int __init fanotify_user_setup(void)
1321 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 10);
1322 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9);
1324 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark,
1325 SLAB_PANIC|SLAB_ACCOUNT);
1326 fanotify_fid_event_cachep = KMEM_CACHE(fanotify_fid_event,
1328 fanotify_path_event_cachep = KMEM_CACHE(fanotify_path_event,
1330 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
1331 fanotify_perm_event_cachep =
1332 KMEM_CACHE(fanotify_perm_event, SLAB_PANIC);
1337 device_initcall(fanotify_user_setup);