1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/fanotify.h>
3 #include <linux/fdtable.h>
4 #include <linux/fsnotify_backend.h>
5 #include <linux/init.h>
6 #include <linux/jiffies.h>
7 #include <linux/kernel.h> /* UINT_MAX */
8 #include <linux/mount.h>
9 #include <linux/sched.h>
10 #include <linux/sched/user.h>
11 #include <linux/types.h>
12 #include <linux/wait.h>
16 static bool should_merge(struct fsnotify_event *old_fsn,
17 struct fsnotify_event *new_fsn)
19 struct fanotify_event_info *old, *new;
21 pr_debug("%s: old=%p new=%p\n", __func__, old_fsn, new_fsn);
22 old = FANOTIFY_E(old_fsn);
23 new = FANOTIFY_E(new_fsn);
25 if (old_fsn->inode == new_fsn->inode && old->tgid == new->tgid &&
26 old->path.mnt == new->path.mnt &&
27 old->path.dentry == new->path.dentry)
32 /* and the list better be locked by something too! */
33 static int fanotify_merge(struct list_head *list, struct fsnotify_event *event)
35 struct fsnotify_event *test_event;
37 pr_debug("%s: list=%p event=%p\n", __func__, list, event);
39 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
41 * Don't merge a permission event with any other event so that we know
42 * the event structure we have created in fanotify_handle_event() is the
43 * one we should check for permission response.
45 if (event->mask & FAN_ALL_PERM_EVENTS)
49 list_for_each_entry_reverse(test_event, list, list) {
50 if (should_merge(test_event, event)) {
51 test_event->mask |= event->mask;
59 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
60 static int fanotify_get_response(struct fsnotify_group *group,
61 struct fanotify_perm_event_info *event,
62 struct fsnotify_iter_info *iter_info)
66 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
69 * fsnotify_prepare_user_wait() fails if we race with mark deletion.
70 * Just let the operation pass in that case.
72 if (!fsnotify_prepare_user_wait(iter_info)) {
73 event->response = FAN_ALLOW;
77 wait_event(group->fanotify_data.access_waitq, event->response);
79 fsnotify_finish_user_wait(iter_info);
81 /* userspace responded, convert to something usable */
82 switch (event->response) {
92 pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
99 static bool fanotify_should_send_event(struct fsnotify_mark *inode_mark,
100 struct fsnotify_mark *vfsmnt_mark,
102 const void *data, int data_type)
104 __u32 marks_mask, marks_ignored_mask;
105 const struct path *path = data;
107 pr_debug("%s: inode_mark=%p vfsmnt_mark=%p mask=%x data=%p"
108 " data_type=%d\n", __func__, inode_mark, vfsmnt_mark,
109 event_mask, data, data_type);
111 /* if we don't have enough info to send an event to userspace say no */
112 if (data_type != FSNOTIFY_EVENT_PATH)
115 /* sorry, fanotify only gives a damn about files and dirs */
116 if (!d_is_reg(path->dentry) &&
117 !d_can_lookup(path->dentry))
120 if (inode_mark && vfsmnt_mark) {
121 marks_mask = (vfsmnt_mark->mask | inode_mark->mask);
122 marks_ignored_mask = (vfsmnt_mark->ignored_mask | inode_mark->ignored_mask);
123 } else if (inode_mark) {
125 * if the event is for a child and this inode doesn't care about
126 * events on the child, don't send it!
128 if ((event_mask & FS_EVENT_ON_CHILD) &&
129 !(inode_mark->mask & FS_EVENT_ON_CHILD))
131 marks_mask = inode_mark->mask;
132 marks_ignored_mask = inode_mark->ignored_mask;
133 } else if (vfsmnt_mark) {
134 marks_mask = vfsmnt_mark->mask;
135 marks_ignored_mask = vfsmnt_mark->ignored_mask;
140 if (d_is_dir(path->dentry) &&
141 !(marks_mask & FS_ISDIR & ~marks_ignored_mask))
144 if (event_mask & FAN_ALL_OUTGOING_EVENTS & marks_mask &
151 struct fanotify_event_info *fanotify_alloc_event(struct inode *inode, u32 mask,
152 const struct path *path)
154 struct fanotify_event_info *event;
156 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
157 if (mask & FAN_ALL_PERM_EVENTS) {
158 struct fanotify_perm_event_info *pevent;
160 pevent = kmem_cache_alloc(fanotify_perm_event_cachep,
164 event = &pevent->fae;
165 pevent->response = 0;
169 event = kmem_cache_alloc(fanotify_event_cachep, GFP_KERNEL);
173 fsnotify_init_event(&event->fse, inode, mask);
174 event->tgid = get_pid(task_tgid(current));
177 path_get(&event->path);
179 event->path.mnt = NULL;
180 event->path.dentry = NULL;
185 static int fanotify_handle_event(struct fsnotify_group *group,
187 struct fsnotify_mark *inode_mark,
188 struct fsnotify_mark *fanotify_mark,
189 u32 mask, const void *data, int data_type,
190 const unsigned char *file_name, u32 cookie,
191 struct fsnotify_iter_info *iter_info)
194 struct fanotify_event_info *event;
195 struct fsnotify_event *fsn_event;
197 BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
198 BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
199 BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
200 BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
201 BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
202 BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
203 BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
204 BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
205 BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
206 BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR);
208 if (!fanotify_should_send_event(inode_mark, fanotify_mark, mask, data,
212 pr_debug("%s: group=%p inode=%p mask=%x\n", __func__, group, inode,
215 event = fanotify_alloc_event(inode, mask, data);
216 if (unlikely(!event))
219 fsn_event = &event->fse;
220 ret = fsnotify_add_event(group, fsn_event, fanotify_merge);
222 /* Permission events shouldn't be merged */
223 BUG_ON(ret == 1 && mask & FAN_ALL_PERM_EVENTS);
224 /* Our event wasn't used in the end. Free it. */
225 fsnotify_destroy_event(group, fsn_event);
230 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
231 if (mask & FAN_ALL_PERM_EVENTS) {
232 ret = fanotify_get_response(group, FANOTIFY_PE(fsn_event),
234 fsnotify_destroy_event(group, fsn_event);
240 static void fanotify_free_group_priv(struct fsnotify_group *group)
242 struct user_struct *user;
244 user = group->fanotify_data.user;
245 atomic_dec(&user->fanotify_listeners);
249 static void fanotify_free_event(struct fsnotify_event *fsn_event)
251 struct fanotify_event_info *event;
253 event = FANOTIFY_E(fsn_event);
254 path_put(&event->path);
255 put_pid(event->tgid);
256 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
257 if (fsn_event->mask & FAN_ALL_PERM_EVENTS) {
258 kmem_cache_free(fanotify_perm_event_cachep,
259 FANOTIFY_PE(fsn_event));
263 kmem_cache_free(fanotify_event_cachep, event);
266 static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
268 kmem_cache_free(fanotify_mark_cache, fsn_mark);
271 const struct fsnotify_ops fanotify_fsnotify_ops = {
272 .handle_event = fanotify_handle_event,
273 .free_group_priv = fanotify_free_group_priv,
274 .free_event = fanotify_free_event,
275 .free_mark = fanotify_free_mark,