FAQ - Help and questions

Can I wipe a password or key from memory when I am finished with it?

#security#zeroize#secretspublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

Yes, as a best-effort measure. The zeroize crate overwrites a buffer in a way the optimizer may not remove, and Zeroizing wraps a value so it is wiped when dropped. It shortens the time a secret sits in memory but cannot reach copies made elsewhere, such as swap, crash dumps or earlier reallocations.

Checked with rustc 1.93.0, zeroize 1.8.1 (feature derive), Windows 11

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

Answer#

When a String or Vec<u8> is dropped, its memory is freed but not cleared, so the bytes may stay readable in the process until something else overwrites them. The zeroize crate gives you a write the compiler is not allowed to optimize away, and wrappers that call it for you.

Cargo.tomltoml
[dependencies]
zeroize = { version = "1.8", features = ["derive"] }
src/main.rsrust
use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};

#[derive(Zeroize, ZeroizeOnDrop)]
struct SessionKey {
    bytes: Vec<u8>,
}

fn main() {
    // Zeroizing<T> wipes the value when it goes out of scope.
    {
        let password = Zeroizing::new(String::from("hunter2"));
        println!("inside the scope: {} bytes", password.len());
    }

    // An explicit wipe: the buffer is cleared in a way the optimizer may not remove.
    let mut buf = vec![1u8, 2, 3, 4];
    buf.zeroize();
    println!("after zeroize: {buf:?}");

    // A struct that wipes its field on drop.
    let key = SessionKey { bytes: vec![9; 16] };
    println!("key holds {} bytes", key.bytes.len());
    drop(key);
}
text
inside the scope: 7 bytes
after zeroize: []
key holds 16 bytes

Zeroizing<T> wipes its content at the end of the scope. #[derive(Zeroize, ZeroizeOnDrop)] does the same for your own struct. Calling .zeroize() on a Vec<u8> wipes the bytes and empties the vector.

What it does not do#

Be honest about the limits, because they decide whether this is worth the effort:

  • A Vec or String that grew may have left an older copy in memory it moved away from. Allocate the full size up front (Vec::with_capacity) for buffers that hold secrets.
  • Copies you made on the way (a clone, a formatted message, a request body) are not wiped.
  • The operating system may have written the page to swap or to a crash dump.
  • Anyone who can already read your process memory can usually read the secret while it is in use.

So treat it as defence in depth for long-running processes, not as a guarantee.

Common mistakes#

  • Believing drop(secret) clears memory. It only frees it.
  • Wiping a clone and keeping the original around.
  • Using it as a reason to skip the basics: keep secrets out of logs and source first.

Does the secrecy crate do the same thing?#

It combines a wrapper that hides the value in Debug output with wiping on drop. See the thread on keeping secrets out of logs for the printing side.

Is it worth doing for every string?#

No. Use it for long-lived secrets such as keys, tokens and passwords held in memory for a while, and where your threat model includes memory disclosure. For everything else, the basics (no secrets in logs or source) matter more.

Sources#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet