]> git.ipfire.org Git - thirdparty/linux.git/blame - fs/notify/fanotify/fanotify.c
Merge tag 'x86-fpu-2020-06-01' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
[thirdparty/linux.git] / fs / notify / fanotify / fanotify.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
33d3dfff 2#include <linux/fanotify.h>
ff0b16a9
EP
3#include <linux/fdtable.h>
4#include <linux/fsnotify_backend.h>
5#include <linux/init.h>
9e66e423 6#include <linux/jiffies.h>
ff0b16a9 7#include <linux/kernel.h> /* UINT_MAX */
1c529063 8#include <linux/mount.h>
9e66e423 9#include <linux/sched.h>
5b825c3a 10#include <linux/sched/user.h>
7a36094d 11#include <linux/sched/signal.h>
ff0b16a9 12#include <linux/types.h>
9e66e423 13#include <linux/wait.h>
de8cd83e 14#include <linux/audit.h>
d46eb14b 15#include <linux/sched/mm.h>
e9e0c890 16#include <linux/statfs.h>
ff0b16a9 17
7053aee2
JK
18#include "fanotify.h"
19
afc894c7
JK
20static bool fanotify_path_equal(struct path *p1, struct path *p2)
21{
22 return p1->mnt == p2->mnt && p1->dentry == p2->dentry;
23}
24
25static inline bool fanotify_fsid_equal(__kernel_fsid_t *fsid1,
26 __kernel_fsid_t *fsid2)
27{
6def1a1d 28 return fsid1->val[0] == fsid2->val[0] && fsid1->val[1] == fsid2->val[1];
afc894c7
JK
29}
30
31static bool fanotify_fh_equal(struct fanotify_fh *fh1,
7088f357 32 struct fanotify_fh *fh2)
afc894c7
JK
33{
34 if (fh1->type != fh2->type || fh1->len != fh2->len)
35 return false;
36
37 /* Do not merge events if we failed to encode fh */
38 if (fh1->type == FILEID_INVALID)
39 return false;
40
41 return !fh1->len ||
42 !memcmp(fanotify_fh_buf(fh1), fanotify_fh_buf(fh2), fh1->len);
43}
44
7088f357
JK
45static bool fanotify_fid_event_equal(struct fanotify_fid_event *ffe1,
46 struct fanotify_fid_event *ffe2)
47{
48 /* Do not merge fid events without object fh */
49 if (!ffe1->object_fh.len)
50 return false;
51
52 return fanotify_fsid_equal(&ffe1->fsid, &ffe2->fsid) &&
53 fanotify_fh_equal(&ffe1->object_fh, &ffe2->object_fh);
54}
55
cacfb956
AG
56static bool fanotify_name_event_equal(struct fanotify_name_event *fne1,
57 struct fanotify_name_event *fne2)
58{
59 /*
60 * Do not merge name events without dir fh.
61 * FAN_DIR_MODIFY does not encode object fh, so it may be empty.
62 */
63 if (!fne1->dir_fh.len)
64 return false;
65
66 if (fne1->name_len != fne2->name_len ||
67 !fanotify_fh_equal(&fne1->dir_fh, &fne2->dir_fh))
68 return false;
69
70 return !memcmp(fne1->name, fne2->name, fne1->name_len);
71}
72
7053aee2
JK
73static bool should_merge(struct fsnotify_event *old_fsn,
74 struct fsnotify_event *new_fsn)
767cd46c 75{
33913997 76 struct fanotify_event *old, *new;
767cd46c 77
7053aee2
JK
78 pr_debug("%s: old=%p new=%p\n", __func__, old_fsn, new_fsn);
79 old = FANOTIFY_E(old_fsn);
80 new = FANOTIFY_E(new_fsn);
81
7088f357
JK
82 if (old_fsn->objectid != new_fsn->objectid ||
83 old->type != new->type || old->pid != new->pid)
e9e0c890
AG
84 return false;
85
7088f357
JK
86 switch (old->type) {
87 case FANOTIFY_EVENT_TYPE_PATH:
afc894c7
JK
88 return fanotify_path_equal(fanotify_event_path(old),
89 fanotify_event_path(new));
7088f357 90 case FANOTIFY_EVENT_TYPE_FID:
e7fce6d9
AG
91 /*
92 * We want to merge many dirent events in the same dir (i.e.
93 * creates/unlinks/renames), but we do not want to merge dirent
94 * events referring to subdirs with dirent events referring to
95 * non subdirs, otherwise, user won't be able to tell from a
96 * mask FAN_CREATE|FAN_DELETE|FAN_ONDIR if it describes mkdir+
97 * unlink pair or rmdir+create pair of events.
98 */
afc894c7
JK
99 if ((old->mask & FS_ISDIR) != (new->mask & FS_ISDIR))
100 return false;
101
7088f357
JK
102 return fanotify_fid_event_equal(FANOTIFY_FE(old),
103 FANOTIFY_FE(new));
cacfb956
AG
104 case FANOTIFY_EVENT_TYPE_FID_NAME:
105 return fanotify_name_event_equal(FANOTIFY_NE(old),
106 FANOTIFY_NE(new));
7088f357
JK
107 default:
108 WARN_ON_ONCE(1);
e9e0c890
AG
109 }
110
767cd46c
EP
111 return false;
112}
113
f70ab54c 114/* and the list better be locked by something too! */
83c0e1b4 115static int fanotify_merge(struct list_head *list, struct fsnotify_event *event)
767cd46c 116{
7053aee2 117 struct fsnotify_event *test_event;
33913997 118 struct fanotify_event *new;
767cd46c
EP
119
120 pr_debug("%s: list=%p event=%p\n", __func__, list, event);
a0a92d26 121 new = FANOTIFY_E(event);
767cd46c 122
13116dfd
JK
123 /*
124 * Don't merge a permission event with any other event so that we know
125 * the event structure we have created in fanotify_handle_event() is the
126 * one we should check for permission response.
127 */
a0a92d26 128 if (fanotify_is_perm_event(new->mask))
83c0e1b4 129 return 0;
13116dfd 130
7053aee2
JK
131 list_for_each_entry_reverse(test_event, list, list) {
132 if (should_merge(test_event, event)) {
a0a92d26 133 FANOTIFY_E(test_event)->mask |= new->mask;
6c71100d 134 return 1;
a12a7dd3
EP
135 }
136 }
f70ab54c 137
6c71100d 138 return 0;
767cd46c
EP
139}
140
fabf7f29
JK
141/*
142 * Wait for response to permission event. The function also takes care of
143 * freeing the permission event (or offloads that in case the wait is canceled
144 * by a signal). The function returns 0 in case access got allowed by userspace,
145 * -EPERM in case userspace disallowed the access, and -ERESTARTSYS in case
146 * the wait got interrupted by a signal.
147 */
f083441b 148static int fanotify_get_response(struct fsnotify_group *group,
33913997 149 struct fanotify_perm_event *event,
05f0e387 150 struct fsnotify_iter_info *iter_info)
9e66e423
EP
151{
152 int ret;
153
154 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
155
b5190579
JK
156 ret = wait_event_killable(group->fanotify_data.access_waitq,
157 event->state == FAN_EVENT_ANSWERED);
fabf7f29
JK
158 /* Signal pending? */
159 if (ret < 0) {
160 spin_lock(&group->notification_lock);
161 /* Event reported to userspace and no answer yet? */
162 if (event->state == FAN_EVENT_REPORTED) {
163 /* Event will get freed once userspace answers to it */
164 event->state = FAN_EVENT_CANCELED;
165 spin_unlock(&group->notification_lock);
166 return ret;
167 }
168 /* Event not yet reported? Just remove it. */
169 if (event->state == FAN_EVENT_INIT)
170 fsnotify_remove_queued_event(group, &event->fae.fse);
171 /*
172 * Event may be also answered in case signal delivery raced
173 * with wakeup. In that case we have nothing to do besides
174 * freeing the event and reporting error.
175 */
176 spin_unlock(&group->notification_lock);
177 goto out;
178 }
9e66e423
EP
179
180 /* userspace responded, convert to something usable */
de8cd83e 181 switch (event->response & ~FAN_AUDIT) {
9e66e423
EP
182 case FAN_ALLOW:
183 ret = 0;
184 break;
185 case FAN_DENY:
186 default:
187 ret = -EPERM;
188 }
de8cd83e
SG
189
190 /* Check if the response should be audited */
191 if (event->response & FAN_AUDIT)
192 audit_fanotify(event->response & ~FAN_AUDIT);
193
b2d87909
EP
194 pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
195 group, event, ret);
fabf7f29
JK
196out:
197 fsnotify_destroy_event(group, &event->fae.fse);
83b535d2 198
9e66e423
EP
199 return ret;
200}
9e66e423 201
2d10b230
MB
202/*
203 * This function returns a mask for an event that only contains the flags
204 * that have been specifically requested by the user. Flags that may have
205 * been included within the event mask, but have not been explicitly
206 * requested by the user, will not be present in the returned mask.
207 */
83b535d2
AG
208static u32 fanotify_group_event_mask(struct fsnotify_group *group,
209 struct fsnotify_iter_info *iter_info,
210 u32 event_mask, const void *data,
211 int data_type)
1c529063 212{
54a307ba 213 __u32 marks_mask = 0, marks_ignored_mask = 0;
e7fce6d9 214 __u32 test_mask, user_mask = FANOTIFY_OUTGOING_EVENTS;
aa93bdc5 215 const struct path *path = fsnotify_data_path(data, data_type);
837a3934
AG
216 struct fsnotify_mark *mark;
217 int type;
1968f5ee 218
837a3934
AG
219 pr_debug("%s: report_mask=%x mask=%x data=%p data_type=%d\n",
220 __func__, iter_info->report_mask, event_mask, data, data_type);
1968f5ee 221
83b535d2
AG
222 if (!FAN_GROUP_FLAG(group, FAN_REPORT_FID)) {
223 /* Do we have path to open a file descriptor? */
aa93bdc5 224 if (!path)
83b535d2
AG
225 return 0;
226 /* Path type events are only relevant for files and dirs */
227 if (!d_is_reg(path->dentry) && !d_can_lookup(path->dentry))
228 return 0;
229 }
e1c048ba 230
837a3934
AG
231 fsnotify_foreach_obj_type(type) {
232 if (!fsnotify_iter_should_report_type(iter_info, type))
233 continue;
234 mark = iter_info->marks[type];
55bf882c
AG
235 /*
236 * If the event is on dir and this mark doesn't care about
237 * events on dir, don't send it!
238 */
239 if (event_mask & FS_ISDIR && !(mark->mask & FS_ISDIR))
240 continue;
241
837a3934 242 /*
b469e7e4
AG
243 * If the event is for a child and this mark doesn't care about
244 * events on a child, don't send it!
837a3934 245 */
b469e7e4
AG
246 if (event_mask & FS_EVENT_ON_CHILD &&
247 (type != FSNOTIFY_OBJ_TYPE_INODE ||
248 !(mark->mask & FS_EVENT_ON_CHILD)))
837a3934
AG
249 continue;
250
251 marks_mask |= mark->mask;
252 marks_ignored_mask |= mark->ignored_mask;
1968f5ee
EP
253 }
254
e7fce6d9
AG
255 test_mask = event_mask & marks_mask & ~marks_ignored_mask;
256
257 /*
9e2ba2c3
AG
258 * For dirent modification events (create/delete/move) that do not carry
259 * the child entry name information, we report FAN_ONDIR for mkdir/rmdir
260 * so user can differentiate them from creat/unlink.
e7fce6d9
AG
261 *
262 * For backward compatibility and consistency, do not report FAN_ONDIR
263 * to user in legacy fanotify mode (reporting fd) and report FAN_ONDIR
264 * to user in FAN_REPORT_FID mode for all event types.
265 */
266 if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) {
267 /* Do not report FAN_ONDIR without any event */
268 if (!(test_mask & ~FAN_ONDIR))
269 return 0;
270 } else {
271 user_mask &= ~FAN_ONDIR;
272 }
273
e7fce6d9 274 return test_mask & user_mask;
1c529063
EP
275}
276
afc894c7
JK
277static void fanotify_encode_fh(struct fanotify_fh *fh, struct inode *inode,
278 gfp_t gfp)
e9e0c890 279{
afc894c7
JK
280 int dwords, type, bytes = 0;
281 char *ext_buf = NULL;
282 void *buf = fh->buf;
283 int err;
e9e0c890 284
cacfb956
AG
285 if (!inode)
286 goto out;
287
e9e0c890
AG
288 dwords = 0;
289 err = -ENOENT;
77115225 290 type = exportfs_encode_inode_fh(inode, NULL, &dwords, NULL);
e9e0c890
AG
291 if (!dwords)
292 goto out_err;
293
e9e0c890
AG
294 bytes = dwords << 2;
295 if (bytes > FANOTIFY_INLINE_FH_LEN) {
296 /* Treat failure to allocate fh as failure to allocate event */
297 err = -ENOMEM;
afc894c7
JK
298 ext_buf = kmalloc(bytes, gfp);
299 if (!ext_buf)
e9e0c890 300 goto out_err;
afc894c7
JK
301
302 *fanotify_fh_ext_buf_ptr(fh) = ext_buf;
303 buf = ext_buf;
e9e0c890
AG
304 }
305
afc894c7 306 type = exportfs_encode_inode_fh(inode, buf, &dwords, NULL);
e9e0c890
AG
307 err = -EINVAL;
308 if (!type || type == FILEID_INVALID || bytes != dwords << 2)
309 goto out_err;
310
afc894c7
JK
311 fh->type = type;
312 fh->len = bytes;
e9e0c890 313
afc894c7 314 return;
e9e0c890
AG
315
316out_err:
afc894c7
JK
317 pr_warn_ratelimited("fanotify: failed to encode fid (type=%d, len=%d, err=%i)\n",
318 type, bytes, err);
319 kfree(ext_buf);
320 *fanotify_fh_ext_buf_ptr(fh) = NULL;
cacfb956 321out:
afc894c7
JK
322 /* Report the event without a file identifier on encode error */
323 fh->type = FILEID_INVALID;
324 fh->len = 0;
e9e0c890
AG
325}
326
83b535d2
AG
327/*
328 * The inode to use as identifier when reporting fid depends on the event.
329 * Report the modified directory inode on dirent modification events.
330 * Report the "victim" inode otherwise.
331 * For example:
332 * FS_ATTRIB reports the child inode even if reported on a watched parent.
333 * FS_CREATE reports the modified dir inode and not the created inode.
334 */
335static struct inode *fanotify_fid_inode(struct inode *to_tell, u32 event_mask,
336 const void *data, int data_type)
337{
338 if (event_mask & ALL_FSNOTIFY_DIRENT_EVENTS)
339 return to_tell;
aa93bdc5
AG
340
341 return (struct inode *)fsnotify_data_inode(data, data_type);
83b535d2
AG
342}
343
33913997 344struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group,
77115225 345 struct inode *inode, u32 mask,
83b535d2 346 const void *data, int data_type,
cacfb956 347 const struct qstr *file_name,
77115225 348 __kernel_fsid_t *fsid)
f083441b 349{
33913997 350 struct fanotify_event *event = NULL;
7088f357 351 struct fanotify_fid_event *ffe = NULL;
cacfb956 352 struct fanotify_name_event *fne = NULL;
d46eb14b 353 gfp_t gfp = GFP_KERNEL_ACCOUNT;
83b535d2 354 struct inode *id = fanotify_fid_inode(inode, mask, data, data_type);
aa93bdc5 355 const struct path *path = fsnotify_data_path(data, data_type);
1f5eaa90
JK
356
357 /*
358 * For queues with unlimited length lost events are not expected and
359 * can possibly have security implications. Avoid losing events when
ec165450
SB
360 * memory is short. For the limited size queues, avoid OOM killer in the
361 * target monitoring memcg as it may have security repercussion.
1f5eaa90
JK
362 */
363 if (group->max_events == UINT_MAX)
364 gfp |= __GFP_NOFAIL;
ec165450
SB
365 else
366 gfp |= __GFP_RETRY_MAYFAIL;
f083441b 367
d46eb14b
SB
368 /* Whoever is interested in the event, pays for the allocation. */
369 memalloc_use_memcg(group->memcg);
370
6685df31 371 if (fanotify_is_perm_event(mask)) {
33913997 372 struct fanotify_perm_event *pevent;
f083441b 373
1f5eaa90 374 pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp);
f083441b 375 if (!pevent)
d46eb14b 376 goto out;
7088f357 377
f083441b 378 event = &pevent->fae;
7088f357 379 event->type = FANOTIFY_EVENT_TYPE_PATH_PERM;
f083441b 380 pevent->response = 0;
40873284 381 pevent->state = FAN_EVENT_INIT;
f083441b
JK
382 goto init;
383 }
7088f357 384
cacfb956
AG
385 /*
386 * For FAN_DIR_MODIFY event, we report the fid of the directory and
387 * the name of the modified entry.
388 * Allocate an fanotify_name_event struct and copy the name.
389 */
390 if (mask & FAN_DIR_MODIFY && !(WARN_ON_ONCE(!file_name))) {
391 fne = kmalloc(sizeof(*fne) + file_name->len + 1, gfp);
392 if (!fne)
393 goto out;
394
395 event = &fne->fae;
396 event->type = FANOTIFY_EVENT_TYPE_FID_NAME;
397 fne->name_len = file_name->len;
398 strcpy(fne->name, file_name->name);
399 goto init;
400 }
401
7088f357
JK
402 if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) {
403 ffe = kmem_cache_alloc(fanotify_fid_event_cachep, gfp);
404 if (!ffe)
405 goto out;
406
407 event = &ffe->fae;
408 event->type = FANOTIFY_EVENT_TYPE_FID;
409 } else {
410 struct fanotify_path_event *pevent;
411
412 pevent = kmem_cache_alloc(fanotify_path_event_cachep, gfp);
413 if (!pevent)
414 goto out;
415
416 event = &pevent->fae;
417 event->type = FANOTIFY_EVENT_TYPE_PATH;
418 }
419
cacfb956 420init:
f367a62a
AG
421 /*
422 * Use the victim inode instead of the watching inode as the id for
423 * event queue, so event reported on parent is merged with event
424 * reported on child when both directory and child watches exist.
425 */
426 fsnotify_init_event(&event->fse, (unsigned long)id);
a0a92d26 427 event->mask = mask;
d0a6a87e
AG
428 if (FAN_GROUP_FLAG(group, FAN_REPORT_TID))
429 event->pid = get_pid(task_pid(current));
430 else
431 event->pid = get_pid(task_tgid(current));
7088f357 432
cacfb956
AG
433 if (fsid && fanotify_event_fsid(event))
434 *fanotify_event_fsid(event) = *fsid;
435
436 if (fanotify_event_object_fh(event))
437 fanotify_encode_fh(fanotify_event_object_fh(event), id, gfp);
438
439 if (fanotify_event_dir_fh(event))
440 fanotify_encode_fh(fanotify_event_dir_fh(event), id, gfp);
441
442 if (fanotify_event_has_path(event)) {
7088f357
JK
443 struct path *p = fanotify_event_path(event);
444
445 if (path) {
446 *p = *path;
447 path_get(path);
448 } else {
449 p->mnt = NULL;
450 p->dentry = NULL;
451 }
f083441b 452 }
d46eb14b
SB
453out:
454 memalloc_unuse_memcg();
f083441b
JK
455 return event;
456}
457
77115225
AG
458/*
459 * Get cached fsid of the filesystem containing the object from any connector.
460 * All connectors are supposed to have the same fsid, but we do not verify that
461 * here.
462 */
463static __kernel_fsid_t fanotify_get_fsid(struct fsnotify_iter_info *iter_info)
464{
465 int type;
466 __kernel_fsid_t fsid = {};
467
468 fsnotify_foreach_obj_type(type) {
b1da6a51
JK
469 struct fsnotify_mark_connector *conn;
470
77115225
AG
471 if (!fsnotify_iter_should_report_type(iter_info, type))
472 continue;
473
b1da6a51
JK
474 conn = READ_ONCE(iter_info->marks[type]->connector);
475 /* Mark is just getting destroyed or created? */
476 if (!conn)
477 continue;
c285a2f0
AG
478 if (!(conn->flags & FSNOTIFY_CONN_FLAG_HAS_FSID))
479 continue;
480 /* Pairs with smp_wmb() in fsnotify_add_mark_list() */
481 smp_rmb();
b1da6a51 482 fsid = conn->fsid;
77115225
AG
483 if (WARN_ON_ONCE(!fsid.val[0] && !fsid.val[1]))
484 continue;
485 return fsid;
486 }
487
488 return fsid;
489}
490
7053aee2
JK
491static int fanotify_handle_event(struct fsnotify_group *group,
492 struct inode *inode,
3cd5eca8 493 u32 mask, const void *data, int data_type,
e43e9c33 494 const struct qstr *file_name, u32 cookie,
9385a84d 495 struct fsnotify_iter_info *iter_info)
7053aee2
JK
496{
497 int ret = 0;
33913997 498 struct fanotify_event *event;
7053aee2 499 struct fsnotify_event *fsn_event;
77115225 500 __kernel_fsid_t fsid = {};
7053aee2
JK
501
502 BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
503 BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
235328d1 504 BUILD_BUG_ON(FAN_ATTRIB != FS_ATTRIB);
7053aee2
JK
505 BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
506 BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
507 BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
235328d1
AG
508 BUILD_BUG_ON(FAN_MOVED_TO != FS_MOVED_TO);
509 BUILD_BUG_ON(FAN_MOVED_FROM != FS_MOVED_FROM);
510 BUILD_BUG_ON(FAN_CREATE != FS_CREATE);
511 BUILD_BUG_ON(FAN_DELETE != FS_DELETE);
9e2ba2c3 512 BUILD_BUG_ON(FAN_DIR_MODIFY != FS_DIR_MODIFY);
235328d1
AG
513 BUILD_BUG_ON(FAN_DELETE_SELF != FS_DELETE_SELF);
514 BUILD_BUG_ON(FAN_MOVE_SELF != FS_MOVE_SELF);
7053aee2
JK
515 BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
516 BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
517 BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
518 BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
519 BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR);
9b076f1c 520 BUILD_BUG_ON(FAN_OPEN_EXEC != FS_OPEN_EXEC);
66917a31 521 BUILD_BUG_ON(FAN_OPEN_EXEC_PERM != FS_OPEN_EXEC_PERM);
7053aee2 522
f1793699 523 BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 19);
bdd5a46f 524
83b535d2
AG
525 mask = fanotify_group_event_mask(group, iter_info, mask, data,
526 data_type);
2d10b230 527 if (!mask)
83c4c4b0
JK
528 return 0;
529
7053aee2
JK
530 pr_debug("%s: group=%p inode=%p mask=%x\n", __func__, group, inode,
531 mask);
532
6685df31 533 if (fanotify_is_perm_event(mask)) {
f37650f1
MS
534 /*
535 * fsnotify_prepare_user_wait() fails if we race with mark
536 * deletion. Just let the operation pass in that case.
537 */
538 if (!fsnotify_prepare_user_wait(iter_info))
539 return 0;
540 }
f37650f1 541
b1da6a51 542 if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) {
77115225 543 fsid = fanotify_get_fsid(iter_info);
b1da6a51
JK
544 /* Racing with mark destruction or creation? */
545 if (!fsid.val[0] && !fsid.val[1])
546 return 0;
547 }
77115225 548
83b535d2 549 event = fanotify_alloc_event(group, inode, mask, data, data_type,
cacfb956 550 file_name, &fsid);
f37650f1 551 ret = -ENOMEM;
7b1f6417
JK
552 if (unlikely(!event)) {
553 /*
554 * We don't queue overflow events for permission events as
555 * there the access is denied and so no event is in fact lost.
556 */
557 if (!fanotify_is_perm_event(mask))
558 fsnotify_queue_overflow(group);
f37650f1 559 goto finish;
7b1f6417 560 }
7053aee2
JK
561
562 fsn_event = &event->fse;
8ba8fa91 563 ret = fsnotify_add_event(group, fsn_event, fanotify_merge);
83c0e1b4 564 if (ret) {
482ef06c 565 /* Permission events shouldn't be merged */
23c9deeb 566 BUG_ON(ret == 1 && mask & FANOTIFY_PERM_EVENTS);
7053aee2
JK
567 /* Our event wasn't used in the end. Free it. */
568 fsnotify_destroy_event(group, fsn_event);
482ef06c 569
f37650f1 570 ret = 0;
6685df31 571 } else if (fanotify_is_perm_event(mask)) {
7088f357 572 ret = fanotify_get_response(group, FANOTIFY_PERM(event),
05f0e387 573 iter_info);
85816794 574 }
f37650f1 575finish:
6685df31 576 if (fanotify_is_perm_event(mask))
f37650f1 577 fsnotify_finish_user_wait(iter_info);
6685df31 578
7053aee2
JK
579 return ret;
580}
581
4afeff85
EP
582static void fanotify_free_group_priv(struct fsnotify_group *group)
583{
584 struct user_struct *user;
585
586 user = group->fanotify_data.user;
587 atomic_dec(&user->fanotify_listeners);
588 free_uid(user);
589}
590
7088f357
JK
591static void fanotify_free_path_event(struct fanotify_event *event)
592{
593 path_put(fanotify_event_path(event));
594 kmem_cache_free(fanotify_path_event_cachep, FANOTIFY_PE(event));
595}
596
597static void fanotify_free_perm_event(struct fanotify_event *event)
598{
599 path_put(fanotify_event_path(event));
600 kmem_cache_free(fanotify_perm_event_cachep, FANOTIFY_PERM(event));
601}
602
603static void fanotify_free_fid_event(struct fanotify_event *event)
604{
605 struct fanotify_fid_event *ffe = FANOTIFY_FE(event);
606
607 if (fanotify_fh_has_ext_buf(&ffe->object_fh))
608 kfree(fanotify_fh_ext_buf(&ffe->object_fh));
609 kmem_cache_free(fanotify_fid_event_cachep, ffe);
610}
611
cacfb956
AG
612static void fanotify_free_name_event(struct fanotify_event *event)
613{
614 struct fanotify_name_event *fne = FANOTIFY_NE(event);
615
616 if (fanotify_fh_has_ext_buf(&fne->dir_fh))
617 kfree(fanotify_fh_ext_buf(&fne->dir_fh));
618 kfree(fne);
619}
620
7053aee2
JK
621static void fanotify_free_event(struct fsnotify_event *fsn_event)
622{
33913997 623 struct fanotify_event *event;
7053aee2
JK
624
625 event = FANOTIFY_E(fsn_event);
d0a6a87e 626 put_pid(event->pid);
7088f357
JK
627 switch (event->type) {
628 case FANOTIFY_EVENT_TYPE_PATH:
629 fanotify_free_path_event(event);
630 break;
631 case FANOTIFY_EVENT_TYPE_PATH_PERM:
632 fanotify_free_perm_event(event);
633 break;
634 case FANOTIFY_EVENT_TYPE_FID:
635 fanotify_free_fid_event(event);
636 break;
cacfb956
AG
637 case FANOTIFY_EVENT_TYPE_FID_NAME:
638 fanotify_free_name_event(event);
639 break;
7088f357
JK
640 default:
641 WARN_ON_ONCE(1);
f083441b 642 }
7053aee2
JK
643}
644
054c636e
JK
645static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
646{
647 kmem_cache_free(fanotify_mark_cache, fsn_mark);
648}
649
ff0b16a9
EP
650const struct fsnotify_ops fanotify_fsnotify_ops = {
651 .handle_event = fanotify_handle_event,
4afeff85 652 .free_group_priv = fanotify_free_group_priv,
7053aee2 653 .free_event = fanotify_free_event,
054c636e 654 .free_mark = fanotify_free_mark,
ff0b16a9 655};