[Qemu-devel] [PATCH v2 34/36] block: Fail bdrv_truncate() with negative size

Kevin Wolf posted 36 patches 7 years, 8 months ago
There is a newer version of this series
[Qemu-devel] [PATCH v2 34/36] block: Fail bdrv_truncate() with negative size
Posted by Kevin Wolf 7 years, 8 months ago
Most callers have their own checks, but something like this should also
be checked centrally. As it happens, x-blockdev-create can pass negative
image sizes to format drivers (because there is no QAPI type that would
reject negative numbers) and triggers the check added by this patch.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Max Reitz <mreitz@redhat.com>
---
 block.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/block.c b/block.c
index 4a7e448226..5c874aefa1 100644
--- a/block.c
+++ b/block.c
@@ -3684,6 +3684,11 @@ int bdrv_truncate(BdrvChild *child, int64_t offset, PreallocMode prealloc,
         error_setg(errp, "No medium inserted");
         return -ENOMEDIUM;
     }
+    if (offset < 0) {
+        error_setg(errp, "Image size cannot be negative");
+        return -EINVAL;
+    }
+
     if (!drv->bdrv_truncate) {
         if (bs->file && drv->is_filter) {
             return bdrv_truncate(bs->file, offset, prealloc, errp);
-- 
2.13.6


Re: [Qemu-devel] [PATCH v2 34/36] block: Fail bdrv_truncate() with negative size
Posted by Eric Blake 7 years, 8 months ago
On 02/21/2018 07:54 AM, Kevin Wolf wrote:
> Most callers have their own checks, but something like this should also
> be checked centrally. As it happens, x-blockdev-create can pass negative
> image sizes to format drivers (because there is no QAPI type that would
> reject negative numbers) and triggers the check added by this patch.
> 
> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
> Reviewed-by: Max Reitz <mreitz@redhat.com>
> ---
>   block.c | 5 +++++
>   1 file changed, 5 insertions(+)

Are there any callers that can be simplified (such as now assert()ing 
rather than checking and returning an error) due to better guarantees 
about a non-negative argument?

> 
> diff --git a/block.c b/block.c
> index 4a7e448226..5c874aefa1 100644
> --- a/block.c
> +++ b/block.c
> @@ -3684,6 +3684,11 @@ int bdrv_truncate(BdrvChild *child, int64_t offset, PreallocMode prealloc,
>           error_setg(errp, "No medium inserted");
>           return -ENOMEDIUM;
>       }
> +    if (offset < 0) {
> +        error_setg(errp, "Image size cannot be negative");
> +        return -EINVAL;
> +    }
> +
>       if (!drv->bdrv_truncate) {
>           if (bs->file && drv->is_filter) {
>               return bdrv_truncate(bs->file, offset, prealloc, errp);
> 

Reviewed-by: Eric Blake <eblake@redhat.com>

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org