Skip to contentSkip to conversation

qcow2: Fixes for invalid images

PATCH In review Max Reitz 2 participants 9 messages Nov 17, 2013 – Nov 19, 2013 Block layer

lore
Share & tools
lore

Max Reitz

author
#0 lore raw

cover qcow2: Fixes for invalid images

This series contains patches for broken images.

Max Reitz (4):
qcow2: Move reading nb_snapshots in qcow2_open
qcow2-refcount: Sanitize refcount table size
qcow2: Sanitize refcount table size
qcow2: Check validity of backing file name length

 block/qcow2-refcount.c |  4 ++++
 block/qcow2.c          | 17 +++++++++++++++--
 2 files changed, 19 insertions(+), 2 deletions(-)
Signature · 1 line
1.8.4.2
author
#1 lore raw

PATCH 1/4 qcow2: Move reading nb_snapshots in qcow2_open

Any goto fail between having read nb_snapshots (returning a non-zero value) and allocating s->snapshots (through qcow2_read_snapshots()) results in qcow2_free_snapshots() being called, dereferencing s->snapshots which is still NULL.

Fix this by moving the reading of nb_snapshots right before the call to qcow2_read_snapshots().

---
 block/qcow2.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
block/qcow2.c +3 -3
index 6e5d98d..3e612a8 100644
@@ -558,9 +558,6 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
     s->refcount_table_size =
         header.refcount_table_clusters << (s->cluster_bits - 3);
 
-    s->snapshots_offset = header.snapshots_offset;
-    s->nb_snapshots = header.nb_snapshots;
-
     /* read the level 1 table */
     s->l1_size = header.l1_size;
 
@@ -637,6 +634,9 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
         bs->backing_file[len] = '\0';
     }
 
+    s->snapshots_offset = header.snapshots_offset;
+    s->nb_snapshots = header.nb_snapshots;
+
     ret = qcow2_read_snapshots(bs);
     if (ret < 0) {
         error_setg_errno(errp, -ret, "Could not read snapshots");
Signature · 1 line
1.8.4.2
author
#3 lore raw

PATCH 2/4 qcow2-refcount: Sanitize refcount table size

In reply to Max Reitz

Make sure the refcount table size will not overflow when multiplied by sizeof(uint64_t) and implicitly casted to int.

---
 block/qcow2-refcount.c | 4 ++++
 1 file changed, 4 insertions(+)
block/qcow2-refcount.c +4 -0
index 1ff43d0..2912097 100644
@@ -42,6 +42,10 @@ int qcow2_refcount_init(BlockDriverState *bs)
     BDRVQcowState *s = bs->opaque;
     int ret, refcount_table_size2, i;
 
+    if (s->refcount_table_size >= INT_MAX / sizeof(uint64_t)) {
+        goto fail;
+    }
+
     refcount_table_size2 = s->refcount_table_size * sizeof(uint64_t);
     s->refcount_table = g_malloc(refcount_table_size2);
     if (s->refcount_table_size > 0) {
Signature · 1 line
1.8.4.2
author
#5 lore raw

PATCH 3/4 qcow2: Sanitize refcount table size

In reply to Max Reitz

Make sure there were no overflows when calculating the in-memory refcount table size from the number of its clusters in-file.

---
 block/qcow2.c | 8 ++++++++
 1 file changed, 8 insertions(+)
block/qcow2.c +8 -0
index 3e612a8..9c29e1a 100644
@@ -558,6 +558,14 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
     s->refcount_table_size =
         header.refcount_table_clusters << (s->cluster_bits - 3);
 
+    if ((s->refcount_table_size >> (s->cluster_bits - 3)) !=
+        header.refcount_table_clusters)
+    {
+        error_setg(errp, "Refcount table is too big");
+        ret = -EINVAL;
+        goto fail;
+    }
+
     /* read the level 1 table */
     s->l1_size = header.l1_size;
 
Signature · 1 line
1.8.4.2
author
#7 lore raw

PATCH 4/4 qcow2: Check validity of backing file name length

In reply to Max Reitz

The len variable is a signed integer, therefore it may overflow when reading the backing file name length from the qcow2 image header. This case should be handled explicitly.

---
 block/qcow2.c | 5 +++++
 1 file changed, 5 insertions(+)
block/qcow2.c +5 -0
index 9c29e1a..e54176e 100644
@@ -630,6 +630,11 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
     /* read the backing file name */
     if (header.backing_file_offset != 0) {
         len = header.backing_file_size;
+        if (len < 0) {
+            error_setg(errp, "Backing file name length is negative");
+            ret = -EINVAL;
+            goto fail;
+        }
         if (len > 1023) {
             len = 1023;
         }
Signature · 1 line
1.8.4.2
Reviewed-by on v1 1/4
#2 lore raw

Re: [Qemu-devel] [PATCH 1/4] qcow2: Move reading nb_snapshots in qcow2_open

In reply to Max Reitz
Quoted 5 lines · Max Reitz

Any goto fail between having read nb_snapshots (returning a non-zero value) and allocating s->snapshots (through qcow2_read_snapshots()) results in qcow2_free_snapshots() being called, dereferencing s->snapshots which is still NULL.

Quoted 7 lines

Fix this by moving the reading of nb_snapshots right before the call to qcow2_read_snapshots().

---
 block/qcow2.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Are you intending this series as a bug fix for 1.7?

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Reviewed-by on v1 2/4
#4 lore raw

Re: [Qemu-devel] [PATCH 2/4] qcow2-refcount: Sanitize refcount table size

In reply to Max Reitz
Quoted 2 lines · Max Reitz

Make sure the refcount table size will not overflow when multiplied by sizeof(uint64_t) and implicitly casted to int.

s/casted/cast/

Quoted 5 lines
---
 block/qcow2-refcount.c | 4 ++++
 1 file changed, 4 insertions(+)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Reviewed-by on v1 3/4
#6 lore raw

Re: [Qemu-devel] [PATCH 3/4] qcow2: Sanitize refcount table size

In reply to Max Reitz
Quoted 7 lines · Max Reitz

Make sure there were no overflows when calculating the in-memory refcount table size from the number of its clusters in-file.

---
 block/qcow2.c | 8 ++++++++
 1 file changed, 8 insertions(+)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Reviewed-by on v1 4/4
#8 lore raw

Re: [Qemu-devel] [PATCH 4/4] qcow2: Check validity of backing file name length

In reply to Max Reitz
Quoted 8 lines · Max Reitz

The len variable is a signed integer, therefore it may overflow when reading the backing file name length from the qcow2 image header. This case should be handled explicitly.

---
 block/qcow2.c | 5 +++++
 1 file changed, 5 insertions(+)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org