From 8066b8fb5e1b910865372376b18c4f368605c63a Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 29 Nov 2021 00:25:09 +0800 Subject: [PATCH] fixed the bug with the pt map encryption, now we hvae two basis! more testing needed, but looking good so far. --- services/pddb/src/backend/basis.rs | 14 +++++++++++++- services/pddb/src/backend/hw.rs | 10 +++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/services/pddb/src/backend/basis.rs b/services/pddb/src/backend/basis.rs index 8874f69c2..b0f5c307e 100644 --- a/services/pddb/src/backend/basis.rs +++ b/services/pddb/src/backend/basis.rs @@ -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, /// 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::, /// modification count @@ -926,6 +937,7 @@ impl BasisCacheEntry { num_dicts: basis_root.num_dictionaries, dicts: HashMap::::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); } } diff --git a/services/pddb/src/backend/hw.rs b/services/pddb/src/backend/hw.rs index 721a293a0..f114b0b76 100644 --- a/services/pddb/src/backend/hw.rs +++ b/services/pddb/src/backend/hw.rs @@ -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, /// fast space cache fspace_cache: HashSet, @@ -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); }