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.
This commit is contained in:
@@ -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;
|
||||
Reference in New Issue
Block a user