GHOST Protocol sheetSheet Note N2 · Sheet 01 TR / EN

Note N2 · GHOST Protocol

SECRETS IN MEMORY

Keeping keys from reaching disk in Rust: mlock, MADV_DONTDUMP, core dumps and zeroize

Sheet
GHOST PROTOCOL
Topic
Rust · Linux · Android
Source
security/ephemeral.rs
Date
2026-11-12
Reading
2 min

Short answer: in an encrypted messaging app, keys and plaintext inevitably sit in RAM; the goal is that they don't leak from there to disk or another process. Lock the pages (mlockall) so they aren't written to swap, exclude key regions from core dumps (MADV_DONTDUMP), turn core dumps off entirely (RLIMIT_CORE = 0), and wipe memory with zeroize when you're done. Know which of these actually works on which platform.

01Threat

GHOST never writes messages to disk; it keeps them only in memory. But the operating system can write memory to disk on its own:

  • Swap: under memory pressure pages move to disk; they can stay in swap even after the app closes.
  • Core dump: if the app crashes, its whole memory can be written to a file.
  • Freed memory: when Rust drops a value it doesn't zero the memory; the content stays there until the next allocation.

02Locking pages

pub fn lock_memory() -> Result<()> {
    unsafe {
        let result = libc::mlockall(libc::MCL_CURRENT | libc::MCL_FUTURE);
        if result != 0 {
            // Permission errors are common on non-root Android; carry on silently
        }
    }
    Ok(())
}

MCL_CURRENT | MCL_FUTURE locks every page, current and future. The regions holding key shares are also mlocked one by one and, on Linux, excluded from core dumps with madvise(MADV_DONTDUMP).

03Turning core dumps off

let rlim = libc::rlimit { rlim_cur: 0, rlim_max: 0 };
libc::setrlimit(libc::RLIMIT_CORE, &rlim);

Both the soft and the hard limit are zeroed; the process can't raise the limit later itself. No memory dump is produced on a crash.

04Wiping

Every intermediate buffer carrying a secret is zeroed with zeroize as soon as it is done. zeroize stops the compiler from optimising the zeroing away on the grounds that "this memory is never read again"; a plain fill(0) doesn't give that guarantee. Temporary files the app used are overwritten in several passes before deletion when it shuts down.

05Limits

These measures don't have the same strength everywhere; to be honest:

  • Android: on a non-root device mlockall mostly fails with a permission error, and the code carries on. So swap protection isn't guaranteed on Android.
  • Windows: as the counterpart of mlockall, the process working set is enlarged; that reduces paging but doesn't prevent it.
  • Encryption in memory: sensitive buffers are also kept encrypted in memory and periodically re-encrypted with new keys. But the keys live in the same process's memory; this only makes ordinary memory scans and dumps harder. It is not protection against an attacker who can read the process's memory.

Knowing these limits sets the promise made to users: "messages are never written to disk" is true; "memory can't be read even if the device is compromised" is not.

→More notes from this project

—End of notewritten by MUHAMMEDPYZ