From c3464858329aa775417732700343137f3f8e4830 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 31 Oct 2023 00:18:42 +0800 Subject: [PATCH] isolate unsafe app loading features behind `unsafe-app-loading` feature gate --- services/gam/Cargo.toml | 1 + services/gam/src/api.rs | 4 +++- services/gam/src/contexts.rs | 3 ++- services/gam/src/lib.rs | 15 +++++++------- services/gam/src/main.rs | 14 ++++++++----- services/gam/src/tokens.rs | 40 +++++++++++++++++++++++------------- 6 files changed, 49 insertions(+), 28 deletions(-) diff --git a/services/gam/Cargo.toml b/services/gam/Cargo.toml index ec9b4d837..f14d489d4 100644 --- a/services/gam/Cargo.toml +++ b/services/gam/Cargo.toml @@ -52,3 +52,4 @@ tts = [] # default = ["debugprint"] # "debugprint" default = [] ditherpunk = [] +unsafe-app-loading = [] # allow new contexts to be registered dynamically - this is unsafe and should not be set for release builds. diff --git a/services/gam/src/api.rs b/services/gam/src/api.rs index 82b79e999..ff995df69 100644 --- a/services/gam/src/api.rs +++ b/services/gam/src/api.rs @@ -110,6 +110,7 @@ pub struct UxRegistration { /// If the LayoutType is not an App, this field is ignored and does nothing pub focuschange_id: Option, } +#[cfg(feature="unsafe-app-loading")] #[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)] pub struct NameRegistration { pub name: String::<128>, @@ -217,7 +218,8 @@ pub(crate) enum Opcode { /// This resolves race conditions that depend upon the PDDB configurations. AllowMainMenu = 33, - /// Register a name that can aquire a token. This is only intended to be used with pre-registered apps + /// Register a name that can acquire a token. This is only intended to be used with pre-registered apps + #[cfg(feature="unsafe-app-loading")] RegisterName = 34, } diff --git a/services/gam/src/contexts.rs b/services/gam/src/contexts.rs index e27e5ad34..4ef21dcfd 100644 --- a/services/gam/src/contexts.rs +++ b/services/gam/src/contexts.rs @@ -130,8 +130,9 @@ impl ContextManager { pub(crate) fn claim_token(&mut self, name: &str) -> Option<[u32; 4]> { self.tm.claim_token(name) } + #[cfg(feature="unsafe-app-loading")] pub(crate) fn register_name(&mut self, name: &str, auth_token: &[u32; 4]) { - self.tm.register_name(name, auth_token); + self.tm.register_name(name, auth_token); } pub(crate) fn allow_untrusted_code(&self) -> bool { self.tm.allow_untrusted_code() diff --git a/services/gam/src/lib.rs b/services/gam/src/lib.rs index 88ce6d5ee..5ead4ecd3 100644 --- a/services/gam/src/lib.rs +++ b/services/gam/src/lib.rs @@ -311,14 +311,15 @@ impl Gam { Ok(returned_claim.token) } + #[cfg(feature="unsafe-app-loading")] pub fn register_name(&self, name: &str, auth_token: [u32; 4]) -> Result<(), xous::Error> { - let name_registration = NameRegistration { - name: String::<128>::from_str(name), - auth_token - }; - let buf = Buffer::into_buf(name_registration).or(Err(xous::Error::InternalError))?; - buf.lend(self.conn, Opcode::RegisterName.to_u32().unwrap()).or(Err(xous::Error::InternalError))?; - Ok(()) + let name_registration = NameRegistration { + name: String::<128>::from_str(name), + auth_token + }; + let buf = Buffer::into_buf(name_registration).or(Err(xous::Error::InternalError))?; + buf.lend(self.conn, Opcode::RegisterName.to_u32().unwrap()).or(Err(xous::Error::InternalError))?; + Ok(()) } pub fn set_predictor_api_token(&self, api_token: [u32; 4], gam_token: [u32; 4]) -> Result<(), xous::Error> { let at = ApiToken { diff --git a/services/gam/src/main.rs b/services/gam/src/main.rs index 69b248ece..c9667ab20 100644 --- a/services/gam/src/main.rs +++ b/services/gam/src/main.rs @@ -567,6 +567,9 @@ fn wrapped_main() -> ! { // set before the leaving state due to interior-mutability issues (see the block under the debug statement // titled "resolving visibility rules"). The fix is to simply not issue a context switch if it's the same app. // It saves CPU thrashing and also works around this problem. (see issue #145) + #[cfg(not(feature="unsafe-app-loading"))] + let authorized_switchers = vec![MAIN_MENU_NAME, ROOTKEY_MODAL_NAME, gam::STATUS_BAR_NAME]; + #[cfg(feature="unsafe-app-loading")] let authorized_switchers = [&[MAIN_MENU_NAME, ROOTKEY_MODAL_NAME, gam::STATUS_BAR_NAME], gam::EXPECTED_APP_CONTEXTS].concat().to_vec(); for switchers in authorized_switchers { if let Some(auth_token) = context_mgr.find_app_token_by_name(switchers) { @@ -670,11 +673,12 @@ fn wrapped_main() -> ! { context_mgr.allow_mainmenu(); xous::return_scalar(msg.sender, 0).ok(); } - Some(Opcode::RegisterName) => { - let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; - let registration = buffer.to_original::().unwrap(); - context_mgr.register_name(registration.name.to_str(), ®istration.auth_token); - } + #[cfg(feature="unsafe-app-loading")] + Some(Opcode::RegisterName) => { + let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; + let registration = buffer.to_original::().unwrap(); + context_mgr.register_name(registration.name.to_str(), ®istration.auth_token); + } Some(Opcode::Quit) => break, None => {log::error!("unhandled message {:?}", msg);} } diff --git a/services/gam/src/tokens.rs b/services/gam/src/tokens.rs index d4c917570..083629ae0 100644 --- a/services/gam/src/tokens.rs +++ b/services/gam/src/tokens.rs @@ -18,6 +18,7 @@ pub(crate) struct NamedToken { } pub(crate) struct TokenManager { tokens: Vec::, + #[cfg(feature="unsafe-app-loading")] extra_names: Vec, trng: trng::Trng, } @@ -25,13 +26,18 @@ impl<'a> TokenManager { pub(crate) fn new(xns: &xous_names::XousNames) -> TokenManager { TokenManager { tokens: Vec::new(), - extra_names: Vec::new(), + #[cfg(feature="unsafe-app-loading")] + extra_names: Vec::new(), trng: trng::Trng::new(&xns).unwrap(), } } /// checks to see if all the slots have been occupied. We can't allow untrusted code to run until all slots have checked in pub(crate) fn allow_untrusted_code(&self) -> bool { - if self.tokens.len() == (EXPECTED_BOOT_CONTEXTS.len() + EXPECTED_APP_CONTEXTS.len() + self.extra_names.len()) { + #[cfg(feature="unsafe-app-loading")] + let expected_len = EXPECTED_BOOT_CONTEXTS.len() + EXPECTED_APP_CONTEXTS.len() + self.extra_names.len(); + #[cfg(not(feature="unsafe-app-loading"))] + let expected_len = EXPECTED_BOOT_CONTEXTS.len() + EXPECTED_APP_CONTEXTS.len(); + if self.tokens.len() == expected_len { true } else { // throw a bone to the dev who has to debug this error. This typically only triggers after a major @@ -53,9 +59,10 @@ impl<'a> TokenManager { if EXPECTED_APP_CONTEXTS.iter().find(|&&context| context == name).is_some() { found = true; } - if self.extra_names.iter().find(|&context| context == name).is_some() { - found = true; - } + #[cfg(feature="unsafe-app-loading")] + if self.extra_names.iter().find(|&context| context == name).is_some() { + found = true; + } if !found { log::error!("Server {} is not pre-registered in gam/lib.rs/EXPECTED_BOOT_CONTEXTS or apps.rs/EXPECTED_APP_CONTEXTS. Did you forget to register it?", name); return None @@ -96,15 +103,20 @@ impl<'a> TokenManager { None } /// Register a new name that can then claim a token. Note that only pre-registered applications are allowed to do this. + #[cfg(feature="unsafe-app-loading")] pub(crate) fn register_name(&mut self, name: &str, auth_token: &[u32; 4]) { - if let Some(registrant) = self.lookup_name(auth_token) { - if EXPECTED_BOOT_CONTEXTS.iter().find(|&&context| context == registrant).is_some() || EXPECTED_APP_CONTEXTS.iter().find(|&&context| context == registrant).is_some() { - self.extra_names.push(name.to_string()); - } else { - log::error!("`{}' does not have permission to register a new name because it is not pre-registered", registrant); - } - } else { - log::error!("Token {:?} does not correspond with a name", auth_token); - } + if let Some(registrant) = self.lookup_name(auth_token) { + if EXPECTED_BOOT_CONTEXTS.iter() + .find( + |&&context| context == registrant).is_some() + || EXPECTED_APP_CONTEXTS.iter() + .find(|&&context| context == registrant).is_some() { + self.extra_names.push(name.to_string()); + } else { + log::error!("`{}' does not have permission to register a new name because it is not pre-registered", registrant); + } + } else { + log::error!("Token {:?} does not correspond with a name", auth_token); + } } }