Blame SOURCES/0008-Fix-file-descriptor-leak-when-tcti-initialization-fa.patch

6a14f3
From 68a7867198c84111bac3068c33d28e320df6a6f6 Mon Sep 17 00:00:00 2001
6a14f3
From: JerryDevis <seclab@huawei.com>
6a14f3
Date: Wed, 13 Oct 2021 11:26:03 +0800
6a14f3
Subject: [PATCH 08/23] Fix file descriptor leak when tcti initialization
6a14f3
 failed
6a14f3
6a14f3
Signed-off-by: JerryDevis <seclab@huawei.com>
6a14f3
---
6a14f3
 src/tss2-tcti/tcti-device.c | 18 ++++++++++++++++--
6a14f3
 1 file changed, 16 insertions(+), 2 deletions(-)
6a14f3
6a14f3
diff --git a/src/tss2-tcti/tcti-device.c b/src/tss2-tcti/tcti-device.c
6a14f3
index 94db070c..364297be 100644
6a14f3
--- a/src/tss2-tcti/tcti-device.c
6a14f3
+++ b/src/tss2-tcti/tcti-device.c
6a14f3
@@ -309,6 +309,16 @@ out:
6a14f3
     return rc;
6a14f3
 }
6a14f3
 
6a14f3
+static void close_tpm(int *fd)
6a14f3
+{
6a14f3
+    if (fd == NULL || *fd < 0) {
6a14f3
+        return;
6a14f3
+    }
6a14f3
+
6a14f3
+    close(*fd);
6a14f3
+    *fd = -1;
6a14f3
+}
6a14f3
+
6a14f3
 void
6a14f3
 tcti_device_finalize (
6a14f3
     TSS2_TCTI_CONTEXT *tctiContext)
6a14f3
@@ -319,7 +329,7 @@ tcti_device_finalize (
6a14f3
     if (tcti_dev == NULL) {
6a14f3
         return;
6a14f3
     }
6a14f3
-    close (tcti_dev->fd);
6a14f3
+    close_tpm (&tcti_dev->fd);
6a14f3
     tcti_common->state = TCTI_STATE_FINAL;
6a14f3
 }
6a14f3
 
6a14f3
@@ -455,6 +465,7 @@ Tss2_Tcti_Device_Init (
6a14f3
     ssize_t sz = write_all (tcti_dev->fd, cmd, sizeof(cmd));
6a14f3
     if (sz < 0 || sz != sizeof(cmd)) {
6a14f3
         LOG_ERROR ("Could not probe device for partial response read support");
6a14f3
+        close_tpm (&tcti_dev->fd);
6a14f3
         return TSS2_TCTI_RC_IO_ERROR;
6a14f3
     }
6a14f3
     LOG_DEBUG ("Command sent, reading header");
6a14f3
@@ -465,12 +476,14 @@ Tss2_Tcti_Device_Init (
6a14f3
     if (rc_poll < 0 || rc_poll == 0) {
6a14f3
         LOG_ERROR ("Failed to poll for response from fd %d, rc %d, errno %d: %s",
6a14f3
                    tcti_dev->fd, rc_poll, errno, strerror(errno));
6a14f3
+        close_tpm (&tcti_dev->fd);
6a14f3
         return TSS2_TCTI_RC_IO_ERROR;
6a14f3
     } else if (fds.revents == POLLIN) {
6a14f3
         TEMP_RETRY (sz, read (tcti_dev->fd, rsp, TPM_HEADER_SIZE));
6a14f3
         if (sz < 0 || sz != TPM_HEADER_SIZE) {
6a14f3
             LOG_ERROR ("Failed to read response header fd %d, got errno %d: %s",
6a14f3
                        tcti_dev->fd, errno, strerror (errno));
6a14f3
+            close_tpm (&tcti_dev->fd);
6a14f3
             return TSS2_TCTI_RC_IO_ERROR;
6a14f3
         }
6a14f3
     }
6a14f3
@@ -482,6 +495,7 @@ Tss2_Tcti_Device_Init (
6a14f3
     if (rc_poll < 0) {
6a14f3
         LOG_DEBUG ("Failed to poll for response from fd %d, rc %d, errno %d: %s",
6a14f3
                    tcti_dev->fd, rc_poll, errno, strerror(errno));
6a14f3
+        close_tpm (&tcti_dev->fd);
6a14f3
         return TSS2_TCTI_RC_IO_ERROR;
6a14f3
 	} else if (rc_poll == 0) {
6a14f3
         LOG_ERROR ("timeout waiting for response from fd %d", tcti_dev->fd);
6a14f3
@@ -495,7 +509,7 @@ Tss2_Tcti_Device_Init (
6a14f3
         LOG_DEBUG ("Failed to get response tail fd %d, got errno %d: %s",
6a14f3
                    tcti_dev->fd, errno, strerror (errno));
6a14f3
         tcti_common->partial_read_supported = 0;
6a14f3
-        close(tcti_dev->fd);
6a14f3
+        close_tpm (&tcti_dev->fd);
6a14f3
         tcti_dev->fd = open_tpm (used_conf);
6a14f3
         if (tcti_dev->fd < 0) {
6a14f3
             LOG_ERROR ("Failed to open specified TCTI device file %s: %s",
6a14f3
-- 
6a14f3
2.34.3
6a14f3