From nobody Thu May 2 13:33:30 2024 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 ARC-Seal: i=1; a=rsa-sha256; t=1604585155; cv=none; d=zohomail.com; s=zohoarc; b=FU7JZd+walRc8t5hMDK8QPxmRab68zfPkHpTg4WcT5x757FSyA9LOq8sdulIdq/NYeWI7i/e9nN4FVQixsufYoAl3SkiJvo7G6xWb2LqdjFZWkGRNpWucYseJ6a/QRzXz8mapkMZgcgJZkqNgxGlpA3Ugz1BC7SmTMuP7vn0gBE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1604585155; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=/oDsDUKEnFCzhHza6UXQeCfiP9Ht2/3ZVBXyKU7wWos=; b=nxPndZDs/+87q29ynzaRc40jGaJhgpDemnuwCyWtGKn9OqozApsQ8DMpQ9Afbjri0x4Fs4ZOXfVhLMWzvRX2kxspriXl+cqAn7SKiCi2rdFxO35FdvNk7zM3Fl24SMdF6QkjP9pdfVboxLGCknzhv9UBuA8XQgn2xpny5+0qKiw= ARC-Authentication-Results: i=1; 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 1604585154831978.957905096437; Thu, 5 Nov 2020 06:05:54 -0800 (PST) Received: from localhost ([::1]:33584 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kafth-0005Pv-BT for importer@patchew.org; Thu, 05 Nov 2020 09:05:53 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:35524) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kafr3-0002Uj-N9 for qemu-devel@nongnu.org; Thu, 05 Nov 2020 09:03:10 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:2333) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kafqy-0002rY-P9 for qemu-devel@nongnu.org; Thu, 05 Nov 2020 09:03:09 -0500 Received: from DGGEMS406-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4CRlb63bGXzkcG4; Thu, 5 Nov 2020 22:02:42 +0800 (CST) Received: from [10.174.187.138] (10.174.187.138) by DGGEMS406-HUB.china.huawei.com (10.3.19.206) with Microsoft SMTP Server id 14.3.487.0; Thu, 5 Nov 2020 22:02:38 +0800 Message-ID: <5FA405FE.1080604@huawei.com> Date: Thu, 5 Nov 2020 22:02:38 +0800 From: AlexChen User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20130509 Thunderbird/17.0.6 MIME-Version: 1.0 To: Subject: [PATCH] usb/hcd-xhci: Fix an index-out-of-bounds in xhci_runtime_write and xhci_runtime_read Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.174.187.138] X-CFilter-Loop: Reflected 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=45.249.212.190; envelope-from=alex.chen@huawei.com; helo=szxga04-in.huawei.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/11/05 07:27:41 X-ACL-Warn: Detected OS = Linux 3.1-3.10 [fuzzy] X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: qemu-devel@nongnu.org, zhang.zhanghailiang@huawei.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Currently, the 'v' is not checked whether it is between 0 and 16, which may result in an out-of-bounds access to the array 'xhci->intr[]'. This is LP#1902112. Following is the reproducer provided in: -->https://bugs.launchpad.net/qemu/+bug/1902112 =3D=3D=3D Reproducer (build with --enable-sanitizers) =3D=3D=3D export UBSAN_OPTIONS=3D"print_stacktrace=3D1:silence_unsigned_overflow=3D1" cat << EOF | ./qemu-system-i386 -display none -machine\ accel=3Dqtest, -m 512M -machine q35 -nodefaults -drive\ file=3Dnull-co://,if=3Dnone,format=3Draw,id=3Ddisk0 -device\ qemu-xhci,id=3Dxhci -device usb-tablet,bus=3Dxhci.0\ -device usb-bot -device usb-storage,drive=3Ddisk0\ -chardev null,id=3Dcd0 -chardev null,id=3Dcd1 -device\ usb-braille,chardev=3Dcd0 -device usb-ccid -device\ usb-ccid -device usb-kbd -device usb-mouse -device\ usb-serial,chardev=3Dcd1 -device usb-tablet -device\ usb-wacom-tablet -device usb-audio -qtest stdio outl 0xcf8 0x80000803 outl 0xcfc 0x18caffff outl 0xcf8 0x80000810 outl 0xcfc 0x555a2e46 write 0x555a1004 0x4 0xe7b9aa7a EOF =3D=3D=3D Stack Trace =3D=3D=3D SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ../hw/usb/hcd-xhci.= c:3012:30 in ../hw/usb/hcd-xhci.c:3012:30: runtime error: index -1 out of bounds for typ= e 'XHCIInterrupter [16]' #0 0x55bd2e97c8b0 in xhci_runtime_write /src/qemu/hw/usb/hcd-xhci.c:3012:30 #1 0x55bd2edfdd13 in memory_region_write_accessor /src/qemu/softmmu/memory.= c:484:5 #2 0x55bd2edfdb14 in access_with_adjusted_size /src/qemu/softmmu/memory.c:5= 45:18 #3 0x55bd2edfd54b in memory_region_dispatch_write /src/qemu/softmmu/memory.= c:0:13 #4 0x55bd2ed7fa46 in flatview_write_continue /src/qemu/softmmu/physmem.c:27= 67:23 #5 0x55bd2ed7cac0 in flatview_write /src/qemu/softmmu/physmem.c:2807:14 This patch fixes this bug. Buglink: https://bugs.launchpad.net/qemu/+bug/1902112 Reported-by: Alexander Bulekov Signed-off-by: Alex Chen --- hw/usb/hcd-xhci.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/hw/usb/hcd-xhci.c b/hw/usb/hcd-xhci.c index 79ce5c4be6..50abef40ad 100644 --- a/hw/usb/hcd-xhci.c +++ b/hw/usb/hcd-xhci.c @@ -2962,8 +2962,9 @@ static uint64_t xhci_runtime_read(void *ptr, hwaddr r= eg, { XHCIState *xhci =3D ptr; uint32_t ret =3D 0; + int v =3D (reg - 0x20) / 0x20; - if (reg < 0x20) { + if (reg < 0x20 || v < 0 || v >=3D XHCI_MAXINTRS) { switch (reg) { case 0x00: /* MFINDEX */ ret =3D xhci_mfindex_get(xhci) & 0x3fff; @@ -2973,7 +2974,6 @@ static uint64_t xhci_runtime_read(void *ptr, hwaddr r= eg, break; } } else { - int v =3D (reg - 0x20) / 0x20; XHCIInterrupter *intr =3D &xhci->intr[v]; switch (reg & 0x1f) { case 0x00: /* IMAN */ @@ -3009,14 +3009,16 @@ static void xhci_runtime_write(void *ptr, hwaddr re= g, { XHCIState *xhci =3D ptr; int v =3D (reg - 0x20) / 0x20; - XHCIInterrupter *intr =3D &xhci->intr[v]; + XHCIInterrupter *intr; trace_usb_xhci_runtime_write(reg, val); - if (reg < 0x20) { + if (reg < 0x20 || v < 0 || v >=3D XHCI_MAXINTRS) { trace_usb_xhci_unimplemented("runtime write", reg); return; } + intr =3D &xhci->intr[v]; + switch (reg & 0x1f) { case 0x00: /* IMAN */ if (val & IMAN_IP) { --=20 2.19.1