From 8570449b049c1906a5820305b4550598a1819ea0 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 13 Aug 2022 23:54:36 +0800 Subject: [PATCH] make vendor backup locked out by default; hook to UX enable dialog box also halts updates of the TOTP timer bar while this is happening to avoid overflowing the server queue with unanswered redraw requests --- apps/vault/src/main.rs | 19 +++--- apps/vault/src/totp.rs | 17 ++++-- apps/vault/src/vendor_commands.rs | 66 +++++++++++---------- apps/vault/tools/vaultbackup-rs/src/main.rs | 4 ++ 4 files changed, 61 insertions(+), 45 deletions(-) diff --git a/apps/vault/src/main.rs b/apps/vault/src/main.rs index 4eaee8ef8..6459e9560 100644 --- a/apps/vault/src/main.rs +++ b/apps/vault/src/main.rs @@ -176,6 +176,7 @@ fn main() -> ! { let mode = Arc::new(Mutex::new(VaultMode::Fido)); let item_list = Arc::new(Mutex::new(Vec::::new())); let action_active = Arc::new(AtomicBool::new(false)); + let allow_host = Arc::new(AtomicBool::new(false)); // spawn the actions server. This is responsible for grooming the UX elements. It // has to be in its own thread because it uses blocking modal calls that would cause @@ -189,6 +190,7 @@ fn main() -> ! { // spawn the FIDO2 USB handler let _ = thread::spawn({ + let allow_host = allow_host.clone(); move || { let xns = xous_names::XousNames::new().unwrap(); let tt = ticktimer_server::Ticktimer::new().unwrap(); @@ -220,9 +222,12 @@ fn main() -> ! { Some(data) => data, None => { log::debug!("starting processing of vendor data..."); - let resp = vendor_commands::handle_vendor_command(&mut vendor_session); + let resp = vendor_commands::handle_vendor_command( + &mut vendor_session, + allow_host.load(Ordering::SeqCst) + ); log::debug!("finished processing of vendor data!"); - + match vendor_session.is_backup() { true => { if vendor_session.has_backup_data() { @@ -292,7 +297,7 @@ fn main() -> ! { }); // spawn the TOTP pumper let pump_sid = xous::create_server().unwrap(); - crate::totp::pumper(mode.clone(), pump_sid, conn); + crate::totp::pumper(mode.clone(), pump_sid, conn, allow_host.clone()); let pump_conn = xous::connect(pump_sid).unwrap(); let menu_sid = xous::create_server().unwrap(); @@ -505,13 +510,9 @@ fn main() -> ! { vaultux.readout_mode(true); modals.dynamic_notification_close().ok(); - // TODO: rethink the integration of this menu item. The problem is that if you're in readout - // mode and the "vault" mode is active, the "pumper" thread keeps sending messages to redraw - // the TOTP timer bar. This will eventually overflow this server's queue and cause a hang. - // However, the final integration for this will depend a bit on how the vault upload/download PR - // turns out. + allow_host.store(true, Ordering::SeqCst); modals.show_notification(t!("vault.readout_active", xous::LANG), None).ok(); - // TODO: add some variable here to de-activate HID readout once this is done + allow_host.store(false, Ordering::SeqCst); modals.dynamic_notification(Some(t!("vault.readout_switchover", xous::LANG)), None).ok(); vaultux.readout_mode(false); diff --git a/apps/vault/src/totp.rs b/apps/vault/src/totp.rs index e959fe8ae..98497d3ad 100644 --- a/apps/vault/src/totp.rs +++ b/apps/vault/src/totp.rs @@ -153,7 +153,12 @@ pub(crate) enum PumpOp { Quit, } -pub(crate) fn pumper(mode: Arc>, sid: xous::SID, main_conn: xous::CID) { +pub(crate) fn pumper( + mode: Arc>, + sid: xous::SID, + main_conn: xous::CID, + allow_host: Arc +) { let _ = thread::spawn({ move || { let tt = ticktimer_server::Ticktimer::new().unwrap(); @@ -164,10 +169,12 @@ pub(crate) fn pumper(mode: Arc>, sid: xous::SID, main_conn: xou log::trace!("{:?}", opcode); match opcode { Some(PumpOp::Pump) => { - xous::try_send_message(main_conn, - Message::new_scalar(crate::VaultOp::Redraw.to_usize().unwrap(), - 0, 0, 0, 0) - ).ok(); // don't panic if the queue overflows + if !allow_host.load(core::sync::atomic::Ordering::SeqCst) { // don't redraw if we're in host access mode + xous::try_send_message(main_conn, + Message::new_scalar(crate::VaultOp::Redraw.to_usize().unwrap(), + 0, 0, 0, 0) + ).ok(); // don't panic if the queue overflows + } let mode_cache = {(*mode.lock().unwrap()).clone()}; { // we really want mode.lock() to be in a different scope so... if mode_cache == VaultMode::Totp { diff --git a/apps/vault/src/vendor_commands.rs b/apps/vault/src/vendor_commands.rs index 4ed3273c4..488c538a2 100644 --- a/apps/vault/src/vendor_commands.rs +++ b/apps/vault/src/vendor_commands.rs @@ -164,7 +164,7 @@ pub fn handle_vendor_data( } } -pub fn handle_vendor_command(session: &mut VendorSession) -> HidPacketIterator { +pub fn handle_vendor_command(session: &mut VendorSession, allow_host: bool) -> HidPacketIterator { let cmd = session.command; let payload = session.data.clone(); let channel_id = session.channel_id; @@ -172,41 +172,45 @@ pub fn handle_vendor_command(session: &mut VendorSession) -> HidPacketIterator { log::debug!("got vendor command: {}", cmd); let xns = xous_names::XousNames::new().unwrap(); - let payload = match session.command { - COMMAND_RESTORE_TOTP_CODES => match handle_restore(payload, &xns) { - Ok(payload) => Message { - cid: channel_id, - cmd, - payload, - }, - Err(error) => { - log::error!("error while restoring codes: {:?}", error); - error_message(channel_id, 41) - } - }, - COMMAND_BACKUP_TOTP_CODES => match handle_backup(&xns, session) { - Ok(payload) => { - log::debug!("sending over chunk: {:?}", payload); - Message { + let payload = if allow_host { + match session.command { + COMMAND_RESTORE_TOTP_CODES => match handle_restore(payload, &xns) { + Ok(payload) => Message { cid: channel_id, cmd, payload, + }, + Err(error) => { + log::error!("error while restoring codes: {:?}", error); + error_message(channel_id, 41) } - } - Err(error) => { - match error { - BackupError::NoMoreChunks => { - log::debug!("no more chunks to send via backup!"); - error_message(channel_id, 88) // TODO(gsora): make this a constant - } - _ => { - log::error!("error while restoring codes: {:?}", error); - error_message(channel_id, 42) + }, + COMMAND_BACKUP_TOTP_CODES => match handle_backup(&xns, session) { + Ok(payload) => { + log::debug!("sending over chunk: {:?}", payload); + Message { + cid: channel_id, + cmd, + payload, } } - } - }, - _ => error_message(channel_id, 0x33), + Err(error) => { + match error { + BackupError::NoMoreChunks => { + log::debug!("no more chunks to send via backup!"); + error_message(channel_id, 88) // TODO(gsora): make this a constant + } + _ => { + log::error!("error while restoring codes: {:?}", error); + error_message(channel_id, 42) + } + } + } + }, + _ => error_message(channel_id, 0x33), + } + } else { + error_message(channel_id, 44) }; HidPacketIterator::new(payload).unwrap() @@ -407,7 +411,7 @@ fn handle_backup( return Ok(new_chunk.into()); } -fn error_message(cid: ChannelID, error_code: u8) -> Message { +pub(crate) fn error_message(cid: ChannelID, error_code: u8) -> Message { // This unwrap is safe because the payload length is 1 <= 7609 bytes. Message { cid, diff --git a/apps/vault/tools/vaultbackup-rs/src/main.rs b/apps/vault/tools/vaultbackup-rs/src/main.rs index 099970a06..2a089c458 100644 --- a/apps/vault/tools/vaultbackup-rs/src/main.rs +++ b/apps/vault/tools/vaultbackup-rs/src/main.rs @@ -119,6 +119,10 @@ fn main() -> Result<()> { if value == 88 { break; } + if value == 44 { + log::error!("Device is not in backup mode, operation aborted!"); + std::process::exit(1); + } return Err(error)?; }