fanotify.c 6.1 KB

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