fdget_raw() users: switch to CLASS(fd_raw)

Reviewed-by: Christian Brauner <brauner@kernel.org>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
This commit is contained in:
Al Viro 2024-05-31 22:45:26 -04:00
parent a6f46579d7
commit 048181992c
8 changed files with 47 additions and 83 deletions

View File

@ -235,12 +235,12 @@ asmlinkage long sys_oabi_fcntl64(unsigned int fd, unsigned int cmd,
unsigned long arg)
{
void __user *argp = (void __user *)arg;
struct fd f = fdget_raw(fd);
CLASS(fd_raw, f)(fd);
struct flock64 flock;
long err = -EBADF;
long err;
if (!fd_file(f))
goto out;
if (fd_empty(f))
return -EBADF;
switch (cmd) {
case F_GETLK64:
@ -271,8 +271,6 @@ asmlinkage long sys_oabi_fcntl64(unsigned int fd, unsigned int cmd,
err = sys_fcntl64(fd, cmd, arg);
break;
}
fdput(f);
out:
return err;
}

View File

@ -570,24 +570,21 @@ static int check_fcntl_cmd(unsigned cmd)
SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
{
struct fd f = fdget_raw(fd);
long err = -EBADF;
CLASS(fd_raw, f)(fd);
long err;
if (!fd_file(f))
goto out;
if (fd_empty(f))
return -EBADF;
if (unlikely(fd_file(f)->f_mode & FMODE_PATH)) {
if (!check_fcntl_cmd(cmd))
goto out1;
return -EBADF;
}
err = security_file_fcntl(fd_file(f), cmd, arg);
if (!err)
err = do_fcntl(fd, cmd, arg, fd_file(f));
out1:
fdput(f);
out:
return err;
}
@ -596,21 +593,21 @@ SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
unsigned long, arg)
{
void __user *argp = (void __user *)arg;
struct fd f = fdget_raw(fd);
CLASS(fd_raw, f)(fd);
struct flock64 flock;
long err = -EBADF;
long err;
if (!fd_file(f))
goto out;
if (fd_empty(f))
return -EBADF;
if (unlikely(fd_file(f)->f_mode & FMODE_PATH)) {
if (!check_fcntl_cmd(cmd))
goto out1;
return -EBADF;
}
err = security_file_fcntl(fd_file(f), cmd, arg);
if (err)
goto out1;
return err;
switch (cmd) {
case F_GETLK64:
@ -635,9 +632,6 @@ SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
err = do_fcntl(fd, cmd, arg, fd_file(f));
break;
}
out1:
fdput(f);
out:
return err;
}
#endif
@ -733,21 +727,21 @@ static int fixup_compat_flock(struct flock *flock)
static long do_compat_fcntl64(unsigned int fd, unsigned int cmd,
compat_ulong_t arg)
{
struct fd f = fdget_raw(fd);
CLASS(fd_raw, f)(fd);
struct flock flock;
long err = -EBADF;
long err;
if (!fd_file(f))
return err;
if (fd_empty(f))
return -EBADF;
if (unlikely(fd_file(f)->f_mode & FMODE_PATH)) {
if (!check_fcntl_cmd(cmd))
goto out_put;
return -EBADF;
}
err = security_file_fcntl(fd_file(f), cmd, arg);
if (err)
goto out_put;
return err;
switch (cmd) {
case F_GETLK:
@ -790,8 +784,6 @@ static long do_compat_fcntl64(unsigned int fd, unsigned int cmd,
err = do_fcntl(fd, cmd, arg, fd_file(f));
break;
}
out_put:
fdput(f);
return err;
}

View File

@ -2503,26 +2503,22 @@ static const char *path_init(struct nameidata *nd, unsigned flags)
}
} else {
/* Caller must check execute permissions on the starting path component */
struct fd f = fdget_raw(nd->dfd);
CLASS(fd_raw, f)(nd->dfd);
struct dentry *dentry;
if (!fd_file(f))
if (fd_empty(f))
return ERR_PTR(-EBADF);
if (flags & LOOKUP_LINKAT_EMPTY) {
if (fd_file(f)->f_cred != current_cred() &&
!ns_capable(fd_file(f)->f_cred->user_ns, CAP_DAC_READ_SEARCH)) {
fdput(f);
!ns_capable(fd_file(f)->f_cred->user_ns, CAP_DAC_READ_SEARCH))
return ERR_PTR(-ENOENT);
}
}
dentry = fd_file(f)->f_path.dentry;
if (*s && unlikely(!d_can_lookup(dentry))) {
fdput(f);
if (*s && unlikely(!d_can_lookup(dentry)))
return ERR_PTR(-ENOTDIR);
}
nd->path = fd_file(f)->f_path;
if (flags & LOOKUP_RCU) {
@ -2532,7 +2528,6 @@ static const char *path_init(struct nameidata *nd, unsigned flags)
path_get(&nd->path);
nd->inode = nd->path.dentry->d_inode;
}
fdput(f);
}
/* For scoped-lookups we need to set the root to the dirfd as well. */

View File

@ -580,23 +580,18 @@ out:
SYSCALL_DEFINE1(fchdir, unsigned int, fd)
{
struct fd f = fdget_raw(fd);
CLASS(fd_raw, f)(fd);
int error;
error = -EBADF;
if (!fd_file(f))
goto out;
if (fd_empty(f))
return -EBADF;
error = -ENOTDIR;
if (!d_can_lookup(fd_file(f)->f_path.dentry))
goto out_putf;
return -ENOTDIR;
error = file_permission(fd_file(f), MAY_EXEC | MAY_CHDIR);
if (!error)
set_fs_pwd(current->fs, &fd_file(f)->f_path);
out_putf:
fdput(f);
out:
return error;
}

View File

@ -976,21 +976,19 @@ SYSCALL_DEFINE4(quotactl_fd, unsigned int, fd, unsigned int, cmd,
struct super_block *sb;
unsigned int cmds = cmd >> SUBCMDSHIFT;
unsigned int type = cmd & SUBCMDMASK;
struct fd f;
CLASS(fd_raw, f)(fd);
int ret;
f = fdget_raw(fd);
if (!fd_file(f))
if (fd_empty(f))
return -EBADF;
ret = -EINVAL;
if (type >= MAXQUOTAS)
goto out;
return -EINVAL;
if (quotactl_cmd_write(cmds)) {
ret = mnt_want_write(fd_file(f)->f_path.mnt);
if (ret)
goto out;
return ret;
}
sb = fd_file(f)->f_path.mnt->mnt_sb;
@ -1008,7 +1006,5 @@ SYSCALL_DEFINE4(quotactl_fd, unsigned int, fd, unsigned int, cmd,
if (quotactl_cmd_write(cmds))
mnt_drop_write(fd_file(f)->f_path.mnt);
out:
fdput(f);
return ret;
}

View File

@ -114,13 +114,11 @@ retry:
int fd_statfs(int fd, struct kstatfs *st)
{
struct fd f = fdget_raw(fd);
int error = -EBADF;
if (fd_file(f)) {
error = vfs_statfs(&fd_file(f)->f_path, st);
fdput(f);
}
return error;
CLASS(fd_raw, f)(fd);
if (fd_empty(f))
return -EBADF;
return vfs_statfs(&fd_file(f)->f_path, st);
}
static int do_statfs_native(struct kstatfs *st, struct statfs __user *p)

View File

@ -6966,14 +6966,11 @@ EXPORT_SYMBOL_GPL(cgroup_get_from_path);
*/
struct cgroup *cgroup_v1v2_get_from_fd(int fd)
{
struct cgroup *cgrp;
struct fd f = fdget_raw(fd);
if (!fd_file(f))
CLASS(fd_raw, f)(fd);
if (fd_empty(f))
return ERR_PTR(-EBADF);
cgrp = cgroup_v1v2_get_from_file(fd_file(f));
fdput(f);
return cgrp;
return cgroup_v1v2_get_from_file(fd_file(f));
}
/**

View File

@ -276,15 +276,12 @@ out_fdput:
*/
static int get_path_from_fd(const s32 fd, struct path *const path)
{
struct fd f;
int err = 0;
CLASS(fd_raw, f)(fd);
BUILD_BUG_ON(!__same_type(
fd, ((struct landlock_path_beneath_attr *)NULL)->parent_fd));
/* Handles O_PATH. */
f = fdget_raw(fd);
if (!fd_file(f))
if (fd_empty(f))
return -EBADF;
/*
* Forbids ruleset FDs, internal filesystems (e.g. nsfs), including
@ -295,16 +292,12 @@ static int get_path_from_fd(const s32 fd, struct path *const path)
(fd_file(f)->f_path.mnt->mnt_flags & MNT_INTERNAL) ||
(fd_file(f)->f_path.dentry->d_sb->s_flags & SB_NOUSER) ||
d_is_negative(fd_file(f)->f_path.dentry) ||
IS_PRIVATE(d_backing_inode(fd_file(f)->f_path.dentry))) {
err = -EBADFD;
goto out_fdput;
}
IS_PRIVATE(d_backing_inode(fd_file(f)->f_path.dentry)))
return -EBADFD;
*path = fd_file(f)->f_path;
path_get(path);
out_fdput:
fdput(f);
return err;
return 0;
}
static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,