Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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