fanotify.c 6.8 KB

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