fanotify.c 6.3 KB

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