From dc2895812a8e5bca344a48b3523a7f60d25a3ebb Mon Sep 17 00:00:00 2001 From: Stefan Berger Date: Fri, 5 Jul 2019 16:43:50 -0400 Subject: [PATCH] swtpm: Have SWTPM_IO_Read read at least the 10 bytes from the TPM header Have SWTPM_IO_Read() read at leat the 10 bytes from the (regular) TPM header. This solves a particular problem with the TPM 2 header prefix for which we will add support in the subsequent patch. In the prefix case the IBM TSS 2 stack sends 4 bytes for the command, then 1 byte for the locality, and then 4 bytes for the length indicator followed by the command. If we just read once we would only then get 4 bytes. Reading 10 bytes gets all of these plus the sub- sequent TPM command or the whole TPM command in case this prefix header is missing. Signed-off-by: Stefan Berger --- src/swtpm/swtpm_io.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/swtpm/swtpm_io.c b/src/swtpm/swtpm_io.c index 77dcf2b..1318477 100644 --- a/src/swtpm/swtpm_io.c +++ b/src/swtpm/swtpm_io.c @@ -64,7 +64,7 @@ #include "logging.h" #include "swtpm_debug.h" #include "swtpm_io.h" - +#include "tpmlib.h" /* global variables @@ -90,6 +90,7 @@ TPM_RESULT SWTPM_IO_Read(TPM_CONNECTION_FD *connection_fd, /* read/write file size_t bufferSize) /* input: max size of output buffer */ { ssize_t n; + size_t offset = 0; if (connection_fd->fd < 0) { TPM_DEBUG("SWTPM_IO_Read: Passed file descriptor is invalid\n"); @@ -97,17 +98,20 @@ TPM_RESULT SWTPM_IO_Read(TPM_CONNECTION_FD *connection_fd, /* read/write file } while (true) { - n = read(connection_fd->fd, buffer, bufferSize); + n = read(connection_fd->fd, &buffer[offset], bufferSize - offset); if (n < 0 && errno == EINTR) continue; if (n > 0) { - *bufferLength = n; + offset += n; + if (offset < sizeof(struct tpm_req_header)) + continue; break; } else { return TPM_IOERROR; } } + *bufferLength = offset; TPM_PrintAll(" SWTPM_IO_Read:", " ", buffer, *bufferLength); return 0;