From ac87e6bb7ab8d42cccef4908d70c756878a0bc87 Mon Sep 17 00:00:00 2001 From: aidan garske Date: Mon, 22 Jun 2026 13:03:13 -0700 Subject: [PATCH] F-6318 - Create readKeyBlob test files with owner-only permissions --- tests/unit_tests.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/tests/unit_tests.c b/tests/unit_tests.c index 80361234..f40bfea1 100644 --- a/tests/unit_tests.c +++ b/tests/unit_tests.c @@ -39,6 +39,9 @@ #include #include +#if defined(__linux__) || defined(__APPLE__) || defined(__unix__) +#include +#endif /* Test Fail Helpers */ #ifndef NO_ABORT @@ -4009,6 +4012,21 @@ static void test_TPM2_SwtpmValidateRspSz(void) #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES) && \ !defined(WOLFTPM2_NO_WOLFCRYPT) && defined(HAVE_ECC) +/* Create a temp file for writing with owner-only permissions, so the test + * does not leave a world-writable file as plain fopen("wb") would. */ +static XFILE openTestFileWrite(const char* fn) +{ +#if defined(__linux__) || defined(__APPLE__) || defined(__unix__) + int fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0600); + if (fd < 0) { + return XBADFILE; + } + return fdopen(fd, "wb"); +#else + return XFOPEN(fn, "wb"); +#endif +} + /* Craft a key-blob file with a small valid public area and an oversized * private tail, then load it with readKeyBlob(). A canary placed directly * after the keyblob catches any write past it. No TPM is required. */ @@ -4049,7 +4067,7 @@ static void test_readKeyBlob_PrivOverflow(void) privTailSz = sizeof(guarded.key.priv) + sizeof(filler); privSizeMarker = 32; - fp = XFOPEN(filename, "wb"); + fp = openTestFileWrite(filename); AssertNotNull(fp); XFWRITE(&tmpl.pub.size, 1, sizeof(tmpl.pub.size), fp); XFWRITE(pubAreaBuffer, 1, sizeof(UINT16) + tmpl.pub.size, fp); @@ -4078,7 +4096,7 @@ static void test_readKeyBlob_PrivOverflow(void) /* Rejection branch: priv.size marker larger than the destination buffer * must be refused with BUFFER_E before any bytes are read. */ bigMarker = (UINT16)(sizeof(guarded.key.priv.buffer) + 1); - fp = XFOPEN(filename, "wb"); + fp = openTestFileWrite(filename); AssertNotNull(fp); XFWRITE(&tmpl.pub.size, 1, sizeof(tmpl.pub.size), fp); XFWRITE(pubAreaBuffer, 1, sizeof(UINT16) + tmpl.pub.size, fp); @@ -4091,7 +4109,7 @@ static void test_readKeyBlob_PrivOverflow(void) /* Rejection branch: pub.size marker larger than the public area buffer. */ bigMarker = (UINT16)sizeof(pubAreaBuffer); - fp = XFOPEN(filename, "wb"); + fp = openTestFileWrite(filename); AssertNotNull(fp); XFWRITE(&bigMarker, 1, sizeof(bigMarker), fp); XFCLOSE(fp);