Skip to content

Commit 1fdb3b3

Browse files
committed
Fixed more copilot findings
1 parent 2768ae6 commit 1fdb3b3

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

src/tpm.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,8 @@ int wolfBoot_seal_auth(const uint8_t* pubkey_hint,
891891
WOLFTPM2_KEYBLOB seal_blob;
892892
word32 nvAttributes;
893893

894+
if (authSz < 0)
895+
return BAD_FUNC_ARG;
894896
if (auth == NULL && authSz > 0)
895897
return BAD_FUNC_ARG;
896898
if (authSz > (int)sizeof(seal_blob.handle.auth.buffer))
@@ -1087,11 +1089,13 @@ int wolfBoot_unseal_blob(const uint8_t* pubkey_hint,
10871089
#endif
10881090

10891091
/* if using password auth, set it otherwise use policy auth */
1092+
if (authSz < 0)
1093+
return BAD_FUNC_ARG;
10901094
if (auth != NULL && authSz > 0) {
10911095
if (authSz > (int)sizeof(seal_blob->handle.auth.buffer))
10921096
return BAD_FUNC_ARG;
10931097
seal_blob->handle.auth.size = authSz;
1094-
memcpy(seal_blob->handle.auth.buffer, auth, authSz);
1098+
XMEMCPY(seal_blob->handle.auth.buffer, auth, authSz);
10951099
wolfTPM2_SetAuthHandle(&wolftpm_dev, 0, &seal_blob->handle);
10961100
}
10971101
else {

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

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,20 @@ int wolfTPM2_PolicyRefMake(TPM_ALG_ID pcrAlg, byte* digest, word32* digestSz,
230230

231231
int TPM2_GetHashDigestSize(TPMI_ALG_HASH hashAlg)
232232
{
233-
(void)hashAlg;
234-
return 32;
233+
switch (hashAlg) {
234+
case TPM_ALG_SHA1:
235+
return 20;
236+
case TPM_ALG_SHA256:
237+
return 32;
238+
case TPM_ALG_SHA384:
239+
return 48;
240+
case TPM_ALG_SHA512:
241+
return 64;
242+
case TPM_ALG_SM3_256:
243+
return 32;
244+
default:
245+
return 0;
246+
}
235247
}
236248

237249
int wolfTPM2_GetKeyTemplate_KeySeal(TPMT_PUBLIC* publicTemplate,
@@ -568,6 +580,25 @@ START_TEST(test_wolfBoot_seal_auth_rejects_oversized_auth)
568580
}
569581
END_TEST
570582

583+
START_TEST(test_wolfBoot_seal_auth_rejects_negative_auth_size)
584+
{
585+
uint8_t auth[8] = {0};
586+
uint8_t pubkey_hint[WOLFBOOT_SHA_DIGEST_SIZE] = {0};
587+
uint8_t policy[sizeof(uint32_t) + 4] = {0};
588+
uint8_t secret[8] = {0};
589+
int rc;
590+
591+
rc = wolfBoot_seal_auth(pubkey_hint, policy, sizeof(policy), 0,
592+
secret, sizeof(secret), auth, -1);
593+
594+
ck_assert_int_eq(rc, BAD_FUNC_ARG);
595+
ck_assert_int_eq(unexpected_nvcreate_calls, 0);
596+
ck_assert_int_eq(unexpected_nvwrite_calls, 0);
597+
ck_assert_int_eq(unexpected_nvopen_calls, 0);
598+
ck_assert_int_eq(unexpected_nvdelete_calls, 0);
599+
}
600+
END_TEST
601+
571602
START_TEST(test_wolfBoot_unseal_blob_zeroes_unseal_output)
572603
{
573604
uint8_t secret[WOLFBOOT_MAX_SEAL_SZ];
@@ -615,6 +646,28 @@ START_TEST(test_wolfBoot_unseal_blob_rejects_oversized_auth)
615646
}
616647
END_TEST
617648

649+
START_TEST(test_wolfBoot_unseal_blob_rejects_negative_auth_size)
650+
{
651+
WOLFTPM2_KEYBLOB blob;
652+
uint8_t auth[8] = {0};
653+
uint8_t secret[WOLFBOOT_MAX_SEAL_SZ];
654+
uint8_t pubkey_hint[WOLFBOOT_SHA_DIGEST_SIZE] = {0};
655+
uint8_t policy[sizeof(uint32_t) + 4] = {0};
656+
int secret_sz;
657+
int rc;
658+
659+
memset(&blob, 0, sizeof(blob));
660+
memset(secret, 0, sizeof(secret));
661+
secret_sz = (int)sizeof(secret);
662+
current_mode = MOCK_OVERSIZE_PUB;
663+
664+
rc = wolfBoot_unseal_blob(pubkey_hint, policy, sizeof(policy), &blob,
665+
secret, &secret_sz, auth, -1);
666+
667+
ck_assert_int_eq(rc, BAD_FUNC_ARG);
668+
}
669+
END_TEST
670+
618671
START_TEST(test_wolfBoot_unseal_blob_rejects_output_larger_than_capacity)
619672
{
620673
struct {
@@ -675,10 +728,12 @@ static Suite *tpm_blob_suite(void)
675728
tcase_add_test(tc, test_wolfBoot_read_blob_rejects_oversized_auth);
676729
tcase_add_test(tc, test_wolfBoot_delete_blob_rejects_oversized_auth);
677730
tcase_add_test(tc, test_wolfBoot_seal_auth_rejects_oversized_auth);
731+
tcase_add_test(tc, test_wolfBoot_seal_auth_rejects_negative_auth_size);
678732
tcase_add_test(tc, test_wolfBoot_read_blob_rejects_oversized_public_area);
679733
tcase_add_test(tc, test_wolfBoot_read_blob_rejects_oversized_private_area);
680734
tcase_add_test(tc, test_wolfBoot_unseal_blob_zeroes_unseal_output);
681735
tcase_add_test(tc, test_wolfBoot_unseal_blob_rejects_oversized_auth);
736+
tcase_add_test(tc, test_wolfBoot_unseal_blob_rejects_negative_auth_size);
682737
tcase_add_test(tc, test_wolfBoot_unseal_blob_rejects_output_larger_than_capacity);
683738
suite_add_tcase(s, tc);
684739
return s;

0 commit comments

Comments
 (0)