Skip to contentSkip to conversation

qcow2: Fix overly broad madvise()

PATCH In review Max Reitz 4 participants 7 messages Nov 14, 2017 – Nov 15, 2017 Block layer

lore
Share & tools
lore

Max Reitz

author
#0 lore raw

PATCH 1/1 qcow2: Fix overly broad madvise()

@mem_size and @offset are both size_t, thus subtracting them from one another will just return a big size_t if mem_size < offset -- even more obvious here because the result is stored in another size_t.

Checking that result to be positive is therefore not sufficient to excluse the case that offset > mem_size. Thus, we currently sometimes issue an madvise() over a very large address range.

This is triggered by iotest 163, but with -m64, this does not result in tangible problems. But with -m32, this test produces three segfaults, all of which are fixed by this patch.

---
 block/qcow2-cache.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/qcow2-cache.c +1 -1
index 75746a7f43..5222a7b94d 100644
@@ -73,7 +73,7 @@ static void qcow2_cache_table_release(BlockDriverState *bs, Qcow2Cache *c,
     size_t mem_size = (size_t) s->cluster_size * num_tables;
     size_t offset = QEMU_ALIGN_UP((uintptr_t) t, align) - (uintptr_t) t;
     size_t length = QEMU_ALIGN_DOWN(mem_size - offset, align);
-    if (length > 0) {
+    if (mem_size > offset && length > 0) {
         madvise((uint8_t *) t + offset, length, MADV_DONTNEED);
     }
 #endif
Signature · 1 line
2.13.6
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Max Reitz

@mem_size and @offset are both size_t, thus subtracting them from one another will just return a big size_t if mem_size < offset -- even more obvious here because the result is stored in another size_t.

Checking that result to be positive is therefore not sufficient to excluse the case that offset > mem_size. Thus, we currently sometimes

s/excluse/exclude/

Quoted 11 lines

issue an madvise() over a very large address range.

This is triggered by iotest 163, but with -m64, this does not result in tangible problems. But with -m32, this test produces three segfaults, all of which are fixed by this patch.

---
 block/qcow2-cache.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v1 1/1
#2 lore raw
In reply to Max Reitz
Quoted 8 lines · Max Reitz

@mem_size and @offset are both size_t, thus subtracting them from one another will just return a big size_t if mem_size < offset -- even more obvious here because the result is stored in another size_t.

Checking that result to be positive is therefore not sufficient to excluse the case that offset > mem_size. Thus, we currently sometimes issue an madvise() over a very large address range.

Quoted 5 lines

This is triggered by iotest 163, but with -m64, this does not result in tangible problems. But with -m32, this test produces three segfaults, all of which are fixed by this patch.

Oh, I guess this happens when the page size is larger than the cluster size? Otherwise I don't see how...

Berto

Reviewed-by on v1 1/1
#4 lore raw
In reply to Max Reitz

FWIW,

Thanks,

Darren.

Quoted 34 lines · Max Reitz

@mem_size and @offset are both size_t, thus subtracting them from one another will just return a big size_t if mem_size < offset -- even more obvious here because the result is stored in another size_t.

Checking that result to be positive is therefore not sufficient to excluse the case that offset > mem_size. Thus, we currently sometimes issue an madvise() over a very large address range.

This is triggered by iotest 163, but with -m64, this does not result in tangible problems. But with -m32, this test produces three segfaults, all of which are fixed by this patch.

---
block/qcow2-cache.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
block/qcow2-cache.c +1 -1
index 75746a7f43..5222a7b94d 100644
@@ -73,7 +73,7 @@ static void qcow2_cache_table_release(BlockDriverState *bs, Qcow2Cache *c,
    size_t mem_size = (size_t) s->cluster_size * num_tables;
    size_t offset = QEMU_ALIGN_UP((uintptr_t) t, align) - (uintptr_t) t;
    size_t length = QEMU_ALIGN_DOWN(mem_size - offset, align);
-    if (length > 0) {
+    if (mem_size > offset && length > 0) {
        madvise((uint8_t *) t + offset, length, MADV_DONTNEED);
    }
 #endif
Signature · 1 line
2.13.6
#5 lore raw

Should have said that this is subject to the typo that Eric pointed out, of course.

Thanks,

Darren.

Quoted 43 lines · Darren Kenny

FWIW,

Thanks,

Darren.

Quoted 34 lines · Max Reitz

@mem_size and @offset are both size_t, thus subtracting them from one another will just return a big size_t if mem_size < offset -- even more obvious here because the result is stored in another size_t.

Checking that result to be positive is therefore not sufficient to excluse the case that offset > mem_size. Thus, we currently sometimes issue an madvise() over a very large address range.

This is triggered by iotest 163, but with -m64, this does not result in tangible problems. But with -m32, this test produces three segfaults, all of which are fixed by this patch.

---
block/qcow2-cache.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
block/qcow2-cache.c +1 -1
index 75746a7f43..5222a7b94d 100644
@@ -73,7 +73,7 @@ static void qcow2_cache_table_release(BlockDriverState *bs, Qcow2Cache *c,
   size_t mem_size = (size_t) s->cluster_size * num_tables;
   size_t offset = QEMU_ALIGN_UP((uintptr_t) t, align) - (uintptr_t) t;
   size_t length = QEMU_ALIGN_DOWN(mem_size - offset, align);
-    if (length > 0) {
+    if (mem_size > offset && length > 0) {
       madvise((uint8_t *) t + offset, length, MADV_DONTNEED);
   }
 #endif
Signature · 1 line
2.13.6
author
#3 lore raw
In reply to Alberto Garcia
Quoted 13 lines · Alberto Garcia
Quoted 12 lines · Max Reitz

@mem_size and @offset are both size_t, thus subtracting them from one another will just return a big size_t if mem_size < offset -- even more obvious here because the result is stored in another size_t.

Checking that result to be positive is therefore not sufficient to excluse the case that offset > mem_size. Thus, we currently sometimes issue an madvise() over a very large address range.

This is triggered by iotest 163, but with -m64, this does not result in tangible problems. But with -m32, this test produces three segfaults, all of which are fixed by this patch.

Quoted 6 lines
Quoted 1 line

Oh, I guess this happens when the page size is larger than the cluster size? Otherwise I don't see how...

Yes, the test uses 512 byte clusters.

Max

author
#6 lore raw
In reply to Max Reitz
Quoted 8 lines · Max Reitz

@mem_size and @offset are both size_t, thus subtracting them from one another will just return a big size_t if mem_size < offset -- even more obvious here because the result is stored in another size_t.

Checking that result to be positive is therefore not sufficient to excluse the case that offset > mem_size. Thus, we currently sometimes issue an madvise() over a very large address range.

Quoted 8 lines

This is triggered by iotest 163, but with -m64, this does not result in tangible problems. But with -m32, this test produces three segfaults, all of which are fixed by this patch.

---
 block/qcow2-cache.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Fixed the typo and applied to my block branch:

https://github.com/XanClic/qemu/commits/block

Max