mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
add mbbb mechanism
This commit is contained in:
parent
65bbc82ef5
commit
d7cc518552
3 changed files with 154 additions and 24 deletions
|
|
@ -34,4 +34,8 @@ utralib = { path = "../../utralib"}
|
|||
# when selected, physical disk addresses are set to 64 bits, otherwise, they are 32 bits.
|
||||
# 32 bit addressing is recommended for Precursor, as its disk is only 128MiB and it has limited RAM for bookkeeping.
|
||||
u64_pa = []
|
||||
default = []
|
||||
# selecting mbbb employs a "make before break" update on the page table. This minimizes risk of corruption of
|
||||
# the page table when being updated in the case of a power loss, in exchange for more than doubling the time it
|
||||
# takes to update the page table.
|
||||
mbbb = []
|
||||
default = ["mbbb"]
|
||||
|
|
|
|||
|
|
@ -84,6 +84,10 @@ pub(crate) struct PddbOs {
|
|||
dna: u64,
|
||||
/// reference to a TrngPool object that's shared among all the hardware functions
|
||||
entropy: Rc<RefCell<TrngPool>>,
|
||||
/// keys to non-system basis that may or may not be mounted
|
||||
/// We don't blend the System basis into this one because we need to refer to the System basis specifically
|
||||
/// for decrypting management structures like the FSCB.
|
||||
basis_keys: Vec::<[u8; AES_KEYSIZE]>,
|
||||
}
|
||||
|
||||
impl PddbOs {
|
||||
|
|
@ -122,6 +126,7 @@ impl PddbOs {
|
|||
fspace_log_next_addr: None,
|
||||
dna: llio.soc_dna().unwrap(),
|
||||
entropy: trngpool,
|
||||
basis_keys: Vec::new(),
|
||||
};
|
||||
// emulated
|
||||
#[cfg(not(any(target_os = "none", target_os = "xous")))]
|
||||
|
|
@ -142,6 +147,7 @@ impl PddbOs {
|
|||
fspace_log_next_addr: None,
|
||||
dna: llio.soc_dna().unwrap(),
|
||||
entropy: trngpool,
|
||||
basis_keys: Vec::new(),
|
||||
}
|
||||
};
|
||||
ret
|
||||
|
|
@ -184,6 +190,55 @@ impl PddbOs {
|
|||
).expect("couldn't write to data region in the PDDB");
|
||||
}
|
||||
fn patch_pagetable(&self, data: &[u8], offset: u32) {
|
||||
if cfg!(feature = "mbbb") {
|
||||
assert!(data.len() + offset as usize <= size_of::<PageTableInFlash>(), "attempt to patch past page table end");
|
||||
// 1. Check if there is an MBBB structure existing. If so, copy it into the empty slot in the page table, then erase the mbbb.
|
||||
if let Some(mbbb) = self.mbbb_retrieve() {
|
||||
if let Some(erased_offset) = self.pt_find_erased_slot() {
|
||||
self.spinor.patch(
|
||||
self.pddb_mr.as_slice(),
|
||||
xous::PDDB_LOC,
|
||||
&mbbb,
|
||||
self.pt_phys_base.as_u32() + erased_offset,
|
||||
).expect("couldn't write to page table");
|
||||
}
|
||||
// if there *isn't* an erased slot, we still want to get rid of the MBBB structure. A lack of an
|
||||
// erased slot would indicate we lost power after we copied the previous MBBB structure but before we could erase
|
||||
// it from storage, so this picks up where we left off.
|
||||
self.mbbb_erase();
|
||||
}
|
||||
// 2. find the page we're patching
|
||||
let base_page = offset as usize & !(PAGE_SIZE - 1);
|
||||
// 3. copy the data to a local buffer
|
||||
let mut mbbb_page = [0u8; PAGE_SIZE];
|
||||
for (&src, dst) in
|
||||
self.pddb_mr.as_slice()[self.pt_phys_base.as_usize() + base_page..self.pt_phys_base.as_usize() + base_page + PAGE_SIZE].iter()
|
||||
.zip(mbbb_page.iter_mut()) {
|
||||
*dst = src;
|
||||
}
|
||||
// 4. patch the local buffer copy
|
||||
let base_offset = offset as usize & (PAGE_SIZE - 1);
|
||||
for (&src, dst) in data.iter()
|
||||
.zip(mbbb_page[base_offset..base_offset + data.len()].iter_mut()) {
|
||||
*dst = src;
|
||||
}
|
||||
// 5. pick a random offset in the MBBB for the target patch, and write the data into the target
|
||||
let offset = (self.entropy.borrow_mut().get_u8() % MBBB_PAGES as u8) as u32 * PAGE_SIZE as u32;
|
||||
self.patch_mbbb(&mbbb_page, offset);
|
||||
// 6. erase the original page area, thus making the MBBB the authorative location
|
||||
let blank = [0xffu8; PAGE_SIZE];
|
||||
self.spinor.patch(
|
||||
self.pddb_mr.as_slice(),
|
||||
xous::PDDB_LOC,
|
||||
&blank,
|
||||
self.pt_phys_base.as_u32() + base_page as u32,
|
||||
).expect("couldn't write to page table");
|
||||
} else {
|
||||
self.patch_pagetable_raw(data, offset);
|
||||
}
|
||||
}
|
||||
/// Direct write to the page table, without MBBB buffering.
|
||||
fn patch_pagetable_raw(&self, data: &[u8], offset: u32) {
|
||||
assert!(data.len() + offset as usize <= size_of::<PageTableInFlash>(), "attempt to patch past page table end");
|
||||
self.spinor.patch(
|
||||
self.pddb_mr.as_slice(),
|
||||
|
|
@ -222,6 +277,21 @@ impl PddbOs {
|
|||
).expect("couldn't burn fscb");
|
||||
}
|
||||
|
||||
/// Searches the page table for an MBBB slot. This is currently an O(N) search but
|
||||
/// in practice for Precursor there are only 8 pages, so it's quite fast on average.
|
||||
/// This would want to be optimized or cached for a much larger filesystem.
|
||||
/// Returns the physical address of the erased slot as an offset from the pt_phys_base()
|
||||
fn pt_find_erased_slot(&self) -> Option<PhysAddr> {
|
||||
let pt = &self.pddb_mr.as_slice()[self.pt_phys_base.as_usize()..self.pt_phys_base.as_usize() + size_of::<PageTableInFlash>()];
|
||||
let blank = [0xffu8; aes::BLOCK_SIZE];
|
||||
for (index, page) in pt.chunks(PAGE_SIZE).enumerate() {
|
||||
if page[..aes::BLOCK_SIZE] == blank {
|
||||
return Some( (index * PAGE_SIZE) as PhysAddr )
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// maps a StaticCryptoData structure into the key area of the PDDB.
|
||||
fn static_crypto_data_get(&self) -> &StaticCryptoData {
|
||||
let scd_ptr = self.key_phys_base.as_usize() as *const StaticCryptoData;
|
||||
|
|
@ -256,6 +326,34 @@ impl PddbOs {
|
|||
}
|
||||
}
|
||||
|
||||
fn mbbb_as_slice(&self) -> &[u8] {
|
||||
&self.pddb_mr.as_slice()[self.mbbb_phys_base.as_usize()..self.mbbb_phys_base.as_usize() + MBBB_PAGES * PAGE_SIZE]
|
||||
}
|
||||
fn mbbb_retrieve(&self) -> Option<&[u8]> {
|
||||
// Invariant: MBBB pages should be blank, unless a page is stashed there. So, just check the first
|
||||
// AES key size region for "blankness"
|
||||
let blank = [0xffu8; aes::BLOCK_SIZE];
|
||||
for (index, page) in self.mbbb_as_slice().chunks(PAGE_SIZE).enumerate() {
|
||||
if page[..aes::BLOCK_SIZE] == blank {
|
||||
continue;
|
||||
} else {
|
||||
return Some(page)
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
fn mbbb_erase(&self) {
|
||||
let blank = [0xffu8; aes::BLOCK_SIZE];
|
||||
for (index, page) in self.mbbb_as_slice().chunks(PAGE_SIZE).enumerate() {
|
||||
if page[..aes::BLOCK_SIZE] == blank {
|
||||
continue;
|
||||
} else {
|
||||
let blank_page = [0xffu8; PAGE_SIZE];
|
||||
self.patch_mbbb(&blank_page, (index * PAGE_SIZE) as u32);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Create fast_space AAD: name, version number, and FPGA ID.
|
||||
/// This data is "well known", and fixed for every device, but changes
|
||||
/// from device to device. It prevents records from one device from being copied
|
||||
|
|
@ -715,6 +813,10 @@ impl PddbOs {
|
|||
unimplemented!();
|
||||
}
|
||||
|
||||
pub(crate) fn pddb_mount(&mut self) {
|
||||
|
||||
}
|
||||
|
||||
/// this function is dangerous in that calling it will completely erase all of the previous data
|
||||
/// in the PDDB an replace it with a brand-spanking new, blank PDDB.
|
||||
/// The number of servers that can connect to the Spinor crate is strictly tracked, so we borrow a reference
|
||||
|
|
@ -746,7 +848,7 @@ impl PddbOs {
|
|||
let mut temp: [u8; PAGE_SIZE] = [0; PAGE_SIZE];
|
||||
for page in (0..size_of::<PageTableInFlash>()).step_by(PAGE_SIZE) {
|
||||
self.entropy.borrow_mut().get_slice(&mut temp);
|
||||
self.patch_pagetable(&temp, page as u32);
|
||||
self.patch_pagetable_raw(&temp, page as u32);
|
||||
}
|
||||
if size_of::<PageTableInFlash>() & (PAGE_SIZE - 1) != 0 {
|
||||
let remainder_start = size_of::<PageTableInFlash>() & !(PAGE_SIZE - 1);
|
||||
|
|
@ -755,7 +857,7 @@ impl PddbOs {
|
|||
for _ in remainder_start..size_of::<PageTableInFlash>() {
|
||||
temp.push(self.entropy.borrow_mut().get_u8());
|
||||
}
|
||||
self.patch_pagetable(&temp, remainder_start as u32);
|
||||
self.patch_pagetable_raw(&temp, remainder_start as u32);
|
||||
}
|
||||
|
||||
// step 3. create our key material
|
||||
|
|
@ -822,12 +924,12 @@ impl PddbOs {
|
|||
}
|
||||
let basis_root = BasisRoot {
|
||||
magic: api::PDDB_MAGIC,
|
||||
version: 0x1111_1111, // api::PDDB_VERSION,
|
||||
version: api::PDDB_VERSION,
|
||||
name,
|
||||
age: 0x2222_2222,
|
||||
age: 0,
|
||||
prealloc_open_end: PageAlignedVa::from(INITIAL_BASIS_ALLOC * VPAGE_SIZE),
|
||||
num_dictionaries: 0x3333_3333,
|
||||
dict_ptr: Some(VirtAddr::new(0x4444_4444_4444_4444).unwrap())//None,
|
||||
num_dictionaries: 0,
|
||||
dict_ptr: None,
|
||||
};
|
||||
log::info!("prealloc_open_end: {:x?}", basis_root.prealloc_open_end);
|
||||
log::info!("prealloc_open_end as vpage: {:x?}", basis_root.prealloc_open_end.as_vpage_num());
|
||||
|
|
|
|||
|
|
@ -9,6 +9,8 @@ import binascii
|
|||
import hashlib
|
||||
|
||||
SYSTEM_BASIS = '.System'
|
||||
PAGE_SIZE = 4096
|
||||
MBBB_PAGES = 10
|
||||
|
||||
# from https://github.com/wc-duck/pymmh3/blob/master/pymmh3.py
|
||||
def xrange( a, b, c ):
|
||||
|
|
@ -124,18 +126,19 @@ def main():
|
|||
print("Found basis keys:")
|
||||
print(keys)
|
||||
|
||||
MBBB_PAGES = 10
|
||||
global MBBB_PAGES
|
||||
FSCB_PAGES = 16
|
||||
KEY_PAGES = 1
|
||||
PAGE_SIZE = 4096
|
||||
global PAGE_SIZE
|
||||
|
||||
with open(imagefile, 'rb') as img_f:
|
||||
raw_img = img_f.read()
|
||||
pddb_len = len(raw_img)
|
||||
pddb_size_pages = pddb_len // PAGE_SIZE
|
||||
|
||||
mbbb_offset = pddb_size_pages * Pte.PTE_LEN + PAGE_SIZE * KEY_PAGES
|
||||
img_index = 0
|
||||
tables = decode_pagetable(raw_img, pddb_size_pages, keys)
|
||||
tables = decode_pagetable(raw_img, pddb_size_pages, keys, raw_img[mbbb_offset:mbbb_offset + MBBB_PAGES * PAGE_SIZE])
|
||||
img_index += pddb_size_pages * Pte.PTE_LEN
|
||||
rawkeys = raw_img[img_index : img_index + PAGE_SIZE * KEY_PAGES]
|
||||
img_index += PAGE_SIZE * KEY_PAGES
|
||||
|
|
@ -161,10 +164,12 @@ def main():
|
|||
cipher = AES_GCM_SIV(key, pp_data[:12])
|
||||
pt_data = cipher.decrypt(pp_data[12:], basis_aad(name))
|
||||
basis_data.extend(bytearray(pt_data))
|
||||
except KeyError:
|
||||
print("decrypted vpage @ {} ppage @ {:x}".format(vp, v2p_table[vp]))
|
||||
except ValueError:
|
||||
errors += 1
|
||||
print("couldn't decrypt vpage @ {} ppage @ {:x}".format(vp, v2p_table[vp]))
|
||||
|
||||
|
||||
if errors == 0:
|
||||
print([hex(x) for x in basis_data[:256]])
|
||||
basis = Basis(basis_data)
|
||||
|
|
@ -271,26 +276,45 @@ class Pte:
|
|||
else:
|
||||
return 'a_{:012x} | f_{} | n_{:08x} | c_{:08x}'.format(self.addr(), self.flags(), self.nonce(), self.checksum())
|
||||
|
||||
def decode_pagetable(img, entries, keys):
|
||||
def find_mbbb(mbbb):
|
||||
global MBBB_PAGES
|
||||
pages = [mbbb[i:i+PAGE_SIZE] for i in range(0, MBBB_PAGES * PAGE_SIZE, PAGE_SIZE)]
|
||||
for page in pages:
|
||||
if bytearray(page[:16]) != bytearray([0xff] * 16):
|
||||
return page
|
||||
return None
|
||||
|
||||
def decode_pagetable(img, entries, keys, mbbb):
|
||||
global PAGE_SIZE
|
||||
key_table = {}
|
||||
for name, key in keys.items():
|
||||
print("Pages for basis {}".format(name))
|
||||
print("key: {}".format(key.hex()))
|
||||
cipher = AES.new(key, AES.MODE_ECB)
|
||||
encrypted_ptes = [img[i:i+Pte.PTE_LEN] for i in range(0, entries * Pte.PTE_LEN, Pte.PTE_LEN)]
|
||||
page = 0
|
||||
pages = [img[i:i+PAGE_SIZE] for i in range(0, entries * Pte.PTE_LEN, PAGE_SIZE)]
|
||||
page_num = 0
|
||||
v2p_table = {}
|
||||
p2v_table = {}
|
||||
for pte in encrypted_ptes:
|
||||
maybe_pte = Pte(cipher.decrypt(pte))
|
||||
if maybe_pte.is_valid():
|
||||
print(maybe_pte.as_str(page))
|
||||
p_addr = page * (4096 // Pte.PTE_LEN)
|
||||
v2p_table[maybe_pte.addr()] = p_addr
|
||||
p2v_table[p_addr] = maybe_pte.addr()
|
||||
for page in pages:
|
||||
if bytearray(page[:16]) == bytearray([0xff] * 16):
|
||||
# decrypt from mbbb
|
||||
print("Found a blank PTE page, falling back to MBBB!")
|
||||
maybe_page = find_mbbb(mbbb)
|
||||
if maybe_page != None:
|
||||
page = maybe_page
|
||||
else:
|
||||
print("Blank entry in PTE found, but no corresponding MBBB entry found. Filesystem likely corrupted.")
|
||||
encrypted_ptes = [page[i:i+Pte.PTE_LEN] for i in range(0, PAGE_SIZE, Pte.PTE_LEN)]
|
||||
for pte in encrypted_ptes:
|
||||
maybe_pte = Pte(cipher.decrypt(pte))
|
||||
if maybe_pte.is_valid():
|
||||
print(maybe_pte.as_str(page_num))
|
||||
p_addr = page_num * (4096 // Pte.PTE_LEN)
|
||||
v2p_table[maybe_pte.addr()] = p_addr
|
||||
p2v_table[p_addr] = maybe_pte.addr()
|
||||
|
||||
page += Pte.PTE_LEN
|
||||
key_table[name] = [v2p_table, p2v_table]
|
||||
page_num += Pte.PTE_LEN
|
||||
key_table[name] = [v2p_table, p2v_table]
|
||||
return key_table
|
||||
|
||||
class PhysPage:
|
||||
|
|
@ -399,7 +423,7 @@ def decode_fscb(img, keys, FSCB_LEN_PAGES=2):
|
|||
if bytearray(page[:32]) == bytearray([0xff] * 32):
|
||||
# page is blank
|
||||
pass
|
||||
elif bytearray(page[:16] == bytearray([0xff] * 16)):
|
||||
elif bytearray(page[:16]) == bytearray([0xff] * 16):
|
||||
space_update.append(page[16:])
|
||||
else:
|
||||
if fscb_start == None:
|
||||
|
|
|
|||
Loading…
Reference in a new issue