8 releases

0.2.3 Jun 20, 2022
0.2.2 Mar 24, 2022
0.2.0 Feb 25, 2022
0.1.3 Dec 16, 2021

#407 in Memory management

Custom license

14KB
259 lines

pkey_mprotect

Typed pkey_mprotect wrapper.

Only works on Linux on CPUs with Memory Protection Keys support

MSRV: 1.59 (due to const panics and asm! macro)

Example

use pkey_mprotect::*;

struct Keys {
    my_key: [u8; 32],
}

fn main() {
    // Protection keys instance is needed to create regions.
    // NOTE: You probably should always reuse it because there
    // are only 15 available keys in system
    let pkey = ProtectionKeys::new(true).unwrap();

    // Protected region is a thread-safe wrapper around mmaped
    // memory which was secured with `pkey_mprotect`
    let region = pkey.make_region(Keys {
        my_key: [42; 32],
    }).unwrap();

    // To read the data you must lock the region. Access is 
    // granted only for the duration of the guards lifetime
    {
        let region = region.lock();
        println!("{:?}", region.my_key);
    }
}

Dependencies

~0.4–1MB
~21K SLoC