Inline fwTPM TIS atomics and semaphore name helpers

pull/588/head
Aidan Garske 2026-09-02 11:59:24 -07:00
parent ab1bdb3602
commit a7b1aa6c3b
11 changed files with 77 additions and 223 deletions

View File

@ -421,11 +421,8 @@ if(WOLFTPM_FWTPM AND UNIX)
endif()
endif()
if(WOLFTPM_FWTPM_TIS AND BUILD_WOLFTPM_LIB)
target_sources(wolftpm PRIVATE src/fwtpm/fwtpm_tis_sem.c)
if(UNIX)
target_link_libraries(wolftpm PRIVATE ${WOLFTPM_FWTPM_TIS_LIBS})
endif()
if(WOLFTPM_FWTPM_TIS AND BUILD_WOLFTPM_LIB AND UNIX)
target_link_libraries(wolftpm PRIVATE ${WOLFTPM_FWTPM_TIS_LIBS})
endif()
# Examples
@ -546,7 +543,6 @@ if(WOLFTPM_FWTPM)
if(WOLFTPM_FWTPM_TIS)
list(APPEND FWTPM_SERVER_SOURCES
src/fwtpm/fwtpm_tis.c
src/fwtpm/fwtpm_tis_sem.c
src/fwtpm/fwtpm_tis_shm.c
)
endif()
@ -618,21 +614,6 @@ if(WOLFTPM_FWTPM)
target_link_libraries(fwtpm_hal_unit_test PRIVATE
${WOLFTPM_FWTPM_TIS_LIBS})
endif()
add_executable(fwtpm_tis_sem_unit_test
tests/fwtpm_tis_sem_unit_tests.c
src/fwtpm/fwtpm_tis_sem.c
)
target_compile_definitions(fwtpm_tis_sem_unit_test PRIVATE
"WOLFTPM_FWTPM_TIS"
)
target_link_libraries(fwtpm_tis_sem_unit_test PRIVATE
wolftpm_wolfssl_dep
)
target_include_directories(fwtpm_tis_sem_unit_test PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
)
endif()
# fwTPM fuzz target (libFuzzer)
@ -665,10 +646,6 @@ if(WOLFTPM_FWTPM)
COMMAND fwtpm_hal_unit_test
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)
add_test(NAME fwtpm_tis_sem_unit_test
COMMAND fwtpm_tis_sem_unit_test
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)
endif()
message(STATUS "fwTPM server: enabled")

View File

@ -45,7 +45,6 @@ fwTPM can replace a hardware TPM for:
| `fwtpm_io.c` | Transport layer -- SWTPM TCP socket protocol (default) |
| `fwtpm_nv.c` | NV storage -- file-based (default); HAL-abstracted, with a built-in append-only mode for write-once flash |
| `fwtpm_tis.c` | TIS register state machine (transport-agnostic) |
| `fwtpm_tis_sem.c` | POSIX semaphore-name derivation shared by server and client HAL |
| `fwtpm_tis_shm.c` | POSIX shared memory + semaphore TIS transport |
| `fwtpm_main.c` | Server entry point, CLI argument parsing |
| `tpm2_util.c` | Shared utilities (hash helpers, ForceZero, PrintBin) |

View File

@ -79,20 +79,27 @@ static void FWTPM_TIS_ClientUnlock(int fd)
} while (rc != 0 && errno == EINTR);
}
static UINT32 FWTPM_TIS_LoadMagic(const FWTPM_TIS_REGS* shm)
{
#if defined(__GNUC__) || defined(__clang__)
return __atomic_load_n(&shm->magic, __ATOMIC_ACQUIRE);
#else
const volatile UINT32* magic = &shm->magic;
return *magic;
#endif
}
static int FWTPM_TIS_ServerActive(const FWTPM_TIS_REGS* shm)
{
return FWTPM_TIS_LoadMagic(shm) == FWTPM_TIS_MAGIC;
return FWTPM_TIS_ATOMIC_LOAD(shm->magic) == FWTPM_TIS_MAGIC;
}
/* Must match the server's naming in fwtpm_tis_shm.c. */
static int FWTPM_TIS_ClientMakeSemNames(uid_t ownerUid, char* semCmd,
size_t semCmdSz, char* semRsp, size_t semRspSz)
{
int cmdLen;
int rspLen;
cmdLen = XSNPRINTF(semCmd, semCmdSz, "%s-%lu", FWTPM_TIS_SEM_CMD,
(unsigned long)ownerUid);
rspLen = XSNPRINTF(semRsp, semRspSz, "%s-%lu", FWTPM_TIS_SEM_RSP,
(unsigned long)ownerUid);
if (cmdLen <= 0 || (size_t)cmdLen >= semCmdSz ||
rspLen <= 0 || (size_t)rspLen >= semRspSz) {
return -1;
}
return 0;
}
static int FWTPM_TIS_ClientValidateShm(const struct stat* st)
@ -219,7 +226,7 @@ int FWTPM_TIS_ClientConnect(FWTPM_TIS_CLIENT_CTX* client)
return TPM_RC_FAILURE;
}
#endif
if (FWTPM_TIS_MakeSemNames((UINT64)st.st_uid, semCmdName,
if (FWTPM_TIS_ClientMakeSemNames(st.st_uid, semCmdName,
sizeof(semCmdName), semRspName, sizeof(semRspName)) != 0) {
#ifdef DEBUG_WOLFTPM
printf("fwTPM HAL: failed to derive semaphore names for uid %lu\n",
@ -240,7 +247,7 @@ int FWTPM_TIS_ClientConnect(FWTPM_TIS_CLIENT_CTX* client)
}
/* Acquire the validity sentinel before reading the published header. */
magic = FWTPM_TIS_LoadMagic(shm);
magic = FWTPM_TIS_ATOMIC_LOAD(shm->magic);
if (magic != FWTPM_TIS_MAGIC ||
shm->version != FWTPM_TIS_VERSION) {
#ifdef DEBUG_WOLFTPM

View File

@ -68,15 +68,6 @@ static UINT32 TisBuildSts(BYTE stsFlags, UINT16 burstCount)
return ((UINT32)burstCount << 8) | (UINT32)stsFlags;
}
static void TisPublishMagic(FWTPM_TIS_REGS* regs)
{
#if defined(__GNUC__) || defined(__clang__)
__atomic_store_n(&regs->magic, FWTPM_TIS_MAGIC, __ATOMIC_RELEASE);
#else
regs->magic = FWTPM_TIS_MAGIC;
#endif
}
/* Handle a single TIS register access */
static void TisHandleRegAccess(FWTPM_CTX* ctx, FWTPM_TIS_REGS* regs)
{
@ -459,8 +450,8 @@ int FWTPM_TIS_Init(FWTPM_CTX* ctx)
regs->did_vid = FWTPM_TIS_DID_VID_VAL;
regs->rid = FWTPM_VERSION_PATCH;
/* Release-publish the validity sentinel after all client-visible state. */
TisPublishMagic(regs);
/* Publish the validity sentinel only after all client-visible state. */
FWTPM_TIS_ATOMIC_STORE(regs->magic, FWTPM_TIS_MAGIC);
/* Auto power-on in TIS mode (no platform port to signal power) */
ctx->powerOn = 1;

View File

@ -1,64 +0,0 @@
/* fwtpm_tis_sem.c
*
* Copyright (C) 2006-2026 wolfSSL Inc.
*
* This file is part of wolfTPM.
*
* wolfTPM is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfTPM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <wolftpm/tpm2_types.h>
#if defined(WOLFTPM_FWTPM_TIS) || defined(WOLFTPM_FWTPM_HAL)
#include <wolftpm/fwtpm/fwtpm_tis.h>
static int TisMakeSemName(const char* base, UINT64 ownerId, char* name,
size_t nameSz)
{
int nameLen;
if (base == NULL || name == NULL || nameSz == 0U) {
return -1;
}
nameLen = XSNPRINTF(name, nameSz, "%s-%016llx", base,
(unsigned long long)ownerId);
if (nameLen <= 0 || (size_t)nameLen >= nameSz) {
return -1;
}
return 0;
}
WOLFTPM_LOCAL int FWTPM_TIS_MakeSemNames(UINT64 ownerUid, char* semCmd,
size_t semCmdSz, char* semRsp, size_t semRspSz)
{
/* Namespacing only, not a cryptographic identity. The authenticated UID
* remains stable when the shared-memory pathname is removed after a
* crash, allowing the next server to collect both semaphore objects. */
UINT64 ownerId = ownerUid * 0x9E3779B185EBCA87ULL;
if (TisMakeSemName(FWTPM_TIS_SEM_CMD, ownerId, semCmd, semCmdSz) != 0 ||
TisMakeSemName(FWTPM_TIS_SEM_RSP, ownerId, semRsp,
semRspSz) != 0) {
return -1;
}
return 0;
}
#endif /* WOLFTPM_FWTPM_TIS || WOLFTPM_FWTPM_HAL */

View File

@ -72,6 +72,24 @@ static void TisShmUnlinkOldEndpoint(const char* semCmdName,
(void)sem_unlink(semRspName);
}
/* Per-UID names keep concurrent users' wakeup semaphores apart. */
static int TisShmMakeSemNames(uid_t ownerUid, char* semCmd, size_t semCmdSz,
char* semRsp, size_t semRspSz)
{
int cmdLen;
int rspLen;
cmdLen = XSNPRINTF(semCmd, semCmdSz, "%s-%lu", FWTPM_TIS_SEM_CMD,
(unsigned long)ownerUid);
rspLen = XSNPRINTF(semRsp, semRspSz, "%s-%lu", FWTPM_TIS_SEM_RSP,
(unsigned long)ownerUid);
if (cmdLen <= 0 || (size_t)cmdLen >= semCmdSz ||
rspLen <= 0 || (size_t)rspLen >= semRspSz) {
return -1;
}
return 0;
}
/* --- HAL Callbacks --- */
static int TisShmInit(void* ctx, FWTPM_TIS_REGS** regs)
@ -140,7 +158,7 @@ static int TisShmInit(void* ctx, FWTPM_TIS_REGS** regs)
(void)unlink(FWTPM_TIS_SHM_PATH);
return -1;
}
if (FWTPM_TIS_MakeSemNames((UINT64)shmStat.st_uid,
if (TisShmMakeSemNames(shmStat.st_uid,
shm->semCmdName, sizeof(shm->semCmdName),
shm->semRspName, sizeof(shm->semRspName)) != 0) {
fprintf(stderr, "fwTPM TIS: failed to derive semaphore names\n");
@ -235,11 +253,7 @@ static void TisShmCleanup(void* ctx)
FWTPM_TIS_SHM_CTX* shm = (FWTPM_TIS_SHM_CTX*)ctx;
if (shm->regs != NULL) {
#if defined(__GNUC__) || defined(__clang__)
__atomic_store_n(&shm->regs->magic, 0U, __ATOMIC_RELEASE);
#else
shm->regs->magic = 0;
#endif
FWTPM_TIS_ATOMIC_STORE(shm->regs->magic, 0U);
TPM2_ForceZero(shm->regs->reg_data,
sizeof(shm->regs->reg_data));
if (shm->semRsp != NULL && shm->semRsp != SEM_FAILED) {

View File

@ -21,14 +21,10 @@ src_fwtpm_fwtpm_server_CPPFLAGS = -DWOLFTPM_FWTPM $(AM_CPPFLAGS)
src_fwtpm_fwtpm_server_LDADD = $(LIB_STATIC_ADD)
if BUILD_FWTPM_TIS
src_fwtpm_fwtpm_server_SOURCES += src/fwtpm/fwtpm_tis.c \
src/fwtpm/fwtpm_tis_sem.c \
src/fwtpm/fwtpm_tis_shm.c
src_fwtpm_fwtpm_server_CFLAGS += -DWOLFTPM_FWTPM_TIS
src_fwtpm_fwtpm_server_CPPFLAGS += -DWOLFTPM_FWTPM_TIS
src_fwtpm_fwtpm_server_LDADD += $(FWTPM_TIS_LIBS)
if !BUILD_FWTPM_ONLY
src_libwolftpm_la_SOURCES += src/fwtpm/fwtpm_tis_sem.c
endif
endif
# fwtpm_server doesn't link libwolftpm.la (it compiles its own copies with

View File

@ -72,8 +72,6 @@ static void TestForceZero(void* mem, word32 len)
}
}
#include "../src/fwtpm/fwtpm_tis_sem.c"
static sem_t* TestSemOpen(const char* name, int oflag, ...)
{
sem_t* sem = sem_open(name, oflag);
@ -167,7 +165,7 @@ static int CreateEndpoint(mode_t mode, off_t sizeAdjust, UINT32 magic,
int rc = -1;
CleanupEndpoint();
if (FWTPM_TIS_MakeSemNames((UINT64)geteuid(), gTestSemCmd,
if (FWTPM_TIS_ClientMakeSemNames(geteuid(), gTestSemCmd,
sizeof(gTestSemCmd), gTestSemRsp, sizeof(gTestSemRsp)) != 0 ||
CreateEndpointFile(gTestShmPath, mode, sizeAdjust, magic,
version) != 0) {
@ -223,7 +221,6 @@ static int ExpectAccepted(void)
FWTPM_TIS_CLIENT_CTX client;
int rc;
/* Default UID-derived names are covered by fwtpm_tis_sem_unit.test. */
if (CreateEndpoint(0600, 0, FWTPM_TIS_MAGIC,
FWTPM_TIS_VERSION) != 0) {
printf("FAIL: trusted endpoint setup\n");
@ -435,22 +432,30 @@ static int TestCustomSemPrefixes(void)
char semRsp[FWTPM_TIS_SEM_NAME_SIZE];
char otherCmd[FWTPM_TIS_SEM_NAME_SIZE];
char otherRsp[FWTPM_TIS_SEM_NAME_SIZE];
UINT64 ownerUid = (UINT64)geteuid();
size_t cmdPrefixSz = XSTRLEN(gTestSemCmdBase);
size_t rspPrefixSz = XSTRLEN(gTestSemRspBase);
char expectedCmd[FWTPM_TIS_SEM_NAME_SIZE];
char expectedRsp[FWTPM_TIS_SEM_NAME_SIZE];
uid_t ownerUid = geteuid();
if (FWTPM_TIS_MakeSemNames(ownerUid, semCmd, sizeof(semCmd), semRsp,
sizeof(semRsp)) != 0 ||
FWTPM_TIS_MakeSemNames(ownerUid + 1U, otherCmd,
(void)snprintf(expectedCmd, sizeof(expectedCmd), "%s-%lu",
gTestSemCmdBase, (unsigned long)ownerUid);
(void)snprintf(expectedRsp, sizeof(expectedRsp), "%s-%lu",
gTestSemRspBase, (unsigned long)ownerUid);
if (FWTPM_TIS_ClientMakeSemNames(ownerUid, semCmd, sizeof(semCmd),
semRsp, sizeof(semRsp)) != 0 ||
FWTPM_TIS_ClientMakeSemNames(ownerUid + 1U, otherCmd,
sizeof(otherCmd), otherRsp, sizeof(otherRsp)) != 0 ||
XSTRNCMP(semCmd, gTestSemCmdBase, cmdPrefixSz) != 0 ||
XSTRNCMP(semRsp, gTestSemRspBase, rspPrefixSz) != 0 ||
semCmd[cmdPrefixSz] != '-' || semRsp[rspPrefixSz] != '-' ||
XSTRCMP(semCmd, expectedCmd) != 0 ||
XSTRCMP(semRsp, expectedRsp) != 0 ||
XSTRCMP(semCmd, otherCmd) == 0 ||
XSTRCMP(semRsp, otherRsp) == 0) {
printf("FAIL: caller semaphore prefixes lost UID namespacing\n");
return 1;
}
if (FWTPM_TIS_ClientMakeSemNames(ownerUid, semCmd, 2U, semRsp,
sizeof(semRsp)) == 0) {
printf("FAIL: accepted a truncated semaphore name\n");
return 1;
}
printf("PASS: namespaced caller-defined semaphore prefixes\n");
return 0;
}

View File

@ -1,71 +0,0 @@
/* fwtpm_tis_sem_unit_tests.c
*
* Copyright (C) 2006-2026 wolfSSL Inc.
*
* This file is part of wolfTPM.
*
* wolfTPM is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfTPM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <wolftpm/tpm2_types.h>
#include <wolftpm/fwtpm/fwtpm_tis.h>
#include <stdio.h>
#include <string.h>
static int TestSemNames(void)
{
const UINT64 uidA = 17U;
const UINT64 uidB = 18U;
char cmdA[FWTPM_TIS_SEM_NAME_SIZE];
char rspA[FWTPM_TIS_SEM_NAME_SIZE];
char cmdB[FWTPM_TIS_SEM_NAME_SIZE];
char rspB[FWTPM_TIS_SEM_NAME_SIZE];
char expectedCmd[FWTPM_TIS_SEM_NAME_SIZE];
char expectedRsp[FWTPM_TIS_SEM_NAME_SIZE];
if (FWTPM_TIS_MakeSemNames(uidA, cmdA, sizeof(cmdA), rspA,
sizeof(rspA)) != 0 ||
FWTPM_TIS_MakeSemNames(uidB, cmdB, sizeof(cmdB), rspB,
sizeof(rspB)) != 0) {
printf("FAIL: could not derive semaphore names\n");
return 1;
}
/* Pinned vector for uid 17: do not recompute the production hash here. */
(void)XSNPRINTF(expectedCmd, sizeof(expectedCmd), "%s-%s",
FWTPM_TIS_SEM_CMD, "81af14c9e4a872f7");
(void)XSNPRINTF(expectedRsp, sizeof(expectedRsp), "%s-%s",
FWTPM_TIS_SEM_RSP, "81af14c9e4a872f7");
if (strcmp(cmdA, expectedCmd) != 0 || strcmp(rspA, expectedRsp) != 0) {
printf("FAIL: semaphore name has the wrong UID suffix\n");
return 1;
}
if (strcmp(cmdA, cmdB) == 0 || strcmp(rspA, rspB) == 0) {
printf("FAIL: different UIDs produced the same semaphore names\n");
return 1;
}
printf("fwTPM TIS semaphore tests: passed\n");
return 0;
}
int main(void)
{
return TestSemNames();
}

View File

@ -38,20 +38,14 @@ tests_fwtpm_unit_test_LDADD = $(LIB_STATIC_ADD)
endif
if BUILD_FWTPM_TIS
check_PROGRAMS += tests/fwtpm_hal_unit.test tests/fwtpm_tis_sem_unit.test
noinst_PROGRAMS += tests/fwtpm_hal_unit.test tests/fwtpm_tis_sem_unit.test
check_PROGRAMS += tests/fwtpm_hal_unit.test
noinst_PROGRAMS += tests/fwtpm_hal_unit.test
tests_fwtpm_hal_unit_test_SOURCES = tests/fwtpm_hal_unit_tests.c
tests_fwtpm_hal_unit_test_CFLAGS = \
-DWOLFTPM_FWTPM_HAL -DWOLFTPM_ADV_IO -DWOLFTPM_EXAMPLE_HAL $(AM_CFLAGS)
tests_fwtpm_hal_unit_test_CPPFLAGS = \
-DWOLFTPM_FWTPM_HAL -DWOLFTPM_ADV_IO -DWOLFTPM_EXAMPLE_HAL $(AM_CPPFLAGS)
tests_fwtpm_hal_unit_test_LDADD = $(FWTPM_TIS_LIBS)
tests_fwtpm_tis_sem_unit_test_SOURCES = \
tests/fwtpm_tis_sem_unit_tests.c \
src/fwtpm/fwtpm_tis_sem.c
tests_fwtpm_tis_sem_unit_test_CFLAGS = -DWOLFTPM_FWTPM_TIS $(AM_CFLAGS)
tests_fwtpm_tis_sem_unit_test_CPPFLAGS = -DWOLFTPM_FWTPM_TIS $(AM_CPPFLAGS)
tests_fwtpm_tis_sem_unit_test_LDADD = $(FWTPM_TIS_LIBS)
endif
# TPM make check: manages server, runs unit.test + run_examples.sh

View File

@ -56,6 +56,18 @@
#define FWTPM_TIS_MAGIC 0x57544953UL /* "WTIS" */
#define FWTPM_TIS_VERSION 2
/* Publish/observe the magic sentinel with release/acquire ordering so a client
* never sees FWTPM_TIS_MAGIC before the header it guards (wc_port.h ladder). */
#if !defined(WOLFSSL_NO_ATOMICS) && defined(__GNUC__) && \
defined(__ATOMIC_RELEASE)
#define FWTPM_TIS_ATOMIC_STORE(x, val) \
__atomic_store_n(&(x), (val), __ATOMIC_RELEASE)
#define FWTPM_TIS_ATOMIC_LOAD(x) __atomic_load_n(&(x), __ATOMIC_ACQUIRE)
#else
#define FWTPM_TIS_ATOMIC_STORE(x, val) ((x) = (val))
#define FWTPM_TIS_ATOMIC_LOAD(x) (x)
#endif
/* Default burst count (bytes per FIFO transfer) */
#ifndef FWTPM_TIS_BURST_COUNT
#define FWTPM_TIS_BURST_COUNT 64
@ -156,12 +168,6 @@ typedef struct FWTPM_TIS_REGS {
/* Backward compatibility alias */
typedef FWTPM_TIS_REGS FWTPM_TIS_SHM;
#if defined(WOLFTPM_FWTPM_TIS) || defined(WOLFTPM_FWTPM_HAL)
/* Build the effective POSIX semaphore names for an authenticated owner UID. */
WOLFTPM_LOCAL int FWTPM_TIS_MakeSemNames(UINT64 ownerUid, char* semCmd,
size_t semCmdSz, char* semRsp, size_t semRspSz);
#endif
/* --- TIS Transport HAL (server-side) ---
* Abstracts the transport between client and server for TIS register