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