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.
This commit is contained in:
bunnie 2026-02-03 18:12:18 +08:00
parent 7a55d6a669
commit f96c01634f
No known key found for this signature in database
2 changed files with 102 additions and 91 deletions

View file

@ -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. /// 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. /// 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] { pub unsafe fn read_data_slot(&self, absolute_offset: usize) -> &[u8] {

View file

@ -9,7 +9,7 @@ use bao1x_api::pubkeys::KEYSLOT_INITIAL_TAGS;
use bao1x_api::pubkeys::SecurityConfiguration; use bao1x_api::pubkeys::SecurityConfiguration;
use bao1x_api::signatures::*; use bao1x_api::signatures::*;
#[cfg(not(feature = "std"))] #[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 digest::Digest;
use sha2_bao1x::{Sha256, Sha512}; use sha2_bao1x::{Sha256, Sha512};
use xous::arch::PAGE_SIZE; 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 mut rram = crate::rram::Reram::new();
let slot = &bao1x_api::offsets::COLLATERAL; let slot = &bao1x_api::offsets::COLLATERAL;
for data_index in slot.try_into_data_iter().unwrap() { bollard!(die_no_std, 4);
bollard!(die_no_std, 4); csprng.as_deref_mut().map(|rng| rng.random_delay());
csprng.as_deref_mut().map(|rng| rng.random_delay()); // only clear ACL if it isn't already cleared
// only clear ACL if it isn't already cleared if slot_mgr
if slot_mgr .get_acl(slot)
.get_acl(slot) .unwrap_or(AccessSettings::Data(DataSlotAccess::new_with_raw_value(0xFFFF_FFFF)))
.unwrap_or(AccessSettings::Data(DataSlotAccess::new_with_raw_value(0xFFFF_FFFF))) .raw_u32()
.raw_u32() != 0
!= 0 {
{ // clear the ACL so we can operate on the data
// clear the ACL so we can operate on the data slot_mgr
slot_mgr .set_acl(&mut rram, slot, &AccessSettings::Data(DataSlotAccess::new_with_raw_value(0)))
.set_acl(&mut rram, slot, &AccessSettings::Data(DataSlotAccess::new_with_raw_value(0))) .expect("couldn't reset ACL");
.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);
} }
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(()) Ok(())
} }
@ -379,72 +377,64 @@ pub fn erase_secrets(csprng: &mut Option<&mut Csprng>) -> Result<(), String> {
for slot in crate::board::KEY_SLOTS.iter() { for slot in crate::board::KEY_SLOTS.iter() {
bollard!(die_no_std, 4); bollard!(die_no_std, 4);
csprng.as_deref_mut().map(|rng| rng.random_delay()); csprng.as_deref_mut().map(|rng| rng.random_delay());
if slot.get_type() == SlotType::Data { let (_pa, rw_perms) = slot.get_access_spec();
let (_pa, rw_perms) = slot.get_access_spec(); let mut erased_keys = 0;
let mut erased_keys = 0; csprng.as_deref_mut().map(|rng| rng.random_delay());
for data_index in slot.try_into_data_iter().unwrap() { match rw_perms {
bollard!(die_no_std, 4); RwPerms::ReadWrite | RwPerms::WriteOnly => {
csprng.as_deref_mut().map(|rng| rng.random_delay()); // only clear ACL if it isn't already cleared
match rw_perms { if slot_mgr
RwPerms::ReadWrite | RwPerms::WriteOnly => { .get_acl(slot)
// only clear ACL if it isn't already cleared .unwrap_or(AccessSettings::Data(DataSlotAccess::new_with_raw_value(0xFFFF_FFFF)))
if slot_mgr .raw_u32()
.get_acl(slot) != 0
.unwrap_or(AccessSettings::Data(DataSlotAccess::new_with_raw_value(0xFFFF_FFFF))) {
.raw_u32() // clear the ACL so we can operate on the data
!= 0 match slot_mgr.set_acl(
{ &mut rram,
// clear the ACL so we can operate on the data slot,
match slot_mgr.set_acl( &AccessSettings::Data(DataSlotAccess::new_with_raw_value(0)),
&mut rram, ) {
slot, Ok(_) => (),
&AccessSettings::Data(DataSlotAccess::new_with_raw_value(0)), Err(e) => {
) { crate::println!("Couldn't erase ACL: {:?}", e);
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;
} }
} }
_ => {}
} }
// 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); 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);
} }
bollard!(die_no_std, 4); bollard!(die_no_std, 4);