blob: e1155d32ef6f2d613267ed512faf26d229a92138 [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
Joe Perches92f778d2014-06-06 14:38:04 -070058struct ctl_table inotify_table[] = {
Amy Griffis2d9048e2006-06-01 13:10:59 -070059 {
Amy Griffis2d9048e2006-06-01 13:10:59 -070060 .procname = "max_user_instances",
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +020061 .data = &init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES],
Amy Griffis2d9048e2006-06-01 13:10:59 -070062 .maxlen = sizeof(int),
63 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -080064 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -070065 .extra1 = SYSCTL_ZERO,
Amy Griffis2d9048e2006-06-01 13:10:59 -070066 },
67 {
Amy Griffis2d9048e2006-06-01 13:10:59 -070068 .procname = "max_user_watches",
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +020069 .data = &init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES],
Amy Griffis2d9048e2006-06-01 13:10:59 -070070 .maxlen = sizeof(int),
71 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -080072 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -070073 .extra1 = SYSCTL_ZERO,
Amy Griffis2d9048e2006-06-01 13:10:59 -070074 },
75 {
Amy Griffis2d9048e2006-06-01 13:10:59 -070076 .procname = "max_queued_events",
77 .data = &inotify_max_queued_events,
78 .maxlen = sizeof(int),
79 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -080080 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -070081 .extra1 = SYSCTL_ZERO
Amy Griffis2d9048e2006-06-01 13:10:59 -070082 },
Eric W. Biedermanab092032009-11-05 14:25:10 -080083 { }
Amy Griffis2d9048e2006-06-01 13:10:59 -070084};
85#endif /* CONFIG_SYSCTL */
86
Amir Goldstein957f7b42020-07-22 15:58:42 +030087static inline __u32 inotify_arg_to_mask(struct inode *inode, u32 arg)
Amy Griffis2d9048e2006-06-01 13:10:59 -070088{
Eric Paris63c882a2009-05-21 17:02:01 -040089 __u32 mask;
90
Eric Paris611da042010-07-28 10:18:37 -040091 /*
Amir Goldstein957f7b42020-07-22 15:58:42 +030092 * Everything should accept their own ignored and should receive events
93 * when the inode is unmounted. All directories care about children.
Eric Paris611da042010-07-28 10:18:37 -040094 */
Amir Goldstein957f7b42020-07-22 15:58:42 +030095 mask = (FS_IN_IGNORED | FS_UNMOUNT);
96 if (S_ISDIR(inode->i_mode))
97 mask |= FS_EVENT_ON_CHILD;
Eric Paris63c882a2009-05-21 17:02:01 -040098
99 /* mask off the flags used to open the fd */
Eric Paris8c1934c2010-07-28 10:18:37 -0400100 mask |= (arg & (IN_ALL_EVENTS | IN_ONESHOT | IN_EXCL_UNLINK));
Eric Paris63c882a2009-05-21 17:02:01 -0400101
102 return mask;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700103}
104
Eric Paris63c882a2009-05-21 17:02:01 -0400105static inline u32 inotify_mask_to_arg(__u32 mask)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700106{
Eric Paris63c882a2009-05-21 17:02:01 -0400107 return mask & (IN_ALL_EVENTS | IN_ISDIR | IN_UNMOUNT | IN_IGNORED |
108 IN_Q_OVERFLOW);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700109}
110
Eric Paris63c882a2009-05-21 17:02:01 -0400111/* intofiy userspace file descriptor functions */
Al Viro076ccb72017-07-03 01:02:18 -0400112static __poll_t inotify_poll(struct file *file, poll_table *wait)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700113{
Eric Paris63c882a2009-05-21 17:02:01 -0400114 struct fsnotify_group *group = file->private_data;
Al Viro076ccb72017-07-03 01:02:18 -0400115 __poll_t ret = 0;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700116
Eric Paris63c882a2009-05-21 17:02:01 -0400117 poll_wait(file, &group->notification_waitq, wait);
Jan Karac21dbe22016-10-07 16:56:52 -0700118 spin_lock(&group->notification_lock);
Eric Paris63c882a2009-05-21 17:02:01 -0400119 if (!fsnotify_notify_queue_is_empty(group))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800120 ret = EPOLLIN | EPOLLRDNORM;
Jan Karac21dbe22016-10-07 16:56:52 -0700121 spin_unlock(&group->notification_lock);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700122
123 return ret;
124}
125
Jan Kara7053aee2014-01-21 15:48:14 -0800126static int round_event_name_len(struct fsnotify_event *fsn_event)
Jan Karae9fe6902014-01-21 15:48:13 -0800127{
Jan Kara7053aee2014-01-21 15:48:14 -0800128 struct inotify_event_info *event;
129
130 event = INOTIFY_E(fsn_event);
Jan Karae9fe6902014-01-21 15:48:13 -0800131 if (!event->name_len)
132 return 0;
133 return roundup(event->name_len + 1, sizeof(struct inotify_event));
134}
135
Vegard Nossum3632dee2009-01-22 15:29:45 +0100136/*
137 * Get an inotify_kernel_event if one exists and is small
138 * enough to fit in "count". Return an error pointer if
139 * not large enough.
140 *
Jan Karac21dbe22016-10-07 16:56:52 -0700141 * Called with the group->notification_lock held.
Vegard Nossum3632dee2009-01-22 15:29:45 +0100142 */
Eric Paris63c882a2009-05-21 17:02:01 -0400143static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
144 size_t count)
Vegard Nossum3632dee2009-01-22 15:29:45 +0100145{
146 size_t event_size = sizeof(struct inotify_event);
Eric Paris63c882a2009-05-21 17:02:01 -0400147 struct fsnotify_event *event;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100148
Eric Paris63c882a2009-05-21 17:02:01 -0400149 if (fsnotify_notify_queue_is_empty(group))
Vegard Nossum3632dee2009-01-22 15:29:45 +0100150 return NULL;
151
Jan Kara8ba8fa912014-08-06 16:03:26 -0700152 event = fsnotify_peek_first_event(group);
Eric Paris63c882a2009-05-21 17:02:01 -0400153
Eric Paris5ba08e22010-07-28 10:18:37 -0400154 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
155
Jan Karae9fe6902014-01-21 15:48:13 -0800156 event_size += round_event_name_len(event);
Vegard Nossum3632dee2009-01-22 15:29:45 +0100157 if (event_size > count)
158 return ERR_PTR(-EINVAL);
159
Jan Karac21dbe22016-10-07 16:56:52 -0700160 /* held the notification_lock the whole time, so this is the
Eric Paris63c882a2009-05-21 17:02:01 -0400161 * same event we peeked above */
Jan Kara8ba8fa912014-08-06 16:03:26 -0700162 fsnotify_remove_first_event(group);
Eric Paris63c882a2009-05-21 17:02:01 -0400163
164 return event;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100165}
166
167/*
168 * Copy an event to user space, returning how much we copied.
169 *
170 * We already checked that the event size is smaller than the
171 * buffer we had in "get_one_event()" above.
172 */
Eric Paris63c882a2009-05-21 17:02:01 -0400173static ssize_t copy_event_to_user(struct fsnotify_group *group,
Jan Kara7053aee2014-01-21 15:48:14 -0800174 struct fsnotify_event *fsn_event,
Vegard Nossum3632dee2009-01-22 15:29:45 +0100175 char __user *buf)
176{
Eric Paris63c882a2009-05-21 17:02:01 -0400177 struct inotify_event inotify_event;
Jan Kara7053aee2014-01-21 15:48:14 -0800178 struct inotify_event_info *event;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100179 size_t event_size = sizeof(struct inotify_event);
Jan Karae9fe6902014-01-21 15:48:13 -0800180 size_t name_len;
181 size_t pad_name_len;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100182
Jan Kara7053aee2014-01-21 15:48:14 -0800183 pr_debug("%s: group=%p event=%p\n", __func__, group, fsn_event);
Eric Paris5ba08e22010-07-28 10:18:37 -0400184
Jan Kara7053aee2014-01-21 15:48:14 -0800185 event = INOTIFY_E(fsn_event);
Jan Karae9fe6902014-01-21 15:48:13 -0800186 name_len = event->name_len;
Brian Rogersb962e732009-08-28 10:00:05 -0400187 /*
Jan Karae9fe6902014-01-21 15:48:13 -0800188 * round up name length so it is a multiple of event_size
Eric W. Biederman0db501b2009-08-27 03:20:04 -0700189 * plus an extra byte for the terminating '\0'.
190 */
Jan Kara7053aee2014-01-21 15:48:14 -0800191 pad_name_len = round_event_name_len(fsn_event);
Jan Karae9fe6902014-01-21 15:48:13 -0800192 inotify_event.len = pad_name_len;
Amir Goldsteina0a92d22019-01-10 19:04:31 +0200193 inotify_event.mask = inotify_mask_to_arg(event->mask);
Jan Kara7053aee2014-01-21 15:48:14 -0800194 inotify_event.wd = event->wd;
Eric Paris63c882a2009-05-21 17:02:01 -0400195 inotify_event.cookie = event->sync_cookie;
196
197 /* send the main event */
198 if (copy_to_user(buf, &inotify_event, event_size))
Vegard Nossum3632dee2009-01-22 15:29:45 +0100199 return -EFAULT;
200
Eric Paris63c882a2009-05-21 17:02:01 -0400201 buf += event_size;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100202
Eric Paris63c882a2009-05-21 17:02:01 -0400203 /*
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)
Jan Karae9fe6902014-01-21 15:48:13 -0800206 * with zeros.
Eric Paris63c882a2009-05-21 17:02:01 -0400207 */
Jan Karae9fe6902014-01-21 15:48:13 -0800208 if (pad_name_len) {
Eric Paris63c882a2009-05-21 17:02:01 -0400209 /* copy the path name */
Jan Kara7053aee2014-01-21 15:48:14 -0800210 if (copy_to_user(buf, event->name, name_len))
Vegard Nossum3632dee2009-01-22 15:29:45 +0100211 return -EFAULT;
Jan Karae9fe6902014-01-21 15:48:13 -0800212 buf += name_len;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100213
Eric W. Biederman0db501b2009-08-27 03:20:04 -0700214 /* fill userspace with 0's */
Jan Karae9fe6902014-01-21 15:48:13 -0800215 if (clear_user(buf, pad_name_len - name_len))
Eric Paris63c882a2009-05-21 17:02:01 -0400216 return -EFAULT;
Jan Karae9fe6902014-01-21 15:48:13 -0800217 event_size += pad_name_len;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100218 }
Eric Paris63c882a2009-05-21 17:02:01 -0400219
Vegard Nossum3632dee2009-01-22 15:29:45 +0100220 return event_size;
221}
222
Amy Griffis2d9048e2006-06-01 13:10:59 -0700223static ssize_t inotify_read(struct file *file, char __user *buf,
224 size_t count, loff_t *pos)
225{
Eric Paris63c882a2009-05-21 17:02:01 -0400226 struct fsnotify_group *group;
227 struct fsnotify_event *kevent;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700228 char __user *start;
229 int ret;
Peter Zijlstrae23738a2014-09-24 10:18:50 +0200230 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700231
232 start = buf;
Eric Paris63c882a2009-05-21 17:02:01 -0400233 group = file->private_data;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700234
Peter Zijlstrae23738a2014-09-24 10:18:50 +0200235 add_wait_queue(&group->notification_waitq, &wait);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700236 while (1) {
Jan Karac21dbe22016-10-07 16:56:52 -0700237 spin_lock(&group->notification_lock);
Eric Paris63c882a2009-05-21 17:02:01 -0400238 kevent = get_one_event(group, count);
Jan Karac21dbe22016-10-07 16:56:52 -0700239 spin_unlock(&group->notification_lock);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700240
Eric Paris5ba08e22010-07-28 10:18:37 -0400241 pr_debug("%s: group=%p kevent=%p\n", __func__, group, kevent);
242
Vegard Nossum3632dee2009-01-22 15:29:45 +0100243 if (kevent) {
244 ret = PTR_ERR(kevent);
245 if (IS_ERR(kevent))
246 break;
Eric Paris63c882a2009-05-21 17:02:01 -0400247 ret = copy_event_to_user(group, kevent, buf);
Jan Kara7053aee2014-01-21 15:48:14 -0800248 fsnotify_destroy_event(group, kevent);
Vegard Nossum3632dee2009-01-22 15:29:45 +0100249 if (ret < 0)
250 break;
251 buf += ret;
252 count -= ret;
253 continue;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700254 }
255
Vegard Nossum3632dee2009-01-22 15:29:45 +0100256 ret = -EAGAIN;
257 if (file->f_flags & O_NONBLOCK)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700258 break;
Eric Paris1ca39ab2012-03-26 13:07:59 -0400259 ret = -ERESTARTSYS;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100260 if (signal_pending(current))
261 break;
262
263 if (start != buf)
264 break;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700265
Peter Zijlstrae23738a2014-09-24 10:18:50 +0200266 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700267 }
Peter Zijlstrae23738a2014-09-24 10:18:50 +0200268 remove_wait_queue(&group->notification_waitq, &wait);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700269
Vegard Nossum3632dee2009-01-22 15:29:45 +0100270 if (start != buf && ret != -EFAULT)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700271 ret = buf - start;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700272 return ret;
273}
274
275static int inotify_release(struct inode *ignored, struct file *file)
276{
Eric Paris63c882a2009-05-21 17:02:01 -0400277 struct fsnotify_group *group = file->private_data;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700278
Eric Paris5ba08e22010-07-28 10:18:37 -0400279 pr_debug("%s: group=%p\n", __func__, group);
280
Eric Paris63c882a2009-05-21 17:02:01 -0400281 /* free this group, matching get was inotify_init->fsnotify_obtain_group */
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200282 fsnotify_destroy_group(group);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700283
284 return 0;
285}
286
287static long inotify_ioctl(struct file *file, unsigned int cmd,
288 unsigned long arg)
289{
Eric Paris63c882a2009-05-21 17:02:01 -0400290 struct fsnotify_group *group;
Jan Kara7053aee2014-01-21 15:48:14 -0800291 struct fsnotify_event *fsn_event;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700292 void __user *p;
293 int ret = -ENOTTY;
Eric Paris63c882a2009-05-21 17:02:01 -0400294 size_t send_len = 0;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700295
Eric Paris63c882a2009-05-21 17:02:01 -0400296 group = file->private_data;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700297 p = (void __user *) arg;
298
Eric Paris5ba08e22010-07-28 10:18:37 -0400299 pr_debug("%s: group=%p cmd=%u\n", __func__, group, cmd);
300
Amy Griffis2d9048e2006-06-01 13:10:59 -0700301 switch (cmd) {
302 case FIONREAD:
Jan Karac21dbe22016-10-07 16:56:52 -0700303 spin_lock(&group->notification_lock);
Jan Kara7053aee2014-01-21 15:48:14 -0800304 list_for_each_entry(fsn_event, &group->notification_list,
305 list) {
Eric Paris63c882a2009-05-21 17:02:01 -0400306 send_len += sizeof(struct inotify_event);
Jan Kara7053aee2014-01-21 15:48:14 -0800307 send_len += round_event_name_len(fsn_event);
Eric Paris63c882a2009-05-21 17:02:01 -0400308 }
Jan Karac21dbe22016-10-07 16:56:52 -0700309 spin_unlock(&group->notification_lock);
Eric Paris63c882a2009-05-21 17:02:01 -0400310 ret = put_user(send_len, (int __user *) p);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700311 break;
Kirill Tkhaie1603b62018-02-09 18:04:54 +0300312#ifdef CONFIG_CHECKPOINT_RESTORE
313 case INOTIFY_IOC_SETNEXTWD:
314 ret = -EINVAL;
315 if (arg >= 1 && arg <= INT_MAX) {
316 struct inotify_group_private_data *data;
317
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);
322 ret = 0;
323 }
324 break;
325#endif /* CONFIG_CHECKPOINT_RESTORE */
Amy Griffis2d9048e2006-06-01 13:10:59 -0700326 }
327
328 return ret;
329}
330
331static const struct file_operations inotify_fops = {
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800332 .show_fdinfo = inotify_show_fdinfo,
Eric Paris63c882a2009-05-21 17:02:01 -0400333 .poll = inotify_poll,
334 .read = inotify_read,
Eric Paris0a6b6bd2011-10-14 17:43:39 -0400335 .fasync = fsnotify_fasync,
Eric Paris63c882a2009-05-21 17:02:01 -0400336 .release = inotify_release,
337 .unlocked_ioctl = inotify_ioctl,
Amy Griffis2d9048e2006-06-01 13:10:59 -0700338 .compat_ioctl = inotify_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200339 .llseek = noop_llseek,
Amy Griffis2d9048e2006-06-01 13:10:59 -0700340};
341
Amy Griffis2d9048e2006-06-01 13:10:59 -0700342
Eric Paris63c882a2009-05-21 17:02:01 -0400343/*
344 * find_inode - resolve a user-given path to a specific inode
345 */
Aaron Goidelac5656d2019-08-12 11:20:00 -0400346static int inotify_find_inode(const char __user *dirname, struct path *path,
347 unsigned int flags, __u64 mask)
Eric Paris63c882a2009-05-21 17:02:01 -0400348{
349 int error;
350
351 error = user_path_at(AT_FDCWD, dirname, flags, path);
352 if (error)
353 return error;
354 /* you can only watch an inode if you have read permissions on it */
Christian Brauner02f92b32021-01-21 14:19:22 +0100355 error = path_permission(path, MAY_READ);
Aaron Goidelac5656d2019-08-12 11:20:00 -0400356 if (error) {
357 path_put(path);
358 return error;
359 }
360 error = security_path_notify(path, mask,
361 FSNOTIFY_OBJ_TYPE_INODE);
Eric Paris63c882a2009-05-21 17:02:01 -0400362 if (error)
363 path_put(path);
Aaron Goidelac5656d2019-08-12 11:20:00 -0400364
Eric Paris63c882a2009-05-21 17:02:01 -0400365 return error;
366}
367
Eric Parisb7ba8372009-12-17 20:12:04 -0500368static int inotify_add_to_idr(struct idr *idr, spinlock_t *idr_lock,
Eric Paris000285d2009-12-17 21:24:24 -0500369 struct inotify_inode_mark *i_mark)
Eric Parisb7ba8372009-12-17 20:12:04 -0500370{
371 int ret;
372
Tejun Heo4542da62013-02-27 17:04:50 -0800373 idr_preload(GFP_KERNEL);
374 spin_lock(idr_lock);
Eric Parisb7ba8372009-12-17 20:12:04 -0500375
Jeff Laytona66c04b2013-04-29 16:21:21 -0700376 ret = idr_alloc_cyclic(idr, i_mark, 1, 0, GFP_NOWAIT);
Tejun Heo4542da62013-02-27 17:04:50 -0800377 if (ret >= 0) {
Eric Parisb7ba8372009-12-17 20:12:04 -0500378 /* we added the mark to the idr, take a reference */
Tejun Heo4542da62013-02-27 17:04:50 -0800379 i_mark->wd = ret;
Tejun Heo4542da62013-02-27 17:04:50 -0800380 fsnotify_get_mark(&i_mark->fsn_mark);
381 }
Eric Parisb7ba8372009-12-17 20:12:04 -0500382
Tejun Heo4542da62013-02-27 17:04:50 -0800383 spin_unlock(idr_lock);
384 idr_preload_end();
385 return ret < 0 ? ret : 0;
Eric Parisb7ba8372009-12-17 20:12:04 -0500386}
387
Eric Paris000285d2009-12-17 21:24:24 -0500388static struct inotify_inode_mark *inotify_idr_find_locked(struct fsnotify_group *group,
Eric Parisb7ba8372009-12-17 20:12:04 -0500389 int wd)
390{
391 struct idr *idr = &group->inotify_data.idr;
392 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
Eric Paris000285d2009-12-17 21:24:24 -0500393 struct inotify_inode_mark *i_mark;
Eric Parisb7ba8372009-12-17 20:12:04 -0500394
395 assert_spin_locked(idr_lock);
396
Eric Paris000285d2009-12-17 21:24:24 -0500397 i_mark = idr_find(idr, wd);
398 if (i_mark) {
399 struct fsnotify_mark *fsn_mark = &i_mark->fsn_mark;
Eric Parisb7ba8372009-12-17 20:12:04 -0500400
Eric Paris000285d2009-12-17 21:24:24 -0500401 fsnotify_get_mark(fsn_mark);
Eric Parisb7ba8372009-12-17 20:12:04 -0500402 /* One ref for being in the idr, one ref we just took */
Elena Reshetovaab97f8732017-10-20 13:26:02 +0300403 BUG_ON(refcount_read(&fsn_mark->refcnt) < 2);
Eric Parisb7ba8372009-12-17 20:12:04 -0500404 }
405
Eric Paris000285d2009-12-17 21:24:24 -0500406 return i_mark;
Eric Parisb7ba8372009-12-17 20:12:04 -0500407}
408
Eric Paris000285d2009-12-17 21:24:24 -0500409static struct inotify_inode_mark *inotify_idr_find(struct fsnotify_group *group,
Eric Parisb7ba8372009-12-17 20:12:04 -0500410 int wd)
411{
Eric Paris000285d2009-12-17 21:24:24 -0500412 struct inotify_inode_mark *i_mark;
Eric Parisb7ba8372009-12-17 20:12:04 -0500413 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
414
415 spin_lock(idr_lock);
Eric Paris000285d2009-12-17 21:24:24 -0500416 i_mark = inotify_idr_find_locked(group, wd);
Eric Parisb7ba8372009-12-17 20:12:04 -0500417 spin_unlock(idr_lock);
418
Eric Paris000285d2009-12-17 21:24:24 -0500419 return i_mark;
Eric Parisb7ba8372009-12-17 20:12:04 -0500420}
421
Eric Parisdead5372009-08-24 16:03:35 -0400422/*
423 * Remove the mark from the idr (if present) and drop the reference
424 * on the mark because it was in the idr.
425 */
Eric Paris7e790dd2009-07-07 10:28:24 -0400426static void inotify_remove_from_idr(struct fsnotify_group *group,
Eric Paris000285d2009-12-17 21:24:24 -0500427 struct inotify_inode_mark *i_mark)
Eric Paris7e790dd2009-07-07 10:28:24 -0400428{
Jan Karae7253762016-12-21 11:50:39 +0100429 struct idr *idr = &group->inotify_data.idr;
Eric Parisb7ba8372009-12-17 20:12:04 -0500430 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
Eric Paris000285d2009-12-17 21:24:24 -0500431 struct inotify_inode_mark *found_i_mark = NULL;
Eric Parisdead5372009-08-24 16:03:35 -0400432 int wd;
Eric Paris7e790dd2009-07-07 10:28:24 -0400433
Eric Parisb7ba8372009-12-17 20:12:04 -0500434 spin_lock(idr_lock);
Eric Paris000285d2009-12-17 21:24:24 -0500435 wd = i_mark->wd;
Eric Parisdead5372009-08-24 16:03:35 -0400436
Eric Parisb7ba8372009-12-17 20:12:04 -0500437 /*
Eric Paris000285d2009-12-17 21:24:24 -0500438 * does this i_mark think it is in the idr? we shouldn't get called
Eric Parisb7ba8372009-12-17 20:12:04 -0500439 * if it wasn't....
440 */
441 if (wd == -1) {
Jan Kara25c829a2016-12-09 09:38:55 +0100442 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);
Eric Parisdead5372009-08-24 16:03:35 -0400444 goto out;
445 }
446
Eric Parisb7ba8372009-12-17 20:12:04 -0500447 /* Lets look in the idr to see if we find it */
Eric Paris000285d2009-12-17 21:24:24 -0500448 found_i_mark = inotify_idr_find_locked(group, wd);
449 if (unlikely(!found_i_mark)) {
Jan Kara25c829a2016-12-09 09:38:55 +0100450 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);
Eric Parisb7ba8372009-12-17 20:12:04 -0500452 goto out;
453 }
Eric Parisdead5372009-08-24 16:03:35 -0400454
Eric Parisb7ba8372009-12-17 20:12:04 -0500455 /*
Eric Paris000285d2009-12-17 21:24:24 -0500456 * 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
Eric Parisb7ba8372009-12-17 20:12:04 -0500458 * fucked up somewhere.
459 */
Eric Paris000285d2009-12-17 21:24:24 -0500460 if (unlikely(found_i_mark != i_mark)) {
461 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p "
Jan Kara25c829a2016-12-09 09:38:55 +0100462 "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);
Eric Parisb7ba8372009-12-17 20:12:04 -0500466 goto out;
467 }
Eric Parisdead5372009-08-24 16:03:35 -0400468
Eric Parisb7ba8372009-12-17 20:12:04 -0500469 /*
470 * One ref for being in the idr
Eric Parisb7ba8372009-12-17 20:12:04 -0500471 * one ref grabbed by inotify_idr_find
472 */
Elena Reshetovaab97f8732017-10-20 13:26:02 +0300473 if (unlikely(refcount_read(&i_mark->fsn_mark.refcnt) < 2)) {
Jan Kara25c829a2016-12-09 09:38:55 +0100474 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);
Eric Parisb7ba8372009-12-17 20:12:04 -0500476 /* we can't really recover with bad ref cnting.. */
477 BUG();
478 }
479
Jan Karae7253762016-12-21 11:50:39 +0100480 idr_remove(idr, wd);
481 /* Removed from the idr, drop that ref. */
482 fsnotify_put_mark(&i_mark->fsn_mark);
Eric Parisdead5372009-08-24 16:03:35 -0400483out:
Jan Karae7253762016-12-21 11:50:39 +0100484 i_mark->wd = -1;
485 spin_unlock(idr_lock);
Eric Parisb7ba8372009-12-17 20:12:04 -0500486 /* match the ref taken by inotify_idr_find_locked() */
Eric Paris000285d2009-12-17 21:24:24 -0500487 if (found_i_mark)
488 fsnotify_put_mark(&found_i_mark->fsn_mark);
Eric Paris7e790dd2009-07-07 10:28:24 -0400489}
Eric Parisdead5372009-08-24 16:03:35 -0400490
Eric Paris63c882a2009-05-21 17:02:01 -0400491/*
Eric Parisdead5372009-08-24 16:03:35 -0400492 * Send IN_IGNORED for this wd, remove this wd from the idr.
Eric Paris63c882a2009-05-21 17:02:01 -0400493 */
Eric Paris000285d2009-12-17 21:24:24 -0500494void inotify_ignored_and_remove_idr(struct fsnotify_mark *fsn_mark,
Eric Paris528da3e2009-06-12 16:04:26 -0400495 struct fsnotify_group *group)
Eric Paris63c882a2009-05-21 17:02:01 -0400496{
Eric Paris000285d2009-12-17 21:24:24 -0500497 struct inotify_inode_mark *i_mark;
Jan Kara7053aee2014-01-21 15:48:14 -0800498
499 /* Queue ignore event for the watch */
Amir Goldstein1a2620a2020-12-02 14:07:08 +0200500 inotify_handle_inode_event(fsn_mark, FS_IN_IGNORED, NULL, NULL, NULL,
501 0);
Eric Paris63c882a2009-05-21 17:02:01 -0400502
Lino Sanfilippo8b99c3c2012-03-24 23:44:19 +0100503 i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
Eric Paris000285d2009-12-17 21:24:24 -0500504 /* remove this mark from the idr */
505 inotify_remove_from_idr(group, i_mark);
Eric Paris63c882a2009-05-21 17:02:01 -0400506
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +0200507 dec_inotify_watches(group->inotify_data.ucounts);
Eric Paris63c882a2009-05-21 17:02:01 -0400508}
509
Eric Paris52cef752009-08-24 16:03:35 -0400510static int inotify_update_existing_watch(struct fsnotify_group *group,
511 struct inode *inode,
512 u32 arg)
Eric Paris63c882a2009-05-21 17:02:01 -0400513{
Eric Paris000285d2009-12-17 21:24:24 -0500514 struct fsnotify_mark *fsn_mark;
515 struct inotify_inode_mark *i_mark;
Eric Paris63c882a2009-05-21 17:02:01 -0400516 __u32 old_mask, new_mask;
Eric Paris52cef752009-08-24 16:03:35 -0400517 __u32 mask;
518 int add = (arg & IN_MASK_ADD);
Henry Wilson4d97f7d2018-05-31 09:43:03 +0000519 int create = (arg & IN_MASK_CREATE);
Eric Paris52cef752009-08-24 16:03:35 -0400520 int ret;
Eric Paris63c882a2009-05-21 17:02:01 -0400521
Amir Goldstein957f7b42020-07-22 15:58:42 +0300522 mask = inotify_arg_to_mask(inode, arg);
Eric Paris63c882a2009-05-21 17:02:01 -0400523
Jan Karab1362ed2016-12-21 16:28:45 +0100524 fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
Eric Paris000285d2009-12-17 21:24:24 -0500525 if (!fsn_mark)
Eric Paris52cef752009-08-24 16:03:35 -0400526 return -ENOENT;
ZhangXiaoxu62c9d262019-03-02 09:17:32 +0800527 else if (create) {
528 ret = -EEXIST;
529 goto out;
530 }
Eric Paris63c882a2009-05-21 17:02:01 -0400531
Eric Paris000285d2009-12-17 21:24:24 -0500532 i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
Eric Paris75fe2b262009-07-07 10:28:23 -0400533
Eric Paris000285d2009-12-17 21:24:24 -0500534 spin_lock(&fsn_mark->lock);
Eric Paris000285d2009-12-17 21:24:24 -0500535 old_mask = fsn_mark->mask;
Eric Paris90b1e7a2009-12-17 21:24:33 -0500536 if (add)
Jan Kara66d2b812016-12-21 16:03:59 +0100537 fsn_mark->mask |= mask;
Eric Paris90b1e7a2009-12-17 21:24:33 -0500538 else
Jan Kara66d2b812016-12-21 16:03:59 +0100539 fsn_mark->mask = mask;
Eric Paris90b1e7a2009-12-17 21:24:33 -0500540 new_mask = fsn_mark->mask;
Eric Paris000285d2009-12-17 21:24:24 -0500541 spin_unlock(&fsn_mark->lock);
Eric Paris63c882a2009-05-21 17:02:01 -0400542
543 if (old_mask != new_mask) {
544 /* more bits in old than in new? */
545 int dropped = (old_mask & ~new_mask);
Eric Paris000285d2009-12-17 21:24:24 -0500546 /* more bits in this fsn_mark than the inode's mask? */
Eric Paris63c882a2009-05-21 17:02:01 -0400547 int do_inode = (new_mask & ~inode->i_fsnotify_mask);
Eric Paris63c882a2009-05-21 17:02:01 -0400548
Eric Paris000285d2009-12-17 21:24:24 -0500549 /* update the inode with this new fsn_mark */
Eric Paris63c882a2009-05-21 17:02:01 -0400550 if (dropped || do_inode)
Jan Kara8920d272016-12-21 16:13:54 +0100551 fsnotify_recalc_mask(inode->i_fsnotify_marks);
Eric Paris63c882a2009-05-21 17:02:01 -0400552
Eric Paris63c882a2009-05-21 17:02:01 -0400553 }
554
Eric Paris52cef752009-08-24 16:03:35 -0400555 /* return the wd */
Eric Paris000285d2009-12-17 21:24:24 -0500556 ret = i_mark->wd;
Eric Paris52cef752009-08-24 16:03:35 -0400557
ZhangXiaoxu62c9d262019-03-02 09:17:32 +0800558out:
Eric Parisd0775442009-12-17 21:24:24 -0500559 /* match the get from fsnotify_find_mark() */
Eric Paris000285d2009-12-17 21:24:24 -0500560 fsnotify_put_mark(fsn_mark);
Eric Paris75fe2b262009-07-07 10:28:23 -0400561
Eric Paris52cef752009-08-24 16:03:35 -0400562 return ret;
563}
564
565static int inotify_new_watch(struct fsnotify_group *group,
566 struct inode *inode,
567 u32 arg)
568{
Eric Paris000285d2009-12-17 21:24:24 -0500569 struct inotify_inode_mark *tmp_i_mark;
Eric Paris52cef752009-08-24 16:03:35 -0400570 __u32 mask;
571 int ret;
Eric Parisb7ba8372009-12-17 20:12:04 -0500572 struct idr *idr = &group->inotify_data.idr;
573 spinlock_t *idr_lock = &group->inotify_data.idr_lock;
Eric Paris52cef752009-08-24 16:03:35 -0400574
Amir Goldstein957f7b42020-07-22 15:58:42 +0300575 mask = inotify_arg_to_mask(inode, arg);
Eric Paris52cef752009-08-24 16:03:35 -0400576
Eric Paris000285d2009-12-17 21:24:24 -0500577 tmp_i_mark = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL);
578 if (unlikely(!tmp_i_mark))
Eric Paris52cef752009-08-24 16:03:35 -0400579 return -ENOMEM;
580
Jan Kara054c6362016-12-21 18:06:12 +0100581 fsnotify_init_mark(&tmp_i_mark->fsn_mark, group);
Eric Paris000285d2009-12-17 21:24:24 -0500582 tmp_i_mark->fsn_mark.mask = mask;
583 tmp_i_mark->wd = -1;
Eric Paris52cef752009-08-24 16:03:35 -0400584
Jeff Laytona66c04b2013-04-29 16:21:21 -0700585 ret = inotify_add_to_idr(idr, idr_lock, tmp_i_mark);
Eric Parisb7ba8372009-12-17 20:12:04 -0500586 if (ret)
Eric Paris52cef752009-08-24 16:03:35 -0400587 goto out_err;
588
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +0200589 /* 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);
592 ret = -ENOSPC;
593 goto out_err;
594 }
595
Eric Paris52cef752009-08-24 16:03:35 -0400596 /* we are on the idr, now get on the inode */
Amir Goldsteinb249f5b2018-04-20 16:10:55 -0700597 ret = fsnotify_add_inode_mark_locked(&tmp_i_mark->fsn_mark, inode, 0);
Eric Paris52cef752009-08-24 16:03:35 -0400598 if (ret) {
599 /* we failed to get on the inode, get off the idr */
Eric Paris000285d2009-12-17 21:24:24 -0500600 inotify_remove_from_idr(group, tmp_i_mark);
Eric Paris52cef752009-08-24 16:03:35 -0400601 goto out_err;
602 }
603
Eric Paris52cef752009-08-24 16:03:35 -0400604
Eric Paris000285d2009-12-17 21:24:24 -0500605 /* return the watch descriptor for this new mark */
606 ret = tmp_i_mark->wd;
Eric Paris52cef752009-08-24 16:03:35 -0400607
Eric Paris52cef752009-08-24 16:03:35 -0400608out_err:
Eric Paris000285d2009-12-17 21:24:24 -0500609 /* match the ref from fsnotify_init_mark() */
610 fsnotify_put_mark(&tmp_i_mark->fsn_mark);
Eric Paris52cef752009-08-24 16:03:35 -0400611
612 return ret;
613}
614
615static int inotify_update_watch(struct fsnotify_group *group, struct inode *inode, u32 arg)
616{
617 int ret = 0;
618
Lino Sanfilippoe1e5a9f2013-07-08 15:59:45 -0700619 mutex_lock(&group->mark_mutex);
Eric Paris52cef752009-08-24 16:03:35 -0400620 /* 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 */
623 if (ret == -ENOENT)
624 ret = inotify_new_watch(group, inode, arg);
Lino Sanfilippoe1e5a9f2013-07-08 15:59:45 -0700625 mutex_unlock(&group->mark_mutex);
Eric Paris52cef752009-08-24 16:03:35 -0400626
Eric Paris63c882a2009-05-21 17:02:01 -0400627 return ret;
628}
629
Eric Parisd0de4dc2011-04-05 17:20:50 -0400630static struct fsnotify_group *inotify_new_group(unsigned int max_events)
Eric Paris63c882a2009-05-21 17:02:01 -0400631{
632 struct fsnotify_group *group;
Jan Karaff57cd52014-02-21 19:14:11 +0100633 struct inotify_event_info *oevent;
Eric Paris63c882a2009-05-21 17:02:01 -0400634
Eric Paris0d2e2a12009-12-17 21:24:22 -0500635 group = fsnotify_alloc_group(&inotify_fsnotify_ops);
Eric Paris63c882a2009-05-21 17:02:01 -0400636 if (IS_ERR(group))
637 return group;
638
Jan Karaff57cd52014-02-21 19:14:11 +0100639 oevent = kmalloc(sizeof(struct inotify_event_info), GFP_KERNEL);
640 if (unlikely(!oevent)) {
641 fsnotify_destroy_group(group);
642 return ERR_PTR(-ENOMEM);
643 }
644 group->overflow_event = &oevent->fse;
Amir Goldsteindfc2d252020-03-19 17:10:15 +0200645 fsnotify_init_event(group->overflow_event, 0);
Amir Goldsteina0a92d22019-01-10 19:04:31 +0200646 oevent->mask = FS_Q_OVERFLOW;
Jan Karaff57cd52014-02-21 19:14:11 +0100647 oevent->wd = -1;
648 oevent->sync_cookie = 0;
649 oevent->name_len = 0;
650
Eric Paris63c882a2009-05-21 17:02:01 -0400651 group->max_events = max_events;
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700652 group->memcg = get_mem_cgroup_from_mm(current->mm);
Eric Paris63c882a2009-05-21 17:02:01 -0400653
654 spin_lock_init(&group->inotify_data.idr_lock);
655 idr_init(&group->inotify_data.idr);
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +0200656 group->inotify_data.ucounts = inc_ucount(current_user_ns(),
657 current_euid(),
658 UCOUNT_INOTIFY_INSTANCES);
Eric Parisd0de4dc2011-04-05 17:20:50 -0400659
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +0200660 if (!group->inotify_data.ucounts) {
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200661 fsnotify_destroy_group(group);
Eric Parisd0de4dc2011-04-05 17:20:50 -0400662 return ERR_PTR(-EMFILE);
663 }
Eric Paris63c882a2009-05-21 17:02:01 -0400664
665 return group;
666}
667
668
669/* inotify syscalls */
Dominik Brodowskid0d89d12018-03-13 21:27:21 +0100670static int do_inotify_init(int flags)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700671{
Eric Paris63c882a2009-05-21 17:02:01 -0400672 struct fsnotify_group *group;
Al Viroc44dcc52010-02-11 02:24:46 -0500673 int ret;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700674
Ulrich Dreppere38b36f2008-07-23 21:29:42 -0700675 /* Check the IN_* constants for consistency. */
676 BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC);
677 BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK);
678
Ulrich Drepper510df2d2008-07-23 21:29:41 -0700679 if (flags & ~(IN_CLOEXEC | IN_NONBLOCK))
Ulrich Drepper40065532008-07-23 21:29:32 -0700680 return -EINVAL;
681
Eric Paris63c882a2009-05-21 17:02:01 -0400682 /* 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 -0400683 group = inotify_new_group(inotify_max_queued_events);
684 if (IS_ERR(group))
685 return PTR_ERR(group);
Al Viro825f9692009-08-05 18:35:21 +0400686
Al Viroc44dcc52010-02-11 02:24:46 -0500687 ret = anon_inode_getfd("inotify", &inotify_fops, group,
688 O_RDONLY | flags);
Eric Parisd0de4dc2011-04-05 17:20:50 -0400689 if (ret < 0)
Lino Sanfilippod8153d42011-06-14 17:29:45 +0200690 fsnotify_destroy_group(group);
Al Viro825f9692009-08-05 18:35:21 +0400691
Amy Griffis2d9048e2006-06-01 13:10:59 -0700692 return ret;
693}
694
Dominik Brodowskid0d89d12018-03-13 21:27:21 +0100695SYSCALL_DEFINE1(inotify_init1, int, flags)
696{
697 return do_inotify_init(flags);
698}
699
Heiko Carstens938bb9f2009-01-14 14:14:30 +0100700SYSCALL_DEFINE0(inotify_init)
Ulrich Drepper40065532008-07-23 21:29:32 -0700701{
Dominik Brodowskid0d89d12018-03-13 21:27:21 +0100702 return do_inotify_init(0);
Ulrich Drepper40065532008-07-23 21:29:32 -0700703}
704
Heiko Carstens2e4d0922009-01-14 14:14:31 +0100705SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,
706 u32, mask)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700707{
Eric Paris63c882a2009-05-21 17:02:01 -0400708 struct fsnotify_group *group;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700709 struct inode *inode;
Al Viro2d8f3032008-07-22 09:59:21 -0400710 struct path path;
Al Viro2903ff02012-08-28 12:52:22 -0400711 struct fd f;
712 int ret;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700713 unsigned flags = 0;
714
Dave Hansend30e2c02015-11-05 18:43:46 -0800715 /*
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.
720 */
721 if (unlikely(mask & ~ALL_INOTIFY_BITS))
722 return -EINVAL;
723 /*
724 * Require at least one valid bit set in the mask.
725 * Without _something_ set, we would have no events to
726 * watch for.
727 */
Zhao Hongjiang04df32f2013-04-30 15:26:46 -0700728 if (unlikely(!(mask & ALL_INOTIFY_BITS)))
729 return -EINVAL;
730
Al Viro2903ff02012-08-28 12:52:22 -0400731 f = fdget(fd);
732 if (unlikely(!f.file))
Amy Griffis2d9048e2006-06-01 13:10:59 -0700733 return -EBADF;
734
Henry Wilson4d97f7d2018-05-31 09:43:03 +0000735 /* IN_MASK_ADD and IN_MASK_CREATE don't make sense together */
Tetsuo Handa125892e2019-01-01 18:54:26 +0900736 if (unlikely((mask & IN_MASK_ADD) && (mask & IN_MASK_CREATE))) {
737 ret = -EINVAL;
738 goto fput_and_out;
739 }
Henry Wilson4d97f7d2018-05-31 09:43:03 +0000740
Amy Griffis2d9048e2006-06-01 13:10:59 -0700741 /* verify that this is indeed an inotify instance */
Al Viro2903ff02012-08-28 12:52:22 -0400742 if (unlikely(f.file->f_op != &inotify_fops)) {
Amy Griffis2d9048e2006-06-01 13:10:59 -0700743 ret = -EINVAL;
744 goto fput_and_out;
745 }
746
747 if (!(mask & IN_DONT_FOLLOW))
748 flags |= LOOKUP_FOLLOW;
749 if (mask & IN_ONLYDIR)
750 flags |= LOOKUP_DIRECTORY;
751
Aaron Goidelac5656d2019-08-12 11:20:00 -0400752 ret = inotify_find_inode(pathname, &path, flags,
753 (mask & IN_ALL_EVENTS));
Eric Paris63c882a2009-05-21 17:02:01 -0400754 if (ret)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700755 goto fput_and_out;
756
Eric Paris63c882a2009-05-21 17:02:01 -0400757 /* inode held in place by reference to path; group by fget on fd */
Al Viro2d8f3032008-07-22 09:59:21 -0400758 inode = path.dentry->d_inode;
Al Viro2903ff02012-08-28 12:52:22 -0400759 group = f.file->private_data;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700760
Eric Paris63c882a2009-05-21 17:02:01 -0400761 /* create/update an inode mark */
762 ret = inotify_update_watch(group, inode, mask);
Al Viro2d8f3032008-07-22 09:59:21 -0400763 path_put(&path);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700764fput_and_out:
Al Viro2903ff02012-08-28 12:52:22 -0400765 fdput(f);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700766 return ret;
767}
768
Heiko Carstens2e4d0922009-01-14 14:14:31 +0100769SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700770{
Eric Paris63c882a2009-05-21 17:02:01 -0400771 struct fsnotify_group *group;
Eric Paris000285d2009-12-17 21:24:24 -0500772 struct inotify_inode_mark *i_mark;
Al Viro2903ff02012-08-28 12:52:22 -0400773 struct fd f;
youngjun7b26aa22020-04-26 07:33:16 -0700774 int ret = -EINVAL;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700775
Al Viro2903ff02012-08-28 12:52:22 -0400776 f = fdget(fd);
777 if (unlikely(!f.file))
Amy Griffis2d9048e2006-06-01 13:10:59 -0700778 return -EBADF;
779
780 /* verify that this is indeed an inotify instance */
Al Viro2903ff02012-08-28 12:52:22 -0400781 if (unlikely(f.file->f_op != &inotify_fops))
Amy Griffis2d9048e2006-06-01 13:10:59 -0700782 goto out;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700783
Al Viro2903ff02012-08-28 12:52:22 -0400784 group = f.file->private_data;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700785
Eric Paris000285d2009-12-17 21:24:24 -0500786 i_mark = inotify_idr_find(group, wd);
787 if (unlikely(!i_mark))
Eric Paris63c882a2009-05-21 17:02:01 -0400788 goto out;
Eric Paris63c882a2009-05-21 17:02:01 -0400789
Eric Parisb7ba8372009-12-17 20:12:04 -0500790 ret = 0;
791
Lino Sanfilippoe2a29942011-06-14 17:29:51 +0200792 fsnotify_destroy_mark(&i_mark->fsn_mark, group);
Eric Parisb7ba8372009-12-17 20:12:04 -0500793
794 /* match ref taken by inotify_idr_find */
Eric Paris000285d2009-12-17 21:24:24 -0500795 fsnotify_put_mark(&i_mark->fsn_mark);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700796
797out:
Al Viro2903ff02012-08-28 12:52:22 -0400798 fdput(f);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700799 return ret;
800}
801
Amy Griffis2d9048e2006-06-01 13:10:59 -0700802/*
Justin P. Mattockae0e47f2011-03-01 15:06:02 +0100803 * inotify_user_setup - Our initialization function. Note that we cannot return
Amy Griffis2d9048e2006-06-01 13:10:59 -0700804 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
805 * must result in panic().
806 */
807static int __init inotify_user_setup(void)
808{
Waiman Long92890122020-11-08 22:59:31 -0500809 unsigned long watches_max;
810 struct sysinfo si;
811
812 si_meminfo(&si);
813 /*
814 * Allow up to 1% of addressable memory to be allocated for inotify
815 * watches (per user) limited to the range [8192, 1048576].
816 */
817 watches_max = (((si.totalram - si.totalhigh) / 100) << PAGE_SHIFT) /
818 INOTIFY_WATCH_COST;
819 watches_max = clamp(watches_max, 8192UL, 1048576UL);
820
Eric Parisf874e1a2010-07-28 10:18:37 -0400821 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);
Eric Parisb29866a2010-10-28 17:21:58 -0400837 BUILD_BUG_ON(IN_ISDIR != FS_ISDIR);
Eric Parisf874e1a2010-07-28 10:18:37 -0400838 BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT);
839
Amir Goldsteina39f7ec2018-10-04 00:25:36 +0300840 BUILD_BUG_ON(HWEIGHT32(ALL_INOTIFY_BITS) != 22);
Eric Parisf874e1a2010-07-28 10:18:37 -0400841
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700842 inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark,
843 SLAB_PANIC|SLAB_ACCOUNT);
Eric Paris63c882a2009-05-21 17:02:01 -0400844
Amy Griffis2d9048e2006-06-01 13:10:59 -0700845 inotify_max_queued_events = 16384;
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +0200846 init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES] = 128;
Waiman Long92890122020-11-08 22:59:31 -0500847 init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES] = watches_max;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700848
Amy Griffis2d9048e2006-06-01 13:10:59 -0700849 return 0;
850}
Paul Gortmakerc013d5a2015-05-01 20:08:20 -0400851fs_initcall(inotify_user_setup);