musig_sessions: evict the oldest session when the storage is full
What changed, and why it matters
This commit changes how the Ledger Bitcoin app manages saved MuSig2 signing sessions in its limited persistent storage. Previously, when storage was full, the app would simply overwrite the first slot, which could discard a newer session instead of the oldest one. The fix adds an age counter so the oldest session is always evicted first. It also shrinks the stored session data to fit within a single memory page, storing only a 28-byte prefix of the session ID instead of the full 32 bytes. The commit itself treats this as a robustness improvement rather than a security fix, and there is no disclosed vulnerability or attacker benefit shown in the diff.
Treat as a normal code-quality and robustness improvement. Review whether the 28-byte prefix is acceptable for the application's collision resistance requirements, and confirm that counter persistence and NVRAM write wear are acceptable. No urgent security response is indicated by the supplied materials.
Security signals we found
Change in persistent storage eviction policy from first-slot overwrite to oldest-session eviction
Reduction of stored session identifier from 32 bytes to 28-byte prefix
Addition of monotonic counter to stored sessions
New regression test for storage-full eviction behavior
No mention of CVE, vulnerability, attacker, or security impact in commit message
Evidence from the diff
The patch refactors musig_sessions.c to introduce a dedicated musig_stored_session_t structure (64 bytes, aligned to one NVRAM page) containing a 28-byte id_prefix, a 32-byte rand_root, and a uint32_t counter. musigsession_store now scans all slots: if a free slot exists it is used; otherwise the slot with the smallest counter is evicted. The new session receives max_counter + 1. musigsession_pop matches sessions by the 28-byte prefix and reconstructs the in-memory musig_psbt_session_t. A test is added to verify that creating MAX_N_MUSIG_SESSIONS + 2 sessions evicts the two oldest. The commit message frames this as addressing a TODO about eviction order, not as a security bug fix.
Changed components
src/musig/musig_sessions.csrc/musig/musig_sessions.htests/test_sign_psbt_musig.pyInspect captured patch +106 / −21
### src/musig/musig_sessions.c
@@ -8,28 +8,55 @@
/* Local headers */
#include "crypto.h"
+// A session in the persistent storage.
+// Only a prefix of the psbt_session_id is stored, so that each session fits in 64 bytes. This is
+// harmless: collisions remain nearly impossible, and anyway a collision on the ID only causes a
+// session to be deleted, and the following round 2 to fail.
+#define STORED_ID_PREFIX_LEN 28
+
+typedef struct {
+ uint8_t id_prefix[STORED_ID_PREFIX_LEN];
+ // Incremented for each stored session, in order to evict the oldest session when the storage is
+ // full. It is never 0 for a stored session; it would take 2^32 sessions to overflow.
+ uint32_t counter;
+ uint8_t rand_root[32];
+} musig_stored_session_t;
+
+_Static_assert(sizeof(musig_stored_session_t) == 64, "A stored session must take 64 bytes");
+
typedef struct {
// Aligning by 4 is necessary due to platform limitations.
// Aligning by 64 further guarantees that each session occupies exactly
// a single NVRAM page, minimizing the number of writes.
- __attribute__((aligned(64))) musig_psbt_session_t sessions[MAX_N_MUSIG_SESSIONS];
+ __attribute__((aligned(64))) musig_stored_session_t sessions[MAX_N_MUSIG_SESSIONS];
} musig_persistent_storage_t;
const musig_persistent_storage_t N_musig_storage_real;
#define N_musig_storage (*(const volatile musig_persistent_storage_t *) PIC(&N_musig_storage_real))
+static bool is_slot_free(int i) {
+ return is_array_all_zeros((uint8_t *) &N_musig_storage.sessions[i],
+ sizeof(musig_stored_session_t));
+}
+
static bool musigsession_pop(const uint8_t psbt_session_id[static 32], musig_psbt_session_t *out) {
for (int i = 0; i < MAX_N_MUSIG_SESSIONS; i++) {
- if (memcmp(psbt_session_id, (const void *) N_musig_storage.sessions[i]._id, 32) == 0) {
+ 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,
- (const void *) &N_musig_storage.sessions[i],
- sizeof(musig_psbt_session_t));
+ 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_psbt_session_t)] = {0};
+ uint8_t zeros[sizeof(musig_stored_session_t)] = {0};
nvm_write((void *) &N_musig_storage.sessions[i],
(void *) zeros,
- sizeof(musig_psbt_session_t));
+ sizeof(musig_stored_session_t));
return true;
}
@@ -53,22 +80,39 @@ static void musigsession_store(const uint8_t psbt_session_id[static 32],
// make sure that no session with the same id exists; delete it otherwise
musigsession_pop(psbt_session_id, NULL);
- int i;
- for (i = 0; i < MAX_N_MUSIG_SESSIONS; i++) {
- if (is_array_all_zeros((uint8_t *) &N_musig_storage.sessions[i],
- sizeof(musig_psbt_session_t))) {
- break;
+ // Use a free slot if any; otherwise, evict the oldest session (the one with the smallest
+ // counter). The new session gets a counter larger than all the stored ones.
+ int free_slot = -1;
+ int oldest_slot = -1;
+ uint32_t max_counter = 0;
+ for (int i = 0; i < MAX_N_MUSIG_SESSIONS; i++) {
+ if (is_slot_free(i)) {
+ if (free_slot < 0) {
+ free_slot = i;
+ }
+ continue;
+ }
+ uint32_t counter = N_musig_storage.sessions[i].counter;
+ if (oldest_slot < 0 || counter < N_musig_storage.sessions[oldest_slot].counter) {
+ oldest_slot = i;
+ }
+ if (counter > max_counter) {
+ max_counter = counter;
}
}
- if (i >= MAX_N_MUSIG_SESSIONS) {
- // no free slot found, delete the first by default
- // TODO: should we use a LIFO structure? Could add a counter to musig_psbt_session_t
- i = 0;
- }
+ int chosen = free_slot >= 0 ? free_slot : oldest_slot;
+
+ musig_stored_session_t stored_session;
+ memcpy(stored_session.id_prefix, psbt_session_id, sizeof(stored_session.id_prefix));
+ stored_session.counter = max_counter + 1;
+ memcpy(stored_session.rand_root, session->_rand_root, sizeof(stored_session.rand_root));
+
// replace the chosen slot
- nvm_write((void *) &N_musig_storage.sessions[i],
- (void *) session,
- sizeof(musig_psbt_session_t));
+ nvm_write((void *) &N_musig_storage.sessions[chosen],
+ (void *) &stored_session,
+ sizeof(musig_stored_session_t));
+
+ explicit_bzero(&stored_session, sizeof(stored_session));
}
void compute_rand_i_j(const musig_psbt_session_t *psbt_session,
### src/musig/musig_sessions.h
@@ -10,7 +10,8 @@
* documentation in docs/musig.md for more information.
*/
-// the maximum number of musig sessions that are stored in permanent memory
+// the maximum number of musig sessions that are stored in permanent memory; when the storage is
+// full, storing a new session evicts the oldest one
#define MAX_N_MUSIG_SESSIONS 8
// state of a musig_psbt_session. Members are private and must not be accessed directly by any
### tests/test_sign_psbt_musig.py
@@ -629,3 +629,43 @@ def test_sign_psbt_musig2_invalid_session_id(navigator: Navigator, firmware: Fir
testname=test_name)
assert DeviceException.exc.get(e.value.status) == IncorrectDataError
assert int.from_bytes(e.value.data, byteorder='big') == EC_SIGN_PSBT_INVALID_MUSIG2_SESSION_ID
+
+
+MAX_N_MUSIG_SESSIONS = 8
+
+
+def test_sign_psbt_musig2_evicts_oldest_session(navigator: Navigator, firmware: Firmware, client: RaggerClient, test_name: str, speculos_globals: SpeculosGlobals):
+ # The device stores at most MAX_N_MUSIG_SESSIONS sessions; when the storage is full, round 1
+ # evicts the oldest session, regardless of the slot it occupies.
+ wallet_policy, wallet_hmac = keypath_wallet_policy(speculos_globals)
+
+ signer_1 = 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)
+
+ # Execute round 1 for MAX_N_MUSIG_SESSIONS + 2 sessions, that only differ in the
+ # MUSIG2_SESSION_ID. Any session left in storage by previous tests is older, hence evicted first.
+ n_sessions = MAX_N_MUSIG_SESSIONS + 2
+ psbts: List[PSBT] = []
+ hot_signers: List[HotMusig2Cosigner] = []
+ for i in range(n_sessions):
+ psbt = PSBT()
+ psbt.deserialize(KEYPATH_PSBT_B64)
+ psbt.musig2_session_id = bytes([i + 1])
+ # the hot cosigner identifies its sessions by the transaction, so each psbt needs its own
+ hot_signer = HotMusig2Cosigner(wallet_policy, KEYPATH_COSIGNER_2_XPRIV)
+ signer_1.generate_public_nonces(psbt)
+ hot_signer.generate_public_nonces(psbt)
+ psbts.append(psbt)
+ hot_signers.append(hot_signer)
+
+ # The two oldest sessions were evicted
+ for psbt in psbts[:2]:
+ with pytest.raises(ExceptionRAPDU) as e:
+ signer_1.generate_partial_signatures(psbt)
+ assert DeviceException.exc.get(e.value.status) == BadStateError
+
+ # 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.
+ for i in [2, n_sessions - 3, n_sessions - 2, n_sessions - 1]:
+ run_musig2_test(wallet_policy, psbts[i],
+ [Round2OnlyCosigner(signer_1), Round2OnlyCosigner(hot_signers[i])], KEYPATH_SIGHASHES)Why this scored 30/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.