Commit 0478adff authored by David Sterba's avatar David Sterba

btrfs: factor out validation of btrfs_ioctl_vol_args_v2::name

The validation of vol args v2 name in snapshot and device remove ioctls
is not done properly. A terminating NUL is written to the end of the
buffer unconditionally, assuming that this would be the last place in
case the buffer is used completely. This does not communicate back the
actual error (either an invalid or too long path).

Factor out all such cases and use a helper to do the verification,
simply look for NUL in the buffer.  There's no expected practical
change, the size of buffer is 4088, this is enough for most paths or
names.
Reviewed-by: default avatarBoris Burkov <boris@bur.io>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 5ab2b180
...@@ -234,6 +234,13 @@ int btrfs_check_ioctl_vol_args_path(const struct btrfs_ioctl_vol_args *vol_args) ...@@ -234,6 +234,13 @@ int btrfs_check_ioctl_vol_args_path(const struct btrfs_ioctl_vol_args *vol_args)
return 0; return 0;
} }
static int btrfs_check_ioctl_vol_args2_subvol_name(const struct btrfs_ioctl_vol_args_v2 *vol_args2)
{
if (memchr(vol_args2->name, 0, sizeof(vol_args2->name)) == NULL)
return -ENAMETOOLONG;
return 0;
}
/* /*
* Set flags/xflags from the internal inode flags. The remaining items of * Set flags/xflags from the internal inode flags. The remaining items of
* fsxattr are zeroed. * fsxattr are zeroed.
...@@ -1363,7 +1370,9 @@ static noinline int btrfs_ioctl_snap_create_v2(struct file *file, ...@@ -1363,7 +1370,9 @@ static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
vol_args = memdup_user(arg, sizeof(*vol_args)); vol_args = memdup_user(arg, sizeof(*vol_args));
if (IS_ERR(vol_args)) if (IS_ERR(vol_args))
return PTR_ERR(vol_args); return PTR_ERR(vol_args);
vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; ret = btrfs_check_ioctl_vol_args2_subvol_name(vol_args);
if (ret < 0)
goto free_args;
if (vol_args->flags & ~BTRFS_SUBVOL_CREATE_ARGS_MASK) { if (vol_args->flags & ~BTRFS_SUBVOL_CREATE_ARGS_MASK) {
ret = -EOPNOTSUPP; ret = -EOPNOTSUPP;
...@@ -2393,7 +2402,9 @@ static noinline int btrfs_ioctl_snap_destroy(struct file *file, ...@@ -2393,7 +2402,9 @@ static noinline int btrfs_ioctl_snap_destroy(struct file *file,
* name, same as v1 currently does. * name, same as v1 currently does.
*/ */
if (!(vol_args2->flags & BTRFS_SUBVOL_SPEC_BY_ID)) { if (!(vol_args2->flags & BTRFS_SUBVOL_SPEC_BY_ID)) {
vol_args2->name[BTRFS_SUBVOL_NAME_MAX] = 0; err = btrfs_check_ioctl_vol_args2_subvol_name(vol_args2);
if (err < 0)
goto out;
subvol_name = vol_args2->name; subvol_name = vol_args2->name;
err = mnt_want_write_file(file); err = mnt_want_write_file(file);
...@@ -2732,7 +2743,10 @@ static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg) ...@@ -2732,7 +2743,10 @@ static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
goto out; goto out;
} }
vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; ret = btrfs_check_ioctl_vol_args2_subvol_name(vol_args);
if (ret < 0)
goto out;
if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) { if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
args.devid = vol_args->devid; args.devid = vol_args->devid;
} else if (!strcmp("cancel", vol_args->name)) { } else if (!strcmp("cancel", vol_args->name)) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment