fanotify: Fix use after free in mask checking
[deliverable/linux.git] / fs / notify / fanotify / fanotify.c
CommitLineData
33d3dfff 1#include <linux/fanotify.h>
ff0b16a9
EP
2#include <linux/fdtable.h>
3#include <linux/fsnotify_backend.h>
4#include <linux/init.h>
9e66e423 5#include <linux/jiffies.h>
ff0b16a9 6#include <linux/kernel.h> /* UINT_MAX */
1c529063 7#include <linux/mount.h>
9e66e423 8#include <linux/sched.h>
ff0b16a9 9#include <linux/types.h>
9e66e423 10#include <linux/wait.h>
ff0b16a9 11
7053aee2
JK
12#include "fanotify.h"
13
14static bool should_merge(struct fsnotify_event *old_fsn,
15 struct fsnotify_event *new_fsn)
767cd46c 16{
7053aee2 17 struct fanotify_event_info *old, *new;
767cd46c 18
7053aee2
JK
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;
767cd46c
EP
27 return false;
28}
29
f70ab54c
EP
30/* and the list better be locked by something too! */
31static struct fsnotify_event *fanotify_merge(struct list_head *list,
32 struct fsnotify_event *event)
767cd46c 33{
7053aee2
JK
34 struct fsnotify_event *test_event;
35 bool do_merge = false;
767cd46c
EP
36
37 pr_debug("%s: list=%p event=%p\n", __func__, list, event);
38
13116dfd
JK
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
7053aee2
JK
49 list_for_each_entry_reverse(test_event, list, list) {
50 if (should_merge(test_event, event)) {
51 do_merge = true;
a12a7dd3
EP
52 break;
53 }
54 }
f70ab54c 55
7053aee2 56 if (!do_merge)
f70ab54c
EP
57 return NULL;
58
7053aee2
JK
59 test_event->mask |= event->mask;
60 return test_event;
767cd46c
EP
61}
62
9e66e423
EP
63#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
64static int fanotify_get_response_from_access(struct fsnotify_group *group,
7053aee2 65 struct fanotify_event_info *event)
9e66e423
EP
66{
67 int ret;
68
69 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
70
09e5f14e
LS
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;
9e66e423
EP
76
77 /* userspace responded, convert to something usable */
9e66e423
EP
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;
9e66e423 87
b2d87909
EP
88 pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
89 group, event, ret);
90
9e66e423
EP
91 return ret;
92}
93#endif
94
83c4c4b0 95static bool fanotify_should_send_event(struct fsnotify_mark *inode_mark,
1968f5ee 96 struct fsnotify_mark *vfsmnt_mark,
83c4c4b0
JK
97 u32 event_mask,
98 void *data, int data_type)
1c529063 99{
1968f5ee 100 __u32 marks_mask, marks_ignored_mask;
e1c048ba 101 struct path *path = data;
1968f5ee 102
83c4c4b0
JK
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);
1968f5ee 106
1c529063 107 /* if we don't have enough info to send an event to userspace say no */
2069601b 108 if (data_type != FSNOTIFY_EVENT_PATH)
1c529063
EP
109 return false;
110
e1c048ba
EP
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
1968f5ee
EP
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
8fcd6528
EP
136 if (S_ISDIR(path->dentry->d_inode->i_mode) &&
137 (marks_ignored_mask & FS_ISDIR))
138 return false;
139
1968f5ee
EP
140 if (event_mask & marks_mask & ~marks_ignored_mask)
141 return true;
142
143 return false;
1c529063
EP
144}
145
7053aee2
JK
146static 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
83c4c4b0
JK
169 if (!fanotify_should_send_event(inode_mark, fanotify_mark, mask, data,
170 data_type))
171 return 0;
172
7053aee2
JK
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);
7053aee2
JK
202 }
203
204#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
13116dfd 205 if (mask & FAN_ALL_PERM_EVENTS)
7053aee2
JK
206 ret = fanotify_get_response_from_access(group, event);
207#endif
208 return ret;
209}
210
4afeff85
EP
211static 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
7053aee2
JK
220static 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
ff0b16a9
EP
230const struct fsnotify_ops fanotify_fsnotify_ops = {
231 .handle_event = fanotify_handle_event,
4afeff85 232 .free_group_priv = fanotify_free_group_priv,
7053aee2 233 .free_event = fanotify_free_event,
ff0b16a9 234};
This page took 0.23811 seconds and 5 git commands to generate.