git: b3180bea9b1d - stable/14 - bhyve: TPM CRB: pass actual TPM request length to backend execute_cmd()

From: Corvin Köhne <corvink_at_FreeBSD.org>
Date: Tue, 12 Nov 2024 07:55:44 UTC
The branch stable/14 has been updated by corvink:

URL: https://cgit.FreeBSD.org/src/commit/?id=b3180bea9b1d0c30d46a10e92d96d4944004f37a

commit b3180bea9b1d0c30d46a10e92d96d4944004f37a
Author:     Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
AuthorDate: 2024-09-09 08:42:42 +0000
Commit:     Corvin Köhne <corvink@FreeBSD.org>
CommitDate: 2024-11-12 07:54:22 +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
    
    (cherry picked from commit bc3d09e90b8625f17017663fad3e049348b04ded)
---
 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 3e0769d1fef6..b1f5b72b6285 100644
--- a/usr.sbin/bhyve/tpm_intf_crb.c
+++ b/usr.sbin/bhyve/tpm_intf_crb.c
@@ -166,6 +166,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;
@@ -177,6 +186,7 @@ struct tpm_crb {
 	bool closing;
 };
 
+
 static void *
 tpm_crb_thread(void *const arg)
 {
@@ -201,6 +211,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;
 
@@ -218,6 +248,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
@@ -238,8 +279,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);