fix bug where old key data was not being zeroized on delete

on valid pages were being updated in a dictionary entry as
it is updated; actually, entries that are invalid should also
be zeroized, otheriwse they "Stick around" as valid entries.
This commit is contained in:
bunnie 2023-01-26 23:32:22 +08:00 committed by Sean Cross
parent 9903aacbad
commit ab90b0044e

View file

@ -572,6 +572,7 @@ impl BasisCache {
// this implementation is still in progress // this implementation is still in progress
dict_entry.key_erase(key); dict_entry.key_erase(key);
} }
assert!(dict_entry.clean == false, "dictionary entry should have been marked unclean");
// sync the key pools to disk // sync the key pools to disk
if !dict_entry.sync_small_pool(hw, &mut basis.v2p_map, &basis.cipher) { if !dict_entry.sync_small_pool(hw, &mut basis.v2p_map, &basis.cipher) {
@ -1042,16 +1043,20 @@ impl BasisCache {
let mut candidates = BinaryHeap::new(); let mut candidates = BinaryHeap::new();
for basis in self.cache.iter() { for basis in self.cache.iter() {
for (dict_name, dict) in basis.dicts.iter() { for (dict_name, dict) in basis.dicts.iter() {
for (key_name, key) in dict.keys.iter() { if dict.flags.valid() {
candidates.push(Reverse( for (key_name, key) in dict.keys.iter() {
KeyAge { if key.flags.valid() {
atime: key.atime(), candidates.push(Reverse(
_size: key.size(), KeyAge {
key: key_name.to_string(), atime: key.atime(),
dict: dict_name.to_string(), _size: key.size(),
basis: basis.name.to_string(), key: key_name.to_string(),
dict: dict_name.to_string(),
basis: basis.name.to_string(),
}
));
} }
)); }
} }
} }
} }
@ -1063,7 +1068,7 @@ impl BasisCache {
match basis.dicts.get_mut(&ka.dict) { match basis.dicts.get_mut(&ka.dict) {
Some(dentry) => { Some(dentry) => {
let freed = dentry.evict_keycache_entry(&ka.key); let freed = dentry.evict_keycache_entry(&ka.key);
log::debug!("pruned {} bytes from atime {} / total {}", freed, ka.atime, pruned); log::debug!("pruned {} bytes from atime {} / total {} key {}", freed, ka.atime, pruned, ka.key);
pruned += freed; pruned += freed;
if pruned >= target_bytes { if pruned >= target_bytes {
return pruned; return pruned;
@ -1475,7 +1480,7 @@ impl BasisCacheEntry {
} }
} }
/// This will sync the named Dictionary header + dirty *key descriptors*. It does not /// This will sync the named Dictionary header + valid *key descriptors*. It does not
/// sync the key data itself. Note this does not mark the surrounding basis structure as clean /// sync the key data itself. Note this does not mark the surrounding basis structure as clean
/// when it exits, even if there are no more dirty entries within. /// when it exits, even if there are no more dirty entries within.
/// ///
@ -1502,7 +1507,8 @@ impl BasisCacheEntry {
// but definitely all the dirty ones are in there (if they aren't, where else would they be??) // but definitely all the dirty ones are in there (if they aren't, where else would they be??)
// this is the virtual page within the dictionary region that we're currently serializing // this is the virtual page within the dictionary region that we're currently serializing
let mut vpage_num = 0; let mut vpage_num = 0; // because the root record is dirty, we always need to regenerate page 0.
// note: other pages are only generated on-demand, based on if there is a dirty key in that page or not.
let mut loopcheck= 0; let mut loopcheck= 0;
let mut sync_count = 0; let mut sync_count = 0;
loop { loop {
@ -1534,7 +1540,7 @@ impl BasisCacheEntry {
} }
// 2(b). fill in the target vpage with data: general key case // 2(b). fill in the target vpage with data: general key case
// Scan the DictCacheEntry.keys record for dirty keys within the current target vpage's range // Scan the DictCacheEntry.keys record for valid keys within the current target vpage's range
// this is not a terribly efficient operation right now, because the DictCacheEntry is designed to // this is not a terribly efficient operation right now, because the DictCacheEntry is designed to
// be searched by name, but in this case, we want to check for an index range. There's a lot // be searched by name, but in this case, we want to check for an index range. There's a lot
// of things we could do to optimize this, depending on the memory/time trade-off we want to // of things we could do to optimize this, depending on the memory/time trade-off we want to
@ -1548,7 +1554,7 @@ impl BasisCacheEntry {
key.start, key.clean, key.flags, key.descriptor_index, key.start, key.clean, key.flags, key.descriptor_index,
); );
}*/ }*/
if !key.clean && key.flags.valid() { if key.flags.valid() {
if key.descriptor_vaddr(dict_offset) >= cur_vpage && if key.descriptor_vaddr(dict_offset) >= cur_vpage &&
key.descriptor_vaddr(dict_offset) < next_vpage { key.descriptor_vaddr(dict_offset) < next_vpage {
log::debug!("merging in key {}", key_name); log::debug!("merging in key {}", key_name);
@ -1589,9 +1595,10 @@ impl BasisCacheEntry {
}; };
for (index, stride) in page[size_of::<JournalType>()..].chunks_mut(DK_STRIDE).enumerate() { for (index, stride) in page[size_of::<JournalType>()..].chunks_mut(DK_STRIDE).enumerate() {
if let Some(elem) = dk_vpage.elements[index] { if let Some(elem) = dk_vpage.elements[index] {
for (&src, dst) in elem.data.iter().zip(stride.iter_mut()) { stride.copy_from_slice(&elem.data);
*dst = src; } else {
} // actively zero-ize fields that are `None` so they don't decrypt to valid records later on.
stride.copy_from_slice(&[0u8; DK_STRIDE]);
} }
} }
// generate nonce and write out // generate nonce and write out
@ -1615,6 +1622,9 @@ impl BasisCacheEntry {
} }
sync_count += 1; sync_count += 1;
} }
// remove invalid keys from the dictionary cache
dict.keys.retain(|_name, entry| entry.flags.valid());
log::debug!("done syncing dict"); log::debug!("done syncing dict");
dict.clean = true; dict.clean = true;
} }
@ -1691,6 +1701,7 @@ impl BasisCacheEntry {
} }
pub(crate) fn sync(&mut self, hw: &mut PddbOs) -> Result<()> { pub(crate) fn sync(&mut self, hw: &mut PddbOs) -> Result<()> {
self.dicts.retain(|_name, entry| entry.flags.valid()); // prune any invalid dictionary entries, as they have been deleted
// this is a bit awkward, but we have to make a copy of all the dictionary names // this is a bit awkward, but we have to make a copy of all the dictionary names
// because otherwise we borrow self as immutable to enumerate the names, and then // because otherwise we borrow self as immutable to enumerate the names, and then
// we borrow it as mutable to do the sync. This deep-copy works around the issue. // we borrow it as mutable to do the sync. This deep-copy works around the issue.