From b82bc5109e8366d5fb9cb9015e1c6d3e187de369 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Sun, 22 Nov 2015 17:56:53 -0500 Subject: [PATCH] tpm: Avoid scatter-gather copying in build_and_send_cmd() Setup the tpm hardware request in a linear area of memory. Signed-off-by: Kevin O'Connor --- src/tcgbios.c | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/src/tcgbios.c b/src/tcgbios.c index 44d7d87..6efa08b 100644 --- a/src/tcgbios.c +++ b/src/tcgbios.c @@ -332,33 +332,30 @@ build_and_send_cmd(u8 locty, u32 ordinal, const u8 *append, u32 append_size, u8 *resbuffer, u32 return_size, u32 *returnCode, enum tpmDurationType to_t) { - u32 rc; + struct { + struct tpm_req_header trqh; + u8 cmd[20]; + } PACKED req = { + .trqh.tag = cpu_to_be16(TPM_TAG_RQU_CMD), + .trqh.totlen = cpu_to_be32(TPM_REQ_HEADER_SIZE + append_size), + .trqh.ordinal = cpu_to_be32(ordinal), + }; u8 obuffer[64]; - struct tpm_req_header trqh; struct tpm_rsp_header *trsh = (struct tpm_rsp_header *)obuffer; - struct iovec iovec[3] = {{ 0 }}; u32 obuffer_len = sizeof(obuffer); + memset(obuffer, 0x0, sizeof(obuffer)); - if (return_size > sizeof(obuffer)) { - dprintf(DEBUG_tcg, "TCGBIOS: size of requested response too big."); + if (return_size > sizeof(obuffer) || append_size > sizeof(req.cmd)) { + warn_internalerror(); return TCG_FIRMWARE_ERROR; } + if (append_size) + memcpy(req.cmd, append, append_size); - trqh.tag = cpu_to_be16(TPM_TAG_RQU_CMD); - trqh.totlen = cpu_to_be32(TPM_REQ_HEADER_SIZE + append_size); - trqh.ordinal = cpu_to_be32(ordinal); - - iovec[0].data = &trqh; - iovec[0].length = TPM_REQ_HEADER_SIZE; - - if (append_size) { - iovec[1].data = append; - iovec[1].length = append_size; - } - - memset(obuffer, 0x0, sizeof(obuffer)); - - rc = transmit(locty, iovec, obuffer, &obuffer_len, to_t); + struct iovec iovec[2] = {{ 0 }}; + iovec[0].data = &req; + iovec[0].length = TPM_REQ_HEADER_SIZE + append_size; + u32 rc = transmit(locty, iovec, obuffer, &obuffer_len, to_t); if (rc) return rc; -- 2.39.5