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

Conversation

prabhpreet
Copy link
Contributor

@prabhpreet prabhpreet commented May 23, 2024

Credits to @koraa for the implementation idea

Adds functions to allocate and free memfd_secret based allocations on Linux only using memfd_secret, memfd_secret_sized and free_memfd_secret methods in alloc module, adding guard pages and canary padding.

Addresses #13 partly by adding functions to allocate using this method.

Plan to use for rosenpass/rosenpass#320

Starting this as a draft PR- please do suggest any comments/improvements. I'll be adding tests soon as well.

@prabhpreet prabhpreet changed the title (feat): Add memfd secret style allocation (feat): Add memfd secret based allocation May 23, 2024
src/alloc.rs Outdated Show resolved Hide resolved
@quininer
Copy link
Owner

This looks good to me, maybe should be put into a separate file and some tests need to be added.

memsec-test/Cargo.toml Outdated Show resolved Hide resolved
src/alloc/allocext.rs Outdated Show resolved Hide resolved
@prabhpreet prabhpreet marked this pull request as ready for review May 30, 2024 10:19
@prabhpreet
Copy link
Contributor Author

@quininer : Thanks for your review comments. I think this is ready for review now.

I have also added extra tests as a sanity check to attempt to write in guard pages, and canary area to see if allocation fails at right stages.

@prabhpreet prabhpreet requested a review from quininer May 30, 2024 10:43
}

/// Attempts to
#[cfg(unix)]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the mod already has cfg os=linux, the cfg here seems redundant

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

// free
let total_size = PAGE_SIZE + PAGE_SIZE + unprotected_size + PAGE_SIZE;
_mprotect(base_ptr, total_size, Prot::ReadWrite);

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be a memzero here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, added it with crate::memzero

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added it for entire memory length, starting with base_ptr

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

let total_size = PAGE_SIZE + PAGE_SIZE + unprotected_size + PAGE_SIZE;
let (base_ptr, fd) = alloc_memfd_secret(total_size)?;
let base_ptr = base_ptr.as_ptr();
let fd_ptr = base_ptr.add(size_of::<usize>());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should add an assert size + fd <= page

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, made the change on line 43. Please let me know if that wasn't what you had in mind.

@prabhpreet prabhpreet requested a review from quininer June 4, 2024 07:59
@@ -83,10 +83,12 @@ fn malloc_mprotect_1_test() {
}
}

procspawn::enable_test_support!();
#[cfg(unix)]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can put it into a separate file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@prabhpreet
Copy link
Contributor Author

@quininer Looks like the checks have passed. When you think it is ready, I request you to please squash the commit changes before merging.

@quininer quininer merged commit 3ee1311 into quininer:master Jun 6, 2024
3 checks passed
@quininer
Copy link
Owner

quininer commented Jun 6, 2024

Thank you!

prabhpreet added a commit to rosenpass/rosenpass that referenced this pull request Jun 10, 2024
Implements:
- An additional allocator to use memfd_secret(2) and guard pages using mmap(2), implemented in quininer/memsec#16
- An allocator that abstracts away underlying allocators, and uses specified allocator set by rosenpass_secret_memory::policy functions (or a function that sets rosenpass_secret_memory::alloc::ALLOC_INIT
- Updates to tests- integration, fuzz, bench: some tests use procspawn to spawn multiple processes with different allocator policies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants