From e6bef77b5fd77534064ad23dceb5acb014525d61 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 27 Oct 2025 18:22:00 +0800 Subject: [PATCH] change boot1 default behavior The motivation for this is to make the tester configuration exactly identical to that used in production. Right now a "cheater" step is used to set the board type to dabao and enable boot waiting because in the test environment there isn't a physical switch anyone can press to invoke that behavior. So, in order to get the test environment to work just like production, the behavior goes from insta-die if no valid image is found on the next stage, to "wait for new image". This is slightly less secure of a default, but only slightly so. Basically the previous scheme required someone to be physically present to press the "give me a new image" button, this one causes the system to automatically accept a new image. I suppose the main downside is this does allow the possibility of say, a fully remote, automated exploit to be delivered if an adversary can damage the loader image such that it doesn't pass signature check and then it will automatically go into a mode looking for a new image, at which point an arbitrary image could be potentially loaded (although because the image would need to be signed, and there is a trap that destroys secrets if a developer image is presented, it wouldn't instantly lead to secret disclosure, but it would be a denial of service). The flip side is that with the paranoid default, developers are "confused" by the silent failure of a brand new chip that has no code loaded in it (because it goes catatonic until the developer knows to press the load button). The loader image is protected from writes after boot, so in theory there isn't a direct path to "spoil" the loader image and trigger this path, so it's not like we're just hanging in the wind. It just means that there isn't a full human-presence requirement as a defense in depth against a potential denial of service if an adversary could find a way to spoil the loader image. If the DoS path is a concern, one option is to configure the system such that the developer key is revoked. This would disallow that from being recognized as a valid image, and would also prevent the DoS from happening, as it would require the adversary to present a valid, signed image which presumably they can't as long as the signing keys are kept safe. --- bao1x-boot/boot1/src/main.rs | 14 +++++++------- bao1x-boot/boot1/src/repl.rs | 2 +- bao1x-boot/boot1/src/secboot.rs | 12 +++++++++--- libs/bao1x-api/src/offsets/common.rs | 10 +++++++++- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/bao1x-boot/boot1/src/main.rs b/bao1x-boot/boot1/src/main.rs index f37fb14a7..119a49c46 100644 --- a/bao1x-boot/boot1/src/main.rs +++ b/bao1x-boot/boot1/src/main.rs @@ -25,9 +25,9 @@ use platform::*; use utralib::*; use ux_api::minigfx::{DrawStyle, FrameBuffer, Point, Rectangle}; +use crate::delay; use crate::platform::usb::glue; -use crate::secboot::boot_or_die; -use crate::{delay, platform::irq::disable_all_irqs}; +use crate::secboot::try_boot; // Notes: // - "Towards" - not a release yet, but working towards the stated milestone @@ -104,9 +104,9 @@ pub unsafe extern "C" fn rust_entry() -> ! { let boot_wait = one_way.get_decoded::().expect("internal error"); if boot_wait == BootWaitCoding::Disable && current_key.is_none() { - disable_all_irqs(); - // this should diverge, rest of code is not run - boot_or_die(); + // diverges if there is code to run + try_boot(false); + // or_die == false means the rest of this gets run if there is no valid image } if boot_wait == BootWaitCoding::Enable { @@ -291,9 +291,9 @@ pub unsafe extern "C" fn rust_entry() -> ! { } }); - disable_all_irqs(); // when we get to this point, there's only two options... - crate::secboot::boot_or_die(); + try_boot(true); + unreachable!("`or_die = true` means this should be unreachable"); } pub fn marquee(sh1107: &mut Oled128x128, msg: &str) { diff --git a/bao1x-boot/boot1/src/repl.rs b/bao1x-boot/boot1/src/repl.rs index e329987b0..0a47f5f62 100644 --- a/bao1x-boot/boot1/src/repl.rs +++ b/bao1x-boot/boot1/src/repl.rs @@ -106,7 +106,7 @@ impl Repl { rcurst.wo(utra::sysctrl::SFR_RCURST0, 0x55AA); } "boot" => { - crate::secboot::boot_or_die(); + crate::secboot::try_boot(true); } "uf2" => { use base64::{Engine as _, engine::general_purpose}; diff --git a/bao1x-boot/boot1/src/secboot.rs b/bao1x-boot/boot1/src/secboot.rs index a05ebc764..39fcc1efc 100644 --- a/bao1x-boot/boot1/src/secboot.rs +++ b/bao1x-boot/boot1/src/secboot.rs @@ -22,7 +22,7 @@ fn seal_boot1_keys() { cu.protect(); } -pub fn boot_or_die() -> ! { +pub fn try_boot(or_die: bool) { // loader is at the same offset as baremetal. Accept either as valid boot. // This diverges if the signature check is successful match bao1x_hal::sigcheck::validate_image( @@ -39,6 +39,10 @@ pub fn boot_or_die() -> ! { k, core::str::from_utf8(&tag).unwrap_or("invalid tag") ); + + // disable IRQs in preparation for next phase + crate::platform::irq::disable_all_irqs(); + // the tag is from signed, trusted data // k is just a nominal slot number. If either match, assume we are dealing with a // developer image. @@ -56,6 +60,8 @@ pub fn boot_or_die() -> ! { } Err(e) => crate::println!("Image did not validate: {:?}", e), } - crate::println!("No valid loader or baremetal image found. Halting!"); - bao1x_hal::sigcheck::die_no_std(); + if or_die { + crate::println!("No valid loader or baremetal image found. Halting!"); + bao1x_hal::sigcheck::die_no_std(); + } } diff --git a/libs/bao1x-api/src/offsets/common.rs b/libs/bao1x-api/src/offsets/common.rs index 3b4f15dde..7aba484db 100644 --- a/libs/bao1x-api/src/offsets/common.rs +++ b/libs/bao1x-api/src/offsets/common.rs @@ -76,8 +76,8 @@ encode_oneway! { encode_oneway! { #[offset = 81] pub enum BoardTypeCoding { - Baosec, Dabao, + Baosec, Oem, } } @@ -151,6 +151,14 @@ pub const IFR_HASH: SlotIndex = SlotIndex::Data(2, PartitionAccess::All, RwPerms /// during CP. pub const CP_ID: SlotIndex = SlotIndex::Data(3, PartitionAccess::All, RwPerms::ReadOnly); +/// Indelible versions of the public keys. The problem with the pubkeys in boot0 region is that +/// boot0 itself has the ability to modify its own memory. A copy here can have a bit set in +/// the IFR that blocks any attempt to modify these keys. +pub const BAO1_PUBKEY: SlotIndex = SlotIndex::Data(4, PartitionAccess::All, RwPerms::ReadOnly); +pub const BAO2_PUBKEY: SlotIndex = SlotIndex::Data(5, PartitionAccess::All, RwPerms::ReadOnly); +pub const BETA_PUBKEY: SlotIndex = SlotIndex::Data(6, PartitionAccess::All, RwPerms::ReadOnly); +pub const DEV_PUBKEY: SlotIndex = SlotIndex::Data(7, PartitionAccess::All, RwPerms::ReadOnly); + // Notes on defining the boot0 IFR region. // RISC-V boot0 region start is defined by IFR slot 6, bits [55:48] // RISC-V boot1 region start (which is boot0 end) is defined by IFR slot 6, bits[47:40]