Skip to contentSkip to conversation

qcow2: avoid misaligned 64bit bswap

PATCH Applied John Snow 3 participants 3 messages Nov 2, 2015 – Nov 4, 2015 Block layer

lore
Share & tools
lore

John Snow

author
#0 lore raw

PATCH 1/1 qcow2: avoid misaligned 64bit bswap

If we create a buffer directly on the stack by using 12 bytes, there's no guarantee the 64bit value we want to swap will be aligned, which could cause errors with undefined behavior.

Spotted with clang -fsanitize=undefined and observed in iotests 15, 26, 44, 115 and 121.

---
 block/qcow2-refcount.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
block/qcow2-refcount.c +7 -4
index 4b81c8d..6e0e5bd 100644
@@ -560,13 +560,16 @@ static int alloc_refcount_block(BlockDriverState *bs,
     }
 
     /* Hook up the new refcount table in the qcow2 header */
-    uint8_t data[12];
-    cpu_to_be64w((uint64_t*)data, table_offset);
-    cpu_to_be32w((uint32_t*)(data + 8), table_clusters);
+    struct QEMU_PACKED {
+        uint64_t d64;
+        uint32_t d32;
+    } data;
+    cpu_to_be64w(&data.d64, table_offset);
+    cpu_to_be32w(&data.d32, table_clusters);
     BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE);
     ret = bdrv_pwrite_sync(bs->file->bs,
                            offsetof(QCowHeader, refcount_table_offset),
-                           data, sizeof(data));
+                           &data, sizeof(data));
     if (ret < 0) {
         goto fail_table;
     }
Signature · 1 line
2.4.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · John Snow

If we create a buffer directly on the stack by using 12 bytes, there's no guarantee the 64bit value we want to swap will be aligned, which could cause errors with undefined behavior.

Spotted with clang -fsanitize=undefined and observed in iotests 15, 26, 44, 115 and 121.

---
 block/qcow2-refcount.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Applied
#2 lore raw

Am 03.11.2015 um 00:46 hat Eric Blake geschrieben:

Quoted 8 lines · John Snow
Quoted 7 lines

If we create a buffer directly on the stack by using 12 bytes, there's no guarantee the 64bit value we want to swap will be aligned, which could cause errors with undefined behavior.

Spotted with clang -fsanitize=undefined and observed in iotests 15, 26, 44, 115 and 121.

Quoted 6 lines
Quoted 4 lines
---
 block/qcow2-refcount.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

Thanks, applied to the block branch.

Kevin