Skip to content

Commit 739ced5

Browse files
committed
tpm2_policy: Do not overflow list of policy digests
Don't try to write more digests into TPML_DIGEST than fit. Signed-off-by: Fabian Vogt <fvogt@suse.de>
1 parent bfa0d81 commit 739ced5

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

lib/tpm2_policy.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,10 @@ static bool tpm2_policy_populate_digest_list(char *buf,
642642
return false;
643643
}
644644

645+
if (policy_list->count == ARRAY_LEN(policy_list->digests)) {
646+
return false;
647+
}
648+
645649
unsigned long file_size;
646650
bool retval = files_get_file_size_path(buf, &file_size);
647651
if (!retval) {

test/unit/test_tpm2_policy.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,18 @@ static void tpm2_policy_parse_policy_list_double_call(void **state) {
559559
assert_memory_equal(policy_list.digests[3].buffer, sha256_digest_2, sizeof(sha256_digest_2));
560560
}
561561

562+
static void tpm2_policy_parse_policy_list_toomany(void **state) {
563+
UNUSED(state);
564+
565+
TPML_DIGEST policy_list = { 0 };
566+
char str[] = "sha256:testpolicy.sha256,testpolicy.sha256,testpolicy.sha256,testpolicy.sha256,"
567+
"testpolicy.sha256,testpolicy.sha256,testpolicy.sha256,testpolicy.sha256,"
568+
"testpolicy.sha256,testpolicy.sha256,testpolicy.sha256,testpolicy.sha256,"
569+
"testpolicy.sha256,testpolicy.sha256,testpolicy.sha256,testpolicy.sha256";
570+
bool res = tpm2_policy_parse_policy_list(str, &policy_list);
571+
assert_false(res);
572+
}
573+
562574
/* link required symbol, but tpm2_tool.c declares it AND main, which
563575
* we have a main below for cmocka tests.
564576
*/
@@ -572,6 +584,7 @@ int main(int argc, char *argv[]) {
572584
cmocka_unit_test(test_tpm2_policy_build_pcr_good),
573585
cmocka_unit_test(tpm2_policy_parse_policy_list_good),
574586
cmocka_unit_test(tpm2_policy_parse_policy_list_double_call),
587+
cmocka_unit_test(tpm2_policy_parse_policy_list_toomany),
575588
cmocka_unit_test_setup_teardown(test_tpm2_policy_build_pcr_file_good,
576589
test_setup, test_teardown),
577590
cmocka_unit_test_setup_teardown(test_tpm2_policy_build_pcr_file_bad_size,

0 commit comments

Comments
 (0)