Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat): Add memfd secret based allocation #16

Merged
merged 16 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ version = "0.6.3"
authors = ["quininer kel <[email protected]>"]
description = "Rust implementation `libsodium/utils`."
repository = "https://github.com/quininer/memsec"
keywords = [ "protection", "memory", "secure" ]
keywords = ["protection", "memory", "secure"]
documentation = "https://docs.rs/memsec/"
license = "MIT"
categories = [ "no-std", "memory-management" ]
categories = ["no-std", "memory-management"]
edition = "2018"

[badges]
Expand All @@ -22,14 +22,15 @@ libc = { version = "0.2", optional = true }

[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.45", default-features = false, features = [
"Win32_System_SystemInformation",
"Win32_System_Memory",
"Win32_Foundation",
"Win32_System_Diagnostics_Debug"
"Win32_System_SystemInformation",
"Win32_System_Memory",
"Win32_Foundation",
"Win32_System_Diagnostics_Debug",
], optional = true }

[features]
default = [ "use_os", "alloc" ]
default = ["use_os", "alloc"]
nightly = []
use_os = [ "libc", "windows-sys" ]
alloc = [ "getrandom", "use_os" ]
use_os = ["libc", "windows-sys"]
alloc = ["getrandom", "use_os"]
alloc_ext = ["alloc"]
6 changes: 5 additions & 1 deletion memsec-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ default-features = false
[dev-dependencies]
libc = "0.2"
quickcheck = "1"
procspawn = {version = "1.0.0", features = ["test-support"]}

[target.'cfg(unix)'.dev-dependencies]
libsodium-sys = { version = "0.2" }
nix = "0.26"
ipc-channel = "0.18.0"
serde = "1.0.203"

[features]
default = [ "alloc", "use_os" ]
default = [ "alloc", "use_os", "alloc_ext"]
nightly = [ "memsec/nightly" ]
use_os = [ "memsec/use_os" ]
alloc = [ "memsec/alloc" ]
alloc_ext = [ "memsec/alloc_ext", "use_os" ]
10 changes: 9 additions & 1 deletion memsec-test/benches/malloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ extern crate test;
use std::ptr::NonNull;
use test::Bencher;


#[bench]
fn memsec_malloc(b: &mut Bencher) {
b.iter(|| unsafe {
Expand All @@ -15,6 +14,15 @@ fn memsec_malloc(b: &mut Bencher) {
});
}

#[cfg(unix)]
#[bench]
fn memsec_memfd_secret(b: &mut Bencher) {
b.iter(|| unsafe {
let ptr: NonNull<[u8; 512]> = memsec::memfd_secret().unwrap();
memsec::free_memfd_secret(ptr);
});
}

#[cfg(unix)]
#[bench]
fn libsodium_malloc(b: &mut Bencher) {
Expand Down
57 changes: 36 additions & 21 deletions memsec-test/benches/memcmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,40 @@

extern crate test;

use test::Bencher;
use std::mem::size_of_val;
use libc::c_void;

use std::mem::size_of_val;
use test::Bencher;

#[bench]
fn memsec_memeq_eq_bench(b: &mut Bencher) {
let x: [u8; 1025] = [9; 1025];
let y: [u8; 1025] = [9; 1025];

b.iter(|| unsafe {
memsec::memeq(x.as_ptr(), y.as_ptr(), size_of_val(&y))
});
b.iter(|| unsafe { memsec::memeq(x.as_ptr(), y.as_ptr(), size_of_val(&y)) });
}

#[bench]
fn memsec_memeq_nq_bench(b: &mut Bencher) {
let x: [u8; 1025] = [8; 1025];
let z: [u8; 1025] = [3; 1025];

b.iter(|| unsafe {
memsec::memeq(x.as_ptr(), z.as_ptr(), size_of_val(&z))
});
b.iter(|| unsafe { memsec::memeq(x.as_ptr(), z.as_ptr(), size_of_val(&z)) });
}

#[bench]
fn memsec_memcmp_eq_bench(b: &mut Bencher) {
let x: [u8; 1025] = [9; 1025];
let y: [u8; 1025] = [9; 1025];

b.iter(|| unsafe {
memsec::memcmp(x.as_ptr(), y.as_ptr(), size_of_val(&y))
});
b.iter(|| unsafe { memsec::memcmp(x.as_ptr(), y.as_ptr(), size_of_val(&y)) });
}

#[bench]
fn memsec_memcmp_nq_bench(b: &mut Bencher) {
let x: [u8; 1025] = [8; 1025];
let z: [u8; 1025] = [3; 1025];

b.iter(|| unsafe {
memsec::memcmp(x.as_ptr(), z.as_ptr(), size_of_val(&z))
});
b.iter(|| unsafe { memsec::memcmp(x.as_ptr(), z.as_ptr(), size_of_val(&z)) });
}

#[cfg(unix)]
Expand All @@ -54,7 +45,11 @@ fn libsodium_memcmp_eq_bench(b: &mut Bencher) {
let y: [u8; 1025] = [9; 1025];

b.iter(|| unsafe {
libsodium_sys::sodium_memcmp(x.as_ptr() as *const _, y.as_ptr() as *const _, size_of_val(&y))
libsodium_sys::sodium_memcmp(
x.as_ptr() as *const _,
y.as_ptr() as *const _,
size_of_val(&y),
)
});
}

Expand All @@ -65,7 +60,11 @@ fn libsodium_memcmp_nq_bench(b: &mut Bencher) {
let z: [u8; 1025] = [3; 1025];

b.iter(|| unsafe {
libsodium_sys::sodium_memcmp(x.as_ptr() as *const _, z.as_ptr() as *const _, size_of_val(&z))
libsodium_sys::sodium_memcmp(
x.as_ptr() as *const _,
z.as_ptr() as *const _,
size_of_val(&z),
)
});
}

Expand All @@ -76,7 +75,11 @@ fn libsodium_compare_nq_bench(b: &mut Bencher) {
let z: [u8; 1025] = [3; 1025];

b.iter(|| unsafe {
libsodium_sys::sodium_compare(x.as_ptr() as *const _, z.as_ptr() as *const _, size_of_val(&z))
libsodium_sys::sodium_compare(
x.as_ptr() as *const _,
z.as_ptr() as *const _,
size_of_val(&z),
)
});
}

Expand All @@ -87,7 +90,11 @@ fn libsodium_compare_eq_bench(b: &mut Bencher) {
let y: [u8; 1025] = [9; 1025];

b.iter(|| unsafe {
libsodium_sys::sodium_compare(x.as_ptr() as *const _, y.as_ptr() as *const _, size_of_val(&y))
libsodium_sys::sodium_compare(
x.as_ptr() as *const _,
y.as_ptr() as *const _,
size_of_val(&y),
)
});
}

Expand All @@ -97,7 +104,11 @@ fn libc_memcmp_eq_bench(b: &mut Bencher) {
let y: [u8; 1025] = [9; 1025];

b.iter(|| unsafe {
libc::memcmp(x.as_ptr() as *const c_void, y.as_ptr() as *const c_void, size_of_val(&y))
libc::memcmp(
x.as_ptr() as *const c_void,
y.as_ptr() as *const c_void,
size_of_val(&y),
)
});
}

Expand All @@ -107,6 +118,10 @@ fn libc_memcmp_nq_bench(b: &mut Bencher) {
let z: [u8; 1025] = [3; 1025];

b.iter(|| unsafe {
libc::memcmp(x.as_ptr() as *const c_void, z.as_ptr() as *const c_void, size_of_val(&z))
libc::memcmp(
x.as_ptr() as *const c_void,
z.as_ptr() as *const c_void,
size_of_val(&z),
)
});
}
10 changes: 3 additions & 7 deletions memsec-test/benches/memzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

extern crate test;

use test::Bencher;
use std::mem::size_of_val;
use test::Bencher;

#[bench]
fn ptr_write_zeroed_bench(b: &mut Bencher) {
Expand All @@ -19,17 +19,13 @@ fn ptr_write_zeroed_bench(b: &mut Bencher) {
fn memsec_memzero_bench(b: &mut Bencher) {
let mut x: [u8; 1025] = [0; 1025];

b.iter(|| unsafe {
memsec::memzero(x.as_mut_ptr(), size_of_val(&x))
});
b.iter(|| unsafe { memsec::memzero(x.as_mut_ptr(), size_of_val(&x)) });
}

#[cfg(unix)]
#[bench]
fn libsodium_memzero_bench(b: &mut Bencher) {
let mut x: [u8; 1025] = [0; 1025];

b.iter(|| unsafe {
libsodium_sys::sodium_memzero(x.as_mut_ptr() as *mut _, size_of_val(&x))
});
b.iter(|| unsafe { libsodium_sys::sodium_memzero(x.as_mut_ptr() as *mut _, size_of_val(&x)) });
}
Loading
Loading