mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
add fscb/spaceupdate journal flushing & periodic flush
- fix journal number bug in basis root record - fix conflict PTE resolution to also consider basis root record, which has a different encryption form.
This commit is contained in:
parent
deecd3651d
commit
ac1f746566
5 changed files with 125 additions and 47 deletions
|
|
@ -204,6 +204,9 @@ pub(crate) enum Opcode {
|
||||||
/// Reset "don't ask to init root keys" flag - pass-through to root keys object - for end of OQC test
|
/// Reset "don't ask to init root keys" flag - pass-through to root keys object - for end of OQC test
|
||||||
ResetDontAskInit = 43,
|
ResetDontAskInit = 43,
|
||||||
|
|
||||||
|
/// Flush the SpaceUpdate log, restoring deniability
|
||||||
|
FlushSpaceUpdate = 44,
|
||||||
|
|
||||||
/// This key type could not be decoded
|
/// This key type could not be decoded
|
||||||
InvalidOpcode = u32::MAX as _,
|
InvalidOpcode = u32::MAX as _,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1601,6 +1601,7 @@ impl BasisCacheEntry {
|
||||||
pub(crate) fn basis_sync(&mut self, hw: &mut PddbOs) {
|
pub(crate) fn basis_sync(&mut self, hw: &mut PddbOs) {
|
||||||
self.last_sync = Some(hw.timestamp_now());
|
self.last_sync = Some(hw.timestamp_now());
|
||||||
if !self.clean {
|
if !self.clean {
|
||||||
|
self.age += 1;
|
||||||
let basis_root = BasisRoot {
|
let basis_root = BasisRoot {
|
||||||
magic: PDDB_MAGIC,
|
magic: PDDB_MAGIC,
|
||||||
version: PDDB_VERSION,
|
version: PDDB_VERSION,
|
||||||
|
|
@ -1611,18 +1612,19 @@ impl BasisCacheEntry {
|
||||||
let pp = self.v2p_map.get(&VirtAddr::new(1 * VPAGE_SIZE as u64).unwrap())
|
let pp = self.v2p_map.get(&VirtAddr::new(1 * VPAGE_SIZE as u64).unwrap())
|
||||||
.expect("Internal consistency error: Basis exists, but its root map was not allocated!");
|
.expect("Internal consistency error: Basis exists, but its root map was not allocated!");
|
||||||
assert!(pp.valid(), "basis page was invalid");
|
assert!(pp.valid(), "basis page was invalid");
|
||||||
|
log::debug!("{} before-sync journal: {}", self.name, self.journal);
|
||||||
let journal_bytes = self.journal.to_le_bytes(); // journal gets bumped by the patching function now
|
let journal_bytes = self.journal.to_le_bytes(); // journal gets bumped by the patching function now
|
||||||
let slice_iter =
|
let slice_iter =
|
||||||
journal_bytes.iter() // journal rev
|
journal_bytes.iter() // journal rev
|
||||||
.chain(basis_root.as_ref().iter());
|
.chain(basis_root.as_ref().iter());
|
||||||
let mut block = [0 as u8; KCOM_CT_LEN];
|
let mut block = [0 as u8; KCOM_CT_LEN];
|
||||||
for (&src, dst) in (hw.trng_u32() % JOURNAL_RAND_RANGE).to_le_bytes().iter().zip(block[..size_of::<JournalType>()].iter_mut()) {
|
|
||||||
*dst = src;
|
|
||||||
}
|
|
||||||
for (&src, dst) in slice_iter.zip(block.iter_mut()) {
|
for (&src, dst) in slice_iter.zip(block.iter_mut()) {
|
||||||
*dst = src;
|
*dst = src;
|
||||||
}
|
}
|
||||||
hw.data_encrypt_and_patch_page_with_commit(self.key.as_slice(), &self.aad, &mut block, &pp);
|
hw.data_encrypt_and_patch_page_with_commit(self.key.as_slice(), &self.aad, &mut block, &pp);
|
||||||
|
// read back the incremented journal state
|
||||||
|
self.journal = u32::from_le_bytes(block[..4].try_into().unwrap());
|
||||||
|
log::debug!("{} after-sync journal: {}", self.name, self.journal);
|
||||||
self.clean = true;
|
self.clean = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -499,6 +499,8 @@ impl PddbOs {
|
||||||
if let Some(prev_page) = map.get(&pte.vaddr()) {
|
if let Some(prev_page) = map.get(&pte.vaddr()) {
|
||||||
let cipher = AesGcmSiv::<Aes256>::new(Key::from_slice(data_key));
|
let cipher = AesGcmSiv::<Aes256>::new(Key::from_slice(data_key));
|
||||||
let aad = self.data_aad(basis_name);
|
let aad = self.data_aad(basis_name);
|
||||||
|
if pte.vaddr() != VirtAddr::new(VPAGE_SIZE as u64).unwrap() {
|
||||||
|
// handle case that there is a conflict in data areas
|
||||||
let prev_data = self.data_decrypt_page(&cipher, &aad, prev_page);
|
let prev_data = self.data_decrypt_page(&cipher, &aad, prev_page);
|
||||||
let new_data = self.data_decrypt_page(&cipher, &aad, &pp);
|
let new_data = self.data_decrypt_page(&cipher, &aad, &pp);
|
||||||
if let Some(new_d) = new_data {
|
if let Some(new_d) = new_data {
|
||||||
|
|
@ -517,6 +519,31 @@ impl PddbOs {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// new data is bogus, ignore it
|
// new data is bogus, ignore it
|
||||||
|
log::warn!("conflicting PTE found, but data is bogus: v{:x?}:p{:x?}", pte.vaddr(), prev_page);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// handle case that there is a conflict in root structure
|
||||||
|
let prev_data = self.data_decrypt_page_with_commit(data_key, &aad, prev_page);
|
||||||
|
let new_data = self.data_decrypt_page_with_commit(data_key, &aad, &pp);
|
||||||
|
if let Some(new_d) = new_data {
|
||||||
|
if let Some(prev_d) = prev_data {
|
||||||
|
let prev_j = JournalType::from_le_bytes(prev_d[..size_of::<JournalType>()].try_into().unwrap());
|
||||||
|
let new_j = JournalType::from_le_bytes(new_d[..size_of::<JournalType>()].try_into().unwrap());
|
||||||
|
if new_j > prev_j {
|
||||||
|
map.insert(pte.vaddr(), pp);
|
||||||
|
} else if new_j == prev_j {
|
||||||
|
log::error!("Found duplicate blocks with same journal age, picking arbitrary block and moving on...");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
self.resolve_pp_journal(&mut pp);
|
||||||
|
// prev data was bogus anyways, replace with the new entry
|
||||||
|
map.insert(pte.vaddr(), pp);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// new data is bogus, ignore it
|
||||||
|
// new data is bogus, ignore it
|
||||||
|
log::warn!("conflicting basis root found, but data is bogus: v{:x?}:p{:x?}", pte.vaddr(), prev_page);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.resolve_pp_journal(&mut pp);
|
self.resolve_pp_journal(&mut pp);
|
||||||
|
|
@ -759,8 +786,7 @@ impl PddbOs {
|
||||||
let ct_to_flash = ciphertext.deref();
|
let ct_to_flash = ciphertext.deref();
|
||||||
// determine which page we're going to write the ciphertext into
|
// determine which page we're going to write the ciphertext into
|
||||||
let page_search_limit = FSCB_PAGES - ((PageAlignedPa::from(ciphertext.len()).as_usize() / PAGE_SIZE) - 1);
|
let page_search_limit = FSCB_PAGES - ((PageAlignedPa::from(ciphertext.len()).as_usize() / PAGE_SIZE) - 1);
|
||||||
let dest_page = self.entropy.borrow_mut().get_u32() % page_search_limit as u32;
|
let dest_page_start = self.entropy.borrow_mut().get_u32() % page_search_limit as u32;
|
||||||
log::info!("picking random page {} out of {} pages for fscb", dest_page, page_search_limit);
|
|
||||||
// atomicity of the FreeSpace structure is a bit of a tough topic. It's a fairly hefty structure,
|
// atomicity of the FreeSpace structure is a bit of a tough topic. It's a fairly hefty structure,
|
||||||
// that runs a risk of corruption as it's being written, if power is lost or the system crashes.
|
// that runs a risk of corruption as it's being written, if power is lost or the system crashes.
|
||||||
// However, the guiding principle of this ordering is that it's better to have no FastSpace structure
|
// However, the guiding principle of this ordering is that it's better to have no FastSpace structure
|
||||||
|
|
@ -768,14 +794,27 @@ impl PddbOs {
|
||||||
// FastSpace structure + stale SpaceUpdates. In particular a stale SpaceUpdate would lead the system
|
// FastSpace structure + stale SpaceUpdates. In particular a stale SpaceUpdate would lead the system
|
||||||
// to conclude that some pages are free when they aren't. Thus, we prefer to completely erase the
|
// to conclude that some pages are free when they aren't. Thus, we prefer to completely erase the
|
||||||
// FSCB region before committing the updated version.
|
// FSCB region before committing the updated version.
|
||||||
|
let patch_data = [&nonce_array, ct_to_flash].concat();
|
||||||
|
assert!(patch_data.len() % PAGE_SIZE == 0); // should be guaranteed by design, if not, throw an error during early testing.
|
||||||
|
let dest_page_end = dest_page_start + (patch_data.len() / PAGE_SIZE) as u32;
|
||||||
|
log::info!("picking random pages [{}-{}) out of {} pages for fscb", dest_page_start, dest_page_end, page_search_limit);
|
||||||
{ // this is where we begin the "it would be bad if we lost power about now" code region
|
{ // this is where we begin the "it would be bad if we lost power about now" code region
|
||||||
// erase the entire fscb area
|
let blank_sector: [u8; PAGE_SIZE] = [0xff; PAGE_SIZE]; // prep a "blank" page for the loop
|
||||||
let blank_sector: [u8; PAGE_SIZE] = [0xff; PAGE_SIZE];
|
for offset in 0..FSCB_PAGES as u32 {
|
||||||
for offset in 0..FSCB_PAGES {
|
if offset >= dest_page_start && offset < dest_page_end {
|
||||||
self.patch_fscb(&blank_sector, (offset * PAGE_SIZE) as u32);
|
// patch the FSCB data in when we find it, and skip on later iterations that are within the page range of the FSCB
|
||||||
|
if offset == dest_page_start {
|
||||||
|
self.patch_fscb(&patch_data, dest_page_start * PAGE_SIZE as u32);
|
||||||
|
} else {
|
||||||
|
// skip, because we already patched it in the first page we hit
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// erase all the other pages in hte FSCB
|
||||||
|
self.patch_fscb(&blank_sector, offset * PAGE_SIZE as u32);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// commit the fscb data
|
// commit the fscb data
|
||||||
self.patch_fscb(&[&nonce_array, ct_to_flash].concat(), dest_page * PAGE_SIZE as u32);
|
log::info!("patch data len: {}", patch_data.len());
|
||||||
} // end "it would be bad if we lost power now" region
|
} // end "it would be bad if we lost power now" region
|
||||||
|
|
||||||
// note: this function should be followed up by a fast_space_read() to regenerate the temporary
|
// note: this function should be followed up by a fast_space_read() to regenerate the temporary
|
||||||
|
|
@ -953,7 +992,8 @@ impl PddbOs {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::warn!("FSCB data was found, but it did not decrypt correctly. Ignoring FSCB record. Error: {:?}", e)
|
log::warn!("FSCB data was found, but it did not decrypt correctly. Ignoring FSCB record. Error: {:?}", e);
|
||||||
|
log::info!("FSCB nonce: {:?}, aad: {:?}, len: {}, msg: {:?}", Nonce::from_slice(&fscb_slice[page_start..page_start + size_of::<Nonce>()]), &aad, fscb_buf.len(), &fscb_buf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -998,7 +1038,8 @@ impl PddbOs {
|
||||||
self.fspace_cache.replace(pp);
|
self.fspace_cache.replace(pp);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log::warn!("Strange...we have a journal entry for a free space page that isn't already in our cache. Guru meditation: {:?}", pp);
|
// this happens when the FSCB is flushed, and then a page is deleted.
|
||||||
|
log::debug!("Journal entry for a free space page that isn't already in our cache {:?}", pp);
|
||||||
self.fspace_cache.insert(pp);
|
self.fspace_cache.insert(pp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1249,6 +1290,13 @@ impl PddbOs {
|
||||||
// log regenration is faster & less intrusive than fastspace regeneration, and we would have
|
// log regenration is faster & less intrusive than fastspace regeneration, and we would have
|
||||||
// to do this more often. So we have a separate path for this outcome.
|
// to do this more often. So we have a separate path for this outcome.
|
||||||
log::warn!("FastSpace alloc forced by lack of log space");
|
log::warn!("FastSpace alloc forced by lack of log space");
|
||||||
|
self.fast_space_flush();
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// This is a "fast" flush that expires all the PDDB SpaceUpdate journal
|
||||||
|
pub(crate) fn fast_space_flush(&mut self) {
|
||||||
let mut fast_space = FastSpace {
|
let mut fast_space = FastSpace {
|
||||||
free_pool: [PhysPage(0); FASTSPACE_FREE_POOL_LEN],
|
free_pool: [PhysPage(0); FASTSPACE_FREE_POOL_LEN],
|
||||||
};
|
};
|
||||||
|
|
@ -1259,6 +1307,8 @@ impl PddbOs {
|
||||||
for (&src, dst) in self.fspace_cache.iter().zip(fast_space.free_pool.iter_mut()) {
|
for (&src, dst) in self.fspace_cache.iter().zip(fast_space.free_pool.iter_mut()) {
|
||||||
*dst = src;
|
*dst = src;
|
||||||
}
|
}
|
||||||
|
log::info!("SpaceUpdate flush with {} pages", fast_space.free_pool.len());
|
||||||
|
let start = self.timestamp_now();
|
||||||
// write just commits a new record to disk, but doesn't update our internal data cache
|
// write just commits a new record to disk, but doesn't update our internal data cache
|
||||||
// this also clears the fast space log.
|
// this also clears the fast space log.
|
||||||
self.fast_space_write(&fast_space);
|
self.fast_space_write(&fast_space);
|
||||||
|
|
@ -1266,9 +1316,7 @@ impl PddbOs {
|
||||||
self.fast_space_read();
|
self.fast_space_read();
|
||||||
// this will locate the next fast space log point.
|
// this will locate the next fast space log point.
|
||||||
self.fast_space_ensure_next_log();
|
self.fast_space_ensure_next_log();
|
||||||
true
|
log::info!("Flush took {}ms", self.timestamp_now() - start);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn data_aad(&self, name: &str) -> Vec::<u8> {
|
pub(crate) fn data_aad(&self, name: &str) -> Vec::<u8> {
|
||||||
|
|
@ -1398,11 +1446,12 @@ impl PddbOs {
|
||||||
/// `data` includes the journal entry on top.
|
/// `data` includes the journal entry on top.
|
||||||
/// The data passed in must be exactly one vpage plus the journal entry minus the length of the commit structure (64 bytes),
|
/// The data passed in must be exactly one vpage plus the journal entry minus the length of the commit structure (64 bytes),
|
||||||
/// which is 4004 bytes total
|
/// which is 4004 bytes total
|
||||||
|
/// This function increments the journal and re-nonces the structure.
|
||||||
pub(crate) fn data_encrypt_and_patch_page_with_commit(&self, key: &[u8], aad: &[u8], data: &mut [u8], pp: &PhysPage) {
|
pub(crate) fn data_encrypt_and_patch_page_with_commit(&self, key: &[u8], aad: &[u8], data: &mut [u8], pp: &PhysPage) {
|
||||||
assert!(data.len() == KCOM_CT_LEN, "did not get a key-commit sized region to patch");
|
assert!(data.len() == KCOM_CT_LEN, "did not get a key-commit sized region to patch");
|
||||||
// updates the journal type
|
// updates the journal type
|
||||||
let j = JournalType::from_le_bytes(data[..size_of::<JournalType>()].try_into().unwrap()).saturating_add(1);
|
let j = JournalType::from_le_bytes(data[..size_of::<JournalType>()].try_into().unwrap()).saturating_add(1);
|
||||||
for (&src, dst) in j.to_le_bytes().iter().zip(data[..size_of::<JournalType>()].iter_mut()) { *dst = src; }
|
data[..size_of::<JournalType>()].copy_from_slice(&j.to_le_bytes());
|
||||||
// gets the AES-GCM-SIV nonce
|
// gets the AES-GCM-SIV nonce
|
||||||
let nonce = self.nonce_gen();
|
let nonce = self.nonce_gen();
|
||||||
// makes a nonce for the key commit
|
// makes a nonce for the key commit
|
||||||
|
|
|
||||||
|
|
@ -635,6 +635,13 @@ impl Pddb {
|
||||||
Message::new_blocking_scalar(Opcode::ResetDontAskInit.to_usize().unwrap(), 0, 0, 0, 0)
|
Message::new_blocking_scalar(Opcode::ResetDontAskInit.to_usize().unwrap(), 0, 0, 0, 0)
|
||||||
).expect("couldn't send ResetDontAskInit");
|
).expect("couldn't send ResetDontAskInit");
|
||||||
}
|
}
|
||||||
|
/// Flush the `SpaceUpdate` journal. SpaceUpdates will leak the last couple hundred or so free space operations,
|
||||||
|
/// so periodically flushing this is needed to restore deniability.
|
||||||
|
pub fn flush_space_update(&self) {
|
||||||
|
send_message(self.conn,
|
||||||
|
Message::new_blocking_scalar(Opcode::FlushSpaceUpdate.to_usize().unwrap(), 0, 0, 0, 0)
|
||||||
|
).expect("couldn't send FlushSpaceUpdate");
|
||||||
|
}
|
||||||
/// Rekey the PDDB. This can be a very long-running blocking operation that will definitely.
|
/// Rekey the PDDB. This can be a very long-running blocking operation that will definitely.
|
||||||
/// interrupt normal user flow.
|
/// interrupt normal user flow.
|
||||||
pub fn rekey_pddb(&self, op: PddbRekeyOp) -> Result<()> {
|
pub fn rekey_pddb(&self, op: PddbRekeyOp) -> Result<()> {
|
||||||
|
|
|
||||||
|
|
@ -532,12 +532,25 @@ fn wrapped_main() -> ! {
|
||||||
let scrub_run = scrub_run.clone();
|
let scrub_run = scrub_run.clone();
|
||||||
move || {
|
move || {
|
||||||
let tt = ticktimer_server::Ticktimer::new().unwrap();
|
let tt = ticktimer_server::Ticktimer::new().unwrap();
|
||||||
|
let mut flush_interval = 0;
|
||||||
|
const ARBITRARY_INTERVAL_MS: usize = 12_513;
|
||||||
|
const PERIODIC_FLUSH_MS: usize = 86_400_000; // one day
|
||||||
loop {
|
loop {
|
||||||
tt.sleep_ms(12_513).unwrap(); // arbitrary interval, but trying to avoid "round" numbers of seconds to interleave periodic tasks
|
tt.sleep_ms(ARBITRARY_INTERVAL_MS).unwrap(); // arbitrary interval, but trying to avoid "round" numbers of seconds to interleave periodic tasks
|
||||||
|
flush_interval += ARBITRARY_INTERVAL_MS;
|
||||||
if scrub_run.load(Ordering::SeqCst) {
|
if scrub_run.load(Ordering::SeqCst) {
|
||||||
|
if flush_interval > PERIODIC_FLUSH_MS {
|
||||||
|
// this runs once a day, and skips the scrub request when it runs
|
||||||
|
flush_interval = 0;
|
||||||
|
send_message(my_cid,
|
||||||
|
Message::new_blocking_scalar(Opcode::FlushSpaceUpdate.to_usize().unwrap(), 0, 0, 0, 0)
|
||||||
|
).expect("couldn't send flush request");
|
||||||
|
} else {
|
||||||
|
// this is what actually runs every interval, to a first order
|
||||||
send_message(my_cid,
|
send_message(my_cid,
|
||||||
Message::new_scalar(Opcode::PeriodicScrub.to_usize().unwrap(), 0, 0, 0, 0)
|
Message::new_scalar(Opcode::PeriodicScrub.to_usize().unwrap(), 0, 0, 0, 0)
|
||||||
).expect("couldn't send mount request");
|
).expect("couldn't send scrub request");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1396,6 +1409,10 @@ fn wrapped_main() -> ! {
|
||||||
let result = basis_cache.rekey(&mut pddb_os, rekey_op);
|
let result = basis_cache.rekey(&mut pddb_os, rekey_op);
|
||||||
buffer.replace(result).unwrap();
|
buffer.replace(result).unwrap();
|
||||||
}
|
}
|
||||||
|
Opcode::FlushSpaceUpdate => {
|
||||||
|
pddb_os.fast_space_flush();
|
||||||
|
xous::return_scalar(msg.sender, 1).ok();
|
||||||
|
}
|
||||||
Opcode::ResetDontAskInit => {
|
Opcode::ResetDontAskInit => {
|
||||||
pddb_os.reset_dont_ask_init();
|
pddb_os.reset_dont_ask_init();
|
||||||
xous::return_scalar(msg.sender, 1).ok();
|
xous::return_scalar(msg.sender, 1).ok();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue