fanotify.c 6.3 KB

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