From f96c01634f5134fb85edc45fa33fb1721e6107ad Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 3 Feb 2026 18:12:18 +0800 Subject: [PATCH] streamline acram/slot manager access Reviewing the code, I was decomposing multi-element slots into individual slots, but erasing them as a multi-element slot. This results in unnecessary churn. The barrier to doing a simple check on the full slot all at once was a missing API for unchecked reads of the full slot: during checking, we want to be able to retrieve the contents even if technically the ACL access is denied (note that the ACL is enforced in *hardware*, the software based check of the ACL is just a "belt and suspenders" check). So, this patch adds a routine that allows for an `unsafe` retrieval of the slot data regardless of the ACL, where the safety condition is that the caller has to be aware that junk data could be passed to it if the ACL isn't correct. --- libs/bao1x-hal/src/acram.rs | 21 ++++ libs/bao1x-hal/src/sigcheck.rs | 172 ++++++++++++++++----------------- 2 files changed, 102 insertions(+), 91 deletions(-) diff --git a/libs/bao1x-hal/src/acram.rs b/libs/bao1x-hal/src/acram.rs index 68106a8a0..e65ff42ec 100644 --- a/libs/bao1x-hal/src/acram.rs +++ b/libs/bao1x-hal/src/acram.rs @@ -360,6 +360,27 @@ impl SlotManager { } } + /// Safety: may return garbage data if the ACLs aren't correct. No security is compromised + /// in the case that the ACL isn't correct, but the caller would not be notified of the + /// mismatch. This call is mostly useful for verifying that data is in fact being disregarded + /// or erased. + /// + /// SlotIndex is also assumed to be correct; if it is invalid, the function will panic. + pub unsafe fn read_unchecked(&self, slot: &SlotIndex) -> &[u8] { + let offset = slot.try_into_data_offset().unwrap(); + + // safety: the unsafes below remind us to check that all values are valid during + // the pointer type cast. In this case, there are no invalid values for a `u8`. + match slot { + SlotIndex::Data(_, _, _) => unsafe { + &self.data_range.as_slice()[offset..offset + SLOT_ELEMENT_LEN_BYTES] + }, + SlotIndex::DataRange(range, _, _) => unsafe { + &self.data_range.as_slice()[offset..offset + SLOT_ELEMENT_LEN_BYTES * range.len()] + }, + } + } + /// Safety: the caller must resolve the index into either the data or the key array correctly. /// There is access or type checking done. This is a "raw read" primitive mostly used for debugging. pub unsafe fn read_data_slot(&self, absolute_offset: usize) -> &[u8] { diff --git a/libs/bao1x-hal/src/sigcheck.rs b/libs/bao1x-hal/src/sigcheck.rs index 59469b25d..5f8599aa5 100644 --- a/libs/bao1x-hal/src/sigcheck.rs +++ b/libs/bao1x-hal/src/sigcheck.rs @@ -9,7 +9,7 @@ use bao1x_api::pubkeys::KEYSLOT_INITIAL_TAGS; use bao1x_api::pubkeys::SecurityConfiguration; use bao1x_api::signatures::*; #[cfg(not(feature = "std"))] -use bao1x_api::{DEVELOPER_MODE, DataSlotAccess, RwPerms, SLOT_ELEMENT_LEN_BYTES, SlotType}; +use bao1x_api::{DEVELOPER_MODE, DataSlotAccess, RwPerms, SLOT_ELEMENT_LEN_BYTES}; use digest::Digest; use sha2_bao1x::{Sha256, Sha512}; use xous::arch::PAGE_SIZE; @@ -318,37 +318,35 @@ pub fn erase_collateral(csprng: &mut Option<&mut Csprng>) -> Result<(), String> let mut rram = crate::rram::Reram::new(); let slot = &bao1x_api::offsets::COLLATERAL; - for data_index in slot.try_into_data_iter().unwrap() { - bollard!(die_no_std, 4); - csprng.as_deref_mut().map(|rng| rng.random_delay()); - // only clear ACL if it isn't already cleared - if slot_mgr - .get_acl(slot) - .unwrap_or(AccessSettings::Data(DataSlotAccess::new_with_raw_value(0xFFFF_FFFF))) - .raw_u32() - != 0 - { - // clear the ACL so we can operate on the data - slot_mgr - .set_acl(&mut rram, slot, &AccessSettings::Data(DataSlotAccess::new_with_raw_value(0))) - .expect("couldn't reset ACL"); - } - let bytes = unsafe { slot_mgr.read_data_slot(data_index) }; - // only erase if the key hasn't already been erased, to avoid stressing the RRAM array - // erase_secrets() may be called on every boot in some modes. - bollard!(die_no_std, 4); - if !bytes.iter().all(|&b| b == ERASE_VALUE) { - let mut eraser = alloc::vec::Vec::with_capacity(slot.len() * SLOT_ELEMENT_LEN_BYTES); - eraser.resize(slot.len() * SLOT_ELEMENT_LEN_BYTES, ERASE_VALUE); - - slot_mgr.write(&mut rram, slot, &eraser).ok(); - } - let check = unsafe { slot_mgr.read_data_slot(data_index) }; - if !check.iter().all(|&b| b == ERASE_VALUE) { - crate::println!("Failed to erase key at {}: {:x?}", data_index, check); - } - bollard!(die_no_std, 4); + bollard!(die_no_std, 4); + csprng.as_deref_mut().map(|rng| rng.random_delay()); + // only clear ACL if it isn't already cleared + if slot_mgr + .get_acl(slot) + .unwrap_or(AccessSettings::Data(DataSlotAccess::new_with_raw_value(0xFFFF_FFFF))) + .raw_u32() + != 0 + { + // clear the ACL so we can operate on the data + slot_mgr + .set_acl(&mut rram, slot, &AccessSettings::Data(DataSlotAccess::new_with_raw_value(0))) + .expect("couldn't reset ACL"); } + let bytes = unsafe { slot_mgr.read_unchecked(slot) }; + // only erase if the key hasn't already been erased, to avoid stressing the RRAM array + // erase_secrets() may be called on every boot in some modes. + bollard!(die_no_std, 4); + if !bytes.iter().all(|&b| b == ERASE_VALUE) { + let mut eraser = alloc::vec::Vec::with_capacity(slot.len() * SLOT_ELEMENT_LEN_BYTES); + eraser.resize(slot.len() * SLOT_ELEMENT_LEN_BYTES, ERASE_VALUE); + + slot_mgr.write(&mut rram, slot, &eraser).ok(); + } + let check = unsafe { slot_mgr.read_unchecked(slot) }; + if !check.iter().all(|&b| b == ERASE_VALUE) { + crate::println!("Failed to erase key at {:?}: {:x?}", slot, check); + } + bollard!(die_no_std, 4); Ok(()) } @@ -379,72 +377,64 @@ pub fn erase_secrets(csprng: &mut Option<&mut Csprng>) -> Result<(), String> { for slot in crate::board::KEY_SLOTS.iter() { bollard!(die_no_std, 4); csprng.as_deref_mut().map(|rng| rng.random_delay()); - if slot.get_type() == SlotType::Data { - let (_pa, rw_perms) = slot.get_access_spec(); - let mut erased_keys = 0; - for data_index in slot.try_into_data_iter().unwrap() { - bollard!(die_no_std, 4); - csprng.as_deref_mut().map(|rng| rng.random_delay()); - match rw_perms { - RwPerms::ReadWrite | RwPerms::WriteOnly => { - // only clear ACL if it isn't already cleared - if slot_mgr - .get_acl(slot) - .unwrap_or(AccessSettings::Data(DataSlotAccess::new_with_raw_value(0xFFFF_FFFF))) - .raw_u32() - != 0 - { - // clear the ACL so we can operate on the data - match slot_mgr.set_acl( - &mut rram, - slot, - &AccessSettings::Data(DataSlotAccess::new_with_raw_value(0)), - ) { - Ok(_) => (), - Err(e) => { - crate::println!("Couldn't erase ACL: {:?}", e); - } - } - } - let bytes = unsafe { slot_mgr.read_data_slot(data_index) }; - if bytes.iter().all(|&b| b == 0) { - zero_key_count += 1; - } - // only erase if the key hasn't already been erased, to avoid stressing the RRAM array - // erase_secrets() may be called on every boot in some modes. - bollard!(die_no_std, 4); - if !bytes.iter().all(|&b| b == ERASE_VALUE) { - let mut eraser = - alloc::vec::Vec::with_capacity(slot.len() * SLOT_ELEMENT_LEN_BYTES); - eraser.resize(slot.len() * SLOT_ELEMENT_LEN_BYTES, ERASE_VALUE); - - slot_mgr.write(&mut rram, slot, &eraser).ok(); - } - let check = unsafe { slot_mgr.read_data_slot(data_index) }; - if !check.iter().all(|&b| b == ERASE_VALUE) { - crate::println!("Failed to erase key at {}: {:x?}", data_index, check); - /* // commented out - can lead to boot loops - // reboot on failure to erase - let mut rcurst = - utralib::CSR::new(utralib::utra::sysctrl::HW_SYSCTRL_BASE as *mut u32); - rcurst.wo(utralib::utra::sysctrl::SFR_RCURST0, 0x55AA); - */ - } else { - erased_keys += 1; + let (_pa, rw_perms) = slot.get_access_spec(); + let mut erased_keys = 0; + csprng.as_deref_mut().map(|rng| rng.random_delay()); + match rw_perms { + RwPerms::ReadWrite | RwPerms::WriteOnly => { + // only clear ACL if it isn't already cleared + if slot_mgr + .get_acl(slot) + .unwrap_or(AccessSettings::Data(DataSlotAccess::new_with_raw_value(0xFFFF_FFFF))) + .raw_u32() + != 0 + { + // clear the ACL so we can operate on the data + match slot_mgr.set_acl( + &mut rram, + slot, + &AccessSettings::Data(DataSlotAccess::new_with_raw_value(0)), + ) { + Ok(_) => (), + Err(e) => { + crate::println!("Couldn't erase ACL: {:?}", e); } } - _ => {} } + // safety: this function knows to check or expect invalid ACL situations + let bytes = unsafe { slot_mgr.read_unchecked(slot) }; + if bytes.iter().all(|&b| b == 0) { + zero_key_count += 1; + } + // only erase if the key hasn't already been erased, to avoid stressing the RRAM array + // erase_secrets() may be called on every boot in some modes. bollard!(die_no_std, 4); + if !bytes.iter().all(|&b| b == ERASE_VALUE) { + let mut eraser = alloc::vec::Vec::with_capacity(slot.len() * SLOT_ELEMENT_LEN_BYTES); + eraser.resize(slot.len() * SLOT_ELEMENT_LEN_BYTES, ERASE_VALUE); + slot_mgr.write(&mut rram, slot, &eraser).ok(); + } + let check = unsafe { slot_mgr.read_unchecked(slot) }; + if !check.iter().all(|&b| b == ERASE_VALUE) { + crate::println!("Failed to erase key at {:?}: {:x?}", slot, check); + /* // commented out - can lead to boot loops + // reboot on failure to erase + let mut rcurst = + utralib::CSR::new(utralib::utra::sysctrl::HW_SYSCTRL_BASE as *mut u32); + rcurst.wo(utralib::utra::sysctrl::SFR_RCURST0, 0x55AA); + */ + } else { + erased_keys += 1; + } } - crate::println!( - "Key range at {}: {}/{} keys confirmed erased", - slot.get_base(), - erased_keys, - slot.len() - ); - bollard!(die_no_std, 4); + _ => {} } + crate::println!( + "Key range at {}: {}/{} keys confirmed erased", + slot.get_base(), + erased_keys, + slot.len() + ); bollard!(die_no_std, 4); } bollard!(die_no_std, 4);