inotify_user.c 22 KB

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