diff --git a/Cargo.lock b/Cargo.lock index a38851bad..2936c19c9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1383,10 +1383,13 @@ dependencies = [ "num-derive", "num-traits", "rkyv", + "spinor", "susres", "ticktimer-server", + "trng", "utralib", "xous", + "xous-ipc", "xous-names", ] @@ -1642,7 +1645,6 @@ dependencies = [ "memoffset", "ptr_meta", "rkyv_derive", - "seahash", ] [[package]] @@ -1822,12 +1824,6 @@ dependencies = [ "version-compare", ] -[[package]] -name = "seahash" -version = "4.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c107b6f4780854c8b126e228ea8869f4d7b71260f962fefb57b996b8959ba6b" - [[package]] name = "semver" version = "1.0.4" diff --git a/services/pddb/Cargo.toml b/services/pddb/Cargo.toml index 9ed0089f8..4c266daeb 100644 --- a/services/pddb/Cargo.toml +++ b/services/pddb/Cargo.toml @@ -7,6 +7,7 @@ description = "Plausibly Deniable Database" [dependencies] xous = { path = "../../xous-rs" } +xous-ipc = { path = "../../xous-ipc" } log-server = { path = "../log-server" } ticktimer-server = { path = "../ticktimer-server" } xous-names = { path = "../xous-names" } @@ -14,7 +15,9 @@ log = "0.4" num-derive = {version = "0.3", default-features = false} num-traits = {version = "0.2", default-features = false} susres = {path = "../susres"} -rkyv = "0.4.3" +rkyv = {version = "0.4.3", features = ["const_generics"], default-features = false} +trng = {path = "../trng"} +spinor = {path = "../spinor"} [target.'cfg(not(any(windows,unix)))'.dependencies] utralib = { path = "../../utralib"} diff --git a/services/pddb/src/api.rs b/services/pddb/src/api.rs index c98cb943b..6bdc4d806 100644 --- a/services/pddb/src/api.rs +++ b/services/pddb/src/api.rs @@ -3,6 +3,7 @@ pub(crate) const SERVER_NAME_PDDB: &str = "_Plausibly Deniable Database_"; #[allow(dead_code)] #[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] pub(crate) enum Opcode { + KeyRequest, /// Suspend/resume callback SuspendResume, diff --git a/services/pddb/src/frontend/pddbkey.rs b/services/pddb/src/frontend/pddbkey.rs index bd86b5efe..6808b216f 100644 --- a/services/pddb/src/frontend/pddbkey.rs +++ b/services/pddb/src/frontend/pddbkey.rs @@ -1,9 +1,20 @@ -use std::io::{Error, ErrorKind, Result}; -use xous::MemoryRange; -use std::io::SeekFrom; +//use std::io::{Error, ErrorKind, Result}; +//use xous::MemoryRange; +//use std::io::SeekFrom; -use crate::PddbDict; +//use crate::*; +use rkyv::{Archive, Deserialize, Serialize}; +use xous_ipc::String; +#[derive(Debug, Archive, Serialize, Deserialize, Copy, Clone)] +pub(crate) struct PddbKeyRequest { + pub(crate) dict: String::, // pending https://github.com/rust-lang/rust/issues/90195 + pub(crate) key: String::, // pending https://github.com/rust-lang/rust/issues/90195 + pub(crate) token: Option<[u32; 3]>, +} + + +/* /// PddbKey is somewhat isomorphic to a File in Rust, in that it provides slices of [u8] that /// can be `read()`, `write()` and `seek()`. /// this is definitely a user-facing structure @@ -42,3 +53,5 @@ impl<'a> PddbKey<'a> { pub fn seek(&mut self, pos: SeekFrom) -> Result { Ok(0) } */ } + +*/ \ No newline at end of file diff --git a/services/pddb/src/lib.rs b/services/pddb/src/lib.rs index f21690951..627e32478 100644 --- a/services/pddb/src/lib.rs +++ b/services/pddb/src/lib.rs @@ -6,89 +6,77 @@ pub mod frontend; pub use frontend::*; use xous::{CID, send_message}; +use xous_ipc::Buffer; + use num_traits::*; +use std::io::{Result, Error, ErrorKind}; +use std::path::Path; +use std::format; +pub const PDDB_MAX_DICT_NAME_LEN: usize = 64; +pub const PDDB_MAX_KEY_NAME_LEN: usize = 256; -/// This constant maps onto a region that's "unused" by Xous, and claimable by user -/// processes. -const PDDB_BACKING_BASE: usize = 0x8000_0000; -/// This designates the largest contiguous extent that we could allocate for a file. -/// The backing isn't allocated -- it is merely reserved. Accesses to the backing trigger -/// page faults that are handled by the PDDB, and just the pages actively being dereferenced is -/// swapped into physical memory on demand. This does put a pretty hard upper limit on file sizes -/// on a 32-bit system, but PDDB is coded such that we could extend to a 64-bit system and -/// increase this limit my changing the constants here. -const PDDB_BACKING_SIZE: usize = 0x4000_0000; - -fn handle_exception(exception: xous::Exception) -> isize { - use xous::Exception::*; - match exception { - IllegalInstruction(epc, instruction) => { - println!( - "Caught illegal instruction {:08x} at {:08x}, just skipping it...", - epc, instruction - ); - 4 - } - InstructionAddressMisaligned(epc, addr) => { - println!( - "Misaligned instruction at {:08x} ({:08x}), trying to nudge forward a bit", - epc, addr - ); - 1 - } - InstructionAccessFault(epc, addr) - | LoadAccessFault(epc, addr) - | StoreAccessFault(epc, addr) => { - panic!( - "Access fault of some sort: ({:08x} {:08x}): {:?}", - epc, addr, epc - ); - } - LoadAddressMisaligned(epc, addr) | StoreAddressMisaligned(epc, addr) => { - println!( - "Load was misaligned ({:08x} @ {:08x}). Just skipping the instruction and hoping nobody notices.", epc, addr - ); - 4 - } - InstructionPageFault(epc, addr) | LoadPageFault(epc, addr) | StorePageFault(epc, addr) => { - println!("Memory access fault at address 0x{:08x} (pc is at 0x{:08x}), allocating a new page", addr, epc); - let base = addr & !4095; - let size = 4096; - xous::map_memory( - None, - xous::MemoryAddress::new(base), - size, - xous::MemoryFlags::R | xous::MemoryFlags::W, - ) - .expect("couldn't map memory"); - // Retry the instruction - 0 - } - Unknown(a1, a2, a3) => panic!("unknown exception: {:08x} {:08x} {:08x}", a1, a2, a3), - } -} - -pub struct Pddb { +pub struct PddbKey { conn: CID, - backing: MemoryRange, + dict: String, + key: String, + token: [u32; 3], } -impl Pddb { - pub fn new(xns: &xous_names::XousNames) -> core::result::Result { +impl PddbKey { + pub fn get>(path: P) -> Result { + let xns = xous_names::XousNames::new().unwrap(); REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); let conn = xns.request_connection_blocking(api::SERVER_NAME_PDDB).expect("Can't connect to Pddb server"); - xous::syscall::set_exception_handler(handle_exception).unwrap(); + if !path.is_absolute() { + return Err(Error::new(ErrorKind::InvalidInput, "All Xous keys must be fully specified relative to a dictionary")); + } + let mut dict = String::new(); + let mut key = String::new(); + if let Some(pathstr) = path.to_str() { + if let Some(path_lstrip) = path.strip_prefix("/") { + if let Some((dictstr, keystr)) = path_lstrip.split_once("/") { + if dictstr.len() < PDDB_MAX_DICT_NAME_LEN { + dict.push_str(dictstr); + } else { + return Err(Error::new(ErrorKind::InvalidInput, format!("Xous dictionary names must be shorter than {} bytes", PDDB_MAX_DICT_NAME_LEN))); + } + if keystr.len() < PDDB_MAX_KEY_NAME_LEN { + key.push_str(keystr); + } else { + return Err(Error::new(ErrorKind::InvalidInput, format!("Xous key names must be shorter than {} bytes", PDDB_MAX_DICT_NAME_LEN))); + } + } else { + return Err(Error::new(ErrorKind::InvalidInput, "All Xous keys must be of the format /dict/key; the key may contain more /'s")); + } + } else { + return Err(Error::new(ErrorKind::InvalidInput, "All Xous keys must be absolute and start with a /")); + } + } else { + return Err(Error::new(ErrorKind::InvalidInput, "All Xous keys must be valid UTF-8")); + } - let backing = unsafe {MemoryRange::new( - 0x8000_0000, - 0x1000_0000, - ).expect("couldn't reserve pages for file backing region") }; + let request = PddbKeyRequest { + dict: xous_ipc::String::::from_str(dict.as_str()), + key: xous_ipc::String::::from_str(key.as_str()), + token: None, + }; + let mut buf = Buffer::into_buf(request) + .or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?; + buf.lend_mut(conn, Opcode::KeyRequest.to_u32().unwrap()) + .or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?; - Ok(Pddb { - conn, - backing - }) + let response = buf.to_original::().unwrap(); + if let Some(token) = response.token { + Ok(PddbKey { + conn, + dict, + key, + token, + }) + } else { + Err(Error::new(ErrorKind::PermissionDenied, "Dict/Key access denied")) + } } pub(crate) fn conn(&self) -> CID { @@ -98,7 +86,7 @@ impl Pddb { use core::sync::atomic::{AtomicU32, Ordering}; static REFCOUNT: AtomicU32 = AtomicU32::new(0); -impl Drop for Pddb { +impl Drop for PddbKey { fn drop(&mut self) { // the connection to the server side must be reference counted, so that multiple instances of this object within // a single process do not end up de-allocating the CID on other threads before they go out of scope. @@ -118,7 +106,6 @@ impl Drop for Pddb { use xous::MemoryRange; use std::collections::HashMap; use std::io::SeekFrom; -use std::io::{Error, ErrorKind, Result}; // this is an intenal structure for managing the overall PDDB pub(crate) struct PddbManager { @@ -141,7 +128,7 @@ impl PddbBasis { // this structure can be shared on the user side? pub struct PddbDict<'a> { - contents: HashMap, &'a [u8]>, + contents: HashMap, callback: Box, } impl<'a> PddbDict<'a> { diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index dcf1deb77..a7f06f086 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -2,9 +2,79 @@ #![cfg_attr(target_os = "none", no_main)] mod api; +use api::*; use num_traits::*; +/// PDDB - Plausibly Deniable DataBase +/// +/// Glossary: +/// Basis - a unionizeable dictionary that can be unlocked with a password-protected key. +/// Dictionary - similar to a "volume" on a typical filesystem. Contains a group of k/v pairs +/// that share attributes such as access permissions +/// Key - a unique string that identifies a piece of data in the PDDB +/// Path - Rust's notion of how to locate a file, except we interpret it as a PDDB key, where +/// the "root" directory is the dictionary, and the remainder is the "key" +/// Value - the value associated with a key, isomorphic ot a "file" in Rust +/// Open Basis - A Basis that is decryptable and known to the system. +/// Closed Basis - A Basis that is not decryptable. It's unknown to the system and potentially treated +/// identically to free disk space, e.g., it could be partially overwritten. +/// +/// A `Path` like the following is deconstructed as follows by the PDDB: +/// +/// Dictionary +/// | Key +/// | | +/// --+- --------+--------------------- +/// /logs/matrix/alice/oct30_2021/bob.txt +/// +/// It could equally have an arbitrary name like "logs/Matrix - alice to bob Oct 30 2021"; +/// as long as the string that identifies a Key is unique, it's stored in the database +/// all the same. Any valid utf-8 unicode characters are acceptable. +/// +/// Likewise, something like this: +/// /settings/wifi/Kosagi.json +/// Would be deconstructed into the "settings" dictionary with a key of wifi/Kosagi.json. +/// +/// General Operation: +/// +/// The initial Basis is known as the "System" Basis. It's a low-security framework basis +/// onto which all other Basis overlay. The initial set of Dictionaries, along with their +/// Key/Value pairs, are available to any and all processes. +/// +/// Each new Basis opened will contain zero or more dictionaries. If the dictionary within +/// the new Basis has the same name as an existing dictionary, the Keys are searched in +/// the reverse order of opening. In other words, a new Basis can temporarily override or +/// mask existing Keys. When updating a Key, one may specify the following modes of +/// operation: +/// +/// - UpdateLatest: updates only the copy in the latest Basis to be opened +/// - UpdateOpened: updates copies in other currently opened Basis +/// +/// Note that `UpdateOpened` is dangerous in the sense that if you have a Basis that is +/// is currently Closed, it cannot update copies in the closed Basis. Thus any global +/// update of database schema requires a user to open any and all knows Basis so that +/// synchronization can be maintained. +/// +/// Furthermore, it is possible for a Basis to be closed on a "File" that is currently open. +/// In this case, two things happen: +/// - If a notification callback is registered, it's pinged by the PDDB. The notification +/// of closure callback is an additional feature to the typical Rust File interface. +/// - If the client attempts to read or write to any keys that span a Basis modification, +/// the now-ambiguous key operation will return a `BrokenPipe` error to the caller. + + +/// This constant maps onto a region that's "unused" by Xous, and claimable by user +/// processes. +const PDDB_BACKING_BASE: usize = 0x8000_0000; +/// This designates the largest contiguous extent that we could allocate for a file. +/// The backing isn't allocated -- it is merely reserved. Accesses to the backing trigger +/// page faults that are handled by the PDDB, and just the pages actively being dereferenced is +/// swapped into physical memory on demand. This does put a pretty hard upper limit on file sizes +/// on a 32-bit system, but PDDB is coded such that we could extend to a 64-bit system and +/// increase this limit my changing the constants here. +const PDDB_BACKING_SIZE: usize = 0x4000_0000; + #[xous::xous_main] fn xmain() -> ! { log_server::init_wait().unwrap(); @@ -24,7 +94,10 @@ fn xmain() -> ! { loop { let msg = xous::receive_message(pddb_sid).unwrap(); match FromPrimitive::from_usize(msg.body.id()) { - Some(api::Opcode::SuspendResume) => xous::msg_scalar_unpack!(msg, token, _, _, _, { + Some(Opcode::KeyRequest) => { + // placeholder + } + Some(Opcode::SuspendResume) => xous::msg_scalar_unpack!(msg, token, _, _, _, { /* pddb.suspend(); susres.suspend_until_resume(token).expect("couldn't execute suspend/resume"); pddb.resume(); */