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 struct ctl_table inotify_table[] = {
60 .procname = "max_user_instances",
61 .data = &init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES],
62 .maxlen = sizeof(int),
64 .proc_handler = proc_dointvec_minmax,
65 .extra1 = SYSCTL_ZERO,
68 .procname = "max_user_watches",
69 .data = &init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES],
70 .maxlen = sizeof(int),
72 .proc_handler = proc_dointvec_minmax,
73 .extra1 = SYSCTL_ZERO,
76 .procname = "max_queued_events",
77 .data = &inotify_max_queued_events,
78 .maxlen = sizeof(int),
80 .proc_handler = proc_dointvec_minmax,
85 #endif /* CONFIG_SYSCTL */
87 static inline __u32 inotify_arg_to_mask(struct inode *inode, u32 arg)
92 * Everything should accept their own ignored and should receive events
93 * when the inode is unmounted. All directories care about children.
95 mask = (FS_IN_IGNORED | FS_UNMOUNT);
96 if (S_ISDIR(inode->i_mode))
97 mask |= FS_EVENT_ON_CHILD;
99 /* mask off the flags used to open the fd */
100 mask |= (arg & (IN_ALL_EVENTS | IN_ONESHOT | IN_EXCL_UNLINK));
105 static inline u32 inotify_mask_to_arg(__u32 mask)
107 return mask & (IN_ALL_EVENTS | IN_ISDIR | IN_UNMOUNT | IN_IGNORED |
111 /* intofiy userspace file descriptor functions */
112 static __poll_t inotify_poll(struct file *file, poll_table *wait)
114 struct fsnotify_group *group = file->private_data;
117 poll_wait(file, &group->notification_waitq, wait);
118 spin_lock(&group->notification_lock);
119 if (!fsnotify_notify_queue_is_empty(group))
120 ret = EPOLLIN | EPOLLRDNORM;
121 spin_unlock(&group->notification_lock);
126 static int round_event_name_len(struct fsnotify_event *fsn_event)
128 struct inotify_event_info *event;
130 event = INOTIFY_E(fsn_event);
131 if (!event->name_len)
133 return roundup(event->name_len + 1, sizeof(struct inotify_event));
137 * Get an inotify_kernel_event if one exists and is small
138 * enough to fit in "count". Return an error pointer if
141 * Called with the group->notification_lock held.
143 static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
146 size_t event_size = sizeof(struct inotify_event);
147 struct fsnotify_event *event;
149 if (fsnotify_notify_queue_is_empty(group))
152 event = fsnotify_peek_first_event(group);
154 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
156 event_size += round_event_name_len(event);
157 if (event_size > count)
158 return ERR_PTR(-EINVAL);
160 /* held the notification_lock the whole time, so this is the
161 * same event we peeked above */
162 fsnotify_remove_first_event(group);
168 * Copy an event to user space, returning how much we copied.
170 * We already checked that the event size is smaller than the
171 * buffer we had in "get_one_event()" above.
173 static ssize_t copy_event_to_user(struct fsnotify_group *group,
174 struct fsnotify_event *fsn_event,
177 struct inotify_event inotify_event;
178 struct inotify_event_info *event;
179 size_t event_size = sizeof(struct inotify_event);
183 pr_debug("%s: group=%p event=%p\n", __func__, group, fsn_event);
185 event = INOTIFY_E(fsn_event);
186 name_len = event->name_len;
188 * round up name length so it is a multiple of event_size
189 * plus an extra byte for the terminating '\0'.
191 pad_name_len = round_event_name_len(fsn_event);
192 inotify_event.len = pad_name_len;
193 inotify_event.mask = inotify_mask_to_arg(event->mask);
194 inotify_event.wd = event->wd;
195 inotify_event.cookie = event->sync_cookie;
197 /* send the main event */
198 if (copy_to_user(buf, &inotify_event, event_size))
204 * fsnotify only stores the pathname, so here we have to send the pathname
205 * and then pad that pathname out to a multiple of sizeof(inotify_event)
209 /* copy the path name */
210 if (copy_to_user(buf, event->name, name_len))
214 /* fill userspace with 0's */
215 if (clear_user(buf, pad_name_len - name_len))
217 event_size += pad_name_len;
223 static ssize_t inotify_read(struct file *file, char __user *buf,
224 size_t count, loff_t *pos)
226 struct fsnotify_group *group;
227 struct fsnotify_event *kevent;
230 DEFINE_WAIT_FUNC(wait, woken_wake_function);
233 group = file->private_data;
235 add_wait_queue(&group->notification_waitq, &wait);
237 spin_lock(&group->notification_lock);
238 kevent = get_one_event(group, count);
239 spin_unlock(&group->notification_lock);
241 pr_debug("%s: group=%p kevent=%p\n", __func__, group, kevent);
244 ret = PTR_ERR(kevent);
247 ret = copy_event_to_user(group, kevent, buf);
248 fsnotify_destroy_event(group, kevent);
257 if (file->f_flags & O_NONBLOCK)
260 if (signal_pending(current))
266 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
268 remove_wait_queue(&group->notification_waitq, &wait);
270 if (start != buf && ret != -EFAULT)
275 static int inotify_release(struct inode *ignored, struct file *file)
277 struct fsnotify_group *group = file->private_data;
279 pr_debug("%s: group=%p\n", __func__, group);
281 /* free this group, matching get was inotify_init->fsnotify_obtain_group */
282 fsnotify_destroy_group(group);
287 static long inotify_ioctl(struct file *file, unsigned int cmd,
290 struct fsnotify_group *group;
291 struct fsnotify_event *fsn_event;
296 group = file->private_data;
297 p = (void __user *) arg;
299 pr_debug("%s: group=%p cmd=%u\n", __func__, group, cmd);
303 spin_lock(&group->notification_lock);
304 list_for_each_entry(fsn_event, &group->notification_list,
306 send_len += sizeof(struct inotify_event);
307 send_len += round_event_name_len(fsn_event);
309 spin_unlock(&group->notification_lock);
310 ret = put_user(send_len, (int __user *) p);
312 #ifdef CONFIG_CHECKPOINT_RESTORE
313 case INOTIFY_IOC_SETNEXTWD:
315 if (arg >= 1 && arg <= INT_MAX) {
316 struct inotify_group_private_data *data;
318 data = &group->inotify_data;
319 spin_lock(&data->idr_lock);
320 idr_set_cursor(&data->idr, (unsigned int)arg);
321 spin_unlock(&data->idr_lock);
325 #endif /* CONFIG_CHECKPOINT_RESTORE */
331 static const struct file_operations inotify_fops = {
332 .show_fdinfo = inotify_show_fdinfo,
333 .poll = inotify_poll,
334 .read = inotify_read,
335 .fasync = fsnotify_fasync,
336 .release = inotify_release,
337 .unlocked_ioctl = inotify_ioctl,
338 .compat_ioctl = inotify_ioctl,
339 .llseek = noop_llseek,
344 * find_inode - resolve a user-given path to a specific inode
346 static int inotify_find_inode(const char __user *dirname, struct path *path,
347 unsigned int flags, __u64 mask)
351 error = user_path_at(AT_FDCWD, dirname, flags, path);
354 /* you can only watch an inode if you have read permissions on it */
355 error = inode_permission(path->dentry->d_inode, MAY_READ);
360 error = security_path_notify(path, mask,
361 FSNOTIFY_OBJ_TYPE_INODE);
368 static int inotify_add_to_idr(struct idr *idr, spinlock_t *idr_lock,
369 struct inotify_inode_mark *i_mark)
373 idr_preload(GFP_KERNEL);
376 ret = idr_alloc_cyclic(idr, i_mark, 1, 0, GFP_NOWAIT);
378 /* we added the mark to the idr, take a reference */
380 fsnotify_get_mark(&i_mark->fsn_mark);
383 spin_unlock(idr_lock);
385 return ret < 0 ? ret : 0;
388 static struct inotify_inode_mark *inotify_idr_find_locked(struct fsnotify_group *group,
391 struct idr *idr = &group->inotify_data.idr;
392 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
393 struct inotify_inode_mark *i_mark;
395 assert_spin_locked(idr_lock);
397 i_mark = idr_find(idr, wd);
399 struct fsnotify_mark *fsn_mark = &i_mark->fsn_mark;
401 fsnotify_get_mark(fsn_mark);
402 /* One ref for being in the idr, one ref we just took */
403 BUG_ON(refcount_read(&fsn_mark->refcnt) < 2);
409 static struct inotify_inode_mark *inotify_idr_find(struct fsnotify_group *group,
412 struct inotify_inode_mark *i_mark;
413 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
416 i_mark = inotify_idr_find_locked(group, wd);
417 spin_unlock(idr_lock);
423 * Remove the mark from the idr (if present) and drop the reference
424 * on the mark because it was in the idr.
426 static void inotify_remove_from_idr(struct fsnotify_group *group,
427 struct inotify_inode_mark *i_mark)
429 struct idr *idr = &group->inotify_data.idr;
430 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
431 struct inotify_inode_mark *found_i_mark = NULL;
438 * does this i_mark think it is in the idr? we shouldn't get called
442 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
443 __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
447 /* Lets look in the idr to see if we find it */
448 found_i_mark = inotify_idr_find_locked(group, wd);
449 if (unlikely(!found_i_mark)) {
450 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
451 __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
456 * We found an mark in the idr at the right wd, but it's
457 * not the mark we were told to remove. eparis seriously
458 * fucked up somewhere.
460 if (unlikely(found_i_mark != i_mark)) {
461 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p "
462 "found_i_mark=%p found_i_mark->wd=%d "
463 "found_i_mark->group=%p\n", __func__, i_mark,
464 i_mark->wd, i_mark->fsn_mark.group, found_i_mark,
465 found_i_mark->wd, found_i_mark->fsn_mark.group);
470 * One ref for being in the idr
471 * one ref grabbed by inotify_idr_find
473 if (unlikely(refcount_read(&i_mark->fsn_mark.refcnt) < 2)) {
474 printk(KERN_ERR "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
475 __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
476 /* we can't really recover with bad ref cnting.. */
481 /* Removed from the idr, drop that ref. */
482 fsnotify_put_mark(&i_mark->fsn_mark);
485 spin_unlock(idr_lock);
486 /* match the ref taken by inotify_idr_find_locked() */
488 fsnotify_put_mark(&found_i_mark->fsn_mark);
492 * Send IN_IGNORED for this wd, remove this wd from the idr.
494 void inotify_ignored_and_remove_idr(struct fsnotify_mark *fsn_mark,
495 struct fsnotify_group *group)
497 struct inotify_inode_mark *i_mark;
499 /* Queue ignore event for the watch */
500 inotify_handle_inode_event(fsn_mark, FS_IN_IGNORED, NULL, NULL, NULL,
503 i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
504 /* remove this mark from the idr */
505 inotify_remove_from_idr(group, i_mark);
507 dec_inotify_watches(group->inotify_data.ucounts);
510 static int inotify_update_existing_watch(struct fsnotify_group *group,
514 struct fsnotify_mark *fsn_mark;
515 struct inotify_inode_mark *i_mark;
516 __u32 old_mask, new_mask;
518 int add = (arg & IN_MASK_ADD);
519 int create = (arg & IN_MASK_CREATE);
522 mask = inotify_arg_to_mask(inode, arg);
524 fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
532 i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
534 spin_lock(&fsn_mark->lock);
535 old_mask = fsn_mark->mask;
537 fsn_mark->mask |= mask;
539 fsn_mark->mask = mask;
540 new_mask = fsn_mark->mask;
541 spin_unlock(&fsn_mark->lock);
543 if (old_mask != new_mask) {
544 /* more bits in old than in new? */
545 int dropped = (old_mask & ~new_mask);
546 /* more bits in this fsn_mark than the inode's mask? */
547 int do_inode = (new_mask & ~inode->i_fsnotify_mask);
549 /* update the inode with this new fsn_mark */
550 if (dropped || do_inode)
551 fsnotify_recalc_mask(inode->i_fsnotify_marks);
559 /* match the get from fsnotify_find_mark() */
560 fsnotify_put_mark(fsn_mark);
565 static int inotify_new_watch(struct fsnotify_group *group,
569 struct inotify_inode_mark *tmp_i_mark;
572 struct idr *idr = &group->inotify_data.idr;
573 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
575 mask = inotify_arg_to_mask(inode, arg);
577 tmp_i_mark = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL);
578 if (unlikely(!tmp_i_mark))
581 fsnotify_init_mark(&tmp_i_mark->fsn_mark, group);
582 tmp_i_mark->fsn_mark.mask = mask;
585 ret = inotify_add_to_idr(idr, idr_lock, tmp_i_mark);
589 /* increment the number of watches the user has */
590 if (!inc_inotify_watches(group->inotify_data.ucounts)) {
591 inotify_remove_from_idr(group, tmp_i_mark);
596 /* we are on the idr, now get on the inode */
597 ret = fsnotify_add_inode_mark_locked(&tmp_i_mark->fsn_mark, inode, 0);
599 /* we failed to get on the inode, get off the idr */
600 inotify_remove_from_idr(group, tmp_i_mark);
605 /* return the watch descriptor for this new mark */
606 ret = tmp_i_mark->wd;
609 /* match the ref from fsnotify_init_mark() */
610 fsnotify_put_mark(&tmp_i_mark->fsn_mark);
615 static int inotify_update_watch(struct fsnotify_group *group, struct inode *inode, u32 arg)
619 mutex_lock(&group->mark_mutex);
620 /* try to update and existing watch with the new arg */
621 ret = inotify_update_existing_watch(group, inode, arg);
622 /* no mark present, try to add a new one */
624 ret = inotify_new_watch(group, inode, arg);
625 mutex_unlock(&group->mark_mutex);
630 static struct fsnotify_group *inotify_new_group(unsigned int max_events)
632 struct fsnotify_group *group;
633 struct inotify_event_info *oevent;
635 group = fsnotify_alloc_group(&inotify_fsnotify_ops);
639 oevent = kmalloc(sizeof(struct inotify_event_info), GFP_KERNEL);
640 if (unlikely(!oevent)) {
641 fsnotify_destroy_group(group);
642 return ERR_PTR(-ENOMEM);
644 group->overflow_event = &oevent->fse;
645 fsnotify_init_event(group->overflow_event, 0);
646 oevent->mask = FS_Q_OVERFLOW;
648 oevent->sync_cookie = 0;
649 oevent->name_len = 0;
651 group->max_events = max_events;
652 group->memcg = get_mem_cgroup_from_mm(current->mm);
654 spin_lock_init(&group->inotify_data.idr_lock);
655 idr_init(&group->inotify_data.idr);
656 group->inotify_data.ucounts = inc_ucount(current_user_ns(),
658 UCOUNT_INOTIFY_INSTANCES);
660 if (!group->inotify_data.ucounts) {
661 fsnotify_destroy_group(group);
662 return ERR_PTR(-EMFILE);
669 /* inotify syscalls */
670 static int do_inotify_init(int flags)
672 struct fsnotify_group *group;
675 /* Check the IN_* constants for consistency. */
676 BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC);
677 BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK);
679 if (flags & ~(IN_CLOEXEC | IN_NONBLOCK))
682 /* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */
683 group = inotify_new_group(inotify_max_queued_events);
685 return PTR_ERR(group);
687 ret = anon_inode_getfd("inotify", &inotify_fops, group,
690 fsnotify_destroy_group(group);
695 SYSCALL_DEFINE1(inotify_init1, int, flags)
697 return do_inotify_init(flags);
700 SYSCALL_DEFINE0(inotify_init)
702 return do_inotify_init(0);
705 SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,
708 struct fsnotify_group *group;
716 * We share a lot of code with fs/dnotify. We also share
717 * the bit layout between inotify's IN_* and the fsnotify
718 * FS_*. This check ensures that only the inotify IN_*
719 * bits get passed in and set in watches/events.
721 if (unlikely(mask & ~ALL_INOTIFY_BITS))
724 * Require at least one valid bit set in the mask.
725 * Without _something_ set, we would have no events to
728 if (unlikely(!(mask & ALL_INOTIFY_BITS)))
732 if (unlikely(!f.file))
735 /* IN_MASK_ADD and IN_MASK_CREATE don't make sense together */
736 if (unlikely((mask & IN_MASK_ADD) && (mask & IN_MASK_CREATE))) {
741 /* verify that this is indeed an inotify instance */
742 if (unlikely(f.file->f_op != &inotify_fops)) {
747 if (!(mask & IN_DONT_FOLLOW))
748 flags |= LOOKUP_FOLLOW;
749 if (mask & IN_ONLYDIR)
750 flags |= LOOKUP_DIRECTORY;
752 ret = inotify_find_inode(pathname, &path, flags,
753 (mask & IN_ALL_EVENTS));
757 /* inode held in place by reference to path; group by fget on fd */
758 inode = path.dentry->d_inode;
759 group = f.file->private_data;
761 /* create/update an inode mark */
762 ret = inotify_update_watch(group, inode, mask);
769 SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd)
771 struct fsnotify_group *group;
772 struct inotify_inode_mark *i_mark;
777 if (unlikely(!f.file))
780 /* verify that this is indeed an inotify instance */
781 if (unlikely(f.file->f_op != &inotify_fops))
784 group = f.file->private_data;
786 i_mark = inotify_idr_find(group, wd);
787 if (unlikely(!i_mark))
792 fsnotify_destroy_mark(&i_mark->fsn_mark, group);
794 /* match ref taken by inotify_idr_find */
795 fsnotify_put_mark(&i_mark->fsn_mark);
803 * inotify_user_setup - Our initialization function. Note that we cannot return
804 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
805 * must result in panic().
807 static int __init inotify_user_setup(void)
809 unsigned long watches_max;
814 * Allow up to 1% of addressable memory to be allocated for inotify
815 * watches (per user) limited to the range [8192, 1048576].
817 watches_max = (((si.totalram - si.totalhigh) / 100) << PAGE_SHIFT) /
819 watches_max = clamp(watches_max, 8192UL, 1048576UL);
821 BUILD_BUG_ON(IN_ACCESS != FS_ACCESS);
822 BUILD_BUG_ON(IN_MODIFY != FS_MODIFY);
823 BUILD_BUG_ON(IN_ATTRIB != FS_ATTRIB);
824 BUILD_BUG_ON(IN_CLOSE_WRITE != FS_CLOSE_WRITE);
825 BUILD_BUG_ON(IN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
826 BUILD_BUG_ON(IN_OPEN != FS_OPEN);
827 BUILD_BUG_ON(IN_MOVED_FROM != FS_MOVED_FROM);
828 BUILD_BUG_ON(IN_MOVED_TO != FS_MOVED_TO);
829 BUILD_BUG_ON(IN_CREATE != FS_CREATE);
830 BUILD_BUG_ON(IN_DELETE != FS_DELETE);
831 BUILD_BUG_ON(IN_DELETE_SELF != FS_DELETE_SELF);
832 BUILD_BUG_ON(IN_MOVE_SELF != FS_MOVE_SELF);
833 BUILD_BUG_ON(IN_UNMOUNT != FS_UNMOUNT);
834 BUILD_BUG_ON(IN_Q_OVERFLOW != FS_Q_OVERFLOW);
835 BUILD_BUG_ON(IN_IGNORED != FS_IN_IGNORED);
836 BUILD_BUG_ON(IN_EXCL_UNLINK != FS_EXCL_UNLINK);
837 BUILD_BUG_ON(IN_ISDIR != FS_ISDIR);
838 BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT);
840 BUILD_BUG_ON(HWEIGHT32(ALL_INOTIFY_BITS) != 22);
842 inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark,
843 SLAB_PANIC|SLAB_ACCOUNT);
845 inotify_max_queued_events = 16384;
846 init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES] = 128;
847 init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES] = watches_max;
851 fs_initcall(inotify_user_setup);