]> git.proxmox.com Git - swtpm.git/commitdiff
swtpm: Simplify the reading of TPM commands
authorStefan Berger <stefanb@linux.vnet.ibm.com>
Fri, 5 Jul 2019 15:09:12 +0000 (11:09 -0400)
committerStefan Berger <stefanb@us.ibm.com>
Fri, 5 Jul 2019 16:23:30 +0000 (12:23 -0400)
Simplify the readin of TPM commands to always read all available
bytes from a file descriptor.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
src/swtpm/mainloop.c
src/swtpm/swtpm_io.c
src/swtpm/swtpm_io.h

index 0a35320ed7a05f6302ff09dd10bf7405a87dfd1e..7d7015611f61da9275c5e957b9e2c8d53cfa642a 100644 (file)
@@ -92,7 +92,6 @@ int mainLoop(struct mainLoopParams *mlp,
     uint32_t            rTotal = 0;                /* total allocated bytes */
     int                 ctrlfd;
     int                 ctrlclntfd;
-    bool                readall;
     int                 sockfd;
     int                 ready;
 
@@ -204,9 +203,8 @@ int mainLoop(struct mainLoopParams *mlp,
 
             /* Read the command.  The number of bytes is determined by 'paramSize' in the stream */
             if (rc == 0) {
-                readall = (mlp->flags & MAIN_LOOP_FLAG_READALL);
                 rc = SWTPM_IO_Read(&connection_fd, command, &command_length,
-                                   max_command_length, readall);
+                                   max_command_length);
                 if (rc != 0) {
                     /* connection broke */
                     SWTPM_IO_Disconnect(&connection_fd);
index 09efb1410098fb5fc9ac1b754605ec47bbf231bc..23668276ca28d1b19de60f13baefaf2e45cb8880 100644 (file)
 #include "swtpm_io.h"
 
 
-/*
-  local prototypes
-*/
-
-static TPM_RESULT SWTPM_IO_ReadBytes(TPM_CONNECTION_FD *connection_fd,
-                                     unsigned char *buffer,
-                                     size_t nbytes);
-
 /*
   global variables
 */
@@ -98,66 +90,30 @@ static int      sock_fd = -1;
 TPM_RESULT SWTPM_IO_Read(TPM_CONNECTION_FD *connection_fd,   /* read/write file descriptor */
                          unsigned char *buffer,   /* output: command stream */
                          uint32_t *bufferLength,  /* output: command stream length */
-                         size_t bufferSize,       /* input: max size of output buffer */
-                         bool readall)
+                         size_t bufferSize)       /* input: max size of output buffer */
 {
-    TPM_RESULT          rc = 0;
-    uint32_t            headerSize;     /* minimum required bytes in command through paramSize */
-    uint32_t            paramSize;      /* from command stream */
     ssize_t             n;
 
-    if (rc == 0) {
-        if (connection_fd->fd < 0) {
-            TPM_DEBUG("SWTPM_IO_Read: Passed file descriptor is invalid\n");
-            rc = TPM_IOERROR;
-        }
+    if (connection_fd->fd < 0) {
+        TPM_DEBUG("SWTPM_IO_Read: Passed file descriptor is invalid\n");
+        return TPM_IOERROR;
     }
 
-    /* check that the buffer can at least fit the command through the paramSize */
-    if (rc == 0) {
-        headerSize = sizeof(TPM_TAG) + sizeof(uint32_t);
-        if (bufferSize < headerSize) {
-            TPM_DEBUG("SWTPM_IO_Read: Error, buffer size %lu less than minimum %u\n",
-                   (unsigned long)bufferSize, headerSize);
-            rc = TPM_SIZE;
-        }
-    }
-    if (rc == 0 && readall) {
+    while (true) {
         n = read(connection_fd->fd, buffer, bufferSize);
-        if (n > 0)
+        if (n < 0 && errno == EINTR)
+            continue;
+        if (n > 0) {
             *bufferLength = n;
-        else
-            rc = TPM_IOERROR;
-        goto out;
-    }
-    /* read the command through the paramSize from the socket stream */
-    if (rc == 0) {
-        rc = SWTPM_IO_ReadBytes(connection_fd, buffer, headerSize);
-    }
-    if (rc == 0) {
-        TPM_PrintAll("  SWTPM_IO_Read: through paramSize", "  ",
-                     buffer, headerSize);
-        /* extract the paramSize value, last field in header */
-        paramSize = LOAD32(buffer, headerSize - sizeof(uint32_t));
-        *bufferLength = headerSize + paramSize - (sizeof(TPM_TAG) + sizeof(uint32_t));
-        if (bufferSize < *bufferLength) {
-            TPM_DEBUG("SWTPM_IO_Read: Error, buffer size %lu is less than required %u\n",
-                   (unsigned long)bufferSize, *bufferLength);
-            rc = TPM_SIZE;
+            break;
+        } else {
+           return TPM_IOERROR;
         }
     }
-    /* read the rest of the command (already read tag and paramSize) */
-    if (rc == 0) {
-        rc = SWTPM_IO_ReadBytes(connection_fd,
-                                buffer + headerSize,
-                                paramSize - (sizeof(TPM_TAG) + sizeof(uint32_t)));
-    }
 
-out:
-    if (rc == 0) {
-        TPM_PrintAll(" SWTPM_IO_Read:", " ", buffer, *bufferLength);
-    }
-    return rc;
+    TPM_PrintAll(" SWTPM_IO_Read:", " ", buffer, *bufferLength);
+
+    return 0;
 }
 
 
@@ -240,55 +196,6 @@ TPM_RESULT SWTPM_IO_Connect(TPM_CONNECTION_FD *connection_fd,     /* read/write
     return rc;
 }
 
-/* SWTPM_IO_ReadBytes() reads nbytes from connection_fd and puts them in buffer.
-
-   The buffer has already been checked for sufficient size.
-
-   This is the Unix platform dependent socket version.
-*/
-
-static TPM_RESULT SWTPM_IO_ReadBytes(TPM_CONNECTION_FD *connection_fd,    /* read/write file descriptor */
-                                     unsigned char *buffer,
-                                     size_t nbytes)
-{
-    TPM_RESULT rc = 0;
-    ssize_t nread = 0;
-    size_t nleft = nbytes;
-    unsigned char *start = buffer;
-
-    TPM_DEBUG("  SWTPM_IO_ReadBytes: Reading %lu bytes\n",
-              (unsigned long)nbytes);
-    /* read() is unspecified with nbytes too large */
-    if (rc == 0) {
-        if (nleft > SSIZE_MAX) {
-            rc = TPM_BAD_PARAMETER;
-        }
-    }
-    while ((rc == 0) && (nleft > 0)) {
-        nread = read(connection_fd->fd, buffer, nleft);
-        if (nread > 0) {
-            nleft -= nread;
-            buffer += nread;
-        }
-        else if (nread < 0) {       /* error */
-            TPM_DEBUG("SWTPM_IO_ReadBytes: Error, read() error %d %s\n",
-                   errno, strerror(errno));
-            rc = TPM_IOERROR;
-        }
-        else if (nread == 0) {          /* EOF */
-            TPM_DEBUG("SWTPM_IO_ReadBytes: Error, read EOF, read %lu bytes\n",
-                   (unsigned long)(nbytes - nleft));
-            rc = TPM_IOERROR;
-        }
-    }
-
-    if (rc == 0) {
-        TPM_PrintAll(" SWTPM_IO_ReadBytes:", " ", start, nbytes - nleft);
-    }
-
-    return rc;
-}
-
 /* SWTPM_IO_Write() writes 'buffer_length' bytes to the host.
 
    This is the Unix platform dependent socket version.
index 874f4d84e630e3a8b7bac69d35cf9ffd655b605f..d2a14a4a4f64bb3db9916df8d974ee9db5749e96 100644 (file)
@@ -50,8 +50,7 @@ TPM_RESULT SWTPM_IO_Connect(TPM_CONNECTION_FD *connection_fd,
 TPM_RESULT SWTPM_IO_Read(TPM_CONNECTION_FD *connection_fd,
                          unsigned char *buffer,
                          uint32_t *paramSize,
-                         size_t buffer_size,
-                         bool readall);
+                         size_t buffer_size);
 TPM_RESULT SWTPM_IO_Write(TPM_CONNECTION_FD *connection_fd,
                           const unsigned char *buffer,
                           size_t buffer_length);