blob: 1968d21a3f37a7e9e122ea4847575a593b92043d [file] [log] [blame]
Andreas Gruenbacher33d3dff2009-12-17 21:24:29 -05001#include <linux/fanotify.h>
Eric Parisff0b16a2009-12-17 21:24:25 -05002#include <linux/fdtable.h>
3#include <linux/fsnotify_backend.h>
4#include <linux/init.h>
Eric Paris9e66e422009-12-17 21:24:34 -05005#include <linux/jiffies.h>
Eric Parisff0b16a2009-12-17 21:24:25 -05006#include <linux/kernel.h> /* UINT_MAX */
Eric Paris1c529062009-12-17 21:24:28 -05007#include <linux/mount.h>
Eric Paris9e66e422009-12-17 21:24:34 -05008#include <linux/sched.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +01009#include <linux/sched/user.h>
Eric Parisff0b16a2009-12-17 21:24:25 -050010#include <linux/types.h>
Eric Paris9e66e422009-12-17 21:24:34 -050011#include <linux/wait.h>
Steve Grubbde8cd832017-10-02 20:21:39 -040012#include <linux/audit.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 */
Steve Grubbde8cd832017-10-02 20:21:39 -040082 switch (event->response & ~FAN_AUDIT) {
Eric Paris9e66e422009-12-17 21:24:34 -050083 case FAN_ALLOW:
84 ret = 0;
85 break;
86 case FAN_DENY:
87 default:
88 ret = -EPERM;
89 }
Steve Grubbde8cd832017-10-02 20:21:39 -040090
91 /* Check if the response should be audited */
92 if (event->response & FAN_AUDIT)
93 audit_fanotify(event->response & ~FAN_AUDIT);
94
Eric Paris9e66e422009-12-17 21:24:34 -050095 event->response = 0;
Eric Paris9e66e422009-12-17 21:24:34 -050096
Eric Parisb2d87902009-12-17 21:24:34 -050097 pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
98 group, event, ret);
99
Eric Paris9e66e422009-12-17 21:24:34 -0500100 return ret;
101}
102#endif
103
Jan Kara83c4c4b2014-01-21 15:48:15 -0800104static bool fanotify_should_send_event(struct fsnotify_mark *inode_mark,
Eric Paris1968f5e2010-07-28 10:18:39 -0400105 struct fsnotify_mark *vfsmnt_mark,
Jan Kara83c4c4b2014-01-21 15:48:15 -0800106 u32 event_mask,
Al Viro3cd5eca2016-11-20 20:19:09 -0500107 const void *data, int data_type)
Eric Paris1c529062009-12-17 21:24:28 -0500108{
Eric Paris1968f5e2010-07-28 10:18:39 -0400109 __u32 marks_mask, marks_ignored_mask;
Al Viro3cd5eca2016-11-20 20:19:09 -0500110 const struct path *path = data;
Eric Paris1968f5e2010-07-28 10:18:39 -0400111
Jan Kara83c4c4b2014-01-21 15:48:15 -0800112 pr_debug("%s: inode_mark=%p vfsmnt_mark=%p mask=%x data=%p"
113 " data_type=%d\n", __func__, inode_mark, vfsmnt_mark,
114 event_mask, data, data_type);
Eric Paris1968f5e2010-07-28 10:18:39 -0400115
Eric Paris1c529062009-12-17 21:24:28 -0500116 /* if we don't have enough info to send an event to userspace say no */
Linus Torvalds20696012010-08-12 14:23:04 -0700117 if (data_type != FSNOTIFY_EVENT_PATH)
Eric Paris1c529062009-12-17 21:24:28 -0500118 return false;
119
Eric Parise1c048b2010-10-28 17:21:58 -0400120 /* sorry, fanotify only gives a damn about files and dirs */
David Howellse36cb0b2015-01-29 12:02:35 +0000121 if (!d_is_reg(path->dentry) &&
David Howells54f2a2f2015-01-29 12:02:36 +0000122 !d_can_lookup(path->dentry))
Eric Parise1c048b2010-10-28 17:21:58 -0400123 return false;
124
Eric Paris1968f5e2010-07-28 10:18:39 -0400125 if (inode_mark && vfsmnt_mark) {
126 marks_mask = (vfsmnt_mark->mask | inode_mark->mask);
127 marks_ignored_mask = (vfsmnt_mark->ignored_mask | inode_mark->ignored_mask);
128 } else if (inode_mark) {
129 /*
130 * if the event is for a child and this inode doesn't care about
131 * events on the child, don't send it!
132 */
133 if ((event_mask & FS_EVENT_ON_CHILD) &&
134 !(inode_mark->mask & FS_EVENT_ON_CHILD))
135 return false;
136 marks_mask = inode_mark->mask;
137 marks_ignored_mask = inode_mark->ignored_mask;
138 } else if (vfsmnt_mark) {
139 marks_mask = vfsmnt_mark->mask;
140 marks_ignored_mask = vfsmnt_mark->ignored_mask;
141 } else {
142 BUG();
143 }
144
David Howellse36cb0b2015-01-29 12:02:35 +0000145 if (d_is_dir(path->dentry) &&
Lino Sanfilippo66ba93c2015-02-10 14:08:27 -0800146 !(marks_mask & FS_ISDIR & ~marks_ignored_mask))
Eric Paris8fcd6522010-10-28 17:21:59 -0400147 return false;
148
Suzuki K. Pouloseb3c10302015-03-12 16:26:08 -0700149 if (event_mask & FAN_ALL_OUTGOING_EVENTS & marks_mask &
150 ~marks_ignored_mask)
Eric Paris1968f5e2010-07-28 10:18:39 -0400151 return true;
152
153 return false;
Eric Paris1c529062009-12-17 21:24:28 -0500154}
155
Jan Karaf0834412014-04-03 14:46:33 -0700156struct fanotify_event_info *fanotify_alloc_event(struct inode *inode, u32 mask,
Al Viro3cd5eca2016-11-20 20:19:09 -0500157 const struct path *path)
Jan Karaf0834412014-04-03 14:46:33 -0700158{
159 struct fanotify_event_info *event;
160
161#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
162 if (mask & FAN_ALL_PERM_EVENTS) {
163 struct fanotify_perm_event_info *pevent;
164
165 pevent = kmem_cache_alloc(fanotify_perm_event_cachep,
166 GFP_KERNEL);
167 if (!pevent)
168 return NULL;
169 event = &pevent->fae;
170 pevent->response = 0;
171 goto init;
172 }
173#endif
174 event = kmem_cache_alloc(fanotify_event_cachep, GFP_KERNEL);
175 if (!event)
176 return NULL;
177init: __maybe_unused
178 fsnotify_init_event(&event->fse, inode, mask);
179 event->tgid = get_pid(task_tgid(current));
180 if (path) {
181 event->path = *path;
182 path_get(&event->path);
183 } else {
184 event->path.mnt = NULL;
185 event->path.dentry = NULL;
186 }
187 return event;
188}
189
Jan Kara7053aee2014-01-21 15:48:14 -0800190static int fanotify_handle_event(struct fsnotify_group *group,
191 struct inode *inode,
192 struct fsnotify_mark *inode_mark,
193 struct fsnotify_mark *fanotify_mark,
Al Viro3cd5eca2016-11-20 20:19:09 -0500194 u32 mask, const void *data, int data_type,
Jan Kara9385a842016-11-10 17:51:50 +0100195 const unsigned char *file_name, u32 cookie,
196 struct fsnotify_iter_info *iter_info)
Jan Kara7053aee2014-01-21 15:48:14 -0800197{
198 int ret = 0;
199 struct fanotify_event_info *event;
200 struct fsnotify_event *fsn_event;
Jan Kara7053aee2014-01-21 15:48:14 -0800201
202 BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
203 BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
204 BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
205 BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
206 BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
207 BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
208 BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
209 BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
210 BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
211 BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR);
212
Jan Kara83c4c4b2014-01-21 15:48:15 -0800213 if (!fanotify_should_send_event(inode_mark, fanotify_mark, mask, data,
214 data_type))
215 return 0;
216
Jan Kara7053aee2014-01-21 15:48:14 -0800217 pr_debug("%s: group=%p inode=%p mask=%x\n", __func__, group, inode,
218 mask);
219
Jan Karaf0834412014-04-03 14:46:33 -0700220 event = fanotify_alloc_event(inode, mask, data);
Jan Kara7053aee2014-01-21 15:48:14 -0800221 if (unlikely(!event))
222 return -ENOMEM;
223
224 fsn_event = &event->fse;
Jan Kara8ba8fa912014-08-06 16:03:26 -0700225 ret = fsnotify_add_event(group, fsn_event, fanotify_merge);
Jan Kara83c0e1b2014-01-28 18:53:22 +0100226 if (ret) {
Jan Kara482ef062014-02-21 19:07:54 +0100227 /* Permission events shouldn't be merged */
228 BUG_ON(ret == 1 && mask & FAN_ALL_PERM_EVENTS);
Jan Kara7053aee2014-01-21 15:48:14 -0800229 /* Our event wasn't used in the end. Free it. */
230 fsnotify_destroy_event(group, fsn_event);
Jan Kara482ef062014-02-21 19:07:54 +0100231
232 return 0;
Jan Kara7053aee2014-01-21 15:48:14 -0800233 }
234
235#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
Jan Kara85816792014-01-28 21:38:06 +0100236 if (mask & FAN_ALL_PERM_EVENTS) {
Jan Kara05f0e382016-11-10 17:45:16 +0100237 ret = fanotify_get_response(group, FANOTIFY_PE(fsn_event),
238 iter_info);
Jan Kara85816792014-01-28 21:38:06 +0100239 fsnotify_destroy_event(group, fsn_event);
240 }
Jan Kara7053aee2014-01-21 15:48:14 -0800241#endif
242 return ret;
243}
244
Eric Paris4afeff82010-10-28 17:21:58 -0400245static void fanotify_free_group_priv(struct fsnotify_group *group)
246{
247 struct user_struct *user;
248
249 user = group->fanotify_data.user;
250 atomic_dec(&user->fanotify_listeners);
251 free_uid(user);
252}
253
Jan Kara7053aee2014-01-21 15:48:14 -0800254static void fanotify_free_event(struct fsnotify_event *fsn_event)
255{
256 struct fanotify_event_info *event;
257
258 event = FANOTIFY_E(fsn_event);
259 path_put(&event->path);
260 put_pid(event->tgid);
Jan Karaf0834412014-04-03 14:46:33 -0700261#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
262 if (fsn_event->mask & FAN_ALL_PERM_EVENTS) {
263 kmem_cache_free(fanotify_perm_event_cachep,
264 FANOTIFY_PE(fsn_event));
265 return;
266 }
267#endif
Jan Kara7053aee2014-01-21 15:48:14 -0800268 kmem_cache_free(fanotify_event_cachep, event);
269}
270
Jan Kara054c6362016-12-21 18:06:12 +0100271static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
272{
273 kmem_cache_free(fanotify_mark_cache, fsn_mark);
274}
275
Eric Parisff0b16a2009-12-17 21:24:25 -0500276const struct fsnotify_ops fanotify_fsnotify_ops = {
277 .handle_event = fanotify_handle_event,
Eric Paris4afeff82010-10-28 17:21:58 -0400278 .free_group_priv = fanotify_free_group_priv,
Jan Kara7053aee2014-01-21 15:48:14 -0800279 .free_event = fanotify_free_event,
Jan Kara054c6362016-12-21 18:06:12 +0100280 .free_mark = fanotify_free_mark,
Eric Parisff0b16a2009-12-17 21:24:25 -0500281};