From d6e7f7ce16615b8dcfb83ff0f32323d4388457ac Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 9 Apr 2024 15:17:49 +0800 Subject: [PATCH] add debugging and asserts to detect stack smash in loader In swap configuration, the loader does a lot more: it has to decrypt full pages of RAM and hold them temporarily; up to two pages at once, once for the decrypt of the source swap image, and one for the encrypt to swap memory. This causes a lot more stack to be used than the original loader. Add some debugging for this, and an assert based on a heuristic that the RPT (runtime page tracker) last few entries ought to be 0. This I think is probably true on most platforms. If this fails to be true, then, we can probably modify the assert to fail if any number in the RPT is greater than the number of processes: this should be true in *all* cases, but a stack smash is very likely to write something in there that's larger than the number of allowable processes in the system. --- loader/src/main.rs | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/loader/src/main.rs b/loader/src/main.rs index fdf9542ac..b526ba34d 100644 --- a/loader/src/main.rs +++ b/loader/src/main.rs @@ -216,7 +216,25 @@ fn boot_sequence(args: KernelArguments, _signature: u32, fs_prehash: [u8; 64]) - if !clean { // The MMU should be set up now, and memory pages assigned to their // respective processes. - let krn_struct_start = cfg.sram_start as usize + cfg.sram_size - cfg.init_size; + let krn_struct_start = cfg.sram_start as usize + cfg.sram_size - cfg.init_size + cfg.swap_offset; + #[cfg(feature = "swap")] + if SDBG && VDBG { + // activate to debug stack smashes. RPT should be 0's here if stack did not overflow. + for (i, r) in + cfg.runtime_page_tracker[cfg.runtime_page_tracker.len() - 1024..].chunks(32).enumerate() + { + println!(" rpt {:08x}: {:02x?}", cfg.runtime_page_tracker.len() - 1024 + i * 32, r); + } + } + // Add a static check for stack overflow, using a heuristic that the last 32 bytes of the RPT + // ought to be 0. This a valid assumption at least on Precursor, as it maps to some CSRs that should + // be unmapped at this point. TODO: check this assumption holds on other archs. + for &check in cfg.runtime_page_tracker[cfg.runtime_page_tracker.len() - 32..].iter() { + assert!( + check == 0x0, + "RPT looks corrupted, suspect stack overflow in loader. Increase GUARD_MEMORY_BYTES!" + ); + } let arg_offset = cfg.args.base as usize - krn_struct_start + KERNEL_ARGUMENT_OFFSET; let ip_offset = cfg.processes.as_ptr() as usize - krn_struct_start + KERNEL_ARGUMENT_OFFSET; let rpt_offset = @@ -345,6 +363,10 @@ pub fn read_initial_config(cfg: &mut BootConfig) { } cfg.sram_start = xarg.data[2] as *mut usize; cfg.sram_size = xarg.data[3] as usize; + #[cfg(feature = "swap")] + if SDBG { + println!("XAarg // sram_start: {:x}, sram_size: {:x}", cfg.sram_start as usize, cfg.sram_size); + } let mut kernel_seen = false; let mut init_seen = false; @@ -400,8 +422,10 @@ pub fn read_swap_config(cfg: &mut BootConfig) { } else if tag.name == u32::from_le_bytes(*b"XArg") { // these are actually specified inside the `Swap` arg, this is just // a mirror because this argument is added by default by the image creator - println!("swap start: {:x}", tag.data[2]); - println!("swap size: {:x}", tag.data[3]); + if SDBG { + println!("Swap start: {:x}", tag.data[2]); + println!("Swap size: {:x}", tag.data[3]); + } } else { println!("Unhandled argument in swap: {:x}", tag.name); }