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