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, ERR_PTR(-E...) or NULL (as returned from ->lookup()) 1063 * 1064 * This can be used to set the result of a 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 or -E..., which must be the return value of ->atomic_open() after 1070 * having called this function. 1071 */ 1072 int finish_no_open(struct file *file, struct dentry *dentry) 1073 { 1074 if (IS_ERR(dentry)) 1075 return PTR_ERR(dentry); 1076 file->__f_path.dentry = dentry; 1077 return 0; 1078 } 1079 EXPORT_SYMBOL(finish_no_open); 1080 1081 char *file_path(struct file *filp, char *buf, int buflen) 1082 { 1083 return d_path(&filp->f_path, buf, buflen); 1084 } 1085 EXPORT_SYMBOL(file_path); 1086 1087 /** 1088 * vfs_open - open the file at the given path 1089 * @path: path to open 1090 * @file: newly allocated file with f_flag initialized 1091 */ 1092 int vfs_open(const struct path *path, struct file *file) 1093 { 1094 int ret; 1095 1096 file->__f_path = *path; 1097 ret = do_dentry_open(file, NULL); 1098 if (!ret) { 1099 /* 1100 * Once we return a file with FMODE_OPENED, __fput() will call 1101 * fsnotify_close(), so we need fsnotify_open() here for 1102 * symmetry. 1103 */ 1104 fsnotify_open(file); 1105 } 1106 return ret; 1107 } 1108 1109 struct file *dentry_open(const struct path *path, int flags, 1110 const struct cred *cred) 1111 { 1112 int error; 1113 struct file *f; 1114 1115 /* We must always pass in a valid mount pointer. */ 1116 BUG_ON(!path->mnt); 1117 1118 f = alloc_empty_file(flags, cred); 1119 if (!IS_ERR(f)) { 1120 error = vfs_open(path, f); 1121 if (error) { 1122 fput(f); 1123 f = ERR_PTR(error); 1124 } 1125 } 1126 return f; 1127 } 1128 EXPORT_SYMBOL(dentry_open); 1129 1130 struct file *dentry_open_nonotify(const struct path *path, int flags, 1131 const struct cred *cred) 1132 { 1133 struct file *f = alloc_empty_file(flags, cred); 1134 if (!IS_ERR(f)) { 1135 int error; 1136 1137 file_set_fsnotify_mode(f, FMODE_NONOTIFY); 1138 error = vfs_open(path, f); 1139 if (error) { 1140 fput(f); 1141 f = ERR_PTR(error); 1142 } 1143 } 1144 return f; 1145 } 1146 1147 /** 1148 * dentry_create - Create and open a file 1149 * @path: path to create 1150 * @flags: O_ flags 1151 * @mode: mode bits for new file 1152 * @cred: credentials to use 1153 * 1154 * Caller must hold the parent directory's lock, and have prepared 1155 * a negative dentry, placed in @path->dentry, for the new file. 1156 * 1157 * Caller sets @path->mnt to the vfsmount of the filesystem where 1158 * the new file is to be created. The parent directory and the 1159 * negative dentry must reside on the same filesystem instance. 1160 * 1161 * On success, returns a "struct file *". Otherwise a ERR_PTR 1162 * is returned. 1163 */ 1164 struct file *dentry_create(const struct path *path, int flags, umode_t mode, 1165 const struct cred *cred) 1166 { 1167 struct file *f; 1168 int error; 1169 1170 f = alloc_empty_file(flags, cred); 1171 if (IS_ERR(f)) 1172 return f; 1173 1174 error = vfs_create(mnt_idmap(path->mnt), 1175 d_inode(path->dentry->d_parent), 1176 path->dentry, mode, true); 1177 if (!error) 1178 error = vfs_open(path, f); 1179 1180 if (unlikely(error)) { 1181 fput(f); 1182 return ERR_PTR(error); 1183 } 1184 return f; 1185 } 1186 EXPORT_SYMBOL(dentry_create); 1187 1188 /** 1189 * kernel_file_open - open a file for kernel internal use 1190 * @path: path of the file to open 1191 * @flags: open flags 1192 * @cred: credentials for open 1193 * 1194 * Open a file for use by in-kernel consumers. The file is not accounted 1195 * against nr_files and must not be installed into the file descriptor 1196 * table. 1197 * 1198 * Return: Opened file on success, an error pointer on failure. 1199 */ 1200 struct file *kernel_file_open(const struct path *path, int flags, 1201 const struct cred *cred) 1202 { 1203 struct file *f; 1204 int error; 1205 1206 f = alloc_empty_file_noaccount(flags, cred); 1207 if (IS_ERR(f)) 1208 return f; 1209 1210 error = vfs_open(path, f); 1211 if (error) { 1212 fput(f); 1213 return ERR_PTR(error); 1214 } 1215 return f; 1216 } 1217 EXPORT_SYMBOL_GPL(kernel_file_open); 1218 1219 #define WILL_CREATE(flags) (flags & (O_CREAT | __O_TMPFILE)) 1220 #define O_PATH_FLAGS (O_DIRECTORY | O_NOFOLLOW | O_PATH | O_CLOEXEC) 1221 1222 inline struct open_how build_open_how(int flags, umode_t mode) 1223 { 1224 struct open_how how = { 1225 .flags = flags & VALID_OPEN_FLAGS, 1226 .mode = mode & S_IALLUGO, 1227 }; 1228 1229 /* O_PATH beats everything else. */ 1230 if (how.flags & O_PATH) 1231 how.flags &= O_PATH_FLAGS; 1232 /* Modes should only be set for create-like flags. */ 1233 if (!WILL_CREATE(how.flags)) 1234 how.mode = 0; 1235 return how; 1236 } 1237 1238 inline int build_open_flags(const struct open_how *how, struct open_flags *op) 1239 { 1240 u64 flags = how->flags; 1241 u64 strip = O_CLOEXEC; 1242 int lookup_flags = 0; 1243 int acc_mode = ACC_MODE(flags); 1244 1245 BUILD_BUG_ON_MSG(upper_32_bits(VALID_OPEN_FLAGS), 1246 "struct open_flags doesn't yet handle flags > 32 bits"); 1247 1248 /* 1249 * Strip flags that aren't relevant in determining struct open_flags. 1250 */ 1251 flags &= ~strip; 1252 1253 /* 1254 * Older syscalls implicitly clear all of the invalid flags or argument 1255 * values before calling build_open_flags(), but openat2(2) checks all 1256 * of its arguments. 1257 */ 1258 if (flags & ~VALID_OPEN_FLAGS) 1259 return -EINVAL; 1260 if (how->resolve & ~VALID_RESOLVE_FLAGS) 1261 return -EINVAL; 1262 1263 /* Scoping flags are mutually exclusive. */ 1264 if ((how->resolve & RESOLVE_BENEATH) && (how->resolve & RESOLVE_IN_ROOT)) 1265 return -EINVAL; 1266 1267 /* Deal with the mode. */ 1268 if (WILL_CREATE(flags)) { 1269 if (how->mode & ~S_IALLUGO) 1270 return -EINVAL; 1271 op->mode = how->mode | S_IFREG; 1272 } else { 1273 if (how->mode != 0) 1274 return -EINVAL; 1275 op->mode = 0; 1276 } 1277 1278 /* 1279 * Block bugs where O_DIRECTORY | O_CREAT created regular files. 1280 * Note, that blocking O_DIRECTORY | O_CREAT here also protects 1281 * O_TMPFILE below which requires O_DIRECTORY being raised. 1282 */ 1283 if ((flags & (O_DIRECTORY | O_CREAT)) == (O_DIRECTORY | O_CREAT)) 1284 return -EINVAL; 1285 1286 /* Now handle the creative implementation of O_TMPFILE. */ 1287 if (flags & __O_TMPFILE) { 1288 /* 1289 * In order to ensure programs get explicit errors when trying 1290 * to use O_TMPFILE on old kernels we enforce that O_DIRECTORY 1291 * is raised alongside __O_TMPFILE. 1292 */ 1293 if (!(flags & O_DIRECTORY)) 1294 return -EINVAL; 1295 if (!(acc_mode & MAY_WRITE)) 1296 return -EINVAL; 1297 } 1298 if (flags & O_PATH) { 1299 /* O_PATH only permits certain other flags to be set. */ 1300 if (flags & ~O_PATH_FLAGS) 1301 return -EINVAL; 1302 acc_mode = 0; 1303 } 1304 1305 /* 1306 * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only 1307 * check for O_DSYNC if the need any syncing at all we enforce it's 1308 * always set instead of having to deal with possibly weird behaviour 1309 * for malicious applications setting only __O_SYNC. 1310 */ 1311 if (flags & __O_SYNC) 1312 flags |= O_DSYNC; 1313 1314 op->open_flag = flags; 1315 1316 /* O_TRUNC implies we need access checks for write permissions */ 1317 if (flags & O_TRUNC) 1318 acc_mode |= MAY_WRITE; 1319 1320 /* Allow the LSM permission hook to distinguish append 1321 access from general write access. */ 1322 if (flags & O_APPEND) 1323 acc_mode |= MAY_APPEND; 1324 1325 op->acc_mode = acc_mode; 1326 1327 op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN; 1328 1329 if (flags & O_CREAT) { 1330 op->intent |= LOOKUP_CREATE; 1331 if (flags & O_EXCL) { 1332 op->intent |= LOOKUP_EXCL; 1333 flags |= O_NOFOLLOW; 1334 } 1335 } 1336 1337 if (flags & O_DIRECTORY) 1338 lookup_flags |= LOOKUP_DIRECTORY; 1339 if (!(flags & O_NOFOLLOW)) 1340 lookup_flags |= LOOKUP_FOLLOW; 1341 1342 if (how->resolve & RESOLVE_NO_XDEV) 1343 lookup_flags |= LOOKUP_NO_XDEV; 1344 if (how->resolve & RESOLVE_NO_MAGICLINKS) 1345 lookup_flags |= LOOKUP_NO_MAGICLINKS; 1346 if (how->resolve & RESOLVE_NO_SYMLINKS) 1347 lookup_flags |= LOOKUP_NO_SYMLINKS; 1348 if (how->resolve & RESOLVE_BENEATH) 1349 lookup_flags |= LOOKUP_BENEATH; 1350 if (how->resolve & RESOLVE_IN_ROOT) 1351 lookup_flags |= LOOKUP_IN_ROOT; 1352 if (how->resolve & RESOLVE_CACHED) { 1353 /* Don't bother even trying for create/truncate/tmpfile open */ 1354 if (flags & (O_TRUNC | O_CREAT | __O_TMPFILE)) 1355 return -EAGAIN; 1356 lookup_flags |= LOOKUP_CACHED; 1357 } 1358 1359 op->lookup_flags = lookup_flags; 1360 return 0; 1361 } 1362 1363 /** 1364 * file_open_name - open file and return file pointer 1365 * 1366 * @name: struct filename containing path to open 1367 * @flags: open flags as per the open(2) second argument 1368 * @mode: mode for the new file if O_CREAT is set, else ignored 1369 * 1370 * This is the helper to open a file from kernelspace if you really 1371 * have to. But in generally you should not do this, so please move 1372 * along, nothing to see here.. 1373 */ 1374 struct file *file_open_name(struct filename *name, int flags, umode_t mode) 1375 { 1376 struct open_flags op; 1377 struct open_how how = build_open_how(flags, mode); 1378 int err = build_open_flags(&how, &op); 1379 if (err) 1380 return ERR_PTR(err); 1381 return do_filp_open(AT_FDCWD, name, &op); 1382 } 1383 1384 /** 1385 * filp_open - open file and return file pointer 1386 * 1387 * @filename: path to open 1388 * @flags: open flags as per the open(2) second argument 1389 * @mode: mode for the new file if O_CREAT is set, else ignored 1390 * 1391 * This is the helper to open a file from kernelspace if you really 1392 * have to. But in generally you should not do this, so please move 1393 * along, nothing to see here.. 1394 */ 1395 struct file *filp_open(const char *filename, int flags, umode_t mode) 1396 { 1397 struct filename *name = getname_kernel(filename); 1398 struct file *file = ERR_CAST(name); 1399 1400 if (!IS_ERR(name)) { 1401 file = file_open_name(name, flags, mode); 1402 putname(name); 1403 } 1404 return file; 1405 } 1406 EXPORT_SYMBOL(filp_open); 1407 1408 struct file *file_open_root(const struct path *root, 1409 const char *filename, int flags, umode_t mode) 1410 { 1411 struct open_flags op; 1412 struct open_how how = build_open_how(flags, mode); 1413 int err = build_open_flags(&how, &op); 1414 if (err) 1415 return ERR_PTR(err); 1416 return do_file_open_root(root, filename, &op); 1417 } 1418 EXPORT_SYMBOL(file_open_root); 1419 1420 static int do_sys_openat2(int dfd, const char __user *filename, 1421 struct open_how *how) 1422 { 1423 struct open_flags op; 1424 struct filename *tmp; 1425 int err, fd; 1426 1427 err = build_open_flags(how, &op); 1428 if (unlikely(err)) 1429 return err; 1430 1431 tmp = getname(filename); 1432 if (IS_ERR(tmp)) 1433 return PTR_ERR(tmp); 1434 1435 fd = get_unused_fd_flags(how->flags); 1436 if (likely(fd >= 0)) { 1437 struct file *f = do_filp_open(dfd, tmp, &op); 1438 if (IS_ERR(f)) { 1439 put_unused_fd(fd); 1440 fd = PTR_ERR(f); 1441 } else { 1442 fd_install(fd, f); 1443 } 1444 } 1445 putname(tmp); 1446 return fd; 1447 } 1448 1449 int do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode) 1450 { 1451 struct open_how how = build_open_how(flags, mode); 1452 return do_sys_openat2(dfd, filename, &how); 1453 } 1454 1455 1456 SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode) 1457 { 1458 if (force_o_largefile()) 1459 flags |= O_LARGEFILE; 1460 return do_sys_open(AT_FDCWD, filename, flags, mode); 1461 } 1462 1463 SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags, 1464 umode_t, mode) 1465 { 1466 if (force_o_largefile()) 1467 flags |= O_LARGEFILE; 1468 return do_sys_open(dfd, filename, flags, mode); 1469 } 1470 1471 SYSCALL_DEFINE4(openat2, int, dfd, const char __user *, filename, 1472 struct open_how __user *, how, size_t, usize) 1473 { 1474 int err; 1475 struct open_how tmp; 1476 1477 BUILD_BUG_ON(sizeof(struct open_how) < OPEN_HOW_SIZE_VER0); 1478 BUILD_BUG_ON(sizeof(struct open_how) != OPEN_HOW_SIZE_LATEST); 1479 1480 if (unlikely(usize < OPEN_HOW_SIZE_VER0)) 1481 return -EINVAL; 1482 if (unlikely(usize > PAGE_SIZE)) 1483 return -E2BIG; 1484 1485 err = copy_struct_from_user(&tmp, sizeof(tmp), how, usize); 1486 if (err) 1487 return err; 1488 1489 audit_openat2_how(&tmp); 1490 1491 /* O_LARGEFILE is only allowed for non-O_PATH. */ 1492 if (!(tmp.flags & O_PATH) && force_o_largefile()) 1493 tmp.flags |= O_LARGEFILE; 1494 1495 return do_sys_openat2(dfd, filename, &tmp); 1496 } 1497 1498 #ifdef CONFIG_COMPAT 1499 /* 1500 * Exactly like sys_open(), except that it doesn't set the 1501 * O_LARGEFILE flag. 1502 */ 1503 COMPAT_SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode) 1504 { 1505 return do_sys_open(AT_FDCWD, filename, flags, mode); 1506 } 1507 1508 /* 1509 * Exactly like sys_openat(), except that it doesn't set the 1510 * O_LARGEFILE flag. 1511 */ 1512 COMPAT_SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags, umode_t, mode) 1513 { 1514 return do_sys_open(dfd, filename, flags, mode); 1515 } 1516 #endif 1517 1518 #ifndef __alpha__ 1519 1520 /* 1521 * For backward compatibility? Maybe this should be moved 1522 * into arch/i386 instead? 1523 */ 1524 SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode) 1525 { 1526 int flags = O_CREAT | O_WRONLY | O_TRUNC; 1527 1528 if (force_o_largefile()) 1529 flags |= O_LARGEFILE; 1530 return do_sys_open(AT_FDCWD, pathname, flags, mode); 1531 } 1532 #endif 1533 1534 /* 1535 * "id" is the POSIX thread ID. We use the 1536 * files pointer for this.. 1537 */ 1538 static int filp_flush(struct file *filp, fl_owner_t id) 1539 { 1540 int retval = 0; 1541 1542 if (CHECK_DATA_CORRUPTION(file_count(filp) == 0, filp, 1543 "VFS: Close: file count is 0 (f_op=%ps)", 1544 filp->f_op)) { 1545 return 0; 1546 } 1547 1548 if (filp->f_op->flush) 1549 retval = filp->f_op->flush(filp, id); 1550 1551 if (likely(!(filp->f_mode & FMODE_PATH))) { 1552 dnotify_flush(filp, id); 1553 locks_remove_posix(filp, id); 1554 } 1555 return retval; 1556 } 1557 1558 int filp_close(struct file *filp, fl_owner_t id) 1559 { 1560 int retval; 1561 1562 retval = filp_flush(filp, id); 1563 fput_close(filp); 1564 1565 return retval; 1566 } 1567 EXPORT_SYMBOL(filp_close); 1568 1569 /* 1570 * Careful here! We test whether the file pointer is NULL before 1571 * releasing the fd. This ensures that one clone task can't release 1572 * an fd while another clone is opening it. 1573 */ 1574 SYSCALL_DEFINE1(close, unsigned int, fd) 1575 { 1576 int retval; 1577 struct file *file; 1578 1579 file = file_close_fd(fd); 1580 if (!file) 1581 return -EBADF; 1582 1583 retval = filp_flush(file, current->files); 1584 1585 /* 1586 * We're returning to user space. Don't bother 1587 * with any delayed fput() cases. 1588 */ 1589 fput_close_sync(file); 1590 1591 if (likely(retval == 0)) 1592 return 0; 1593 1594 /* can't restart close syscall because file table entry was cleared */ 1595 if (retval == -ERESTARTSYS || 1596 retval == -ERESTARTNOINTR || 1597 retval == -ERESTARTNOHAND || 1598 retval == -ERESTART_RESTARTBLOCK) 1599 retval = -EINTR; 1600 1601 return retval; 1602 } 1603 1604 /* 1605 * This routine simulates a hangup on the tty, to arrange that users 1606 * are given clean terminals at login time. 1607 */ 1608 SYSCALL_DEFINE0(vhangup) 1609 { 1610 if (capable(CAP_SYS_TTY_CONFIG)) { 1611 tty_vhangup_self(); 1612 return 0; 1613 } 1614 return -EPERM; 1615 } 1616 1617 /* 1618 * Called when an inode is about to be open. 1619 * We use this to disallow opening large files on 32bit systems if 1620 * the caller didn't specify O_LARGEFILE. On 64bit systems we force 1621 * on this flag in sys_open. 1622 */ 1623 int generic_file_open(struct inode * inode, struct file * filp) 1624 { 1625 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS) 1626 return -EOVERFLOW; 1627 return 0; 1628 } 1629 1630 EXPORT_SYMBOL(generic_file_open); 1631 1632 /* 1633 * This is used by subsystems that don't want seekable 1634 * file descriptors. The function is not supposed to ever fail, the only 1635 * reason it returns an 'int' and not 'void' is so that it can be plugged 1636 * directly into file_operations structure. 1637 */ 1638 int nonseekable_open(struct inode *inode, struct file *filp) 1639 { 1640 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE); 1641 return 0; 1642 } 1643 1644 EXPORT_SYMBOL(nonseekable_open); 1645 1646 /* 1647 * stream_open is used by subsystems that want stream-like file descriptors. 1648 * Such file descriptors are not seekable and don't have notion of position 1649 * (file.f_pos is always 0 and ppos passed to .read()/.write() is always NULL). 1650 * Contrary to file descriptors of other regular files, .read() and .write() 1651 * can run simultaneously. 1652 * 1653 * stream_open never fails and is marked to return int so that it could be 1654 * directly used as file_operations.open . 1655 */ 1656 int stream_open(struct inode *inode, struct file *filp) 1657 { 1658 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE | FMODE_ATOMIC_POS); 1659 filp->f_mode |= FMODE_STREAM; 1660 return 0; 1661 } 1662 1663 EXPORT_SYMBOL(stream_open); 1664