Skip to content

Commit cbc99ba

Browse files
committed
Fix TPM Linux read() return code handling. When using the /dev/tpmrm0 (resource manager) its possible for the read to fail or return would block. This fixes the return code on failures.
1 parent 02c9e8b commit cbc99ba

File tree

1 file changed

+42
-24
lines changed

1 file changed

+42
-24
lines changed

src/tpm2_linux.c

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,22 @@ int TPM2_LINUX_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet)
9191
#include <errno.h>
9292
#include <string.h>
9393

94-
94+
/* TPM Device Path Configuration:
95+
* - /dev/tpm0: TPM raw device (default)
96+
* - /dev/tpmrm0: TPM resource manager (requires kernel 5.12+)
97+
* Enabled with WOLFTPM_USE_TPMRM
98+
*/
9599
#ifndef TPM2_LINUX_DEV
96-
#define TPM2_LINUX_DEV "/dev/tpm0"
100+
#ifdef WOLFTPM_USE_TPMRM
101+
#define TPM2_LINUX_DEV "/dev/tpmrm0"
102+
#else
103+
#define TPM2_LINUX_DEV "/dev/tpm0"
104+
#endif
97105
#endif
98106

107+
#ifndef TPM2_LINUX_DEV_POLL_TIMEOUT
99108
#define TPM2_LINUX_DEV_POLL_TIMEOUT -1 /* Infinite time for poll events */
109+
#endif
100110

101111
/* Linux kernels older than v4.20 (before December 2018) do not support
102112
* partial reads. The only way to receive a complete response is to read
@@ -111,7 +121,7 @@ int TPM2_LINUX_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet)
111121
int fd;
112122
int rc_poll, nfds = 1; /* Polling single TPM dev file */
113123
struct pollfd fds;
114-
size_t rspSz = 0;
124+
int rspSz = 0;
115125

116126
#ifdef WOLFTPM_DEBUG_VERBOSE
117127
printf("Command size: %d\n", packet->pos);
@@ -127,47 +137,55 @@ int TPM2_LINUX_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet)
127137
/* Wait for response to be available */
128138
rc_poll = poll(&fds, nfds, TPM2_LINUX_DEV_POLL_TIMEOUT);
129139
if (rc_poll > 0 && fds.revents == POLLIN) {
130-
rspSz = read(fd, packet->buf, packet->size);
140+
ssize_t ret = read(fd, packet->buf, packet->size);
131141
/* The caller parses the TPM_Packet for correctness */
132-
if (rspSz >= TPM2_HEADER_SIZE) {
142+
if (ret >= TPM2_HEADER_SIZE) {
133143
/* Enough bytes for a TPM response */
144+
rspSz = (int)ret;
134145
rc = TPM_RC_SUCCESS;
135146
}
136-
#ifdef DEBUG_WOLFTPM
137147
else if (rspSz == 0) {
138-
printf("Received EOF instead of TPM response.\n");
139-
}
140-
else
141-
{
142-
printf("Failed to read from TPM device %d, got errno %d"
143-
" = %s\n", fd, errno, strerror(errno));
148+
#ifdef DEBUG_WOLFTPM
149+
printf("Received EOF(0) from %s: errno %d = %s\n",
150+
TPM2_LINUX_DEV, errno, strerror(errno));
151+
#endif
144152
}
153+
else {
154+
#ifdef DEBUG_WOLFTPM
155+
printf("Failed to read from %s: errno %d = %s\n",
156+
TPM2_LINUX_DEV, errno, strerror(errno));
145157
#endif
158+
rc = TPM_RC_FAILURE;
159+
}
146160
}
147-
#ifdef WOLFTPM_DEBUG_VERBOSE
148161
else {
149-
printf("Failed to get a response from fd %d, got errno %d ="
150-
"%s\n", fd, errno, strerror(errno));
162+
#ifdef DEBUG_WOLFTPM
163+
printf("Failed poll on %s: errno %d = %s\n",
164+
TPM2_LINUX_DEV, errno, strerror(errno));
165+
#endif
166+
rc = TPM_RC_FAILURE;
151167
}
152-
#endif
153168
}
154-
#ifdef WOLFTPM_DEBUG_VERBOSE
155169
else {
156-
printf("Failed to send the TPM command to fd %d, got errno %d ="
157-
"%s\n", fd, errno, strerror(errno));
158-
}
170+
#ifdef DEBUG_WOLFTPM
171+
printf("Failed write to %s: errno %d = %s\n",
172+
TPM2_LINUX_DEV, errno, strerror(errno));
159173
#endif
174+
rc = TPM_RC_FAILURE;
175+
}
160176

161177
close(fd);
162178
}
163-
#ifdef DEBUG_WOLFTPM
164179
else if (fd == -1 && errno == EACCES) {
165-
printf("Permission denied. Use sudo or change the user group.\n");
180+
printf("Permission denied on %s\n"
181+
"Use sudo or add tss group to user.\n", TPM2_LINUX_DEV);
166182
}
167183
else {
168-
perror("Failed to open device");
184+
#ifdef DEBUG_WOLFTPM
185+
printf("Failed to open %s: errno %d = %s\n",
186+
TPM2_LINUX_DEV, errno, strerror(errno));
187+
#endif
169188
}
170-
#endif
171189

172190
#ifdef WOLFTPM_DEBUG_VERBOSE
173191
if (rspSz > 0) {

0 commit comments

Comments
 (0)