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_OLD_DEFAULT_MAX_MARKS 8192
31 #define FANOTIFY_DEFAULT_MAX_GROUPS 128
34 * Legacy fanotify marks limits (8192) is per group and we introduced a tunable
35 * limit of marks per user, similar to inotify. Effectively, the legacy limit
36 * of fanotify marks per user is <max marks per group> * <max groups per user>.
37 * This default limit (1M) also happens to match the increased limit of inotify
38 * max_user_watches since v5.10.
40 #define FANOTIFY_DEFAULT_MAX_USER_MARKS \
41 (FANOTIFY_OLD_DEFAULT_MAX_MARKS * FANOTIFY_DEFAULT_MAX_GROUPS)
44 * Most of the memory cost of adding an inode mark is pinning the marked inode.
45 * The size of the filesystem inode struct is not uniform across filesystems,
46 * so double the size of a VFS inode is used as a conservative approximation.
48 #define INODE_MARK_COST (2 * sizeof(struct inode))
50 /* configurable via /proc/sys/fs/fanotify/ */
51 static int fanotify_max_queued_events __read_mostly;
55 #include <linux/sysctl.h>
57 struct ctl_table fanotify_table[] = {
59 .procname = "max_user_groups",
60 .data = &init_user_ns.ucount_max[UCOUNT_FANOTIFY_GROUPS],
61 .maxlen = sizeof(int),
63 .proc_handler = proc_dointvec_minmax,
64 .extra1 = SYSCTL_ZERO,
67 .procname = "max_user_marks",
68 .data = &init_user_ns.ucount_max[UCOUNT_FANOTIFY_MARKS],
69 .maxlen = sizeof(int),
71 .proc_handler = proc_dointvec_minmax,
72 .extra1 = SYSCTL_ZERO,
75 .procname = "max_queued_events",
76 .data = &fanotify_max_queued_events,
77 .maxlen = sizeof(int),
79 .proc_handler = proc_dointvec_minmax,
84 #endif /* CONFIG_SYSCTL */
87 * All flags that may be specified in parameter event_f_flags of fanotify_init.
89 * Internal and external open flags are stored together in field f_flags of
90 * struct file. Only external open flags shall be allowed in event_f_flags.
91 * Internal flags like FMODE_NONOTIFY, FMODE_EXEC, FMODE_NOCMTIME shall be
94 #define FANOTIFY_INIT_ALL_EVENT_F_BITS ( \
95 O_ACCMODE | O_APPEND | O_NONBLOCK | \
96 __O_SYNC | O_DSYNC | O_CLOEXEC | \
97 O_LARGEFILE | O_NOATIME )
99 extern const struct fsnotify_ops fanotify_fsnotify_ops;
101 struct kmem_cache *fanotify_mark_cache __read_mostly;
102 struct kmem_cache *fanotify_fid_event_cachep __read_mostly;
103 struct kmem_cache *fanotify_path_event_cachep __read_mostly;
104 struct kmem_cache *fanotify_perm_event_cachep __read_mostly;
106 #define FANOTIFY_EVENT_ALIGN 4
107 #define FANOTIFY_INFO_HDR_LEN \
108 (sizeof(struct fanotify_event_info_fid) + sizeof(struct file_handle))
110 static int fanotify_fid_info_len(int fh_len, int name_len)
112 int info_len = fh_len;
115 info_len += name_len + 1;
117 return roundup(FANOTIFY_INFO_HDR_LEN + info_len, FANOTIFY_EVENT_ALIGN);
120 static int fanotify_event_info_len(unsigned int fid_mode,
121 struct fanotify_event *event)
123 struct fanotify_info *info = fanotify_event_info(event);
124 int dir_fh_len = fanotify_event_dir_fh_len(event);
125 int fh_len = fanotify_event_object_fh_len(event);
130 info_len += fanotify_fid_info_len(dir_fh_len, info->name_len);
131 } else if ((fid_mode & FAN_REPORT_NAME) && (event->mask & FAN_ONDIR)) {
133 * With group flag FAN_REPORT_NAME, if name was not recorded in
134 * event on a directory, we will report the name ".".
140 info_len += fanotify_fid_info_len(fh_len, dot_len);
146 * Remove an hashed event from merge hash table.
148 static void fanotify_unhash_event(struct fsnotify_group *group,
149 struct fanotify_event *event)
151 assert_spin_locked(&group->notification_lock);
153 pr_debug("%s: group=%p event=%p bucket=%u\n", __func__,
154 group, event, fanotify_event_hash_bucket(group, event));
156 if (WARN_ON_ONCE(hlist_unhashed(&event->merge_list)))
159 hlist_del_init(&event->merge_list);
163 * Get an fanotify notification event if one exists and is small
164 * enough to fit in "count". Return an error pointer if the count
165 * is not large enough. When permission event is dequeued, its state is
166 * updated accordingly.
168 static struct fanotify_event *get_one_event(struct fsnotify_group *group,
171 size_t event_size = FAN_EVENT_METADATA_LEN;
172 struct fanotify_event *event = NULL;
173 struct fsnotify_event *fsn_event;
174 unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
176 pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
178 spin_lock(&group->notification_lock);
179 fsn_event = fsnotify_peek_first_event(group);
183 event = FANOTIFY_E(fsn_event);
185 event_size += fanotify_event_info_len(fid_mode, event);
187 if (event_size > count) {
188 event = ERR_PTR(-EINVAL);
193 * Held the notification_lock the whole time, so this is the
194 * same event we peeked above.
196 fsnotify_remove_first_event(group);
197 if (fanotify_is_perm_event(event->mask))
198 FANOTIFY_PERM(event)->state = FAN_EVENT_REPORTED;
199 if (fanotify_is_hashed_event(event->mask))
200 fanotify_unhash_event(group, event);
202 spin_unlock(&group->notification_lock);
206 static int create_fd(struct fsnotify_group *group, struct path *path,
210 struct file *new_file;
212 client_fd = get_unused_fd_flags(group->fanotify_data.f_flags);
217 * we need a new file handle for the userspace program so it can read even if it was
218 * originally opened O_WRONLY.
220 new_file = dentry_open(path,
221 group->fanotify_data.f_flags | FMODE_NONOTIFY,
223 if (IS_ERR(new_file)) {
225 * we still send an event even if we can't open the file. this
226 * can happen when say tasks are gone and we try to open their
227 * /proc files or we try to open a WRONLY file like in sysfs
228 * we just send the errno to userspace since there isn't much
231 put_unused_fd(client_fd);
232 client_fd = PTR_ERR(new_file);
241 * Finish processing of permission event by setting it to ANSWERED state and
242 * drop group->notification_lock.
244 static void finish_permission_event(struct fsnotify_group *group,
245 struct fanotify_perm_event *event,
246 unsigned int response)
247 __releases(&group->notification_lock)
249 bool destroy = false;
251 assert_spin_locked(&group->notification_lock);
252 event->response = response;
253 if (event->state == FAN_EVENT_CANCELED)
256 event->state = FAN_EVENT_ANSWERED;
257 spin_unlock(&group->notification_lock);
259 fsnotify_destroy_event(group, &event->fae.fse);
262 static int process_access_response(struct fsnotify_group *group,
263 struct fanotify_response *response_struct)
265 struct fanotify_perm_event *event;
266 int fd = response_struct->fd;
267 int response = response_struct->response;
269 pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
272 * make sure the response is valid, if invalid we do nothing and either
273 * userspace can send a valid response or we will clean it up after the
276 switch (response & ~FAN_AUDIT) {
287 if ((response & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT))
290 spin_lock(&group->notification_lock);
291 list_for_each_entry(event, &group->fanotify_data.access_list,
296 list_del_init(&event->fae.fse.list);
297 finish_permission_event(group, event, response);
298 wake_up(&group->fanotify_data.access_waitq);
301 spin_unlock(&group->notification_lock);
306 static int copy_info_to_user(__kernel_fsid_t *fsid, struct fanotify_fh *fh,
307 int info_type, const char *name, size_t name_len,
308 char __user *buf, size_t count)
310 struct fanotify_event_info_fid info = { };
311 struct file_handle handle = { };
312 unsigned char bounce[FANOTIFY_INLINE_FH_LEN], *fh_buf;
313 size_t fh_len = fh ? fh->len : 0;
314 size_t info_len = fanotify_fid_info_len(fh_len, name_len);
315 size_t len = info_len;
317 pr_debug("%s: fh_len=%zu name_len=%zu, info_len=%zu, count=%zu\n",
318 __func__, fh_len, name_len, info_len, count);
323 if (WARN_ON_ONCE(len < sizeof(info) || len > count))
327 * Copy event info fid header followed by variable sized file handle
328 * and optionally followed by variable sized filename.
331 case FAN_EVENT_INFO_TYPE_FID:
332 case FAN_EVENT_INFO_TYPE_DFID:
333 if (WARN_ON_ONCE(name_len))
336 case FAN_EVENT_INFO_TYPE_DFID_NAME:
337 if (WARN_ON_ONCE(!name || !name_len))
344 info.hdr.info_type = info_type;
347 if (copy_to_user(buf, &info, sizeof(info)))
352 if (WARN_ON_ONCE(len < sizeof(handle)))
355 handle.handle_type = fh->type;
356 handle.handle_bytes = fh_len;
357 if (copy_to_user(buf, &handle, sizeof(handle)))
360 buf += sizeof(handle);
361 len -= sizeof(handle);
362 if (WARN_ON_ONCE(len < fh_len))
366 * For an inline fh and inline file name, copy through stack to exclude
367 * the copy from usercopy hardening protections.
369 fh_buf = fanotify_fh_buf(fh);
370 if (fh_len <= FANOTIFY_INLINE_FH_LEN) {
371 memcpy(bounce, fh_buf, fh_len);
374 if (copy_to_user(buf, fh_buf, fh_len))
381 /* Copy the filename with terminating null */
383 if (WARN_ON_ONCE(len < name_len))
386 if (copy_to_user(buf, name, name_len))
394 WARN_ON_ONCE(len < 0 || len >= FANOTIFY_EVENT_ALIGN);
395 if (len > 0 && clear_user(buf, len))
401 static ssize_t copy_event_to_user(struct fsnotify_group *group,
402 struct fanotify_event *event,
403 char __user *buf, size_t count)
405 struct fanotify_event_metadata metadata;
406 struct path *path = fanotify_event_path(event);
407 struct fanotify_info *info = fanotify_event_info(event);
408 unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
409 struct file *f = NULL;
410 int ret, fd = FAN_NOFD;
413 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
415 metadata.event_len = FAN_EVENT_METADATA_LEN +
416 fanotify_event_info_len(fid_mode, event);
417 metadata.metadata_len = FAN_EVENT_METADATA_LEN;
418 metadata.vers = FANOTIFY_METADATA_VERSION;
419 metadata.reserved = 0;
420 metadata.mask = event->mask & FANOTIFY_OUTGOING_EVENTS;
421 metadata.pid = pid_vnr(event->pid);
423 * For an unprivileged listener, event->pid can be used to identify the
424 * events generated by the listener process itself, without disclosing
425 * the pids of other processes.
427 if (!capable(CAP_SYS_ADMIN) &&
428 task_tgid(current) != event->pid)
431 if (path && path->mnt && path->dentry) {
432 fd = create_fd(group, path, &f);
440 * Sanity check copy size in case get_one_event() and
441 * event_len sizes ever get out of sync.
443 if (WARN_ON_ONCE(metadata.event_len > count))
446 if (copy_to_user(buf, &metadata, FAN_EVENT_METADATA_LEN))
449 buf += FAN_EVENT_METADATA_LEN;
450 count -= FAN_EVENT_METADATA_LEN;
452 if (fanotify_is_perm_event(event->mask))
453 FANOTIFY_PERM(event)->fd = fd;
458 /* Event info records order is: dir fid + name, child fid */
459 if (fanotify_event_dir_fh_len(event)) {
460 info_type = info->name_len ? FAN_EVENT_INFO_TYPE_DFID_NAME :
461 FAN_EVENT_INFO_TYPE_DFID;
462 ret = copy_info_to_user(fanotify_event_fsid(event),
463 fanotify_info_dir_fh(info),
464 info_type, fanotify_info_name(info),
465 info->name_len, buf, count);
473 if (fanotify_event_object_fh_len(event)) {
474 const char *dot = NULL;
477 if (fid_mode == FAN_REPORT_FID || info_type) {
479 * With only group flag FAN_REPORT_FID only type FID is
480 * reported. Second info record type is always FID.
482 info_type = FAN_EVENT_INFO_TYPE_FID;
483 } else if ((fid_mode & FAN_REPORT_NAME) &&
484 (event->mask & FAN_ONDIR)) {
486 * With group flag FAN_REPORT_NAME, if name was not
487 * recorded in an event on a directory, report the
488 * name "." with info type DFID_NAME.
490 info_type = FAN_EVENT_INFO_TYPE_DFID_NAME;
493 } else if ((event->mask & ALL_FSNOTIFY_DIRENT_EVENTS) ||
494 (event->mask & FAN_ONDIR)) {
496 * With group flag FAN_REPORT_DIR_FID, a single info
497 * record has type DFID for directory entry modification
498 * event and for event on a directory.
500 info_type = FAN_EVENT_INFO_TYPE_DFID;
503 * With group flags FAN_REPORT_DIR_FID|FAN_REPORT_FID,
504 * a single info record has type FID for event on a
505 * non-directory, when there is no directory to report.
506 * For example, on FAN_DELETE_SELF event.
508 info_type = FAN_EVENT_INFO_TYPE_FID;
511 ret = copy_info_to_user(fanotify_event_fsid(event),
512 fanotify_event_object_fh(event),
513 info_type, dot, dot_len, buf, count);
521 return metadata.event_len;
524 if (fd != FAN_NOFD) {
531 /* intofiy userspace file descriptor functions */
532 static __poll_t fanotify_poll(struct file *file, poll_table *wait)
534 struct fsnotify_group *group = file->private_data;
537 poll_wait(file, &group->notification_waitq, wait);
538 spin_lock(&group->notification_lock);
539 if (!fsnotify_notify_queue_is_empty(group))
540 ret = EPOLLIN | EPOLLRDNORM;
541 spin_unlock(&group->notification_lock);
546 static ssize_t fanotify_read(struct file *file, char __user *buf,
547 size_t count, loff_t *pos)
549 struct fsnotify_group *group;
550 struct fanotify_event *event;
553 DEFINE_WAIT_FUNC(wait, woken_wake_function);
556 group = file->private_data;
558 pr_debug("%s: group=%p\n", __func__, group);
560 add_wait_queue(&group->notification_waitq, &wait);
563 * User can supply arbitrarily large buffer. Avoid softlockups
564 * in case there are lots of available events.
567 event = get_one_event(group, count);
569 ret = PTR_ERR(event);
575 if (file->f_flags & O_NONBLOCK)
579 if (signal_pending(current))
585 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
589 ret = copy_event_to_user(group, event, buf, count);
590 if (unlikely(ret == -EOPENSTALE)) {
592 * We cannot report events with stale fd so drop it.
593 * Setting ret to 0 will continue the event loop and
594 * do the right thing if there are no more events to
595 * read (i.e. return bytes read, -EAGAIN or wait).
601 * Permission events get queued to wait for response. Other
602 * events can be destroyed now.
604 if (!fanotify_is_perm_event(event->mask)) {
605 fsnotify_destroy_event(group, &event->fse);
608 spin_lock(&group->notification_lock);
609 finish_permission_event(group,
610 FANOTIFY_PERM(event), FAN_DENY);
611 wake_up(&group->fanotify_data.access_waitq);
613 spin_lock(&group->notification_lock);
614 list_add_tail(&event->fse.list,
615 &group->fanotify_data.access_list);
616 spin_unlock(&group->notification_lock);
624 remove_wait_queue(&group->notification_waitq, &wait);
626 if (start != buf && ret != -EFAULT)
631 static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
633 struct fanotify_response response = { .fd = -1, .response = -1 };
634 struct fsnotify_group *group;
637 if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
640 group = file->private_data;
642 if (count < sizeof(response))
645 count = sizeof(response);
647 pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
649 if (copy_from_user(&response, buf, count))
652 ret = process_access_response(group, &response);
659 static int fanotify_release(struct inode *ignored, struct file *file)
661 struct fsnotify_group *group = file->private_data;
662 struct fsnotify_event *fsn_event;
665 * Stop new events from arriving in the notification queue. since
666 * userspace cannot use fanotify fd anymore, no event can enter or
667 * leave access_list by now either.
669 fsnotify_group_stop_queueing(group);
672 * Process all permission events on access_list and notification queue
673 * and simulate reply from userspace.
675 spin_lock(&group->notification_lock);
676 while (!list_empty(&group->fanotify_data.access_list)) {
677 struct fanotify_perm_event *event;
679 event = list_first_entry(&group->fanotify_data.access_list,
680 struct fanotify_perm_event, fae.fse.list);
681 list_del_init(&event->fae.fse.list);
682 finish_permission_event(group, event, FAN_ALLOW);
683 spin_lock(&group->notification_lock);
687 * Destroy all non-permission events. For permission events just
688 * dequeue them and set the response. They will be freed once the
689 * response is consumed and fanotify_get_response() returns.
691 while ((fsn_event = fsnotify_remove_first_event(group))) {
692 struct fanotify_event *event = FANOTIFY_E(fsn_event);
694 if (!(event->mask & FANOTIFY_PERM_EVENTS)) {
695 spin_unlock(&group->notification_lock);
696 fsnotify_destroy_event(group, fsn_event);
698 finish_permission_event(group, FANOTIFY_PERM(event),
701 spin_lock(&group->notification_lock);
703 spin_unlock(&group->notification_lock);
705 /* Response for all permission events it set, wakeup waiters */
706 wake_up(&group->fanotify_data.access_waitq);
708 /* matches the fanotify_init->fsnotify_alloc_group */
709 fsnotify_destroy_group(group);
714 static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
716 struct fsnotify_group *group;
717 struct fsnotify_event *fsn_event;
722 group = file->private_data;
724 p = (void __user *) arg;
728 spin_lock(&group->notification_lock);
729 list_for_each_entry(fsn_event, &group->notification_list, list)
730 send_len += FAN_EVENT_METADATA_LEN;
731 spin_unlock(&group->notification_lock);
732 ret = put_user(send_len, (int __user *) p);
739 static const struct file_operations fanotify_fops = {
740 .show_fdinfo = fanotify_show_fdinfo,
741 .poll = fanotify_poll,
742 .read = fanotify_read,
743 .write = fanotify_write,
745 .release = fanotify_release,
746 .unlocked_ioctl = fanotify_ioctl,
747 .compat_ioctl = compat_ptr_ioctl,
748 .llseek = noop_llseek,
751 static int fanotify_find_path(int dfd, const char __user *filename,
752 struct path *path, unsigned int flags, __u64 mask,
753 unsigned int obj_type)
757 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
758 dfd, filename, flags);
760 if (filename == NULL) {
761 struct fd f = fdget(dfd);
768 if ((flags & FAN_MARK_ONLYDIR) &&
769 !(S_ISDIR(file_inode(f.file)->i_mode))) {
774 *path = f.file->f_path;
778 unsigned int lookup_flags = 0;
780 if (!(flags & FAN_MARK_DONT_FOLLOW))
781 lookup_flags |= LOOKUP_FOLLOW;
782 if (flags & FAN_MARK_ONLYDIR)
783 lookup_flags |= LOOKUP_DIRECTORY;
785 ret = user_path_at(dfd, filename, lookup_flags, path);
790 /* you can only watch an inode if you have read permissions on it */
791 ret = path_permission(path, MAY_READ);
797 ret = security_path_notify(path, mask, obj_type);
805 static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
806 __u32 mask, unsigned int flags,
807 __u32 umask, int *destroy)
811 /* umask bits cannot be removed by user */
813 spin_lock(&fsn_mark->lock);
814 if (!(flags & FAN_MARK_IGNORED_MASK)) {
815 oldmask = fsn_mark->mask;
816 fsn_mark->mask &= ~mask;
818 fsn_mark->ignored_mask &= ~mask;
821 * We need to keep the mark around even if remaining mask cannot
822 * result in any events (e.g. mask == FAN_ONDIR) to support incremenal
823 * changes to the mask.
824 * Destroy mark when only umask bits remain.
826 *destroy = !((fsn_mark->mask | fsn_mark->ignored_mask) & ~umask);
827 spin_unlock(&fsn_mark->lock);
829 return mask & oldmask;
832 static int fanotify_remove_mark(struct fsnotify_group *group,
833 fsnotify_connp_t *connp, __u32 mask,
834 unsigned int flags, __u32 umask)
836 struct fsnotify_mark *fsn_mark = NULL;
840 mutex_lock(&group->mark_mutex);
841 fsn_mark = fsnotify_find_mark(connp, group);
843 mutex_unlock(&group->mark_mutex);
847 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
848 umask, &destroy_mark);
849 if (removed & fsnotify_conn_mask(fsn_mark->connector))
850 fsnotify_recalc_mask(fsn_mark->connector);
852 fsnotify_detach_mark(fsn_mark);
853 mutex_unlock(&group->mark_mutex);
855 fsnotify_free_mark(fsn_mark);
857 /* matches the fsnotify_find_mark() */
858 fsnotify_put_mark(fsn_mark);
862 static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
863 struct vfsmount *mnt, __u32 mask,
864 unsigned int flags, __u32 umask)
866 return fanotify_remove_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
870 static int fanotify_remove_sb_mark(struct fsnotify_group *group,
871 struct super_block *sb, __u32 mask,
872 unsigned int flags, __u32 umask)
874 return fanotify_remove_mark(group, &sb->s_fsnotify_marks, mask,
878 static int fanotify_remove_inode_mark(struct fsnotify_group *group,
879 struct inode *inode, __u32 mask,
880 unsigned int flags, __u32 umask)
882 return fanotify_remove_mark(group, &inode->i_fsnotify_marks, mask,
886 static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
892 spin_lock(&fsn_mark->lock);
893 if (!(flags & FAN_MARK_IGNORED_MASK)) {
894 oldmask = fsn_mark->mask;
895 fsn_mark->mask |= mask;
897 fsn_mark->ignored_mask |= mask;
898 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
899 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
901 spin_unlock(&fsn_mark->lock);
903 return mask & ~oldmask;
906 static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
907 fsnotify_connp_t *connp,
909 __kernel_fsid_t *fsid)
911 struct ucounts *ucounts = group->fanotify_data.ucounts;
912 struct fsnotify_mark *mark;
916 * Enforce per user marks limits per user in all containing user ns.
917 * A group with FAN_UNLIMITED_MARKS does not contribute to mark count
918 * in the limited groups account.
920 if (!FAN_GROUP_FLAG(group, FAN_UNLIMITED_MARKS) &&
921 !inc_ucount(ucounts->ns, ucounts->uid, UCOUNT_FANOTIFY_MARKS))
922 return ERR_PTR(-ENOSPC);
924 mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
927 goto out_dec_ucounts;
930 fsnotify_init_mark(mark, group);
931 ret = fsnotify_add_mark_locked(mark, connp, type, 0, fsid);
933 fsnotify_put_mark(mark);
934 goto out_dec_ucounts;
940 if (!FAN_GROUP_FLAG(group, FAN_UNLIMITED_MARKS))
941 dec_ucount(ucounts, UCOUNT_FANOTIFY_MARKS);
946 static int fanotify_add_mark(struct fsnotify_group *group,
947 fsnotify_connp_t *connp, unsigned int type,
948 __u32 mask, unsigned int flags,
949 __kernel_fsid_t *fsid)
951 struct fsnotify_mark *fsn_mark;
954 mutex_lock(&group->mark_mutex);
955 fsn_mark = fsnotify_find_mark(connp, group);
957 fsn_mark = fanotify_add_new_mark(group, connp, type, fsid);
958 if (IS_ERR(fsn_mark)) {
959 mutex_unlock(&group->mark_mutex);
960 return PTR_ERR(fsn_mark);
963 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
964 if (added & ~fsnotify_conn_mask(fsn_mark->connector))
965 fsnotify_recalc_mask(fsn_mark->connector);
966 mutex_unlock(&group->mark_mutex);
968 fsnotify_put_mark(fsn_mark);
972 static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
973 struct vfsmount *mnt, __u32 mask,
974 unsigned int flags, __kernel_fsid_t *fsid)
976 return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
977 FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags, fsid);
980 static int fanotify_add_sb_mark(struct fsnotify_group *group,
981 struct super_block *sb, __u32 mask,
982 unsigned int flags, __kernel_fsid_t *fsid)
984 return fanotify_add_mark(group, &sb->s_fsnotify_marks,
985 FSNOTIFY_OBJ_TYPE_SB, mask, flags, fsid);
988 static int fanotify_add_inode_mark(struct fsnotify_group *group,
989 struct inode *inode, __u32 mask,
990 unsigned int flags, __kernel_fsid_t *fsid)
992 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
995 * If some other task has this inode open for write we should not add
996 * an ignored mark, unless that ignored mark is supposed to survive
997 * modification changes anyway.
999 if ((flags & FAN_MARK_IGNORED_MASK) &&
1000 !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
1001 inode_is_open_for_write(inode))
1004 return fanotify_add_mark(group, &inode->i_fsnotify_marks,
1005 FSNOTIFY_OBJ_TYPE_INODE, mask, flags, fsid);
1008 static struct fsnotify_event *fanotify_alloc_overflow_event(void)
1010 struct fanotify_event *oevent;
1012 oevent = kmalloc(sizeof(*oevent), GFP_KERNEL_ACCOUNT);
1016 fanotify_init_event(oevent, 0, FS_Q_OVERFLOW);
1017 oevent->type = FANOTIFY_EVENT_TYPE_OVERFLOW;
1019 return &oevent->fse;
1022 static struct hlist_head *fanotify_alloc_merge_hash(void)
1024 struct hlist_head *hash;
1026 hash = kmalloc(sizeof(struct hlist_head) << FANOTIFY_HTABLE_BITS,
1027 GFP_KERNEL_ACCOUNT);
1031 __hash_init(hash, FANOTIFY_HTABLE_SIZE);
1036 /* fanotify syscalls */
1037 SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
1039 struct fsnotify_group *group;
1041 unsigned int fid_mode = flags & FANOTIFY_FID_BITS;
1042 unsigned int class = flags & FANOTIFY_CLASS_BITS;
1044 pr_debug("%s: flags=%x event_f_flags=%x\n",
1045 __func__, flags, event_f_flags);
1047 if (!capable(CAP_SYS_ADMIN)) {
1049 * An unprivileged user can setup an fanotify group with
1050 * limited functionality - an unprivileged group is limited to
1051 * notification events with file handles and it cannot use
1052 * unlimited queue/marks.
1054 if ((flags & FANOTIFY_ADMIN_INIT_FLAGS) || !fid_mode)
1058 #ifdef CONFIG_AUDITSYSCALL
1059 if (flags & ~(FANOTIFY_INIT_FLAGS | FAN_ENABLE_AUDIT))
1061 if (flags & ~FANOTIFY_INIT_FLAGS)
1065 if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS)
1068 switch (event_f_flags & O_ACCMODE) {
1077 if (fid_mode && class != FAN_CLASS_NOTIF)
1081 * Child name is reported with parent fid so requires dir fid.
1082 * We can report both child fid and dir fid with or without name.
1084 if ((fid_mode & FAN_REPORT_NAME) && !(fid_mode & FAN_REPORT_DIR_FID))
1087 f_flags = O_RDWR | FMODE_NONOTIFY;
1088 if (flags & FAN_CLOEXEC)
1089 f_flags |= O_CLOEXEC;
1090 if (flags & FAN_NONBLOCK)
1091 f_flags |= O_NONBLOCK;
1093 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
1094 group = fsnotify_alloc_user_group(&fanotify_fsnotify_ops);
1095 if (IS_ERR(group)) {
1096 return PTR_ERR(group);
1099 /* Enforce groups limits per user in all containing user ns */
1100 group->fanotify_data.ucounts = inc_ucount(current_user_ns(),
1102 UCOUNT_FANOTIFY_GROUPS);
1103 if (!group->fanotify_data.ucounts) {
1105 goto out_destroy_group;
1108 group->fanotify_data.flags = flags;
1109 group->memcg = get_mem_cgroup_from_mm(current->mm);
1111 group->fanotify_data.merge_hash = fanotify_alloc_merge_hash();
1112 if (!group->fanotify_data.merge_hash) {
1114 goto out_destroy_group;
1117 group->overflow_event = fanotify_alloc_overflow_event();
1118 if (unlikely(!group->overflow_event)) {
1120 goto out_destroy_group;
1123 if (force_o_largefile())
1124 event_f_flags |= O_LARGEFILE;
1125 group->fanotify_data.f_flags = event_f_flags;
1126 init_waitqueue_head(&group->fanotify_data.access_waitq);
1127 INIT_LIST_HEAD(&group->fanotify_data.access_list);
1129 case FAN_CLASS_NOTIF:
1130 group->priority = FS_PRIO_0;
1132 case FAN_CLASS_CONTENT:
1133 group->priority = FS_PRIO_1;
1135 case FAN_CLASS_PRE_CONTENT:
1136 group->priority = FS_PRIO_2;
1140 goto out_destroy_group;
1143 if (flags & FAN_UNLIMITED_QUEUE) {
1145 if (!capable(CAP_SYS_ADMIN))
1146 goto out_destroy_group;
1147 group->max_events = UINT_MAX;
1149 group->max_events = fanotify_max_queued_events;
1152 if (flags & FAN_UNLIMITED_MARKS) {
1154 if (!capable(CAP_SYS_ADMIN))
1155 goto out_destroy_group;
1158 if (flags & FAN_ENABLE_AUDIT) {
1160 if (!capable(CAP_AUDIT_WRITE))
1161 goto out_destroy_group;
1164 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
1166 goto out_destroy_group;
1171 fsnotify_destroy_group(group);
1175 /* Check if filesystem can encode a unique fid */
1176 static int fanotify_test_fid(struct path *path, __kernel_fsid_t *fsid)
1178 __kernel_fsid_t root_fsid;
1182 * Make sure path is not in filesystem with zero fsid (e.g. tmpfs).
1184 err = vfs_get_fsid(path->dentry, fsid);
1188 if (!fsid->val[0] && !fsid->val[1])
1192 * Make sure path is not inside a filesystem subvolume (e.g. btrfs)
1193 * which uses a different fsid than sb root.
1195 err = vfs_get_fsid(path->dentry->d_sb->s_root, &root_fsid);
1199 if (root_fsid.val[0] != fsid->val[0] ||
1200 root_fsid.val[1] != fsid->val[1])
1204 * We need to make sure that the file system supports at least
1205 * encoding a file handle so user can use name_to_handle_at() to
1206 * compare fid returned with event to the file handle of watched
1207 * objects. However, name_to_handle_at() requires that the
1208 * filesystem also supports decoding file handles.
1210 if (!path->dentry->d_sb->s_export_op ||
1211 !path->dentry->d_sb->s_export_op->fh_to_dentry)
1217 static int fanotify_events_supported(struct path *path, __u64 mask)
1220 * Some filesystems such as 'proc' acquire unusual locks when opening
1221 * files. For them fanotify permission events have high chances of
1222 * deadlocking the system - open done when reporting fanotify event
1223 * blocks on this "unusual" lock while another process holding the lock
1224 * waits for fanotify permission event to be answered. Just disallow
1225 * permission events for such filesystems.
1227 if (mask & FANOTIFY_PERM_EVENTS &&
1228 path->mnt->mnt_sb->s_type->fs_flags & FS_DISALLOW_NOTIFY_PERM)
1233 static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
1234 int dfd, const char __user *pathname)
1236 struct inode *inode = NULL;
1237 struct vfsmount *mnt = NULL;
1238 struct fsnotify_group *group;
1241 __kernel_fsid_t __fsid, *fsid = NULL;
1242 u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS;
1243 unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS;
1244 bool ignored = flags & FAN_MARK_IGNORED_MASK;
1245 unsigned int obj_type, fid_mode;
1249 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
1250 __func__, fanotify_fd, flags, dfd, pathname, mask);
1252 /* we only use the lower 32 bits as of right now. */
1253 if (mask & ((__u64)0xffffffff << 32))
1256 if (flags & ~FANOTIFY_MARK_FLAGS)
1259 switch (mark_type) {
1260 case FAN_MARK_INODE:
1261 obj_type = FSNOTIFY_OBJ_TYPE_INODE;
1263 case FAN_MARK_MOUNT:
1264 obj_type = FSNOTIFY_OBJ_TYPE_VFSMOUNT;
1266 case FAN_MARK_FILESYSTEM:
1267 obj_type = FSNOTIFY_OBJ_TYPE_SB;
1273 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
1275 case FAN_MARK_REMOVE:
1279 case FAN_MARK_FLUSH:
1280 if (flags & ~(FANOTIFY_MARK_TYPE_BITS | FAN_MARK_FLUSH))
1287 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
1288 valid_mask |= FANOTIFY_PERM_EVENTS;
1290 if (mask & ~valid_mask)
1293 /* Event flags (ONDIR, ON_CHILD) are meaningless in ignored mask */
1295 mask &= ~FANOTIFY_EVENT_FLAGS;
1297 f = fdget(fanotify_fd);
1298 if (unlikely(!f.file))
1301 /* verify that this is indeed an fanotify instance */
1303 if (unlikely(f.file->f_op != &fanotify_fops))
1305 group = f.file->private_data;
1308 * An unprivileged user is not allowed to watch a mount point nor
1312 if (!capable(CAP_SYS_ADMIN) &&
1313 mark_type != FAN_MARK_INODE)
1317 * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
1318 * allowed to set permissions events.
1321 if (mask & FANOTIFY_PERM_EVENTS &&
1322 group->priority == FS_PRIO_0)
1326 * Events with data type inode do not carry enough information to report
1327 * event->fd, so we do not allow setting a mask for inode events unless
1328 * group supports reporting fid.
1329 * inode events are not supported on a mount mark, because they do not
1330 * carry enough information (i.e. path) to be filtered by mount point.
1332 fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
1333 if (mask & FANOTIFY_INODE_EVENTS &&
1334 (!fid_mode || mark_type == FAN_MARK_MOUNT))
1337 if (flags & FAN_MARK_FLUSH) {
1339 if (mark_type == FAN_MARK_MOUNT)
1340 fsnotify_clear_vfsmount_marks_by_group(group);
1341 else if (mark_type == FAN_MARK_FILESYSTEM)
1342 fsnotify_clear_sb_marks_by_group(group);
1344 fsnotify_clear_inode_marks_by_group(group);
1348 ret = fanotify_find_path(dfd, pathname, &path, flags,
1349 (mask & ALL_FSNOTIFY_EVENTS), obj_type);
1353 if (flags & FAN_MARK_ADD) {
1354 ret = fanotify_events_supported(&path, mask);
1356 goto path_put_and_out;
1360 ret = fanotify_test_fid(&path, &__fsid);
1362 goto path_put_and_out;
1367 /* inode held in place by reference to path; group by fget on fd */
1368 if (mark_type == FAN_MARK_INODE)
1369 inode = path.dentry->d_inode;
1373 /* Mask out FAN_EVENT_ON_CHILD flag for sb/mount/non-dir marks */
1374 if (mnt || !S_ISDIR(inode->i_mode)) {
1375 mask &= ~FAN_EVENT_ON_CHILD;
1376 umask = FAN_EVENT_ON_CHILD;
1378 * If group needs to report parent fid, register for getting
1379 * events with parent/name info for non-directory.
1381 if ((fid_mode & FAN_REPORT_DIR_FID) &&
1382 (flags & FAN_MARK_ADD) && !ignored)
1383 mask |= FAN_EVENT_ON_CHILD;
1386 /* create/update an inode mark */
1387 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
1389 if (mark_type == FAN_MARK_MOUNT)
1390 ret = fanotify_add_vfsmount_mark(group, mnt, mask,
1392 else if (mark_type == FAN_MARK_FILESYSTEM)
1393 ret = fanotify_add_sb_mark(group, mnt->mnt_sb, mask,
1396 ret = fanotify_add_inode_mark(group, inode, mask,
1399 case FAN_MARK_REMOVE:
1400 if (mark_type == FAN_MARK_MOUNT)
1401 ret = fanotify_remove_vfsmount_mark(group, mnt, mask,
1403 else if (mark_type == FAN_MARK_FILESYSTEM)
1404 ret = fanotify_remove_sb_mark(group, mnt->mnt_sb, mask,
1407 ret = fanotify_remove_inode_mark(group, inode, mask,
1421 #ifndef CONFIG_ARCH_SPLIT_ARG64
1422 SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
1423 __u64, mask, int, dfd,
1424 const char __user *, pathname)
1426 return do_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname);
1430 #if defined(CONFIG_ARCH_SPLIT_ARG64) || defined(CONFIG_COMPAT)
1431 SYSCALL32_DEFINE6(fanotify_mark,
1432 int, fanotify_fd, unsigned int, flags,
1433 SC_ARG64(mask), int, dfd,
1434 const char __user *, pathname)
1436 return do_fanotify_mark(fanotify_fd, flags, SC_VAL64(__u64, mask),
1442 * fanotify_user_setup - Our initialization function. Note that we cannot return
1443 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
1444 * must result in panic().
1446 static int __init fanotify_user_setup(void)
1453 * Allow up to 1% of addressable memory to be accounted for per user
1454 * marks limited to the range [8192, 1048576]. mount and sb marks are
1455 * a lot cheaper than inode marks, but there is no reason for a user
1456 * to have many of those, so calculate by the cost of inode marks.
1458 max_marks = (((si.totalram - si.totalhigh) / 100) << PAGE_SHIFT) /
1460 max_marks = clamp(max_marks, FANOTIFY_OLD_DEFAULT_MAX_MARKS,
1461 FANOTIFY_DEFAULT_MAX_USER_MARKS);
1463 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 10);
1464 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9);
1466 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark,
1467 SLAB_PANIC|SLAB_ACCOUNT);
1468 fanotify_fid_event_cachep = KMEM_CACHE(fanotify_fid_event,
1470 fanotify_path_event_cachep = KMEM_CACHE(fanotify_path_event,
1472 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
1473 fanotify_perm_event_cachep =
1474 KMEM_CACHE(fanotify_perm_event, SLAB_PANIC);
1477 fanotify_max_queued_events = FANOTIFY_DEFAULT_MAX_EVENTS;
1478 init_user_ns.ucount_max[UCOUNT_FANOTIFY_GROUPS] =
1479 FANOTIFY_DEFAULT_MAX_GROUPS;
1480 init_user_ns.ucount_max[UCOUNT_FANOTIFY_MARKS] = max_marks;
1484 device_initcall(fanotify_user_setup);