vmdk: A few small fixes
v3 cover vmdk: A few small fixes
v3: Fix 3/6 again. (Markus)
v2: 3/6: Don't overwrite the last byte of buffer. (Don)
Other patches are unchanged (added Markus' rev-by line).
Here are some improvements on miscellaneous things such as CID generation, comments, input validation.
Fam Zheng (6):
vmdk: Use g_random_int to generate CID
vmdk: Fix comment to match code of extent lines
vmdk: Clean up descriptor file reading
vmdk: Check descriptor file length when reading it
vmdk: Remove unnecessary initialization
vmdk: Set errp on failures in vmdk_open_vmdk4
block/vmdk.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-)
Signature · 1 line
1.9.3
PATCH v3 1/6 vmdk: Use g_random_int to generate CID
This replaces two "time(NULL)" invocations with "g_random_int()". According to VMDK spec, CID "is a random 32‐bit value updated the first time the content of the virtual disk is modified after the virtual disk is opened". Using "seconds since epoch" is just a "lame way" to generate it, and not completely safe because of the low precision.
--- block/vmdk.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
block/vmdk.c +3 -2
@@ -28,6 +28,7 @@#include "qemu/module.h" #include "migration/migration.h" #include <zlib.h> +#include <glib.h> #define VMDK3_MAGIC (('C' << 24) | ('O' << 16) | ('W' << 8) | 'D')#define VMDK4_MAGIC (('K' << 24) | ('D' << 16) | ('M' << 8) | 'V')
@@ -1538,7 +1539,7 @@ static int vmdk_write(BlockDriverState *bs, int64_t sector_num,/* update CID on the first write every time the virtual disk is * opened */ if (!s->cid_updated) { - ret =vmdk_write_cid (bs,time (NULL )); + ret =vmdk_write_cid (bs,g_random_int ());if (ret <0 ) {return ret; }
@@ -1922,7 +1923,7 @@ static int vmdk_create(const char *filename, QemuOpts *opts, Error **errp) }/* generate descriptor file */ desc = g_strdup_printf(desc_template, - (uint32_t)time(NULL), + g_random_int(), parent_cid, fmt, parent_desc_line,
Signature · 1 line
1.9.3
Re: [Qemu-devel] [PATCH v3 1/6] vmdk: Use g_random_int to generate CID
Quoted 18 lines · Fam Zheng
This replaces two "time(NULL)" invocations with "
g_random_int()". According to VMDK spec, CID "is a random 32‐bit value updated the first time the content of the virtual disk is modified after the virtual disk is opened". Using "seconds since epoch" is just a "lame way" to generate it, and not completely safe because of the low precision.Suggested-by Markus Armbruster <[email protected]>Signed-off-by Fam Zheng <[email protected]>Reviewed-by Markus Armbruster <[email protected]>--- block/vmdk.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)block/vmdk.c +0
-0
block/vmdk.c line 31
Quoted 4 lines
block/vmdk.c +1
-0@@ -28,6 +28,7 @@#include "qemu/module.h" #include "migration/migration.h" #include <zlib.h> +#include <glib.h>
This should be covered by qemu-common.h already. But anyway:
Re: [Qemu-devel] [PATCH v3 1/6] vmdk: Use g_random_int to generate CID
In reply to Fam ZhengQuoted 10 lines · On Wed, 3 Dec 2014 18:28:36 +0800 Fam Zheng <[email protected]> wrote:
This replaces two "time(NULL)" invocations with "
g_random_int()". According to VMDK spec, CID "is a random 32‐bit value updated the first time the content of the virtual disk is modified after the virtual disk is opened". Using "seconds since epoch" is just a "lame way" to generate it, and not completely safe because of the low precision.Suggested-by Markus Armbruster <[email protected]>Signed-off-by Fam Zheng <[email protected]>Reviewed-by Markus Armbruster <[email protected]>---
Quoted 37 lines
block/vmdk.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)block/vmdk.c +3
-2@@ -28,6 +28,7 @@#include "qemu/module.h" #include "migration/migration.h" #include <zlib.h> +#include <glib.h> #define VMDK3_MAGIC (('C' << 24) | ('O' << 16) | ('W' << 8) | 'D')#define VMDK4_MAGIC (('K' << 24) | ('D' << 16) | ('M' << 8) | 'V')@@ -1538,7 +1539,7 @@ static int vmdk_write(BlockDriverState *bs, int64_t sector_num,/* update CID on the first write every time the virtual disk is * opened */ if (!s->cid_updated) { - ret =vmdk_write_cid (bs,time (NULL )); + ret =vmdk_write_cid (bs,g_random_int ());if (ret <0 ) {return ret; }@@ -1922,7 +1923,7 @@ static int vmdk_create(const char *filename, QemuOpts *opts, Error **errp) }/* generate descriptor file */ desc = g_strdup_printf(desc_template, - (uint32_t)time(NULL), + g_random_int(), parent_cid, fmt, parent_desc_line,Signature · 1 line
1.9.3
PATCH v3 2/6 vmdk: Fix comment to match code of extent lines
In reply to Fam Zhengcommit 04d542c8b (vmdk: support vmfs files) added support of VMFS extent type but the comment above the changed code is left out. Update the comment so they are consistent.
--- block/vmdk.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
block/vmdk.c +3 -2
@@ -785,10 +785,11 @@ static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,VmdkExtent *extent;while (*p) { -/* parse extent line: +/* parse extent line in one of below formats: +* * RW [size in sectors] FLAT "file-name.vmdk" OFFSET -* or * RW [size in sectors] SPARSE "file-name.vmdk" +* RW [size in sectors] VMFS "file-name.vmdk" */ flat_offset =-1 ; ret = sscanf(p, "%10s %" SCNd64 " %10s \"%511[^\n\r\"]\" %" SCNd64,
Signature · 1 line
1.9.3
Re: [Qemu-devel] [PATCH v3 2/6] vmdk: Fix comment to match code of extent lines
Quoted 18 lines · Fam Zheng
commit
04d542c8b(vmdk: support vmfs files) added support of VMFS extent type but the comment above the changed code is left out. Update the comment so they are consistent.Signed-off-by Fam Zheng <[email protected]>Reviewed-by Markus Armbruster <[email protected]>--- block/vmdk.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)block/vmdk.c +0
-0@@ -785,10 +785,11 @@ static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,VmdkExtent *extent;while (*p) {
block/vmdk.c static int vmdk_parse_extents(const char *desc, BlockDriverState *bs, · line 795
Quoted 10 lines
block/vmdk.c +3
-2-/* parse extent line: +/* parse extent line in one of below formats: +* * RW [size in sectors] FLAT "file-name.vmdk" OFFSET -* or * RW [size in sectors] SPARSE "file-name.vmdk" +* RW [size in sectors] VMFS "file-name.vmdk" */ flat_offset =-1 ; ret = sscanf(p, "%10s %" SCNd64 " %10s \"%511[^\n\r\"]\" %" SCNd64,
Isn't VMFSSPARSE missing? It doesn't seem to be handled any different than SPARSE, but then again, VMFS isn't handled so differently than FLAT either (except it doesn't take an offset)...
Max
PATCH v3 3/6 vmdk: Clean up descriptor file reading
In reply to Fam ZhengZeroing a buffer that will be filled right after is not necessary, and allocating a power of two + 1 is naughty.
--- block/vmdk.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
block/vmdk.c +3 -2
@@ -557,8 +557,8 @@ static char *vmdk_read_desc(BlockDriverState *file, uint64_t desc_offset,return NULL ; } - size =MIN (size,1 <<20 );/* avoid unbounded allocation */ - buf =g_malloc0 (size +1 ); + size =MIN (size, (1 <<20 ) -1 );/* avoid unbounded allocation */ + buf =g_malloc (size +1 ); ret =bdrv_pread (file, desc_offset, buf, size);if (ret <0 ) {
@@ -566,6 +566,7 @@ static char *vmdk_read_desc(BlockDriverState *file, uint64_t desc_offset,g_free (buf);return NULL ; } + buf[ret] =0 ;return buf; }
Signature · 1 line
1.9.3
Re: [Qemu-devel] [PATCH v3 3/6] vmdk: Clean up descriptor file reading
Quoted 8 lines · Fam Zheng
Zeroing a buffer that will be filled right after is not necessary, and allocating a power of two + 1 is naughty.
Suggested-by Markus Armbruster <[email protected]>Signed-off-by Fam Zheng <[email protected]>--- block/vmdk.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
Re: [Qemu-devel] [PATCH v3 3/6] vmdk: Clean up descriptor file reading
In reply to Fam ZhengQuoted 5 lines · Fam Zheng
Zeroing a buffer that will be filled right after is not necessary, and allocating a power of two + 1 is naughty.
Suggested-by Markus Armbruster <[email protected]>Signed-off-by Fam Zheng <[email protected]>
Re: [Qemu-devel] [PATCH v3 3/6] vmdk: Clean up descriptor file reading
In reply to Fam ZhengQuoted 6 lines · On Wed, 3 Dec 2014 18:28:38 +0800 Fam Zheng <[email protected]> wrote:
Zeroing a buffer that will be filled right after is not necessary, and allocating a power of two + 1 is naughty.
Suggested-by Markus Armbruster <[email protected]>Signed-off-by Fam Zheng <[email protected]>---
Quoted 30 lines
block/vmdk.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)block/vmdk.c +3
-2@@ -557,8 +557,8 @@ static char *vmdk_read_desc(BlockDriverState *file, uint64_t desc_offset,return NULL ; } - size =MIN (size,1 <<20 );/* avoid unbounded allocation */ - buf =g_malloc0 (size +1 ); + size =MIN (size, (1 <<20 ) -1 );/* avoid unbounded allocation */ + buf =g_malloc (size +1 ); ret =bdrv_pread (file, desc_offset, buf, size);if (ret <0 ) {@@ -566,6 +566,7 @@ static char *vmdk_read_desc(BlockDriverState *file, uint64_t desc_offset,g_free (buf);return NULL ; } + buf[ret] =0 ;return buf; }Signature · 1 line
1.9.3
PATCH v3 4/6 vmdk: Check descriptor file length when reading it
In reply to Fam ZhengSince a too small file cannot be a valid VMDK image, and also since the buffer's first 4 bytes will be unconditionally examined by vmdk_open_sparse, let's error out the small file case to be clear.
--- block/vmdk.c | 5 +++++ 1 file changed, 5 insertions(+)
block/vmdk.c +5 -0
@@ -557,6 +557,11 @@ static char *vmdk_read_desc(BlockDriverState *file, uint64_t desc_offset,return NULL ; } +if (size <4 ) { +error_setg_errno (errp, -size,"File is too small, not a valid image" ); +return NULL ; + } + size =MIN (size, (1 <<20 ) -1 );/* avoid unbounded allocation */ buf =g_malloc (size +1 );
Signature · 1 line
1.9.3
Re: [Qemu-devel] [PATCH v3 4/6] vmdk: Check descriptor file length when reading it
Quoted 15 lines · Fam Zheng
Since a too small file cannot be a valid VMDK image, and also since the buffer's first 4 bytes will be unconditionally examined by
vmdk_open_sparse, let's error out the small file case to be clear.Signed-off-by Fam Zheng <[email protected]>Reviewed-by Markus Armbruster <[email protected]>--- block/vmdk.c | 5 +++++ 1 file changed, 5 insertions(+)block/vmdk.c +0
-0
block/vmdk.c static char *vmdk_read_desc(BlockDriverState *file, uint64_t desc_offset, · line 561
Quoted 5 lines
block/vmdk.c +2
-0@@ -557,6 +557,11 @@ static char *vmdk_read_desc(BlockDriverState *file, uint64_t desc_offset,return NULL ; } +if (size <4 ) { +error_setg_errno (errp, -size,"File is too small, not a valid image" );
0 <= size < 4, strerror(0..-3) doesn't make a whole lot of sense. Should be simply error_setg() instead of error_setg_errno().
Max
Quoted 6 lines
block/vmdk.c +3
-0+return NULL ; + } + size =MIN (size, (1 <<20 ) -1 );/* avoid unbounded allocation */ buf =g_malloc (size +1 );
Re: [Qemu-devel] [PATCH v3 4/6] vmdk: Check descriptor file length when reading it
Quoted 11 lines · Max Reitz
Quoted 10 lines · Fam Zheng
Since a too small file cannot be a valid VMDK image, and also since the buffer's first 4 bytes will be unconditionally examined by
vmdk_open_sparse, let's error out the small file case to be clear.Signed-off-by Fam Zheng <[email protected]>Reviewed-by Markus Armbruster <[email protected]>--- block/vmdk.c | 5 +++++ 1 file changed, 5 insertions(+)
Quoted 12 lines
block/vmdk.c static char *vmdk_read_desc(BlockDriverState *file, uint64_t desc_offset, · line 560
Quoted 9 lines
block/vmdk.c +2
-0@@ -557,6 +557,11 @@ static char *vmdk_read_desc(BlockDriverState *file, uint64_t desc_offset,return NULL ; } +if (size <4 ) { +error_setg_errno (errp, -size,"File is too small, not a valid image" );0 <= size < 4, strerror(0..-3) doesn't make a whole lot of sense. Should be simply
error_setg()instead oferror_setg_errno().
Yes, fixing.
Fam
PATCH v3 5/6 vmdk: Remove unnecessary initialization
In reply to Fam ZhengIt will be assigned to the return value of vmdk_read_desc.
--- block/vmdk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
block/vmdk.c +1 -1
@@ -910,7 +910,7 @@ exit:static int vmdk_open (BlockDriverState *bs,QDict *options,int flags,Error **errp) { -char *buf =NULL ; +char *buf;int ret;BDRVVmdkState *s = bs->opaque;uint32_t magic;
Signature · 1 line
1.9.3
Re: [Qemu-devel] [PATCH v3 5/6] vmdk: Remove unnecessary initialization
Quoted 8 lines · Fam Zheng
It will be assigned to the return value of
vmdk_read_desc.Suggested-by Markus Armbruster <[email protected]>Signed-off-by Fam Zheng <[email protected]>Reviewed-by Markus Armbruster <[email protected]>--- block/vmdk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Re: [Qemu-devel] [PATCH v3 5/6] vmdk: Remove unnecessary initialization
In reply to Fam ZhengQuoted 6 lines · On Wed, 3 Dec 2014 18:28:40 +0800 Fam Zheng <[email protected]> wrote:
It will be assigned to the return value of
vmdk_read_desc.Suggested-by Markus Armbruster <[email protected]>Signed-off-by Fam Zheng <[email protected]>Reviewed-by Markus Armbruster <[email protected]>---
Quoted 20 lines
block/vmdk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)block/vmdk.c +1
-1@@ -910,7 +910,7 @@ exit:static int vmdk_open (BlockDriverState *bs,QDict *options,int flags,Error **errp) { -char *buf =NULL ; +char *buf;int ret;BDRVVmdkState *s = bs->opaque;uint32_t magic;Signature · 1 line
1.9.3
PATCH v3 6/6 vmdk: Set errp on failures in vmdk_open_vmdk4
In reply to Fam Zheng--- block/vmdk.c | 3 +++ 1 file changed, 3 insertions(+)
block/vmdk.c +3 -0
@@ -642,6 +642,7 @@ static int vmdk_open_vmdk4(BlockDriverState *bs, bs->file->total_sectors *512 -1536 , &footer, sizeof(footer));if (ret <0 ) { +error_setg_errno (errp, -ret,"Failed to read footer" );return ret; }
@@ -653,6 +654,7 @@ static int vmdk_open_vmdk4(BlockDriverState *bs, le32_to_cpu(footer.eos_marker.size) != 0 || le32_to_cpu(footer.eos_marker.type) != MARKER_END_OF_STREAM) { +error_setg (errp,"Invalid footer" );return -EINVAL ; }
@@ -683,6 +685,7 @@ static int vmdk_open_vmdk4(BlockDriverState *bs, l1_entry_sectors =le32_to_cpu (header.num_gtes_per_gt) *le64_to_cpu (header.granularity);if (l1_entry_sectors ==0 ) { +error_setg (errp,"L1 entry size is invalid" );return -EINVAL ; } l1_size = (le64_to_cpu (header.capacity) + l1_entry_sectors -1 )
Signature · 1 line
1.9.3
Re: [Qemu-devel] [PATCH v3 6/6] vmdk: Set errp on failures in vmdk_open_vmdk4
Quoted 6 lines · Fam Zheng
Reported-by Markus Armbruster <[email protected]>Signed-off-by Fam Zheng <[email protected]>Reviewed-by Markus Armbruster <[email protected]>--- block/vmdk.c | 3 +++ 1 file changed, 3 insertions(+)
Re: [Qemu-devel] [PATCH v3 6/6] vmdk: Set errp on failures in vmdk_open_vmdk4
In reply to Fam ZhengQuoted 4 lines · On Wed, 3 Dec 2014 18:28:41 +0800 Fam Zheng <[email protected]> wrote:
Reported-by Markus Armbruster <[email protected]>Signed-off-by Fam Zheng <[email protected]>Reviewed-by Markus Armbruster <[email protected]>---
Quoted 35 lines
block/vmdk.c | 3 +++ 1 file changed, 3 insertions(+)block/vmdk.c +3
-0@@ -642,6 +642,7 @@ static int vmdk_open_vmdk4(BlockDriverState *bs, bs->file->total_sectors *512 -1536 , &footer, sizeof(footer));if (ret <0 ) { +error_setg_errno (errp, -ret,"Failed to read footer" );return ret; }@@ -653,6 +654,7 @@ static int vmdk_open_vmdk4(BlockDriverState *bs, le32_to_cpu(footer.eos_marker.size) != 0 || le32_to_cpu(footer.eos_marker.type) != MARKER_END_OF_STREAM) { +error_setg (errp,"Invalid footer" );return -EINVAL ; }@@ -683,6 +685,7 @@ static int vmdk_open_vmdk4(BlockDriverState *bs, l1_entry_sectors =le32_to_cpu (header.num_gtes_per_gt) *le64_to_cpu (header.granularity);if (l1_entry_sectors ==0 ) { +error_setg (errp,"L1 entry size is invalid" );return -EINVAL ; } l1_size = (le64_to_cpu (header.capacity) + l1_entry_sectors -1 )Signature · 1 line
1.9.3