fixup loader heap; move signature validation; fixup swap structures

- loader heap is now fixed in a "throw-away" region of memory.
this means the heap should only be used sparingly, and somewhat
dangerously, because the values can't persist beyond the lifetime
of the loader.

- signature validation moved earlier

- fixup swap structures to refer to the API so that image creation
is easier/consistent
This commit is contained in:
bunnie 2025-10-04 00:56:18 +08:00
parent 07e43d4459
commit 9b11361256
5 changed files with 77 additions and 66 deletions

View file

@ -23,7 +23,7 @@ armv7 = { git = "https://github.com/Foundation-Devices/armv7.git", branch = "upd
atsama5d27 = { git = "https://github.com/Foundation-Devices/atsama5d27.git", branch = "master", optional = true }
sha2-loader = { path = "./sha2-loader", default-features = false, optional = true }
bao1x-hal = { path = "../libs/bao1x-hal", optional = true, default-features = false }
bao1x-api = { path = "../libs/bao1x-api", optional = true, default-features = false }
bao1x-api = { path = "../libs/bao1x-api", default-features = false }
ux-api = { path = "../libs/ux-api", optional = true, default-features = false }
xous-pl230 = { path = "../libs/xous-pl230", optional = true, features = [
"tests",
@ -93,7 +93,6 @@ bao1x = [
# "bao1x-hal/debug-print-usb",
"debug-print",
"bao1x-hal",
"bao1x-api",
"rand_chacha",
# "boot-delay",
]

View file

@ -107,12 +107,24 @@ const SDBG: bool = false; // swap debug
#[cfg(test)]
mod test;
// Bao1x needs a heap because the signature checking code assumes its availability.
// However the heap promises not to allocate any objects that are needed by the kernel,
// so we stick it in a region in low RAM, just about the statics, and restrict it to
// just 4k in size. Note that maybe we should strive to avoid the loader from allocating
// into these regions - but actually, later in the loader, we should avoid using the heaps
// and most data is done with, and it's nice to be able to reclaim these pages, so we
// somewhat dangerously tell the loader to go ahead and allocate over this region by
// telling it has the whole rest of SRAM to stick initial process pages in.
#[global_allocator]
#[cfg(feature = "swap")]
#[cfg(feature = "bao1x")]
static ALLOCATOR: linked_list_allocator::LockedHeap = linked_list_allocator::LockedHeap::empty();
// heap start is selected by looking at the total reserved .data + .bss region in the compiled loader.
// it hovers at 0x5000 unless I start adding lots of statics to the loader (which there are not).
#[cfg(feature = "bao1x")]
const HEAP_OFFSET: usize = 0x5000;
// just a small heap, big enough for us to use alloc to simplify argument processing
#[cfg(feature = "swap")]
const HEAP_LEN: usize = 4096;
#[cfg(feature = "bao1x")]
const HEAP_LEN: usize = 0x1000;
#[repr(C)]
pub struct MemoryRegionExtra {
@ -225,6 +237,56 @@ pub unsafe extern "C" fn rust_entry(signed_buffer: *const usize, signature: u32)
);
}
// Initialize the allocator with heap memory range. The heap memory is "throw-away"
// so we stick it near the bottom of RAM, with the assumption that the loader process
// won't smash over it.
#[cfg(any(feature = "bao1x"))]
{
let heap_start = utralib::HW_SRAM_MEM + HEAP_OFFSET;
crate::println!("Setting up heap @ {:x}-{:x}", heap_start, heap_start + HEAP_LEN);
unsafe {
ALLOCATOR.lock().init(heap_start as *mut u8, HEAP_LEN);
}
}
// Run kernel image validation now that the heap is set up.
#[cfg(feature = "bao1x")]
{
use bao1x_api::signatures::FunctionCode;
// validate using the bao1x signature scheme
match bao1x_hal::sigcheck::validate_image(
bao1x_api::KERNEL_START as *const u32,
bao1x_api::LOADER_START as *const u32,
bao1x_api::LOADER_REVOCATION_OFFSET,
&[
FunctionCode::Kernel as u32,
FunctionCode::UpdatedKernel as u32,
FunctionCode::Developer as u32,
],
false,
) {
Ok(k) => println!("*** Kernel signature check by key @ {} OK ***", k),
Err(e) => {
println!("Kernel failed signature check. Dying: {:?}", e);
bao1x_hal::sigcheck::die_no_std();
}
}
// todo: figure out how to do validation on the swap image. I think it should
// be something like this:
// - if the first page can decrypt using the local encryption key, accept the swap as valid
// - if it decrypts using the NULL key, do a hash + signature check of the whole thing
// The reason for the two-part test is that the signature check wouldn't work
// once the partition is re-encrypted (the signature is only on the NULL key
// image) and also it takes a lot of time to read in the partition twice and
// signature check it; also, the signature check itself is subject to a bit of
// a TOCTOU because of the block-by-block readout nature of the SPIM. This is
// why each block is encrypted anyways with an AEAD.
//
// So basically, the pubkey signature is only used to deliver an app for updates;
// the AEAD is for operation once the application has been accepted and re-encrypted
// to the device-specific key.
}
// the kernel arg buffer is SIG_BLOCK_SIZE into the signed region
#[cfg(not(feature = "bao1x"))]
let signature_size = SIGBLOCK_SIZE;
@ -268,32 +330,6 @@ fn boot_sequence(args: KernelArguments, _signature: u32, fs_prehash: [u8; 64], _
println!("Configured for simulation. Skipping RAM clear!");
clear_ram(&mut cfg);
phase_1(&mut cfg);
#[cfg(feature = "bao1x")]
{
// this is the soonest we can run the image validation because we need to
// have the heap setup first.
use bao1x_api::signatures::FunctionCode;
// validate using the bao1x signature scheme
match bao1x_hal::sigcheck::validate_image(
bao1x_api::KERNEL_START as *const u32,
bao1x_api::LOADER_START as *const u32,
bao1x_api::LOADER_REVOCATION_OFFSET,
&[
FunctionCode::Kernel as u32,
FunctionCode::UpdatedKernel as u32,
FunctionCode::Developer as u32,
],
false,
) {
Ok(k) => println!("*** Kernel signature check by key @ {} OK ***", k),
Err(e) => {
println!("Kernel failed signature check. Dying: {:?}", e);
bao1x_hal::sigcheck::die_no_std();
}
}
}
phase_2(&mut cfg, &fs_prehash);
#[cfg(any(feature = "debug-print", feature = "swap"))]
if VDBG || SDBG {

View file

@ -42,20 +42,6 @@ pub fn phase_1(cfg: &mut BootConfig) {
cfg.init_size += GUARD_MEMORY_BYTES;
println!("Loader runtime stack should not exceed: {:x}", cfg.get_top() as usize);
// Initialize the allocator with heap memory range
#[cfg(any(feature = "bao1x", feature = "swap"))]
{
crate::println!(
"Setting up heap @ {:x}-{:x}",
cfg.get_top() as usize,
cfg.get_top() as usize + HEAP_LEN
);
cfg.init_size += HEAP_LEN;
unsafe {
ALLOCATOR.lock().init(cfg.get_top() as *mut u8, HEAP_LEN);
}
}
// The first region is defined as being "main RAM", which will be used
// to keep track of allocations.
println!("Allocating regions");
@ -187,7 +173,7 @@ pub fn phase_1(cfg: &mut BootConfig) {
// We also skip the an additional index as that is the clean suspend page. This
// needs to be claimed by the susres server before the kernel allocates it.
// Lower numbered indices corresponding to higher address pages.
println!("Marking pages as in-use");
println!("Marking loader pages as in-use");
for i in ((GUARD_MEMORY_BYTES / PAGE_SIZE) + 1)..(cfg.init_size / PAGE_SIZE) {
cfg.runtime_page_tracker[cfg.sram_size / PAGE_SIZE - i] = XousAlloc::from(1);
}

View file

@ -208,6 +208,15 @@ impl SwapHal {
println!("Swap key: {:x?}", &swap.key);
println!("Dest key: {:x?}", &dest_key);
}
if swap.key == [0u8; 32] {
// TODO: check the signature of the swap region, because it was encrypted with the "null"
// key and the key imparts no security.
}
// TODO: do a trial decryption of block 0 with the swap.key. If it passes; great:
// proceed. If not, change out swap.key to the derived key for the swap key (which is
// still a process TBD) and try the decryption again. If it does not pass, then,
// the swap FS is either corrupted or non-existent.
let mut hal = SwapHal {
image_start: SWAP_IMG_START as usize + 4096,
image_mac_start: SWAP_IMG_START as usize + 4096 + ssh.mac_offset as usize,

View file

@ -2,6 +2,7 @@ use core::fmt;
use core::mem::size_of;
use aes_gcm_siv::Tag;
pub use bao1x_api::signatures::{SwapDescriptor, SwapSourceHeader};
use crate::PAGE_SIZE;
use crate::SWAP_FLG_DIRTY;
@ -35,26 +36,6 @@ use crate::SWAPPER_PID;
/// - The top 2 bits of the physical address are 0
/// - The middle 20 bits the PA are the MSB of the address to the PA of the target page
#[repr(C)]
pub struct SwapDescriptor {
pub ram_offset: u32,
pub ram_size: u32,
pub name: u32,
pub key: [u8; 32],
pub flash_offset: u32,
}
#[derive(Debug)]
#[repr(C)]
pub struct SwapSourceHeader {
pub version: u32,
pub partial_nonce: [u8; 8],
pub mac_offset: u32,
pub aad_len: u32,
// aad is limited to 64 bytes!
pub aad: [u8; 64],
}
#[repr(C, align(16))]
pub struct RawPage {
pub data: [u8; 4096],