1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/fanotify.h>
3 #include <linux/fdtable.h>
4 #include <linux/fsnotify_backend.h>
5 #include <linux/init.h>
6 #include <linux/jiffies.h>
7 #include <linux/kernel.h> /* UINT_MAX */
8 #include <linux/mount.h>
9 #include <linux/sched.h>
10 #include <linux/sched/user.h>
11 #include <linux/sched/signal.h>
12 #include <linux/types.h>
13 #include <linux/wait.h>
14 #include <linux/audit.h>
15 #include <linux/sched/mm.h>
16 #include <linux/statfs.h>
17 #include <linux/stringhash.h>
18
19 #include "fanotify.h"
20
fanotify_path_equal(const struct path * p1,const struct path * p2)21 static bool fanotify_path_equal(const struct path *p1, const struct path *p2)
22 {
23 return p1->mnt == p2->mnt && p1->dentry == p2->dentry;
24 }
25
fanotify_hash_path(const struct path * path)26 static unsigned int fanotify_hash_path(const struct path *path)
27 {
28 return hash_ptr(path->dentry, FANOTIFY_EVENT_HASH_BITS) ^
29 hash_ptr(path->mnt, FANOTIFY_EVENT_HASH_BITS);
30 }
31
fanotify_hash_fsid(__kernel_fsid_t * fsid)32 static unsigned int fanotify_hash_fsid(__kernel_fsid_t *fsid)
33 {
34 return hash_32(fsid->val[0], FANOTIFY_EVENT_HASH_BITS) ^
35 hash_32(fsid->val[1], FANOTIFY_EVENT_HASH_BITS);
36 }
37
fanotify_fh_equal(struct fanotify_fh * fh1,struct fanotify_fh * fh2)38 static bool fanotify_fh_equal(struct fanotify_fh *fh1,
39 struct fanotify_fh *fh2)
40 {
41 if (fh1->type != fh2->type || fh1->len != fh2->len)
42 return false;
43
44 return !fh1->len ||
45 !memcmp(fanotify_fh_buf(fh1), fanotify_fh_buf(fh2), fh1->len);
46 }
47
fanotify_hash_fh(struct fanotify_fh * fh)48 static unsigned int fanotify_hash_fh(struct fanotify_fh *fh)
49 {
50 long salt = (long)fh->type | (long)fh->len << 8;
51
52 /*
53 * full_name_hash() works long by long, so it handles fh buf optimally.
54 */
55 return full_name_hash((void *)salt, fanotify_fh_buf(fh), fh->len);
56 }
57
fanotify_fid_event_equal(struct fanotify_fid_event * ffe1,struct fanotify_fid_event * ffe2)58 static bool fanotify_fid_event_equal(struct fanotify_fid_event *ffe1,
59 struct fanotify_fid_event *ffe2)
60 {
61 /* Do not merge fid events without object fh */
62 if (!ffe1->object_fh.len)
63 return false;
64
65 return fanotify_fsid_equal(&ffe1->fsid, &ffe2->fsid) &&
66 fanotify_fh_equal(&ffe1->object_fh, &ffe2->object_fh);
67 }
68
fanotify_info_equal(struct fanotify_info * info1,struct fanotify_info * info2)69 static bool fanotify_info_equal(struct fanotify_info *info1,
70 struct fanotify_info *info2)
71 {
72 if (info1->dir_fh_totlen != info2->dir_fh_totlen ||
73 info1->dir2_fh_totlen != info2->dir2_fh_totlen ||
74 info1->file_fh_totlen != info2->file_fh_totlen ||
75 info1->name_len != info2->name_len ||
76 info1->name2_len != info2->name2_len)
77 return false;
78
79 if (info1->dir_fh_totlen &&
80 !fanotify_fh_equal(fanotify_info_dir_fh(info1),
81 fanotify_info_dir_fh(info2)))
82 return false;
83
84 if (info1->dir2_fh_totlen &&
85 !fanotify_fh_equal(fanotify_info_dir2_fh(info1),
86 fanotify_info_dir2_fh(info2)))
87 return false;
88
89 if (info1->file_fh_totlen &&
90 !fanotify_fh_equal(fanotify_info_file_fh(info1),
91 fanotify_info_file_fh(info2)))
92 return false;
93
94 if (info1->name_len &&
95 memcmp(fanotify_info_name(info1), fanotify_info_name(info2),
96 info1->name_len))
97 return false;
98
99 return !info1->name2_len ||
100 !memcmp(fanotify_info_name2(info1), fanotify_info_name2(info2),
101 info1->name2_len);
102 }
103
fanotify_name_event_equal(struct fanotify_name_event * fne1,struct fanotify_name_event * fne2)104 static bool fanotify_name_event_equal(struct fanotify_name_event *fne1,
105 struct fanotify_name_event *fne2)
106 {
107 struct fanotify_info *info1 = &fne1->info;
108 struct fanotify_info *info2 = &fne2->info;
109
110 /* Do not merge name events without dir fh */
111 if (!info1->dir_fh_totlen)
112 return false;
113
114 if (!fanotify_fsid_equal(&fne1->fsid, &fne2->fsid))
115 return false;
116
117 return fanotify_info_equal(info1, info2);
118 }
119
fanotify_error_event_equal(struct fanotify_error_event * fee1,struct fanotify_error_event * fee2)120 static bool fanotify_error_event_equal(struct fanotify_error_event *fee1,
121 struct fanotify_error_event *fee2)
122 {
123 /* Error events against the same file system are always merged. */
124 if (!fanotify_fsid_equal(&fee1->fsid, &fee2->fsid))
125 return false;
126
127 return true;
128 }
129
fanotify_should_merge(struct fanotify_event * old,struct fanotify_event * new)130 static bool fanotify_should_merge(struct fanotify_event *old,
131 struct fanotify_event *new)
132 {
133 pr_debug("%s: old=%p new=%p\n", __func__, old, new);
134
135 if (old->hash != new->hash ||
136 old->type != new->type || old->pid != new->pid)
137 return false;
138
139 /*
140 * We want to merge many dirent events in the same dir (i.e.
141 * creates/unlinks/renames), but we do not want to merge dirent
142 * events referring to subdirs with dirent events referring to
143 * non subdirs, otherwise, user won't be able to tell from a
144 * mask FAN_CREATE|FAN_DELETE|FAN_ONDIR if it describes mkdir+
145 * unlink pair or rmdir+create pair of events.
146 */
147 if ((old->mask & FS_ISDIR) != (new->mask & FS_ISDIR))
148 return false;
149
150 /*
151 * FAN_RENAME event is reported with special info record types,
152 * so we cannot merge it with other events.
153 */
154 if ((old->mask & FAN_RENAME) != (new->mask & FAN_RENAME))
155 return false;
156
157 switch (old->type) {
158 case FANOTIFY_EVENT_TYPE_PATH:
159 return fanotify_path_equal(fanotify_event_path(old),
160 fanotify_event_path(new));
161 case FANOTIFY_EVENT_TYPE_FID:
162 return fanotify_fid_event_equal(FANOTIFY_FE(old),
163 FANOTIFY_FE(new));
164 case FANOTIFY_EVENT_TYPE_FID_NAME:
165 return fanotify_name_event_equal(FANOTIFY_NE(old),
166 FANOTIFY_NE(new));
167 case FANOTIFY_EVENT_TYPE_FS_ERROR:
168 return fanotify_error_event_equal(FANOTIFY_EE(old),
169 FANOTIFY_EE(new));
170 default:
171 WARN_ON_ONCE(1);
172 }
173
174 return false;
175 }
176
177 /* Limit event merges to limit CPU overhead per event */
178 #define FANOTIFY_MAX_MERGE_EVENTS 128
179
180 /* and the list better be locked by something too! */
fanotify_merge(struct fsnotify_group * group,struct fsnotify_event * event)181 static int fanotify_merge(struct fsnotify_group *group,
182 struct fsnotify_event *event)
183 {
184 struct fanotify_event *old, *new = FANOTIFY_E(event);
185 unsigned int bucket = fanotify_event_hash_bucket(group, new);
186 struct hlist_head *hlist = &group->fanotify_data.merge_hash[bucket];
187 int i = 0;
188
189 pr_debug("%s: group=%p event=%p bucket=%u\n", __func__,
190 group, event, bucket);
191
192 /*
193 * Don't merge a permission event with any other event so that we know
194 * the event structure we have created in fanotify_handle_event() is the
195 * one we should check for permission response.
196 */
197 if (fanotify_is_perm_event(new->mask))
198 return 0;
199
200 hlist_for_each_entry(old, hlist, merge_list) {
201 if (++i > FANOTIFY_MAX_MERGE_EVENTS)
202 break;
203 if (fanotify_should_merge(old, new)) {
204 old->mask |= new->mask;
205
206 if (fanotify_is_error_event(old->mask))
207 FANOTIFY_EE(old)->err_count++;
208
209 return 1;
210 }
211 }
212
213 return 0;
214 }
215
216 /*
217 * Wait for response to permission event. The function also takes care of
218 * freeing the permission event (or offloads that in case the wait is canceled
219 * by a signal). The function returns 0 in case access got allowed by userspace,
220 * -EPERM in case userspace disallowed the access, and -ERESTARTSYS in case
221 * the wait got interrupted by a signal.
222 */
fanotify_get_response(struct fsnotify_group * group,struct fanotify_perm_event * event,struct fsnotify_iter_info * iter_info)223 static int fanotify_get_response(struct fsnotify_group *group,
224 struct fanotify_perm_event *event,
225 struct fsnotify_iter_info *iter_info)
226 {
227 int ret;
228
229 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
230
231 ret = wait_event_state(group->fanotify_data.access_waitq,
232 event->state == FAN_EVENT_ANSWERED,
233 (TASK_KILLABLE|TASK_FREEZABLE));
234
235 /* Signal pending? */
236 if (ret < 0) {
237 spin_lock(&group->notification_lock);
238 /* Event reported to userspace and no answer yet? */
239 if (event->state == FAN_EVENT_REPORTED) {
240 /* Event will get freed once userspace answers to it */
241 event->state = FAN_EVENT_CANCELED;
242 spin_unlock(&group->notification_lock);
243 return ret;
244 }
245 /* Event not yet reported? Just remove it. */
246 if (event->state == FAN_EVENT_INIT) {
247 fsnotify_remove_queued_event(group, &event->fae.fse);
248 /* Permission events are not supposed to be hashed */
249 WARN_ON_ONCE(!hlist_unhashed(&event->fae.merge_list));
250 }
251 /*
252 * Event may be also answered in case signal delivery raced
253 * with wakeup. In that case we have nothing to do besides
254 * freeing the event and reporting error.
255 */
256 spin_unlock(&group->notification_lock);
257 goto out;
258 }
259
260 /* userspace responded, convert to something usable */
261 switch (event->response & FANOTIFY_RESPONSE_ACCESS) {
262 case FAN_ALLOW:
263 ret = 0;
264 break;
265 case FAN_DENY:
266 default:
267 ret = -EPERM;
268 }
269
270 /* Check if the response should be audited */
271 if (event->response & FAN_AUDIT)
272 audit_fanotify(event->response & ~FAN_AUDIT,
273 &event->audit_rule);
274
275 pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
276 group, event, ret);
277 out:
278 fsnotify_destroy_event(group, &event->fae.fse);
279
280 return ret;
281 }
282
283 /*
284 * This function returns a mask for an event that only contains the flags
285 * that have been specifically requested by the user. Flags that may have
286 * been included within the event mask, but have not been explicitly
287 * requested by the user, will not be present in the returned mask.
288 */
fanotify_group_event_mask(struct fsnotify_group * group,struct fsnotify_iter_info * iter_info,u32 * match_mask,u32 event_mask,const void * data,int data_type,struct inode * dir)289 static u32 fanotify_group_event_mask(struct fsnotify_group *group,
290 struct fsnotify_iter_info *iter_info,
291 u32 *match_mask, u32 event_mask,
292 const void *data, int data_type,
293 struct inode *dir)
294 {
295 __u32 marks_mask = 0, marks_ignore_mask = 0;
296 __u32 test_mask, user_mask = FANOTIFY_OUTGOING_EVENTS |
297 FANOTIFY_EVENT_FLAGS;
298 const struct path *path = fsnotify_data_path(data, data_type);
299 unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
300 struct fsnotify_mark *mark;
301 bool ondir = event_mask & FAN_ONDIR;
302 int type;
303
304 pr_debug("%s: report_mask=%x mask=%x data=%p data_type=%d\n",
305 __func__, iter_info->report_mask, event_mask, data, data_type);
306
307 if (!fid_mode) {
308 /* Do we have path to open a file descriptor? */
309 if (!path)
310 return 0;
311 /* Path type events are only relevant for files and dirs */
312 if (!d_is_reg(path->dentry) && !d_can_lookup(path->dentry))
313 return 0;
314 } else if (!(fid_mode & FAN_REPORT_FID)) {
315 /* Do we have a directory inode to report? */
316 if (!dir && !ondir)
317 return 0;
318 }
319
320 fsnotify_foreach_iter_mark_type(iter_info, mark, type) {
321 /*
322 * Apply ignore mask depending on event flags in ignore mask.
323 */
324 marks_ignore_mask |=
325 fsnotify_effective_ignore_mask(mark, ondir, type);
326
327 /*
328 * Send the event depending on event flags in mark mask.
329 */
330 if (!fsnotify_mask_applicable(mark->mask, ondir, type))
331 continue;
332
333 marks_mask |= mark->mask;
334
335 /* Record the mark types of this group that matched the event */
336 *match_mask |= 1U << type;
337 }
338
339 test_mask = event_mask & marks_mask & ~marks_ignore_mask;
340
341 /*
342 * For dirent modification events (create/delete/move) that do not carry
343 * the child entry name information, we report FAN_ONDIR for mkdir/rmdir
344 * so user can differentiate them from creat/unlink.
345 *
346 * For backward compatibility and consistency, do not report FAN_ONDIR
347 * to user in legacy fanotify mode (reporting fd) and report FAN_ONDIR
348 * to user in fid mode for all event types.
349 *
350 * We never report FAN_EVENT_ON_CHILD to user, but we do pass it in to
351 * fanotify_alloc_event() when group is reporting fid as indication
352 * that event happened on child.
353 */
354 if (fid_mode) {
355 /* Do not report event flags without any event */
356 if (!(test_mask & ~FANOTIFY_EVENT_FLAGS))
357 return 0;
358 } else {
359 user_mask &= ~FANOTIFY_EVENT_FLAGS;
360 }
361
362 return test_mask & user_mask;
363 }
364
365 /*
366 * Check size needed to encode fanotify_fh.
367 *
368 * Return size of encoded fh without fanotify_fh header.
369 * Return 0 on failure to encode.
370 */
fanotify_encode_fh_len(struct inode * inode)371 static int fanotify_encode_fh_len(struct inode *inode)
372 {
373 int dwords = 0;
374 int fh_len;
375
376 if (!inode)
377 return 0;
378
379 exportfs_encode_fid(inode, NULL, &dwords);
380 fh_len = dwords << 2;
381
382 /*
383 * struct fanotify_error_event might be preallocated and is
384 * limited to MAX_HANDLE_SZ. This should never happen, but
385 * safeguard by forcing an invalid file handle.
386 */
387 if (WARN_ON_ONCE(fh_len > MAX_HANDLE_SZ))
388 return 0;
389
390 return fh_len;
391 }
392
393 /*
394 * Encode fanotify_fh.
395 *
396 * Return total size of encoded fh including fanotify_fh header.
397 * Return 0 on failure to encode.
398 */
fanotify_encode_fh(struct fanotify_fh * fh,struct inode * inode,unsigned int fh_len,unsigned int * hash,gfp_t gfp)399 static int fanotify_encode_fh(struct fanotify_fh *fh, struct inode *inode,
400 unsigned int fh_len, unsigned int *hash,
401 gfp_t gfp)
402 {
403 int dwords, type = 0;
404 char *ext_buf = NULL;
405 void *buf = fh->buf;
406 int err;
407
408 fh->type = FILEID_ROOT;
409 fh->len = 0;
410 fh->flags = 0;
411
412 /*
413 * Invalid FHs are used by FAN_FS_ERROR for errors not
414 * linked to any inode. The f_handle won't be reported
415 * back to userspace.
416 */
417 if (!inode)
418 goto out;
419
420 /*
421 * !gpf means preallocated variable size fh, but fh_len could
422 * be zero in that case if encoding fh len failed.
423 */
424 err = -ENOENT;
425 if (fh_len < 4 || WARN_ON_ONCE(fh_len % 4) || fh_len > MAX_HANDLE_SZ)
426 goto out_err;
427
428 /* No external buffer in a variable size allocated fh */
429 if (gfp && fh_len > FANOTIFY_INLINE_FH_LEN) {
430 /* Treat failure to allocate fh as failure to encode fh */
431 err = -ENOMEM;
432 ext_buf = kmalloc(fh_len, gfp);
433 if (!ext_buf)
434 goto out_err;
435
436 *fanotify_fh_ext_buf_ptr(fh) = ext_buf;
437 buf = ext_buf;
438 fh->flags |= FANOTIFY_FH_FLAG_EXT_BUF;
439 }
440
441 dwords = fh_len >> 2;
442 type = exportfs_encode_fid(inode, buf, &dwords);
443 err = -EINVAL;
444 /*
445 * Unlike file_handle, type and len of struct fanotify_fh are u8.
446 * Traditionally, filesystem return handle_type < 0xff, but there
447 * is no enforecement for that in vfs.
448 */
449 BUILD_BUG_ON(MAX_HANDLE_SZ > 0xff || FILEID_INVALID > 0xff);
450 if (type <= 0 || type >= FILEID_INVALID || fh_len != dwords << 2)
451 goto out_err;
452
453 fh->type = type;
454 fh->len = fh_len;
455
456 out:
457 /*
458 * Mix fh into event merge key. Hash might be NULL in case of
459 * unhashed FID events (i.e. FAN_FS_ERROR).
460 */
461 if (hash)
462 *hash ^= fanotify_hash_fh(fh);
463
464 return FANOTIFY_FH_HDR_LEN + fh_len;
465
466 out_err:
467 pr_warn_ratelimited("fanotify: failed to encode fid (type=%d, len=%d, err=%i)\n",
468 type, fh_len, err);
469 kfree(ext_buf);
470 *fanotify_fh_ext_buf_ptr(fh) = NULL;
471 /* Report the event without a file identifier on encode error */
472 fh->type = FILEID_INVALID;
473 fh->len = 0;
474 return 0;
475 }
476
477 /*
478 * FAN_REPORT_FID is ambiguous in that it reports the fid of the child for
479 * some events and the fid of the parent for create/delete/move events.
480 *
481 * With the FAN_REPORT_TARGET_FID flag, the fid of the child is reported
482 * also in create/delete/move events in addition to the fid of the parent
483 * and the name of the child.
484 */
fanotify_report_child_fid(unsigned int fid_mode,u32 mask)485 static inline bool fanotify_report_child_fid(unsigned int fid_mode, u32 mask)
486 {
487 if (mask & ALL_FSNOTIFY_DIRENT_EVENTS)
488 return (fid_mode & FAN_REPORT_TARGET_FID);
489
490 return (fid_mode & FAN_REPORT_FID) && !(mask & FAN_ONDIR);
491 }
492
493 /*
494 * The inode to use as identifier when reporting fid depends on the event
495 * and the group flags.
496 *
497 * With the group flag FAN_REPORT_TARGET_FID, always report the child fid.
498 *
499 * Without the group flag FAN_REPORT_TARGET_FID, report the modified directory
500 * fid on dirent events and the child fid otherwise.
501 *
502 * For example:
503 * FS_ATTRIB reports the child fid even if reported on a watched parent.
504 * FS_CREATE reports the modified dir fid without FAN_REPORT_TARGET_FID.
505 * and reports the created child fid with FAN_REPORT_TARGET_FID.
506 */
fanotify_fid_inode(u32 event_mask,const void * data,int data_type,struct inode * dir,unsigned int fid_mode)507 static struct inode *fanotify_fid_inode(u32 event_mask, const void *data,
508 int data_type, struct inode *dir,
509 unsigned int fid_mode)
510 {
511 if ((event_mask & ALL_FSNOTIFY_DIRENT_EVENTS) &&
512 !(fid_mode & FAN_REPORT_TARGET_FID))
513 return dir;
514
515 return fsnotify_data_inode(data, data_type);
516 }
517
518 /*
519 * The inode to use as identifier when reporting dir fid depends on the event.
520 * Report the modified directory inode on dirent modification events.
521 * Report the "victim" inode if "victim" is a directory.
522 * Report the parent inode if "victim" is not a directory and event is
523 * reported to parent.
524 * Otherwise, do not report dir fid.
525 */
fanotify_dfid_inode(u32 event_mask,const void * data,int data_type,struct inode * dir)526 static struct inode *fanotify_dfid_inode(u32 event_mask, const void *data,
527 int data_type, struct inode *dir)
528 {
529 struct inode *inode = fsnotify_data_inode(data, data_type);
530
531 if (event_mask & ALL_FSNOTIFY_DIRENT_EVENTS)
532 return dir;
533
534 if (inode && S_ISDIR(inode->i_mode))
535 return inode;
536
537 return dir;
538 }
539
fanotify_alloc_path_event(const struct path * path,unsigned int * hash,gfp_t gfp)540 static struct fanotify_event *fanotify_alloc_path_event(const struct path *path,
541 unsigned int *hash,
542 gfp_t gfp)
543 {
544 struct fanotify_path_event *pevent;
545
546 pevent = kmem_cache_alloc(fanotify_path_event_cachep, gfp);
547 if (!pevent)
548 return NULL;
549
550 pevent->fae.type = FANOTIFY_EVENT_TYPE_PATH;
551 pevent->path = *path;
552 *hash ^= fanotify_hash_path(path);
553 path_get(path);
554
555 return &pevent->fae;
556 }
557
fanotify_alloc_perm_event(const struct path * path,gfp_t gfp)558 static struct fanotify_event *fanotify_alloc_perm_event(const struct path *path,
559 gfp_t gfp)
560 {
561 struct fanotify_perm_event *pevent;
562
563 pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp);
564 if (!pevent)
565 return NULL;
566
567 pevent->fae.type = FANOTIFY_EVENT_TYPE_PATH_PERM;
568 pevent->response = 0;
569 pevent->hdr.type = FAN_RESPONSE_INFO_NONE;
570 pevent->hdr.pad = 0;
571 pevent->hdr.len = 0;
572 pevent->state = FAN_EVENT_INIT;
573 pevent->path = *path;
574 path_get(path);
575
576 return &pevent->fae;
577 }
578
fanotify_alloc_fid_event(struct inode * id,__kernel_fsid_t * fsid,unsigned int * hash,gfp_t gfp)579 static struct fanotify_event *fanotify_alloc_fid_event(struct inode *id,
580 __kernel_fsid_t *fsid,
581 unsigned int *hash,
582 gfp_t gfp)
583 {
584 struct fanotify_fid_event *ffe;
585
586 ffe = kmem_cache_alloc(fanotify_fid_event_cachep, gfp);
587 if (!ffe)
588 return NULL;
589
590 ffe->fae.type = FANOTIFY_EVENT_TYPE_FID;
591 ffe->fsid = *fsid;
592 *hash ^= fanotify_hash_fsid(fsid);
593 fanotify_encode_fh(&ffe->object_fh, id, fanotify_encode_fh_len(id),
594 hash, gfp);
595
596 return &ffe->fae;
597 }
598
fanotify_alloc_name_event(struct inode * dir,__kernel_fsid_t * fsid,const struct qstr * name,struct inode * child,struct dentry * moved,unsigned int * hash,gfp_t gfp)599 static struct fanotify_event *fanotify_alloc_name_event(struct inode *dir,
600 __kernel_fsid_t *fsid,
601 const struct qstr *name,
602 struct inode *child,
603 struct dentry *moved,
604 unsigned int *hash,
605 gfp_t gfp)
606 {
607 struct fanotify_name_event *fne;
608 struct fanotify_info *info;
609 struct fanotify_fh *dfh, *ffh;
610 struct inode *dir2 = moved ? d_inode(moved->d_parent) : NULL;
611 const struct qstr *name2 = moved ? &moved->d_name : NULL;
612 unsigned int dir_fh_len = fanotify_encode_fh_len(dir);
613 unsigned int dir2_fh_len = fanotify_encode_fh_len(dir2);
614 unsigned int child_fh_len = fanotify_encode_fh_len(child);
615 unsigned long name_len = name ? name->len : 0;
616 unsigned long name2_len = name2 ? name2->len : 0;
617 unsigned int len, size;
618
619 /* Reserve terminating null byte even for empty name */
620 size = sizeof(*fne) + name_len + name2_len + 2;
621 if (dir_fh_len)
622 size += FANOTIFY_FH_HDR_LEN + dir_fh_len;
623 if (dir2_fh_len)
624 size += FANOTIFY_FH_HDR_LEN + dir2_fh_len;
625 if (child_fh_len)
626 size += FANOTIFY_FH_HDR_LEN + child_fh_len;
627 fne = kmalloc(size, gfp);
628 if (!fne)
629 return NULL;
630
631 fne->fae.type = FANOTIFY_EVENT_TYPE_FID_NAME;
632 fne->fsid = *fsid;
633 *hash ^= fanotify_hash_fsid(fsid);
634 info = &fne->info;
635 fanotify_info_init(info);
636 if (dir_fh_len) {
637 dfh = fanotify_info_dir_fh(info);
638 len = fanotify_encode_fh(dfh, dir, dir_fh_len, hash, 0);
639 fanotify_info_set_dir_fh(info, len);
640 }
641 if (dir2_fh_len) {
642 dfh = fanotify_info_dir2_fh(info);
643 len = fanotify_encode_fh(dfh, dir2, dir2_fh_len, hash, 0);
644 fanotify_info_set_dir2_fh(info, len);
645 }
646 if (child_fh_len) {
647 ffh = fanotify_info_file_fh(info);
648 len = fanotify_encode_fh(ffh, child, child_fh_len, hash, 0);
649 fanotify_info_set_file_fh(info, len);
650 }
651 if (name_len) {
652 fanotify_info_copy_name(info, name);
653 *hash ^= full_name_hash((void *)name_len, name->name, name_len);
654 }
655 if (name2_len) {
656 fanotify_info_copy_name2(info, name2);
657 *hash ^= full_name_hash((void *)name2_len, name2->name,
658 name2_len);
659 }
660
661 pr_debug("%s: size=%u dir_fh_len=%u child_fh_len=%u name_len=%u name='%.*s'\n",
662 __func__, size, dir_fh_len, child_fh_len,
663 info->name_len, info->name_len, fanotify_info_name(info));
664
665 if (dir2_fh_len) {
666 pr_debug("%s: dir2_fh_len=%u name2_len=%u name2='%.*s'\n",
667 __func__, dir2_fh_len, info->name2_len,
668 info->name2_len, fanotify_info_name2(info));
669 }
670
671 return &fne->fae;
672 }
673
fanotify_alloc_error_event(struct fsnotify_group * group,__kernel_fsid_t * fsid,const void * data,int data_type,unsigned int * hash)674 static struct fanotify_event *fanotify_alloc_error_event(
675 struct fsnotify_group *group,
676 __kernel_fsid_t *fsid,
677 const void *data, int data_type,
678 unsigned int *hash)
679 {
680 struct fs_error_report *report =
681 fsnotify_data_error_report(data, data_type);
682 struct inode *inode;
683 struct fanotify_error_event *fee;
684 int fh_len;
685
686 if (WARN_ON_ONCE(!report))
687 return NULL;
688
689 fee = mempool_alloc(&group->fanotify_data.error_events_pool, GFP_NOFS);
690 if (!fee)
691 return NULL;
692
693 fee->fae.type = FANOTIFY_EVENT_TYPE_FS_ERROR;
694 fee->error = report->error;
695 fee->err_count = 1;
696 fee->fsid = *fsid;
697
698 inode = report->inode;
699 fh_len = fanotify_encode_fh_len(inode);
700
701 /* Bad fh_len. Fallback to using an invalid fh. Should never happen. */
702 if (!fh_len && inode)
703 inode = NULL;
704
705 fanotify_encode_fh(&fee->object_fh, inode, fh_len, NULL, 0);
706
707 *hash ^= fanotify_hash_fsid(fsid);
708
709 return &fee->fae;
710 }
711
fanotify_alloc_event(struct fsnotify_group * group,u32 mask,const void * data,int data_type,struct inode * dir,const struct qstr * file_name,__kernel_fsid_t * fsid,u32 match_mask)712 static struct fanotify_event *fanotify_alloc_event(
713 struct fsnotify_group *group,
714 u32 mask, const void *data, int data_type,
715 struct inode *dir, const struct qstr *file_name,
716 __kernel_fsid_t *fsid, u32 match_mask)
717 {
718 struct fanotify_event *event = NULL;
719 gfp_t gfp = GFP_KERNEL_ACCOUNT;
720 unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
721 struct inode *id = fanotify_fid_inode(mask, data, data_type, dir,
722 fid_mode);
723 struct inode *dirid = fanotify_dfid_inode(mask, data, data_type, dir);
724 const struct path *path = fsnotify_data_path(data, data_type);
725 struct mem_cgroup *old_memcg;
726 struct dentry *moved = NULL;
727 struct inode *child = NULL;
728 bool name_event = false;
729 unsigned int hash = 0;
730 bool ondir = mask & FAN_ONDIR;
731 struct pid *pid;
732
733 if ((fid_mode & FAN_REPORT_DIR_FID) && dirid) {
734 /*
735 * For certain events and group flags, report the child fid
736 * in addition to reporting the parent fid and maybe child name.
737 */
738 if (fanotify_report_child_fid(fid_mode, mask) && id != dirid)
739 child = id;
740
741 id = dirid;
742
743 /*
744 * We record file name only in a group with FAN_REPORT_NAME
745 * and when we have a directory inode to report.
746 *
747 * For directory entry modification event, we record the fid of
748 * the directory and the name of the modified entry.
749 *
750 * For event on non-directory that is reported to parent, we
751 * record the fid of the parent and the name of the child.
752 *
753 * Even if not reporting name, we need a variable length
754 * fanotify_name_event if reporting both parent and child fids.
755 */
756 if (!(fid_mode & FAN_REPORT_NAME)) {
757 name_event = !!child;
758 file_name = NULL;
759 } else if ((mask & ALL_FSNOTIFY_DIRENT_EVENTS) || !ondir) {
760 name_event = true;
761 }
762
763 /*
764 * In the special case of FAN_RENAME event, use the match_mask
765 * to determine if we need to report only the old parent+name,
766 * only the new parent+name or both.
767 * 'dirid' and 'file_name' are the old parent+name and
768 * 'moved' has the new parent+name.
769 */
770 if (mask & FAN_RENAME) {
771 bool report_old, report_new;
772
773 if (WARN_ON_ONCE(!match_mask))
774 return NULL;
775
776 /* Report both old and new parent+name if sb watching */
777 report_old = report_new =
778 match_mask & (1U << FSNOTIFY_ITER_TYPE_SB);
779 report_old |=
780 match_mask & (1U << FSNOTIFY_ITER_TYPE_INODE);
781 report_new |=
782 match_mask & (1U << FSNOTIFY_ITER_TYPE_INODE2);
783
784 if (!report_old) {
785 /* Do not report old parent+name */
786 dirid = NULL;
787 file_name = NULL;
788 }
789 if (report_new) {
790 /* Report new parent+name */
791 moved = fsnotify_data_dentry(data, data_type);
792 }
793 }
794 }
795
796 /*
797 * For queues with unlimited length lost events are not expected and
798 * can possibly have security implications. Avoid losing events when
799 * memory is short. For the limited size queues, avoid OOM killer in the
800 * target monitoring memcg as it may have security repercussion.
801 */
802 if (group->max_events == UINT_MAX)
803 gfp |= __GFP_NOFAIL;
804 else
805 gfp |= __GFP_RETRY_MAYFAIL;
806
807 /* Whoever is interested in the event, pays for the allocation. */
808 old_memcg = set_active_memcg(group->memcg);
809
810 if (fanotify_is_perm_event(mask)) {
811 event = fanotify_alloc_perm_event(path, gfp);
812 } else if (fanotify_is_error_event(mask)) {
813 event = fanotify_alloc_error_event(group, fsid, data,
814 data_type, &hash);
815 } else if (name_event && (file_name || moved || child)) {
816 event = fanotify_alloc_name_event(dirid, fsid, file_name, child,
817 moved, &hash, gfp);
818 } else if (fid_mode) {
819 event = fanotify_alloc_fid_event(id, fsid, &hash, gfp);
820 } else {
821 event = fanotify_alloc_path_event(path, &hash, gfp);
822 }
823
824 if (!event)
825 goto out;
826
827 if (FAN_GROUP_FLAG(group, FAN_REPORT_TID))
828 pid = get_pid(task_pid(current));
829 else
830 pid = get_pid(task_tgid(current));
831
832 /* Mix event info, FAN_ONDIR flag and pid into event merge key */
833 hash ^= hash_long((unsigned long)pid | ondir, FANOTIFY_EVENT_HASH_BITS);
834 fanotify_init_event(event, hash, mask);
835 event->pid = pid;
836
837 out:
838 set_active_memcg(old_memcg);
839 return event;
840 }
841
842 /*
843 * Get cached fsid of the filesystem containing the object from any mark.
844 * All marks are supposed to have the same fsid, but we do not verify that here.
845 */
fanotify_get_fsid(struct fsnotify_iter_info * iter_info)846 static __kernel_fsid_t fanotify_get_fsid(struct fsnotify_iter_info *iter_info)
847 {
848 struct fsnotify_mark *mark;
849 int type;
850 __kernel_fsid_t fsid = {};
851
852 fsnotify_foreach_iter_mark_type(iter_info, mark, type) {
853 if (!(mark->flags & FSNOTIFY_MARK_FLAG_HAS_FSID))
854 continue;
855 fsid = FANOTIFY_MARK(mark)->fsid;
856 if (!(mark->flags & FSNOTIFY_MARK_FLAG_WEAK_FSID) &&
857 WARN_ON_ONCE(!fsid.val[0] && !fsid.val[1]))
858 continue;
859 return fsid;
860 }
861
862 return fsid;
863 }
864
865 /*
866 * Add an event to hash table for faster merge.
867 */
fanotify_insert_event(struct fsnotify_group * group,struct fsnotify_event * fsn_event)868 static void fanotify_insert_event(struct fsnotify_group *group,
869 struct fsnotify_event *fsn_event)
870 {
871 struct fanotify_event *event = FANOTIFY_E(fsn_event);
872 unsigned int bucket = fanotify_event_hash_bucket(group, event);
873 struct hlist_head *hlist = &group->fanotify_data.merge_hash[bucket];
874
875 assert_spin_locked(&group->notification_lock);
876
877 if (!fanotify_is_hashed_event(event->mask))
878 return;
879
880 pr_debug("%s: group=%p event=%p bucket=%u\n", __func__,
881 group, event, bucket);
882
883 hlist_add_head(&event->merge_list, hlist);
884 }
885
fanotify_handle_event(struct fsnotify_group * group,u32 mask,const void * data,int data_type,struct inode * dir,const struct qstr * file_name,u32 cookie,struct fsnotify_iter_info * iter_info)886 static int fanotify_handle_event(struct fsnotify_group *group, u32 mask,
887 const void *data, int data_type,
888 struct inode *dir,
889 const struct qstr *file_name, u32 cookie,
890 struct fsnotify_iter_info *iter_info)
891 {
892 int ret = 0;
893 struct fanotify_event *event;
894 struct fsnotify_event *fsn_event;
895 __kernel_fsid_t fsid = {};
896 u32 match_mask = 0;
897
898 BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
899 BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
900 BUILD_BUG_ON(FAN_ATTRIB != FS_ATTRIB);
901 BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
902 BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
903 BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
904 BUILD_BUG_ON(FAN_MOVED_TO != FS_MOVED_TO);
905 BUILD_BUG_ON(FAN_MOVED_FROM != FS_MOVED_FROM);
906 BUILD_BUG_ON(FAN_CREATE != FS_CREATE);
907 BUILD_BUG_ON(FAN_DELETE != FS_DELETE);
908 BUILD_BUG_ON(FAN_DELETE_SELF != FS_DELETE_SELF);
909 BUILD_BUG_ON(FAN_MOVE_SELF != FS_MOVE_SELF);
910 BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
911 BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
912 BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
913 BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
914 BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR);
915 BUILD_BUG_ON(FAN_OPEN_EXEC != FS_OPEN_EXEC);
916 BUILD_BUG_ON(FAN_OPEN_EXEC_PERM != FS_OPEN_EXEC_PERM);
917 BUILD_BUG_ON(FAN_FS_ERROR != FS_ERROR);
918 BUILD_BUG_ON(FAN_RENAME != FS_RENAME);
919
920 BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 21);
921
922 mask = fanotify_group_event_mask(group, iter_info, &match_mask,
923 mask, data, data_type, dir);
924 if (!mask)
925 return 0;
926
927 pr_debug("%s: group=%p mask=%x report_mask=%x\n", __func__,
928 group, mask, match_mask);
929
930 if (fanotify_is_perm_event(mask)) {
931 /*
932 * fsnotify_prepare_user_wait() fails if we race with mark
933 * deletion. Just let the operation pass in that case.
934 */
935 if (!fsnotify_prepare_user_wait(iter_info))
936 return 0;
937 }
938
939 if (FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS))
940 fsid = fanotify_get_fsid(iter_info);
941
942 event = fanotify_alloc_event(group, mask, data, data_type, dir,
943 file_name, &fsid, match_mask);
944 ret = -ENOMEM;
945 if (unlikely(!event)) {
946 /*
947 * We don't queue overflow events for permission events as
948 * there the access is denied and so no event is in fact lost.
949 */
950 if (!fanotify_is_perm_event(mask))
951 fsnotify_queue_overflow(group);
952 goto finish;
953 }
954
955 fsn_event = &event->fse;
956 ret = fsnotify_insert_event(group, fsn_event, fanotify_merge,
957 fanotify_insert_event);
958 if (ret) {
959 /* Permission events shouldn't be merged */
960 BUG_ON(ret == 1 && mask & FANOTIFY_PERM_EVENTS);
961 /* Our event wasn't used in the end. Free it. */
962 fsnotify_destroy_event(group, fsn_event);
963
964 ret = 0;
965 } else if (fanotify_is_perm_event(mask)) {
966 ret = fanotify_get_response(group, FANOTIFY_PERM(event),
967 iter_info);
968 }
969 finish:
970 if (fanotify_is_perm_event(mask))
971 fsnotify_finish_user_wait(iter_info);
972
973 return ret;
974 }
975
fanotify_free_group_priv(struct fsnotify_group * group)976 static void fanotify_free_group_priv(struct fsnotify_group *group)
977 {
978 kfree(group->fanotify_data.merge_hash);
979 if (group->fanotify_data.ucounts)
980 dec_ucount(group->fanotify_data.ucounts,
981 UCOUNT_FANOTIFY_GROUPS);
982
983 if (mempool_initialized(&group->fanotify_data.error_events_pool))
984 mempool_exit(&group->fanotify_data.error_events_pool);
985 }
986
fanotify_free_path_event(struct fanotify_event * event)987 static void fanotify_free_path_event(struct fanotify_event *event)
988 {
989 path_put(fanotify_event_path(event));
990 kmem_cache_free(fanotify_path_event_cachep, FANOTIFY_PE(event));
991 }
992
fanotify_free_perm_event(struct fanotify_event * event)993 static void fanotify_free_perm_event(struct fanotify_event *event)
994 {
995 path_put(fanotify_event_path(event));
996 kmem_cache_free(fanotify_perm_event_cachep, FANOTIFY_PERM(event));
997 }
998
fanotify_free_fid_event(struct fanotify_event * event)999 static void fanotify_free_fid_event(struct fanotify_event *event)
1000 {
1001 struct fanotify_fid_event *ffe = FANOTIFY_FE(event);
1002
1003 if (fanotify_fh_has_ext_buf(&ffe->object_fh))
1004 kfree(fanotify_fh_ext_buf(&ffe->object_fh));
1005 kmem_cache_free(fanotify_fid_event_cachep, ffe);
1006 }
1007
fanotify_free_name_event(struct fanotify_event * event)1008 static void fanotify_free_name_event(struct fanotify_event *event)
1009 {
1010 kfree(FANOTIFY_NE(event));
1011 }
1012
fanotify_free_error_event(struct fsnotify_group * group,struct fanotify_event * event)1013 static void fanotify_free_error_event(struct fsnotify_group *group,
1014 struct fanotify_event *event)
1015 {
1016 struct fanotify_error_event *fee = FANOTIFY_EE(event);
1017
1018 mempool_free(fee, &group->fanotify_data.error_events_pool);
1019 }
1020
fanotify_free_event(struct fsnotify_group * group,struct fsnotify_event * fsn_event)1021 static void fanotify_free_event(struct fsnotify_group *group,
1022 struct fsnotify_event *fsn_event)
1023 {
1024 struct fanotify_event *event;
1025
1026 event = FANOTIFY_E(fsn_event);
1027 put_pid(event->pid);
1028 switch (event->type) {
1029 case FANOTIFY_EVENT_TYPE_PATH:
1030 fanotify_free_path_event(event);
1031 break;
1032 case FANOTIFY_EVENT_TYPE_PATH_PERM:
1033 fanotify_free_perm_event(event);
1034 break;
1035 case FANOTIFY_EVENT_TYPE_FID:
1036 fanotify_free_fid_event(event);
1037 break;
1038 case FANOTIFY_EVENT_TYPE_FID_NAME:
1039 fanotify_free_name_event(event);
1040 break;
1041 case FANOTIFY_EVENT_TYPE_OVERFLOW:
1042 kfree(event);
1043 break;
1044 case FANOTIFY_EVENT_TYPE_FS_ERROR:
1045 fanotify_free_error_event(group, event);
1046 break;
1047 default:
1048 WARN_ON_ONCE(1);
1049 }
1050 }
1051
fanotify_freeing_mark(struct fsnotify_mark * mark,struct fsnotify_group * group)1052 static void fanotify_freeing_mark(struct fsnotify_mark *mark,
1053 struct fsnotify_group *group)
1054 {
1055 if (!FAN_GROUP_FLAG(group, FAN_UNLIMITED_MARKS))
1056 dec_ucount(group->fanotify_data.ucounts, UCOUNT_FANOTIFY_MARKS);
1057 }
1058
fanotify_free_mark(struct fsnotify_mark * fsn_mark)1059 static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
1060 {
1061 kmem_cache_free(fanotify_mark_cache, FANOTIFY_MARK(fsn_mark));
1062 }
1063
1064 const struct fsnotify_ops fanotify_fsnotify_ops = {
1065 .handle_event = fanotify_handle_event,
1066 .free_group_priv = fanotify_free_group_priv,
1067 .free_event = fanotify_free_event,
1068 .freeing_mark = fanotify_freeing_mark,
1069 .free_mark = fanotify_free_mark,
1070 };
1071