Skip to content

Commit 5011c5a

Browse files
Dan Carpenterkdave
authored andcommitted
btrfs: validate qgroup inherit for SNAP_CREATE_V2 ioctl
The problem is we're copying "inherit" from user space but we don't necessarily know that we're copying enough data for a 64 byte struct. Then the next problem is that 'inherit' has a variable size array at the end, and we have to verify that array is the size we expected. Fixes: 6f72c7e ("Btrfs: add qgroup inheritance") CC: stable@vger.kernel.org # 4.4+ Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com> Reviewed-by: David Sterba <dsterba@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 4f6a49d commit 5011c5a

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

fs/btrfs/ioctl.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1935,7 +1935,10 @@ static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
19351935
if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
19361936
readonly = true;
19371937
if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1938-
if (vol_args->size > PAGE_SIZE) {
1938+
u64 nums;
1939+
1940+
if (vol_args->size < sizeof(*inherit) ||
1941+
vol_args->size > PAGE_SIZE) {
19391942
ret = -EINVAL;
19401943
goto free_args;
19411944
}
@@ -1944,6 +1947,20 @@ static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
19441947
ret = PTR_ERR(inherit);
19451948
goto free_args;
19461949
}
1950+
1951+
if (inherit->num_qgroups > PAGE_SIZE ||
1952+
inherit->num_ref_copies > PAGE_SIZE ||
1953+
inherit->num_excl_copies > PAGE_SIZE) {
1954+
ret = -EINVAL;
1955+
goto free_inherit;
1956+
}
1957+
1958+
nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
1959+
2 * inherit->num_excl_copies;
1960+
if (vol_args->size != struct_size(inherit, qgroups, nums)) {
1961+
ret = -EINVAL;
1962+
goto free_inherit;
1963+
}
19471964
}
19481965

19491966
ret = __btrfs_ioctl_snap_create(file, vol_args->name, vol_args->fd,

0 commit comments

Comments
 (0)