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