From nobody Fri Aug 19 22:25:36 2022 X-Original-To: dev-commits-src-all@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 4M8btS3hZvz4ZLQx; Fri, 19 Aug 2022 22:25:36 +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 "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4M8btS2lJ6z3phC; Fri, 19 Aug 2022 22:25:36 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1660947936; 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=xO5Xq4U0MsocHP7zNKQ0q3JWTLmf8nqOFTzqMgp8SUc=; b=JvRBnQFElz9V+y7WG1zkQW6UihJt5v8Q7t3MsYFLA4bdSgKDVjj9DqYuMV65V0aCuZO92M jlNhmO3zDRoVXK7AyNrnmH4HPes+ano57UgEqocqE2+8wP72jUtUpi98PA214KDK55Quo/ ky2CABgsDTKbnsqLVd6ne1mg+bcJmwV1iCuBg9zgHdmprQXFiJ217kPIgDX525PV7vLl+v hmlZ4rpNrREVkMwedWpyylhgNASvPTwzOCcTBKKE8T4kHgE49xjCwprjil0uZREZQI5lnw y4TcvhEMCIe8iKd42edF/c2bAkpVCEXtFl7etzS2iRJaMzkPifWYNeOLCbHHJg== 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 4M8btS1sYLz17mr; Fri, 19 Aug 2022 22:25:36 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 27JMPaEl068526; Fri, 19 Aug 2022 22:25:36 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 27JMPaI6068525; Fri, 19 Aug 2022 22:25:36 GMT (envelope-from git) Date: Fri, 19 Aug 2022 22:25:36 GMT Message-Id: <202208192225.27JMPaI6068525@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: John Baldwin Subject: git: c94f30ea85b7 - main - bhyve: Validate host PAs used to map passthrough BARs. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jhb X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: c94f30ea85b745a5137471876013f79689e0af03 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1660947936; 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=xO5Xq4U0MsocHP7zNKQ0q3JWTLmf8nqOFTzqMgp8SUc=; b=xLfTA9BAV/7vf/vYuN0V15rffrtQaKb+Z2laDoeUPkaLQi4KhS6iRRMPKV8dz7F84pJFgY CWSHiK3RL63MSKJK1UN6oWCN/9tXpmcNoqXEJh7OiEMqX72FUEqq+gGpKc21/Y9CoNb6l3 pq4Wijommf6kLRtE8RSPBYerSRxDyariVHca7PpEhGHRFTkJ8MWyJZiBoESk6VtFjZvBua 0MvEYYTaJwRYzdIs9bapdeExXvXW9j24yHOx6BbOB9mwl+fmbWpdvH+OJ+48P3Spkzv42h jfRGryF6w9SmmLCau4IvwW2Ljn5jKgmMwVl98kvuJc8pj340bUDVFNA411tOnw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1660947936; a=rsa-sha256; cv=none; b=cSnMH+FdZf5AE29TJFVt2EsCdO4bKlku5jdf0U4P5CsXgBwBqPEjuq3ZaTNhPcX1kB/LbF m6nFejtsoOuozRv9sS/d79B/IEH1ueSUcHmSO+wc0fGkXDBJ+ITBvQ2aZAzMDdwt6dJbZL CWTqd53dD/FOx333z+Klux7F7IN33o9eivU8qEuoOFYMa1QSvUNNnK+VdmV+K1FtlGkzeK ZkodarAReLqCjuiBz5gvu6NePfeL5xPIIYtCK60jLjwlS4Y6H9hKtm13XAs3kuBuP5Ljtp 3Pi4UP1AnjtaCDgMmtDVJDuZLJVbfB9rLnE6/PPkYUv4gBi3W1Gn7ptbS4XtkQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=c94f30ea85b745a5137471876013f79689e0af03 commit c94f30ea85b745a5137471876013f79689e0af03 Author: John Baldwin AuthorDate: 2022-08-19 21:59:44 +0000 Commit: John Baldwin CommitDate: 2022-08-19 22:03:01 +0000 bhyve: Validate host PAs used to map passthrough BARs. Reject attempts to map host physical address ranges that are not subsets of a passthrough device's BAR into a guest. Reviewed by: markj, emaste MFC after: 1 week Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D36238 --- sys/amd64/vmm/io/ppt.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/sys/amd64/vmm/io/ppt.c b/sys/amd64/vmm/io/ppt.c index 6bcd8f78bbab..6621c0ba2996 100644 --- a/sys/amd64/vmm/io/ppt.c +++ b/sys/amd64/vmm/io/ppt.c @@ -439,6 +439,23 @@ ppt_unassign_all(struct vm *vm) return (0); } +static bool +ppt_valid_bar_mapping(struct pptdev *ppt, vm_paddr_t hpa, size_t len) +{ + struct pci_map *pm; + pci_addr_t base, size; + + for (pm = pci_first_bar(ppt->dev); pm != NULL; pm = pci_next_bar(pm)) { + if (!PCI_BAR_MEM(pm->pm_value)) + continue; + base = pm->pm_value & PCIM_BAR_MEM_BASE; + size = (pci_addr_t)1 << pm->pm_size; + if (hpa >= base && hpa + len <= base + size) + return (true); + } + return (false); +} + int ppt_map_mmio(struct vm *vm, int bus, int slot, int func, vm_paddr_t gpa, size_t len, vm_paddr_t hpa) @@ -447,10 +464,17 @@ ppt_map_mmio(struct vm *vm, int bus, int slot, int func, struct pptseg *seg; struct pptdev *ppt; + if (len % PAGE_SIZE != 0 || len == 0 || gpa % PAGE_SIZE != 0 || + hpa % PAGE_SIZE != 0 || gpa + len < gpa || hpa + len < hpa) + return (EINVAL); + error = ppt_find(vm, bus, slot, func, &ppt); if (error) return (error); + if (!ppt_valid_bar_mapping(ppt, hpa, len)) + return (EINVAL); + for (i = 0; i < MAX_MMIOSEGS; i++) { seg = &ppt->mmio[i]; if (seg->len == 0) {