fanotify.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/fanotify.h>
  3. #include <linux/fdtable.h>
  4. #include <linux/fsnotify_backend.h>
  5. #include <linux/init.h>
  6. #include <linux/jiffies.h>
  7. #include <linux/kernel.h> /* UINT_MAX */
  8. #include <linux/mount.h>
  9. #include <linux/sched.h>
  10. #include <linux/sched/user.h>
  11. #include <linux/types.h>
  12. #include <linux/wait.h>
  13. #include "fanotify.h"
  14. static bool should_merge(struct fsnotify_event *old_fsn,
  15. struct fsnotify_event *new_fsn)
  16. {
  17. struct fanotify_event_info *old, *new;
  18. pr_debug("%s: old=%p new=%p\n", __func__, old_fsn, new_fsn);
  19. old = FANOTIFY_E(old_fsn);
  20. new = FANOTIFY_E(new_fsn);
  21. if (old_fsn->inode == new_fsn->inode && old->tgid == new->tgid &&
  22. old->path.mnt == new->path.mnt &&
  23. old->path.dentry == new->path.dentry)
  24. return true;
  25. return false;
  26. }
  27. /* and the list better be locked by something too! */
  28. static int fanotify_merge(struct list_head *list, struct fsnotify_event *event)
  29. {
  30. struct fsnotify_event *test_event;
  31. pr_debug("%s: list=%p event=%p\n", __func__, list, event);
  32. /*
  33. * Don't merge a permission event with any other event so that we know
  34. * the event structure we have created in fanotify_handle_event() is the
  35. * one we should check for permission response.
  36. */
  37. if (fanotify_is_perm_event(event->mask))
  38. return 0;
  39. list_for_each_entry_reverse(test_event, list, list) {
  40. if (should_merge(test_event, event)) {
  41. test_event->mask |= event->mask;
  42. return 1;
  43. }
  44. }
  45. return 0;
  46. }
  47. static int fanotify_get_response(struct fsnotify_group *group,
  48. struct fanotify_perm_event_info *event,
  49. struct fsnotify_iter_info *iter_info)
  50. {
  51. int ret;
  52. pr_debug("%s: group=%p event=%p\n", __func__, group, event);
  53. wait_event(group->fanotify_data.access_waitq, event->response);
  54. /* userspace responded, convert to something usable */
  55. switch (event->response) {
  56. case FAN_ALLOW:
  57. ret = 0;
  58. break;
  59. case FAN_DENY:
  60. default:
  61. ret = -EPERM;
  62. }
  63. event->response = 0;
  64. pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
  65. group, event, ret);
  66. return ret;
  67. }
  68. static bool fanotify_should_send_event(struct fsnotify_mark *inode_mark,
  69. struct fsnotify_mark *vfsmnt_mark,
  70. u32 event_mask,
  71. const void *data, int data_type)
  72. {
  73. __u32 marks_mask, marks_ignored_mask;
  74. const struct path *path = data;
  75. pr_debug("%s: inode_mark=%p vfsmnt_mark=%p mask=%x data=%p"
  76. " data_type=%d\n", __func__, inode_mark, vfsmnt_mark,
  77. event_mask, data, data_type);
  78. /* if we don't have enough info to send an event to userspace say no */
  79. if (data_type != FSNOTIFY_EVENT_PATH)
  80. return false;
  81. /* sorry, fanotify only gives a damn about files and dirs */
  82. if (!d_is_reg(path->dentry) &&
  83. !d_can_lookup(path->dentry))
  84. return false;
  85. if (inode_mark && vfsmnt_mark) {
  86. marks_mask = (vfsmnt_mark->mask | inode_mark->mask);
  87. marks_ignored_mask = (vfsmnt_mark->ignored_mask | inode_mark->ignored_mask);
  88. } else if (inode_mark) {
  89. /*
  90. * if the event is for a child and this inode doesn't care about
  91. * events on the child, don't send it!
  92. */
  93. if ((event_mask & FS_EVENT_ON_CHILD) &&
  94. !(inode_mark->mask & FS_EVENT_ON_CHILD))
  95. return false;
  96. marks_mask = inode_mark->mask;
  97. marks_ignored_mask = inode_mark->ignored_mask;
  98. } else if (vfsmnt_mark) {
  99. marks_mask = vfsmnt_mark->mask;
  100. marks_ignored_mask = vfsmnt_mark->ignored_mask;
  101. } else {
  102. BUG();
  103. }
  104. if (d_is_dir(path->dentry) &&
  105. !(marks_mask & FS_ISDIR & ~marks_ignored_mask))
  106. return false;
  107. if (event_mask & FAN_ALL_OUTGOING_EVENTS & marks_mask &
  108. ~marks_ignored_mask)
  109. return true;
  110. return false;
  111. }
  112. struct fanotify_event_info *fanotify_alloc_event(struct inode *inode, u32 mask,
  113. const struct path *path)
  114. {
  115. struct fanotify_event_info *event;
  116. if (fanotify_is_perm_event(mask)) {
  117. struct fanotify_perm_event_info *pevent;
  118. pevent = kmem_cache_alloc(fanotify_perm_event_cachep,
  119. GFP_KERNEL);
  120. if (!pevent)
  121. return NULL;
  122. event = &pevent->fae;
  123. pevent->response = 0;
  124. goto init;
  125. }
  126. event = kmem_cache_alloc(fanotify_event_cachep, GFP_KERNEL);
  127. if (!event)
  128. return NULL;
  129. init: __maybe_unused
  130. fsnotify_init_event(&event->fse, inode, mask);
  131. event->tgid = get_pid(task_tgid(current));
  132. if (path) {
  133. event->path = *path;
  134. path_get(&event->path);
  135. } else {
  136. event->path.mnt = NULL;
  137. event->path.dentry = NULL;
  138. }
  139. return event;
  140. }
  141. static int fanotify_handle_event(struct fsnotify_group *group,
  142. struct inode *inode,
  143. struct fsnotify_mark *inode_mark,
  144. struct fsnotify_mark *fanotify_mark,
  145. u32 mask, const void *data, int data_type,
  146. const unsigned char *file_name, u32 cookie,
  147. struct fsnotify_iter_info *iter_info)
  148. {
  149. int ret = 0;
  150. struct fanotify_event_info *event;
  151. struct fsnotify_event *fsn_event;
  152. BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
  153. BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
  154. BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
  155. BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
  156. BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
  157. BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
  158. BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
  159. BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
  160. BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
  161. BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR);
  162. if (!fanotify_should_send_event(inode_mark, fanotify_mark, mask, data,
  163. data_type))
  164. return 0;
  165. pr_debug("%s: group=%p inode=%p mask=%x\n", __func__, group, inode,
  166. mask);
  167. if (fanotify_is_perm_event(mask)) {
  168. /*
  169. * fsnotify_prepare_user_wait() fails if we race with mark
  170. * deletion. Just let the operation pass in that case.
  171. */
  172. if (!fsnotify_prepare_user_wait(iter_info))
  173. return 0;
  174. }
  175. event = fanotify_alloc_event(inode, mask, data);
  176. ret = -ENOMEM;
  177. if (unlikely(!event))
  178. goto finish;
  179. fsn_event = &event->fse;
  180. ret = fsnotify_add_event(group, fsn_event, fanotify_merge);
  181. if (ret) {
  182. /* Permission events shouldn't be merged */
  183. BUG_ON(ret == 1 && mask & FAN_ALL_PERM_EVENTS);
  184. /* Our event wasn't used in the end. Free it. */
  185. fsnotify_destroy_event(group, fsn_event);
  186. ret = 0;
  187. } else if (fanotify_is_perm_event(mask)) {
  188. ret = fanotify_get_response(group, FANOTIFY_PE(fsn_event),
  189. iter_info);
  190. fsnotify_destroy_event(group, fsn_event);
  191. }
  192. finish:
  193. if (fanotify_is_perm_event(mask))
  194. fsnotify_finish_user_wait(iter_info);
  195. return ret;
  196. }
  197. static void fanotify_free_group_priv(struct fsnotify_group *group)
  198. {
  199. struct user_struct *user;
  200. user = group->fanotify_data.user;
  201. atomic_dec(&user->fanotify_listeners);
  202. free_uid(user);
  203. }
  204. static void fanotify_free_event(struct fsnotify_event *fsn_event)
  205. {
  206. struct fanotify_event_info *event;
  207. event = FANOTIFY_E(fsn_event);
  208. path_put(&event->path);
  209. put_pid(event->tgid);
  210. if (fanotify_is_perm_event(fsn_event->mask)) {
  211. kmem_cache_free(fanotify_perm_event_cachep,
  212. FANOTIFY_PE(fsn_event));
  213. return;
  214. }
  215. kmem_cache_free(fanotify_event_cachep, event);
  216. }
  217. static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
  218. {
  219. kmem_cache_free(fanotify_mark_cache, fsn_mark);
  220. }
  221. const struct fsnotify_ops fanotify_fsnotify_ops = {
  222. .handle_event = fanotify_handle_event,
  223. .free_group_priv = fanotify_free_group_priv,
  224. .free_event = fanotify_free_event,
  225. .free_mark = fanotify_free_mark,
  226. };