From nobody Sun Feb 8 16:53:09 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=philmd@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1612479047; cv=none; d=zohomail.com; s=zohoarc; b=lbgI6ThZQ+KeeffC+gUTtsl3Da6Tr/M6xCG0t6nsJphsAz9ljdws70F8Xr61J0sEUAGD1RGeeJxb+Wi091Z/4XlrOwQ9RCwdFRmNBlSLQtOyk9xjV4mudOCBflgFumNsZe0OA4koBnUtfGVSF4deBYOwsQ7w48bhaHzYuz00yO4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1612479047; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:MIME-Version:Message-ID:Subject:To; bh=oPGTla5PrkCsppu+7N9sKNWNuYVuSz/EYc1S5deb/zQ=; b=b0PdRAXzVUz9DB3Z7w7SZNnaTeSeAxV0Vne0FUZaEcW+LpUUwTBrNANH0RMgc6oAHTEuIJy077edCbv34KCPcyxr8ty65gPiwmzjOhmUsypo7aeOLOsbZ+jRD0Xzu0FGwDV99dYLY0rAbVg4gug080aeY22LApBtGoMu9ruwt+s= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1612479047345898.531390827416; Thu, 4 Feb 2021 14:50:47 -0800 (PST) Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-7-U9vA3kY-NOCjO2JMtYJWOg-1; Thu, 04 Feb 2021 17:50:44 -0500 Received: by mail-ej1-f70.google.com with SMTP id aq28so4173810ejc.20 for ; Thu, 04 Feb 2021 14:50:44 -0800 (PST) Return-Path: Return-Path: Received: from x1w.redhat.com (107.red-83-59-163.dynamicip.rima-tde.net. [83.59.163.107]) by smtp.gmail.com with ESMTPSA id cw21sm3083641edb.85.2021.02.04.14.50.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Feb 2021 14:50:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612479046; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=oPGTla5PrkCsppu+7N9sKNWNuYVuSz/EYc1S5deb/zQ=; b=EfoD2MV0tbsNOfetVLSqIW7a2YUvjn7Tff1Gmx146lTHHfTVXS4dRWKaRXrtctSH09nOma 91C08K/a+TivCAsQ1hF3vIIXWIpiw4tNCYFGXAzlaCwMTQsAcK30/vMd4lz2a5yNs5DreI 0ouN7zt3tgweDhz18nnipHJD/qrFdKA= X-MC-Unique: U9vA3kY-NOCjO2JMtYJWOg-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=oPGTla5PrkCsppu+7N9sKNWNuYVuSz/EYc1S5deb/zQ=; b=SDAk/dbMD8haKI+g2Ce2ufPPlUNUFU9uzs5ff3bzuHdx6znsoBv9rqLqCD6VTfCHHr YAH7RM8XrxoyqfYN3QSJLTnIgE7TX3Lkagf10TGwvuUJHmwvgqCgW7Nzi+SAeBSzWYW6 3Lj9fH3FWYuZ5KAFOVrqpsSo3eUTWMeBtaXJav4OTU4H95+LIKRiBKnRC1vI6tVvE+NE /yAzF9Qyb7JFFPTG5IhcsmZhRKC08PCiBhKlzq0ggAtCtZc3PVWgI3vhjqGjniWcR5LA pBzO1gdrhBItMivQ7l71m7ZLZO6Uo0jxMyOOieIsxN/mE3Tun3P+MW4x2XIN9D8fzZrE YyPQ== X-Gm-Message-State: AOAM532aReHHf2WluAzPnKyfZtcVeFe4znG6IQdtbmOu2A9lsEfdK9bL iuaciR6/wFBn0l9sOkg2g7R+odMaNj7qBmTKIuo3zByEMsDX1j7eDjbuQmv7r70cGOoyFIlZcAv 3CjwG08MgDZ/+fg== X-Received: by 2002:a17:906:d8b5:: with SMTP id qc21mr1247211ejb.62.1612479043387; Thu, 04 Feb 2021 14:50:43 -0800 (PST) X-Google-Smtp-Source: ABdhPJxda5jqG/9SAr1NKPWhmgnkgZT1Om4oLaqKxWLHM3keC8no89hmuUxGPwAz3c0j+qMKmOQDdg== X-Received: by 2002:a17:906:d8b5:: with SMTP id qc21mr1247208ejb.62.1612479043253; Thu, 04 Feb 2021 14:50:43 -0800 (PST) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: Darren Kenny , Paolo Bonzini , Li Qiang , Fam Zheng , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , qemu-stable@nongnu.org, Alexander Bulekov Subject: [PATCH v2] hw/scsi/scsi-disk: Fix out of bounds access in mode_sense_page() Date: Thu, 4 Feb 2021 23:50:40 +0100 Message-Id: <20210204225041.1822673-1-philmd@redhat.com> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=philmd@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Per the "SCSI Commands Reference Manual" (Rev. J) chapter 5.3 "Mode parameters" and table 359 "Mode page codes and subpage codes", the last page code is 0x3f. When using it as array index, the array must have 0x40 elements. Replace the magic 0x3f value by its definition and increase the size of the mode_sense_valid[] to avoid an out of bound access (reproducer available in [Buglink]): hw/scsi/scsi-disk.c:1104:10: runtime error: index 63 out of bounds for ty= pe 'const int [63]' SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior hw/scsi/scsi-disk= .c:1104:10 in =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D1813911=3D=3DERROR: AddressSanitizer: global-buffer-overflow READ of size 4 at 0x5624b84aff1c thread T0 #0 0x5624b63004b3 in mode_sense_page hw/scsi/scsi-disk.c:1104:10 #1 0x5624b630f798 in scsi_disk_check_mode_select hw/scsi/scsi-disk.c:= 1447:11 #2 0x5624b630efb8 in mode_select_pages hw/scsi/scsi-disk.c:1515:17 #3 0x5624b630988e in scsi_disk_emulate_mode_select hw/scsi/scsi-disk.= c:1570:13 #4 0x5624b62f08e7 in scsi_disk_emulate_write_data hw/scsi/scsi-disk.c= :1861:9 #5 0x5624b62b171b in scsi_req_continue hw/scsi/scsi-bus.c:1391:9 #6 0x5624b62b2d4c in scsi_req_data hw/scsi/scsi-bus.c:1427:5 #7 0x5624b62f05f6 in scsi_disk_emulate_write_data hw/scsi/scsi-disk.c= :1853:9 #8 0x5624b62b171b in scsi_req_continue hw/scsi/scsi-bus.c:1391:9 #9 0x5624b63e47ed in megasas_enqueue_req hw/scsi/megasas.c:1660:9 #10 0x5624b63b9cfa in megasas_handle_scsi hw/scsi/megasas.c:1735:5 #11 0x5624b63acf91 in megasas_handle_frame hw/scsi/megasas.c:1974:24 #12 0x5624b63aa200 in megasas_mmio_write hw/scsi/megasas.c:2131:9 #13 0x5624b63ebed3 in megasas_port_write hw/scsi/megasas.c:2182:5 #14 0x5624b6f43568 in memory_region_write_accessor softmmu/memory.c:4= 91:5 Cc: qemu-stable@nongnu.org Reported-by: OSS-Fuzz Reported-by: Alexander Bulekov Buglink: https://bugs.launchpad.net/qemu/+bug/1914638 Fixes: a8f4bbe2900 ("scsi-disk: store valid mode pages in a table") Signed-off-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Alexander Bulekov Reviewed-by: Li Qiang --- v2: Mention reproducer link --- hw/scsi/scsi-disk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hw/scsi/scsi-disk.c b/hw/scsi/scsi-disk.c index ed52fcd49ff..93aec483e88 100644 --- a/hw/scsi/scsi-disk.c +++ b/hw/scsi/scsi-disk.c @@ -1089,7 +1089,7 @@ static int scsi_emulate_mechanism_status(SCSIDiskStat= e *s, uint8_t *outbuf) static int mode_sense_page(SCSIDiskState *s, int page, uint8_t **p_outbuf, int page_control) { - static const int mode_sense_valid[0x3f] =3D { + static const int mode_sense_valid[MODE_PAGE_ALLS + 1] =3D { [MODE_PAGE_HD_GEOMETRY] =3D (1 << TYPE_DISK), [MODE_PAGE_FLEXIBLE_DISK_GEOMETRY] =3D (1 << TYPE_DISK), [MODE_PAGE_CACHING] =3D (1 << TYPE_DISK) | (1 << TY= PE_ROM), --=20 2.26.2