|
64 | 64 | #ifdef WOLFTPM_SWTPM_UART |
65 | 65 | #include <fcntl.h> |
66 | 66 | #include <termios.h> |
| 67 | +#include <sys/stat.h> |
67 | 68 | #endif |
68 | 69 |
|
69 | 70 | #include <wolftpm/tpm2_socket.h> |
@@ -99,17 +100,23 @@ static TPM_RC SwTpmTransmit(TPM2_CTX* ctx, const void* buffer, ssize_t bufSz) |
99 | 100 | return BAD_FUNC_ARG; |
100 | 101 | } |
101 | 102 |
|
102 | | - wrc = write(ctx->tcpCtx.fd, buffer, bufSz); |
103 | | - if (bufSz != wrc) { |
104 | | - rc = TPM_RC_FAILURE; |
105 | | - } |
106 | | - |
107 | | -#ifdef WOLFTPM_DEBUG_VERBOSE |
108 | | - if (wrc < 0) { |
109 | | - printf("Failed to send the TPM command to fd %d, got errno %d =" |
110 | | - "%s\n", ctx->tcpCtx.fd, errno, strerror(errno)); |
| 103 | + { |
| 104 | + const char* ptr = (const char*)buffer; |
| 105 | + int remaining = bufSz; |
| 106 | + while (remaining > 0) { |
| 107 | + wrc = write(ctx->tcpCtx.fd, ptr, remaining); |
| 108 | + if (wrc <= 0) { |
| 109 | + #ifdef WOLFTPM_DEBUG_VERBOSE |
| 110 | + printf("Failed to send the TPM command to fd %d, got errno %d =" |
| 111 | + "%s\n", ctx->tcpCtx.fd, errno, strerror(errno)); |
| 112 | + #endif |
| 113 | + rc = TPM_RC_FAILURE; |
| 114 | + break; |
| 115 | + } |
| 116 | + remaining -= (int)wrc; |
| 117 | + ptr += wrc; |
| 118 | + } |
111 | 119 | } |
112 | | -#endif |
113 | 120 |
|
114 | 121 | return rc; |
115 | 122 | } |
@@ -170,13 +177,21 @@ static TPM_RC SwTpmConnect(TPM2_CTX* ctx, const char* host, const char* port) |
170 | 177 | /* Note: TPM2_SWTPM_HOST env var is checked by caller |
171 | 178 | * (TPM2_SWTPM_SendCommand) before invoking SwTpmConnect */ |
172 | 179 |
|
173 | | - fd = open(host, O_RDWR | O_NOCTTY); |
| 180 | + fd = open(host, O_RDWR | O_NOCTTY | O_CLOEXEC | O_NOFOLLOW); |
174 | 181 | if (fd < 0) { |
175 | 182 | #ifdef DEBUG_WOLFTPM |
176 | 183 | printf("Failed to open UART device %s: %s\n", host, strerror(errno)); |
177 | 184 | #endif |
178 | 185 | return TPM_RC_FAILURE; |
179 | 186 | } |
| 187 | + /* Verify the opened path is a character device */ |
| 188 | + { |
| 189 | + struct stat st; |
| 190 | + if (fstat(fd, &st) != 0 || !S_ISCHR(st.st_mode)) { |
| 191 | + close(fd); |
| 192 | + return TPM_RC_FAILURE; |
| 193 | + } |
| 194 | + } |
180 | 195 |
|
181 | 196 | /* Configure serial port: 8N1, raw mode, no flow control */ |
182 | 197 | XMEMSET(&tty, 0, sizeof(tty)); |
|
0 commit comments