From nobody Tue Oct 22 12:06:49 2024 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 4XXrWZ1z81z5FDw1; Tue, 22 Oct 2024 12:06:50 +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 4XXrWZ1RRFz4qrS; Tue, 22 Oct 2024 12:06:50 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1729598810; 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=ITD5HwekqWgOBcW18DzdAiVf8zqf4+xwIZc26UVZYek=; b=PNG7sv0CEK7xnkpq2swd3dp2CSWD6X1CyWq3FPKwiwufthn+kF+TL654YErnAebkuVb++m TjjAJIdgqUiAOlgVYBN7hMnsT7yblGrYBS061gMxpRp5n5say2y1wScAQPcnCE16agUVv5 N6u8rRI6eQZNYm9jdKdwLTdRZdI7n7T0E9OnKm2rPITZYFd4SPUfx8JqxbY1rwfNf9yM1K wiEts0K38PR9lnwSVnWmvFRIaSKcV1MHyu+Cgqie/jnyD/ovdpnGB7tu/Of7GeieiFC0Tz pJkkgxv6bkS0kXD4uKqWp1n19Z8x3KApZEnsphaWy4y7ULgNAl8RdsvdxPm+QA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1729598810; 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=ITD5HwekqWgOBcW18DzdAiVf8zqf4+xwIZc26UVZYek=; b=fSW4Ow7T2COIv/Jpq3Bxze7c4eATvAKOTGFlMYka7OO8s9u33opwIkfwrTUwCUQcYVs1gl HCoW2oq2ktQxi3MUkZdxGdBEU8VfZDfHoyTxVHGPQk/mIyEQfPzOixJ7crg5EMXHQBNJaP qRF7h0Jj6l74Eli2m4juXLWc8VP2bes8/p0R7K4wV/WSN0C1xoKhxShvnMz2AZNCr4H4yu OX30fUiv0YrS+nal0wyr8RIvoIDkGbiTdrBDA5hPB5maEW6MtIpTRrPrVMUMwE9pZyOF97 8uNQIUoNss7VFcSdfhj4SUa2hBUZkspvMaakjVSO3VXrIrlGgkDrghH/JGEj/Q== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1729598810; a=rsa-sha256; cv=none; b=yRZydazJyS+8M8eF4/lgRFlyKfP6UUrLG+Y0f/gSX0aJl10WlZzgiU15kyoEz1mSgIyGUa xgf4OyAky/ppFEBsmWL6Z23RJ0XPdakYONGIV1HZ9p31knMu/LdvIfwljD1/tTTI/JfPaA dmlf4FuINkbsnS3xKQ+s6b+mRERX/UP82jB+Wnt/qIpmtOZZ146XSUhSpp2xWlOkwQWNn2 yO6RllXRP/jHMcLhWml/nCxbcMRxuc5oVHFhM03Lv6u0k5DRytlZuIGhY4s4aCO40kxJX4 J8amwQMJK69bltoQpTZpzg5RH9jAYex2ocrkM7cx9f6Wmo+yvr0z3AmSpabZDQ== 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 4XXrWZ0fmLz1CBM; Tue, 22 Oct 2024 12:06:50 +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 49MC6nk2085249; Tue, 22 Oct 2024 12:06:49 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 49MC6njp085246; Tue, 22 Oct 2024 12:06:49 GMT (envelope-from git) Date: Tue, 22 Oct 2024 12:06:49 GMT Message-Id: <202410221206.49MC6njp085246@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Corvin =?utf-8?Q?K=C3=B6hne?= Subject: git: bc3d09e90b86 - main - bhyve: TPM CRB: pass actual TPM request length to backend execute_cmd() 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: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: corvink X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: bc3d09e90b8625f17017663fad3e049348b04ded Auto-Submitted: auto-generated The branch main has been updated by corvink: URL: https://cgit.FreeBSD.org/src/commit/?id=bc3d09e90b8625f17017663fad3e049348b04ded commit bc3d09e90b8625f17017663fad3e049348b04ded Author: Hans Rosenfeld AuthorDate: 2024-09-09 08:42:42 +0000 Commit: Corvin Köhne CommitDate: 2024-10-22 12:04:23 +0000 bhyve: TPM CRB: pass actual TPM request length to backend execute_cmd() The TPM spec (TPM Library, Part3: Commands, Section 5.2: Command Header Validation) requires that no more bytes are written than the size of the commands, as given in the request header. Thus the TPM CRB interface needs to get the command size from the request header and pass that to the emulation backend. As the guest OS driver can set the address and size of the command and response buffers freely within the limits of the provided CRB data buffer, bhyve should verify that the values set in the corresponding registers make sense before processing a command. Reviewed by: corvink MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D46564 --- usr.sbin/bhyve/tpm_intf_crb.c | 45 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/usr.sbin/bhyve/tpm_intf_crb.c b/usr.sbin/bhyve/tpm_intf_crb.c index d4ac2b47d28a..9b44d1d133b0 100644 --- a/usr.sbin/bhyve/tpm_intf_crb.c +++ b/usr.sbin/bhyve/tpm_intf_crb.c @@ -165,6 +165,15 @@ static_assert(sizeof(struct tpm_crb_regs) == TPM_CRB_REGS_SIZE, regs.rsp_addr = val; \ } while (0) +struct tpm_cmd_hdr { + uint16_t tag; + uint32_t len; + union { + uint32_t ordinal; + uint32_t errcode; + }; +} __packed; + struct tpm_crb { struct tpm_emul *emul; void *emul_sc; @@ -176,6 +185,7 @@ struct tpm_crb { bool closing; }; + static void * tpm_crb_thread(void *const arg) { @@ -200,6 +210,26 @@ tpm_crb_thread(void *const arg) const uint32_t cmd_size = CRB_CMD_SIZE_READ(crb->regs); const uint32_t rsp_size = CRB_RSP_SIZE_READ(crb->regs); + if ((cmd_addr < TPM_CRB_DATA_BUFFER_ADDRESS) || + (cmd_size < sizeof (struct tpm_cmd_hdr)) || + (cmd_size > TPM_CRB_DATA_BUFFER_SIZE) || + (cmd_addr + cmd_size > + TPM_CRB_DATA_BUFFER_ADDRESS + TPM_CRB_DATA_BUFFER_SIZE)) { + warnx("%s: invalid cmd [%16lx/%8x] outside of TPM " + "buffer", __func__, cmd_addr, cmd_size); + break; + } + + if ((rsp_addr < TPM_CRB_DATA_BUFFER_ADDRESS) || + (rsp_size < sizeof (struct tpm_cmd_hdr)) || + (rsp_size > TPM_CRB_DATA_BUFFER_SIZE) || + (rsp_addr + rsp_size > + TPM_CRB_DATA_BUFFER_ADDRESS + TPM_CRB_DATA_BUFFER_SIZE)) { + warnx("%s: invalid rsp [%16lx/%8x] outside of TPM " + "buffer", __func__, rsp_addr, rsp_size); + break; + } + const uint64_t cmd_off = cmd_addr - TPM_CRB_DATA_BUFFER_ADDRESS; const uint64_t rsp_off = rsp_addr - TPM_CRB_DATA_BUFFER_ADDRESS; @@ -217,6 +247,17 @@ tpm_crb_thread(void *const arg) uint8_t cmd[TPM_CRB_DATA_BUFFER_SIZE]; memcpy(cmd, crb->regs.data_buffer, TPM_CRB_DATA_BUFFER_SIZE); + /* + * Do a basic sanity check of the TPM request header. We'll need + * the TPM request length for execute_cmd() below. + */ + struct tpm_cmd_hdr *req = (struct tpm_cmd_hdr *)&cmd[cmd_off]; + if (be32toh(req->len) < sizeof (struct tpm_cmd_hdr) || + be32toh(req->len) > cmd_size) { + warnx("%s: invalid TPM request header", __func__); + break; + } + /* * A TPM command can take multiple seconds to execute. As we've * copied all required values and buffers at this point, we can @@ -237,8 +278,8 @@ tpm_crb_thread(void *const arg) * response. */ uint8_t rsp[TPM_CRB_DATA_BUFFER_SIZE] = { 0 }; - crb->emul->execute_cmd(crb->emul_sc, &cmd[cmd_off], cmd_size, - &rsp[rsp_off], rsp_size); + (void) crb->emul->execute_cmd(crb->emul_sc, req, + be32toh(req->len), &rsp[rsp_off], rsp_size); pthread_mutex_lock(&crb->mutex); memset(crb->regs.data_buffer, 0, TPM_CRB_DATA_BUFFER_SIZE);