From be227adde1dcbd8bc98bbdbb793538650963b8c6 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 22 Sep 2024 15:44:05 +0800 Subject: [PATCH] removed sized strings from pddb interchange structures I think this is safe to do because no strings can be directly serialized into the PDDB's structures. The place to check for this is inside the backend/ directory. For example, `DictName` is where the name field is serialized into a structure that goes onto the disk. It's explicitly length-checked. What this means is that we could have calls into the pddb crate with invalid length strings, however, if you check the lib implementation, all of the APIs also pre-flight the length of arguments to ensure they fit into the destination string. So mostly what we're losing is the constraints on the intermediate IPC representation, but the length is still checked on either side. --- services/pddb/src/api.rs | 32 +++++------ services/pddb/src/lib.rs | 114 +++++++++++++++----------------------- services/pddb/src/main.rs | 2 +- 3 files changed, 62 insertions(+), 86 deletions(-) diff --git a/services/pddb/src/api.rs b/services/pddb/src/api.rs index f16260d44..ab392138d 100644 --- a/services/pddb/src/api.rs +++ b/services/pddb/src/api.rs @@ -264,7 +264,7 @@ pub type ApiToken = [u32; 3]; pub struct PddbBasisList { /// the first 63 that fit in the list -- generally we anticipate not more than a few being open at a /// time, so this should be enough. - pub list: [String; 63], + pub list: [String; 63], /// total number of basis open. Should be <= 63, but we allow it to be larger to indicate cases where /// this structure wasn't big enough. pub num: u32, @@ -298,16 +298,16 @@ impl BasisRetentionPolicy { } #[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] pub struct PddbBasisRequest { - pub name: String, + pub name: String, pub code: PddbRequestCode, pub policy: Option, } #[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] pub struct PddbDictRequest { pub basis_specified: bool, - pub basis: String, - pub dict: String, - pub key: String, + pub basis: String, + pub dict: String, + pub key: String, pub index: u32, pub token: [u32; 4], pub code: PddbRequestCode, @@ -323,9 +323,9 @@ pub struct PddbDictRequest { #[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] pub struct PddbKeyRequest { pub basis_specified: bool, - pub basis: String, - pub dict: String, - pub key: String, + pub basis: String, + pub dict: String, + pub key: String, pub token: Option, pub create_dict: bool, pub create_key: bool, @@ -350,8 +350,8 @@ pub(crate) const MAX_PDDB_DELETE_LEN: usize = 3800; // approximate limit, might #[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] pub(crate) struct PddbDeleteList { pub basis_specified: bool, - pub basis: String, - pub dict: String, + pub basis: String, + pub dict: String, pub data: [u8; MAX_PDDB_DELETE_LEN], pub retcode: PddbRetcode, } @@ -524,8 +524,8 @@ pub struct PddbKeyAttrIpc { pub len: u64, pub reserved: u64, pub age: u64, - pub dict: String, - pub basis: String, + pub dict: String, + pub basis: String, pub flags: u32, pub index: u32, pub token: ApiToken, @@ -537,8 +537,8 @@ impl PddbKeyAttrIpc { len: 0, reserved: 0, age: 0, - dict: String::::new(), - basis: String::::new(), + dict: String::new(), + basis: String::new(), flags: 0, index: 0, token, @@ -564,8 +564,8 @@ impl PddbKeyAttrIpc { len: attr.len as u64, reserved: attr.reserved as u64, age: attr.age as u64, - dict: String::::from_str(&attr.dict), - basis: String::::from_str(&attr.basis), + dict: String::from(&attr.dict), + basis: String::from(&attr.basis), flags: attr.flags.0, index: attr.index.get(), token, diff --git a/services/pddb/src/lib.rs b/services/pddb/src/lib.rs index 66c398176..84e27141b 100644 --- a/services/pddb/src/lib.rs +++ b/services/pddb/src/lib.rs @@ -21,13 +21,6 @@ use core::ops::DerefMut; use std::cell::RefCell; use std::convert::TryInto; -use rkyv::{ - archived_value, - de::deserializers::AllocDeserializer, - ser::{serializers::WriteSerializer, Serializer}, - AlignedVec, Deserialize, -}; - #[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] pub enum CbOp { Change, @@ -280,7 +273,7 @@ impl Pddb { /// return a list of all open bases pub fn list_basis(&self) -> Vec { - let list_alloc = PddbBasisList { list: [String::::default(); 63], num: 0 }; + let list_alloc = PddbBasisList { list: std::array::from_fn(|_| String::new()), num: 0 }; let mut buf = Buffer::into_buf(list_alloc).expect("Couldn't convert to memory structure"); buf.lend_mut(self.conn, Opcode::ListBasis.to_u32().unwrap()) .expect("Couldn't execute ListBasis opcode"); @@ -300,7 +293,7 @@ impl Pddb { /// The caller of this function will block and return only if the order of Bases has changed. pub fn monitor_basis(&self) -> Vec { - let list_alloc = PddbBasisList { list: [String::::default(); 63], num: 0 }; + let list_alloc = PddbBasisList { list: std::array::from_fn(|_| String::new()), num: 0 }; let mut buf = Buffer::into_buf(list_alloc).expect("Couldn't convert to memory structure"); buf.lend_mut(self.conn, Opcode::BasisMonitor.to_u32().unwrap()) .expect("Couldn't execute ListBasis opcode"); @@ -321,11 +314,7 @@ impl Pddb { /// returns the latest basis that is opened -- this is where all new values are being sent by default /// if the PDDB is not mounted, returns None pub fn latest_basis(&self) -> Option { - let mgmt = PddbBasisRequest { - name: String::::new(), - code: PddbRequestCode::Uninit, - policy: None, - }; + let mgmt = PddbBasisRequest { name: String::new(), code: PddbRequestCode::Uninit, policy: None }; let mut buf = Buffer::into_buf(mgmt).expect("Couldn't convert to memory structure"); buf.lend_mut(self.conn, Opcode::LatestBasis.to_u32().unwrap()) .expect("Couldn't execute ListBasis opcode"); @@ -344,11 +333,8 @@ impl Pddb { if basis_name.len() > BASIS_NAME_LEN - 1 { return Err(Error::new(ErrorKind::InvalidInput, "basis name too long")); } - let mgmt = PddbBasisRequest { - name: String::::from_str(basis_name), - code: PddbRequestCode::Create, - policy: None, - }; + let mgmt = + PddbBasisRequest { name: String::from(basis_name), code: PddbRequestCode::Create, policy: None }; let mut buf = Buffer::into_buf(mgmt).expect("Couldn't convert to memory structure"); buf.lend_mut(self.conn, Opcode::CreateBasis.to_u32().unwrap()) .expect("Couldn't execute CreateBasis opcode"); @@ -375,11 +361,7 @@ impl Pddb { if basis_name.len() > BASIS_NAME_LEN - 1 { return Err(Error::new(ErrorKind::InvalidInput, "basis name too long")); } - let mgmt = PddbBasisRequest { - name: String::::from_str(basis_name), - code: PddbRequestCode::Open, - policy, - }; + let mgmt = PddbBasisRequest { name: String::from(basis_name), code: PddbRequestCode::Open, policy }; let mut buf = Buffer::into_buf(mgmt).expect("Couldn't convert to memory structure"); buf.lend_mut(self.conn, Opcode::OpenBasis.to_u32().unwrap()) .expect("Couldn't execute OpenBasis opcode"); @@ -403,11 +385,8 @@ impl Pddb { if basis_name.len() > BASIS_NAME_LEN - 1 { return Err(Error::new(ErrorKind::InvalidInput, "basis name too long")); } - let mgmt = PddbBasisRequest { - name: String::::from_str(basis_name), - code: PddbRequestCode::Close, - policy: None, - }; + let mgmt = + PddbBasisRequest { name: String::from(basis_name), code: PddbRequestCode::Close, policy: None }; let mut buf = Buffer::into_buf(mgmt).expect("Couldn't convert to memory structure"); buf.lend_mut(self.conn, Opcode::CloseBasis.to_u32().unwrap()) .expect("Couldn't execute CloseBasis opcode"); @@ -429,11 +408,8 @@ impl Pddb { if basis_name.len() > BASIS_NAME_LEN - 1 { return Err(Error::new(ErrorKind::InvalidInput, "basis name too long")); } - let mgmt = PddbBasisRequest { - name: String::::from_str(basis_name), - code: PddbRequestCode::Delete, - policy: None, - }; + let mgmt = + PddbBasisRequest { name: String::from(basis_name), code: PddbRequestCode::Delete, policy: None }; let mut buf = Buffer::into_buf(mgmt).expect("Couldn't convert to memory structure"); buf.lend_mut(self.conn, Opcode::DeleteBasis.to_u32().unwrap()) .expect("Couldn't execute DeleteBasis opcode"); @@ -477,9 +453,9 @@ impl Pddb { if bname.len() > BASIS_NAME_LEN - 1 { return Err(Error::new(ErrorKind::InvalidInput, "basis name too long")); } - String::::from_str(bname) + String::from(bname) } else { - String::::new() + String::new() }; if key_changed_cb.is_some() { @@ -492,9 +468,9 @@ impl Pddb { let request = PddbKeyRequest { basis_specified: basis_name.is_some(), - basis: String::::from_str(&bname), - dict: String::::from_str(dict_name), - key: String::::from_str(key_name), + basis: String::from(&bname), + dict: String::from(dict_name), + key: String::from(key_name), create_dict, create_key, token: None, @@ -560,9 +536,9 @@ impl Pddb { if bname.len() > BASIS_NAME_LEN - 1 { return Err(Error::new(ErrorKind::InvalidInput, "basis name too long")); } - String::::from_str(bname) + String::from(bname) } else { - String::::new() + String::new() }; let maybe_cb = self.cb.take(); @@ -571,9 +547,9 @@ impl Pddb { let request = PddbKeyRequest { basis_specified: basis_name.is_some(), - basis: String::::from_str(&bname), - dict: String::::from_str(dict_name), - key: String::::from_str(key_name), + basis: String::from(&bname), + dict: String::from(dict_name), + key: String::from(key_name), create_dict: false, create_key: false, token: None, @@ -614,14 +590,14 @@ impl Pddb { if bname.len() > BASIS_NAME_LEN - 1 { return Err(Error::new(ErrorKind::InvalidInput, "basis name too long")); } - String::::from_str(bname) + String::from(bname) } else { - String::::new() + String::new() }; let mut request = PddbDeleteList { basis_specified: basis_name.is_some(), - basis: String::::from_str(&bname), - dict: String::::from_str(dict_name), + basis: String::from(&bname), + dict: String::from(dict_name), retcode: PddbRetcode::Uninit, data: [0u8; MAX_PDDB_DELETE_LEN], }; @@ -670,9 +646,9 @@ impl Pddb { if bname.len() > BASIS_NAME_LEN - 1 { return Err(Error::new(ErrorKind::InvalidInput, "basis name too long")); } - String::::from_str(bname) + String::from(bname) } else { - String::::new() + String::new() }; let maybe_cb = self.cb.take(); @@ -681,9 +657,9 @@ impl Pddb { let request = PddbKeyRequest { basis_specified: basis_name.is_some(), - basis: String::::from_str(&bname), - dict: String::::from_str(dict_name), - key: String::::new(), + basis: String::from(&bname), + dict: String::from(dict_name), + key: String::new(), create_dict: false, create_key: false, token: None, @@ -764,9 +740,9 @@ impl Pddb { if bname.len() > BASIS_NAME_LEN - 1 { return Err(Error::new(ErrorKind::InvalidInput, "basis name too long")); } - String::::from_str(bname) + String::from(bname) } else { - String::::new() + String::new() }; // this is a two-phase query, because it's quite likely that the number of keys can be very large in a // dict. @@ -778,9 +754,9 @@ impl Pddb { ]; let request = PddbDictRequest { basis_specified: basis_name.is_some(), - basis: String::::from_str(&bname), - dict: String::::from_str(dict_name), - key: String::::new(), + basis: String::from(&bname), + dict: String::from(dict_name), + key: String::new(), index: 0, code: PddbRequestCode::Uninit, token, @@ -877,9 +853,9 @@ impl Pddb { if bname.len() > BASIS_NAME_LEN - 1 { return Err(Error::new(ErrorKind::InvalidInput, "basis name too long")); } - String::::from_str(bname) + String::from(bname) } else { - String::::new() + String::new() }; // this is a two-phase query, because it's quite likely that the number of keys can be very large in a // dict. @@ -891,9 +867,9 @@ impl Pddb { ]; let request = PddbDictRequest { basis_specified: basis_name.is_some(), - basis: String::::from_str(&bname), - dict: String::::new(), - key: String::::new(), + basis: String::from(&bname), + dict: String::new(), + key: String::new(), index: 0, code: PddbRequestCode::Uninit, token, @@ -918,9 +894,9 @@ impl Pddb { for index in 0..count { let request = PddbDictRequest { basis_specified: basis_name.is_some(), - basis: String::::from_str(&bname), - dict: String::::new(), - key: String::::new(), + basis: String::from(&bname), + dict: String::new(), + key: String::new(), index, code: PddbRequestCode::Uninit, token, @@ -1106,9 +1082,9 @@ impl Pddb { // setup the request arguments let mut request = PddbDictRequest { basis_specified: basis.is_some(), - basis: String::::from_str(basis.unwrap_or("")), - dict: String::::from_str(dict), - key: String::::new(), + basis: String::from(basis.unwrap_or("")), + dict: String::from(dict), + key: String::new(), index: 0, token: [0u32; 4], code: PddbRequestCode::BulkRead, diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index 794f85a9c..1a7fe27f4 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -1589,7 +1589,7 @@ fn wrapped_main() -> ! { if req.index >= dict_list.len() as u32 { req.code = PddbRequestCode::InternalError; } else { - req.dict = String::::from_str(&dict_list[req.index as usize]); + req.dict = String::from(&dict_list[req.index as usize]); req.code = PddbRequestCode::NoErr; // the last index requested must be the highest one! if req.index == dict_list.len() as u32 - 1 {