Skip to content

Commit 1c1c874

Browse files
author
Al Viro
committed
btrfs: sanitize BTRFS_IOC_FILE_EXTENT_SAME
* don't assume that ->dest_count won't change between copy_from_user() and memdup_user() * use fdget instead of fget * don't bother comparing superblocks when we'd already compared vfsmounts * get rid of excessive goto * use file_inode() instead of open-coding the sucker Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 208adb6 commit 1c1c874

1 file changed

Lines changed: 24 additions & 46 deletions

File tree

‎fs/btrfs/ioctl.c‎

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2686,21 +2686,19 @@ static int btrfs_extent_same(struct inode *src, u64 loff, u64 len,
26862686
#define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024)
26872687

26882688
static long btrfs_ioctl_file_extent_same(struct file *file,
2689-
void __user *argp)
2689+
struct btrfs_ioctl_same_args __user *argp)
26902690
{
2691-
struct btrfs_ioctl_same_args tmp;
26922691
struct btrfs_ioctl_same_args *same;
26932692
struct btrfs_ioctl_same_extent_info *info;
2694-
struct inode *src = file->f_dentry->d_inode;
2695-
struct file *dst_file = NULL;
2696-
struct inode *dst;
2693+
struct inode *src = file_inode(file);
26972694
u64 off;
26982695
u64 len;
26992696
int i;
27002697
int ret;
27012698
unsigned long size;
27022699
u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
27032700
bool is_admin = capable(CAP_SYS_ADMIN);
2701+
u16 count;
27042702

27052703
if (!(file->f_mode & FMODE_READ))
27062704
return -EINVAL;
@@ -2709,17 +2707,14 @@ static long btrfs_ioctl_file_extent_same(struct file *file,
27092707
if (ret)
27102708
return ret;
27112709

2712-
if (copy_from_user(&tmp,
2713-
(struct btrfs_ioctl_same_args __user *)argp,
2714-
sizeof(tmp))) {
2710+
if (get_user(count, &argp->dest_count)) {
27152711
ret = -EFAULT;
27162712
goto out;
27172713
}
27182714

2719-
size = sizeof(tmp) +
2720-
tmp.dest_count * sizeof(struct btrfs_ioctl_same_extent_info);
2715+
size = offsetof(struct btrfs_ioctl_same_args __user, info[count]);
27212716

2722-
same = memdup_user((struct btrfs_ioctl_same_args __user *)argp, size);
2717+
same = memdup_user(argp, size);
27232718

27242719
if (IS_ERR(same)) {
27252720
ret = PTR_ERR(same);
@@ -2756,52 +2751,35 @@ static long btrfs_ioctl_file_extent_same(struct file *file,
27562751
goto out;
27572752

27582753
/* pre-format output fields to sane values */
2759-
for (i = 0; i < same->dest_count; i++) {
2754+
for (i = 0; i < count; i++) {
27602755
same->info[i].bytes_deduped = 0ULL;
27612756
same->info[i].status = 0;
27622757
}
27632758

2764-
ret = 0;
2765-
for (i = 0; i < same->dest_count; i++) {
2766-
info = &same->info[i];
2767-
2768-
dst_file = fget(info->fd);
2769-
if (!dst_file) {
2759+
for (i = 0, info = same->info; i < count; i++, info++) {
2760+
struct inode *dst;
2761+
struct fd dst_file = fdget(info->fd);
2762+
if (!dst_file.file) {
27702763
info->status = -EBADF;
2771-
goto next;
2764+
continue;
27722765
}
2766+
dst = file_inode(dst_file.file);
27732767

2774-
if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
2768+
if (!(is_admin || (dst_file.file->f_mode & FMODE_WRITE))) {
27752769
info->status = -EINVAL;
2776-
goto next;
2777-
}
2778-
2779-
info->status = -EXDEV;
2780-
if (file->f_path.mnt != dst_file->f_path.mnt)
2781-
goto next;
2782-
2783-
dst = dst_file->f_dentry->d_inode;
2784-
if (src->i_sb != dst->i_sb)
2785-
goto next;
2786-
2787-
if (S_ISDIR(dst->i_mode)) {
2770+
} else if (file->f_path.mnt != dst_file.file->f_path.mnt) {
2771+
info->status = -EXDEV;
2772+
} else if (S_ISDIR(dst->i_mode)) {
27882773
info->status = -EISDIR;
2789-
goto next;
2790-
}
2791-
2792-
if (!S_ISREG(dst->i_mode)) {
2774+
} else if (!S_ISREG(dst->i_mode)) {
27932775
info->status = -EACCES;
2794-
goto next;
2776+
} else {
2777+
info->status = btrfs_extent_same(src, off, len, dst,
2778+
info->logical_offset);
2779+
if (info->status == 0)
2780+
info->bytes_deduped += len;
27952781
}
2796-
2797-
info->status = btrfs_extent_same(src, off, len, dst,
2798-
info->logical_offset);
2799-
if (info->status == 0)
2800-
info->bytes_deduped += len;
2801-
2802-
next:
2803-
if (dst_file)
2804-
fput(dst_file);
2782+
fdput(dst_file);
28052783
}
28062784

28072785
ret = copy_to_user(argp, same, size);

0 commit comments

Comments
 (0)