1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * linux/fs/open.c 4 * 5 * Copyright (C) 1991, 1992 Linus Torvalds 6 */ 7 8 #include <linux/string.h> 9 #include <linux/mm.h> 10 #include <linux/file.h> 11 #include <linux/fdtable.h> 12 #include <linux/fsnotify.h> 13 #include <linux/module.h> 14 #include <linux/tty.h> 15 #include <linux/namei.h> 16 #include <linux/backing-dev.h> 17 #include <linux/capability.h> 18 #include <linux/securebits.h> 19 #include <linux/security.h> 20 #include <linux/mount.h> 21 #include <linux/fcntl.h> 22 #include <linux/slab.h> 23 #include <linux/uaccess.h> 24 #include <linux/fs.h> 25 #include <linux/personality.h> 26 #include <linux/pagemap.h> 27 #include <linux/syscalls.h> 28 #include <linux/rcupdate.h> 29 #include <linux/audit.h> 30 #include <linux/falloc.h> 31 #include <linux/fs_struct.h> 32 #include <linux/dnotify.h> 33 #include <linux/compat.h> 34 #include <linux/mnt_idmapping.h> 35 #include <linux/filelock.h> 36 37 #include "internal.h" 38 39 int do_truncate(const struct mnt_idmap *idmap, struct dentry *dentry, 40 loff_t length, unsigned int time_attrs, struct file *filp) 41 { 42 int ret; 43 struct iattr newattrs; 44 45 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */ 46 if (length < 0) 47 return -EINVAL; 48 49 newattrs.ia_size = length; 50 newattrs.ia_valid = ATTR_SIZE | time_attrs; 51 if (filp) { 52 newattrs.ia_file = filp; 53 newattrs.ia_valid |= ATTR_FILE; 54 } 55 56 /* Remove suid, sgid, and file capabilities on truncate too */ 57 ret = dentry_needs_remove_privs(idmap, dentry); 58 if (ret < 0) 59 return ret; 60 if (ret) 61 newattrs.ia_valid |= ret | ATTR_FORCE; 62 63 ret = inode_lock_killable(dentry->d_inode); 64 if (ret) 65 return ret; 66 67 /* Note any delegations or leases have already been broken: */ 68 ret = notify_change(idmap, dentry, &newattrs, NULL); 69 inode_unlock(dentry->d_inode); 70 return ret; 71 } 72 73 int vfs_truncate(const struct path *path, loff_t length) 74 { 75 const struct mnt_idmap *idmap; 76 struct inode *inode; 77 int error; 78 79 inode = path->dentry->d_inode; 80 81 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */ 82 if (S_ISDIR(inode->i_mode)) 83 return -EISDIR; 84 if (!S_ISREG(inode->i_mode)) 85 return -EINVAL; 86 87 idmap = mnt_idmap(path->mnt); 88 error = inode_permission(idmap, inode, MAY_WRITE); 89 if (error) 90 return error; 91 92 error = fsnotify_truncate_perm(path, length); 93 if (error) 94 return error; 95 96 error = mnt_want_write(path->mnt); 97 if (error) 98 return error; 99 100 error = -EPERM; 101 if (IS_APPEND(inode)) 102 goto mnt_drop_write_and_out; 103 104 error = get_write_access(inode); 105 if (error) 106 goto mnt_drop_write_and_out; 107 108 /* 109 * Make sure that there are no leases. get_write_access() protects 110 * against the truncate racing with a lease-granting setlease(). 111 */ 112 error = break_lease(inode, O_WRONLY); 113 if (error) 114 goto put_write_and_out; 115 116 error = security_path_truncate(path); 117 if (!error) 118 error = do_truncate(idmap, path->dentry, length, 0, NULL); 119 120 put_write_and_out: 121 put_write_access(inode); 122 mnt_drop_write_and_out: 123 mnt_drop_write(path->mnt); 124 125 return error; 126 } 127 EXPORT_SYMBOL_GPL(vfs_truncate); 128 129 int ksys_truncate(const char __user *pathname, loff_t length) 130 { 131 unsigned int lookup_flags = LOOKUP_FOLLOW; 132 struct path path; 133 int error; 134 135 if (length < 0) /* sorry, but loff_t says... */ 136 return -EINVAL; 137 138 CLASS(filename, name)(pathname); 139 retry: 140 error = filename_lookup(AT_FDCWD, name, lookup_flags, &path, NULL); 141 if (!error) { 142 error = vfs_truncate(&path, length); 143 path_put(&path); 144 if (retry_estale(error, lookup_flags)) { 145 lookup_flags |= LOOKUP_REVAL; 146 goto retry; 147 } 148 } 149 return error; 150 } 151 152 SYSCALL_DEFINE2(truncate, const char __user *, path, long, length) 153 { 154 return ksys_truncate(path, length); 155 } 156 157 #ifdef CONFIG_COMPAT 158 COMPAT_SYSCALL_DEFINE2(truncate, const char __user *, path, compat_off_t, length) 159 { 160 return ksys_truncate(path, length); 161 } 162 #endif 163 164 int do_ftruncate(struct file *file, loff_t length, unsigned int flags) 165 { 166 struct dentry *dentry = file->f_path.dentry; 167 struct inode *inode = dentry->d_inode; 168 int error; 169 170 if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE)) 171 return -EINVAL; 172 173 /* 174 * Cannot ftruncate over 2^31 bytes without large file support, either 175 * through opening with O_LARGEFILE or by using ftruncate64(). 176 */ 177 if (length > MAX_NON_LFS && 178 !(file->f_flags & O_LARGEFILE) && !(flags & FTRUNCATE_LFS)) 179 return -EINVAL; 180 181 /* Check IS_APPEND on real upper inode */ 182 if (IS_APPEND(file_inode(file))) 183 return -EPERM; 184 185 error = security_file_truncate(file); 186 if (error) 187 return error; 188 189 error = fsnotify_truncate_perm(&file->f_path, length); 190 if (error) 191 return error; 192 193 scoped_guard(super_write, inode->i_sb) 194 return do_truncate(file_mnt_idmap(file), dentry, length, 195 ATTR_MTIME | ATTR_CTIME, file); 196 } 197 198 int ksys_ftruncate(unsigned int fd, loff_t length, unsigned int flags) 199 { 200 if (length < 0) 201 return -EINVAL; 202 CLASS(fd, f)(fd); 203 if (fd_empty(f)) 204 return -EBADF; 205 206 return do_ftruncate(fd_file(f), length, flags); 207 } 208 209 SYSCALL_DEFINE2(ftruncate, unsigned int, fd, off_t, length) 210 { 211 return ksys_ftruncate(fd, length, 0); 212 } 213 214 #ifdef CONFIG_COMPAT 215 COMPAT_SYSCALL_DEFINE2(ftruncate, unsigned int, fd, compat_off_t, length) 216 { 217 return ksys_ftruncate(fd, length, 0); 218 } 219 #endif 220 221 /* LFS versions of truncate are only needed on 32 bit machines */ 222 #if BITS_PER_LONG == 32 223 SYSCALL_DEFINE2(truncate64, const char __user *, path, loff_t, length) 224 { 225 return ksys_truncate(path, length); 226 } 227 228 SYSCALL_DEFINE2(ftruncate64, unsigned int, fd, loff_t, length) 229 { 230 return ksys_ftruncate(fd, length, FTRUNCATE_LFS); 231 } 232 #endif /* BITS_PER_LONG == 32 */ 233 234 #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_TRUNCATE64) 235 COMPAT_SYSCALL_DEFINE3(truncate64, const char __user *, pathname, 236 compat_arg_u64_dual(length)) 237 { 238 return ksys_truncate(pathname, compat_arg_u64_glue(length)); 239 } 240 #endif 241 242 #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_FTRUNCATE64) 243 COMPAT_SYSCALL_DEFINE3(ftruncate64, unsigned int, fd, 244 compat_arg_u64_dual(length)) 245 { 246 return ksys_ftruncate(fd, compat_arg_u64_glue(length), FTRUNCATE_LFS); 247 } 248 #endif 249 250 int vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len) 251 { 252 struct inode *inode = file_inode(file); 253 int ret; 254 loff_t sum; 255 256 if (offset < 0 || len <= 0) 257 return -EINVAL; 258 259 if (mode & ~(FALLOC_FL_MODE_MASK | FALLOC_FL_KEEP_SIZE)) 260 return -EOPNOTSUPP; 261 262 /* 263 * Modes are exclusive, even if that is not obvious from the encoding 264 * as bit masks and the mix with the flag in the same namespace. 265 * 266 * To make things even more complicated, FALLOC_FL_ALLOCATE_RANGE is 267 * encoded as no bit set. 268 */ 269 switch (mode & FALLOC_FL_MODE_MASK) { 270 case FALLOC_FL_ALLOCATE_RANGE: 271 case FALLOC_FL_UNSHARE_RANGE: 272 case FALLOC_FL_ZERO_RANGE: 273 break; 274 case FALLOC_FL_PUNCH_HOLE: 275 if (!(mode & FALLOC_FL_KEEP_SIZE)) 276 return -EOPNOTSUPP; 277 break; 278 case FALLOC_FL_COLLAPSE_RANGE: 279 case FALLOC_FL_INSERT_RANGE: 280 case FALLOC_FL_WRITE_ZEROES: 281 if (mode & FALLOC_FL_KEEP_SIZE) 282 return -EOPNOTSUPP; 283 break; 284 default: 285 return -EOPNOTSUPP; 286 } 287 288 if (!(file->f_mode & FMODE_WRITE)) 289 return -EBADF; 290 291 /* 292 * On append-only files only space preallocation is supported. 293 */ 294 if ((mode & ~FALLOC_FL_KEEP_SIZE) && IS_APPEND(inode)) 295 return -EPERM; 296 297 if (IS_IMMUTABLE(inode)) 298 return -EPERM; 299 300 /* 301 * We cannot allow any fallocate operation on an active swapfile 302 */ 303 if (IS_SWAPFILE(inode)) 304 return -ETXTBSY; 305 306 /* 307 * Revalidate the write permissions, in case security policy has 308 * changed since the files were opened. 309 */ 310 ret = security_file_permission(file, MAY_WRITE); 311 if (ret) 312 return ret; 313 314 ret = fsnotify_file_area_perm(file, MAY_WRITE, &offset, len); 315 if (ret) 316 return ret; 317 318 if (S_ISFIFO(inode->i_mode)) 319 return -ESPIPE; 320 321 if (S_ISDIR(inode->i_mode)) 322 return -EISDIR; 323 324 if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) 325 return -ENODEV; 326 327 /* Check for wraparound */ 328 if (check_add_overflow(offset, len, &sum)) 329 return -EFBIG; 330 331 if (sum > inode->i_sb->s_maxbytes) 332 return -EFBIG; 333 334 if (!file->f_op->fallocate) 335 return -EOPNOTSUPP; 336 337 file_start_write(file); 338 ret = file->f_op->fallocate(file, mode, offset, len); 339 340 /* 341 * Create inotify and fanotify events. 342 * 343 * To keep the logic simple always create events if fallocate succeeds. 344 * This implies that events are even created if the file size remains 345 * unchanged, e.g. when using flag FALLOC_FL_KEEP_SIZE. 346 */ 347 if (ret == 0) 348 fsnotify_modify(file); 349 350 file_end_write(file); 351 return ret; 352 } 353 EXPORT_SYMBOL_GPL(vfs_fallocate); 354 355 int ksys_fallocate(int fd, int mode, loff_t offset, loff_t len) 356 { 357 CLASS(fd, f)(fd); 358 359 if (fd_empty(f)) 360 return -EBADF; 361 362 return vfs_fallocate(fd_file(f), mode, offset, len); 363 } 364 365 SYSCALL_DEFINE4(fallocate, int, fd, int, mode, loff_t, offset, loff_t, len) 366 { 367 return ksys_fallocate(fd, mode, offset, len); 368 } 369 370 #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_FALLOCATE) 371 COMPAT_SYSCALL_DEFINE6(fallocate, int, fd, int, mode, compat_arg_u64_dual(offset), 372 compat_arg_u64_dual(len)) 373 { 374 return ksys_fallocate(fd, mode, compat_arg_u64_glue(offset), 375 compat_arg_u64_glue(len)); 376 } 377 #endif 378 379 /* 380 * access() needs to use the real uid/gid, not the effective uid/gid. 381 * We do this by temporarily clearing all FS-related capabilities and 382 * switching the fsuid/fsgid around to the real ones. 383 * 384 * Creating new credentials is expensive, so we try to skip doing it, 385 * which we can if the result would match what we already got. 386 */ 387 static bool access_need_override_creds(int flags) 388 { 389 const struct cred *cred; 390 391 if (flags & AT_EACCESS) 392 return false; 393 394 cred = current_cred(); 395 if (!uid_eq(cred->fsuid, cred->uid) || 396 !gid_eq(cred->fsgid, cred->gid)) 397 return true; 398 399 if (!issecure(SECURE_NO_SETUID_FIXUP)) { 400 kuid_t root_uid = make_kuid(cred->user_ns, 0); 401 if (!uid_eq(cred->uid, root_uid)) { 402 if (!cap_isclear(cred->cap_effective)) 403 return true; 404 } else { 405 if (!cap_isidentical(cred->cap_effective, 406 cred->cap_permitted)) 407 return true; 408 } 409 } 410 411 return false; 412 } 413 414 static const struct cred *access_override_creds(void) 415 { 416 struct cred *override_cred; 417 418 override_cred = prepare_creds(); 419 if (!override_cred) 420 return NULL; 421 422 /* 423 * XXX access_need_override_creds performs checks in hopes of skipping 424 * this work. Make sure it stays in sync if making any changes in this 425 * routine. 426 */ 427 428 override_cred->fsuid = override_cred->uid; 429 override_cred->fsgid = override_cred->gid; 430 431 if (!issecure(SECURE_NO_SETUID_FIXUP)) { 432 /* Clear the capabilities if we switch to a non-root user */ 433 kuid_t root_uid = make_kuid(override_cred->user_ns, 0); 434 if (!uid_eq(override_cred->uid, root_uid)) 435 cap_clear(override_cred->cap_effective); 436 else 437 override_cred->cap_effective = 438 override_cred->cap_permitted; 439 } 440 441 /* 442 * The new set of credentials can *only* be used in 443 * task-synchronous circumstances, and does not need 444 * RCU freeing, unless somebody then takes a separate 445 * reference to it. 446 * 447 * NOTE! This is _only_ true because this credential 448 * is used purely for override_creds() that installs 449 * it as the subjective cred. Other threads will be 450 * accessing ->real_cred, not the subjective cred. 451 * 452 * If somebody _does_ make a copy of this (using the 453 * 'get_current_cred()' function), that will clear the 454 * non_rcu field, because now that other user may be 455 * expecting RCU freeing. But normal thread-synchronous 456 * cred accesses will keep things non-racy to avoid RCU 457 * freeing. 458 */ 459 override_cred->non_rcu = 1; 460 return override_creds(override_cred); 461 } 462 463 static int do_faccessat(int dfd, const char __user *filename, int mode, int flags) 464 { 465 struct path path; 466 struct inode *inode; 467 int res; 468 unsigned int lookup_flags = LOOKUP_FOLLOW; 469 const struct cred *old_cred = NULL; 470 471 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */ 472 return -EINVAL; 473 474 if (flags & ~(AT_EACCESS | AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) 475 return -EINVAL; 476 477 if (flags & AT_SYMLINK_NOFOLLOW) 478 lookup_flags &= ~LOOKUP_FOLLOW; 479 480 if (access_need_override_creds(flags)) { 481 old_cred = access_override_creds(); 482 if (!old_cred) 483 return -ENOMEM; 484 } 485 486 CLASS(filename_uflags, name)(filename, flags); 487 retry: 488 res = filename_lookup(dfd, name, lookup_flags, &path, NULL); 489 if (res) 490 goto out; 491 492 inode = d_backing_inode(path.dentry); 493 494 if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) { 495 /* 496 * MAY_EXEC on regular files is denied if the fs is mounted 497 * with the "noexec" flag. 498 */ 499 res = -EACCES; 500 if (path_noexec(&path)) 501 goto out_path_release; 502 } 503 504 res = inode_permission(mnt_idmap(path.mnt), inode, mode | MAY_ACCESS); 505 /* SuS v2 requires we report a read only fs too */ 506 if (res || !(mode & S_IWOTH) || special_file(inode->i_mode)) 507 goto out_path_release; 508 /* 509 * This is a rare case where using __mnt_is_readonly() 510 * is OK without a mnt_want/drop_write() pair. Since 511 * no actual write to the fs is performed here, we do 512 * not need to telegraph to that to anyone. 513 * 514 * By doing this, we accept that this access is 515 * inherently racy and know that the fs may change 516 * state before we even see this result. 517 */ 518 if (__mnt_is_readonly(path.mnt)) 519 res = -EROFS; 520 521 out_path_release: 522 path_put(&path); 523 if (retry_estale(res, lookup_flags)) { 524 lookup_flags |= LOOKUP_REVAL; 525 goto retry; 526 } 527 out: 528 if (old_cred) 529 put_cred(revert_creds(old_cred)); 530 531 return res; 532 } 533 534 SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode) 535 { 536 return do_faccessat(dfd, filename, mode, 0); 537 } 538 539 SYSCALL_DEFINE4(faccessat2, int, dfd, const char __user *, filename, int, mode, 540 int, flags) 541 { 542 return do_faccessat(dfd, filename, mode, flags); 543 } 544 545 SYSCALL_DEFINE2(access, const char __user *, filename, int, mode) 546 { 547 return do_faccessat(AT_FDCWD, filename, mode, 0); 548 } 549 550 SYSCALL_DEFINE1(chdir, const char __user *, filename) 551 { 552 struct path path; 553 int error; 554 unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 555 CLASS(filename, name)(filename); 556 retry: 557 error = filename_lookup(AT_FDCWD, name, lookup_flags, &path, NULL); 558 if (!error) { 559 error = path_permission(&path, MAY_EXEC | MAY_CHDIR); 560 if (!error) 561 set_fs_pwd(current->fs, &path); 562 path_put(&path); 563 if (retry_estale(error, lookup_flags)) { 564 lookup_flags |= LOOKUP_REVAL; 565 goto retry; 566 } 567 } 568 return error; 569 } 570 571 SYSCALL_DEFINE1(fchdir, unsigned int, fd) 572 { 573 int error; 574 575 if ((int)fd == FD_FAILFS_ROOT) 576 return failfs_current_chdir(); 577 578 CLASS(fd_raw, f)(fd); 579 if (fd_empty(f)) 580 return -EBADF; 581 582 if (!d_can_lookup(fd_file(f)->f_path.dentry)) 583 return -ENOTDIR; 584 585 error = file_permission(fd_file(f), MAY_EXEC | MAY_CHDIR); 586 if (!error) 587 set_fs_pwd(current->fs, &fd_file(f)->f_path); 588 return error; 589 } 590 591 SYSCALL_DEFINE1(chroot, const char __user *, filename) 592 { 593 struct path path; 594 int error; 595 unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 596 CLASS(filename, name)(filename); 597 retry: 598 error = filename_lookup(AT_FDCWD, name, lookup_flags, &path, NULL); 599 if (error) 600 return error; 601 602 error = path_permission(&path, MAY_EXEC | MAY_CHDIR); 603 if (error) 604 goto dput_and_out; 605 606 error = -EPERM; 607 if (!ns_capable(current_user_ns(), CAP_SYS_CHROOT)) 608 goto dput_and_out; 609 error = security_path_chroot(&path); 610 if (!error) 611 set_fs_root(current->fs, &path); 612 dput_and_out: 613 path_put(&path); 614 if (retry_estale(error, lookup_flags)) { 615 lookup_flags |= LOOKUP_REVAL; 616 goto retry; 617 } 618 return error; 619 } 620 621 SYSCALL_DEFINE2(fchroot, int, fd, unsigned int, flags) 622 { 623 struct path path; 624 int error; 625 626 if (flags) 627 return -EINVAL; 628 629 if (fd == FD_FAILFS_ROOT) { 630 if (!ns_capable(current_user_ns(), CAP_SYS_CHROOT)) { 631 if (!task_no_new_privs(current)) 632 return -EPERM; 633 /* A shared fs_struct lets a sibling exec setuid past the check above. */ 634 if (current->fs->users != 1) 635 return -EINVAL; 636 /* Moving the root to failfs lifts the old root's ".." barrier. */ 637 if (current_chrooted()) 638 return -EPERM; 639 } 640 failfs_get_root(&path); 641 } else { 642 CLASS(fd_raw, f)(fd); 643 if (fd_empty(f)) 644 return -EBADF; 645 646 if (!d_can_lookup(fd_file(f)->f_path.dentry)) 647 return -ENOTDIR; 648 649 error = file_permission(fd_file(f), MAY_EXEC | MAY_CHDIR); 650 if (error) 651 return error; 652 653 if (!ns_capable(current_user_ns(), CAP_SYS_CHROOT)) 654 return -EPERM; 655 656 path = fd_file(f)->f_path; 657 path_get(&path); 658 } 659 660 error = security_path_chroot(&path); 661 if (!error) 662 set_fs_root(current->fs, &path); 663 path_put(&path); 664 return error; 665 } 666 667 int chmod_common(const struct path *path, umode_t mode) 668 { 669 struct inode *inode = path->dentry->d_inode; 670 struct delegated_inode delegated_inode = { }; 671 struct iattr newattrs; 672 int error; 673 674 error = mnt_want_write(path->mnt); 675 if (error) 676 return error; 677 retry_deleg: 678 error = inode_lock_killable(inode); 679 if (error) 680 goto out_mnt_unlock; 681 error = security_path_chmod(path, mode); 682 if (error) 683 goto out_unlock; 684 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO); 685 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME; 686 error = notify_change(mnt_idmap(path->mnt), path->dentry, 687 &newattrs, &delegated_inode); 688 out_unlock: 689 inode_unlock(inode); 690 if (is_delegated(&delegated_inode)) { 691 error = break_deleg_wait(&delegated_inode); 692 if (!error) 693 goto retry_deleg; 694 } 695 out_mnt_unlock: 696 mnt_drop_write(path->mnt); 697 return error; 698 } 699 700 int vfs_fchmod(struct file *file, umode_t mode) 701 { 702 audit_file(file); 703 return chmod_common(&file->f_path, mode); 704 } 705 706 SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode) 707 { 708 CLASS(fd, f)(fd); 709 710 if (fd_empty(f)) 711 return -EBADF; 712 713 return vfs_fchmod(fd_file(f), mode); 714 } 715 716 static int do_fchmodat(int dfd, const char __user *filename, umode_t mode, 717 unsigned int flags) 718 { 719 struct path path; 720 int error; 721 unsigned int lookup_flags; 722 723 if (unlikely(flags & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH))) 724 return -EINVAL; 725 726 lookup_flags = (flags & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW; 727 CLASS(filename_uflags, name)(filename, flags); 728 retry: 729 error = filename_lookup(dfd, name, lookup_flags, &path, NULL); 730 if (!error) { 731 error = chmod_common(&path, mode); 732 path_put(&path); 733 if (retry_estale(error, lookup_flags)) { 734 lookup_flags |= LOOKUP_REVAL; 735 goto retry; 736 } 737 } 738 return error; 739 } 740 741 SYSCALL_DEFINE4(fchmodat2, int, dfd, const char __user *, filename, 742 umode_t, mode, unsigned int, flags) 743 { 744 return do_fchmodat(dfd, filename, mode, flags); 745 } 746 747 SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, 748 umode_t, mode) 749 { 750 return do_fchmodat(dfd, filename, mode, 0); 751 } 752 753 SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode) 754 { 755 return do_fchmodat(AT_FDCWD, filename, mode, 0); 756 } 757 758 /* 759 * Check whether @kuid is valid and if so generate and set vfsuid_t in 760 * ia_vfsuid. 761 * 762 * Return: true if @kuid is valid, false if not. 763 */ 764 static inline bool setattr_vfsuid(struct iattr *attr, kuid_t kuid) 765 { 766 if (!uid_valid(kuid)) 767 return false; 768 attr->ia_valid |= ATTR_UID; 769 attr->ia_vfsuid = VFSUIDT_INIT(kuid); 770 return true; 771 } 772 773 /* 774 * Check whether @kgid is valid and if so generate and set vfsgid_t in 775 * ia_vfsgid. 776 * 777 * Return: true if @kgid is valid, false if not. 778 */ 779 static inline bool setattr_vfsgid(struct iattr *attr, kgid_t kgid) 780 { 781 if (!gid_valid(kgid)) 782 return false; 783 attr->ia_valid |= ATTR_GID; 784 attr->ia_vfsgid = VFSGIDT_INIT(kgid); 785 return true; 786 } 787 788 int chown_common(const struct path *path, uid_t user, gid_t group) 789 { 790 const struct mnt_idmap *idmap; 791 struct user_namespace *fs_userns; 792 struct inode *inode = path->dentry->d_inode; 793 struct delegated_inode delegated_inode = { }; 794 int error; 795 struct iattr newattrs; 796 kuid_t uid; 797 kgid_t gid; 798 799 uid = make_kuid(current_user_ns(), user); 800 gid = make_kgid(current_user_ns(), group); 801 802 idmap = mnt_idmap(path->mnt); 803 fs_userns = i_user_ns(inode); 804 805 retry_deleg: 806 newattrs.ia_vfsuid = INVALID_VFSUID; 807 newattrs.ia_vfsgid = INVALID_VFSGID; 808 newattrs.ia_valid = ATTR_CTIME; 809 if ((user != (uid_t)-1) && !setattr_vfsuid(&newattrs, uid)) 810 return -EINVAL; 811 if ((group != (gid_t)-1) && !setattr_vfsgid(&newattrs, gid)) 812 return -EINVAL; 813 error = inode_lock_killable(inode); 814 if (error) 815 return error; 816 if (!S_ISDIR(inode->i_mode)) 817 newattrs.ia_valid |= ATTR_KILL_SUID | ATTR_KILL_PRIV | 818 setattr_should_drop_sgid(idmap, inode); 819 /* Continue to send actual fs values, not the mount values. */ 820 error = security_path_chown( 821 path, 822 from_vfsuid(idmap, fs_userns, newattrs.ia_vfsuid), 823 from_vfsgid(idmap, fs_userns, newattrs.ia_vfsgid)); 824 if (!error) 825 error = notify_change(idmap, path->dentry, &newattrs, 826 &delegated_inode); 827 inode_unlock(inode); 828 if (is_delegated(&delegated_inode)) { 829 error = break_deleg_wait(&delegated_inode); 830 if (!error) 831 goto retry_deleg; 832 } 833 return error; 834 } 835 836 int do_fchownat(int dfd, const char __user *filename, uid_t user, gid_t group, 837 int flag) 838 { 839 struct path path; 840 int error; 841 int lookup_flags; 842 843 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0) 844 return -EINVAL; 845 846 lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW; 847 CLASS(filename_uflags, name)(filename, flag); 848 retry: 849 error = filename_lookup(dfd, name, lookup_flags, &path, NULL); 850 if (!error) { 851 error = mnt_want_write(path.mnt); 852 if (!error) { 853 error = chown_common(&path, user, group); 854 mnt_drop_write(path.mnt); 855 } 856 path_put(&path); 857 if (retry_estale(error, lookup_flags)) { 858 lookup_flags |= LOOKUP_REVAL; 859 goto retry; 860 } 861 } 862 return error; 863 } 864 865 SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user, 866 gid_t, group, int, flag) 867 { 868 return do_fchownat(dfd, filename, user, group, flag); 869 } 870 871 SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group) 872 { 873 return do_fchownat(AT_FDCWD, filename, user, group, 0); 874 } 875 876 SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group) 877 { 878 return do_fchownat(AT_FDCWD, filename, user, group, 879 AT_SYMLINK_NOFOLLOW); 880 } 881 882 int vfs_fchown(struct file *file, uid_t user, gid_t group) 883 { 884 int error; 885 886 error = mnt_want_write_file(file); 887 if (error) 888 return error; 889 audit_file(file); 890 error = chown_common(&file->f_path, user, group); 891 mnt_drop_write_file(file); 892 return error; 893 } 894 895 int ksys_fchown(unsigned int fd, uid_t user, gid_t group) 896 { 897 CLASS(fd, f)(fd); 898 899 if (fd_empty(f)) 900 return -EBADF; 901 902 return vfs_fchown(fd_file(f), user, group); 903 } 904 905 SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group) 906 { 907 return ksys_fchown(fd, user, group); 908 } 909 910 static inline int file_get_write_access(struct file *f) 911 { 912 int error; 913 914 error = get_write_access(f->f_inode); 915 if (unlikely(error)) 916 return error; 917 error = mnt_get_write_access(f->f_path.mnt); 918 if (unlikely(error)) 919 goto cleanup_inode; 920 if (unlikely(f->f_mode & FMODE_BACKING)) { 921 error = mnt_get_write_access(backing_file_user_path(f)->mnt); 922 if (unlikely(error)) 923 goto cleanup_mnt; 924 } 925 return 0; 926 927 cleanup_mnt: 928 mnt_put_write_access(f->f_path.mnt); 929 cleanup_inode: 930 put_write_access(f->f_inode); 931 return error; 932 } 933 934 /* 935 * Populate struct file 936 * 937 * NOTE: it assumes f_path is populated and consumes the caller's reference. 938 */ 939 static int do_dentry_open(struct file *f, 940 int (*open)(struct inode *, struct file *)) 941 { 942 static const struct file_operations empty_fops = {}; 943 struct inode *inode = f->f_path.dentry->d_inode; 944 int error; 945 946 f->f_inode = inode; 947 f->f_mapping = inode->i_mapping; 948 f->f_wb_err = filemap_sample_wb_err(f->f_mapping); 949 f->f_sb_err = file_sample_sb_err(f); 950 951 if (unlikely(f->f_flags & O_PATH)) { 952 f->f_mode = FMODE_PATH | FMODE_OPENED; 953 file_set_fsnotify_mode(f, FMODE_NONOTIFY); 954 f->f_op = &empty_fops; 955 return 0; 956 } 957 958 if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) { 959 i_readcount_inc(inode); 960 } else if (f->f_mode & FMODE_WRITE && !special_file(inode->i_mode)) { 961 error = file_get_write_access(f); 962 if (unlikely(error)) 963 goto cleanup_file; 964 f->f_mode |= FMODE_WRITER; 965 } 966 967 /* POSIX.1-2008/SUSv4 Section XSI 2.9.7 */ 968 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) 969 f->f_mode |= FMODE_ATOMIC_POS; 970 971 f->f_op = fops_get(inode->i_fop); 972 if (WARN_ON(!f->f_op)) { 973 error = -ENODEV; 974 goto cleanup_all; 975 } 976 977 error = security_file_open(f); 978 if (unlikely(error)) 979 goto cleanup_all; 980 981 /* 982 * Call fsnotify open permission hook and set FMODE_NONOTIFY_* bits 983 * according to existing permission watches. 984 * If FMODE_NONOTIFY mode was already set for an fanotify fd or for a 985 * pseudo file, this call will not change the mode. 986 */ 987 error = fsnotify_open_perm_and_set_mode(f); 988 if (unlikely(error)) 989 goto cleanup_all; 990 991 error = break_lease(file_inode(f), f->f_flags); 992 if (unlikely(error)) 993 goto cleanup_all; 994 995 /* normally all 3 are set; ->open() can clear them if needed */ 996 f->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE; 997 if (!open) 998 open = f->f_op->open; 999 if (open) { 1000 error = open(inode, f); 1001 if (error) 1002 goto cleanup_all; 1003 } 1004 f->f_mode |= FMODE_OPENED; 1005 if ((f->f_mode & FMODE_READ) && 1006 likely(f->f_op->read || f->f_op->read_iter)) 1007 f->f_mode |= FMODE_CAN_READ; 1008 if ((f->f_mode & FMODE_WRITE) && 1009 likely(f->f_op->write || f->f_op->write_iter)) 1010 f->f_mode |= FMODE_CAN_WRITE; 1011 if ((f->f_mode & FMODE_LSEEK) && !f->f_op->llseek) 1012 f->f_mode &= ~FMODE_LSEEK; 1013 if (f->f_mapping->a_ops && f->f_mapping->a_ops->direct_IO) 1014 f->f_mode |= FMODE_CAN_ODIRECT; 1015 1016 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC | __O_REGULAR); 1017 f->f_iocb_flags = iocb_flags(f); 1018 1019 file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping); 1020 1021 if ((f->f_flags & O_DIRECT) && !(f->f_mode & FMODE_CAN_ODIRECT)) 1022 return -EINVAL; 1023 1024 return 0; 1025 1026 cleanup_all: 1027 if (WARN_ON_ONCE(error > 0)) 1028 error = -EINVAL; 1029 fops_put(f->f_op); 1030 put_file_access(f); 1031 cleanup_file: 1032 path_put(&f->f_path); 1033 f->__f_path.mnt = NULL; 1034 f->__f_path.dentry = NULL; 1035 f->f_inode = NULL; 1036 return error; 1037 } 1038 1039 /** 1040 * finish_open - finish opening a file 1041 * @file: file pointer 1042 * @dentry: pointer to dentry 1043 * @open: open callback 1044 * 1045 * This can be used to finish opening a file passed to i_op->atomic_open(). 1046 * 1047 * If the open callback is set to NULL, then the standard f_op->open() 1048 * filesystem callback is substituted. 1049 * 1050 * NB: the dentry reference is _not_ consumed. If, for example, the dentry is 1051 * the return value of d_splice_alias(), then the caller needs to perform dput() 1052 * on it after finish_open(). 1053 * 1054 * Returns zero on success or -errno if the open failed. 1055 */ 1056 int finish_open(struct file *file, struct dentry *dentry, 1057 int (*open)(struct inode *, struct file *)) 1058 { 1059 BUG_ON(file->f_mode & FMODE_OPENED); /* once it's opened, it's opened */ 1060 1061 file->__f_path.dentry = dentry; 1062 path_get(&file->f_path); 1063 return do_dentry_open(file, open); 1064 } 1065 EXPORT_SYMBOL(finish_open); 1066 1067 /** 1068 * finish_no_open - finish ->atomic_open() without opening the file 1069 * 1070 * @file: file pointer 1071 * @dentry: dentry, ERR_PTR(-E...) or NULL (as returned from ->lookup()) 1072 * 1073 * This can be used to set the result of a lookup in ->atomic_open(). 1074 * 1075 * NB: unlike finish_open() this function does consume the dentry reference and 1076 * the caller need not dput() it. 1077 * 1078 * Returns 0 or -E..., which must be the return value of ->atomic_open() after 1079 * having called this function. 1080 */ 1081 int finish_no_open(struct file *file, struct dentry *dentry) 1082 { 1083 if (IS_ERR(dentry)) 1084 return PTR_ERR(dentry); 1085 file->__f_path.dentry = dentry; 1086 return 0; 1087 } 1088 EXPORT_SYMBOL(finish_no_open); 1089 1090 char *file_path(struct file *filp, char *buf, int buflen) 1091 { 1092 return d_path(&filp->f_path, buf, buflen); 1093 } 1094 EXPORT_SYMBOL(file_path); 1095 1096 /** 1097 * vfs_open - open the file at the given path 1098 * @path: path to open 1099 * @file: newly allocated file with f_flag initialized 1100 */ 1101 int vfs_open(const struct path *path, struct file *file) 1102 { 1103 int ret; 1104 1105 file->__f_path = *path; 1106 path_get(&file->f_path); 1107 ret = do_dentry_open(file, NULL); 1108 if (!ret) { 1109 /* 1110 * Once we return a file with FMODE_OPENED, __fput() will call 1111 * fsnotify_close(), so we need fsnotify_open() here for 1112 * symmetry. 1113 */ 1114 fsnotify_open(file); 1115 } 1116 return ret; 1117 } 1118 1119 /** 1120 * vfs_open_consume - open the file at the given path and consume the reference 1121 * @path: path to open 1122 * @file: newly allocated file with f_flag initialized 1123 */ 1124 int vfs_open_consume(struct path *path, struct file *file) 1125 { 1126 int ret; 1127 1128 file->__f_path = *path; 1129 path->mnt = NULL; 1130 path->dentry = NULL; 1131 ret = do_dentry_open(file, NULL); 1132 if (!ret) { 1133 fsnotify_open(file); 1134 } 1135 return ret; 1136 } 1137 1138 struct file *dentry_open(const struct path *path, int flags, 1139 const struct cred *cred) 1140 { 1141 int error; 1142 struct file *f; 1143 1144 /* We must always pass in a valid mount pointer. */ 1145 BUG_ON(!path->mnt); 1146 1147 f = alloc_empty_file(flags, cred); 1148 if (!IS_ERR(f)) { 1149 error = vfs_open(path, f); 1150 if (error) { 1151 fput(f); 1152 f = ERR_PTR(error); 1153 } 1154 } 1155 return f; 1156 } 1157 EXPORT_SYMBOL(dentry_open); 1158 1159 struct file *dentry_open_nonotify(const struct path *path, int flags, 1160 const struct cred *cred) 1161 { 1162 struct file *f = alloc_empty_file(flags, cred); 1163 if (!IS_ERR(f)) { 1164 int error; 1165 1166 file_set_fsnotify_mode(f, FMODE_NONOTIFY); 1167 error = vfs_open(path, f); 1168 if (error) { 1169 fput(f); 1170 f = ERR_PTR(error); 1171 } 1172 } 1173 return f; 1174 } 1175 1176 /** 1177 * kernel_file_open - open a file for kernel internal use 1178 * @path: path of the file to open 1179 * @flags: open flags 1180 * @cred: credentials for open 1181 * 1182 * Open a file for use by in-kernel consumers. The file is not accounted 1183 * against nr_files and must not be installed into the file descriptor 1184 * table. 1185 * 1186 * Return: Opened file on success, an error pointer on failure. 1187 */ 1188 struct file *kernel_file_open(const struct path *path, int flags, 1189 const struct cred *cred) 1190 { 1191 struct file *f; 1192 int error; 1193 1194 f = alloc_empty_file_noaccount(flags, cred); 1195 if (IS_ERR(f)) 1196 return f; 1197 1198 error = vfs_open(path, f); 1199 if (error) { 1200 fput(f); 1201 return ERR_PTR(error); 1202 } 1203 return f; 1204 } 1205 EXPORT_SYMBOL_GPL(kernel_file_open); 1206 1207 #define WILL_CREATE(flags) (flags & (O_CREAT | __O_TMPFILE)) 1208 #define O_PATH_FLAGS (O_DIRECTORY | O_NOFOLLOW | O_PATH | O_CLOEXEC | O_EMPTYPATH) 1209 1210 inline struct open_how build_open_how(int flags, umode_t mode) 1211 { 1212 struct open_how how = { 1213 .flags = flags & VALID_OPEN_FLAGS, 1214 .mode = mode & S_IALLUGO, 1215 }; 1216 1217 /* O_PATH beats everything else. */ 1218 if (how.flags & O_PATH) 1219 how.flags &= O_PATH_FLAGS; 1220 /* Modes should only be set for create-like flags. */ 1221 if (!WILL_CREATE(how.flags)) 1222 how.mode = 0; 1223 return how; 1224 } 1225 1226 inline int build_open_flags(const struct open_how *how, struct open_flags *op) 1227 { 1228 u64 flags = how->flags; 1229 u64 strip = O_CLOEXEC; 1230 int lookup_flags = 0; 1231 int acc_mode = ACC_MODE(flags); 1232 1233 BUILD_BUG_ON_MSG(upper_32_bits(VALID_OPEN_FLAGS), 1234 "VALID_OPEN_FLAGS must fit in 32 bits"); 1235 /* The whole point: OPENAT2_REGULAR must be unrepresentable in int. */ 1236 BUILD_BUG_ON_MSG(!upper_32_bits(OPENAT2_REGULAR), 1237 "OPENAT2_REGULAR must live in the upper 32 bits of open_how::flags"); 1238 /* Prevent a future bit collision between UAPI and internal carrier. */ 1239 BUILD_BUG_ON_MSG(OPENAT2_REGULAR & VALID_OPEN_FLAGS, 1240 "OPENAT2_REGULAR must not alias any open()/openat() flag"); 1241 BUILD_BUG_ON_MSG(__O_REGULAR & VALID_OPENAT2_FLAGS, 1242 "__O_REGULAR must not alias any user-visible flag"); 1243 1244 /* 1245 * Strip flags that aren't relevant in determining struct open_flags. 1246 */ 1247 flags &= ~strip; 1248 1249 /* 1250 * Older syscalls implicitly clear all of the invalid flags or argument 1251 * values before calling build_open_flags(), but openat2(2) checks all 1252 * of its arguments. 1253 */ 1254 if (flags & ~VALID_OPENAT2_FLAGS) 1255 return -EINVAL; 1256 if (how->resolve & ~VALID_RESOLVE_FLAGS) 1257 return -EINVAL; 1258 1259 /* Scoping flags are mutually exclusive. */ 1260 if ((how->resolve & RESOLVE_BENEATH) && (how->resolve & RESOLVE_IN_ROOT)) 1261 return -EINVAL; 1262 1263 /* Deal with the mode. */ 1264 if (WILL_CREATE(flags)) { 1265 if (how->mode & ~S_IALLUGO) 1266 return -EINVAL; 1267 op->mode = how->mode | S_IFREG; 1268 } else { 1269 if (how->mode != 0) 1270 return -EINVAL; 1271 op->mode = 0; 1272 } 1273 1274 /* 1275 * Block bugs where O_DIRECTORY | O_CREAT created regular files. 1276 * Note, that blocking O_DIRECTORY | O_CREAT here also protects 1277 * O_TMPFILE below which requires O_DIRECTORY being raised. 1278 */ 1279 if ((flags & (O_DIRECTORY | O_CREAT)) == (O_DIRECTORY | O_CREAT)) 1280 return -EINVAL; 1281 1282 /* Now handle the creative implementation of O_TMPFILE. */ 1283 if (flags & __O_TMPFILE) { 1284 /* 1285 * In order to ensure programs get explicit errors when trying 1286 * to use O_TMPFILE on old kernels we enforce that O_DIRECTORY 1287 * is raised alongside __O_TMPFILE. 1288 */ 1289 if (!(flags & O_DIRECTORY)) 1290 return -EINVAL; 1291 if (!(acc_mode & MAY_WRITE)) 1292 return -EINVAL; 1293 } 1294 /* 1295 * Asking to open a directory and a regular file at the same time is 1296 * contradictory. 1297 */ 1298 if ((flags & (O_DIRECTORY | OPENAT2_REGULAR)) == 1299 (O_DIRECTORY | OPENAT2_REGULAR)) 1300 return -EINVAL; 1301 1302 if (flags & O_PATH) { 1303 /* O_PATH only permits certain other flags to be set. */ 1304 if (flags & ~O_PATH_FLAGS) 1305 return -EINVAL; 1306 acc_mode = 0; 1307 } 1308 1309 /* 1310 * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only 1311 * check for O_DSYNC if the need any syncing at all we enforce it's 1312 * always set instead of having to deal with possibly weird behaviour 1313 * for malicious applications setting only __O_SYNC. 1314 */ 1315 if (flags & __O_SYNC) 1316 flags |= O_DSYNC; 1317 1318 /* 1319 * Translate the upper-32-bit UAPI bit OPENAT2_REGULAR into the 1320 * kernel-internal lower-32-bit __O_REGULAR carrier so the bit 1321 * survives the assignment to op->open_flag (an int) below and the 1322 * subsequent flow through f->f_flags (unsigned int) and the 1323 * i_op->atomic_open() callback (unsigned). do_dentry_open() strips 1324 * __O_REGULAR before the file becomes visible to userspace. 1325 */ 1326 if (flags & OPENAT2_REGULAR) { 1327 flags &= ~OPENAT2_REGULAR; 1328 flags |= __O_REGULAR; 1329 } 1330 1331 op->open_flag = flags; 1332 1333 /* O_TRUNC implies we need access checks for write permissions */ 1334 if (flags & O_TRUNC) 1335 acc_mode |= MAY_WRITE; 1336 1337 /* Allow the LSM permission hook to distinguish append 1338 access from general write access. */ 1339 if (flags & O_APPEND) 1340 acc_mode |= MAY_APPEND; 1341 1342 op->acc_mode = acc_mode; 1343 1344 op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN; 1345 1346 if (flags & O_CREAT) { 1347 op->intent |= LOOKUP_CREATE; 1348 if (flags & O_EXCL) { 1349 op->intent |= LOOKUP_EXCL; 1350 flags |= O_NOFOLLOW; 1351 } 1352 } 1353 1354 if (flags & O_DIRECTORY) 1355 lookup_flags |= LOOKUP_DIRECTORY; 1356 if (!(flags & O_NOFOLLOW)) 1357 lookup_flags |= LOOKUP_FOLLOW; 1358 if (flags & O_EMPTYPATH) 1359 lookup_flags |= LOOKUP_EMPTY; 1360 1361 if (how->resolve & RESOLVE_NO_XDEV) 1362 lookup_flags |= LOOKUP_NO_XDEV; 1363 if (how->resolve & RESOLVE_NO_MAGICLINKS) 1364 lookup_flags |= LOOKUP_NO_MAGICLINKS; 1365 if (how->resolve & RESOLVE_NO_SYMLINKS) 1366 lookup_flags |= LOOKUP_NO_SYMLINKS; 1367 if (how->resolve & RESOLVE_BENEATH) 1368 lookup_flags |= LOOKUP_BENEATH; 1369 if (how->resolve & RESOLVE_IN_ROOT) 1370 lookup_flags |= LOOKUP_IN_ROOT; 1371 if (how->resolve & RESOLVE_CACHED) { 1372 /* Don't bother even trying for create/truncate/tmpfile open */ 1373 if (flags & (O_TRUNC | O_CREAT | __O_TMPFILE)) 1374 return -EAGAIN; 1375 lookup_flags |= LOOKUP_CACHED; 1376 } 1377 1378 op->lookup_flags = lookup_flags; 1379 return 0; 1380 } 1381 1382 /** 1383 * file_open_name - open file and return file pointer 1384 * 1385 * @name: struct filename containing path to open 1386 * @flags: open flags as per the open(2) second argument 1387 * @mode: mode for the new file if O_CREAT is set, else ignored 1388 * 1389 * This is the helper to open a file from kernelspace if you really 1390 * have to. But in generally you should not do this, so please move 1391 * along, nothing to see here.. 1392 */ 1393 struct file *file_open_name(struct filename *name, int flags, umode_t mode) 1394 { 1395 struct open_flags op; 1396 struct open_how how = build_open_how(flags, mode); 1397 int err = build_open_flags(&how, &op); 1398 if (err) 1399 return ERR_PTR(err); 1400 return do_file_open(AT_FDCWD, name, &op); 1401 } 1402 1403 /** 1404 * filp_open - open file and return file pointer 1405 * 1406 * @filename: path to open 1407 * @flags: open flags as per the open(2) second argument 1408 * @mode: mode for the new file if O_CREAT is set, else ignored 1409 * 1410 * This is the helper to open a file from kernelspace if you really 1411 * have to. But in generally you should not do this, so please move 1412 * along, nothing to see here.. 1413 */ 1414 struct file *filp_open(const char *filename, int flags, umode_t mode) 1415 { 1416 CLASS(filename_kernel, name)(filename); 1417 return file_open_name(name, flags, mode); 1418 } 1419 EXPORT_SYMBOL(filp_open); 1420 1421 struct file *file_open_root(const struct path *root, 1422 const char *filename, int flags, umode_t mode) 1423 { 1424 struct open_flags op; 1425 struct open_how how = build_open_how(flags, mode); 1426 int err = build_open_flags(&how, &op); 1427 if (err) 1428 return ERR_PTR(err); 1429 return do_file_open_root(root, filename, &op); 1430 } 1431 EXPORT_SYMBOL(file_open_root); 1432 1433 static int do_sys_openat2(int dfd, const char __user *filename, 1434 struct open_how *how) 1435 { 1436 struct open_flags op; 1437 int err = build_open_flags(how, &op); 1438 if (unlikely(err)) 1439 return err; 1440 1441 CLASS(filename_flags, name)(filename, op.lookup_flags); 1442 return FD_ADD(how->flags, do_file_open(dfd, name, &op)); 1443 } 1444 1445 int do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode) 1446 { 1447 struct open_how how = build_open_how(flags, mode); 1448 return do_sys_openat2(dfd, filename, &how); 1449 } 1450 1451 1452 SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode) 1453 { 1454 if (force_o_largefile()) 1455 flags |= O_LARGEFILE; 1456 return do_sys_open(AT_FDCWD, filename, flags, mode); 1457 } 1458 1459 SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags, 1460 umode_t, mode) 1461 { 1462 if (force_o_largefile()) 1463 flags |= O_LARGEFILE; 1464 return do_sys_open(dfd, filename, flags, mode); 1465 } 1466 1467 SYSCALL_DEFINE4(openat2, int, dfd, const char __user *, filename, 1468 struct open_how __user *, how, size_t, usize) 1469 { 1470 int err; 1471 struct open_how tmp; 1472 1473 BUILD_BUG_ON(sizeof(struct open_how) < OPEN_HOW_SIZE_VER0); 1474 BUILD_BUG_ON(sizeof(struct open_how) != OPEN_HOW_SIZE_LATEST); 1475 1476 if (unlikely(usize < OPEN_HOW_SIZE_VER0)) 1477 return -EINVAL; 1478 if (unlikely(usize > PAGE_SIZE)) 1479 return -E2BIG; 1480 1481 err = copy_struct_from_user(&tmp, sizeof(tmp), how, usize); 1482 if (err) 1483 return err; 1484 1485 audit_openat2_how(&tmp); 1486 1487 /* O_LARGEFILE is only allowed for non-O_PATH. */ 1488 if (!(tmp.flags & O_PATH) && force_o_largefile()) 1489 tmp.flags |= O_LARGEFILE; 1490 1491 return do_sys_openat2(dfd, filename, &tmp); 1492 } 1493 1494 #ifdef CONFIG_COMPAT 1495 /* 1496 * Exactly like sys_open(), except that it doesn't set the 1497 * O_LARGEFILE flag. 1498 */ 1499 COMPAT_SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode) 1500 { 1501 return do_sys_open(AT_FDCWD, filename, flags, mode); 1502 } 1503 1504 /* 1505 * Exactly like sys_openat(), except that it doesn't set the 1506 * O_LARGEFILE flag. 1507 */ 1508 COMPAT_SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags, umode_t, mode) 1509 { 1510 return do_sys_open(dfd, filename, flags, mode); 1511 } 1512 #endif 1513 1514 #ifndef __alpha__ 1515 1516 /* 1517 * For backward compatibility? Maybe this should be moved 1518 * into arch/i386 instead? 1519 */ 1520 SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode) 1521 { 1522 int flags = O_CREAT | O_WRONLY | O_TRUNC; 1523 1524 if (force_o_largefile()) 1525 flags |= O_LARGEFILE; 1526 return do_sys_open(AT_FDCWD, pathname, flags, mode); 1527 } 1528 #endif 1529 1530 /* 1531 * "id" is the POSIX thread ID. We use the 1532 * files pointer for this.. 1533 */ 1534 static int filp_flush(struct file *filp, fl_owner_t id) 1535 { 1536 int retval = 0; 1537 1538 if (CHECK_DATA_CORRUPTION(file_count(filp) == 0, filp, 1539 "VFS: Close: file count is 0 (f_op=%ps)", 1540 filp->f_op)) { 1541 return 0; 1542 } 1543 1544 if (filp->f_op->flush) 1545 retval = filp->f_op->flush(filp, id); 1546 1547 if (likely(!(filp->f_mode & FMODE_PATH))) { 1548 dnotify_flush(filp, id); 1549 locks_remove_posix(filp, id); 1550 } 1551 return retval; 1552 } 1553 1554 int filp_close(struct file *filp, fl_owner_t id) 1555 { 1556 int retval; 1557 1558 retval = filp_flush(filp, id); 1559 fput_close(filp); 1560 1561 return retval; 1562 } 1563 EXPORT_SYMBOL(filp_close); 1564 1565 /* Like filp_close() but the last reference is put right here. */ 1566 int filp_close_sync(struct file *filp, fl_owner_t id) 1567 { 1568 int retval; 1569 1570 /* Kernel threads must never put their final reference here. */ 1571 VFS_WARN_ON_ONCE(current->flags & PF_KTHREAD); 1572 retval = filp_flush(filp, id); 1573 fput_close_sync(filp); 1574 1575 return retval; 1576 } 1577 1578 /* 1579 * Careful here! We test whether the file pointer is NULL before 1580 * releasing the fd. This ensures that one clone task can't release 1581 * an fd while another clone is opening it. 1582 */ 1583 SYSCALL_DEFINE1(close, unsigned int, fd) 1584 { 1585 int retval; 1586 struct file *file; 1587 1588 file = file_close_fd(fd); 1589 if (!file) 1590 return -EBADF; 1591 1592 /* 1593 * We're returning to user space. Don't bother 1594 * with any delayed fput() cases. 1595 */ 1596 retval = filp_close_sync(file, current->files); 1597 1598 if (likely(retval == 0)) 1599 return 0; 1600 1601 /* can't restart close syscall because file table entry was cleared */ 1602 if (retval == -ERESTARTSYS || 1603 retval == -ERESTARTNOINTR || 1604 retval == -ERESTARTNOHAND || 1605 retval == -ERESTART_RESTARTBLOCK) 1606 retval = -EINTR; 1607 1608 return retval; 1609 } 1610 1611 /* 1612 * This routine simulates a hangup on the tty, to arrange that users 1613 * are given clean terminals at login time. 1614 */ 1615 SYSCALL_DEFINE0(vhangup) 1616 { 1617 if (capable(CAP_SYS_TTY_CONFIG)) { 1618 tty_vhangup_self(); 1619 return 0; 1620 } 1621 return -EPERM; 1622 } 1623 1624 /* 1625 * Called when an inode is about to be open. 1626 * We use this to disallow opening large files on 32bit systems if 1627 * the caller didn't specify O_LARGEFILE. On 64bit systems we force 1628 * on this flag in sys_open. 1629 */ 1630 int generic_file_open(struct inode * inode, struct file * filp) 1631 { 1632 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS) 1633 return -EOVERFLOW; 1634 return 0; 1635 } 1636 1637 EXPORT_SYMBOL(generic_file_open); 1638 1639 /* 1640 * This is used by subsystems that don't want seekable 1641 * file descriptors. The function is not supposed to ever fail, the only 1642 * reason it returns an 'int' and not 'void' is so that it can be plugged 1643 * directly into file_operations structure. 1644 */ 1645 int nonseekable_open(struct inode *inode, struct file *filp) 1646 { 1647 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE); 1648 return 0; 1649 } 1650 1651 EXPORT_SYMBOL(nonseekable_open); 1652 1653 /* 1654 * stream_open is used by subsystems that want stream-like file descriptors. 1655 * Such file descriptors are not seekable and don't have notion of position 1656 * (file.f_pos is always 0 and ppos passed to .read()/.write() is always NULL). 1657 * Contrary to file descriptors of other regular files, .read() and .write() 1658 * can run simultaneously. 1659 * 1660 * stream_open never fails and is marked to return int so that it could be 1661 * directly used as file_operations.open . 1662 */ 1663 int stream_open(struct inode *inode, struct file *filp) 1664 { 1665 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE | FMODE_ATOMIC_POS); 1666 filp->f_mode |= FMODE_STREAM; 1667 return 0; 1668 } 1669 1670 EXPORT_SYMBOL(stream_open); 1671