test: the zeroize probe also watches the expanded ML-DSA key (QA S-04)
The probe looked only for its 32-byte canary, i.e. the seeds; the key material that actually signs is derived from them and never contains it, so an unwiped copy of the expanded key went unseen (QA's mutation Q15). The probe allocator now also watches up to four further 32-byte patterns, held in atomics (dealloc must not allocate or lock). The test computes the canary key's FIPS 204 expanded encoding with ml-dsa itself and watches K (held raw inside ml-dsa's ExpandedSigningKey) and a 32-byte window of the encoded s1; controls show an unwiped copy is seen and a Zeroizing one is not; then building, signing, binding and dropping a canary SigningKey must free no block holding either. Q15 re-checked: a SigningKey field holding an unwiped Vec of ml.to_expanded() fails the probe at the new assertion (reverted). The ml-dsa rnd / rho'' / y stack residue is out of the probe's reach (stack, not heap) and is recorded as a gap.
This commit is contained in:
Generated
+1
@@ -36,6 +36,7 @@ version = "0.0.0"
|
||||
dependencies = [
|
||||
"arcanum",
|
||||
"chacha20",
|
||||
"ml-dsa",
|
||||
"rand_core",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
@@ -11,6 +11,9 @@ arcanum = { path = ".." }
|
||||
zeroize = { version = "1.8", default-features = false, features = ["alloc"] }
|
||||
|
||||
[dev-dependencies]
|
||||
# QA S-04: the probe computes the canary key's expanded ML-DSA key to watch its bytes. Same version
|
||||
# and features as Arcanum's own dependency.
|
||||
ml-dsa = { version = "=0.1.2", default-features = false, features = ["alloc", "rand_core", "zeroize"] }
|
||||
chacha20 = { version = "0.10", default-features = false, features = ["rng"] }
|
||||
rand_core = { version = "0.10", default-features = false }
|
||||
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
//! 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.
|
||||
//! being wiped first, [`disarm`] returns true.
|
||||
//!
|
||||
//! QA S-04 (PR #2): secrets *derived* from the canary never contain it, so up to [`MAX_PATTERNS`]
|
||||
//! further 32-byte patterns can be watched too ([`watch`]), e.g. bytes of the expanded ML-DSA key.
|
||||
//!
|
||||
//! `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};
|
||||
use std::sync::atomic::{AtomicBool, AtomicU8, AtomicUsize, Ordering};
|
||||
|
||||
/// The marker. Exactly 32 bytes, the size of every Arcanum secret.
|
||||
pub const CANARY: [u8; 32] = *b"ARCANUM-ZEROIZE-CANARY-012345678";
|
||||
@@ -15,6 +18,43 @@ pub const CANARY: [u8; 32] = *b"ARCANUM-ZEROIZE-CANARY-012345678";
|
||||
static SEEN: AtomicBool = AtomicBool::new(false);
|
||||
static ARMED: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
/// How many extra patterns [`watch`] can hold.
|
||||
pub const MAX_PATTERNS: usize = 4;
|
||||
// Atomics, not a `Vec` or a `Mutex`: `dealloc` must not allocate or block.
|
||||
static PATTERNS: [[AtomicU8; 32]; MAX_PATTERNS] = [const { [const { AtomicU8::new(0) }; 32] }; MAX_PATTERNS];
|
||||
static PATTERN_COUNT: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
/// Also look for these 32-byte patterns (besides [`CANARY`]) from now on; `watch(&[])` stops.
|
||||
///
|
||||
/// # Panics
|
||||
/// If given more than [`MAX_PATTERNS`] patterns.
|
||||
pub fn watch(patterns: &[[u8; 32]]) {
|
||||
assert!(patterns.len() <= MAX_PATTERNS, "at most {MAX_PATTERNS} patterns");
|
||||
PATTERN_COUNT.store(0, Ordering::SeqCst);
|
||||
for (slot, p) in PATTERNS.iter().zip(patterns) {
|
||||
for (a, b) in slot.iter().zip(p) {
|
||||
a.store(*b, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
PATTERN_COUNT.store(patterns.len(), Ordering::SeqCst);
|
||||
}
|
||||
|
||||
fn holds_secret(block: &[u8]) -> bool {
|
||||
if block.windows(CANARY.len()).any(|w| w == CANARY) {
|
||||
return true;
|
||||
}
|
||||
for slot in PATTERNS.iter().take(PATTERN_COUNT.load(Ordering::SeqCst)) {
|
||||
let mut p = [0u8; 32];
|
||||
for (b, a) in p.iter_mut().zip(slot) {
|
||||
*b = a.load(Ordering::Relaxed);
|
||||
}
|
||||
if block.windows(32).any(|w| w == p) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// Start watching (and forget anything seen before).
|
||||
pub fn arm() {
|
||||
SEEN.store(false, Ordering::SeqCst);
|
||||
@@ -39,7 +79,7 @@ unsafe impl GlobalAlloc for Probe {
|
||||
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) {
|
||||
if holds_secret(block) {
|
||||
SEEN.store(true, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,10 +10,10 @@ use arcanum::exchange;
|
||||
use arcanum::seal;
|
||||
use arcanum::sign::{self, AppContext, SigningKey};
|
||||
use arcanum::xwing::SecretKey;
|
||||
use arcanum_zeroize_probe::{CANARY, arm, disarm};
|
||||
use arcanum_zeroize_probe::{CANARY, arm, disarm, watch};
|
||||
use chacha20::ChaCha20Rng;
|
||||
use rand_core::SeedableRng;
|
||||
use zeroize::Zeroizing;
|
||||
use zeroize::{Zeroize, Zeroizing};
|
||||
|
||||
fn rng(tag: u8) -> ChaCha20Rng {
|
||||
ChaCha20Rng::from_seed([tag; 32])
|
||||
@@ -115,4 +115,31 @@ fn secrets_are_wiped_before_their_heap_memory_is_freed() {
|
||||
assert_eq!(sign::verify_binding(sk.verifying_key(), b"id", kem.public_key(), (0, 1), 0, &bound), Ok(()));
|
||||
drop((sig, bound, sk));
|
||||
assert!(!disarm(), "signing freed a buffer that held a seed");
|
||||
|
||||
// --- QA S-04: the expanded ML-DSA key, which never contains the canary ---
|
||||
// FIPS 204 skEncode of the canary key: rho (0..32), K (32..64), tr (64..128), s1 (128..768), ...
|
||||
// Watch K (held raw in ml-dsa's ExpandedSigningKey) and a window of the encoded s1 (in any
|
||||
// encoded copy of the key, the shape QA's mutation Q15 leaked).
|
||||
#[allow(deprecated)]
|
||||
let mut expanded = ml_dsa::ExpandedSigningKey::<ml_dsa::MlDsa65>::from_seed(&CANARY.into()).to_expanded();
|
||||
let k: [u8; 32] = expanded[32..64].try_into().unwrap();
|
||||
let s1: [u8; 32] = expanded[128..160].try_into().unwrap();
|
||||
watch(&[k, s1]);
|
||||
// Controls: an unwiped copy of the encoded expanded key is seen, a wiped one is not.
|
||||
arm();
|
||||
drop(std::hint::black_box(expanded.to_vec()));
|
||||
assert!(disarm(), "control: an unwiped copy of the expanded key must be seen");
|
||||
arm();
|
||||
drop(std::hint::black_box(Zeroizing::new(expanded.to_vec())));
|
||||
assert!(!disarm(), "control: a Zeroizing copy must not be seen");
|
||||
expanded.as_mut_slice().zeroize();
|
||||
|
||||
// Arcanum: build, sign, bind and drop with the canary key, watching the expanded key's bytes.
|
||||
arm();
|
||||
let sk = Box::new(SigningKey::from_bytes(&seeds).unwrap());
|
||||
let sig = sk.sign(b"message", &ctx, &mut rng(8)).unwrap();
|
||||
let bound = sign::bind_kem_key(&sk, b"id", kem.public_key(), (0, 1), &mut rng(9)).unwrap();
|
||||
drop((sig, bound, sk));
|
||||
assert!(!disarm(), "a signing key was freed with expanded ML-DSA key material (K or s1) in memory");
|
||||
watch(&[]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user