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

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