mirror of
https://github.com/edk2-porting/linux-next.git
synced 2024-11-23 18:14:04 +08:00
btrfs: factor out validation of btrfs_ioctl_vol_args::name
The validation of vol args name in several 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: Boris Burkov <boris@bur.io> Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
parent
f33163ee4c
commit
5ab2b18088
@ -973,6 +973,8 @@ void btrfs_exclop_finish(struct btrfs_fs_info *fs_info);
|
|||||||
void btrfs_exclop_balance(struct btrfs_fs_info *fs_info,
|
void btrfs_exclop_balance(struct btrfs_fs_info *fs_info,
|
||||||
enum btrfs_exclusive_operation op);
|
enum btrfs_exclusive_operation op);
|
||||||
|
|
||||||
|
int btrfs_check_ioctl_vol_args_path(const struct btrfs_ioctl_vol_args *vol_args);
|
||||||
|
|
||||||
/* Compatibility and incompatibility defines */
|
/* Compatibility and incompatibility defines */
|
||||||
void __btrfs_set_fs_incompat(struct btrfs_fs_info *fs_info, u64 flag,
|
void __btrfs_set_fs_incompat(struct btrfs_fs_info *fs_info, u64 flag,
|
||||||
const char *name);
|
const char *name);
|
||||||
|
@ -227,6 +227,13 @@ static int check_fsflags_compatible(struct btrfs_fs_info *fs_info,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int btrfs_check_ioctl_vol_args_path(const struct btrfs_ioctl_vol_args *vol_args)
|
||||||
|
{
|
||||||
|
if (memchr(vol_args->name, 0, sizeof(vol_args->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.
|
||||||
@ -1126,7 +1133,10 @@ static noinline int btrfs_ioctl_resize(struct file *file,
|
|||||||
ret = PTR_ERR(vol_args);
|
ret = PTR_ERR(vol_args);
|
||||||
goto out_drop;
|
goto out_drop;
|
||||||
}
|
}
|
||||||
vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
|
ret = btrfs_check_ioctl_vol_args_path(vol_args);
|
||||||
|
if (ret < 0)
|
||||||
|
goto out_free;
|
||||||
|
|
||||||
sizestr = vol_args->name;
|
sizestr = vol_args->name;
|
||||||
cancel = (strcmp("cancel", sizestr) == 0);
|
cancel = (strcmp("cancel", sizestr) == 0);
|
||||||
ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_RESIZE, cancel);
|
ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_RESIZE, cancel);
|
||||||
@ -1326,12 +1336,15 @@ static noinline int btrfs_ioctl_snap_create(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_PATH_NAME_MAX] = '\0';
|
ret = btrfs_check_ioctl_vol_args_path(vol_args);
|
||||||
|
if (ret < 0)
|
||||||
|
goto out;
|
||||||
|
|
||||||
ret = __btrfs_ioctl_snap_create(file, file_mnt_idmap(file),
|
ret = __btrfs_ioctl_snap_create(file, file_mnt_idmap(file),
|
||||||
vol_args->name, vol_args->fd, subvol,
|
vol_args->name, vol_args->fd, subvol,
|
||||||
false, NULL);
|
false, NULL);
|
||||||
|
|
||||||
|
out:
|
||||||
kfree(vol_args);
|
kfree(vol_args);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -2464,7 +2477,10 @@ static noinline int btrfs_ioctl_snap_destroy(struct file *file,
|
|||||||
if (IS_ERR(vol_args))
|
if (IS_ERR(vol_args))
|
||||||
return PTR_ERR(vol_args);
|
return PTR_ERR(vol_args);
|
||||||
|
|
||||||
vol_args->name[BTRFS_PATH_NAME_MAX] = 0;
|
err = btrfs_check_ioctl_vol_args_path(vol_args);
|
||||||
|
if (err < 0)
|
||||||
|
goto out;
|
||||||
|
|
||||||
subvol_name = vol_args->name;
|
subvol_name = vol_args->name;
|
||||||
|
|
||||||
err = mnt_want_write_file(file);
|
err = mnt_want_write_file(file);
|
||||||
@ -2675,12 +2691,16 @@ static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
|
|||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
|
ret = btrfs_check_ioctl_vol_args_path(vol_args);
|
||||||
|
if (ret < 0)
|
||||||
|
goto out_free;
|
||||||
|
|
||||||
ret = btrfs_init_new_device(fs_info, vol_args->name);
|
ret = btrfs_init_new_device(fs_info, vol_args->name);
|
||||||
|
|
||||||
if (!ret)
|
if (!ret)
|
||||||
btrfs_info(fs_info, "disk added %s", vol_args->name);
|
btrfs_info(fs_info, "disk added %s", vol_args->name);
|
||||||
|
|
||||||
|
out_free:
|
||||||
kfree(vol_args);
|
kfree(vol_args);
|
||||||
out:
|
out:
|
||||||
if (restore_op)
|
if (restore_op)
|
||||||
@ -2772,7 +2792,10 @@ static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
|
|||||||
if (IS_ERR(vol_args))
|
if (IS_ERR(vol_args))
|
||||||
return PTR_ERR(vol_args);
|
return PTR_ERR(vol_args);
|
||||||
|
|
||||||
vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
|
ret = btrfs_check_ioctl_vol_args_path(vol_args);
|
||||||
|
if (ret < 0)
|
||||||
|
goto out_free;
|
||||||
|
|
||||||
if (!strcmp("cancel", vol_args->name)) {
|
if (!strcmp("cancel", vol_args->name)) {
|
||||||
cancel = true;
|
cancel = true;
|
||||||
} else {
|
} else {
|
||||||
@ -2799,6 +2822,7 @@ static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
|
|||||||
bdev_release(bdev_handle);
|
bdev_release(bdev_handle);
|
||||||
out:
|
out:
|
||||||
btrfs_put_dev_args_from_path(&args);
|
btrfs_put_dev_args_from_path(&args);
|
||||||
|
out_free:
|
||||||
kfree(vol_args);
|
kfree(vol_args);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -2201,7 +2201,9 @@ static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
|
|||||||
vol = memdup_user((void __user *)arg, sizeof(*vol));
|
vol = memdup_user((void __user *)arg, sizeof(*vol));
|
||||||
if (IS_ERR(vol))
|
if (IS_ERR(vol))
|
||||||
return PTR_ERR(vol);
|
return PTR_ERR(vol);
|
||||||
vol->name[BTRFS_PATH_NAME_MAX] = '\0';
|
ret = btrfs_check_ioctl_vol_args_path(vol);
|
||||||
|
if (ret < 0)
|
||||||
|
goto out;
|
||||||
|
|
||||||
switch (cmd) {
|
switch (cmd) {
|
||||||
case BTRFS_IOC_SCAN_DEV:
|
case BTRFS_IOC_SCAN_DEV:
|
||||||
@ -2243,6 +2245,7 @@ static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
out:
|
||||||
kfree(vol);
|
kfree(vol);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user