isolate unsafe app loading features behind unsafe-app-loading feature gate

This commit is contained in:
bunnie 2023-10-31 00:18:42 +08:00
parent 57c00987b8
commit c346485832
6 changed files with 49 additions and 28 deletions

View file

@ -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.

View file

@ -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<u32>,
}
#[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,
}

View file

@ -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()

View file

@ -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 {

View file

@ -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::<NameRegistration, _>().unwrap();
context_mgr.register_name(registration.name.to_str(), &registration.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::<NameRegistration, _>().unwrap();
context_mgr.register_name(registration.name.to_str(), &registration.auth_token);
}
Some(Opcode::Quit) => break,
None => {log::error!("unhandled message {:?}", msg);}
}

View file

@ -18,6 +18,7 @@ pub(crate) struct NamedToken {
}
pub(crate) struct TokenManager {
tokens: Vec::<NamedToken>,
#[cfg(feature="unsafe-app-loading")]
extra_names: Vec<String>,
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);
}
}
}