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 __ro_after_init;
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,
90 static void __init inotify_sysctls_init(void)
92 register_sysctl("fs/inotify", inotify_table);
96 #define inotify_sysctls_init() do { } while (0)
97 #endif /* CONFIG_SYSCTL */
99 static inline __u32 inotify_arg_to_mask(struct inode *inode, u32 arg)
104 * Everything should receive events when the inode is unmounted.
105 * All directories care about children.
108 if (S_ISDIR(inode->i_mode))
109 mask |= FS_EVENT_ON_CHILD;
111 /* mask off the flags used to open the fd */
112 mask |= (arg & INOTIFY_USER_MASK);
117 #define INOTIFY_MARK_FLAGS \
118 (FSNOTIFY_MARK_FLAG_EXCL_UNLINK | FSNOTIFY_MARK_FLAG_IN_ONESHOT)
120 static inline unsigned int inotify_arg_to_flags(u32 arg)
122 unsigned int flags = 0;
124 if (arg & IN_EXCL_UNLINK)
125 flags |= FSNOTIFY_MARK_FLAG_EXCL_UNLINK;
126 if (arg & IN_ONESHOT)
127 flags |= FSNOTIFY_MARK_FLAG_IN_ONESHOT;
132 static inline u32 inotify_mask_to_arg(__u32 mask)
134 return mask & (IN_ALL_EVENTS | IN_ISDIR | IN_UNMOUNT | IN_IGNORED |
138 /* inotify userspace file descriptor functions */
139 static __poll_t inotify_poll(struct file *file, poll_table *wait)
141 struct fsnotify_group *group = file->private_data;
144 poll_wait(file, &group->notification_waitq, wait);
145 spin_lock(&group->notification_lock);
146 if (!fsnotify_notify_queue_is_empty(group))
147 ret = EPOLLIN | EPOLLRDNORM;
148 spin_unlock(&group->notification_lock);
153 static int round_event_name_len(struct fsnotify_event *fsn_event)
155 struct inotify_event_info *event;
157 event = INOTIFY_E(fsn_event);
158 if (!event->name_len)
160 return roundup(event->name_len + 1, sizeof(struct inotify_event));
164 * Get an inotify_kernel_event if one exists and is small
165 * enough to fit in "count". Return an error pointer if
168 * Called with the group->notification_lock held.
170 static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
173 size_t event_size = sizeof(struct inotify_event);
174 struct fsnotify_event *event;
176 event = fsnotify_peek_first_event(group);
180 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
182 event_size += round_event_name_len(event);
183 if (event_size > count)
184 return ERR_PTR(-EINVAL);
186 /* held the notification_lock the whole time, so this is the
187 * same event we peeked above */
188 fsnotify_remove_first_event(group);
194 * Copy an event to user space, returning how much we copied.
196 * We already checked that the event size is smaller than the
197 * buffer we had in "get_one_event()" above.
199 static ssize_t copy_event_to_user(struct fsnotify_group *group,
200 struct fsnotify_event *fsn_event,
203 struct inotify_event inotify_event;
204 struct inotify_event_info *event;
205 size_t event_size = sizeof(struct inotify_event);
209 pr_debug("%s: group=%p event=%p\n", __func__, group, fsn_event);
211 event = INOTIFY_E(fsn_event);
212 name_len = event->name_len;
214 * round up name length so it is a multiple of event_size
215 * plus an extra byte for the terminating '\0'.
217 pad_name_len = round_event_name_len(fsn_event);
218 inotify_event.len = pad_name_len;
219 inotify_event.mask = inotify_mask_to_arg(event->mask);
220 inotify_event.wd = event->wd;
221 inotify_event.cookie = event->sync_cookie;
223 /* send the main event */
224 if (copy_to_user(buf, &inotify_event, event_size))
230 * fsnotify only stores the pathname, so here we have to send the pathname
231 * and then pad that pathname out to a multiple of sizeof(inotify_event)
235 /* copy the path name */
236 if (copy_to_user(buf, event->name, name_len))
240 /* fill userspace with 0's */
241 if (clear_user(buf, pad_name_len - name_len))
243 event_size += pad_name_len;
249 static ssize_t inotify_read(struct file *file, char __user *buf,
250 size_t count, loff_t *pos)
252 struct fsnotify_group *group;
253 struct fsnotify_event *kevent;
256 DEFINE_WAIT_FUNC(wait, woken_wake_function);
259 group = file->private_data;
261 add_wait_queue(&group->notification_waitq, &wait);
263 spin_lock(&group->notification_lock);
264 kevent = get_one_event(group, count);
265 spin_unlock(&group->notification_lock);
267 pr_debug("%s: group=%p kevent=%p\n", __func__, group, kevent);
270 ret = PTR_ERR(kevent);
273 ret = copy_event_to_user(group, kevent, buf);
274 fsnotify_destroy_event(group, kevent);
283 if (file->f_flags & O_NONBLOCK)
286 if (signal_pending(current))
292 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
294 remove_wait_queue(&group->notification_waitq, &wait);
296 if (start != buf && ret != -EFAULT)
301 static int inotify_release(struct inode *ignored, struct file *file)
303 struct fsnotify_group *group = file->private_data;
305 pr_debug("%s: group=%p\n", __func__, group);
307 /* free this group, matching get was inotify_init->fsnotify_obtain_group */
308 fsnotify_destroy_group(group);
313 static long inotify_ioctl(struct file *file, unsigned int cmd,
316 struct fsnotify_group *group;
317 struct fsnotify_event *fsn_event;
322 group = file->private_data;
323 p = (void __user *) arg;
325 pr_debug("%s: group=%p cmd=%u\n", __func__, group, cmd);
329 spin_lock(&group->notification_lock);
330 list_for_each_entry(fsn_event, &group->notification_list,
332 send_len += sizeof(struct inotify_event);
333 send_len += round_event_name_len(fsn_event);
335 spin_unlock(&group->notification_lock);
336 ret = put_user(send_len, (int __user *) p);
338 #ifdef CONFIG_CHECKPOINT_RESTORE
339 case INOTIFY_IOC_SETNEXTWD:
341 if (arg >= 1 && arg <= INT_MAX) {
342 struct inotify_group_private_data *data;
344 data = &group->inotify_data;
345 spin_lock(&data->idr_lock);
346 idr_set_cursor(&data->idr, (unsigned int)arg);
347 spin_unlock(&data->idr_lock);
351 #endif /* CONFIG_CHECKPOINT_RESTORE */
357 static const struct file_operations inotify_fops = {
358 .show_fdinfo = inotify_show_fdinfo,
359 .poll = inotify_poll,
360 .read = inotify_read,
361 .fasync = fsnotify_fasync,
362 .release = inotify_release,
363 .unlocked_ioctl = inotify_ioctl,
364 .compat_ioctl = inotify_ioctl,
365 .llseek = noop_llseek,
370 * find_inode - resolve a user-given path to a specific inode
372 static int inotify_find_inode(const char __user *dirname, struct path *path,
373 unsigned int flags, __u64 mask)
377 error = user_path_at(AT_FDCWD, dirname, flags, path);
380 /* you can only watch an inode if you have read permissions on it */
381 error = path_permission(path, MAY_READ);
386 error = security_path_notify(path, mask,
387 FSNOTIFY_OBJ_TYPE_INODE);
394 static int inotify_add_to_idr(struct idr *idr, spinlock_t *idr_lock,
395 struct inotify_inode_mark *i_mark)
399 idr_preload(GFP_KERNEL);
402 ret = idr_alloc_cyclic(idr, i_mark, 1, 0, GFP_NOWAIT);
404 /* we added the mark to the idr, take a reference */
406 fsnotify_get_mark(&i_mark->fsn_mark);
409 spin_unlock(idr_lock);
411 return ret < 0 ? ret : 0;
414 static struct inotify_inode_mark *inotify_idr_find_locked(struct fsnotify_group *group,
417 struct idr *idr = &group->inotify_data.idr;
418 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
419 struct inotify_inode_mark *i_mark;
421 assert_spin_locked(idr_lock);
423 i_mark = idr_find(idr, wd);
425 struct fsnotify_mark *fsn_mark = &i_mark->fsn_mark;
427 fsnotify_get_mark(fsn_mark);
428 /* One ref for being in the idr, one ref we just took */
429 BUG_ON(refcount_read(&fsn_mark->refcnt) < 2);
435 static struct inotify_inode_mark *inotify_idr_find(struct fsnotify_group *group,
438 struct inotify_inode_mark *i_mark;
439 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
442 i_mark = inotify_idr_find_locked(group, wd);
443 spin_unlock(idr_lock);
449 * Remove the mark from the idr (if present) and drop the reference
450 * on the mark because it was in the idr.
452 static void inotify_remove_from_idr(struct fsnotify_group *group,
453 struct inotify_inode_mark *i_mark)
455 struct idr *idr = &group->inotify_data.idr;
456 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
457 struct inotify_inode_mark *found_i_mark = NULL;
464 * does this i_mark think it is in the idr? we shouldn't get called
468 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
469 __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
473 /* Lets look in the idr to see if we find it */
474 found_i_mark = inotify_idr_find_locked(group, wd);
475 if (unlikely(!found_i_mark)) {
476 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
477 __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
482 * We found an mark in the idr at the right wd, but it's
483 * not the mark we were told to remove. eparis seriously
484 * fucked up somewhere.
486 if (unlikely(found_i_mark != i_mark)) {
487 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p "
488 "found_i_mark=%p found_i_mark->wd=%d "
489 "found_i_mark->group=%p\n", __func__, i_mark,
490 i_mark->wd, i_mark->fsn_mark.group, found_i_mark,
491 found_i_mark->wd, found_i_mark->fsn_mark.group);
496 * One ref for being in the idr
497 * one ref grabbed by inotify_idr_find
499 if (unlikely(refcount_read(&i_mark->fsn_mark.refcnt) < 2)) {
500 printk(KERN_ERR "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
501 __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
502 /* we can't really recover with bad ref cnting.. */
507 /* Removed from the idr, drop that ref. */
508 fsnotify_put_mark(&i_mark->fsn_mark);
511 spin_unlock(idr_lock);
512 /* match the ref taken by inotify_idr_find_locked() */
514 fsnotify_put_mark(&found_i_mark->fsn_mark);
518 * Send IN_IGNORED for this wd, remove this wd from the idr.
520 void inotify_ignored_and_remove_idr(struct fsnotify_mark *fsn_mark,
521 struct fsnotify_group *group)
523 struct inotify_inode_mark *i_mark;
525 /* Queue ignore event for the watch */
526 inotify_handle_inode_event(fsn_mark, FS_IN_IGNORED, NULL, NULL, NULL,
529 i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
530 /* remove this mark from the idr */
531 inotify_remove_from_idr(group, i_mark);
533 dec_inotify_watches(group->inotify_data.ucounts);
536 static int inotify_update_existing_watch(struct fsnotify_group *group,
540 struct fsnotify_mark *fsn_mark;
541 struct inotify_inode_mark *i_mark;
542 __u32 old_mask, new_mask;
543 int replace = !(arg & IN_MASK_ADD);
544 int create = (arg & IN_MASK_CREATE);
547 fsn_mark = fsnotify_find_inode_mark(inode, group);
555 i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
557 spin_lock(&fsn_mark->lock);
558 old_mask = fsn_mark->mask;
561 fsn_mark->flags &= ~INOTIFY_MARK_FLAGS;
563 fsn_mark->mask |= inotify_arg_to_mask(inode, arg);
564 fsn_mark->flags |= inotify_arg_to_flags(arg);
565 new_mask = fsn_mark->mask;
566 spin_unlock(&fsn_mark->lock);
568 if (old_mask != new_mask) {
569 /* more bits in old than in new? */
570 int dropped = (old_mask & ~new_mask);
571 /* more bits in this fsn_mark than the inode's mask? */
572 int do_inode = (new_mask & ~inode->i_fsnotify_mask);
574 /* update the inode with this new fsn_mark */
575 if (dropped || do_inode)
576 fsnotify_recalc_mask(inode->i_fsnotify_marks);
584 /* match the get from fsnotify_find_mark() */
585 fsnotify_put_mark(fsn_mark);
590 static int inotify_new_watch(struct fsnotify_group *group,
594 struct inotify_inode_mark *tmp_i_mark;
596 struct idr *idr = &group->inotify_data.idr;
597 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
599 tmp_i_mark = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL);
600 if (unlikely(!tmp_i_mark))
603 fsnotify_init_mark(&tmp_i_mark->fsn_mark, group);
604 tmp_i_mark->fsn_mark.mask = inotify_arg_to_mask(inode, arg);
605 tmp_i_mark->fsn_mark.flags = inotify_arg_to_flags(arg);
608 ret = inotify_add_to_idr(idr, idr_lock, tmp_i_mark);
612 /* increment the number of watches the user has */
613 if (!inc_inotify_watches(group->inotify_data.ucounts)) {
614 inotify_remove_from_idr(group, tmp_i_mark);
619 /* we are on the idr, now get on the inode */
620 ret = fsnotify_add_inode_mark_locked(&tmp_i_mark->fsn_mark, inode, 0);
622 /* we failed to get on the inode, get off the idr */
623 inotify_remove_from_idr(group, tmp_i_mark);
628 /* return the watch descriptor for this new mark */
629 ret = tmp_i_mark->wd;
632 /* match the ref from fsnotify_init_mark() */
633 fsnotify_put_mark(&tmp_i_mark->fsn_mark);
638 static int inotify_update_watch(struct fsnotify_group *group, struct inode *inode, u32 arg)
642 fsnotify_group_lock(group);
643 /* try to update and existing watch with the new arg */
644 ret = inotify_update_existing_watch(group, inode, arg);
645 /* no mark present, try to add a new one */
647 ret = inotify_new_watch(group, inode, arg);
648 fsnotify_group_unlock(group);
653 static struct fsnotify_group *inotify_new_group(unsigned int max_events)
655 struct fsnotify_group *group;
656 struct inotify_event_info *oevent;
658 group = fsnotify_alloc_group(&inotify_fsnotify_ops,
659 FSNOTIFY_GROUP_USER);
663 oevent = kmalloc(sizeof(struct inotify_event_info), GFP_KERNEL_ACCOUNT);
664 if (unlikely(!oevent)) {
665 fsnotify_destroy_group(group);
666 return ERR_PTR(-ENOMEM);
668 group->overflow_event = &oevent->fse;
669 fsnotify_init_event(group->overflow_event);
670 oevent->mask = FS_Q_OVERFLOW;
672 oevent->sync_cookie = 0;
673 oevent->name_len = 0;
675 group->max_events = max_events;
676 group->memcg = get_mem_cgroup_from_mm(current->mm);
678 spin_lock_init(&group->inotify_data.idr_lock);
679 idr_init(&group->inotify_data.idr);
680 group->inotify_data.ucounts = inc_ucount(current_user_ns(),
682 UCOUNT_INOTIFY_INSTANCES);
684 if (!group->inotify_data.ucounts) {
685 fsnotify_destroy_group(group);
686 return ERR_PTR(-EMFILE);
693 /* inotify syscalls */
694 static int do_inotify_init(int flags)
696 struct fsnotify_group *group;
699 /* Check the IN_* constants for consistency. */
700 BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC);
701 BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK);
703 if (flags & ~(IN_CLOEXEC | IN_NONBLOCK))
706 /* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */
707 group = inotify_new_group(inotify_max_queued_events);
709 return PTR_ERR(group);
711 ret = anon_inode_getfd("inotify", &inotify_fops, group,
714 fsnotify_destroy_group(group);
719 SYSCALL_DEFINE1(inotify_init1, int, flags)
721 return do_inotify_init(flags);
724 SYSCALL_DEFINE0(inotify_init)
726 return do_inotify_init(0);
729 SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,
732 struct fsnotify_group *group;
740 * We share a lot of code with fs/dnotify. We also share
741 * the bit layout between inotify's IN_* and the fsnotify
742 * FS_*. This check ensures that only the inotify IN_*
743 * bits get passed in and set in watches/events.
745 if (unlikely(mask & ~ALL_INOTIFY_BITS))
748 * Require at least one valid bit set in the mask.
749 * Without _something_ set, we would have no events to
752 if (unlikely(!(mask & ALL_INOTIFY_BITS)))
756 if (unlikely(!f.file))
759 /* IN_MASK_ADD and IN_MASK_CREATE don't make sense together */
760 if (unlikely((mask & IN_MASK_ADD) && (mask & IN_MASK_CREATE))) {
765 /* verify that this is indeed an inotify instance */
766 if (unlikely(f.file->f_op != &inotify_fops)) {
771 if (!(mask & IN_DONT_FOLLOW))
772 flags |= LOOKUP_FOLLOW;
773 if (mask & IN_ONLYDIR)
774 flags |= LOOKUP_DIRECTORY;
776 ret = inotify_find_inode(pathname, &path, flags,
777 (mask & IN_ALL_EVENTS));
781 /* inode held in place by reference to path; group by fget on fd */
782 inode = path.dentry->d_inode;
783 group = f.file->private_data;
785 /* create/update an inode mark */
786 ret = inotify_update_watch(group, inode, mask);
793 SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd)
795 struct fsnotify_group *group;
796 struct inotify_inode_mark *i_mark;
801 if (unlikely(!f.file))
804 /* verify that this is indeed an inotify instance */
805 if (unlikely(f.file->f_op != &inotify_fops))
808 group = f.file->private_data;
810 i_mark = inotify_idr_find(group, wd);
811 if (unlikely(!i_mark))
816 fsnotify_destroy_mark(&i_mark->fsn_mark, group);
818 /* match ref taken by inotify_idr_find */
819 fsnotify_put_mark(&i_mark->fsn_mark);
827 * inotify_user_setup - Our initialization function. Note that we cannot return
828 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
829 * must result in panic().
831 static int __init inotify_user_setup(void)
833 unsigned long watches_max;
838 * Allow up to 1% of addressable memory to be allocated for inotify
839 * watches (per user) limited to the range [8192, 1048576].
841 watches_max = (((si.totalram - si.totalhigh) / 100) << PAGE_SHIFT) /
843 watches_max = clamp(watches_max, 8192UL, 1048576UL);
845 BUILD_BUG_ON(IN_ACCESS != FS_ACCESS);
846 BUILD_BUG_ON(IN_MODIFY != FS_MODIFY);
847 BUILD_BUG_ON(IN_ATTRIB != FS_ATTRIB);
848 BUILD_BUG_ON(IN_CLOSE_WRITE != FS_CLOSE_WRITE);
849 BUILD_BUG_ON(IN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
850 BUILD_BUG_ON(IN_OPEN != FS_OPEN);
851 BUILD_BUG_ON(IN_MOVED_FROM != FS_MOVED_FROM);
852 BUILD_BUG_ON(IN_MOVED_TO != FS_MOVED_TO);
853 BUILD_BUG_ON(IN_CREATE != FS_CREATE);
854 BUILD_BUG_ON(IN_DELETE != FS_DELETE);
855 BUILD_BUG_ON(IN_DELETE_SELF != FS_DELETE_SELF);
856 BUILD_BUG_ON(IN_MOVE_SELF != FS_MOVE_SELF);
857 BUILD_BUG_ON(IN_UNMOUNT != FS_UNMOUNT);
858 BUILD_BUG_ON(IN_Q_OVERFLOW != FS_Q_OVERFLOW);
859 BUILD_BUG_ON(IN_IGNORED != FS_IN_IGNORED);
860 BUILD_BUG_ON(IN_ISDIR != FS_ISDIR);
862 BUILD_BUG_ON(HWEIGHT32(ALL_INOTIFY_BITS) != 22);
864 inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark,
865 SLAB_PANIC|SLAB_ACCOUNT);
867 inotify_max_queued_events = 16384;
868 init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES] = 128;
869 init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES] = watches_max;
870 inotify_sysctls_init();
874 fs_initcall(inotify_user_setup);