Skip to content

Commit dc7309c

Browse files
committed
Fix TPM unseal handle cleanup
F/2568
1 parent e10d9c1 commit dc7309c

2 files changed

Lines changed: 33 additions & 5 deletions

File tree

src/tpm.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,11 +1117,15 @@ int wolfBoot_unseal_blob(const uint8_t* pubkey_hint,
11171117
#endif
11181118

11191119
/* if using password auth, set it otherwise use policy auth */
1120-
if (authSz < 0)
1121-
return BAD_FUNC_ARG;
1120+
if (authSz < 0) {
1121+
rc = BAD_FUNC_ARG;
1122+
goto exit;
1123+
}
11221124
if (auth != NULL && authSz > 0) {
1123-
if (authSz > (int)sizeof(seal_blob->handle.auth.buffer))
1124-
return BAD_FUNC_ARG;
1125+
if (authSz > (int)sizeof(seal_blob->handle.auth.buffer)) {
1126+
rc = BAD_FUNC_ARG;
1127+
goto exit;
1128+
}
11251129
seal_blob->handle.auth.size = authSz;
11261130
memcpy(seal_blob->handle.auth.buffer, auth, authSz);
11271131
wolfTPM2_SetAuthHandle(&wolftpm_dev, 0, &seal_blob->handle);
@@ -1151,6 +1155,7 @@ int wolfBoot_unseal_blob(const uint8_t* pubkey_hint,
11511155
}
11521156
TPM2_ForceZero(&unsealOut, sizeof(unsealOut));
11531157

1158+
exit:
11541159
wolfTPM2_UnloadHandle(&wolftpm_dev, &seal_blob->handle);
11551160
wolfTPM2_UnloadHandle(&wolftpm_dev, &policy_session.handle);
11561161
wolfTPM2_UnsetAuthSession(&wolftpm_dev, 1, &wolftpm_session);

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ static int oversized_priv_read_attempted;
4040
static int forcezero_calls;
4141
static word32 last_forcezero_len;
4242
static word32 last_pub_read_request_sz;
43+
static int unload_handle_calls;
44+
static int unload_seal_blob_calls;
45+
static int unload_policy_session_calls;
46+
static int unload_auth_key_calls;
4347
static uint8_t test_hdr[64];
4448
static uint8_t test_modulus[256];
4549
static uint8_t test_exponent_der[] = { 0xAA, 0x01, 0x00, 0x01, 0x7B };
@@ -119,7 +123,18 @@ int wolfTPM2_LoadKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEYBLOB* keyBlob,
119123
int wolfTPM2_UnloadHandle(WOLFTPM2_DEV* dev, WOLFTPM2_HANDLE* handle)
120124
{
121125
(void)dev;
122-
(void)handle;
126+
unload_handle_calls++;
127+
if (handle != NULL) {
128+
if (handle->hndl == 1) {
129+
unload_policy_session_calls++;
130+
}
131+
else if (handle->hndl == 2) {
132+
unload_seal_blob_calls++;
133+
}
134+
else {
135+
unload_auth_key_calls++;
136+
}
137+
}
123138
return 0;
124139
}
125140

@@ -488,6 +503,10 @@ static void setup(void)
488503
forcezero_calls = 0;
489504
last_forcezero_len = 0;
490505
last_pub_read_request_sz = 0;
506+
unload_handle_calls = 0;
507+
unload_seal_blob_calls = 0;
508+
unload_policy_session_calls = 0;
509+
unload_auth_key_calls = 0;
491510
memset(test_hdr, 0x22, sizeof(test_hdr));
492511
memset(test_modulus, 0x33, sizeof(test_modulus));
493512
}
@@ -643,6 +662,8 @@ START_TEST(test_wolfBoot_unseal_blob_rejects_oversized_auth)
643662
secret, &secret_sz, auth, (int)sizeof(auth));
644663

645664
ck_assert_int_eq(rc, BAD_FUNC_ARG);
665+
ck_assert_int_eq(unload_seal_blob_calls, 1);
666+
ck_assert_int_eq(unload_policy_session_calls, 1);
646667
}
647668
END_TEST
648669

@@ -665,6 +686,8 @@ START_TEST(test_wolfBoot_unseal_blob_rejects_negative_auth_size)
665686
secret, &secret_sz, auth, -1);
666687

667688
ck_assert_int_eq(rc, BAD_FUNC_ARG);
689+
ck_assert_int_eq(unload_seal_blob_calls, 1);
690+
ck_assert_int_eq(unload_policy_session_calls, 1);
668691
}
669692
END_TEST
670693

0 commit comments

Comments
 (0)