Skip to content

Commit 4dfe229

Browse files
committed
Add optional x86 cache flush sanitization
1 parent a023f42 commit 4dfe229

7 files changed

Lines changed: 390 additions & 15 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
fixed-size secrets backed by private `mmap` storage and `mlock`.
2020
- Added the optional x86_64 `asm-compare` feature for assembly-backed
2121
equal-length byte comparison with portable fallback elsewhere.
22+
- Added the optional x86_64 `cache-flush` feature for explicit volatile-clear
23+
plus `clflush`/`mfence` cache-line eviction helpers.
2224
- Updated README, safety notes, and threat model for the new clearing model.
2325

2426
## 1.0.0-rc.4

‎Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ alloc = []
3838
std = ["alloc"]
3939
memory-lock = []
4040
asm-compare = []
41+
cache-flush = []
4142
# Compatibility flag for older release-candidate dependency declarations.
4243
# Volatile wiping is now the default clear path.
4344
unsafe-wipe = []

‎README.md‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Implemented now:
5050
- optional `alloc` support with `SecretVec` and `SecretString`.
5151
- optional Linux memory locking with `LockedSecretBytes<N>`.
5252
- optional x86_64 assembly-backed equal-length comparison.
53+
- optional x86_64 volatile-clear plus cache-line eviction helpers.
5354
- explicit volatile helper APIs for existing ordinary buffers.
5455
- redacted `Debug` for secret-owning wrapper types.
5556
- clear-on-drop behavior for crate-owned secret containers.
@@ -69,7 +70,7 @@ Implemented now:
6970
| Heap support | `alloc` feature |
7071
| Proc macros | none |
7172
| Main guarantee | narrow ownership, redaction, and clear-on-drop hygiene |
72-
| Out of scope | stack-history wiping, cache flushing, crash dumps, privileged reads |
73+
| Out of scope | stack-history wiping, global cache secrecy, crash dumps, privileged reads |
7374

7475
Read [THREAT_MODEL.md](THREAT_MODEL.md) and [SAFETY.md](SAFETY.md) before
7576
using this crate for high-assurance secret handling.
@@ -126,6 +127,7 @@ sanitization = { version = "1.0.0-rc.5", features = ["memory-lock"] }
126127
| `std` | no | Currently aliases `alloc` for downstream convenience. |
127128
| `memory-lock` | no | Enables Linux `LockedSecretBytes<N>` on `x86_64` and `aarch64`. |
128129
| `asm-compare` | no | Uses an x86_64 inline-assembly loop for equal-length byte comparison. |
130+
| `cache-flush` | no | Enables explicit x86_64 clear-and-cache-line-evict helpers. |
129131
| `unsafe-wipe` | no | Compatibility no-op; volatile wiping is default. |
130132

131133
Default builds are dependency-free and `no_std`.
@@ -370,6 +372,33 @@ let secret = VolatileOnDrop::new([1_u8, 2, 3, 4]);
370372
assert_eq!(secret.with_secret(|bytes| bytes.len()), 4);
371373
```
372374

375+
## Cache Flush Sanitization
376+
377+
Enable `cache-flush` on x86_64 when a call site explicitly needs volatile
378+
clearing followed by `clflush`/`mfence` over the affected cache lines:
379+
380+
```toml
381+
[dependencies]
382+
sanitization = { version = "1.0.0-rc.5", features = ["cache-flush"] }
383+
```
384+
385+
```rust
386+
use sanitization::{cache_flush::cache_flush_sanitize_bytes, SecretBytes};
387+
388+
let mut scratch = [0xA5; 32];
389+
cache_flush_sanitize_bytes(&mut scratch);
390+
assert_eq!(scratch, [0; 32]);
391+
392+
let mut key = SecretBytes::<32>::from_array([7; 32]);
393+
key.secure_clear_and_flush();
394+
assert!(key.constant_time_eq(&[0; 32]));
395+
```
396+
397+
With `alloc`, `cache_flush_sanitize_vec` and `cache_flush_sanitize_string`
398+
clear the full allocation capacity before flushing the allocation's cache
399+
lines. Unsupported targets, Miri, and builds without `cache-flush` do not expose
400+
the `cache_flush` module.
401+
373402
## Assembly Comparison
374403

375404
Enable `asm-compare` on x86_64 when you want equal-length secret comparisons to
@@ -398,6 +427,7 @@ targets, Miri, and builds without `asm-compare` use the portable Rust fallback.
398427
| Existing ordinary buffer | `unsafe_wipe::volatile_sanitize_*` |
399428
| Generic clear-on-drop wrapper | `Secret<T>` |
400429
| Explicit x86_64 comparison compiler boundary | `asm-compare` feature |
430+
| Explicit x86_64 cache-line eviction after clearing | `cache-flush` feature |
401431

402432
## Relationship to `zeroize`
403433

‎ROADMAP.md‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,19 +207,36 @@ the published crate's dependency posture.
207207

208208
### 3. Architecture-Specific Cache Eviction
209209

210+
Status: implemented for x86_64 behind the `cache-flush` feature.
211+
210212
Priority: optional, target-specific hardening.
211213

212214
After memory is zeroed, old values can still exist transiently in CPU caches.
213215
Some targets provide cache-line flush instructions such as x86/x86_64 `clflush`.
214216

215-
This should only be considered as an explicit feature after the core volatile
216-
wipe path is stable because:
217+
This is available as an explicit feature because:
217218

218219
- instructions and guarantees differ by architecture;
219-
- cache-line size detection matters;
220-
- the operation is not universally available;
221-
- cache eviction does not solve all side channels;
222-
- it can be expensive and surprising as a default.
220+
- cache-line size detection matters: current x86_64 support uses 64-byte
221+
stepping and documents the limit;
222+
- the operation is not universally available: unsupported targets do not expose
223+
the module;
224+
- cache eviction does not solve all side channels: documented;
225+
- it can be expensive and surprising as a default: it remains explicit.
226+
227+
Current implementation:
228+
229+
- `cache-flush` exposes the `cache_flush` module on x86_64 outside Miri.
230+
- Helpers clear with the crate's volatile wipe backend before issuing
231+
`clflush` over covered cache lines and `mfence`.
232+
- `SecretBytes<N>`, `SecretVec`, `SecretString`, and `LockedSecretBytes<N>`
233+
have explicit clear-and-flush methods when the feature is available.
234+
235+
Remaining work:
236+
237+
- evaluate non-x86_64 support separately;
238+
- review cache-line sizing assumptions before stable;
239+
- keep guard-page allocation as a separate design.
223240

224241
### 4. Assembly-Backed Constant-Time Comparison
225242

‎SAFETY.md‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Unsafe code is allowed only inside narrow `src/lib.rs` modules:
1010
Linux targets.
1111
- `compare_asm`, available only with the `asm-compare` feature on x86_64
1212
outside Miri.
13+
- `cache_flush`, available only with the `cache-flush` feature on x86_64
14+
outside Miri.
1315

1416
Public APIs, including `unsafe_wipe`, are safe wrappers around those internal
1517
backends.
@@ -112,10 +114,42 @@ Invariant:
112114
- Length remains public metadata and mismatched lengths return before the
113115
assembly path.
114116

117+
### x86_64 cache-line flush instructions
118+
119+
Location: `cache_flush`
120+
121+
Purpose: provide explicit volatile-clear plus cache-line eviction helpers for
122+
call sites that need x86_64 cache hardening.
123+
124+
Operation:
125+
126+
- Public sanitization helpers first route through the crate's volatile wipe
127+
backend.
128+
- The module aligns the provided address range down to 64-byte cache-line
129+
boundaries.
130+
- The module executes `clflush` for every covered cache line, followed by
131+
`mfence`.
132+
- Unsupported targets, Miri, and builds without `cache-flush` do not expose the
133+
module.
134+
135+
Invariant:
136+
137+
- The pointer and length come from a live slice or owned contiguous allocation.
138+
- The zero-length path does not execute `clflush`.
139+
- `clflush` does not read or write through the Rust pointer; it asks the CPU to
140+
evict the addressed cache line.
141+
- The module assumes 64-byte x86_64 cache-line stepping. This can over-flush
142+
adjacent bytes in the same cache line but does not read or write them through
143+
Rust references.
144+
- `mfence` orders the flush operations before later memory operations.
145+
115146
## Non-Goals
116147

117148
This unsafe boundary intentionally does not implement stack scanning, cache
118-
flushes, SIMD clearing, guard pages, or broad platform memory policy. Memory
119-
locking is explicit, Linux-only, feature-gated, and still does not protect
120-
against crash dumps, hibernation, privileged reads, DMA, or external copies.
149+
flushes for non-x86_64 targets, SIMD clearing, guard pages, or broad platform
150+
memory policy. Memory locking is explicit, Linux-only, feature-gated, and still
151+
does not protect against crash dumps, hibernation, privileged reads, DMA, or
152+
external copies.
121153
Assembly-backed comparison is x86_64-only and does not make length private.
154+
Cache-line eviction is explicit, x86_64-only, and does not prove full CPU-cache
155+
or microarchitectural side-channel secrecy.

‎THREAT_MODEL.md‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Rust applications.
1717
`memory-lock` feature is enabled on supported architectures.
1818
- Optional x86_64 assembly-backed equal-length comparison when the
1919
`asm-compare` feature is enabled.
20+
- Optional x86_64 volatile-clear plus cache-line eviction when the
21+
`cache-flush` feature is enabled.
2022

2123
## Out of Scope
2224

@@ -26,12 +28,13 @@ Rust applications.
2628
stored inside `LockedSecretBytes<N>`.
2729
- Preventing disclosure through a debugger, `/proc/<pid>/mem`, ptrace, kernel
2830
compromise, DMA, malicious firmware, or privileged co-tenants.
29-
- Soundly scrubbing old stack frames, prior Rust move copies, CPU registers, CPU
30-
caches, SIMD registers, allocator metadata, or third-party library copies.
31+
- Soundly scrubbing old stack frames, prior Rust move copies, CPU registers,
32+
unrelated CPU cache lines, SIMD registers, allocator metadata, or third-party
33+
library copies.
3134
- Clearing temporary stack copies after process abort. Closure helpers clear
3235
their temporaries on normal return and unwinding paths only; `panic = "abort"`
3336
and other abort paths skip destructors and post-closure cleanup.
34-
- Guard pages and cache-line flushing.
37+
- Guard pages and non-x86_64 cache-line flushing.
3538

3639
## Design Position
3740

@@ -57,6 +60,12 @@ inline-assembly loop. This gives the comparison body a stronger compiler
5760
boundary, but it does not hide length metadata and does not claim protection
5861
against all microarchitectural side channels.
5962

63+
With the `cache-flush` feature on x86_64, explicit clear-and-flush helpers
64+
volatile-clear the target storage and then execute `clflush` over the covered
65+
cache lines. This can evict the addressed lines from CPU caches, but it does not
66+
prove all historical copies are gone and does not solve general
67+
microarchitectural side channels.
68+
6069
`SecretBytes::expose_secret_volatile` makes the volatile temporary-copy cleanup
6170
explicit at the call site. It clears on normal return and unwinding paths, but
6271
it is still not a solution for aborting processes.

0 commit comments

Comments
 (0)