1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/fanotify.h>
3 #include <linux/fcntl.h>
4 #include <linux/fdtable.h>
5 #include <linux/file.h>
7 #include <linux/anon_inodes.h>
8 #include <linux/fsnotify_backend.h>
9 #include <linux/init.h>
10 #include <linux/mount.h>
11 #include <linux/namei.h>
12 #include <linux/poll.h>
13 #include <linux/security.h>
14 #include <linux/syscalls.h>
15 #include <linux/slab.h>
16 #include <linux/types.h>
17 #include <linux/uaccess.h>
18 #include <linux/compat.h>
19 #include <linux/sched/signal.h>
20 #include <linux/memcontrol.h>
21 #include <linux/statfs.h>
22 #include <linux/exportfs.h>
24 #include <asm/ioctls.h>
26 #include "../../mount.h"
27 #include "../fdinfo.h"
30 #define FANOTIFY_DEFAULT_MAX_EVENTS 16384
31 #define FANOTIFY_OLD_DEFAULT_MAX_MARKS 8192
32 #define FANOTIFY_DEFAULT_MAX_GROUPS 128
33 #define FANOTIFY_DEFAULT_FEE_POOL_SIZE 32
36 * Legacy fanotify marks limits (8192) is per group and we introduced a tunable
37 * limit of marks per user, similar to inotify. Effectively, the legacy limit
38 * of fanotify marks per user is <max marks per group> * <max groups per user>.
39 * This default limit (1M) also happens to match the increased limit of inotify
40 * max_user_watches since v5.10.
42 #define FANOTIFY_DEFAULT_MAX_USER_MARKS \
43 (FANOTIFY_OLD_DEFAULT_MAX_MARKS * FANOTIFY_DEFAULT_MAX_GROUPS)
46 * Most of the memory cost of adding an inode mark is pinning the marked inode.
47 * The size of the filesystem inode struct is not uniform across filesystems,
48 * so double the size of a VFS inode is used as a conservative approximation.
50 #define INODE_MARK_COST (2 * sizeof(struct inode))
52 /* configurable via /proc/sys/fs/fanotify/ */
53 static int fanotify_max_queued_events __read_mostly;
57 #include <linux/sysctl.h>
59 static long ft_zero = 0;
60 static long ft_int_max = INT_MAX;
62 static struct ctl_table fanotify_table[] = {
64 .procname = "max_user_groups",
65 .data = &init_user_ns.ucount_max[UCOUNT_FANOTIFY_GROUPS],
66 .maxlen = sizeof(long),
68 .proc_handler = proc_doulongvec_minmax,
70 .extra2 = &ft_int_max,
73 .procname = "max_user_marks",
74 .data = &init_user_ns.ucount_max[UCOUNT_FANOTIFY_MARKS],
75 .maxlen = sizeof(long),
77 .proc_handler = proc_doulongvec_minmax,
79 .extra2 = &ft_int_max,
82 .procname = "max_queued_events",
83 .data = &fanotify_max_queued_events,
84 .maxlen = sizeof(int),
86 .proc_handler = proc_dointvec_minmax,
92 static void __init fanotify_sysctls_init(void)
94 register_sysctl("fs/fanotify", fanotify_table);
97 #define fanotify_sysctls_init() do { } while (0)
98 #endif /* CONFIG_SYSCTL */
101 * All flags that may be specified in parameter event_f_flags of fanotify_init.
103 * Internal and external open flags are stored together in field f_flags of
104 * struct file. Only external open flags shall be allowed in event_f_flags.
105 * Internal flags like FMODE_NONOTIFY, FMODE_EXEC, FMODE_NOCMTIME shall be
108 #define FANOTIFY_INIT_ALL_EVENT_F_BITS ( \
109 O_ACCMODE | O_APPEND | O_NONBLOCK | \
110 __O_SYNC | O_DSYNC | O_CLOEXEC | \
111 O_LARGEFILE | O_NOATIME )
113 extern const struct fsnotify_ops fanotify_fsnotify_ops;
115 struct kmem_cache *fanotify_mark_cache __read_mostly;
116 struct kmem_cache *fanotify_fid_event_cachep __read_mostly;
117 struct kmem_cache *fanotify_path_event_cachep __read_mostly;
118 struct kmem_cache *fanotify_perm_event_cachep __read_mostly;
120 #define FANOTIFY_EVENT_ALIGN 4
121 #define FANOTIFY_FID_INFO_HDR_LEN \
122 (sizeof(struct fanotify_event_info_fid) + sizeof(struct file_handle))
123 #define FANOTIFY_PIDFD_INFO_HDR_LEN \
124 sizeof(struct fanotify_event_info_pidfd)
125 #define FANOTIFY_ERROR_INFO_LEN \
126 (sizeof(struct fanotify_event_info_error))
128 static int fanotify_fid_info_len(int fh_len, int name_len)
130 int info_len = fh_len;
133 info_len += name_len + 1;
135 return roundup(FANOTIFY_FID_INFO_HDR_LEN + info_len,
136 FANOTIFY_EVENT_ALIGN);
139 /* FAN_RENAME may have one or two dir+name info records */
140 static int fanotify_dir_name_info_len(struct fanotify_event *event)
142 struct fanotify_info *info = fanotify_event_info(event);
143 int dir_fh_len = fanotify_event_dir_fh_len(event);
144 int dir2_fh_len = fanotify_event_dir2_fh_len(event);
148 info_len += fanotify_fid_info_len(dir_fh_len,
151 info_len += fanotify_fid_info_len(dir2_fh_len,
157 static size_t fanotify_event_len(unsigned int info_mode,
158 struct fanotify_event *event)
160 size_t event_len = FAN_EVENT_METADATA_LEN;
167 if (fanotify_is_error_event(event->mask))
168 event_len += FANOTIFY_ERROR_INFO_LEN;
170 if (fanotify_event_has_any_dir_fh(event)) {
171 event_len += fanotify_dir_name_info_len(event);
172 } else if ((info_mode & FAN_REPORT_NAME) &&
173 (event->mask & FAN_ONDIR)) {
175 * With group flag FAN_REPORT_NAME, if name was not recorded in
176 * event on a directory, we will report the name ".".
181 if (info_mode & FAN_REPORT_PIDFD)
182 event_len += FANOTIFY_PIDFD_INFO_HDR_LEN;
184 if (fanotify_event_has_object_fh(event)) {
185 fh_len = fanotify_event_object_fh_len(event);
186 event_len += fanotify_fid_info_len(fh_len, dot_len);
193 * Remove an hashed event from merge hash table.
195 static void fanotify_unhash_event(struct fsnotify_group *group,
196 struct fanotify_event *event)
198 assert_spin_locked(&group->notification_lock);
200 pr_debug("%s: group=%p event=%p bucket=%u\n", __func__,
201 group, event, fanotify_event_hash_bucket(group, event));
203 if (WARN_ON_ONCE(hlist_unhashed(&event->merge_list)))
206 hlist_del_init(&event->merge_list);
210 * Get an fanotify notification event if one exists and is small
211 * enough to fit in "count". Return an error pointer if the count
212 * is not large enough. When permission event is dequeued, its state is
213 * updated accordingly.
215 static struct fanotify_event *get_one_event(struct fsnotify_group *group,
219 struct fanotify_event *event = NULL;
220 struct fsnotify_event *fsn_event;
221 unsigned int info_mode = FAN_GROUP_FLAG(group, FANOTIFY_INFO_MODES);
223 pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
225 spin_lock(&group->notification_lock);
226 fsn_event = fsnotify_peek_first_event(group);
230 event = FANOTIFY_E(fsn_event);
231 event_size = fanotify_event_len(info_mode, event);
233 if (event_size > count) {
234 event = ERR_PTR(-EINVAL);
239 * Held the notification_lock the whole time, so this is the
240 * same event we peeked above.
242 fsnotify_remove_first_event(group);
243 if (fanotify_is_perm_event(event->mask))
244 FANOTIFY_PERM(event)->state = FAN_EVENT_REPORTED;
245 if (fanotify_is_hashed_event(event->mask))
246 fanotify_unhash_event(group, event);
248 spin_unlock(&group->notification_lock);
252 static int create_fd(struct fsnotify_group *group, const struct path *path,
256 struct file *new_file;
258 client_fd = get_unused_fd_flags(group->fanotify_data.f_flags);
263 * we need a new file handle for the userspace program so it can read even if it was
264 * originally opened O_WRONLY.
266 new_file = dentry_open(path,
267 group->fanotify_data.f_flags | __FMODE_NONOTIFY,
269 if (IS_ERR(new_file)) {
271 * we still send an event even if we can't open the file. this
272 * can happen when say tasks are gone and we try to open their
273 * /proc files or we try to open a WRONLY file like in sysfs
274 * we just send the errno to userspace since there isn't much
277 put_unused_fd(client_fd);
278 client_fd = PTR_ERR(new_file);
286 static int process_access_response_info(const char __user *info,
288 struct fanotify_response_info_audit_rule *friar)
290 if (info_len != sizeof(*friar))
293 if (copy_from_user(friar, info, sizeof(*friar)))
296 if (friar->hdr.type != FAN_RESPONSE_INFO_AUDIT_RULE)
298 if (friar->hdr.pad != 0)
300 if (friar->hdr.len != sizeof(*friar))
307 * Finish processing of permission event by setting it to ANSWERED state and
308 * drop group->notification_lock.
310 static void finish_permission_event(struct fsnotify_group *group,
311 struct fanotify_perm_event *event, u32 response,
312 struct fanotify_response_info_audit_rule *friar)
313 __releases(&group->notification_lock)
315 bool destroy = false;
317 assert_spin_locked(&group->notification_lock);
318 event->response = response & ~FAN_INFO;
319 if (response & FAN_INFO)
320 memcpy(&event->audit_rule, friar, sizeof(*friar));
322 if (event->state == FAN_EVENT_CANCELED)
325 event->state = FAN_EVENT_ANSWERED;
326 spin_unlock(&group->notification_lock);
328 fsnotify_destroy_event(group, &event->fae.fse);
331 static int process_access_response(struct fsnotify_group *group,
332 struct fanotify_response *response_struct,
333 const char __user *info,
336 struct fanotify_perm_event *event;
337 int fd = response_struct->fd;
338 u32 response = response_struct->response;
340 struct fanotify_response_info_audit_rule friar;
342 pr_debug("%s: group=%p fd=%d response=%u buf=%p size=%zu\n", __func__,
343 group, fd, response, info, info_len);
345 * make sure the response is valid, if invalid we do nothing and either
346 * userspace can send a valid response or we will clean it up after the
349 if (response & ~FANOTIFY_RESPONSE_VALID_MASK)
352 switch (response & FANOTIFY_RESPONSE_ACCESS) {
360 if ((response & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT))
363 if (response & FAN_INFO) {
364 ret = process_access_response_info(info, info_len, &friar);
376 spin_lock(&group->notification_lock);
377 list_for_each_entry(event, &group->fanotify_data.access_list,
382 list_del_init(&event->fae.fse.list);
383 finish_permission_event(group, event, response, &friar);
384 wake_up(&group->fanotify_data.access_waitq);
387 spin_unlock(&group->notification_lock);
392 static size_t copy_error_info_to_user(struct fanotify_event *event,
393 char __user *buf, int count)
395 struct fanotify_event_info_error info = { };
396 struct fanotify_error_event *fee = FANOTIFY_EE(event);
398 info.hdr.info_type = FAN_EVENT_INFO_TYPE_ERROR;
399 info.hdr.len = FANOTIFY_ERROR_INFO_LEN;
401 if (WARN_ON(count < info.hdr.len))
404 info.error = fee->error;
405 info.error_count = fee->err_count;
407 if (copy_to_user(buf, &info, sizeof(info)))
413 static int copy_fid_info_to_user(__kernel_fsid_t *fsid, struct fanotify_fh *fh,
414 int info_type, const char *name,
416 char __user *buf, size_t count)
418 struct fanotify_event_info_fid info = { };
419 struct file_handle handle = { };
420 unsigned char bounce[FANOTIFY_INLINE_FH_LEN], *fh_buf;
421 size_t fh_len = fh ? fh->len : 0;
422 size_t info_len = fanotify_fid_info_len(fh_len, name_len);
423 size_t len = info_len;
425 pr_debug("%s: fh_len=%zu name_len=%zu, info_len=%zu, count=%zu\n",
426 __func__, fh_len, name_len, info_len, count);
428 if (WARN_ON_ONCE(len < sizeof(info) || len > count))
432 * Copy event info fid header followed by variable sized file handle
433 * and optionally followed by variable sized filename.
436 case FAN_EVENT_INFO_TYPE_FID:
437 case FAN_EVENT_INFO_TYPE_DFID:
438 if (WARN_ON_ONCE(name_len))
441 case FAN_EVENT_INFO_TYPE_DFID_NAME:
442 case FAN_EVENT_INFO_TYPE_OLD_DFID_NAME:
443 case FAN_EVENT_INFO_TYPE_NEW_DFID_NAME:
444 if (WARN_ON_ONCE(!name || !name_len))
451 info.hdr.info_type = info_type;
454 if (copy_to_user(buf, &info, sizeof(info)))
459 if (WARN_ON_ONCE(len < sizeof(handle)))
462 handle.handle_type = fh->type;
463 handle.handle_bytes = fh_len;
465 /* Mangle handle_type for bad file_handle */
467 handle.handle_type = FILEID_INVALID;
469 if (copy_to_user(buf, &handle, sizeof(handle)))
472 buf += sizeof(handle);
473 len -= sizeof(handle);
474 if (WARN_ON_ONCE(len < fh_len))
478 * For an inline fh and inline file name, copy through stack to exclude
479 * the copy from usercopy hardening protections.
481 fh_buf = fanotify_fh_buf(fh);
482 if (fh_len <= FANOTIFY_INLINE_FH_LEN) {
483 memcpy(bounce, fh_buf, fh_len);
486 if (copy_to_user(buf, fh_buf, fh_len))
493 /* Copy the filename with terminating null */
495 if (WARN_ON_ONCE(len < name_len))
498 if (copy_to_user(buf, name, name_len))
506 WARN_ON_ONCE(len < 0 || len >= FANOTIFY_EVENT_ALIGN);
507 if (len > 0 && clear_user(buf, len))
513 static int copy_pidfd_info_to_user(int pidfd,
517 struct fanotify_event_info_pidfd info = { };
518 size_t info_len = FANOTIFY_PIDFD_INFO_HDR_LEN;
520 if (WARN_ON_ONCE(info_len > count))
523 info.hdr.info_type = FAN_EVENT_INFO_TYPE_PIDFD;
524 info.hdr.len = info_len;
527 if (copy_to_user(buf, &info, info_len))
533 static int copy_info_records_to_user(struct fanotify_event *event,
534 struct fanotify_info *info,
535 unsigned int info_mode, int pidfd,
536 char __user *buf, size_t count)
538 int ret, total_bytes = 0, info_type = 0;
539 unsigned int fid_mode = info_mode & FANOTIFY_FID_BITS;
540 unsigned int pidfd_mode = info_mode & FAN_REPORT_PIDFD;
543 * Event info records order is as follows:
545 * 2. (optional) new dir fid + new name
546 * 3. (optional) child fid
548 if (fanotify_event_has_dir_fh(event)) {
549 info_type = info->name_len ? FAN_EVENT_INFO_TYPE_DFID_NAME :
550 FAN_EVENT_INFO_TYPE_DFID;
552 /* FAN_RENAME uses special info types */
553 if (event->mask & FAN_RENAME)
554 info_type = FAN_EVENT_INFO_TYPE_OLD_DFID_NAME;
556 ret = copy_fid_info_to_user(fanotify_event_fsid(event),
557 fanotify_info_dir_fh(info),
559 fanotify_info_name(info),
560 info->name_len, buf, count);
569 /* New dir fid+name may be reported in addition to old dir fid+name */
570 if (fanotify_event_has_dir2_fh(event)) {
571 info_type = FAN_EVENT_INFO_TYPE_NEW_DFID_NAME;
572 ret = copy_fid_info_to_user(fanotify_event_fsid(event),
573 fanotify_info_dir2_fh(info),
575 fanotify_info_name2(info),
576 info->name2_len, buf, count);
585 if (fanotify_event_has_object_fh(event)) {
586 const char *dot = NULL;
589 if (fid_mode == FAN_REPORT_FID || info_type) {
591 * With only group flag FAN_REPORT_FID only type FID is
592 * reported. Second info record type is always FID.
594 info_type = FAN_EVENT_INFO_TYPE_FID;
595 } else if ((fid_mode & FAN_REPORT_NAME) &&
596 (event->mask & FAN_ONDIR)) {
598 * With group flag FAN_REPORT_NAME, if name was not
599 * recorded in an event on a directory, report the name
600 * "." with info type DFID_NAME.
602 info_type = FAN_EVENT_INFO_TYPE_DFID_NAME;
605 } else if ((event->mask & ALL_FSNOTIFY_DIRENT_EVENTS) ||
606 (event->mask & FAN_ONDIR)) {
608 * With group flag FAN_REPORT_DIR_FID, a single info
609 * record has type DFID for directory entry modification
610 * event and for event on a directory.
612 info_type = FAN_EVENT_INFO_TYPE_DFID;
615 * With group flags FAN_REPORT_DIR_FID|FAN_REPORT_FID,
616 * a single info record has type FID for event on a
617 * non-directory, when there is no directory to report.
618 * For example, on FAN_DELETE_SELF event.
620 info_type = FAN_EVENT_INFO_TYPE_FID;
623 ret = copy_fid_info_to_user(fanotify_event_fsid(event),
624 fanotify_event_object_fh(event),
625 info_type, dot, dot_len,
636 ret = copy_pidfd_info_to_user(pidfd, buf, count);
645 if (fanotify_is_error_event(event->mask)) {
646 ret = copy_error_info_to_user(event, buf, count);
657 static ssize_t copy_event_to_user(struct fsnotify_group *group,
658 struct fanotify_event *event,
659 char __user *buf, size_t count)
661 struct fanotify_event_metadata metadata;
662 const struct path *path = fanotify_event_path(event);
663 struct fanotify_info *info = fanotify_event_info(event);
664 unsigned int info_mode = FAN_GROUP_FLAG(group, FANOTIFY_INFO_MODES);
665 unsigned int pidfd_mode = info_mode & FAN_REPORT_PIDFD;
666 struct file *f = NULL, *pidfd_file = NULL;
667 int ret, pidfd = FAN_NOPIDFD, fd = FAN_NOFD;
669 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
671 metadata.event_len = fanotify_event_len(info_mode, event);
672 metadata.metadata_len = FAN_EVENT_METADATA_LEN;
673 metadata.vers = FANOTIFY_METADATA_VERSION;
674 metadata.reserved = 0;
675 metadata.mask = event->mask & FANOTIFY_OUTGOING_EVENTS;
676 metadata.pid = pid_vnr(event->pid);
678 * For an unprivileged listener, event->pid can be used to identify the
679 * events generated by the listener process itself, without disclosing
680 * the pids of other processes.
682 if (FAN_GROUP_FLAG(group, FANOTIFY_UNPRIV) &&
683 task_tgid(current) != event->pid)
687 * For now, fid mode is required for an unprivileged listener and
688 * fid mode does not report fd in events. Keep this check anyway
689 * for safety in case fid mode requirement is relaxed in the future
690 * to allow unprivileged listener to get events with no fd and no fid.
692 if (!FAN_GROUP_FLAG(group, FANOTIFY_UNPRIV) &&
693 path && path->mnt && path->dentry) {
694 fd = create_fd(group, path, &f);
702 * Complain if the FAN_REPORT_PIDFD and FAN_REPORT_TID mutual
703 * exclusion is ever lifted. At the time of incoporating pidfd
704 * support within fanotify, the pidfd API only supported the
705 * creation of pidfds for thread-group leaders.
707 WARN_ON_ONCE(FAN_GROUP_FLAG(group, FAN_REPORT_TID));
710 * The PIDTYPE_TGID check for an event->pid is performed
711 * preemptively in an attempt to catch out cases where the event
712 * listener reads events after the event generating process has
713 * already terminated. Report FAN_NOPIDFD to the event listener
714 * in those cases, with all other pidfd creation errors being
715 * reported as FAN_EPIDFD.
717 if (metadata.pid == 0 ||
718 !pid_has_task(event->pid, PIDTYPE_TGID)) {
721 pidfd = pidfd_prepare(event->pid, 0, &pidfd_file);
729 * Sanity check copy size in case get_one_event() and
730 * event_len sizes ever get out of sync.
732 if (WARN_ON_ONCE(metadata.event_len > count))
735 if (copy_to_user(buf, &metadata, FAN_EVENT_METADATA_LEN))
738 buf += FAN_EVENT_METADATA_LEN;
739 count -= FAN_EVENT_METADATA_LEN;
741 if (fanotify_is_perm_event(event->mask))
742 FANOTIFY_PERM(event)->fd = fd;
745 ret = copy_info_records_to_user(event, info, info_mode, pidfd,
755 fd_install(pidfd, pidfd_file);
757 return metadata.event_len;
760 if (fd != FAN_NOFD) {
766 put_unused_fd(pidfd);
773 /* intofiy userspace file descriptor functions */
774 static __poll_t fanotify_poll(struct file *file, poll_table *wait)
776 struct fsnotify_group *group = file->private_data;
779 poll_wait(file, &group->notification_waitq, wait);
780 spin_lock(&group->notification_lock);
781 if (!fsnotify_notify_queue_is_empty(group))
782 ret = EPOLLIN | EPOLLRDNORM;
783 spin_unlock(&group->notification_lock);
788 static ssize_t fanotify_read(struct file *file, char __user *buf,
789 size_t count, loff_t *pos)
791 struct fsnotify_group *group;
792 struct fanotify_event *event;
795 DEFINE_WAIT_FUNC(wait, woken_wake_function);
798 group = file->private_data;
800 pr_debug("%s: group=%p\n", __func__, group);
802 add_wait_queue(&group->notification_waitq, &wait);
805 * User can supply arbitrarily large buffer. Avoid softlockups
806 * in case there are lots of available events.
809 event = get_one_event(group, count);
811 ret = PTR_ERR(event);
817 if (file->f_flags & O_NONBLOCK)
821 if (signal_pending(current))
827 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
831 ret = copy_event_to_user(group, event, buf, count);
832 if (unlikely(ret == -EOPENSTALE)) {
834 * We cannot report events with stale fd so drop it.
835 * Setting ret to 0 will continue the event loop and
836 * do the right thing if there are no more events to
837 * read (i.e. return bytes read, -EAGAIN or wait).
843 * Permission events get queued to wait for response. Other
844 * events can be destroyed now.
846 if (!fanotify_is_perm_event(event->mask)) {
847 fsnotify_destroy_event(group, &event->fse);
850 spin_lock(&group->notification_lock);
851 finish_permission_event(group,
852 FANOTIFY_PERM(event), FAN_DENY, NULL);
853 wake_up(&group->fanotify_data.access_waitq);
855 spin_lock(&group->notification_lock);
856 list_add_tail(&event->fse.list,
857 &group->fanotify_data.access_list);
858 spin_unlock(&group->notification_lock);
866 remove_wait_queue(&group->notification_waitq, &wait);
868 if (start != buf && ret != -EFAULT)
873 static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
875 struct fanotify_response response;
876 struct fsnotify_group *group;
878 const char __user *info_buf = buf + sizeof(struct fanotify_response);
881 if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
884 group = file->private_data;
886 pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
888 if (count < sizeof(response))
891 if (copy_from_user(&response, buf, sizeof(response)))
894 info_len = count - sizeof(response);
896 ret = process_access_response(group, &response, info_buf, info_len);
900 count = sizeof(response) + ret;
905 static int fanotify_release(struct inode *ignored, struct file *file)
907 struct fsnotify_group *group = file->private_data;
908 struct fsnotify_event *fsn_event;
911 * Stop new events from arriving in the notification queue. since
912 * userspace cannot use fanotify fd anymore, no event can enter or
913 * leave access_list by now either.
915 fsnotify_group_stop_queueing(group);
918 * Process all permission events on access_list and notification queue
919 * and simulate reply from userspace.
921 spin_lock(&group->notification_lock);
922 while (!list_empty(&group->fanotify_data.access_list)) {
923 struct fanotify_perm_event *event;
925 event = list_first_entry(&group->fanotify_data.access_list,
926 struct fanotify_perm_event, fae.fse.list);
927 list_del_init(&event->fae.fse.list);
928 finish_permission_event(group, event, FAN_ALLOW, NULL);
929 spin_lock(&group->notification_lock);
933 * Destroy all non-permission events. For permission events just
934 * dequeue them and set the response. They will be freed once the
935 * response is consumed and fanotify_get_response() returns.
937 while ((fsn_event = fsnotify_remove_first_event(group))) {
938 struct fanotify_event *event = FANOTIFY_E(fsn_event);
940 if (!(event->mask & FANOTIFY_PERM_EVENTS)) {
941 spin_unlock(&group->notification_lock);
942 fsnotify_destroy_event(group, fsn_event);
944 finish_permission_event(group, FANOTIFY_PERM(event),
947 spin_lock(&group->notification_lock);
949 spin_unlock(&group->notification_lock);
951 /* Response for all permission events it set, wakeup waiters */
952 wake_up(&group->fanotify_data.access_waitq);
954 /* matches the fanotify_init->fsnotify_alloc_group */
955 fsnotify_destroy_group(group);
960 static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
962 struct fsnotify_group *group;
963 struct fsnotify_event *fsn_event;
968 group = file->private_data;
970 p = (void __user *) arg;
974 spin_lock(&group->notification_lock);
975 list_for_each_entry(fsn_event, &group->notification_list, list)
976 send_len += FAN_EVENT_METADATA_LEN;
977 spin_unlock(&group->notification_lock);
978 ret = put_user(send_len, (int __user *) p);
985 static const struct file_operations fanotify_fops = {
986 .show_fdinfo = fanotify_show_fdinfo,
987 .poll = fanotify_poll,
988 .read = fanotify_read,
989 .write = fanotify_write,
991 .release = fanotify_release,
992 .unlocked_ioctl = fanotify_ioctl,
993 .compat_ioctl = compat_ptr_ioctl,
994 .llseek = noop_llseek,
997 static int fanotify_find_path(int dfd, const char __user *filename,
998 struct path *path, unsigned int flags, __u64 mask,
999 unsigned int obj_type)
1003 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
1004 dfd, filename, flags);
1006 if (filename == NULL) {
1007 struct fd f = fdget(dfd);
1014 if ((flags & FAN_MARK_ONLYDIR) &&
1015 !(S_ISDIR(file_inode(f.file)->i_mode))) {
1020 *path = f.file->f_path;
1024 unsigned int lookup_flags = 0;
1026 if (!(flags & FAN_MARK_DONT_FOLLOW))
1027 lookup_flags |= LOOKUP_FOLLOW;
1028 if (flags & FAN_MARK_ONLYDIR)
1029 lookup_flags |= LOOKUP_DIRECTORY;
1031 ret = user_path_at(dfd, filename, lookup_flags, path);
1036 /* you can only watch an inode if you have read permissions on it */
1037 ret = path_permission(path, MAY_READ);
1043 ret = security_path_notify(path, mask, obj_type);
1051 static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
1052 __u32 mask, unsigned int flags,
1053 __u32 umask, int *destroy)
1055 __u32 oldmask, newmask;
1057 /* umask bits cannot be removed by user */
1059 spin_lock(&fsn_mark->lock);
1060 oldmask = fsnotify_calc_mask(fsn_mark);
1061 if (!(flags & FANOTIFY_MARK_IGNORE_BITS)) {
1062 fsn_mark->mask &= ~mask;
1064 fsn_mark->ignore_mask &= ~mask;
1066 newmask = fsnotify_calc_mask(fsn_mark);
1068 * We need to keep the mark around even if remaining mask cannot
1069 * result in any events (e.g. mask == FAN_ONDIR) to support incremenal
1070 * changes to the mask.
1071 * Destroy mark when only umask bits remain.
1073 *destroy = !((fsn_mark->mask | fsn_mark->ignore_mask) & ~umask);
1074 spin_unlock(&fsn_mark->lock);
1076 return oldmask & ~newmask;
1079 static int fanotify_remove_mark(struct fsnotify_group *group,
1080 fsnotify_connp_t *connp, __u32 mask,
1081 unsigned int flags, __u32 umask)
1083 struct fsnotify_mark *fsn_mark = NULL;
1087 fsnotify_group_lock(group);
1088 fsn_mark = fsnotify_find_mark(connp, group);
1090 fsnotify_group_unlock(group);
1094 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
1095 umask, &destroy_mark);
1096 if (removed & fsnotify_conn_mask(fsn_mark->connector))
1097 fsnotify_recalc_mask(fsn_mark->connector);
1099 fsnotify_detach_mark(fsn_mark);
1100 fsnotify_group_unlock(group);
1102 fsnotify_free_mark(fsn_mark);
1104 /* matches the fsnotify_find_mark() */
1105 fsnotify_put_mark(fsn_mark);
1109 static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
1110 struct vfsmount *mnt, __u32 mask,
1111 unsigned int flags, __u32 umask)
1113 return fanotify_remove_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
1114 mask, flags, umask);
1117 static int fanotify_remove_sb_mark(struct fsnotify_group *group,
1118 struct super_block *sb, __u32 mask,
1119 unsigned int flags, __u32 umask)
1121 return fanotify_remove_mark(group, &sb->s_fsnotify_marks, mask,
1125 static int fanotify_remove_inode_mark(struct fsnotify_group *group,
1126 struct inode *inode, __u32 mask,
1127 unsigned int flags, __u32 umask)
1129 return fanotify_remove_mark(group, &inode->i_fsnotify_marks, mask,
1133 static bool fanotify_mark_update_flags(struct fsnotify_mark *fsn_mark,
1134 unsigned int fan_flags)
1136 bool want_iref = !(fan_flags & FAN_MARK_EVICTABLE);
1137 unsigned int ignore = fan_flags & FANOTIFY_MARK_IGNORE_BITS;
1138 bool recalc = false;
1141 * When using FAN_MARK_IGNORE for the first time, mark starts using
1142 * independent event flags in ignore mask. After that, trying to
1143 * update the ignore mask with the old FAN_MARK_IGNORED_MASK API
1144 * will result in EEXIST error.
1146 if (ignore == FAN_MARK_IGNORE)
1147 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS;
1150 * Setting FAN_MARK_IGNORED_SURV_MODIFY for the first time may lead to
1151 * the removal of the FS_MODIFY bit in calculated mask if it was set
1152 * because of an ignore mask that is now going to survive FS_MODIFY.
1154 if (ignore && (fan_flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
1155 !(fsn_mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)) {
1156 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
1157 if (!(fsn_mark->mask & FS_MODIFY))
1161 if (fsn_mark->connector->type != FSNOTIFY_OBJ_TYPE_INODE ||
1162 want_iref == !(fsn_mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF))
1166 * NO_IREF may be removed from a mark, but not added.
1167 * When removed, fsnotify_recalc_mask() will take the inode ref.
1169 WARN_ON_ONCE(!want_iref);
1170 fsn_mark->flags &= ~FSNOTIFY_MARK_FLAG_NO_IREF;
1175 static bool fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
1176 __u32 mask, unsigned int fan_flags)
1180 spin_lock(&fsn_mark->lock);
1181 if (!(fan_flags & FANOTIFY_MARK_IGNORE_BITS))
1182 fsn_mark->mask |= mask;
1184 fsn_mark->ignore_mask |= mask;
1186 recalc = fsnotify_calc_mask(fsn_mark) &
1187 ~fsnotify_conn_mask(fsn_mark->connector);
1189 recalc |= fanotify_mark_update_flags(fsn_mark, fan_flags);
1190 spin_unlock(&fsn_mark->lock);
1195 static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
1196 fsnotify_connp_t *connp,
1197 unsigned int obj_type,
1198 unsigned int fan_flags,
1199 __kernel_fsid_t *fsid)
1201 struct ucounts *ucounts = group->fanotify_data.ucounts;
1202 struct fsnotify_mark *mark;
1206 * Enforce per user marks limits per user in all containing user ns.
1207 * A group with FAN_UNLIMITED_MARKS does not contribute to mark count
1208 * in the limited groups account.
1210 if (!FAN_GROUP_FLAG(group, FAN_UNLIMITED_MARKS) &&
1211 !inc_ucount(ucounts->ns, ucounts->uid, UCOUNT_FANOTIFY_MARKS))
1212 return ERR_PTR(-ENOSPC);
1214 mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
1217 goto out_dec_ucounts;
1220 fsnotify_init_mark(mark, group);
1221 if (fan_flags & FAN_MARK_EVICTABLE)
1222 mark->flags |= FSNOTIFY_MARK_FLAG_NO_IREF;
1224 ret = fsnotify_add_mark_locked(mark, connp, obj_type, 0, fsid);
1226 fsnotify_put_mark(mark);
1227 goto out_dec_ucounts;
1233 if (!FAN_GROUP_FLAG(group, FAN_UNLIMITED_MARKS))
1234 dec_ucount(ucounts, UCOUNT_FANOTIFY_MARKS);
1235 return ERR_PTR(ret);
1238 static int fanotify_group_init_error_pool(struct fsnotify_group *group)
1240 if (mempool_initialized(&group->fanotify_data.error_events_pool))
1243 return mempool_init_kmalloc_pool(&group->fanotify_data.error_events_pool,
1244 FANOTIFY_DEFAULT_FEE_POOL_SIZE,
1245 sizeof(struct fanotify_error_event));
1248 static int fanotify_may_update_existing_mark(struct fsnotify_mark *fsn_mark,
1249 unsigned int fan_flags)
1252 * Non evictable mark cannot be downgraded to evictable mark.
1254 if (fan_flags & FAN_MARK_EVICTABLE &&
1255 !(fsn_mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF))
1259 * New ignore mask semantics cannot be downgraded to old semantics.
1261 if (fan_flags & FAN_MARK_IGNORED_MASK &&
1262 fsn_mark->flags & FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS)
1266 * An ignore mask that survives modify could never be downgraded to not
1267 * survive modify. With new FAN_MARK_IGNORE semantics we make that rule
1268 * explicit and return an error when trying to update the ignore mask
1269 * without the original FAN_MARK_IGNORED_SURV_MODIFY value.
1271 if (fan_flags & FAN_MARK_IGNORE &&
1272 !(fan_flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
1273 fsn_mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)
1279 static int fanotify_add_mark(struct fsnotify_group *group,
1280 fsnotify_connp_t *connp, unsigned int obj_type,
1281 __u32 mask, unsigned int fan_flags,
1282 __kernel_fsid_t *fsid)
1284 struct fsnotify_mark *fsn_mark;
1288 fsnotify_group_lock(group);
1289 fsn_mark = fsnotify_find_mark(connp, group);
1291 fsn_mark = fanotify_add_new_mark(group, connp, obj_type,
1293 if (IS_ERR(fsn_mark)) {
1294 fsnotify_group_unlock(group);
1295 return PTR_ERR(fsn_mark);
1300 * Check if requested mark flags conflict with an existing mark flags.
1302 ret = fanotify_may_update_existing_mark(fsn_mark, fan_flags);
1307 * Error events are pre-allocated per group, only if strictly
1308 * needed (i.e. FAN_FS_ERROR was requested).
1310 if (!(fan_flags & FANOTIFY_MARK_IGNORE_BITS) &&
1311 (mask & FAN_FS_ERROR)) {
1312 ret = fanotify_group_init_error_pool(group);
1317 recalc = fanotify_mark_add_to_mask(fsn_mark, mask, fan_flags);
1319 fsnotify_recalc_mask(fsn_mark->connector);
1322 fsnotify_group_unlock(group);
1324 fsnotify_put_mark(fsn_mark);
1328 static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
1329 struct vfsmount *mnt, __u32 mask,
1330 unsigned int flags, __kernel_fsid_t *fsid)
1332 return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
1333 FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags, fsid);
1336 static int fanotify_add_sb_mark(struct fsnotify_group *group,
1337 struct super_block *sb, __u32 mask,
1338 unsigned int flags, __kernel_fsid_t *fsid)
1340 return fanotify_add_mark(group, &sb->s_fsnotify_marks,
1341 FSNOTIFY_OBJ_TYPE_SB, mask, flags, fsid);
1344 static int fanotify_add_inode_mark(struct fsnotify_group *group,
1345 struct inode *inode, __u32 mask,
1346 unsigned int flags, __kernel_fsid_t *fsid)
1348 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
1351 * If some other task has this inode open for write we should not add
1352 * an ignore mask, unless that ignore mask is supposed to survive
1353 * modification changes anyway.
1355 if ((flags & FANOTIFY_MARK_IGNORE_BITS) &&
1356 !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
1357 inode_is_open_for_write(inode))
1360 return fanotify_add_mark(group, &inode->i_fsnotify_marks,
1361 FSNOTIFY_OBJ_TYPE_INODE, mask, flags, fsid);
1364 static struct fsnotify_event *fanotify_alloc_overflow_event(void)
1366 struct fanotify_event *oevent;
1368 oevent = kmalloc(sizeof(*oevent), GFP_KERNEL_ACCOUNT);
1372 fanotify_init_event(oevent, 0, FS_Q_OVERFLOW);
1373 oevent->type = FANOTIFY_EVENT_TYPE_OVERFLOW;
1375 return &oevent->fse;
1378 static struct hlist_head *fanotify_alloc_merge_hash(void)
1380 struct hlist_head *hash;
1382 hash = kmalloc(sizeof(struct hlist_head) << FANOTIFY_HTABLE_BITS,
1383 GFP_KERNEL_ACCOUNT);
1387 __hash_init(hash, FANOTIFY_HTABLE_SIZE);
1392 /* fanotify syscalls */
1393 SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
1395 struct fsnotify_group *group;
1397 unsigned int fid_mode = flags & FANOTIFY_FID_BITS;
1398 unsigned int class = flags & FANOTIFY_CLASS_BITS;
1399 unsigned int internal_flags = 0;
1401 pr_debug("%s: flags=%x event_f_flags=%x\n",
1402 __func__, flags, event_f_flags);
1404 if (!capable(CAP_SYS_ADMIN)) {
1406 * An unprivileged user can setup an fanotify group with
1407 * limited functionality - an unprivileged group is limited to
1408 * notification events with file handles and it cannot use
1409 * unlimited queue/marks.
1411 if ((flags & FANOTIFY_ADMIN_INIT_FLAGS) || !fid_mode)
1415 * Setting the internal flag FANOTIFY_UNPRIV on the group
1416 * prevents setting mount/filesystem marks on this group and
1417 * prevents reporting pid and open fd in events.
1419 internal_flags |= FANOTIFY_UNPRIV;
1422 #ifdef CONFIG_AUDITSYSCALL
1423 if (flags & ~(FANOTIFY_INIT_FLAGS | FAN_ENABLE_AUDIT))
1425 if (flags & ~FANOTIFY_INIT_FLAGS)
1430 * A pidfd can only be returned for a thread-group leader; thus
1431 * FAN_REPORT_PIDFD and FAN_REPORT_TID need to remain mutually
1434 if ((flags & FAN_REPORT_PIDFD) && (flags & FAN_REPORT_TID))
1437 if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS)
1440 switch (event_f_flags & O_ACCMODE) {
1449 if (fid_mode && class != FAN_CLASS_NOTIF)
1453 * Child name is reported with parent fid so requires dir fid.
1454 * We can report both child fid and dir fid with or without name.
1456 if ((fid_mode & FAN_REPORT_NAME) && !(fid_mode & FAN_REPORT_DIR_FID))
1460 * FAN_REPORT_TARGET_FID requires FAN_REPORT_NAME and FAN_REPORT_FID
1461 * and is used as an indication to report both dir and child fid on all
1464 if ((fid_mode & FAN_REPORT_TARGET_FID) &&
1465 (!(fid_mode & FAN_REPORT_NAME) || !(fid_mode & FAN_REPORT_FID)))
1468 f_flags = O_RDWR | __FMODE_NONOTIFY;
1469 if (flags & FAN_CLOEXEC)
1470 f_flags |= O_CLOEXEC;
1471 if (flags & FAN_NONBLOCK)
1472 f_flags |= O_NONBLOCK;
1474 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
1475 group = fsnotify_alloc_group(&fanotify_fsnotify_ops,
1476 FSNOTIFY_GROUP_USER | FSNOTIFY_GROUP_NOFS);
1477 if (IS_ERR(group)) {
1478 return PTR_ERR(group);
1481 /* Enforce groups limits per user in all containing user ns */
1482 group->fanotify_data.ucounts = inc_ucount(current_user_ns(),
1484 UCOUNT_FANOTIFY_GROUPS);
1485 if (!group->fanotify_data.ucounts) {
1487 goto out_destroy_group;
1490 group->fanotify_data.flags = flags | internal_flags;
1491 group->memcg = get_mem_cgroup_from_mm(current->mm);
1493 group->fanotify_data.merge_hash = fanotify_alloc_merge_hash();
1494 if (!group->fanotify_data.merge_hash) {
1496 goto out_destroy_group;
1499 group->overflow_event = fanotify_alloc_overflow_event();
1500 if (unlikely(!group->overflow_event)) {
1502 goto out_destroy_group;
1505 if (force_o_largefile())
1506 event_f_flags |= O_LARGEFILE;
1507 group->fanotify_data.f_flags = event_f_flags;
1508 init_waitqueue_head(&group->fanotify_data.access_waitq);
1509 INIT_LIST_HEAD(&group->fanotify_data.access_list);
1511 case FAN_CLASS_NOTIF:
1512 group->priority = FS_PRIO_0;
1514 case FAN_CLASS_CONTENT:
1515 group->priority = FS_PRIO_1;
1517 case FAN_CLASS_PRE_CONTENT:
1518 group->priority = FS_PRIO_2;
1522 goto out_destroy_group;
1525 if (flags & FAN_UNLIMITED_QUEUE) {
1527 if (!capable(CAP_SYS_ADMIN))
1528 goto out_destroy_group;
1529 group->max_events = UINT_MAX;
1531 group->max_events = fanotify_max_queued_events;
1534 if (flags & FAN_UNLIMITED_MARKS) {
1536 if (!capable(CAP_SYS_ADMIN))
1537 goto out_destroy_group;
1540 if (flags & FAN_ENABLE_AUDIT) {
1542 if (!capable(CAP_AUDIT_WRITE))
1543 goto out_destroy_group;
1546 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
1548 goto out_destroy_group;
1553 fsnotify_destroy_group(group);
1557 static int fanotify_test_fsid(struct dentry *dentry, __kernel_fsid_t *fsid)
1559 __kernel_fsid_t root_fsid;
1563 * Make sure dentry is not of a filesystem with zero fsid (e.g. fuse).
1565 err = vfs_get_fsid(dentry, fsid);
1569 if (!fsid->val[0] && !fsid->val[1])
1573 * Make sure dentry is not of a filesystem subvolume (e.g. btrfs)
1574 * which uses a different fsid than sb root.
1576 err = vfs_get_fsid(dentry->d_sb->s_root, &root_fsid);
1580 if (root_fsid.val[0] != fsid->val[0] ||
1581 root_fsid.val[1] != fsid->val[1])
1587 /* Check if filesystem can encode a unique fid */
1588 static int fanotify_test_fid(struct dentry *dentry, unsigned int flags)
1590 unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS;
1591 const struct export_operations *nop = dentry->d_sb->s_export_op;
1594 * We need to make sure that the filesystem supports encoding of
1595 * file handles so user can use name_to_handle_at() to compare fids
1596 * reported with events to the file handle of watched objects.
1602 * For sb/mount mark, we also need to make sure that the filesystem
1603 * supports decoding file handles, so user has a way to map back the
1604 * reported fids to filesystem objects.
1606 if (mark_type != FAN_MARK_INODE && !nop->fh_to_dentry)
1612 static int fanotify_events_supported(struct fsnotify_group *group,
1613 const struct path *path, __u64 mask,
1616 unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS;
1617 /* Strict validation of events in non-dir inode mask with v5.17+ APIs */
1618 bool strict_dir_events = FAN_GROUP_FLAG(group, FAN_REPORT_TARGET_FID) ||
1619 (mask & FAN_RENAME) ||
1620 (flags & FAN_MARK_IGNORE);
1623 * Some filesystems such as 'proc' acquire unusual locks when opening
1624 * files. For them fanotify permission events have high chances of
1625 * deadlocking the system - open done when reporting fanotify event
1626 * blocks on this "unusual" lock while another process holding the lock
1627 * waits for fanotify permission event to be answered. Just disallow
1628 * permission events for such filesystems.
1630 if (mask & FANOTIFY_PERM_EVENTS &&
1631 path->mnt->mnt_sb->s_type->fs_flags & FS_DISALLOW_NOTIFY_PERM)
1635 * mount and sb marks are not allowed on kernel internal pseudo fs,
1636 * like pipe_mnt, because that would subscribe to events on all the
1637 * anonynous pipes in the system.
1639 * SB_NOUSER covers all of the internal pseudo fs whose objects are not
1640 * exposed to user's mount namespace, but there are other SB_KERNMOUNT
1641 * fs, like nsfs, debugfs, for which the value of allowing sb and mount
1642 * mark is questionable. For now we leave them alone.
1644 if (mark_type != FAN_MARK_INODE &&
1645 path->mnt->mnt_sb->s_flags & SB_NOUSER)
1649 * We shouldn't have allowed setting dirent events and the directory
1650 * flags FAN_ONDIR and FAN_EVENT_ON_CHILD in mask of non-dir inode,
1651 * but because we always allowed it, error only when using new APIs.
1653 if (strict_dir_events && mark_type == FAN_MARK_INODE &&
1654 !d_is_dir(path->dentry) && (mask & FANOTIFY_DIRONLY_EVENT_BITS))
1660 static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
1661 int dfd, const char __user *pathname)
1663 struct inode *inode = NULL;
1664 struct vfsmount *mnt = NULL;
1665 struct fsnotify_group *group;
1668 __kernel_fsid_t __fsid, *fsid = NULL;
1669 u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS;
1670 unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS;
1671 unsigned int mark_cmd = flags & FANOTIFY_MARK_CMD_BITS;
1672 unsigned int ignore = flags & FANOTIFY_MARK_IGNORE_BITS;
1673 unsigned int obj_type, fid_mode;
1677 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
1678 __func__, fanotify_fd, flags, dfd, pathname, mask);
1680 /* we only use the lower 32 bits as of right now. */
1681 if (upper_32_bits(mask))
1684 if (flags & ~FANOTIFY_MARK_FLAGS)
1687 switch (mark_type) {
1688 case FAN_MARK_INODE:
1689 obj_type = FSNOTIFY_OBJ_TYPE_INODE;
1691 case FAN_MARK_MOUNT:
1692 obj_type = FSNOTIFY_OBJ_TYPE_VFSMOUNT;
1694 case FAN_MARK_FILESYSTEM:
1695 obj_type = FSNOTIFY_OBJ_TYPE_SB;
1703 case FAN_MARK_REMOVE:
1707 case FAN_MARK_FLUSH:
1708 if (flags & ~(FANOTIFY_MARK_TYPE_BITS | FAN_MARK_FLUSH))
1715 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
1716 valid_mask |= FANOTIFY_PERM_EVENTS;
1718 if (mask & ~valid_mask)
1722 /* We don't allow FAN_MARK_IGNORE & FAN_MARK_IGNORED_MASK together */
1723 if (ignore == (FAN_MARK_IGNORE | FAN_MARK_IGNORED_MASK))
1727 * Event flags (FAN_ONDIR, FAN_EVENT_ON_CHILD) have no effect with
1728 * FAN_MARK_IGNORED_MASK.
1730 if (ignore == FAN_MARK_IGNORED_MASK) {
1731 mask &= ~FANOTIFY_EVENT_FLAGS;
1732 umask = FANOTIFY_EVENT_FLAGS;
1735 f = fdget(fanotify_fd);
1736 if (unlikely(!f.file))
1739 /* verify that this is indeed an fanotify instance */
1741 if (unlikely(f.file->f_op != &fanotify_fops))
1743 group = f.file->private_data;
1746 * An unprivileged user is not allowed to setup mount nor filesystem
1747 * marks. This also includes setting up such marks by a group that
1748 * was initialized by an unprivileged user.
1751 if ((!capable(CAP_SYS_ADMIN) ||
1752 FAN_GROUP_FLAG(group, FANOTIFY_UNPRIV)) &&
1753 mark_type != FAN_MARK_INODE)
1757 * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
1758 * allowed to set permissions events.
1761 if (mask & FANOTIFY_PERM_EVENTS &&
1762 group->priority == FS_PRIO_0)
1765 if (mask & FAN_FS_ERROR &&
1766 mark_type != FAN_MARK_FILESYSTEM)
1770 * Evictable is only relevant for inode marks, because only inode object
1771 * can be evicted on memory pressure.
1773 if (flags & FAN_MARK_EVICTABLE &&
1774 mark_type != FAN_MARK_INODE)
1778 * Events that do not carry enough information to report
1779 * event->fd require a group that supports reporting fid. Those
1780 * events are not supported on a mount mark, because they do not
1781 * carry enough information (i.e. path) to be filtered by mount
1784 fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
1785 if (mask & ~(FANOTIFY_FD_EVENTS|FANOTIFY_EVENT_FLAGS) &&
1786 (!fid_mode || mark_type == FAN_MARK_MOUNT))
1790 * FAN_RENAME uses special info type records to report the old and
1791 * new parent+name. Reporting only old and new parent id is less
1792 * useful and was not implemented.
1794 if (mask & FAN_RENAME && !(fid_mode & FAN_REPORT_NAME))
1797 if (mark_cmd == FAN_MARK_FLUSH) {
1799 if (mark_type == FAN_MARK_MOUNT)
1800 fsnotify_clear_vfsmount_marks_by_group(group);
1801 else if (mark_type == FAN_MARK_FILESYSTEM)
1802 fsnotify_clear_sb_marks_by_group(group);
1804 fsnotify_clear_inode_marks_by_group(group);
1808 ret = fanotify_find_path(dfd, pathname, &path, flags,
1809 (mask & ALL_FSNOTIFY_EVENTS), obj_type);
1813 if (mark_cmd == FAN_MARK_ADD) {
1814 ret = fanotify_events_supported(group, &path, mask, flags);
1816 goto path_put_and_out;
1820 ret = fanotify_test_fsid(path.dentry, &__fsid);
1822 goto path_put_and_out;
1824 ret = fanotify_test_fid(path.dentry, flags);
1826 goto path_put_and_out;
1831 /* inode held in place by reference to path; group by fget on fd */
1832 if (mark_type == FAN_MARK_INODE)
1833 inode = path.dentry->d_inode;
1837 ret = mnt ? -EINVAL : -EISDIR;
1838 /* FAN_MARK_IGNORE requires SURV_MODIFY for sb/mount/dir marks */
1839 if (mark_cmd == FAN_MARK_ADD && ignore == FAN_MARK_IGNORE &&
1840 (mnt || S_ISDIR(inode->i_mode)) &&
1841 !(flags & FAN_MARK_IGNORED_SURV_MODIFY))
1842 goto path_put_and_out;
1844 /* Mask out FAN_EVENT_ON_CHILD flag for sb/mount/non-dir marks */
1845 if (mnt || !S_ISDIR(inode->i_mode)) {
1846 mask &= ~FAN_EVENT_ON_CHILD;
1847 umask = FAN_EVENT_ON_CHILD;
1849 * If group needs to report parent fid, register for getting
1850 * events with parent/name info for non-directory.
1852 if ((fid_mode & FAN_REPORT_DIR_FID) &&
1853 (flags & FAN_MARK_ADD) && !ignore)
1854 mask |= FAN_EVENT_ON_CHILD;
1857 /* create/update an inode mark */
1860 if (mark_type == FAN_MARK_MOUNT)
1861 ret = fanotify_add_vfsmount_mark(group, mnt, mask,
1863 else if (mark_type == FAN_MARK_FILESYSTEM)
1864 ret = fanotify_add_sb_mark(group, mnt->mnt_sb, mask,
1867 ret = fanotify_add_inode_mark(group, inode, mask,
1870 case FAN_MARK_REMOVE:
1871 if (mark_type == FAN_MARK_MOUNT)
1872 ret = fanotify_remove_vfsmount_mark(group, mnt, mask,
1874 else if (mark_type == FAN_MARK_FILESYSTEM)
1875 ret = fanotify_remove_sb_mark(group, mnt->mnt_sb, mask,
1878 ret = fanotify_remove_inode_mark(group, inode, mask,
1892 #ifndef CONFIG_ARCH_SPLIT_ARG64
1893 SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
1894 __u64, mask, int, dfd,
1895 const char __user *, pathname)
1897 return do_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname);
1901 #if defined(CONFIG_ARCH_SPLIT_ARG64) || defined(CONFIG_COMPAT)
1902 SYSCALL32_DEFINE6(fanotify_mark,
1903 int, fanotify_fd, unsigned int, flags,
1904 SC_ARG64(mask), int, dfd,
1905 const char __user *, pathname)
1907 return do_fanotify_mark(fanotify_fd, flags, SC_VAL64(__u64, mask),
1913 * fanotify_user_setup - Our initialization function. Note that we cannot return
1914 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
1915 * must result in panic().
1917 static int __init fanotify_user_setup(void)
1924 * Allow up to 1% of addressable memory to be accounted for per user
1925 * marks limited to the range [8192, 1048576]. mount and sb marks are
1926 * a lot cheaper than inode marks, but there is no reason for a user
1927 * to have many of those, so calculate by the cost of inode marks.
1929 max_marks = (((si.totalram - si.totalhigh) / 100) << PAGE_SHIFT) /
1931 max_marks = clamp(max_marks, FANOTIFY_OLD_DEFAULT_MAX_MARKS,
1932 FANOTIFY_DEFAULT_MAX_USER_MARKS);
1934 BUILD_BUG_ON(FANOTIFY_INIT_FLAGS & FANOTIFY_INTERNAL_GROUP_FLAGS);
1935 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 12);
1936 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 11);
1938 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark,
1939 SLAB_PANIC|SLAB_ACCOUNT);
1940 fanotify_fid_event_cachep = KMEM_CACHE(fanotify_fid_event,
1942 fanotify_path_event_cachep = KMEM_CACHE(fanotify_path_event,
1944 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
1945 fanotify_perm_event_cachep =
1946 KMEM_CACHE(fanotify_perm_event, SLAB_PANIC);
1949 fanotify_max_queued_events = FANOTIFY_DEFAULT_MAX_EVENTS;
1950 init_user_ns.ucount_max[UCOUNT_FANOTIFY_GROUPS] =
1951 FANOTIFY_DEFAULT_MAX_GROUPS;
1952 init_user_ns.ucount_max[UCOUNT_FANOTIFY_MARKS] = max_marks;
1953 fanotify_sysctls_init();
1957 device_initcall(fanotify_user_setup);