Skip to main content

SecMem

Struct SecMem 

Source
pub struct SecMem<S: Zeroize> { /* private fields */ }
Available on Unix and crate feature sec_mem only.
Expand description

A hardware-accelerated secure memory container for isolating sensitive data from the OS.

SecMem uses raw Linux syscalls (mmap, mprotect, mseal, mlock) and Intel Memory Protection Keys (MPK/WRPKRU) to physically isolate cryptographic keys or passwords from the rest of the process. It strictly guards against buffer overflows via msealed guard pages, and actively prevents memory dumping using MADV_DONTDUMP, MADV_DONTFORK, and PR_SET_DUMPABLE(0).

When the encryption feature is enabled, data is permanently blinded at rest in RAM using ChaCha20. It utilizes OpenSSH-style Key Shielding, splitting a massive 16 KiB pre-key buffer across kernel memfd_secret mappings and dynamically deriving the true key using blake3. This mathematically neutralizes /proc/self/mem dumping, physical cold-boot attacks, and side-channel exploits like Spectre or Rowhammer.

§Examples

use sec_mem::SecMem;

// Create a highly protected memory vault
let mut secure_key = SecMem::new([0xAAu8; 32]);

// Access is granted exclusively within this closure bounds
secure_key.access_mut(|key| {
    key[0] = 0xBB;
    // Data is processed here...
}); 
// Instantly upon closure exit, memory is zeroized, re-encrypted, and hardware-locked.

Implementations§

Source§

impl<S: Zeroize> SecMem<S>

Source

pub fn new(secret: S) -> Self

Creates a new protected secret

Source

pub fn access<F, R>(&self, f: F) -> R
where F: FnOnce(&S) -> R,

Access secret for reading

Source

pub fn access_mut<F, R>(&mut self, f: F) -> R
where F: FnOnce(&mut S) -> R,

Access secret for mutation

Source

pub fn constant_time_eq(&self, other: &Self) -> Choice
where S: AsRef<[u8]>,

Constant-time equality comparison

Source

pub fn seal_guard_pages(&self)

Explicitly applies Linux memory sealing (mseal) to the guard pages.

WARNING: mseal permanently locks the memory region’s permissions. Because Linux prevents munmap on sealed memory, calling this method means the guard pages (and thus the allocation) will leak and remain in RAM until the process terminates. It should only be used for global, long-lived singletons (like root application keys). Requires Linux 6.10+. Fails silently on older kernels.

Trait Implementations§

Source§

impl<S: Zeroize + Clone> Clone for SecMem<S>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S: Zeroize + Debug> Debug for SecMem<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S: Zeroize + Default> Default for SecMem<S>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<S: Zeroize> Drop for SecMem<S>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<T: Zeroize> SecretAccess<T> for SecMem<T>

Source§

fn access<F, R>(&self, f: F) -> R
where F: FnOnce(&T) -> R,

Access the underlying data for reading
Source§

fn access_mut<F, R>(&mut self, f: F) -> R
where F: FnOnce(&mut T) -> R,

Access the underlying data for mutation
Source§

impl<const N: usize> SecretAccess<[u8]> for SecMem<[u8; N]>

Source§

fn access<F, R>(&self, f: F) -> R
where F: FnOnce(&[u8]) -> R,

Access the underlying data for reading
Source§

fn access_mut<F, R>(&mut self, f: F) -> R
where F: FnOnce(&mut [u8]) -> R,

Access the underlying data for mutation
Source§

impl SecretAccess<[u8]> for SecMem<Vec<u8>>

Source§

fn access<F, R>(&self, f: F) -> R
where F: FnOnce(&[u8]) -> R,

Access the underlying data for reading
Source§

fn access_mut<F, R>(&mut self, f: F) -> R
where F: FnOnce(&mut [u8]) -> R,

Access the underlying data for mutation
Source§

impl<S: Zeroize + Send> Send for SecMem<S>

Source§

impl<S: Zeroize + Sync> Sync for SecMem<S>

Source§

impl<S: Zeroize> Zeroize for SecMem<S>

Source§

fn zeroize(&mut self)

Zero out this object from memory using Rust intrinsics which ensure the zeroization operation is not “optimized away” by the compiler.
Source§

impl<S: Zeroize> ZeroizeOnDrop for SecMem<S>

Auto Trait Implementations§

§

impl<S> !Freeze for SecMem<S>

§

impl<S> RefUnwindSafe for SecMem<S>
where S: RefUnwindSafe,

§

impl<S> Unpin for SecMem<S>

§

impl<S> UnsafeUnpin for SecMem<S>

§

impl<S> UnwindSafe for SecMem<S>
where S: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.