From 426fe4b91f2f653271f562c00d8ff133841393dd Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Thu, 30 Jun 2022 19:45:08 +0800 Subject: [PATCH] kernel: riscv: don't crash when mapping invalid memory Previously, the kernel would dutifully try to map memory that was in the kernel space. It did this by investigating the kernel memory offset, which immediately causes a double-fault. Check to make sure a userspace process is not trying to map kernel memory before checking the valid page. Signed-off-by: Sean Cross --- kernel/src/arch/riscv/mem.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/kernel/src/arch/riscv/mem.rs b/kernel/src/arch/riscv/mem.rs index 1df5dbc08..cbd9e5ed2 100644 --- a/kernel/src/arch/riscv/mem.rs +++ b/kernel/src/arch/riscv/mem.rs @@ -259,6 +259,10 @@ impl MemoryMapping { PID::new((self.satp >> 22 & ((1 << 9) - 1)) as _).unwrap() } + pub fn is_kernel(&self) -> bool { + self.get_pid().get() == 1 + } + /// Set this mapping as the systemwide mapping. /// **Note:** This should only be called from an interrupt in the /// kernel, which should be mapped into every possible address space. @@ -288,7 +292,7 @@ impl MemoryMapping { ); // Page 1023 is only available to PID1 - if i == 1023 && self.get_pid().get() != 1 { + if i == 1023 && !self.is_kernel() { println!(" "); continue; } @@ -872,6 +876,10 @@ pub fn virt_to_phys(virt: usize) -> Result { } pub fn ensure_page_exists_inner(address: usize) -> Result { + // Disallow mapping memory outside of user land + if ! MemoryMapping::current().is_kernel() && address >= USER_AREA_END { + return Err(xous_kernel::Error::OutOfMemory); + } let virt = address & !0xfff; let entry = crate::arch::mem::pagetable_entry(virt).or(Err(xous_kernel::Error::BadAddress))?; // let entry = crate::arch::mem::pagetable_entry(virt).or_else(|e| {