From cae402fad0f9384444f2ab6618d9547da26b326b Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 6 Jul 2023 16:36:10 +0800 Subject: [PATCH] fix case where an idle font selection menu can crash the system while in TOTP mode when the font menu is raised while in TOTP mode, the "pump" thread continues to prompt for redraws, which can eventually lead to an overflow of the server queue on the main thread that leads to deadlock and eventually a crash. This patch does two things: - splits the allow_host variable and the allow_totp_rendering variable so that the two flags have distinct meanings - use the allow_totp_rendering variable to control when the totp thread is allowed to pump the main thread to animate the timer bar. Previously the allow_host variable was introduced to control access to sensitive data, and it incidentally was also used to control TOTP rendering because when host readout is activated this can freeze the UI for extended periods of time. By splitting the flag we can now control both independently of each other. Rendering still stops when host readout is enabled, but it also stops when any other blocking dialog boxes are popped up, hopefully preventing future crashes if a user is distracted in the middle of a UX flow that can block the main loop. --- apps/vault/src/main.rs | 19 ++++++++++++++++--- apps/vault/src/totp.rs | 4 ++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/apps/vault/src/main.rs b/apps/vault/src/main.rs index bc83014ad..e8a585438 100644 --- a/apps/vault/src/main.rs +++ b/apps/vault/src/main.rs @@ -127,6 +127,7 @@ fn main() -> ! { let item_lists = Arc::new(Mutex::new(ItemLists::new())); let action_active = Arc::new(AtomicBool::new(false)); let allow_host = Arc::new(AtomicBool::new(false)); + let allow_totp_rendering = Arc::new(AtomicBool::new(true)); // Protects access to the openSK PDDB entries from simultaneous readout on the UX while OpenSK is updating it let opensk_mutex = Arc::new(Mutex::new(0)); // storage for lefty mode @@ -424,7 +425,7 @@ fn main() -> ! { }); // spawn the TOTP pumper let pump_sid = xous::create_server().unwrap(); - crate::totp::pumper(mode.clone(), pump_sid, conn, allow_host.clone()); + crate::totp::pumper(mode.clone(), pump_sid, conn, allow_totp_rendering.clone()); let pump_conn = xous::connect(pump_sid).unwrap(); let menu_sid = xous::create_server().unwrap(); @@ -601,15 +602,18 @@ fn main() -> ! { .add_list_item(item) .expect("couldn't build radio item list"); } + allow_totp_rendering.store(false, Ordering::SeqCst); match modals.get_radiobutton(t!("vault.select_font", locales::LANG)) { Ok(style) => { vaultux.set_glyph_style(name_to_style(&style).unwrap_or(DEFAULT_FONT)); }, _ => log::error!("get_radiobutton failed"), } + allow_totp_rendering.store(true, Ordering::SeqCst); vaultux.update_mode(); } Some(VaultOp::MenuAutotype) => { + allow_totp_rendering.store(false, Ordering::SeqCst); modals.dynamic_notification(Some(t!("vault.autotyping", locales::LANG)), None).ok(); match vaultux.autotype() { Err(xous::Error::UseBeforeInit) => { // USB not plugged in @@ -641,6 +645,7 @@ fn main() -> ! { } } modals.dynamic_notification_close().ok(); + allow_totp_rendering.store(true, Ordering::SeqCst); // force the one entry to update its UX cache so that the autotype time increments up if let Some(entry) = vaultux.selected_entry() { let buf = Buffer::into_buf(entry).expect("IPC error"); @@ -653,8 +658,10 @@ fn main() -> ! { let buf = Buffer::into_buf(entry).expect("IPC error"); buf.send(actions_conn, ActionOp::MenuDeleteStage2.to_u32().unwrap()).expect("messaging error"); } else { - // this will block redraws, but it's just one notification in a sequence so it's OK. + allow_totp_rendering.store(false, Ordering::SeqCst); + // this will block redraws modals.show_notification(t!("vault.error.nothing_selected", locales::LANG), None).ok(); + allow_totp_rendering.store(true, Ordering::SeqCst); } } Some(VaultOp::MenuEditStage1) => { @@ -664,8 +671,10 @@ fn main() -> ! { let buf = Buffer::into_buf(entry).expect("IPC error"); buf.send(actions_conn, ActionOp::MenuEditStage2.to_u32().unwrap()).expect("messaging error"); } else { - // this will block redraws, but it's just one notification in a sequence so it's OK. + // this will block redraws + allow_totp_rendering.store(false, Ordering::SeqCst); modals.show_notification(t!("vault.error.nothing_selected", locales::LANG), None).ok(); + allow_totp_rendering.store(true, Ordering::SeqCst); } } Some(VaultOp::MenuReadoutMode) => { @@ -674,8 +683,10 @@ fn main() -> ! { modals.dynamic_notification_close().ok(); allow_host.store(true, Ordering::SeqCst); + allow_totp_rendering.store(false, Ordering::SeqCst); modals.show_notification(t!("vault.readout_active", locales::LANG), None).ok(); allow_host.store(false, Ordering::SeqCst); + allow_totp_rendering.store(true, Ordering::SeqCst); modals.dynamic_notification(Some(t!("vault.readout_switchover", locales::LANG)), None).ok(); vaultux.readout_mode(false); @@ -710,6 +721,7 @@ fn main() -> ! { let cv = prefs.lefty_mode_or_default().unwrap(); modals.add_list(vec![t!("prefs.yes", locales::LANG), t!("prefs.no", locales::LANG)]).unwrap(); + allow_totp_rendering.store(false, Ordering::SeqCst); let mode = yes_no_to_bool( modals .get_radiobutton(&format!("{} {}", t!("prefs.current_setting", locales::LANG), @@ -717,6 +729,7 @@ fn main() -> ! { .unwrap() .as_str(), ); + allow_totp_rendering.store(true, Ordering::SeqCst); prefs.set_lefty_mode(mode).unwrap(); lefty_mode.store(mode, Ordering::SeqCst); } diff --git a/apps/vault/src/totp.rs b/apps/vault/src/totp.rs index 6c978583b..8d003247b 100644 --- a/apps/vault/src/totp.rs +++ b/apps/vault/src/totp.rs @@ -163,7 +163,7 @@ pub(crate) fn pumper( mode: Arc>, sid: xous::SID, main_conn: xous::CID, - allow_host: Arc + allow_totp_rendering: Arc ) { let _ = thread::spawn({ move || { @@ -175,7 +175,7 @@ pub(crate) fn pumper( log::trace!("{:?}", opcode); match opcode { Some(PumpOp::Pump) => { - if !allow_host.load(core::sync::atomic::Ordering::SeqCst) { // don't redraw if we're in host access mode + if allow_totp_rendering.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)