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>
14 static bool should_merge(struct fsnotify_event *old_fsn,
15 struct fsnotify_event *new_fsn)
17 struct fanotify_event_info *old, *new;
19 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
20 /* dont merge two permission events */
21 if ((old_fsn->mask & FAN_ALL_PERM_EVENTS) &&
22 (new_fsn->mask & FAN_ALL_PERM_EVENTS))
25 pr_debug("%s: old=%p new=%p\n", __func__, old_fsn, new_fsn);
26 old = FANOTIFY_E(old_fsn);
27 new = FANOTIFY_E(new_fsn);
29 if (old_fsn->inode == new_fsn->inode && old->tgid == new->tgid &&
30 old->path.mnt == new->path.mnt &&
31 old->path.dentry == new->path.dentry)
36 /* and the list better be locked by something too! */
37 static struct fsnotify_event *fanotify_merge(struct list_head *list,
38 struct fsnotify_event *event)
40 struct fsnotify_event *test_event;
41 bool do_merge = false;
43 pr_debug("%s: list=%p event=%p\n", __func__, list, event);
45 list_for_each_entry_reverse(test_event, list, list) {
46 if (should_merge(test_event, event)) {
55 test_event->mask |= event->mask;
59 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
60 static int fanotify_get_response_from_access(struct fsnotify_group *group,
61 struct fanotify_event_info *event)
65 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
67 wait_event(group->fanotify_data.access_waitq, event->response ||
68 atomic_read(&group->fanotify_data.bypass_perm));
70 if (!event->response) /* bypass_perm set */
73 /* userspace responded, convert to something usable */
74 switch (event->response) {
84 pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
91 static bool fanotify_should_send_event(struct fsnotify_group *group,
93 struct fsnotify_mark *inode_mark,
94 struct fsnotify_mark *vfsmnt_mark,
95 __u32 event_mask, void *data, int data_type)
97 __u32 marks_mask, marks_ignored_mask;
98 struct path *path = data;
100 pr_debug("%s: group=%p inode=%p inode_mark=%p vfsmnt_mark=%p "
101 "mask=%x data=%p data_type=%d\n", __func__, group, inode,
102 inode_mark, vfsmnt_mark, event_mask, data, data_type);
104 /* if we don't have enough info to send an event to userspace say no */
105 if (data_type != FSNOTIFY_EVENT_PATH)
108 /* sorry, fanotify only gives a damn about files and dirs */
109 if (!S_ISREG(path->dentry->d_inode->i_mode) &&
110 !S_ISDIR(path->dentry->d_inode->i_mode))
113 if (inode_mark && vfsmnt_mark) {
114 marks_mask = (vfsmnt_mark->mask | inode_mark->mask);
115 marks_ignored_mask = (vfsmnt_mark->ignored_mask | inode_mark->ignored_mask);
116 } else if (inode_mark) {
118 * if the event is for a child and this inode doesn't care about
119 * events on the child, don't send it!
121 if ((event_mask & FS_EVENT_ON_CHILD) &&
122 !(inode_mark->mask & FS_EVENT_ON_CHILD))
124 marks_mask = inode_mark->mask;
125 marks_ignored_mask = inode_mark->ignored_mask;
126 } else if (vfsmnt_mark) {
127 marks_mask = vfsmnt_mark->mask;
128 marks_ignored_mask = vfsmnt_mark->ignored_mask;
133 if (S_ISDIR(path->dentry->d_inode->i_mode) &&
134 (marks_ignored_mask & FS_ISDIR))
137 if (event_mask & marks_mask & ~marks_ignored_mask)
143 static int fanotify_handle_event(struct fsnotify_group *group,
145 struct fsnotify_mark *inode_mark,
146 struct fsnotify_mark *fanotify_mark,
147 u32 mask, void *data, int data_type,
148 const unsigned char *file_name)
151 struct fanotify_event_info *event;
152 struct fsnotify_event *fsn_event;
153 struct fsnotify_event *notify_fsn_event;
155 BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
156 BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
157 BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
158 BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
159 BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
160 BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
161 BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
162 BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
163 BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
164 BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR);
166 pr_debug("%s: group=%p inode=%p mask=%x\n", __func__, group, inode,
169 event = kmem_cache_alloc(fanotify_event_cachep, GFP_KERNEL);
170 if (unlikely(!event))
173 fsn_event = &event->fse;
174 fsnotify_init_event(fsn_event, inode, mask);
175 event->tgid = get_pid(task_tgid(current));
176 if (data_type == FSNOTIFY_EVENT_PATH) {
177 struct path *path = data;
179 path_get(&event->path);
181 event->path.mnt = NULL;
182 event->path.dentry = NULL;
184 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
188 notify_fsn_event = fsnotify_add_notify_event(group, fsn_event,
190 if (notify_fsn_event) {
191 /* Our event wasn't used in the end. Free it. */
192 fsnotify_destroy_event(group, fsn_event);
193 if (IS_ERR(notify_fsn_event))
194 return PTR_ERR(notify_fsn_event);
195 /* We need to ask about a different events after a merge... */
196 event = FANOTIFY_E(notify_fsn_event);
197 fsn_event = notify_fsn_event;
200 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
201 if (fsn_event->mask & FAN_ALL_PERM_EVENTS)
202 ret = fanotify_get_response_from_access(group, event);
207 static void fanotify_free_group_priv(struct fsnotify_group *group)
209 struct user_struct *user;
211 user = group->fanotify_data.user;
212 atomic_dec(&user->fanotify_listeners);
216 static void fanotify_free_event(struct fsnotify_event *fsn_event)
218 struct fanotify_event_info *event;
220 event = FANOTIFY_E(fsn_event);
221 path_put(&event->path);
222 put_pid(event->tgid);
223 kmem_cache_free(fanotify_event_cachep, event);
226 const struct fsnotify_ops fanotify_fsnotify_ops = {
227 .handle_event = fanotify_handle_event,
228 .should_send_event = fanotify_should_send_event,
229 .free_group_priv = fanotify_free_group_priv,
230 .free_event = fanotify_free_event,
231 .freeing_mark = NULL,