blob: 29fca3284bb5c83c25a669b213dfa3da870631ed [file] [log] [blame]
Thomas Gleixner3e0a4e82019-05-23 11:14:55 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Amy Griffis2d9048e2006-06-01 13:10:59 -07002/*
3 * fs/inotify_user.c - inotify support for userspace
4 *
5 * Authors:
6 * John McCutchan <ttb@tentacle.dhs.org>
7 * Robert Love <rml@novell.com>
8 *
9 * Copyright (C) 2005 John McCutchan
10 * Copyright 2006 Hewlett-Packard Development Company, L.P.
11 *
Eric Paris63c882a2009-05-21 17:02:01 -040012 * Copyright (C) 2009 Eric Paris <Red Hat Inc>
13 * inotify was largely rewriten to make use of the fsnotify infrastructure
Amy Griffis2d9048e2006-06-01 13:10:59 -070014 */
15
Amy Griffis2d9048e2006-06-01 13:10:59 -070016#include <linux/file.h>
Eric Paris63c882a2009-05-21 17:02:01 -040017#include <linux/fs.h> /* struct inode */
18#include <linux/fsnotify_backend.h>
19#include <linux/idr.h>
Paul Gortmakerc013d5a2015-05-01 20:08:20 -040020#include <linux/init.h> /* fs_initcall */
Amy Griffis2d9048e2006-06-01 13:10:59 -070021#include <linux/inotify.h>
Eric Paris63c882a2009-05-21 17:02:01 -040022#include <linux/kernel.h> /* roundup() */
Eric Paris63c882a2009-05-21 17:02:01 -040023#include <linux/namei.h> /* LOOKUP_FOLLOW */
Ingo Molnar174cd4b2017-02-02 19:15:33 +010024#include <linux/sched/signal.h>
Eric Paris63c882a2009-05-21 17:02:01 -040025#include <linux/slab.h> /* struct kmem_cache */
Amy Griffis2d9048e2006-06-01 13:10:59 -070026#include <linux/syscalls.h>
Eric Paris63c882a2009-05-21 17:02:01 -040027#include <linux/types.h>
Al Viroc44dcc52010-02-11 02:24:46 -050028#include <linux/anon_inodes.h>
Eric Paris63c882a2009-05-21 17:02:01 -040029#include <linux/uaccess.h>
30#include <linux/poll.h>
31#include <linux/wait.h>
Shakeel Buttd46eb14b2018-08-17 15:46:39 -070032#include <linux/memcontrol.h>
Aaron Goidelac5656d2019-08-12 11:20:00 -040033#include <linux/security.h>
Eric Paris63c882a2009-05-21 17:02:01 -040034
35#include "inotify.h"
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080036#include "../fdinfo.h"
Amy Griffis2d9048e2006-06-01 13:10:59 -070037
38#include <asm/ioctls.h>
39
Waiman Long92890122020-11-08 22:59:31 -050040/*
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
44 * size increase.
45 */
46#define INOTIFY_WATCH_COST (sizeof(struct inotify_inode_mark) + \
47 2 * sizeof(struct inode))
48
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +020049/* configurable via /proc/sys/fs/inotify/ */
Harvey Harrison3c828e42008-02-14 19:31:21 -080050static int inotify_max_queued_events __read_mostly;
Eric Paris63c882a2009-05-21 17:02:01 -040051
Jan Kara054c6362016-12-21 18:06:12 +010052struct kmem_cache *inotify_inode_mark_cachep __read_mostly;
Amy Griffis2d9048e2006-06-01 13:10:59 -070053
Amy Griffis2d9048e2006-06-01 13:10:59 -070054#ifdef CONFIG_SYSCTL
55
56#include <linux/sysctl.h>
57
Sven Schnellef153c222021-07-30 08:28:54 +020058static long it_zero = 0;
59static long it_int_max = INT_MAX;
60
Joe Perches92f778d2014-06-06 14:38:04 -070061struct ctl_table inotify_table[] = {
Amy Griffis2d9048e2006-06-01 13:10:59 -070062 {
Amy Griffis2d9048e2006-06-01 13:10:59 -070063 .procname = "max_user_instances",
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +020064 .data = &init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES],
Sven Schnellef153c222021-07-30 08:28:54 +020065 .maxlen = sizeof(long),
Amy Griffis2d9048e2006-06-01 13:10:59 -070066 .mode = 0644,
Sven Schnellef153c222021-07-30 08:28:54 +020067 .proc_handler = proc_doulongvec_minmax,
68 .extra1 = &it_zero,
69 .extra2 = &it_int_max,
Amy Griffis2d9048e2006-06-01 13:10:59 -070070 },
71 {
Amy Griffis2d9048e2006-06-01 13:10:59 -070072 .procname = "max_user_watches",
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +020073 .data = &init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES],
Sven Schnellef153c222021-07-30 08:28:54 +020074 .maxlen = sizeof(long),
Amy Griffis2d9048e2006-06-01 13:10:59 -070075 .mode = 0644,
Sven Schnellef153c222021-07-30 08:28:54 +020076 .proc_handler = proc_doulongvec_minmax,
77 .extra1 = &it_zero,
78 .extra2 = &it_int_max,
Amy Griffis2d9048e2006-06-01 13:10:59 -070079 },
80 {
Amy Griffis2d9048e2006-06-01 13:10:59 -070081 .procname = "max_queued_events",
82 .data = &inotify_max_queued_events,
83 .maxlen = sizeof(int),
84 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -080085 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -070086 .extra1 = SYSCTL_ZERO
Amy Griffis2d9048e2006-06-01 13:10:59 -070087 },
Eric W. Biedermanab092032009-11-05 14:25:10 -080088 { }
Amy Griffis2d9048e2006-06-01 13:10:59 -070089};
90#endif /* CONFIG_SYSCTL */
91
Amir Goldstein957f7b42020-07-22 15:58:42 +030092static inline __u32 inotify_arg_to_mask(struct inode *inode, u32 arg)
Amy Griffis2d9048e2006-06-01 13:10:59 -070093{
Eric Paris63c882a2009-05-21 17:02:01 -040094 __u32 mask;
95
Eric Paris611da042010-07-28 10:18:37 -040096 /*
Gabriel Krisman Bertazie0462f92021-10-25 16:27:22 -030097 * Everything should receive events when the inode is unmounted.
98 * All directories care about children.
Eric Paris611da042010-07-28 10:18:37 -040099 */
Gabriel Krisman Bertazie0462f92021-10-25 16:27:22 -0300100 mask = (FS_UNMOUNT);
Amir Goldstein957f7b42020-07-22 15:58:42 +0300101 if (S_ISDIR(inode->i_mode))
102 mask |= FS_EVENT_ON_CHILD;
Eric Paris63c882a2009-05-21 17:02:01 -0400103
104 /* mask off the flags used to open the fd */
Eric Paris8c1934c2010-07-28 10:18:37 -0400105 mask |= (arg & (IN_ALL_EVENTS | IN_ONESHOT | IN_EXCL_UNLINK));
Eric Paris63c882a2009-05-21 17:02:01 -0400106
107 return mask;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700108}
109
Eric Paris63c882a2009-05-21 17:02:01 -0400110static inline u32 inotify_mask_to_arg(__u32 mask)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700111{
Eric Paris63c882a2009-05-21 17:02:01 -0400112 return mask & (IN_ALL_EVENTS | IN_ISDIR | IN_UNMOUNT | IN_IGNORED |
113 IN_Q_OVERFLOW);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700114}
115
Eric Paris63c882a2009-05-21 17:02:01 -0400116/* intofiy userspace file descriptor functions */
Al Viro076ccb72017-07-03 01:02:18 -0400117static __poll_t inotify_poll(struct file *file, poll_table *wait)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700118{
Eric Paris63c882a2009-05-21 17:02:01 -0400119 struct fsnotify_group *group = file->private_data;
Al Viro076ccb72017-07-03 01:02:18 -0400120 __poll_t ret = 0;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700121
Eric Paris63c882a2009-05-21 17:02:01 -0400122 poll_wait(file, &group->notification_waitq, wait);
Jan Karac21dbe22016-10-07 16:56:52 -0700123 spin_lock(&group->notification_lock);
Eric Paris63c882a2009-05-21 17:02:01 -0400124 if (!fsnotify_notify_queue_is_empty(group))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800125 ret = EPOLLIN | EPOLLRDNORM;
Jan Karac21dbe22016-10-07 16:56:52 -0700126 spin_unlock(&group->notification_lock);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700127
128 return ret;
129}
130
Jan Kara7053aee2014-01-21 15:48:14 -0800131static int round_event_name_len(struct fsnotify_event *fsn_event)
Jan Karae9fe6902014-01-21 15:48:13 -0800132{
Jan Kara7053aee2014-01-21 15:48:14 -0800133 struct inotify_event_info *event;
134
135 event = INOTIFY_E(fsn_event);
Jan Karae9fe6902014-01-21 15:48:13 -0800136 if (!event->name_len)
137 return 0;
138 return roundup(event->name_len + 1, sizeof(struct inotify_event));
139}
140
Vegard Nossum3632dee2009-01-22 15:29:45 +0100141/*
142 * Get an inotify_kernel_event if one exists and is small
143 * enough to fit in "count". Return an error pointer if
144 * not large enough.
145 *
Jan Karac21dbe22016-10-07 16:56:52 -0700146 * Called with the group->notification_lock held.
Vegard Nossum3632dee2009-01-22 15:29:45 +0100147 */
Eric Paris63c882a2009-05-21 17:02:01 -0400148static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
149 size_t count)
Vegard Nossum3632dee2009-01-22 15:29:45 +0100150{
151 size_t event_size = sizeof(struct inotify_event);
Eric Paris63c882a2009-05-21 17:02:01 -0400152 struct fsnotify_event *event;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100153
Jan Kara8ba8fa912014-08-06 16:03:26 -0700154 event = fsnotify_peek_first_event(group);
Amir Goldstein6f731712021-03-04 12:48:22 +0200155 if (!event)
156 return NULL;
Eric Paris63c882a2009-05-21 17:02:01 -0400157
Eric Paris5ba08e22010-07-28 10:18:37 -0400158 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
159
Jan Karae9fe6902014-01-21 15:48:13 -0800160 event_size += round_event_name_len(event);
Vegard Nossum3632dee2009-01-22 15:29:45 +0100161 if (event_size > count)
162 return ERR_PTR(-EINVAL);
163
Jan Karac21dbe22016-10-07 16:56:52 -0700164 /* held the notification_lock the whole time, so this is the
Eric Paris63c882a2009-05-21 17:02:01 -0400165 * same event we peeked above */
Jan Kara8ba8fa912014-08-06 16:03:26 -0700166 fsnotify_remove_first_event(group);
Eric Paris63c882a2009-05-21 17:02:01 -0400167
168 return event;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100169}
170
171/*
172 * Copy an event to user space, returning how much we copied.
173 *
174 * We already checked that the event size is smaller than the
175 * buffer we had in "get_one_event()" above.
176 */
Eric Paris63c882a2009-05-21 17:02:01 -0400177static ssize_t copy_event_to_user(struct fsnotify_group *group,
Jan Kara7053aee2014-01-21 15:48:14 -0800178 struct fsnotify_event *fsn_event,
Vegard Nossum3632dee2009-01-22 15:29:45 +0100179 char __user *buf)
180{
Eric Paris63c882a2009-05-21 17:02:01 -0400181 struct inotify_event inotify_event;
Jan Kara7053aee2014-01-21 15:48:14 -0800182 struct inotify_event_info *event;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100183 size_t event_size = sizeof(struct inotify_event);
Jan Karae9fe6902014-01-21 15:48:13 -0800184 size_t name_len;
185 size_t pad_name_len;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100186
Jan Kara7053aee2014-01-21 15:48:14 -0800187 pr_debug("%s: group=%p event=%p\n", __func__, group, fsn_event);
Eric Paris5ba08e22010-07-28 10:18:37 -0400188
Jan Kara7053aee2014-01-21 15:48:14 -0800189 event = INOTIFY_E(fsn_event);
Jan Karae9fe6902014-01-21 15:48:13 -0800190 name_len = event->name_len;
Brian Rogersb962e732009-08-28 10:00:05 -0400191 /*
Jan Karae9fe6902014-01-21 15:48:13 -0800192 * round up name length so it is a multiple of event_size
Eric W. Biederman0db501b2009-08-27 03:20:04 -0700193 * plus an extra byte for the terminating '\0'.
194 */
Jan Kara7053aee2014-01-21 15:48:14 -0800195 pad_name_len = round_event_name_len(fsn_event);
Jan Karae9fe6902014-01-21 15:48:13 -0800196 inotify_event.len = pad_name_len;
Amir Goldsteina0a92d22019-01-10 19:04:31 +0200197 inotify_event.mask = inotify_mask_to_arg(event->mask);
Jan Kara7053aee2014-01-21 15:48:14 -0800198 inotify_event.wd = event->wd;
Eric Paris63c882a2009-05-21 17:02:01 -0400199 inotify_event.cookie = event->sync_cookie;
200
201 /* send the main event */
202 if (copy_to_user(buf, &inotify_event, event_size))
Vegard Nossum3632dee2009-01-22 15:29:45 +0100203 return -EFAULT;
204
Eric Paris63c882a2009-05-21 17:02:01 -0400205 buf += event_size;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100206
Eric Paris63c882a2009-05-21 17:02:01 -0400207 /*
208 * fsnotify only stores the pathname, so here we have to send the pathname
209 * and then pad that pathname out to a multiple of sizeof(inotify_event)
Jan Karae9fe6902014-01-21 15:48:13 -0800210 * with zeros.
Eric Paris63c882a2009-05-21 17:02:01 -0400211 */
Jan Karae9fe6902014-01-21 15:48:13 -0800212 if (pad_name_len) {
Eric Paris63c882a2009-05-21 17:02:01 -0400213 /* copy the path name */
Jan Kara7053aee2014-01-21 15:48:14 -0800214 if (copy_to_user(buf, event->name, name_len))
Vegard Nossum3632dee2009-01-22 15:29:45 +0100215 return -EFAULT;
Jan Karae9fe6902014-01-21 15:48:13 -0800216 buf += name_len;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100217
Eric W. Biederman0db501b2009-08-27 03:20:04 -0700218 /* fill userspace with 0's */
Jan Karae9fe6902014-01-21 15:48:13 -0800219 if (clear_user(buf, pad_name_len - name_len))
Eric Paris63c882a2009-05-21 17:02:01 -0400220 return -EFAULT;
Jan Karae9fe6902014-01-21 15:48:13 -0800221 event_size += pad_name_len;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100222 }
Eric Paris63c882a2009-05-21 17:02:01 -0400223
Vegard Nossum3632dee2009-01-22 15:29:45 +0100224 return event_size;
225}
226
Amy Griffis2d9048e2006-06-01 13:10:59 -0700227static ssize_t inotify_read(struct file *file, char __user *buf,
228 size_t count, loff_t *pos)
229{
Eric Paris63c882a2009-05-21 17:02:01 -0400230 struct fsnotify_group *group;
231 struct fsnotify_event *kevent;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700232 char __user *start;
233 int ret;
Peter Zijlstrae23738a2014-09-24 10:18:50 +0200234 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700235
236 start = buf;
Eric Paris63c882a2009-05-21 17:02:01 -0400237 group = file->private_data;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700238
Peter Zijlstrae23738a2014-09-24 10:18:50 +0200239 add_wait_queue(&group->notification_waitq, &wait);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700240 while (1) {
Jan Karac21dbe22016-10-07 16:56:52 -0700241 spin_lock(&group->notification_lock);
Eric Paris63c882a2009-05-21 17:02:01 -0400242 kevent = get_one_event(group, count);
Jan Karac21dbe22016-10-07 16:56:52 -0700243 spin_unlock(&group->notification_lock);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700244
Eric Paris5ba08e22010-07-28 10:18:37 -0400245 pr_debug("%s: group=%p kevent=%p\n", __func__, group, kevent);
246
Vegard Nossum3632dee2009-01-22 15:29:45 +0100247 if (kevent) {
248 ret = PTR_ERR(kevent);
249 if (IS_ERR(kevent))
250 break;
Eric Paris63c882a2009-05-21 17:02:01 -0400251 ret = copy_event_to_user(group, kevent, buf);
Jan Kara7053aee2014-01-21 15:48:14 -0800252 fsnotify_destroy_event(group, kevent);
Vegard Nossum3632dee2009-01-22 15:29:45 +0100253 if (ret < 0)
254 break;
255 buf += ret;
256 count -= ret;
257 continue;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700258 }
259
Vegard Nossum3632dee2009-01-22 15:29:45 +0100260 ret = -EAGAIN;
261 if (file->f_flags & O_NONBLOCK)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700262 break;
Eric Paris1ca39ab2012-03-26 13:07:59 -0400263 ret = -ERESTARTSYS;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100264 if (signal_pending(current))
265 break;
266
267 if (start != buf)
268 break;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700269
Peter Zijlstrae23738a2014-09-24 10:18:50 +0200270 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700271 }
Peter Zijlstrae23738a2014-09-24 10:18:50 +0200272 remove_wait_queue(&group->notification_waitq, &wait);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700273
Vegard Nossum3632dee2009-01-22 15:29:45 +0100274 if (start != buf && ret != -EFAULT)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700275 ret = buf - start;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700276 return ret;
277}
278
279static int inotify_release(struct inode *ignored, struct file *file)
280{
Eric Paris63c882a2009-05-21 17:02:01 -0400281 struct fsnotify_group *group = file->private_data;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700282
Eric Paris5ba08e22010-07-28 10:18:37 -0400283 pr_debug("%s: group=%p\n", __func__, group);
284
Eric Paris63c882a2009-05-21 17:02:01 -0400285 /* free this group, matching get was inotify_init->fsnotify_obtain_group */
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200286 fsnotify_destroy_group(group);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700287
288 return 0;
289}
290
291static long inotify_ioctl(struct file *file, unsigned int cmd,
292 unsigned long arg)
293{
Eric Paris63c882a2009-05-21 17:02:01 -0400294 struct fsnotify_group *group;
Jan Kara7053aee2014-01-21 15:48:14 -0800295 struct fsnotify_event *fsn_event;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700296 void __user *p;
297 int ret = -ENOTTY;
Eric Paris63c882a2009-05-21 17:02:01 -0400298 size_t send_len = 0;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700299
Eric Paris63c882a2009-05-21 17:02:01 -0400300 group = file->private_data;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700301 p = (void __user *) arg;
302
Eric Paris5ba08e22010-07-28 10:18:37 -0400303 pr_debug("%s: group=%p cmd=%u\n", __func__, group, cmd);
304
Amy Griffis2d9048e2006-06-01 13:10:59 -0700305 switch (cmd) {
306 case FIONREAD:
Jan Karac21dbe22016-10-07 16:56:52 -0700307 spin_lock(&group->notification_lock);
Jan Kara7053aee2014-01-21 15:48:14 -0800308 list_for_each_entry(fsn_event, &group->notification_list,
309 list) {
Eric Paris63c882a2009-05-21 17:02:01 -0400310 send_len += sizeof(struct inotify_event);
Jan Kara7053aee2014-01-21 15:48:14 -0800311 send_len += round_event_name_len(fsn_event);
Eric Paris63c882a2009-05-21 17:02:01 -0400312 }
Jan Karac21dbe22016-10-07 16:56:52 -0700313 spin_unlock(&group->notification_lock);
Eric Paris63c882a2009-05-21 17:02:01 -0400314 ret = put_user(send_len, (int __user *) p);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700315 break;
Kirill Tkhaie1603b62018-02-09 18:04:54 +0300316#ifdef CONFIG_CHECKPOINT_RESTORE
317 case INOTIFY_IOC_SETNEXTWD:
318 ret = -EINVAL;
319 if (arg >= 1 && arg <= INT_MAX) {
320 struct inotify_group_private_data *data;
321
322 data = &group->inotify_data;
323 spin_lock(&data->idr_lock);
324 idr_set_cursor(&data->idr, (unsigned int)arg);
325 spin_unlock(&data->idr_lock);
326 ret = 0;
327 }
328 break;
329#endif /* CONFIG_CHECKPOINT_RESTORE */
Amy Griffis2d9048e2006-06-01 13:10:59 -0700330 }
331
332 return ret;
333}
334
335static const struct file_operations inotify_fops = {
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800336 .show_fdinfo = inotify_show_fdinfo,
Eric Paris63c882a2009-05-21 17:02:01 -0400337 .poll = inotify_poll,
338 .read = inotify_read,
Eric Paris0a6b6bd2011-10-14 17:43:39 -0400339 .fasync = fsnotify_fasync,
Eric Paris63c882a2009-05-21 17:02:01 -0400340 .release = inotify_release,
341 .unlocked_ioctl = inotify_ioctl,
Amy Griffis2d9048e2006-06-01 13:10:59 -0700342 .compat_ioctl = inotify_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200343 .llseek = noop_llseek,
Amy Griffis2d9048e2006-06-01 13:10:59 -0700344};
345
Amy Griffis2d9048e2006-06-01 13:10:59 -0700346
Eric Paris63c882a2009-05-21 17:02:01 -0400347/*
348 * find_inode - resolve a user-given path to a specific inode
349 */
Aaron Goidelac5656d2019-08-12 11:20:00 -0400350static int inotify_find_inode(const char __user *dirname, struct path *path,
351 unsigned int flags, __u64 mask)
Eric Paris63c882a2009-05-21 17:02:01 -0400352{
353 int error;
354
355 error = user_path_at(AT_FDCWD, dirname, flags, path);
356 if (error)
357 return error;
358 /* you can only watch an inode if you have read permissions on it */
Christian Brauner02f92b32021-01-21 14:19:22 +0100359 error = path_permission(path, MAY_READ);
Aaron Goidelac5656d2019-08-12 11:20:00 -0400360 if (error) {
361 path_put(path);
362 return error;
363 }
364 error = security_path_notify(path, mask,
365 FSNOTIFY_OBJ_TYPE_INODE);
Eric Paris63c882a2009-05-21 17:02:01 -0400366 if (error)
367 path_put(path);
Aaron Goidelac5656d2019-08-12 11:20:00 -0400368
Eric Paris63c882a2009-05-21 17:02:01 -0400369 return error;
370}
371
Eric Parisb7ba8372009-12-17 20:12:04 -0500372static int inotify_add_to_idr(struct idr *idr, spinlock_t *idr_lock,
Eric Paris000285d2009-12-17 21:24:24 -0500373 struct inotify_inode_mark *i_mark)
Eric Parisb7ba8372009-12-17 20:12:04 -0500374{
375 int ret;
376
Tejun Heo4542da62013-02-27 17:04:50 -0800377 idr_preload(GFP_KERNEL);
378 spin_lock(idr_lock);
Eric Parisb7ba8372009-12-17 20:12:04 -0500379
Jeff Laytona66c04b2013-04-29 16:21:21 -0700380 ret = idr_alloc_cyclic(idr, i_mark, 1, 0, GFP_NOWAIT);
Tejun Heo4542da62013-02-27 17:04:50 -0800381 if (ret >= 0) {
Eric Parisb7ba8372009-12-17 20:12:04 -0500382 /* we added the mark to the idr, take a reference */
Tejun Heo4542da62013-02-27 17:04:50 -0800383 i_mark->wd = ret;
Tejun Heo4542da62013-02-27 17:04:50 -0800384 fsnotify_get_mark(&i_mark->fsn_mark);
385 }
Eric Parisb7ba8372009-12-17 20:12:04 -0500386
Tejun Heo4542da62013-02-27 17:04:50 -0800387 spin_unlock(idr_lock);
388 idr_preload_end();
389 return ret < 0 ? ret : 0;
Eric Parisb7ba8372009-12-17 20:12:04 -0500390}
391
Eric Paris000285d2009-12-17 21:24:24 -0500392static struct inotify_inode_mark *inotify_idr_find_locked(struct fsnotify_group *group,
Eric Parisb7ba8372009-12-17 20:12:04 -0500393 int wd)
394{
395 struct idr *idr = &group->inotify_data.idr;
396 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
Eric Paris000285d2009-12-17 21:24:24 -0500397 struct inotify_inode_mark *i_mark;
Eric Parisb7ba8372009-12-17 20:12:04 -0500398
399 assert_spin_locked(idr_lock);
400
Eric Paris000285d2009-12-17 21:24:24 -0500401 i_mark = idr_find(idr, wd);
402 if (i_mark) {
403 struct fsnotify_mark *fsn_mark = &i_mark->fsn_mark;
Eric Parisb7ba8372009-12-17 20:12:04 -0500404
Eric Paris000285d2009-12-17 21:24:24 -0500405 fsnotify_get_mark(fsn_mark);
Eric Parisb7ba8372009-12-17 20:12:04 -0500406 /* One ref for being in the idr, one ref we just took */
Elena Reshetovaab97f8732017-10-20 13:26:02 +0300407 BUG_ON(refcount_read(&fsn_mark->refcnt) < 2);
Eric Parisb7ba8372009-12-17 20:12:04 -0500408 }
409
Eric Paris000285d2009-12-17 21:24:24 -0500410 return i_mark;
Eric Parisb7ba8372009-12-17 20:12:04 -0500411}
412
Eric Paris000285d2009-12-17 21:24:24 -0500413static struct inotify_inode_mark *inotify_idr_find(struct fsnotify_group *group,
Eric Parisb7ba8372009-12-17 20:12:04 -0500414 int wd)
415{
Eric Paris000285d2009-12-17 21:24:24 -0500416 struct inotify_inode_mark *i_mark;
Eric Parisb7ba8372009-12-17 20:12:04 -0500417 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
418
419 spin_lock(idr_lock);
Eric Paris000285d2009-12-17 21:24:24 -0500420 i_mark = inotify_idr_find_locked(group, wd);
Eric Parisb7ba8372009-12-17 20:12:04 -0500421 spin_unlock(idr_lock);
422
Eric Paris000285d2009-12-17 21:24:24 -0500423 return i_mark;
Eric Parisb7ba8372009-12-17 20:12:04 -0500424}
425
Eric Parisdead5372009-08-24 16:03:35 -0400426/*
427 * Remove the mark from the idr (if present) and drop the reference
428 * on the mark because it was in the idr.
429 */
Eric Paris7e790dd2009-07-07 10:28:24 -0400430static void inotify_remove_from_idr(struct fsnotify_group *group,
Eric Paris000285d2009-12-17 21:24:24 -0500431 struct inotify_inode_mark *i_mark)
Eric Paris7e790dd2009-07-07 10:28:24 -0400432{
Jan Karae7253762016-12-21 11:50:39 +0100433 struct idr *idr = &group->inotify_data.idr;
Eric Parisb7ba8372009-12-17 20:12:04 -0500434 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
Eric Paris000285d2009-12-17 21:24:24 -0500435 struct inotify_inode_mark *found_i_mark = NULL;
Eric Parisdead5372009-08-24 16:03:35 -0400436 int wd;
Eric Paris7e790dd2009-07-07 10:28:24 -0400437
Eric Parisb7ba8372009-12-17 20:12:04 -0500438 spin_lock(idr_lock);
Eric Paris000285d2009-12-17 21:24:24 -0500439 wd = i_mark->wd;
Eric Parisdead5372009-08-24 16:03:35 -0400440
Eric Parisb7ba8372009-12-17 20:12:04 -0500441 /*
Eric Paris000285d2009-12-17 21:24:24 -0500442 * does this i_mark think it is in the idr? we shouldn't get called
Eric Parisb7ba8372009-12-17 20:12:04 -0500443 * if it wasn't....
444 */
445 if (wd == -1) {
Jan Kara25c829a2016-12-09 09:38:55 +0100446 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
447 __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
Eric Parisdead5372009-08-24 16:03:35 -0400448 goto out;
449 }
450
Eric Parisb7ba8372009-12-17 20:12:04 -0500451 /* Lets look in the idr to see if we find it */
Eric Paris000285d2009-12-17 21:24:24 -0500452 found_i_mark = inotify_idr_find_locked(group, wd);
453 if (unlikely(!found_i_mark)) {
Jan Kara25c829a2016-12-09 09:38:55 +0100454 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
455 __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
Eric Parisb7ba8372009-12-17 20:12:04 -0500456 goto out;
457 }
Eric Parisdead5372009-08-24 16:03:35 -0400458
Eric Parisb7ba8372009-12-17 20:12:04 -0500459 /*
Eric Paris000285d2009-12-17 21:24:24 -0500460 * We found an mark in the idr at the right wd, but it's
461 * not the mark we were told to remove. eparis seriously
Eric Parisb7ba8372009-12-17 20:12:04 -0500462 * fucked up somewhere.
463 */
Eric Paris000285d2009-12-17 21:24:24 -0500464 if (unlikely(found_i_mark != i_mark)) {
465 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p "
Jan Kara25c829a2016-12-09 09:38:55 +0100466 "found_i_mark=%p found_i_mark->wd=%d "
467 "found_i_mark->group=%p\n", __func__, i_mark,
468 i_mark->wd, i_mark->fsn_mark.group, found_i_mark,
469 found_i_mark->wd, found_i_mark->fsn_mark.group);
Eric Parisb7ba8372009-12-17 20:12:04 -0500470 goto out;
471 }
Eric Parisdead5372009-08-24 16:03:35 -0400472
Eric Parisb7ba8372009-12-17 20:12:04 -0500473 /*
474 * One ref for being in the idr
Eric Parisb7ba8372009-12-17 20:12:04 -0500475 * one ref grabbed by inotify_idr_find
476 */
Elena Reshetovaab97f8732017-10-20 13:26:02 +0300477 if (unlikely(refcount_read(&i_mark->fsn_mark.refcnt) < 2)) {
Jan Kara25c829a2016-12-09 09:38:55 +0100478 printk(KERN_ERR "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
479 __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
Eric Parisb7ba8372009-12-17 20:12:04 -0500480 /* we can't really recover with bad ref cnting.. */
481 BUG();
482 }
483
Jan Karae7253762016-12-21 11:50:39 +0100484 idr_remove(idr, wd);
485 /* Removed from the idr, drop that ref. */
486 fsnotify_put_mark(&i_mark->fsn_mark);
Eric Parisdead5372009-08-24 16:03:35 -0400487out:
Jan Karae7253762016-12-21 11:50:39 +0100488 i_mark->wd = -1;
489 spin_unlock(idr_lock);
Eric Parisb7ba8372009-12-17 20:12:04 -0500490 /* match the ref taken by inotify_idr_find_locked() */
Eric Paris000285d2009-12-17 21:24:24 -0500491 if (found_i_mark)
492 fsnotify_put_mark(&found_i_mark->fsn_mark);
Eric Paris7e790dd2009-07-07 10:28:24 -0400493}
Eric Parisdead5372009-08-24 16:03:35 -0400494
Eric Paris63c882a2009-05-21 17:02:01 -0400495/*
Eric Parisdead5372009-08-24 16:03:35 -0400496 * Send IN_IGNORED for this wd, remove this wd from the idr.
Eric Paris63c882a2009-05-21 17:02:01 -0400497 */
Eric Paris000285d2009-12-17 21:24:24 -0500498void inotify_ignored_and_remove_idr(struct fsnotify_mark *fsn_mark,
Eric Paris528da3e2009-06-12 16:04:26 -0400499 struct fsnotify_group *group)
Eric Paris63c882a2009-05-21 17:02:01 -0400500{
Eric Paris000285d2009-12-17 21:24:24 -0500501 struct inotify_inode_mark *i_mark;
Jan Kara7053aee2014-01-21 15:48:14 -0800502
503 /* Queue ignore event for the watch */
Amir Goldstein1a2620a2020-12-02 14:07:08 +0200504 inotify_handle_inode_event(fsn_mark, FS_IN_IGNORED, NULL, NULL, NULL,
505 0);
Eric Paris63c882a2009-05-21 17:02:01 -0400506
Lino Sanfilippo8b99c3c2012-03-24 23:44:19 +0100507 i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
Eric Paris000285d2009-12-17 21:24:24 -0500508 /* remove this mark from the idr */
509 inotify_remove_from_idr(group, i_mark);
Eric Paris63c882a2009-05-21 17:02:01 -0400510
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +0200511 dec_inotify_watches(group->inotify_data.ucounts);
Eric Paris63c882a2009-05-21 17:02:01 -0400512}
513
Eric Paris52cef752009-08-24 16:03:35 -0400514static int inotify_update_existing_watch(struct fsnotify_group *group,
515 struct inode *inode,
516 u32 arg)
Eric Paris63c882a2009-05-21 17:02:01 -0400517{
Eric Paris000285d2009-12-17 21:24:24 -0500518 struct fsnotify_mark *fsn_mark;
519 struct inotify_inode_mark *i_mark;
Eric Paris63c882a2009-05-21 17:02:01 -0400520 __u32 old_mask, new_mask;
Eric Paris52cef752009-08-24 16:03:35 -0400521 __u32 mask;
522 int add = (arg & IN_MASK_ADD);
Henry Wilson4d97f7d2018-05-31 09:43:03 +0000523 int create = (arg & IN_MASK_CREATE);
Eric Paris52cef752009-08-24 16:03:35 -0400524 int ret;
Eric Paris63c882a2009-05-21 17:02:01 -0400525
Amir Goldstein957f7b42020-07-22 15:58:42 +0300526 mask = inotify_arg_to_mask(inode, arg);
Eric Paris63c882a2009-05-21 17:02:01 -0400527
Jan Karab1362ed2016-12-21 16:28:45 +0100528 fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
Eric Paris000285d2009-12-17 21:24:24 -0500529 if (!fsn_mark)
Eric Paris52cef752009-08-24 16:03:35 -0400530 return -ENOENT;
ZhangXiaoxu62c9d262019-03-02 09:17:32 +0800531 else if (create) {
532 ret = -EEXIST;
533 goto out;
534 }
Eric Paris63c882a2009-05-21 17:02:01 -0400535
Eric Paris000285d2009-12-17 21:24:24 -0500536 i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
Eric Paris75fe2b262009-07-07 10:28:23 -0400537
Eric Paris000285d2009-12-17 21:24:24 -0500538 spin_lock(&fsn_mark->lock);
Eric Paris000285d2009-12-17 21:24:24 -0500539 old_mask = fsn_mark->mask;
Eric Paris90b1e7a2009-12-17 21:24:33 -0500540 if (add)
Jan Kara66d2b812016-12-21 16:03:59 +0100541 fsn_mark->mask |= mask;
Eric Paris90b1e7a2009-12-17 21:24:33 -0500542 else
Jan Kara66d2b812016-12-21 16:03:59 +0100543 fsn_mark->mask = mask;
Eric Paris90b1e7a2009-12-17 21:24:33 -0500544 new_mask = fsn_mark->mask;
Eric Paris000285d2009-12-17 21:24:24 -0500545 spin_unlock(&fsn_mark->lock);
Eric Paris63c882a2009-05-21 17:02:01 -0400546
547 if (old_mask != new_mask) {
548 /* more bits in old than in new? */
549 int dropped = (old_mask & ~new_mask);
Eric Paris000285d2009-12-17 21:24:24 -0500550 /* more bits in this fsn_mark than the inode's mask? */
Eric Paris63c882a2009-05-21 17:02:01 -0400551 int do_inode = (new_mask & ~inode->i_fsnotify_mask);
Eric Paris63c882a2009-05-21 17:02:01 -0400552
Eric Paris000285d2009-12-17 21:24:24 -0500553 /* update the inode with this new fsn_mark */
Eric Paris63c882a2009-05-21 17:02:01 -0400554 if (dropped || do_inode)
Jan Kara8920d272016-12-21 16:13:54 +0100555 fsnotify_recalc_mask(inode->i_fsnotify_marks);
Eric Paris63c882a2009-05-21 17:02:01 -0400556
Eric Paris63c882a2009-05-21 17:02:01 -0400557 }
558
Eric Paris52cef752009-08-24 16:03:35 -0400559 /* return the wd */
Eric Paris000285d2009-12-17 21:24:24 -0500560 ret = i_mark->wd;
Eric Paris52cef752009-08-24 16:03:35 -0400561
ZhangXiaoxu62c9d262019-03-02 09:17:32 +0800562out:
Eric Parisd0775442009-12-17 21:24:24 -0500563 /* match the get from fsnotify_find_mark() */
Eric Paris000285d2009-12-17 21:24:24 -0500564 fsnotify_put_mark(fsn_mark);
Eric Paris75fe2b262009-07-07 10:28:23 -0400565
Eric Paris52cef752009-08-24 16:03:35 -0400566 return ret;
567}
568
569static int inotify_new_watch(struct fsnotify_group *group,
570 struct inode *inode,
571 u32 arg)
572{
Eric Paris000285d2009-12-17 21:24:24 -0500573 struct inotify_inode_mark *tmp_i_mark;
Eric Paris52cef752009-08-24 16:03:35 -0400574 __u32 mask;
575 int ret;
Eric Parisb7ba8372009-12-17 20:12:04 -0500576 struct idr *idr = &group->inotify_data.idr;
577 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
Eric Paris52cef752009-08-24 16:03:35 -0400578
Amir Goldstein957f7b42020-07-22 15:58:42 +0300579 mask = inotify_arg_to_mask(inode, arg);
Eric Paris52cef752009-08-24 16:03:35 -0400580
Eric Paris000285d2009-12-17 21:24:24 -0500581 tmp_i_mark = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL);
582 if (unlikely(!tmp_i_mark))
Eric Paris52cef752009-08-24 16:03:35 -0400583 return -ENOMEM;
584
Jan Kara054c6362016-12-21 18:06:12 +0100585 fsnotify_init_mark(&tmp_i_mark->fsn_mark, group);
Eric Paris000285d2009-12-17 21:24:24 -0500586 tmp_i_mark->fsn_mark.mask = mask;
587 tmp_i_mark->wd = -1;
Eric Paris52cef752009-08-24 16:03:35 -0400588
Jeff Laytona66c04b2013-04-29 16:21:21 -0700589 ret = inotify_add_to_idr(idr, idr_lock, tmp_i_mark);
Eric Parisb7ba8372009-12-17 20:12:04 -0500590 if (ret)
Eric Paris52cef752009-08-24 16:03:35 -0400591 goto out_err;
592
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +0200593 /* increment the number of watches the user has */
594 if (!inc_inotify_watches(group->inotify_data.ucounts)) {
595 inotify_remove_from_idr(group, tmp_i_mark);
596 ret = -ENOSPC;
597 goto out_err;
598 }
599
Eric Paris52cef752009-08-24 16:03:35 -0400600 /* we are on the idr, now get on the inode */
Amir Goldsteinb249f5b2018-04-20 16:10:55 -0700601 ret = fsnotify_add_inode_mark_locked(&tmp_i_mark->fsn_mark, inode, 0);
Eric Paris52cef752009-08-24 16:03:35 -0400602 if (ret) {
603 /* we failed to get on the inode, get off the idr */
Eric Paris000285d2009-12-17 21:24:24 -0500604 inotify_remove_from_idr(group, tmp_i_mark);
Eric Paris52cef752009-08-24 16:03:35 -0400605 goto out_err;
606 }
607
Eric Paris52cef752009-08-24 16:03:35 -0400608
Eric Paris000285d2009-12-17 21:24:24 -0500609 /* return the watch descriptor for this new mark */
610 ret = tmp_i_mark->wd;
Eric Paris52cef752009-08-24 16:03:35 -0400611
Eric Paris52cef752009-08-24 16:03:35 -0400612out_err:
Eric Paris000285d2009-12-17 21:24:24 -0500613 /* match the ref from fsnotify_init_mark() */
614 fsnotify_put_mark(&tmp_i_mark->fsn_mark);
Eric Paris52cef752009-08-24 16:03:35 -0400615
616 return ret;
617}
618
619static int inotify_update_watch(struct fsnotify_group *group, struct inode *inode, u32 arg)
620{
621 int ret = 0;
622
Lino Sanfilippoe1e5a9f2013-07-08 15:59:45 -0700623 mutex_lock(&group->mark_mutex);
Eric Paris52cef752009-08-24 16:03:35 -0400624 /* try to update and existing watch with the new arg */
625 ret = inotify_update_existing_watch(group, inode, arg);
626 /* no mark present, try to add a new one */
627 if (ret == -ENOENT)
628 ret = inotify_new_watch(group, inode, arg);
Lino Sanfilippoe1e5a9f2013-07-08 15:59:45 -0700629 mutex_unlock(&group->mark_mutex);
Eric Paris52cef752009-08-24 16:03:35 -0400630
Eric Paris63c882a2009-05-21 17:02:01 -0400631 return ret;
632}
633
Eric Parisd0de4dc2011-04-05 17:20:50 -0400634static struct fsnotify_group *inotify_new_group(unsigned int max_events)
Eric Paris63c882a2009-05-21 17:02:01 -0400635{
636 struct fsnotify_group *group;
Jan Karaff57cd52014-02-21 19:14:11 +0100637 struct inotify_event_info *oevent;
Eric Paris63c882a2009-05-21 17:02:01 -0400638
Shakeel Buttac7b79f2020-12-19 20:46:08 -0800639 group = fsnotify_alloc_user_group(&inotify_fsnotify_ops);
Eric Paris63c882a2009-05-21 17:02:01 -0400640 if (IS_ERR(group))
641 return group;
642
Shakeel Buttac7b79f2020-12-19 20:46:08 -0800643 oevent = kmalloc(sizeof(struct inotify_event_info), GFP_KERNEL_ACCOUNT);
Jan Karaff57cd52014-02-21 19:14:11 +0100644 if (unlikely(!oevent)) {
645 fsnotify_destroy_group(group);
646 return ERR_PTR(-ENOMEM);
647 }
648 group->overflow_event = &oevent->fse;
Amir Goldstein8988f112021-03-04 12:48:23 +0200649 fsnotify_init_event(group->overflow_event);
Amir Goldsteina0a92d22019-01-10 19:04:31 +0200650 oevent->mask = FS_Q_OVERFLOW;
Jan Karaff57cd52014-02-21 19:14:11 +0100651 oevent->wd = -1;
652 oevent->sync_cookie = 0;
653 oevent->name_len = 0;
654
Eric Paris63c882a2009-05-21 17:02:01 -0400655 group->max_events = max_events;
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700656 group->memcg = get_mem_cgroup_from_mm(current->mm);
Eric Paris63c882a2009-05-21 17:02:01 -0400657
658 spin_lock_init(&group->inotify_data.idr_lock);
659 idr_init(&group->inotify_data.idr);
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +0200660 group->inotify_data.ucounts = inc_ucount(current_user_ns(),
661 current_euid(),
662 UCOUNT_INOTIFY_INSTANCES);
Eric Parisd0de4dc2011-04-05 17:20:50 -0400663
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +0200664 if (!group->inotify_data.ucounts) {
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200665 fsnotify_destroy_group(group);
Eric Parisd0de4dc2011-04-05 17:20:50 -0400666 return ERR_PTR(-EMFILE);
667 }
Eric Paris63c882a2009-05-21 17:02:01 -0400668
669 return group;
670}
671
672
673/* inotify syscalls */
Dominik Brodowskid0d89d12018-03-13 21:27:21 +0100674static int do_inotify_init(int flags)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700675{
Eric Paris63c882a2009-05-21 17:02:01 -0400676 struct fsnotify_group *group;
Al Viroc44dcc52010-02-11 02:24:46 -0500677 int ret;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700678
Ulrich Dreppere38b36f2008-07-23 21:29:42 -0700679 /* Check the IN_* constants for consistency. */
680 BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC);
681 BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK);
682
Ulrich Drepper510df2d2008-07-23 21:29:41 -0700683 if (flags & ~(IN_CLOEXEC | IN_NONBLOCK))
Ulrich Drepper40065532008-07-23 21:29:32 -0700684 return -EINVAL;
685
Eric Paris63c882a2009-05-21 17:02:01 -0400686 /* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */
Eric Parisd0de4dc2011-04-05 17:20:50 -0400687 group = inotify_new_group(inotify_max_queued_events);
688 if (IS_ERR(group))
689 return PTR_ERR(group);
Al Viro825f9692009-08-05 18:35:21 +0400690
Al Viroc44dcc52010-02-11 02:24:46 -0500691 ret = anon_inode_getfd("inotify", &inotify_fops, group,
692 O_RDONLY | flags);
Eric Parisd0de4dc2011-04-05 17:20:50 -0400693 if (ret < 0)
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200694 fsnotify_destroy_group(group);
Al Viro825f9692009-08-05 18:35:21 +0400695
Amy Griffis2d9048e2006-06-01 13:10:59 -0700696 return ret;
697}
698
Dominik Brodowskid0d89d12018-03-13 21:27:21 +0100699SYSCALL_DEFINE1(inotify_init1, int, flags)
700{
701 return do_inotify_init(flags);
702}
703
Heiko Carstens938bb9f2009-01-14 14:14:30 +0100704SYSCALL_DEFINE0(inotify_init)
Ulrich Drepper40065532008-07-23 21:29:32 -0700705{
Dominik Brodowskid0d89d12018-03-13 21:27:21 +0100706 return do_inotify_init(0);
Ulrich Drepper40065532008-07-23 21:29:32 -0700707}
708
Heiko Carstens2e4d0922009-01-14 14:14:31 +0100709SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,
710 u32, mask)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700711{
Eric Paris63c882a2009-05-21 17:02:01 -0400712 struct fsnotify_group *group;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700713 struct inode *inode;
Al Viro2d8f3032008-07-22 09:59:21 -0400714 struct path path;
Al Viro2903ff02012-08-28 12:52:22 -0400715 struct fd f;
716 int ret;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700717 unsigned flags = 0;
718
Dave Hansend30e2c02015-11-05 18:43:46 -0800719 /*
720 * We share a lot of code with fs/dnotify. We also share
721 * the bit layout between inotify's IN_* and the fsnotify
722 * FS_*. This check ensures that only the inotify IN_*
723 * bits get passed in and set in watches/events.
724 */
725 if (unlikely(mask & ~ALL_INOTIFY_BITS))
726 return -EINVAL;
727 /*
728 * Require at least one valid bit set in the mask.
729 * Without _something_ set, we would have no events to
730 * watch for.
731 */
Zhao Hongjiang04df32f2013-04-30 15:26:46 -0700732 if (unlikely(!(mask & ALL_INOTIFY_BITS)))
733 return -EINVAL;
734
Al Viro2903ff02012-08-28 12:52:22 -0400735 f = fdget(fd);
736 if (unlikely(!f.file))
Amy Griffis2d9048e2006-06-01 13:10:59 -0700737 return -EBADF;
738
Henry Wilson4d97f7d2018-05-31 09:43:03 +0000739 /* IN_MASK_ADD and IN_MASK_CREATE don't make sense together */
Tetsuo Handa125892e2019-01-01 18:54:26 +0900740 if (unlikely((mask & IN_MASK_ADD) && (mask & IN_MASK_CREATE))) {
741 ret = -EINVAL;
742 goto fput_and_out;
743 }
Henry Wilson4d97f7d2018-05-31 09:43:03 +0000744
Amy Griffis2d9048e2006-06-01 13:10:59 -0700745 /* verify that this is indeed an inotify instance */
Al Viro2903ff02012-08-28 12:52:22 -0400746 if (unlikely(f.file->f_op != &inotify_fops)) {
Amy Griffis2d9048e2006-06-01 13:10:59 -0700747 ret = -EINVAL;
748 goto fput_and_out;
749 }
750
751 if (!(mask & IN_DONT_FOLLOW))
752 flags |= LOOKUP_FOLLOW;
753 if (mask & IN_ONLYDIR)
754 flags |= LOOKUP_DIRECTORY;
755
Aaron Goidelac5656d2019-08-12 11:20:00 -0400756 ret = inotify_find_inode(pathname, &path, flags,
757 (mask & IN_ALL_EVENTS));
Eric Paris63c882a2009-05-21 17:02:01 -0400758 if (ret)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700759 goto fput_and_out;
760
Eric Paris63c882a2009-05-21 17:02:01 -0400761 /* inode held in place by reference to path; group by fget on fd */
Al Viro2d8f3032008-07-22 09:59:21 -0400762 inode = path.dentry->d_inode;
Al Viro2903ff02012-08-28 12:52:22 -0400763 group = f.file->private_data;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700764
Eric Paris63c882a2009-05-21 17:02:01 -0400765 /* create/update an inode mark */
766 ret = inotify_update_watch(group, inode, mask);
Al Viro2d8f3032008-07-22 09:59:21 -0400767 path_put(&path);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700768fput_and_out:
Al Viro2903ff02012-08-28 12:52:22 -0400769 fdput(f);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700770 return ret;
771}
772
Heiko Carstens2e4d0922009-01-14 14:14:31 +0100773SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700774{
Eric Paris63c882a2009-05-21 17:02:01 -0400775 struct fsnotify_group *group;
Eric Paris000285d2009-12-17 21:24:24 -0500776 struct inotify_inode_mark *i_mark;
Al Viro2903ff02012-08-28 12:52:22 -0400777 struct fd f;
youngjun7b26aa22020-04-26 07:33:16 -0700778 int ret = -EINVAL;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700779
Al Viro2903ff02012-08-28 12:52:22 -0400780 f = fdget(fd);
781 if (unlikely(!f.file))
Amy Griffis2d9048e2006-06-01 13:10:59 -0700782 return -EBADF;
783
784 /* verify that this is indeed an inotify instance */
Al Viro2903ff02012-08-28 12:52:22 -0400785 if (unlikely(f.file->f_op != &inotify_fops))
Amy Griffis2d9048e2006-06-01 13:10:59 -0700786 goto out;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700787
Al Viro2903ff02012-08-28 12:52:22 -0400788 group = f.file->private_data;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700789
Eric Paris000285d2009-12-17 21:24:24 -0500790 i_mark = inotify_idr_find(group, wd);
791 if (unlikely(!i_mark))
Eric Paris63c882a2009-05-21 17:02:01 -0400792 goto out;
Eric Paris63c882a2009-05-21 17:02:01 -0400793
Eric Parisb7ba8372009-12-17 20:12:04 -0500794 ret = 0;
795
Lino Sanfilippoe2a29942011-06-14 17:29:51 +0200796 fsnotify_destroy_mark(&i_mark->fsn_mark, group);
Eric Parisb7ba8372009-12-17 20:12:04 -0500797
798 /* match ref taken by inotify_idr_find */
Eric Paris000285d2009-12-17 21:24:24 -0500799 fsnotify_put_mark(&i_mark->fsn_mark);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700800
801out:
Al Viro2903ff02012-08-28 12:52:22 -0400802 fdput(f);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700803 return ret;
804}
805
Amy Griffis2d9048e2006-06-01 13:10:59 -0700806/*
Justin P. Mattockae0e47f2011-03-01 15:06:02 +0100807 * inotify_user_setup - Our initialization function. Note that we cannot return
Amy Griffis2d9048e2006-06-01 13:10:59 -0700808 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
809 * must result in panic().
810 */
811static int __init inotify_user_setup(void)
812{
Waiman Long92890122020-11-08 22:59:31 -0500813 unsigned long watches_max;
814 struct sysinfo si;
815
816 si_meminfo(&si);
817 /*
818 * Allow up to 1% of addressable memory to be allocated for inotify
819 * watches (per user) limited to the range [8192, 1048576].
820 */
821 watches_max = (((si.totalram - si.totalhigh) / 100) << PAGE_SHIFT) /
822 INOTIFY_WATCH_COST;
823 watches_max = clamp(watches_max, 8192UL, 1048576UL);
824
Eric Parisf874e1a2010-07-28 10:18:37 -0400825 BUILD_BUG_ON(IN_ACCESS != FS_ACCESS);
826 BUILD_BUG_ON(IN_MODIFY != FS_MODIFY);
827 BUILD_BUG_ON(IN_ATTRIB != FS_ATTRIB);
828 BUILD_BUG_ON(IN_CLOSE_WRITE != FS_CLOSE_WRITE);
829 BUILD_BUG_ON(IN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
830 BUILD_BUG_ON(IN_OPEN != FS_OPEN);
831 BUILD_BUG_ON(IN_MOVED_FROM != FS_MOVED_FROM);
832 BUILD_BUG_ON(IN_MOVED_TO != FS_MOVED_TO);
833 BUILD_BUG_ON(IN_CREATE != FS_CREATE);
834 BUILD_BUG_ON(IN_DELETE != FS_DELETE);
835 BUILD_BUG_ON(IN_DELETE_SELF != FS_DELETE_SELF);
836 BUILD_BUG_ON(IN_MOVE_SELF != FS_MOVE_SELF);
837 BUILD_BUG_ON(IN_UNMOUNT != FS_UNMOUNT);
838 BUILD_BUG_ON(IN_Q_OVERFLOW != FS_Q_OVERFLOW);
839 BUILD_BUG_ON(IN_IGNORED != FS_IN_IGNORED);
840 BUILD_BUG_ON(IN_EXCL_UNLINK != FS_EXCL_UNLINK);
Eric Parisb29866a2010-10-28 17:21:58 -0400841 BUILD_BUG_ON(IN_ISDIR != FS_ISDIR);
Eric Parisf874e1a2010-07-28 10:18:37 -0400842 BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT);
843
Amir Goldsteina39f7ec2018-10-04 00:25:36 +0300844 BUILD_BUG_ON(HWEIGHT32(ALL_INOTIFY_BITS) != 22);
Eric Parisf874e1a2010-07-28 10:18:37 -0400845
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700846 inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark,
847 SLAB_PANIC|SLAB_ACCOUNT);
Eric Paris63c882a2009-05-21 17:02:01 -0400848
Amy Griffis2d9048e2006-06-01 13:10:59 -0700849 inotify_max_queued_events = 16384;
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +0200850 init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES] = 128;
Waiman Long92890122020-11-08 22:59:31 -0500851 init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES] = watches_max;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700852
Amy Griffis2d9048e2006-06-01 13:10:59 -0700853 return 0;
854}
Paul Gortmakerc013d5a2015-05-01 20:08:20 -0400855fs_initcall(inotify_user_setup);