block/vhdx: Add alignment checks for region tables
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.
--- block/vhdx.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
block/vhdx.c +10 -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 ((rt_entry.file_offset & (MiB -1 )) || + (rt_entry.length & (MiB -1 )) || rt_entry.length < MiB) { + 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
Quoted 20 lines · Thomas Huth
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.
Reported-by Daniel Wade <[email protected]>Signed-off-by Thomas Huth <[email protected]>--- block/vhdx.c | 10 ++++++++++ 1 file changed, 10 insertions(+)block/vhdx.c +0
-0
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 ((rt_entry.file_offset & (MiB -1 )) || + (rt_entry.length & (MiB -1 )) || rt_entry.length < MiB) {
Would QEMU_IS_ALIGNED() be better than open-coding the math here? Also, the 'rt_entry.length < MiB' clause could be simplified to 'rt_entry.length == 0', since all other values less than MiB fail the earlier alignment clause.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. Virtualization: qemu.org | libguestfs.org
Am 11.11.26 um 21:32 schrieb Eric Blake:
Quoted 24 lines · Thomas Huth
Quoted 23 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."Thus let's add a sanity check to avoid that bad things could happen later.
Reported-by Daniel Wade <[email protected]>Signed-off-by Thomas Huth <[email protected]>--- block/vhdx.c | 10 ++++++++++ 1 file changed, 10 insertions(+)block/vhdx.c +0
-0@@ -566,6 +566,16 @@ vhdx_open_region_tables(BlockDriverState *bs, BDRVVHDXState *s)vhdx_region_entry_le_import (&rt_entry);
Quoted 8 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 ((rt_entry.file_offset & (MiB -1 )) || + (rt_entry.length & (MiB -1 )) || rt_entry.length < MiB) {Would
QEMU_IS_ALIGNED()be better than open-coding the math here?
That looks nicer, indeed. I'll send a v2...
Thanks,
Thomas