diff --git a/bao1x-boot/boot0/src/main.rs b/bao1x-boot/boot0/src/main.rs index 50f08d93e..e47359ea7 100644 --- a/bao1x-boot/boot0/src/main.rs +++ b/bao1x-boot/boot0/src/main.rs @@ -15,7 +15,7 @@ use alloc::collections::VecDeque; use core::cell::RefCell; use bao1x_api::pubkeys::{BOOT0_SELF_CHECK, BOOT0_TO_ALTBOOT1, BOOT0_TO_BOOT1}; -use bao1x_api::{BOOT0_PUBKEY_FAIL, bollard}; +use bao1x_api::{BOOT0_PUBKEY_FAIL, DEVELOPER_MODE, bollard}; use bao1x_api::{HardenedBool, PARANOID_MODE, PARANOID_MODE_DUPE}; use bao1x_hal::acram::OneWayCounter; use bao1x_hal::hardening::{check_pll, die, mesh_setup}; @@ -172,6 +172,25 @@ pub unsafe extern "C" fn rust_entry() -> ! { bollard!(die, 4); // == end self validation == + // If the developer bit is set, ensure that keys are erased. The edge case we're worried about is + // if an attacker sets developer bit, even with signed images - this can allow for an easier + // time of booting a malicious kernel because we can't erase secret keys inside the loader + // due to access restrictions. + csprng.random_delay(); + let (dev1, dev2) = owc.hardened_get(DEVELOPER_MODE).unwrap(); + if dev1 != 0 { + bao1x_hal::sigcheck::erase_secrets(&mut Some(&mut csprng)) + .inspect_err(|e| crate::println!("{}", e)) + .ok(); // "ok" because the expected error is a check on logic/configuration bugs, not attacks + } + bollard!(die, 4); + csprng.random_delay(); + if dev2 != 0 { + bao1x_hal::sigcheck::erase_secrets(&mut Some(&mut csprng)) + .inspect_err(|e| crate::println!("{}", e)) + .ok(); // "ok" because the expected error is a check on logic/configuration bugs, not attacks + } + bollard!(die, 4); let boot_order = match owc.get_decoded::() { // Primary boot selected. Check Boot1 first, then fall back to LOADER/BAREMETAL. diff --git a/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs b/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs index 0e0d62e6b..aeb0d2c1c 100644 --- a/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs +++ b/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs @@ -71,7 +71,10 @@ pub fn setup_backup_region() -> u32 { let mut bu_mgr = bao1x_hal::buram::BackupManager::new(); if !bu_mgr.is_backup_valid() { // zeroize the backup RAM - bu_mgr.bu_ram_as_mut().fill(0); + // safety: make_valid is called after this is done. + unsafe { + bu_mgr.bu_ram_as_mut().fill(0); + } // calculate the hash and mark as valid bu_mgr.make_valid(); diff --git a/bao1x-boot/boot1/src/platform/bao1x/slots.rs b/bao1x-boot/boot1/src/platform/bao1x/slots.rs index 216888f3e..995fcbf87 100644 --- a/bao1x-boot/boot1/src/platform/bao1x/slots.rs +++ b/bao1x-boot/boot1/src/platform/bao1x/slots.rs @@ -23,6 +23,8 @@ pub fn check_slots(board_type: &bao1x_api::BoardTypeCoding) { let mut rram = Reram::new(); // check the lifecycle status + // glitch_safety: this is done in a controlled environment, and not done again later, + // so we don't need to harden it. If it is repeated due to a glitch it actually leads to data loss. if owc.get(bao1x_api::CP_BOOT_SETUP_DONE).unwrap() == 0 { crate::println!("CP setup not yet done. Initializing public identifiers..."); // CP boot setup is not done. Initialize the basic identifiers. @@ -72,52 +74,6 @@ pub fn check_slots(board_type: &bao1x_api::BoardTypeCoding) { crate::println!("Public ID init done."); } - // Delegate in-system key setup to the OS. Don't do it in the bootloader. This code is - // too hard to get right to be doing it here, and there is no ostensible security advantage - // in doing it at this stage versus in the system. Reasoning: the in-system key setup would - // be done in the factory, post-wafer probe, regardless. Whether it's hard-coded into the boot1 - // stage or left to be flexible based on the OS's needs doesn't make a difference because the - // point of keying is still going to be the factory. - /* - if (*board_type == bao1x_api::BoardTypeCoding::Baosec - && owc.get(bao1x_api::IN_SYSTEM_BOOT_SETUP_DONE).unwrap() == 0) - || (*board_type == bao1x_api::BoardTypeCoding::Dabao - && owc.get(bao1x_api::INVOKE_DABAO_KEY_SETUP).unwrap() != 0 - && owc.get(bao1x_api::DABAO_KEY_SETUP_DONE).unwrap() == 0) - { - crate::println!("System setup not yet done. Initializing secret identifiers..."); - let trng = maybe_trng.get_or_insert_with(|| super::trng::ManagedTrng::new(&board_type)); - // generate all the keys - let key_set = if *board_type == bao1x_api::BoardTypeCoding::Baosec { - &bao1x_api::baosec::KEY_SLOTS[..] - } else { - &bao1x_api::dabao::KEY_SLOTS[..] - }; - for key_range in key_set.iter() { - let mut storage = alloc::vec::Vec::with_capacity(key_range.len() * SLOT_ELEMENT_LEN_BYTES); - storage.resize(key_range.len() * SLOT_ELEMENT_LEN_BYTES, 0); - for chunk in storage.chunks_mut(SLOT_ELEMENT_LEN_BYTES) { - chunk.copy_from_slice(&trng.generate_key()); - } - match slot_mgr.write(&mut rram, key_range, &storage) { - Ok(_) => {} - Err(e) => { - crate::println!("Couldn't initialize slot {:?}: {:?}", key_range, e); - } - } - } - // once all values are written, advance the IN_SYSTEM_BOOT_SETUP_DONE state - // safety: the offset is correct because we're pulling it from our pre-defined constants and - // those are manually checked. - if *board_type == bao1x_api::BoardTypeCoding::Baosec { - unsafe { owc.inc(bao1x_api::IN_SYSTEM_BOOT_SETUP_DONE).unwrap() }; - } else if *board_type == bao1x_api::BoardTypeCoding::Dabao { - unsafe { owc.inc(bao1x_api::DABAO_KEY_SETUP_DONE).unwrap() }; - } - crate::println!("Secret ID init done."); - } - */ - #[cfg(feature = "print-ifr")] print_ifr(); @@ -128,11 +84,13 @@ pub fn check_slots(board_type: &bao1x_api::BoardTypeCoding) { // wear-out on the ACL entries as every time a transition is made into a developer images, a set // of keys are checked to be erased. While it is possible to bypass the ACL checks by flipping // the developer mode bit, the key check/erasure would still happen upon launch into developer mode. + // + // glitch_safety: we don't harden this because this is already a "seatbelt" safety measure + // in most cases it should do nothing because the ACLs are already correct. Also, if DEVELOPER_MODE + // is tripped, there is a hardened check that ensures keys are erased when that bit is set. #[cfg(feature = "unsafe-debug")] print_slots(&slot_mgr, &bao1x_api::baosec::KEY_SLOTS); // this prints all the keys as they are created - if (*board_type == bao1x_api::BoardTypeCoding::Baosec || *board_type == bao1x_api::BoardTypeCoding::Dabao) - && owc.get(bao1x_api::DEVELOPER_MODE).unwrap() == 0 - { + if owc.get(bao1x_api::DEVELOPER_MODE).unwrap() == 0 { #[cfg(feature = "unsafe-debug")] print_slots(&slot_mgr, &bao1x_api::baosec::KEY_SLOTS); check_and_fix_acls(&mut rram, &mut slot_mgr, &bao1x_api::baosec::KEY_SLOTS); diff --git a/bao1x-boot/boot1/src/secboot.rs b/bao1x-boot/boot1/src/secboot.rs index e8764c741..4927bfc16 100644 --- a/bao1x-boot/boot1/src/secboot.rs +++ b/bao1x-boot/boot1/src/secboot.rs @@ -1,4 +1,6 @@ -use bao1x_api::{PARANOID_MODE, PARANOID_MODE_DUPE, bollard, pubkeys::BOOT1_TO_LOADER_OR_BAREMETAL}; +use bao1x_api::{ + DEVELOPER_MODE, PARANOID_MODE, PARANOID_MODE_DUPE, bollard, pubkeys::BOOT1_TO_LOADER_OR_BAREMETAL, +}; use bao1x_hal::hardening::Csprng; #[inline(always)] @@ -19,6 +21,22 @@ fn seal_boot1_keys() { pub fn try_boot(or_die: bool, csprng: &mut Csprng) { let one_way = bao1x_hal::acram::OneWayCounter::new(); bollard!(bao1x_hal::sigcheck::die_no_std, 4); + + // If the developer bit is set, ensure that keys are erased. The edge case we're worried about is + // if an attacker sets developer bit, even with signed images - this can allow for an easier + // time of booting a malicious kernel because we can't erase secret keys inside the loader + // due to access restrictions. + csprng.random_delay(); + let (dev1, dev2) = one_way.hardened_get(DEVELOPER_MODE).unwrap(); + if dev1 != 0 { + bao1x_hal::sigcheck::erase_secrets(&mut Some(csprng)).inspect_err(|e| crate::println!("{}", e)).ok(); // "ok" because the expected error is a check on logic/configuration bugs, not attacks + } + bollard!(bao1x_hal::sigcheck::die_no_std, 4); + csprng.random_delay(); + if dev2 != 0 { + bao1x_hal::sigcheck::erase_secrets(&mut Some(csprng)).inspect_err(|e| crate::println!("{}", e)).ok(); // "ok" because the expected error is a check on logic/configuration bugs, not attacks + } + csprng.random_delay(); let (paranoid1, paranoid2) = one_way.hardened_get2(PARANOID_MODE, PARANOID_MODE_DUPE).unwrap(); csprng.random_delay();