Skip to contentSkip to conversation

qemu-img: fix offset calculation in bench

PATCH Applied [email protected] 2 participants 2 messages May 6, 2025 – May 20, 2025

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 qemu-img: fix offset calculation in bench

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 | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
qemu-img.c +2 -2
index 76ac5d3028..e64acfafb3 100644
@@ -4488,10 +4488,10 @@ static void bench_cb(void *opaque, int ret)
          */
         b->in_flight++;
         b->offset += b->step;
-        if (b->image_size == 0) {
+        if (b->image_size - b->bufsize <= 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
Applied
#1 lore raw

Am 06.05.2025 um 16:13 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: 4096

This fix ensures that offset remains within a valid range.

---
 qemu-img.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
qemu-img.c +0 -0
index 76ac5d3028..e64acfafb3 100644

qemu-img.c static void bench_cb(void *opaque, int ret) · line 4491

Quoted 5 lines
qemu-img.c +1 -1
@@ -4488,10 +4488,10 @@ static void bench_cb(void *opaque, int ret)
          */
         b->in_flight++;
         b->offset += b->step;
-        if (b->image_size == 0) {
+        if (b->image_size - b->bufsize <= 0) {

The result of b->image_size - b->bufsize is unsigned, so this doesn't do what it looks like. I'm replacing it with b->image_size <= b->bufsize.

qemu-img.c static void bench_cb(void *opaque, int ret) · line 4497

Quoted 7 lines
qemu-img.c +1 -1
             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);

Thanks, applied to the block branch.

Kevin