feat(core): zeroize the profile/loc signing copy and the TUI passphrase buffer
All checks were successful
ci / test (pull_request) Successful in 4m14s
All checks were successful
ci / test (pull_request) Successful in 4m14s
The Ed25519 identity secret was still copied into a bare [u8; 32] on the stack in two signers (profile::sign, loc::sign), and the TUI kept the typed backup/restore passphrase in a plain String for the life of the screen. Both now go through Zeroizing: - profile::signing_key_bytes() builds the 32-byte copy into a Zeroizing<[u8; 32]> (same pattern as pay::sign_bytes / qr::encode, but with copy_from_slice so no Copy temporary is left behind); loc::sign shares the helper instead of making its own copy. - Screen::Passphrase now owns a PassphraseEntry whose buffers are Zeroizing<String>. Every exit path (Enter, mismatch, Esc) calls close(), which wipes both buffers eagerly; a drop — including Ctrl-Q and a panic — is the backstop. Evidence: tests/zeroize.rs gains a drop probe (ManuallyDrop + drop_in_place + read_volatile) on the Zeroizing signing buffer and a length-rejection test; src/tui.rs gains unit tests asserting the buffer types and that close() leaves every byte of the passphrase's heap capacity zeroed.
This commit is contained in:
parent
c39259b919
commit
e3be8ffdd0
4 changed files with 215 additions and 30 deletions
177
src/tui.rs
177
src/tui.rs
|
|
@ -8,6 +8,7 @@ use ratatui::style::{Color, Modifier, Style};
|
|||
use ratatui::symbols::border;
|
||||
use ratatui::widgets::{Block, Borders, Clear, List, ListItem, ListState, Paragraph, Wrap};
|
||||
use ratatui::{DefaultTerminal, Frame};
|
||||
use zeroize::{Zeroize, Zeroizing};
|
||||
|
||||
use crate::node::Node;
|
||||
use crate::qr::{self, QrPayload};
|
||||
|
|
@ -761,6 +762,78 @@ impl BackupPrompt {
|
|||
}
|
||||
}
|
||||
|
||||
/// The in-progress passphrase on the backup/restore screen: the hidden buffer
|
||||
/// plus the first entry, kept across the second prompt of the backup flow.
|
||||
///
|
||||
/// Both are `Zeroizing<String>`, so the passphrase does not sit in a plain
|
||||
/// heap `String` for the life of the screen; `close()` wipes eagerly on every
|
||||
/// exit path (submit, mismatch, cancel) instead of waiting for the screen
|
||||
/// value to be dropped, and the wrapper covers the paths that cannot call it
|
||||
/// (Ctrl-Q, a panic).
|
||||
pub struct PassphraseEntry {
|
||||
first: Option<Zeroizing<String>>,
|
||||
buf: Zeroizing<String>,
|
||||
}
|
||||
|
||||
impl Default for PassphraseEntry {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
first: None,
|
||||
buf: Zeroizing::new(String::new()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PassphraseEntry {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
/// Second prompt of the backup flow: the first passphrase was already typed.
|
||||
pub fn with_first(first: &str) -> Self {
|
||||
Self {
|
||||
first: Some(Zeroizing::new(first.to_string())),
|
||||
..Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push(&mut self, c: char) {
|
||||
self.buf.push(c);
|
||||
}
|
||||
|
||||
pub fn backspace(&mut self) {
|
||||
self.buf.pop();
|
||||
}
|
||||
|
||||
/// What the screen echoes back — the passphrase itself is not drawn.
|
||||
pub fn buf(&self) -> &str {
|
||||
self.buf.as_str()
|
||||
}
|
||||
|
||||
pub fn first(&self) -> Option<&str> {
|
||||
self.first.as_deref().map(String::as_str)
|
||||
}
|
||||
|
||||
/// Take the typed passphrase for submission, leaving this entry empty.
|
||||
pub fn take_buf(&mut self) -> Zeroizing<String> {
|
||||
std::mem::replace(&mut self.buf, Zeroizing::new(String::new()))
|
||||
}
|
||||
|
||||
/// Take the first-passphrase copy for the confirm comparison.
|
||||
pub fn take_first(&mut self) -> Option<Zeroizing<String>> {
|
||||
self.first.take()
|
||||
}
|
||||
|
||||
/// Wipe both buffers: the screen is done with them.
|
||||
pub fn close(&mut self) {
|
||||
self.buf.zeroize();
|
||||
if let Some(first) = self.first.as_mut() {
|
||||
first.zeroize();
|
||||
}
|
||||
self.first = None;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn backup_screen_text() -> &'static str {
|
||||
"Write encrypted identity backup?\n\
|
||||
This file is your identity secret. Treat it like the database.\n\
|
||||
|
|
@ -840,8 +913,7 @@ enum Screen {
|
|||
Passphrase {
|
||||
kind: BackupKind,
|
||||
path: String,
|
||||
first: Option<String>,
|
||||
buf: String,
|
||||
entry: PassphraseEntry,
|
||||
},
|
||||
Quit {
|
||||
prompt: QuitPrompt,
|
||||
|
|
@ -929,6 +1001,7 @@ impl App {
|
|||
let mut tip_cmd = None;
|
||||
let mut file_cmd = None;
|
||||
let mut chat_send = None;
|
||||
let mut pass_submit = None;
|
||||
match &mut self.screen {
|
||||
Screen::Main => match key.code {
|
||||
KeyCode::Tab => self.focus = self.focus.next(),
|
||||
|
|
@ -1090,8 +1163,7 @@ impl App {
|
|||
self.screen = Screen::Passphrase {
|
||||
kind,
|
||||
path: path.clone(),
|
||||
first: None,
|
||||
buf: String::new(),
|
||||
entry: PassphraseEntry::new(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -1101,25 +1173,17 @@ impl App {
|
|||
_ => {}
|
||||
}
|
||||
}
|
||||
Screen::Passphrase {
|
||||
kind,
|
||||
path,
|
||||
first,
|
||||
buf,
|
||||
} => match key.code {
|
||||
KeyCode::Esc => self.screen = Screen::Main,
|
||||
KeyCode::Backspace => {
|
||||
buf.pop();
|
||||
Screen::Passphrase { kind, path, entry } => match key.code {
|
||||
KeyCode::Esc => {
|
||||
entry.close();
|
||||
self.screen = Screen::Main;
|
||||
}
|
||||
KeyCode::Backspace => entry.backspace(),
|
||||
KeyCode::Enter => {
|
||||
let kind = *kind;
|
||||
let path = path.clone();
|
||||
let first = first.clone();
|
||||
let pass = buf.clone();
|
||||
self.submit_passphrase(kind, &path, first.as_deref(), &pass)?;
|
||||
pass_submit = Some((*kind, path.clone(), entry.take_first(), entry.take_buf()));
|
||||
}
|
||||
KeyCode::Char(c) if !key.modifiers.contains(KeyModifiers::CONTROL) => {
|
||||
buf.push(c);
|
||||
entry.push(c);
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
|
|
@ -1166,6 +1230,9 @@ impl App {
|
|||
if let Some(text) = chat_send {
|
||||
self.send_chat(&text)?;
|
||||
}
|
||||
if let Some((kind, path, first, pass)) = pass_submit {
|
||||
self.submit_passphrase(kind, &path, first.as_deref().map(String::as_str), &pass)?;
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
|
|
@ -1279,8 +1346,7 @@ impl App {
|
|||
self.screen = Screen::Passphrase {
|
||||
kind,
|
||||
path: path.to_string(),
|
||||
first: Some(pass.to_string()),
|
||||
buf: String::new(),
|
||||
entry: PassphraseEntry::with_first(pass),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -1487,9 +1553,9 @@ impl App {
|
|||
Screen::Wipe { kind, prompt } => draw_wipe(f, *kind, prompt.typed()),
|
||||
Screen::Profile { editor } => draw_profile(f, editor),
|
||||
Screen::ConfirmKeys { kind, prompt, .. } => draw_confirm_keys(f, *kind, prompt.typed()),
|
||||
Screen::Passphrase {
|
||||
kind, first, buf, ..
|
||||
} => draw_passphrase(f, *kind, first.is_some(), buf),
|
||||
Screen::Passphrase { kind, entry, .. } => {
|
||||
draw_passphrase(f, *kind, entry.first().is_some(), entry.buf())
|
||||
}
|
||||
Screen::Quit { prompt } => draw_quit(f, prompt.typed()),
|
||||
}
|
||||
if self.help_open {
|
||||
|
|
@ -2017,3 +2083,66 @@ fn to_hex(bytes: &[u8]) -> String {
|
|||
fn io_err(e: io::Error) -> String {
|
||||
e.to_string()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod passphrase_buffer_tests {
|
||||
use super::*;
|
||||
|
||||
fn assert_zeroize_on_drop<T: zeroize::ZeroizeOnDrop>() {}
|
||||
|
||||
/// The typed passphrase lives in `Zeroizing` buffers, not in bare
|
||||
/// `String`s that sit in the heap for the life of the screen.
|
||||
#[test]
|
||||
fn passphrase_buffers_are_zeroizing() {
|
||||
let mut entry = PassphraseEntry::new();
|
||||
entry.push('x');
|
||||
|
||||
let _: &Zeroizing<String> = &entry.buf;
|
||||
let _: &Option<Zeroizing<String>> = &entry.first;
|
||||
assert_zeroize_on_drop::<Zeroizing<String>>();
|
||||
}
|
||||
|
||||
/// Closing the screen (submit, mismatch or cancel) must leave nothing of
|
||||
/// the typed passphrase behind in its heap buffer.
|
||||
#[test]
|
||||
fn closing_the_passphrase_screen_wipes_the_typed_secret() {
|
||||
let mut entry = PassphraseEntry::new();
|
||||
for c in "hunter2-secret".chars() {
|
||||
entry.push(c);
|
||||
}
|
||||
assert_eq!(entry.buf(), "hunter2-secret");
|
||||
|
||||
let ptr = entry.buf.as_ptr();
|
||||
let cap = entry.buf.capacity();
|
||||
assert!(cap >= 14, "expected a heap buffer, capacity {cap}");
|
||||
|
||||
entry.close();
|
||||
|
||||
assert!(entry.buf().is_empty(), "buffer not cleared");
|
||||
// SAFETY: `close()` wipes through `Zeroize`, which zeroes the whole
|
||||
// capacity and *keeps* the allocation (`Vec::zeroize`: zero the
|
||||
// elements, clear the length, zero the spare capacity). `entry` still
|
||||
// owns that buffer, so these `cap` bytes are live memory.
|
||||
let bytes = unsafe { std::slice::from_raw_parts(ptr, cap) };
|
||||
assert!(
|
||||
bytes.iter().all(|b| *b == 0),
|
||||
"typed passphrase survived close(): {bytes:?}"
|
||||
);
|
||||
}
|
||||
|
||||
/// The first entry of the two-step backup flow is a secret too, and the
|
||||
/// screen holds it across the second prompt.
|
||||
#[test]
|
||||
fn closing_the_passphrase_screen_drops_the_first_entry_copy() {
|
||||
let mut entry = PassphraseEntry::with_first("first-typed");
|
||||
assert_eq!(entry.first(), Some("first-typed"));
|
||||
|
||||
entry.close();
|
||||
|
||||
assert_eq!(
|
||||
entry.first(),
|
||||
None,
|
||||
"first passphrase copy survived close()"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue