Merge remote-tracking branch 'agent/benma-agent/unstable-key-sorting'
What changed, and why it matters
This commit replaces Rust's stable sort with an unstable sort in three places inside the BitBox02 firmware. Stable and unstable sorts produce the same output when all items are different, but can differ when there are duplicate items. In two places (multisig xpub hashing and pkscript building), the sorted data is hashed or turned into a Bitcoin script, so any change in ordering could change the resulting wallet fingerprint or transaction address. In the third place (Cardano signing keypaths), the list is deduplicated right after sorting, so duplicates are removed anyway. The change is likely intended to save a small amount of memory or runtime, but it is a partial patch: the commit message gives no explanation, and there is no visible follow-up to ensure callers never rely on stability for duplicate entries.
Treat this as a code-quality and determinism concern, not as a confirmed vulnerability. Review whether duplicate xpubs or pubkeys can reach `get_hash` and `pkscript`; if they can, either deduplicate before sorting or revert to `.sort()` to preserve deterministic ordering. For Cardano, the change is safe because `dedup()` follows. Ask the vendor for the rationale behind the merge and whether it was paired with validation that guarantees unique inputs.
Security signals we found
Use of unstable sort on data that is later hashed or serialized into a script
No visible duplicate-removal before hashing/scripting in the two multisig sites
Merge commit with no explanatory security rationale
Potential non-determinism in multisig account/address derivation if duplicate keys are present
Evidence from the diff
The diff changes .sort() to .sort_unstable() in three locations: (1) serialized xpubs before hashing in bitcoin/multisig.rs::get_hash, (2) pubkeys before building a multisig pkscript in bitcoin/multisig.rs::pkscript, and (3) Cardano signing keypaths before deduplication in cardano/sign_transaction.rs::_process. Rust’s sort_unstable is generally faster and uses O(1) extra memory, but it does not preserve the relative order of equal elements. For the Cardano path, dedup() immediately follows, so equal adjacent elements are collapsed and order-of-equals is irrelevant. For the Bitcoin multisig paths, duplicates are not obviously removed before hashing or script construction. If duplicate xpubs/pubkeys can reach these functions, the resulting multisig hash or pkscript may become non-deterministic across firmware versions or compiler builds, which could break address/account consistency or reproducibility. The commit message is only a merge title and does not state a security rationale or assert that duplicates are impossible.
Changed components
src/rust/bitbox02-rust/src/hww/api/bitcoin/multisig.rssrc/rust/bitbox02-rust/src/hww/api/cardano/sign_transaction.rsInspect captured patch +3 / −3
### src/rust/bitbox02-rust/src/hww/api/bitcoin/multisig.rs
@@ -73,7 +73,7 @@ pub fn get_hash(
.map(|xpub| bip32::Xpub::from(xpub).serialize(None))
.collect::<Result<Vec<Vec<u8>>, ()>>()?;
if let SortXpubs::Yes = sort_xpubs {
- xpubs_serialized.sort();
+ xpubs_serialized.sort_unstable();
}
for xpub in xpubs_serialized.iter() {
hasher.update(xpub);
@@ -303,7 +303,7 @@ pub fn pkscript(
.to_vec())
})
.collect::<Result<_, _>>()?;
- pubkeys.sort();
+ pubkeys.sort_unstable();
let mut script_builder = bitcoin::script::Builder::new().push_int(multisig.threshold as _);
for pk in pubkeys.iter() {
### src/rust/bitbox02-rust/src/hww/api/cardano/sign_transaction.rs
@@ -317,7 +317,7 @@ async fn _process(
out
};
- signing_keypaths.sort();
+ signing_keypaths.sort_unstable();
signing_keypaths.dedup();
let mut shelley_witnesses: Vec<ShelleyWitness> = Vec::with_capacity(signing_keypaths.len());Why this scored 24/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.