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 "../../mount.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 33 /* 34 * Legacy fanotify marks limits (8192) is per group and we introduced a tunable 35 * limit of marks per user, similar to inotify. Effectively, the legacy limit 36 * of fanotify marks per user is <max marks per group> * <max groups per user>. 37 * This default limit (1M) also happens to match the increased limit of inotify 38 * max_user_watches since v5.10. 39 */ 40 #define FANOTIFY_DEFAULT_MAX_USER_MARKS \ 41 (FANOTIFY_OLD_DEFAULT_MAX_MARKS * FANOTIFY_DEFAULT_MAX_GROUPS) 42 43 /* 44 * Most of the memory cost of adding an inode mark is pinning the marked inode. 45 * The size of the filesystem inode struct is not uniform across filesystems, 46 * so double the size of a VFS inode is used as a conservative approximation. 47 */ 48 #define INODE_MARK_COST (2 * sizeof(struct inode)) 49 50 /* configurable via /proc/sys/fs/fanotify/ */ 51 static int fanotify_max_queued_events __read_mostly; 52 53 #ifdef CONFIG_SYSCTL 54 55 #include <linux/sysctl.h> 56 57 struct ctl_table fanotify_table[] = { 58 { 59 .procname = "max_user_groups", 60 .data = &init_user_ns.ucount_max[UCOUNT_FANOTIFY_GROUPS], 61 .maxlen = sizeof(int), 62 .mode = 0644, 63 .proc_handler = proc_dointvec_minmax, 64 .extra1 = SYSCTL_ZERO, 65 }, 66 { 67 .procname = "max_user_marks", 68 .data = &init_user_ns.ucount_max[UCOUNT_FANOTIFY_MARKS], 69 .maxlen = sizeof(int), 70 .mode = 0644, 71 .proc_handler = proc_dointvec_minmax, 72 .extra1 = SYSCTL_ZERO, 73 }, 74 { 75 .procname = "max_queued_events", 76 .data = &fanotify_max_queued_events, 77 .maxlen = sizeof(int), 78 .mode = 0644, 79 .proc_handler = proc_dointvec_minmax, 80 .extra1 = SYSCTL_ZERO 81 }, 82 { } 83 }; 84 #endif /* CONFIG_SYSCTL */ 85 86 /* 87 * All flags that may be specified in parameter event_f_flags of fanotify_init. 88 * 89 * Internal and external open flags are stored together in field f_flags of 90 * struct file. Only external open flags shall be allowed in event_f_flags. 91 * Internal flags like FMODE_NONOTIFY, FMODE_EXEC, FMODE_NOCMTIME shall be 92 * excluded. 93 */ 94 #define FANOTIFY_INIT_ALL_EVENT_F_BITS ( \ 95 O_ACCMODE | O_APPEND | O_NONBLOCK | \ 96 __O_SYNC | O_DSYNC | O_CLOEXEC | \ 97 O_LARGEFILE | O_NOATIME ) 98 99 extern const struct fsnotify_ops fanotify_fsnotify_ops; 100 101 struct kmem_cache *fanotify_mark_cache __read_mostly; 102 struct kmem_cache *fanotify_fid_event_cachep __read_mostly; 103 struct kmem_cache *fanotify_path_event_cachep __read_mostly; 104 struct kmem_cache *fanotify_perm_event_cachep __read_mostly; 105 106 #define FANOTIFY_EVENT_ALIGN 4 107 #define FANOTIFY_INFO_HDR_LEN \ 108 (sizeof(struct fanotify_event_info_fid) + sizeof(struct file_handle)) 109 110 static int fanotify_fid_info_len(int fh_len, int name_len) 111 { 112 int info_len = fh_len; 113 114 if (name_len) 115 info_len += name_len + 1; 116 117 return roundup(FANOTIFY_INFO_HDR_LEN + info_len, FANOTIFY_EVENT_ALIGN); 118 } 119 120 static int fanotify_event_info_len(unsigned int fid_mode, 121 struct fanotify_event *event) 122 { 123 struct fanotify_info *info = fanotify_event_info(event); 124 int dir_fh_len = fanotify_event_dir_fh_len(event); 125 int fh_len = fanotify_event_object_fh_len(event); 126 int info_len = 0; 127 int dot_len = 0; 128 129 if (dir_fh_len) { 130 info_len += fanotify_fid_info_len(dir_fh_len, info->name_len); 131 } else if ((fid_mode & FAN_REPORT_NAME) && (event->mask & FAN_ONDIR)) { 132 /* 133 * With group flag FAN_REPORT_NAME, if name was not recorded in 134 * event on a directory, we will report the name ".". 135 */ 136 dot_len = 1; 137 } 138 139 if (fh_len) 140 info_len += fanotify_fid_info_len(fh_len, dot_len); 141 142 return info_len; 143 } 144 145 /* 146 * Remove an hashed event from merge hash table. 147 */ 148 static void fanotify_unhash_event(struct fsnotify_group *group, 149 struct fanotify_event *event) 150 { 151 assert_spin_locked(&group->notification_lock); 152 153 pr_debug("%s: group=%p event=%p bucket=%u\n", __func__, 154 group, event, fanotify_event_hash_bucket(group, event)); 155 156 if (WARN_ON_ONCE(hlist_unhashed(&event->merge_list))) 157 return; 158 159 hlist_del_init(&event->merge_list); 160 } 161 162 /* 163 * Get an fanotify notification event if one exists and is small 164 * enough to fit in "count". Return an error pointer if the count 165 * is not large enough. When permission event is dequeued, its state is 166 * updated accordingly. 167 */ 168 static struct fanotify_event *get_one_event(struct fsnotify_group *group, 169 size_t count) 170 { 171 size_t event_size = FAN_EVENT_METADATA_LEN; 172 struct fanotify_event *event = NULL; 173 struct fsnotify_event *fsn_event; 174 unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS); 175 176 pr_debug("%s: group=%p count=%zd\n", __func__, group, count); 177 178 spin_lock(&group->notification_lock); 179 fsn_event = fsnotify_peek_first_event(group); 180 if (!fsn_event) 181 goto out; 182 183 event = FANOTIFY_E(fsn_event); 184 if (fid_mode) 185 event_size += fanotify_event_info_len(fid_mode, event); 186 187 if (event_size > count) { 188 event = ERR_PTR(-EINVAL); 189 goto out; 190 } 191 192 /* 193 * Held the notification_lock the whole time, so this is the 194 * same event we peeked above. 195 */ 196 fsnotify_remove_first_event(group); 197 if (fanotify_is_perm_event(event->mask)) 198 FANOTIFY_PERM(event)->state = FAN_EVENT_REPORTED; 199 if (fanotify_is_hashed_event(event->mask)) 200 fanotify_unhash_event(group, event); 201 out: 202 spin_unlock(&group->notification_lock); 203 return event; 204 } 205 206 static int create_fd(struct fsnotify_group *group, struct path *path, 207 struct file **file) 208 { 209 int client_fd; 210 struct file *new_file; 211 212 client_fd = get_unused_fd_flags(group->fanotify_data.f_flags); 213 if (client_fd < 0) 214 return client_fd; 215 216 /* 217 * we need a new file handle for the userspace program so it can read even if it was 218 * originally opened O_WRONLY. 219 */ 220 new_file = dentry_open(path, 221 group->fanotify_data.f_flags | FMODE_NONOTIFY, 222 current_cred()); 223 if (IS_ERR(new_file)) { 224 /* 225 * we still send an event even if we can't open the file. this 226 * can happen when say tasks are gone and we try to open their 227 * /proc files or we try to open a WRONLY file like in sysfs 228 * we just send the errno to userspace since there isn't much 229 * else we can do. 230 */ 231 put_unused_fd(client_fd); 232 client_fd = PTR_ERR(new_file); 233 } else { 234 *file = new_file; 235 } 236 237 return client_fd; 238 } 239 240 /* 241 * Finish processing of permission event by setting it to ANSWERED state and 242 * drop group->notification_lock. 243 */ 244 static void finish_permission_event(struct fsnotify_group *group, 245 struct fanotify_perm_event *event, 246 unsigned int response) 247 __releases(&group->notification_lock) 248 { 249 bool destroy = false; 250 251 assert_spin_locked(&group->notification_lock); 252 event->response = response; 253 if (event->state == FAN_EVENT_CANCELED) 254 destroy = true; 255 else 256 event->state = FAN_EVENT_ANSWERED; 257 spin_unlock(&group->notification_lock); 258 if (destroy) 259 fsnotify_destroy_event(group, &event->fae.fse); 260 } 261 262 static int process_access_response(struct fsnotify_group *group, 263 struct fanotify_response *response_struct) 264 { 265 struct fanotify_perm_event *event; 266 int fd = response_struct->fd; 267 int response = response_struct->response; 268 269 pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group, 270 fd, response); 271 /* 272 * make sure the response is valid, if invalid we do nothing and either 273 * userspace can send a valid response or we will clean it up after the 274 * timeout 275 */ 276 switch (response & ~FAN_AUDIT) { 277 case FAN_ALLOW: 278 case FAN_DENY: 279 break; 280 default: 281 return -EINVAL; 282 } 283 284 if (fd < 0) 285 return -EINVAL; 286 287 if ((response & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT)) 288 return -EINVAL; 289 290 spin_lock(&group->notification_lock); 291 list_for_each_entry(event, &group->fanotify_data.access_list, 292 fae.fse.list) { 293 if (event->fd != fd) 294 continue; 295 296 list_del_init(&event->fae.fse.list); 297 finish_permission_event(group, event, response); 298 wake_up(&group->fanotify_data.access_waitq); 299 return 0; 300 } 301 spin_unlock(&group->notification_lock); 302 303 return -ENOENT; 304 } 305 306 static int copy_info_to_user(__kernel_fsid_t *fsid, struct fanotify_fh *fh, 307 int info_type, const char *name, size_t name_len, 308 char __user *buf, size_t count) 309 { 310 struct fanotify_event_info_fid info = { }; 311 struct file_handle handle = { }; 312 unsigned char bounce[FANOTIFY_INLINE_FH_LEN], *fh_buf; 313 size_t fh_len = fh ? fh->len : 0; 314 size_t info_len = fanotify_fid_info_len(fh_len, name_len); 315 size_t len = info_len; 316 317 pr_debug("%s: fh_len=%zu name_len=%zu, info_len=%zu, count=%zu\n", 318 __func__, fh_len, name_len, info_len, count); 319 320 if (!fh_len) 321 return 0; 322 323 if (WARN_ON_ONCE(len < sizeof(info) || len > count)) 324 return -EFAULT; 325 326 /* 327 * Copy event info fid header followed by variable sized file handle 328 * and optionally followed by variable sized filename. 329 */ 330 switch (info_type) { 331 case FAN_EVENT_INFO_TYPE_FID: 332 case FAN_EVENT_INFO_TYPE_DFID: 333 if (WARN_ON_ONCE(name_len)) 334 return -EFAULT; 335 break; 336 case FAN_EVENT_INFO_TYPE_DFID_NAME: 337 if (WARN_ON_ONCE(!name || !name_len)) 338 return -EFAULT; 339 break; 340 default: 341 return -EFAULT; 342 } 343 344 info.hdr.info_type = info_type; 345 info.hdr.len = len; 346 info.fsid = *fsid; 347 if (copy_to_user(buf, &info, sizeof(info))) 348 return -EFAULT; 349 350 buf += sizeof(info); 351 len -= sizeof(info); 352 if (WARN_ON_ONCE(len < sizeof(handle))) 353 return -EFAULT; 354 355 handle.handle_type = fh->type; 356 handle.handle_bytes = fh_len; 357 if (copy_to_user(buf, &handle, sizeof(handle))) 358 return -EFAULT; 359 360 buf += sizeof(handle); 361 len -= sizeof(handle); 362 if (WARN_ON_ONCE(len < fh_len)) 363 return -EFAULT; 364 365 /* 366 * For an inline fh and inline file name, copy through stack to exclude 367 * the copy from usercopy hardening protections. 368 */ 369 fh_buf = fanotify_fh_buf(fh); 370 if (fh_len <= FANOTIFY_INLINE_FH_LEN) { 371 memcpy(bounce, fh_buf, fh_len); 372 fh_buf = bounce; 373 } 374 if (copy_to_user(buf, fh_buf, fh_len)) 375 return -EFAULT; 376 377 buf += fh_len; 378 len -= fh_len; 379 380 if (name_len) { 381 /* Copy the filename with terminating null */ 382 name_len++; 383 if (WARN_ON_ONCE(len < name_len)) 384 return -EFAULT; 385 386 if (copy_to_user(buf, name, name_len)) 387 return -EFAULT; 388 389 buf += name_len; 390 len -= name_len; 391 } 392 393 /* Pad with 0's */ 394 WARN_ON_ONCE(len < 0 || len >= FANOTIFY_EVENT_ALIGN); 395 if (len > 0 && clear_user(buf, len)) 396 return -EFAULT; 397 398 return info_len; 399 } 400 401 static ssize_t copy_event_to_user(struct fsnotify_group *group, 402 struct fanotify_event *event, 403 char __user *buf, size_t count) 404 { 405 struct fanotify_event_metadata metadata; 406 struct path *path = fanotify_event_path(event); 407 struct fanotify_info *info = fanotify_event_info(event); 408 unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS); 409 struct file *f = NULL; 410 int ret, fd = FAN_NOFD; 411 int info_type = 0; 412 413 pr_debug("%s: group=%p event=%p\n", __func__, group, event); 414 415 metadata.event_len = FAN_EVENT_METADATA_LEN + 416 fanotify_event_info_len(fid_mode, event); 417 metadata.metadata_len = FAN_EVENT_METADATA_LEN; 418 metadata.vers = FANOTIFY_METADATA_VERSION; 419 metadata.reserved = 0; 420 metadata.mask = event->mask & FANOTIFY_OUTGOING_EVENTS; 421 metadata.pid = pid_vnr(event->pid); 422 /* 423 * For an unprivileged listener, event->pid can be used to identify the 424 * events generated by the listener process itself, without disclosing 425 * the pids of other processes. 426 */ 427 if (FAN_GROUP_FLAG(group, FANOTIFY_UNPRIV) && 428 task_tgid(current) != event->pid) 429 metadata.pid = 0; 430 431 /* 432 * For now, fid mode is required for an unprivileged listener and 433 * fid mode does not report fd in events. Keep this check anyway 434 * for safety in case fid mode requirement is relaxed in the future 435 * to allow unprivileged listener to get events with no fd and no fid. 436 */ 437 if (!FAN_GROUP_FLAG(group, FANOTIFY_UNPRIV) && 438 path && path->mnt && path->dentry) { 439 fd = create_fd(group, path, &f); 440 if (fd < 0) 441 return fd; 442 } 443 metadata.fd = fd; 444 445 ret = -EFAULT; 446 /* 447 * Sanity check copy size in case get_one_event() and 448 * event_len sizes ever get out of sync. 449 */ 450 if (WARN_ON_ONCE(metadata.event_len > count)) 451 goto out_close_fd; 452 453 if (copy_to_user(buf, &metadata, FAN_EVENT_METADATA_LEN)) 454 goto out_close_fd; 455 456 buf += FAN_EVENT_METADATA_LEN; 457 count -= FAN_EVENT_METADATA_LEN; 458 459 if (fanotify_is_perm_event(event->mask)) 460 FANOTIFY_PERM(event)->fd = fd; 461 462 if (f) 463 fd_install(fd, f); 464 465 /* Event info records order is: dir fid + name, child fid */ 466 if (fanotify_event_dir_fh_len(event)) { 467 info_type = info->name_len ? FAN_EVENT_INFO_TYPE_DFID_NAME : 468 FAN_EVENT_INFO_TYPE_DFID; 469 ret = copy_info_to_user(fanotify_event_fsid(event), 470 fanotify_info_dir_fh(info), 471 info_type, fanotify_info_name(info), 472 info->name_len, buf, count); 473 if (ret < 0) 474 goto out_close_fd; 475 476 buf += ret; 477 count -= ret; 478 } 479 480 if (fanotify_event_object_fh_len(event)) { 481 const char *dot = NULL; 482 int dot_len = 0; 483 484 if (fid_mode == FAN_REPORT_FID || info_type) { 485 /* 486 * With only group flag FAN_REPORT_FID only type FID is 487 * reported. Second info record type is always FID. 488 */ 489 info_type = FAN_EVENT_INFO_TYPE_FID; 490 } else if ((fid_mode & FAN_REPORT_NAME) && 491 (event->mask & FAN_ONDIR)) { 492 /* 493 * With group flag FAN_REPORT_NAME, if name was not 494 * recorded in an event on a directory, report the 495 * name "." with info type DFID_NAME. 496 */ 497 info_type = FAN_EVENT_INFO_TYPE_DFID_NAME; 498 dot = "."; 499 dot_len = 1; 500 } else if ((event->mask & ALL_FSNOTIFY_DIRENT_EVENTS) || 501 (event->mask & FAN_ONDIR)) { 502 /* 503 * With group flag FAN_REPORT_DIR_FID, a single info 504 * record has type DFID for directory entry modification 505 * event and for event on a directory. 506 */ 507 info_type = FAN_EVENT_INFO_TYPE_DFID; 508 } else { 509 /* 510 * With group flags FAN_REPORT_DIR_FID|FAN_REPORT_FID, 511 * a single info record has type FID for event on a 512 * non-directory, when there is no directory to report. 513 * For example, on FAN_DELETE_SELF event. 514 */ 515 info_type = FAN_EVENT_INFO_TYPE_FID; 516 } 517 518 ret = copy_info_to_user(fanotify_event_fsid(event), 519 fanotify_event_object_fh(event), 520 info_type, dot, dot_len, buf, count); 521 if (ret < 0) 522 goto out_close_fd; 523 524 buf += ret; 525 count -= ret; 526 } 527 528 return metadata.event_len; 529 530 out_close_fd: 531 if (fd != FAN_NOFD) { 532 put_unused_fd(fd); 533 fput(f); 534 } 535 return ret; 536 } 537 538 /* intofiy userspace file descriptor functions */ 539 static __poll_t fanotify_poll(struct file *file, poll_table *wait) 540 { 541 struct fsnotify_group *group = file->private_data; 542 __poll_t ret = 0; 543 544 poll_wait(file, &group->notification_waitq, wait); 545 spin_lock(&group->notification_lock); 546 if (!fsnotify_notify_queue_is_empty(group)) 547 ret = EPOLLIN | EPOLLRDNORM; 548 spin_unlock(&group->notification_lock); 549 550 return ret; 551 } 552 553 static ssize_t fanotify_read(struct file *file, char __user *buf, 554 size_t count, loff_t *pos) 555 { 556 struct fsnotify_group *group; 557 struct fanotify_event *event; 558 char __user *start; 559 int ret; 560 DEFINE_WAIT_FUNC(wait, woken_wake_function); 561 562 start = buf; 563 group = file->private_data; 564 565 pr_debug("%s: group=%p\n", __func__, group); 566 567 add_wait_queue(&group->notification_waitq, &wait); 568 while (1) { 569 /* 570 * User can supply arbitrarily large buffer. Avoid softlockups 571 * in case there are lots of available events. 572 */ 573 cond_resched(); 574 event = get_one_event(group, count); 575 if (IS_ERR(event)) { 576 ret = PTR_ERR(event); 577 break; 578 } 579 580 if (!event) { 581 ret = -EAGAIN; 582 if (file->f_flags & O_NONBLOCK) 583 break; 584 585 ret = -ERESTARTSYS; 586 if (signal_pending(current)) 587 break; 588 589 if (start != buf) 590 break; 591 592 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); 593 continue; 594 } 595 596 ret = copy_event_to_user(group, event, buf, count); 597 if (unlikely(ret == -EOPENSTALE)) { 598 /* 599 * We cannot report events with stale fd so drop it. 600 * Setting ret to 0 will continue the event loop and 601 * do the right thing if there are no more events to 602 * read (i.e. return bytes read, -EAGAIN or wait). 603 */ 604 ret = 0; 605 } 606 607 /* 608 * Permission events get queued to wait for response. Other 609 * events can be destroyed now. 610 */ 611 if (!fanotify_is_perm_event(event->mask)) { 612 fsnotify_destroy_event(group, &event->fse); 613 } else { 614 if (ret <= 0) { 615 spin_lock(&group->notification_lock); 616 finish_permission_event(group, 617 FANOTIFY_PERM(event), FAN_DENY); 618 wake_up(&group->fanotify_data.access_waitq); 619 } else { 620 spin_lock(&group->notification_lock); 621 list_add_tail(&event->fse.list, 622 &group->fanotify_data.access_list); 623 spin_unlock(&group->notification_lock); 624 } 625 } 626 if (ret < 0) 627 break; 628 buf += ret; 629 count -= ret; 630 } 631 remove_wait_queue(&group->notification_waitq, &wait); 632 633 if (start != buf && ret != -EFAULT) 634 ret = buf - start; 635 return ret; 636 } 637 638 static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos) 639 { 640 struct fanotify_response response = { .fd = -1, .response = -1 }; 641 struct fsnotify_group *group; 642 int ret; 643 644 if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) 645 return -EINVAL; 646 647 group = file->private_data; 648 649 if (count < sizeof(response)) 650 return -EINVAL; 651 652 count = sizeof(response); 653 654 pr_debug("%s: group=%p count=%zu\n", __func__, group, count); 655 656 if (copy_from_user(&response, buf, count)) 657 return -EFAULT; 658 659 ret = process_access_response(group, &response); 660 if (ret < 0) 661 count = ret; 662 663 return count; 664 } 665 666 static int fanotify_release(struct inode *ignored, struct file *file) 667 { 668 struct fsnotify_group *group = file->private_data; 669 struct fsnotify_event *fsn_event; 670 671 /* 672 * Stop new events from arriving in the notification queue. since 673 * userspace cannot use fanotify fd anymore, no event can enter or 674 * leave access_list by now either. 675 */ 676 fsnotify_group_stop_queueing(group); 677 678 /* 679 * Process all permission events on access_list and notification queue 680 * and simulate reply from userspace. 681 */ 682 spin_lock(&group->notification_lock); 683 while (!list_empty(&group->fanotify_data.access_list)) { 684 struct fanotify_perm_event *event; 685 686 event = list_first_entry(&group->fanotify_data.access_list, 687 struct fanotify_perm_event, fae.fse.list); 688 list_del_init(&event->fae.fse.list); 689 finish_permission_event(group, event, FAN_ALLOW); 690 spin_lock(&group->notification_lock); 691 } 692 693 /* 694 * Destroy all non-permission events. For permission events just 695 * dequeue them and set the response. They will be freed once the 696 * response is consumed and fanotify_get_response() returns. 697 */ 698 while ((fsn_event = fsnotify_remove_first_event(group))) { 699 struct fanotify_event *event = FANOTIFY_E(fsn_event); 700 701 if (!(event->mask & FANOTIFY_PERM_EVENTS)) { 702 spin_unlock(&group->notification_lock); 703 fsnotify_destroy_event(group, fsn_event); 704 } else { 705 finish_permission_event(group, FANOTIFY_PERM(event), 706 FAN_ALLOW); 707 } 708 spin_lock(&group->notification_lock); 709 } 710 spin_unlock(&group->notification_lock); 711 712 /* Response for all permission events it set, wakeup waiters */ 713 wake_up(&group->fanotify_data.access_waitq); 714 715 /* matches the fanotify_init->fsnotify_alloc_group */ 716 fsnotify_destroy_group(group); 717 718 return 0; 719 } 720 721 static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 722 { 723 struct fsnotify_group *group; 724 struct fsnotify_event *fsn_event; 725 void __user *p; 726 int ret = -ENOTTY; 727 size_t send_len = 0; 728 729 group = file->private_data; 730 731 p = (void __user *) arg; 732 733 switch (cmd) { 734 case FIONREAD: 735 spin_lock(&group->notification_lock); 736 list_for_each_entry(fsn_event, &group->notification_list, list) 737 send_len += FAN_EVENT_METADATA_LEN; 738 spin_unlock(&group->notification_lock); 739 ret = put_user(send_len, (int __user *) p); 740 break; 741 } 742 743 return ret; 744 } 745 746 static const struct file_operations fanotify_fops = { 747 .show_fdinfo = fanotify_show_fdinfo, 748 .poll = fanotify_poll, 749 .read = fanotify_read, 750 .write = fanotify_write, 751 .fasync = NULL, 752 .release = fanotify_release, 753 .unlocked_ioctl = fanotify_ioctl, 754 .compat_ioctl = compat_ptr_ioctl, 755 .llseek = noop_llseek, 756 }; 757 758 static int fanotify_find_path(int dfd, const char __user *filename, 759 struct path *path, unsigned int flags, __u64 mask, 760 unsigned int obj_type) 761 { 762 int ret; 763 764 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__, 765 dfd, filename, flags); 766 767 if (filename == NULL) { 768 struct fd f = fdget(dfd); 769 770 ret = -EBADF; 771 if (!f.file) 772 goto out; 773 774 ret = -ENOTDIR; 775 if ((flags & FAN_MARK_ONLYDIR) && 776 !(S_ISDIR(file_inode(f.file)->i_mode))) { 777 fdput(f); 778 goto out; 779 } 780 781 *path = f.file->f_path; 782 path_get(path); 783 fdput(f); 784 } else { 785 unsigned int lookup_flags = 0; 786 787 if (!(flags & FAN_MARK_DONT_FOLLOW)) 788 lookup_flags |= LOOKUP_FOLLOW; 789 if (flags & FAN_MARK_ONLYDIR) 790 lookup_flags |= LOOKUP_DIRECTORY; 791 792 ret = user_path_at(dfd, filename, lookup_flags, path); 793 if (ret) 794 goto out; 795 } 796 797 /* you can only watch an inode if you have read permissions on it */ 798 ret = path_permission(path, MAY_READ); 799 if (ret) { 800 path_put(path); 801 goto out; 802 } 803 804 ret = security_path_notify(path, mask, obj_type); 805 if (ret) 806 path_put(path); 807 808 out: 809 return ret; 810 } 811 812 static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark, 813 __u32 mask, unsigned int flags, 814 __u32 umask, int *destroy) 815 { 816 __u32 oldmask = 0; 817 818 /* umask bits cannot be removed by user */ 819 mask &= ~umask; 820 spin_lock(&fsn_mark->lock); 821 if (!(flags & FAN_MARK_IGNORED_MASK)) { 822 oldmask = fsn_mark->mask; 823 fsn_mark->mask &= ~mask; 824 } else { 825 fsn_mark->ignored_mask &= ~mask; 826 } 827 /* 828 * We need to keep the mark around even if remaining mask cannot 829 * result in any events (e.g. mask == FAN_ONDIR) to support incremenal 830 * changes to the mask. 831 * Destroy mark when only umask bits remain. 832 */ 833 *destroy = !((fsn_mark->mask | fsn_mark->ignored_mask) & ~umask); 834 spin_unlock(&fsn_mark->lock); 835 836 return mask & oldmask; 837 } 838 839 static int fanotify_remove_mark(struct fsnotify_group *group, 840 fsnotify_connp_t *connp, __u32 mask, 841 unsigned int flags, __u32 umask) 842 { 843 struct fsnotify_mark *fsn_mark = NULL; 844 __u32 removed; 845 int destroy_mark; 846 847 mutex_lock(&group->mark_mutex); 848 fsn_mark = fsnotify_find_mark(connp, group); 849 if (!fsn_mark) { 850 mutex_unlock(&group->mark_mutex); 851 return -ENOENT; 852 } 853 854 removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags, 855 umask, &destroy_mark); 856 if (removed & fsnotify_conn_mask(fsn_mark->connector)) 857 fsnotify_recalc_mask(fsn_mark->connector); 858 if (destroy_mark) 859 fsnotify_detach_mark(fsn_mark); 860 mutex_unlock(&group->mark_mutex); 861 if (destroy_mark) 862 fsnotify_free_mark(fsn_mark); 863 864 /* matches the fsnotify_find_mark() */ 865 fsnotify_put_mark(fsn_mark); 866 return 0; 867 } 868 869 static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group, 870 struct vfsmount *mnt, __u32 mask, 871 unsigned int flags, __u32 umask) 872 { 873 return fanotify_remove_mark(group, &real_mount(mnt)->mnt_fsnotify_marks, 874 mask, flags, umask); 875 } 876 877 static int fanotify_remove_sb_mark(struct fsnotify_group *group, 878 struct super_block *sb, __u32 mask, 879 unsigned int flags, __u32 umask) 880 { 881 return fanotify_remove_mark(group, &sb->s_fsnotify_marks, mask, 882 flags, umask); 883 } 884 885 static int fanotify_remove_inode_mark(struct fsnotify_group *group, 886 struct inode *inode, __u32 mask, 887 unsigned int flags, __u32 umask) 888 { 889 return fanotify_remove_mark(group, &inode->i_fsnotify_marks, mask, 890 flags, umask); 891 } 892 893 static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark, 894 __u32 mask, 895 unsigned int flags) 896 { 897 __u32 oldmask = -1; 898 899 spin_lock(&fsn_mark->lock); 900 if (!(flags & FAN_MARK_IGNORED_MASK)) { 901 oldmask = fsn_mark->mask; 902 fsn_mark->mask |= mask; 903 } else { 904 fsn_mark->ignored_mask |= mask; 905 if (flags & FAN_MARK_IGNORED_SURV_MODIFY) 906 fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY; 907 } 908 spin_unlock(&fsn_mark->lock); 909 910 return mask & ~oldmask; 911 } 912 913 static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group, 914 fsnotify_connp_t *connp, 915 unsigned int type, 916 __kernel_fsid_t *fsid) 917 { 918 struct ucounts *ucounts = group->fanotify_data.ucounts; 919 struct fsnotify_mark *mark; 920 int ret; 921 922 /* 923 * Enforce per user marks limits per user in all containing user ns. 924 * A group with FAN_UNLIMITED_MARKS does not contribute to mark count 925 * in the limited groups account. 926 */ 927 if (!FAN_GROUP_FLAG(group, FAN_UNLIMITED_MARKS) && 928 !inc_ucount(ucounts->ns, ucounts->uid, UCOUNT_FANOTIFY_MARKS)) 929 return ERR_PTR(-ENOSPC); 930 931 mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL); 932 if (!mark) { 933 ret = -ENOMEM; 934 goto out_dec_ucounts; 935 } 936 937 fsnotify_init_mark(mark, group); 938 ret = fsnotify_add_mark_locked(mark, connp, type, 0, fsid); 939 if (ret) { 940 fsnotify_put_mark(mark); 941 goto out_dec_ucounts; 942 } 943 944 return mark; 945 946 out_dec_ucounts: 947 if (!FAN_GROUP_FLAG(group, FAN_UNLIMITED_MARKS)) 948 dec_ucount(ucounts, UCOUNT_FANOTIFY_MARKS); 949 return ERR_PTR(ret); 950 } 951 952 953 static int fanotify_add_mark(struct fsnotify_group *group, 954 fsnotify_connp_t *connp, unsigned int type, 955 __u32 mask, unsigned int flags, 956 __kernel_fsid_t *fsid) 957 { 958 struct fsnotify_mark *fsn_mark; 959 __u32 added; 960 961 mutex_lock(&group->mark_mutex); 962 fsn_mark = fsnotify_find_mark(connp, group); 963 if (!fsn_mark) { 964 fsn_mark = fanotify_add_new_mark(group, connp, type, fsid); 965 if (IS_ERR(fsn_mark)) { 966 mutex_unlock(&group->mark_mutex); 967 return PTR_ERR(fsn_mark); 968 } 969 } 970 added = fanotify_mark_add_to_mask(fsn_mark, mask, flags); 971 if (added & ~fsnotify_conn_mask(fsn_mark->connector)) 972 fsnotify_recalc_mask(fsn_mark->connector); 973 mutex_unlock(&group->mark_mutex); 974 975 fsnotify_put_mark(fsn_mark); 976 return 0; 977 } 978 979 static int fanotify_add_vfsmount_mark(struct fsnotify_group *group, 980 struct vfsmount *mnt, __u32 mask, 981 unsigned int flags, __kernel_fsid_t *fsid) 982 { 983 return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks, 984 FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags, fsid); 985 } 986 987 static int fanotify_add_sb_mark(struct fsnotify_group *group, 988 struct super_block *sb, __u32 mask, 989 unsigned int flags, __kernel_fsid_t *fsid) 990 { 991 return fanotify_add_mark(group, &sb->s_fsnotify_marks, 992 FSNOTIFY_OBJ_TYPE_SB, mask, flags, fsid); 993 } 994 995 static int fanotify_add_inode_mark(struct fsnotify_group *group, 996 struct inode *inode, __u32 mask, 997 unsigned int flags, __kernel_fsid_t *fsid) 998 { 999 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode); 1000 1001 /* 1002 * If some other task has this inode open for write we should not add 1003 * an ignored mark, unless that ignored mark is supposed to survive 1004 * modification changes anyway. 1005 */ 1006 if ((flags & FAN_MARK_IGNORED_MASK) && 1007 !(flags & FAN_MARK_IGNORED_SURV_MODIFY) && 1008 inode_is_open_for_write(inode)) 1009 return 0; 1010 1011 return fanotify_add_mark(group, &inode->i_fsnotify_marks, 1012 FSNOTIFY_OBJ_TYPE_INODE, mask, flags, fsid); 1013 } 1014 1015 static struct fsnotify_event *fanotify_alloc_overflow_event(void) 1016 { 1017 struct fanotify_event *oevent; 1018 1019 oevent = kmalloc(sizeof(*oevent), GFP_KERNEL_ACCOUNT); 1020 if (!oevent) 1021 return NULL; 1022 1023 fanotify_init_event(oevent, 0, FS_Q_OVERFLOW); 1024 oevent->type = FANOTIFY_EVENT_TYPE_OVERFLOW; 1025 1026 return &oevent->fse; 1027 } 1028 1029 static struct hlist_head *fanotify_alloc_merge_hash(void) 1030 { 1031 struct hlist_head *hash; 1032 1033 hash = kmalloc(sizeof(struct hlist_head) << FANOTIFY_HTABLE_BITS, 1034 GFP_KERNEL_ACCOUNT); 1035 if (!hash) 1036 return NULL; 1037 1038 __hash_init(hash, FANOTIFY_HTABLE_SIZE); 1039 1040 return hash; 1041 } 1042 1043 /* fanotify syscalls */ 1044 SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) 1045 { 1046 struct fsnotify_group *group; 1047 int f_flags, fd; 1048 unsigned int fid_mode = flags & FANOTIFY_FID_BITS; 1049 unsigned int class = flags & FANOTIFY_CLASS_BITS; 1050 unsigned int internal_flags = 0; 1051 1052 pr_debug("%s: flags=%x event_f_flags=%x\n", 1053 __func__, flags, event_f_flags); 1054 1055 if (!capable(CAP_SYS_ADMIN)) { 1056 /* 1057 * An unprivileged user can setup an fanotify group with 1058 * limited functionality - an unprivileged group is limited to 1059 * notification events with file handles and it cannot use 1060 * unlimited queue/marks. 1061 */ 1062 if ((flags & FANOTIFY_ADMIN_INIT_FLAGS) || !fid_mode) 1063 return -EPERM; 1064 1065 /* 1066 * Setting the internal flag FANOTIFY_UNPRIV on the group 1067 * prevents setting mount/filesystem marks on this group and 1068 * prevents reporting pid and open fd in events. 1069 */ 1070 internal_flags |= FANOTIFY_UNPRIV; 1071 } 1072 1073 #ifdef CONFIG_AUDITSYSCALL 1074 if (flags & ~(FANOTIFY_INIT_FLAGS | FAN_ENABLE_AUDIT)) 1075 #else 1076 if (flags & ~FANOTIFY_INIT_FLAGS) 1077 #endif 1078 return -EINVAL; 1079 1080 if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS) 1081 return -EINVAL; 1082 1083 switch (event_f_flags & O_ACCMODE) { 1084 case O_RDONLY: 1085 case O_RDWR: 1086 case O_WRONLY: 1087 break; 1088 default: 1089 return -EINVAL; 1090 } 1091 1092 if (fid_mode && class != FAN_CLASS_NOTIF) 1093 return -EINVAL; 1094 1095 /* 1096 * Child name is reported with parent fid so requires dir fid. 1097 * We can report both child fid and dir fid with or without name. 1098 */ 1099 if ((fid_mode & FAN_REPORT_NAME) && !(fid_mode & FAN_REPORT_DIR_FID)) 1100 return -EINVAL; 1101 1102 f_flags = O_RDWR | FMODE_NONOTIFY; 1103 if (flags & FAN_CLOEXEC) 1104 f_flags |= O_CLOEXEC; 1105 if (flags & FAN_NONBLOCK) 1106 f_flags |= O_NONBLOCK; 1107 1108 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */ 1109 group = fsnotify_alloc_user_group(&fanotify_fsnotify_ops); 1110 if (IS_ERR(group)) { 1111 return PTR_ERR(group); 1112 } 1113 1114 /* Enforce groups limits per user in all containing user ns */ 1115 group->fanotify_data.ucounts = inc_ucount(current_user_ns(), 1116 current_euid(), 1117 UCOUNT_FANOTIFY_GROUPS); 1118 if (!group->fanotify_data.ucounts) { 1119 fd = -EMFILE; 1120 goto out_destroy_group; 1121 } 1122 1123 group->fanotify_data.flags = flags | internal_flags; 1124 group->memcg = get_mem_cgroup_from_mm(current->mm); 1125 1126 group->fanotify_data.merge_hash = fanotify_alloc_merge_hash(); 1127 if (!group->fanotify_data.merge_hash) { 1128 fd = -ENOMEM; 1129 goto out_destroy_group; 1130 } 1131 1132 group->overflow_event = fanotify_alloc_overflow_event(); 1133 if (unlikely(!group->overflow_event)) { 1134 fd = -ENOMEM; 1135 goto out_destroy_group; 1136 } 1137 1138 if (force_o_largefile()) 1139 event_f_flags |= O_LARGEFILE; 1140 group->fanotify_data.f_flags = event_f_flags; 1141 init_waitqueue_head(&group->fanotify_data.access_waitq); 1142 INIT_LIST_HEAD(&group->fanotify_data.access_list); 1143 switch (class) { 1144 case FAN_CLASS_NOTIF: 1145 group->priority = FS_PRIO_0; 1146 break; 1147 case FAN_CLASS_CONTENT: 1148 group->priority = FS_PRIO_1; 1149 break; 1150 case FAN_CLASS_PRE_CONTENT: 1151 group->priority = FS_PRIO_2; 1152 break; 1153 default: 1154 fd = -EINVAL; 1155 goto out_destroy_group; 1156 } 1157 1158 if (flags & FAN_UNLIMITED_QUEUE) { 1159 fd = -EPERM; 1160 if (!capable(CAP_SYS_ADMIN)) 1161 goto out_destroy_group; 1162 group->max_events = UINT_MAX; 1163 } else { 1164 group->max_events = fanotify_max_queued_events; 1165 } 1166 1167 if (flags & FAN_UNLIMITED_MARKS) { 1168 fd = -EPERM; 1169 if (!capable(CAP_SYS_ADMIN)) 1170 goto out_destroy_group; 1171 } 1172 1173 if (flags & FAN_ENABLE_AUDIT) { 1174 fd = -EPERM; 1175 if (!capable(CAP_AUDIT_WRITE)) 1176 goto out_destroy_group; 1177 } 1178 1179 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags); 1180 if (fd < 0) 1181 goto out_destroy_group; 1182 1183 return fd; 1184 1185 out_destroy_group: 1186 fsnotify_destroy_group(group); 1187 return fd; 1188 } 1189 1190 /* Check if filesystem can encode a unique fid */ 1191 static int fanotify_test_fid(struct path *path, __kernel_fsid_t *fsid) 1192 { 1193 __kernel_fsid_t root_fsid; 1194 int err; 1195 1196 /* 1197 * Make sure path is not in filesystem with zero fsid (e.g. tmpfs). 1198 */ 1199 err = vfs_get_fsid(path->dentry, fsid); 1200 if (err) 1201 return err; 1202 1203 if (!fsid->val[0] && !fsid->val[1]) 1204 return -ENODEV; 1205 1206 /* 1207 * Make sure path is not inside a filesystem subvolume (e.g. btrfs) 1208 * which uses a different fsid than sb root. 1209 */ 1210 err = vfs_get_fsid(path->dentry->d_sb->s_root, &root_fsid); 1211 if (err) 1212 return err; 1213 1214 if (root_fsid.val[0] != fsid->val[0] || 1215 root_fsid.val[1] != fsid->val[1]) 1216 return -EXDEV; 1217 1218 /* 1219 * We need to make sure that the file system supports at least 1220 * encoding a file handle so user can use name_to_handle_at() to 1221 * compare fid returned with event to the file handle of watched 1222 * objects. However, name_to_handle_at() requires that the 1223 * filesystem also supports decoding file handles. 1224 */ 1225 if (!path->dentry->d_sb->s_export_op || 1226 !path->dentry->d_sb->s_export_op->fh_to_dentry) 1227 return -EOPNOTSUPP; 1228 1229 return 0; 1230 } 1231 1232 static int fanotify_events_supported(struct path *path, __u64 mask) 1233 { 1234 /* 1235 * Some filesystems such as 'proc' acquire unusual locks when opening 1236 * files. For them fanotify permission events have high chances of 1237 * deadlocking the system - open done when reporting fanotify event 1238 * blocks on this "unusual" lock while another process holding the lock 1239 * waits for fanotify permission event to be answered. Just disallow 1240 * permission events for such filesystems. 1241 */ 1242 if (mask & FANOTIFY_PERM_EVENTS && 1243 path->mnt->mnt_sb->s_type->fs_flags & FS_DISALLOW_NOTIFY_PERM) 1244 return -EINVAL; 1245 return 0; 1246 } 1247 1248 static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, 1249 int dfd, const char __user *pathname) 1250 { 1251 struct inode *inode = NULL; 1252 struct vfsmount *mnt = NULL; 1253 struct fsnotify_group *group; 1254 struct fd f; 1255 struct path path; 1256 __kernel_fsid_t __fsid, *fsid = NULL; 1257 u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS; 1258 unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS; 1259 bool ignored = flags & FAN_MARK_IGNORED_MASK; 1260 unsigned int obj_type, fid_mode; 1261 u32 umask = 0; 1262 int ret; 1263 1264 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n", 1265 __func__, fanotify_fd, flags, dfd, pathname, mask); 1266 1267 /* we only use the lower 32 bits as of right now. */ 1268 if (upper_32_bits(mask)) 1269 return -EINVAL; 1270 1271 if (flags & ~FANOTIFY_MARK_FLAGS) 1272 return -EINVAL; 1273 1274 switch (mark_type) { 1275 case FAN_MARK_INODE: 1276 obj_type = FSNOTIFY_OBJ_TYPE_INODE; 1277 break; 1278 case FAN_MARK_MOUNT: 1279 obj_type = FSNOTIFY_OBJ_TYPE_VFSMOUNT; 1280 break; 1281 case FAN_MARK_FILESYSTEM: 1282 obj_type = FSNOTIFY_OBJ_TYPE_SB; 1283 break; 1284 default: 1285 return -EINVAL; 1286 } 1287 1288 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) { 1289 case FAN_MARK_ADD: 1290 case FAN_MARK_REMOVE: 1291 if (!mask) 1292 return -EINVAL; 1293 break; 1294 case FAN_MARK_FLUSH: 1295 if (flags & ~(FANOTIFY_MARK_TYPE_BITS | FAN_MARK_FLUSH)) 1296 return -EINVAL; 1297 break; 1298 default: 1299 return -EINVAL; 1300 } 1301 1302 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) 1303 valid_mask |= FANOTIFY_PERM_EVENTS; 1304 1305 if (mask & ~valid_mask) 1306 return -EINVAL; 1307 1308 /* Event flags (ONDIR, ON_CHILD) are meaningless in ignored mask */ 1309 if (ignored) 1310 mask &= ~FANOTIFY_EVENT_FLAGS; 1311 1312 f = fdget(fanotify_fd); 1313 if (unlikely(!f.file)) 1314 return -EBADF; 1315 1316 /* verify that this is indeed an fanotify instance */ 1317 ret = -EINVAL; 1318 if (unlikely(f.file->f_op != &fanotify_fops)) 1319 goto fput_and_out; 1320 group = f.file->private_data; 1321 1322 /* 1323 * An unprivileged user is not allowed to setup mount nor filesystem 1324 * marks. This also includes setting up such marks by a group that 1325 * was initialized by an unprivileged user. 1326 */ 1327 ret = -EPERM; 1328 if ((!capable(CAP_SYS_ADMIN) || 1329 FAN_GROUP_FLAG(group, FANOTIFY_UNPRIV)) && 1330 mark_type != FAN_MARK_INODE) 1331 goto fput_and_out; 1332 1333 /* 1334 * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not 1335 * allowed to set permissions events. 1336 */ 1337 ret = -EINVAL; 1338 if (mask & FANOTIFY_PERM_EVENTS && 1339 group->priority == FS_PRIO_0) 1340 goto fput_and_out; 1341 1342 /* 1343 * Events with data type inode do not carry enough information to report 1344 * event->fd, so we do not allow setting a mask for inode events unless 1345 * group supports reporting fid. 1346 * inode events are not supported on a mount mark, because they do not 1347 * carry enough information (i.e. path) to be filtered by mount point. 1348 */ 1349 fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS); 1350 if (mask & FANOTIFY_INODE_EVENTS && 1351 (!fid_mode || mark_type == FAN_MARK_MOUNT)) 1352 goto fput_and_out; 1353 1354 if (flags & FAN_MARK_FLUSH) { 1355 ret = 0; 1356 if (mark_type == FAN_MARK_MOUNT) 1357 fsnotify_clear_vfsmount_marks_by_group(group); 1358 else if (mark_type == FAN_MARK_FILESYSTEM) 1359 fsnotify_clear_sb_marks_by_group(group); 1360 else 1361 fsnotify_clear_inode_marks_by_group(group); 1362 goto fput_and_out; 1363 } 1364 1365 ret = fanotify_find_path(dfd, pathname, &path, flags, 1366 (mask & ALL_FSNOTIFY_EVENTS), obj_type); 1367 if (ret) 1368 goto fput_and_out; 1369 1370 if (flags & FAN_MARK_ADD) { 1371 ret = fanotify_events_supported(&path, mask); 1372 if (ret) 1373 goto path_put_and_out; 1374 } 1375 1376 if (fid_mode) { 1377 ret = fanotify_test_fid(&path, &__fsid); 1378 if (ret) 1379 goto path_put_and_out; 1380 1381 fsid = &__fsid; 1382 } 1383 1384 /* inode held in place by reference to path; group by fget on fd */ 1385 if (mark_type == FAN_MARK_INODE) 1386 inode = path.dentry->d_inode; 1387 else 1388 mnt = path.mnt; 1389 1390 /* Mask out FAN_EVENT_ON_CHILD flag for sb/mount/non-dir marks */ 1391 if (mnt || !S_ISDIR(inode->i_mode)) { 1392 mask &= ~FAN_EVENT_ON_CHILD; 1393 umask = FAN_EVENT_ON_CHILD; 1394 /* 1395 * If group needs to report parent fid, register for getting 1396 * events with parent/name info for non-directory. 1397 */ 1398 if ((fid_mode & FAN_REPORT_DIR_FID) && 1399 (flags & FAN_MARK_ADD) && !ignored) 1400 mask |= FAN_EVENT_ON_CHILD; 1401 } 1402 1403 /* create/update an inode mark */ 1404 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) { 1405 case FAN_MARK_ADD: 1406 if (mark_type == FAN_MARK_MOUNT) 1407 ret = fanotify_add_vfsmount_mark(group, mnt, mask, 1408 flags, fsid); 1409 else if (mark_type == FAN_MARK_FILESYSTEM) 1410 ret = fanotify_add_sb_mark(group, mnt->mnt_sb, mask, 1411 flags, fsid); 1412 else 1413 ret = fanotify_add_inode_mark(group, inode, mask, 1414 flags, fsid); 1415 break; 1416 case FAN_MARK_REMOVE: 1417 if (mark_type == FAN_MARK_MOUNT) 1418 ret = fanotify_remove_vfsmount_mark(group, mnt, mask, 1419 flags, umask); 1420 else if (mark_type == FAN_MARK_FILESYSTEM) 1421 ret = fanotify_remove_sb_mark(group, mnt->mnt_sb, mask, 1422 flags, umask); 1423 else 1424 ret = fanotify_remove_inode_mark(group, inode, mask, 1425 flags, umask); 1426 break; 1427 default: 1428 ret = -EINVAL; 1429 } 1430 1431 path_put_and_out: 1432 path_put(&path); 1433 fput_and_out: 1434 fdput(f); 1435 return ret; 1436 } 1437 1438 #ifndef CONFIG_ARCH_SPLIT_ARG64 1439 SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags, 1440 __u64, mask, int, dfd, 1441 const char __user *, pathname) 1442 { 1443 return do_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname); 1444 } 1445 #endif 1446 1447 #if defined(CONFIG_ARCH_SPLIT_ARG64) || defined(CONFIG_COMPAT) 1448 SYSCALL32_DEFINE6(fanotify_mark, 1449 int, fanotify_fd, unsigned int, flags, 1450 SC_ARG64(mask), int, dfd, 1451 const char __user *, pathname) 1452 { 1453 return do_fanotify_mark(fanotify_fd, flags, SC_VAL64(__u64, mask), 1454 dfd, pathname); 1455 } 1456 #endif 1457 1458 /* 1459 * fanotify_user_setup - Our initialization function. Note that we cannot return 1460 * error because we have compiled-in VFS hooks. So an (unlikely) failure here 1461 * must result in panic(). 1462 */ 1463 static int __init fanotify_user_setup(void) 1464 { 1465 struct sysinfo si; 1466 int max_marks; 1467 1468 si_meminfo(&si); 1469 /* 1470 * Allow up to 1% of addressable memory to be accounted for per user 1471 * marks limited to the range [8192, 1048576]. mount and sb marks are 1472 * a lot cheaper than inode marks, but there is no reason for a user 1473 * to have many of those, so calculate by the cost of inode marks. 1474 */ 1475 max_marks = (((si.totalram - si.totalhigh) / 100) << PAGE_SHIFT) / 1476 INODE_MARK_COST; 1477 max_marks = clamp(max_marks, FANOTIFY_OLD_DEFAULT_MAX_MARKS, 1478 FANOTIFY_DEFAULT_MAX_USER_MARKS); 1479 1480 BUILD_BUG_ON(FANOTIFY_INIT_FLAGS & FANOTIFY_INTERNAL_GROUP_FLAGS); 1481 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 10); 1482 BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9); 1483 1484 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, 1485 SLAB_PANIC|SLAB_ACCOUNT); 1486 fanotify_fid_event_cachep = KMEM_CACHE(fanotify_fid_event, 1487 SLAB_PANIC); 1488 fanotify_path_event_cachep = KMEM_CACHE(fanotify_path_event, 1489 SLAB_PANIC); 1490 if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) { 1491 fanotify_perm_event_cachep = 1492 KMEM_CACHE(fanotify_perm_event, SLAB_PANIC); 1493 } 1494 1495 fanotify_max_queued_events = FANOTIFY_DEFAULT_MAX_EVENTS; 1496 init_user_ns.ucount_max[UCOUNT_FANOTIFY_GROUPS] = 1497 FANOTIFY_DEFAULT_MAX_GROUPS; 1498 init_user_ns.ucount_max[UCOUNT_FANOTIFY_MARKS] = max_marks; 1499 1500 return 0; 1501 } 1502 device_initcall(fanotify_user_setup); 1503