diff --git a/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs b/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs index c5b134d17..d2abed39f 100644 --- a/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs +++ b/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs @@ -70,13 +70,6 @@ pub fn setup_dabao_se0_pin(iox: &T) -> (IoxPort, u8) { /// This can change the board type coding to a safer, simpler board type if the declared board type has /// problems booting. pub fn early_init(mut board_type: bao1x_api::BoardTypeCoding) -> (bao1x_api::BoardTypeCoding, u32) { - // This is a security-critical initialization. Failure to do this correctly breaks - // the hardware access control scheme for key/data slots. - let mut cu = bao1x_hal::coreuser::Coreuser::new(); - // safety: this is safe because it's followed by a `protect()` call. - unsafe { cu.set() }; - cu.protect(); // locks out future modifications to the Coreuser setting - let iox = Iox::new(utra::iox::HW_IOX_BASE as *mut u32); // setup board-specific I/Os - early boot set. These are items that have to be @@ -247,8 +240,15 @@ pub fn early_init(mut board_type: bao1x_api::BoardTypeCoding) -> (bao1x_api::Boa setup_timer(fclk_freq); - // check key slots for integrity + // check key slots for integrity. Has to be done late in boot because we might + // have to generate keys, and that requires a lot of stuff to be working correctly: + // in particular, we'll need `alloc` (to store the random vectors) and `timer` + // (to detect disconnected/failed TRNG). + let mut cu = bao1x_hal::coreuser::Coreuser::new(); + // Coreuser needs to be set up correctly for check_slots to succeed. + cu.set(); crate::platform::slots::check_slots(&board_type); + // protect() is called inside sigcheck on boot! // Rx setup let _udma_uart = setup_console(&board_type, &iox, perclk); diff --git a/bao1x-boot/boot1/src/platform/bao1x/slots.rs b/bao1x-boot/boot1/src/platform/bao1x/slots.rs index fd193694d..34b19d8db 100644 --- a/bao1x-boot/boot1/src/platform/bao1x/slots.rs +++ b/bao1x-boot/boot1/src/platform/bao1x/slots.rs @@ -93,9 +93,9 @@ pub fn check_slots(board_type: &bao1x_api::BoardTypeCoding) { crate::println!("Secret ID init done."); } + // print_ifr(); // test code for figuring out if boot0 write-protect is working or not. /* - print_ifr(); crate::println!("test boot0 write"); let test = [7u8; 32]; let test_data = unsafe { core::slice::from_raw_parts(0x6001_0000 as *const u8, 32) }; @@ -115,6 +115,10 @@ pub fn check_slots(board_type: &bao1x_api::BoardTypeCoding) { /* fn print_ifr() { + let coreuser = utralib::CSR::new(utralib::utra::coreuser::HW_COREUSER_BASE as *mut u32); + // needs to be 0x118 for IFR to be readable when the protection bit is set. + crate::println!("coreuser status: {:x}", coreuser.r(utralib::utra::coreuser::STATUS)); + let ifr = unsafe { core::slice::from_raw_parts(0x6040_0000 as *const u32, 0x100) }; for (i, &d) in ifr.iter().enumerate() { if i % 8 == 0 { @@ -158,7 +162,9 @@ fn check_and_fix_acls(rram: &mut Reram, slot_mgr: &mut SlotManager, slot_list: & */ sa.get_partition_access() == pa && sa.get_rw_permissions() == rw } - AccessSettings::Key(sa) => sa.get_partition_access() == pa && sa.get_rw_permissions() == rw, + AccessSettings::Key(sa) => { + sa.get_partition_access() == pa && sa.get_rw_permissions() == rw && sa.akey_id() == 31 + } }; if !is_correct || !is_consistent { crate::println!( @@ -175,6 +181,7 @@ fn check_and_fix_acls(rram: &mut Reram, slot_mgr: &mut SlotManager, slot_list: & AccessSettings::Key(sa) => { sa.set_partition_access(&pa); sa.set_rw_permissions(rw); + sa.set_akey_id(31); // testing ID } } crate::println!("Fixed ACL raw value: {:x?}", acl); @@ -197,12 +204,22 @@ fn print_slots(slot_mgr: &SlotManager, slot_list: &[SlotIndex]) { SlotType::Data => unsafe { let bytes = slot_mgr.read_data_slot(data_index); let acl = slot_mgr.get_data_acl(acl_index); - crate::println!(" Data {} ({:?}): {:x?}", data_index, acl, bytes); + crate::println!( + " Data {} ({:x?}): {:x?}", + data_index / SLOT_ELEMENT_LEN_BYTES, + acl, + bytes + ); }, SlotType::Key => unsafe { let bytes = slot_mgr.read_key_slot(data_index); let acl = slot_mgr.get_key_acl(acl_index); - crate::println!(" Key {} ({:?}): {:x?}", data_index, acl, bytes); + crate::println!( + " Key {} ({:x?}): {:x?}", + data_index / SLOT_ELEMENT_LEN_BYTES, + acl, + bytes + ); }, } } diff --git a/bao1x-boot/boot1/src/platform/bao1x/trng.rs b/bao1x-boot/boot1/src/platform/bao1x/trng.rs index a526304e7..7fda54441 100644 --- a/bao1x-boot/boot1/src/platform/bao1x/trng.rs +++ b/bao1x-boot/boot1/src/platform/bao1x/trng.rs @@ -48,6 +48,7 @@ impl ManagedTrng { bio_ss.bio.wo(utra::bio_bdma::SFR_TXF0, trng_pin as u32); // start the sampling // check if the TRNG seems to be working. If not, reject the board setting and reboot + /* let mut timer = utralib::CSR::new(utra::timer0::HW_TIMER0_BASE as *mut u32); timer.wfo(utra::timer0::EV_PENDING_ZERO, 1); let av_good: bool; @@ -91,7 +92,8 @@ impl ManagedTrng { rcurst.wo(utra::sysctrl::SFR_RCURST0, 0x55AA); // this is actually unreachable None - } + }*/ + None } else { None }; diff --git a/bao1x-boot/boot1/src/secboot.rs b/bao1x-boot/boot1/src/secboot.rs index f41fc7246..8b0d684bf 100644 --- a/bao1x-boot/boot1/src/secboot.rs +++ b/bao1x-boot/boot1/src/secboot.rs @@ -9,19 +9,20 @@ pub const ALLOWED_FUNCTIONS: [u32; 5] = [ ]; fn seal_boot1_keys() { - // TODO: - // - setup an initial coreuser table - // - set an ASID that locks out any boot0 secrets (currently none, as it's PK based) - // - this does not offer strong security, but prevents someone with an arbitrary read primitive from - // accessing any boot0 secrets. An arbitrary-exec primitive at this point can, of course, undo the ASID - // mapping. + // This is a security-critical initialization. Failure to do this correctly totally breaks + // the hardware access control scheme for key/data slots. // - // Only necessary if we have secrets to seal. The current implementation only contains a public key, - // so there's no secrets to seal. + // However, this does not offer unbreakable security. Rather, it prevents someone with an arbitrary read + // primitive from accessing secret keys. An arbitrary-exec primitive can, of course, forge the ASID + // and work around the coreuser mechanism. + let mut cu = bao1x_hal::coreuser::Coreuser::new(); + cu.set(); // re-sets the coreuser settings. Just in case they got modified along the way... + // locks out future modifications to the Coreuser setting. Also inverts mm sense, which means you must + // enter a virtual memory user state to access sealed keys. + cu.protect(); } pub fn boot_or_die() -> ! { - seal_boot1_keys(); // 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( @@ -29,14 +30,30 @@ pub fn boot_or_die() -> ! { bao1x_api::BOOT1_START as *const u32, bao1x_api::BOOT1_REVOCATION_OFFSET, &ALLOWED_FUNCTIONS, - true, + false, None, ) { - Ok((k, tag)) => crate::println!( - "**should be unreachable** Booted with key {}({})", - k, - core::str::from_utf8(&tag).unwrap_or("invalid tag") - ), + Ok((k, tag)) => { + crate::println!( + "Booting with key {}({})", + k, + core::str::from_utf8(&tag).unwrap_or("invalid tag") + ); + // 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. + if tag == *bao1x_api::pubkeys::KEYSLOT_INITIAL_TAGS[bao1x_api::pubkeys::DEVELOPER_KEY_SLOT] + || k == bao1x_api::pubkeys::DEVELOPER_KEY_SLOT + { + crate::println!("Developer key detected, erasing secrets (broken in alpha-0 release)"); + bao1x_hal::sigcheck::erase_secrets(); + } + // this has to be called *after* erase_secrets, because we can't erase the secrets + // once the mappings have been sealed off. This is why we can't use the auto-jump method + // like we do in boot0. + seal_boot1_keys(); + bao1x_hal::sigcheck::jump_to(bao1x_api::LOADER_START); + } Err(e) => crate::println!("Image did not validate: {:?}", e), } crate::println!("No valid loader or baremetal image found. Halting!");