1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/fanotify.h> 3 #include <linux/fcntl.h> 4 #include <linux/file.h> 5 #include <linux/fs.h> 6 #include <linux/anon_inodes.h> 7 #include <linux/fsnotify_backend.h> 8 #include <linux/init.h> 9 #include <linux/mount.h> 10 #include <linux/namei.h> 11 #include <linux/poll.h> 12 #include <linux/security.h> 13 #include <linux/syscalls.h> 14 #include <linux/slab.h> 15 #include <linux/types.h> 16 #include <linux/uaccess.h> 17 #include <linux/compat.h> 18 #include <linux/sched/signal.h> 19 #include <linux/memcontrol.h> 20 #include <linux/statfs.h> 21 #include <linux/exportfs.h> 22 23 #include <asm/ioctls.h> 24 25 #include "../fsnotify.h" 26 #include "../fdinfo.h" 27 #include "fanotify.h" 28 29 #define FANOTIFY_DEFAULT_MAX_EVENTS 16384 30 #define FANOTIFY_OLD_DEFAULT_MAX_MARKS 8192 31 #define FANOTIFY_DEFAULT_MAX_GROUPS 128 32 #define FANOTIFY_DEFAULT_FEE_POOL_SIZE 32 33 34 /* 35 * Legacy fanotify marks limits (8192) is per group and we introduced a tunable 36 * limit of marks per user, similar to inotify. Effectively, the legacy limit 37 * of fanotify marks per user is <max marks per group> * <max groups per user>. 38 * This default limit (1M) also happens to match the increased limit of inotify 39 * max_user_watches since v5.10. 40 */ 41 #define FANOTIFY_DEFAULT_MAX_USER_MARKS \ 42 (FANOTIFY_OLD_DEFAULT_MAX_MARKS * FANOTIFY_DEFAULT_MAX_GROUPS) 43 44 /* 45 * Most of the memory cost of adding an inode mark is pinning the marked inode. 46 * The size of the filesystem inode struct is not uniform across filesystems, 47 * so double the size of a VFS inode is used as a conservative approximation. 48 */ 49 #define INODE_MARK_COST (2 * sizeof(struct inode)) 50 51 /* configurable via /proc/sys/fs/fanotify/ */ 52 static int fanotify_max_queued_events __read_mostly; 53 54 #ifdef CONFIG_SYSCTL 55 56 #include <linux/sysctl.h> 57 58 static long ft_zero = 0; 59 static long ft_int_max = INT_MAX; 60 61 static struct ctl_table fanotify_table[] = { 62 { 63 .procname = "max_user_groups", 64 .data = &init_user_ns.ucount_max[UCOUNT_FANOTIFY_GROUPS], 65 .maxlen = sizeof(long), 66 .mode = 0644, 67 .proc_handler = proc_doulongvec_minmax, 68 .extra1 = &ft_zero, 69 .extra2 = &ft_int_max, 70 }, 71 { 72 .procname = "max_user_marks", 73 .data = &init_user_ns.ucount_max[UCOUNT_FANOTIFY_MARKS], 74 .maxlen = sizeof(long), 75 .mode = 0644, 76 .proc_handler = proc_doulongvec_minmax, 77 .extra1 = &ft_zero, 78 .extra2 = &ft_int_max, 79 }, 80 { 81 .procname = "max_queued_events", 82 .data = &fanotify_max_queued_events, 83 .maxlen = sizeof(int), 84 .mode = 0644, 85 .proc_handler = proc_dointvec_minmax, 86 .extra1 = SYSCTL_ZERO 87 }, 88 }; 89 90 static void __init fanotify_sysctls_init(void) 91 { 92 register_sysctl("fs/fanotify", fanotify_table); 93 } 94 #else 95 #define fanotify_sysctls_init() do { } while (0) 96 #endif /* CONFIG_SYSCTL */ 97 98 /* 99 * All flags that may be specified in parameter event_f_flags of fanotify_init. 100 * 101 * Internal and external open flags are stored together in field f_flags of 102 * struct file. Only external open flags shall be allowed in event_f_flags. 103 * Internal flags like FMODE_EXEC shall be excluded. 104 */ 105 #define FANOTIFY_INIT_ALL_EVENT_F_BITS ( \ 106 O_ACCMODE | O_APPEND | O_NONBLOCK | \ 107 __O_SYNC | O_DSYNC | O_CLOEXEC | \ 108 O_LARGEFILE | O_NOATIME ) 109 110 extern const struct fsnotify_ops fanotify_fsnotify_ops; 111 112 struct kmem_cache *fanotify_mark_cache __ro_after_init; 113 struct kmem_cache *fanotify_fid_event_cachep __ro_after_init; 114 struct kmem_cache *fanotify_path_event_cachep __ro_after_init; 115 struct kmem_cache *fanotify_perm_event_cachep __ro_after_init; 116 117 #define FANOTIFY_EVENT_ALIGN 4 118 #define FANOTIFY_FID_INFO_HDR_LEN \ 119 (sizeof(struct fanotify_event_info_fid) + sizeof(struct file_handle)) 120 #define FANOTIFY_PIDFD_INFO_LEN \ 121 sizeof(struct fanotify_event_info_pidfd) 122 #define FANOTIFY_ERROR_INFO_LEN \ 123 (sizeof(struct fanotify_event_info_error)) 124 #define FANOTIFY_RANGE_INFO_LEN \ 125 (sizeof(struct fanotify_event_info_range)) 126 127 static int fanotify_fid_info_len(int fh_len, int name_len) 128 { 129 int info_len = fh_len; 130 131 if (name_len) 132 info_len += name_len + 1; 133 134 return roundup(FANOTIFY_FID_INFO_HDR_LEN + info_len, 135 FANOTIFY_EVENT_ALIGN); 136 } 137 138 /* FAN_RENAME may have one or two dir+name info records */ 139 static int fanotify_dir_name_info_len(struct fanotify_event *event) 140 { 141 struct fanotify_info *info = fanotify_event_info(event); 142 int dir_fh_len = fanotify_event_dir_fh_len(event); 143 int dir2_fh_len = fanotify_event_dir2_fh_len(event); 144 int info_len = 0; 145 146 if (dir_fh_len) 147 info_len += fanotify_fid_info_len(dir_fh_len, 148 info->name_len); 149 if (dir2_fh_len) 150 info_len += fanotify_fid_info_len(dir2_fh_len, 151 info->name2_len); 152 153 return info_len; 154 } 155 156 static size_t fanotify_event_len(unsigned int info_mode, 157 struct fanotify_event *event) 158 { 159 size_t event_len = FAN_EVENT_METADATA_LEN; 160 int fh_len; 161 int dot_len = 0; 162 163 if (fanotify_is_error_event(event->mask)) 164 event_len += FANOTIFY_ERROR_INFO_LEN; 165 166 if (fanotify_event_has_any_dir_fh(event)) { 167 event_len += fanotify_dir_name_info_len(event); 168 } else if ((info_mode & FAN_REPORT_NAME) && 169 (event->mask & FAN_ONDIR)) { 170 /* 171 * With group flag FAN_REPORT_NAME, if name was not recorded in 172 * event on a directory, we will report the name ".". 173 */ 174 dot_len = 1; 175 } 176 177 if (fanotify_event_has_object_fh(event)) { 178 fh_len = fanotify_event_object_fh_len(event); 179 event_len += fanotify_fid_info_len(fh_len, dot_len); 180 } 181 182 if (info_mode & FAN_REPORT_PIDFD) 183 event_len += FANOTIFY_PIDFD_INFO_LEN; 184 185 if (fanotify_event_has_access_range(event)) 186 event_len += FANOTIFY_RANGE_INFO_LEN; 187 188 return event_len; 189 } 190 191 /* 192 * Remove an hashed event from merge hash table. 193 */ 194 static void fanotify_unhash_event(struct fsnotify_group *group, 195 struct fanotify_event *event) 196 { 197 assert_spin_locked(&group->notification_lock); 198 199 pr_debug("%s: group=%p event=%p bucket=%u\n", __func__, 200 group, event, fanotify_event_hash_bucket(group, event)); 201 202 if (WARN_ON_ONCE(hlist_unhashed(&event->merge_list))) 203 return; 204 205 hlist_del_init(&event->merge_list); 206 } 207 208 /* 209 * Get an fanotify notification event if one exists and is small 210 * enough to fit in "count". Return an error pointer if the count 211 * is not large enough. When permission event is dequeued, its state is 212 * updated accordingly. 213 */ 214 static struct fanotify_event *get_one_event(struct fsnotify_group *group, 215 size_t count) 216 { 217 size_t event_size; 218 struct fanotify_event *event = NULL; 219 struct fsnotify_event *fsn_event; 220 unsigned int info_mode = FAN_GROUP_FLAG(group, FANOTIFY_INFO_MODES); 221 222 pr_debug("%s: group=%p count=%zd\n", __func__, group, count); 223 224 spin_lock(&group->notification_lock); 225 fsn_event = fsnotify_peek_first_event(group); 226 if (!fsn_event) 227 goto out; 228 229 event = FANOTIFY_E(fsn_event); 230 event_size = fanotify_event_len(info_mode, event); 231 232 if (event_size > count) { 233 event = ERR_PTR(-EINVAL); 234 goto out; 235 } 236 237 /* 238 * Held the notification_lock the whole time, so this is the 239 * same event we peeked above. 240 */ 241 fsnotify_remove_first_event(group); 242 if (fanotify_is_perm_event(event->mask)) 243 FANOTIFY_PERM(event)->state = FAN_EVENT_REPORTED; 244 if (fanotify_is_hashed_event(event->mask)) 245 fanotify_unhash_event(group, event); 246 out: 247 spin_unlock(&group->notification_lock); 248 return event; 249 } 250 251 static int create_fd(struct fsnotify_group *group, const struct path *path, 252 struct file **file) 253 { 254 int client_fd; 255 struct file *new_file; 256 257 client_fd = get_unused_fd_flags(group->fanotify_data.f_flags); 258 if (client_fd < 0) 259 return client_fd; 260 261 /* 262 * We provide an fd for the userspace program, so it could access the 263 * file without generating fanotify events itself. 264 */ 265 new_file = dentry_open_nonotify(path, group->fanotify_data.f_flags, 266 current_cred()); 267 if (IS_ERR(new_file)) { 268 put_unused_fd(client_fd); 269 client_fd = PTR_ERR(new_file); 270 } else { 271 *file = new_file; 272 } 273 274 return client_fd; 275 } 276 277 static int process_access_response_info(const char __user *info, 278 size_t info_len, 279 struct fanotify_response_info_audit_rule *friar) 280 { 281 if (info_len != sizeof(*friar)) 282 return -EINVAL; 283 284 if (copy_from_user(friar, info, sizeof(*friar))) 285 return -EFAULT; 286 287 if (friar->hdr.type != FAN_RESPONSE_INFO_AUDIT_RULE) 288 return -EINVAL; 289 if (friar->hdr.pad != 0) 290 return -EINVAL; 291 if (friar->hdr.len != sizeof(*friar)) 292 return -EINVAL; 293 294 return info_len; 295 } 296 297 /* 298 * Finish processing of permission event by setting it to ANSWERED state and 299 * drop group->notification_lock. 300 */ 301 static void finish_permission_event(struct fsnotify_group *group, 302 struct fanotify_perm_event *event, u32 response, 303 struct fanotify_response_info_audit_rule *friar) 304 __releases(&group->notification_lock) 305 { 306 bool destroy = false; 307 308 assert_spin_locked(&group->notification_lock); 309 event->response = response & ~FAN_INFO; 310 if (response & FAN_INFO) 311 memcpy(&event->audit_rule, friar, sizeof(*friar)); 312 313 if (event->state == FAN_EVENT_CANCELED) 314 destroy = true; 315 else 316 event->state = FAN_EVENT_ANSWERED; 317 spin_unlock(&group->notification_lock); 318 if (destroy) 319 fsnotify_destroy_event(group, &event->fae.fse); 320 } 321 322 static int process_access_response(struct fsnotify_group *group, 323 struct fanotify_response *response_struct, 324 const char __user *info, 325 size_t info_len) 326 { 327 struct fanotify_perm_event *event; 328 int fd = response_struct->fd; 329 u32 response = response_struct->response; 330 int errno = fanotify_get_response_errno(response); 331 int ret = info_len; 332 struct fanotify_response_info_audit_rule friar; 333 334 pr_debug("%s: group=%p fd=%d response=%x errno=%d buf=%p size=%zu\n", 335 __func__, group, fd, response, errno, info, info_len); 336 /* 337 * make sure the response is valid, if invalid we do nothing and either 338 * userspace can send a valid response or we will clean it up after the 339 * timeout 340 */ 341 if (response & ~FANOTIFY_RESPONSE_VALID_MASK) 342 return -EINVAL; 343 344 switch (response & FANOTIFY_RESPONSE_ACCESS) { 345 case FAN_ALLOW: 346 if (errno) 347 return -EINVAL; 348 break; 349 case FAN_DENY: 350 /* Custom errno is supported only for pre-content groups */ 351 if (errno && group->priority != FSNOTIFY_PRIO_PRE_CONTENT) 352 return -EINVAL; 353 354 /* 355 * Limit errno to values expected on open(2)/read(2)/write(2) 356 * of regular files. 357 */ 358 switch (errno) { 359 case 0: 360 case EIO: 361 case EPERM: 362 case EBUSY: 363 case ETXTBSY: 364 case EAGAIN: 365 case ENOSPC: 366 case EDQUOT: 367 break; 368 default: 369 return -EINVAL; 370 } 371 break; 372 default: 373 return -EINVAL; 374 } 375 376 if ((response & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT)) 377 return -EINVAL; 378 379 if (response & FAN_INFO) { 380 ret = process_access_response_info(info, info_len, &friar); 381 if (ret < 0) 382 return ret; 383 if (fd == FAN_NOFD) 384 return ret; 385 } else { 386 ret = 0; 387 } 388 389 if (fd < 0) 390 return -EINVAL; 391 392 spin_lock(&group->notification_lock); 393 list_for_each_entry(event, &group->fanotify_data.access_list, 394 fae.fse.list) { 395 if (event->fd != fd) 396 continue; 397 398 list_del_init(&event->fae.fse.list); 399 finish_permission_event(group, event, response, &friar); 400 wake_up(&group->fanotify_data.access_waitq); 401 return ret; 402 } 403 spin_unlock(&group->notification_lock); 404 405 return -ENOENT; 406 } 407 408 static size_t copy_error_info_to_user(struct fanotify_event *event, 409 char __user *buf, int count) 410 { 411 struct fanotify_event_info_error info = { }; 412 struct fanotify_error_event *fee = FANOTIFY_EE(event); 413 414 info.hdr.info_type = FAN_EVENT_INFO_TYPE_ERROR; 415 info.hdr.len = FANOTIFY_ERROR_INFO_LEN; 416 417 if (WARN_ON(count < info.hdr.len)) 418 return -EFAULT; 419 420 info.error = fee->error; 421 info.error_count = fee->err_count; 422 423 if (copy_to_user(buf, &info, sizeof(info))) 424 return -EFAULT; 425 426 return info.hdr.len; 427 } 428 429 static int copy_fid_info_to_user(__kernel_fsid_t *fsid, struct fanotify_fh *fh, 430 int info_type, const char *name, 431 size_t name_len, 432 char __user *buf, size_t count) 433 { 434 struct fanotify_event_info_fid info = { }; 435 struct file_handle handle = { }; 436 unsigned char bounce[FANOTIFY_INLINE_FH_LEN], *fh_buf; 437 size_t fh_len = fh ? fh->len : 0; 438 size_t info_len = fanotify_fid_info_len(fh_len, name_len); 439 size_t len = info_len; 440 441 pr_debug("%s: fh_len=%zu name_len=%zu, info_len=%zu, count=%zu\n", 442 __func__, fh_len, name_len, info_len, count); 443 444 if (WARN_ON_ONCE(len < sizeof(info) || len > count)) 445 return -EFAULT; 446 447 /* 448 * Copy event info fid header followed by variable sized file handle 449 * and optionally followed by variable sized filename. 450 */ 451 switch (info_type) { 452 case FAN_EVENT_INFO_TYPE_FID: 453 case FAN_EVENT_INFO_TYPE_DFID: 454 if (WARN_ON_ONCE(name_len)) 455 return -EFAULT; 456 break; 457 case FAN_EVENT_INFO_TYPE_DFID_NAME: 458 case FAN_EVENT_INFO_TYPE_OLD_DFID_NAME: 459 case FAN_EVENT_INFO_TYPE_NEW_DFID_NAME: 460 if (WARN_ON_ONCE(!name || !name_len)) 461 return -EFAULT; 462 break; 463 default: 464 return -EFAULT; 465 } 466 467 info.hdr.info_type = info_type; 468 info.hdr.len = len; 469 info.fsid = *fsid; 470 if (copy_to_user(buf, &info, sizeof(info))) 471 return -EFAULT; 472 473 buf += sizeof(info); 474 len -= sizeof(info); 475 if (WARN_ON_ONCE(len < sizeof(handle))) 476 return -EFAULT; 477 478 handle.handle_type = fh->type; 479 handle.handle_bytes = fh_len; 480 481 /* Mangle handle_type for bad file_handle */ 482 if (!fh_len) 483 handle.handle_type = FILEID_INVALID; 484 485 if (copy_to_user(buf, &handle, sizeof(handle))) 486 return -EFAULT; 487 488 buf += sizeof(handle); 489 len -= sizeof(handle); 490 if (WARN_ON_ONCE(len < fh_len)) 491 return -EFAULT; 492 493 /* 494 * For an inline fh and inline file name, copy through stack to exclude 495 * the copy from usercopy hardening protections. 496 */ 497 fh_buf = fanotify_fh_buf(fh); 498 if (fh_len <= FANOTIFY_INLINE_FH_LEN) { 499 memcpy(bounce, fh_buf, fh_len); 500 fh_buf = bounce; 501 } 502 if (copy_to_user(buf, fh_buf, fh_len)) 503 return -EFAULT; 504 505 buf += fh_len; 506 len -= fh_len; 507 508 if (name_len) { 509 /* Copy the filename with terminating null */ 510 name_len++; 511 if (WARN_ON_ONCE(len < name_len)) 512 return -EFAULT; 513 514 if (copy_to_user(buf, name, name_len)) 515 return -EFAULT; 516 517 buf += name_len; 518 len -= name_len; 519 } 520 521 /* Pad with 0's */ 522 WARN_ON_ONCE(len < 0 || len >= FANOTIFY_EVENT_ALIGN); 523 if (len > 0 && clear_user(buf, len)) 524 return -EFAULT; 525 526 return info_len; 527 } 528 529 static int copy_pidfd_info_to_user(int pidfd, 530 char __user *buf, 531 size_t count) 532 { 533 struct fanotify_event_info_pidfd info = { }; 534 size_t info_len = FANOTIFY_PIDFD_INFO_LEN; 535 536 if (WARN_ON_ONCE(info_len > count)) 537 return -EFAULT; 538 539 info.hdr.info_type = FAN_EVENT_INFO_TYPE_PIDFD; 540 info.hdr.len = info_len; 541 info.pidfd = pidfd; 542 543 if (copy_to_user(buf, &info, info_len)) 544 return -EFAULT; 545 546 return info_len; 547 } 548 549 static size_t copy_range_info_to_user(struct fanotify_event *event, 550 char __user *buf, int count) 551 { 552 struct fanotify_perm_event *pevent = FANOTIFY_PERM(event); 553 struct fanotify_event_info_range info = { }; 554 size_t info_len = FANOTIFY_RANGE_INFO_LEN; 555 556 if (WARN_ON_ONCE(info_len > count)) 557 return -EFAULT; 558 559 if (WARN_ON_ONCE(!pevent->ppos)) 560 return -EINVAL; 561 562 info.hdr.info_type = FAN_EVENT_INFO_TYPE_RANGE; 563 info.hdr.len = info_len; 564 info.offset = *(pevent->ppos); 565 info.count = pevent->count; 566 567 if (copy_to_user(buf, &info, info_len)) 568 return -EFAULT; 569 570 return info_len; 571 } 572 573 static int copy_info_records_to_user(struct fanotify_event *event, 574 struct fanotify_info *info, 575 unsigned int info_mode, int pidfd, 576 char __user *buf, size_t count) 577 { 578 int ret, total_bytes = 0, info_type = 0; 579 unsigned int fid_mode = info_mode & FANOTIFY_FID_BITS; 580 unsigned int pidfd_mode = info_mode & FAN_REPORT_PIDFD; 581 582 /* 583 * Event info records order is as follows: 584 * 1. dir fid + name 585 * 2. (optional) new dir fid + new name 586 * 3. (optional) child fid 587 */ 588 if (fanotify_event_has_dir_fh(event)) { 589 info_type = info->name_len ? FAN_EVENT_INFO_TYPE_DFID_NAME : 590 FAN_EVENT_INFO_TYPE_DFID; 591 592 /* FAN_RENAME uses special info types */ 593 if (event->mask & FAN_RENAME) 594 info_type = FAN_EVENT_INFO_TYPE_OLD_DFID_NAME; 595 596 ret = copy_fid_info_to_user(fanotify_event_fsid(event), 597 fanotify_info_dir_fh(info), 598 info_type, 599 fanotify_info_name(info), 600 info->name_len, buf, count); 601 if (ret < 0) 602 return ret; 603 604 buf += ret; 605 count -= ret; 606 total_bytes += ret; 607 } 608 609 /* New dir fid+name may be reported in addition to old dir fid+name */ 610 if (fanotify_event_has_dir2_fh(event)) { 611 info_type = FAN_EVENT_INFO_TYPE_NEW_DFID_NAME; 612 ret = copy_fid_info_to_user(fanotify_event_fsid(event), 613 fanotify_info_dir2_fh(info), 614 info_type, 615 fanotify_info_name2(info), 616 info->name2_len, buf, count); 617 if (ret < 0) 618 return ret; 619 620 buf += ret; 621 count -= ret; 622 total_bytes += ret; 623 } 624 625 if (fanotify_event_has_object_fh(event)) { 626 const char *dot = NULL; 627 int dot_len = 0; 628 629 if (fid_mode == FAN_REPORT_FID || info_type) { 630 /* 631 * With only group flag FAN_REPORT_FID only type FID is 632 * reported. Second info record type is always FID. 633 */ 634 info_type = FAN_EVENT_INFO_TYPE_FID; 635 } else if ((fid_mode & FAN_REPORT_NAME) && 636 (event->mask & FAN_ONDIR)) { 637 /* 638 * With group flag FAN_REPORT_NAME, if name was not 639 * recorded in an event on a directory, report the name 640 * "." with info type DFID_NAME. 641 */ 642 info_type = FAN_EVENT_INFO_TYPE_DFID_NAME; 643 dot = "."; 644 dot_len = 1; 645 } else if ((event->mask & ALL_FSNOTIFY_DIRENT_EVENTS) || 646 (event->mask & FAN_ONDIR)) { 647 /* 648 * With group flag FAN_REPORT_DIR_FID, a single info 649 * record has type DFID for directory entry modification 650 * event and for event on a directory. 651 */ 652 info_type = FAN_EVENT_INFO_TYPE_DFID; 653 } else { 654 /* 655 * With group flags FAN_REPORT_DIR_FID|FAN_REPORT_FID, 656 * a single info record has type FID for event on a 657 * non-directory, when there is no directory to report. 658 * For example, on FAN_DELETE_SELF event. 659 */ 660 info_type = FAN_EVENT_INFO_TYPE_FID; 661 } 662 663 ret = copy_fid_info_to_user(fanotify_event_fsid(event), 664 fanotify_event_object_fh(event), 665 info_type, dot, dot_len, 666 buf, count); 667 if (ret < 0) 668 return ret; 669 670 buf += ret; 671 count -= ret; 672 total_bytes += ret; 673 } 674 675 if (pidfd_mode) { 676 ret = copy_pidfd_info_to_user(pidfd, buf, count); 677 if (ret < 0) 678 return ret; 679 680 buf += ret; 681 count -= ret; 682 total_bytes += ret; 683 } 684 685 if (fanotify_is_error_event(event->mask)) { 686 ret = copy_error_info_to_user(event, buf, count); 687 if (ret < 0) 688 return ret; 689 buf += ret; 690 count -= ret; 691 total_bytes += ret; 692 } 693 694 if (fanotify_event_has_access_range(event)) { 695 ret = copy_range_info_to_user(event, buf, count); 696 if (ret < 0) 697 return ret; 698 buf += ret; 699 count -= ret; 700 total_bytes += ret; 701 } 702 703 return total_bytes; 704 } 705 706 static ssize_t copy_event_to_user(struct fsnotify_group *group, 707 struct fanotify_event *event, 708 char __user *buf, size_t count) 709 { 710 struct fanotify_event_metadata metadata; 711 const struct path *path = fanotify_event_path(event); 712 struct fanotify_info *info = fanotify_event_info(event); 713 unsigned int info_mode = FAN_GROUP_FLAG(group, FANOTIFY_INFO_MODES); 714 unsigned int pidfd_mode = info_mode & FAN_REPORT_PIDFD; 715 struct file *f = NULL, *pidfd_file = NULL; 716 int ret, pidfd = -ESRCH, fd = -EBADF; 717 718 pr_debug("%s: group=%p event=%p\n", __func__, group, event); 719 720 metadata.event_len = fanotify_event_len(info_mode, event); 721 metadata.metadata_len = FAN_EVENT_METADATA_LEN; 722 metadata.vers = FANOTIFY_METADATA_VERSION; 723 metadata.reserved = 0; 724 metadata.mask = event->mask & FANOTIFY_OUTGOING_EVENTS; 725 metadata.pid = pid_vnr(event->pid); 726 /* 727 * For an unprivileged listener, event->pid can be used to identify the 728 * events generated by the listener process itself, without disclosing 729 * the pids of other processes. 730 */ 731 if (FAN_GROUP_FLAG(group, FANOTIFY_UNPRIV) && 732 task_tgid(current) != event->pid) 733 metadata.pid = 0; 734 735 /* 736 * For now, fid mode is required for an unprivileged listener and 737 * fid mode does not report fd in events. Keep this check anyway 738 * for safety in case fid mode requirement is relaxed in the future 739 * to allow unprivileged listener to get events with no fd and no fid. 740 */ 741 if (!FAN_GROUP_FLAG(group, FANOTIFY_UNPRIV) && 742 path && path->mnt && path->dentry) { 743 fd = create_fd(group, path, &f); 744 /* 745 * Opening an fd from dentry can fail for several reasons. 746 * For example, when tasks are gone and we try to open their 747 * /proc files or we try to open a WRONLY file like in sysfs 748 * or when trying to open a file that was deleted on the 749 * remote network server. 750 * 751 * For a group with FAN_REPORT_FD_ERROR, we will send the 752 * event with the error instead of the open fd, otherwise 753 * Userspace may not get the error at all. 754 * In any case, userspace will not know which file failed to 755 * open, so add a debug print for further investigation. 756 */ 757 if (fd < 0) { 758 pr_debug("fanotify: create_fd(%pd2) failed err=%d\n", 759 path->dentry, fd); 760 if (!FAN_GROUP_FLAG(group, FAN_REPORT_FD_ERROR)) { 761 /* 762 * Historically, we've handled EOPENSTALE in a 763 * special way and silently dropped such 764 * events. Now we have to keep it to maintain 765 * backward compatibility... 766 */ 767 if (fd == -EOPENSTALE) 768 fd = 0; 769 return fd; 770 } 771 } 772 } 773 if (FAN_GROUP_FLAG(group, FAN_REPORT_FD_ERROR)) 774 metadata.fd = fd; 775 else 776 metadata.fd = fd >= 0 ? fd : FAN_NOFD; 777 778 if (pidfd_mode) { 779 /* 780 * Complain if the FAN_REPORT_PIDFD and FAN_REPORT_TID mutual 781 * exclusion is ever lifted. At the time of incoporating pidfd 782 * support within fanotify, the pidfd API only supported the 783 * creation of pidfds for thread-group leaders. 784 */ 785 WARN_ON_ONCE(FAN_GROUP_FLAG(group, FAN_REPORT_TID)); 786 787 /* 788 * The PIDTYPE_TGID check for an event->pid is performed 789 * preemptively in an attempt to catch out cases where the event 790 * listener reads events after the event generating process has 791 * already terminated. Depending on flag FAN_REPORT_FD_ERROR, 792 * report either -ESRCH or FAN_NOPIDFD to the event listener in 793 * those cases with all other pidfd creation errors reported as 794 * the error code itself or as FAN_EPIDFD. 795 */ 796 if (metadata.pid && pid_has_task(event->pid, PIDTYPE_TGID)) 797 pidfd = pidfd_prepare(event->pid, 0, &pidfd_file); 798 799 if (!FAN_GROUP_FLAG(group, FAN_REPORT_FD_ERROR) && pidfd < 0) 800 pidfd = pidfd == -ESRCH ? FAN_NOPIDFD : FAN_EPIDFD; 801 } 802 803 ret = -EFAULT; 804 /* 805 * Sanity check copy size in case get_one_event() and 806 * event_len sizes ever get out of sync. 807 */ 808 if (WARN_ON_ONCE(metadata.event_len > count)) 809 goto out_close_fd; 810 811 if (copy_to_user(buf, &metadata, FAN_EVENT_METADATA_LEN)) 812 goto out_close_fd; 813 814 buf += FAN_EVENT_METADATA_LEN; 815 count -= FAN_EVENT_METADATA_LEN; 816 817 ret = copy_info_records_to_user(event, info, info_mode, pidfd, 818 buf, count); 819 if (ret < 0) 820 goto out_close_fd; 821 822 if (f) 823 fd_install(fd, f); 824 825 if (pidfd_file) 826 fd_install(pidfd, pidfd_file); 827 828 if (fanotify_is_perm_event(event->mask)) 829 FANOTIFY_PERM(event)->fd = fd; 830 831 return metadata.event_len; 832 833 out_close_fd: 834 if (f) { 835 put_unused_fd(fd); 836 fput(f); 837 } 838 839 if (pidfd_file) { 840 put_unused_fd(pidfd); 841 fput(pidfd_file); 842 } 843 844 return ret; 845 } 846 847 /* intofiy userspace file descriptor functions */ 848 static __poll_t fanotify_poll(struct file *file, poll_table *wait) 849 { 850 struct fsnotify_group *group = file->private_data; 851 __poll_t ret = 0; 852 853 poll_wait(file, &group->notification_waitq, wait); 854 spin_lock(&group->notification_lock); 855 if (!fsnotify_notify_queue_is_empty(group)) 856 ret = EPOLLIN | EPOLLRDNORM; 857 spin_unlock(&group->notification_lock); 858 859 return ret; 860 } 861 862 static ssize_t fanotify_read(struct file *file, char __user *buf, 863 size_t count, loff_t *pos) 864 { 865 struct fsnotify_group *group; 866 struct fanotify_event *event; 867 char __user *start; 868 int ret; 869 DEFINE_WAIT_FUNC(wait, woken_wake_function); 870 871 start = buf; 872 group = file->private_data; 873 874 pr_debug("%s: group=%p\n", __func__, group); 875 876 add_wait_queue(&group->notification_waitq, &wait); 877 while (1) { 878 /* 879 * User can supply arbitrarily large buffer. Avoid softlockups 880 * in case there are lots of available events. 881 */ 882 cond_resched(); 883 event = get_one_event(group, count); 884 if (IS_ERR(event)) { 885 ret = PTR_ERR(event); 886 break; 887 } 888 889 if (!event) { 890 ret = -EAGAIN; 891 if (file->f_flags & O_NONBLOCK) 892 break; 893 894 ret = -ERESTARTSYS; 895 if (signal_pending(current)) 896 break; 897 898 if (start != buf) 899 break; 900 901 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); 902 continue; 903 } 904 905 ret = copy_event_to_user(group, event, buf, count); 906 907 /* 908 * Permission events get queued to wait for response. Other 909 * events can be destroyed now. 910 */ 911 if (!fanotify_is_perm_event(event->mask)) { 912 fsnotify_destroy_event(group, &event->fse); 913 } else { 914 if (ret <= 0 || FANOTIFY_PERM(event)->fd < 0) { 915 spin_lock(&group->notification_lock); 916 finish_permission_event(group, 917 FANOTIFY_PERM(event), FAN_DENY, NULL); 918 wake_up(&group->fanotify_data.access_waitq); 919 } else { 920 spin_lock(&group->notification_lock); 921 list_add_tail(&event->fse.list, 922 &group->fanotify_data.access_list); 923 spin_unlock(&group->notification_lock); 924 } 925 } 926 if (ret < 0) 927 break; 928 buf += ret; 929 count -= ret; 930 } 931 remove_wait_queue(&group->notification_waitq, &wait); 932 933 if (start != buf && ret != -EFAULT) 934 ret = buf - start; 935 return ret; 936 } 937 938 static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos) 939 { 940 struct fanotify_response response; 941 struct fsnotify_group *group; 942 int ret; 943 const char __user *info_buf = buf + sizeof(struct fanotify_response); 944 size_t info_len; 945 946 if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) 947 return -EINVAL; 948 949 group = file->private_data; 950 951 pr_debug("%s: group=%p count=%zu\n", __func__, group, count); 952 953 if (count < sizeof(response)) 954 return -EINVAL; 955 956 if (copy_from_user(&response, buf, sizeof(response))) 957 return -EFAULT; 958 959 info_len = count - sizeof(response); 960 961 ret = process_access_response(group, &response, info_buf, info_len); 962 if (ret < 0) 963 count = ret; 964 else 965 count = sizeof(response) + ret; 966 967 return count; 968 } 969 970 static int fanotify_release(struct inode *ignored, struct file *file) 971 { 972 struct fsnotify_group *group = file->private_data; 973 struct fsnotify_event *fsn_event; 974 975 /* 976 * Stop new events from arriving in the notification queue. since 977 * userspace cannot use fanotify fd anymore, no event can enter or 978 * leave access_list by now either. 979 */ 980 fsnotify_group_stop_queueing(group); 981 982 /* 983 * Process all permission events on access_list and notification queue 984 * and simulate reply from userspace. 985 */ 986 spin_lock(&group->notification_lock); 987 while (!list_empty(&group->fanotify_data.access_list)) { 988 struct fanotify_perm_event *event; 989 990 event = list_first_entry(&group->fanotify_data.access_list, 991 struct fanotify_perm_event, fae.fse.list); 992 list_del_init(&event->fae.fse.list); 993 finish_permission_event(group, event, FAN_ALLOW, NULL); 994 spin_lock(&group->notification_lock); 995 } 996 997 /* 998 * Destroy all non-permission events. For permission events just 999 * dequeue them and set the response. They will be freed once the 1000 * response is consumed and fanotify_get_response() returns. 1001 */ 1002 while ((fsn_event = fsnotify_remove_first_event(group))) { 1003 struct fanotify_event *event = FANOTIFY_E(fsn_event); 1004 1005 if (!(event->mask & FANOTIFY_PERM_EVENTS)) { 1006 spin_unlock(&group->notification_lock); 1007 fsnotify_destroy_event(group, fsn_event); 1008 } else { 1009 finish_permission_event(group, FANOTIFY_PERM(event), 1010 FAN_ALLOW, NULL); 1011 } 1012 spin_lock(&group->notification_lock); 1013 } 1014 spin_unlock(&group->notification_lock); 1015 1016 /* Response for all permission events it set, wakeup waiters */ 1017 wake_up(&group->fanotify_data.access_waitq); 1018 1019 /* matches the fanotify_init->fsnotify_alloc_group */ 1020 fsnotify_destroy_group(group); 1021 1022 return 0; 1023 } 1024 1025 static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 1026 { 1027 struct fsnotify_group *group; 1028 struct fsnotify_event *fsn_event; 1029 void __user *p; 1030 int ret = -ENOTTY; 1031 size_t send_len = 0; 1032 1033 group = file->private_data; 1034 1035 p = (void __user *) arg; 1036 1037 switch (cmd) { 1038 case FIONREAD: 1039 spin_lock(&group->notification_lock); 1040 list_for_each_entry(fsn_event, &group->notification_list, list) 1041 send_len += FAN_EVENT_METADATA_LEN; 1042 spin_unlock(&group->notification_lock); 1043 ret = put_user(send_len, (int __user *) p); 1044 break; 1045 } 1046 1047 return ret; 1048 } 1049 1050 static const struct file_operations fanotify_fops = { 1051 .show_fdinfo = fanotify_show_fdinfo, 1052 .poll = fanotify_poll, 1053 .read = fanotify_read, 1054 .write = fanotify_write, 1055 .fasync = NULL, 1056 .release = fanotify_release, 1057 .unlocked_ioctl = fanotify_ioctl, 1058 .compat_ioctl = compat_ptr_ioctl, 1059 .llseek = noop_llseek, 1060 }; 1061 1062 static int fanotify_find_path(int dfd, const char __user *filename, 1063 struct path *path, unsigned int flags, __u64 mask, 1064 unsigned int obj_type) 1065 { 1066 int ret; 1067 1068 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__, 1069 dfd, filename, flags); 1070 1071 if (filename == NULL) { 1072 CLASS(fd, f)(dfd); 1073 1074 if (fd_empty(f)) 1075 return -EBADF; 1076 1077 if ((flags & FAN_MARK_ONLYDIR) && 1078 !(S_ISDIR(file_inode(fd_file(f))->i_mode))) 1079 return -ENOTDIR; 1080 1081 *path = fd_file(f)->f_path; 1082 path_get(path); 1083 } else { 1084 unsigned int lookup_flags = 0; 1085 1086 if (!(flags & FAN_MARK_DONT_FOLLOW)) 1087 lookup_flags |= LOOKUP_FOLLOW; 1088 if (flags & FAN_MARK_ONLYDIR) 1089 lookup_flags |= LOOKUP_DIRECTORY; 1090 1091 ret = user_path_at(dfd, filename, lookup_flags, path); 1092 if (ret) 1093 goto out; 1094 } 1095 1096 /* you can only watch an inode if you have read permissions on it */ 1097 ret = path_permission(path, MAY_READ); 1098 if (ret) { 1099 path_put(path); 1100 goto out; 1101 } 1102 1103 ret = security_path_notify(path, mask, obj_type); 1104 if (ret) 1105 path_put(path); 1106 1107 out: 1108 return ret; 1109 } 1110 1111 static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark, 1112 __u32 mask, unsigned int flags, 1113 __u32 umask, int *destroy) 1114 { 1115 __u32 oldmask, newmask; 1116 1117 /* umask bits cannot be removed by user */ 1118 mask &= ~umask; 1119 spin_lock(&fsn_mark->lock); 1120 oldmask = fsnotify_calc_mask(fsn_mark); 1121 if (!(flags & FANOTIFY_MARK_IGNORE_BITS)) { 1122 fsn_mark->mask &= ~mask; 1123 } else { 1124 fsn_mark->ignore_mask &= ~mask; 1125 } 1126 newmask = fsnotify_calc_mask(fsn_mark); 1127 /* 1128 * We need to keep the mark around even if remaining mask cannot 1129 * result in any events (e.g. mask == FAN_ONDIR) to support incremenal 1130 * changes to the mask. 1131 * Destroy mark when only umask bits remain. 1132 */ 1133 *destroy = !((fsn_mark->mask | fsn_mark->ignore_mask) & ~umask); 1134 spin_unlock(&fsn_mark->lock); 1135 1136 return oldmask & ~newmask; 1137 } 1138 1139 static int fanotify_remove_mark(struct fsnotify_group *group, 1140 void *obj, unsigned int obj_type, __u32 mask, 1141 unsigned int flags, __u32 umask) 1142 { 1143 struct fsnotify_mark *fsn_mark = NULL; 1144 __u32 removed; 1145 int destroy_mark; 1146 1147 fsnotify_group_lock(group); 1148 fsn_mark = fsnotify_find_mark(obj, obj_type, group); 1149 if (!fsn_mark) { 1150 fsnotify_group_unlock(group); 1151 return -ENOENT; 1152 } 1153 1154 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags, 1155 umask, &destroy_mark); 1156 if (removed & fsnotify_conn_mask(fsn_mark->connector)) 1157 fsnotify_recalc_mask(fsn_mark->connector); 1158 if (destroy_mark) 1159 fsnotify_detach_mark(fsn_mark); 1160 fsnotify_group_unlock(group); 1161 if (destroy_mark) 1162 fsnotify_free_mark(fsn_mark); 1163 1164 /* matches the fsnotify_find_mark() */ 1165 fsnotify_put_mark(fsn_mark); 1166 return 0; 1167 } 1168 1169 static bool fanotify_mark_update_flags(struct fsnotify_mark *fsn_mark, 1170 unsigned int fan_flags) 1171 { 1172 bool want_iref = !(fan_flags & FAN_MARK_EVICTABLE); 1173 unsigned int ignore = fan_flags & FANOTIFY_MARK_IGNORE_BITS; 1174 bool recalc = false; 1175 1176 /* 1177 * When using FAN_MARK_IGNORE for the first time, mark starts using 1178 * independent event flags in ignore mask. After that, trying to 1179 * update the ignore mask with the old FAN_MARK_IGNORED_MASK API 1180 * will result in EEXIST error. 1181 */ 1182 if (ignore == FAN_MARK_IGNORE) 1183 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS; 1184 1185 /* 1186 * Setting FAN_MARK_IGNORED_SURV_MODIFY for the first time may lead to 1187 * the removal of the FS_MODIFY bit in calculated mask if it was set 1188 * because of an ignore mask that is now going to survive FS_MODIFY. 1189 */ 1190 if (ignore && (fan_flags & FAN_MARK_IGNORED_SURV_MODIFY) && 1191 !(fsn_mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)) { 1192 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY; 1193 if (!(fsn_mark->mask & FS_MODIFY)) 1194 recalc = true; 1195 } 1196 1197 if (fsn_mark->connector->type != FSNOTIFY_OBJ_TYPE_INODE || 1198 want_iref == !(fsn_mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF)) 1199 return recalc; 1200 1201 /* 1202 * NO_IREF may be removed from a mark, but not added. 1203 * When removed, fsnotify_recalc_mask() will take the inode ref. 1204 */ 1205 WARN_ON_ONCE(!want_iref); 1206 fsn_mark->flags &= ~FSNOTIFY_MARK_FLAG_NO_IREF; 1207 1208 return true; 1209 } 1210 1211 static bool fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark, 1212 __u32 mask, unsigned int fan_flags) 1213 { 1214 bool recalc; 1215 1216 spin_lock(&fsn_mark->lock); 1217 if (!(fan_flags & FANOTIFY_MARK_IGNORE_BITS)) 1218 fsn_mark->mask |= mask; 1219 else 1220 fsn_mark->ignore_mask |= mask; 1221 1222 recalc = fsnotify_calc_mask(fsn_mark) & 1223 ~fsnotify_conn_mask(fsn_mark->connector); 1224 1225 recalc |= fanotify_mark_update_flags(fsn_mark, fan_flags); 1226 spin_unlock(&fsn_mark->lock); 1227 1228 return recalc; 1229 } 1230 1231 struct fan_fsid { 1232 struct super_block *sb; 1233 __kernel_fsid_t id; 1234 bool weak; 1235 }; 1236 1237 static int fanotify_set_mark_fsid(struct fsnotify_group *group, 1238 struct fsnotify_mark *mark, 1239 struct fan_fsid *fsid) 1240 { 1241 struct fsnotify_mark_connector *conn; 1242 struct fsnotify_mark *old; 1243 struct super_block *old_sb = NULL; 1244 1245 FANOTIFY_MARK(mark)->fsid = fsid->id; 1246 mark->flags |= FSNOTIFY_MARK_FLAG_HAS_FSID; 1247 if (fsid->weak) 1248 mark->flags |= FSNOTIFY_MARK_FLAG_WEAK_FSID; 1249 1250 /* First mark added will determine if group is single or multi fsid */ 1251 if (list_empty(&group->marks_list)) 1252 return 0; 1253 1254 /* Find sb of an existing mark */ 1255 list_for_each_entry(old, &group->marks_list, g_list) { 1256 conn = READ_ONCE(old->connector); 1257 if (!conn) 1258 continue; 1259 old_sb = fsnotify_connector_sb(conn); 1260 if (old_sb) 1261 break; 1262 } 1263 1264 /* Only detached marks left? */ 1265 if (!old_sb) 1266 return 0; 1267 1268 /* Do not allow mixing of marks with weak and strong fsid */ 1269 if ((mark->flags ^ old->flags) & FSNOTIFY_MARK_FLAG_WEAK_FSID) 1270 return -EXDEV; 1271 1272 /* Allow mixing of marks with strong fsid from different fs */ 1273 if (!fsid->weak) 1274 return 0; 1275 1276 /* Do not allow mixing marks with weak fsid from different fs */ 1277 if (old_sb != fsid->sb) 1278 return -EXDEV; 1279 1280 /* Do not allow mixing marks from different btrfs sub-volumes */ 1281 if (!fanotify_fsid_equal(&FANOTIFY_MARK(old)->fsid, 1282 &FANOTIFY_MARK(mark)->fsid)) 1283 return -EXDEV; 1284 1285 return 0; 1286 } 1287 1288 static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group, 1289 void *obj, 1290 unsigned int obj_type, 1291 unsigned int fan_flags, 1292 struct fan_fsid *fsid) 1293 { 1294 struct ucounts *ucounts = group->fanotify_data.ucounts; 1295 struct fanotify_mark *fan_mark; 1296 struct fsnotify_mark *mark; 1297 int ret; 1298 1299 /* 1300 * Enforce per user marks limits per user in all containing user ns. 1301 * A group with FAN_UNLIMITED_MARKS does not contribute to mark count 1302 * in the limited groups account. 1303 */ 1304 if (!FAN_GROUP_FLAG(group, FAN_UNLIMITED_MARKS) && 1305 !inc_ucount(ucounts->ns, ucounts->uid, UCOUNT_FANOTIFY_MARKS)) 1306 return ERR_PTR(-ENOSPC); 1307 1308 fan_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL); 1309 if (!fan_mark) { 1310 ret = -ENOMEM; 1311 goto out_dec_ucounts; 1312 } 1313 1314 mark = &fan_mark->fsn_mark; 1315 fsnotify_init_mark(mark, group); 1316 if (fan_flags & FAN_MARK_EVICTABLE) 1317 mark->flags |= FSNOTIFY_MARK_FLAG_NO_IREF; 1318 1319 /* Cache fsid of filesystem containing the marked object */ 1320 if (fsid) { 1321 ret = fanotify_set_mark_fsid(group, mark, fsid); 1322 if (ret) 1323 goto out_put_mark; 1324 } else { 1325 fan_mark->fsid.val[0] = fan_mark->fsid.val[1] = 0; 1326 } 1327 1328 ret = fsnotify_add_mark_locked(mark, obj, obj_type, 0); 1329 if (ret) 1330 goto out_put_mark; 1331 1332 return mark; 1333 1334 out_put_mark: 1335 fsnotify_put_mark(mark); 1336 out_dec_ucounts: 1337 if (!FAN_GROUP_FLAG(group, FAN_UNLIMITED_MARKS)) 1338 dec_ucount(ucounts, UCOUNT_FANOTIFY_MARKS); 1339 return ERR_PTR(ret); 1340 } 1341 1342 static int fanotify_group_init_error_pool(struct fsnotify_group *group) 1343 { 1344 if (mempool_initialized(&group->fanotify_data.error_events_pool)) 1345 return 0; 1346 1347 return mempool_init_kmalloc_pool(&group->fanotify_data.error_events_pool, 1348 FANOTIFY_DEFAULT_FEE_POOL_SIZE, 1349 sizeof(struct fanotify_error_event)); 1350 } 1351 1352 static int fanotify_may_update_existing_mark(struct fsnotify_mark *fsn_mark, 1353 __u32 mask, unsigned int fan_flags) 1354 { 1355 /* 1356 * Non evictable mark cannot be downgraded to evictable mark. 1357 */ 1358 if (fan_flags & FAN_MARK_EVICTABLE && 1359 !(fsn_mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF)) 1360 return -EEXIST; 1361 1362 /* 1363 * New ignore mask semantics cannot be downgraded to old semantics. 1364 */ 1365 if (fan_flags & FAN_MARK_IGNORED_MASK && 1366 fsn_mark->flags & FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS) 1367 return -EEXIST; 1368 1369 /* 1370 * An ignore mask that survives modify could never be downgraded to not 1371 * survive modify. With new FAN_MARK_IGNORE semantics we make that rule 1372 * explicit and return an error when trying to update the ignore mask 1373 * without the original FAN_MARK_IGNORED_SURV_MODIFY value. 1374 */ 1375 if (fan_flags & FAN_MARK_IGNORE && 1376 !(fan_flags & FAN_MARK_IGNORED_SURV_MODIFY) && 1377 fsn_mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY) 1378 return -EEXIST; 1379 1380 /* For now pre-content events are not generated for directories */ 1381 mask |= fsn_mark->mask; 1382 if (mask & FANOTIFY_PRE_CONTENT_EVENTS && mask & FAN_ONDIR) 1383 return -EEXIST; 1384 1385 return 0; 1386 } 1387 1388 static int fanotify_add_mark(struct fsnotify_group *group, 1389 void *obj, unsigned int obj_type, 1390 __u32 mask, unsigned int fan_flags, 1391 struct fan_fsid *fsid) 1392 { 1393 struct fsnotify_mark *fsn_mark; 1394 bool recalc; 1395 int ret = 0; 1396 1397 fsnotify_group_lock(group); 1398 fsn_mark = fsnotify_find_mark(obj, obj_type, group); 1399 if (!fsn_mark) { 1400 fsn_mark = fanotify_add_new_mark(group, obj, obj_type, 1401 fan_flags, fsid); 1402 if (IS_ERR(fsn_mark)) { 1403 fsnotify_group_unlock(group); 1404 return PTR_ERR(fsn_mark); 1405 } 1406 } 1407 1408 /* 1409 * Check if requested mark flags conflict with an existing mark flags. 1410 */ 1411 ret = fanotify_may_update_existing_mark(fsn_mark, mask, fan_flags); 1412 if (ret) 1413 goto out; 1414 1415 /* 1416 * Error events are pre-allocated per group, only if strictly 1417 * needed (i.e. FAN_FS_ERROR was requested). 1418 */ 1419 if (!(fan_flags & FANOTIFY_MARK_IGNORE_BITS) && 1420 (mask & FAN_FS_ERROR)) { 1421 ret = fanotify_group_init_error_pool(group); 1422 if (ret) 1423 goto out; 1424 } 1425 1426 recalc = fanotify_mark_add_to_mask(fsn_mark, mask, fan_flags); 1427 if (recalc) 1428 fsnotify_recalc_mask(fsn_mark->connector); 1429 1430 out: 1431 fsnotify_group_unlock(group); 1432 1433 fsnotify_put_mark(fsn_mark); 1434 return ret; 1435 } 1436 1437 static struct fsnotify_event *fanotify_alloc_overflow_event(void) 1438 { 1439 struct fanotify_event *oevent; 1440 1441 oevent = kmalloc(sizeof(*oevent), GFP_KERNEL_ACCOUNT); 1442 if (!oevent) 1443 return NULL; 1444 1445 fanotify_init_event(oevent, 0, FS_Q_OVERFLOW); 1446 oevent->type = FANOTIFY_EVENT_TYPE_OVERFLOW; 1447 1448 return &oevent->fse; 1449 } 1450 1451 static struct hlist_head *fanotify_alloc_merge_hash(void) 1452 { 1453 struct hlist_head *hash; 1454 1455 hash = kmalloc(sizeof(struct hlist_head) << FANOTIFY_HTABLE_BITS, 1456 GFP_KERNEL_ACCOUNT); 1457 if (!hash) 1458 return NULL; 1459 1460 __hash_init(hash, FANOTIFY_HTABLE_SIZE); 1461 1462 return hash; 1463 } 1464 1465 /* fanotify syscalls */ 1466 SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) 1467 { 1468 struct fsnotify_group *group; 1469 int f_flags, fd; 1470 unsigned int fid_mode = flags & FANOTIFY_FID_BITS; 1471 unsigned int class = flags & FANOTIFY_CLASS_BITS; 1472 unsigned int internal_flags = 0; 1473 struct file *file; 1474 1475 pr_debug("%s: flags=%x event_f_flags=%x\n", 1476 __func__, flags, event_f_flags); 1477 1478 if (!capable(CAP_SYS_ADMIN)) { 1479 /* 1480 * An unprivileged user can setup an fanotify group with 1481 * limited functionality - an unprivileged group is limited to 1482 * notification events with file handles and it cannot use 1483 * unlimited queue/marks. 1484 */ 1485 if ((flags & FANOTIFY_ADMIN_INIT_FLAGS) || !fid_mode) 1486 return -EPERM; 1487 1488 /* 1489 * Setting the internal flag FANOTIFY_UNPRIV on the group 1490 * prevents setting mount/filesystem marks on this group and 1491 * prevents reporting pid and open fd in events. 1492 */ 1493 internal_flags |= FANOTIFY_UNPRIV; 1494 } 1495 1496 #ifdef CONFIG_AUDITSYSCALL 1497 if (flags & ~(FANOTIFY_INIT_FLAGS | FAN_ENABLE_AUDIT)) 1498 #else 1499 if (flags & ~FANOTIFY_INIT_FLAGS) 1500 #endif 1501 return -EINVAL; 1502 1503 /* 1504 * A pidfd can only be returned for a thread-group leader; thus 1505 * FAN_REPORT_PIDFD and FAN_REPORT_TID need to remain mutually 1506 * exclusive. 1507 */ 1508 if ((flags & FAN_REPORT_PIDFD) && (flags & FAN_REPORT_TID)) 1509 return -EINVAL; 1510 1511 if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS) 1512 return -EINVAL; 1513 1514 switch (event_f_flags & O_ACCMODE) { 1515 case O_RDONLY: 1516 case O_RDWR: 1517 case O_WRONLY: 1518 break; 1519 default: 1520 return -EINVAL; 1521 } 1522 1523 if (fid_mode && class != FAN_CLASS_NOTIF) 1524 return -EINVAL; 1525 1526 /* 1527 * Child name is reported with parent fid so requires dir fid. 1528 * We can report both child fid and dir fid with or without name. 1529 */ 1530 if ((fid_mode & FAN_REPORT_NAME) && !(fid_mode & FAN_REPORT_DIR_FID)) 1531 return -EINVAL; 1532 1533 /* 1534 * FAN_REPORT_TARGET_FID requires FAN_REPORT_NAME and FAN_REPORT_FID 1535 * and is used as an indication to report both dir and child fid on all 1536 * dirent events. 1537 */ 1538 if ((fid_mode & FAN_REPORT_TARGET_FID) && 1539 (!(fid_mode & FAN_REPORT_NAME) || !(fid_mode & FAN_REPORT_FID))) 1540 return -EINVAL; 1541 1542 f_flags = O_RDWR; 1543 if (flags & FAN_CLOEXEC) 1544 f_flags |= O_CLOEXEC; 1545 if (flags & FAN_NONBLOCK) 1546 f_flags |= O_NONBLOCK; 1547 1548 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */ 1549 group = fsnotify_alloc_group(&fanotify_fsnotify_ops, 1550 FSNOTIFY_GROUP_USER); 1551 if (IS_ERR(group)) { 1552 return PTR_ERR(group); 1553 } 1554 1555 /* Enforce groups limits per user in all containing user ns */ 1556 group->fanotify_data.ucounts = inc_ucount(current_user_ns(), 1557 current_euid(), 1558 UCOUNT_FANOTIFY_GROUPS); 1559 if (!group->fanotify_data.ucounts) { 1560 fd = -EMFILE; 1561 goto out_destroy_group; 1562 } 1563 1564 group->fanotify_data.flags = flags | internal_flags; 1565 group->memcg = get_mem_cgroup_from_mm(current->mm); 1566 1567 group->fanotify_data.merge_hash = fanotify_alloc_merge_hash(); 1568 if (!group->fanotify_data.merge_hash) { 1569 fd = -ENOMEM; 1570 goto out_destroy_group; 1571 } 1572 1573 group->overflow_event = fanotify_alloc_overflow_event(); 1574 if (unlikely(!group->overflow_event)) { 1575 fd = -ENOMEM; 1576 goto out_destroy_group; 1577 } 1578 1579 if (force_o_largefile()) 1580 event_f_flags |= O_LARGEFILE; 1581 group->fanotify_data.f_flags = event_f_flags; 1582 init_waitqueue_head(&group->fanotify_data.access_waitq); 1583 INIT_LIST_HEAD(&group->fanotify_data.access_list); 1584 switch (class) { 1585 case FAN_CLASS_NOTIF: 1586 group->priority = FSNOTIFY_PRIO_NORMAL; 1587 break; 1588 case FAN_CLASS_CONTENT: 1589 group->priority = FSNOTIFY_PRIO_CONTENT; 1590 break; 1591 case FAN_CLASS_PRE_CONTENT: 1592 group->priority = FSNOTIFY_PRIO_PRE_CONTENT; 1593 break; 1594 default: 1595 fd = -EINVAL; 1596 goto out_destroy_group; 1597 } 1598 1599 if (flags & FAN_UNLIMITED_QUEUE) { 1600 fd = -EPERM; 1601 if (!capable(CAP_SYS_ADMIN)) 1602 goto out_destroy_group; 1603 group->max_events = UINT_MAX; 1604 } else { 1605 group->max_events = fanotify_max_queued_events; 1606 } 1607 1608 if (flags & FAN_UNLIMITED_MARKS) { 1609 fd = -EPERM; 1610 if (!capable(CAP_SYS_ADMIN)) 1611 goto out_destroy_group; 1612 } 1613 1614 if (flags & FAN_ENABLE_AUDIT) { 1615 fd = -EPERM; 1616 if (!capable(CAP_AUDIT_WRITE)) 1617 goto out_destroy_group; 1618 } 1619 1620 fd = get_unused_fd_flags(f_flags); 1621 if (fd < 0) 1622 goto out_destroy_group; 1623 1624 file = anon_inode_getfile_fmode("[fanotify]", &fanotify_fops, group, 1625 f_flags, FMODE_NONOTIFY); 1626 if (IS_ERR(file)) { 1627 put_unused_fd(fd); 1628 fd = PTR_ERR(file); 1629 goto out_destroy_group; 1630 } 1631 fd_install(fd, file); 1632 return fd; 1633 1634 out_destroy_group: 1635 fsnotify_destroy_group(group); 1636 return fd; 1637 } 1638 1639 static int fanotify_test_fsid(struct dentry *dentry, unsigned int flags, 1640 struct fan_fsid *fsid) 1641 { 1642 unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS; 1643 __kernel_fsid_t root_fsid; 1644 int err; 1645 1646 /* 1647 * Make sure dentry is not of a filesystem with zero fsid (e.g. fuse). 1648 */ 1649 err = vfs_get_fsid(dentry, &fsid->id); 1650 if (err) 1651 return err; 1652 1653 fsid->sb = dentry->d_sb; 1654 if (!fsid->id.val[0] && !fsid->id.val[1]) { 1655 err = -ENODEV; 1656 goto weak; 1657 } 1658 1659 /* 1660 * Make sure dentry is not of a filesystem subvolume (e.g. btrfs) 1661 * which uses a different fsid than sb root. 1662 */ 1663 err = vfs_get_fsid(dentry->d_sb->s_root, &root_fsid); 1664 if (err) 1665 return err; 1666 1667 if (!fanotify_fsid_equal(&root_fsid, &fsid->id)) { 1668 err = -EXDEV; 1669 goto weak; 1670 } 1671 1672 fsid->weak = false; 1673 return 0; 1674 1675 weak: 1676 /* Allow weak fsid when marking inodes */ 1677 fsid->weak = true; 1678 return (mark_type == FAN_MARK_INODE) ? 0 : err; 1679 } 1680 1681 /* Check if filesystem can encode a unique fid */ 1682 static int fanotify_test_fid(struct dentry *dentry, unsigned int flags) 1683 { 1684 unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS; 1685 const struct export_operations *nop = dentry->d_sb->s_export_op; 1686 1687 /* 1688 * We need to make sure that the filesystem supports encoding of 1689 * file handles so user can use name_to_handle_at() to compare fids 1690 * reported with events to the file handle of watched objects. 1691 */ 1692 if (!exportfs_can_encode_fid(nop)) 1693 return -EOPNOTSUPP; 1694 1695 /* 1696 * For sb/mount mark, we also need to make sure that the filesystem 1697 * supports decoding file handles, so user has a way to map back the 1698 * reported fids to filesystem objects. 1699 */ 1700 if (mark_type != FAN_MARK_INODE && !exportfs_can_decode_fh(nop)) 1701 return -EOPNOTSUPP; 1702 1703 return 0; 1704 } 1705 1706 static int fanotify_events_supported(struct fsnotify_group *group, 1707 const struct path *path, __u64 mask, 1708 unsigned int flags) 1709 { 1710 unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS; 1711 bool is_dir = d_is_dir(path->dentry); 1712 /* Strict validation of events in non-dir inode mask with v5.17+ APIs */ 1713 bool strict_dir_events = FAN_GROUP_FLAG(group, FAN_REPORT_TARGET_FID) || 1714 (mask & FAN_RENAME) || 1715 (flags & FAN_MARK_IGNORE); 1716 1717 /* 1718 * Filesystems need to opt-into pre-content evnets (a.k.a HSM) 1719 * and they are only supported on regular files and directories. 1720 */ 1721 if (mask & FANOTIFY_PRE_CONTENT_EVENTS) { 1722 if (!(path->mnt->mnt_sb->s_iflags & SB_I_ALLOW_HSM)) 1723 return -EOPNOTSUPP; 1724 if (!is_dir && !d_is_reg(path->dentry)) 1725 return -EINVAL; 1726 } 1727 1728 /* 1729 * Some filesystems such as 'proc' acquire unusual locks when opening 1730 * files. For them fanotify permission events have high chances of 1731 * deadlocking the system - open done when reporting fanotify event 1732 * blocks on this "unusual" lock while another process holding the lock 1733 * waits for fanotify permission event to be answered. Just disallow 1734 * permission events for such filesystems. 1735 */ 1736 if (mask & FANOTIFY_PERM_EVENTS && 1737 path->mnt->mnt_sb->s_type->fs_flags & FS_DISALLOW_NOTIFY_PERM) 1738 return -EINVAL; 1739 1740 /* 1741 * mount and sb marks are not allowed on kernel internal pseudo fs, 1742 * like pipe_mnt, because that would subscribe to events on all the 1743 * anonynous pipes in the system. 1744 * 1745 * SB_NOUSER covers all of the internal pseudo fs whose objects are not 1746 * exposed to user's mount namespace, but there are other SB_KERNMOUNT 1747 * fs, like nsfs, debugfs, for which the value of allowing sb and mount 1748 * mark is questionable. For now we leave them alone. 1749 */ 1750 if (mark_type != FAN_MARK_INODE && 1751 path->mnt->mnt_sb->s_flags & SB_NOUSER) 1752 return -EINVAL; 1753 1754 /* 1755 * We shouldn't have allowed setting dirent events and the directory 1756 * flags FAN_ONDIR and FAN_EVENT_ON_CHILD in mask of non-dir inode, 1757 * but because we always allowed it, error only when using new APIs. 1758 */ 1759 if (strict_dir_events && mark_type == FAN_MARK_INODE && 1760 !is_dir && (mask & FANOTIFY_DIRONLY_EVENT_BITS)) 1761 return -ENOTDIR; 1762 1763 return 0; 1764 } 1765 1766 static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, 1767 int dfd, const char __user *pathname) 1768 { 1769 struct inode *inode = NULL; 1770 struct vfsmount *mnt = NULL; 1771 struct fsnotify_group *group; 1772 struct path path; 1773 struct fan_fsid __fsid, *fsid = NULL; 1774 u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS; 1775 unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS; 1776 unsigned int mark_cmd = flags & FANOTIFY_MARK_CMD_BITS; 1777 unsigned int ignore = flags & FANOTIFY_MARK_IGNORE_BITS; 1778 unsigned int obj_type, fid_mode; 1779 void *obj; 1780 u32 umask = 0; 1781 int ret; 1782 1783 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n", 1784 __func__, fanotify_fd, flags, dfd, pathname, mask); 1785 1786 /* we only use the lower 32 bits as of right now. */ 1787 if (upper_32_bits(mask)) 1788 return -EINVAL; 1789 1790 if (flags & ~FANOTIFY_MARK_FLAGS) 1791 return -EINVAL; 1792 1793 switch (mark_type) { 1794 case FAN_MARK_INODE: 1795 obj_type = FSNOTIFY_OBJ_TYPE_INODE; 1796 break; 1797 case FAN_MARK_MOUNT: 1798 obj_type = FSNOTIFY_OBJ_TYPE_VFSMOUNT; 1799 break; 1800 case FAN_MARK_FILESYSTEM: 1801 obj_type = FSNOTIFY_OBJ_TYPE_SB; 1802 break; 1803 default: 1804 return -EINVAL; 1805 } 1806 1807 switch (mark_cmd) { 1808 case FAN_MARK_ADD: 1809 case FAN_MARK_REMOVE: 1810 if (!mask) 1811 return -EINVAL; 1812 break; 1813 case FAN_MARK_FLUSH: 1814 if (flags & ~(FANOTIFY_MARK_TYPE_BITS | FAN_MARK_FLUSH)) 1815 return -EINVAL; 1816 break; 1817 default: 1818 return -EINVAL; 1819 } 1820 1821 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) 1822 valid_mask |= FANOTIFY_PERM_EVENTS; 1823 1824 if (mask & ~valid_mask) 1825 return -EINVAL; 1826 1827 1828 /* We don't allow FAN_MARK_IGNORE & FAN_MARK_IGNORED_MASK together */ 1829 if (ignore == (FAN_MARK_IGNORE | FAN_MARK_IGNORED_MASK)) 1830 return -EINVAL; 1831 1832 /* 1833 * Event flags (FAN_ONDIR, FAN_EVENT_ON_CHILD) have no effect with 1834 * FAN_MARK_IGNORED_MASK. 1835 */ 1836 if (ignore == FAN_MARK_IGNORED_MASK) { 1837 mask &= ~FANOTIFY_EVENT_FLAGS; 1838 umask = FANOTIFY_EVENT_FLAGS; 1839 } 1840 1841 CLASS(fd, f)(fanotify_fd); 1842 if (fd_empty(f)) 1843 return -EBADF; 1844 1845 /* verify that this is indeed an fanotify instance */ 1846 if (unlikely(fd_file(f)->f_op != &fanotify_fops)) 1847 return -EINVAL; 1848 group = fd_file(f)->private_data; 1849 1850 /* 1851 * An unprivileged user is not allowed to setup mount nor filesystem 1852 * marks. This also includes setting up such marks by a group that 1853 * was initialized by an unprivileged user. 1854 */ 1855 if ((!capable(CAP_SYS_ADMIN) || 1856 FAN_GROUP_FLAG(group, FANOTIFY_UNPRIV)) && 1857 mark_type != FAN_MARK_INODE) 1858 return -EPERM; 1859 1860 /* 1861 * Permission events are not allowed for FAN_CLASS_NOTIF. 1862 * Pre-content permission events are not allowed for FAN_CLASS_CONTENT. 1863 */ 1864 if (mask & FANOTIFY_PERM_EVENTS && 1865 group->priority == FSNOTIFY_PRIO_NORMAL) 1866 return -EINVAL; 1867 else if (mask & FANOTIFY_PRE_CONTENT_EVENTS && 1868 group->priority == FSNOTIFY_PRIO_CONTENT) 1869 return -EINVAL; 1870 1871 if (mask & FAN_FS_ERROR && 1872 mark_type != FAN_MARK_FILESYSTEM) 1873 return -EINVAL; 1874 1875 /* 1876 * Evictable is only relevant for inode marks, because only inode object 1877 * can be evicted on memory pressure. 1878 */ 1879 if (flags & FAN_MARK_EVICTABLE && 1880 mark_type != FAN_MARK_INODE) 1881 return -EINVAL; 1882 1883 /* 1884 * Events that do not carry enough information to report 1885 * event->fd require a group that supports reporting fid. Those 1886 * events are not supported on a mount mark, because they do not 1887 * carry enough information (i.e. path) to be filtered by mount 1888 * point. 1889 */ 1890 fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS); 1891 if (mask & ~(FANOTIFY_FD_EVENTS|FANOTIFY_EVENT_FLAGS) && 1892 (!fid_mode || mark_type == FAN_MARK_MOUNT)) 1893 return -EINVAL; 1894 1895 /* 1896 * FAN_RENAME uses special info type records to report the old and 1897 * new parent+name. Reporting only old and new parent id is less 1898 * useful and was not implemented. 1899 */ 1900 if (mask & FAN_RENAME && !(fid_mode & FAN_REPORT_NAME)) 1901 return -EINVAL; 1902 1903 /* Pre-content events are not currently generated for directories. */ 1904 if (mask & FANOTIFY_PRE_CONTENT_EVENTS && mask & FAN_ONDIR) 1905 return -EINVAL; 1906 1907 if (mark_cmd == FAN_MARK_FLUSH) { 1908 if (mark_type == FAN_MARK_MOUNT) 1909 fsnotify_clear_vfsmount_marks_by_group(group); 1910 else if (mark_type == FAN_MARK_FILESYSTEM) 1911 fsnotify_clear_sb_marks_by_group(group); 1912 else 1913 fsnotify_clear_inode_marks_by_group(group); 1914 return 0; 1915 } 1916 1917 ret = fanotify_find_path(dfd, pathname, &path, flags, 1918 (mask & ALL_FSNOTIFY_EVENTS), obj_type); 1919 if (ret) 1920 return ret; 1921 1922 if (mark_cmd == FAN_MARK_ADD) { 1923 ret = fanotify_events_supported(group, &path, mask, flags); 1924 if (ret) 1925 goto path_put_and_out; 1926 } 1927 1928 if (fid_mode) { 1929 ret = fanotify_test_fsid(path.dentry, flags, &__fsid); 1930 if (ret) 1931 goto path_put_and_out; 1932 1933 ret = fanotify_test_fid(path.dentry, flags); 1934 if (ret) 1935 goto path_put_and_out; 1936 1937 fsid = &__fsid; 1938 } 1939 1940 /* inode held in place by reference to path; group by fget on fd */ 1941 if (mark_type == FAN_MARK_INODE) { 1942 inode = path.dentry->d_inode; 1943 obj = inode; 1944 } else { 1945 mnt = path.mnt; 1946 if (mark_type == FAN_MARK_MOUNT) 1947 obj = mnt; 1948 else 1949 obj = mnt->mnt_sb; 1950 } 1951 1952 /* 1953 * If some other task has this inode open for write we should not add 1954 * an ignore mask, unless that ignore mask is supposed to survive 1955 * modification changes anyway. 1956 */ 1957 if (mark_cmd == FAN_MARK_ADD && (flags & FANOTIFY_MARK_IGNORE_BITS) && 1958 !(flags & FAN_MARK_IGNORED_SURV_MODIFY)) { 1959 ret = mnt ? -EINVAL : -EISDIR; 1960 /* FAN_MARK_IGNORE requires SURV_MODIFY for sb/mount/dir marks */ 1961 if (ignore == FAN_MARK_IGNORE && 1962 (mnt || S_ISDIR(inode->i_mode))) 1963 goto path_put_and_out; 1964 1965 ret = 0; 1966 if (inode && inode_is_open_for_write(inode)) 1967 goto path_put_and_out; 1968 } 1969 1970 /* Mask out FAN_EVENT_ON_CHILD flag for sb/mount/non-dir marks */ 1971 if (mnt || !S_ISDIR(inode->i_mode)) { 1972 mask &= ~FAN_EVENT_ON_CHILD; 1973 umask = FAN_EVENT_ON_CHILD; 1974 /* 1975 * If group needs to report parent fid, register for getting 1976 * events with parent/name info for non-directory. 1977 */ 1978 if ((fid_mode & FAN_REPORT_DIR_FID) && 1979 (flags & FAN_MARK_ADD) && !ignore) 1980 mask |= FAN_EVENT_ON_CHILD; 1981 } 1982 1983 /* create/update an inode mark */ 1984 switch (mark_cmd) { 1985 case FAN_MARK_ADD: 1986 ret = fanotify_add_mark(group, obj, obj_type, mask, flags, 1987 fsid); 1988 break; 1989 case FAN_MARK_REMOVE: 1990 ret = fanotify_remove_mark(group, obj, obj_type, mask, flags, 1991 umask); 1992 break; 1993 default: 1994 ret = -EINVAL; 1995 } 1996 1997 path_put_and_out: 1998 path_put(&path); 1999 return ret; 2000 } 2001 2002 #ifndef CONFIG_ARCH_SPLIT_ARG64 2003 SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags, 2004 __u64, mask, int, dfd, 2005 const char __user *, pathname) 2006 { 2007 return do_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname); 2008 } 2009 #endif 2010 2011 #if defined(CONFIG_ARCH_SPLIT_ARG64) || defined(CONFIG_COMPAT) 2012 SYSCALL32_DEFINE6(fanotify_mark, 2013 int, fanotify_fd, unsigned int, flags, 2014 SC_ARG64(mask), int, dfd, 2015 const char __user *, pathname) 2016 { 2017 return do_fanotify_mark(fanotify_fd, flags, SC_VAL64(__u64, mask), 2018 dfd, pathname); 2019 } 2020 #endif 2021 2022 /* 2023 * fanotify_user_setup - Our initialization function. Note that we cannot return 2024 * error because we have compiled-in VFS hooks. So an (unlikely) failure here 2025 * must result in panic(). 2026 */ 2027 static int __init fanotify_user_setup(void) 2028 { 2029 struct sysinfo si; 2030 int max_marks; 2031 2032 si_meminfo(&si); 2033 /* 2034 * Allow up to 1% of addressable memory to be accounted for per user 2035 * marks limited to the range [8192, 1048576]. mount and sb marks are 2036 * a lot cheaper than inode marks, but there is no reason for a user 2037 * to have many of those, so calculate by the cost of inode marks. 2038 */ 2039 max_marks = (((si.totalram - si.totalhigh) / 100) << PAGE_SHIFT) / 2040 INODE_MARK_COST; 2041 max_marks = clamp(max_marks, FANOTIFY_OLD_DEFAULT_MAX_MARKS, 2042 FANOTIFY_DEFAULT_MAX_USER_MARKS); 2043 2044 BUILD_BUG_ON(FANOTIFY_INIT_FLAGS & FANOTIFY_INTERNAL_GROUP_FLAGS); 2045 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 13); 2046 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 11); 2047 2048 fanotify_mark_cache = KMEM_CACHE(fanotify_mark, 2049 SLAB_PANIC|SLAB_ACCOUNT); 2050 fanotify_fid_event_cachep = KMEM_CACHE(fanotify_fid_event, 2051 SLAB_PANIC); 2052 fanotify_path_event_cachep = KMEM_CACHE(fanotify_path_event, 2053 SLAB_PANIC); 2054 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) { 2055 fanotify_perm_event_cachep = 2056 KMEM_CACHE(fanotify_perm_event, SLAB_PANIC); 2057 } 2058 2059 fanotify_max_queued_events = FANOTIFY_DEFAULT_MAX_EVENTS; 2060 init_user_ns.ucount_max[UCOUNT_FANOTIFY_GROUPS] = 2061 FANOTIFY_DEFAULT_MAX_GROUPS; 2062 init_user_ns.ucount_max[UCOUNT_FANOTIFY_MARKS] = max_marks; 2063 fanotify_sysctls_init(); 2064 2065 return 0; 2066 } 2067 device_initcall(fanotify_user_setup); 2068