fanotify_user.c 13 KB

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