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