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
55 static int fanotify_fid_info_len(int fh_len)
57 return roundup(sizeof(struct fanotify_event_info_fid) +
58 sizeof(struct file_handle) + fh_len,
59 FANOTIFY_EVENT_ALIGN);
62 static int fanotify_event_info_len(struct fanotify_event *event)
64 int fh_len = fanotify_event_object_fh_len(event);
69 return fanotify_fid_info_len(fh_len);
73 * Get an fanotify notification event if one exists and is small
74 * enough to fit in "count". Return an error pointer if the count
75 * is not large enough. When permission event is dequeued, its state is
76 * updated accordingly.
78 static struct fanotify_event *get_one_event(struct fsnotify_group *group,
81 size_t event_size = FAN_EVENT_METADATA_LEN;
82 struct fanotify_event *event = NULL;
84 pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
86 spin_lock(&group->notification_lock);
87 if (fsnotify_notify_queue_is_empty(group))
90 if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) {
91 event_size += fanotify_event_info_len(
92 FANOTIFY_E(fsnotify_peek_first_event(group)));
95 if (event_size > count) {
96 event = ERR_PTR(-EINVAL);
99 event = FANOTIFY_E(fsnotify_remove_first_event(group));
100 if (fanotify_is_perm_event(event->mask))
101 FANOTIFY_PERM(event)->state = FAN_EVENT_REPORTED;
103 spin_unlock(&group->notification_lock);
107 static int create_fd(struct fsnotify_group *group, struct path *path,
111 struct file *new_file;
113 client_fd = get_unused_fd_flags(group->fanotify_data.f_flags);
118 * we need a new file handle for the userspace program so it can read even if it was
119 * originally opened O_WRONLY.
121 new_file = dentry_open(path,
122 group->fanotify_data.f_flags | FMODE_NONOTIFY,
124 if (IS_ERR(new_file)) {
126 * we still send an event even if we can't open the file. this
127 * can happen when say tasks are gone and we try to open their
128 * /proc files or we try to open a WRONLY file like in sysfs
129 * we just send the errno to userspace since there isn't much
132 put_unused_fd(client_fd);
133 client_fd = PTR_ERR(new_file);
142 * Finish processing of permission event by setting it to ANSWERED state and
143 * drop group->notification_lock.
145 static void finish_permission_event(struct fsnotify_group *group,
146 struct fanotify_perm_event *event,
147 unsigned int response)
148 __releases(&group->notification_lock)
150 bool destroy = false;
152 assert_spin_locked(&group->notification_lock);
153 event->response = response;
154 if (event->state == FAN_EVENT_CANCELED)
157 event->state = FAN_EVENT_ANSWERED;
158 spin_unlock(&group->notification_lock);
160 fsnotify_destroy_event(group, &event->fae.fse);
163 static int process_access_response(struct fsnotify_group *group,
164 struct fanotify_response *response_struct)
166 struct fanotify_perm_event *event;
167 int fd = response_struct->fd;
168 int response = response_struct->response;
170 pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
173 * make sure the response is valid, if invalid we do nothing and either
174 * userspace can send a valid response or we will clean it up after the
177 switch (response & ~FAN_AUDIT) {
188 if ((response & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT))
191 spin_lock(&group->notification_lock);
192 list_for_each_entry(event, &group->fanotify_data.access_list,
197 list_del_init(&event->fae.fse.list);
198 finish_permission_event(group, event, response);
199 wake_up(&group->fanotify_data.access_waitq);
202 spin_unlock(&group->notification_lock);
207 static int copy_fid_to_user(__kernel_fsid_t *fsid, struct fanotify_fh *fh,
210 struct fanotify_event_info_fid info = { };
211 struct file_handle handle = { };
212 unsigned char bounce[FANOTIFY_INLINE_FH_LEN], *fh_buf;
213 size_t fh_len = fh ? fh->len : 0;
214 size_t len = fanotify_fid_info_len(fh_len);
219 if (WARN_ON_ONCE(len < sizeof(info) + sizeof(handle) + fh_len))
222 /* Copy event info fid header followed by vaiable sized file handle */
223 info.hdr.info_type = FAN_EVENT_INFO_TYPE_FID;
226 if (copy_to_user(buf, &info, sizeof(info)))
231 handle.handle_type = fh->type;
232 handle.handle_bytes = fh_len;
233 if (copy_to_user(buf, &handle, sizeof(handle)))
236 buf += sizeof(handle);
237 len -= sizeof(handle);
239 * For an inline fh, copy through stack to exclude the copy from
240 * usercopy hardening protections.
242 fh_buf = fanotify_fh_buf(fh);
243 if (fh_len <= FANOTIFY_INLINE_FH_LEN) {
244 memcpy(bounce, fh_buf, fh_len);
247 if (copy_to_user(buf, fh_buf, fh_len))
253 WARN_ON_ONCE(len < 0 || len >= FANOTIFY_EVENT_ALIGN);
254 if (len > 0 && clear_user(buf, len))
260 static ssize_t copy_event_to_user(struct fsnotify_group *group,
261 struct fanotify_event *event,
262 char __user *buf, size_t count)
264 struct fanotify_event_metadata metadata;
265 struct path *path = fanotify_event_path(event);
266 struct file *f = NULL;
267 int ret, fd = FAN_NOFD;
269 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
271 metadata.event_len = FAN_EVENT_METADATA_LEN;
272 metadata.metadata_len = FAN_EVENT_METADATA_LEN;
273 metadata.vers = FANOTIFY_METADATA_VERSION;
274 metadata.reserved = 0;
275 metadata.mask = event->mask & FANOTIFY_OUTGOING_EVENTS;
276 metadata.pid = pid_vnr(event->pid);
278 if (fanotify_event_object_fh(event)) {
279 metadata.event_len += fanotify_event_info_len(event);
280 } else if (path && path->mnt && path->dentry) {
281 fd = create_fd(group, path, &f);
289 * Sanity check copy size in case get_one_event() and
290 * fill_event_metadata() event_len sizes ever get out of sync.
292 if (WARN_ON_ONCE(metadata.event_len > count))
295 if (copy_to_user(buf, &metadata, FAN_EVENT_METADATA_LEN))
298 if (fanotify_is_perm_event(event->mask))
299 FANOTIFY_PERM(event)->fd = fd;
303 } else if (fanotify_event_object_fh(event)) {
304 ret = copy_fid_to_user(fanotify_event_fsid(event),
305 fanotify_event_object_fh(event),
306 buf + FAN_EVENT_METADATA_LEN);
311 return metadata.event_len;
314 if (fd != FAN_NOFD) {
321 /* intofiy userspace file descriptor functions */
322 static __poll_t fanotify_poll(struct file *file, poll_table *wait)
324 struct fsnotify_group *group = file->private_data;
327 poll_wait(file, &group->notification_waitq, wait);
328 spin_lock(&group->notification_lock);
329 if (!fsnotify_notify_queue_is_empty(group))
330 ret = EPOLLIN | EPOLLRDNORM;
331 spin_unlock(&group->notification_lock);
336 static ssize_t fanotify_read(struct file *file, char __user *buf,
337 size_t count, loff_t *pos)
339 struct fsnotify_group *group;
340 struct fanotify_event *event;
343 DEFINE_WAIT_FUNC(wait, woken_wake_function);
346 group = file->private_data;
348 pr_debug("%s: group=%p\n", __func__, group);
350 add_wait_queue(&group->notification_waitq, &wait);
352 event = get_one_event(group, count);
354 ret = PTR_ERR(event);
360 if (file->f_flags & O_NONBLOCK)
364 if (signal_pending(current))
370 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
374 ret = copy_event_to_user(group, event, buf, count);
375 if (unlikely(ret == -EOPENSTALE)) {
377 * We cannot report events with stale fd so drop it.
378 * Setting ret to 0 will continue the event loop and
379 * do the right thing if there are no more events to
380 * read (i.e. return bytes read, -EAGAIN or wait).
386 * Permission events get queued to wait for response. Other
387 * events can be destroyed now.
389 if (!fanotify_is_perm_event(event->mask)) {
390 fsnotify_destroy_event(group, &event->fse);
393 spin_lock(&group->notification_lock);
394 finish_permission_event(group,
395 FANOTIFY_PERM(event), FAN_DENY);
396 wake_up(&group->fanotify_data.access_waitq);
398 spin_lock(&group->notification_lock);
399 list_add_tail(&event->fse.list,
400 &group->fanotify_data.access_list);
401 spin_unlock(&group->notification_lock);
409 remove_wait_queue(&group->notification_waitq, &wait);
411 if (start != buf && ret != -EFAULT)
416 static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
418 struct fanotify_response response = { .fd = -1, .response = -1 };
419 struct fsnotify_group *group;
422 if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
425 group = file->private_data;
427 if (count > sizeof(response))
428 count = sizeof(response);
430 pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
432 if (copy_from_user(&response, buf, count))
435 ret = process_access_response(group, &response);
442 static int fanotify_release(struct inode *ignored, struct file *file)
444 struct fsnotify_group *group = file->private_data;
447 * Stop new events from arriving in the notification queue. since
448 * userspace cannot use fanotify fd anymore, no event can enter or
449 * leave access_list by now either.
451 fsnotify_group_stop_queueing(group);
454 * Process all permission events on access_list and notification queue
455 * and simulate reply from userspace.
457 spin_lock(&group->notification_lock);
458 while (!list_empty(&group->fanotify_data.access_list)) {
459 struct fanotify_perm_event *event;
461 event = list_first_entry(&group->fanotify_data.access_list,
462 struct fanotify_perm_event, fae.fse.list);
463 list_del_init(&event->fae.fse.list);
464 finish_permission_event(group, event, FAN_ALLOW);
465 spin_lock(&group->notification_lock);
469 * Destroy all non-permission events. For permission events just
470 * dequeue them and set the response. They will be freed once the
471 * response is consumed and fanotify_get_response() returns.
473 while (!fsnotify_notify_queue_is_empty(group)) {
474 struct fanotify_event *event;
476 event = FANOTIFY_E(fsnotify_remove_first_event(group));
477 if (!(event->mask & FANOTIFY_PERM_EVENTS)) {
478 spin_unlock(&group->notification_lock);
479 fsnotify_destroy_event(group, &event->fse);
481 finish_permission_event(group, FANOTIFY_PERM(event),
484 spin_lock(&group->notification_lock);
486 spin_unlock(&group->notification_lock);
488 /* Response for all permission events it set, wakeup waiters */
489 wake_up(&group->fanotify_data.access_waitq);
491 /* matches the fanotify_init->fsnotify_alloc_group */
492 fsnotify_destroy_group(group);
497 static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
499 struct fsnotify_group *group;
500 struct fsnotify_event *fsn_event;
505 group = file->private_data;
507 p = (void __user *) arg;
511 spin_lock(&group->notification_lock);
512 list_for_each_entry(fsn_event, &group->notification_list, list)
513 send_len += FAN_EVENT_METADATA_LEN;
514 spin_unlock(&group->notification_lock);
515 ret = put_user(send_len, (int __user *) p);
522 static const struct file_operations fanotify_fops = {
523 .show_fdinfo = fanotify_show_fdinfo,
524 .poll = fanotify_poll,
525 .read = fanotify_read,
526 .write = fanotify_write,
528 .release = fanotify_release,
529 .unlocked_ioctl = fanotify_ioctl,
530 .compat_ioctl = compat_ptr_ioctl,
531 .llseek = noop_llseek,
534 static int fanotify_find_path(int dfd, const char __user *filename,
535 struct path *path, unsigned int flags, __u64 mask,
536 unsigned int obj_type)
540 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
541 dfd, filename, flags);
543 if (filename == NULL) {
544 struct fd f = fdget(dfd);
551 if ((flags & FAN_MARK_ONLYDIR) &&
552 !(S_ISDIR(file_inode(f.file)->i_mode))) {
557 *path = f.file->f_path;
561 unsigned int lookup_flags = 0;
563 if (!(flags & FAN_MARK_DONT_FOLLOW))
564 lookup_flags |= LOOKUP_FOLLOW;
565 if (flags & FAN_MARK_ONLYDIR)
566 lookup_flags |= LOOKUP_DIRECTORY;
568 ret = user_path_at(dfd, filename, lookup_flags, path);
573 /* you can only watch an inode if you have read permissions on it */
574 ret = inode_permission(path->dentry->d_inode, MAY_READ);
580 ret = security_path_notify(path, mask, obj_type);
588 static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
595 spin_lock(&fsn_mark->lock);
596 if (!(flags & FAN_MARK_IGNORED_MASK)) {
597 oldmask = fsn_mark->mask;
598 fsn_mark->mask &= ~mask;
600 fsn_mark->ignored_mask &= ~mask;
602 *destroy = !(fsn_mark->mask | fsn_mark->ignored_mask);
603 spin_unlock(&fsn_mark->lock);
605 return mask & oldmask;
608 static int fanotify_remove_mark(struct fsnotify_group *group,
609 fsnotify_connp_t *connp, __u32 mask,
612 struct fsnotify_mark *fsn_mark = NULL;
616 mutex_lock(&group->mark_mutex);
617 fsn_mark = fsnotify_find_mark(connp, group);
619 mutex_unlock(&group->mark_mutex);
623 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
625 if (removed & fsnotify_conn_mask(fsn_mark->connector))
626 fsnotify_recalc_mask(fsn_mark->connector);
628 fsnotify_detach_mark(fsn_mark);
629 mutex_unlock(&group->mark_mutex);
631 fsnotify_free_mark(fsn_mark);
633 /* matches the fsnotify_find_mark() */
634 fsnotify_put_mark(fsn_mark);
638 static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
639 struct vfsmount *mnt, __u32 mask,
642 return fanotify_remove_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
646 static int fanotify_remove_sb_mark(struct fsnotify_group *group,
647 struct super_block *sb, __u32 mask,
650 return fanotify_remove_mark(group, &sb->s_fsnotify_marks, mask, flags);
653 static int fanotify_remove_inode_mark(struct fsnotify_group *group,
654 struct inode *inode, __u32 mask,
657 return fanotify_remove_mark(group, &inode->i_fsnotify_marks, mask,
661 static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
667 spin_lock(&fsn_mark->lock);
668 if (!(flags & FAN_MARK_IGNORED_MASK)) {
669 oldmask = fsn_mark->mask;
670 fsn_mark->mask |= mask;
672 fsn_mark->ignored_mask |= mask;
673 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
674 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
676 spin_unlock(&fsn_mark->lock);
678 return mask & ~oldmask;
681 static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
682 fsnotify_connp_t *connp,
684 __kernel_fsid_t *fsid)
686 struct fsnotify_mark *mark;
689 if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
690 return ERR_PTR(-ENOSPC);
692 mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
694 return ERR_PTR(-ENOMEM);
696 fsnotify_init_mark(mark, group);
697 ret = fsnotify_add_mark_locked(mark, connp, type, 0, fsid);
699 fsnotify_put_mark(mark);
707 static int fanotify_add_mark(struct fsnotify_group *group,
708 fsnotify_connp_t *connp, unsigned int type,
709 __u32 mask, unsigned int flags,
710 __kernel_fsid_t *fsid)
712 struct fsnotify_mark *fsn_mark;
715 mutex_lock(&group->mark_mutex);
716 fsn_mark = fsnotify_find_mark(connp, group);
718 fsn_mark = fanotify_add_new_mark(group, connp, type, fsid);
719 if (IS_ERR(fsn_mark)) {
720 mutex_unlock(&group->mark_mutex);
721 return PTR_ERR(fsn_mark);
724 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
725 if (added & ~fsnotify_conn_mask(fsn_mark->connector))
726 fsnotify_recalc_mask(fsn_mark->connector);
727 mutex_unlock(&group->mark_mutex);
729 fsnotify_put_mark(fsn_mark);
733 static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
734 struct vfsmount *mnt, __u32 mask,
735 unsigned int flags, __kernel_fsid_t *fsid)
737 return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
738 FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags, fsid);
741 static int fanotify_add_sb_mark(struct fsnotify_group *group,
742 struct super_block *sb, __u32 mask,
743 unsigned int flags, __kernel_fsid_t *fsid)
745 return fanotify_add_mark(group, &sb->s_fsnotify_marks,
746 FSNOTIFY_OBJ_TYPE_SB, mask, flags, fsid);
749 static int fanotify_add_inode_mark(struct fsnotify_group *group,
750 struct inode *inode, __u32 mask,
751 unsigned int flags, __kernel_fsid_t *fsid)
753 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
756 * If some other task has this inode open for write we should not add
757 * an ignored mark, unless that ignored mark is supposed to survive
758 * modification changes anyway.
760 if ((flags & FAN_MARK_IGNORED_MASK) &&
761 !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
762 inode_is_open_for_write(inode))
765 return fanotify_add_mark(group, &inode->i_fsnotify_marks,
766 FSNOTIFY_OBJ_TYPE_INODE, mask, flags, fsid);
769 /* fanotify syscalls */
770 SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
772 struct fsnotify_group *group;
774 struct user_struct *user;
775 struct fanotify_event *oevent;
777 pr_debug("%s: flags=%x event_f_flags=%x\n",
778 __func__, flags, event_f_flags);
780 if (!capable(CAP_SYS_ADMIN))
783 #ifdef CONFIG_AUDITSYSCALL
784 if (flags & ~(FANOTIFY_INIT_FLAGS | FAN_ENABLE_AUDIT))
786 if (flags & ~FANOTIFY_INIT_FLAGS)
790 if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS)
793 switch (event_f_flags & O_ACCMODE) {
802 if ((flags & FAN_REPORT_FID) &&
803 (flags & FANOTIFY_CLASS_BITS) != FAN_CLASS_NOTIF)
806 user = get_current_user();
807 if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {
812 f_flags = O_RDWR | FMODE_NONOTIFY;
813 if (flags & FAN_CLOEXEC)
814 f_flags |= O_CLOEXEC;
815 if (flags & FAN_NONBLOCK)
816 f_flags |= O_NONBLOCK;
818 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
819 group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
822 return PTR_ERR(group);
825 group->fanotify_data.user = user;
826 group->fanotify_data.flags = flags;
827 atomic_inc(&user->fanotify_listeners);
828 group->memcg = get_mem_cgroup_from_mm(current->mm);
830 oevent = fanotify_alloc_event(group, NULL, FS_Q_OVERFLOW, NULL,
831 FSNOTIFY_EVENT_NONE, NULL, NULL);
832 if (unlikely(!oevent)) {
834 goto out_destroy_group;
836 group->overflow_event = &oevent->fse;
838 if (force_o_largefile())
839 event_f_flags |= O_LARGEFILE;
840 group->fanotify_data.f_flags = event_f_flags;
841 init_waitqueue_head(&group->fanotify_data.access_waitq);
842 INIT_LIST_HEAD(&group->fanotify_data.access_list);
843 switch (flags & FANOTIFY_CLASS_BITS) {
844 case FAN_CLASS_NOTIF:
845 group->priority = FS_PRIO_0;
847 case FAN_CLASS_CONTENT:
848 group->priority = FS_PRIO_1;
850 case FAN_CLASS_PRE_CONTENT:
851 group->priority = FS_PRIO_2;
855 goto out_destroy_group;
858 if (flags & FAN_UNLIMITED_QUEUE) {
860 if (!capable(CAP_SYS_ADMIN))
861 goto out_destroy_group;
862 group->max_events = UINT_MAX;
864 group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
867 if (flags & FAN_UNLIMITED_MARKS) {
869 if (!capable(CAP_SYS_ADMIN))
870 goto out_destroy_group;
871 group->fanotify_data.max_marks = UINT_MAX;
873 group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS;
876 if (flags & FAN_ENABLE_AUDIT) {
878 if (!capable(CAP_AUDIT_WRITE))
879 goto out_destroy_group;
882 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
884 goto out_destroy_group;
889 fsnotify_destroy_group(group);
893 /* Check if filesystem can encode a unique fid */
894 static int fanotify_test_fid(struct path *path, __kernel_fsid_t *fsid)
896 __kernel_fsid_t root_fsid;
900 * Make sure path is not in filesystem with zero fsid (e.g. tmpfs).
902 err = vfs_get_fsid(path->dentry, fsid);
906 if (!fsid->val[0] && !fsid->val[1])
910 * Make sure path is not inside a filesystem subvolume (e.g. btrfs)
911 * which uses a different fsid than sb root.
913 err = vfs_get_fsid(path->dentry->d_sb->s_root, &root_fsid);
917 if (root_fsid.val[0] != fsid->val[0] ||
918 root_fsid.val[1] != fsid->val[1])
922 * We need to make sure that the file system supports at least
923 * encoding a file handle so user can use name_to_handle_at() to
924 * compare fid returned with event to the file handle of watched
925 * objects. However, name_to_handle_at() requires that the
926 * filesystem also supports decoding file handles.
928 if (!path->dentry->d_sb->s_export_op ||
929 !path->dentry->d_sb->s_export_op->fh_to_dentry)
935 static int fanotify_events_supported(struct path *path, __u64 mask)
938 * Some filesystems such as 'proc' acquire unusual locks when opening
939 * files. For them fanotify permission events have high chances of
940 * deadlocking the system - open done when reporting fanotify event
941 * blocks on this "unusual" lock while another process holding the lock
942 * waits for fanotify permission event to be answered. Just disallow
943 * permission events for such filesystems.
945 if (mask & FANOTIFY_PERM_EVENTS &&
946 path->mnt->mnt_sb->s_type->fs_flags & FS_DISALLOW_NOTIFY_PERM)
951 static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
952 int dfd, const char __user *pathname)
954 struct inode *inode = NULL;
955 struct vfsmount *mnt = NULL;
956 struct fsnotify_group *group;
959 __kernel_fsid_t __fsid, *fsid = NULL;
960 u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS;
961 unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS;
962 unsigned int obj_type;
965 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
966 __func__, fanotify_fd, flags, dfd, pathname, mask);
968 /* we only use the lower 32 bits as of right now. */
969 if (mask & ((__u64)0xffffffff << 32))
972 if (flags & ~FANOTIFY_MARK_FLAGS)
977 obj_type = FSNOTIFY_OBJ_TYPE_INODE;
980 obj_type = FSNOTIFY_OBJ_TYPE_VFSMOUNT;
982 case FAN_MARK_FILESYSTEM:
983 obj_type = FSNOTIFY_OBJ_TYPE_SB;
989 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
990 case FAN_MARK_ADD: /* fallthrough */
991 case FAN_MARK_REMOVE:
996 if (flags & ~(FANOTIFY_MARK_TYPE_BITS | FAN_MARK_FLUSH))
1003 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
1004 valid_mask |= FANOTIFY_PERM_EVENTS;
1006 if (mask & ~valid_mask)
1009 f = fdget(fanotify_fd);
1010 if (unlikely(!f.file))
1013 /* verify that this is indeed an fanotify instance */
1015 if (unlikely(f.file->f_op != &fanotify_fops))
1017 group = f.file->private_data;
1020 * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
1021 * allowed to set permissions events.
1024 if (mask & FANOTIFY_PERM_EVENTS &&
1025 group->priority == FS_PRIO_0)
1029 * Events with data type inode do not carry enough information to report
1030 * event->fd, so we do not allow setting a mask for inode events unless
1031 * group supports reporting fid.
1032 * inode events are not supported on a mount mark, because they do not
1033 * carry enough information (i.e. path) to be filtered by mount point.
1035 if (mask & FANOTIFY_INODE_EVENTS &&
1036 (!FAN_GROUP_FLAG(group, FAN_REPORT_FID) ||
1037 mark_type == FAN_MARK_MOUNT))
1040 if (flags & FAN_MARK_FLUSH) {
1042 if (mark_type == FAN_MARK_MOUNT)
1043 fsnotify_clear_vfsmount_marks_by_group(group);
1044 else if (mark_type == FAN_MARK_FILESYSTEM)
1045 fsnotify_clear_sb_marks_by_group(group);
1047 fsnotify_clear_inode_marks_by_group(group);
1051 ret = fanotify_find_path(dfd, pathname, &path, flags,
1052 (mask & ALL_FSNOTIFY_EVENTS), obj_type);
1056 if (flags & FAN_MARK_ADD) {
1057 ret = fanotify_events_supported(&path, mask);
1059 goto path_put_and_out;
1062 if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) {
1063 ret = fanotify_test_fid(&path, &__fsid);
1065 goto path_put_and_out;
1070 /* inode held in place by reference to path; group by fget on fd */
1071 if (mark_type == FAN_MARK_INODE)
1072 inode = path.dentry->d_inode;
1076 /* create/update an inode mark */
1077 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
1079 if (mark_type == FAN_MARK_MOUNT)
1080 ret = fanotify_add_vfsmount_mark(group, mnt, mask,
1082 else if (mark_type == FAN_MARK_FILESYSTEM)
1083 ret = fanotify_add_sb_mark(group, mnt->mnt_sb, mask,
1086 ret = fanotify_add_inode_mark(group, inode, mask,
1089 case FAN_MARK_REMOVE:
1090 if (mark_type == FAN_MARK_MOUNT)
1091 ret = fanotify_remove_vfsmount_mark(group, mnt, mask,
1093 else if (mark_type == FAN_MARK_FILESYSTEM)
1094 ret = fanotify_remove_sb_mark(group, mnt->mnt_sb, mask,
1097 ret = fanotify_remove_inode_mark(group, inode, mask,
1111 SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
1112 __u64, mask, int, dfd,
1113 const char __user *, pathname)
1115 return do_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname);
1118 #ifdef CONFIG_COMPAT
1119 COMPAT_SYSCALL_DEFINE6(fanotify_mark,
1120 int, fanotify_fd, unsigned int, flags,
1121 __u32, mask0, __u32, mask1, int, dfd,
1122 const char __user *, pathname)
1124 return do_fanotify_mark(fanotify_fd, flags,
1126 ((__u64)mask0 << 32) | mask1,
1128 ((__u64)mask1 << 32) | mask0,
1135 * fanotify_user_setup - Our initialization function. Note that we cannot return
1136 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
1137 * must result in panic().
1139 static int __init fanotify_user_setup(void)
1141 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 8);
1142 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9);
1144 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark,
1145 SLAB_PANIC|SLAB_ACCOUNT);
1146 fanotify_fid_event_cachep = KMEM_CACHE(fanotify_fid_event,
1148 fanotify_path_event_cachep = KMEM_CACHE(fanotify_path_event,
1150 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
1151 fanotify_perm_event_cachep =
1152 KMEM_CACHE(fanotify_perm_event, SLAB_PANIC);
1157 device_initcall(fanotify_user_setup);