sign_psbt: fail before the UI if the MuSig2 session is missing
What changed, and why it matters
This commit improves the user experience and error handling for Ledger's Bitcoin app when using MuSig2 multi-signatures. Previously, if a required MuSig2 session was missing during the second round of signing, the device would only report the error after the user had already reviewed and approved the transaction on screen, causing a confusing 'Transaction rejected' message. Now, the app checks for the session before showing the transaction to the user, so it fails earlier and more clearly. There is no direct evidence this fixes a security vulnerability, but it reduces the chance of user confusion and wasted approvals.
Treat as a routine quality/UX improvement. No urgent security response is indicated by the diff alone. If maintaining this app, ensure the new error code is documented in client integrations so users understand why signing fails early when a MuSig2 session is missing or evicted.
Security signals we found
UX-hardening: fail-fast before user approval instead of after
New specific error code EC_SIGN_PSBT_MUSIG2_SESSION_NOT_FOUND for missing MuSig2 sessions
Refactored session storage lookup to support non-destructive existence check
No cryptographic or access-control changes observed
Tests added/updated to assert early failure behavior and error code
Evidence from the diff
The change moves the MuSig2 Round 2 session existence check to an earlier point in the sign_psbt flow. A new function musig_check_round2_session computes the expected session ID from the wallet policy and MUSIG2_SESSION_ID, then uses a new musigsession_exists helper to check persistent storage without deleting or reading the session. If the session is missing, the app now returns a new error code EC_SIGN_PSBT_MUSIG2_SESSION_NOT_FOUND (0x0017) before any transaction review UI is shown. The existing late check in sign_sighash_musig_and_yield is updated to use the same specific error code. The session storage code is refactored to share a find_slot helper between pop and exists operations.
Changed components
src/handler/sign_psbt.csrc/handler/sign_psbt/musig_signing.csrc/handler/sign_psbt/musig_signing.hsrc/musig/musig_sessions.csrc/musig/musig_sessions.hsrc/error_codes.htests/test_sign_psbt_musig.pyInspect captured patch +118 / −18
### src/error_codes.h
@@ -112,6 +112,9 @@
// The MUSIG2_SESSION_ID global field is present, but its value is empty or longer than 32 bytes.
#define EC_SIGN_PSBT_INVALID_MUSIG2_SESSION_ID 0x0016
+// The PSBT contains MuSig2 pubnonces, but the device has no corresponding MuSig2 session for it
+#define EC_SIGN_PSBT_MUSIG2_SESSION_NOT_FOUND 0x0017
+
/**
* Swap
*/
### src/handler/sign_psbt.c
@@ -21,6 +21,7 @@
#include "sign_psbt.h"
#include "sign_psbt/bip322_validation.h"
#include "sign_psbt/init_global_state.h"
+#include "sign_psbt/musig_signing.h"
#include "sign_psbt/preprocess_inputs.h"
#include "sign_psbt/preprocess_outputs.h"
#include "sign_psbt/sign_input.h"
@@ -96,11 +97,18 @@ static void sign_psbt(dispatcher_context_t *dc,
if (st->bip322.is_message_signing && !validate_bip322_request(dc, st)) return;
// check if we're only executing the MuSig2 Round 1
+ // true at the end of the loop if all key expressions we're signing for are MuSig2
bool only_signing_for_musig = true;
+ // true if any of the key expressions we're signing for is MuSig2
+ bool signing_for_musig = false;
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) {
+ if (!st->account.internal_key_expressions[i].to_sign) {
+ continue;
+ }
+ if (st->account.internal_key_expressions[i].key_expression_ptr->type ==
+ KEY_EXPRESSION_MUSIG) {
+ signing_for_musig = true;
+ } else {
// at least one of the key expressions we're signing for is not a MuSig
only_signing_for_musig = false;
}
@@ -124,6 +132,12 @@ static void sign_psbt(dispatcher_context_t *dc,
// 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 MuSig2 Round 2 is going to be executed, make sure that the session exists before
+ // proceeding, so we can fail early if missing.
+ if (signing_for_musig && st->has_musig2_pub_nonces && !musig_check_round2_session(dc, st)) {
+ return;
+ }
+
#ifdef HAVE_SWAP
if (G_called_from_swap) {
/** SWAP CHECKS
### src/handler/sign_psbt/musig_signing.c
@@ -25,6 +25,7 @@
/* Local headers */
#include "client_commands.h"
#include "commands.h"
+#include "error_codes.h"
#include "get_merkleized_map_value.h"
#include "policy.h"
#include "psbt.h"
@@ -73,6 +74,18 @@ static void musig_compute_session_id(const sign_psbt_state_t *st, uint8_t out[st
crypto_hash_digest(&hash_context.header, out, 32);
}
+bool musig_check_round2_session(dispatcher_context_t *dc, const sign_psbt_state_t *st) {
+ uint8_t psbt_session_id[32];
+ musig_compute_session_id(st, psbt_session_id);
+
+ if (!musigsession_exists(psbt_session_id)) {
+ PRINTF("Missing MuSig2 session\n");
+ SEND_SW_EC(dc, SW_BAD_STATE, EC_SIGN_PSBT_MUSIG2_SESSION_NOT_FOUND);
+ return false;
+ }
+ return true;
+}
+
bool compute_musig_per_input_info(dispatcher_context_t *dc,
sign_psbt_state_t *st,
const input_info_t *input,
@@ -443,7 +456,7 @@ bool __attribute__((noinline)) sign_sighash_musig_and_yield(dispatcher_context_t
// session in storage. Either it was deleted, or the pubnonces were not real. Either
// way, we cannot continue.
PRINTF("Missing MuSig2 session\n");
- SEND_SW(dc, SW_BAD_STATE);
+ SEND_SW_EC(dc, SW_BAD_STATE, EC_SIGN_PSBT_MUSIG2_SESSION_NOT_FOUND);
return false;
}
### src/handler/sign_psbt/musig_signing.h
@@ -34,6 +34,15 @@ typedef struct {
bool is_xonly[3]; // 2 or 3 elements
} musig_per_input_info_t;
+/**
+ * Checks that the MuSig2 session needed for Round 2 is in the persistent storage, without deleting
+ * it and without accessing its content.
+ *
+ * Returns true if the session exists, false otherwise. In case of failure, it already sends an
+ * error status word.
+ */
+bool musig_check_round2_session(dispatcher_context_t *dc, const sign_psbt_state_t *st);
+
/**
* Computes the MuSig2 per-input, per-key-expression information.
*
### src/musig/musig_sessions.c
@@ -39,29 +39,42 @@ static bool is_slot_free(int i) {
sizeof(musig_stored_session_t));
}
-static bool musigsession_pop(const uint8_t psbt_session_id[static 32], musig_psbt_session_t *out) {
+// Returns the index of the slot storing the session with the given id, or -1 if there is none.
+static int find_slot(const uint8_t psbt_session_id[static 32]) {
for (int i = 0; i < MAX_N_MUSIG_SESSIONS; i++) {
if (is_slot_free(i)) {
continue;
}
if (memcmp(psbt_session_id,
(const void *) N_musig_storage.sessions[i].id_prefix,
STORED_ID_PREFIX_LEN) == 0) {
- if (out != NULL) {
- memcpy(out->_id, psbt_session_id, sizeof(out->_id));
- memcpy(out->_rand_root,
- (const void *) N_musig_storage.sessions[i].rand_root,
- sizeof(out->_rand_root));
- }
- uint8_t zeros[sizeof(musig_stored_session_t)] = {0};
- nvm_write((void *) &N_musig_storage.sessions[i],
- (void *) zeros,
- sizeof(musig_stored_session_t));
-
- return true;
+ return i;
}
}
- return false;
+ return -1;
+}
+
+static bool musigsession_pop(const uint8_t psbt_session_id[static 32], musig_psbt_session_t *out) {
+ int i = find_slot(psbt_session_id);
+ if (i < 0) {
+ return false;
+ }
+ if (out != NULL) {
+ memcpy(out->_id, psbt_session_id, sizeof(out->_id));
+ memcpy(out->_rand_root,
+ (const void *) N_musig_storage.sessions[i].rand_root,
+ sizeof(out->_rand_root));
+ }
+ uint8_t zeros[sizeof(musig_stored_session_t)] = {0};
+ nvm_write((void *) &N_musig_storage.sessions[i],
+ (void *) zeros,
+ sizeof(musig_stored_session_t));
+
+ return true;
+}
+
+bool musigsession_exists(const uint8_t psbt_session_id[static 32]) {
+ return find_slot(psbt_session_id) >= 0;
}
__attribute__((warn_unused_result)) static bool musigsession_init_randomness(
### src/musig/musig_sessions.h
@@ -99,6 +99,17 @@ __attribute__((warn_unused_result)) const musig_psbt_session_t *musigsession_rou
uint8_t psbt_session_id[static 32],
musig_signing_state_t *musig_signing_state);
+/**
+ * Checks whether a session with the given id is in the persistent memory, without deleting it and
+ * without accessing its content.
+ *
+ * @param[in] psbt_session_id
+ * Pointer to the musig psbt session id.
+ *
+ * @return true if the session exists, false otherwise.
+ */
+bool musigsession_exists(const uint8_t psbt_session_id[static 32]);
+
/**
* If a session produced in round 1 is active in volatile memory, it is stored in the persistent
* memory. The signing state is then zeroed out, as the signing flow is over.
### tests/test_sign_psbt_musig.py
@@ -28,6 +28,7 @@
# error codes, see src/error_codes.h
EC_SIGN_PSBT_INVALID_MUSIG2_SESSION_ID = 0x0016
+EC_SIGN_PSBT_MUSIG2_SESSION_NOT_FOUND = 0x0017
# for now, we assume that there's a single internal musig placeholder, with a single internal key
@@ -498,6 +499,7 @@ def copy_psbt(psbt: PSBT) -> PSBT:
with pytest.raises(ExceptionRAPDU) as e:
signer_1.generate_partial_signatures(replayed_psbt)
assert DeviceException.exc.get(e.value.status) == BadStateError
+ assert int.from_bytes(e.value.data, byteorder='big') == EC_SIGN_PSBT_MUSIG2_SESSION_NOT_FOUND
assert len(replayed_psbt.inputs[0].musig2_partial_sigs) == 0
# 2) A failed round 2 consumes the session as well: after a round 2 that is rejected because of
@@ -517,6 +519,7 @@ def copy_psbt(psbt: PSBT) -> PSBT:
with pytest.raises(ExceptionRAPDU) as e:
signer_1.generate_partial_signatures(psbt)
assert DeviceException.exc.get(e.value.status) == BadStateError
+ assert int.from_bytes(e.value.data, byteorder='big') == EC_SIGN_PSBT_MUSIG2_SESSION_NOT_FOUND
assert len(psbt.inputs[0].musig2_partial_sigs) == 0
@@ -611,6 +614,7 @@ def test_sign_psbt_musig2_session_id_must_not_change(navigator: Navigator, firmw
with pytest.raises(ExceptionRAPDU) as e:
signer_1.generate_partial_signatures(psbt)
assert DeviceException.exc.get(e.value.status) == BadStateError
+ assert int.from_bytes(e.value.data, byteorder='big') == EC_SIGN_PSBT_MUSIG2_SESSION_NOT_FOUND
assert len(psbt.inputs[0].musig2_partial_sigs) == 0
@@ -631,6 +635,38 @@ def test_sign_psbt_musig2_invalid_session_id(navigator: Navigator, firmware: Fir
assert int.from_bytes(e.value.data, byteorder='big') == EC_SIGN_PSBT_INVALID_MUSIG2_SESSION_ID
+def test_sign_psbt_musig2_missing_session_fails_before_review(navigator: Navigator, firmware: Firmware, client: RaggerClient, test_name: str, speculos_globals: SpeculosGlobals):
+ # If the session for round 2 is missing, the device fails before asking the user to review the
+ # transaction, rather than after the approval.
+ wallet_policy, wallet_hmac = keypath_wallet_policy(speculos_globals)
+
+ # No navigator: round 1 has no UI, and round 2 must fail without showing any screen
+ signer_1 = LedgerMusig2Cosigner(client, wallet_policy, wallet_hmac)
+ signer_2 = HotMusig2Cosigner(wallet_policy, KEYPATH_COSIGNER_2_XPRIV)
+
+ psbt = PSBT()
+ psbt.deserialize(KEYPATH_PSBT_B64)
+ psbt.musig2_session_id = b"\x01" * 32
+ signer_1.generate_public_nonces(psbt)
+ signer_2.generate_public_nonces(psbt)
+
+ # round 2 with a MUSIG2_SESSION_ID for which round 1 was never executed
+ other_psbt = PSBT()
+ other_psbt.deserialize(psbt.serialize())
+ other_psbt.musig2_session_id = b"\x02" * 32
+ with pytest.raises(ExceptionRAPDU) as e:
+ signer_1.generate_partial_signatures(other_psbt)
+ assert DeviceException.exc.get(e.value.status) == BadStateError
+ assert int.from_bytes(e.value.data, byteorder='big') == EC_SIGN_PSBT_MUSIG2_SESSION_NOT_FOUND
+ assert len(other_psbt.inputs[0].musig2_partial_sigs) == 0
+
+ # the session of the first psbt is unaffected, and round 2 completes after the review
+ signer_1_with_review = LedgerMusig2Cosigner(client, wallet_policy, wallet_hmac,
+ navigator=navigator, instructions=sign_psbt_instruction_approve(firmware, save_screenshot=False, has_spend_from_wallet=True, has_feewarning=True), testname=test_name)
+ run_musig2_test(wallet_policy, psbt,
+ [Round2OnlyCosigner(signer_1_with_review), Round2OnlyCosigner(signer_2)], KEYPATH_SIGHASHES)
+
+
MAX_N_MUSIG_SESSIONS = 8
@@ -663,6 +699,7 @@ def test_sign_psbt_musig2_evicts_oldest_session(navigator: Navigator, firmware:
with pytest.raises(ExceptionRAPDU) as e:
signer_1.generate_partial_signatures(psbt)
assert DeviceException.exc.get(e.value.status) == BadStateError
+ assert int.from_bytes(e.value.data, byteorder='big') == EC_SIGN_PSBT_MUSIG2_SESSION_NOT_FOUND
# The others are still there. Storing the second to last session must not have evicted the one
# stored right before it, nor the last one the second to last.Why this scored 26/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.