qcow2: Fix overly broad madvise()
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
@@ -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
Quoted 6 lines · Max Reitz
@mem_size and @offset are both
size_t, thus subtracting them from one another will just return a bigsize_tifmem_size< offset -- even more obvious here because the result is stored in anothersize_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.
Signed-off-by Max Reitz <[email protected]>--- 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
Quoted 8 lines · Max Reitz
@mem_size and @offset are both
size_t, thus subtracting them from one another will just return a bigsize_tifmem_size< offset -- even more obvious here because the result is stored in anothersize_t.Checking that result to be positive is therefore not sufficient to excluse the case that offset >
mem_size. Thus, we currently sometimes issue anmadvise()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.
Signed-off-by Max Reitz <[email protected]>
Oh, I guess this happens when the page size is larger than the cluster size? Otherwise I don't see how...
Berto
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 bigsize_tifmem_size< offset -- even more obvious here because the result is stored in anothersize_t.Checking that result to be positive is therefore not sufficient to excluse the case that offset >
mem_size. Thus, we currently sometimes issue anmadvise()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
Signed-off-by Max Reitz <[email protected]>Oh, I guess this happens when the page size is larger than the cluster size? Otherwise I don't see how...
Reviewed-by Alberto Garcia <[email protected]>
Yes, the test uses 512 byte clusters.
Max
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 bigsize_tifmem_size< offset -- even more obvious here because the result is stored in anothersize_t.Checking that result to be positive is therefore not sufficient to excluse the case that offset >
mem_size. Thus, we currently sometimes issue anmadvise()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.
Signed-off-by Max Reitz <[email protected]>---
block/qcow2-cache.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)block/qcow2-cache.c +1
-1@@ -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
Should have said that this is subject to the typo that Eric pointed out, of course.
Thanks,
Darren.
Quoted 43 lines · Darren Kenny
FWIW,
Reviewed-by Darren Kenny <[email protected]>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 bigsize_tifmem_size< offset -- even more obvious here because the result is stored in anothersize_t.Checking that result to be positive is therefore not sufficient to excluse the case that offset >
mem_size. Thus, we currently sometimes issue anmadvise()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.
Signed-off-by Max Reitz <[email protected]>---
block/qcow2-cache.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)block/qcow2-cache.c +1
-1@@ -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
Quoted 8 lines · Max Reitz
@mem_size and @offset are both
size_t, thus subtracting them from one another will just return a bigsize_tifmem_size< offset -- even more obvious here because the result is stored in anothersize_t.Checking that result to be positive is therefore not sufficient to excluse the case that offset >
mem_size. Thus, we currently sometimes issue anmadvise()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.
Signed-off-by Max Reitz <[email protected]>--- 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