pkey_mprotect

Crates.iopkey_mprotect
lib.rspkey_mprotect
version0.2.3
sourcesrc
created_at2021-12-16 17:49:51.03767
updated_at2022-06-20 10:12:58.954168
descriptionTyped `pkey_mprotect` wrapper
homepage
repositoryhttps://github.com/Rexagon/pkey_mprotect
max_upload_size
id499292
size24,765
Ivan Kalinin (Rexagon)

documentation

README

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);
    }
}
Commit count: 15

cargo fmt