fanotify.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. /* userspace responded, convert to something usable */
  79. spin_lock(&event->lock);
  80. switch (event->response) {
  81. case FAN_ALLOW:
  82. ret = 0;
  83. break;
  84. case FAN_DENY:
  85. default:
  86. ret = -EPERM;
  87. }
  88. event->response = 0;
  89. spin_unlock(&event->lock);
  90. pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
  91. group, event, ret);
  92. return ret;
  93. }
  94. #endif
  95. static int fanotify_handle_event(struct fsnotify_group *group,
  96. struct fsnotify_mark *inode_mark,
  97. struct fsnotify_mark *fanotify_mark,
  98. struct fsnotify_event *event)
  99. {
  100. int ret = 0;
  101. struct fsnotify_event *notify_event = NULL;
  102. BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
  103. BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
  104. BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
  105. BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
  106. BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
  107. BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
  108. BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
  109. BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
  110. BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
  111. pr_debug("%s: group=%p event=%p\n", __func__, group, event);
  112. notify_event = fsnotify_add_notify_event(group, event, NULL, fanotify_merge);
  113. if (IS_ERR(notify_event))
  114. return PTR_ERR(notify_event);
  115. #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
  116. if (event->mask & FAN_ALL_PERM_EVENTS) {
  117. /* if we merged we need to wait on the new event */
  118. if (notify_event)
  119. event = notify_event;
  120. ret = fanotify_get_response_from_access(group, event);
  121. }
  122. #endif
  123. if (notify_event)
  124. fsnotify_put_event(notify_event);
  125. return ret;
  126. }
  127. static bool fanotify_should_send_event(struct fsnotify_group *group,
  128. struct inode *to_tell,
  129. struct fsnotify_mark *inode_mark,
  130. struct fsnotify_mark *vfsmnt_mark,
  131. __u32 event_mask, void *data, int data_type)
  132. {
  133. __u32 marks_mask, marks_ignored_mask;
  134. pr_debug("%s: group=%p to_tell=%p inode_mark=%p vfsmnt_mark=%p "
  135. "mask=%x data=%p data_type=%d\n", __func__, group, to_tell,
  136. inode_mark, vfsmnt_mark, event_mask, data, data_type);
  137. /* sorry, fanotify only gives a damn about files and dirs */
  138. if (!S_ISREG(to_tell->i_mode) &&
  139. !S_ISDIR(to_tell->i_mode))
  140. return false;
  141. /* if we don't have enough info to send an event to userspace say no */
  142. if (data_type != FSNOTIFY_EVENT_PATH)
  143. return false;
  144. if (inode_mark && vfsmnt_mark) {
  145. marks_mask = (vfsmnt_mark->mask | inode_mark->mask);
  146. marks_ignored_mask = (vfsmnt_mark->ignored_mask | inode_mark->ignored_mask);
  147. } else if (inode_mark) {
  148. /*
  149. * if the event is for a child and this inode doesn't care about
  150. * events on the child, don't send it!
  151. */
  152. if ((event_mask & FS_EVENT_ON_CHILD) &&
  153. !(inode_mark->mask & FS_EVENT_ON_CHILD))
  154. return false;
  155. marks_mask = inode_mark->mask;
  156. marks_ignored_mask = inode_mark->ignored_mask;
  157. } else if (vfsmnt_mark) {
  158. marks_mask = vfsmnt_mark->mask;
  159. marks_ignored_mask = vfsmnt_mark->ignored_mask;
  160. } else {
  161. BUG();
  162. }
  163. if (event_mask & marks_mask & ~marks_ignored_mask)
  164. return true;
  165. return false;
  166. }
  167. const struct fsnotify_ops fanotify_fsnotify_ops = {
  168. .handle_event = fanotify_handle_event,
  169. .should_send_event = fanotify_should_send_event,
  170. .free_group_priv = NULL,
  171. .free_event_priv = NULL,
  172. .freeing_mark = NULL,
  173. };