qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: Alberto Garcia <berto@igalia.com>, qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
	qemu-block@nongnu.org, Max Reitz <mreitz@redhat.com>
Subject: Re: [PATCH] qcow2: Avoid integer wraparound in qcow2_co_truncate()
Date: Fri, 1 May 2020 12:12:42 -0500	[thread overview]
Message-ID: <5ba91898-9d3b-d55d-c360-83cca41d88f4@redhat.com> (raw)
In-Reply-To: <20200501131525.6745-1-berto@igalia.com>

On 5/1/20 8:15 AM, Alberto Garcia wrote:
> After commit f01643fb8b47e8a70c04bbf45e0f12a9e5bc54de when an image is
> extended and BDRV_REQ_ZERO_WRITE is set then the new clusters are
> zeroized.
> 
> The code however does not detect correctly situations when the old and
> the new end of the image are within the same cluster. The problem can
> be reproduced with these steps:
> 
>     qemu-img create -f qcow2 backing.qcow2 1M
>     qemu-img create -f qcow2 -b backing.qcow2 top.qcow2

We should get in the habit of documenting -F qcow2 (I have a series, 
still awaiting review, that would warn if you don't).

>     qemu-img resize --shrink top.qcow2 520k
>     qemu-img resize top.qcow2 567k
> 
> In the last step offset - zero_start causes an integer wraparound.
> 
> Signed-off-by: Alberto Garcia <berto@igalia.com>
> ---
>   block/qcow2.c | 3 +++
>   1 file changed, 3 insertions(+)
> 
> diff --git a/block/qcow2.c b/block/qcow2.c
> index 2ba0b17c39..6d34d28c60 100644
> --- a/block/qcow2.c
> +++ b/block/qcow2.c
> @@ -4234,6 +4234,9 @@ static int coroutine_fn qcow2_co_truncate(BlockDriverState *bs, int64_t offset,
>       if ((flags & BDRV_REQ_ZERO_WRITE) && offset > old_length) {
>           uint64_t zero_start = QEMU_ALIGN_UP(old_length, s->cluster_size);
>   
> +        /* zero_start should not be after the new end of the image */
> +        zero_start = MIN(zero_start, offset);
> +

So, using your numbers, pre-patch, we have zero_start = 0x90000 (0x82000 
rounded up to 0x10000 alignment).  post-patch, the new MIN() lowers it 
back to 0x8dc00 (the new size), which is unaligned.

>           /*
>            * Use zero clusters as much as we can. qcow2_cluster_zeroize()
>            * requires a cluster-aligned start. The end may be unaligned if it is
          * at the end of the image (which it is here).
          */
         ret = qcow2_cluster_zeroize(bs, zero_start, offset - 
zero_start, 0);

pre-patch, it called zeroize(, 0x90000, 0xffffffffffffdc00, )
post-patch, it calls zeroize(, 0x8dc00, 0, )

Looking at qcow2_cluster_zeroize, we have:
     assert(QEMU_IS_ALIGNED(offset, s->cluster_size));

which will now trigger.  This patch is a good idea, but needs a v2.

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



  reply	other threads:[~2020-05-01 17:13 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-01 13:15 [PATCH] qcow2: Avoid integer wraparound in qcow2_co_truncate() Alberto Garcia
2020-05-01 17:12 ` Eric Blake [this message]
2020-05-01 18:48   ` Eric Blake
2020-05-04 13:47     ` Alberto Garcia
2020-05-04 14:51       ` Eric Blake

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5ba91898-9d3b-d55d-c360-83cca41d88f4@redhat.com \
    --to=eblake@redhat.com \
    --cc=berto@igalia.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).