18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * linux/fs/open.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 1991, 1992 Linus Torvalds 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/string.h> 98c2ecf20Sopenharmony_ci#include <linux/mm.h> 108c2ecf20Sopenharmony_ci#include <linux/file.h> 118c2ecf20Sopenharmony_ci#include <linux/fdtable.h> 128c2ecf20Sopenharmony_ci#include <linux/fsnotify.h> 138c2ecf20Sopenharmony_ci#include <linux/module.h> 148c2ecf20Sopenharmony_ci#include <linux/tty.h> 158c2ecf20Sopenharmony_ci#include <linux/namei.h> 168c2ecf20Sopenharmony_ci#include <linux/backing-dev.h> 178c2ecf20Sopenharmony_ci#include <linux/capability.h> 188c2ecf20Sopenharmony_ci#include <linux/securebits.h> 198c2ecf20Sopenharmony_ci#include <linux/security.h> 208c2ecf20Sopenharmony_ci#include <linux/mount.h> 218c2ecf20Sopenharmony_ci#include <linux/fcntl.h> 228c2ecf20Sopenharmony_ci#include <linux/slab.h> 238c2ecf20Sopenharmony_ci#include <linux/uaccess.h> 248c2ecf20Sopenharmony_ci#include <linux/fs.h> 258c2ecf20Sopenharmony_ci#include <linux/personality.h> 268c2ecf20Sopenharmony_ci#include <linux/pagemap.h> 278c2ecf20Sopenharmony_ci#include <linux/syscalls.h> 288c2ecf20Sopenharmony_ci#include <linux/rcupdate.h> 298c2ecf20Sopenharmony_ci#include <linux/audit.h> 308c2ecf20Sopenharmony_ci#include <linux/falloc.h> 318c2ecf20Sopenharmony_ci#include <linux/fs_struct.h> 328c2ecf20Sopenharmony_ci#include <linux/ima.h> 338c2ecf20Sopenharmony_ci#include <linux/dnotify.h> 348c2ecf20Sopenharmony_ci#include <linux/compat.h> 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci#include "internal.h" 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ciint do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs, 398c2ecf20Sopenharmony_ci struct file *filp) 408c2ecf20Sopenharmony_ci{ 418c2ecf20Sopenharmony_ci int ret; 428c2ecf20Sopenharmony_ci struct iattr newattrs; 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */ 458c2ecf20Sopenharmony_ci if (length < 0) 468c2ecf20Sopenharmony_ci return -EINVAL; 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci newattrs.ia_size = length; 498c2ecf20Sopenharmony_ci newattrs.ia_valid = ATTR_SIZE | time_attrs; 508c2ecf20Sopenharmony_ci if (filp) { 518c2ecf20Sopenharmony_ci newattrs.ia_file = filp; 528c2ecf20Sopenharmony_ci newattrs.ia_valid |= ATTR_FILE; 538c2ecf20Sopenharmony_ci } 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci /* Remove suid, sgid, and file capabilities on truncate too */ 568c2ecf20Sopenharmony_ci ret = dentry_needs_remove_privs(dentry); 578c2ecf20Sopenharmony_ci if (ret < 0) 588c2ecf20Sopenharmony_ci return ret; 598c2ecf20Sopenharmony_ci if (ret) 608c2ecf20Sopenharmony_ci newattrs.ia_valid |= ret | ATTR_FORCE; 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci inode_lock(dentry->d_inode); 638c2ecf20Sopenharmony_ci /* Note any delegations or leases have already been broken: */ 648c2ecf20Sopenharmony_ci ret = notify_change(dentry, &newattrs, NULL); 658c2ecf20Sopenharmony_ci inode_unlock(dentry->d_inode); 668c2ecf20Sopenharmony_ci return ret; 678c2ecf20Sopenharmony_ci} 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_cilong vfs_truncate(const struct path *path, loff_t length) 708c2ecf20Sopenharmony_ci{ 718c2ecf20Sopenharmony_ci struct inode *inode; 728c2ecf20Sopenharmony_ci long error; 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci inode = path->dentry->d_inode; 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci /* For directories it's -EISDIR, for other non-regulars - -EINVAL */ 778c2ecf20Sopenharmony_ci if (S_ISDIR(inode->i_mode)) 788c2ecf20Sopenharmony_ci return -EISDIR; 798c2ecf20Sopenharmony_ci if (!S_ISREG(inode->i_mode)) 808c2ecf20Sopenharmony_ci return -EINVAL; 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci error = mnt_want_write(path->mnt); 838c2ecf20Sopenharmony_ci if (error) 848c2ecf20Sopenharmony_ci goto out; 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci error = inode_permission(inode, MAY_WRITE); 878c2ecf20Sopenharmony_ci if (error) 888c2ecf20Sopenharmony_ci goto mnt_drop_write_and_out; 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci error = -EPERM; 918c2ecf20Sopenharmony_ci if (IS_APPEND(inode)) 928c2ecf20Sopenharmony_ci goto mnt_drop_write_and_out; 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci error = get_write_access(inode); 958c2ecf20Sopenharmony_ci if (error) 968c2ecf20Sopenharmony_ci goto mnt_drop_write_and_out; 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci /* 998c2ecf20Sopenharmony_ci * Make sure that there are no leases. get_write_access() protects 1008c2ecf20Sopenharmony_ci * against the truncate racing with a lease-granting setlease(). 1018c2ecf20Sopenharmony_ci */ 1028c2ecf20Sopenharmony_ci error = break_lease(inode, O_WRONLY); 1038c2ecf20Sopenharmony_ci if (error) 1048c2ecf20Sopenharmony_ci goto put_write_and_out; 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci error = locks_verify_truncate(inode, NULL, length); 1078c2ecf20Sopenharmony_ci if (!error) 1088c2ecf20Sopenharmony_ci error = security_path_truncate(path); 1098c2ecf20Sopenharmony_ci if (!error) 1108c2ecf20Sopenharmony_ci error = do_truncate(path->dentry, length, 0, NULL); 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ciput_write_and_out: 1138c2ecf20Sopenharmony_ci put_write_access(inode); 1148c2ecf20Sopenharmony_cimnt_drop_write_and_out: 1158c2ecf20Sopenharmony_ci mnt_drop_write(path->mnt); 1168c2ecf20Sopenharmony_ciout: 1178c2ecf20Sopenharmony_ci return error; 1188c2ecf20Sopenharmony_ci} 1198c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(vfs_truncate); 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_cilong do_sys_truncate(const char __user *pathname, loff_t length) 1228c2ecf20Sopenharmony_ci{ 1238c2ecf20Sopenharmony_ci unsigned int lookup_flags = LOOKUP_FOLLOW; 1248c2ecf20Sopenharmony_ci struct path path; 1258c2ecf20Sopenharmony_ci int error; 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci if (length < 0) /* sorry, but loff_t says... */ 1288c2ecf20Sopenharmony_ci return -EINVAL; 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ciretry: 1318c2ecf20Sopenharmony_ci error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path); 1328c2ecf20Sopenharmony_ci if (!error) { 1338c2ecf20Sopenharmony_ci error = vfs_truncate(&path, length); 1348c2ecf20Sopenharmony_ci path_put(&path); 1358c2ecf20Sopenharmony_ci } 1368c2ecf20Sopenharmony_ci if (retry_estale(error, lookup_flags)) { 1378c2ecf20Sopenharmony_ci lookup_flags |= LOOKUP_REVAL; 1388c2ecf20Sopenharmony_ci goto retry; 1398c2ecf20Sopenharmony_ci } 1408c2ecf20Sopenharmony_ci return error; 1418c2ecf20Sopenharmony_ci} 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ciSYSCALL_DEFINE2(truncate, const char __user *, path, long, length) 1448c2ecf20Sopenharmony_ci{ 1458c2ecf20Sopenharmony_ci return do_sys_truncate(path, length); 1468c2ecf20Sopenharmony_ci} 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci#ifdef CONFIG_COMPAT 1498c2ecf20Sopenharmony_ciCOMPAT_SYSCALL_DEFINE2(truncate, const char __user *, path, compat_off_t, length) 1508c2ecf20Sopenharmony_ci{ 1518c2ecf20Sopenharmony_ci return do_sys_truncate(path, length); 1528c2ecf20Sopenharmony_ci} 1538c2ecf20Sopenharmony_ci#endif 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_cilong do_sys_ftruncate(unsigned int fd, loff_t length, int small) 1568c2ecf20Sopenharmony_ci{ 1578c2ecf20Sopenharmony_ci struct inode *inode; 1588c2ecf20Sopenharmony_ci struct dentry *dentry; 1598c2ecf20Sopenharmony_ci struct fd f; 1608c2ecf20Sopenharmony_ci int error; 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci error = -EINVAL; 1638c2ecf20Sopenharmony_ci if (length < 0) 1648c2ecf20Sopenharmony_ci goto out; 1658c2ecf20Sopenharmony_ci error = -EBADF; 1668c2ecf20Sopenharmony_ci f = fdget(fd); 1678c2ecf20Sopenharmony_ci if (!f.file) 1688c2ecf20Sopenharmony_ci goto out; 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci /* explicitly opened as large or we are on 64-bit box */ 1718c2ecf20Sopenharmony_ci if (f.file->f_flags & O_LARGEFILE) 1728c2ecf20Sopenharmony_ci small = 0; 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci dentry = f.file->f_path.dentry; 1758c2ecf20Sopenharmony_ci inode = dentry->d_inode; 1768c2ecf20Sopenharmony_ci error = -EINVAL; 1778c2ecf20Sopenharmony_ci if (!S_ISREG(inode->i_mode) || !(f.file->f_mode & FMODE_WRITE)) 1788c2ecf20Sopenharmony_ci goto out_putf; 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci error = -EINVAL; 1818c2ecf20Sopenharmony_ci /* Cannot ftruncate over 2^31 bytes without large file support */ 1828c2ecf20Sopenharmony_ci if (small && length > MAX_NON_LFS) 1838c2ecf20Sopenharmony_ci goto out_putf; 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci error = -EPERM; 1868c2ecf20Sopenharmony_ci /* Check IS_APPEND on real upper inode */ 1878c2ecf20Sopenharmony_ci if (IS_APPEND(file_inode(f.file))) 1888c2ecf20Sopenharmony_ci goto out_putf; 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci sb_start_write(inode->i_sb); 1918c2ecf20Sopenharmony_ci error = locks_verify_truncate(inode, f.file, length); 1928c2ecf20Sopenharmony_ci if (!error) 1938c2ecf20Sopenharmony_ci error = security_path_truncate(&f.file->f_path); 1948c2ecf20Sopenharmony_ci if (!error) 1958c2ecf20Sopenharmony_ci error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, f.file); 1968c2ecf20Sopenharmony_ci sb_end_write(inode->i_sb); 1978c2ecf20Sopenharmony_ciout_putf: 1988c2ecf20Sopenharmony_ci fdput(f); 1998c2ecf20Sopenharmony_ciout: 2008c2ecf20Sopenharmony_ci return error; 2018c2ecf20Sopenharmony_ci} 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ciSYSCALL_DEFINE2(ftruncate, unsigned int, fd, off_t, length) 2048c2ecf20Sopenharmony_ci{ 2058c2ecf20Sopenharmony_ci return do_sys_ftruncate(fd, length, 1); 2068c2ecf20Sopenharmony_ci} 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci#ifdef CONFIG_COMPAT 2098c2ecf20Sopenharmony_ciCOMPAT_SYSCALL_DEFINE2(ftruncate, unsigned int, fd, compat_off_t, length) 2108c2ecf20Sopenharmony_ci{ 2118c2ecf20Sopenharmony_ci return do_sys_ftruncate(fd, length, 1); 2128c2ecf20Sopenharmony_ci} 2138c2ecf20Sopenharmony_ci#endif 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_ci/* LFS versions of truncate are only needed on 32 bit machines */ 2168c2ecf20Sopenharmony_ci#if BITS_PER_LONG == 32 2178c2ecf20Sopenharmony_ciSYSCALL_DEFINE2(truncate64, const char __user *, path, loff_t, length) 2188c2ecf20Sopenharmony_ci{ 2198c2ecf20Sopenharmony_ci return do_sys_truncate(path, length); 2208c2ecf20Sopenharmony_ci} 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ciSYSCALL_DEFINE2(ftruncate64, unsigned int, fd, loff_t, length) 2238c2ecf20Sopenharmony_ci{ 2248c2ecf20Sopenharmony_ci return do_sys_ftruncate(fd, length, 0); 2258c2ecf20Sopenharmony_ci} 2268c2ecf20Sopenharmony_ci#endif /* BITS_PER_LONG == 32 */ 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ciint vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len) 2308c2ecf20Sopenharmony_ci{ 2318c2ecf20Sopenharmony_ci struct inode *inode = file_inode(file); 2328c2ecf20Sopenharmony_ci long ret; 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci if (offset < 0 || len <= 0) 2358c2ecf20Sopenharmony_ci return -EINVAL; 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci /* Return error if mode is not supported */ 2388c2ecf20Sopenharmony_ci if (mode & ~FALLOC_FL_SUPPORTED_MASK) 2398c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_ci /* Punch hole and zero range are mutually exclusive */ 2428c2ecf20Sopenharmony_ci if ((mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)) == 2438c2ecf20Sopenharmony_ci (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)) 2448c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci /* Punch hole must have keep size set */ 2478c2ecf20Sopenharmony_ci if ((mode & FALLOC_FL_PUNCH_HOLE) && 2488c2ecf20Sopenharmony_ci !(mode & FALLOC_FL_KEEP_SIZE)) 2498c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci /* Collapse range should only be used exclusively. */ 2528c2ecf20Sopenharmony_ci if ((mode & FALLOC_FL_COLLAPSE_RANGE) && 2538c2ecf20Sopenharmony_ci (mode & ~FALLOC_FL_COLLAPSE_RANGE)) 2548c2ecf20Sopenharmony_ci return -EINVAL; 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_ci /* Insert range should only be used exclusively. */ 2578c2ecf20Sopenharmony_ci if ((mode & FALLOC_FL_INSERT_RANGE) && 2588c2ecf20Sopenharmony_ci (mode & ~FALLOC_FL_INSERT_RANGE)) 2598c2ecf20Sopenharmony_ci return -EINVAL; 2608c2ecf20Sopenharmony_ci 2618c2ecf20Sopenharmony_ci /* Unshare range should only be used with allocate mode. */ 2628c2ecf20Sopenharmony_ci if ((mode & FALLOC_FL_UNSHARE_RANGE) && 2638c2ecf20Sopenharmony_ci (mode & ~(FALLOC_FL_UNSHARE_RANGE | FALLOC_FL_KEEP_SIZE))) 2648c2ecf20Sopenharmony_ci return -EINVAL; 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_ci if (!(file->f_mode & FMODE_WRITE)) 2678c2ecf20Sopenharmony_ci return -EBADF; 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ci /* 2708c2ecf20Sopenharmony_ci * We can only allow pure fallocate on append only files 2718c2ecf20Sopenharmony_ci */ 2728c2ecf20Sopenharmony_ci if ((mode & ~FALLOC_FL_KEEP_SIZE) && IS_APPEND(inode)) 2738c2ecf20Sopenharmony_ci return -EPERM; 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci if (IS_IMMUTABLE(inode)) 2768c2ecf20Sopenharmony_ci return -EPERM; 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_ci /* 2798c2ecf20Sopenharmony_ci * We cannot allow any fallocate operation on an active swapfile 2808c2ecf20Sopenharmony_ci */ 2818c2ecf20Sopenharmony_ci if (IS_SWAPFILE(inode)) 2828c2ecf20Sopenharmony_ci return -ETXTBSY; 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci /* 2858c2ecf20Sopenharmony_ci * Revalidate the write permissions, in case security policy has 2868c2ecf20Sopenharmony_ci * changed since the files were opened. 2878c2ecf20Sopenharmony_ci */ 2888c2ecf20Sopenharmony_ci ret = security_file_permission(file, MAY_WRITE); 2898c2ecf20Sopenharmony_ci if (ret) 2908c2ecf20Sopenharmony_ci return ret; 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci if (S_ISFIFO(inode->i_mode)) 2938c2ecf20Sopenharmony_ci return -ESPIPE; 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ci if (S_ISDIR(inode->i_mode)) 2968c2ecf20Sopenharmony_ci return -EISDIR; 2978c2ecf20Sopenharmony_ci 2988c2ecf20Sopenharmony_ci if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) 2998c2ecf20Sopenharmony_ci return -ENODEV; 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_ci /* Check for wrap through zero too */ 3028c2ecf20Sopenharmony_ci if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0)) 3038c2ecf20Sopenharmony_ci return -EFBIG; 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci if (!file->f_op->fallocate) 3068c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 3078c2ecf20Sopenharmony_ci 3088c2ecf20Sopenharmony_ci file_start_write(file); 3098c2ecf20Sopenharmony_ci ret = file->f_op->fallocate(file, mode, offset, len); 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_ci /* 3128c2ecf20Sopenharmony_ci * Create inotify and fanotify events. 3138c2ecf20Sopenharmony_ci * 3148c2ecf20Sopenharmony_ci * To keep the logic simple always create events if fallocate succeeds. 3158c2ecf20Sopenharmony_ci * This implies that events are even created if the file size remains 3168c2ecf20Sopenharmony_ci * unchanged, e.g. when using flag FALLOC_FL_KEEP_SIZE. 3178c2ecf20Sopenharmony_ci */ 3188c2ecf20Sopenharmony_ci if (ret == 0) 3198c2ecf20Sopenharmony_ci fsnotify_modify(file); 3208c2ecf20Sopenharmony_ci 3218c2ecf20Sopenharmony_ci file_end_write(file); 3228c2ecf20Sopenharmony_ci return ret; 3238c2ecf20Sopenharmony_ci} 3248c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(vfs_fallocate); 3258c2ecf20Sopenharmony_ci 3268c2ecf20Sopenharmony_ciint ksys_fallocate(int fd, int mode, loff_t offset, loff_t len) 3278c2ecf20Sopenharmony_ci{ 3288c2ecf20Sopenharmony_ci struct fd f = fdget(fd); 3298c2ecf20Sopenharmony_ci int error = -EBADF; 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_ci if (f.file) { 3328c2ecf20Sopenharmony_ci error = vfs_fallocate(f.file, mode, offset, len); 3338c2ecf20Sopenharmony_ci fdput(f); 3348c2ecf20Sopenharmony_ci } 3358c2ecf20Sopenharmony_ci return error; 3368c2ecf20Sopenharmony_ci} 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_ciSYSCALL_DEFINE4(fallocate, int, fd, int, mode, loff_t, offset, loff_t, len) 3398c2ecf20Sopenharmony_ci{ 3408c2ecf20Sopenharmony_ci return ksys_fallocate(fd, mode, offset, len); 3418c2ecf20Sopenharmony_ci} 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_ci/* 3448c2ecf20Sopenharmony_ci * access() needs to use the real uid/gid, not the effective uid/gid. 3458c2ecf20Sopenharmony_ci * We do this by temporarily clearing all FS-related capabilities and 3468c2ecf20Sopenharmony_ci * switching the fsuid/fsgid around to the real ones. 3478c2ecf20Sopenharmony_ci */ 3488c2ecf20Sopenharmony_cistatic const struct cred *access_override_creds(void) 3498c2ecf20Sopenharmony_ci{ 3508c2ecf20Sopenharmony_ci const struct cred *old_cred; 3518c2ecf20Sopenharmony_ci struct cred *override_cred; 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ci override_cred = prepare_creds(); 3548c2ecf20Sopenharmony_ci if (!override_cred) 3558c2ecf20Sopenharmony_ci return NULL; 3568c2ecf20Sopenharmony_ci 3578c2ecf20Sopenharmony_ci override_cred->fsuid = override_cred->uid; 3588c2ecf20Sopenharmony_ci override_cred->fsgid = override_cred->gid; 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_ci if (!issecure(SECURE_NO_SETUID_FIXUP)) { 3618c2ecf20Sopenharmony_ci /* Clear the capabilities if we switch to a non-root user */ 3628c2ecf20Sopenharmony_ci kuid_t root_uid = make_kuid(override_cred->user_ns, 0); 3638c2ecf20Sopenharmony_ci if (!uid_eq(override_cred->uid, root_uid)) 3648c2ecf20Sopenharmony_ci cap_clear(override_cred->cap_effective); 3658c2ecf20Sopenharmony_ci else 3668c2ecf20Sopenharmony_ci override_cred->cap_effective = 3678c2ecf20Sopenharmony_ci override_cred->cap_permitted; 3688c2ecf20Sopenharmony_ci } 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_ci /* 3718c2ecf20Sopenharmony_ci * The new set of credentials can *only* be used in 3728c2ecf20Sopenharmony_ci * task-synchronous circumstances, and does not need 3738c2ecf20Sopenharmony_ci * RCU freeing, unless somebody then takes a separate 3748c2ecf20Sopenharmony_ci * reference to it. 3758c2ecf20Sopenharmony_ci * 3768c2ecf20Sopenharmony_ci * NOTE! This is _only_ true because this credential 3778c2ecf20Sopenharmony_ci * is used purely for override_creds() that installs 3788c2ecf20Sopenharmony_ci * it as the subjective cred. Other threads will be 3798c2ecf20Sopenharmony_ci * accessing ->real_cred, not the subjective cred. 3808c2ecf20Sopenharmony_ci * 3818c2ecf20Sopenharmony_ci * If somebody _does_ make a copy of this (using the 3828c2ecf20Sopenharmony_ci * 'get_current_cred()' function), that will clear the 3838c2ecf20Sopenharmony_ci * non_rcu field, because now that other user may be 3848c2ecf20Sopenharmony_ci * expecting RCU freeing. But normal thread-synchronous 3858c2ecf20Sopenharmony_ci * cred accesses will keep things non-RCY. 3868c2ecf20Sopenharmony_ci */ 3878c2ecf20Sopenharmony_ci override_cred->non_rcu = 1; 3888c2ecf20Sopenharmony_ci 3898c2ecf20Sopenharmony_ci old_cred = override_creds(override_cred); 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_ci /* override_cred() gets its own ref */ 3928c2ecf20Sopenharmony_ci put_cred(override_cred); 3938c2ecf20Sopenharmony_ci 3948c2ecf20Sopenharmony_ci return old_cred; 3958c2ecf20Sopenharmony_ci} 3968c2ecf20Sopenharmony_ci 3978c2ecf20Sopenharmony_cistatic long do_faccessat(int dfd, const char __user *filename, int mode, int flags) 3988c2ecf20Sopenharmony_ci{ 3998c2ecf20Sopenharmony_ci struct path path; 4008c2ecf20Sopenharmony_ci struct inode *inode; 4018c2ecf20Sopenharmony_ci int res; 4028c2ecf20Sopenharmony_ci unsigned int lookup_flags = LOOKUP_FOLLOW; 4038c2ecf20Sopenharmony_ci const struct cred *old_cred = NULL; 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_ci if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */ 4068c2ecf20Sopenharmony_ci return -EINVAL; 4078c2ecf20Sopenharmony_ci 4088c2ecf20Sopenharmony_ci if (flags & ~(AT_EACCESS | AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) 4098c2ecf20Sopenharmony_ci return -EINVAL; 4108c2ecf20Sopenharmony_ci 4118c2ecf20Sopenharmony_ci if (flags & AT_SYMLINK_NOFOLLOW) 4128c2ecf20Sopenharmony_ci lookup_flags &= ~LOOKUP_FOLLOW; 4138c2ecf20Sopenharmony_ci if (flags & AT_EMPTY_PATH) 4148c2ecf20Sopenharmony_ci lookup_flags |= LOOKUP_EMPTY; 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_ci if (!(flags & AT_EACCESS)) { 4178c2ecf20Sopenharmony_ci old_cred = access_override_creds(); 4188c2ecf20Sopenharmony_ci if (!old_cred) 4198c2ecf20Sopenharmony_ci return -ENOMEM; 4208c2ecf20Sopenharmony_ci } 4218c2ecf20Sopenharmony_ci 4228c2ecf20Sopenharmony_ciretry: 4238c2ecf20Sopenharmony_ci res = user_path_at(dfd, filename, lookup_flags, &path); 4248c2ecf20Sopenharmony_ci if (res) 4258c2ecf20Sopenharmony_ci goto out; 4268c2ecf20Sopenharmony_ci 4278c2ecf20Sopenharmony_ci inode = d_backing_inode(path.dentry); 4288c2ecf20Sopenharmony_ci 4298c2ecf20Sopenharmony_ci if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) { 4308c2ecf20Sopenharmony_ci /* 4318c2ecf20Sopenharmony_ci * MAY_EXEC on regular files is denied if the fs is mounted 4328c2ecf20Sopenharmony_ci * with the "noexec" flag. 4338c2ecf20Sopenharmony_ci */ 4348c2ecf20Sopenharmony_ci res = -EACCES; 4358c2ecf20Sopenharmony_ci if (path_noexec(&path)) 4368c2ecf20Sopenharmony_ci goto out_path_release; 4378c2ecf20Sopenharmony_ci } 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci res = inode_permission(inode, mode | MAY_ACCESS); 4408c2ecf20Sopenharmony_ci /* SuS v2 requires we report a read only fs too */ 4418c2ecf20Sopenharmony_ci if (res || !(mode & S_IWOTH) || special_file(inode->i_mode)) 4428c2ecf20Sopenharmony_ci goto out_path_release; 4438c2ecf20Sopenharmony_ci /* 4448c2ecf20Sopenharmony_ci * This is a rare case where using __mnt_is_readonly() 4458c2ecf20Sopenharmony_ci * is OK without a mnt_want/drop_write() pair. Since 4468c2ecf20Sopenharmony_ci * no actual write to the fs is performed here, we do 4478c2ecf20Sopenharmony_ci * not need to telegraph to that to anyone. 4488c2ecf20Sopenharmony_ci * 4498c2ecf20Sopenharmony_ci * By doing this, we accept that this access is 4508c2ecf20Sopenharmony_ci * inherently racy and know that the fs may change 4518c2ecf20Sopenharmony_ci * state before we even see this result. 4528c2ecf20Sopenharmony_ci */ 4538c2ecf20Sopenharmony_ci if (__mnt_is_readonly(path.mnt)) 4548c2ecf20Sopenharmony_ci res = -EROFS; 4558c2ecf20Sopenharmony_ci 4568c2ecf20Sopenharmony_ciout_path_release: 4578c2ecf20Sopenharmony_ci path_put(&path); 4588c2ecf20Sopenharmony_ci if (retry_estale(res, lookup_flags)) { 4598c2ecf20Sopenharmony_ci lookup_flags |= LOOKUP_REVAL; 4608c2ecf20Sopenharmony_ci goto retry; 4618c2ecf20Sopenharmony_ci } 4628c2ecf20Sopenharmony_ciout: 4638c2ecf20Sopenharmony_ci if (old_cred) 4648c2ecf20Sopenharmony_ci revert_creds(old_cred); 4658c2ecf20Sopenharmony_ci 4668c2ecf20Sopenharmony_ci return res; 4678c2ecf20Sopenharmony_ci} 4688c2ecf20Sopenharmony_ci 4698c2ecf20Sopenharmony_ciSYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode) 4708c2ecf20Sopenharmony_ci{ 4718c2ecf20Sopenharmony_ci return do_faccessat(dfd, filename, mode, 0); 4728c2ecf20Sopenharmony_ci} 4738c2ecf20Sopenharmony_ci 4748c2ecf20Sopenharmony_ciSYSCALL_DEFINE4(faccessat2, int, dfd, const char __user *, filename, int, mode, 4758c2ecf20Sopenharmony_ci int, flags) 4768c2ecf20Sopenharmony_ci{ 4778c2ecf20Sopenharmony_ci return do_faccessat(dfd, filename, mode, flags); 4788c2ecf20Sopenharmony_ci} 4798c2ecf20Sopenharmony_ci 4808c2ecf20Sopenharmony_ciSYSCALL_DEFINE2(access, const char __user *, filename, int, mode) 4818c2ecf20Sopenharmony_ci{ 4828c2ecf20Sopenharmony_ci return do_faccessat(AT_FDCWD, filename, mode, 0); 4838c2ecf20Sopenharmony_ci} 4848c2ecf20Sopenharmony_ci 4858c2ecf20Sopenharmony_ciSYSCALL_DEFINE1(chdir, const char __user *, filename) 4868c2ecf20Sopenharmony_ci{ 4878c2ecf20Sopenharmony_ci struct path path; 4888c2ecf20Sopenharmony_ci int error; 4898c2ecf20Sopenharmony_ci unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 4908c2ecf20Sopenharmony_ciretry: 4918c2ecf20Sopenharmony_ci error = user_path_at(AT_FDCWD, filename, lookup_flags, &path); 4928c2ecf20Sopenharmony_ci if (error) 4938c2ecf20Sopenharmony_ci goto out; 4948c2ecf20Sopenharmony_ci 4958c2ecf20Sopenharmony_ci error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR); 4968c2ecf20Sopenharmony_ci if (error) 4978c2ecf20Sopenharmony_ci goto dput_and_out; 4988c2ecf20Sopenharmony_ci 4998c2ecf20Sopenharmony_ci set_fs_pwd(current->fs, &path); 5008c2ecf20Sopenharmony_ci 5018c2ecf20Sopenharmony_cidput_and_out: 5028c2ecf20Sopenharmony_ci path_put(&path); 5038c2ecf20Sopenharmony_ci if (retry_estale(error, lookup_flags)) { 5048c2ecf20Sopenharmony_ci lookup_flags |= LOOKUP_REVAL; 5058c2ecf20Sopenharmony_ci goto retry; 5068c2ecf20Sopenharmony_ci } 5078c2ecf20Sopenharmony_ciout: 5088c2ecf20Sopenharmony_ci return error; 5098c2ecf20Sopenharmony_ci} 5108c2ecf20Sopenharmony_ci 5118c2ecf20Sopenharmony_ciSYSCALL_DEFINE1(fchdir, unsigned int, fd) 5128c2ecf20Sopenharmony_ci{ 5138c2ecf20Sopenharmony_ci struct fd f = fdget_raw(fd); 5148c2ecf20Sopenharmony_ci int error; 5158c2ecf20Sopenharmony_ci 5168c2ecf20Sopenharmony_ci error = -EBADF; 5178c2ecf20Sopenharmony_ci if (!f.file) 5188c2ecf20Sopenharmony_ci goto out; 5198c2ecf20Sopenharmony_ci 5208c2ecf20Sopenharmony_ci error = -ENOTDIR; 5218c2ecf20Sopenharmony_ci if (!d_can_lookup(f.file->f_path.dentry)) 5228c2ecf20Sopenharmony_ci goto out_putf; 5238c2ecf20Sopenharmony_ci 5248c2ecf20Sopenharmony_ci error = inode_permission(file_inode(f.file), MAY_EXEC | MAY_CHDIR); 5258c2ecf20Sopenharmony_ci if (!error) 5268c2ecf20Sopenharmony_ci set_fs_pwd(current->fs, &f.file->f_path); 5278c2ecf20Sopenharmony_ciout_putf: 5288c2ecf20Sopenharmony_ci fdput(f); 5298c2ecf20Sopenharmony_ciout: 5308c2ecf20Sopenharmony_ci return error; 5318c2ecf20Sopenharmony_ci} 5328c2ecf20Sopenharmony_ci 5338c2ecf20Sopenharmony_ciSYSCALL_DEFINE1(chroot, const char __user *, filename) 5348c2ecf20Sopenharmony_ci{ 5358c2ecf20Sopenharmony_ci struct path path; 5368c2ecf20Sopenharmony_ci int error; 5378c2ecf20Sopenharmony_ci unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 5388c2ecf20Sopenharmony_ciretry: 5398c2ecf20Sopenharmony_ci error = user_path_at(AT_FDCWD, filename, lookup_flags, &path); 5408c2ecf20Sopenharmony_ci if (error) 5418c2ecf20Sopenharmony_ci goto out; 5428c2ecf20Sopenharmony_ci 5438c2ecf20Sopenharmony_ci error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR); 5448c2ecf20Sopenharmony_ci if (error) 5458c2ecf20Sopenharmony_ci goto dput_and_out; 5468c2ecf20Sopenharmony_ci 5478c2ecf20Sopenharmony_ci error = -EPERM; 5488c2ecf20Sopenharmony_ci if (!ns_capable(current_user_ns(), CAP_SYS_CHROOT)) 5498c2ecf20Sopenharmony_ci goto dput_and_out; 5508c2ecf20Sopenharmony_ci error = security_path_chroot(&path); 5518c2ecf20Sopenharmony_ci if (error) 5528c2ecf20Sopenharmony_ci goto dput_and_out; 5538c2ecf20Sopenharmony_ci 5548c2ecf20Sopenharmony_ci set_fs_root(current->fs, &path); 5558c2ecf20Sopenharmony_ci error = 0; 5568c2ecf20Sopenharmony_cidput_and_out: 5578c2ecf20Sopenharmony_ci path_put(&path); 5588c2ecf20Sopenharmony_ci if (retry_estale(error, lookup_flags)) { 5598c2ecf20Sopenharmony_ci lookup_flags |= LOOKUP_REVAL; 5608c2ecf20Sopenharmony_ci goto retry; 5618c2ecf20Sopenharmony_ci } 5628c2ecf20Sopenharmony_ciout: 5638c2ecf20Sopenharmony_ci return error; 5648c2ecf20Sopenharmony_ci} 5658c2ecf20Sopenharmony_ci 5668c2ecf20Sopenharmony_ciint chmod_common(const struct path *path, umode_t mode) 5678c2ecf20Sopenharmony_ci{ 5688c2ecf20Sopenharmony_ci struct inode *inode = path->dentry->d_inode; 5698c2ecf20Sopenharmony_ci struct inode *delegated_inode = NULL; 5708c2ecf20Sopenharmony_ci struct iattr newattrs; 5718c2ecf20Sopenharmony_ci int error; 5728c2ecf20Sopenharmony_ci 5738c2ecf20Sopenharmony_ci error = mnt_want_write(path->mnt); 5748c2ecf20Sopenharmony_ci if (error) 5758c2ecf20Sopenharmony_ci return error; 5768c2ecf20Sopenharmony_ciretry_deleg: 5778c2ecf20Sopenharmony_ci inode_lock(inode); 5788c2ecf20Sopenharmony_ci error = security_path_chmod(path, mode); 5798c2ecf20Sopenharmony_ci if (error) 5808c2ecf20Sopenharmony_ci goto out_unlock; 5818c2ecf20Sopenharmony_ci newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO); 5828c2ecf20Sopenharmony_ci newattrs.ia_valid = ATTR_MODE | ATTR_CTIME; 5838c2ecf20Sopenharmony_ci error = notify_change(path->dentry, &newattrs, &delegated_inode); 5848c2ecf20Sopenharmony_ciout_unlock: 5858c2ecf20Sopenharmony_ci inode_unlock(inode); 5868c2ecf20Sopenharmony_ci if (delegated_inode) { 5878c2ecf20Sopenharmony_ci error = break_deleg_wait(&delegated_inode); 5888c2ecf20Sopenharmony_ci if (!error) 5898c2ecf20Sopenharmony_ci goto retry_deleg; 5908c2ecf20Sopenharmony_ci } 5918c2ecf20Sopenharmony_ci mnt_drop_write(path->mnt); 5928c2ecf20Sopenharmony_ci return error; 5938c2ecf20Sopenharmony_ci} 5948c2ecf20Sopenharmony_ci 5958c2ecf20Sopenharmony_ciint vfs_fchmod(struct file *file, umode_t mode) 5968c2ecf20Sopenharmony_ci{ 5978c2ecf20Sopenharmony_ci audit_file(file); 5988c2ecf20Sopenharmony_ci return chmod_common(&file->f_path, mode); 5998c2ecf20Sopenharmony_ci} 6008c2ecf20Sopenharmony_ci 6018c2ecf20Sopenharmony_ciSYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode) 6028c2ecf20Sopenharmony_ci{ 6038c2ecf20Sopenharmony_ci struct fd f = fdget(fd); 6048c2ecf20Sopenharmony_ci int err = -EBADF; 6058c2ecf20Sopenharmony_ci 6068c2ecf20Sopenharmony_ci if (f.file) { 6078c2ecf20Sopenharmony_ci err = vfs_fchmod(f.file, mode); 6088c2ecf20Sopenharmony_ci fdput(f); 6098c2ecf20Sopenharmony_ci } 6108c2ecf20Sopenharmony_ci return err; 6118c2ecf20Sopenharmony_ci} 6128c2ecf20Sopenharmony_ci 6138c2ecf20Sopenharmony_cistatic int do_fchmodat(int dfd, const char __user *filename, umode_t mode) 6148c2ecf20Sopenharmony_ci{ 6158c2ecf20Sopenharmony_ci struct path path; 6168c2ecf20Sopenharmony_ci int error; 6178c2ecf20Sopenharmony_ci unsigned int lookup_flags = LOOKUP_FOLLOW; 6188c2ecf20Sopenharmony_ciretry: 6198c2ecf20Sopenharmony_ci error = user_path_at(dfd, filename, lookup_flags, &path); 6208c2ecf20Sopenharmony_ci if (!error) { 6218c2ecf20Sopenharmony_ci error = chmod_common(&path, mode); 6228c2ecf20Sopenharmony_ci path_put(&path); 6238c2ecf20Sopenharmony_ci if (retry_estale(error, lookup_flags)) { 6248c2ecf20Sopenharmony_ci lookup_flags |= LOOKUP_REVAL; 6258c2ecf20Sopenharmony_ci goto retry; 6268c2ecf20Sopenharmony_ci } 6278c2ecf20Sopenharmony_ci } 6288c2ecf20Sopenharmony_ci return error; 6298c2ecf20Sopenharmony_ci} 6308c2ecf20Sopenharmony_ci 6318c2ecf20Sopenharmony_ciSYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, 6328c2ecf20Sopenharmony_ci umode_t, mode) 6338c2ecf20Sopenharmony_ci{ 6348c2ecf20Sopenharmony_ci return do_fchmodat(dfd, filename, mode); 6358c2ecf20Sopenharmony_ci} 6368c2ecf20Sopenharmony_ci 6378c2ecf20Sopenharmony_ciSYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode) 6388c2ecf20Sopenharmony_ci{ 6398c2ecf20Sopenharmony_ci return do_fchmodat(AT_FDCWD, filename, mode); 6408c2ecf20Sopenharmony_ci} 6418c2ecf20Sopenharmony_ci 6428c2ecf20Sopenharmony_ciint chown_common(const struct path *path, uid_t user, gid_t group) 6438c2ecf20Sopenharmony_ci{ 6448c2ecf20Sopenharmony_ci struct inode *inode = path->dentry->d_inode; 6458c2ecf20Sopenharmony_ci struct inode *delegated_inode = NULL; 6468c2ecf20Sopenharmony_ci int error; 6478c2ecf20Sopenharmony_ci struct iattr newattrs; 6488c2ecf20Sopenharmony_ci kuid_t uid; 6498c2ecf20Sopenharmony_ci kgid_t gid; 6508c2ecf20Sopenharmony_ci 6518c2ecf20Sopenharmony_ci uid = make_kuid(current_user_ns(), user); 6528c2ecf20Sopenharmony_ci gid = make_kgid(current_user_ns(), group); 6538c2ecf20Sopenharmony_ci 6548c2ecf20Sopenharmony_ciretry_deleg: 6558c2ecf20Sopenharmony_ci newattrs.ia_valid = ATTR_CTIME; 6568c2ecf20Sopenharmony_ci if (user != (uid_t) -1) { 6578c2ecf20Sopenharmony_ci if (!uid_valid(uid)) 6588c2ecf20Sopenharmony_ci return -EINVAL; 6598c2ecf20Sopenharmony_ci newattrs.ia_valid |= ATTR_UID; 6608c2ecf20Sopenharmony_ci newattrs.ia_uid = uid; 6618c2ecf20Sopenharmony_ci } 6628c2ecf20Sopenharmony_ci if (group != (gid_t) -1) { 6638c2ecf20Sopenharmony_ci if (!gid_valid(gid)) 6648c2ecf20Sopenharmony_ci return -EINVAL; 6658c2ecf20Sopenharmony_ci newattrs.ia_valid |= ATTR_GID; 6668c2ecf20Sopenharmony_ci newattrs.ia_gid = gid; 6678c2ecf20Sopenharmony_ci } 6688c2ecf20Sopenharmony_ci inode_lock(inode); 6698c2ecf20Sopenharmony_ci if (!S_ISDIR(inode->i_mode)) 6708c2ecf20Sopenharmony_ci newattrs.ia_valid |= ATTR_KILL_SUID | ATTR_KILL_PRIV | 6718c2ecf20Sopenharmony_ci setattr_should_drop_sgid(inode); 6728c2ecf20Sopenharmony_ci error = security_path_chown(path, uid, gid); 6738c2ecf20Sopenharmony_ci if (!error) 6748c2ecf20Sopenharmony_ci error = notify_change(path->dentry, &newattrs, &delegated_inode); 6758c2ecf20Sopenharmony_ci inode_unlock(inode); 6768c2ecf20Sopenharmony_ci if (delegated_inode) { 6778c2ecf20Sopenharmony_ci error = break_deleg_wait(&delegated_inode); 6788c2ecf20Sopenharmony_ci if (!error) 6798c2ecf20Sopenharmony_ci goto retry_deleg; 6808c2ecf20Sopenharmony_ci } 6818c2ecf20Sopenharmony_ci return error; 6828c2ecf20Sopenharmony_ci} 6838c2ecf20Sopenharmony_ci 6848c2ecf20Sopenharmony_ciint do_fchownat(int dfd, const char __user *filename, uid_t user, gid_t group, 6858c2ecf20Sopenharmony_ci int flag) 6868c2ecf20Sopenharmony_ci{ 6878c2ecf20Sopenharmony_ci struct path path; 6888c2ecf20Sopenharmony_ci int error = -EINVAL; 6898c2ecf20Sopenharmony_ci int lookup_flags; 6908c2ecf20Sopenharmony_ci 6918c2ecf20Sopenharmony_ci if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0) 6928c2ecf20Sopenharmony_ci goto out; 6938c2ecf20Sopenharmony_ci 6948c2ecf20Sopenharmony_ci lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW; 6958c2ecf20Sopenharmony_ci if (flag & AT_EMPTY_PATH) 6968c2ecf20Sopenharmony_ci lookup_flags |= LOOKUP_EMPTY; 6978c2ecf20Sopenharmony_ciretry: 6988c2ecf20Sopenharmony_ci error = user_path_at(dfd, filename, lookup_flags, &path); 6998c2ecf20Sopenharmony_ci if (error) 7008c2ecf20Sopenharmony_ci goto out; 7018c2ecf20Sopenharmony_ci error = mnt_want_write(path.mnt); 7028c2ecf20Sopenharmony_ci if (error) 7038c2ecf20Sopenharmony_ci goto out_release; 7048c2ecf20Sopenharmony_ci error = chown_common(&path, user, group); 7058c2ecf20Sopenharmony_ci mnt_drop_write(path.mnt); 7068c2ecf20Sopenharmony_ciout_release: 7078c2ecf20Sopenharmony_ci path_put(&path); 7088c2ecf20Sopenharmony_ci if (retry_estale(error, lookup_flags)) { 7098c2ecf20Sopenharmony_ci lookup_flags |= LOOKUP_REVAL; 7108c2ecf20Sopenharmony_ci goto retry; 7118c2ecf20Sopenharmony_ci } 7128c2ecf20Sopenharmony_ciout: 7138c2ecf20Sopenharmony_ci return error; 7148c2ecf20Sopenharmony_ci} 7158c2ecf20Sopenharmony_ci 7168c2ecf20Sopenharmony_ciSYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user, 7178c2ecf20Sopenharmony_ci gid_t, group, int, flag) 7188c2ecf20Sopenharmony_ci{ 7198c2ecf20Sopenharmony_ci return do_fchownat(dfd, filename, user, group, flag); 7208c2ecf20Sopenharmony_ci} 7218c2ecf20Sopenharmony_ci 7228c2ecf20Sopenharmony_ciSYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group) 7238c2ecf20Sopenharmony_ci{ 7248c2ecf20Sopenharmony_ci return do_fchownat(AT_FDCWD, filename, user, group, 0); 7258c2ecf20Sopenharmony_ci} 7268c2ecf20Sopenharmony_ci 7278c2ecf20Sopenharmony_ciSYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group) 7288c2ecf20Sopenharmony_ci{ 7298c2ecf20Sopenharmony_ci return do_fchownat(AT_FDCWD, filename, user, group, 7308c2ecf20Sopenharmony_ci AT_SYMLINK_NOFOLLOW); 7318c2ecf20Sopenharmony_ci} 7328c2ecf20Sopenharmony_ci 7338c2ecf20Sopenharmony_ciint vfs_fchown(struct file *file, uid_t user, gid_t group) 7348c2ecf20Sopenharmony_ci{ 7358c2ecf20Sopenharmony_ci int error; 7368c2ecf20Sopenharmony_ci 7378c2ecf20Sopenharmony_ci error = mnt_want_write_file(file); 7388c2ecf20Sopenharmony_ci if (error) 7398c2ecf20Sopenharmony_ci return error; 7408c2ecf20Sopenharmony_ci audit_file(file); 7418c2ecf20Sopenharmony_ci error = chown_common(&file->f_path, user, group); 7428c2ecf20Sopenharmony_ci mnt_drop_write_file(file); 7438c2ecf20Sopenharmony_ci return error; 7448c2ecf20Sopenharmony_ci} 7458c2ecf20Sopenharmony_ci 7468c2ecf20Sopenharmony_ciint ksys_fchown(unsigned int fd, uid_t user, gid_t group) 7478c2ecf20Sopenharmony_ci{ 7488c2ecf20Sopenharmony_ci struct fd f = fdget(fd); 7498c2ecf20Sopenharmony_ci int error = -EBADF; 7508c2ecf20Sopenharmony_ci 7518c2ecf20Sopenharmony_ci if (f.file) { 7528c2ecf20Sopenharmony_ci error = vfs_fchown(f.file, user, group); 7538c2ecf20Sopenharmony_ci fdput(f); 7548c2ecf20Sopenharmony_ci } 7558c2ecf20Sopenharmony_ci return error; 7568c2ecf20Sopenharmony_ci} 7578c2ecf20Sopenharmony_ci 7588c2ecf20Sopenharmony_ciSYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group) 7598c2ecf20Sopenharmony_ci{ 7608c2ecf20Sopenharmony_ci return ksys_fchown(fd, user, group); 7618c2ecf20Sopenharmony_ci} 7628c2ecf20Sopenharmony_ci 7638c2ecf20Sopenharmony_cistatic int do_dentry_open(struct file *f, 7648c2ecf20Sopenharmony_ci struct inode *inode, 7658c2ecf20Sopenharmony_ci int (*open)(struct inode *, struct file *)) 7668c2ecf20Sopenharmony_ci{ 7678c2ecf20Sopenharmony_ci static const struct file_operations empty_fops = {}; 7688c2ecf20Sopenharmony_ci int error; 7698c2ecf20Sopenharmony_ci 7708c2ecf20Sopenharmony_ci path_get(&f->f_path); 7718c2ecf20Sopenharmony_ci f->f_inode = inode; 7728c2ecf20Sopenharmony_ci f->f_mapping = inode->i_mapping; 7738c2ecf20Sopenharmony_ci f->f_wb_err = filemap_sample_wb_err(f->f_mapping); 7748c2ecf20Sopenharmony_ci f->f_sb_err = file_sample_sb_err(f); 7758c2ecf20Sopenharmony_ci 7768c2ecf20Sopenharmony_ci if (unlikely(f->f_flags & O_PATH)) { 7778c2ecf20Sopenharmony_ci f->f_mode = FMODE_PATH | FMODE_OPENED; 7788c2ecf20Sopenharmony_ci f->f_op = &empty_fops; 7798c2ecf20Sopenharmony_ci return 0; 7808c2ecf20Sopenharmony_ci } 7818c2ecf20Sopenharmony_ci 7828c2ecf20Sopenharmony_ci if (f->f_mode & FMODE_WRITE && !special_file(inode->i_mode)) { 7838c2ecf20Sopenharmony_ci error = get_write_access(inode); 7848c2ecf20Sopenharmony_ci if (unlikely(error)) 7858c2ecf20Sopenharmony_ci goto cleanup_file; 7868c2ecf20Sopenharmony_ci error = __mnt_want_write(f->f_path.mnt); 7878c2ecf20Sopenharmony_ci if (unlikely(error)) { 7888c2ecf20Sopenharmony_ci put_write_access(inode); 7898c2ecf20Sopenharmony_ci goto cleanup_file; 7908c2ecf20Sopenharmony_ci } 7918c2ecf20Sopenharmony_ci f->f_mode |= FMODE_WRITER; 7928c2ecf20Sopenharmony_ci } 7938c2ecf20Sopenharmony_ci 7948c2ecf20Sopenharmony_ci /* POSIX.1-2008/SUSv4 Section XSI 2.9.7 */ 7958c2ecf20Sopenharmony_ci if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) 7968c2ecf20Sopenharmony_ci f->f_mode |= FMODE_ATOMIC_POS; 7978c2ecf20Sopenharmony_ci 7988c2ecf20Sopenharmony_ci f->f_op = fops_get(inode->i_fop); 7998c2ecf20Sopenharmony_ci if (WARN_ON(!f->f_op)) { 8008c2ecf20Sopenharmony_ci error = -ENODEV; 8018c2ecf20Sopenharmony_ci goto cleanup_all; 8028c2ecf20Sopenharmony_ci } 8038c2ecf20Sopenharmony_ci 8048c2ecf20Sopenharmony_ci error = security_file_open(f); 8058c2ecf20Sopenharmony_ci if (error) 8068c2ecf20Sopenharmony_ci goto cleanup_all; 8078c2ecf20Sopenharmony_ci 8088c2ecf20Sopenharmony_ci error = break_lease(locks_inode(f), f->f_flags); 8098c2ecf20Sopenharmony_ci if (error) 8108c2ecf20Sopenharmony_ci goto cleanup_all; 8118c2ecf20Sopenharmony_ci 8128c2ecf20Sopenharmony_ci /* normally all 3 are set; ->open() can clear them if needed */ 8138c2ecf20Sopenharmony_ci f->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE; 8148c2ecf20Sopenharmony_ci if (!open) 8158c2ecf20Sopenharmony_ci open = f->f_op->open; 8168c2ecf20Sopenharmony_ci if (open) { 8178c2ecf20Sopenharmony_ci error = open(inode, f); 8188c2ecf20Sopenharmony_ci if (error) 8198c2ecf20Sopenharmony_ci goto cleanup_all; 8208c2ecf20Sopenharmony_ci } 8218c2ecf20Sopenharmony_ci f->f_mode |= FMODE_OPENED; 8228c2ecf20Sopenharmony_ci if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) 8238c2ecf20Sopenharmony_ci i_readcount_inc(inode); 8248c2ecf20Sopenharmony_ci if ((f->f_mode & FMODE_READ) && 8258c2ecf20Sopenharmony_ci likely(f->f_op->read || f->f_op->read_iter)) 8268c2ecf20Sopenharmony_ci f->f_mode |= FMODE_CAN_READ; 8278c2ecf20Sopenharmony_ci if ((f->f_mode & FMODE_WRITE) && 8288c2ecf20Sopenharmony_ci likely(f->f_op->write || f->f_op->write_iter)) 8298c2ecf20Sopenharmony_ci f->f_mode |= FMODE_CAN_WRITE; 8308c2ecf20Sopenharmony_ci 8318c2ecf20Sopenharmony_ci f->f_write_hint = WRITE_LIFE_NOT_SET; 8328c2ecf20Sopenharmony_ci f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC); 8338c2ecf20Sopenharmony_ci 8348c2ecf20Sopenharmony_ci file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping); 8358c2ecf20Sopenharmony_ci 8368c2ecf20Sopenharmony_ci /* NB: we're sure to have correct a_ops only after f_op->open */ 8378c2ecf20Sopenharmony_ci if (f->f_flags & O_DIRECT) { 8388c2ecf20Sopenharmony_ci if (!f->f_mapping->a_ops || !f->f_mapping->a_ops->direct_IO) 8398c2ecf20Sopenharmony_ci return -EINVAL; 8408c2ecf20Sopenharmony_ci } 8418c2ecf20Sopenharmony_ci 8428c2ecf20Sopenharmony_ci /* 8438c2ecf20Sopenharmony_ci * XXX: Huge page cache doesn't support writing yet. Drop all page 8448c2ecf20Sopenharmony_ci * cache for this file before processing writes. 8458c2ecf20Sopenharmony_ci */ 8468c2ecf20Sopenharmony_ci if ((f->f_mode & FMODE_WRITE) && filemap_nr_thps(inode->i_mapping)) 8478c2ecf20Sopenharmony_ci truncate_pagecache(inode, 0); 8488c2ecf20Sopenharmony_ci 8498c2ecf20Sopenharmony_ci return 0; 8508c2ecf20Sopenharmony_ci 8518c2ecf20Sopenharmony_cicleanup_all: 8528c2ecf20Sopenharmony_ci if (WARN_ON_ONCE(error > 0)) 8538c2ecf20Sopenharmony_ci error = -EINVAL; 8548c2ecf20Sopenharmony_ci fops_put(f->f_op); 8558c2ecf20Sopenharmony_ci if (f->f_mode & FMODE_WRITER) { 8568c2ecf20Sopenharmony_ci put_write_access(inode); 8578c2ecf20Sopenharmony_ci __mnt_drop_write(f->f_path.mnt); 8588c2ecf20Sopenharmony_ci } 8598c2ecf20Sopenharmony_cicleanup_file: 8608c2ecf20Sopenharmony_ci path_put(&f->f_path); 8618c2ecf20Sopenharmony_ci f->f_path.mnt = NULL; 8628c2ecf20Sopenharmony_ci f->f_path.dentry = NULL; 8638c2ecf20Sopenharmony_ci f->f_inode = NULL; 8648c2ecf20Sopenharmony_ci return error; 8658c2ecf20Sopenharmony_ci} 8668c2ecf20Sopenharmony_ci 8678c2ecf20Sopenharmony_ci/** 8688c2ecf20Sopenharmony_ci * finish_open - finish opening a file 8698c2ecf20Sopenharmony_ci * @file: file pointer 8708c2ecf20Sopenharmony_ci * @dentry: pointer to dentry 8718c2ecf20Sopenharmony_ci * @open: open callback 8728c2ecf20Sopenharmony_ci * @opened: state of open 8738c2ecf20Sopenharmony_ci * 8748c2ecf20Sopenharmony_ci * This can be used to finish opening a file passed to i_op->atomic_open(). 8758c2ecf20Sopenharmony_ci * 8768c2ecf20Sopenharmony_ci * If the open callback is set to NULL, then the standard f_op->open() 8778c2ecf20Sopenharmony_ci * filesystem callback is substituted. 8788c2ecf20Sopenharmony_ci * 8798c2ecf20Sopenharmony_ci * NB: the dentry reference is _not_ consumed. If, for example, the dentry is 8808c2ecf20Sopenharmony_ci * the return value of d_splice_alias(), then the caller needs to perform dput() 8818c2ecf20Sopenharmony_ci * on it after finish_open(). 8828c2ecf20Sopenharmony_ci * 8838c2ecf20Sopenharmony_ci * Returns zero on success or -errno if the open failed. 8848c2ecf20Sopenharmony_ci */ 8858c2ecf20Sopenharmony_ciint finish_open(struct file *file, struct dentry *dentry, 8868c2ecf20Sopenharmony_ci int (*open)(struct inode *, struct file *)) 8878c2ecf20Sopenharmony_ci{ 8888c2ecf20Sopenharmony_ci BUG_ON(file->f_mode & FMODE_OPENED); /* once it's opened, it's opened */ 8898c2ecf20Sopenharmony_ci 8908c2ecf20Sopenharmony_ci file->f_path.dentry = dentry; 8918c2ecf20Sopenharmony_ci return do_dentry_open(file, d_backing_inode(dentry), open); 8928c2ecf20Sopenharmony_ci} 8938c2ecf20Sopenharmony_ciEXPORT_SYMBOL(finish_open); 8948c2ecf20Sopenharmony_ci 8958c2ecf20Sopenharmony_ci/** 8968c2ecf20Sopenharmony_ci * finish_no_open - finish ->atomic_open() without opening the file 8978c2ecf20Sopenharmony_ci * 8988c2ecf20Sopenharmony_ci * @file: file pointer 8998c2ecf20Sopenharmony_ci * @dentry: dentry or NULL (as returned from ->lookup()) 9008c2ecf20Sopenharmony_ci * 9018c2ecf20Sopenharmony_ci * This can be used to set the result of a successful lookup in ->atomic_open(). 9028c2ecf20Sopenharmony_ci * 9038c2ecf20Sopenharmony_ci * NB: unlike finish_open() this function does consume the dentry reference and 9048c2ecf20Sopenharmony_ci * the caller need not dput() it. 9058c2ecf20Sopenharmony_ci * 9068c2ecf20Sopenharmony_ci * Returns "0" which must be the return value of ->atomic_open() after having 9078c2ecf20Sopenharmony_ci * called this function. 9088c2ecf20Sopenharmony_ci */ 9098c2ecf20Sopenharmony_ciint finish_no_open(struct file *file, struct dentry *dentry) 9108c2ecf20Sopenharmony_ci{ 9118c2ecf20Sopenharmony_ci file->f_path.dentry = dentry; 9128c2ecf20Sopenharmony_ci return 0; 9138c2ecf20Sopenharmony_ci} 9148c2ecf20Sopenharmony_ciEXPORT_SYMBOL(finish_no_open); 9158c2ecf20Sopenharmony_ci 9168c2ecf20Sopenharmony_cichar *file_path(struct file *filp, char *buf, int buflen) 9178c2ecf20Sopenharmony_ci{ 9188c2ecf20Sopenharmony_ci return d_path(&filp->f_path, buf, buflen); 9198c2ecf20Sopenharmony_ci} 9208c2ecf20Sopenharmony_ciEXPORT_SYMBOL(file_path); 9218c2ecf20Sopenharmony_ci 9228c2ecf20Sopenharmony_ci/** 9238c2ecf20Sopenharmony_ci * vfs_open - open the file at the given path 9248c2ecf20Sopenharmony_ci * @path: path to open 9258c2ecf20Sopenharmony_ci * @file: newly allocated file with f_flag initialized 9268c2ecf20Sopenharmony_ci * @cred: credentials to use 9278c2ecf20Sopenharmony_ci */ 9288c2ecf20Sopenharmony_ciint vfs_open(const struct path *path, struct file *file) 9298c2ecf20Sopenharmony_ci{ 9308c2ecf20Sopenharmony_ci file->f_path = *path; 9318c2ecf20Sopenharmony_ci return do_dentry_open(file, d_backing_inode(path->dentry), NULL); 9328c2ecf20Sopenharmony_ci} 9338c2ecf20Sopenharmony_ci 9348c2ecf20Sopenharmony_cistruct file *dentry_open(const struct path *path, int flags, 9358c2ecf20Sopenharmony_ci const struct cred *cred) 9368c2ecf20Sopenharmony_ci{ 9378c2ecf20Sopenharmony_ci int error; 9388c2ecf20Sopenharmony_ci struct file *f; 9398c2ecf20Sopenharmony_ci 9408c2ecf20Sopenharmony_ci validate_creds(cred); 9418c2ecf20Sopenharmony_ci 9428c2ecf20Sopenharmony_ci /* We must always pass in a valid mount pointer. */ 9438c2ecf20Sopenharmony_ci BUG_ON(!path->mnt); 9448c2ecf20Sopenharmony_ci 9458c2ecf20Sopenharmony_ci f = alloc_empty_file(flags, cred); 9468c2ecf20Sopenharmony_ci if (!IS_ERR(f)) { 9478c2ecf20Sopenharmony_ci error = vfs_open(path, f); 9488c2ecf20Sopenharmony_ci if (error) { 9498c2ecf20Sopenharmony_ci fput(f); 9508c2ecf20Sopenharmony_ci f = ERR_PTR(error); 9518c2ecf20Sopenharmony_ci } 9528c2ecf20Sopenharmony_ci } 9538c2ecf20Sopenharmony_ci return f; 9548c2ecf20Sopenharmony_ci} 9558c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dentry_open); 9568c2ecf20Sopenharmony_ci 9578c2ecf20Sopenharmony_cistruct file *open_with_fake_path(const struct path *path, int flags, 9588c2ecf20Sopenharmony_ci struct inode *inode, const struct cred *cred) 9598c2ecf20Sopenharmony_ci{ 9608c2ecf20Sopenharmony_ci struct file *f = alloc_empty_file_noaccount(flags, cred); 9618c2ecf20Sopenharmony_ci if (!IS_ERR(f)) { 9628c2ecf20Sopenharmony_ci int error; 9638c2ecf20Sopenharmony_ci 9648c2ecf20Sopenharmony_ci f->f_path = *path; 9658c2ecf20Sopenharmony_ci error = do_dentry_open(f, inode, NULL); 9668c2ecf20Sopenharmony_ci if (error) { 9678c2ecf20Sopenharmony_ci fput(f); 9688c2ecf20Sopenharmony_ci f = ERR_PTR(error); 9698c2ecf20Sopenharmony_ci } 9708c2ecf20Sopenharmony_ci } 9718c2ecf20Sopenharmony_ci return f; 9728c2ecf20Sopenharmony_ci} 9738c2ecf20Sopenharmony_ciEXPORT_SYMBOL(open_with_fake_path); 9748c2ecf20Sopenharmony_ci 9758c2ecf20Sopenharmony_ci#define WILL_CREATE(flags) (flags & (O_CREAT | __O_TMPFILE)) 9768c2ecf20Sopenharmony_ci#define O_PATH_FLAGS (O_DIRECTORY | O_NOFOLLOW | O_PATH | O_CLOEXEC) 9778c2ecf20Sopenharmony_ci 9788c2ecf20Sopenharmony_ciinline struct open_how build_open_how(int flags, umode_t mode) 9798c2ecf20Sopenharmony_ci{ 9808c2ecf20Sopenharmony_ci struct open_how how = { 9818c2ecf20Sopenharmony_ci .flags = flags & VALID_OPEN_FLAGS, 9828c2ecf20Sopenharmony_ci .mode = mode & S_IALLUGO, 9838c2ecf20Sopenharmony_ci }; 9848c2ecf20Sopenharmony_ci 9858c2ecf20Sopenharmony_ci /* O_PATH beats everything else. */ 9868c2ecf20Sopenharmony_ci if (how.flags & O_PATH) 9878c2ecf20Sopenharmony_ci how.flags &= O_PATH_FLAGS; 9888c2ecf20Sopenharmony_ci /* Modes should only be set for create-like flags. */ 9898c2ecf20Sopenharmony_ci if (!WILL_CREATE(how.flags)) 9908c2ecf20Sopenharmony_ci how.mode = 0; 9918c2ecf20Sopenharmony_ci return how; 9928c2ecf20Sopenharmony_ci} 9938c2ecf20Sopenharmony_ci 9948c2ecf20Sopenharmony_ciinline int build_open_flags(const struct open_how *how, struct open_flags *op) 9958c2ecf20Sopenharmony_ci{ 9968c2ecf20Sopenharmony_ci u64 flags = how->flags; 9978c2ecf20Sopenharmony_ci u64 strip = FMODE_NONOTIFY | O_CLOEXEC; 9988c2ecf20Sopenharmony_ci int lookup_flags = 0; 9998c2ecf20Sopenharmony_ci int acc_mode = ACC_MODE(flags); 10008c2ecf20Sopenharmony_ci 10018c2ecf20Sopenharmony_ci BUILD_BUG_ON_MSG(upper_32_bits(VALID_OPEN_FLAGS), 10028c2ecf20Sopenharmony_ci "struct open_flags doesn't yet handle flags > 32 bits"); 10038c2ecf20Sopenharmony_ci 10048c2ecf20Sopenharmony_ci /* 10058c2ecf20Sopenharmony_ci * Strip flags that either shouldn't be set by userspace like 10068c2ecf20Sopenharmony_ci * FMODE_NONOTIFY or that aren't relevant in determining struct 10078c2ecf20Sopenharmony_ci * open_flags like O_CLOEXEC. 10088c2ecf20Sopenharmony_ci */ 10098c2ecf20Sopenharmony_ci flags &= ~strip; 10108c2ecf20Sopenharmony_ci 10118c2ecf20Sopenharmony_ci /* 10128c2ecf20Sopenharmony_ci * Older syscalls implicitly clear all of the invalid flags or argument 10138c2ecf20Sopenharmony_ci * values before calling build_open_flags(), but openat2(2) checks all 10148c2ecf20Sopenharmony_ci * of its arguments. 10158c2ecf20Sopenharmony_ci */ 10168c2ecf20Sopenharmony_ci if (flags & ~VALID_OPEN_FLAGS) 10178c2ecf20Sopenharmony_ci return -EINVAL; 10188c2ecf20Sopenharmony_ci if (how->resolve & ~VALID_RESOLVE_FLAGS) 10198c2ecf20Sopenharmony_ci return -EINVAL; 10208c2ecf20Sopenharmony_ci 10218c2ecf20Sopenharmony_ci /* Scoping flags are mutually exclusive. */ 10228c2ecf20Sopenharmony_ci if ((how->resolve & RESOLVE_BENEATH) && (how->resolve & RESOLVE_IN_ROOT)) 10238c2ecf20Sopenharmony_ci return -EINVAL; 10248c2ecf20Sopenharmony_ci 10258c2ecf20Sopenharmony_ci /* Deal with the mode. */ 10268c2ecf20Sopenharmony_ci if (WILL_CREATE(flags)) { 10278c2ecf20Sopenharmony_ci if (how->mode & ~S_IALLUGO) 10288c2ecf20Sopenharmony_ci return -EINVAL; 10298c2ecf20Sopenharmony_ci op->mode = how->mode | S_IFREG; 10308c2ecf20Sopenharmony_ci } else { 10318c2ecf20Sopenharmony_ci if (how->mode != 0) 10328c2ecf20Sopenharmony_ci return -EINVAL; 10338c2ecf20Sopenharmony_ci op->mode = 0; 10348c2ecf20Sopenharmony_ci } 10358c2ecf20Sopenharmony_ci 10368c2ecf20Sopenharmony_ci /* 10378c2ecf20Sopenharmony_ci * In order to ensure programs get explicit errors when trying to use 10388c2ecf20Sopenharmony_ci * O_TMPFILE on old kernels, O_TMPFILE is implemented such that it 10398c2ecf20Sopenharmony_ci * looks like (O_DIRECTORY|O_RDWR & ~O_CREAT) to old kernels. But we 10408c2ecf20Sopenharmony_ci * have to require userspace to explicitly set it. 10418c2ecf20Sopenharmony_ci */ 10428c2ecf20Sopenharmony_ci if (flags & __O_TMPFILE) { 10438c2ecf20Sopenharmony_ci if ((flags & O_TMPFILE_MASK) != O_TMPFILE) 10448c2ecf20Sopenharmony_ci return -EINVAL; 10458c2ecf20Sopenharmony_ci if (!(acc_mode & MAY_WRITE)) 10468c2ecf20Sopenharmony_ci return -EINVAL; 10478c2ecf20Sopenharmony_ci } 10488c2ecf20Sopenharmony_ci if (flags & O_PATH) { 10498c2ecf20Sopenharmony_ci /* O_PATH only permits certain other flags to be set. */ 10508c2ecf20Sopenharmony_ci if (flags & ~O_PATH_FLAGS) 10518c2ecf20Sopenharmony_ci return -EINVAL; 10528c2ecf20Sopenharmony_ci acc_mode = 0; 10538c2ecf20Sopenharmony_ci } 10548c2ecf20Sopenharmony_ci 10558c2ecf20Sopenharmony_ci /* 10568c2ecf20Sopenharmony_ci * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only 10578c2ecf20Sopenharmony_ci * check for O_DSYNC if the need any syncing at all we enforce it's 10588c2ecf20Sopenharmony_ci * always set instead of having to deal with possibly weird behaviour 10598c2ecf20Sopenharmony_ci * for malicious applications setting only __O_SYNC. 10608c2ecf20Sopenharmony_ci */ 10618c2ecf20Sopenharmony_ci if (flags & __O_SYNC) 10628c2ecf20Sopenharmony_ci flags |= O_DSYNC; 10638c2ecf20Sopenharmony_ci 10648c2ecf20Sopenharmony_ci op->open_flag = flags; 10658c2ecf20Sopenharmony_ci 10668c2ecf20Sopenharmony_ci /* O_TRUNC implies we need access checks for write permissions */ 10678c2ecf20Sopenharmony_ci if (flags & O_TRUNC) 10688c2ecf20Sopenharmony_ci acc_mode |= MAY_WRITE; 10698c2ecf20Sopenharmony_ci 10708c2ecf20Sopenharmony_ci /* Allow the LSM permission hook to distinguish append 10718c2ecf20Sopenharmony_ci access from general write access. */ 10728c2ecf20Sopenharmony_ci if (flags & O_APPEND) 10738c2ecf20Sopenharmony_ci acc_mode |= MAY_APPEND; 10748c2ecf20Sopenharmony_ci 10758c2ecf20Sopenharmony_ci op->acc_mode = acc_mode; 10768c2ecf20Sopenharmony_ci 10778c2ecf20Sopenharmony_ci op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN; 10788c2ecf20Sopenharmony_ci 10798c2ecf20Sopenharmony_ci if (flags & O_CREAT) { 10808c2ecf20Sopenharmony_ci op->intent |= LOOKUP_CREATE; 10818c2ecf20Sopenharmony_ci if (flags & O_EXCL) { 10828c2ecf20Sopenharmony_ci op->intent |= LOOKUP_EXCL; 10838c2ecf20Sopenharmony_ci flags |= O_NOFOLLOW; 10848c2ecf20Sopenharmony_ci } 10858c2ecf20Sopenharmony_ci } 10868c2ecf20Sopenharmony_ci 10878c2ecf20Sopenharmony_ci if (flags & O_DIRECTORY) 10888c2ecf20Sopenharmony_ci lookup_flags |= LOOKUP_DIRECTORY; 10898c2ecf20Sopenharmony_ci if (!(flags & O_NOFOLLOW)) 10908c2ecf20Sopenharmony_ci lookup_flags |= LOOKUP_FOLLOW; 10918c2ecf20Sopenharmony_ci 10928c2ecf20Sopenharmony_ci if (how->resolve & RESOLVE_NO_XDEV) 10938c2ecf20Sopenharmony_ci lookup_flags |= LOOKUP_NO_XDEV; 10948c2ecf20Sopenharmony_ci if (how->resolve & RESOLVE_NO_MAGICLINKS) 10958c2ecf20Sopenharmony_ci lookup_flags |= LOOKUP_NO_MAGICLINKS; 10968c2ecf20Sopenharmony_ci if (how->resolve & RESOLVE_NO_SYMLINKS) 10978c2ecf20Sopenharmony_ci lookup_flags |= LOOKUP_NO_SYMLINKS; 10988c2ecf20Sopenharmony_ci if (how->resolve & RESOLVE_BENEATH) 10998c2ecf20Sopenharmony_ci lookup_flags |= LOOKUP_BENEATH; 11008c2ecf20Sopenharmony_ci if (how->resolve & RESOLVE_IN_ROOT) 11018c2ecf20Sopenharmony_ci lookup_flags |= LOOKUP_IN_ROOT; 11028c2ecf20Sopenharmony_ci if (how->resolve & RESOLVE_CACHED) { 11038c2ecf20Sopenharmony_ci /* Don't bother even trying for create/truncate/tmpfile open */ 11048c2ecf20Sopenharmony_ci if (flags & (O_TRUNC | O_CREAT | __O_TMPFILE)) 11058c2ecf20Sopenharmony_ci return -EAGAIN; 11068c2ecf20Sopenharmony_ci lookup_flags |= LOOKUP_CACHED; 11078c2ecf20Sopenharmony_ci } 11088c2ecf20Sopenharmony_ci 11098c2ecf20Sopenharmony_ci op->lookup_flags = lookup_flags; 11108c2ecf20Sopenharmony_ci return 0; 11118c2ecf20Sopenharmony_ci} 11128c2ecf20Sopenharmony_ci 11138c2ecf20Sopenharmony_ci/** 11148c2ecf20Sopenharmony_ci * file_open_name - open file and return file pointer 11158c2ecf20Sopenharmony_ci * 11168c2ecf20Sopenharmony_ci * @name: struct filename containing path to open 11178c2ecf20Sopenharmony_ci * @flags: open flags as per the open(2) second argument 11188c2ecf20Sopenharmony_ci * @mode: mode for the new file if O_CREAT is set, else ignored 11198c2ecf20Sopenharmony_ci * 11208c2ecf20Sopenharmony_ci * This is the helper to open a file from kernelspace if you really 11218c2ecf20Sopenharmony_ci * have to. But in generally you should not do this, so please move 11228c2ecf20Sopenharmony_ci * along, nothing to see here.. 11238c2ecf20Sopenharmony_ci */ 11248c2ecf20Sopenharmony_cistruct file *file_open_name(struct filename *name, int flags, umode_t mode) 11258c2ecf20Sopenharmony_ci{ 11268c2ecf20Sopenharmony_ci struct open_flags op; 11278c2ecf20Sopenharmony_ci struct open_how how = build_open_how(flags, mode); 11288c2ecf20Sopenharmony_ci int err = build_open_flags(&how, &op); 11298c2ecf20Sopenharmony_ci if (err) 11308c2ecf20Sopenharmony_ci return ERR_PTR(err); 11318c2ecf20Sopenharmony_ci return do_filp_open(AT_FDCWD, name, &op); 11328c2ecf20Sopenharmony_ci} 11338c2ecf20Sopenharmony_ci 11348c2ecf20Sopenharmony_ci/** 11358c2ecf20Sopenharmony_ci * filp_open - open file and return file pointer 11368c2ecf20Sopenharmony_ci * 11378c2ecf20Sopenharmony_ci * @filename: path to open 11388c2ecf20Sopenharmony_ci * @flags: open flags as per the open(2) second argument 11398c2ecf20Sopenharmony_ci * @mode: mode for the new file if O_CREAT is set, else ignored 11408c2ecf20Sopenharmony_ci * 11418c2ecf20Sopenharmony_ci * This is the helper to open a file from kernelspace if you really 11428c2ecf20Sopenharmony_ci * have to. But in generally you should not do this, so please move 11438c2ecf20Sopenharmony_ci * along, nothing to see here.. 11448c2ecf20Sopenharmony_ci */ 11458c2ecf20Sopenharmony_cistruct file *filp_open(const char *filename, int flags, umode_t mode) 11468c2ecf20Sopenharmony_ci{ 11478c2ecf20Sopenharmony_ci struct filename *name = getname_kernel(filename); 11488c2ecf20Sopenharmony_ci struct file *file = ERR_CAST(name); 11498c2ecf20Sopenharmony_ci 11508c2ecf20Sopenharmony_ci if (!IS_ERR(name)) { 11518c2ecf20Sopenharmony_ci file = file_open_name(name, flags, mode); 11528c2ecf20Sopenharmony_ci putname(name); 11538c2ecf20Sopenharmony_ci } 11548c2ecf20Sopenharmony_ci return file; 11558c2ecf20Sopenharmony_ci} 11568c2ecf20Sopenharmony_ciEXPORT_SYMBOL(filp_open); 11578c2ecf20Sopenharmony_ci 11588c2ecf20Sopenharmony_cistruct file *file_open_root(const struct path *root, 11598c2ecf20Sopenharmony_ci const char *filename, int flags, umode_t mode) 11608c2ecf20Sopenharmony_ci{ 11618c2ecf20Sopenharmony_ci struct open_flags op; 11628c2ecf20Sopenharmony_ci struct open_how how = build_open_how(flags, mode); 11638c2ecf20Sopenharmony_ci int err = build_open_flags(&how, &op); 11648c2ecf20Sopenharmony_ci if (err) 11658c2ecf20Sopenharmony_ci return ERR_PTR(err); 11668c2ecf20Sopenharmony_ci return do_file_open_root(root, filename, &op); 11678c2ecf20Sopenharmony_ci} 11688c2ecf20Sopenharmony_ciEXPORT_SYMBOL(file_open_root); 11698c2ecf20Sopenharmony_ci 11708c2ecf20Sopenharmony_cistatic long do_sys_openat2(int dfd, const char __user *filename, 11718c2ecf20Sopenharmony_ci struct open_how *how) 11728c2ecf20Sopenharmony_ci{ 11738c2ecf20Sopenharmony_ci struct open_flags op; 11748c2ecf20Sopenharmony_ci int fd = build_open_flags(how, &op); 11758c2ecf20Sopenharmony_ci struct filename *tmp; 11768c2ecf20Sopenharmony_ci 11778c2ecf20Sopenharmony_ci if (fd) 11788c2ecf20Sopenharmony_ci return fd; 11798c2ecf20Sopenharmony_ci 11808c2ecf20Sopenharmony_ci tmp = getname(filename); 11818c2ecf20Sopenharmony_ci if (IS_ERR(tmp)) 11828c2ecf20Sopenharmony_ci return PTR_ERR(tmp); 11838c2ecf20Sopenharmony_ci 11848c2ecf20Sopenharmony_ci fd = get_unused_fd_flags(how->flags); 11858c2ecf20Sopenharmony_ci if (fd >= 0) { 11868c2ecf20Sopenharmony_ci struct file *f = do_filp_open(dfd, tmp, &op); 11878c2ecf20Sopenharmony_ci if (IS_ERR(f)) { 11888c2ecf20Sopenharmony_ci put_unused_fd(fd); 11898c2ecf20Sopenharmony_ci fd = PTR_ERR(f); 11908c2ecf20Sopenharmony_ci } else { 11918c2ecf20Sopenharmony_ci fsnotify_open(f); 11928c2ecf20Sopenharmony_ci fd_install(fd, f); 11938c2ecf20Sopenharmony_ci } 11948c2ecf20Sopenharmony_ci } 11958c2ecf20Sopenharmony_ci putname(tmp); 11968c2ecf20Sopenharmony_ci return fd; 11978c2ecf20Sopenharmony_ci} 11988c2ecf20Sopenharmony_ci 11998c2ecf20Sopenharmony_cilong do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode) 12008c2ecf20Sopenharmony_ci{ 12018c2ecf20Sopenharmony_ci struct open_how how = build_open_how(flags, mode); 12028c2ecf20Sopenharmony_ci return do_sys_openat2(dfd, filename, &how); 12038c2ecf20Sopenharmony_ci} 12048c2ecf20Sopenharmony_ci 12058c2ecf20Sopenharmony_ci 12068c2ecf20Sopenharmony_ciSYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode) 12078c2ecf20Sopenharmony_ci{ 12088c2ecf20Sopenharmony_ci if (force_o_largefile()) 12098c2ecf20Sopenharmony_ci flags |= O_LARGEFILE; 12108c2ecf20Sopenharmony_ci return do_sys_open(AT_FDCWD, filename, flags, mode); 12118c2ecf20Sopenharmony_ci} 12128c2ecf20Sopenharmony_ci 12138c2ecf20Sopenharmony_ciSYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags, 12148c2ecf20Sopenharmony_ci umode_t, mode) 12158c2ecf20Sopenharmony_ci{ 12168c2ecf20Sopenharmony_ci if (force_o_largefile()) 12178c2ecf20Sopenharmony_ci flags |= O_LARGEFILE; 12188c2ecf20Sopenharmony_ci return do_sys_open(dfd, filename, flags, mode); 12198c2ecf20Sopenharmony_ci} 12208c2ecf20Sopenharmony_ci 12218c2ecf20Sopenharmony_ciSYSCALL_DEFINE4(openat2, int, dfd, const char __user *, filename, 12228c2ecf20Sopenharmony_ci struct open_how __user *, how, size_t, usize) 12238c2ecf20Sopenharmony_ci{ 12248c2ecf20Sopenharmony_ci int err; 12258c2ecf20Sopenharmony_ci struct open_how tmp; 12268c2ecf20Sopenharmony_ci 12278c2ecf20Sopenharmony_ci BUILD_BUG_ON(sizeof(struct open_how) < OPEN_HOW_SIZE_VER0); 12288c2ecf20Sopenharmony_ci BUILD_BUG_ON(sizeof(struct open_how) != OPEN_HOW_SIZE_LATEST); 12298c2ecf20Sopenharmony_ci 12308c2ecf20Sopenharmony_ci if (unlikely(usize < OPEN_HOW_SIZE_VER0)) 12318c2ecf20Sopenharmony_ci return -EINVAL; 12328c2ecf20Sopenharmony_ci 12338c2ecf20Sopenharmony_ci err = copy_struct_from_user(&tmp, sizeof(tmp), how, usize); 12348c2ecf20Sopenharmony_ci if (err) 12358c2ecf20Sopenharmony_ci return err; 12368c2ecf20Sopenharmony_ci 12378c2ecf20Sopenharmony_ci /* O_LARGEFILE is only allowed for non-O_PATH. */ 12388c2ecf20Sopenharmony_ci if (!(tmp.flags & O_PATH) && force_o_largefile()) 12398c2ecf20Sopenharmony_ci tmp.flags |= O_LARGEFILE; 12408c2ecf20Sopenharmony_ci 12418c2ecf20Sopenharmony_ci return do_sys_openat2(dfd, filename, &tmp); 12428c2ecf20Sopenharmony_ci} 12438c2ecf20Sopenharmony_ci 12448c2ecf20Sopenharmony_ci#ifdef CONFIG_COMPAT 12458c2ecf20Sopenharmony_ci/* 12468c2ecf20Sopenharmony_ci * Exactly like sys_open(), except that it doesn't set the 12478c2ecf20Sopenharmony_ci * O_LARGEFILE flag. 12488c2ecf20Sopenharmony_ci */ 12498c2ecf20Sopenharmony_ciCOMPAT_SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode) 12508c2ecf20Sopenharmony_ci{ 12518c2ecf20Sopenharmony_ci return do_sys_open(AT_FDCWD, filename, flags, mode); 12528c2ecf20Sopenharmony_ci} 12538c2ecf20Sopenharmony_ci 12548c2ecf20Sopenharmony_ci/* 12558c2ecf20Sopenharmony_ci * Exactly like sys_openat(), except that it doesn't set the 12568c2ecf20Sopenharmony_ci * O_LARGEFILE flag. 12578c2ecf20Sopenharmony_ci */ 12588c2ecf20Sopenharmony_ciCOMPAT_SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags, umode_t, mode) 12598c2ecf20Sopenharmony_ci{ 12608c2ecf20Sopenharmony_ci return do_sys_open(dfd, filename, flags, mode); 12618c2ecf20Sopenharmony_ci} 12628c2ecf20Sopenharmony_ci#endif 12638c2ecf20Sopenharmony_ci 12648c2ecf20Sopenharmony_ci#ifndef __alpha__ 12658c2ecf20Sopenharmony_ci 12668c2ecf20Sopenharmony_ci/* 12678c2ecf20Sopenharmony_ci * For backward compatibility? Maybe this should be moved 12688c2ecf20Sopenharmony_ci * into arch/i386 instead? 12698c2ecf20Sopenharmony_ci */ 12708c2ecf20Sopenharmony_ciSYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode) 12718c2ecf20Sopenharmony_ci{ 12728c2ecf20Sopenharmony_ci int flags = O_CREAT | O_WRONLY | O_TRUNC; 12738c2ecf20Sopenharmony_ci 12748c2ecf20Sopenharmony_ci if (force_o_largefile()) 12758c2ecf20Sopenharmony_ci flags |= O_LARGEFILE; 12768c2ecf20Sopenharmony_ci return do_sys_open(AT_FDCWD, pathname, flags, mode); 12778c2ecf20Sopenharmony_ci} 12788c2ecf20Sopenharmony_ci#endif 12798c2ecf20Sopenharmony_ci 12808c2ecf20Sopenharmony_ci/* 12818c2ecf20Sopenharmony_ci * "id" is the POSIX thread ID. We use the 12828c2ecf20Sopenharmony_ci * files pointer for this.. 12838c2ecf20Sopenharmony_ci */ 12848c2ecf20Sopenharmony_ciint filp_close(struct file *filp, fl_owner_t id) 12858c2ecf20Sopenharmony_ci{ 12868c2ecf20Sopenharmony_ci int retval = 0; 12878c2ecf20Sopenharmony_ci 12888c2ecf20Sopenharmony_ci if (!file_count(filp)) { 12898c2ecf20Sopenharmony_ci printk(KERN_ERR "VFS: Close: file count is 0\n"); 12908c2ecf20Sopenharmony_ci return 0; 12918c2ecf20Sopenharmony_ci } 12928c2ecf20Sopenharmony_ci 12938c2ecf20Sopenharmony_ci if (filp->f_op->flush) 12948c2ecf20Sopenharmony_ci retval = filp->f_op->flush(filp, id); 12958c2ecf20Sopenharmony_ci 12968c2ecf20Sopenharmony_ci if (likely(!(filp->f_mode & FMODE_PATH))) { 12978c2ecf20Sopenharmony_ci dnotify_flush(filp, id); 12988c2ecf20Sopenharmony_ci locks_remove_posix(filp, id); 12998c2ecf20Sopenharmony_ci } 13008c2ecf20Sopenharmony_ci fput(filp); 13018c2ecf20Sopenharmony_ci return retval; 13028c2ecf20Sopenharmony_ci} 13038c2ecf20Sopenharmony_ci 13048c2ecf20Sopenharmony_ciEXPORT_SYMBOL(filp_close); 13058c2ecf20Sopenharmony_ci 13068c2ecf20Sopenharmony_ci/* 13078c2ecf20Sopenharmony_ci * Careful here! We test whether the file pointer is NULL before 13088c2ecf20Sopenharmony_ci * releasing the fd. This ensures that one clone task can't release 13098c2ecf20Sopenharmony_ci * an fd while another clone is opening it. 13108c2ecf20Sopenharmony_ci */ 13118c2ecf20Sopenharmony_ciSYSCALL_DEFINE1(close, unsigned int, fd) 13128c2ecf20Sopenharmony_ci{ 13138c2ecf20Sopenharmony_ci int retval = __close_fd(current->files, fd); 13148c2ecf20Sopenharmony_ci 13158c2ecf20Sopenharmony_ci /* can't restart close syscall because file table entry was cleared */ 13168c2ecf20Sopenharmony_ci if (unlikely(retval == -ERESTARTSYS || 13178c2ecf20Sopenharmony_ci retval == -ERESTARTNOINTR || 13188c2ecf20Sopenharmony_ci retval == -ERESTARTNOHAND || 13198c2ecf20Sopenharmony_ci retval == -ERESTART_RESTARTBLOCK)) 13208c2ecf20Sopenharmony_ci retval = -EINTR; 13218c2ecf20Sopenharmony_ci 13228c2ecf20Sopenharmony_ci return retval; 13238c2ecf20Sopenharmony_ci} 13248c2ecf20Sopenharmony_ci 13258c2ecf20Sopenharmony_ci/** 13268c2ecf20Sopenharmony_ci * close_range() - Close all file descriptors in a given range. 13278c2ecf20Sopenharmony_ci * 13288c2ecf20Sopenharmony_ci * @fd: starting file descriptor to close 13298c2ecf20Sopenharmony_ci * @max_fd: last file descriptor to close 13308c2ecf20Sopenharmony_ci * @flags: reserved for future extensions 13318c2ecf20Sopenharmony_ci * 13328c2ecf20Sopenharmony_ci * This closes a range of file descriptors. All file descriptors 13338c2ecf20Sopenharmony_ci * from @fd up to and including @max_fd are closed. 13348c2ecf20Sopenharmony_ci * Currently, errors to close a given file descriptor are ignored. 13358c2ecf20Sopenharmony_ci */ 13368c2ecf20Sopenharmony_ciSYSCALL_DEFINE3(close_range, unsigned int, fd, unsigned int, max_fd, 13378c2ecf20Sopenharmony_ci unsigned int, flags) 13388c2ecf20Sopenharmony_ci{ 13398c2ecf20Sopenharmony_ci return __close_range(fd, max_fd, flags); 13408c2ecf20Sopenharmony_ci} 13418c2ecf20Sopenharmony_ci 13428c2ecf20Sopenharmony_ci/* 13438c2ecf20Sopenharmony_ci * This routine simulates a hangup on the tty, to arrange that users 13448c2ecf20Sopenharmony_ci * are given clean terminals at login time. 13458c2ecf20Sopenharmony_ci */ 13468c2ecf20Sopenharmony_ciSYSCALL_DEFINE0(vhangup) 13478c2ecf20Sopenharmony_ci{ 13488c2ecf20Sopenharmony_ci if (capable(CAP_SYS_TTY_CONFIG)) { 13498c2ecf20Sopenharmony_ci tty_vhangup_self(); 13508c2ecf20Sopenharmony_ci return 0; 13518c2ecf20Sopenharmony_ci } 13528c2ecf20Sopenharmony_ci return -EPERM; 13538c2ecf20Sopenharmony_ci} 13548c2ecf20Sopenharmony_ci 13558c2ecf20Sopenharmony_ci/* 13568c2ecf20Sopenharmony_ci * Called when an inode is about to be open. 13578c2ecf20Sopenharmony_ci * We use this to disallow opening large files on 32bit systems if 13588c2ecf20Sopenharmony_ci * the caller didn't specify O_LARGEFILE. On 64bit systems we force 13598c2ecf20Sopenharmony_ci * on this flag in sys_open. 13608c2ecf20Sopenharmony_ci */ 13618c2ecf20Sopenharmony_ciint generic_file_open(struct inode * inode, struct file * filp) 13628c2ecf20Sopenharmony_ci{ 13638c2ecf20Sopenharmony_ci if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS) 13648c2ecf20Sopenharmony_ci return -EOVERFLOW; 13658c2ecf20Sopenharmony_ci return 0; 13668c2ecf20Sopenharmony_ci} 13678c2ecf20Sopenharmony_ci 13688c2ecf20Sopenharmony_ciEXPORT_SYMBOL(generic_file_open); 13698c2ecf20Sopenharmony_ci 13708c2ecf20Sopenharmony_ci/* 13718c2ecf20Sopenharmony_ci * This is used by subsystems that don't want seekable 13728c2ecf20Sopenharmony_ci * file descriptors. The function is not supposed to ever fail, the only 13738c2ecf20Sopenharmony_ci * reason it returns an 'int' and not 'void' is so that it can be plugged 13748c2ecf20Sopenharmony_ci * directly into file_operations structure. 13758c2ecf20Sopenharmony_ci */ 13768c2ecf20Sopenharmony_ciint nonseekable_open(struct inode *inode, struct file *filp) 13778c2ecf20Sopenharmony_ci{ 13788c2ecf20Sopenharmony_ci filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE); 13798c2ecf20Sopenharmony_ci return 0; 13808c2ecf20Sopenharmony_ci} 13818c2ecf20Sopenharmony_ci 13828c2ecf20Sopenharmony_ciEXPORT_SYMBOL(nonseekable_open); 13838c2ecf20Sopenharmony_ci 13848c2ecf20Sopenharmony_ci/* 13858c2ecf20Sopenharmony_ci * stream_open is used by subsystems that want stream-like file descriptors. 13868c2ecf20Sopenharmony_ci * Such file descriptors are not seekable and don't have notion of position 13878c2ecf20Sopenharmony_ci * (file.f_pos is always 0 and ppos passed to .read()/.write() is always NULL). 13888c2ecf20Sopenharmony_ci * Contrary to file descriptors of other regular files, .read() and .write() 13898c2ecf20Sopenharmony_ci * can run simultaneously. 13908c2ecf20Sopenharmony_ci * 13918c2ecf20Sopenharmony_ci * stream_open never fails and is marked to return int so that it could be 13928c2ecf20Sopenharmony_ci * directly used as file_operations.open . 13938c2ecf20Sopenharmony_ci */ 13948c2ecf20Sopenharmony_ciint stream_open(struct inode *inode, struct file *filp) 13958c2ecf20Sopenharmony_ci{ 13968c2ecf20Sopenharmony_ci filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE | FMODE_ATOMIC_POS); 13978c2ecf20Sopenharmony_ci filp->f_mode |= FMODE_STREAM; 13988c2ecf20Sopenharmony_ci return 0; 13998c2ecf20Sopenharmony_ci} 14008c2ecf20Sopenharmony_ci 14018c2ecf20Sopenharmony_ciEXPORT_SYMBOL(stream_open); 1402