fanotify.c 6.1 KB

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