From 514afc2385316a3e6890912362f2a8330d7ad9ff Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 25 May 2024 00:04:59 +0800 Subject: [PATCH] refine swapper OOM handler security checks The Hard OOM re-entrant syscall can actually originate in any process space, but only from inside the kernel. Modify the checks so that the PID==2 check is applied only to all the other calls; and that a supervisor mode check is applied to the hard OOM call. --- kernel/src/syscall.rs | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index c65d0b85e..195a357e9 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -1014,14 +1014,13 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR } #[cfg(feature = "swap")] SysCall::SwapOp(op, a1, a2, a3, a4, a5, a6) => { - if pid.get() != xous_kernel::SWAPPER_PID { - klog!("Illegal caller"); // only PID 2 can call this - return Err(xous_kernel::Error::AccessDenied); - } #[cfg(feature = "debug-swap")] println!("Swap ABI via syscall: {:?}", SwapAbi::from(op)); match SwapAbi::from(op) { SwapAbi::Evict => { + if pid.get() != xous_kernel::SWAPPER_PID { + return Err(xous_kernel::Error::AccessDenied); + } let pid = a1; if pid != 0 && pid != 1 && pid != 2 { Swap::with_mut(|swap| swap.evict_page(pid, a2)) @@ -1032,10 +1031,23 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR Err(xous_kernel::Error::InvalidPID) } } - SwapAbi::GetFreePages => Swap::with(|swap| swap.get_free_mem()), - SwapAbi::FetchAllocs => Swap::with_mut(|swap| swap.fetch_allocs()), + SwapAbi::GetFreePages => { + if pid.get() != xous_kernel::SWAPPER_PID { + return Err(xous_kernel::Error::AccessDenied); + } + Swap::with(|swap| swap.get_free_mem()) + } + SwapAbi::FetchAllocs => { + if pid.get() != xous_kernel::SWAPPER_PID { + return Err(xous_kernel::Error::AccessDenied); + } + Swap::with_mut(|swap| swap.fetch_allocs()) + } // SwapAbi::SetOomThresh => Swap::with_mut(|swap| swap.set_oom_thresh(a1, a2)), SwapAbi::StealPage => { + if pid.get() != xous_kernel::SWAPPER_PID { + return Err(xous_kernel::Error::AccessDenied); + } let target_pid = a1 as u8; let vaddr = a2; match crate::arch::mem::evict_page_inner( @@ -1053,6 +1065,9 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR } } SwapAbi::ReleaseMemory => { + if pid.get() != xous_kernel::SWAPPER_PID { + return Err(xous_kernel::Error::AccessDenied); + } let vaddr_to_release = a1; let original_pid = a2 as u8; MemoryManager::with_mut(|mm| { @@ -1070,6 +1085,16 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR Ok(xous_kernel::Result::Ok) }) } + SwapAbi::HardOom => { + // Hard OOM can be invoked from any PID, but, only by a kernel routine. Check that + // we're in supervisor mode! + if riscv::register::sstatus::read().spp() == riscv::register::sstatus::SPP::Supervisor { + // any process can invoke the hard OOM syscall, but only from within the kernel + Swap::with_mut(|swap| swap.hard_oom_syscall()) + } else { + Err(xous_kernel::Error::AccessDenied) + } + } SwapAbi::Invalid => { println!( "Invalid SwapOp: {:x} {:x} {:x} {:x} {:x} {:x} {:x}",