Skip to contentSkip to conversation

block/vhdx: Add alignment checks for region tables

PATCH Superseded Thomas Huth 2 participants 5 messages Nov 12 – Nov 13 Block layer

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block/vhdx: Add alignment checks for region tables

According to the VHDX specification:

"The FileOffset and Length fields specify the 64-bit byte offset and 32-bit
byte length of the object within the file. The values must be a multiple of 1 MB, and FileOffset must be at least 1 MB."

Thus let's add a sanity check to avoid that bad things could happen later.

---
v2: Use QEMU_IS_ALIGNED() as suggested by Eric

 block/vhdx.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
block/vhdx.c +10 -0
index 8013a90a1a4..21270988b5a 100644
@@ -566,6 +566,16 @@ vhdx_open_region_tables(BlockDriverState *bs, BDRVVHDXState *s)
 
         vhdx_region_entry_le_import(&rt_entry);
 
+        /*
+         * According to the VHDX spec, offset and length must be multiple
+         * of 1 MiB and length must be at least 1 MiB.
+         */
+        if (!QEMU_IS_ALIGNED(rt_entry.file_offset, MiB) ||
+            !QEMU_IS_ALIGNED(rt_entry.length, MiB) || !rt_entry.length) {
+            ret = -EINVAL;
+            goto fail;
+        }
+
         /* check for region overlap between these entries, and any
          * other memory regions in the file */
         ret = vhdx_region_check(s, rt_entry.file_offset, rt_entry.length);
Signature · 1 line
2.56.0
#1 lore raw

Am 12.11.2026 um 10:22 hat Thomas Huth geschrieben:

Quoted 5 lines

According to the VHDX specification:

"The FileOffset and Length fields specify the 64-bit byte offset and 32-bit
byte length of the object within the file. The values must be a multiple of 1 MB, and FileOffset must be at least 1 MB."

This requires that the offset (not length) is at least 1 MB...

Quoted 16 lines

Thus let's add a sanity check to avoid that bad things could happen later.

---
v2: Use QEMU_IS_ALIGNED() as suggested by Eric

 block/vhdx.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
block/vhdx.c +0 -0
index 8013a90a1a4..21270988b5a 100644

block/vhdx.c vhdx_open_region_tables(BlockDriverState *bs, BDRVVHDXState *s) · line 574

Quoted 9 lines
block/vhdx.c +6 -0
@@ -566,6 +566,16 @@ vhdx_open_region_tables(BlockDriverState *bs, BDRVVHDXState *s)
 
         vhdx_region_entry_le_import(&rt_entry);
 
+        /*
+         * According to the VHDX spec, offset and length must be multiple
+         * of 1 MiB and length must be at least 1 MiB.
+         */
+        if (!QEMU_IS_ALIGNED(rt_entry.file_offset, MiB) ||
+            !QEMU_IS_ALIGNED(rt_entry.length, MiB) || !rt_entry.length) {

...but the comment and code do the opposite and check only that the length is at least 1 MB, leaving FileOffset (which was the actual requirement) unchecked.

block/vhdx.c vhdx_open_region_tables(BlockDriverState *bs, BDRVVHDXState *s) · line 581

Quoted 8 lines
block/vhdx.c +4 -0
+            ret = -EINVAL;
+            goto fail;
+        }
+
         /* check for region overlap between these entries, and any
          * other memory regions in the file */
         ret = vhdx_region_check(s, rt_entry.file_offset, rt_entry.length);

I'm also wondering if the check shouldn't be in vhdx_region_check() instead, which we're already calling here.

Kevin

#2 lore raw

Am 12.11.26 um 13:49 schrieb Kevin Wolf:

Quoted 28 lines

Am 12.11.2026 um 10:22 hat Thomas Huth geschrieben:

Quoted 5 lines

According to the VHDX specification:

"The FileOffset and Length fields specify the 64-bit byte offset and 32-bit
byte length of the object within the file. The values must be a multiple of 1 MB, and FileOffset must be at least 1 MB."

This requires that the offset (not length) is at least 1 MB...

Quoted 19 lines

Thus let's add a sanity check to avoid that bad things could happen later.

---
v2: Use QEMU_IS_ALIGNED() as suggested by Eric

  block/vhdx.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
block/vhdx.c +0 -0
index 8013a90a1a4..21270988b5a 100644
@@ -566,6 +566,16 @@ vhdx_open_region_tables(BlockDriverState *bs, BDRVVHDXState *s)
 
          vhdx_region_entry_le_import(&rt_entry);
 
Quoted 10 lines

block/vhdx.c vhdx_open_region_tables(BlockDriverState *bs, BDRVVHDXState *s) · line 574

Quoted 6 lines
block/vhdx.c +6 -0
+        /*
+         * According to the VHDX spec, offset and length must be multiple
+         * of 1 MiB and length must be at least 1 MiB.
+         */
+        if (!QEMU_IS_ALIGNED(rt_entry.file_offset, MiB) ||
+            !QEMU_IS_ALIGNED(rt_entry.length, MiB) || !rt_entry.length) {

...but the comment and code do the opposite and check only that the length is at least 1 MB, leaving FileOffset (which was the actual requirement) unchecked.

D'oh, stupid me! I was apparently sloppy when reading the spec, sorry! (though a zero length likely also does not make much sense...)

Quoted 11 lines

block/vhdx.c vhdx_open_region_tables(BlockDriverState *bs, BDRVVHDXState *s) · line 581

Quoted 8 lines
block/vhdx.c +4 -0
+            ret = -EINVAL;
+            goto fail;
+        }
+
          /* check for region overlap between these entries, and any
           * other memory regions in the file */
          ret = vhdx_region_check(s, rt_entry.file_offset, rt_entry.length);

I'm also wondering if the check shouldn't be in vhdx_region_check() instead, which we're already calling here.

I considered that, too, but finally decided against it since vhdx_region_check() seemed rather about overlapping regions... but if you prefer, I can also move it there.

Thomas

#3 lore raw

Am 12.11.2026 um 15:48 hat Thomas Huth geschrieben:

Quoted 38 lines

Am 12.11.26 um 13:49 schrieb Kevin Wolf:

Quoted 20 lines

Am 12.11.2026 um 10:22 hat Thomas Huth geschrieben:

Quoted 5 lines

According to the VHDX specification:

"The FileOffset and Length fields specify the 64-bit byte offset and 32-bit
byte length of the object within the file. The values must be a multiple of 1 MB, and FileOffset must be at least 1 MB."

This requires that the offset (not length) is at least 1 MB...

Quoted 11 lines

Thus let's add a sanity check to avoid that bad things could happen later.

---
v2: Use QEMU_IS_ALIGNED() as suggested by Eric

  block/vhdx.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
Quoted 16 lines

block/vhdx.c vhdx_open_region_tables(BlockDriverState *bs, BDRVVHDXState *s) · line 572

Quoted 12 lines
block/vhdx.c +6 -0
index 8013a90a1a4..21270988b5a 100644
@@ -566,6 +566,16 @@ vhdx_open_region_tables(BlockDriverState *bs, BDRVVHDXState *s)
          vhdx_region_entry_le_import(&rt_entry);
+        /*
+         * According to the VHDX spec, offset and length must be multiple
+         * of 1 MiB and length must be at least 1 MiB.
+         */
+        if (!QEMU_IS_ALIGNED(rt_entry.file_offset, MiB) ||
+            !QEMU_IS_ALIGNED(rt_entry.length, MiB) || !rt_entry.length) {

...but the comment and code do the opposite and check only that the length is at least 1 MB, leaving FileOffset (which was the actual requirement) unchecked.

Quoted 2 lines

D'oh, stupid me! I was apparently sloppy when reading the spec, sorry! (though a zero length likely also does not make much sense...)

Yes, I wouldn't have complained about the extra check for length, but checking the offset seems important.

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
block/vhdx.c +4 -0
+            ret = -EINVAL;
+            goto fail;
+        }
+
          /* check for region overlap between these entries, and any
           * other memory regions in the file */
          ret = vhdx_region_check(s, rt_entry.file_offset, rt_entry.length);
Quoted 6 lines
Quoted 2 lines

I'm also wondering if the check shouldn't be in vhdx_region_check() instead, which we're already calling here.

I considered that, too, but finally decided against it since vhdx_region_check() seemed rather about overlapping regions... but if you prefer, I can also move it there.

There's a comment at vhdx_region_check() that could use an update then, but the name vhdx_region_check() feels generic enough to me.

But this is not a requirement from my side, just a thought I had. If we both had it, maybe that means something, but you decide.

Kevin

#4 lore raw

Am 12.11.26 um 17:19 schrieb Kevin Wolf:

Quoted 12 lines

Am 12.11.2026 um 15:48 hat Thomas Huth geschrieben:

Quoted 11 lines

Am 12.11.26 um 13:49 schrieb Kevin Wolf:

Quoted 10 lines

Am 12.11.2026 um 10:22 hat Thomas Huth geschrieben:

Quoted 5 lines

According to the VHDX specification:

"The FileOffset and Length fields specify the 64-bit byte offset and 32-bit
byte length of the object within the file. The values must be a multiple of 1 MB, and FileOffset must be at least 1 MB."

This requires that the offset (not length) is at least 1 MB...

Quoted 1 line

Thus let's add a sanity check to avoid that bad things could happen later.

...

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
block/vhdx.c +6 -0
index 8013a90a1a4..21270988b5a 100644
@@ -566,6 +566,16 @@ vhdx_open_region_tables(BlockDriverState *bs, BDRVVHDXState *s)
           vhdx_region_entry_le_import(&rt_entry);
+        /*
+         * According to the VHDX spec, offset and length must be multiple
+         * of 1 MiB and length must be at least 1 MiB.
+         */
+        if (!QEMU_IS_ALIGNED(rt_entry.file_offset, MiB) ||
+            !QEMU_IS_ALIGNED(rt_entry.length, MiB) || !rt_entry.length) {

...

Quoted 15 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
block/vhdx.c +4 -0
+            ret = -EINVAL;
+            goto fail;
+        }
+
           /* check for region overlap between these entries, and any
            * other memory regions in the file */
           ret = vhdx_region_check(s, rt_entry.file_offset, rt_entry.length);
Quoted 6 lines
Quoted 2 lines

I'm also wondering if the check shouldn't be in vhdx_region_check() instead, which we're already calling here.

I considered that, too, but finally decided against it since vhdx_region_check() seemed rather about overlapping regions... but if you prefer, I can also move it there.

Quoted 5 lines

There's a comment at vhdx_region_check() that could use an update then, but the name vhdx_region_check() feels generic enough to me.

But this is not a requirement from my side, just a thought I had. If we both had it, maybe that means something, but you decide.

I had another closer look, and vhdx_region_check() is also used to check some BAT entries for overlap - and those have different requirements as far as I understood the spec. So I decided against moving the check into that function in v3:

https://lore.kernel.org/qemu-devel/[email protected]/

Thomas