fixed the bug with the pt map encryption, now we hvae two basis!

more testing needed, but looking good so far.
This commit is contained in:
bunnie 2021-11-29 00:25:09 +08:00
parent c15841cfdc
commit 8066b8fb5e
2 changed files with 18 additions and 6 deletions

View file

@ -138,6 +138,8 @@ use core::mem::size_of;
use std::convert::TryInto;
use aes_gcm_siv::{Aes256GcmSiv, Key};
use aes_gcm_siv::aead::NewAead;
use aes::Aes256;
use aes::cipher::{NewBlockCipher, generic_array::GenericArray};
use std::iter::IntoIterator;
use std::collections::{BinaryHeap, HashMap, HashSet};
use std::io::{Result, Error, ErrorKind};
@ -804,6 +806,13 @@ impl BasisCache {
let key = Key::clone_from_slice(&basis_key);
hw.data_encrypt_and_patch_page(&Aes256GcmSiv::new(&key), &aad, &mut block, &pp);
let cipher = Aes256::new(GenericArray::from_slice(&basis_key));
for (&virt, phys) in basis_v2p_map.iter_mut() {
hw.pt_patch_mapping(virt, phys.page_number(), &cipher);
// mark the entry as clean, as it has been sync'd to disk
phys.set_clean(true);
}
Ok(())
}
@ -870,6 +879,8 @@ pub(crate) struct BasisCacheEntry {
pub free_dict_offset: Option<u32>,
/// the cipher for the basis
pub cipher: Aes256GcmSiv,
/// derived cipher for encrypting PTEs -- cache it, so we can save the time cost of constructing the cipher key schedule
pub cipher_ecb: Aes256,
/// the AAD associated with this Basis
pub aad: Vec::<u8>,
/// modification count
@ -926,6 +937,7 @@ impl BasisCacheEntry {
num_dicts: basis_root.num_dictionaries,
dicts: HashMap::<String, DictCacheEntry>::new(),
cipher,
cipher_ecb: Aes256::new(GenericArray::from_slice(key)),
aad,
age: basis_root.age,
free_dict_offset: None,
@ -1182,7 +1194,7 @@ impl BasisCacheEntry {
hw.pt_erase(phys.page_number());
} else if !phys.clean() {
log::info!("syncing dirty pte va: {:x?} pa: {:x?}", virt, phys);
hw.pt_patch_mapping(virt, phys.page_number());
hw.pt_patch_mapping(virt, phys.page_number(), &self.cipher_ecb);
phys.set_clean(true);
}
}

View file

@ -69,7 +69,7 @@ pub(crate) struct PddbOs {
fscb_phys_base: PageAlignedPa,
data_phys_base: PageAlignedPa,
system_basis_key: Option<[u8; AES_KEYSIZE]>,
/// derived cipher for encrypting PTEs -- cache it, so we can save the time cost of constructing the cipher key schedule
/// derived cipher for handling fastspace -- cache it, so we can save the time cost of constructing the cipher key schedule
cipher_ecb: Option<Aes256>,
/// fast space cache
fspace_cache: HashSet<PhysPage>,
@ -306,9 +306,7 @@ impl PddbOs {
/// physical mapping. Note that the `va` is an *address* (in units of bytes), and the `phy_page_num` is
/// a *page number* (so a physical address divided by the page size). It's a slightly awkward units, but
/// it saves a bit of math going back and forth between the native storage formats of the records.
pub(crate) fn pt_patch_mapping(&mut self, va: VirtAddr, phys_page_num: u32) {
self.syskey_ensure();
let cipher = self.cipher_ecb.as_ref().expect("Inconsistent internal state - syskey_ensure() failed");
pub(crate) fn pt_patch_mapping(&mut self, va: VirtAddr, phys_page_num: u32, cipher: &Aes256) {
let mut pte = Pte::new(va, PtFlags::CLEAN, Rc::clone(&self.entropy));
let mut block = Block::from_mut_slice(pte.deref_mut());
//log::info!("pte pt: {:x?}", block);
@ -316,6 +314,7 @@ impl PddbOs {
//log::info!("pte ct: {:x?}", block);
self.patch_pagetable(&block, phys_page_num * aes::BLOCK_SIZE as u32);
}
/// erases a page table entry by overwriting it with garbage
pub(crate) fn pt_erase(&mut self, phys_page_num: u32) {
let mut eraseblock = [0u8; aes::BLOCK_SIZE];
@ -1140,6 +1139,7 @@ impl PddbOs {
let mut system_basis_key: [u8; AES_KEYSIZE] = [0; AES_KEYSIZE];
self.entropy.borrow_mut().get_slice(&mut system_basis_key);
let mut basis_key_aes: [u8; AES_KEYSIZE] = system_basis_key.clone();
let cipher = Aes256::new(GenericArray::from_slice(&basis_key_aes));
self.system_basis_key = Some(system_basis_key); // causes system_basis_key to be owned by self
for block in basis_key_aes.chunks_mut(aes::BLOCK_SIZE) {
self.rootkeys.encrypt_block(block.try_into().unwrap());
@ -1250,7 +1250,7 @@ impl PddbOs {
// step 9. generate & write initial page table entries
for (&virt, phys) in basis_v2p_map.iter_mut() {
self.pt_patch_mapping(virt, phys.page_number());
self.pt_patch_mapping(virt, phys.page_number(), &cipher);
// mark the entry as clean, as it has been sync'd to disk
phys.set_clean(true);
}