From nobody Thu Sep 19 12:48:52 2024 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4X8b1K3VWHz5WWN4; Thu, 19 Sep 2024 12:48:53 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4X8b1K1Nx0z4mw5; Thu, 19 Sep 2024 12:48:53 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1726750133; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=TcrQoBgNQ61EheokNvZcGccFqZPPpu2O1Duwa/PL0mI=; b=JffPN/JsuXgEebPDsqM6To64WZlG6Cf+JYySPPpzH7U+k2kWH2s9UxHzB/aKJV9Q46+XGq urs0dZ1hCWj426ysRxoaOqZCBL/U8h+f/QJc+UCK55g41rK2HuYXsGbJblB+jIij31p1W4 /o1zSajAksfrQhQLb+HTZBtcoMp/ARfR7VinQROLg7mGc8hubLYEl8WArsHbboo1XUufla DCFcuRR5jNySHIdasqhRGLjT60ndbmYrwZXp9PS5ATQvRmUfMQ5IHD5ZialKG5Mo+UjNvU //2+5cFh0eutJ3toiKUXN1u5B1ieo1pdCQU++u5NRo0RV0FmgdCJ0kBSbVNggA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1726750133; a=rsa-sha256; cv=none; b=LWZFnx5MlfdYU8aNYIpCFLYxRxeCQLKvPfkESrjAtcBP8xqbZWHeZvvQaWjQ3icVJRKZ0D +X+jPMdfnUHNpmuxSSK3uk8nKPwy79XYVJqzhhLdsY6hgbSfzItQreSARw5bCM72ppNMeF MuEbJoFt/GthL71hRICaM8iYKibLg7lcYkeA4MyxEPa72P+S9OGShdGBkyLcv5/YTJ6JXO XwxfpGbGzdZZ0qIKcu2njpIsVdKA53RMYAkJkXe1rsfuklvi4s2sgEPHVBTEjBUvi2Dypk y1WnMMug5Zgd3czk0vgGyylpHXcA3lwAAFv1YVo2hu5VBxAuRUGMekugOl02Qg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1726750133; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=TcrQoBgNQ61EheokNvZcGccFqZPPpu2O1Duwa/PL0mI=; b=ns3p6LW+BF//24ZGY18dICptoMAajvFPuUHG1IhyIiC2jaThBnJwQ4nfv7FNBH42qmEVbD YjqScQS0samwV7W5S9jQqciHzNS3oApAgMda59Axwfku6BYmzudK2PuTflLgoY1AD2/Jil haNRzE6X4e7fjhUJr2zKAIuh1vn+mWReYmaewy7ljNTpZLg4TRmPNjN6KkM0T/vSTUOGA0 YDi6++4zr+2S1+Y+rLBHvi2MdpZVMHUuRcF/uOCfK4zUiTJ8p7OVwOtRbim80/wxy9tDrG 8RHWojQlbRUitMYRMk1OgHZy268znaLLuCihLhh8bsAsXVx00lpjy3SvyLjTsA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4X8b1K0tfWz15mF; Thu, 19 Sep 2024 12:48:53 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 48JCmqgJ065431; Thu, 19 Sep 2024 12:48:52 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 48JCmqB3065428; Thu, 19 Sep 2024 12:48:52 GMT (envelope-from git) Date: Thu, 19 Sep 2024 12:48:52 GMT Message-Id: <202409191248.48JCmqB3065428@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Ed Maste Subject: git: 2abd2ad64899 - stable/13 - bhyve: improve input validation in pci_xhci List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-branches@freebsd.org Sender: owner-dev-commits-src-branches@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: emaste X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 2abd2ad648994e8444f479e54773a36311f522e5 Auto-Submitted: auto-generated The branch stable/13 has been updated by emaste: URL: https://cgit.FreeBSD.org/src/commit/?id=2abd2ad648994e8444f479e54773a36311f522e5 commit 2abd2ad648994e8444f479e54773a36311f522e5 Author: Pierre Pronchery AuthorDate: 2024-07-17 15:04:23 +0000 Commit: Ed Maste CommitDate: 2024-09-19 12:40:49 +0000 bhyve: improve input validation in pci_xhci Several functions did not validate the slot index resulting in OOB read on the heap of the slot device structure which could lead to arbitrary reads/writes and potentially code execution. Reported by: Synacktiv Reviewed by: markj (earlier), jhb Security: CVE-2024-41721 Security: HYP-02 Sponsored by: The Alpha-Omega Project Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D45996 (cherry picked from commit e72d86ad9c62c8054d7977a71f08e68ef755c132) (cherry picked from commit 419da61f8203ac475550ae4b0971dbef10f811f2) --- usr.sbin/bhyve/pci_xhci.c | 78 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 73 insertions(+), 5 deletions(-) diff --git a/usr.sbin/bhyve/pci_xhci.c b/usr.sbin/bhyve/pci_xhci.c index 93161affd732..06c71a6643a6 100644 --- a/usr.sbin/bhyve/pci_xhci.c +++ b/usr.sbin/bhyve/pci_xhci.c @@ -579,7 +579,7 @@ pci_xhci_get_dev_ctx(struct pci_xhci_softc *sc, uint32_t slot) uint64_t devctx_addr; struct xhci_dev_ctx *devctx; - assert(slot > 0 && slot <= XHCI_MAX_DEVS); + assert(slot > 0 && slot <= XHCI_MAX_SLOTS); assert(XHCI_SLOTDEV_PTR(sc, slot) != NULL); assert(sc->opregs.dcbaa_p != NULL); @@ -852,7 +852,10 @@ pci_xhci_cmd_disable_slot(struct pci_xhci_softc *sc, uint32_t slot) if (sc->portregs == NULL) goto done; - if (slot > XHCI_MAX_SLOTS) { + if (slot == 0) { + cmderr = XHCI_TRB_ERROR_TRB; + goto done; + } else if (slot > XHCI_MAX_SLOTS) { cmderr = XHCI_TRB_ERROR_SLOT_NOT_ON; goto done; } @@ -888,6 +891,14 @@ pci_xhci_cmd_reset_device(struct pci_xhci_softc *sc, uint32_t slot) DPRINTF(("pci_xhci reset device slot %u", slot)); + if (slot == 0) { + cmderr = XHCI_TRB_ERROR_TRB; + goto done; + } else if (slot > XHCI_MAX_SLOTS) { + cmderr = XHCI_TRB_ERROR_SLOT_NOT_ON; + goto done; + } + dev = XHCI_SLOTDEV_PTR(sc, slot); if (!dev || dev->dev_slotstate == XHCI_ST_DISABLED) cmderr = XHCI_TRB_ERROR_SLOT_NOT_ON; @@ -896,6 +907,10 @@ pci_xhci_cmd_reset_device(struct pci_xhci_softc *sc, uint32_t slot) dev->hci.hci_address = 0; dev_ctx = pci_xhci_get_dev_ctx(sc, slot); + if (dev_ctx == NULL) { + cmderr = XHCI_TRB_ERROR_PARAMETER; + goto done; + } /* slot state */ dev_ctx->ctx_slot.dwSctx3 = FIELD_REPLACE( @@ -956,8 +971,20 @@ pci_xhci_cmd_address_device(struct pci_xhci_softc *sc, uint32_t slot, goto done; } + if (slot == 0) { + cmderr = XHCI_TRB_ERROR_TRB; + goto done; + } else if (slot > XHCI_MAX_SLOTS) { + cmderr = XHCI_TRB_ERROR_SLOT_NOT_ON; + goto done; + } + /* assign address to slot */ dev_ctx = pci_xhci_get_dev_ctx(sc, slot); + if (dev_ctx == NULL) { + cmderr = XHCI_TRB_ERROR_PARAMETER; + goto done; + } DPRINTF(("pci_xhci: address device, dev ctx")); DPRINTF((" slot %08x %08x %08x %08x", @@ -1018,6 +1045,14 @@ pci_xhci_cmd_config_ep(struct pci_xhci_softc *sc, uint32_t slot, DPRINTF(("pci_xhci config_ep slot %u", slot)); + if (slot == 0) { + cmderr = XHCI_TRB_ERROR_TRB; + goto done; + } else if (slot > XHCI_MAX_SLOTS) { + cmderr = XHCI_TRB_ERROR_SLOT_NOT_ON; + goto done; + } + dev = XHCI_SLOTDEV_PTR(sc, slot); assert(dev != NULL); @@ -1031,6 +1066,10 @@ pci_xhci_cmd_config_ep(struct pci_xhci_softc *sc, uint32_t slot, dev->hci.hci_address = 0; dev_ctx = pci_xhci_get_dev_ctx(sc, slot); + if (dev_ctx == NULL) { + cmderr = XHCI_TRB_ERROR_PARAMETER; + goto done; + } /* number of contexts */ dev_ctx->ctx_slot.dwSctx0 = FIELD_REPLACE( @@ -1137,11 +1176,19 @@ pci_xhci_cmd_reset_ep(struct pci_xhci_softc *sc, uint32_t slot, cmderr = XHCI_TRB_ERROR_SUCCESS; - type = XHCI_TRB_3_TYPE_GET(trb->dwTrb3); + if (slot == 0) { + cmderr = XHCI_TRB_ERROR_TRB; + goto done; + } else if (slot > XHCI_MAX_SLOTS) { + cmderr = XHCI_TRB_ERROR_SLOT_NOT_ON; + goto done; + } dev = XHCI_SLOTDEV_PTR(sc, slot); assert(dev != NULL); + type = XHCI_TRB_3_TYPE_GET(trb->dwTrb3); + if (type == XHCI_TRB_TYPE_STOP_EP && (trb->dwTrb3 & XHCI_TRB_3_SUSP_EP_BIT) != 0) { /* XXX suspend endpoint for 10ms */ @@ -1226,6 +1273,14 @@ pci_xhci_cmd_set_tr(struct pci_xhci_softc *sc, uint32_t slot, cmderr = XHCI_TRB_ERROR_SUCCESS; + if (slot == 0) { + cmderr = XHCI_TRB_ERROR_TRB; + goto done; + } else if (slot > XHCI_MAX_SLOTS) { + cmderr = XHCI_TRB_ERROR_SLOT_NOT_ON; + goto done; + } + dev = XHCI_SLOTDEV_PTR(sc, slot); assert(dev != NULL); @@ -1324,8 +1379,20 @@ pci_xhci_cmd_eval_ctx(struct pci_xhci_softc *sc, uint32_t slot, goto done; } + if (slot == 0) { + cmderr = XHCI_TRB_ERROR_TRB; + goto done; + } else if (slot > XHCI_MAX_SLOTS) { + cmderr = XHCI_TRB_ERROR_SLOT_NOT_ON; + goto done; + } + /* assign address to slot; in this emulation, slot_id = address */ dev_ctx = pci_xhci_get_dev_ctx(sc, slot); + if (dev_ctx == NULL) { + cmderr = XHCI_TRB_ERROR_PARAMETER; + goto done; + } DPRINTF(("pci_xhci: eval ctx, dev ctx")); DPRINTF((" slot %08x %08x %08x %08x", @@ -1554,8 +1621,9 @@ pci_xhci_xfer_complete(struct pci_xhci_softc *sc, struct usb_data_xfer *xfer, dev = XHCI_SLOTDEV_PTR(sc, slot); devep = &dev->eps[epid]; dev_ctx = pci_xhci_get_dev_ctx(sc, slot); - - assert(dev_ctx != NULL); + if (dev_ctx == NULL) { + return XHCI_TRB_ERROR_PARAMETER; + } ep_ctx = &dev_ctx->ctx_ep[epid];