diff --git a/services/pddb/src/api.rs b/services/pddb/src/api.rs index d2361c90b..77cea8b74 100644 --- a/services/pddb/src/api.rs +++ b/services/pddb/src/api.rs @@ -39,8 +39,8 @@ pub(crate) const PDDB_VERSION: u32 = 0x00_00_02_01; #[allow(dead_code)] // PDDB_A_LEN may be shorter than xous::PDDB_LEN, to speed up testing. #[allow(dead_code)] -pub(crate) const PDDB_A_LEN: usize = xous::PDDB_LEN as usize; -// pub(crate) const PDDB_A_LEN: usize = 4 * 1024 * 1024; +// pub(crate) const PDDB_A_LEN: usize = xous::PDDB_LEN as usize; +pub const PDDB_A_LEN: usize = 4 * 1024 * 1024; /// range for the starting point of a journal number, picked from a random seed /// the goal is to reduce info leakage about the age of structures relative to each other diff --git a/services/pddb/src/backend/basis.rs b/services/pddb/src/backend/basis.rs index 1be8cff21..62a6fe862 100644 --- a/services/pddb/src/backend/basis.rs +++ b/services/pddb/src/backend/basis.rs @@ -801,7 +801,7 @@ impl BasisCache { pub(crate) fn basis_unlock(&mut self, hw: &mut PddbOs, name: &str, password: &str, policy: BasisRetentionPolicy) -> Option { let basis_key = hw.basis_derive_key(name, password); - if let Some(basis_map) = hw.pt_scan_key(&basis_key.pt, name) { + if let Some(basis_map) = hw.pt_scan_key(&basis_key.pt, &basis_key.data, name) { let aad = hw.data_aad(name); if let Some(root_page) = basis_map.get(&VirtAddr::new(VPAGE_SIZE as u64).unwrap()) { let vpage = match hw.data_decrypt_page_with_commit(&basis_key.data, &aad, root_page) { @@ -1090,7 +1090,11 @@ pub(crate) struct BasisCacheEntry { pub cipher: AesGcmSiv::, /// derived cipher for encrypting PTEs -- cache it, so we can save the time cost of constructing the cipher key schedule pub cipher_ecb: Aes256, - /// raw AES key -- needed because we have to use this to derive commitment keys for the basis root record, to work around AES-GCM-SIV salamanders + /// raw AES page table key -- needed because we have to do a low-level PT scan to generate FSCB, and sometimes the key comes from + /// a copy cached here, or from one derived solely for the FSCB scan. There is no way to copy an Aes256 record, so, we include + /// the raw key because we can copy that. :P so much for semantics. + pub pt_key: GenericArray, + /// raw AES data key -- needed because we have to use this to derive commitment keys for the basis root record, to work around AES-GCM-SIV salamanders pub key: GenericArray, /// the AAD associated with this Basis pub aad: Vec::, @@ -1113,7 +1117,7 @@ impl BasisCacheEntry { /// If it `lazy` is false, it will populate the dictionary cache and key cache entries, as well as /// discover the location of the `large_alloc_ptr`. pub(crate) fn mount(hw: &mut PddbOs, name: &str, key: &BasisKeys, lazy: bool, policy: BasisRetentionPolicy) -> Option { - if let Some(basis_map) = hw.pt_scan_key(&key.pt, name) { + if let Some(basis_map) = hw.pt_scan_key(&key.pt, &key.data, name) { let cipher = AesGcmSiv::::new(Key::from_slice(&key.data)); let aad = hw.data_aad(name); // get the first page, where the basis root is guaranteed to be @@ -1149,6 +1153,7 @@ impl BasisCacheEntry { dicts: HashMap::::new(), cipher, cipher_ecb: Aes256::new(GenericArray::from_slice(&key.pt)), + pt_key: GenericArray::clone_from_slice(&key.pt), key: GenericArray::clone_from_slice(&key.data), aad, age: basis_root.age, diff --git a/services/pddb/src/backend/hw.rs b/services/pddb/src/backend/hw.rs index 581f24272..6583622db 100644 --- a/services/pddb/src/backend/hw.rs +++ b/services/pddb/src/backend/hw.rs @@ -444,8 +444,8 @@ impl PddbOs { /// that also maps to the imposter), the imposter is effectively de-allocated because the FSCB report is inserted directly /// into the page table, overwriting the impostor entry in the paget able. On the next mount, this turns into the "trivial conflict" /// case, where one page will validate and the other will not. - pub(crate) fn pt_scan_key(&self, key: &[u8; AES_KEYSIZE], basis_name: &str) -> Option> { - let cipher = Aes256::new(&GenericArray::from_slice(key)); + pub(crate) fn pt_scan_key(&self, pt_key: &[u8; AES_KEYSIZE], data_key: &[u8; AES_KEYSIZE], basis_name: &str) -> Option> { + let cipher = Aes256::new(&GenericArray::from_slice(pt_key)); let pt = self.pt_as_slice(); let mut map = HashMap::::new(); let blank = [0xffu8; aes::BLOCK_SIZE]; @@ -473,7 +473,7 @@ impl PddbOs { pp.set_space_state(SpaceState::Used); // handle conflicting journal versions here if let Some(prev_page) = map.get(&pte.vaddr()) { - let cipher = AesGcmSiv::::new(Key::from_slice(key)); + let cipher = AesGcmSiv::::new(Key::from_slice(data_key)); let aad = self.data_aad(basis_name); let prev_data = self.data_decrypt_page(&cipher, &aad, prev_page); let new_data = self.data_decrypt_page(&cipher, &aad, &pp); @@ -1400,7 +1400,7 @@ impl PddbOs { self.fast_space_read(); self.syskey_ensure(); if let Some(syskey) = self.system_basis_key.take() { - if let Some(sysbasis_map) = self.pt_scan_key(&syskey.pt, PDDB_DEFAULT_SYSTEM_BASIS) { + if let Some(sysbasis_map) = self.pt_scan_key(&syskey.pt, &syskey.data, PDDB_DEFAULT_SYSTEM_BASIS) { let aad = self.data_aad(PDDB_DEFAULT_SYSTEM_BASIS); // get the first page, where the basis root is guaranteed to be if let Some(root_page) = sysbasis_map.get(&VirtAddr::new(VPAGE_SIZE as u64).unwrap()) { @@ -1767,14 +1767,26 @@ impl PddbOs { pub(crate) fn pddb_generate_used_map(&self, cache: &Vec::) -> Option>> { if let Some(all_keys) = self.pddb_get_all_keys(&cache) { let mut page_heap = BinaryHeap::new(); - for (key, name) in all_keys { + for (basis_keys, name) in all_keys { // scan the disclosed bases - if let Some(map) = self.pt_scan_key(&key, &name) { + if let Some(map) = self.pt_scan_key(&basis_keys.pt, &basis_keys.data, &name) { for pp in map.values() { page_heap.push(Reverse(pp.page_number())) } + } else { + log::warn!("pt_scan for basis {} failed, data may be lost", name); } } + + + // need to incorporate all of the FSCB knowledge into the scan as well, because there could be + // cached pages inside "live" Basis that have not been committed to disk. However, it *should* be + // the case that any cached allocations came out of the previous FSCB pool, therefore, by merging + // in the "used list" from the existing FSCB we should be able to generate an accurate + // representation of the actually used pages + + + Some(page_heap) } else { None @@ -1788,20 +1800,13 @@ impl PddbOs { /// a Vec of keys & names, not a BasisCacheEntry -- so it means that the Basis still are "closed" /// at the conclusion of the sweep, but their page use can be accounted for. #[cfg(not(all(feature="pddbtest", feature="autobasis")))] - pub(crate) fn pddb_get_all_keys(&self, cache: &Vec::) -> Option> { + pub(crate) fn pddb_get_all_keys(&self, cache: &Vec::) -> Option> { const SWAP_DELAY_MS: usize = 300; - let mut ret = Vec::<([u8; AES_KEYSIZE], String)>::new(); + // populate the "known" entries + let mut ret = Vec::<(BasisKeys, String)>::new(); for entry in cache { - ret.push((entry.key.into(), entry.name.to_string())); + ret.push((BasisKeys { pt: entry.pt_key.into(), data: entry.key.into() }, entry.name.to_string())); } - /* - if let Some(sys_key) = &self.system_basis_key { - ret.push((sys_key.pt, PDDB_DEFAULT_SYSTEM_BASIS.to_string())); - } else { - log::error!("No default system basis was set up, this should be an illegal condition when requesting FSCB scan!"); - panic!("No default system basis was set up, this should be an illegal condition when requesting FSCB scan!"); - } - */ log::info!("{} basis are open, with the following names:", cache.len()); for entry in cache { log::info!(" - {}", entry.name); @@ -1846,14 +1851,16 @@ impl PddbOs { &name, retpass.plaintext_pw.unwrap().as_str().unwrap() ); + // validate the password by finding the root block of the basis. We rely entirely + // upon the AEAD with key commit to ensure the password is correct. let maybe_entry = if let Some(basis_map) = - self.pt_scan_key(&basis_key.pt, &name) { + self.pt_scan_key(&basis_key.pt, &basis_key.data, &name) { let aad = self.data_aad(&name); if let Some(root_page) = basis_map.get(&VirtAddr::new(VPAGE_SIZE as u64).unwrap()) { match self.data_decrypt_page_with_commit(&basis_key.data, &aad, root_page) { Some(_data) => { // if the root page decrypts, we accept the password; no further checking is done. - Some((basis_key.pt, name.to_string())) + Some((basis_key, name.to_string())) }, None => None, } @@ -1864,8 +1871,8 @@ impl PddbOs { None }; // 3. add to the Aes256 return vec - if let Some((key, name)) = maybe_entry { - ret.push((key, name)); + if let Some((basis_key, name)) = maybe_entry { + ret.push((basis_key, name)); } else { modals.show_notification(t!("pddb.freespace.badpass", xous::LANG), None).ok(); self.tt.sleep_ms(SWAP_DELAY_MS).unwrap(); @@ -2041,11 +2048,11 @@ impl PddbOs { } /// In testing, we want a way to automatically unlock all known test Bases. This stand-in feature automates that. #[cfg(all(feature="pddbtest", feature="autobasis"))] - pub(crate) fn pddb_get_all_keys(&self, cache: &Vec::) -> Option> { + pub(crate) fn pddb_get_all_keys<'a>(&'a self, cache: &'a Vec::) -> Option> { // populate the "known" entries - let mut ret = Vec::<([u8; AES_KEYSIZE], String)>::new(); + let mut ret = Vec::<(BasisKeys, String)>::new(); for entry in cache { - ret.push((entry.key.into(), entry.name.to_string())); + ret.push((BasisKeys { pt: entry.pt_key.into(), data: entry.key.into() }, entry.name.to_string())); } // now iterate through the bases that have been enumerated so far, and populate the missing ones. @@ -2053,7 +2060,7 @@ impl PddbOs { // if the entry isn't known already, add it // yah, this is O(n^2) awful. yolo! let mut exists = false; - for (_key, bname) in ret.iter() { + for (_bk, bname) in ret.iter() { if bname == name { exists = true; break; @@ -2064,10 +2071,13 @@ impl PddbOs { name, name ); - ret.push((basis_key.pt, name.to_string())); + ret.push((basis_key, name.to_string())); } } - log::info!("for FSCB testing, we auto-unlocked {} bases", ret.len()); + log::info!("for FSCB testing, we auto-unlocked {} bases:", ret.len()); + for (_bk, entry) in ret.iter() { + log::info!(" - {}", entry); + } Some(ret) } #[cfg(all(feature="pddbtest", feature="autobasis"))]