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