blob: 09640b5463638672409ef692b066e7c349e203bc [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Andreas Gruenbacher33d3dff2009-12-17 21:24:29 -05002#include <linux/fanotify.h>
Eric Parisff0b16a2009-12-17 21:24:25 -05003#include <linux/fdtable.h>
4#include <linux/fsnotify_backend.h>
5#include <linux/init.h>
Eric Paris9e66e422009-12-17 21:24:34 -05006#include <linux/jiffies.h>
Eric Parisff0b16a2009-12-17 21:24:25 -05007#include <linux/kernel.h> /* UINT_MAX */
Eric Paris1c529062009-12-17 21:24:28 -05008#include <linux/mount.h>
Eric Paris9e66e422009-12-17 21:24:34 -05009#include <linux/sched.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010010#include <linux/sched/user.h>
Eric Parisff0b16a2009-12-17 21:24:25 -050011#include <linux/types.h>
Eric Paris9e66e422009-12-17 21:24:34 -050012#include <linux/wait.h>
Eric Parisff0b16a2009-12-17 21:24:25 -050013
Jan Kara7053aee2014-01-21 15:48:14 -080014#include "fanotify.h"
Eric Paris767cd462009-12-17 21:24:25 -050015
Jan Kara7053aee2014-01-21 15:48:14 -080016static bool should_merge(struct fsnotify_event *old_fsn,
17 struct fsnotify_event *new_fsn)
18{
19 struct fanotify_event_info *old, *new;
20
Jan Kara7053aee2014-01-21 15:48:14 -080021 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);
24
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)
28 return true;
Eric Paris767cd462009-12-17 21:24:25 -050029 return false;
30}
31
Eric Parisf70ab542010-07-28 10:18:37 -040032/* and the list better be locked by something too! */
Jan Kara83c0e1b2014-01-28 18:53:22 +010033static int fanotify_merge(struct list_head *list, struct fsnotify_event *event)
Eric Paris767cd462009-12-17 21:24:25 -050034{
Jan Kara7053aee2014-01-21 15:48:14 -080035 struct fsnotify_event *test_event;
Eric Paris767cd462009-12-17 21:24:25 -050036
37 pr_debug("%s: list=%p event=%p\n", __func__, list, event);
38
Jan Kara13116df2014-01-28 18:29:24 +010039#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)
Jan Kara83c0e1b2014-01-28 18:53:22 +010046 return 0;
Jan Kara13116df2014-01-28 18:29:24 +010047#endif
48
Jan Kara7053aee2014-01-21 15:48:14 -080049 list_for_each_entry_reverse(test_event, list, list) {
50 if (should_merge(test_event, event)) {
Kinglong Mee6c711002017-02-09 20:45:22 +080051 test_event->mask |= event->mask;
52 return 1;
Eric Parisa12a7dd2009-12-17 21:24:25 -050053 }
54 }
Eric Parisf70ab542010-07-28 10:18:37 -040055
Kinglong Mee6c711002017-02-09 20:45:22 +080056 return 0;
Eric Paris767cd462009-12-17 21:24:25 -050057}
58
Eric Paris9e66e422009-12-17 21:24:34 -050059#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
Jan Karaf0834412014-04-03 14:46:33 -070060static int fanotify_get_response(struct fsnotify_group *group,
Jan Kara05f0e382016-11-10 17:45:16 +010061 struct fanotify_perm_event_info *event,
62 struct fsnotify_iter_info *iter_info)
Eric Paris9e66e422009-12-17 21:24:34 -050063{
64 int ret;
65
66 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
67
Jan Kara05f0e382016-11-10 17:45:16 +010068 /*
69 * fsnotify_prepare_user_wait() fails if we race with mark deletion.
70 * Just let the operation pass in that case.
71 */
72 if (!fsnotify_prepare_user_wait(iter_info)) {
73 event->response = FAN_ALLOW;
74 goto out;
75 }
76
Jan Kara96d41012016-09-19 14:44:30 -070077 wait_event(group->fanotify_data.access_waitq, event->response);
Eric Paris9e66e422009-12-17 21:24:34 -050078
Jan Kara05f0e382016-11-10 17:45:16 +010079 fsnotify_finish_user_wait(iter_info);
80out:
Eric Paris9e66e422009-12-17 21:24:34 -050081 /* userspace responded, convert to something usable */
Eric Paris9e66e422009-12-17 21:24:34 -050082 switch (event->response) {
83 case FAN_ALLOW:
84 ret = 0;
85 break;
86 case FAN_DENY:
87 default:
88 ret = -EPERM;
89 }
90 event->response = 0;
Eric Paris9e66e422009-12-17 21:24:34 -050091
Eric Parisb2d87902009-12-17 21:24:34 -050092 pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
93 group, event, ret);
94
Eric Paris9e66e422009-12-17 21:24:34 -050095 return ret;
96}
97#endif
98
Jan Kara83c4c4b2014-01-21 15:48:15 -080099static bool fanotify_should_send_event(struct fsnotify_mark *inode_mark,
Eric Paris1968f5e2010-07-28 10:18:39 -0400100 struct fsnotify_mark *vfsmnt_mark,
Jan Kara83c4c4b2014-01-21 15:48:15 -0800101 u32 event_mask,
Al Viro3cd5eca2016-11-20 20:19:09 -0500102 const void *data, int data_type)
Eric Paris1c529062009-12-17 21:24:28 -0500103{
Eric Paris1968f5e2010-07-28 10:18:39 -0400104 __u32 marks_mask, marks_ignored_mask;
Al Viro3cd5eca2016-11-20 20:19:09 -0500105 const struct path *path = data;
Eric Paris1968f5e2010-07-28 10:18:39 -0400106
Jan Kara83c4c4b2014-01-21 15:48:15 -0800107 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);
Eric Paris1968f5e2010-07-28 10:18:39 -0400110
Eric Paris1c529062009-12-17 21:24:28 -0500111 /* if we don't have enough info to send an event to userspace say no */
Linus Torvalds20696012010-08-12 14:23:04 -0700112 if (data_type != FSNOTIFY_EVENT_PATH)
Eric Paris1c529062009-12-17 21:24:28 -0500113 return false;
114
Eric Parise1c048b2010-10-28 17:21:58 -0400115 /* sorry, fanotify only gives a damn about files and dirs */
David Howellse36cb0b2015-01-29 12:02:35 +0000116 if (!d_is_reg(path->dentry) &&
David Howells54f2a2f2015-01-29 12:02:36 +0000117 !d_can_lookup(path->dentry))
Eric Parise1c048b2010-10-28 17:21:58 -0400118 return false;
119
Eric Paris1968f5e2010-07-28 10:18:39 -0400120 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) {
124 /*
125 * if the event is for a child and this inode doesn't care about
126 * events on the child, don't send it!
127 */
128 if ((event_mask & FS_EVENT_ON_CHILD) &&
129 !(inode_mark->mask & FS_EVENT_ON_CHILD))
130 return false;
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;
136 } else {
137 BUG();
138 }
139
David Howellse36cb0b2015-01-29 12:02:35 +0000140 if (d_is_dir(path->dentry) &&
Lino Sanfilippo66ba93c2015-02-10 14:08:27 -0800141 !(marks_mask & FS_ISDIR & ~marks_ignored_mask))
Eric Paris8fcd6522010-10-28 17:21:59 -0400142 return false;
143
Suzuki K. Pouloseb3c10302015-03-12 16:26:08 -0700144 if (event_mask & FAN_ALL_OUTGOING_EVENTS & marks_mask &
145 ~marks_ignored_mask)
Eric Paris1968f5e2010-07-28 10:18:39 -0400146 return true;
147
148 return false;
Eric Paris1c529062009-12-17 21:24:28 -0500149}
150
Jan Karaf0834412014-04-03 14:46:33 -0700151struct fanotify_event_info *fanotify_alloc_event(struct inode *inode, u32 mask,
Al Viro3cd5eca2016-11-20 20:19:09 -0500152 const struct path *path)
Jan Karaf0834412014-04-03 14:46:33 -0700153{
154 struct fanotify_event_info *event;
155
156#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
157 if (mask & FAN_ALL_PERM_EVENTS) {
158 struct fanotify_perm_event_info *pevent;
159
160 pevent = kmem_cache_alloc(fanotify_perm_event_cachep,
161 GFP_KERNEL);
162 if (!pevent)
163 return NULL;
164 event = &pevent->fae;
165 pevent->response = 0;
166 goto init;
167 }
168#endif
169 event = kmem_cache_alloc(fanotify_event_cachep, GFP_KERNEL);
170 if (!event)
171 return NULL;
172init: __maybe_unused
173 fsnotify_init_event(&event->fse, inode, mask);
174 event->tgid = get_pid(task_tgid(current));
175 if (path) {
176 event->path = *path;
177 path_get(&event->path);
178 } else {
179 event->path.mnt = NULL;
180 event->path.dentry = NULL;
181 }
182 return event;
183}
184
Jan Kara7053aee2014-01-21 15:48:14 -0800185static int fanotify_handle_event(struct fsnotify_group *group,
186 struct inode *inode,
187 struct fsnotify_mark *inode_mark,
188 struct fsnotify_mark *fanotify_mark,
Al Viro3cd5eca2016-11-20 20:19:09 -0500189 u32 mask, const void *data, int data_type,
Jan Kara9385a842016-11-10 17:51:50 +0100190 const unsigned char *file_name, u32 cookie,
191 struct fsnotify_iter_info *iter_info)
Jan Kara7053aee2014-01-21 15:48:14 -0800192{
193 int ret = 0;
194 struct fanotify_event_info *event;
195 struct fsnotify_event *fsn_event;
Jan Kara7053aee2014-01-21 15:48:14 -0800196
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);
207
Jan Kara83c4c4b2014-01-21 15:48:15 -0800208 if (!fanotify_should_send_event(inode_mark, fanotify_mark, mask, data,
209 data_type))
210 return 0;
211
Jan Kara7053aee2014-01-21 15:48:14 -0800212 pr_debug("%s: group=%p inode=%p mask=%x\n", __func__, group, inode,
213 mask);
214
Jan Karaf0834412014-04-03 14:46:33 -0700215 event = fanotify_alloc_event(inode, mask, data);
Jan Kara7053aee2014-01-21 15:48:14 -0800216 if (unlikely(!event))
217 return -ENOMEM;
218
219 fsn_event = &event->fse;
Jan Kara8ba8fa912014-08-06 16:03:26 -0700220 ret = fsnotify_add_event(group, fsn_event, fanotify_merge);
Jan Kara83c0e1b2014-01-28 18:53:22 +0100221 if (ret) {
Jan Kara482ef062014-02-21 19:07:54 +0100222 /* Permission events shouldn't be merged */
223 BUG_ON(ret == 1 && mask & FAN_ALL_PERM_EVENTS);
Jan Kara7053aee2014-01-21 15:48:14 -0800224 /* Our event wasn't used in the end. Free it. */
225 fsnotify_destroy_event(group, fsn_event);
Jan Kara482ef062014-02-21 19:07:54 +0100226
227 return 0;
Jan Kara7053aee2014-01-21 15:48:14 -0800228 }
229
230#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
Jan Kara85816792014-01-28 21:38:06 +0100231 if (mask & FAN_ALL_PERM_EVENTS) {
Jan Kara05f0e382016-11-10 17:45:16 +0100232 ret = fanotify_get_response(group, FANOTIFY_PE(fsn_event),
233 iter_info);
Jan Kara85816792014-01-28 21:38:06 +0100234 fsnotify_destroy_event(group, fsn_event);
235 }
Jan Kara7053aee2014-01-21 15:48:14 -0800236#endif
237 return ret;
238}
239
Eric Paris4afeff82010-10-28 17:21:58 -0400240static void fanotify_free_group_priv(struct fsnotify_group *group)
241{
242 struct user_struct *user;
243
244 user = group->fanotify_data.user;
245 atomic_dec(&user->fanotify_listeners);
246 free_uid(user);
247}
248
Jan Kara7053aee2014-01-21 15:48:14 -0800249static void fanotify_free_event(struct fsnotify_event *fsn_event)
250{
251 struct fanotify_event_info *event;
252
253 event = FANOTIFY_E(fsn_event);
254 path_put(&event->path);
255 put_pid(event->tgid);
Jan Karaf0834412014-04-03 14:46:33 -0700256#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));
260 return;
261 }
262#endif
Jan Kara7053aee2014-01-21 15:48:14 -0800263 kmem_cache_free(fanotify_event_cachep, event);
264}
265
Jan Kara054c6362016-12-21 18:06:12 +0100266static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
267{
268 kmem_cache_free(fanotify_mark_cache, fsn_mark);
269}
270
Eric Parisff0b16a2009-12-17 21:24:25 -0500271const struct fsnotify_ops fanotify_fsnotify_ops = {
272 .handle_event = fanotify_handle_event,
Eric Paris4afeff82010-10-28 17:21:58 -0400273 .free_group_priv = fanotify_free_group_priv,
Jan Kara7053aee2014-01-21 15:48:14 -0800274 .free_event = fanotify_free_event,
Jan Kara054c6362016-12-21 18:06:12 +0100275 .free_mark = fanotify_free_mark,
Eric Parisff0b16a2009-12-17 21:24:25 -0500276};