diff --git a/hbvm/fuzz/fuzz_targets/vm.rs b/hbvm/fuzz/fuzz_targets/vm.rs index 03b3455..2c368db 100644 --- a/hbvm/fuzz/fuzz_targets/vm.rs +++ b/hbvm/fuzz/fuzz_targets/vm.rs @@ -16,7 +16,7 @@ fuzz_target!(|data: &[u8]| { if validate(data).is_ok() { let mut vm = unsafe { Vm::<_, 16384>::new( - SoftPagedMem { + SoftPagedMem::<_, true> { pf_handler: TestTrapHandler, program: data, root_pt: Box::into_raw(Default::default()), diff --git a/hbvm/src/main.rs b/hbvm/src/main.rs index 80814c6..7bb4502 100644 --- a/hbvm/src/main.rs +++ b/hbvm/src/main.rs @@ -17,7 +17,7 @@ fn main() -> Result<(), Box> { } else { unsafe { let mut vm = Vm::<_, 0>::new( - SoftPagedMem { + SoftPagedMem::<_, true> { pf_handler: TestTrapHandler, program: &prog, root_pt: Box::into_raw(Default::default()), diff --git a/hbvm/src/mem/softpaging/mapping.rs b/hbvm/src/mem/softpaging/mapping.rs index 0d6858e..8f406a4 100644 --- a/hbvm/src/mem/softpaging/mapping.rs +++ b/hbvm/src/mem/softpaging/mapping.rs @@ -10,7 +10,7 @@ use { derive_more::Display, }; -impl<'p, A> SoftPagedMem<'p, A> { +impl<'p, A, const OUT_PROG_EXEC: bool> SoftPagedMem<'p, A, OUT_PROG_EXEC> { /// Maps host's memory into VM's memory /// /// # Safety diff --git a/hbvm/src/mem/softpaging/mod.rs b/hbvm/src/mem/softpaging/mod.rs index 1acf984..11fa465 100644 --- a/hbvm/src/mem/softpaging/mod.rs +++ b/hbvm/src/mem/softpaging/mod.rs @@ -18,8 +18,12 @@ use { }; /// HoleyBytes software paged memory +/// +/// - `OUT_PROG_EXEC`: set to `false` to disable executing program +/// not contained in initially provided program, even the pages +/// are executable #[derive(Clone, Debug)] -pub struct SoftPagedMem<'p, PfH> { +pub struct SoftPagedMem<'p, PfH, const OUT_PROG_EXEC: bool = true> { /// Root page table pub root_pt: *mut PageTable, /// Page fault handler @@ -30,7 +34,9 @@ pub struct SoftPagedMem<'p, PfH> { pub icache: ICache, } -impl<'p, PfH: HandlePageFault> Memory for SoftPagedMem<'p, PfH> { +impl<'p, PfH: HandlePageFault, const OUT_PROG_EXEC: bool> Memory + for SoftPagedMem<'p, PfH, OUT_PROG_EXEC> +{ /// Load value from an address /// /// # Safety @@ -70,7 +76,7 @@ impl<'p, PfH: HandlePageFault> Memory for SoftPagedMem<'p, PfH> { #[inline(always)] unsafe fn prog_read(&mut self, addr: u64) -> Option { - if addr as usize > self.program.len() { + if OUT_PROG_EXEC && addr as usize > self.program.len() { return self.icache.fetch::(addr, self.root_pt); } @@ -82,7 +88,7 @@ impl<'p, PfH: HandlePageFault> Memory for SoftPagedMem<'p, PfH> { #[inline(always)] unsafe fn prog_read_unchecked(&mut self, addr: u64) -> T { - if addr as usize > self.program.len() { + if OUT_PROG_EXEC && addr as usize > self.program.len() { return self .icache .fetch::(addr as _, self.root_pt) @@ -93,7 +99,7 @@ impl<'p, PfH: HandlePageFault> Memory for SoftPagedMem<'p, PfH> { } } -impl<'p, PfH: HandlePageFault> SoftPagedMem<'p, PfH> { +impl<'p, PfH: HandlePageFault, const OUT_PROG_EXEC: bool> SoftPagedMem<'p, PfH, OUT_PROG_EXEC> { // Everyone behold, the holy function, the god of HBVM memory accesses! /// Split address to pages, check their permissions and feed pointers with offset