qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] qemu-img: fix division by zero in bench_cb() for zero-sized images
@ 2025-03-18 10:19 gerben
  2025-03-18 12:03 ` Kevin Wolf
  0 siblings, 1 reply; 2+ messages in thread
From: gerben @ 2025-03-18 10:19 UTC (permalink / raw)
  To: qemu-devel, kwolf, hreitz; +Cc: sdl.qemu, Denis Rastyogin

From: Denis Rastyogin <gerben@altlinux.org>

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.

Signed-off-by: Denis Rastyogin <gerben@altlinux.org>
---
 qemu-img.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/qemu-img.c b/qemu-img.c
index 89c93c1eb5..2044c22a4c 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -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 {
-- 
2.42.2



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] qemu-img: fix division by zero in bench_cb() for zero-sized images
  2025-03-18 10:19 [PATCH] qemu-img: fix division by zero in bench_cb() for zero-sized images gerben
@ 2025-03-18 12:03 ` Kevin Wolf
  0 siblings, 0 replies; 2+ messages in thread
From: Kevin Wolf @ 2025-03-18 12:03 UTC (permalink / raw)
  To: gerben; +Cc: qemu-devel, hreitz, sdl.qemu

Am 18.03.2025 um 11:19 hat gerben@altlinux.org geschrieben:
> From: Denis Rastyogin <gerben@altlinux.org>
> 
> 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.
> 
> Signed-off-by: Denis Rastyogin <gerben@altlinux.org>

Thanks, applied to the block branch.

Kevin



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-03-18 12:04 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-18 10:19 [PATCH] qemu-img: fix division by zero in bench_cb() for zero-sized images gerben
2025-03-18 12:03 ` Kevin Wolf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).