Skip to contentSkip to conversation

block: vhdx - fix reading beyond pointer during image creation

PATCH Applied Jeff Cody 3 participants 6 messages Sep 16, 2014 – Sep 19, 2014 Block layer

lore
Share & tools
lore

Jeff Cody

author
#0 lore raw

PATCH 1/1 block: vhdx - fix reading beyond pointer during image creation

In vhdx_create_metadata(), we allocate 40 bytes to entry_buffer for the various metadata table entries. However, we write out 64kB from that buffer into the new file. Only write out the correct 40 bytes.

---
 block/vhdx.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
block/vhdx.c +8 -8
index 796b7bd..b52ec32 100644
@@ -1407,6 +1407,12 @@ exit:
     return ret;
 }
 
+#define VHDX_METADATA_ENTRY_BUFFER_SIZE \
+                                    (sizeof(VHDXFileParameters)               +\
+                                     sizeof(VHDXVirtualDiskSize)              +\
+                                     sizeof(VHDXPage83Data)                   +\
+                                     sizeof(VHDXVirtualDiskLogicalSectorSize) +\
+                                     sizeof(VHDXVirtualDiskPhysicalSectorSize))
 
 /*
  * Create the Metadata entries.
@@ -1445,11 +1451,7 @@ static int vhdx_create_new_metadata(BlockDriverState *bs,
     VHDXVirtualDiskLogicalSectorSize  *mt_log_sector_size;
     VHDXVirtualDiskPhysicalSectorSize *mt_phys_sector_size;
 
-    entry_buffer = g_malloc0(sizeof(VHDXFileParameters)               +
-                             sizeof(VHDXVirtualDiskSize)              +
-                             sizeof(VHDXPage83Data)                   +
-                             sizeof(VHDXVirtualDiskLogicalSectorSize) +
-                             sizeof(VHDXVirtualDiskPhysicalSectorSize));
+    entry_buffer = g_malloc0(VHDX_METADATA_ENTRY_BUFFER_SIZE);
 
     mt_file_params = entry_buffer;
     offset += sizeof(VHDXFileParameters);
@@ -1530,7 +1532,7 @@ static int vhdx_create_new_metadata(BlockDriverState *bs,
     }
 
     ret = bdrv_pwrite(bs, metadata_offset + (64 * KiB), entry_buffer,
-                      VHDX_HEADER_BLOCK_SIZE);
+                      VHDX_METADATA_ENTRY_BUFFER_SIZE);
     if (ret < 0) {
         goto exit;
     }
@@ -1725,7 +1727,6 @@ static int vhdx_create_new_region_table(BlockDriverState *bs,
         goto exit;
     }
 
-
 exit:
     g_free(s);
     g_free(buffer);
@@ -1876,7 +1877,6 @@ static int vhdx_create(const char *filename, QemuOpts *opts, Error **errp)
     }
 
 
-
 delete_and_exit:
     bdrv_unref(bs);
 exit:
Signature · 1 line
1.9.3
#1 lore raw
Quoted 14 lines · Jeff Cody

In vhdx_create_metadata(), we allocate 40 bytes to entry_buffer for the various metadata table entries. However, we write out 64kB from that buffer into the new file. Only write out the correct 40 bytes.

---
 block/vhdx.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
block/vhdx.c +0 -0
index 796b7bd..b52ec32 100644

block/vhdx.c exit: · line 1415

Quoted 9 lines
block/vhdx.c +6 -0
@@ -1407,6 +1407,12 @@ exit:
     return ret;
 }
 
+#define VHDX_METADATA_ENTRY_BUFFER_SIZE \
+                                    (sizeof(VHDXFileParameters)               +\
+                                     sizeof(VHDXVirtualDiskSize)              +\
+                                     sizeof(VHDXPage83Data)                   +\
+                                     sizeof(VHDXVirtualDiskLogicalSectorSize) +\
+                                     sizeof(VHDXVirtualDiskPhysicalSectorSize))

Long lines, caused by excessive indentation. Emacs suggests

#define VHDX_METADATA_ENTRY_BUFFER_SIZE         \
    (sizeof(VHDXFileParameters)               + \
     sizeof(VHDXVirtualDiskSize)              + \
     sizeof(VHDXPage83Data)                   + \
     sizeof(VHDXVirtualDiskLogicalSectorSize) + \
     sizeof(VHDXVirtualDiskPhysicalSectorSize))
Quoted 17 lines
block/vhdx.c +1 -5
 /*
  * Create the Metadata entries.
@@ -1445,11 +1451,7 @@ static int vhdx_create_new_metadata(BlockDriverState *bs,
     VHDXVirtualDiskLogicalSectorSize  *mt_log_sector_size;
     VHDXVirtualDiskPhysicalSectorSize *mt_phys_sector_size;
 
-    entry_buffer = g_malloc0(sizeof(VHDXFileParameters)               +
-                             sizeof(VHDXVirtualDiskSize)              +
-                             sizeof(VHDXPage83Data)                   +
-                             sizeof(VHDXVirtualDiskLogicalSectorSize) +
-                             sizeof(VHDXVirtualDiskPhysicalSectorSize));
+    entry_buffer = g_malloc0(VHDX_METADATA_ENTRY_BUFFER_SIZE);
 
     mt_file_params = entry_buffer;
     offset += sizeof(VHDXFileParameters);

block/vhdx.c static int vhdx_create_new_metadata(BlockDriverState *bs, · line 1538

Quoted 8 lines
block/vhdx.c +1 -1
@@ -1530,7 +1532,7 @@ static int vhdx_create_new_metadata(BlockDriverState *bs,
     }
 
     ret = bdrv_pwrite(bs, metadata_offset + (64 * KiB), entry_buffer,
-                      VHDX_HEADER_BLOCK_SIZE);
+                      VHDX_METADATA_ENTRY_BUFFER_SIZE);
     if (ret < 0) {
         goto exit;
     }

Fixes read beyond end of buffer. Crash bug when sufficiently unlucky.

Quoted 9 lines
block/vhdx.c +0 -1
@@ -1725,7 +1727,6 @@ static int vhdx_create_new_region_table(BlockDriverState *bs,
         goto exit;
     }
 
-
 exit:
     g_free(s);
     g_free(buffer);

block/vhdx.c static int vhdx_create(const char *filename, QemuOpts *opts, Error **errp) · line 1882

Quoted 7 lines
block/vhdx.c +0 -1
@@ -1876,7 +1877,6 @@ static int vhdx_create(const char *filename, QemuOpts *opts, Error **errp)
     }
 
 
-
 delete_and_exit:
     bdrv_unref(bs);
 exit:

These two hunks are unrelated. I wouldn't include them. Advice, not objection.

Keeping two out of three blank lines looks odd, but pairs of blank lines occur elsewhere in the function, so I guess it's intentional.

If you clean up the long lines, you can add my R-by.

author
#2 lore raw
Quoted 28 lines · Markus Armbruster
Quoted 14 lines · Jeff Cody

In vhdx_create_metadata(), we allocate 40 bytes to entry_buffer for the various metadata table entries. However, we write out 64kB from that buffer into the new file. Only write out the correct 40 bytes.

---
 block/vhdx.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
block/vhdx.c +0 -0
index 796b7bd..b52ec32 100644

block/vhdx.c exit: · line 1415

Quoted 9 lines
block/vhdx.c +6 -0
@@ -1407,6 +1407,12 @@ exit:
     return ret;
 }
 
+#define VHDX_METADATA_ENTRY_BUFFER_SIZE \
+                                    (sizeof(VHDXFileParameters)               +\
+                                     sizeof(VHDXVirtualDiskSize)              +\
+                                     sizeof(VHDXPage83Data)                   +\
+                                     sizeof(VHDXVirtualDiskLogicalSectorSize) +\
+                                     sizeof(VHDXVirtualDiskPhysicalSectorSize))

Long lines, caused by excessive indentation. Emacs suggests

Quoted 7 lines
#define VHDX_METADATA_ENTRY_BUFFER_SIZE         \
    (sizeof(VHDXFileParameters)               + \
     sizeof(VHDXVirtualDiskSize)              + \
     sizeof(VHDXPage83Data)                   + \
     sizeof(VHDXVirtualDiskLogicalSectorSize) + \
     sizeof(VHDXVirtualDiskPhysicalSectorSize))

So, I was getting ready to respin this, but double checked the patch - it shows the lines ending on column 80 (as intended), and checkpatch.pl had no issue with it. Did you accidentally (or intentionally!) count the leading '+' of the patch itself?

Quoted 52 lines
Quoted 17 lines
block/vhdx.c +1 -5
 /*
  * Create the Metadata entries.
@@ -1445,11 +1451,7 @@ static int vhdx_create_new_metadata(BlockDriverState *bs,
     VHDXVirtualDiskLogicalSectorSize  *mt_log_sector_size;
     VHDXVirtualDiskPhysicalSectorSize *mt_phys_sector_size;
 
-    entry_buffer = g_malloc0(sizeof(VHDXFileParameters)               +
-                             sizeof(VHDXVirtualDiskSize)              +
-                             sizeof(VHDXPage83Data)                   +
-                             sizeof(VHDXVirtualDiskLogicalSectorSize) +
-                             sizeof(VHDXVirtualDiskPhysicalSectorSize));
+    entry_buffer = g_malloc0(VHDX_METADATA_ENTRY_BUFFER_SIZE);
 
     mt_file_params = entry_buffer;
     offset += sizeof(VHDXFileParameters);

block/vhdx.c static int vhdx_create_new_metadata(BlockDriverState *bs, · line 1538

Quoted 8 lines
block/vhdx.c +1 -1
@@ -1530,7 +1532,7 @@ static int vhdx_create_new_metadata(BlockDriverState *bs,
     }
 
     ret = bdrv_pwrite(bs, metadata_offset + (64 * KiB), entry_buffer,
-                      VHDX_HEADER_BLOCK_SIZE);
+                      VHDX_METADATA_ENTRY_BUFFER_SIZE);
     if (ret < 0) {
         goto exit;
     }

Fixes read beyond end of buffer. Crash bug when sufficiently unlucky.

Quoted 9 lines
block/vhdx.c +0 -1
@@ -1725,7 +1727,6 @@ static int vhdx_create_new_region_table(BlockDriverState *bs,
         goto exit;
     }
 
-
 exit:
     g_free(s);
     g_free(buffer);

block/vhdx.c static int vhdx_create(const char *filename, QemuOpts *opts, Error **errp) · line 1882

Quoted 7 lines
block/vhdx.c +0 -1
@@ -1876,7 +1877,6 @@ static int vhdx_create(const char *filename, QemuOpts *opts, Error **errp)
     }
 
 
-
 delete_and_exit:
     bdrv_unref(bs);
 exit:

These two hunks are unrelated. I wouldn't include them. Advice, not objection.

Keeping two out of three blank lines looks odd, but pairs of blank lines occur elsewhere in the function, so I guess it's intentional.

If you clean up the long lines, you can add my R-by.

#3 lore raw
Quoted 37 lines · Jeff Cody
Quoted 28 lines · Markus Armbruster
Quoted 14 lines · Jeff Cody

In vhdx_create_metadata(), we allocate 40 bytes to entry_buffer for the various metadata table entries. However, we write out 64kB from that buffer into the new file. Only write out the correct 40 bytes.

---
 block/vhdx.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
block/vhdx.c +0 -0
index 796b7bd..b52ec32 100644

block/vhdx.c exit: · line 1415

Quoted 9 lines
block/vhdx.c +6 -0
@@ -1407,6 +1407,12 @@ exit:
     return ret;
 }
 
+#define VHDX_METADATA_ENTRY_BUFFER_SIZE \
+                                    (sizeof(VHDXFileParameters)               +\
+                                     sizeof(VHDXVirtualDiskSize)              +\
+                                     sizeof(VHDXPage83Data)                   +\
+                                     sizeof(VHDXVirtualDiskLogicalSectorSize) +\
+                                     sizeof(VHDXVirtualDiskPhysicalSectorSize))

Long lines, caused by excessive indentation. Emacs suggests

Quoted 7 lines
#define VHDX_METADATA_ENTRY_BUFFER_SIZE         \
    (sizeof(VHDXFileParameters)               + \
     sizeof(VHDXVirtualDiskSize)              + \
     sizeof(VHDXPage83Data)                   + \
     sizeof(VHDXVirtualDiskLogicalSectorSize) + \
     sizeof(VHDXVirtualDiskPhysicalSectorSize))
Quoted 4 lines

So, I was getting ready to respin this, but double checked the patch - it shows the lines ending on column 80 (as intended), and checkpatch.pl had no issue with it. Did you accidentally (or intentionally!) count the leading '+' of the patch itself?

I didn't count anything, I trusted my eyes, which screamed "ugly!" :)

Reviewed-by
#4 lore raw
Quoted 39 lines · Markus Armbruster
Quoted 37 lines · Jeff Cody
Quoted 35 lines · Markus Armbruster
Quoted 14 lines · Jeff Cody

In vhdx_create_metadata(), we allocate 40 bytes to entry_buffer for the various metadata table entries. However, we write out 64kB from that buffer into the new file. Only write out the correct 40 bytes.

---
 block/vhdx.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
block/vhdx.c +0 -0
index 796b7bd..b52ec32 100644

block/vhdx.c exit: · line 1415

Quoted 9 lines
block/vhdx.c +6 -0
@@ -1407,6 +1407,12 @@ exit:
     return ret;
 }
 
+#define VHDX_METADATA_ENTRY_BUFFER_SIZE \
+                                    (sizeof(VHDXFileParameters)               +\
+                                     sizeof(VHDXVirtualDiskSize)              +\
+                                     sizeof(VHDXPage83Data)                   +\
+                                     sizeof(VHDXVirtualDiskLogicalSectorSize) +\
+                                     sizeof(VHDXVirtualDiskPhysicalSectorSize))

Long lines, caused by excessive indentation. Emacs suggests

#define VHDX_METADATA_ENTRY_BUFFER_SIZE         \
    (sizeof(VHDXFileParameters)               + \
     sizeof(VHDXVirtualDiskSize)              + \
     sizeof(VHDXPage83Data)                   + \
     sizeof(VHDXVirtualDiskLogicalSectorSize) + \
     sizeof(VHDXVirtualDiskPhysicalSectorSize))
Quoted 6 lines
Quoted 4 lines

So, I was getting ready to respin this, but double checked the patch - it shows the lines ending on column 80 (as intended), and checkpatch.pl had no issue with it. Did you accidentally (or intentionally!) count the leading '+' of the patch itself?

I didn't count anything, I trusted my eyes, which screamed "ugly!" :)

Forgot to say: since checkpatch is happy, I guess this is a technically a matter of taste, so

Just in case you actually *like* hanging your code right off the right margin of the window. *Shudder* ;-P

#5 lore raw
In reply to Jeff Cody
Quoted 8 lines · Jeff Cody

In vhdx_create_metadata(), we allocate 40 bytes to entry_buffer for the various metadata table entries. However, we write out 64kB from that buffer into the new file. Only write out the correct 40 bytes.

---
 block/vhdx.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

Thanks, applied to my block tree:
https://github.com/stefanha/qemu/commits/block

Stefan