Skip to content

Commit c513790

Browse files
Merge pull request #773 from LedgerHQ/fix/apa/potential_memory_leak_tlv_parsing
Fix potential memory leak with incorrect TLV structs
2 parents b5db163 + bdd40f9 commit c513790

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

src_features/generic_tx_parser/cmd_field.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313
static bool handle_tlv_payload(const uint8_t *payload, uint16_t size, bool to_free) {
1414
s_field field = {0};
1515
s_field_ctx ctx = {0};
16+
bool parsing_ret;
17+
bool hashing_ret;
1618

1719
ctx.field = &field;
18-
if (!tlv_parse(payload, size, (f_tlv_data_handler) &handle_field_struct, &ctx)) return false;
19-
if (cx_hash_no_throw(get_fields_hash_ctx(), 0, payload, size, NULL, 0) != CX_OK) return false;
20+
parsing_ret = tlv_parse(payload, size, (f_tlv_data_handler) &handle_field_struct, &ctx);
21+
hashing_ret = cx_hash_no_throw(get_fields_hash_ctx(), 0, payload, size, NULL, 0) == CX_OK;
2022
if (to_free) mem_dealloc(size);
23+
if (!parsing_ret || !hashing_ret) return false;
2124
if (!verify_field_struct(&ctx)) {
2225
PRINTF("Error: could not verify the field struct!\n");
2326
return false;

src_features/generic_tx_parser/cmd_tx_info.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ static uint8_t g_tx_info_alignment;
1717

1818
static bool handle_tlv_payload(const uint8_t *payload, uint16_t size, bool to_free) {
1919
s_tx_info_ctx ctx = {0};
20+
bool parsing_ret;
2021

2122
ctx.tx_info = g_tx_info;
2223
explicit_bzero(ctx.tx_info, sizeof(*ctx.tx_info));
2324
cx_sha256_init((cx_sha256_t *) &ctx.struct_hash);
24-
if (!tlv_parse(payload, size, (f_tlv_data_handler) &handle_tx_info_struct, &ctx)) return false;
25+
parsing_ret = tlv_parse(payload, size, (f_tlv_data_handler) &handle_tx_info_struct, &ctx);
2526
if (to_free) mem_dealloc(sizeof(size));
26-
if (!verify_tx_info_struct(&ctx)) {
27+
if (!parsing_ret || !verify_tx_info_struct(&ctx)) {
2728
return false;
2829
}
2930
if (cx_sha3_init_no_throw(&hash_ctx, 256) != CX_OK) {

src_features/provide_proxy_info/cmd_proxy_info.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88

99
static bool handle_tlv_payload(const uint8_t *payload, uint16_t size, bool to_free) {
1010
s_proxy_info_ctx ctx = {0};
11+
bool parsing_ret;
1112

1213
cx_sha256_init(&ctx.struct_hash);
13-
if (!tlv_parse(payload, size, (f_tlv_data_handler) &handle_proxy_info_struct, &ctx)) {
14-
return false;
15-
}
14+
parsing_ret = tlv_parse(payload, size, (f_tlv_data_handler) &handle_proxy_info_struct, &ctx);
1615
if (to_free) mem_dealloc(sizeof(size));
17-
if (!verify_proxy_info_struct(&ctx)) {
16+
if (!parsing_ret || !verify_proxy_info_struct(&ctx)) {
1817
return false;
1918
}
2019
return true;

src_features/provide_trusted_name/cmd_trusted_name.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@
1010

1111
static bool handle_tlv_payload(const uint8_t *payload, uint16_t size, bool to_free) {
1212
s_trusted_name_ctx ctx = {0};
13-
bool parsing_success;
13+
bool parsing_ret;
1414

1515
ctx.trusted_name.name = g_trusted_name;
1616
cx_sha256_init(&ctx.hash_ctx);
17-
parsing_success =
18-
tlv_parse(payload, size, (f_tlv_data_handler) &handle_trusted_name_struct, &ctx);
17+
parsing_ret = tlv_parse(payload, size, (f_tlv_data_handler) &handle_trusted_name_struct, &ctx);
1918
if (to_free) mem_dealloc(size);
20-
if (!parsing_success || !verify_trusted_name_struct(&ctx)) {
19+
if (!parsing_ret || !verify_trusted_name_struct(&ctx)) {
2120
roll_challenge(); // prevent brute-force guesses
2221
return false;
2322
}

0 commit comments

Comments
 (0)