Skip to content

Commit cc7c3bb

Browse files
committed
Reject oversized signature TLV lengths
F/2585
1 parent 46645a0 commit cc7c3bb

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

tools/keytools/sign.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,6 +1945,22 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
19451945
DEBUG_BUFFER(policy + sizeof(uint32_t), CMD.policy_sz);
19461946
}
19471947

1948+
if (CMD.signature_sz > (uint32_t)UINT16_MAX) {
1949+
printf("Error: Signature too large for TLV encoding (%u > %u)\n",
1950+
CMD.signature_sz, (unsigned int)UINT16_MAX);
1951+
ret = -1;
1952+
goto failure;
1953+
}
1954+
1955+
if (CMD.hybrid &&
1956+
CMD.secondary_signature_sz > (uint32_t)UINT16_MAX) {
1957+
printf("Error: Secondary signature too large for TLV encoding "
1958+
"(%u > %u)\n",
1959+
CMD.secondary_signature_sz, (unsigned int)UINT16_MAX);
1960+
ret = -1;
1961+
goto failure;
1962+
}
1963+
19481964
/* Add signature to header */
19491965
ALIGN_8(header_idx);
19501966
header_append_tag(header, &header_idx, HDR_SIGNATURE, CMD.signature_sz,

tools/unit-tests/unit-sign-encrypted-output.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,53 @@ START_TEST(test_make_header_ex_rejects_cert_chain_tlv_length_overflow)
699699
}
700700
END_TEST
701701

702+
START_TEST(test_make_header_ex_rejects_signature_tlv_length_overflow)
703+
{
704+
char tempdir[] = "/tmp/wolfboot-sign-XXXXXX";
705+
char image_path[PATH_MAX];
706+
char output_path[PATH_MAX];
707+
char signature_path[PATH_MAX];
708+
uint8_t image_buf[] = { 0x11, 0x22, 0x33, 0x44 };
709+
uint8_t pubkey[] = { 0xA5 };
710+
uint8_t *signature_buf = NULL;
711+
const uint32_t signature_len = 65536U;
712+
int ret;
713+
714+
ck_assert_ptr_nonnull(mkdtemp(tempdir));
715+
716+
snprintf(image_path, sizeof(image_path), "%s/image.bin", tempdir);
717+
snprintf(output_path, sizeof(output_path), "%s/output.bin", tempdir);
718+
snprintf(signature_path, sizeof(signature_path), "%s/signature.bin", tempdir);
719+
720+
signature_buf = malloc(signature_len);
721+
ck_assert_ptr_nonnull(signature_buf);
722+
memset(signature_buf, 0x5A, signature_len);
723+
724+
ck_assert_int_eq(write_file(image_path, image_buf, sizeof(image_buf)), 0);
725+
ck_assert_int_eq(write_file(signature_path, signature_buf, signature_len), 0);
726+
727+
reset_cmd_defaults();
728+
CMD.sign = SIGN_RSA2048;
729+
CMD.manual_sign = 1;
730+
CMD.signature_file = signature_path;
731+
CMD.signature_sz = signature_len;
732+
/* Keep room for a large signature TLV to expose uint16_t truncation. */
733+
CMD.header_sz = 131072U;
734+
735+
reset_mocks(NULL, 0);
736+
ret = make_header_ex(0, pubkey, sizeof(pubkey), image_path, output_path,
737+
0, 0, 0, 0, NULL, 0, NULL, 0);
738+
739+
ck_assert_int_ne(ret, 0);
740+
741+
free(signature_buf);
742+
unlink(output_path);
743+
unlink(signature_path);
744+
unlink(image_path);
745+
rmdir(tempdir);
746+
}
747+
END_TEST
748+
702749
Suite *wolfboot_suite(void)
703750
{
704751
Suite *s = suite_create("sign-encrypted-output");
@@ -717,6 +764,8 @@ Suite *wolfboot_suite(void)
717764
test_make_header_ex_keeps_boundary_header_for_sha384_sha3_hybrid_cert_chain);
718765
tcase_add_test(tcase,
719766
test_make_header_ex_rejects_cert_chain_tlv_length_overflow);
767+
tcase_add_test(tcase,
768+
test_make_header_ex_rejects_signature_tlv_length_overflow);
720769
suite_add_tcase(s, tcase);
721770

722771
return s;

0 commit comments

Comments
 (0)