unit tests: Fix length overflow in unit-test PSBT parser
What changed, and why it matters
This commit fixes an arithmetic overflow bug in a test-only PSBT (Bitcoin transaction format) parser used in unit tests. The bug could let a crafted test input make the parser think a data length is huge, potentially confusing the mock dispatcher in tests. It also adds a check that no unexpected trailing bytes remain after parsing. The fix only affects unit-test code, not the actual Ledger app that runs on devices.
No action needed for end users; the change is in unit-test infrastructure. For developers, ensure the fixed unit-test parser is rebuilt and tests are re-run. Consider whether similar additive length checks exist in the production PSBT parser and apply the same subtractive pattern if present.
Security signals we found
Integer overflow/wrap-around in length calculation
Out-of-bounds length derived from arithmetic on attacker-influenced offsets
Unit-test-only code path, not device firmware
Addition of trailing-byte validation improving parser strictness
Evidence from the diff
In unit-tests/libs/psbt_parse.c, two length checks were rewritten from additive form (offset + key_len > data_len and offset + value_len > data_len) to subtractive form (key_len > data_len - offset and value_len > data_len - offset). This prevents a size_t/uint64_t wrap-around when the sum exceeds UINT64_MAX, which could otherwise produce a bogus length passed to the unit-test mock dispatcher. A trailing-bytes check (offset != data_len) was also added to psbt_parse() so extra bytes after the last output map are rejected.
Changed components
unit-tests/libs/psbt_parse.cUnit-test mock dispatcher (indirectly, via parsed lengths)Inspect captured patch +6 / −2
### unit-tests/libs/psbt_parse.c
@@ -53,7 +53,8 @@ static int parse_one_map(const uint8_t *data, size_t data_len, size_t *offset, p
return 0;
}
- if (*offset + key_len > data_len) return -1;
+ /* Subtraction form: *offset <= data_len holds here, and `*offset + key_len` could wrap */
+ if (key_len > data_len - *offset) return -1;
if (map->n_entries >= PSBT_MAP_MAX_ENTRIES) return -1;
psbt_kv_t *entry = &map->entries[map->n_entries];
@@ -67,7 +68,7 @@ static int parse_one_map(const uint8_t *data, size_t data_len, size_t *offset, p
if (n < 0) return -1;
*offset += (size_t) n;
- if (*offset + value_len > data_len) return -1;
+ if (value_len > data_len - *offset) return -1;
entry->value = data + *offset;
entry->value_len = (size_t) value_len;
@@ -110,6 +111,9 @@ int psbt_parse(const uint8_t *data,
if (parse_one_map(data, data_len, &offset, &out->output_maps[i]) < 0) return -1;
}
+ /* Trailing bytes mean the PSBT has more maps than n_inputs/n_outputs say */
+ if (offset != data_len) return -1;
+
return 0;
}
Why this scored 37/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.