mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
free_pool scanning first cut
This commit is contained in:
parent
81d9fb3fda
commit
7858aaad0e
8 changed files with 414 additions and 185 deletions
|
|
@ -30,4 +30,7 @@ aes-gcm-siv = "0.10.3"
|
||||||
utralib = { path = "../../utralib"}
|
utralib = { path = "../../utralib"}
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
|
# 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 = []
|
default = []
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,20 @@ pub(crate) const PDDB_MAX_DICT_NAME_LEN: usize = 64;
|
||||||
pub(crate) const PDDB_MAX_KEY_NAME_LEN: usize = 128;
|
pub(crate) const PDDB_MAX_KEY_NAME_LEN: usize = 128;
|
||||||
pub(crate) const PDDB_MAGIC: [u8; 4] = [0x50, 0x44, 0x44, 0x42];
|
pub(crate) const PDDB_MAGIC: [u8; 4] = [0x50, 0x44, 0x44, 0x42];
|
||||||
pub(crate) const PDDB_VERSION: u16 = 0;
|
pub(crate) const PDDB_VERSION: u16 = 0;
|
||||||
|
/// PDDB_A_LEN may be shorter than xous::PDDB_LEN, to speed up testing
|
||||||
|
pub(crate) const PDDB_A_LEN: usize = 16 * 1024 * 1024;
|
||||||
|
|
||||||
|
/// A number between (0, 1] that defines how many of the "truly free" pages we
|
||||||
|
/// should put into the FSCB. A value of 0.0 is not allowed as that leaves no free pages.
|
||||||
|
/// A value of 1.0 would allow an attacker to deduce the real size of data because all
|
||||||
|
/// the freee space would be tracked in the FSCB. Thus the trade-off is deniability versus
|
||||||
|
/// performance: a fill coefficient of 1.0 means we'd never have to do a brute force scan
|
||||||
|
/// for free pages, but you would have no deniability; a fill coefficient of near-0 means
|
||||||
|
/// we could plausibly deny a lot of pages as being free space, but every time you wanted
|
||||||
|
/// to grow a record, you'd have to unlock all your Basis and do a brute force scan, otherwise
|
||||||
|
/// you risk overwriting hidden data by mistaking it as free space. The initial setting is
|
||||||
|
/// 0.5: with this setting, we won't be more than a factor of 2 off from the ideal setting!
|
||||||
|
pub(crate) const FSCB_FILL_COEFFICIENT: f32 = 0.5;
|
||||||
|
|
||||||
pub const PDDB_DEFAULT_SYSTEM_BASIS: &'static str = ".System";
|
pub const PDDB_DEFAULT_SYSTEM_BASIS: &'static str = ".System";
|
||||||
pub(crate) const PDDB_FAST_SPACE_SYSTEM_BASIS: &'static str = ".FastSpace";
|
pub(crate) const PDDB_FAST_SPACE_SYSTEM_BASIS: &'static str = ".FastSpace";
|
||||||
|
|
@ -72,6 +86,9 @@ mod tests {
|
||||||
fn test_pddbuf_size() {
|
fn test_pddbuf_size() {
|
||||||
assert!(core::mem::size_of::<PddbBuf>() == 4096, "PddBuf record has the wrong size");
|
assert!(core::mem::size_of::<PddbBuf>() == 4096, "PddBuf record has the wrong size");
|
||||||
}
|
}
|
||||||
|
fn test_pddb_len() {
|
||||||
|
assert!(PDDB_A_LEN <= xous::PDDB_LEN, "PDDB_A_LEN is larger than the maximum extents available in the hardware");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
impl PddbBuf {
|
impl PddbBuf {
|
||||||
pub(crate) fn from_slice_mut(slice: &mut [u8]) -> &mut PddbBuf {
|
pub(crate) fn from_slice_mut(slice: &mut [u8]) -> &mut PddbBuf {
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,7 @@ pub(crate) struct BasisRoot {
|
||||||
*/
|
*/
|
||||||
/// "open end" of the pre-allocated space for the Basis. All Basis data must exist in an extent that is
|
/// "open end" of the pre-allocated space for the Basis. All Basis data must exist in an extent that is
|
||||||
/// less than this value. This can be grown and shrunk with allocation and compaction processes.
|
/// less than this value. This can be grown and shrunk with allocation and compaction processes.
|
||||||
pub(crate) prealloc_open_end: PageAlignedU64,
|
pub(crate) prealloc_open_end: PageAlignedVa,
|
||||||
pub(crate) num_dictionaries: u32,
|
pub(crate) num_dictionaries: u32,
|
||||||
// dict_slice: [DictPointer; num_dictionaries], // DictPointers + num_dictionaries above can be turned into a dict_slice
|
// dict_slice: [DictPointer; num_dictionaries], // DictPointers + num_dictionaries above can be turned into a dict_slice
|
||||||
////// the following records are appended by the Serialization routine
|
////// the following records are appended by the Serialization routine
|
||||||
|
|
|
||||||
|
|
@ -3,33 +3,72 @@ use core::num::NonZeroU32;
|
||||||
use super::PAGE_SIZE;
|
use super::PAGE_SIZE;
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
|
||||||
|
/// Each free_pool entry takes about 4 bytes, so give-or-take we have about 1000 free_pool
|
||||||
|
/// entries per page of storage for the free_pool, or 4k * 1000 ~ 4MiB per page, when PhysAddr is a u32
|
||||||
|
pub(crate) const FASTSPACE_PAGES: usize = 2;
|
||||||
|
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
pub(crate) enum SpaceState {
|
pub(crate) enum SpaceState {
|
||||||
/// pages that are completely un-spoken for
|
/// pages that are completely un-spoken for
|
||||||
Free,
|
Free = 0,
|
||||||
/// pages that are in the process of being used, but the journal has yet to be committed
|
/// pages that are in the process of being used, but the journal has yet to be committed
|
||||||
MaybeUsed,
|
/// in other words, these are pages that might be in the RAM cache.
|
||||||
/// pages that are now fully used
|
MaybeUsed = 1,
|
||||||
Used,
|
/// pages that are confirm plus chop fully used
|
||||||
|
Used = 2,
|
||||||
/// pages that are no longer used and need to be erased
|
/// pages that are no longer used and need to be erased
|
||||||
Dirty,
|
Dirty = 3,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// FastSpace tracks a limited set of physical pages
|
/// FastSpace tracks a limited set of physical pages
|
||||||
/// This is designed to fill exactly one erase sector of 4096 bytes
|
/// An optimal implementation of this would fill whole pages with the free_pool array.
|
||||||
|
/// This record is meant to be updated rarely, and atomically, in a make-before-break fashion:
|
||||||
|
/// Make-before-break: (maybe it's COW? idk, I'm a hardware guy, so I'm switched on to make-before-break)
|
||||||
|
/// Write a new version of this with a higher journal number, verify its
|
||||||
|
/// integrity, and then erase the previous version.
|
||||||
|
/// Atomically:
|
||||||
|
/// The entire contents of this structure is written in the update operation; there are no partial updates
|
||||||
|
/// possible to the structure
|
||||||
|
/// Rarely:
|
||||||
|
/// Instead of partial updates to this structure, individual "SpaceUpdate" records are encrypted to the
|
||||||
|
/// system basis and stored in a non-cryptographically free region. These are decrypted and merged
|
||||||
|
/// into an in-RAM cache that facilitates the "rare" update of this large-ish structure. This creates
|
||||||
|
/// a side-channel, where an attacker would be able to observe the rate at which pages are modified...
|
||||||
|
/// and hopefully nothing else?
|
||||||
|
/// The "rare" property is important especially as the disk size scales up; if we wanted to keep
|
||||||
|
/// 100MiB of "fast space" on hand, this structure would span 25 pages.
|
||||||
#[repr(C, packed)]
|
#[repr(C, packed)]
|
||||||
pub (crate) struct FastSpace {
|
pub (crate) struct FastSpace {
|
||||||
p_nonce: [u8; 12],
|
p_nonce: [u8; 12],
|
||||||
|
/// u32 in lsb byte order
|
||||||
journal_rev: [u8; 4],
|
journal_rev: [u8; 4],
|
||||||
free_pool: [PhysPage; (PAGE_SIZE - 16 - 4 -12) / 4],
|
/// Not sure if there is a "better" way to compute things, but we want the number of entries in the
|
||||||
|
/// free_pool array to "round out" the FastSpace record to be equal to exactly one page
|
||||||
|
free_pool: [PhysPage; ((PAGE_SIZE * FASTSPACE_PAGES) - (12 + 4 + 16)) / core::mem::size_of::<PhysPage>()],
|
||||||
p_tag: [u8; 16],
|
p_tag: [u8; 16],
|
||||||
}
|
}
|
||||||
|
|
||||||
/// a 128-bit record that stores an encrypted update to the FastSpace pool
|
/// a 128-bit record that stores an encrypted update to the FastSpace pool, facilitating the "rarely" trait of the structure.
|
||||||
#[repr(C, packed)]
|
#[repr(C, packed)]
|
||||||
|
#[cfg(not(feature = "u64_pa"))]
|
||||||
pub (crate) struct SpaceUpdate {
|
pub (crate) struct SpaceUpdate {
|
||||||
nonce: u64,
|
nonce: u64,
|
||||||
page_number: u32,
|
page_number: PhysAddr,
|
||||||
reserved: [u8; 3],
|
reserved: [u8; 4],
|
||||||
state: SpaceState,
|
}
|
||||||
|
#[cfg(feature = "u64_pa")]
|
||||||
|
pub (crate) struct SpaceUpdate {
|
||||||
|
nonce: u64,
|
||||||
|
page_number: PhysAddr,
|
||||||
|
}
|
||||||
|
impl SpaceUpdate {
|
||||||
|
// add accessors, decryptors, and constructors so we don't shoot ourselves in the foot so much.
|
||||||
|
}
|
||||||
|
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn test_fast_space_size() {
|
||||||
|
assert!(core::mem::size_of::<FastSpace>() & !(PAGE_SIZE - 1) == 0, "FastSpace is not exactly a multiple of one page in size");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,12 +10,14 @@ use core::ops::Deref;
|
||||||
use core::convert::TryFrom;
|
use core::convert::TryFrom;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::collections::BinaryHeap;
|
||||||
|
use std::cmp::Reverse;
|
||||||
use std::io::{Result, Error, ErrorKind};
|
use std::io::{Result, Error, ErrorKind};
|
||||||
|
|
||||||
/// Implementation-specific PDDB structures: for Precursor/Xous OS pair
|
/// Implementation-specific PDDB structures: for Precursor/Xous OS pair
|
||||||
|
|
||||||
pub(crate) const MBBB_PAGES: usize = 10;
|
pub(crate) const MBBB_PAGES: usize = 10;
|
||||||
pub(crate) const FSCB_PAGES: usize = 10;
|
pub(crate) const FSCB_PAGES: usize = 16;
|
||||||
pub(crate) const INITIAL_BASIS_ALLOC: usize = 16;
|
pub(crate) const INITIAL_BASIS_ALLOC: usize = 16;
|
||||||
|
|
||||||
pub const PAGE_SIZE: usize = spinor::SPINOR_ERASE_SIZE as usize;
|
pub const PAGE_SIZE: usize = spinor::SPINOR_ERASE_SIZE as usize;
|
||||||
|
|
@ -45,17 +47,20 @@ pub(crate) struct PddbOs {
|
||||||
pddb_mr: xous::MemoryRange,
|
pddb_mr: xous::MemoryRange,
|
||||||
trng: trng::Trng,
|
trng: trng::Trng,
|
||||||
/// page table base -- location in FLASH, offset from physical bottom of pddb_mr
|
/// page table base -- location in FLASH, offset from physical bottom of pddb_mr
|
||||||
pt_phys_base: PageAlignedU32,
|
pt_phys_base: PageAlignedPa,
|
||||||
/// local key store -- one page, to store exactly one key, used for the system basis.
|
/// local key store -- one page, to store exactly one key, used for the system basis.
|
||||||
/// the rest of the keys are generated on the fly entirely from the user password + a salt also stored in this page
|
/// the rest of the keys are generated on the fly entirely from the user password + a salt also stored in this page
|
||||||
key_phys_base: PageAlignedU32,
|
key_phys_base: PageAlignedPa,
|
||||||
/// make before break buffer base -- location in FLASH, offset from physical bottom of pddb_mr
|
/// make before break buffer base -- location in FLASH, offset from physical bottom of pddb_mr
|
||||||
mbbb_phys_base: PageAlignedU32,
|
mbbb_phys_base: PageAlignedPa,
|
||||||
/// free space circular buffer base -- location in FLASH, offset from physical bottom of pddb_mr
|
/// free space circular buffer base -- location in FLASH, offset from physical bottom of pddb_mr
|
||||||
fscb_phys_base: PageAlignedU32,
|
fscb_phys_base: PageAlignedPa,
|
||||||
data_phys_base: PageAlignedU32,
|
data_phys_base: PageAlignedPa,
|
||||||
system_basis_key: Option<[u8; 32]>,
|
system_basis_key: Option<[u8; 32]>,
|
||||||
v2p_map: HashMap<BasisRootName, HashMap<VirtAddr, PhysPage>>,
|
v2p_map: HashMap<BasisRootName, HashMap<VirtAddr, PhysPage>>,
|
||||||
|
/// The PDDB eats a lot of entropy. Keep a local pool of entropy, so we're not wasting a lot of
|
||||||
|
/// overhead passing messages to the TRNG.
|
||||||
|
e_cache: Vec::<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PddbOs {
|
impl PddbOs {
|
||||||
|
|
@ -64,37 +69,189 @@ impl PddbOs {
|
||||||
let pddb = xous::syscall::map_memory(
|
let pddb = xous::syscall::map_memory(
|
||||||
xous::MemoryAddress::new(xous::PDDB_LOC as usize),
|
xous::MemoryAddress::new(xous::PDDB_LOC as usize),
|
||||||
None,
|
None,
|
||||||
xous::PDDB_LEN as usize,
|
PDDB_A_LEN as usize,
|
||||||
xous::MemoryFlags::R,
|
xous::MemoryFlags::R,
|
||||||
)
|
)
|
||||||
.expect("Couldn't map the PDDB memory range");
|
.expect("Couldn't map the PDDB memory range");
|
||||||
|
|
||||||
// the mbbb is located one page off from the Page Table
|
// the mbbb is located one page off from the Page Table
|
||||||
let key_phys_base = PageAlignedU32::from(core::mem::size_of::<PageTableInFlash>());
|
let key_phys_base = PageAlignedPa::from(core::mem::size_of::<PageTableInFlash>());
|
||||||
let mbbb_phys_base = key_phys_base + PageAlignedU32::from(PAGE_SIZE);
|
let mbbb_phys_base = key_phys_base + PageAlignedPa::from(PAGE_SIZE);
|
||||||
let fscb_phys_base = PageAlignedU32::from(mbbb_phys_base.as_u32() + MBBB_PAGES as u32 * PAGE_SIZE as u32);
|
let fscb_phys_base = PageAlignedPa::from(mbbb_phys_base.as_u32() + MBBB_PAGES as u32 * PAGE_SIZE as u32);
|
||||||
|
|
||||||
|
let mut trng = trng::Trng::new(&xns).unwrap();
|
||||||
|
let mut cache: [u8; 8192] = [0; 8192];
|
||||||
|
trng.fill_bytes(&mut cache);
|
||||||
|
|
||||||
PddbOs {
|
PddbOs {
|
||||||
spinor: spinor::Spinor::new(&xns).unwrap(),
|
spinor: spinor::Spinor::new(&xns).unwrap(),
|
||||||
rootkeys: root_keys::RootKeys::new(&xns, Some(AesRootkeyType::User0)).expect("FATAL: couldn't access RootKeys!"),
|
rootkeys: root_keys::RootKeys::new(&xns, Some(AesRootkeyType::User0)).expect("FATAL: couldn't access RootKeys!"),
|
||||||
pddb_mr: pddb,
|
pddb_mr: pddb,
|
||||||
trng: trng::Trng::new(&xns).unwrap(),
|
trng,
|
||||||
pt_phys_base: PageAlignedU32::from(0 as u32),
|
pt_phys_base: PageAlignedPa::from(0 as u32),
|
||||||
key_phys_base,
|
key_phys_base,
|
||||||
mbbb_phys_base,
|
mbbb_phys_base,
|
||||||
fscb_phys_base,
|
fscb_phys_base,
|
||||||
data_phys_base: PageAlignedU32::from(fscb_phys_base.as_u32() + FSCB_PAGES as u32 * PAGE_SIZE as u32),
|
data_phys_base: PageAlignedPa::from(fscb_phys_base.as_u32() + FSCB_PAGES as u32 * PAGE_SIZE as u32),
|
||||||
system_basis_key: None,
|
system_basis_key: None,
|
||||||
v2p_map: HashMap::<BasisRootName, HashMap<VirtAddr, PhysPage>>::new(),
|
v2p_map: HashMap::<BasisRootName, HashMap<VirtAddr, PhysPage>>::new(),
|
||||||
|
e_cache: cache.to_vec(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// patches data at an offset starting from the data physical base address, which corresponds
|
||||||
|
/// exactly to the first entry in the page table
|
||||||
|
fn patch_data(&self, data: &[u8], offset: u32) {
|
||||||
|
assert!(data.len() + offset as usize <= PDDB_A_LEN - self.data_phys_base.as_usize(), "attempt to store past disk boundary");
|
||||||
|
self.spinor.patch(
|
||||||
|
self.pddb_mr.as_slice(),
|
||||||
|
xous::PDDB_LOC,
|
||||||
|
&data,
|
||||||
|
offset + self.data_phys_base.as_u32(),
|
||||||
|
).expect("couldn't write to data region in the PDDB");
|
||||||
|
}
|
||||||
|
fn patch_keys(&self, data: &[u8], offset: u32) {
|
||||||
|
assert!(data.len() + offset as usize <= PAGE_SIZE, "attempt to burn key data that is outside the key region");
|
||||||
|
self.spinor.patch(
|
||||||
|
self.pddb_mr.as_slice(),
|
||||||
|
xous::PDDB_LOC,
|
||||||
|
data,
|
||||||
|
self.key_phys_base.as_u32() + offset
|
||||||
|
).expect("couldn't burn keys");
|
||||||
|
}
|
||||||
|
fn patch_mbbb(&self, data: &[u8], offset: u32) {
|
||||||
|
assert!(data.len() + offset as usize <= PAGE_SIZE * MBBB_PAGES, "mbbb patch would go out of bounds");
|
||||||
|
self.spinor.patch(
|
||||||
|
self.pddb_mr.as_slice(),
|
||||||
|
xous::PDDB_LOC,
|
||||||
|
data,
|
||||||
|
self.mbbb_phys_base.as_u32() + offset
|
||||||
|
).expect("couldn't burn mbbb");
|
||||||
|
}
|
||||||
|
fn patch_fscb(&self, data: &[u8], offset: u32) {
|
||||||
|
assert!(data.len() + offset as usize <= PAGE_SIZE * FSCB_PAGES, "fscb patch would go out of bounds");
|
||||||
|
self.spinor.patch(
|
||||||
|
self.pddb_mr.as_slice(),
|
||||||
|
xous::PDDB_LOC,
|
||||||
|
data,
|
||||||
|
self.fscb_phys_base.as_u32() + offset
|
||||||
|
).expect("couldn't burn fscb");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ensure_entropy(&mut self, amount: usize) {
|
||||||
|
if self.e_cache.len() < amount {
|
||||||
|
let mut cache: [u8; 8192] = [0; 8192];
|
||||||
|
self.trng.fill_bytes(&mut cache);
|
||||||
|
self.e_cache.extend_from_slice(&cache);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn trng_cache_u8(&mut self) -> u8 {
|
||||||
|
self.ensure_entropy(1);
|
||||||
|
self.e_cache.pop().unwrap()
|
||||||
|
}
|
||||||
|
fn trng_cache_u32(&mut self) -> u32 {
|
||||||
|
self.ensure_entropy(4);
|
||||||
|
let ret = u32::from_le_bytes(self.e_cache[self.e_cache.len() - 4..].try_into().unwrap());
|
||||||
|
self.e_cache.truncate(self.e_cache.len() - 4);
|
||||||
|
ret
|
||||||
|
}
|
||||||
|
fn trng_cache_u64(&mut self) -> u64 {
|
||||||
|
self.ensure_entropy(8);
|
||||||
|
let ret = u64::from_le_bytes(self.e_cache[self.e_cache.len() - 8..].try_into().unwrap());
|
||||||
|
self.e_cache.truncate(self.e_cache.len() - 8);
|
||||||
|
ret
|
||||||
|
}
|
||||||
|
fn trng_cache_slice(&mut self, bucket: &mut [u8]) {
|
||||||
|
self.ensure_entropy(bucket.len());
|
||||||
|
for (src, dst) in self.e_cache.drain(
|
||||||
|
(self.e_cache.len() - bucket.len())..
|
||||||
|
).zip(bucket.iter_mut()) {
|
||||||
|
*dst = src;
|
||||||
|
}
|
||||||
|
}
|
||||||
/// generates a 96-bit nonce using the CPRNG
|
/// generates a 96-bit nonce using the CPRNG
|
||||||
pub fn gen_nonce(&mut self) -> [u8; 12] {
|
pub fn gen_nonce(&mut self) -> [u8; 12] {
|
||||||
let mut nonce: [u8; 12] = [0; 12];
|
let mut nonce: [u8; 12] = [0; 12];
|
||||||
self.trng.fill_bytes(&mut nonce);
|
self.trng_cache_slice(&mut nonce);
|
||||||
nonce
|
nonce
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// WARNING: only call this function when all knows Basis have been unlocked, otherwise locked
|
||||||
|
/// Basis will be marked in the freespace sweep for deletion.
|
||||||
|
///
|
||||||
|
/// Sweeps through the entire set of known data (as loaded in v2p_map) and
|
||||||
|
/// returns a subset of the total free space in a PhysPage vector that is a list of physical pages,
|
||||||
|
/// in random order, that can be used by PDDB operations in the future without worry about
|
||||||
|
/// accidentally overwriting Basis data that are locked.
|
||||||
|
///
|
||||||
|
/// The function is coded to prioritize small peak memory footprint over speed, as it
|
||||||
|
/// needs to run in a fairly memory-constrained environment, keeping in mind that if the PDDB
|
||||||
|
/// structures were to be extended to run on say, an external USB drive with gigabytes of space,
|
||||||
|
/// we cannot afford to naively allocate vectors that count every single page.
|
||||||
|
fn collect_fastspace(&self) -> Vec::<PhysPage> {
|
||||||
|
let mut free_pool = Vec::<usize>::new();
|
||||||
|
let max_entries = FASTSPACE_PAGES * PAGE_SIZE / core::mem::size_of::<PhysPage>();
|
||||||
|
free_pool.reserve_exact(max_entries);
|
||||||
|
// 1. scan through all of the known physical pages, and add them to a binary heap.
|
||||||
|
// WARNING: this could get really big for a very large filesystem. It's capped at ~100k for
|
||||||
|
// Precursor's ~100MiB storage increment.
|
||||||
|
let mut page_heap = BinaryHeap::new();
|
||||||
|
for (_, basismap) in self.v2p_map {
|
||||||
|
for (_, pp) in basismap {
|
||||||
|
page_heap.push(Reverse(pp.page_number()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let total_free_pages = page_heap.len();
|
||||||
|
if total_free_pages == 0 {
|
||||||
|
log::warn!("Disk is out of space, no free pages available!");
|
||||||
|
// return an empty free_pool vector.
|
||||||
|
return Vec::<PhysPage>::new();
|
||||||
|
}
|
||||||
|
// 2. fill the free_pool, avoiding entries in the page_heap. This algorithm
|
||||||
|
// uses a fixed amount of storage regardless of the size of the disk, but
|
||||||
|
// it produces a free_pool that has entries biased toward the high
|
||||||
|
// addresses.
|
||||||
|
let mut min_used_page = page_heap.pop();
|
||||||
|
// consider every page
|
||||||
|
for page_candidate in (0..PDDB_A_LEN / PAGE_SIZE) {
|
||||||
|
// if the page is used, skip it.
|
||||||
|
if let Some(Reverse(mp)) = min_used_page {
|
||||||
|
if page_candidate == mp as usize {
|
||||||
|
min_used_page = page_heap.pop();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// page is free. if we've space in the pool, just deposit it there
|
||||||
|
if free_pool.len() < max_entries {
|
||||||
|
free_pool.push(page_candidate);
|
||||||
|
} else {
|
||||||
|
// page is free, but we have no space in the pool.
|
||||||
|
// pick a random page from the pool, and replace it with the current page
|
||||||
|
free_pool[self.trng_cache_u32() as usize % free_pool.len()] = page_candidate;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 3. shuffle the contents of free_pool. This is important in the case that the
|
||||||
|
// amount of free space starts to approach the size of the free pool, as it will
|
||||||
|
// essentially come out of step 2 as a sorted list.
|
||||||
|
// this shuffle is stolen out of the rand crate directly -- it's a small function,
|
||||||
|
// and pulling in the ENTIRE rand crate for this code seemed very unnecessary.
|
||||||
|
// https://github.com/rust-random/rand/blob/0f4fc6b4c303696bd5f8765a375162ac7142b1df/src/seq/mod.rs#L586-L592
|
||||||
|
for i in (1..free_pool.len()).rev() {
|
||||||
|
// invariant: elements with index > i have been locked in place.
|
||||||
|
free_pool.swap(i, self.trng_cache_u32() as usize % (i+1));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. ensure that the free pool stays within the defined deniability ratio
|
||||||
|
let deniable_free_pages = (total_free_pages as f32 * FSCB_FILL_COEFFICIENT) as usize;
|
||||||
|
// we're guarantede to have at least one free page, because we errored out if the pages was 0 above.
|
||||||
|
let deniable_free_pages = if deniable_free_pages == 0 { 1 } else { deniable_free_pages };
|
||||||
|
free_pool.truncate(deniable_free_pages);
|
||||||
|
|
||||||
|
/// TODO: translate the free_pool into a PhysPage vector with the right bitfields attached to it.
|
||||||
|
free_pool
|
||||||
|
}
|
||||||
|
|
||||||
/// this function is dangerous in that calling it will completely erase all of the previous data
|
/// 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.
|
/// 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
|
/// The number of servers that can connect to the Spinor crate is strictly tracked, so we borrow a reference
|
||||||
|
|
@ -107,12 +264,13 @@ impl PddbOs {
|
||||||
log::info!("Erasing the PDDB region");
|
log::info!("Erasing the PDDB region");
|
||||||
let blank_sector: [u8; PAGE_SIZE] = [0xff; PAGE_SIZE];
|
let blank_sector: [u8; PAGE_SIZE] = [0xff; PAGE_SIZE];
|
||||||
|
|
||||||
for offset in (0..xous::PDDB_LEN).step_by(PAGE_SIZE) {
|
// there is no convenience routine for erasing the entire disk. Maybe that's a good thing?
|
||||||
|
for offset in (0..PDDB_A_LEN).step_by(PAGE_SIZE) {
|
||||||
self.spinor.patch(
|
self.spinor.patch(
|
||||||
self.pddb_mr.as_slice(),
|
self.pddb_mr.as_slice(),
|
||||||
xous::PDDB_LOC,
|
xous::PDDB_LOC,
|
||||||
&blank_sector,
|
&blank_sector,
|
||||||
offset
|
offset as u32
|
||||||
).expect("couldn't erase memory");
|
).expect("couldn't erase memory");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -129,7 +287,7 @@ impl PddbOs {
|
||||||
name,
|
name,
|
||||||
age: 0,
|
age: 0,
|
||||||
num_dictionaries: 0,
|
num_dictionaries: 0,
|
||||||
prealloc_open_end: PageAlignedU64::from(INITIAL_BASIS_ALLOC * PAGE_SIZE),
|
prealloc_open_end: PageAlignedVa::from(INITIAL_BASIS_ALLOC * PAGE_SIZE),
|
||||||
};
|
};
|
||||||
// extract a slice-u8 that maps onto the basis_root record, allowing us to patch this into a FLASH page
|
// extract a slice-u8 that maps onto the basis_root record, allowing us to patch this into a FLASH page
|
||||||
let br_slice: &[u8] = basis_root.deref();
|
let br_slice: &[u8] = basis_root.deref();
|
||||||
|
|
@ -158,12 +316,7 @@ impl PddbOs {
|
||||||
}
|
}
|
||||||
self.trng.fill_bytes(&mut crypto_keys.salt_base);
|
self.trng.fill_bytes(&mut crypto_keys.salt_base);
|
||||||
self.trng.fill_bytes(&mut crypto_keys.reserved);
|
self.trng.fill_bytes(&mut crypto_keys.reserved);
|
||||||
self.spinor.patch(
|
self.patch_keys(crypto_keys.deref(), 0);
|
||||||
self.pddb_mr.as_slice(),
|
|
||||||
xous::PDDB_LOC,
|
|
||||||
crypto_keys.deref(),
|
|
||||||
self.key_phys_base.as_u32()
|
|
||||||
).expect("couldn't burn keys");
|
|
||||||
// now we have a copy of the AES key necessary to encrypt the default System basis that we created in step 2.
|
// now we have a copy of the AES key necessary to encrypt the default System basis that we created in step 2.
|
||||||
|
|
||||||
// step 4. Create a hashmap for our reverse PTE, and add it to the Pddb's cache
|
// step 4. Create a hashmap for our reverse PTE, and add it to the Pddb's cache
|
||||||
|
|
@ -192,25 +345,35 @@ impl PddbOs {
|
||||||
if let Some(system_basis_key) = self.system_basis_key {
|
if let Some(system_basis_key) = self.system_basis_key {
|
||||||
let key = Key::from_slice(&system_basis_key);
|
let key = Key::from_slice(&system_basis_key);
|
||||||
let cipher = Aes256GcmSiv::new(key);
|
let cipher = Aes256GcmSiv::new(key);
|
||||||
let nonce = Nonce::from_slice(&basis_root.p_nonce);
|
let nonce_array = self.gen_nonce();
|
||||||
|
let nonce = Nonce::from_slice(&nonce_array);
|
||||||
let ciphertext = cipher.encrypt(nonce, &basis_ser[12..]);
|
let ciphertext = cipher.encrypt(nonce, &basis_ser[12..]);
|
||||||
|
|
||||||
let ct_to_flash: &[u8] = ciphertext.as_ref().unwrap(); // this now contains the encrypted basis + 16-byte tag at the very end
|
let ct_to_flash: &[u8] = ciphertext.as_ref().unwrap(); // this now contains the encrypted basis + 16-byte tag at the very end
|
||||||
assert!( ( ct_to_flash.len() + basis_root.p_nonce.len()) & (PAGE_SIZE - 1) == 0, "Padding failure during basis serialization!");
|
assert!( ( ct_to_flash.len() + basis_root.p_nonce.len()) & (PAGE_SIZE - 1) == 0, "Padding failure during basis serialization!");
|
||||||
// we're now ready to write the encrypted basis to Flash.
|
// we're now ready to write the encrypted basis to Flash.
|
||||||
self.spinor.patch(
|
self.patch_data(&[&nonce_array, ct_to_flash].concat(), 0);
|
||||||
self.pddb_mr.as_slice(),
|
|
||||||
xous::PDDB_LOC,
|
// now fill in the rest of Flash with random data. This includes filling in the current Basis allocation
|
||||||
&[&basis_root.p_nonce, ct_to_flash].concat(),
|
// with random data, as that is the "free" state (as well as, at least facially, the "used" state)
|
||||||
self.data_phys_base.as_u32()
|
let start_offset = PageAlignedPa::from(ct_to_flash.len() + basis_root.p_nonce.len());
|
||||||
).expect("couldn't write basis structure");
|
let mut erase_buf: [u8; 4096] = [0; 4096];
|
||||||
// now fill in the rest of Flash with random data. Because the rest of the Basis is blank, random data is "just fine".
|
for page_offset in (start_offset.as_u32()..PDDB_A_LEN as u32).step_by(PAGE_SIZE) {
|
||||||
// LEFT OFF HERE
|
self.trng.fill_bytes(&mut erase_buf);
|
||||||
|
self.patch_data(&erase_buf, page_offset);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
panic!("invalid state"); // we should never hit this because we created the key earlier in the same routine.
|
panic!("invalid state"); // we should never hit this because we created the key earlier in the same routine.
|
||||||
}
|
}
|
||||||
|
|
||||||
// step 6. generate & write initial page table entries
|
// step 6. mbbb handling
|
||||||
|
// mbbb should just be blank at this point, so there's nothing to do.
|
||||||
|
|
||||||
|
// step 7. fscb handling
|
||||||
|
// pick a set of random pages from the free pool and assign it to the fscb
|
||||||
|
let page_state = Vec::<u8>::new(); // bools also take up a byte
|
||||||
|
|
||||||
|
// step 8. generate & write initial page table entries
|
||||||
// page table organization:
|
// page table organization:
|
||||||
//
|
//
|
||||||
// offset from |
|
// offset from |
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,6 @@ use core::num::NonZeroU32;
|
||||||
use core::num::NonZeroU64;
|
use core::num::NonZeroU64;
|
||||||
|
|
||||||
use bitflags::bitflags;
|
use bitflags::bitflags;
|
||||||
use bitfield::bitfield;
|
|
||||||
|
|
||||||
bitflags! {
|
bitflags! {
|
||||||
/// flags used by the page table
|
/// flags used by the page table
|
||||||
|
|
@ -50,17 +49,7 @@ pub(crate) struct Pte {
|
||||||
checksum: [u8; 4],
|
checksum: [u8; 4],
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Physical page information, coded as a bitfield, because space is a premium!
|
pub const PDDB_SIZE_PAGES: usize = crate::PDDB_A_LEN as usize / PAGE_SIZE;
|
||||||
bitfield! {
|
|
||||||
#[derive(Copy, Clone, Hash)]
|
|
||||||
pub struct PhysPage(u32);
|
|
||||||
impl Debug;
|
|
||||||
pub page_number, set_page_number: 20, 0;
|
|
||||||
pub clean, set_clean: 21;
|
|
||||||
pub valid, set_valid: 22;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub const PDDB_SIZE_PAGES: usize = xous::PDDB_LEN as usize / PAGE_SIZE;
|
|
||||||
/// This structure is mapped into the top of FLASH memory, starting at
|
/// This structure is mapped into the top of FLASH memory, starting at
|
||||||
/// xous::PDDB_LOC. This actually slightly over-sizes the page table,
|
/// xous::PDDB_LOC. This actually slightly over-sizes the page table,
|
||||||
/// because the page table does not map the locations for the page table
|
/// because the page table does not map the locations for the page table
|
||||||
|
|
|
||||||
|
|
@ -2,139 +2,122 @@ use core::num::{NonZeroU32, NonZeroU64};
|
||||||
use core::convert::TryFrom;
|
use core::convert::TryFrom;
|
||||||
use core::ops::Add;
|
use core::ops::Add;
|
||||||
use super::PAGE_SIZE;
|
use super::PAGE_SIZE;
|
||||||
/// PAGE_SIZE is required to be a power of two. 0x1000 -> 0x1000 - 1 = 0xFFF, which forms the bitmasks below.
|
use bitfield::bitfield;
|
||||||
#[derive(Copy, Clone)]
|
|
||||||
pub(crate) struct PageAlignedU64(u64);
|
/// We should be able to change this to a u64 and everything should "just work", but
|
||||||
impl PageAlignedU64 {
|
/// we'd end up using 2x the amount of data for overhead and bookkeeping.
|
||||||
pub(crate) fn as_u32(&self) -> u32 {
|
#[cfg(not(feature = "u64_pa"))]
|
||||||
if self.0 <= u32::MAX as u64 {
|
pub type PhysAddr = u32;
|
||||||
self.0 as u32
|
#[cfg(feature = "u64_pa")]
|
||||||
} else {
|
pub type PhysAddr = u64;
|
||||||
panic!("This PageAlignedU64 would not fit into a PageAlignedU32");
|
const BITFIELD_PAGE_WIDTH: usize = (core::mem::size_of::<PhysAddr>() * 8 - 12);
|
||||||
}
|
/// Physical page information, coded as a bitfield, because space is a premium!
|
||||||
}
|
bitfield! {
|
||||||
pub(crate) fn as_u64(&self) -> u64 {self.0}
|
#[derive(Copy, Clone, Hash)]
|
||||||
}
|
pub struct PhysPage(PhysAddr);
|
||||||
impl From<u64> for PageAlignedU64 {
|
impl Debug;
|
||||||
fn from(arg: u64) -> Self {
|
pub page_number, set_page_number: BITFIELD_PAGE_WIDTH - 1, 0;
|
||||||
if arg & (PAGE_SIZE as u64 - 1) == 0 {
|
pub clean, set_clean: BITFIELD_PAGE_WIDTH + 0;
|
||||||
PageAlignedU64(arg & !(PAGE_SIZE as u64 - 1))
|
pub valid, set_valid: BITFIELD_PAGE_WIDTH + 1;
|
||||||
} else {
|
// these are only used by the FastSpace mechanism; they have no meaning in other contexts
|
||||||
PageAlignedU64((arg & !(PAGE_SIZE as u64 - 1)) + PAGE_SIZE as u64)
|
pub space_state, set_space_state: BITFIELD_PAGE_WIDTH + 3, BITFIELD_PAGE_WIDTH + 2;
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl From<PageAlignedU32> for PageAlignedU64 {
|
|
||||||
fn from(arg: PageAlignedU32) -> Self { PageAlignedU64(arg.0 as u64) } // already aligned, and it fits.
|
|
||||||
}
|
|
||||||
impl From<u32> for PageAlignedU64 {
|
|
||||||
fn from(arg: u32) -> Self {
|
|
||||||
if arg & (PAGE_SIZE as u32 - 1) == 0 {
|
|
||||||
PageAlignedU64(arg as u64 & !(PAGE_SIZE as u64 - 1))
|
|
||||||
} else {
|
|
||||||
PageAlignedU64((arg as u64 & !(PAGE_SIZE as u64 - 1)) + PAGE_SIZE as u64)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl From<usize> for PageAlignedU64 {
|
|
||||||
fn from(arg: usize) -> Self {
|
|
||||||
if arg & (PAGE_SIZE - 1) == 0 {
|
|
||||||
PageAlignedU64(arg as u64 & !(PAGE_SIZE as u64 - 1))
|
|
||||||
} else {
|
|
||||||
PageAlignedU64((arg as u64 & !(PAGE_SIZE as u64 - 1)) + PAGE_SIZE as u64)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl Add for PageAlignedU64 {
|
|
||||||
type Output = PageAlignedU64;
|
|
||||||
fn add(self, other: PageAlignedU64) -> PageAlignedU64 {
|
|
||||||
PageAlignedU64(self.0 + other.0)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
|
||||||
pub(crate) struct PageAlignedU32(u32);
|
|
||||||
impl PageAlignedU32 {
|
|
||||||
pub(crate) fn as_u32(&self) -> u32 {self.0}
|
|
||||||
pub(crate) fn as_u64(&self) -> u64 {self.0 as u64}
|
|
||||||
}
|
|
||||||
impl From<u32> for PageAlignedU32 {
|
|
||||||
fn from(arg: u32) -> Self {
|
|
||||||
if arg & (PAGE_SIZE as u32 - 1) == 0 {
|
|
||||||
PageAlignedU32(arg & !(PAGE_SIZE as u32 - 1))
|
|
||||||
} else {
|
|
||||||
PageAlignedU32((arg & !(PAGE_SIZE as u32 - 1)) + PAGE_SIZE as u32)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl From<usize> for PageAlignedU32 {
|
|
||||||
fn from(arg: usize) -> Self {
|
|
||||||
if arg & (PAGE_SIZE as usize - 1) == 0 {
|
|
||||||
PageAlignedU32(arg as u32 & !(PAGE_SIZE as u32 - 1))
|
|
||||||
} else {
|
|
||||||
PageAlignedU32((arg as u32 & !(PAGE_SIZE as u32 - 1)) + PAGE_SIZE as u32)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/* // let's get real, this is kind of a waste of cycles to check this...
|
|
||||||
impl TryFrom<usize> for PageAlignedU32 {
|
|
||||||
type Error = &'static str;
|
|
||||||
fn try_from(arg: usize) -> Result<Self, Self::Error> {
|
|
||||||
if arg > u32::MAX as usize {
|
|
||||||
Err("PageAlignedU32 only accepts usize less than u32::MAX")
|
|
||||||
} else {
|
|
||||||
Ok(
|
|
||||||
if arg & (PAGE_SIZE - 1) == 0 {
|
|
||||||
PageAlignedU32(arg as u32 & !(PAGE_SIZE as u32 - 1))
|
|
||||||
} else {
|
|
||||||
PageAlignedU32((arg as u32 & !(PAGE_SIZE as u32 - 1)) + PAGE_SIZE as u32)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
impl From<PageAlignedU32> for u32 {
|
|
||||||
fn from(arg: PageAlignedU32) -> Self {
|
|
||||||
arg.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl Add for PageAlignedU32 {
|
|
||||||
type Output = PageAlignedU32;
|
|
||||||
fn add(self, other: PageAlignedU32) -> PageAlignedU32 {
|
|
||||||
PageAlignedU32(self.0 + other.0 as u32)
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
fn add(self, other: usize) -> PageAlignedU32 {
|
|
||||||
let page_aligned_other = if other & (PAGE_SIZE - 1) == 0 {
|
|
||||||
other & !(PAGE_SIZE - 1)
|
|
||||||
} else {
|
|
||||||
(other & !(PAGE_SIZE - 1)) + PAGE_SIZE
|
|
||||||
};
|
|
||||||
// saturate adds
|
|
||||||
if self.0 as usize + page_aligned_other > u32::MAX as usize {
|
|
||||||
PageAlignedU32(u32::MAX)
|
|
||||||
} else {
|
|
||||||
PageAlignedU32(self.0 + page_aligned_other as u32)
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
#[derive(Hash, Copy, Clone, PartialEq, Eq)]
|
|
||||||
pub struct PhysAddr(NonZeroU32);
|
|
||||||
impl From<u32> for PhysAddr {
|
|
||||||
fn from(arg: u32) -> Self {
|
|
||||||
PhysAddr( NonZeroU32::try_from(arg).expect("Physical addresses must not be zero") )
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
pub type VirtAddr = u64;
|
pub type VirtAddr = u64;
|
||||||
|
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
|
pub(crate) struct PageAlignedVa(VirtAddr);
|
||||||
|
impl PageAlignedVa {
|
||||||
|
pub(crate) fn as_u32(&self) -> u32 {
|
||||||
|
if self.0 <= u32::MAX as VirtAddr {
|
||||||
|
self.0 as u32
|
||||||
|
} else {
|
||||||
|
panic!("This PageAlignedVa would not fit into a PageAlignedPa");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub(crate) fn as_u64(&self) -> u64 {self.0 as u64}
|
||||||
|
pub(crate) fn as_usize(&self) -> usize {self.0 as usize}
|
||||||
|
}
|
||||||
|
impl From<u64> for PageAlignedVa {
|
||||||
|
fn from(arg: u64) -> Self {
|
||||||
|
if arg & (PAGE_SIZE as u64 - 1) == 0 {
|
||||||
|
PageAlignedVa(arg & !(PAGE_SIZE as VirtAddr - 1))
|
||||||
|
} else {
|
||||||
|
PageAlignedVa((arg & !(PAGE_SIZE as VirtAddr - 1)) + PAGE_SIZE as VirtAddr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl From<PageAlignedPa> for PageAlignedVa {
|
||||||
|
fn from(arg: PageAlignedPa) -> Self { PageAlignedVa(arg.0 as VirtAddr) } // already aligned, and it fits.
|
||||||
|
}
|
||||||
|
impl From<u32> for PageAlignedVa {
|
||||||
|
fn from(arg: u32) -> Self {
|
||||||
|
if arg & (PAGE_SIZE as u32 - 1) == 0 {
|
||||||
|
PageAlignedVa(arg as VirtAddr & !(PAGE_SIZE as VirtAddr - 1))
|
||||||
|
} else {
|
||||||
|
PageAlignedVa((arg as VirtAddr & !(PAGE_SIZE as VirtAddr - 1)) + PAGE_SIZE as VirtAddr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl From<usize> for PageAlignedVa {
|
||||||
|
fn from(arg: usize) -> Self {
|
||||||
|
if arg & (PAGE_SIZE - 1) == 0 {
|
||||||
|
PageAlignedVa(arg as VirtAddr & !(PAGE_SIZE as VirtAddr - 1))
|
||||||
|
} else {
|
||||||
|
PageAlignedVa((arg as VirtAddr & !(PAGE_SIZE as VirtAddr - 1)) + PAGE_SIZE as VirtAddr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Add for PageAlignedVa {
|
||||||
|
type Output = PageAlignedVa;
|
||||||
|
fn add(self, other: PageAlignedVa) -> PageAlignedVa {
|
||||||
|
PageAlignedVa(self.0 + other.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
|
pub(crate) struct PageAlignedPa(PhysAddr);
|
||||||
|
impl PageAlignedPa {
|
||||||
|
pub(crate) fn as_u32(&self) -> u32 {self.0 as u32}
|
||||||
|
pub(crate) fn as_u64(&self) -> u64 {self.0 as u64}
|
||||||
|
pub(crate) fn as_usize(&self) -> usize {self.0 as usize}
|
||||||
|
}
|
||||||
|
impl From<u32> for PageAlignedPa {
|
||||||
|
fn from(arg: u32) -> Self {
|
||||||
|
if arg & (PAGE_SIZE as u32 - 1) == 0 {
|
||||||
|
PageAlignedPa( (arg & !(PAGE_SIZE as u32 - 1)) as PhysAddr )
|
||||||
|
} else {
|
||||||
|
PageAlignedPa( ((arg & !(PAGE_SIZE as u32 - 1)) + PAGE_SIZE as u32) as PhysAddr )
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl From<usize> for PageAlignedPa {
|
||||||
|
fn from(arg: usize) -> Self {
|
||||||
|
if arg & (PAGE_SIZE as usize - 1) == 0 {
|
||||||
|
PageAlignedPa( (arg as u32 & !(PAGE_SIZE as u32 - 1)) as PhysAddr )
|
||||||
|
} else {
|
||||||
|
PageAlignedPa( ((arg as u32 & !(PAGE_SIZE as u32 - 1)) + PAGE_SIZE as u32) as PhysAddr )
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl From<PageAlignedPa> for u32 {
|
||||||
|
fn from(arg: PageAlignedPa) -> Self {
|
||||||
|
arg.0 as u32
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Add for PageAlignedPa {
|
||||||
|
type Output = PageAlignedPa;
|
||||||
|
fn add(self, other: PageAlignedPa) -> PageAlignedPa {
|
||||||
|
PageAlignedPa(self.0 + other.0 as PhysAddr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
#[test]
|
#[test]
|
||||||
|
/// PAGE_SIZE is required to be a power of two. 0x1000 -> 0x1000 - 1 = 0xFFF, which forms the bitmasks.
|
||||||
fn test_page_size() {
|
fn test_page_size() {
|
||||||
assert!(PAGE_SIZE & !(PAGE_SIZE - 1) == 0, "PAGE_SIZE is not a power of two!");
|
assert!(PAGE_SIZE & !(PAGE_SIZE - 1) == 0, "PAGE_SIZE is not a power of two!");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,34 @@ use xous::{msg_blocking_scalar_unpack, msg_scalar_unpack};
|
||||||
/// settings:wifi/Kosagi.json
|
/// settings:wifi/Kosagi.json
|
||||||
/// Would be deconstructed into the "settings" dictionary with a key of wifi/Kosagi.json.
|
/// Would be deconstructed into the "settings" dictionary with a key of wifi/Kosagi.json.
|
||||||
///
|
///
|
||||||
|
///
|
||||||
|
/// Threat model:
|
||||||
|
///
|
||||||
|
/// The user is forced to divulge "all the Basis passwords" on the device, through
|
||||||
|
/// coercion, blackmail, subpoena, customs checkpoint etc. The adversary has physical
|
||||||
|
/// access to the device, and is able to take a static disk image; they may even have
|
||||||
|
/// the opportunity to take several disk images over time and diff the images.
|
||||||
|
/// The adversary may be able to decrypt the root key of the cryptographic enclave.
|
||||||
|
/// The adversary may also be able to observe the contents of encrypted data within the
|
||||||
|
/// "system" basis, which includes some of the bookkeeping information for the PDDB,
|
||||||
|
/// as the user will have at least been forced to divulge the system basis password as
|
||||||
|
/// this is a password that every device requires and they cannot deny its existence.
|
||||||
|
///
|
||||||
|
/// Under these conditions, it should be impossible for the adversary to conclusively prove or
|
||||||
|
/// disprove that every Basis password has been presented and unlocked for inspection
|
||||||
|
/// through forensic analysis of the PDDB alone (significantly, we cannot prevent disclosure
|
||||||
|
/// by poorly constructed end-user applications storing things like "last opened Basis"
|
||||||
|
/// convenience lists, or if the user themselves wrote a note referring to a secret
|
||||||
|
/// Basis in a less secret area). Furthermore, if a device is permanently seized by
|
||||||
|
/// the adversary for extensive analysis, any Basis whose password that has not been
|
||||||
|
/// voluntarily disclosed should be "as good as deleted".
|
||||||
|
///
|
||||||
|
/// The PDDB also cannot protect against key loggers or surveillance cameras recording
|
||||||
|
/// key strokes as the user operates the device. Resistance to key loggers is instead
|
||||||
|
/// a problem handled by the OS, and it is up to the user to not type secret passwords
|
||||||
|
/// in areas that may be under camera surveillance.
|
||||||
|
///
|
||||||
|
///
|
||||||
/// General Operation:
|
/// General Operation:
|
||||||
///
|
///
|
||||||
/// The initial Basis is known as the "System" Basis. It's a low-security framework basis
|
/// The initial Basis is known as the "System" Basis. It's a low-security framework basis
|
||||||
|
|
@ -97,7 +125,7 @@ const PDDB_BACKING_SIZE: usize = 0x4000_0000;
|
||||||
/// an encrypted, plausibly deniable filesystem, as you have to trade-off the side channel of partially
|
/// an encrypted, plausibly deniable filesystem, as you have to trade-off the side channel of partially
|
||||||
/// erased blocks versus the finite write lifetime of flash storage.
|
/// erased blocks versus the finite write lifetime of flash storage.
|
||||||
///
|
///
|
||||||
/// When a Basis is created, it starts with 64k of FLASH allocated to it. Most of this is blank (but encrypted
|
/// When a Basis is created, it starts with 64k of space allocated to it. Most of this is blank (but encrypted
|
||||||
/// in flash), but it is important in a PD filesystem to claim some freespace for day-to-day operations. This
|
/// in flash), but it is important in a PD filesystem to claim some freespace for day-to-day operations. This
|
||||||
/// is because when you need to allocate more data, you have to make a guess as to what is free, and what
|
/// is because when you need to allocate more data, you have to make a guess as to what is free, and what
|
||||||
/// previously allocated but currently denied to exist (locked Basis look identical to free space when locked).
|
/// previously allocated but currently denied to exist (locked Basis look identical to free space when locked).
|
||||||
|
|
@ -109,9 +137,17 @@ const PDDB_BACKING_SIZE: usize = 0x4000_0000;
|
||||||
/// Blocks on disk are mapped into a virtual PDDB space via a page table. The virtual PDDB space is 64-bit.
|
/// Blocks on disk are mapped into a virtual PDDB space via a page table. The virtual PDDB space is 64-bit.
|
||||||
/// Each Basis gets its own 48-bit address space, allowing for up to 64k Basis.
|
/// Each Basis gets its own 48-bit address space, allowing for up to 64k Basis.
|
||||||
/// The PDDB page table guarantees that no matter the order of allocation in FLASH, a Basis can linearly
|
/// The PDDB page table guarantees that no matter the order of allocation in FLASH, a Basis can linearly
|
||||||
/// access its blocks by doing a page table lookup. A reverse-lookup structure is constructed on boot
|
/// access its blocks through a page table lookup. A reverse-lookup structure is constructed on boot
|
||||||
/// by scanning the entire PDDB, and creating a HashMap of (basis, offset) -> pddb_addr tuples.
|
/// by scanning the entire PDDB, and creating a HashMap of (basis, offset) -> pddb_addr tuples.
|
||||||
///
|
///
|
||||||
|
/// Physical addresses can be 32 or 64 bit based upon the specific implementation. The physical address
|
||||||
|
/// type is defined in types.rs. For Precursor, it's defined as a u32, which limits the physical address
|
||||||
|
/// size to 32 bits. This is important because Precursor operates out of a very small amount of RAM,
|
||||||
|
/// and doubling the size of the overhead bookkeeping structures to handle disk sizes that are well
|
||||||
|
/// beyond the current implementation has a real impact on the system's free memory footprint. However,
|
||||||
|
/// the PDDB is coded in a way such that one "should" be able to swap out the u32 for a u64 in the
|
||||||
|
/// "newtype" definition for a PhysAddr, and the system will work.
|
||||||
|
///
|
||||||
/// Page table entries don't define themselves with a Basis -- every time a Basis is opened, the entire
|
/// Page table entries don't define themselves with a Basis -- every time a Basis is opened, the entire
|
||||||
/// table must be scanned, brute-force decrypting every entry against the Basis key, and seeing if the
|
/// table must be scanned, brute-force decrypting every entry against the Basis key, and seeing if the
|
||||||
/// checksum in the table entry matches the address. If there's a match, then that entry is recorded as
|
/// checksum in the table entry matches the address. If there's a match, then that entry is recorded as
|
||||||
|
|
@ -134,7 +170,6 @@ const PDDB_BACKING_SIZE: usize = 0x4000_0000;
|
||||||
/// to all 1's) in a circular buffer basis. The state of where the .FastSpace record is in the "clean" pages
|
/// to all 1's) in a circular buffer basis. The state of where the .FastSpace record is in the "clean" pages
|
||||||
/// disclose nothing other than the fact that the system has been used.
|
/// disclose nothing other than the fact that the system has been used.
|
||||||
///
|
///
|
||||||
/// TODO: define .FastSpace records and structures.
|
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
/// Basis Deniability
|
/// Basis Deniability
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue