fanotify_user.c 12 KB

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