diff --git a/services/pddb/src/api.rs b/services/pddb/src/api.rs index 6bdc4d806..8a6e67dac 100644 --- a/services/pddb/src/api.rs +++ b/services/pddb/src/api.rs @@ -1,10 +1,75 @@ -pub(crate) const SERVER_NAME_PDDB: &str = "_Plausibly Deniable Database_"; +use rkyv::{Archive, Deserialize, Serialize}; + +pub(crate) const SERVER_NAME_PDDB: &str = "_Plausibly Deniable Database_"; +pub const PDDB_MAX_DICT_NAME_LEN: usize = 64; +pub const PDDB_MAX_KEY_NAME_LEN: usize = 256; -#[allow(dead_code)] #[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] pub(crate) enum Opcode { KeyRequest, + /// read implemented using only scalars -- fast, but limited size + ReadKeyScalar, + /// read implemented using a memory buffer -- slower, but just shy of a page in size + ReadKeyMem, + + WriteKeyScalar1, + WriteKeyScalar2, + WriteKeyScalar3, + WriteKeyScalar4, + WriteKeyMem, + WriteKeyFlush, + /// Suspend/resume callback SuspendResume, } + +/// A structure for requesting a token to access a particular key/value pair +#[derive(Archive, Serialize, Deserialize)] +pub(crate) struct PddbKeyRequest { + pub(crate) dict: xous_ipc::String::, // pending https://github.com/rust-lang/rust/issues/90195 + pub(crate) key: xous_ipc::String::, // pending https://github.com/rust-lang/rust/issues/90195 + pub(crate) token: Option<[u32; 3]>, +} + +/// Return codes for Read/Write API calls to the main server +#[repr(u8)] +#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] +pub(crate) enum PddbRetcode { + Uninit = 0, + Ok = 1, + BasisLost = 2, + AccessDenied = 3, +} +/// PddbBuf is a C-representation of a page of memory that's used +/// to shuttle data for streaming channels. It must be exactly one +/// page in size, with some overhead specific to the PDDB book-keeping +/// at the top, and the remainder available for shuttling data. +/// +/// It does not use rkyv, as the blanket implementation of that tends to +/// incur too many extra copies an/or zeroing operations. +#[repr(C, packed)] +pub(crate) struct PddbBuf { + /// api token for the given buffer + pub(crate) token: [u32; 3], + /// length of the data field + pub(crate) len: u16, + /// a field reserved for the return code + pub(crate) retcode: PddbRetcode, + pub(crate) reserved: u8, + pub(crate) data: [u8; 4080], +} +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_pddbuf_size() { + assert!(core::mem::size_of::() == 4096, "PddBuf record has the wrong size"); + } +} +impl PddbBuf { + pub fn from_slice_mut(slice: &mut [u8]) -> &mut PddbBuf { + // this transforms the slice [u8] into a PddbBuf ref. + unsafe {core::mem::transmute::<*mut u8, &mut PddbBuf>(slice.as_mut_ptr()) } + } +} diff --git a/services/pddb/src/frontend/pddbkey.rs b/services/pddb/src/frontend/pddbkey.rs index 6808b216f..f90a1d73f 100644 --- a/services/pddb/src/frontend/pddbkey.rs +++ b/services/pddb/src/frontend/pddbkey.rs @@ -1,57 +1,283 @@ -//use std::io::{Error, ErrorKind, Result}; -//use xous::MemoryRange; -//use std::io::SeekFrom; +use crate::*; +use xous::{CID, send_message, Message}; +use xous_ipc::Buffer; -//use crate::*; -use rkyv::{Archive, Deserialize, Serialize}; -use xous_ipc::String; +use num_traits::*; +use std::io::{Result, Error, ErrorKind}; +use std::path::{Path, Component}; +use std::format; +use std::io::{Read, Write}; -#[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]>, -} +use std::string::String; - -/* -/// 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 pub struct PddbKey<'a> { - // dictionary to search for the key within - dict: PddbDict<'a>, - // a copy of my name - name: String, - // called when the key changes (basis or is modified otherwise) - key_changed_cb: Box, - // mapped memory for the plaintext contexts, typically not all resident - mem: MemoryRange, + conn: CID, + dict: String, + key: String, + token: [u32; 3], + buf: Buffer<'a>, } - impl<'a> PddbKey<'a> { - pub fn set_callback(&mut self, key_changed_cb: impl FnMut() + 'a) { - self.key_changed_cb = Box::new(key_changed_cb); + 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"); + + if !path.as_ref().is_absolute() { + return Err(Error::new(ErrorKind::InvalidInput, "All PDDB keys must be fully specified relative to a dictionary")); + } + let mut dict = String::new(); + let mut key = String::new(); + let mut components = path.as_ref().components(); + match components.next().unwrap() { + Component::Prefix(prefix_component) => { + if let Some(dictstr) = prefix_component.as_os_str().to_str() { + if dictstr.len() <= PDDB_MAX_DICT_NAME_LEN { + dict.push_str(dictstr); + } else { + return Err(Error::new(ErrorKind::InvalidInput, format!("PDDB dictionary names must be shorter than {} bytes", PDDB_MAX_DICT_NAME_LEN))); + } + } else { + return Err(Error::new(ErrorKind::InvalidInput, "PDDB dictionary names must valid UTF-8")); + } + } + _ => { + return Err(Error::new(ErrorKind::InvalidInput, "All PDDB entries must be of the format `dict:key`, where `dict` is treated as a Prefix")); + } + } + // collect the remaining components into the key + for comps in components { + if let Some(keystr) = comps.as_os_str().to_str() { + key.push_str(keystr); + } else { + return Err(Error::new(ErrorKind::InvalidInput, "PDDB dictionary names must valid UTF-8")); + } + } + + if key.len() > PDDB_MAX_KEY_NAME_LEN { + return Err(Error::new(ErrorKind::InvalidInput, format!("PDDB key names must be shorter than {} bytes", PDDB_MAX_DICT_NAME_LEN))); + } + + 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")))?; + + let response = buf.to_original::().unwrap(); + + // we probably should never remove this check -- the code may compile correctly and + // "work" without this being an even page size, but it's pretty easy to get this wrong, + // and if it's wrong we can lose a lot in terms of efficiency of execution. + assert!(core::mem::size_of::() == 4096, "PddBuf record has the wrong size"); + if let Some(token) = response.token { + Ok(PddbKey { + conn, + dict, + key, + token, + buf: Buffer::new(core::mem::size_of::()), + }) + } else { + Err(Error::new(ErrorKind::PermissionDenied, "Dict/Key access denied")) + } + } + /// this will clear all residual values in the buffer. Should be called whenever the Basis set changes. + pub fn volatile_clear(&mut self) { + self.buf.volatile_clear(); } - pub fn new() { - let mem = xous::syscall::map_memory( - None, - None, - 4096, - xous::MemoryFlags::R | xous::MemoryFlags::W, - ).expect("couldn't map initial memory page"); - + pub(crate) fn conn(&self) -> CID { + self.conn + } +} + +impl<'a> Read for PddbKey<'a> { + fn read(&mut self, buf: &mut [u8]) -> Result { + if buf.len() == 0 { + Ok(0) + } else if buf.len() <= 8 { + let response = send_message( + self.conn, + Message::new_blocking_scalar( + Opcode::ReadKeyScalar.to_usize().unwrap(), + self.token[0] as usize, + self.token[1] as usize, + self.token[2] as usize, + buf.len() as usize, + ) + ).or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?; + // scalars can return up to 64 bits of data. Split into an array and copy into the buf as demanded. + if let xous::Result::Scalar2(a, b) = response { + let mut maxbuf: [u8; 8] = [0; 8]; + for (&src, dst) in (a as u32).to_le_bytes().iter().zip(maxbuf[..4].iter_mut()) { + *dst = src; + } + if buf.len() > 4 { + for (&src, dst) in (b as u32).to_le_bytes().iter().zip(maxbuf[4..].iter_mut()) { + *dst = src; + } + } + for (&src, dst) in maxbuf.iter().zip(buf.iter_mut()) { + *dst = src; + } + Ok(buf.len()) + } else { + Err(Error::new(ErrorKind::Other, "Xous internal error")) + } + } else { + // create pbuf from a pre-reserved chunk of memory, to save on allocator thrashing + // note that it does mean that un-erased data from previous reads and writes are passed back + // to the server, which is a kind of information leakage, but I think in practice we're + // leaking that data back to a server where the data had either originated from or was disclosed at + // one point. + let readlen = { + let pbuf = PddbBuf::from_slice_mut(self.buf.as_mut()); + // sure, we could make it a loop, but...unrolled seems better + pbuf.token[0] = self.token[0]; + pbuf.token[1] = self.token[1]; + pbuf.token[2] = self.token[2]; + let readlen = if buf.len() <= pbuf.data.len() { + buf.len() as u16 + } else { + pbuf.data.len() as u16 + }; + pbuf.len = readlen; + pbuf.retcode = PddbRetcode::Uninit; + readlen + }; + // this takes the buffer and remaps it to the server, and on return the data is mapped back + self.buf.lend_mut(self.conn, Opcode::ReadKeyMem.to_u32().unwrap()) + .or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?; + { + // at this point, pbuf has been mutated by the server with a return code and the return data. + let pbuf = PddbBuf::from_slice_mut(self.buf.as_mut()); + match pbuf.retcode { + PddbRetcode::Ok => { + for (&src, dst) in pbuf.data.iter().zip(buf.iter_mut()) { + *dst = src; + } + assert!(pbuf.len <= readlen, "More data returned than we requested"); + Ok(pbuf.len as usize) + } + PddbRetcode::BasisLost => Err(Error::new(ErrorKind::BrokenPipe, "Basis lost")), + PddbRetcode::AccessDenied => Err(Error::new(ErrorKind::PermissionDenied, "Access denied")), + _ => Err(Error::new(ErrorKind::Other, "Unhandled error code in PddbKey Read")), + } + } + } + } +} + +impl<'a> Write for PddbKey<'a> { + fn write(&mut self, buf: &[u8]) -> Result { + if buf.len() == 0 { + Ok(0) + } else if buf.len() <= 4 { + let opcode = match buf.len() { + 1 => Opcode::WriteKeyScalar1.to_usize().unwrap(), + 2 => Opcode::WriteKeyScalar2.to_usize().unwrap(), + 3 => Opcode::WriteKeyScalar3.to_usize().unwrap(), + 4 => Opcode::WriteKeyScalar4.to_usize().unwrap(), + _ => panic!("This shouldn't happen") + }; + // guarantee we have enough bytes to do the u32 conversion, even if we have less data in the incoming buf + let mut u32buf: [u8; 4] = [0; 4]; + for (&src, dst) in buf.iter().zip(u32buf.iter_mut()) { + *dst = src; + } + let response = send_message( + self.conn, + Message::new_blocking_scalar( + opcode, + self.token[0] as usize, + self.token[1] as usize, + self.token[2] as usize, + u32::from_le_bytes(u32buf) as usize, + ) + ).or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?; + if let xous::Result::Scalar2(rcode, len) = response { + match FromPrimitive::from_u8(rcode as u8) { + Some(PddbRetcode::Ok) => Ok(len), + Some(PddbRetcode::BasisLost) => Err(Error::new(ErrorKind::BrokenPipe, "Basis lost")), + Some(PddbRetcode::AccessDenied) => Err(Error::new(ErrorKind::PermissionDenied, "Access denied")), + _ => Err(Error::new(ErrorKind::Other, "Unhandled error code in PddbKey Read")), + } + } else { + Err(Error::new(ErrorKind::Other, "Xous internal error")) + } + } else { + let writelen = { + let pbuf = PddbBuf::from_slice_mut(self.buf.as_mut()); + // sure, we could make it a loop, but...unrolled seems better + pbuf.token[0] = self.token[0]; + pbuf.token[1] = self.token[1]; + pbuf.token[2] = self.token[2]; + let writelen = if buf.len() <= pbuf.data.len() { + buf.len() as u16 + } else { + pbuf.data.len() as u16 + }; + pbuf.len = writelen; + pbuf.retcode = PddbRetcode::Uninit; + for (&src, dst) in buf.iter().zip(pbuf.data.iter_mut()) { + *dst = src; + } + writelen + }; + // this takes the buffer and remaps it to the server, and on return the data is mapped back + self.buf.lend_mut(self.conn, Opcode::WriteKeyMem.to_u32().unwrap()) + .or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?; + { + // at this point, pbuf has been mutated by the server with a return code and the return data. + let pbuf = PddbBuf::from_slice_mut(self.buf.as_mut()); + match pbuf.retcode { + PddbRetcode::Ok => { + assert!(pbuf.len <= writelen, "More data written than we requested"); + Ok(pbuf.len as usize) + } + PddbRetcode::BasisLost => Err(Error::new(ErrorKind::BrokenPipe, "Basis lost")), + PddbRetcode::AccessDenied => Err(Error::new(ErrorKind::PermissionDenied, "Access denied")), + _ => Err(Error::new(ErrorKind::Other, "Unhandled error code in PddbKey Read")), + } + } + } + } + fn flush(&mut self) -> Result<()> { + let response = send_message( + self.conn, + Message::new_blocking_scalar(Opcode::WriteKeyFlush.to_usize().unwrap(), 0, 0, 0, 0) + ).or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?; + if let xous::Result::Scalar1(rcode) = response { + match FromPrimitive::from_u8(rcode as u8) { + Some(PddbRetcode::Ok) => Ok(()), + Some(PddbRetcode::BasisLost) => Err(Error::new(ErrorKind::BrokenPipe, "Basis lost")), + _ => Err(Error::new(ErrorKind::Interrupted, "Flush failed for unspecified reasons")), + } + } else { + Err(Error::new(ErrorKind::Other, "Xous internal error")) + } + } +} + +use core::sync::atomic::{AtomicU32, Ordering}; +static REFCOUNT: AtomicU32 = AtomicU32::new(0); +impl<'a> Drop for PddbKey<'a> { + fn drop(&mut self) { + self.buf.volatile_clear(); // clears any confidential data in our memory buffer + + // 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. + // Note to future me: you want this. Don't get rid of it because you think, "nah, nobody will ever make more than one copy of this object". + if REFCOUNT.load(Ordering::Relaxed) == 0 { + unsafe{xous::disconnect(self.conn).unwrap();} + } + // if there was object-specific state (such as a one-time use server for async callbacks, specific to the object instance), + // de-allocate those items here. They don't need a reference count because they are object-specific } - /* these get moved to traits */ - /* - // reads are transparent and "just happen" - pub fn read(&mut self, buf: &mut [u8]) -> Result { Ok(0) } - // writes will call update() on the dictionary - pub fn write(&mut self, buf: &[u8]) -> Result { Ok(0) } - // provided for compatibility with Rust API - 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 f8c676c99..34296cf99 100644 --- a/services/pddb/src/lib.rs +++ b/services/pddb/src/lib.rs @@ -5,114 +5,16 @@ pub use api::*; 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, Component}; -use std::format; - -pub const PDDB_MAX_DICT_NAME_LEN: usize = 64; -pub const PDDB_MAX_KEY_NAME_LEN: usize = 256; - -pub struct PddbKey { - conn: CID, - dict: String, - key: String, - token: [u32; 3], -} -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"); - - if !path.as_ref().is_absolute() { - return Err(Error::new(ErrorKind::InvalidInput, "All PDDB keys must be fully specified relative to a dictionary")); - } - let mut dict = String::new(); - let mut key = String::new(); - let mut components = path.as_ref().components(); - match components.next().unwrap() { - Component::Prefix(prefix_component) => { - if let Some(dictstr) = prefix_component.as_os_str().to_str() { - if dictstr.len() <= PDDB_MAX_DICT_NAME_LEN { - dict.push_str(dictstr); - } else { - return Err(Error::new(ErrorKind::InvalidInput, format!("PDDB dictionary names must be shorter than {} bytes", PDDB_MAX_DICT_NAME_LEN))); - } - } else { - return Err(Error::new(ErrorKind::InvalidInput, "PDDB dictionary names must valid UTF-8")); - } - } - _ => { - return Err(Error::new(ErrorKind::InvalidInput, "All PDDB entries must be of the format `dict:key`, where `dict` is treated as a Prefix")); - } - } - // collect the remaining components into the key - for comps in components { - if let Some(keystr) = comps.as_os_str().to_str() { - key.push_str(keystr); - } else { - return Err(Error::new(ErrorKind::InvalidInput, "PDDB dictionary names must valid UTF-8")); - } - } - - if key.len() > PDDB_MAX_KEY_NAME_LEN { - return Err(Error::new(ErrorKind::InvalidInput, format!("PDDB key names must be shorter than {} bytes", PDDB_MAX_DICT_NAME_LEN))); - } - - 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")))?; - - 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 { - self.conn - } -} - -use core::sync::atomic::{AtomicU32, Ordering}; -static REFCOUNT: AtomicU32 = AtomicU32::new(0); -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. - // Note to future me: you want this. Don't get rid of it because you think, "nah, nobody will ever make more than one copy of this object". - if REFCOUNT.load(Ordering::Relaxed) == 0 { - unsafe{xous::disconnect(self.conn).unwrap();} - } - // if there was object-specific state (such as a one-time use server for async callbacks, specific to the object instance), - // de-allocate those items here. They don't need a reference count because they are object-specific - } -} - - - - use xous::MemoryRange; use std::collections::HashMap; -use std::io::SeekFrom; + + + +////// probably things in this file should eventually be split ount into their own +////// frontend module file. // this is an intenal structure for managing the overall PDDB pub(crate) struct PddbManager { @@ -135,7 +37,7 @@ impl PddbBasis { // this structure can be shared on the user side? pub struct PddbDict<'a> { - contents: HashMap, + contents: HashMap, &'a [u8]>, callback: Box, } impl<'a> PddbDict<'a> { diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index a7f06f086..1df338581 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -5,6 +5,7 @@ mod api; use api::*; use num_traits::*; +use xous::{msg_blocking_scalar_unpack, msg_scalar_unpack}; /// PDDB - Plausibly Deniable DataBase /// @@ -97,7 +98,25 @@ fn xmain() -> ! { Some(Opcode::KeyRequest) => { // placeholder } - Some(Opcode::SuspendResume) => xous::msg_scalar_unpack!(msg, token, _, _, _, { + Some(Opcode::ReadKeyScalar) => msg_blocking_scalar_unpack!(msg, tok0, tok1, tok2, len, { + // placeholder + }), + Some(Opcode::ReadKeyMem) => { + // placeholder + } + Some(Opcode::WriteKeyScalar1) + | Some(Opcode::WriteKeyScalar2) + | Some(Opcode::WriteKeyScalar3) + | Some(Opcode::WriteKeyScalar4) => msg_blocking_scalar_unpack!(msg, tok0, tok1, tok2, data, { + // placeholder + }), + Some(Opcode::WriteKeyMem) => { + // placeholder + } + Some(Opcode::WriteKeyFlush) => { + // placeholder + } + Some(Opcode::SuspendResume) => msg_scalar_unpack!(msg, token, _, _, _, { /* pddb.suspend(); susres.suspend_until_resume(token).expect("couldn't execute suspend/resume"); pddb.resume(); */