fix: borrow plaintext in Keystore::seal to avoid un-zeroized copy#438
Open
NnnOooPppEee wants to merge 1 commit into
Open
Conversation
K_intermediate was copied into an owned, un-zeroized Vec<u8> before being sealed on first-time key generation, so the plaintext master key could linger in freed heap memory. Keystore::seal now borrows &[u8] so the caller's Zeroizing buffer is never handed away as an owned copy. The one remaining copy needed to cross into the uniffi DeviceKeystore callback interface (which only supports pass-by-value parameters) is an accepted, documented limitation. Ref SEC-2260 / HackerOne #3598947
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
walletkit_db::Keystore::sealtook its plaintext as an ownedVec<u8>. On first-time key generation,init_or_open_envelope_keycalledk_intermediate.to_vec()to satisfy that signature, creating a copy of the 32-byte master key (K_intermediate) that was never zeroized on drop and could linger in freed heap memory.sealnow borrows&[u8]for bothaadandplaintext, sok_intermediate(already held in aZeroizingbuffer) is passed by reference and never copied into an un-zeroized owned buffer at this layer.The
Ksbridge inwalletkit-core(which adaptsKeystoreonto the uniffi-exportedDeviceKeystoretrait) still makes one owned copy to cross intoDeviceKeystore::seal. That is unavoidable: uniffi callback interfaces only support pass-by-value parameters (no&[u8]), confirmed against the current uniffi docs. This matches the known limitation already discussed on the report (further copies inside the foreign Swift/Kotlin/etc. implementation are outside Rust's control regardless of this change).Scope note
This only changes
Keystore::seal's parameter types (notopen_sealed, whoseaad/ciphertextaren't sensitive) and does not touchDeviceKeystore(walletkit-core's uniffi trait), since uniffi does not support borrowed parameters for foreign trait implementations.walletkit-dbis published (publish = true); this is a breaking change toKeystore::seal's signature for any external implementer, worth a minor version bump at the next release.Linear
SEC-2260
Test plan
cargo test -p walletkit-db -p walletkit-core --lib(140 tests passed)cargo clippy -p walletkit-db -p walletkit-core --lib --tests(clean)rustfmt --checkon changed filesNote
Medium Risk
Breaking API change to a published crate's crypto trait, but behavior is tighter around secret handling; FFI path still copies key material once by design.
Overview
walletkit_db::Keystore::sealnow takesaadandplaintextas&[u8]instead of ownedVec<u8>, so callers can pass secrets fromZeroizingbuffers without handing this crate an extra heap copy that may not be zeroized on drop.On first envelope creation,
init_or_open_envelope_keyno longer callsk_intermediate.to_vec()before sealing; it passesk_intermediate.as_slice()directly.open_sealedis unchanged (still ownedVecfor non-sensitive ciphertext/AAD).The
Ksadapter inwalletkit-corestill **to_vec()**s at the uniffiDeviceKeystoreboundary (pass-by-value only); comments document that as an accepted limitation. TestXorKeystoreand trait docs are updated for the new signature.Breaking change for external
Keystoreimplementers of the publishedwalletkit-dbcrate.Reviewed by Cursor Bugbot for commit 0f8a5aa. Bugbot is set up for automated code reviews on this repo. Configure here.