From nobody Wed Apr 2 13:43:53 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 174309277143896.72352772247132; Thu, 27 Mar 2025 09:26:11 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1txq22-00022x-Ha; Thu, 27 Mar 2025 12:24:38 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1txq20-00022G-HG; Thu, 27 Mar 2025 12:24:36 -0400 Received: from air.basealt.ru ([193.43.8.18]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1txq1y-00041z-Pn; Thu, 27 Mar 2025 12:24:36 -0400 Received: from boringlust.malta.altlinux.ru (obninsk.basealt.ru [217.15.195.17]) (Authenticated sender: rastyoginds) by air.basealt.ru (Postfix) with ESMTPSA id 7521123383; Thu, 27 Mar 2025 19:24:31 +0300 (MSK) From: gerben@altlinux.org To: qemu-block@nongnu.org, kwolf@redhat.com, hreitz@redhat.com Cc: qemu-devel@nongnu.org, sdl.qemu@linuxtesting.org, Denis Rastyogin Subject: [PATCH 1/4] qemu-img: fix division by zero in bench_cb() for zero-sized Date: Thu, 27 Mar 2025 19:24:20 +0300 Message-ID: <20250327162423.25154-2-gerben@altlinux.org> X-Mailer: git-send-email 2.42.2 In-Reply-To: <20250327162423.25154-1-gerben@altlinux.org> References: <20250327162423.25154-1-gerben@altlinux.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=193.43.8.18; envelope-from=gerben@altlinux.org; helo=air.basealt.ru X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1743092772240019000 Content-Type: text/plain; charset="utf-8" From: Denis Rastyogin 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 %=3D b->image_size). This patch adds a check for b->image_size =3D=3D 0 and resets b->offset to 0 in such cases, preventing the error. Signed-off-by: Denis Rastyogin --- 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 +=3D b->step; - b->offset %=3D b->image_size; + if (b->image_size =3D=3D 0) { + b->offset =3D 0; + } else { + b->offset %=3D b->image_size; + } if (b->write) { acb =3D blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, = b); } else { --=20 2.42.2 From nobody Wed Apr 2 13:43:53 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 174309273209849.412455494614505; Thu, 27 Mar 2025 09:25:32 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1txq2C-0002AO-5m; Thu, 27 Mar 2025 12:24:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1txq2A-00028l-5L; Thu, 27 Mar 2025 12:24:46 -0400 Received: from air.basealt.ru ([193.43.8.18]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1txq28-00042z-BE; Thu, 27 Mar 2025 12:24:45 -0400 Received: from boringlust.malta.altlinux.ru (obninsk.basealt.ru [217.15.195.17]) (Authenticated sender: rastyoginds) by air.basealt.ru (Postfix) with ESMTPSA id 53C9323383; Thu, 27 Mar 2025 19:24:42 +0300 (MSK) From: gerben@altlinux.org To: qemu-block@nongnu.org, kwolf@redhat.com, hreitz@redhat.com Cc: qemu-devel@nongnu.org, sdl.qemu@linuxtesting.org, Denis Rastyogin Subject: [PATCH 2/4] qemu-img: fix offset calculation in bench Date: Thu, 27 Mar 2025 19:24:21 +0300 Message-ID: <20250327162423.25154-3-gerben@altlinux.org> X-Mailer: git-send-email 2.42.2 In-Reply-To: <20250327162423.25154-1-gerben@altlinux.org> References: <20250327162423.25154-1-gerben@altlinux.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=193.43.8.18; envelope-from=gerben@altlinux.org; helo=air.basealt.ru X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1743092735401019100 Content-Type: text/plain; charset="utf-8" From: Denis Rastyogin 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. Signed-off-by: Denis Rastyogin --- qemu-img.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qemu-img.c b/qemu-img.c index 2044c22a4c..71c9fe496f 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -4491,7 +4491,7 @@ static void bench_cb(void *opaque, int ret) if (b->image_size =3D=3D 0) { b->offset =3D 0; } else { - b->offset %=3D b->image_size; + b->offset %=3D b->image_size - b->bufsize; } if (b->write) { acb =3D blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, = b); --=20 2.42.2 From nobody Wed Apr 2 13:43:53 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1743092731822575.5258305603141; Thu, 27 Mar 2025 09:25:31 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1txq2I-0002Bc-NE; Thu, 27 Mar 2025 12:24:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1txq2H-0002B7-QU; Thu, 27 Mar 2025 12:24:53 -0400 Received: from air.basealt.ru ([193.43.8.18]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1txq2B-00043A-41; Thu, 27 Mar 2025 12:24:53 -0400 Received: from boringlust.malta.altlinux.ru (obninsk.basealt.ru [217.15.195.17]) (Authenticated sender: rastyoginds) by air.basealt.ru (Postfix) with ESMTPSA id 57C8023382; Thu, 27 Mar 2025 19:24:44 +0300 (MSK) From: gerben@altlinux.org To: qemu-block@nongnu.org, kwolf@redhat.com, hreitz@redhat.com Cc: qemu-devel@nongnu.org, sdl.qemu@linuxtesting.org, Denis Rastyogin , Vasiliy Kovalev Subject: [PATCH 3/4] qemu-img: prevent stack overflow in bench by using bottom half Date: Thu, 27 Mar 2025 19:24:22 +0300 Message-ID: <20250327162423.25154-4-gerben@altlinux.org> X-Mailer: git-send-email 2.42.2 In-Reply-To: <20250327162423.25154-1-gerben@altlinux.org> References: <20250327162423.25154-1-gerben@altlinux.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=193.43.8.18; envelope-from=gerben@altlinux.org; helo=air.basealt.ru X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1743092735397019000 Content-Type: text/plain; charset="utf-8" From: Denis Rastyogin 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. Signed-off-by: Vasiliy Kovalev Signed-off-by: Denis Rastyogin --- qemu-img.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/qemu-img.c b/qemu-img.c index 71c9fe496f..5cbf3d18d7 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -4426,6 +4426,7 @@ typedef struct BenchData { int in_flight; bool in_flush; uint64_t offset; + QEMUBH *bh; } BenchData; =20 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); + } +} =20 +static void bench_bh(void *opaque) +{ + BenchData *b =3D opaque; + BlockAIOCB *acb; + =20 while (b->n > b->in_flight && b->in_flight < b->nrreq) { int64_t offset =3D 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) } =20 gettimeofday(&t1, NULL); + data.bh =3D qemu_bh_new(bench_bh, &data); bench_cb(&data, 0); =20 while (data.n > 0) { @@ -4755,6 +4766,9 @@ out: qemu_vfree(data.buf); blk_unref(blk); =20 + if (data.bh) { + qemu_bh_delete(data.bh); + } if (ret) { return 1; } --=20 2.42.2 From nobody Wed Apr 2 13:43:53 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1743092778388615.8940327168532; Thu, 27 Mar 2025 09:26:18 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1txq2L-0002C4-2H; Thu, 27 Mar 2025 12:24:57 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1txq2I-0002BB-0n; Thu, 27 Mar 2025 12:24:54 -0400 Received: from air.basealt.ru ([193.43.8.18]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1txq2F-00043a-Eo; Thu, 27 Mar 2025 12:24:53 -0400 Received: from boringlust.malta.altlinux.ru (obninsk.basealt.ru [217.15.195.17]) (Authenticated sender: rastyoginds) by air.basealt.ru (Postfix) with ESMTPSA id 771E023383; Thu, 27 Mar 2025 19:24:49 +0300 (MSK) From: gerben@altlinux.org To: qemu-block@nongnu.org, kwolf@redhat.com, hreitz@redhat.com Cc: qemu-devel@nongnu.org, sdl.qemu@linuxtesting.org, Denis Rastyogin Subject: [PATCH 4/4] qemu-img: improve queue depth validation in img_bench Date: Thu, 27 Mar 2025 19:24:23 +0300 Message-ID: <20250327162423.25154-5-gerben@altlinux.org> X-Mailer: git-send-email 2.42.2 In-Reply-To: <20250327162423.25154-1-gerben@altlinux.org> References: <20250327162423.25154-1-gerben@altlinux.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=193.43.8.18; envelope-from=gerben@altlinux.org; helo=air.basealt.ru X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1743092781147019000 Content-Type: text/plain; charset="utf-8" From: Denis Rastyogin 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. Signed-off-by: Denis Rastyogin --- qemu-img.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qemu-img.c b/qemu-img.c index 5cbf3d18d7..4817bd9b05 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -4581,7 +4581,7 @@ static int img_bench(int argc, char **argv) { unsigned long res; =20 - if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { + if (qemu_strtoul(optarg, NULL, 0, &res) <=3D 0 || res > INT_MA= X) { error_report("Invalid queue depth specified"); return 1; } --=20 2.42.2