patching test written

need to figure out how to compute if it passes; it of course, breaks
the checksum attached for CI purposes. Maybe it's just a matter
of reading back the data and patching it again -- works things even harder!

also, ran the manual test case on real hardware tonight, after 389 hours
of simulation-only development. happy to report that it worked as expected,
so there's no huge obvious gaps between simulation and reality.
This commit is contained in:
bunnie 2021-11-23 04:36:58 +08:00
parent d716fea17a
commit 9c54152f4d
5 changed files with 140 additions and 74 deletions

View file

@ -317,6 +317,22 @@ impl BasisCache {
}
dict_set
}
pub(crate) fn key_list(&mut self, hw: &mut PddbOs, dict: &str) -> Result<HashSet::<String>> {
let mut merge_list = HashSet::<String>::new();
let mut found_dict = false;
for basis in self.cache.iter_mut() {
basis.populate_caches(hw);
if let Some(dcache) = basis.dicts.get_mut(dict) {
dcache.key_list(hw, &basis.v2p_map, &basis.cipher, &mut merge_list);
found_dict = true;
}
}
if found_dict {
Ok(merge_list)
} else {
return Err(Error::new(ErrorKind::NotFound, "dictionary not found"))
}
}
/// This version of the call only removes one instance of a dictionary from the specified basis.
/// Perhaps there also needs to be a `dict_remove_all` call which iterates through every basis
@ -502,7 +518,7 @@ impl BasisCache {
// index exists, see if the page exists
let key_index = (((kcache.start - SMALL_POOL_START as u64) - (dict_entry.index as u64 * SMALL_POOL_STRIDE as u64)) / SMALL_CAPACITY as u64) as usize;
if dict_entry.small_pool.len() > key_index {
log::info!("resoved key index {}, small pool len: {}", key_index, dict_entry.small_pool.len());
log::info!("resolved key index {}, small pool len: {}", key_index, dict_entry.small_pool.len());
// see if the pool's address exists in the page table
let pool_vaddr = VirtAddr::new(dict_entry.index as u64 * SMALL_POOL_STRIDE + SMALL_POOL_START).unwrap();
if !basis.v2p_map.contains_key(&pool_vaddr) {

View file

@ -5,7 +5,7 @@ use std::num::NonZeroU32;
use core::ops::{Deref, DerefMut};
use core::mem::size_of;
use aes_gcm_siv::Aes256GcmSiv;
use std::collections::{HashMap, BinaryHeap};
use std::collections::{HashMap, BinaryHeap, HashSet};
use std::io::{Result, Error, ErrorKind};
use bitfield::bitfield;
use std::cmp::Ordering;
@ -221,7 +221,7 @@ impl DictCacheEntry {
}
self.keys.insert(kname, kcache);
} else {
log::info!("fill: entry already present {}", kname);
log::trace!("fill: entry already present {}", kname);
}
key_count += 1;
}
@ -236,6 +236,17 @@ impl DictCacheEntry {
alloc_top
}
/// merges the list of keys in this dict cache entry into a merge_list.
/// The `merge_list` is used because keys are presented as a union across all open basis.
pub(crate) fn key_list(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::<VirtAddr, PhysPage>, cipher: &Aes256GcmSiv, merge_list: &mut HashSet<String>) {
// ensure that the key cache is filled
if self.keys.len() < self.key_count as usize {
self.fill(hw, v2p_map, cipher);
}
for key in self.keys.keys() {
merge_list.insert(key.to_string());
}
}
/// Simply ensures we have the description of a key in cache. Only tries to load small key data.
/// Required by meta-operations on the keys that operate only out of the cache.
/// This shares a lot of code with the fill() routine -- we should condense the common routines
@ -407,6 +418,8 @@ impl DictCacheEntry {
if let Some(_kcd) = &kcache.data {
unimplemented!("caching is not yet implemented for large data sets");
} else {
kcache.age = kcache.age.saturating_add(1);
kcache.clean = false;
// 1. handle unaligned start offsets
let mut written: usize = 0;
if ((kcache.start + offset as u64) % VPAGE_SIZE as u64) != 0 {
@ -417,7 +430,9 @@ impl DictCacheEntry {
*dst = src;
written += 1;
}
assert!((kcache.start + offset as u64 + written as u64) % VPAGE_SIZE as u64 == 0, "alignment algorithm failed");
if written < data.len() {
assert!((kcache.start + offset as u64 + written as u64) % VPAGE_SIZE as u64 == 0, "alignment algorithm failed");
}
hw.data_encrypt_and_patch_page(cipher, &self.aad, &mut pt_data, &pp);
}
// 2. do the rest

View file

@ -108,7 +108,7 @@ impl PddbOs {
let xns = xous_names::XousNames::new().unwrap();
#[cfg(any(target_os = "none", target_os = "xous"))]
let pddb = xous::syscall::map_memory(
xous::MemoryAddress::new(xous::PDDB_LOC as usize),
xous::MemoryAddress::new(xous::PDDB_LOC as usize + xous::FLASH_PHYS_BASE as usize),
None,
PDDB_A_LEN as usize,
xous::MemoryFlags::R,
@ -1064,12 +1064,12 @@ impl PddbOs {
/// in the PDDB an replace it with a brand-spanking new, blank PDDB.
/// The number of servers that can connect to the Spinor crate is strictly tracked, so we borrow a reference
/// to the Spinor object allocated to the PDDB implementation for this operation.
pub(crate) fn pddb_format(&mut self) -> Result<()> {
pub(crate) fn pddb_format(&mut self, fast: bool) -> Result<()> {
if !self.rootkeys.is_initialized().unwrap() {
return Err(Error::new(ErrorKind::Unsupported, "Root keys are not initialized; cannot format a PDDB without root keys!"));
}
// step 1. Erase the entire PDDB region - leaves the state in all 1's
{
if !fast {
log::info!("Erasing the PDDB region");
let blank_sector: [u8; PAGE_SIZE] = [0xff; PAGE_SIZE];
@ -1148,10 +1148,30 @@ impl PddbOs {
// step 5. salt the free space with random numbers. this can take a while, we might need a "progress report" of some kind...
// this is coded using "direct disk" offsets...under the assumption that we only ever really want to do this here, and
// not re-use this routine elsewhere.
let blank = [0xffu8; aes::BLOCK_SIZE];
for offset in (self.data_phys_base.as_usize()..PDDB_A_LEN).step_by(PAGE_SIZE) {
if fast {
// we could "skip" pages already encrypted to an old key as a short cut -- because we nuked our
// session key, previous data should be undecipherable. You shouldn't do this for a production erase
// but this is good for speeding up testing.
let mut is_blank = true;
let block: &[u8] = &self.pddb_mr.as_slice()[offset + aes::BLOCK_SIZE * 3..offset + aes::BLOCK_SIZE * 4];
for (&a, &b) in block.iter().zip(blank.iter()) {
if a != b {
is_blank = false;
break;
}
}
if !is_blank {
if (offset / PAGE_SIZE) % 16 == 0 {
log::info!("Page at {} is likely to already have cryptographic data, skipping...", offset);
}
continue;
}
}
self.entropy.borrow_mut().get_slice(&mut temp);
if (offset / PAGE_SIZE) % 64 == 0 {
log::info!("Crytpographic 'erase': {}/{}", offset, PDDB_A_LEN);
log::info!("Cryptographic 'erase': {}/{}", offset, PDDB_A_LEN);
}
self.spinor.patch(
self.pddb_mr.as_slice(),

View file

@ -340,19 +340,7 @@ use std::rc::Rc;
/// ...
/// 0x0008_5000 | data_phys_base - start of basis + dictionary + key data region
/*
/// This constant maps onto a region that's "unused" by Xous, and claimable by user
/// processes.
const PDDB_BACKING_BASE: usize = 0x8000_0000;
/// This designates the largest contiguous extent that we could allocate for a file.
/// The backing isn't allocated -- it is merely reserved. Accesses to the backing trigger
/// page faults that are handled by the PDDB, and just the pages actively being dereferenced is
/// swapped into physical memory on demand. This does put a pretty hard upper limit on file sizes
/// on a 32-bit system, but PDDB is coded such that we could extend to a 64-bit system and
/// increase this limit my changing the constants here.
const PDDB_BACKING_SIZE: usize = 0x4000_0000;
*/
// historical note: 389 hours, 11 mins elapsed since the start of the PDDB coding, and the first attempt at a hardware test -- as evidenced by the uptime of the hardware validation unit.
#[xous::xous_main]
fn xmain() -> ! {
@ -384,8 +372,12 @@ fn xmain() -> ! {
delete_add_dict_consistency(&mut pddb_os, &mut basis_cache, None, None, None);
log::info!("Saving `dacheck` to local host");
pddb_os.dbg_dump(Some("dacheck".to_string()));
log::info!("Doing patch test");
patch_test(&mut pddb_os, &mut basis_cache, None, None);
pddb_os.dbg_dump(Some("patch".to_string()));
log::info!("CI done");
}
log::info!("CI done");
/*
{ // a simple case that could be run directly on the hardware
log::info!("Running `manual` test case");
@ -472,3 +464,61 @@ fn xmain() -> ! {
log::trace!("quitting");
xous::terminate_process(0)
}
#[allow(dead_code)]
pub(crate) fn manual_testcase(hw: &mut PddbOs) {
log::info!("Initializing disk...");
hw.pddb_format(true).unwrap();
log::info!("Done initializing disk");
// it's a vector because order is important: by default access to keys/dicts go into the latest entry first, and then recurse to the earliest
let mut basis_cache = BasisCache::new();
log::info!("Attempting to mount the PDDB");
if let Some(sys_basis) = hw.pddb_mount() {
log::info!("PDDB mount operation finished successfully");
basis_cache.basis_add(sys_basis);
} else {
log::info!("PDDB did not mount; did you remember to format the PDDB region?");
}
log::info!("size of vpage: {}", VPAGE_SIZE);
// add a "system settings" dictionary to the default basis
log::info!("adding 'system settings' dictionary");
basis_cache.dict_add(hw, "system settings", None).expect("couldn't add system settings dictionary");
basis_cache.key_update(hw, "system settings", "wifi/wpa_keys/Kosagi", "my_wpa_key_here".as_bytes(), None, None, None, false).expect("couldn't add a key");
let mut readback = [0u8; 15];
match basis_cache.key_read(hw, "system settings", "wifi/wpa_keys/Kosagi", &mut readback, None, None) {
Ok(readsize) => {
log::info!("read back {} bytes", readsize);
log::info!("read data: {}", String::from_utf8_lossy(&readback));
},
Err(e) => {
log::info!("couldn't read data: {:?}", e);
}
}
basis_cache.key_update(hw, "system settings", "wifi/wpa_keys/e4200", "12345678".as_bytes(), None, None, None, false).expect("couldn't add a key");
// add a "big" key
let mut bigdata = [0u8; 5000];
for (i, d) in bigdata.iter_mut().enumerate() {
*d = i as u8;
}
basis_cache.key_update(hw, "system settings", "big_pool1", &bigdata, None, None, None, false).expect("couldn't add a key");
basis_cache.dict_add(hw, "test_dict_2", None).expect("couldn't add test dictionary 2");
basis_cache.key_update(hw, "test_dict_2", "test key in dict 2", "some data".as_bytes(), None, Some(128), None, false).expect("couldn't add a key to second dict");
basis_cache.key_update(hw, "system settings", "wifi/wpa_keys/e4200", "ABC".as_bytes(), Some(2), None, None, false).expect("couldn't update e4200 key");
log::info!("test readback of wifi/wpa_keys/e4200");
match basis_cache.key_read(hw, "system settings", "wifi/wpa_keys/e4200", &mut readback, None, None) {
Ok(readsize) => {
log::info!("read back {} bytes", readsize);
log::info!("read data: {}", String::from_utf8_lossy(&readback));
},
Err(e) => {
log::info!("couldn't read data: {:?}", e);
}
}
}

View file

@ -34,7 +34,7 @@ fn gen_key(dictname: &str, keynum: usize, lower_size_bound: usize, upper_size_bo
pub(crate) fn create_basis_testcase(hw: &mut PddbOs, basis_cache: &mut BasisCache,
maybe_num_dicts: Option<usize>, maybe_num_keys: Option<usize>, maybe_key_sizes: Option<(usize, usize)>) {
hw.pddb_format().unwrap();
hw.pddb_format(false).unwrap();
let sys_basis = hw.pddb_mount().expect("couldn't mount system basis");
basis_cache.basis_add(sys_basis);
@ -90,60 +90,25 @@ pub(crate) fn delete_add_dict_consistency(hw: &mut PddbOs, basis_cache: &mut Bas
}
}
#[allow(dead_code)]
pub(crate) fn manual_testcase(hw: &mut PddbOs) {
log::info!("Initializing disk...");
hw.pddb_format().unwrap();
log::info!("Done initializing disk");
/// patch data check
pub(crate) fn patch_test(hw: &mut PddbOs, basis_cache: &mut BasisCache,
maybe_patch_offset: Option<usize>, maybe_patch_data: Option<String>) {
// it's a vector because order is important: by default access to keys/dicts go into the latest entry first, and then recurse to the earliest
let mut basis_cache = BasisCache::new();
let patch_offset = maybe_patch_offset.unwrap_or(5);
let patch_data = maybe_patch_data.unwrap_or("patched!".to_string());
log::info!("Attempting to mount the PDDB");
if let Some(sys_basis) = hw.pddb_mount() {
log::info!("PDDB mount operation finished successfully");
basis_cache.basis_add(sys_basis);
} else {
log::info!("PDDB did not mount; did you remember to format the PDDB region?");
}
log::info!("size of vpage: {}", VPAGE_SIZE);
// add a "system settings" dictionary to the default basis
log::info!("adding 'system settings' dictionary");
basis_cache.dict_add(hw, "system settings", None).expect("couldn't add system settings dictionary");
basis_cache.key_update(hw, "system settings", "wifi/wpa_keys/Kosagi", "my_wpa_key_here".as_bytes(), None, None, None, false).expect("couldn't add a key");
let mut readback = [0u8; 15];
match basis_cache.key_read(hw, "system settings", "wifi/wpa_keys/Kosagi", &mut readback, None, None) {
Ok(readsize) => {
log::info!("read back {} bytes", readsize);
log::info!("read data: {}", String::from_utf8_lossy(&readback));
},
Err(e) => {
log::info!("couldn't read data: {:?}", e);
let dict_list = basis_cache.dict_list(hw);
for dict in dict_list.iter() {
if let Ok(key_list) = basis_cache.key_list(hw, dict) {
for key in key_list.iter() {
// this actually does something a bit more complicated on a multi-basis system than you'd think:
// it will get the union of all key names, and then patch the *latest open basis* only with new data.
// note: if the key didn't already exist in the latest open basis, it's added, with just that patch data in it.
// to override this behavior, you'd want to specify a _specific_ basis, but for testing purposes, we only have one
// so we're doing this way that would lead to surprising results if it were copied as template code elsewhere.
basis_cache.key_update(hw, dict, key, patch_data.as_bytes(), Some(patch_offset), None, None, false).unwrap();
}
}
}
basis_cache.key_update(hw, "system settings", "wifi/wpa_keys/e4200", "12345678".as_bytes(), None, None, None, false).expect("couldn't add a key");
// add a "big" key
let mut bigdata = [0u8; 5000];
for (i, d) in bigdata.iter_mut().enumerate() {
*d = i as u8;
}
basis_cache.key_update(hw, "system settings", "big_pool1", &bigdata, None, None, None, false).expect("couldn't add a key");
basis_cache.dict_add(hw, "test_dict_2", None).expect("couldn't add test dictionary 2");
basis_cache.key_update(hw, "test_dict_2", "test key in dict 2", "some data".as_bytes(), None, Some(128), None, false).expect("couldn't add a key to second dict");
basis_cache.key_update(hw, "system settings", "wifi/wpa_keys/e4200", "ABC".as_bytes(), Some(2), None, None, false).expect("couldn't update e4200 key");
log::info!("test readback of wifi/wpa_keys/e4200");
match basis_cache.key_read(hw, "system settings", "wifi/wpa_keys/e4200", &mut readback, None, None) {
Ok(readsize) => {
log::info!("read back {} bytes", readsize);
log::info!("read data: {}", String::from_utf8_lossy(&readback));
},
Err(e) => {
log::info!("couldn't read data: {:?}", e);
}
}
}