diff options
author | Kevin Wolf <kwolf@redhat.com> | 2013-05-14 16:14:33 +0200 |
---|---|---|
committer | Stefan Hajnoczi <stefanha@redhat.com> | 2013-05-14 16:44:33 +0200 |
commit | 2cf7cfa1cde6672b8a35bbed3fbc989f28c05dce (patch) | |
tree | 221828f1201c58fc23331366214972171a64fa0d /block/qcow2.c | |
parent | b087143b4d010451208264b7c841436aafe1cbb1 (diff) | |
download | qemu-2cf7cfa1cde6672b8a35bbed3fbc989f28c05dce.tar.gz qemu-2cf7cfa1cde6672b8a35bbed3fbc989f28c05dce.tar.bz2 qemu-2cf7cfa1cde6672b8a35bbed3fbc989f28c05dce.zip |
qcow2: Catch some L1 table index overflows
This catches the situation that is described in the bug report at
https://bugs.launchpad.net/qemu/+bug/865518 and goes like this:
$ qemu-img create -f qcow2 huge.qcow2 $((1024*1024))T
Formatting 'huge.qcow2', fmt=qcow2 size=1152921504606846976 encryption=off cluster_size=65536 lazy_refcounts=off
$ qemu-io /tmp/huge.qcow2 -c "write $((1024*1024*1024*1024*1024*1024 - 1024)) 512"
Segmentation fault
With this patch applied the segfault will be avoided, however the case
will still fail, though gracefully:
$ qemu-img create -f qcow2 /tmp/huge.qcow2 $((1024*1024))T
Formatting 'huge.qcow2', fmt=qcow2 size=1152921504606846976 encryption=off cluster_size=65536 lazy_refcounts=off
qemu-img: The image size is too large for file format 'qcow2'
Note that even long before these overflow checks kick in, you get
insanely high memory usage (up to INT_MAX * sizeof(uint64_t) = 16 GB for
the L1 table), so with somewhat smaller image sizes you'll probably see
qemu aborting for a failed g_malloc().
If you need huge image sizes, you should increase the cluster size to
the maximum of 2 MB in order to get higher limits.
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Diffstat (limited to 'block/qcow2.c')
-rw-r--r-- | block/qcow2.c | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/block/qcow2.c b/block/qcow2.c index 2e346d8c42..0fa5cb29ae 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -307,6 +307,7 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags) QemuOpts *opts; Error *local_err = NULL; uint64_t ext_end; + uint64_t l1_vm_state_index; ret = bdrv_pread(bs->file, 0, &header, sizeof(header)); if (ret < 0) { @@ -424,7 +425,14 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags) /* read the level 1 table */ s->l1_size = header.l1_size; - s->l1_vm_state_index = size_to_l1(s, header.size); + + l1_vm_state_index = size_to_l1(s, header.size); + if (l1_vm_state_index > INT_MAX) { + ret = -EFBIG; + goto fail; + } + s->l1_vm_state_index = l1_vm_state_index; + /* the L1 table must contain at least enough entries to put header.size bytes */ if (s->l1_size < s->l1_vm_state_index) { @@ -1480,7 +1488,8 @@ static coroutine_fn int qcow2_co_discard(BlockDriverState *bs, static int qcow2_truncate(BlockDriverState *bs, int64_t offset) { BDRVQcowState *s = bs->opaque; - int ret, new_l1_size; + int64_t new_l1_size; + int ret; if (offset & 511) { error_report("The new size must be a multiple of 512"); |