fanotify.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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_FILE):
  19. if ((old->file->f_path.mnt == new->file->f_path.mnt) &&
  20. (old->file->f_path.dentry == new->file->f_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, struct fsnotify_event *event)
  96. {
  97. int ret = 0;
  98. struct fsnotify_event *notify_event = NULL;
  99. BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
  100. BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
  101. BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
  102. BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
  103. BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
  104. BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
  105. BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
  106. BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
  107. BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
  108. pr_debug("%s: group=%p event=%p\n", __func__, group, event);
  109. notify_event = fsnotify_add_notify_event(group, event, NULL, fanotify_merge);
  110. if (IS_ERR(notify_event))
  111. return PTR_ERR(notify_event);
  112. #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
  113. if (event->mask & FAN_ALL_PERM_EVENTS) {
  114. /* if we merged we need to wait on the new event */
  115. if (notify_event)
  116. event = notify_event;
  117. ret = fanotify_get_response_from_access(group, event);
  118. }
  119. #endif
  120. if (notify_event)
  121. fsnotify_put_event(notify_event);
  122. return ret;
  123. }
  124. static bool should_send_vfsmount_event(struct fsnotify_group *group, struct vfsmount *mnt,
  125. struct inode *inode, __u32 mask)
  126. {
  127. struct fsnotify_mark *mnt_mark;
  128. struct fsnotify_mark *inode_mark;
  129. pr_debug("%s: group=%p vfsmount=%p mask=%x\n",
  130. __func__, group, mnt, mask);
  131. mnt_mark = fsnotify_find_vfsmount_mark(group, mnt);
  132. if (!mnt_mark)
  133. return false;
  134. mask &= mnt_mark->mask;
  135. mask &= ~mnt_mark->ignored_mask;
  136. if (mask) {
  137. inode_mark = fsnotify_find_inode_mark(group, inode);
  138. if (inode_mark) {
  139. mask &= ~inode_mark->ignored_mask;
  140. fsnotify_put_mark(inode_mark);
  141. }
  142. }
  143. /* find took a reference */
  144. fsnotify_put_mark(mnt_mark);
  145. return mask;
  146. }
  147. static bool should_send_inode_event(struct fsnotify_group *group, struct inode *inode,
  148. __u32 mask)
  149. {
  150. struct fsnotify_mark *fsn_mark;
  151. pr_debug("%s: group=%p inode=%p mask=%x\n",
  152. __func__, group, inode, mask);
  153. fsn_mark = fsnotify_find_inode_mark(group, inode);
  154. if (!fsn_mark)
  155. return false;
  156. /* if the event is for a child and this inode doesn't care about
  157. * events on the child, don't send it! */
  158. if ((mask & FS_EVENT_ON_CHILD) &&
  159. !(fsn_mark->mask & FS_EVENT_ON_CHILD)) {
  160. mask = 0;
  161. } else {
  162. /*
  163. * We care about children, but do we care about this particular
  164. * type of event?
  165. */
  166. mask &= ~FS_EVENT_ON_CHILD;
  167. mask &= fsn_mark->mask;
  168. mask &= ~fsn_mark->ignored_mask;
  169. }
  170. /* find took a reference */
  171. fsnotify_put_mark(fsn_mark);
  172. return mask;
  173. }
  174. static bool fanotify_should_send_event(struct fsnotify_group *group, struct inode *to_tell,
  175. struct vfsmount *mnt, __u32 mask, void *data,
  176. int data_type)
  177. {
  178. pr_debug("%s: group=%p to_tell=%p mnt=%p mask=%x data=%p data_type=%d\n",
  179. __func__, group, to_tell, mnt, mask, data, data_type);
  180. /* sorry, fanotify only gives a damn about files and dirs */
  181. if (!S_ISREG(to_tell->i_mode) &&
  182. !S_ISDIR(to_tell->i_mode))
  183. return false;
  184. /* if we don't have enough info to send an event to userspace say no */
  185. if (data_type != FSNOTIFY_EVENT_FILE)
  186. return false;
  187. if (mnt)
  188. return should_send_vfsmount_event(group, mnt, to_tell, mask);
  189. else
  190. return should_send_inode_event(group, to_tell, mask);
  191. }
  192. const struct fsnotify_ops fanotify_fsnotify_ops = {
  193. .handle_event = fanotify_handle_event,
  194. .should_send_event = fanotify_should_send_event,
  195. .free_group_priv = NULL,
  196. .free_event_priv = NULL,
  197. .freeing_mark = NULL,
  198. };