xref: /linux/fs/notify/inotify/inotify_user.c (revision 81e51378b78a11b168ffec08d80ff4b47e5b0227)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
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  *
12  * Copyright (C) 2009 Eric Paris <Red Hat Inc>
13  * inotify was largely rewriten to make use of the fsnotify infrastructure
14  */
15 
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>
34 
35 #include "inotify.h"
36 #include "../fdinfo.h"
37 
38 #include <asm/ioctls.h>
39 
40 /*
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 
49 /* configurable via /proc/sys/fs/inotify/ */
50 static int inotify_max_queued_events __read_mostly;
51 
52 struct kmem_cache *inotify_inode_mark_cachep __ro_after_init;
53 
54 #ifdef CONFIG_SYSCTL
55 
56 #include <linux/sysctl.h>
57 
58 static long it_zero = 0;
59 static long it_int_max = INT_MAX;
60 
61 static const struct ctl_table inotify_table[] = {
62 	{
63 		.procname	= "max_user_instances",
64 		.data		= &init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES],
65 		.maxlen		= sizeof(long),
66 		.mode		= 0644,
67 		.proc_handler	= proc_doulongvec_minmax,
68 		.extra1		= &it_zero,
69 		.extra2		= &it_int_max,
70 	},
71 	{
72 		.procname	= "max_user_watches",
73 		.data		= &init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES],
74 		.maxlen		= sizeof(long),
75 		.mode		= 0644,
76 		.proc_handler	= proc_doulongvec_minmax,
77 		.extra1		= &it_zero,
78 		.extra2		= &it_int_max,
79 	},
80 	{
81 		.procname	= "max_queued_events",
82 		.data		= &inotify_max_queued_events,
83 		.maxlen		= sizeof(int),
84 		.mode		= 0644,
85 		.proc_handler	= proc_dointvec_minmax,
86 		.extra1		= SYSCTL_ZERO
87 	},
88 };
89 
inotify_sysctls_init(void)90 static void __init inotify_sysctls_init(void)
91 {
92 	register_sysctl("fs/inotify", inotify_table);
93 }
94 
95 #else
96 #define inotify_sysctls_init() do { } while (0)
97 #endif /* CONFIG_SYSCTL */
98 
inotify_arg_to_mask(struct inode * inode,u32 arg)99 static inline __u32 inotify_arg_to_mask(struct inode *inode, u32 arg)
100 {
101 	__u32 mask;
102 
103 	/*
104 	 * Everything should receive events when the inode is unmounted.
105 	 * All directories care about children.
106 	 */
107 	mask = (FS_UNMOUNT);
108 	if (S_ISDIR(inode->i_mode))
109 		mask |= FS_EVENT_ON_CHILD;
110 
111 	/* mask off the flags used to open the fd */
112 	mask |= (arg & INOTIFY_USER_MASK);
113 
114 	return mask;
115 }
116 
117 #define INOTIFY_MARK_FLAGS \
118 	(FSNOTIFY_MARK_FLAG_EXCL_UNLINK | FSNOTIFY_MARK_FLAG_IN_ONESHOT)
119 
inotify_arg_to_flags(u32 arg)120 static inline unsigned int inotify_arg_to_flags(u32 arg)
121 {
122 	unsigned int flags = 0;
123 
124 	if (arg & IN_EXCL_UNLINK)
125 		flags |= FSNOTIFY_MARK_FLAG_EXCL_UNLINK;
126 	if (arg & IN_ONESHOT)
127 		flags |= FSNOTIFY_MARK_FLAG_IN_ONESHOT;
128 
129 	return flags;
130 }
131 
inotify_mask_to_arg(__u32 mask)132 static inline u32 inotify_mask_to_arg(__u32 mask)
133 {
134 	return mask & (IN_ALL_EVENTS | IN_ISDIR | IN_UNMOUNT | IN_IGNORED |
135 		       IN_Q_OVERFLOW);
136 }
137 
138 /* inotify userspace file descriptor functions */
inotify_poll(struct file * file,poll_table * wait)139 static __poll_t inotify_poll(struct file *file, poll_table *wait)
140 {
141 	struct fsnotify_group *group = file->private_data;
142 	__poll_t ret = 0;
143 
144 	poll_wait(file, &group->notification_waitq, wait);
145 	spin_lock(&group->notification_lock);
146 	if (!fsnotify_notify_queue_is_empty(group))
147 		ret = EPOLLIN | EPOLLRDNORM;
148 	spin_unlock(&group->notification_lock);
149 
150 	return ret;
151 }
152 
round_event_name_len(struct fsnotify_event * fsn_event)153 static int round_event_name_len(struct fsnotify_event *fsn_event)
154 {
155 	struct inotify_event_info *event;
156 
157 	event = INOTIFY_E(fsn_event);
158 	if (!event->name_len)
159 		return 0;
160 	return roundup(event->name_len + 1, sizeof(struct inotify_event));
161 }
162 
163 /*
164  * Get an inotify_kernel_event if one exists and is small
165  * enough to fit in "count". Return an error pointer if
166  * not large enough.
167  *
168  * Called with the group->notification_lock held.
169  */
get_one_event(struct fsnotify_group * group,size_t count)170 static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
171 					    size_t count)
172 {
173 	size_t event_size = sizeof(struct inotify_event);
174 	struct fsnotify_event *event;
175 
176 	event = fsnotify_peek_first_event(group);
177 	if (!event)
178 		return NULL;
179 
180 	pr_debug("%s: group=%p event=%p\n", __func__, group, event);
181 
182 	event_size += round_event_name_len(event);
183 	if (event_size > count)
184 		return ERR_PTR(-EINVAL);
185 
186 	/* held the notification_lock the whole time, so this is the
187 	 * same event we peeked above */
188 	fsnotify_remove_first_event(group);
189 
190 	return event;
191 }
192 
193 /*
194  * Copy an event to user space, returning how much we copied.
195  *
196  * We already checked that the event size is smaller than the
197  * buffer we had in "get_one_event()" above.
198  */
copy_event_to_user(struct fsnotify_group * group,struct fsnotify_event * fsn_event,char __user * buf)199 static ssize_t copy_event_to_user(struct fsnotify_group *group,
200 				  struct fsnotify_event *fsn_event,
201 				  char __user *buf)
202 {
203 	struct inotify_event inotify_event;
204 	struct inotify_event_info *event;
205 	size_t event_size = sizeof(struct inotify_event);
206 	size_t name_len;
207 	size_t pad_name_len;
208 
209 	pr_debug("%s: group=%p event=%p\n", __func__, group, fsn_event);
210 
211 	event = INOTIFY_E(fsn_event);
212 	name_len = event->name_len;
213 	/*
214 	 * round up name length so it is a multiple of event_size
215 	 * plus an extra byte for the terminating '\0'.
216 	 */
217 	pad_name_len = round_event_name_len(fsn_event);
218 	inotify_event.len = pad_name_len;
219 	inotify_event.mask = inotify_mask_to_arg(event->mask);
220 	inotify_event.wd = event->wd;
221 	inotify_event.cookie = event->sync_cookie;
222 
223 	/* send the main event */
224 	if (copy_to_user(buf, &inotify_event, event_size))
225 		return -EFAULT;
226 
227 	buf += event_size;
228 
229 	/*
230 	 * fsnotify only stores the pathname, so here we have to send the pathname
231 	 * and then pad that pathname out to a multiple of sizeof(inotify_event)
232 	 * with zeros.
233 	 */
234 	if (pad_name_len) {
235 		/* copy the path name */
236 		if (copy_to_user(buf, event->name, name_len))
237 			return -EFAULT;
238 		buf += name_len;
239 
240 		/* fill userspace with 0's */
241 		if (clear_user(buf, pad_name_len - name_len))
242 			return -EFAULT;
243 		event_size += pad_name_len;
244 	}
245 
246 	return event_size;
247 }
248 
inotify_read(struct file * file,char __user * buf,size_t count,loff_t * pos)249 static ssize_t inotify_read(struct file *file, char __user *buf,
250 			    size_t count, loff_t *pos)
251 {
252 	struct fsnotify_group *group;
253 	struct fsnotify_event *kevent;
254 	char __user *start;
255 	int ret;
256 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
257 
258 	start = buf;
259 	group = file->private_data;
260 
261 	add_wait_queue(&group->notification_waitq, &wait);
262 	while (1) {
263 		spin_lock(&group->notification_lock);
264 		kevent = get_one_event(group, count);
265 		spin_unlock(&group->notification_lock);
266 
267 		pr_debug("%s: group=%p kevent=%p\n", __func__, group, kevent);
268 
269 		if (kevent) {
270 			ret = PTR_ERR(kevent);
271 			if (IS_ERR(kevent))
272 				break;
273 			ret = copy_event_to_user(group, kevent, buf);
274 			fsnotify_destroy_event(group, kevent);
275 			if (ret < 0)
276 				break;
277 			buf += ret;
278 			count -= ret;
279 			continue;
280 		}
281 
282 		ret = -EAGAIN;
283 		if (file->f_flags & O_NONBLOCK)
284 			break;
285 		ret = -ERESTARTSYS;
286 		if (signal_pending(current))
287 			break;
288 
289 		if (start != buf)
290 			break;
291 
292 		wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
293 	}
294 	remove_wait_queue(&group->notification_waitq, &wait);
295 
296 	if (start != buf && ret != -EFAULT)
297 		ret = buf - start;
298 	return ret;
299 }
300 
inotify_release(struct inode * ignored,struct file * file)301 static int inotify_release(struct inode *ignored, struct file *file)
302 {
303 	struct fsnotify_group *group = file->private_data;
304 
305 	pr_debug("%s: group=%p\n", __func__, group);
306 
307 	/* free this group, matching get was inotify_init->fsnotify_obtain_group */
308 	fsnotify_destroy_group(group);
309 
310 	return 0;
311 }
312 
inotify_ioctl(struct file * file,unsigned int cmd,unsigned long arg)313 static long inotify_ioctl(struct file *file, unsigned int cmd,
314 			  unsigned long arg)
315 {
316 	struct fsnotify_group *group;
317 	struct fsnotify_event *fsn_event;
318 	void __user *p;
319 	int ret = -ENOTTY;
320 	size_t send_len = 0;
321 
322 	group = file->private_data;
323 	p = (void __user *) arg;
324 
325 	pr_debug("%s: group=%p cmd=%u\n", __func__, group, cmd);
326 
327 	switch (cmd) {
328 	case FIONREAD:
329 		spin_lock(&group->notification_lock);
330 		list_for_each_entry(fsn_event, &group->notification_list,
331 				    list) {
332 			send_len += sizeof(struct inotify_event);
333 			send_len += round_event_name_len(fsn_event);
334 		}
335 		spin_unlock(&group->notification_lock);
336 		ret = put_user(send_len, (int __user *) p);
337 		break;
338 #ifdef CONFIG_CHECKPOINT_RESTORE
339 	case INOTIFY_IOC_SETNEXTWD:
340 		ret = -EINVAL;
341 		if (arg >= 1 && arg <= INT_MAX) {
342 			struct inotify_group_private_data *data;
343 
344 			data = &group->inotify_data;
345 			spin_lock(&data->idr_lock);
346 			idr_set_cursor(&data->idr, (unsigned int)arg);
347 			spin_unlock(&data->idr_lock);
348 			ret = 0;
349 		}
350 		break;
351 #endif /* CONFIG_CHECKPOINT_RESTORE */
352 	}
353 
354 	return ret;
355 }
356 
357 static const struct file_operations inotify_fops = {
358 	.show_fdinfo	= inotify_show_fdinfo,
359 	.poll		= inotify_poll,
360 	.read		= inotify_read,
361 	.fasync		= fsnotify_fasync,
362 	.release	= inotify_release,
363 	.unlocked_ioctl	= inotify_ioctl,
364 	.compat_ioctl	= inotify_ioctl,
365 	.llseek		= noop_llseek,
366 };
367 
368 
369 /*
370  * find_inode - resolve a user-given path to a specific inode
371  */
inotify_find_inode(const char __user * dirname,struct path * path,unsigned int flags,__u64 mask)372 static int inotify_find_inode(const char __user *dirname, struct path *path,
373 						unsigned int flags, __u64 mask)
374 {
375 	int error;
376 
377 	error = user_path_at(AT_FDCWD, dirname, flags, path);
378 	if (error)
379 		return error;
380 	/* you can only watch an inode if you have read permissions on it */
381 	error = path_permission(path, MAY_READ);
382 	if (error) {
383 		path_put(path);
384 		return error;
385 	}
386 	error = security_path_notify(path, mask,
387 				FSNOTIFY_OBJ_TYPE_INODE);
388 	if (error)
389 		path_put(path);
390 
391 	return error;
392 }
393 
inotify_add_to_idr(struct idr * idr,spinlock_t * idr_lock,struct inotify_inode_mark * i_mark)394 static int inotify_add_to_idr(struct idr *idr, spinlock_t *idr_lock,
395 			      struct inotify_inode_mark *i_mark)
396 {
397 	int ret;
398 
399 	idr_preload(GFP_KERNEL);
400 	spin_lock(idr_lock);
401 
402 	ret = idr_alloc_cyclic(idr, i_mark, 1, 0, GFP_NOWAIT);
403 	if (ret >= 0) {
404 		/* we added the mark to the idr, take a reference */
405 		i_mark->wd = ret;
406 		fsnotify_get_mark(&i_mark->fsn_mark);
407 	}
408 
409 	spin_unlock(idr_lock);
410 	idr_preload_end();
411 	return ret < 0 ? ret : 0;
412 }
413 
inotify_idr_find_locked(struct fsnotify_group * group,int wd)414 static struct inotify_inode_mark *inotify_idr_find_locked(struct fsnotify_group *group,
415 								int wd)
416 {
417 	struct idr *idr = &group->inotify_data.idr;
418 	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
419 	struct inotify_inode_mark *i_mark;
420 
421 	assert_spin_locked(idr_lock);
422 
423 	i_mark = idr_find(idr, wd);
424 	if (i_mark) {
425 		struct fsnotify_mark *fsn_mark = &i_mark->fsn_mark;
426 
427 		fsnotify_get_mark(fsn_mark);
428 		/* One ref for being in the idr, one ref we just took */
429 		BUG_ON(refcount_read(&fsn_mark->refcnt) < 2);
430 	}
431 
432 	return i_mark;
433 }
434 
inotify_idr_find(struct fsnotify_group * group,int wd)435 static struct inotify_inode_mark *inotify_idr_find(struct fsnotify_group *group,
436 							 int wd)
437 {
438 	struct inotify_inode_mark *i_mark;
439 	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
440 
441 	spin_lock(idr_lock);
442 	i_mark = inotify_idr_find_locked(group, wd);
443 	spin_unlock(idr_lock);
444 
445 	return i_mark;
446 }
447 
448 /*
449  * Remove the mark from the idr (if present) and drop the reference
450  * on the mark because it was in the idr.
451  */
inotify_remove_from_idr(struct fsnotify_group * group,struct inotify_inode_mark * i_mark)452 static void inotify_remove_from_idr(struct fsnotify_group *group,
453 				    struct inotify_inode_mark *i_mark)
454 {
455 	struct idr *idr = &group->inotify_data.idr;
456 	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
457 	struct inotify_inode_mark *found_i_mark = NULL;
458 	int wd;
459 
460 	spin_lock(idr_lock);
461 	wd = i_mark->wd;
462 
463 	/*
464 	 * does this i_mark think it is in the idr?  we shouldn't get called
465 	 * if it wasn't....
466 	 */
467 	if (wd == -1) {
468 		WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
469 			__func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
470 		goto out;
471 	}
472 
473 	/* Lets look in the idr to see if we find it */
474 	found_i_mark = inotify_idr_find_locked(group, wd);
475 	if (unlikely(!found_i_mark)) {
476 		WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
477 			__func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
478 		goto out;
479 	}
480 
481 	/*
482 	 * We found an mark in the idr at the right wd, but it's
483 	 * not the mark we were told to remove.  eparis seriously
484 	 * fucked up somewhere.
485 	 */
486 	if (unlikely(found_i_mark != i_mark)) {
487 		WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p "
488 			"found_i_mark=%p found_i_mark->wd=%d "
489 			"found_i_mark->group=%p\n", __func__, i_mark,
490 			i_mark->wd, i_mark->fsn_mark.group, found_i_mark,
491 			found_i_mark->wd, found_i_mark->fsn_mark.group);
492 		goto out;
493 	}
494 
495 	/*
496 	 * One ref for being in the idr
497 	 * one ref grabbed by inotify_idr_find
498 	 */
499 	if (unlikely(refcount_read(&i_mark->fsn_mark.refcnt) < 2)) {
500 		printk(KERN_ERR "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
501 			 __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
502 		/* we can't really recover with bad ref cnting.. */
503 		BUG();
504 	}
505 
506 	idr_remove(idr, wd);
507 	/* Removed from the idr, drop that ref. */
508 	fsnotify_put_mark(&i_mark->fsn_mark);
509 out:
510 	i_mark->wd = -1;
511 	spin_unlock(idr_lock);
512 	/* match the ref taken by inotify_idr_find_locked() */
513 	if (found_i_mark)
514 		fsnotify_put_mark(&found_i_mark->fsn_mark);
515 }
516 
517 /*
518  * Send IN_IGNORED for this wd, remove this wd from the idr.
519  */
inotify_ignored_and_remove_idr(struct fsnotify_mark * fsn_mark,struct fsnotify_group * group)520 void inotify_ignored_and_remove_idr(struct fsnotify_mark *fsn_mark,
521 				    struct fsnotify_group *group)
522 {
523 	struct inotify_inode_mark *i_mark;
524 
525 	/* Queue ignore event for the watch */
526 	inotify_handle_inode_event(fsn_mark, FS_IN_IGNORED, NULL, NULL, NULL,
527 				   0);
528 
529 	i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
530 	/* remove this mark from the idr */
531 	inotify_remove_from_idr(group, i_mark);
532 
533 	dec_inotify_watches(group->inotify_data.ucounts);
534 }
535 
inotify_update_existing_watch(struct fsnotify_group * group,struct inode * inode,u32 arg)536 static int inotify_update_existing_watch(struct fsnotify_group *group,
537 					 struct inode *inode,
538 					 u32 arg)
539 {
540 	struct fsnotify_mark *fsn_mark;
541 	struct inotify_inode_mark *i_mark;
542 	int replace = !(arg & IN_MASK_ADD);
543 	int create = (arg & IN_MASK_CREATE);
544 	int ret;
545 
546 	fsn_mark = fsnotify_find_inode_mark(inode, group);
547 	if (!fsn_mark)
548 		return -ENOENT;
549 	else if (create) {
550 		ret = -EEXIST;
551 		goto out;
552 	}
553 
554 	i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
555 
556 	spin_lock(&fsn_mark->lock);
557 	if (replace) {
558 		fsn_mark->mask = 0;
559 		fsn_mark->flags &= ~INOTIFY_MARK_FLAGS;
560 	}
561 	fsn_mark->mask |= inotify_arg_to_mask(inode, arg);
562 	fsn_mark->flags |= inotify_arg_to_flags(arg);
563 	spin_unlock(&fsn_mark->lock);
564 
565 	fsnotify_recalc_mask(fsn_mark->connector);
566 
567 	/* return the wd */
568 	ret = i_mark->wd;
569 
570 out:
571 	/* match the get from fsnotify_find_mark() */
572 	fsnotify_put_mark(fsn_mark);
573 
574 	return ret;
575 }
576 
inotify_new_watch(struct fsnotify_group * group,struct inode * inode,u32 arg)577 static int inotify_new_watch(struct fsnotify_group *group,
578 			     struct inode *inode,
579 			     u32 arg)
580 {
581 	struct inotify_inode_mark *tmp_i_mark;
582 	int ret;
583 	struct idr *idr = &group->inotify_data.idr;
584 	spinlock_t *idr_lock = &group->inotify_data.idr_lock;
585 
586 	tmp_i_mark = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL);
587 	if (unlikely(!tmp_i_mark))
588 		return -ENOMEM;
589 
590 	fsnotify_init_mark(&tmp_i_mark->fsn_mark, group);
591 	tmp_i_mark->fsn_mark.mask = inotify_arg_to_mask(inode, arg);
592 	tmp_i_mark->fsn_mark.flags = inotify_arg_to_flags(arg);
593 	tmp_i_mark->wd = -1;
594 
595 	ret = inotify_add_to_idr(idr, idr_lock, tmp_i_mark);
596 	if (ret)
597 		goto out_err;
598 
599 	/* increment the number of watches the user has */
600 	if (!inc_inotify_watches(group->inotify_data.ucounts)) {
601 		inotify_remove_from_idr(group, tmp_i_mark);
602 		ret = -ENOSPC;
603 		goto out_err;
604 	}
605 
606 	/* we are on the idr, now get on the inode */
607 	ret = fsnotify_add_inode_mark_locked(&tmp_i_mark->fsn_mark, inode, 0);
608 	if (ret) {
609 		/* we failed to get on the inode, get off the idr */
610 		inotify_remove_from_idr(group, tmp_i_mark);
611 		dec_inotify_watches(group->inotify_data.ucounts);
612 		goto out_err;
613 	}
614 
615 
616 	/* return the watch descriptor for this new mark */
617 	ret = tmp_i_mark->wd;
618 
619 out_err:
620 	/* match the ref from fsnotify_init_mark() */
621 	fsnotify_put_mark(&tmp_i_mark->fsn_mark);
622 
623 	return ret;
624 }
625 
inotify_update_watch(struct fsnotify_group * group,struct inode * inode,u32 arg)626 static int inotify_update_watch(struct fsnotify_group *group, struct inode *inode, u32 arg)
627 {
628 	int ret = 0;
629 
630 	fsnotify_group_lock(group);
631 	/* try to update and existing watch with the new arg */
632 	ret = inotify_update_existing_watch(group, inode, arg);
633 	/* no mark present, try to add a new one */
634 	if (ret == -ENOENT)
635 		ret = inotify_new_watch(group, inode, arg);
636 	fsnotify_group_unlock(group);
637 
638 	return ret;
639 }
640 
inotify_new_group(unsigned int max_events)641 static struct fsnotify_group *inotify_new_group(unsigned int max_events)
642 {
643 	struct fsnotify_group *group;
644 	struct inotify_event_info *oevent;
645 
646 	group = fsnotify_alloc_group(&inotify_fsnotify_ops,
647 				     FSNOTIFY_GROUP_USER);
648 	if (IS_ERR(group))
649 		return group;
650 
651 	oevent = kmalloc_obj(struct inotify_event_info, GFP_KERNEL_ACCOUNT);
652 	if (unlikely(!oevent)) {
653 		fsnotify_destroy_group(group);
654 		return ERR_PTR(-ENOMEM);
655 	}
656 	group->overflow_event = &oevent->fse;
657 	fsnotify_init_event(group->overflow_event);
658 	oevent->mask = FS_Q_OVERFLOW;
659 	oevent->wd = -1;
660 	oevent->sync_cookie = 0;
661 	oevent->name_len = 0;
662 
663 	group->max_events = max_events;
664 	group->memcg = get_mem_cgroup_from_mm(current->mm);
665 
666 	spin_lock_init(&group->inotify_data.idr_lock);
667 	idr_init(&group->inotify_data.idr);
668 	group->inotify_data.ucounts = inc_ucount(current_user_ns(),
669 						 current_euid(),
670 						 UCOUNT_INOTIFY_INSTANCES);
671 
672 	if (!group->inotify_data.ucounts) {
673 		fsnotify_destroy_group(group);
674 		return ERR_PTR(-EMFILE);
675 	}
676 
677 	return group;
678 }
679 
680 
681 /* inotify syscalls */
do_inotify_init(int flags)682 static int do_inotify_init(int flags)
683 {
684 	struct fsnotify_group *group;
685 	int ret;
686 
687 	/* Check the IN_* constants for consistency.  */
688 	BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC);
689 	BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK);
690 
691 	if (flags & ~(IN_CLOEXEC | IN_NONBLOCK))
692 		return -EINVAL;
693 
694 	/* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */
695 	group = inotify_new_group(inotify_max_queued_events);
696 	if (IS_ERR(group))
697 		return PTR_ERR(group);
698 
699 	ret = anon_inode_getfd("inotify", &inotify_fops, group,
700 				  O_RDONLY | flags);
701 	if (ret < 0)
702 		fsnotify_destroy_group(group);
703 
704 	return ret;
705 }
706 
SYSCALL_DEFINE1(inotify_init1,int,flags)707 SYSCALL_DEFINE1(inotify_init1, int, flags)
708 {
709 	return do_inotify_init(flags);
710 }
711 
SYSCALL_DEFINE0(inotify_init)712 SYSCALL_DEFINE0(inotify_init)
713 {
714 	return do_inotify_init(0);
715 }
716 
SYSCALL_DEFINE3(inotify_add_watch,int,fd,const char __user *,pathname,u32,mask)717 SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,
718 		u32, mask)
719 {
720 	struct fsnotify_group *group;
721 	struct inode *inode;
722 	struct path path;
723 	int ret;
724 	unsigned flags = 0;
725 
726 	/*
727 	 * We share a lot of code with fs/dnotify.  We also share
728 	 * the bit layout between inotify's IN_* and the fsnotify
729 	 * FS_*.  This check ensures that only the inotify IN_*
730 	 * bits get passed in and set in watches/events.
731 	 */
732 	if (unlikely(mask & ~ALL_INOTIFY_BITS))
733 		return -EINVAL;
734 	/*
735 	 * Require at least one valid bit set in the mask.
736 	 * Without _something_ set, we would have no events to
737 	 * watch for.
738 	 */
739 	if (unlikely(!(mask & ALL_INOTIFY_BITS)))
740 		return -EINVAL;
741 
742 	CLASS(fd, f)(fd);
743 	if (fd_empty(f))
744 		return -EBADF;
745 
746 	/* IN_MASK_ADD and IN_MASK_CREATE don't make sense together */
747 	if (unlikely((mask & IN_MASK_ADD) && (mask & IN_MASK_CREATE)))
748 		return -EINVAL;
749 
750 	/* verify that this is indeed an inotify instance */
751 	if (unlikely(fd_file(f)->f_op != &inotify_fops))
752 		return -EINVAL;
753 
754 	if (!(mask & IN_DONT_FOLLOW))
755 		flags |= LOOKUP_FOLLOW;
756 	if (mask & IN_ONLYDIR)
757 		flags |= LOOKUP_DIRECTORY;
758 
759 	ret = inotify_find_inode(pathname, &path, flags,
760 			(mask & IN_ALL_EVENTS));
761 	if (ret)
762 		return ret;
763 
764 	/* inode held in place by reference to path; group by fget on fd */
765 	inode = path.dentry->d_inode;
766 	group = fd_file(f)->private_data;
767 
768 	/* create/update an inode mark */
769 	ret = inotify_update_watch(group, inode, mask);
770 	path_put(&path);
771 	return ret;
772 }
773 
SYSCALL_DEFINE2(inotify_rm_watch,int,fd,__s32,wd)774 SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd)
775 {
776 	struct fsnotify_group *group;
777 	struct inotify_inode_mark *i_mark;
778 	CLASS(fd, f)(fd);
779 
780 	if (fd_empty(f))
781 		return -EBADF;
782 
783 	/* verify that this is indeed an inotify instance */
784 	if (unlikely(fd_file(f)->f_op != &inotify_fops))
785 		return -EINVAL;
786 
787 	group = fd_file(f)->private_data;
788 
789 	i_mark = inotify_idr_find(group, wd);
790 	if (unlikely(!i_mark))
791 		return -EINVAL;
792 
793 	fsnotify_destroy_mark(&i_mark->fsn_mark, group);
794 
795 	/* match ref taken by inotify_idr_find */
796 	fsnotify_put_mark(&i_mark->fsn_mark);
797 	return 0;
798 }
799 
800 /*
801  * inotify_user_setup - Our initialization function.  Note that we cannot return
802  * error because we have compiled-in VFS hooks.  So an (unlikely) failure here
803  * must result in panic().
804  */
inotify_user_setup(void)805 static int __init inotify_user_setup(void)
806 {
807 	unsigned long watches_max;
808 	struct sysinfo si;
809 
810 	si_meminfo(&si);
811 	/*
812 	 * Allow up to 1% of addressable memory to be allocated for inotify
813 	 * watches (per user) limited to the range [8192, 1048576].
814 	 */
815 	watches_max = (((si.totalram - si.totalhigh) / 100) << PAGE_SHIFT) /
816 			INOTIFY_WATCH_COST;
817 	watches_max = clamp(watches_max, 8192UL, 1048576UL);
818 
819 	BUILD_BUG_ON(IN_ACCESS != FS_ACCESS);
820 	BUILD_BUG_ON(IN_MODIFY != FS_MODIFY);
821 	BUILD_BUG_ON(IN_ATTRIB != FS_ATTRIB);
822 	BUILD_BUG_ON(IN_CLOSE_WRITE != FS_CLOSE_WRITE);
823 	BUILD_BUG_ON(IN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
824 	BUILD_BUG_ON(IN_OPEN != FS_OPEN);
825 	BUILD_BUG_ON(IN_MOVED_FROM != FS_MOVED_FROM);
826 	BUILD_BUG_ON(IN_MOVED_TO != FS_MOVED_TO);
827 	BUILD_BUG_ON(IN_CREATE != FS_CREATE);
828 	BUILD_BUG_ON(IN_DELETE != FS_DELETE);
829 	BUILD_BUG_ON(IN_DELETE_SELF != FS_DELETE_SELF);
830 	BUILD_BUG_ON(IN_MOVE_SELF != FS_MOVE_SELF);
831 	BUILD_BUG_ON(IN_UNMOUNT != FS_UNMOUNT);
832 	BUILD_BUG_ON(IN_Q_OVERFLOW != FS_Q_OVERFLOW);
833 	BUILD_BUG_ON(IN_IGNORED != FS_IN_IGNORED);
834 	BUILD_BUG_ON(IN_ISDIR != FS_ISDIR);
835 
836 	BUILD_BUG_ON(HWEIGHT32(ALL_INOTIFY_BITS) != 22);
837 
838 	inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark,
839 					       SLAB_PANIC|SLAB_ACCOUNT);
840 
841 	inotify_max_queued_events = 16384;
842 	init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES] = 128;
843 	init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES] = watches_max;
844 	inotify_sysctls_init();
845 
846 	return 0;
847 }
848 fs_initcall(inotify_user_setup);
849