fanotify_user.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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/namei.h>
  8. #include <linux/security.h>
  9. #include <linux/syscalls.h>
  10. #include <linux/types.h>
  11. #include "fanotify.h"
  12. static struct kmem_cache *fanotify_mark_cache __read_mostly;
  13. static int fanotify_release(struct inode *ignored, struct file *file)
  14. {
  15. struct fsnotify_group *group = file->private_data;
  16. pr_debug("%s: file=%p group=%p\n", __func__, file, group);
  17. /* matches the fanotify_init->fsnotify_alloc_group */
  18. fsnotify_put_group(group);
  19. return 0;
  20. }
  21. static const struct file_operations fanotify_fops = {
  22. .poll = NULL,
  23. .read = NULL,
  24. .fasync = NULL,
  25. .release = fanotify_release,
  26. .unlocked_ioctl = NULL,
  27. .compat_ioctl = NULL,
  28. };
  29. static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
  30. {
  31. kmem_cache_free(fanotify_mark_cache, fsn_mark);
  32. }
  33. static int fanotify_find_path(int dfd, const char __user *filename,
  34. struct path *path, unsigned int flags)
  35. {
  36. int ret;
  37. pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
  38. dfd, filename, flags);
  39. if (filename == NULL) {
  40. struct file *file;
  41. int fput_needed;
  42. ret = -EBADF;
  43. file = fget_light(dfd, &fput_needed);
  44. if (!file)
  45. goto out;
  46. ret = -ENOTDIR;
  47. if ((flags & FAN_MARK_ONLYDIR) &&
  48. !(S_ISDIR(file->f_path.dentry->d_inode->i_mode))) {
  49. fput_light(file, fput_needed);
  50. goto out;
  51. }
  52. *path = file->f_path;
  53. path_get(path);
  54. fput_light(file, fput_needed);
  55. } else {
  56. unsigned int lookup_flags = 0;
  57. if (!(flags & FAN_MARK_DONT_FOLLOW))
  58. lookup_flags |= LOOKUP_FOLLOW;
  59. if (flags & FAN_MARK_ONLYDIR)
  60. lookup_flags |= LOOKUP_DIRECTORY;
  61. ret = user_path_at(dfd, filename, lookup_flags, path);
  62. if (ret)
  63. goto out;
  64. }
  65. /* you can only watch an inode if you have read permissions on it */
  66. ret = inode_permission(path->dentry->d_inode, MAY_READ);
  67. if (ret)
  68. path_put(path);
  69. out:
  70. return ret;
  71. }
  72. static int fanotify_remove_mark(struct fsnotify_group *group,
  73. struct inode *inode,
  74. __u32 mask)
  75. {
  76. struct fsnotify_mark *fsn_mark;
  77. __u32 new_mask;
  78. pr_debug("%s: group=%p inode=%p mask=%x\n", __func__,
  79. group, inode, mask);
  80. fsn_mark = fsnotify_find_mark(group, inode);
  81. if (!fsn_mark)
  82. return -ENOENT;
  83. spin_lock(&fsn_mark->lock);
  84. fsn_mark->mask &= ~mask;
  85. new_mask = fsn_mark->mask;
  86. spin_unlock(&fsn_mark->lock);
  87. if (!new_mask)
  88. fsnotify_destroy_mark(fsn_mark);
  89. else
  90. fsnotify_recalc_inode_mask(inode);
  91. fsnotify_recalc_group_mask(group);
  92. /* matches the fsnotify_find_mark() */
  93. fsnotify_put_mark(fsn_mark);
  94. return 0;
  95. }
  96. static int fanotify_add_mark(struct fsnotify_group *group,
  97. struct inode *inode,
  98. __u32 mask)
  99. {
  100. struct fsnotify_mark *fsn_mark;
  101. __u32 old_mask, new_mask;
  102. int ret;
  103. pr_debug("%s: group=%p inode=%p mask=%x\n", __func__,
  104. group, inode, mask);
  105. fsn_mark = fsnotify_find_mark(group, inode);
  106. if (!fsn_mark) {
  107. struct fsnotify_mark *new_fsn_mark;
  108. ret = -ENOMEM;
  109. new_fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
  110. if (!new_fsn_mark)
  111. goto out;
  112. fsnotify_init_mark(new_fsn_mark, fanotify_free_mark);
  113. ret = fsnotify_add_mark(new_fsn_mark, group, inode, 0);
  114. if (ret) {
  115. fanotify_free_mark(new_fsn_mark);
  116. goto out;
  117. }
  118. fsn_mark = new_fsn_mark;
  119. }
  120. ret = 0;
  121. spin_lock(&fsn_mark->lock);
  122. old_mask = fsn_mark->mask;
  123. fsn_mark->mask |= mask;
  124. new_mask = fsn_mark->mask;
  125. spin_unlock(&fsn_mark->lock);
  126. /* we made changes to a mask, update the group mask and the inode mask
  127. * so things happen quickly. */
  128. if (old_mask != new_mask) {
  129. /* more bits in old than in new? */
  130. int dropped = (old_mask & ~new_mask);
  131. /* more bits in this mark than the inode's mask? */
  132. int do_inode = (new_mask & ~inode->i_fsnotify_mask);
  133. /* more bits in this mark than the group? */
  134. int do_group = (new_mask & ~group->mask);
  135. /* update the inode with this new mark */
  136. if (dropped || do_inode)
  137. fsnotify_recalc_inode_mask(inode);
  138. /* update the group mask with the new mask */
  139. if (dropped || do_group)
  140. fsnotify_recalc_group_mask(group);
  141. }
  142. /* match the init or the find.... */
  143. fsnotify_put_mark(fsn_mark);
  144. out:
  145. return ret;
  146. }
  147. static int fanotify_update_mark(struct fsnotify_group *group,
  148. struct inode *inode, int flags,
  149. __u32 mask)
  150. {
  151. pr_debug("%s: group=%p inode=%p flags=%x mask=%x\n", __func__,
  152. group, inode, flags, mask);
  153. if (flags & FAN_MARK_ADD)
  154. fanotify_add_mark(group, inode, mask);
  155. else if (flags & FAN_MARK_REMOVE)
  156. fanotify_remove_mark(group, inode, mask);
  157. else
  158. BUG();
  159. return 0;
  160. }
  161. static bool fanotify_mark_validate_input(int flags,
  162. __u32 mask)
  163. {
  164. pr_debug("%s: flags=%x mask=%x\n", __func__, flags, mask);
  165. /* are flags valid of this operation? */
  166. if (!fanotify_mark_flags_valid(flags))
  167. return false;
  168. /* is the mask valid? */
  169. if (!fanotify_mask_valid(mask))
  170. return false;
  171. return true;
  172. }
  173. /* fanotify syscalls */
  174. SYSCALL_DEFINE3(fanotify_init, unsigned int, flags, unsigned int, event_f_flags,
  175. unsigned int, priority)
  176. {
  177. struct fsnotify_group *group;
  178. int f_flags, fd;
  179. pr_debug("%s: flags=%d event_f_flags=%d priority=%d\n",
  180. __func__, flags, event_f_flags, priority);
  181. if (event_f_flags)
  182. return -EINVAL;
  183. if (priority)
  184. return -EINVAL;
  185. if (!capable(CAP_SYS_ADMIN))
  186. return -EACCES;
  187. if (flags & ~FAN_ALL_INIT_FLAGS)
  188. return -EINVAL;
  189. f_flags = (O_RDONLY | FMODE_NONOTIFY);
  190. if (flags & FAN_CLOEXEC)
  191. f_flags |= O_CLOEXEC;
  192. if (flags & FAN_NONBLOCK)
  193. f_flags |= O_NONBLOCK;
  194. /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
  195. group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
  196. if (IS_ERR(group))
  197. return PTR_ERR(group);
  198. fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
  199. if (fd < 0)
  200. goto out_put_group;
  201. return fd;
  202. out_put_group:
  203. fsnotify_put_group(group);
  204. return fd;
  205. }
  206. SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
  207. __u64, mask, int, dfd, const char __user *, pathname)
  208. {
  209. struct inode *inode;
  210. struct fsnotify_group *group;
  211. struct file *filp;
  212. struct path path;
  213. int ret, fput_needed;
  214. pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
  215. __func__, fanotify_fd, flags, dfd, pathname, mask);
  216. /* we only use the lower 32 bits as of right now. */
  217. if (mask & ((__u64)0xffffffff << 32))
  218. return -EINVAL;
  219. if (!fanotify_mark_validate_input(flags, mask))
  220. return -EINVAL;
  221. filp = fget_light(fanotify_fd, &fput_needed);
  222. if (unlikely(!filp))
  223. return -EBADF;
  224. /* verify that this is indeed an fanotify instance */
  225. ret = -EINVAL;
  226. if (unlikely(filp->f_op != &fanotify_fops))
  227. goto fput_and_out;
  228. ret = fanotify_find_path(dfd, pathname, &path, flags);
  229. if (ret)
  230. goto fput_and_out;
  231. /* inode held in place by reference to path; group by fget on fd */
  232. inode = path.dentry->d_inode;
  233. group = filp->private_data;
  234. /* create/update an inode mark */
  235. ret = fanotify_update_mark(group, inode, flags, mask);
  236. path_put(&path);
  237. fput_and_out:
  238. fput_light(filp, fput_needed);
  239. return ret;
  240. }
  241. /*
  242. * fanotify_user_setup - Our initialization function. Note that we cannnot return
  243. * error because we have compiled-in VFS hooks. So an (unlikely) failure here
  244. * must result in panic().
  245. */
  246. static int __init fanotify_user_setup(void)
  247. {
  248. fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
  249. return 0;
  250. }
  251. device_initcall(fanotify_user_setup);