Skip to content

Commit a2f52d4

Browse files
committed
Cleanups from copilot review. Fix m33mu test.
1 parent 918235e commit a2f52d4

File tree

9 files changed

+80
-31
lines changed

9 files changed

+80
-31
lines changed

examples/run_examples.sh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,11 @@ run_tpm_tls_client() { # Usage: run_tpm_tls_client [ecc/rsa] [tpmargs] [tlsversi
449449
echo -e "./examples/server/server -v $3 -p $port -w -g -A ./certs/tpm-ca-$1-cert.pem -R $READY_FILE"
450450
./examples/server/server -v $3 -p $port -w -g -A ./certs/tpm-ca-$1-cert.pem -R "$READY_FILE" >> $TPMPWD/run.out 2>&1 &
451451
popd >> $TPMPWD/run.out 2>&1
452-
wait_for_ready "$READY_FILE" 500
452+
if ! wait_for_ready "$READY_FILE" 500; then
453+
echo -e "wolfSSL server failed to start for $1 $2"
454+
rm -f "$READY_FILE"
455+
exit 1
456+
fi
453457
rm -f "$READY_FILE"
454458

455459
echo -e "./examples/tls/tls_client -p=$port -$1 $2"
@@ -464,7 +468,10 @@ run_tpm_tls_server() { # Usage: run_tpm_tls_server [ecc/rsa] [tpmargs] [tlsversi
464468

465469
echo -e "./examples/tls/tls_server -p=$port -$1 $2"
466470
./examples/tls/tls_server -p=$port -$1 $2 >> $TPMPWD/run.out 2>&1 &
467-
wait_for_port "$port" 500
471+
if ! wait_for_port "$port" 500; then
472+
echo -e "TPM TLS server failed to start on port $port for $1 $2"
473+
exit 1
474+
fi
468475
pushd $WOLFSSL_PATH >> $TPMPWD/run.out 2>&1
469476

470477
echo -e "./examples/client/client -v $3 -p $port -w -g -A ./certs/tpm-ca-$1-cert.pem $4"

hal/tpm_io_fwtpm.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@
4545
#include <sys/mman.h>
4646
#include <semaphore.h>
4747

48-
/* Static client context (one connection per process) */
48+
/* Static client context (one connection per process).
49+
* By design, only one fwTPM server instance is connected per process.
50+
* Thread safety is provided by TPM2_AcquireLock in tpm2_tis.c. */
4951
static FWTPM_TIS_CLIENT_CTX gFwtpmClient;
5052
static int gFwtpmClientInit = 0;
5153

scripts/fwtpm_emu_test.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ if [ -z "$M33MU" ] || [ ! -x "$M33MU" ]; then
6464
exit 1
6565
fi
6666

67+
# Verify m33mu can actually run (catch missing shared libs)
68+
if ! "$M33MU" --version > /dev/null 2>&1; then
69+
echo "ERROR: m33mu found at $M33MU but failed to execute."
70+
echo " Checking shared library dependencies:"
71+
ldd "$M33MU" 2>&1 | grep -i "not found" || echo " (no missing libraries detected)"
72+
echo " File type: $(file "$M33MU")"
73+
exit 1
74+
fi
75+
6776
echo "=== fwTPM Emulator Test ==="
6877
echo " m33mu: $M33MU"
6978
echo " TZEN: $TZEN"
@@ -94,8 +103,10 @@ fi
94103

95104
echo "Running in m33mu emulator..."
96105
LOG="/tmp/fwtpm_emu_test.log"
106+
set +e
97107
$M33MU $M33MU_ARGS "$ELF" > "$LOG" 2>&1
98108
RC=$?
109+
set -e
99110

100111
# Show UART output (filter emulator noise)
101112
echo ""

src/fwtpm/fwtpm_tis.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,12 @@ static void TisHandleRegAccess(FWTPM_CTX* ctx, FWTPM_TIS_REGS* regs)
309309
break;
310310
}
311311

312+
/* Clamp len for scalar registers (max 4 bytes) and zero-fill
313+
* to prevent stale data in reg_data from being read back */
314+
if (len > 4) {
315+
len = 4;
316+
}
317+
XMEMSET(regs->reg_data, 0, len);
312318
/* Pack value into reg_data (little-endian, matching TIS spec) */
313319
if (len >= 1) regs->reg_data[0] = (BYTE)(val);
314320
if (len >= 2) regs->reg_data[1] = (BYTE)(val >> 8);

src/tpm2.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,11 @@ TPM_RC TPM2_Cleanup(TPM2_CTX* ctx)
816816
close(ctx->fd);
817817
#endif
818818

819+
#ifdef WOLFTPM_SWTPM_UART
820+
/* Close the persistent UART connection */
821+
TPM2_SwtpmCloseUART(ctx);
822+
#endif
823+
819824
return TPM_RC_SUCCESS;
820825
}
821826

src/tpm2_crypto.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,8 @@ int TPM2_HmacCompute(
396396
int hashType;
397397
int dSz;
398398

399-
if (digest == NULL || (data == NULL && dataSz > 0)) {
399+
if (digest == NULL || (key == NULL && keySz > 0) ||
400+
(data == NULL && dataSz > 0)) {
400401
return BAD_FUNC_ARG;
401402
}
402403

src/tpm2_swtpm.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,8 @@ static TPM_RC SwTpmDisconnect(TPM2_CTX* ctx)
352352

353353
#ifdef WOLFTPM_SWTPM_UART
354354
/* UART: keep the port open for the next command.
355-
* The SESSION_END tells the server the command sequence is done. */
355+
* The SESSION_END tells the server the command sequence is done.
356+
* Final cleanup of the UART FD is handled in TPM2_SwtpmCloseUART. */
356357
(void)ctx;
357358
#else
358359
if (0 != close(ctx->tcpCtx.fd)) {
@@ -479,4 +480,15 @@ int TPM2_SWTPM_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet)
479480

480481
return rc;
481482
}
483+
484+
#ifdef WOLFTPM_SWTPM_UART
485+
/* Close the persistent UART FD during final TPM context cleanup */
486+
void TPM2_SwtpmCloseUART(TPM2_CTX* ctx)
487+
{
488+
if (ctx != NULL && ctx->tcpCtx.fd >= 0) {
489+
close(ctx->tcpCtx.fd);
490+
ctx->tcpCtx.fd = -1;
491+
}
492+
}
493+
#endif
482494
#endif /* WOLFTPM_SWTPM */

tests/fwtpm_check.sh

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -84,42 +84,35 @@ check_wolfssl_options() {
8484
}
8585

8686
ensure_wolfssl() {
87+
local src
88+
8789
# 1. Explicit WOLFSSL_PATH from environment
8890
if [ -n "$WOLFSSL_PATH" ] && check_wolfssl_options "$WOLFSSL_PATH"; then
8991
echo " wolfSSL: using $WOLFSSL_PATH"
9092
return 0
9193
fi
9294

9395
# 2. Reuse prior /tmp build
94-
local src="/tmp/wolfssl-fwtpm"
96+
src="/tmp/wolfssl-fwtpm"
9597
if [ -d "$src" ] && check_wolfssl_options "$src"; then
9698
WOLFSSL_PATH="$src"
9799
echo " wolfSSL: using $WOLFSSL_PATH"
98100
return 0
99101
fi
100102

101-
# 3. Clone and build to /tmp (no sudo)
102-
echo " Building wolfSSL to $src"
103-
if [ ! -d "$src/.git" ]; then
104-
rm -rf "$src"
105-
git clone --depth 1 https://github.com/wolfssl/wolfssl.git "$src" \
106-
> /tmp/wolfssl-fwtpm-clone.log 2>&1 || return 1
107-
fi
108-
if ! check_wolfssl_options "$src"; then
109-
(cd "$src" && \
110-
./autogen.sh > /dev/null 2>&1 && \
111-
./configure \
112-
--enable-wolftpm --enable-pkcallbacks --enable-keygen \
113-
CFLAGS="-DWC_RSA_NO_PADDING" \
114-
> /tmp/wolfssl-fwtpm-configure.log 2>&1 && \
115-
make > /tmp/wolfssl-fwtpm-build.log 2>&1) || {
116-
echo " wolfSSL build failed -- see /tmp/wolfssl-fwtpm-*.log"
117-
return 1
118-
}
119-
fi
120-
WOLFSSL_PATH="$src"
121-
echo " wolfSSL: built at $WOLFSSL_PATH"
122-
return 0
103+
# 3. Check system install paths
104+
for src in /usr/local /usr /opt/homebrew /opt/local; do
105+
if check_wolfssl_options "$src"; then
106+
WOLFSSL_PATH="$src"
107+
echo " wolfSSL: using system install at $WOLFSSL_PATH"
108+
return 0
109+
fi
110+
done
111+
112+
echo " wolfSSL not available with required options."
113+
echo " Set WOLFSSL_PATH or install wolfSSL system-wide."
114+
echo " Skipping TLS-dependent tests."
115+
return 1
123116
}
124117

125118
# --- Cleanup ---
@@ -260,9 +253,16 @@ if [ $IS_FWTPM_MODE -eq 1 ]; then
260253
rm -f "$BUILD_DIR"/certs/tpm-*-cert.pem "$BUILD_DIR"/certs/tpm-*-cert.csr
261254
rm -f "$BUILD_DIR"/certs/server-*-cert.pem "$BUILD_DIR"/certs/client-*-cert.pem
262255

263-
# Kill any orphaned servers from prior crashed runs (intentional pre-flight)
264-
killall fwtpm_server 2>/dev/null || true
265-
sleep 0.3
256+
# Clean up any stale PID files from prior crashed runs
257+
for stale_pid_file in /tmp/fwtpm_check_*.pid; do
258+
[ -f "$stale_pid_file" ] || continue
259+
stale_pid="$(cat "$stale_pid_file" 2>/dev/null)"
260+
if [ -n "$stale_pid" ] && kill -0 "$stale_pid" 2>/dev/null; then
261+
kill "$stale_pid" 2>/dev/null
262+
sleep 0.3
263+
fi
264+
rm -f "$stale_pid_file"
265+
done
266266

267267
if [ $HAS_GETENV -eq 1 ] && [ $IS_SWTPM_MODE -eq 1 ]; then
268268
FWTPM_PORT=$(pick_available_port)

wolftpm/tpm2_swtpm.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747
/* TPM2 IO for using TPM through a Socket connection */
4848
WOLFTPM_LOCAL int TPM2_SWTPM_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet);
4949

50+
#ifdef WOLFTPM_SWTPM_UART
51+
/* Close the persistent UART FD during final TPM context cleanup */
52+
WOLFTPM_LOCAL void TPM2_SwtpmCloseUART(TPM2_CTX* ctx);
53+
#endif
54+
5055
#ifdef __cplusplus
5156
} /* extern "C" */
5257
#endif

0 commit comments

Comments
 (0)