From 4c777976ac708d037361205eea49c68ed27cd9f3 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 22 Jul 2021 03:03:27 +0800 Subject: [PATCH] clear RAM on a cold boot since the RAM is battery backed and persistent, it's probably a good idea just to zero-ize it before using it on a cold boot. --- loader/src/main.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/loader/src/main.rs b/loader/src/main.rs index 0cd7fd2c9..cfe763792 100644 --- a/loader/src/main.rs +++ b/loader/src/main.rs @@ -1197,6 +1197,7 @@ fn boot_sequence(args: KernelArguments, _signature: u32) -> ! { if !clean { // cold boot path println!("no suspend marker found, doing a cold boot!"); + clear_ram(&mut cfg); phase_1(&mut cfg); phase_2(&mut cfg); println!("done initializing for cold boot."); @@ -1372,6 +1373,19 @@ fn check_resume(cfg: &mut BootConfig) -> (bool, bool, u32) { (clean, was_forced_suspend, pid) } +fn clear_ram(cfg: &mut BootConfig) { + // clear RAM on a cold boot. + // RAM is persistent and battery-backed. This means secret material could potentiall + // stay there forever, if not explicitly cleared. This clear adds a couple seconds + // to a cold boot, but it's probably worth it. Note that it doesn't happen on a suspend/resume. + let ram: *mut u32 = cfg.sram_start as *mut u32; + unsafe { + for addr in 0..(cfg.sram_size - 8192) / 4 { // 8k is reserved for our own stack + ram.add(addr).write_volatile(0); + } + } +} + fn phase_1(cfg: &mut BootConfig) { // Allocate space for the stack pointer. // The bootloader should have placed the stack pointer at the end of RAM