Clear the sign_psbt state on every exit path of handler_sign_psbt
What changed, and why it matters
This commit refactors the Bitcoin app's PSBT signing handler so that sensitive temporary data (keys, transaction details, signing state) is always wiped from the device's stack memory when the handler finishes, even if it exits early due to an error. Before the change, some early return paths could leave that data in memory. The patch itself does not claim to fix an active exploit, but it removes a class of information-disclosure risk.
Treat as a defensive hardening commit. Review whether any other handlers in the app use similar local sensitive state without guaranteed explicit_bzero cleanup, and apply the same wrapper pattern if so. No immediate incident response is indicated by the diff alone.
Security signals we found
explicit_bzero used to clear signing state, cache, and PSBT state on all exit paths
Refactor moves early-return logic into a wrapper so cleanup always runs
Targets residual secret/sensitive data left on the stack after signing operations
No functional change to signing logic; change is purely cleanup/initialization ordering
Evidence from the diff
The change moves the body of handler_sign_psbt into a new static helper, sign_psbt, while the public wrapper now declares local state variables (sign_psbt_state_t st, sign_psbt_cache_t cache, signing_state_t signing_state), initializes them, calls the helper, and then calls explicit_bzero on all three after the helper returns regardless of how the helper exited. Previously, st/cache/signing_state were local to the single function and early returns inside the handler would skip any centralized cleanup. The patch therefore guarantees that sensitive stack-resident state is zeroed on every exit path.
Changed components
src/handler/sign_psbt.chandler_sign_psbtsign_psbt_state_tsign_psbt_cache_tsigning_state_tInspect captured patch +51 / −40
### src/handler/sign_psbt.c
@@ -43,35 +43,25 @@
#include "sw.h"
#include "txhashes.h"
-void handler_sign_psbt(dispatcher_context_t *dc, uint8_t protocol_version) {
- LOG_PROCESSOR(__FILE__, __LINE__, __func__);
-
- /* Setting transaction loading information screen */
- ui_set_processing_screen_text(GA_LOADING_TRANSACTION);
-
- sign_psbt_state_t st;
- memset(&st, 0, sizeof(st));
-
- st.protocol_version = protocol_version;
-
+static void sign_psbt(dispatcher_context_t *dc,
+ sign_psbt_state_t *st,
+ sign_psbt_cache_t *cache,
+ signing_state_t *signing_state) {
// read APDU inputs, initialize global state and read global PSBT map
- if (!init_global_state(dc, &st)) return;
+ if (!init_global_state(dc, st)) return;
#ifdef HAVE_SWAP
- if (G_called_from_swap && st.bip322.is_message_signing) {
+ if (G_called_from_swap && st->bip322.is_message_signing) {
PRINTF("BIP-322 message signing is not allowed during swap\n");
SEND_SW_EC(dc, SW_NOT_SUPPORTED, EC_SIGN_PSBT_BIP322_NOT_ALLOWED_IN_SWAP);
return;
}
#endif /* HAVE_SWAP */
- if (st.bip322.is_message_signing) {
+ if (st->bip322.is_message_signing) {
ui_set_processing_screen_text(GA_LOADING_MESSAGE);
}
- sign_psbt_cache_t cache;
- init_sign_psbt_cache(&cache);
-
// bitmap to keep track of which inputs are internal
uint8_t internal_inputs[BITVECTOR_REAL_SIZE(MAX_N_INPUTS_CAN_SIGN)];
memset(internal_inputs, 0, sizeof(internal_inputs));
@@ -88,58 +78,52 @@ void handler_sign_psbt(dispatcher_context_t *dc, uint8_t protocol_version) {
* - detect internal inputs that should be signed, and if there are external inputs or unusual
* sighashes
*/
- if (!preprocess_inputs(dc, &st, &cache, internal_inputs)) return;
+ if (!preprocess_inputs(dc, st, cache, internal_inputs)) return;
/** OUTPUTS VERIFICATION FLOW
*
* For each output, check if it's a change address.
* Check if it's an acceptable output.
*/
- if (!preprocess_outputs(dc, &st, &cache, internal_outputs)) return;
+ if (!preprocess_outputs(dc, st, cache, internal_outputs)) return;
/** BIP-322 STRUCTURAL VALIDATION
*
* If the PSBT declares itself as a BIP-322 message signing request, enforce the exact
* to_sign structure; the PSBT is never reviewed as a transaction once the field is present,
* but it shares the same signing flow.
*/
- if (st.bip322.is_message_signing && !validate_bip322_request(dc, &st)) return;
+ if (st->bip322.is_message_signing && !validate_bip322_request(dc, st)) return;
// check if we're only executing the MuSig2 Round 1
bool only_signing_for_musig = true;
- for (size_t i = 0; i < st.account.n_internal_key_expressions; i++) {
- if (st.account.internal_key_expressions[i].to_sign &&
- st.account.internal_key_expressions[i].key_expression_ptr->type !=
+ for (size_t i = 0; i < st->account.n_internal_key_expressions; i++) {
+ if (st->account.internal_key_expressions[i].to_sign &&
+ st->account.internal_key_expressions[i].key_expression_ptr->type !=
KEY_EXPRESSION_MUSIG) {
// at least one of the key expressions we're signing for is not a MuSig
only_signing_for_musig = false;
}
}
- signing_state_t signing_state;
- memset(&signing_state, 0, sizeof(signing_state));
-
- // Make sure that the signing state for MuSig2 is initialized correctly
- musigsession_initialize_signing_state(&signing_state.musig);
-
// compute all the tx-wide hashes
- if (!compute_tx_hashes(dc, &st, &signing_state.tx_hashes)) {
+ if (!compute_tx_hashes(dc, st, &signing_state->tx_hashes)) {
return;
}
- if (!st.has_musig2_pub_nonces) {
+ if (!st->has_musig2_pub_nonces) {
// We execute the first round of MuSig for any musig2 key expression, producing the
// pubnonces; this does not involve the private keys, therefore we can do it without user
// confirmation
- if (!produce_musig2_pubnonces(dc, &st, &signing_state, &cache, internal_inputs)) {
+ if (!produce_musig2_pubnonces(dc, st, signing_state, cache, internal_inputs)) {
return;
}
}
// we execute the signing flow only if we're expected to produce any signature
// (including, possibly, any MuSig2 partial signature from Round 2 of MuSig2)
- if (!only_signing_for_musig || st.has_musig2_pub_nonces) {
+ if (!only_signing_for_musig || st->has_musig2_pub_nonces) {
#ifdef HAVE_SWAP
if (G_called_from_swap) {
/** SWAP CHECKS
@@ -148,23 +132,23 @@ void handler_sign_psbt(dispatcher_context_t *dc, uint8_t protocol_version) {
*/
// During swaps, the user approval was already obtained in the exchange app
- if (!execute_swap_checks(dc, &st)) return;
+ if (!execute_swap_checks(dc, st)) return;
} else
#endif /* HAVE_SWAP */
{
- if (st.bip322.is_message_signing) {
+ if (st->bip322.is_message_signing) {
/** BIP-322 MESSAGE CONFIRMATION
*
* Review as a message signature (account, address, message).
*/
- if (!display_bip322_message(dc, &st)) return;
+ if (!display_bip322_message(dc, st)) return;
} else {
/** TRANSACTION CONFIRMATION
*
* Display each non-change output, and transaction fees, and acquire user
* confirmation,
*/
- if (!display_transaction(dc, &st, internal_outputs)) return;
+ if (!display_transaction(dc, st, internal_outputs)) return;
}
}
@@ -176,13 +160,13 @@ void handler_sign_psbt(dispatcher_context_t *dc, uint8_t protocol_version) {
* For each internal key expression, and for each internal input, sign using the
* appropriate algorithm.
*/
- int sign_result = sign_transaction(dc, &st, &cache, &signing_state, internal_inputs);
+ int sign_result = sign_transaction(dc, st, cache, signing_state, internal_inputs);
#ifdef HAVE_SWAP
if (!G_called_from_swap)
#endif /* HAVE_SWAP */
{
- if (st.bip322.is_message_signing) {
+ if (st->bip322.is_message_signing) {
ui_post_processing_confirm_message(dc, sign_result);
} else {
ui_post_processing_confirm_transaction(dc, sign_result);
@@ -204,7 +188,34 @@ void handler_sign_psbt(dispatcher_context_t *dc, uint8_t protocol_version) {
// MuSig2: if there is an active session at the end of round 1, we move it to persistent
// storage. It is important that this is only done at the very end of the signing process,
// end only if everything is successful.
- musigsession_commit(&signing_state.musig);
+ musigsession_commit(&signing_state->musig);
SEND_SW(dc, SW_OK);
}
+
+void handler_sign_psbt(dispatcher_context_t *dc, uint8_t protocol_version) {
+ LOG_PROCESSOR(__FILE__, __LINE__, __func__);
+
+ /* Setting transaction loading information screen */
+ ui_set_processing_screen_text(GA_LOADING_TRANSACTION);
+
+ sign_psbt_state_t st;
+ memset(&st, 0, sizeof(st));
+ st.protocol_version = protocol_version;
+
+ sign_psbt_cache_t cache;
+ init_sign_psbt_cache(&cache);
+
+ signing_state_t signing_state;
+ memset(&signing_state, 0, sizeof(signing_state));
+
+ // Make sure that the signing state for MuSig2 is initialized correctly
+ musigsession_initialize_signing_state(&signing_state.musig);
+
+ sign_psbt(dc, &st, &cache, &signing_state);
+
+ // Leave nothing behind on the stack, whatever the outcome of the signing flow
+ explicit_bzero(&st, sizeof(st));
+ explicit_bzero(&cache, sizeof(cache));
+ explicit_bzero(&signing_state, sizeof(signing_state));
+}Why this scored 59/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.