Skip to content

Commit 8bb444f

Browse files
committed
td-payload: decrypt private memory during allocation
Decrypt the private memory at initialization will leak the data of the linked list allocator. The solution can be moving decrytion to the moment the shared pages are allocated and encypting the shared memory before they are freed. Signed-off-by: Jiaqi Gao <[email protected]>
1 parent 9b4e454 commit 8bb444f

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

td-payload/src/mm/shared.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ use core::{alloc::Layout, ptr::NonNull};
66
use linked_list_allocator::LockedHeap;
77

88
use super::SIZE_4K;
9-
use crate::arch::shared::decrypt;
9+
use crate::arch::shared::{decrypt, encrypt};
1010

1111
static SHARED_MEMORY_ALLOCATOR: LockedHeap = LockedHeap::empty();
1212

1313
pub fn init_shared_memory(start: u64, size: usize) {
14-
// Set the shared memory region to be shared
15-
decrypt(start, size);
1614
// Initialize the shared memory allocator
1715
unsafe {
1816
SHARED_MEMORY_ALLOCATOR.lock().init(start as *mut u8, size);
@@ -45,6 +43,8 @@ impl SharedMemory {
4543

4644
impl Drop for SharedMemory {
4745
fn drop(&mut self) {
46+
// Set the shared memory region to be private before it is freed
47+
encrypt(self.addr as u64, self.size);
4848
unsafe { free_shared_pages(self.addr, self.size / SIZE_4K) }
4949
}
5050
}
@@ -62,6 +62,9 @@ pub unsafe fn alloc_shared_pages(num: usize) -> Option<usize> {
6262

6363
core::slice::from_raw_parts_mut(addr as *mut u8, size).fill(0);
6464

65+
// Set the shared memory region to be shared
66+
decrypt(addr as u64, size);
67+
6568
Some(addr)
6669
}
6770

0 commit comments

Comments
 (0)