add staging location lockout and xous-names completion checking

This commit is contained in:
bunnie 2021-08-10 04:30:45 +08:00
parent 184bb8405f
commit cf3ed935e7
8 changed files with 75 additions and 17 deletions

View file

@ -399,10 +399,12 @@ pub(crate) struct RootKeys {
gfx: graphics_server::Gfx, // for reading out font planes for signing verification
spinor: spinor::Spinor,
ticktimer: ticktimer_server::Ticktimer,
xns: xous_names::XousNames,
}
impl<'a> RootKeys {
pub fn new(xns: &xous_names::XousNames) -> RootKeys {
pub fn new() -> RootKeys {
let xns = xous_names::XousNames::new().unwrap();
let keyrom = xous::syscall::map_memory(
xous::MemoryAddress::new(utra::keyrom::HW_KEYROM_BASE),
None,
@ -473,6 +475,7 @@ impl<'a> RootKeys {
gfx: graphics_server::Gfx::new(&xns).expect("couldn't connect to gfx"),
spinor,
ticktimer: ticktimer_server::Ticktimer::new().expect("couldn't connect to ticktimer"),
xns,
};
keys
@ -494,6 +497,16 @@ impl<'a> RootKeys {
}
pub fn kernel_base(&self) -> u32 { self.kernel_base }
fn xns_interlock(&self) {
loop {
if self.xns.trusted_init_done().expect("couldn't query init done status") {
break;
} else {
log::warn!("trusted init not finished, rootkeys is holding off on sensitive operations");
self.ticktimer.sleep_ms(650).expect("couldn't sleep");
}
}
}
fn purge_password(&mut self, pw_type: PasswordType) {
unsafe {
let pcache_ptr: *mut PasswordCache = self.pass_cache.as_mut_ptr() as *mut PasswordCache;
@ -686,6 +699,7 @@ impl<'a> RootKeys {
/// Called by the UX layer to set up a key init run. It disables suspend/resume for the duration
/// of the run, and also sets up some missing fields of KEYROM necessary to encrypt passwords.
pub fn setup_key_init(&mut self) {
self.xns_interlock();
// block suspend/resume ops during security-sensitive operations
self.susres.set_suspendable(false).expect("couldn't block suspend/resume");
// in this block, keyrom data is copied into RAM.
@ -720,6 +734,9 @@ impl<'a> RootKeys {
/// - sign the FPGA image
/// - get ready for a reboot
pub fn do_key_init(&mut self, rootkeys_modal: &mut Modal, main_cid: xous::CID) -> Result<(), RootkeyResult> {
self.xns_interlock();
self.spinor.set_staging_write_protect(true).expect("couldn't protect the staging area");
let mut progress_action = Slider::new(main_cid, Opcode::UxGutter.to_u32().unwrap(),
0, 100, 10, Some("%"), 0, true, true
);
@ -933,6 +950,7 @@ impl<'a> RootKeys {
// clean up the oracles
src_oracle.clear();
dst_oracle.clear();
self.spinor.set_staging_write_protect(false).expect("couldn't un-protect the staging area");
// finalize the progress bar on exit -- always leave at 100%
pb.update_text(t!("rootkeys.init.finished", xous::LANG));

View file

@ -38,7 +38,7 @@ mod implementation {
}
impl RootKeys {
pub fn new(xns: &xous_names::XousNames) -> RootKeys {
pub fn new() -> RootKeys {
RootKeys {
password_type: None,
}
@ -103,16 +103,16 @@ fn xmain() -> ! {
let xns = xous_names::XousNames::new().unwrap();
/*
Connections allowed to the keys server:
1. Shellchat (to originate update test requests)
2. Password entry UX thread
3. Key purge timer
4. Main menu -> trigger initialization
5. (future) PDDB
0. Password entry UX thread (self, created without xns)
0. Key purge timer (self, created without xns)
1. Shellchat for test initiation
2. Main menu -> trigger initialization
3. (future) PDDB
*/
let keys_sid = xns.register_name(api::SERVER_NAME_KEYS, Some(4)).expect("can't register server");
let keys_sid = xns.register_name(api::SERVER_NAME_KEYS, Some(2)).expect("can't register server");
log::trace!("registered with NS -- {:?}", keys_sid);
let mut keys = RootKeys::new(&xns);
let mut keys = RootKeys::new();
log::trace!("ready to accept requests");
@ -316,8 +316,8 @@ fn xmain() -> ! {
// we also need to include a command that does the reboot.
match result {
Ok(_) => {
keys.finish_key_init();
// TODO: need to insert the reboot command here
}
Err(RootkeyResult::AlignmentError) => {
dismiss_modal_action.set_action_opcode(Opcode::UxGutter.to_u32().unwrap());

View file

@ -581,8 +581,8 @@ fn xmain() -> ! {
// - GAM
// - shellchat/rtc
// - shellchat/sleep x2
// - UX thread
let rtc_sid = xns.register_name(api::SERVER_NAME_RTC, Some(5)).expect("can't register server");
// - UX thread (self, created without xns, so does not count)
let rtc_sid = xns.register_name(api::SERVER_NAME_RTC, Some(4)).expect("can't register server");
log::trace!("registered with NS -- {:?}", rtc_sid);
CB_TO_MAIN_CONN.store(xous::connect(rtc_sid).unwrap(), Ordering::Relaxed);

View file

@ -12,6 +12,9 @@ pub(crate) enum Opcode {
ReleaseExclusive,
/// a special token is reserved for writing to the SoC region, only one service is allowed to do that
RegisterSocToken,
/// the SocToken holder can allow for writes to the staging area by other processes
SetStagingWriteProtect,
ClearStagingWriteProtect,
/// program a region. Erase is accomplished by calling WriteRegion with all 0xFF's as data.
WriteRegion,

View file

@ -66,6 +66,25 @@ impl Spinor {
self.token[3] as usize,
)).map(|_| ())
}
pub fn set_staging_write_protect(&self, protect: bool) -> Result<(), xous::Error> {
if protect {
send_message(self.conn,
Message::new_scalar(Opcode::SetStagingWriteProtect.to_usize().unwrap(),
self.token[0] as usize,
self.token[1] as usize,
self.token[2] as usize,
self.token[3] as usize,
)).map(|_| ())
} else {
send_message(self.conn,
Message::new_scalar(Opcode::ClearStagingWriteProtect.to_usize().unwrap(),
self.token[0] as usize,
self.token[1] as usize,
self.token[2] as usize,
self.token[3] as usize,
)).map(|_| ())
}
}
#[cfg(not(test))]
fn send_write_region(&self, wr: &WriteRegion) -> Result<(), SpinorError> {

View file

@ -558,6 +558,7 @@ fn xmain() -> ! {
let mut client_id: Option<[u32; 4]> = None;
let mut soc_token: Option<[u32; 4]> = None;
let mut ecc_errors: [Option<u32>; 4] = [None, None, None, None]; // just record the first few errors, until we can get `std` and a convenient Vec/Queue
let mut staging_write_protect: bool = false;
loop {
let mut msg = xous::receive_message(spinor_sid).unwrap();
@ -580,6 +581,22 @@ fn xmain() -> ! {
soc_token = Some([id0 as u32, id1 as u32, id2 as u32, id3 as u32]);
}
}),
Some(Opcode::SetStagingWriteProtect) => msg_scalar_unpack!(msg, id0, id1, id2, id3, {
if let Some(token) = soc_token {
if token[0] == id0 as u32 && token[1] == id1 as u32 &&
token[2] == id2 as u32 && token[3] == id3 as u32 {
staging_write_protect = true;
}
}
}),
Some(Opcode::ClearStagingWriteProtect) => msg_scalar_unpack!(msg, id0, id1, id2, id3, {
if let Some(token) = soc_token {
if token[0] == id0 as u32 && token[1] == id1 as u32 &&
token[2] == id2 as u32 && token[3] == id3 as u32 {
staging_write_protect = false;
}
}
}),
Some(Opcode::AcquireExclusive) => msg_blocking_scalar_unpack!(msg, id0, id1, id2, id3, {
if soc_token.is_none() { // reject any ops until a soc token is registered
xous::return_scalar(msg.sender, 0).unwrap();
@ -618,7 +635,8 @@ fn xmain() -> ! {
let mut wr = buffer.to_original::<WriteRegion, _>().unwrap();
let mut authorized = true;
if let Some(st) = soc_token {
if (wr.start >= xous::SOC_REGION_LOC) && (wr.start < xous::LOADER_LOC) {
if staging_write_protect && ((wr.start >= xous::SOC_REGION_LOC) && (wr.start < xous::LOADER_LOC)) ||
!staging_write_protect && ((wr.start >= xous::SOC_REGION_LOC) && (wr.start < xous::SOC_STAGING_GW_LOC)) {
// if only the holder of the ID that matches the SoC token can write to the SOC flash area
// other areas are not as strictly controlled because signature checks ostensibly should catch
// attempts to modify them. However, access to the gateware definition would allow one to rewrite

View file

@ -98,8 +98,8 @@ fn xmain() -> ! {
log::trace!("|status: my canvas {:?}", status_gid);
log::trace!("|status: registering GAM|status thread");
// should be only one connection here, from the status main loop
let status_sid = xns.register_name(SERVER_NAME_STATUS, Some(1)).expect("|status: can't register server");
// we have one connection, from the status main loop; but we make it with a local call, not using xns. so there's 0 in xns.
let status_sid = xns.register_name(SERVER_NAME_STATUS, Some(0)).expect("|status: can't register server");
// create a connection for callback hooks
unsafe{CB_TO_MAIN_CONN = Some(xous::connect(status_sid).unwrap())};
let pump_conn = xous::connect(status_sid).unwrap();
@ -204,7 +204,6 @@ fn xmain() -> ! {
} else {
let mut sn = clone.lock().unwrap();
sn.insert("secnotes.state_fail".to_string(), t!("secnote.state_fail", xous::LANG).to_string());
}
}
});

View file

@ -158,9 +158,10 @@ impl CheckedHashMap {
}
pub fn trusted_init_done(&self) -> bool {
let mut trusted_done = true;
for (_name, entry) in self.map.iter() {
for (name, entry) in self.map.iter() {
if let Some(max) = entry.max_conns {
if max != entry.current_conns {
log::info!("server {} has {} conns but expects {}", name, entry.current_conns, max);
trusted_done = false;
}
}