Skip to contentSkip to conversation

qcow2 corruption: Fix alloc_cluster_link_l2

PATCH Applied Kevin Wolf 7 participants 8 messages Apr 15, 2009 – Apr 26, 2009

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

PATCH 1/1 qcow2 corruption: Fix alloc_cluster_link_l2

This patch fixes a qcow2 corruption bug introduced in SVN Rev 5861. L2 tables are big endian, so entries must be converted before being passed to functions.

This bug is easy to trigger. The following script will create and destroy a qcow2 image (the header is gone after three loop iterations):

    #!/bin/bash
    qemu-img create -f qcow2 test.qcow 1M
    for i in $(seq 1 10); do
    qemu-system-x86_64 -hda test.qcow -monitor stdio > /dev/null 2>&1 <<EOF
    savevm test-$i
    quit
    EOF
    done
---
 block-qcow2.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
block-qcow2.c +1 -1
index da8fb42..985214f 100644
@@ -1007,7 +1007,7 @@ static int alloc_cluster_link_l2(BlockDriverState *bs, uint64_t cluster_offset,
         goto err;
 
     for (i = 0; i < j; i++)
-        free_any_clusters(bs, old_cluster[i], 1);
+        free_any_clusters(bs, be64_to_cpu(old_cluster[i]), 1);
 
     ret = 0;
 err:
Signature · 1 line
1.6.0.6
Acked-by on v1 1/1
#1 lore raw
Quoted 15 lines · Kevin Wolf

This patch fixes a qcow2 corruption bug introduced in SVN Rev 5861. L2 tables are big endian, so entries must be converted before being passed to functions.

This bug is easy to trigger. The following script will create and destroy a qcow2 image (the header is gone after three loop iterations):

    #!/bin/bash
    qemu-img create -f qcow2 test.qcow 1M
    for i in $(seq 1 10); do
    qemu-system-x86_64 -hda test.qcow -monitor stdio > /dev/null 2>&1 <<EOF
    savevm test-$i
    quit
    EOF
    done
Quoted 1 line
Quoted 19 lines
---
 block-qcow2.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
block-qcow2.c +1 -1
index da8fb42..985214f 100644
@@ -1007,7 +1007,7 @@ static int alloc_cluster_link_l2(BlockDriverState *bs, uint64_t cluster_offset,
         goto err;
 
     for (i = 0; i < j; i++)
-        free_any_clusters(bs, old_cluster[i], 1);
+        free_any_clusters(bs, be64_to_cpu(old_cluster[i]), 1);
 
     ret = 0;
 err:
Signature · 1 line
1.6.0.6

--
Gleb.

Acked-by on v1 1/1
#2 lore raw
In reply to Kevin Wolf
Quoted 3 lines · Kevin Wolf

This patch fixes a qcow2 corruption bug introduced in SVN Rev 5861. L2 tables are big endian, so entries must be converted before being passed to functions.

Excellent! I think it is this bug who bit me more than once.

Quoted 30 lines

This bug is easy to trigger. The following script will create and destroy a qcow2 image (the header is gone after three loop iterations):

    #!/bin/bash
    qemu-img create -f qcow2 test.qcow 1M
    for i in $(seq 1 10); do
    qemu-system-x86_64 -hda test.qcow -monitor stdio > /dev/null 2>&1 <<EOF
    savevm test-$i
    quit
    EOF
    done
---
 block-qcow2.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
block-qcow2.c +1 -1
index da8fb42..985214f 100644
@@ -1007,7 +1007,7 @@ static int alloc_cluster_link_l2(BlockDriverState *bs, uint64_t cluster_offset,
         goto err;
 
     for (i = 0; i < j; i++)
-        free_any_clusters(bs, old_cluster[i], 1);
+        free_any_clusters(bs, be64_to_cpu(old_cluster[i]), 1);
 
     ret = 0;
 err:
#3 lore raw
Quoted 9 lines · Consul
Quoted 6 lines · Kevin Wolf

This patch fixes a qcow2 corruption bug introduced in SVN Rev 5861. L2 tables
are big endian, so entries must be converted before being passed to functions.

Excellent! I think it is this bug who bit me more than once.

Holly cow. Me too, that definitely sounds like the bug that bit me a couple months ago:
http://article.gmane.org/gmane.comp.emulators.kvm.devel/28361

-marc

#4 lore raw
In reply to Kevin Wolf
Quoted 15 lines · Kevin Wolf

This patch fixes a qcow2 corruption bug introduced in SVN Rev 5861. L2 tables are big endian, so entries must be converted before being passed to functions.

This bug is easy to trigger. The following script will create and destroy a qcow2 image (the header is gone after three loop iterations):

    #!/bin/bash
    qemu-img create -f qcow2 test.qcow 1M
    for i in $(seq 1 10); do
    qemu-system-x86_64 -hda test.qcow -monitor stdio > /dev/null 2>&1 <<EOF
    savevm test-$i
    quit
    EOF
    done
Quoted 1 line

Applied to stable and trunk. Thanks!

Signature · 3 lines
Regards,

Anthony Liguori
#5 lore raw
In reply to Kevin Wolf
Quoted 25 lines · Kevin Wolf

This patch fixes a qcow2 corruption bug introduced in SVN Rev 5861. L2 tables are big endian, so entries must be converted before being passed to functions.

This bug is easy to trigger. The following script will create and destroy a qcow2 image (the header is gone after three loop iterations):

    #!/bin/bash
    qemu-img create -f qcow2 test.qcow 1M
    for i in $(seq 1 10); do
    qemu-system-x86_64 -hda test.qcow -monitor stdio > /dev/null 2>&1 <<EOF
    savevm test-$i
    quit
    EOF
    done
---
 block-qcow2.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
block-qcow2.c +0 -0
index da8fb42..985214f 100644

block-qcow2.c static int alloc_cluster_link_l2(BlockDriverState *bs, uint64_t cluster_offset, · line 1010

Quoted 5 lines
block-qcow2.c +1 -1
@@ -1007,7 +1007,7 @@ static int alloc_cluster_link_l2(BlockDriverState *bs, uint64_t cluster_offset,
         goto err;
 
     for (i = 0; i < j; i++)
-        free_any_clusters(bs, old_cluster[i], 1);
+        free_any_clusters(bs, be64_to_cpu(old_cluster[i]), 1);

Maybe it's time to make use of sparse's endianess annotations in qcow2 to prevent further problems like this one?

#6 lore raw
Quoted 2 lines · Christoph Hellwig

Maybe it's time to make use of sparse's endianess annotations in qcow2 to prevent further problems like this one

I'd much prefer strong type checking at compile time:

   typedef struct le32 {
       uint32_t blah;
   } le32;

static inline le32 cpu_to_le32(uint32_t blah) { ... } static inline uint32_t le32_to_cpu(le32 blah) { ... }

Signature · 1 line
error compiling committee.c: too many arguments to function
#7 lore raw
Quoted 5 lines · Avi Kivity
Quoted 2 lines · Christoph Hellwig

Maybe it's time to make use of sparse's endianess annotations in qcow2 to prevent further problems like this one

I'd much prefer strong type checking at compile time:

sparse has strong typechecking at compiletime for the __bitwise types, although you need the special sparse compiler to parse the annotations :)

And the big advantage is that qemu already has the infrastructure for it, while for your new structs you'd need various new helpers, including for comparisms of disk endian types, etc.