fanotify: Fix use after free in mask checking
[deliverable/linux.git] / 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 pr_debug("%s: old=%p new=%p\n", __func__, old_fsn, new_fsn);
20 old = FANOTIFY_E(old_fsn);
21 new = FANOTIFY_E(new_fsn);
22
23 if (old_fsn->inode == new_fsn->inode && old->tgid == new->tgid &&
24 old->path.mnt == new->path.mnt &&
25 old->path.dentry == new->path.dentry)
26 return true;
27 return false;
28 }
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 *test_event;
35 bool do_merge = false;
36
37 pr_debug("%s: list=%p event=%p\n", __func__, list, event);
38
39 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
40 /*
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.
44 */
45 if (event->mask & FAN_ALL_PERM_EVENTS)
46 return NULL;
47 #endif
48
49 list_for_each_entry_reverse(test_event, list, list) {
50 if (should_merge(test_event, event)) {
51 do_merge = true;
52 break;
53 }
54 }
55
56 if (!do_merge)
57 return NULL;
58
59 test_event->mask |= event->mask;
60 return test_event;
61 }
62
63 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
64 static int fanotify_get_response_from_access(struct fsnotify_group *group,
65 struct fanotify_event_info *event)
66 {
67 int ret;
68
69 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
70
71 wait_event(group->fanotify_data.access_waitq, event->response ||
72 atomic_read(&group->fanotify_data.bypass_perm));
73
74 if (!event->response) /* bypass_perm set */
75 return 0;
76
77 /* userspace responded, convert to something usable */
78 switch (event->response) {
79 case FAN_ALLOW:
80 ret = 0;
81 break;
82 case FAN_DENY:
83 default:
84 ret = -EPERM;
85 }
86 event->response = 0;
87
88 pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
89 group, event, ret);
90
91 return ret;
92 }
93 #endif
94
95 static bool fanotify_should_send_event(struct fsnotify_mark *inode_mark,
96 struct fsnotify_mark *vfsmnt_mark,
97 u32 event_mask,
98 void *data, int data_type)
99 {
100 __u32 marks_mask, marks_ignored_mask;
101 struct path *path = data;
102
103 pr_debug("%s: inode_mark=%p vfsmnt_mark=%p mask=%x data=%p"
104 " data_type=%d\n", __func__, inode_mark, vfsmnt_mark,
105 event_mask, data, data_type);
106
107 /* if we don't have enough info to send an event to userspace say no */
108 if (data_type != FSNOTIFY_EVENT_PATH)
109 return false;
110
111 /* sorry, fanotify only gives a damn about files and dirs */
112 if (!S_ISREG(path->dentry->d_inode->i_mode) &&
113 !S_ISDIR(path->dentry->d_inode->i_mode))
114 return false;
115
116 if (inode_mark && vfsmnt_mark) {
117 marks_mask = (vfsmnt_mark->mask | inode_mark->mask);
118 marks_ignored_mask = (vfsmnt_mark->ignored_mask | inode_mark->ignored_mask);
119 } else if (inode_mark) {
120 /*
121 * if the event is for a child and this inode doesn't care about
122 * events on the child, don't send it!
123 */
124 if ((event_mask & FS_EVENT_ON_CHILD) &&
125 !(inode_mark->mask & FS_EVENT_ON_CHILD))
126 return false;
127 marks_mask = inode_mark->mask;
128 marks_ignored_mask = inode_mark->ignored_mask;
129 } else if (vfsmnt_mark) {
130 marks_mask = vfsmnt_mark->mask;
131 marks_ignored_mask = vfsmnt_mark->ignored_mask;
132 } else {
133 BUG();
134 }
135
136 if (S_ISDIR(path->dentry->d_inode->i_mode) &&
137 (marks_ignored_mask & FS_ISDIR))
138 return false;
139
140 if (event_mask & marks_mask & ~marks_ignored_mask)
141 return true;
142
143 return false;
144 }
145
146 static int fanotify_handle_event(struct fsnotify_group *group,
147 struct inode *inode,
148 struct fsnotify_mark *inode_mark,
149 struct fsnotify_mark *fanotify_mark,
150 u32 mask, void *data, int data_type,
151 const unsigned char *file_name)
152 {
153 int ret = 0;
154 struct fanotify_event_info *event;
155 struct fsnotify_event *fsn_event;
156 struct fsnotify_event *notify_fsn_event;
157
158 BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
159 BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
160 BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
161 BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
162 BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
163 BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
164 BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
165 BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
166 BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
167 BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR);
168
169 if (!fanotify_should_send_event(inode_mark, fanotify_mark, mask, data,
170 data_type))
171 return 0;
172
173 pr_debug("%s: group=%p inode=%p mask=%x\n", __func__, group, inode,
174 mask);
175
176 event = kmem_cache_alloc(fanotify_event_cachep, GFP_KERNEL);
177 if (unlikely(!event))
178 return -ENOMEM;
179
180 fsn_event = &event->fse;
181 fsnotify_init_event(fsn_event, inode, mask);
182 event->tgid = get_pid(task_tgid(current));
183 if (data_type == FSNOTIFY_EVENT_PATH) {
184 struct path *path = data;
185 event->path = *path;
186 path_get(&event->path);
187 } else {
188 event->path.mnt = NULL;
189 event->path.dentry = NULL;
190 }
191 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
192 event->response = 0;
193 #endif
194
195 notify_fsn_event = fsnotify_add_notify_event(group, fsn_event,
196 fanotify_merge);
197 if (notify_fsn_event) {
198 /* Our event wasn't used in the end. Free it. */
199 fsnotify_destroy_event(group, fsn_event);
200 if (IS_ERR(notify_fsn_event))
201 return PTR_ERR(notify_fsn_event);
202 }
203
204 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
205 if (mask & FAN_ALL_PERM_EVENTS)
206 ret = fanotify_get_response_from_access(group, event);
207 #endif
208 return ret;
209 }
210
211 static void fanotify_free_group_priv(struct fsnotify_group *group)
212 {
213 struct user_struct *user;
214
215 user = group->fanotify_data.user;
216 atomic_dec(&user->fanotify_listeners);
217 free_uid(user);
218 }
219
220 static void fanotify_free_event(struct fsnotify_event *fsn_event)
221 {
222 struct fanotify_event_info *event;
223
224 event = FANOTIFY_E(fsn_event);
225 path_put(&event->path);
226 put_pid(event->tgid);
227 kmem_cache_free(fanotify_event_cachep, event);
228 }
229
230 const struct fsnotify_ops fanotify_fsnotify_ops = {
231 .handle_event = fanotify_handle_event,
232 .free_group_priv = fanotify_free_group_priv,
233 .free_event = fanotify_free_event,
234 };
This page took 0.04272 seconds and 5 git commands to generate.