mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-21 09:14:34 +00:00
isolate unsafe app loading features behind unsafe-app-loading feature gate
This commit is contained in:
parent
57c00987b8
commit
c346485832
6 changed files with 49 additions and 28 deletions
|
|
@ -52,3 +52,4 @@ tts = []
|
||||||
# default = ["debugprint"] # "debugprint"
|
# default = ["debugprint"] # "debugprint"
|
||||||
default = []
|
default = []
|
||||||
ditherpunk = []
|
ditherpunk = []
|
||||||
|
unsafe-app-loading = [] # allow new contexts to be registered dynamically - this is unsafe and should not be set for release builds.
|
||||||
|
|
|
||||||
|
|
@ -110,6 +110,7 @@ pub struct UxRegistration {
|
||||||
/// If the LayoutType is not an App, this field is ignored and does nothing
|
/// If the LayoutType is not an App, this field is ignored and does nothing
|
||||||
pub focuschange_id: Option<u32>,
|
pub focuschange_id: Option<u32>,
|
||||||
}
|
}
|
||||||
|
#[cfg(feature="unsafe-app-loading")]
|
||||||
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
|
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
|
||||||
pub struct NameRegistration {
|
pub struct NameRegistration {
|
||||||
pub name: String::<128>,
|
pub name: String::<128>,
|
||||||
|
|
@ -217,7 +218,8 @@ pub(crate) enum Opcode {
|
||||||
/// This resolves race conditions that depend upon the PDDB configurations.
|
/// This resolves race conditions that depend upon the PDDB configurations.
|
||||||
AllowMainMenu = 33,
|
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,
|
RegisterName = 34,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -130,8 +130,9 @@ impl ContextManager {
|
||||||
pub(crate) fn claim_token(&mut self, name: &str) -> Option<[u32; 4]> {
|
pub(crate) fn claim_token(&mut self, name: &str) -> Option<[u32; 4]> {
|
||||||
self.tm.claim_token(name)
|
self.tm.claim_token(name)
|
||||||
}
|
}
|
||||||
|
#[cfg(feature="unsafe-app-loading")]
|
||||||
pub(crate) fn register_name(&mut self, name: &str, auth_token: &[u32; 4]) {
|
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 {
|
pub(crate) fn allow_untrusted_code(&self) -> bool {
|
||||||
self.tm.allow_untrusted_code()
|
self.tm.allow_untrusted_code()
|
||||||
|
|
|
||||||
|
|
@ -311,14 +311,15 @@ impl Gam {
|
||||||
|
|
||||||
Ok(returned_claim.token)
|
Ok(returned_claim.token)
|
||||||
}
|
}
|
||||||
|
#[cfg(feature="unsafe-app-loading")]
|
||||||
pub fn register_name(&self, name: &str, auth_token: [u32; 4]) -> Result<(), xous::Error> {
|
pub fn register_name(&self, name: &str, auth_token: [u32; 4]) -> Result<(), xous::Error> {
|
||||||
let name_registration = NameRegistration {
|
let name_registration = NameRegistration {
|
||||||
name: String::<128>::from_str(name),
|
name: String::<128>::from_str(name),
|
||||||
auth_token
|
auth_token
|
||||||
};
|
};
|
||||||
let buf = Buffer::into_buf(name_registration).or(Err(xous::Error::InternalError))?;
|
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))?;
|
buf.lend(self.conn, Opcode::RegisterName.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
pub fn set_predictor_api_token(&self, api_token: [u32; 4], gam_token: [u32; 4]) -> Result<(), xous::Error> {
|
pub fn set_predictor_api_token(&self, api_token: [u32; 4], gam_token: [u32; 4]) -> Result<(), xous::Error> {
|
||||||
let at = ApiToken {
|
let at = ApiToken {
|
||||||
|
|
|
||||||
|
|
@ -567,6 +567,9 @@ fn wrapped_main() -> ! {
|
||||||
// set before the leaving state due to interior-mutability issues (see the block under the debug statement
|
// 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.
|
// 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)
|
// 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();
|
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 {
|
for switchers in authorized_switchers {
|
||||||
if let Some(auth_token) = context_mgr.find_app_token_by_name(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();
|
context_mgr.allow_mainmenu();
|
||||||
xous::return_scalar(msg.sender, 0).ok();
|
xous::return_scalar(msg.sender, 0).ok();
|
||||||
}
|
}
|
||||||
Some(Opcode::RegisterName) => {
|
#[cfg(feature="unsafe-app-loading")]
|
||||||
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
|
Some(Opcode::RegisterName) => {
|
||||||
let registration = buffer.to_original::<NameRegistration, _>().unwrap();
|
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
|
||||||
context_mgr.register_name(registration.name.to_str(), ®istration.auth_token);
|
let registration = buffer.to_original::<NameRegistration, _>().unwrap();
|
||||||
}
|
context_mgr.register_name(registration.name.to_str(), ®istration.auth_token);
|
||||||
|
}
|
||||||
Some(Opcode::Quit) => break,
|
Some(Opcode::Quit) => break,
|
||||||
None => {log::error!("unhandled message {:?}", msg);}
|
None => {log::error!("unhandled message {:?}", msg);}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ pub(crate) struct NamedToken {
|
||||||
}
|
}
|
||||||
pub(crate) struct TokenManager {
|
pub(crate) struct TokenManager {
|
||||||
tokens: Vec::<NamedToken>,
|
tokens: Vec::<NamedToken>,
|
||||||
|
#[cfg(feature="unsafe-app-loading")]
|
||||||
extra_names: Vec<String>,
|
extra_names: Vec<String>,
|
||||||
trng: trng::Trng,
|
trng: trng::Trng,
|
||||||
}
|
}
|
||||||
|
|
@ -25,13 +26,18 @@ impl<'a> TokenManager {
|
||||||
pub(crate) fn new(xns: &xous_names::XousNames) -> TokenManager {
|
pub(crate) fn new(xns: &xous_names::XousNames) -> TokenManager {
|
||||||
TokenManager {
|
TokenManager {
|
||||||
tokens: Vec::new(),
|
tokens: Vec::new(),
|
||||||
extra_names: Vec::new(),
|
#[cfg(feature="unsafe-app-loading")]
|
||||||
|
extra_names: Vec::new(),
|
||||||
trng: trng::Trng::new(&xns).unwrap(),
|
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
|
/// 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 {
|
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
|
true
|
||||||
} else {
|
} else {
|
||||||
// throw a bone to the dev who has to debug this error. This typically only triggers after a major
|
// 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() {
|
if EXPECTED_APP_CONTEXTS.iter().find(|&&context| context == name).is_some() {
|
||||||
found = true;
|
found = true;
|
||||||
}
|
}
|
||||||
if self.extra_names.iter().find(|&context| context == name).is_some() {
|
#[cfg(feature="unsafe-app-loading")]
|
||||||
found = true;
|
if self.extra_names.iter().find(|&context| context == name).is_some() {
|
||||||
}
|
found = true;
|
||||||
|
}
|
||||||
if !found {
|
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);
|
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
|
return None
|
||||||
|
|
@ -96,15 +103,20 @@ impl<'a> TokenManager {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
/// Register a new name that can then claim a token. Note that only pre-registered applications are allowed to do this.
|
/// 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]) {
|
pub(crate) fn register_name(&mut self, name: &str, auth_token: &[u32; 4]) {
|
||||||
if let Some(registrant) = self.lookup_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() {
|
if EXPECTED_BOOT_CONTEXTS.iter()
|
||||||
self.extra_names.push(name.to_string());
|
.find(
|
||||||
} else {
|
|&&context| context == registrant).is_some()
|
||||||
log::error!("`{}' does not have permission to register a new name because it is not pre-registered", registrant);
|
|| EXPECTED_APP_CONTEXTS.iter()
|
||||||
}
|
.find(|&&context| context == registrant).is_some() {
|
||||||
} else {
|
self.extra_names.push(name.to_string());
|
||||||
log::error!("Token {:?} does not correspond with a name", auth_token);
|
} 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue