From a66e4f5901c640f8740ed94dbe77be5345f1cc25 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 9 Apr 2024 15:24:22 +0800 Subject: [PATCH] modify stack smash check to detect if non-PID is written this is more robust, and should work across all architectures. Note that the reliability of the check is slightly impacted since we're now checking for a subset of valid numbers, so the number of bytes checked is doubled. --- loader/src/main.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/loader/src/main.rs b/loader/src/main.rs index b526ba34d..06d9361e1 100644 --- a/loader/src/main.rs +++ b/loader/src/main.rs @@ -219,19 +219,20 @@ fn boot_sequence(args: KernelArguments, _signature: u32, fs_prehash: [u8; 64]) - 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. + // activate to debug stack smashes. RPT should be 0's here (or at least valid PIDs) 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() { + // Add a static check for stack overflow, using a heuristic that the last 64 bytes of the RPT + // ought to be a valid PID. A stack smash is likely to write something that does not obey + // this heuristic within that range (any stack-stored pointer, for example, will break this). + for &check in cfg.runtime_page_tracker[cfg.runtime_page_tracker.len() - 64..].iter() { assert!( - check == 0x0, + check <= cfg.processes.len() as u8, "RPT looks corrupted, suspect stack overflow in loader. Increase GUARD_MEMORY_BYTES!" ); }