fanotify.c 6.1 KB

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