1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * fs/inotify_user.c - inotify support for userspace
6 * John McCutchan <ttb@tentacle.dhs.org>
7 * Robert Love <rml@novell.com>
9 * Copyright (C) 2005 John McCutchan
10 * Copyright 2006 Hewlett-Packard Development Company, L.P.
12 * Copyright (C) 2009 Eric Paris <Red Hat Inc>
13 * inotify was largely rewriten to make use of the fsnotify infrastructure
16 #include <linux/file.h>
17 #include <linux/fs.h> /* struct inode */
18 #include <linux/fsnotify_backend.h>
19 #include <linux/idr.h>
20 #include <linux/init.h> /* fs_initcall */
21 #include <linux/inotify.h>
22 #include <linux/kernel.h> /* roundup() */
23 #include <linux/namei.h> /* LOOKUP_FOLLOW */
24 #include <linux/sched/signal.h>
25 #include <linux/slab.h> /* struct kmem_cache */
26 #include <linux/syscalls.h>
27 #include <linux/types.h>
28 #include <linux/anon_inodes.h>
29 #include <linux/uaccess.h>
30 #include <linux/poll.h>
31 #include <linux/wait.h>
32 #include <linux/memcontrol.h>
33 #include <linux/security.h>
36 #include "../fdinfo.h"
38 #include <asm/ioctls.h>
41 * An inotify watch requires allocating an inotify_inode_mark structure as
42 * well as pinning the watched inode. Doubling the size of a VFS inode
43 * should be more than enough to cover the additional filesystem inode
46 #define INOTIFY_WATCH_COST (sizeof(struct inotify_inode_mark) + \
47 2 * sizeof(struct inode))
49 /* configurable via /proc/sys/fs/inotify/ */
50 static int inotify_max_queued_events __read_mostly;
52 struct kmem_cache *inotify_inode_mark_cachep __read_mostly;
56 #include <linux/sysctl.h>
58 static long it_zero = 0;
59 static long it_int_max = INT_MAX;
61 static struct ctl_table inotify_table[] = {
63 .procname = "max_user_instances",
64 .data = &init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES],
65 .maxlen = sizeof(long),
67 .proc_handler = proc_doulongvec_minmax,
69 .extra2 = &it_int_max,
72 .procname = "max_user_watches",
73 .data = &init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES],
74 .maxlen = sizeof(long),
76 .proc_handler = proc_doulongvec_minmax,
78 .extra2 = &it_int_max,
81 .procname = "max_queued_events",
82 .data = &inotify_max_queued_events,
83 .maxlen = sizeof(int),
85 .proc_handler = proc_dointvec_minmax,
91 static void __init inotify_sysctls_init(void)
93 register_sysctl("fs/inotify", inotify_table);
97 #define inotify_sysctls_init() do { } while (0)
98 #endif /* CONFIG_SYSCTL */
100 static inline __u32 inotify_arg_to_mask(struct inode *inode, u32 arg)
105 * Everything should receive events when the inode is unmounted.
106 * All directories care about children.
109 if (S_ISDIR(inode->i_mode))
110 mask |= FS_EVENT_ON_CHILD;
112 /* mask off the flags used to open the fd */
113 mask |= (arg & INOTIFY_USER_MASK);
118 #define INOTIFY_MARK_FLAGS \
119 (FSNOTIFY_MARK_FLAG_EXCL_UNLINK | FSNOTIFY_MARK_FLAG_IN_ONESHOT)
121 static inline unsigned int inotify_arg_to_flags(u32 arg)
123 unsigned int flags = 0;
125 if (arg & IN_EXCL_UNLINK)
126 flags |= FSNOTIFY_MARK_FLAG_EXCL_UNLINK;
127 if (arg & IN_ONESHOT)
128 flags |= FSNOTIFY_MARK_FLAG_IN_ONESHOT;
133 static inline u32 inotify_mask_to_arg(__u32 mask)
135 return mask & (IN_ALL_EVENTS | IN_ISDIR | IN_UNMOUNT | IN_IGNORED |
139 /* inotify userspace file descriptor functions */
140 static __poll_t inotify_poll(struct file *file, poll_table *wait)
142 struct fsnotify_group *group = file->private_data;
145 poll_wait(file, &group->notification_waitq, wait);
146 spin_lock(&group->notification_lock);
147 if (!fsnotify_notify_queue_is_empty(group))
148 ret = EPOLLIN | EPOLLRDNORM;
149 spin_unlock(&group->notification_lock);
154 static int round_event_name_len(struct fsnotify_event *fsn_event)
156 struct inotify_event_info *event;
158 event = INOTIFY_E(fsn_event);
159 if (!event->name_len)
161 return roundup(event->name_len + 1, sizeof(struct inotify_event));
165 * Get an inotify_kernel_event if one exists and is small
166 * enough to fit in "count". Return an error pointer if
169 * Called with the group->notification_lock held.
171 static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
174 size_t event_size = sizeof(struct inotify_event);
175 struct fsnotify_event *event;
177 event = fsnotify_peek_first_event(group);
181 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
183 event_size += round_event_name_len(event);
184 if (event_size > count)
185 return ERR_PTR(-EINVAL);
187 /* held the notification_lock the whole time, so this is the
188 * same event we peeked above */
189 fsnotify_remove_first_event(group);
195 * Copy an event to user space, returning how much we copied.
197 * We already checked that the event size is smaller than the
198 * buffer we had in "get_one_event()" above.
200 static ssize_t copy_event_to_user(struct fsnotify_group *group,
201 struct fsnotify_event *fsn_event,
204 struct inotify_event inotify_event;
205 struct inotify_event_info *event;
206 size_t event_size = sizeof(struct inotify_event);
210 pr_debug("%s: group=%p event=%p\n", __func__, group, fsn_event);
212 event = INOTIFY_E(fsn_event);
213 name_len = event->name_len;
215 * round up name length so it is a multiple of event_size
216 * plus an extra byte for the terminating '\0'.
218 pad_name_len = round_event_name_len(fsn_event);
219 inotify_event.len = pad_name_len;
220 inotify_event.mask = inotify_mask_to_arg(event->mask);
221 inotify_event.wd = event->wd;
222 inotify_event.cookie = event->sync_cookie;
224 /* send the main event */
225 if (copy_to_user(buf, &inotify_event, event_size))
231 * fsnotify only stores the pathname, so here we have to send the pathname
232 * and then pad that pathname out to a multiple of sizeof(inotify_event)
236 /* copy the path name */
237 if (copy_to_user(buf, event->name, name_len))
241 /* fill userspace with 0's */
242 if (clear_user(buf, pad_name_len - name_len))
244 event_size += pad_name_len;
250 static ssize_t inotify_read(struct file *file, char __user *buf,
251 size_t count, loff_t *pos)
253 struct fsnotify_group *group;
254 struct fsnotify_event *kevent;
257 DEFINE_WAIT_FUNC(wait, woken_wake_function);
260 group = file->private_data;
262 add_wait_queue(&group->notification_waitq, &wait);
264 spin_lock(&group->notification_lock);
265 kevent = get_one_event(group, count);
266 spin_unlock(&group->notification_lock);
268 pr_debug("%s: group=%p kevent=%p\n", __func__, group, kevent);
271 ret = PTR_ERR(kevent);
274 ret = copy_event_to_user(group, kevent, buf);
275 fsnotify_destroy_event(group, kevent);
284 if (file->f_flags & O_NONBLOCK)
287 if (signal_pending(current))
293 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
295 remove_wait_queue(&group->notification_waitq, &wait);
297 if (start != buf && ret != -EFAULT)
302 static int inotify_release(struct inode *ignored, struct file *file)
304 struct fsnotify_group *group = file->private_data;
306 pr_debug("%s: group=%p\n", __func__, group);
308 /* free this group, matching get was inotify_init->fsnotify_obtain_group */
309 fsnotify_destroy_group(group);
314 static long inotify_ioctl(struct file *file, unsigned int cmd,
317 struct fsnotify_group *group;
318 struct fsnotify_event *fsn_event;
323 group = file->private_data;
324 p = (void __user *) arg;
326 pr_debug("%s: group=%p cmd=%u\n", __func__, group, cmd);
330 spin_lock(&group->notification_lock);
331 list_for_each_entry(fsn_event, &group->notification_list,
333 send_len += sizeof(struct inotify_event);
334 send_len += round_event_name_len(fsn_event);
336 spin_unlock(&group->notification_lock);
337 ret = put_user(send_len, (int __user *) p);
339 #ifdef CONFIG_CHECKPOINT_RESTORE
340 case INOTIFY_IOC_SETNEXTWD:
342 if (arg >= 1 && arg <= INT_MAX) {
343 struct inotify_group_private_data *data;
345 data = &group->inotify_data;
346 spin_lock(&data->idr_lock);
347 idr_set_cursor(&data->idr, (unsigned int)arg);
348 spin_unlock(&data->idr_lock);
352 #endif /* CONFIG_CHECKPOINT_RESTORE */
358 static const struct file_operations inotify_fops = {
359 .show_fdinfo = inotify_show_fdinfo,
360 .poll = inotify_poll,
361 .read = inotify_read,
362 .fasync = fsnotify_fasync,
363 .release = inotify_release,
364 .unlocked_ioctl = inotify_ioctl,
365 .compat_ioctl = inotify_ioctl,
366 .llseek = noop_llseek,
371 * find_inode - resolve a user-given path to a specific inode
373 static int inotify_find_inode(const char __user *dirname, struct path *path,
374 unsigned int flags, __u64 mask)
378 error = user_path_at(AT_FDCWD, dirname, flags, path);
381 /* you can only watch an inode if you have read permissions on it */
382 error = path_permission(path, MAY_READ);
387 error = security_path_notify(path, mask,
388 FSNOTIFY_OBJ_TYPE_INODE);
395 static int inotify_add_to_idr(struct idr *idr, spinlock_t *idr_lock,
396 struct inotify_inode_mark *i_mark)
400 idr_preload(GFP_KERNEL);
403 ret = idr_alloc_cyclic(idr, i_mark, 1, 0, GFP_NOWAIT);
405 /* we added the mark to the idr, take a reference */
407 fsnotify_get_mark(&i_mark->fsn_mark);
410 spin_unlock(idr_lock);
412 return ret < 0 ? ret : 0;
415 static struct inotify_inode_mark *inotify_idr_find_locked(struct fsnotify_group *group,
418 struct idr *idr = &group->inotify_data.idr;
419 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
420 struct inotify_inode_mark *i_mark;
422 assert_spin_locked(idr_lock);
424 i_mark = idr_find(idr, wd);
426 struct fsnotify_mark *fsn_mark = &i_mark->fsn_mark;
428 fsnotify_get_mark(fsn_mark);
429 /* One ref for being in the idr, one ref we just took */
430 BUG_ON(refcount_read(&fsn_mark->refcnt) < 2);
436 static struct inotify_inode_mark *inotify_idr_find(struct fsnotify_group *group,
439 struct inotify_inode_mark *i_mark;
440 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
443 i_mark = inotify_idr_find_locked(group, wd);
444 spin_unlock(idr_lock);
450 * Remove the mark from the idr (if present) and drop the reference
451 * on the mark because it was in the idr.
453 static void inotify_remove_from_idr(struct fsnotify_group *group,
454 struct inotify_inode_mark *i_mark)
456 struct idr *idr = &group->inotify_data.idr;
457 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
458 struct inotify_inode_mark *found_i_mark = NULL;
465 * does this i_mark think it is in the idr? we shouldn't get called
469 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
470 __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
474 /* Lets look in the idr to see if we find it */
475 found_i_mark = inotify_idr_find_locked(group, wd);
476 if (unlikely(!found_i_mark)) {
477 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
478 __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
483 * We found an mark in the idr at the right wd, but it's
484 * not the mark we were told to remove. eparis seriously
485 * fucked up somewhere.
487 if (unlikely(found_i_mark != i_mark)) {
488 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p "
489 "found_i_mark=%p found_i_mark->wd=%d "
490 "found_i_mark->group=%p\n", __func__, i_mark,
491 i_mark->wd, i_mark->fsn_mark.group, found_i_mark,
492 found_i_mark->wd, found_i_mark->fsn_mark.group);
497 * One ref for being in the idr
498 * one ref grabbed by inotify_idr_find
500 if (unlikely(refcount_read(&i_mark->fsn_mark.refcnt) < 2)) {
501 printk(KERN_ERR "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
502 __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
503 /* we can't really recover with bad ref cnting.. */
508 /* Removed from the idr, drop that ref. */
509 fsnotify_put_mark(&i_mark->fsn_mark);
512 spin_unlock(idr_lock);
513 /* match the ref taken by inotify_idr_find_locked() */
515 fsnotify_put_mark(&found_i_mark->fsn_mark);
519 * Send IN_IGNORED for this wd, remove this wd from the idr.
521 void inotify_ignored_and_remove_idr(struct fsnotify_mark *fsn_mark,
522 struct fsnotify_group *group)
524 struct inotify_inode_mark *i_mark;
526 /* Queue ignore event for the watch */
527 inotify_handle_inode_event(fsn_mark, FS_IN_IGNORED, NULL, NULL, NULL,
530 i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
531 /* remove this mark from the idr */
532 inotify_remove_from_idr(group, i_mark);
534 dec_inotify_watches(group->inotify_data.ucounts);
537 static int inotify_update_existing_watch(struct fsnotify_group *group,
541 struct fsnotify_mark *fsn_mark;
542 struct inotify_inode_mark *i_mark;
543 __u32 old_mask, new_mask;
544 int replace = !(arg & IN_MASK_ADD);
545 int create = (arg & IN_MASK_CREATE);
548 fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
556 i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
558 spin_lock(&fsn_mark->lock);
559 old_mask = fsn_mark->mask;
562 fsn_mark->flags &= ~INOTIFY_MARK_FLAGS;
564 fsn_mark->mask |= inotify_arg_to_mask(inode, arg);
565 fsn_mark->flags |= inotify_arg_to_flags(arg);
566 new_mask = fsn_mark->mask;
567 spin_unlock(&fsn_mark->lock);
569 if (old_mask != new_mask) {
570 /* more bits in old than in new? */
571 int dropped = (old_mask & ~new_mask);
572 /* more bits in this fsn_mark than the inode's mask? */
573 int do_inode = (new_mask & ~inode->i_fsnotify_mask);
575 /* update the inode with this new fsn_mark */
576 if (dropped || do_inode)
577 fsnotify_recalc_mask(inode->i_fsnotify_marks);
585 /* match the get from fsnotify_find_mark() */
586 fsnotify_put_mark(fsn_mark);
591 static int inotify_new_watch(struct fsnotify_group *group,
595 struct inotify_inode_mark *tmp_i_mark;
597 struct idr *idr = &group->inotify_data.idr;
598 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
600 tmp_i_mark = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL);
601 if (unlikely(!tmp_i_mark))
604 fsnotify_init_mark(&tmp_i_mark->fsn_mark, group);
605 tmp_i_mark->fsn_mark.mask = inotify_arg_to_mask(inode, arg);
606 tmp_i_mark->fsn_mark.flags = inotify_arg_to_flags(arg);
609 ret = inotify_add_to_idr(idr, idr_lock, tmp_i_mark);
613 /* increment the number of watches the user has */
614 if (!inc_inotify_watches(group->inotify_data.ucounts)) {
615 inotify_remove_from_idr(group, tmp_i_mark);
620 /* we are on the idr, now get on the inode */
621 ret = fsnotify_add_inode_mark_locked(&tmp_i_mark->fsn_mark, inode, 0);
623 /* we failed to get on the inode, get off the idr */
624 inotify_remove_from_idr(group, tmp_i_mark);
629 /* return the watch descriptor for this new mark */
630 ret = tmp_i_mark->wd;
633 /* match the ref from fsnotify_init_mark() */
634 fsnotify_put_mark(&tmp_i_mark->fsn_mark);
639 static int inotify_update_watch(struct fsnotify_group *group, struct inode *inode, u32 arg)
643 fsnotify_group_lock(group);
644 /* try to update and existing watch with the new arg */
645 ret = inotify_update_existing_watch(group, inode, arg);
646 /* no mark present, try to add a new one */
648 ret = inotify_new_watch(group, inode, arg);
649 fsnotify_group_unlock(group);
654 static struct fsnotify_group *inotify_new_group(unsigned int max_events)
656 struct fsnotify_group *group;
657 struct inotify_event_info *oevent;
659 group = fsnotify_alloc_group(&inotify_fsnotify_ops,
660 FSNOTIFY_GROUP_USER);
664 oevent = kmalloc(sizeof(struct inotify_event_info), GFP_KERNEL_ACCOUNT);
665 if (unlikely(!oevent)) {
666 fsnotify_destroy_group(group);
667 return ERR_PTR(-ENOMEM);
669 group->overflow_event = &oevent->fse;
670 fsnotify_init_event(group->overflow_event);
671 oevent->mask = FS_Q_OVERFLOW;
673 oevent->sync_cookie = 0;
674 oevent->name_len = 0;
676 group->max_events = max_events;
677 group->memcg = get_mem_cgroup_from_mm(current->mm);
679 spin_lock_init(&group->inotify_data.idr_lock);
680 idr_init(&group->inotify_data.idr);
681 group->inotify_data.ucounts = inc_ucount(current_user_ns(),
683 UCOUNT_INOTIFY_INSTANCES);
685 if (!group->inotify_data.ucounts) {
686 fsnotify_destroy_group(group);
687 return ERR_PTR(-EMFILE);
694 /* inotify syscalls */
695 static int do_inotify_init(int flags)
697 struct fsnotify_group *group;
700 /* Check the IN_* constants for consistency. */
701 BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC);
702 BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK);
704 if (flags & ~(IN_CLOEXEC | IN_NONBLOCK))
707 /* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */
708 group = inotify_new_group(inotify_max_queued_events);
710 return PTR_ERR(group);
712 ret = anon_inode_getfd("inotify", &inotify_fops, group,
715 fsnotify_destroy_group(group);
720 SYSCALL_DEFINE1(inotify_init1, int, flags)
722 return do_inotify_init(flags);
725 SYSCALL_DEFINE0(inotify_init)
727 return do_inotify_init(0);
730 SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,
733 struct fsnotify_group *group;
741 * We share a lot of code with fs/dnotify. We also share
742 * the bit layout between inotify's IN_* and the fsnotify
743 * FS_*. This check ensures that only the inotify IN_*
744 * bits get passed in and set in watches/events.
746 if (unlikely(mask & ~ALL_INOTIFY_BITS))
749 * Require at least one valid bit set in the mask.
750 * Without _something_ set, we would have no events to
753 if (unlikely(!(mask & ALL_INOTIFY_BITS)))
757 if (unlikely(!f.file))
760 /* IN_MASK_ADD and IN_MASK_CREATE don't make sense together */
761 if (unlikely((mask & IN_MASK_ADD) && (mask & IN_MASK_CREATE))) {
766 /* verify that this is indeed an inotify instance */
767 if (unlikely(f.file->f_op != &inotify_fops)) {
772 if (!(mask & IN_DONT_FOLLOW))
773 flags |= LOOKUP_FOLLOW;
774 if (mask & IN_ONLYDIR)
775 flags |= LOOKUP_DIRECTORY;
777 ret = inotify_find_inode(pathname, &path, flags,
778 (mask & IN_ALL_EVENTS));
782 /* inode held in place by reference to path; group by fget on fd */
783 inode = path.dentry->d_inode;
784 group = f.file->private_data;
786 /* create/update an inode mark */
787 ret = inotify_update_watch(group, inode, mask);
794 SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd)
796 struct fsnotify_group *group;
797 struct inotify_inode_mark *i_mark;
802 if (unlikely(!f.file))
805 /* verify that this is indeed an inotify instance */
806 if (unlikely(f.file->f_op != &inotify_fops))
809 group = f.file->private_data;
811 i_mark = inotify_idr_find(group, wd);
812 if (unlikely(!i_mark))
817 fsnotify_destroy_mark(&i_mark->fsn_mark, group);
819 /* match ref taken by inotify_idr_find */
820 fsnotify_put_mark(&i_mark->fsn_mark);
828 * inotify_user_setup - Our initialization function. Note that we cannot return
829 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
830 * must result in panic().
832 static int __init inotify_user_setup(void)
834 unsigned long watches_max;
839 * Allow up to 1% of addressable memory to be allocated for inotify
840 * watches (per user) limited to the range [8192, 1048576].
842 watches_max = (((si.totalram - si.totalhigh) / 100) << PAGE_SHIFT) /
844 watches_max = clamp(watches_max, 8192UL, 1048576UL);
846 BUILD_BUG_ON(IN_ACCESS != FS_ACCESS);
847 BUILD_BUG_ON(IN_MODIFY != FS_MODIFY);
848 BUILD_BUG_ON(IN_ATTRIB != FS_ATTRIB);
849 BUILD_BUG_ON(IN_CLOSE_WRITE != FS_CLOSE_WRITE);
850 BUILD_BUG_ON(IN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
851 BUILD_BUG_ON(IN_OPEN != FS_OPEN);
852 BUILD_BUG_ON(IN_MOVED_FROM != FS_MOVED_FROM);
853 BUILD_BUG_ON(IN_MOVED_TO != FS_MOVED_TO);
854 BUILD_BUG_ON(IN_CREATE != FS_CREATE);
855 BUILD_BUG_ON(IN_DELETE != FS_DELETE);
856 BUILD_BUG_ON(IN_DELETE_SELF != FS_DELETE_SELF);
857 BUILD_BUG_ON(IN_MOVE_SELF != FS_MOVE_SELF);
858 BUILD_BUG_ON(IN_UNMOUNT != FS_UNMOUNT);
859 BUILD_BUG_ON(IN_Q_OVERFLOW != FS_Q_OVERFLOW);
860 BUILD_BUG_ON(IN_IGNORED != FS_IN_IGNORED);
861 BUILD_BUG_ON(IN_ISDIR != FS_ISDIR);
863 BUILD_BUG_ON(HWEIGHT32(ALL_INOTIFY_BITS) != 22);
865 inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark,
866 SLAB_PANIC|SLAB_ACCOUNT);
868 inotify_max_queued_events = 16384;
869 init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES] = 128;
870 init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES] = watches_max;
871 inotify_sysctls_init();
875 fs_initcall(inotify_user_setup);