Skip to content
This repository has been archived by the owner on Sep 1, 2024. It is now read-only.

Commit

Permalink
Bug fix due to incorrect parameter for get PT
Browse files Browse the repository at this point in the history
- The `get_page_table_as_mut` function should take the guest large page as a parameter instead of the guest 4KB page as each large page is mapped to a pre-allocated Page Table for hooking.
  • Loading branch information
memN0ps committed Jun 10, 2024
1 parent 140b6d2 commit 107f9b2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
5 changes: 4 additions & 1 deletion hypervisor/src/intel/hooks/hook_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,13 @@ impl HookManager {
let guest_page_pa = guest_function_pa.align_down_to_base_page();
debug!("Guest page PA: {:#x}", guest_page_pa.as_u64());

let guest_large_page_pa = guest_function_pa.align_down_to_large_page();
debug!("Guest large page PA: {:#x}", guest_large_page_pa.as_u64());

let pre_alloc_pt = vm
.hook_manager
.memory_manager
.get_page_table_as_mut(guest_page_pa.as_u64())
.get_page_table_as_mut(guest_large_page_pa.as_u64())
.ok_or(HypervisorError::PageTableNotFound)?;

// Swap the page back and restore the original page permissions
Expand Down
10 changes: 8 additions & 2 deletions hypervisor/src/intel/vmexit/ept.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ pub fn handle_ept_violation(vm: &mut Vm) -> Result<ExitType, HypervisorError> {
let guest_page_pa = PAddr::from(guest_pa).align_down_to_base_page();
trace!("Faulting Guest Page PA: {:#x}", guest_page_pa);

let guest_large_page_pa = guest_page_pa.align_down_to_large_page();
trace!("Faulting Guest Large Page PA: {:#x}", guest_large_page_pa);

let shadow_page_pa = PAddr::from(
vm.hook_manager
.memory_manager
Expand All @@ -39,7 +42,7 @@ pub fn handle_ept_violation(vm: &mut Vm) -> Result<ExitType, HypervisorError> {
let pre_alloc_pt = vm
.hook_manager
.memory_manager
.get_page_table_as_mut(guest_page_pa.as_u64())
.get_page_table_as_mut(guest_large_page_pa.as_u64())
.ok_or(HypervisorError::PageTableNotFound)?;

// dump_primary_ept_entries(vm, guest_pa)?;
Expand Down Expand Up @@ -138,13 +141,16 @@ pub fn dump_primary_ept_entries(vm: &mut Vm, faulting_guest_pa: u64) -> Result<(
let faulting_guest_page_pa = PAddr::from(faulting_guest_pa).align_down_to_base_page().as_u64();
trace!("Faulting guest page address: {:#x}", faulting_guest_page_pa);

let guest_large_page_pa = PAddr::from(faulting_guest_pa).align_down_to_large_page();
trace!("Faulting guest large page address: {:#x}", guest_large_page_pa);

// Get the primary EPTs.
let primary_ept = &mut vm.primary_ept;

let pre_alloc_pt = vm
.hook_manager
.memory_manager
.get_page_table_as_mut(faulting_guest_page_pa)
.get_page_table_as_mut(guest_large_page_pa.as_u64())
.ok_or(HypervisorError::PageTableNotFound)?;

trace!("Dumping Primary EPT entries for guest physical address: {:#x}", faulting_guest_pa);
Expand Down
6 changes: 5 additions & 1 deletion hypervisor/src/intel/vmexit/mtf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ pub fn handle_monitor_trap_flag(vm: &mut Vm) -> Result<ExitType, HypervisorError
trace!("Guest PA: {:#x}", guest_pa.as_u64());

let guest_page_pa = guest_pa.align_down_to_base_page();
trace!("Guest Page PA: {:#x}", guest_page_pa.as_u64());

let guest_large_page_pa = guest_page_pa.align_down_to_large_page();
trace!("Guest Large Page PA: {:#x}", guest_large_page_pa.as_u64());

let shadow_page_pa = PAddr::from(
vm.hook_manager
Expand All @@ -53,7 +57,7 @@ pub fn handle_monitor_trap_flag(vm: &mut Vm) -> Result<ExitType, HypervisorError
let pre_alloc_pt = vm
.hook_manager
.memory_manager
.get_page_table_as_mut(guest_page_pa.as_u64())
.get_page_table_as_mut(guest_large_page_pa.as_u64())
.ok_or(HypervisorError::PageTableNotFound)?;

// Restore the hook to continue monitoring
Expand Down
5 changes: 4 additions & 1 deletion hypervisor/src/intel/vmexit/vmcall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ pub fn handle_vmcall(vm: &mut Vm) -> Result<ExitType, HypervisorError> {
let guest_page_pa = guest_function_pa.align_down_to_base_page();
trace!("Guest Page PA: {:#x}", guest_page_pa.as_u64());

let guest_large_page_pa = guest_page_pa.align_down_to_large_page();
trace!("Guest Large Page PA: {:#x}", guest_large_page_pa.as_u64());

// Set the current hook to the EPT hook for handling MTF exit
let exit_type = if let Some(shadow_page_pa) = vm.hook_manager.memory_manager.get_shadow_page_as_ptr(guest_page_pa.as_u64()) {
trace!("Shadow Page PA: {:#x}", shadow_page_pa);
Expand All @@ -69,7 +72,7 @@ pub fn handle_vmcall(vm: &mut Vm) -> Result<ExitType, HypervisorError> {
let pre_alloc_pt = vm
.hook_manager
.memory_manager
.get_page_table_as_mut(guest_page_pa.as_u64())
.get_page_table_as_mut(guest_large_page_pa.as_u64())
.ok_or(HypervisorError::PageTableNotFound)?;

// Perform swap_page before the mutable borrow for update_guest_interrupt_flag
Expand Down

0 comments on commit 107f9b2

Please sign in to comment.