-
Notifications
You must be signed in to change notification settings - Fork 12
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
Security-harden the guest's memory allocator #726
base: master
Are you sure you want to change the base?
Conversation
Extracted from #631
@@ -4,26 +4,34 @@ | |||
use core::alloc::{GlobalAlloc, Layout}; | |||
|
|||
struct SimpleAllocator { | |||
next_alloc: usize, | |||
next_alloc: *mut u8, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This type is more truthful, and simplifies our code.
} | ||
core::hint::assert_unchecked(align.is_power_of_two()); | ||
core::hint::assert_unchecked(align != 0); | ||
heap_pos = heap_pos.add(heap_pos.align_offset(align)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use the proper Rust functions for working with pointers now. That makes both the type checker happier (when working with pointers instead of usize
), and hopefully also clues in the optimiser a bit more.
But the latter would just be a welcome side effect, if it does happen.
next_alloc: 0xFFFF_FFFF, | ||
}; | ||
|
||
pub unsafe fn init_heap() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We no longer need init_heap
, because we can directly statically initialise to &raw mut _sheap,
.
// (We could also return a null pointer, but only malicious programs would ever hit this.) | ||
heap_pos = heap_pos.strict_add(layout.size()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alas, ptr
type doesn't have strict_add
, but add
does nearly the same.
Here we harden the security of the guest's memory allocator: we no longer rely on manual initialisation code for its proper operation.
Because we remove code, it also simplifies the allocator. Hopefully making it easier to understand.
Extracted from #631