Store opreturn_amount as uint64_t in cross-chain swaps
What changed, and why it matters
This commit fixes a variable type in Ledger's Bitcoin app used during cross-chain cryptocurrency swaps. The amount attached to an OP_RETURN output was being stored in a `size_t` variable, which on Ledger's embedded device is likely 32 bits, while the actual amount is a 64-bit value. This mismatch could cause the amount to be silently truncated (cut off), potentially allowing a swap to be validated with an incorrect amount. The fix changes the variable to `uint64_t` to match the real data size and prevent truncation.
Treat this as a security-relevant correctness fix. Review whether the truncated value could bypass swap amount checks or be exploited to approve an under-funded or over-funded swap. Verify no other monetary amounts in swap paths use `size_t` or other narrower types. Consider whether this issue warrants a security advisory or CVE if swap integrity guarantees could be violated.
Security signals we found
Integer width mismatch between `size_t` and `uint64_t` for a monetary amount
Potential silent truncation of output amount during cross-chain swap validation
Fix located in swap-specific security check path (`execute_swap_checks`)
Commit message frames change as preventing truncation problems
Evidence from the diff
In src/handler/sign_psbt.c, within execute_swap_checks(), the local variable holding st->outputs.output_amounts[0] was declared as size_t. On the target embedded platform size_t is typically 32 bits, whereas output_amounts elements are uint64_t. This width mismatch can truncate the output amount during cross-chain swap validation. The patch changes the declaration to uint64_t, eliminating the truncation risk. The commit message explicitly states this prevents ‘possible truncation problems’.
Changed components
src/handler/sign_psbt.cexecute_swap_checks()Cross-chain swap validation logicOP_RETURN output amount handlingInspect captured patch +1 / −1
diff --git a/src/handler/sign_psbt.c b/src/handler/sign_psbt.c
index 471ba66..211db5b 100644
--- a/src/handler/sign_psbt.c
+++ b/src/handler/sign_psbt.c
@@ -1095,7 +1095,7 @@ execute_swap_checks(dispatcher_context_t *dc, sign_psbt_state_t *st) {
uint8_t *opreturn_script = st->outputs.output_scripts[0];
size_t opreturn_script_len = st->outputs.output_script_lengths[0];
- size_t opreturn_amount = st->outputs.output_amounts[0];
+ uint64_t opreturn_amount = st->outputs.output_amounts[0];
if (opreturn_script_len < 4 || opreturn_script[0] != OP_RETURN) {
PRINTF("The first output must be OP_RETURN <data> for a cross-chain swap\n");
SEND_SW_EC(dc,
Why this scored 58/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.