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

Improve memfd-secret guard page allocation #18

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

prabhpreet
Copy link
Contributor

Improve memfd-secret guard page allocation by using combination of mmap to map allocation area, and nest memfd-secret mapping and meta information with different permissions within the area

Ideated by @koraa

prabhpreet added a commit to rosenpass/rosenpass that referenced this pull request Jun 13, 2024
Improve memfd-secret guard page allocation by using combination of mmap to map allocation area, and nest memfd-secret mapping and meta information with different permissions within the area

Implemented in quininer/memsec#18 

Co-authored-by: Prabhpreet Dua <[email protected]>
Co-authored-by: Karolin Varner <[email protected]>
@@ -21,10 +28,17 @@ mod memfd_secret_alloc {
let _ = libc::ftruncate(fd, size as libc::off_t);

let ptr = libc::mmap(
ptr::null_mut(),
if ptr.is_some() {
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
if ptr.is_some() {
ptr.unwrap_or_else(ptr::null_mut)

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, changed

let total_size = front_guard_size + unprotected_size + back_guard_size;

let base_ptr = libc::mmap(
null_mut(),
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
null_mut(),
ptr::null_mut(),

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, changed

pub unsafe fn alloc_memfd_secret(size: usize) -> Option<(NonNull<u8>, libc::c_int)> {
pub unsafe fn alloc_memfd_secret(
size: usize,
ptr: Option<*mut libc::c_void>,
Copy link
Owner

Choose a reason for hiding this comment

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

It seems that no call is None?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is just for added flexibility, under cases a user directly wants to use alloc_memfd_secret and doesn't want to set a specific address for the mapping

Copy link
Owner

Choose a reason for hiding this comment

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

This is an internal function and no user will call it directly. we don't have to keep Option.

use super::*;
use core::convert::TryInto;

#[inline]
pub unsafe fn alloc_memfd_secret(size: usize) -> Option<(NonNull<u8>, libc::c_int)> {
pub unsafe fn alloc_memfd_secret(
Copy link
Owner

Choose a reason for hiding this comment

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

maybe name change is needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to alloc_memfd_secret_at_ptr

pub unsafe fn alloc_memfd_secret(size: usize) -> Option<(NonNull<u8>, libc::c_int)> {
pub unsafe fn alloc_memfd_secret(
size: usize,
ptr: Option<*mut libc::c_void>,
Copy link
Owner

Choose a reason for hiding this comment

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

This is an internal function and no user will call it directly. we don't have to keep Option.

@prabhpreet
Copy link
Contributor Author

@quininer seems like the Github action is running forever. Is it possible to restart it?

@prabhpreet prabhpreet closed this Jun 14, 2024
@prabhpreet prabhpreet reopened this Jun 14, 2024
@prabhpreet
Copy link
Contributor Author

It looks like procspawn has some issue in the process spawned- I'll be able to attend to this only after next week

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