From nobody Sun Sep 28 17:00:22 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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; dmarc=fail(p=none dis=none) header.from=ilande.co.uk Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1758803442600462.8343465910183; Thu, 25 Sep 2025 05:30:42 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1v1l5x-00053r-14; Thu, 25 Sep 2025 08:29:09 -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 1v1l5u-00053Y-1v for qemu-devel@nongnu.org; Thu, 25 Sep 2025 08:29:06 -0400 Received: from mail.ilande.co.uk ([2001:41c9:1:41f::167]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1v1l5q-00005r-SO for qemu-devel@nongnu.org; Thu, 25 Sep 2025 08:29:05 -0400 Received: from [217.155.175.48] (helo=cheesecake.fritz.box) by mail.ilande.co.uk with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1v1l3h-0008Tc-2X; Thu, 25 Sep 2025 13:26:49 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=ilande.co.uk; s=20220518; h=Subject:Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:To:From:Sender:Reply-To:Cc: Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=yzR378beEnouElcu9qJJzaLyoNzQwCHIKwblljupTIQ=; b=JAjQXQsShvDtmZ4VlWOYEl2BPZ LTAYeHQdXLe8j3rrsY1KvwNLkfmR3VK+5dVT+jEHWTFUysLb68Qo/Gj//0RUzGsQbYcFwM9rqrk9I 8FKQ+2U9BsJnhwV1SlYWkzTtgd+V1V/Ohn6Rmv3BKQFMQBhl7zvn66f8Cb4q+1/T+VPJu/DkoCvZh 6FPaunZVfznmHlHLpva8kKNlo/Mw6XB4cixXUaDmfBbcfY1Rxx0Ucf/aF3QZlEjZHS+IVoMBnU/9/ EJAkE+xk/QjCTlFkdr4OgZj6CID7VANWtOSDBb8wlVZP+6h41qNYWagIZToFjnR40IhcSkrWMMSw+ eRHwjmKe4JTDNRS6ehRzH94ig2iH8ZGJ2eVIYSuUQcCi+V9Rp8qmsIkzkEqBLHkwGII4i8R+1A+1i Rr92OaU1RBm7H+1ash4jgM/fMyKyIKx/jznzBS49rFH5A/zWOyZwu5mg7i20o0GknGlUTN0Sb3sP5 xaTJf7o+HmHONjZ786pM5gg72nN0MSB90irCiZtUYRGEyPotnuJMHAFjdtSsKwylY5aJUoSZpWbuj 1WkkYQz5Z9BFyQaCWb7gnnQCrRaWazM4cBTMKotQEyztaYPQHnPZP3RADXkemO/Akpul1Hhcq8FxR SnOtnpgvWpCaHh+eXHnY64+VDEfDgn/ZBPSIrnNWs=; From: Mark Cave-Ayland To: pbonzini@redhat.com, fam@euphon.net, farosas@suse.de, lvivier@redhat.com, qemu-devel@nongnu.org Date: Thu, 25 Sep 2025 13:28:45 +0100 Message-Id: <20250925122846.527615-2-mark.cave-ayland@ilande.co.uk> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250925122846.527615-1-mark.cave-ayland@ilande.co.uk> References: <20250925122846.527615-1-mark.cave-ayland@ilande.co.uk> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-SA-Exim-Connect-IP: 217.155.175.48 X-SA-Exim-Mail-From: mark.cave-ayland@ilande.co.uk Subject: [PATCH 1/2] esp.c: fix esp_cdb_ready() FIFO wraparound limit calculation X-SA-Exim-Version: 4.2.1 (built Wed, 08 May 2019 21:11:16 +0000) X-SA-Exim-Scanned: Yes (on mail.ilande.co.uk) 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=2001:41c9:1:41f::167; envelope-from=mark.cave-ayland@ilande.co.uk; helo=mail.ilande.co.uk X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, 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-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1758803444145116600 Content-Type: text/plain; charset="utf-8" The original calculation in commit 3cc70889a3 ("esp.c: prevent cmdfifo over= flow in esp_cdb_ready()") subtracted cmdfifo_cdb_offset from fifo8_num_used() to calculate the outstanding cmdfifo length, but this is incorrect because fifo8_num_used() can also include wraparound data. Instead calculate the maximum offset used by scsi_cdb_length() which is just the first byte after cmdfifo_cdb_offset, and then peek the entire content of the cmdfifo. The fifo8_peek_bufptr() result will then return the maximum length of remaining data up to the end of the internal cmdfifo array, which can then be used for the overflow check. Signed-off-by: Mark Cave-Ayland Fixes: 3cc70889a3 ("esp.c: prevent cmdfifo overflow in esp_cdb_ready()") Resolves: https://gitlab.com/qemu-project/qemu/-/issues/3082 Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- hw/scsi/esp.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/hw/scsi/esp.c b/hw/scsi/esp.c index 1d264c40e5..2809fcdee0 100644 --- a/hw/scsi/esp.c +++ b/hw/scsi/esp.c @@ -447,7 +447,9 @@ static void write_response(ESPState *s) =20 static bool esp_cdb_ready(ESPState *s) { - int len =3D fifo8_num_used(&s->cmdfifo) - s->cmdfifo_cdb_offset; + /* scsi_cdb_length() only reads the first byte */ + int limit =3D s->cmdfifo_cdb_offset + 1; + int len =3D fifo8_num_used(&s->cmdfifo); const uint8_t *pbuf; uint32_t n; int cdblen; @@ -457,7 +459,7 @@ static bool esp_cdb_ready(ESPState *s) } =20 pbuf =3D fifo8_peek_bufptr(&s->cmdfifo, len, &n); - if (n < len) { + if (n < limit) { /* * In normal use the cmdfifo should never wrap, but include this c= heck * to prevent a malicious guest from reading past the end of the --=20 2.39.5 From nobody Sun Sep 28 17:00:22 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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; dmarc=fail(p=none dis=none) header.from=ilande.co.uk Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1758803442121910.3364679937524; Thu, 25 Sep 2025 05:30:42 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1v1l60-00054i-Bg; Thu, 25 Sep 2025 08:29:12 -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 1v1l5x-00054G-Gw for qemu-devel@nongnu.org; Thu, 25 Sep 2025 08:29:09 -0400 Received: from mail.ilande.co.uk ([2001:41c9:1:41f::167]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1v1l5r-00005q-Ou for qemu-devel@nongnu.org; Thu, 25 Sep 2025 08:29:09 -0400 Received: from [217.155.175.48] (helo=cheesecake.fritz.box) by mail.ilande.co.uk with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1v1l3h-0008Tc-IT; Thu, 25 Sep 2025 13:26:49 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=ilande.co.uk; s=20220518; h=Subject:Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:To:From:Sender:Reply-To:Cc: Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=MULB+DIJQhKdzn2/TWpCjrhJkhbPGOGvv2er1hdeWcw=; b=zGgZMSpL7YaR4xxond6UX2Bpmb ETT9FeWbQugfA+QxoaSeuiVJ0QPqTXp9dgAY0GKMIfG4BIrRhb3VmlLXCeDrw5a4eEZGeQ50ix02e 5BWEAwO16FohppUNwaZ72CSZysCMXOa6MQxCkuGBmkYcRnY3+cb3Ccf/RcawE1DRU6FqkIzS/6Mwu hprhVtbjFpDFdwOmo72fZaIkIn47sYiWZ6xG7P5W91upefhGk6zlrjPLanx5sqx9MWJg/PKZ0Cbs7 tcnw3m7NqonbkuN7P2YiMudUOH8uJCRCi7WiztO1+7htcmoc8IqtHnZFKtwaPyKVxdKLN1wauRb8a rAUAddW3+4Ev4rb3ssKCB2L52PXw4VEBt6i5l3sU2TmN5TThzy5gQSPWxXgaZqeceAlwBOZiwLvwU h8MS1nU5VEZNbCLBqsuXlqQWUQ+b4mRMR+aowdxBdMuMmN2JOdgcWkb1nPDhXpYlUJVqS8sws+PqZ te1dfBnCa+X0XkQRETEiR1vXt4pj6IhTtC2WEJksZPd7fd/mf4ids6JFMv+YMtXIV7lQ3Gqetqmay C1sBUinS6MLXXukGhuZR2gxACs7PDPQAF09Wv09gCB7xh6DI1qnznkDVRFQJHPJLCffVs1qPgLyqw WsO/FHVM7ttlDmf+FEZEOAa8dpi6Hr8xggnr5aJgc=; From: Mark Cave-Ayland To: pbonzini@redhat.com, fam@euphon.net, farosas@suse.de, lvivier@redhat.com, qemu-devel@nongnu.org Date: Thu, 25 Sep 2025 13:28:46 +0100 Message-Id: <20250925122846.527615-3-mark.cave-ayland@ilande.co.uk> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250925122846.527615-1-mark.cave-ayland@ilande.co.uk> References: <20250925122846.527615-1-mark.cave-ayland@ilande.co.uk> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-SA-Exim-Connect-IP: 217.155.175.48 X-SA-Exim-Mail-From: mark.cave-ayland@ilande.co.uk Subject: [PATCH 2/2] qtest/am53c974-test: add additional test for cmdfifo overflow X-SA-Exim-Version: 4.2.1 (built Wed, 08 May 2019 21:11:16 +0000) X-SA-Exim-Scanned: Yes (on mail.ilande.co.uk) 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=2001:41c9:1:41f::167; envelope-from=mark.cave-ayland@ilande.co.uk; helo=mail.ilande.co.uk X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, 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-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1758803444081116600 Content-Type: text/plain; charset="utf-8" Based upon the qtest reproducer posted to Gitlab issue #3082 at https://gitlab.com/qemu-project/qemu/-/issues/3082. Signed-off-by: Mark Cave-Ayland Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- tests/qtest/am53c974-test.c | 40 +++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/qtest/am53c974-test.c b/tests/qtest/am53c974-test.c index ed3ac7db20..a3667275ac 100644 --- a/tests/qtest/am53c974-test.c +++ b/tests/qtest/am53c974-test.c @@ -109,6 +109,44 @@ static void test_cmdfifo_overflow2_ok(void) qtest_quit(s); } =20 +/* Reported as https://issues.oss-fuzz.com/issues/439878564 */ +static void test_cmdfifo_overflow3_ok(void) +{ + QTestState *s =3D qtest_init( + "-device am53c974,id=3Dscsi -device scsi-hd,drive=3Ddisk0 " + "-drive id=3Ddisk0,if=3Dnone,file=3Dnull-co://,format=3Draw -nodef= aults"); + qtest_outl(s, 0xcf8, 0x80001010); + qtest_outl(s, 0xcfc, 0xc000); + qtest_outl(s, 0xcf8, 0x80001004); + qtest_outw(s, 0xcfc, 0x01); + qtest_outb(s, 0xc00c, 0x43); + qtest_outl(s, 0xc00b, 0x9100); + qtest_outl(s, 0xc009, 0x02000000); + qtest_outl(s, 0xc000, 0x0b); + qtest_outl(s, 0xc00b, 0x00); + qtest_outl(s, 0xc00b, 0x00); + qtest_outl(s, 0xc00b, 0xc200); + qtest_outl(s, 0xc00b, 0x1000); + qtest_outl(s, 0xc00b, 0x9000); + qtest_outb(s, 0xc008, 0x00); + qtest_outb(s, 0xc008, 0x00); + qtest_outl(s, 0xc03f, 0x0300); + qtest_outl(s, 0xc00b, 0x00); + qtest_outw(s, 0xc00b, 0x4200); + qtest_outl(s, 0xc00b, 0x00); + qtest_outw(s, 0xc00b, 0x1200); + qtest_outl(s, 0xc00b, 0x00); + qtest_outb(s, 0xc00c, 0x43); + qtest_outl(s, 0xc00b, 0x00); + qtest_outl(s, 0xc00b, 0x00); + qtest_outl(s, 0xc007, 0x00); + qtest_outl(s, 0xc007, 0x00); + qtest_outl(s, 0xc007, 0x00); + qtest_outl(s, 0xc00b, 0x1000); + qtest_outl(s, 0xc007, 0x00); + qtest_quit(s); +} + /* Reported as crash_0900379669 */ static void test_fifo_pop_buf(void) { @@ -266,6 +304,8 @@ int main(int argc, char **argv) test_cmdfifo_overflow_ok); qtest_add_func("am53c974/test_cmdfifo_overflow2_ok", test_cmdfifo_overflow2_ok); + qtest_add_func("am53c974/test_cmdfifo_overflow3_ok", + test_cmdfifo_overflow3_ok); qtest_add_func("am53c974/test_fifo_pop_buf", test_fifo_pop_buf); qtest_add_func("am53c974/test_target_selected_ok", --=20 2.39.5