Skip to content

Commit

Permalink
fdget(), more trivial conversions
Browse files Browse the repository at this point in the history
all failure exits prior to fdget() leave the scope, all matching fdput()
are immediately followed by leaving the scope.

[xfs_ioc_commit_range() chunk moved here as well]

Reviewed-by: Christian Brauner <[email protected]>
Signed-off-by: Al Viro <[email protected]>
  • Loading branch information
Al Viro committed Nov 3, 2024
1 parent 6348be0 commit 8152f82
Show file tree
Hide file tree
Showing 26 changed files with 202 additions and 418 deletions.
19 changes: 6 additions & 13 deletions drivers/infiniband/core/ucma.c
Original file line number Diff line number Diff line change
Expand Up @@ -1615,29 +1615,24 @@ static ssize_t ucma_migrate_id(struct ucma_file *new_file,
struct ucma_event *uevent, *tmp;
struct ucma_context *ctx;
LIST_HEAD(event_list);
struct fd f;
struct ucma_file *cur_file;
int ret = 0;

if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
return -EFAULT;

/* Get current fd to protect against it being closed */
f = fdget(cmd.fd);
if (!fd_file(f))
CLASS(fd, f)(cmd.fd);
if (fd_empty(f))
return -ENOENT;
if (fd_file(f)->f_op != &ucma_fops) {
ret = -EINVAL;
goto file_put;
}
if (fd_file(f)->f_op != &ucma_fops)
return -EINVAL;
cur_file = fd_file(f)->private_data;

/* Validate current fd and prevent destruction of id. */
ctx = ucma_get_ctx(cur_file, cmd.id);
if (IS_ERR(ctx)) {
ret = PTR_ERR(ctx);
goto file_put;
}
if (IS_ERR(ctx))
return PTR_ERR(ctx);

rdma_lock_handler(ctx->cm_id);
/*
Expand Down Expand Up @@ -1678,8 +1673,6 @@ static ssize_t ucma_migrate_id(struct ucma_file *new_file,
err_unlock:
rdma_unlock_handler(ctx->cm_id);
ucma_put_ctx(ctx);
file_put:
fdput(f);
return ret;
}

Expand Down
6 changes: 2 additions & 4 deletions drivers/vfio/group.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,14 @@ static int vfio_group_ioctl_set_container(struct vfio_group *group,
{
struct vfio_container *container;
struct iommufd_ctx *iommufd;
struct fd f;
int ret;
int fd;

if (get_user(fd, arg))
return -EFAULT;

f = fdget(fd);
if (!fd_file(f))
CLASS(fd, f)(fd);
if (fd_empty(f))
return -EBADF;

mutex_lock(&group->group_lock);
Expand Down Expand Up @@ -153,7 +152,6 @@ static int vfio_group_ioctl_set_container(struct vfio_group *group,

out_unlock:
mutex_unlock(&group->group_lock);
fdput(f);
return ret;
}

Expand Down
15 changes: 4 additions & 11 deletions fs/eventpoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -2415,8 +2415,6 @@ SYSCALL_DEFINE4(epoll_ctl, int, epfd, int, op, int, fd,
static int do_epoll_wait(int epfd, struct epoll_event __user *events,
int maxevents, struct timespec64 *to)
{
int error;
struct fd f;
struct eventpoll *ep;

/* The maximum number of event must be greater than zero */
Expand All @@ -2428,17 +2426,16 @@ static int do_epoll_wait(int epfd, struct epoll_event __user *events,
return -EFAULT;

/* Get the "struct file *" for the eventpoll file */
f = fdget(epfd);
if (!fd_file(f))
CLASS(fd, f)(epfd);
if (fd_empty(f))
return -EBADF;

/*
* We have to check that the file structure underneath the fd
* the user passed to us _is_ an eventpoll file.
*/
error = -EINVAL;
if (!is_file_epoll(fd_file(f)))
goto error_fput;
return -EINVAL;

/*
* At this point it is safe to assume that the "private_data" contains
Expand All @@ -2447,11 +2444,7 @@ static int do_epoll_wait(int epfd, struct epoll_event __user *events,
ep = fd_file(f)->private_data;

/* Time to fish for events ... */
error = ep_poll(ep, events, maxevents, to);

error_fput:
fdput(f);
return error;
return ep_poll(ep, events, maxevents, to);
}

SYSCALL_DEFINE4(epoll_wait, int, epfd, struct epoll_event __user *, events,
Expand Down
21 changes: 7 additions & 14 deletions fs/ext4/ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1330,7 +1330,6 @@ static long __ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)

case EXT4_IOC_MOVE_EXT: {
struct move_extent me;
struct fd donor;
int err;

if (!(filp->f_mode & FMODE_READ) ||
Expand All @@ -1342,30 +1341,26 @@ static long __ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
return -EFAULT;
me.moved_len = 0;

donor = fdget(me.donor_fd);
if (!fd_file(donor))
CLASS(fd, donor)(me.donor_fd);
if (fd_empty(donor))
return -EBADF;

if (!(fd_file(donor)->f_mode & FMODE_WRITE)) {
err = -EBADF;
goto mext_out;
}
if (!(fd_file(donor)->f_mode & FMODE_WRITE))
return -EBADF;

if (ext4_has_feature_bigalloc(sb)) {
ext4_msg(sb, KERN_ERR,
"Online defrag not supported with bigalloc");
err = -EOPNOTSUPP;
goto mext_out;
return -EOPNOTSUPP;
} else if (IS_DAX(inode)) {
ext4_msg(sb, KERN_ERR,
"Online defrag not supported with DAX");
err = -EOPNOTSUPP;
goto mext_out;
return -EOPNOTSUPP;
}

err = mnt_want_write_file(filp);
if (err)
goto mext_out;
return err;

err = ext4_move_extents(filp, fd_file(donor), me.orig_start,
me.donor_start, me.len, &me.moved_len);
Expand All @@ -1374,8 +1369,6 @@ static long __ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
if (copy_to_user((struct move_extent __user *)arg,
&me, sizeof(me)))
err = -EFAULT;
mext_out:
fdput(donor);
return err;
}

Expand Down
15 changes: 5 additions & 10 deletions fs/f2fs/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -3038,32 +3038,27 @@ static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
static int __f2fs_ioc_move_range(struct file *filp,
struct f2fs_move_range *range)
{
struct fd dst;
int err;

if (!(filp->f_mode & FMODE_READ) ||
!(filp->f_mode & FMODE_WRITE))
return -EBADF;

dst = fdget(range->dst_fd);
if (!fd_file(dst))
CLASS(fd, dst)(range->dst_fd);
if (fd_empty(dst))
return -EBADF;

if (!(fd_file(dst)->f_mode & FMODE_WRITE)) {
err = -EBADF;
goto err_out;
}
if (!(fd_file(dst)->f_mode & FMODE_WRITE))
return -EBADF;

err = mnt_want_write_file(filp);
if (err)
goto err_out;
return err;

err = f2fs_move_file_range(filp, range->pos_in, fd_file(dst),
range->pos_out, range->len);

mnt_drop_write_file(filp);
err_out:
fdput(dst);
return err;
}

Expand Down
19 changes: 6 additions & 13 deletions fs/fsopen.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,6 @@ SYSCALL_DEFINE5(fsconfig,
int, aux)
{
struct fs_context *fc;
struct fd f;
int ret;
int lookup_flags = 0;

Expand Down Expand Up @@ -392,12 +391,11 @@ SYSCALL_DEFINE5(fsconfig,
return -EOPNOTSUPP;
}

f = fdget(fd);
if (!fd_file(f))
CLASS(fd, f)(fd);
if (fd_empty(f))
return -EBADF;
ret = -EINVAL;
if (fd_file(f)->f_op != &fscontext_fops)
goto out_f;
return -EINVAL;

fc = fd_file(f)->private_data;
if (fc->ops == &legacy_fs_context_ops) {
Expand All @@ -407,17 +405,14 @@ SYSCALL_DEFINE5(fsconfig,
case FSCONFIG_SET_PATH_EMPTY:
case FSCONFIG_SET_FD:
case FSCONFIG_CMD_CREATE_EXCL:
ret = -EOPNOTSUPP;
goto out_f;
return -EOPNOTSUPP;
}
}

if (_key) {
param.key = strndup_user(_key, 256);
if (IS_ERR(param.key)) {
ret = PTR_ERR(param.key);
goto out_f;
}
if (IS_ERR(param.key))
return PTR_ERR(param.key);
}

switch (cmd) {
Expand Down Expand Up @@ -496,7 +491,5 @@ SYSCALL_DEFINE5(fsconfig,
}
out_key:
kfree(param.key);
out_f:
fdput(f);
return ret;
}
6 changes: 2 additions & 4 deletions fs/fuse/dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -2371,13 +2371,12 @@ static long fuse_dev_ioctl_clone(struct file *file, __u32 __user *argp)
int res;
int oldfd;
struct fuse_dev *fud = NULL;
struct fd f;

if (get_user(oldfd, argp))
return -EFAULT;

f = fdget(oldfd);
if (!fd_file(f))
CLASS(fd, f)(oldfd);
if (fd_empty(f))
return -EINVAL;

/*
Expand All @@ -2394,7 +2393,6 @@ static long fuse_dev_ioctl_clone(struct file *file, __u32 __user *argp)
mutex_unlock(&fuse_mutex);
}

fdput(f);
return res;
}

Expand Down
15 changes: 5 additions & 10 deletions fs/locks.c
Original file line number Diff line number Diff line change
Expand Up @@ -2136,7 +2136,6 @@ SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
{
int can_sleep, error, type;
struct file_lock fl;
struct fd f;

/*
* LOCK_MAND locks were broken for a long time in that they never
Expand All @@ -2155,19 +2154,18 @@ SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
if (type < 0)
return type;

error = -EBADF;
f = fdget(fd);
if (!fd_file(f))
return error;
CLASS(fd, f)(fd);
if (fd_empty(f))
return -EBADF;

if (type != F_UNLCK && !(fd_file(f)->f_mode & (FMODE_READ | FMODE_WRITE)))
goto out_putf;
return -EBADF;

flock_make_lock(fd_file(f), &fl, type);

error = security_file_lock(fd_file(f), fl.c.flc_type);
if (error)
goto out_putf;
return error;

can_sleep = !(cmd & LOCK_NB);
if (can_sleep)
Expand All @@ -2181,9 +2179,6 @@ SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
error = locks_lock_file_wait(fd_file(f), &fl);

locks_release_private(&fl);
out_putf:
fdput(f);

return error;
}

Expand Down
Loading

0 comments on commit 8152f82

Please sign in to comment.