From f3e17f3d5c902780216db04793578c77db07e238 Mon Sep 17 00:00:00 2001 From: mnovak Date: Wed, 25 Nov 2026 08:16:44 +0100 Subject: [PATCH] test(qa): A-29 allocator probe, secrets wiped before their heap memory is freed A separate, unpublished crate with its own workspace (like fuzz/), because a GlobalAlloc needs unsafe and arcanum keeps forbid(unsafe_code). dealloc scans each freed block for a 32-byte canary. Controls prove it sees an unwiped Vec and the reallocation case; checked by adding a reserve(1) after decryption in open(), which it catches. Heap only: stack copies are out of its reach. --- zeroize-probe/Cargo.lock | 437 +++++++++++++++++++++++++++++++++ zeroize-probe/Cargo.toml | 20 ++ zeroize-probe/src/lib.rs | 53 ++++ zeroize-probe/tests/zeroize.rs | 91 +++++++ 4 files changed, 601 insertions(+) create mode 100644 zeroize-probe/Cargo.lock create mode 100644 zeroize-probe/Cargo.toml create mode 100644 zeroize-probe/src/lib.rs create mode 100644 zeroize-probe/tests/zeroize.rs diff --git a/zeroize-probe/Cargo.lock b/zeroize-probe/Cargo.lock new file mode 100644 index 0000000..6a25988 --- /dev/null +++ b/zeroize-probe/Cargo.lock @@ -0,0 +1,437 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "aead" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1973cfbc1a2daf9cf550e74e1f088c28e7f7d8c1e1418fb6c9dc5184b7e84c99" +dependencies = [ + "crypto-common", + "inout", +] + +[[package]] +name = "arcanum" +version = "0.1.0" +dependencies = [ + "chacha20poly1305", + "hkdf", + "hmac", + "ml-kem", + "rand_core", + "sha2", + "sha3", + "shake", + "x25519-dalek", + "zeroize", +] + +[[package]] +name = "arcanum-zeroize-probe" +version = "0.0.0" +dependencies = [ + "arcanum", + "chacha20", + "rand_core", + "zeroize", +] + +[[package]] +name = "autocfg" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" + +[[package]] +name = "block-buffer" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2f6c7dbe95a6ed67ad9f18e57daf93a2f034c524b99fd2b76d18fdfeb6660aa" +dependencies = [ + "hybrid-array", + "zeroize", +] + +[[package]] +name = "cfg-if" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" + +[[package]] +name = "chacha20" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06" +dependencies = [ + "cfg-if", + "cipher", + "cpufeatures", + "rand_core", + "zeroize", +] + +[[package]] +name = "chacha20poly1305" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b89e1c441e926b9c82a8d023f6e1b7ae0adcfaa7d621814e4d60789bac751cb" +dependencies = [ + "aead", + "chacha20", + "cipher", + "poly1305", + "zeroize", +] + +[[package]] +name = "cipher" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8cf2a2c93cd704877c0858356ed03480ff301ee950b43f1cbe4573b088bfa6c" +dependencies = [ + "block-buffer", + "crypto-common", + "inout", +] + +[[package]] +name = "cmov" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c9ea0ac24bc397ab3c98583a3c9ba74fa56b09a4449bbe172b9b1ddb016027a" + +[[package]] +name = "cpufeatures" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca28b0ae3115b884660db4118d803791fd6756b6e88f39c0f3f7859060d7566" +dependencies = [ + "libc", +] + +[[package]] +name = "crypto-common" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce6e4c961d6cd6c9a86db418387425e8bdeaf05b3c8bc1411e6dca4c252f1453" +dependencies = [ + "hybrid-array", + "rand_core", +] + +[[package]] +name = "ctutils" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "261af4dc7a06ea8a527a8717d8cbba3417530d6ea14e1edb3de6b67806be474f" +dependencies = [ + "cmov", +] + +[[package]] +name = "curve25519-dalek" +version = "5.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5eed333089e2e1c1ac8c6c0398e5e2497b4c9926ca6d0365ed1e099afa5bc23" +dependencies = [ + "cfg-if", + "cpufeatures", + "curve25519-dalek-derive", + "fiat-crypto", + "rustc_version", + "subtle", + "zeroize", +] + +[[package]] +name = "curve25519-dalek-derive" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "digest" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1dd6dbb5841937940781866fa1281a1ff7bd3bf827091440879f9994983d5c2" +dependencies = [ + "block-buffer", + "crypto-common", + "ctutils", + "zeroize", +] + +[[package]] +name = "fiat-crypto" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64cd1e32ddd350061ae6edb1b082d7c54915b5c672c389143b9a63403a109f24" + +[[package]] +name = "hkdf" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4aaa26c720c68b866f2c96ef5c1264b3e6f473fe5d4ce61cd44bbe913e553018" +dependencies = [ + "hmac", +] + +[[package]] +name = "hmac" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6303bc9732ae41b04cb554b844a762b4115a61bfaa81e3e83050991eeb56863f" +dependencies = [ + "digest", +] + +[[package]] +name = "hybrid-array" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27f864f10dfb56725ce5ce5472bc52252c8f93a4ab86327122cebf62c5f59a17" +dependencies = [ + "ctutils", + "typenum", + "zeroize", +] + +[[package]] +name = "inout" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a580c13f0ffc335ef42dd3f6b1a41b726051c5e1656d9a3aa6fddb3c5dfbbf9" +dependencies = [ + "hybrid-array", +] + +[[package]] +name = "keccak" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8f198d1db720e4940b5a493201d199d9f24f568f8f746bd13706243a2f71598" +dependencies = [ + "cfg-if", + "cpufeatures", +] + +[[package]] +name = "kem" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01737161ba802849cfd486b5bd209d38ba4943494c249a8126005170c7621edd" +dependencies = [ + "crypto-common", + "rand_core", +] + +[[package]] +name = "libc" +version = "0.2.191" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" + +[[package]] +name = "ml-kem" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84ca78b66f46050d49fa21693ae8adab2081d8101b65d8067316b6b70b64a0d1" +dependencies = [ + "hybrid-array", + "kem", + "module-lattice", + "rand_core", + "sha3", + "shake", + "zeroize", +] + +[[package]] +name = "module-lattice" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4a1e412c8ee83a1c678f427806219c49c96b2511b9f07497514f0e43fb26155" +dependencies = [ + "ctutils", + "hybrid-array", + "num-traits", + "zeroize", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "poly1305" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e2d0073b297041425c7c3df6eb4792d598a15323fe63346852b092eca02904c" +dependencies = [ + "cpufeatures", + "universal-hash", +] + +[[package]] +name = "proc-macro2" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand_core" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69" + +[[package]] +name = "rustc_version" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" +dependencies = [ + "semver", +] + +[[package]] +name = "semver" +version = "1.0.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd" + +[[package]] +name = "sha2" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d7069beb7d6ac7b9acd1039986e73443f24234f41074da099d6f994ac9ad19" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "sha3" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc9bad02c26382724b2d2692c6f179285e4b54eeecd7968f52a50059c3c11759" +dependencies = [ + "digest", + "keccak", + "sponge-cursor", +] + +[[package]] +name = "shake" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09057cb2149ad4cbd2da1e26b351f9a4c354219421229c69c3063e6f61947c4a" +dependencies = [ + "digest", + "keccak", + "sponge-cursor", +] + +[[package]] +name = "sponge-cursor" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a0219bd7d979d58245a4f41f695e1ac9f8befdffadd7f61f1bae9e39abc6620" +dependencies = [ + "zeroize", +] + +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + +[[package]] +name = "syn" +version = "2.0.119" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "typenum" +version = "1.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" + +[[package]] +name = "unicode-ident" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" + +[[package]] +name = "universal-hash" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4987bdc12753382e0bec4a65c50738ffaabc998b9cdd1f952fb5f39b0048a96" +dependencies = [ + "crypto-common", + "ctutils", +] + +[[package]] +name = "x25519-dalek" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7e8131a03190127fb2263afc72b322ecadae46b6ff8c6f399ff5d02f5559af6" +dependencies = [ + "curve25519-dalek", + "rand_core", + "zeroize", +] + +[[package]] +name = "zeroize" +version = "1.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85ccff8a4805aba1eaa4cd4afcd49f7557dd2cf5fd79ffc37f8959466effc5aa" +dependencies = [ + "zeroize_derive", +] + +[[package]] +name = "zeroize_derive" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c50655cbb0fe3fc43170059e702f1ce5e19b84cec58dc87b037a09935c2f328" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/zeroize-probe/Cargo.toml b/zeroize-probe/Cargo.toml new file mode 100644 index 0000000..00e0d52 --- /dev/null +++ b/zeroize-probe/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "arcanum-zeroize-probe" +version = "0.0.0" +edition = "2024" +license = "CPAL-1.0" +publish = false +description = "QA A-29: a counting allocator that checks Arcanum's secrets are wiped before their heap memory is freed. Test-only; never a dependency of anything." + +[dependencies] +arcanum = { path = ".." } +zeroize = { version = "1.8", default-features = false, features = ["alloc"] } + +[dev-dependencies] +chacha20 = { version = "0.10", default-features = false, features = ["rng"] } +rand_core = { version = "0.10", default-features = false } + +# Its own workspace, like fuzz/: the probe needs `unsafe` (a GlobalAlloc), and Arcanum itself +# keeps `#![forbid(unsafe_code)]`. Nothing depends on this crate. +[workspace] +members = ["."] diff --git a/zeroize-probe/src/lib.rs b/zeroize-probe/src/lib.rs new file mode 100644 index 0000000..bf243c3 --- /dev/null +++ b/zeroize-probe/src/lib.rs @@ -0,0 +1,53 @@ +//! QA A-29: a global allocator that, in `dealloc`, scans every block for a 32-byte canary before +//! handing it back to the system allocator. If a secret built from the canary is freed without +//! being wiped first, [`seen`] turns true. +//! +//! `unsafe` lives here only, in a crate that is not published and that nothing depends on; +//! Arcanum itself keeps `#![forbid(unsafe_code)]`. Limits: the probe sees the heap only. Stack +//! copies made by moves are invisible to it (and to `zeroize`), see docs/THREAT_MODEL.md. + +use std::alloc::{GlobalAlloc, Layout, System}; +use std::sync::atomic::{AtomicBool, Ordering}; + +/// The marker. Exactly 32 bytes, the size of every Arcanum secret. +pub const CANARY: [u8; 32] = *b"ARCANUM-ZEROIZE-CANARY-012345678"; + +static SEEN: AtomicBool = AtomicBool::new(false); +static ARMED: AtomicBool = AtomicBool::new(false); + +/// Start watching (and forget anything seen before). +pub fn arm() { + SEEN.store(false, Ordering::SeqCst); + ARMED.store(true, Ordering::SeqCst); +} + +/// Stop watching; true if a freed block held the canary since [`arm`]. +pub fn disarm() -> bool { + ARMED.store(false, Ordering::SeqCst); + SEEN.load(Ordering::SeqCst) +} + +struct Probe; + +// SAFETY: forwards every call to `System` unchanged; `dealloc` only reads the block it is about +// to free, which is valid for `layout.size()` bytes by the `GlobalAlloc` contract. +unsafe impl GlobalAlloc for Probe { + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + unsafe { System.alloc(layout) } + } + + unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { + if ARMED.load(Ordering::Relaxed) && layout.size() >= CANARY.len() { + let block = unsafe { std::slice::from_raw_parts(ptr, layout.size()) }; + if block.windows(CANARY.len()).any(|w| w == CANARY) { + SEEN.store(true, Ordering::SeqCst); + } + } + unsafe { System.dealloc(ptr, layout) } + } + // `realloc` is deliberately not overridden: the default moves the data with `self.alloc` and + // frees the old block with `self.dealloc`, so a growing buffer's old copy is scanned too. +} + +#[global_allocator] +static GLOBAL: Probe = Probe; diff --git a/zeroize-probe/tests/zeroize.rs b/zeroize-probe/tests/zeroize.rs new file mode 100644 index 0000000..4bf3dcd --- /dev/null +++ b/zeroize-probe/tests/zeroize.rs @@ -0,0 +1,91 @@ +//! QA A-29: heap memory that held Arcanum secret bytes is wiped before it is freed. +//! +//! One test function, run sequentially, because the probe is process-global. The controls come +//! first and prove the probe is not blind: an unwiped `Vec` and a `Zeroizing` that grows +//! (the reallocation case) are both caught; a `Zeroizing` that does not grow is not. +//! +//! Run: `cargo test --manifest-path zeroize-probe/Cargo.toml -- --test-threads=1` + +use arcanum::exchange; +use arcanum::seal; +use arcanum::xwing::SecretKey; +use arcanum_zeroize_probe::{CANARY, arm, disarm}; +use chacha20::ChaCha20Rng; +use rand_core::SeedableRng; +use zeroize::Zeroizing; + +fn rng(tag: u8) -> ChaCha20Rng { + ChaCha20Rng::from_seed([tag; 32]) +} + +#[test] +fn secrets_are_wiped_before_their_heap_memory_is_freed() { + // --- controls: the probe sees what it should --- + arm(); + drop(std::hint::black_box(CANARY.to_vec())); + assert!(disarm(), "control: an unwiped Vec holding the canary must be seen"); + + arm(); + drop(std::hint::black_box(Zeroizing::new(CANARY.to_vec()))); + assert!(!disarm(), "control: a Zeroizing that never grows must not be seen"); + + arm(); + let mut grows = Zeroizing::new(CANARY.to_vec()); + grows.extend_from_slice(&[0u8; 4096]); // reallocates: the old block is freed unwiped + drop(std::hint::black_box(grows)); + assert!(disarm(), "control: the reallocation case (A-29's typical bug) must be seen"); + + // --- Arcanum --- + // The seed (= the stored secret key) inside a heap-allocated SecretKey. + arm(); + drop(std::hint::black_box(Box::new(SecretKey::from_bytes(&CANARY).unwrap()))); + assert!(!disarm(), "a boxed SecretKey was freed with its seed still in memory"); + + // The exported seed. + arm(); + let sk = SecretKey::from_bytes(&CANARY).unwrap(); + drop(std::hint::black_box(Box::new(sk.to_seed()))); + assert!(!disarm(), "a boxed to_seed() copy was freed unwiped"); + + // seal: the plaintext is copied into the output buffer and encrypted in place. + let recipient = SecretKey::generate(&mut rng(1)); + arm(); + let sealed = seal::seal(recipient.public_key(), &CANARY, b"aad", &mut rng(2)).unwrap(); + let sealed_copy = sealed.clone(); + drop(std::hint::black_box(sealed)); + assert!(!disarm(), "seal freed a buffer that held the plaintext"); + + // open: the returned plaintext, dropped by the caller. + arm(); + let opened = seal::open(&recipient, &sealed_copy, b"aad").unwrap(); + assert_eq!(opened.as_slice(), CANARY); + drop(std::hint::black_box(opened)); + assert!(!disarm(), "an opened plaintext was freed unwiped"); + + // open with the wrong aad: the failure path frees its buffer. + arm(); + assert!(seal::open(&recipient, &sealed_copy, b"wrong").is_err()); + assert!(!disarm(), "a failed open freed a buffer that held plaintext"); + + // A plaintext large enough to be split across many blocks by nobody but us. + let big: Vec = CANARY.iter().copied().cycle().take(70_000).collect(); + arm(); + let sealed_big = seal::seal(recipient.public_key(), &big, b"", &mut rng(3)).unwrap(); + let opened_big = seal::open(&recipient, &sealed_big, b"").unwrap(); + assert_eq!(opened_big.as_slice(), big.as_slice()); + drop(opened_big); + drop(sealed_big); + assert!(!disarm(), "a 70 kB seal/open freed plaintext unwiped"); + drop(Zeroizing::new(big)); + + // A whole exchange on the heap, with the canary as context. + let a = SecretKey::from_bytes(&[7; 32]).unwrap(); + let b = SecretKey::from_bytes(&[8; 32]).unwrap(); + arm(); + let init = Box::new(exchange::initiate(&a, b.public_key(), &CANARY, &mut rng(4)).unwrap()); + let (ct, rs) = exchange::respond(&b, a.public_key(), init.ciphertext(), &CANARY, &mut rng(5)).unwrap(); + let is = Box::new(init.finish(&ct).unwrap()); + let psk = Box::new(is.wireguard_psk(1)); + drop((psk, is, rs)); + assert!(!disarm(), "an exchange freed its context unwiped"); +}