Skip to content

Commit 0b9641f

Browse files
committed
wolfBoot_unseal_blob: ensure secret is zeroed from stack after use
F/444
1 parent f7967fc commit 0b9641f

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

src/tpm.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,7 @@ int wolfBoot_unseal_blob(const uint8_t* pubkey_hint,
10941094
memcpy(secret, unsealOut.outData.buffer, *secret_sz);
10951095
}
10961096
}
1097+
TPM2_ForceZero(&unsealOut, sizeof(unsealOut));
10971098

10981099
wolfTPM2_UnloadHandle(&wolftpm_dev, &seal_blob->handle);
10991100
wolfTPM2_UnloadHandle(&wolftpm_dev, &policy_session.handle);

tools/unit-tests/unit-tpm-blob.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@
2525
enum mock_mode {
2626
MOCK_OVERSIZE_PUB,
2727
MOCK_OVERSIZE_PRIV,
28+
MOCK_UNSEAL_OK,
2829
MOCK_UNSEAL_OVERSIZE
2930
};
3031

3132
static enum mock_mode current_mode;
3233
static int nvread_calls;
3334
static int oversized_pub_read_attempted;
3435
static int oversized_priv_read_attempted;
36+
static int forcezero_calls;
37+
static word32 last_forcezero_len;
3538
static uint8_t test_hdr[64];
3639
static uint8_t test_modulus[256];
3740
static uint8_t test_exponent_der[] = { 0xAA, 0x01, 0x00, 0x01, 0x7B };
@@ -217,6 +220,12 @@ TPM_RC TPM2_Unseal(Unseal_In* in, Unseal_Out* out)
217220
{
218221
(void)in;
219222

223+
if (current_mode == MOCK_UNSEAL_OK) {
224+
out->outData.size = 4;
225+
memset(out->outData.buffer, 0x5A, out->outData.size);
226+
return 0;
227+
}
228+
220229
if (current_mode != MOCK_UNSEAL_OVERSIZE) {
221230
ck_abort_msg("Unexpected TPM2_Unseal call in mode %d", current_mode);
222231
}
@@ -226,6 +235,13 @@ TPM_RC TPM2_Unseal(Unseal_In* in, Unseal_Out* out)
226235
return 0;
227236
}
228237

238+
void TPM2_ForceZero(void* mem, word32 len)
239+
{
240+
forcezero_calls++;
241+
last_forcezero_len = len;
242+
memset(mem, 0, len);
243+
}
244+
229245
int keyslot_id_by_sha(const uint8_t* pubkey_hint)
230246
{
231247
(void)pubkey_hint;
@@ -312,6 +328,8 @@ static void setup(void)
312328
nvread_calls = 0;
313329
oversized_pub_read_attempted = 0;
314330
oversized_priv_read_attempted = 0;
331+
forcezero_calls = 0;
332+
last_forcezero_len = 0;
315333
memset(test_hdr, 0x22, sizeof(test_hdr));
316334
memset(test_modulus, 0x33, sizeof(test_modulus));
317335
}
@@ -332,6 +350,30 @@ START_TEST(test_wolfBoot_read_blob_rejects_oversized_public_area)
332350
}
333351
END_TEST
334352

353+
START_TEST(test_wolfBoot_unseal_blob_zeroes_unseal_output)
354+
{
355+
uint8_t secret[WOLFBOOT_MAX_SEAL_SZ];
356+
WOLFTPM2_KEYBLOB blob;
357+
uint8_t pubkey_hint[WOLFBOOT_SHA_DIGEST_SIZE] = {0};
358+
uint8_t policy[sizeof(uint32_t) + 4] = {0};
359+
int secret_sz;
360+
int rc;
361+
362+
memset(&blob, 0, sizeof(blob));
363+
memset(secret, 0, sizeof(secret));
364+
current_mode = MOCK_UNSEAL_OK;
365+
secret_sz = (int)sizeof(secret);
366+
367+
rc = wolfBoot_unseal_blob(pubkey_hint, policy, sizeof(policy), &blob,
368+
secret, &secret_sz, NULL, 0);
369+
370+
ck_assert_int_eq(rc, 0);
371+
ck_assert_int_eq(secret_sz, 4);
372+
ck_assert_int_eq(forcezero_calls, 1);
373+
ck_assert_uint_eq(last_forcezero_len, sizeof(Unseal_Out));
374+
}
375+
END_TEST
376+
335377
START_TEST(test_wolfBoot_unseal_blob_rejects_output_larger_than_capacity)
336378
{
337379
struct {
@@ -388,6 +430,7 @@ static Suite *tpm_blob_suite(void)
388430
tcase_add_checked_fixture(tc, setup, NULL);
389431
tcase_add_test(tc, test_wolfBoot_read_blob_rejects_oversized_public_area);
390432
tcase_add_test(tc, test_wolfBoot_read_blob_rejects_oversized_private_area);
433+
tcase_add_test(tc, test_wolfBoot_unseal_blob_zeroes_unseal_output);
391434
tcase_add_test(tc, test_wolfBoot_unseal_blob_rejects_output_larger_than_capacity);
392435
suite_add_tcase(s, tc);
393436
return s;

0 commit comments

Comments
 (0)