Skip to contentSkip to conversation

vmdk: Fix overflow if l1_size is 0x20000000

PATCH Applied Fam Zheng 4 participants 4 messages May 5, 2015 – May 8, 2015 Block layer

lore
Share & tools
lore

Fam Zheng

author
#0 lore raw

PATCH 1/1 vmdk: Fix overflow if l1_size is 0x20000000

Richard Jones caught this bug with afl fuzzer.

In fact, that's the only possible value to overflow (extent->l1_size = 0x20000000) l1_size:

l1_size = extent->l1_size * sizeof(long) => 0x80000000;

g_try_malloc returns NULL because l1_size is interpreted as negative during type casting from 'int' to 'gsize', which yields a enormous value. Hence, by coincidence, we get a "not too bad" behavior:

qemu-img: Could not open '/tmp/afl6.img': Could not open '/tmp/afl6.img': Cannot allocate memory

Values larger than 0x20000000 will be refused by the validation in vmdk_add_extent.

Values smaller than 0x20000000 will not overflow l1_size.

---
 block/vmdk.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
block/vmdk.c +2 -1
index 1c5e2ef..e095156 100644
@@ -451,7 +451,8 @@ static int vmdk_init_tables(BlockDriverState *bs, VmdkExtent *extent,
                             Error **errp)
 {
     int ret;
-    int l1_size, i;
+    size_t l1_size;
+    int i;
 
     /* read the L1 table */
     l1_size = extent->l1_size * sizeof(uint32_t);
Signature · 1 line
1.9.3
Tested-by on v1 1/1
#1 lore raw
Quoted 14 lines · Fam Zheng

Richard Jones caught this bug with afl fuzzer.

In fact, that's the only possible value to overflow (extent->l1_size = 0x20000000) l1_size:

l1_size = extent->l1_size * sizeof(long) => 0x80000000;

g_try_malloc returns NULL because l1_size is interpreted as negative during type casting from 'int' to 'gsize', which yields a enormous value. Hence, by coincidence, we get a "not too bad" behavior:

qemu-img: Could not open '/tmp/afl6.img': Could not open '/tmp/afl6.img': Cannot allocate memory

Quoted 7 lines

Values larger than 0x20000000 will be refused by the validation in vmdk_add_extent.

Values smaller than 0x20000000 will not overflow l1_size.

ACK, and:

Rich.

Quoted 19 lines
 block/vmdk.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
block/vmdk.c +2 -1
index 1c5e2ef..e095156 100644
@@ -451,7 +451,8 @@ static int vmdk_init_tables(BlockDriverState *bs, VmdkExtent *extent,
                             Error **errp)
 {
     int ret;
-    int l1_size, i;
+    size_t l1_size;
+    int i;
 
     /* read the L1 table */
     l1_size = extent->l1_size * sizeof(uint32_t);
Signature · 1 line
1.9.3
Signature · 5 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
Fedora Windows cross-compiler. Compile Windows programs, test, and
build Windows installers. Over 100 libraries supported.
http://fedoraproject.org/wiki/MinGW
Reviewed-by on v1 1/1
#2 lore raw
In reply to Fam Zheng
Quoted 17 lines · Fam Zheng

Richard Jones caught this bug with afl fuzzer.

In fact, that's the only possible value to overflow (extent->l1_size = 0x20000000) l1_size:

l1_size = extent->l1_size * sizeof(long) => 0x80000000;

g_try_malloc returns NULL because l1_size is interpreted as negative during type casting from 'int' to 'gsize', which yields a enormous value. Hence, by coincidence, we get a "not too bad" behavior:

qemu-img: Could not open '/tmp/afl6.img': Could not open '/tmp/afl6.img': Cannot allocate memory

Values larger than 0x20000000 will be refused by the validation in vmdk_add_extent.

Quoted 7 lines

Values smaller than 0x20000000 will not overflow l1_size.

---
  block/vmdk.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)

Okay, so because size_t is unsigned, this will not overflow on systems with sizeof(size_t) == 4 either.

Applied
#3 lore raw

Am 06.05.2015 um 19:50 hat Max Reitz geschrieben:

Quoted 26 lines · Fam Zheng
Quoted 17 lines

Richard Jones caught this bug with afl fuzzer.

In fact, that's the only possible value to overflow (extent->l1_size = 0x20000000) l1_size:

l1_size = extent->l1_size * sizeof(long) => 0x80000000;

g_try_malloc returns NULL because l1_size is interpreted as negative during type casting from 'int' to 'gsize', which yields a enormous value. Hence, by coincidence, we get a "not too bad" behavior:

qemu-img: Could not open '/tmp/afl6.img': Could not open '/tmp/afl6.img': Cannot allocate memory

Values larger than 0x20000000 will be refused by the validation in vmdk_add_extent.

Quoted 7 lines

Values smaller than 0x20000000 will not overflow l1_size.

---
 block/vmdk.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 4 lines

Okay, so because size_t is unsigned, this will not overflow on systems with sizeof(size_t) == 4 either.

Thanks, applied to the block branch. I think this one is for stable, too.

Kevin