Fix qemu-img bench issues and improve checks
cover Fix qemu-img bench issues and improve checks
From: Denis Rastyogin <[email protected]>
This series fixes several qemu-img crashes found during fuzzing.
The patch "qemu-img: fix division by zero in bench_cb() for zero-sized" was already submitted earlier:
https://lore.kernel.org/qemu-devel/[email protected]/
However, it has been included in this series because it has not yet been merged into master. Without it, the series would conflict with this commit due to modifications in the same parts of the code.
Denis Rastyogin (4):
qemu-img: fix division by zero in bench_cb() for zero-sized
qemu-img: fix offset calculation in bench
qemu-img: prevent stack overflow in bench by using bottom half
qemu-img: improve queue depth validation in img_bench
qemu-img.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-)
Signature · 1 line
2.42.2
PATCH 1/4 qemu-img: fix division by zero in bench_cb() for zero-sized
From: Denis Rastyogin <[email protected]>
This error was discovered by fuzzing qemu-img.
This commit fixes a division by zero error in the bench_cb() function that occurs when using the bench command with a zero-sized image.
The issue arises because b->image_size can be zero, leading to a division by zero in the modulo operation (b->offset %= b->image_size). This patch adds a check for b->image_size == 0 and resets b->offset to 0 in such cases, preventing the error.
--- qemu-img.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
qemu-img.c +5 -1
@@ -4488,7 +4488,11 @@ static void bench_cb(void *opaque, int ret)*/ b->in_flight++; b->offset += b->step; - b->offset %= b->image_size; +if (b->image_size ==0 ) { + b->offset =0 ; + }else { + b->offset %= b->image_size; + }if (b->write) { acb =blk_aio_pwritev (b->blk, offset, b->qiov,0 , bench_cb, b); }else {
Signature · 1 line
2.42.2
PATCH 2/4 qemu-img: fix offset calculation in bench
In reply to [email protected]From: Denis Rastyogin <[email protected]>
This error was discovered by fuzzing qemu-img.
The current offset calculation leads to an EIO error in block/block-backend.c: blk_check_byte_request():
if (offset > len || len - offset < bytes) {
return -EIO;}
This triggers the error message:
"qemu-img: Failed request: Input/output error".
Example of the issue:
offset: 260076
len: 260096
bytes: 4096
This fix ensures that offset remains within a valid range.
--- qemu-img.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
qemu-img.c +1 -1
@@ -4491,7 +4491,7 @@ static void bench_cb(void *opaque, int ret)if (b->image_size ==0 ) { b->offset =0 ; }else { - b->offset %= b->image_size; + b->offset %= b->image_size - b->bufsize; }if (b->write) { acb =blk_aio_pwritev (b->blk, offset, b->qiov,0 , bench_cb, b);
Signature · 1 line
2.42.2
Re: [PATCH 2/4] qemu-img: fix offset calculation in bench
Am 27.03.2025 um 17:24 hat [email protected] geschrieben:
Quoted 31 lines
From: Denis Rastyogin <[email protected]>
This error was discovered by fuzzing qemu-img.
The current offset calculation leads to an EIO error in
block/block-backend.c:blk_check_byte_request():if (offset > len || len - offset < bytes) { return -EIO;}
This triggers the error message:
"qemu-img: Failed request: Input/output error".Example of the issue:
offset: 260076
len: 260096
bytes: 4096This fix ensures that offset remains within a valid range.
Signed-off-by Denis Rastyogin <[email protected]>--- qemu-img.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)qemu-img.c +0
-0
qemu-img.c static void bench_cb(void *opaque, int ret) · line 4494
Quoted 5 lines
qemu-img.c +1
-1@@ -4491,7 +4491,7 @@ static void bench_cb(void *opaque, int ret)if (b->image_size ==0 ) { b->offset =0 ; }else { - b->offset %= b->image_size; + b->offset %= b->image_size - b->bufsize;
The approach makes sense in principle, but you just introduced a new division by zero here if image_size == bufsize (in this case we want to use 0 as the new offset).
We probably also don't want to allow this to become negative.
Kevin
PATCH 3/4 qemu-img: prevent stack overflow in bench by using bottom half
In reply to [email protected]From: Denis Rastyogin <[email protected]>
This error was discovered by fuzzing qemu-img.
Previously, new I/O requests were launched synchronously inside the completion callback bench_cb, leading to deep recursion and stack overflow. This patch moves the launching of new requests to a separate function bench_bh, scheduled via qemu_bh_schedule to run in the event loop context, thus unwinding the stack and preventing overflow.
--- qemu-img.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
qemu-img.c +14 -0
@@ -4426,6 +4426,7 @@ typedef struct BenchData {int in_flight;bool in_flush;uint64_t offset; +QEMUBH *bh; } BenchData;static void bench_undrained_flush_cb (void *opaque,int ret)
@@ -4479,7 +4480,16 @@ static void bench_cb(void *opaque, int ret) } } } +if (b->n > b->in_flight && b->in_flight < b->nrreq) { +qemu_bh_schedule (b->bh); + } +} +static void bench_bh (void *opaque) +{ +BenchData *b = opaque; +BlockAIOCB *acb; +while (b->n > b->in_flight && b->in_flight < b->nrreq) {int64_t offset = b->offset;/* blk_aio_* might look for completed I/Os and kick bench_cb
@@ -4737,6 +4747,7 @@ static int img_bench(int argc, char **argv) }gettimeofday (&t1,NULL ); + data.bh =qemu_bh_new (bench_bh, &data);bench_cb (&data,0 );while (data.n >0 ) {
@@ -4755,6 +4766,9 @@ out:qemu_vfree (data.buf);blk_unref (blk); +if (data.bh) { +qemu_bh_delete (data.bh); + }if (ret) {return 1 ; }
Signature · 1 line
2.42.2
Re: [PATCH 3/4] qemu-img: prevent stack overflow in bench by using bottom half
Am 27.03.2025 um 17:24 hat [email protected] geschrieben:
Quoted 4 lines
From: Denis Rastyogin <[email protected]>
This error was discovered by fuzzing qemu-img.
Quoted 8 lines
Previously, new I/O requests were launched synchronously inside the completion callback
bench_cb, leading to deep recursion and stack overflow. This patch moves the launching of new requests to a separate functionbench_bh, scheduled viaqemu_bh_scheduleto run in the event loop context, thus unwinding the stack and preventing overflow.Signed-off-by Vasiliy Kovalev <[email protected]>Signed-off-by Denis Rastyogin <[email protected]>
Normally, the callback shouldn't immediately be called, except for errors, which take a different code path anyway.
Was this tested with the null block driver or with a backend that is actually relevant in practice?
I wonder if switching qemu-img bench to coroutines would make sense. But since this is a benchmarking tool, we need to measure the performance difference from both an additional BH and from switching to coroutines compared to the current state. In case of doubt, I'd leave this unfixed, this isn't something that is run in production.
Kevin
PATCH 4/4 qemu-img: improve queue depth validation in img_bench
In reply to [email protected]From: Denis Rastyogin <[email protected]>
This error was discovered by fuzzing qemu-img.
Currently, running qemu-img bench -d 0 in img_bench is allowed, which is a pointless operation and causes qemu-img to hang.
--- qemu-img.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
qemu-img.c +1 -1
@@ -4581,7 +4581,7 @@ static int img_bench(int argc, char **argv) {unsigned long res; -if (qemu_strtoul (optarg,NULL ,0 , &res) <0 || res >INT_MAX ) { +if (qemu_strtoul (optarg,NULL ,0 , &res) <=0 || res >INT_MAX ) {error_report ("Invalid queue depth specified" );return 1 ; }
Signature · 1 line
2.42.2
Kevin Wolf
AppliedRe: [PATCH 4/4] qemu-img: improve queue depth validation in img_bench
Am 27.03.2025 um 17:24 hat [email protected] geschrieben:
Quoted 8 lines
From: Denis Rastyogin <[email protected]>
This error was discovered by fuzzing qemu-img.
Currently, running
qemu-img bench -d 0inimg_benchis allowed, which is a pointless operation and causes qemu-img to hang.Signed-off-by Denis Rastyogin <[email protected]>
This one seems obvious, so I'm already taking it.
Thanks, applied to the block branch.
Kevin