git: bc3d09e90b86 - main - bhyve: TPM CRB: pass actual TPM request length to backend execute_cmd()

From: Corvin Köhne <corvink_at_FreeBSD.org>
Date: Tue, 22 Oct 2024 12:06:49 UTC
The branch main has been updated by corvink:

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

commit bc3d09e90b8625f17017663fad3e049348b04ded
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-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);