Skip to contentSkip to conversation

allow reading variable size vmdk descriptor files

PATCH Superseded Evgeny Budilovsky 3 participants 8 messages Jun 12, 2013 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 allow reading variable size vmdk descriptor files

The hard-coded 2k buffer on the stack won't allow reading big descriptor files which can be generated when storing big images (For example 500G vmdk splitted to 2G chunks).

---
 block/vmdk.c |   28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)
block/vmdk.c +21 -7
index 608daaf..1bc944b 100644
@@ -719,27 +719,41 @@ static int vmdk_open_desc_file(BlockDriverState *bs, int flags,
                                int64_t desc_offset)
 {
     int ret;
-    char buf[2048];
+    char *buf = NULL;
     char ct[128];
     BDRVVmdkState *s = bs->opaque;
+    int64_t size;
 
-    ret = bdrv_pread(bs->file, desc_offset, buf, sizeof(buf));
+    size = bdrv_get_allocated_file_size(bs);
+    if (size < 0) {
+        return -EINVAL;
+    }
+
+    buf = g_malloc0(size+1);
+
+    ret = bdrv_pread(bs->file, desc_offset, buf, size);
     if (ret < 0) {
-        return ret;
+        goto exit;
     }
-    buf[2047] = '\0';
     if (vmdk_parse_description(buf, "createType", ct, sizeof(ct))) {
-        return -EMEDIUMTYPE;
+        ret = -EMEDIUMTYPE;
+        goto exit;
     }
     if (strcmp(ct, "monolithicFlat") &&
         strcmp(ct, "twoGbMaxExtentSparse") &&
         strcmp(ct, "twoGbMaxExtentFlat")) {
         fprintf(stderr,
                 "VMDK: Not supported image type \"%s\""".\n", ct);
-        return -ENOTSUP;
+        ret = -ENOTSUP;
+        goto exit;
     }
     s->desc_offset = 0;
-    return vmdk_parse_extents(buf, bs, bs->file->filename);
+    ret = vmdk_parse_extents(buf, bs, bs->file->filename);
+exit:
+    if (buf) {
+        g_free(buf);
+    }
+    return ret;
 }
 
 static int vmdk_open(BlockDriverState *bs, QDict *options, int flags)

--
1.7.9.5

#1 lore raw
Quoted 6 lines · Evgeny Budilovsky
block/vmdk.c +0 -0
index 608daaf..1bc944b 100644
@@ -719,27 +719,41 @@ static int vmdk_open_desc_file(BlockDriverState *bs, int flags,
                                int64_t desc_offset)

block/vmdk.c static int vmdk_open_desc_file(BlockDriverState *bs, int flags, · line 727

Quoted 10 lines
block/vmdk.c +3 -2
 {
     int ret;
-    char buf[2048];
+    char *buf = NULL;
     char ct[128];
     BDRVVmdkState *s = bs->opaque;
+    int64_t size;
 
-    ret = bdrv_pread(bs->file, desc_offset, buf, sizeof(buf));
+    size = bdrv_get_allocated_file_size(bs);

Please use bdrv_getlength() instead of bdrv_get_allocated_file_size(), which checks stat.st_blocks. From the stat(2) man page:

The st_blocks field indicates the number of blocks allocated to the file, 512-byte units. (This may be smaller than st_size/512 when the file has holes.).

block/vmdk.c static int vmdk_open_desc_file(BlockDriverState *bs, int flags, · line 732

Quoted 5 lines
block/vmdk.c +5 -0
+    if (size < 0) {
+        return -EINVAL;
+    }
+
+    buf = g_malloc0(size+1);

Spaces please:
g_malloc0(size + 1)

block/vmdk.c static int vmdk_open_desc_file(BlockDriverState *bs, int flags, · line 730

Quoted 7 lines
block/vmdk.c +3 -2
+
+    ret = bdrv_pread(bs->file, desc_offset, buf, size);
     if (ret < 0) {
-        return ret;
+        goto exit;
     }
-    buf[2047] = '\0';

The buffer must be NUL-terminated.

Quoted 11 lines
block/vmdk.c +2 -2
     if (vmdk_parse_description(buf, "createType", ct, sizeof(ct))) {
-        return -EMEDIUMTYPE;
+        ret = -EMEDIUMTYPE;
+        goto exit;
     }
     if (strcmp(ct, "monolithicFlat") &&
         strcmp(ct, "twoGbMaxExtentSparse") &&
         strcmp(ct, "twoGbMaxExtentFlat")) {
         fprintf(stderr,
                 "VMDK: Not supported image type \"%s\""".\n", ct);
-        return -ENOTSUP;

block/vmdk.c static int vmdk_open_desc_file(BlockDriverState *bs, int flags, · line 755

Quoted 10 lines
block/vmdk.c +7 -1
+        ret = -ENOTSUP;
+        goto exit;
     }
     s->desc_offset = 0;
-    return vmdk_parse_extents(buf, bs, bs->file->filename);
+    ret = vmdk_parse_extents(buf, bs, bs->file->filename);
+exit:
+    if (buf) {
+        g_free(buf);
+    }

The if is not necessary since g_free(NULL) is a nop.

author Applied
#2 lore raw
Quoted 47 lines · Stefan Hajnoczi

block/vmdk.c static int vmdk_open_desc_file(BlockDriverState

Quoted 5 lines · Evgeny Budilovsky
block/vmdk.c +0 -0
index 608daaf..1bc944b 100644
@@ -719,27 +719,41 @@ static int vmdk_open_desc_file(BlockDriverState

*bs, int flags,

block/vmdk.c static int vmdk_open_desc_file(BlockDriverState *bs, int flags, · line 727

Quoted 11 lines
block/vmdk.c +3 -2
                                int64_t desc_offset)
 {
     int ret;
-    char buf[2048];
+    char *buf = NULL;
     char ct[128];
     BDRVVmdkState *s = bs->opaque;
+    int64_t size;
 
-    ret = bdrv_pread(bs->file, desc_offset, buf, sizeof(buf));
+    size = bdrv_get_allocated_file_size(bs);

Please use bdrv_getlength() instead of bdrv_get_allocated_file_size(), which checks stat.st_blocks. From the stat(2) man page:

The st_blocks field indicates the number of blocks allocated to the file, 512-byte units. (This may be smaller than st_size/512 when the file has holes.).

applied

block/vmdk.c static int vmdk_open_desc_file(BlockDriverState *bs, int flags, · line 732

Quoted 5 lines
block/vmdk.c +5 -0
+    if (size < 0) {
+        return -EINVAL;
+    }
+
+    buf = g_malloc0(size+1);

Spaces please:
g_malloc0(size + 1)

applied

block/vmdk.c static int vmdk_open_desc_file(BlockDriverState *bs, int flags, · line 730

Quoted 7 lines
block/vmdk.c +3 -2
+
+    ret = bdrv_pread(bs->file, desc_offset, buf, size);
     if (ret < 0) {
-        return ret;
+        goto exit;
     }
-    buf[2047] = '\0';
Quoted 3 lines

The buffer must be NUL-terminated.

g_malloc0 allocates buffer which is zero initialized so I can skeep the

null termination

Quoted 22 lines
Quoted 11 lines
block/vmdk.c +2 -2
     if (vmdk_parse_description(buf, "createType", ct, sizeof(ct))) {
-        return -EMEDIUMTYPE;
+        ret = -EMEDIUMTYPE;
+        goto exit;
     }
     if (strcmp(ct, "monolithicFlat") &&
         strcmp(ct, "twoGbMaxExtentSparse") &&
         strcmp(ct, "twoGbMaxExtentFlat")) {
         fprintf(stderr,
                 "VMDK: Not supported image type \"%s\""".\n", ct);
-        return -ENOTSUP;

block/vmdk.c static int vmdk_open_desc_file(BlockDriverState *bs, int flags, · line 755

Quoted 10 lines
block/vmdk.c +7 -1
+        ret = -ENOTSUP;
+        goto exit;
     }
     s->desc_offset = 0;
-    return vmdk_parse_extents(buf, bs, bs->file->filename);
+    ret = vmdk_parse_extents(buf, bs, bs->file->filename);
+exit:
+    if (buf) {
+        g_free(buf);
+    }
Quoted 2 lines

The if is not necessary since g_free(NULL) is a nop.

applied

#3 lore raw
In reply to Evgeny Budilovsky

Am 12.06.2013 um 10:04 hat Evgeny Budilovsky geschrieben:

Quoted 20 lines

The hard-coded 2k buffer on the stack won't allow reading big descriptor files which can be generated when storing big images (For example 500G vmdk splitted to 2G chunks).

---
 block/vmdk.c |   28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)
block/vmdk.c +1 -1
index 608daaf..1bc944b 100644
@@ -719,27 +719,41 @@ static int vmdk_open_desc_file(BlockDriverState *bs, int flags,
                                int64_t desc_offset)
 {
     int ret;
-    char buf[2048];
+    char *buf = NULL;
     char ct[128];

block/vmdk.c static int vmdk_open_desc_file(BlockDriverState *bs, int flags, · line 732

Quoted 10 lines
block/vmdk.c +7 -1
     BDRVVmdkState *s = bs->opaque;
+    int64_t size;
 
-    ret = bdrv_pread(bs->file, desc_offset, buf, sizeof(buf));
+    size = bdrv_get_allocated_file_size(bs);
+    if (size < 0) {
+        return -EINVAL;
+    }
+
+    buf = g_malloc0(size+1);

This is an unbounded allocation. Not sure if this is a good idea. Can we restrict the maximum size to something reasonably small, like a megabyte?

Kevin

#4 lore raw
Quoted 33 lines · Kevin Wolf

Am 12.06.2013 um 10:04 hat Evgeny Budilovsky geschrieben:

block/vmdk.c static int vmdk_open_desc_file(BlockDriverState

Quoted 14 lines

The hard-coded 2k buffer on the stack won't allow reading big descriptor files which can be generated when storing big images (For example 500G vmdk splitted to 2G chunks).

---
 block/vmdk.c |   28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)
block/vmdk.c +0 -0
index 608daaf..1bc944b 100644
@@ -719,27 +719,41 @@ static int vmdk_open_desc_file(BlockDriverState

*bs, int flags,

Quoted 6 lines
block/vmdk.c +1 -1
                                int64_t desc_offset)
 {
     int ret;
-    char buf[2048];
+    char *buf = NULL;
     char ct[128];

block/vmdk.c static int vmdk_open_desc_file(BlockDriverState *bs, int flags, · line 732

Quoted 10 lines
block/vmdk.c +7 -1
     BDRVVmdkState *s = bs->opaque;
+    int64_t size;
 
-    ret = bdrv_pread(bs->file, desc_offset, buf, sizeof(buf));
+    size = bdrv_get_allocated_file_size(bs);
+    if (size < 0) {
+        return -EINVAL;
+    }
+
+    buf = g_malloc0(size+1);
Quoted 5 lines

This is an unbounded allocation. Not sure if this is a good idea. Can we restrict the maximum size to something reasonably small, like a megabyte?

Kevin

yes good idea !

Signature · 2 lines
Best Regards,
Evgeny
#5 lore raw

PATCH v2 1/1 allow reading variable size vmdk descriptor files

In reply to Evgeny Budilovsky

the hard-coded 2k buffer on the stack won't allow reading big descriptor files which can be generated when storing big images. For example 500G vmdk splitted to 2G chunks.

---
 block/vmdk.c |   27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)
block/vmdk.c +20 -7
index 608daaf..751b63b 100644
@@ -719,27 +719,40 @@ static int vmdk_open_desc_file(BlockDriverState *bs, int flags,
                                int64_t desc_offset)
 {
     int ret;
-    char buf[2048];
+    char *buf = NULL;
     char ct[128];
     BDRVVmdkState *s = bs->opaque;
+    int64_t size;
 
-    ret = bdrv_pread(bs->file, desc_offset, buf, sizeof(buf));
+    size = bdrv_getlength(bs->file);
+    if (size < 0) {
+        return -EINVAL;
+    }
+
+    size = MIN(size, 1 << 20);  /* avoid unbounded allocation */
+    buf = g_malloc0(size + 1);
+
+    ret = bdrv_pread(bs->file, desc_offset, buf, size);
     if (ret < 0) {
-        return ret;
+        goto exit;
     }
-    buf[2047] = '\0';
     if (vmdk_parse_description(buf, "createType", ct, sizeof(ct))) {
-        return -EMEDIUMTYPE;
+        ret = -EMEDIUMTYPE;
+        goto exit;
     }
     if (strcmp(ct, "monolithicFlat") &&
         strcmp(ct, "twoGbMaxExtentSparse") &&
         strcmp(ct, "twoGbMaxExtentFlat")) {
         fprintf(stderr,
                 "VMDK: Not supported image type \"%s\""".\n", ct);
-        return -ENOTSUP;
+        ret = -ENOTSUP;
+        goto exit;
     }
     s->desc_offset = 0;
-    return vmdk_parse_extents(buf, bs, bs->file->filename);
+    ret = vmdk_parse_extents(buf, bs, bs->file->filename);
+exit:
+    g_free(buf);
+    return ret;
 }
 
 static int vmdk_open(BlockDriverState *bs, QDict *options, int flags)

--
1.7.9.5

Reviewed-by on v2 1/1
#6 lore raw
Quoted 7 lines · Evgeny Budilovsky
block/vmdk.c +1 -1
@@ -719,27 +719,40 @@ static int vmdk_open_desc_file(BlockDriverState *bs, int flags,
                                int64_t desc_offset)
 {
     int ret;
-    char buf[2048];
+    char *buf = NULL;
     char ct[128];

block/vmdk.c static int vmdk_open_desc_file(BlockDriverState *bs, int flags, · line 732

Quoted 10 lines
block/vmdk.c +7 -1
     BDRVVmdkState *s = bs->opaque;
+    int64_t size;
 
-    ret = bdrv_pread(bs->file, desc_offset, buf, sizeof(buf));
+    size = bdrv_getlength(bs->file);
+    if (size < 0) {
+        return -EINVAL;
+    }
+
+    size = MIN(size, 1 << 20);  /* avoid unbounded allocation */

I think this is okay, initially I was worried that this function might be called to probe files which we have not identified as descriptor files yet (they could be big!).

But looking at the callers, it should be reasonable to read up to 1 MB. (Would have been bad to read 1 MB just to check if this parses as a descriptor file.)

block/vmdk.c static int vmdk_open_desc_file(BlockDriverState *bs, int flags, · line 730

Quoted 8 lines
block/vmdk.c +4 -2
+    buf = g_malloc0(size + 1);
+
+    ret = bdrv_pread(bs->file, desc_offset, buf, size);
     if (ret < 0) {
-        return ret;
+        goto exit;
     }
-    buf[2047] = '\0';

Thanks for pointing out the g_malloc0(), I missed it.