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