]> Pileus Git - ~andy/linux/blob - fs/notify/fanotify/fanotify.c
fsnotify: do not share events between notification groups
[~andy/linux] / fs / notify / fanotify / fanotify.c
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
12 #include "fanotify.h"
13
14 static bool should_merge(struct fsnotify_event *old_fsn,
15                          struct fsnotify_event *new_fsn)
16 {
17         struct fanotify_event_info *old, *new;
18
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))
23                 return false;
24 #endif
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);
28
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)
32                 return true;
33         return false;
34 }
35
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)
39 {
40         struct fsnotify_event *test_event;
41         bool do_merge = false;
42
43         pr_debug("%s: list=%p event=%p\n", __func__, list, event);
44
45         list_for_each_entry_reverse(test_event, list, list) {
46                 if (should_merge(test_event, event)) {
47                         do_merge = true;
48                         break;
49                 }
50         }
51
52         if (!do_merge)
53                 return NULL;
54
55         test_event->mask |= event->mask;
56         return test_event;
57 }
58
59 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
60 static int fanotify_get_response_from_access(struct fsnotify_group *group,
61                                              struct fanotify_event_info *event)
62 {
63         int ret;
64
65         pr_debug("%s: group=%p event=%p\n", __func__, group, event);
66
67         wait_event(group->fanotify_data.access_waitq, event->response ||
68                                 atomic_read(&group->fanotify_data.bypass_perm));
69
70         if (!event->response) /* bypass_perm set */
71                 return 0;
72
73         /* userspace responded, convert to something usable */
74         switch (event->response) {
75         case FAN_ALLOW:
76                 ret = 0;
77                 break;
78         case FAN_DENY:
79         default:
80                 ret = -EPERM;
81         }
82         event->response = 0;
83
84         pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
85                  group, event, ret);
86         
87         return ret;
88 }
89 #endif
90
91 static bool fanotify_should_send_event(struct fsnotify_group *group,
92                                        struct inode *inode,
93                                        struct fsnotify_mark *inode_mark,
94                                        struct fsnotify_mark *vfsmnt_mark,
95                                        __u32 event_mask, void *data, int data_type)
96 {
97         __u32 marks_mask, marks_ignored_mask;
98         struct path *path = data;
99
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);
103
104         /* if we don't have enough info to send an event to userspace say no */
105         if (data_type != FSNOTIFY_EVENT_PATH)
106                 return false;
107
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))
111                 return false;
112
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) {
117                 /*
118                  * if the event is for a child and this inode doesn't care about
119                  * events on the child, don't send it!
120                  */
121                 if ((event_mask & FS_EVENT_ON_CHILD) &&
122                     !(inode_mark->mask & FS_EVENT_ON_CHILD))
123                         return false;
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;
129         } else {
130                 BUG();
131         }
132
133         if (S_ISDIR(path->dentry->d_inode->i_mode) &&
134             (marks_ignored_mask & FS_ISDIR))
135                 return false;
136
137         if (event_mask & marks_mask & ~marks_ignored_mask)
138                 return true;
139
140         return false;
141 }
142
143 static int fanotify_handle_event(struct fsnotify_group *group,
144                                  struct inode *inode,
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)
149 {
150         int ret = 0;
151         struct fanotify_event_info *event;
152         struct fsnotify_event *fsn_event;
153         struct fsnotify_event *notify_fsn_event;
154
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);
165
166         pr_debug("%s: group=%p inode=%p mask=%x\n", __func__, group, inode,
167                  mask);
168
169         event = kmem_cache_alloc(fanotify_event_cachep, GFP_KERNEL);
170         if (unlikely(!event))
171                 return -ENOMEM;
172
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;
178                 event->path = *path;
179                 path_get(&event->path);
180         } else {
181                 event->path.mnt = NULL;
182                 event->path.dentry = NULL;
183         }
184 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
185         event->response = 0;
186 #endif
187
188         notify_fsn_event = fsnotify_add_notify_event(group, fsn_event,
189                                                      fanotify_merge);
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;
198         }
199
200 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
201         if (fsn_event->mask & FAN_ALL_PERM_EVENTS)
202                 ret = fanotify_get_response_from_access(group, event);
203 #endif
204         return ret;
205 }
206
207 static void fanotify_free_group_priv(struct fsnotify_group *group)
208 {
209         struct user_struct *user;
210
211         user = group->fanotify_data.user;
212         atomic_dec(&user->fanotify_listeners);
213         free_uid(user);
214 }
215
216 static void fanotify_free_event(struct fsnotify_event *fsn_event)
217 {
218         struct fanotify_event_info *event;
219
220         event = FANOTIFY_E(fsn_event);
221         path_put(&event->path);
222         put_pid(event->tgid);
223         kmem_cache_free(fanotify_event_cachep, event);
224 }
225
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,
232 };