Skip to content

Commit

Permalink
fdget_raw() users: switch to CLASS(fd_raw)
Browse files Browse the repository at this point in the history
Reviewed-by: Christian Brauner <[email protected]>
Signed-off-by: Al Viro <[email protected]>
  • Loading branch information
Al Viro committed Nov 3, 2024
1 parent a6f4657 commit 0481819
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 83 deletions.
10 changes: 4 additions & 6 deletions arch/arm/kernel/sys_oabi-compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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;
}

Expand Down
42 changes: 17 additions & 25 deletions fs/fcntl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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;
}

Expand Down
13 changes: 4 additions & 9 deletions fs/namei.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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. */
Expand Down
13 changes: 4 additions & 9 deletions fs/open.c
Original file line number Diff line number Diff line change
Expand Up @@ -580,23 +580,18 @@ SYSCALL_DEFINE1(chdir, const char __user *, filename)

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;
}

Expand Down
12 changes: 4 additions & 8 deletions fs/quota/quota.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
12 changes: 5 additions & 7 deletions fs/statfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,11 @@ int user_statfs(const char __user *pathname, struct kstatfs *st)

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)
Expand Down
9 changes: 3 additions & 6 deletions kernel/cgroup/cgroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
19 changes: 6 additions & 13 deletions security/landlock/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,15 +276,12 @@ static struct landlock_ruleset *get_ruleset_from_fd(const int fd,
*/
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
Expand All @@ -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,
Expand Down

0 comments on commit 0481819

Please sign in to comment.