fanotify_user.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. #include <linux/fcntl.h>
  2. #include <linux/file.h>
  3. #include <linux/fs.h>
  4. #include <linux/anon_inodes.h>
  5. #include <linux/fsnotify_backend.h>
  6. #include <linux/init.h>
  7. #include <linux/mount.h>
  8. #include <linux/namei.h>
  9. #include <linux/poll.h>
  10. #include <linux/security.h>
  11. #include <linux/syscalls.h>
  12. #include <linux/types.h>
  13. #include <linux/uaccess.h>
  14. #include <asm/ioctls.h>
  15. #include "fanotify.h"
  16. static struct kmem_cache *fanotify_mark_cache __read_mostly;
  17. /*
  18. * Get an fsnotify notification event if one exists and is small
  19. * enough to fit in "count". Return an error pointer if the count
  20. * is not large enough.
  21. *
  22. * Called with the group->notification_mutex held.
  23. */
  24. static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
  25. size_t count)
  26. {
  27. BUG_ON(!mutex_is_locked(&group->notification_mutex));
  28. pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
  29. if (fsnotify_notify_queue_is_empty(group))
  30. return NULL;
  31. if (FAN_EVENT_METADATA_LEN > count)
  32. return ERR_PTR(-EINVAL);
  33. /* held the notification_mutex the whole time, so this is the
  34. * same event we peeked above */
  35. return fsnotify_remove_notify_event(group);
  36. }
  37. static int create_fd(struct fsnotify_group *group, struct fsnotify_event *event)
  38. {
  39. int client_fd;
  40. struct dentry *dentry;
  41. struct vfsmount *mnt;
  42. struct file *new_file;
  43. pr_debug("%s: group=%p event=%p\n", __func__, group, event);
  44. client_fd = get_unused_fd();
  45. if (client_fd < 0)
  46. return client_fd;
  47. if (event->data_type != FSNOTIFY_EVENT_PATH) {
  48. WARN_ON(1);
  49. put_unused_fd(client_fd);
  50. return -EINVAL;
  51. }
  52. /*
  53. * we need a new file handle for the userspace program so it can read even if it was
  54. * originally opened O_WRONLY.
  55. */
  56. dentry = dget(event->path.dentry);
  57. mnt = mntget(event->path.mnt);
  58. /* it's possible this event was an overflow event. in that case dentry and mnt
  59. * are NULL; That's fine, just don't call dentry open */
  60. if (dentry && mnt)
  61. new_file = dentry_open(dentry, mnt,
  62. O_RDONLY | O_LARGEFILE | FMODE_NONOTIFY,
  63. current_cred());
  64. else
  65. new_file = ERR_PTR(-EOVERFLOW);
  66. if (IS_ERR(new_file)) {
  67. /*
  68. * we still send an event even if we can't open the file. this
  69. * can happen when say tasks are gone and we try to open their
  70. * /proc files or we try to open a WRONLY file like in sysfs
  71. * we just send the errno to userspace since there isn't much
  72. * else we can do.
  73. */
  74. put_unused_fd(client_fd);
  75. client_fd = PTR_ERR(new_file);
  76. } else {
  77. fd_install(client_fd, new_file);
  78. }
  79. return client_fd;
  80. }
  81. static ssize_t fill_event_metadata(struct fsnotify_group *group,
  82. struct fanotify_event_metadata *metadata,
  83. struct fsnotify_event *event)
  84. {
  85. pr_debug("%s: group=%p metadata=%p event=%p\n", __func__,
  86. group, metadata, event);
  87. metadata->event_len = FAN_EVENT_METADATA_LEN;
  88. metadata->vers = FANOTIFY_METADATA_VERSION;
  89. metadata->mask = fanotify_outgoing_mask(event->mask);
  90. metadata->pid = pid_vnr(event->tgid);
  91. metadata->fd = create_fd(group, event);
  92. return metadata->fd;
  93. }
  94. static ssize_t copy_event_to_user(struct fsnotify_group *group,
  95. struct fsnotify_event *event,
  96. char __user *buf)
  97. {
  98. struct fanotify_event_metadata fanotify_event_metadata;
  99. int ret;
  100. pr_debug("%s: group=%p event=%p\n", __func__, group, event);
  101. ret = fill_event_metadata(group, &fanotify_event_metadata, event);
  102. if (ret < 0)
  103. return ret;
  104. if (copy_to_user(buf, &fanotify_event_metadata, FAN_EVENT_METADATA_LEN))
  105. return -EFAULT;
  106. return FAN_EVENT_METADATA_LEN;
  107. }
  108. /* intofiy userspace file descriptor functions */
  109. static unsigned int fanotify_poll(struct file *file, poll_table *wait)
  110. {
  111. struct fsnotify_group *group = file->private_data;
  112. int ret = 0;
  113. poll_wait(file, &group->notification_waitq, wait);
  114. mutex_lock(&group->notification_mutex);
  115. if (!fsnotify_notify_queue_is_empty(group))
  116. ret = POLLIN | POLLRDNORM;
  117. mutex_unlock(&group->notification_mutex);
  118. return ret;
  119. }
  120. static ssize_t fanotify_read(struct file *file, char __user *buf,
  121. size_t count, loff_t *pos)
  122. {
  123. struct fsnotify_group *group;
  124. struct fsnotify_event *kevent;
  125. char __user *start;
  126. int ret;
  127. DEFINE_WAIT(wait);
  128. start = buf;
  129. group = file->private_data;
  130. pr_debug("%s: group=%p\n", __func__, group);
  131. while (1) {
  132. prepare_to_wait(&group->notification_waitq, &wait, TASK_INTERRUPTIBLE);
  133. mutex_lock(&group->notification_mutex);
  134. kevent = get_one_event(group, count);
  135. mutex_unlock(&group->notification_mutex);
  136. if (kevent) {
  137. ret = PTR_ERR(kevent);
  138. if (IS_ERR(kevent))
  139. break;
  140. ret = copy_event_to_user(group, kevent, buf);
  141. fsnotify_put_event(kevent);
  142. if (ret < 0)
  143. break;
  144. buf += ret;
  145. count -= ret;
  146. continue;
  147. }
  148. ret = -EAGAIN;
  149. if (file->f_flags & O_NONBLOCK)
  150. break;
  151. ret = -EINTR;
  152. if (signal_pending(current))
  153. break;
  154. if (start != buf)
  155. break;
  156. schedule();
  157. }
  158. finish_wait(&group->notification_waitq, &wait);
  159. if (start != buf && ret != -EFAULT)
  160. ret = buf - start;
  161. return ret;
  162. }
  163. static int fanotify_release(struct inode *ignored, struct file *file)
  164. {
  165. struct fsnotify_group *group = file->private_data;
  166. pr_debug("%s: file=%p group=%p\n", __func__, file, group);
  167. /* matches the fanotify_init->fsnotify_alloc_group */
  168. fsnotify_put_group(group);
  169. return 0;
  170. }
  171. static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  172. {
  173. struct fsnotify_group *group;
  174. struct fsnotify_event_holder *holder;
  175. void __user *p;
  176. int ret = -ENOTTY;
  177. size_t send_len = 0;
  178. group = file->private_data;
  179. p = (void __user *) arg;
  180. switch (cmd) {
  181. case FIONREAD:
  182. mutex_lock(&group->notification_mutex);
  183. list_for_each_entry(holder, &group->notification_list, event_list)
  184. send_len += FAN_EVENT_METADATA_LEN;
  185. mutex_unlock(&group->notification_mutex);
  186. ret = put_user(send_len, (int __user *) p);
  187. break;
  188. }
  189. return ret;
  190. }
  191. static const struct file_operations fanotify_fops = {
  192. .poll = fanotify_poll,
  193. .read = fanotify_read,
  194. .fasync = NULL,
  195. .release = fanotify_release,
  196. .unlocked_ioctl = fanotify_ioctl,
  197. .compat_ioctl = fanotify_ioctl,
  198. };
  199. static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
  200. {
  201. kmem_cache_free(fanotify_mark_cache, fsn_mark);
  202. }
  203. static int fanotify_find_path(int dfd, const char __user *filename,
  204. struct path *path, unsigned int flags)
  205. {
  206. int ret;
  207. pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
  208. dfd, filename, flags);
  209. if (filename == NULL) {
  210. struct file *file;
  211. int fput_needed;
  212. ret = -EBADF;
  213. file = fget_light(dfd, &fput_needed);
  214. if (!file)
  215. goto out;
  216. ret = -ENOTDIR;
  217. if ((flags & FAN_MARK_ONLYDIR) &&
  218. !(S_ISDIR(file->f_path.dentry->d_inode->i_mode))) {
  219. fput_light(file, fput_needed);
  220. goto out;
  221. }
  222. *path = file->f_path;
  223. path_get(path);
  224. fput_light(file, fput_needed);
  225. } else {
  226. unsigned int lookup_flags = 0;
  227. if (!(flags & FAN_MARK_DONT_FOLLOW))
  228. lookup_flags |= LOOKUP_FOLLOW;
  229. if (flags & FAN_MARK_ONLYDIR)
  230. lookup_flags |= LOOKUP_DIRECTORY;
  231. ret = user_path_at(dfd, filename, lookup_flags, path);
  232. if (ret)
  233. goto out;
  234. }
  235. /* you can only watch an inode if you have read permissions on it */
  236. ret = inode_permission(path->dentry->d_inode, MAY_READ);
  237. if (ret)
  238. path_put(path);
  239. out:
  240. return ret;
  241. }
  242. static void fanotify_update_object_mask(struct fsnotify_group *group,
  243. struct inode *inode,
  244. struct vfsmount *mnt,
  245. struct fsnotify_mark *fsn_mark,
  246. unsigned int flags,
  247. __u32 mask)
  248. {
  249. __u32 old_mask, new_mask;
  250. pr_debug("%s: group=%p inode=%p mnt=%p fsn_mark=%p flags=%x mask=%x\n",
  251. __func__, group, inode, mnt, fsn_mark, flags, mask);
  252. spin_lock(&fsn_mark->lock);
  253. old_mask = fsn_mark->mask;
  254. if (flags & FAN_MARK_ADD)
  255. fsn_mark->mask |= mask;
  256. else if (flags & FAN_MARK_REMOVE)
  257. fsn_mark->mask &= ~mask;
  258. else
  259. BUG();
  260. new_mask = fsn_mark->mask;
  261. spin_unlock(&fsn_mark->lock);
  262. if (!new_mask)
  263. fsnotify_destroy_mark(fsn_mark);
  264. /* we made changes to a mask, update the group mask and the object mask
  265. * so things happen quickly. */
  266. if (old_mask != new_mask) {
  267. __u32 dropped, do_object, do_group;
  268. /* more bits in old than in new? */
  269. dropped = (old_mask & ~new_mask);
  270. /* more bits in this fsn_mark than the group? */
  271. do_group = (new_mask & ~group->mask);
  272. if (inode) {
  273. /* more bits in this fsn_mark than the object's mask? */
  274. do_object = (new_mask & ~inode->i_fsnotify_mask);
  275. /* update the object with this new fsn_mark */
  276. if (dropped || do_object)
  277. fsnotify_recalc_inode_mask(inode);
  278. } else if (mnt) {
  279. /* more bits in this fsn_mark than the object's mask? */
  280. do_object = (new_mask & ~mnt->mnt_fsnotify_mask);
  281. /* update the object with this new fsn_mark */
  282. if (dropped || do_object)
  283. fsnotify_recalc_vfsmount_mask(mnt);
  284. } else {
  285. BUG();
  286. }
  287. /* update the group mask with the new mask */
  288. if (dropped || do_group)
  289. fsnotify_recalc_group_mask(group);
  290. }
  291. }
  292. static int fanotify_remove_mark(struct fsnotify_group *group, struct inode *inode,
  293. struct vfsmount *mnt, unsigned int flags, __u32 mask)
  294. {
  295. struct fsnotify_mark *fsn_mark = NULL;
  296. BUG_ON(inode && mnt);
  297. BUG_ON(!inode && !mnt);
  298. if (inode)
  299. fsn_mark = fsnotify_find_inode_mark(group, inode);
  300. else if (mnt)
  301. fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
  302. else
  303. BUG();
  304. if (!fsn_mark)
  305. return -ENOENT;
  306. fanotify_update_object_mask(group, inode, mnt, fsn_mark, flags, mask);
  307. /* matches the fsnotify_find_inode_mark() */
  308. fsnotify_put_mark(fsn_mark);
  309. return 0;
  310. }
  311. static struct fsnotify_mark *fanotify_add_vfsmount_mark(struct fsnotify_group *group,
  312. struct vfsmount *mnt)
  313. {
  314. struct fsnotify_mark *fsn_mark;
  315. fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
  316. if (!fsn_mark) {
  317. struct fsnotify_mark *new_fsn_mark;
  318. int ret;
  319. fsn_mark = ERR_PTR(-ENOMEM);
  320. new_fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
  321. if (!new_fsn_mark)
  322. goto out;
  323. fsnotify_init_mark(new_fsn_mark, fanotify_free_mark);
  324. ret = fsnotify_add_mark(new_fsn_mark, group, NULL, mnt, 0);
  325. if (ret) {
  326. fsn_mark = ERR_PTR(ret);
  327. fanotify_free_mark(new_fsn_mark);
  328. goto out;
  329. }
  330. fsn_mark = new_fsn_mark;
  331. }
  332. out:
  333. return fsn_mark;
  334. }
  335. static struct fsnotify_mark *fanotify_add_inode_mark(struct fsnotify_group *group,
  336. struct inode *inode)
  337. {
  338. struct fsnotify_mark *fsn_mark;
  339. pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
  340. fsn_mark = fsnotify_find_inode_mark(group, inode);
  341. if (!fsn_mark) {
  342. struct fsnotify_mark *new_fsn_mark;
  343. int ret;
  344. fsn_mark = ERR_PTR(-ENOMEM);
  345. new_fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
  346. if (!new_fsn_mark)
  347. goto out;
  348. fsnotify_init_mark(new_fsn_mark, fanotify_free_mark);
  349. ret = fsnotify_add_mark(new_fsn_mark, group, inode, NULL, 0);
  350. if (ret) {
  351. fsn_mark = ERR_PTR(ret);
  352. fanotify_free_mark(new_fsn_mark);
  353. goto out;
  354. }
  355. fsn_mark = new_fsn_mark;
  356. }
  357. out:
  358. return fsn_mark;
  359. }
  360. static int fanotify_add_mark(struct fsnotify_group *group, struct inode *inode,
  361. struct vfsmount *mnt, unsigned int flags, __u32 mask)
  362. {
  363. struct fsnotify_mark *fsn_mark;
  364. pr_debug("%s: group=%p inode=%p mnt=%p flags=%x mask=%x\n",
  365. __func__, group, inode, mnt, flags, mask);
  366. BUG_ON(inode && mnt);
  367. BUG_ON(!inode && !mnt);
  368. if (inode)
  369. fsn_mark = fanotify_add_inode_mark(group, inode);
  370. else if (mnt)
  371. fsn_mark = fanotify_add_vfsmount_mark(group, mnt);
  372. else
  373. BUG();
  374. if (IS_ERR(fsn_mark))
  375. goto out;
  376. fanotify_update_object_mask(group, inode, mnt, fsn_mark, flags, mask);
  377. /* match the init or the find.... */
  378. fsnotify_put_mark(fsn_mark);
  379. out:
  380. return PTR_ERR(fsn_mark);
  381. }
  382. static int fanotify_update_mark(struct fsnotify_group *group,
  383. struct inode *inode, struct vfsmount *mnt,
  384. int flags, __u32 mask)
  385. {
  386. pr_debug("%s: group=%p inode=%p mnt=%p flags=%x mask=%x\n",
  387. __func__, group, inode, mnt, flags, mask);
  388. BUG_ON(inode && mnt);
  389. BUG_ON(!inode && !mnt);
  390. if (flags & FAN_MARK_ADD)
  391. fanotify_add_mark(group, inode, mnt, flags, mask);
  392. else if (flags & FAN_MARK_REMOVE)
  393. fanotify_remove_mark(group, inode, mnt, flags, mask);
  394. else
  395. BUG();
  396. return 0;
  397. }
  398. static bool fanotify_mark_validate_input(int flags,
  399. __u32 mask)
  400. {
  401. pr_debug("%s: flags=%x mask=%x\n", __func__, flags, mask);
  402. /* are flags valid of this operation? */
  403. if (!fanotify_mark_flags_valid(flags))
  404. return false;
  405. /* is the mask valid? */
  406. if (!fanotify_mask_valid(mask))
  407. return false;
  408. return true;
  409. }
  410. /* fanotify syscalls */
  411. SYSCALL_DEFINE3(fanotify_init, unsigned int, flags, unsigned int, event_f_flags,
  412. unsigned int, priority)
  413. {
  414. struct fsnotify_group *group;
  415. int f_flags, fd;
  416. pr_debug("%s: flags=%d event_f_flags=%d priority=%d\n",
  417. __func__, flags, event_f_flags, priority);
  418. if (event_f_flags)
  419. return -EINVAL;
  420. if (priority)
  421. return -EINVAL;
  422. if (!capable(CAP_SYS_ADMIN))
  423. return -EACCES;
  424. if (flags & ~FAN_ALL_INIT_FLAGS)
  425. return -EINVAL;
  426. f_flags = (O_RDONLY | FMODE_NONOTIFY);
  427. if (flags & FAN_CLOEXEC)
  428. f_flags |= O_CLOEXEC;
  429. if (flags & FAN_NONBLOCK)
  430. f_flags |= O_NONBLOCK;
  431. /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
  432. group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
  433. if (IS_ERR(group))
  434. return PTR_ERR(group);
  435. fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
  436. if (fd < 0)
  437. goto out_put_group;
  438. return fd;
  439. out_put_group:
  440. fsnotify_put_group(group);
  441. return fd;
  442. }
  443. SYSCALL_DEFINE(fanotify_mark)(int fanotify_fd, unsigned int flags,
  444. __u64 mask, int dfd,
  445. const char __user * pathname)
  446. {
  447. struct inode *inode;
  448. struct fsnotify_group *group;
  449. struct file *filp;
  450. struct path path;
  451. int ret, fput_needed;
  452. pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
  453. __func__, fanotify_fd, flags, dfd, pathname, mask);
  454. /* we only use the lower 32 bits as of right now. */
  455. if (mask & ((__u64)0xffffffff << 32))
  456. return -EINVAL;
  457. if (!fanotify_mark_validate_input(flags, mask))
  458. return -EINVAL;
  459. filp = fget_light(fanotify_fd, &fput_needed);
  460. if (unlikely(!filp))
  461. return -EBADF;
  462. /* verify that this is indeed an fanotify instance */
  463. ret = -EINVAL;
  464. if (unlikely(filp->f_op != &fanotify_fops))
  465. goto fput_and_out;
  466. ret = fanotify_find_path(dfd, pathname, &path, flags);
  467. if (ret)
  468. goto fput_and_out;
  469. /* inode held in place by reference to path; group by fget on fd */
  470. inode = path.dentry->d_inode;
  471. group = filp->private_data;
  472. /* create/update an inode mark */
  473. ret = fanotify_update_mark(group, inode, NULL, flags, mask);
  474. path_put(&path);
  475. fput_and_out:
  476. fput_light(filp, fput_needed);
  477. return ret;
  478. }
  479. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  480. asmlinkage long SyS_fanotify_mark(long fanotify_fd, long flags, __u64 mask,
  481. long dfd, long pathname)
  482. {
  483. return SYSC_fanotify_mark((int) fanotify_fd, (unsigned int) flags,
  484. mask, (int) dfd,
  485. (const char __user *) pathname);
  486. }
  487. SYSCALL_ALIAS(sys_fanotify_mark, SyS_fanotify_mark);
  488. #endif
  489. /*
  490. * fanotify_user_setup - Our initialization function. Note that we cannnot return
  491. * error because we have compiled-in VFS hooks. So an (unlikely) failure here
  492. * must result in panic().
  493. */
  494. static int __init fanotify_user_setup(void)
  495. {
  496. fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
  497. return 0;
  498. }
  499. device_initcall(fanotify_user_setup);