mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-20 09:03:28 +00:00
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:
parent
7a55d6a669
commit
f96c01634f
2 changed files with 102 additions and 91 deletions
|
|
@ -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] {
|
||||||
|
|
|
||||||
|
|
@ -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,7 +318,6 @@ 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
|
||||||
|
|
@ -333,7 +332,7 @@ pub fn erase_collateral(csprng: &mut Option<&mut Csprng>) -> Result<(), String>
|
||||||
.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) };
|
let bytes = unsafe { slot_mgr.read_unchecked(slot) };
|
||||||
// only erase if the key hasn't already been erased, to avoid stressing the RRAM array
|
// 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.
|
// erase_secrets() may be called on every boot in some modes.
|
||||||
bollard!(die_no_std, 4);
|
bollard!(die_no_std, 4);
|
||||||
|
|
@ -343,12 +342,11 @@ pub fn erase_collateral(csprng: &mut Option<&mut Csprng>) -> Result<(), String>
|
||||||
|
|
||||||
slot_mgr.write(&mut rram, slot, &eraser).ok();
|
slot_mgr.write(&mut rram, slot, &eraser).ok();
|
||||||
}
|
}
|
||||||
let check = unsafe { slot_mgr.read_data_slot(data_index) };
|
let check = unsafe { slot_mgr.read_unchecked(slot) };
|
||||||
if !check.iter().all(|&b| b == ERASE_VALUE) {
|
if !check.iter().all(|&b| b == ERASE_VALUE) {
|
||||||
crate::println!("Failed to erase key at {}: {:x?}", data_index, check);
|
crate::println!("Failed to erase key at {:?}: {:x?}", slot, check);
|
||||||
}
|
}
|
||||||
bollard!(die_no_std, 4);
|
bollard!(die_no_std, 4);
|
||||||
}
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -379,11 +377,8 @@ 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;
|
||||||
for data_index in slot.try_into_data_iter().unwrap() {
|
|
||||||
bollard!(die_no_std, 4);
|
|
||||||
csprng.as_deref_mut().map(|rng| rng.random_delay());
|
csprng.as_deref_mut().map(|rng| rng.random_delay());
|
||||||
match rw_perms {
|
match rw_perms {
|
||||||
RwPerms::ReadWrite | RwPerms::WriteOnly => {
|
RwPerms::ReadWrite | RwPerms::WriteOnly => {
|
||||||
|
|
@ -406,7 +401,8 @@ pub fn erase_secrets(csprng: &mut Option<&mut Csprng>) -> Result<(), String> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let bytes = unsafe { slot_mgr.read_data_slot(data_index) };
|
// 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) {
|
if bytes.iter().all(|&b| b == 0) {
|
||||||
zero_key_count += 1;
|
zero_key_count += 1;
|
||||||
}
|
}
|
||||||
|
|
@ -414,15 +410,13 @@ pub fn erase_secrets(csprng: &mut Option<&mut Csprng>) -> Result<(), String> {
|
||||||
// erase_secrets() may be called on every boot in some modes.
|
// 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) {
|
if !bytes.iter().all(|&b| b == ERASE_VALUE) {
|
||||||
let mut eraser =
|
let mut eraser = alloc::vec::Vec::with_capacity(slot.len() * SLOT_ELEMENT_LEN_BYTES);
|
||||||
alloc::vec::Vec::with_capacity(slot.len() * SLOT_ELEMENT_LEN_BYTES);
|
|
||||||
eraser.resize(slot.len() * SLOT_ELEMENT_LEN_BYTES, ERASE_VALUE);
|
eraser.resize(slot.len() * SLOT_ELEMENT_LEN_BYTES, ERASE_VALUE);
|
||||||
|
|
||||||
slot_mgr.write(&mut rram, slot, &eraser).ok();
|
slot_mgr.write(&mut rram, slot, &eraser).ok();
|
||||||
}
|
}
|
||||||
let check = unsafe { slot_mgr.read_data_slot(data_index) };
|
let check = unsafe { slot_mgr.read_unchecked(slot) };
|
||||||
if !check.iter().all(|&b| b == ERASE_VALUE) {
|
if !check.iter().all(|&b| b == ERASE_VALUE) {
|
||||||
crate::println!("Failed to erase key at {}: {:x?}", data_index, check);
|
crate::println!("Failed to erase key at {:?}: {:x?}", slot, check);
|
||||||
/* // commented out - can lead to boot loops
|
/* // commented out - can lead to boot loops
|
||||||
// reboot on failure to erase
|
// reboot on failure to erase
|
||||||
let mut rcurst =
|
let mut rcurst =
|
||||||
|
|
@ -435,8 +429,6 @@ pub fn erase_secrets(csprng: &mut Option<&mut Csprng>) -> Result<(), String> {
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
bollard!(die_no_std, 4);
|
|
||||||
}
|
|
||||||
crate::println!(
|
crate::println!(
|
||||||
"Key range at {}: {}/{} keys confirmed erased",
|
"Key range at {}: {}/{} keys confirmed erased",
|
||||||
slot.get_base(),
|
slot.get_base(),
|
||||||
|
|
@ -446,8 +438,6 @@ pub fn erase_secrets(csprng: &mut Option<&mut Csprng>) -> Result<(), String> {
|
||||||
bollard!(die_no_std, 4);
|
bollard!(die_no_std, 4);
|
||||||
}
|
}
|
||||||
bollard!(die_no_std, 4);
|
bollard!(die_no_std, 4);
|
||||||
}
|
|
||||||
bollard!(die_no_std, 4);
|
|
||||||
|
|
||||||
// store the proof that the key array was erased - could lead to disclosure of one key,
|
// store the proof that the key array was erased - could lead to disclosure of one key,
|
||||||
// but we also can't simply trust that the oneway counter below is accurate
|
// but we also can't simply trust that the oneway counter below is accurate
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue