From 0e5fb14894c1fce67ffa97237c75ebfe4c347ef5 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 27 Jun 2022 02:00:22 +0800 Subject: [PATCH] add a compatibility gate on USB HID interface If the SoC isn't compatible, don't try starting the U2F server. --- apps/vault/src/main.rs | 61 +++++++++++++++------------- services/usb-device-xous/src/api.rs | 3 ++ services/usb-device-xous/src/lib.rs | 18 ++++++++ services/usb-device-xous/src/main.rs | 6 +++ 4 files changed, 60 insertions(+), 28 deletions(-) diff --git a/apps/vault/src/main.rs b/apps/vault/src/main.rs index 61adf217f..82118d9f3 100644 --- a/apps/vault/src/main.rs +++ b/apps/vault/src/main.rs @@ -192,41 +192,46 @@ fn main() -> ! { let mut rng = ctap_crypto::rng256::XousRng256::new(&xns); // this call will block until the PDDB is mounted. let usb = usb_device_xous::UsbHid::new(); - let mut ctap_state = CtapState::new(&mut rng, check_user_presence, boot_time); - let mut ctap_hid = CtapHid::new(); - loop { - match usb.u2f_wait_incoming() { - Ok(msg) => { - log::trace!("FIDO listener got message: {:?}", msg); - let now = ClockValue::new(tt.elapsed_ms() as i64, 1000); - let reply = ctap_hid.process_hid_packet(&msg.packet, now, &mut ctap_state); - // This block handles sending packets. - for pkt_reply in reply { - let mut reply = RawFidoMsg::default(); - reply.packet.copy_from_slice(&pkt_reply); - let status = usb.u2f_send(reply); - match status { - Ok(()) => { - log::trace!("Sent U2F packet"); - } - Err(e) => { - log::error!("Error sending U2F packet: {:?}", e); + // only run the main loop if the SoC is compatible + if usb.is_soc_compatible() { + let mut ctap_state = CtapState::new(&mut rng, check_user_presence, boot_time); + let mut ctap_hid = CtapHid::new(); + loop { + match usb.u2f_wait_incoming() { + Ok(msg) => { + log::trace!("FIDO listener got message: {:?}", msg); + let now = ClockValue::new(tt.elapsed_ms() as i64, 1000); + let reply = ctap_hid.process_hid_packet(&msg.packet, now, &mut ctap_state); + // This block handles sending packets. + for pkt_reply in reply { + let mut reply = RawFidoMsg::default(); + reply.packet.copy_from_slice(&pkt_reply); + let status = usb.u2f_send(reply); + match status { + Ok(()) => { + log::trace!("Sent U2F packet"); + } + Err(e) => { + log::error!("Error sending U2F packet: {:?}", e); + } } } } - } - Err(e) => { - match e { - xous::Error::ProcessTerminated => { // unplug happened, reset the authenticator - log::info!("CTAP unplug_reset"); - ctap_state.unplug_reset(); - }, - _ => { - log::warn!("FIDO listener got an error: {:?}", e); + Err(e) => { + match e { + xous::Error::ProcessTerminated => { // unplug happened, reset the authenticator + log::info!("CTAP unplug_reset"); + ctap_state.unplug_reset(); + }, + _ => { + log::warn!("FIDO listener got an error: {:?}", e); + } } } } } + } else { + log::warn!("SoC rev is incompatible with USB HID operations, the U2F/FIDO2 server is not started"); } } }); diff --git a/services/usb-device-xous/src/api.rs b/services/usb-device-xous/src/api.rs index 4782384ac..bcd55b93c 100644 --- a/services/usb-device-xous/src/api.rs +++ b/services/usb-device-xous/src/api.rs @@ -28,6 +28,9 @@ pub(crate) enum Opcode { /// Blocks the caller, waiting for a U2F message U2fRxDeferred, + /// Query if the HID driver was able to start + IsSocCompatible, + /// Handle the USB interrupt UsbIrqHandler, /// Suspend/resume callback diff --git a/services/usb-device-xous/src/lib.rs b/services/usb-device-xous/src/lib.rs index ca9983174..20078203e 100644 --- a/services/usb-device-xous/src/lib.rs +++ b/services/usb-device-xous/src/lib.rs @@ -29,6 +29,24 @@ impl UsbHid { conn } } + /// used to query if the HID core was able to start. Mainly to handle edge cases between updates. + pub fn is_soc_compatible(&self) -> bool { + match send_message( + self.conn, + Message::new_blocking_scalar( + Opcode::IsSocCompatible.to_usize().unwrap(), + 0, 0, 0, 0 + ) + ) { + Ok(xous::Result::Scalar1(code)) => { + match code { + 0 => false, + _ => true + } + } + _ => panic!("Internal error: illegal return type"), + } + } /// this will always trigger a reset, even if it's the same core we're switching to pub fn switch_to_core(&self, core: UsbDeviceType) -> Result<(), xous::Error> { match send_message( diff --git a/services/usb-device-xous/src/main.rs b/services/usb-device-xous/src/main.rs index 4fea3bfcd..b234b8398 100644 --- a/services/usb-device-xous/src/main.rs +++ b/services/usb-device-xous/src/main.rs @@ -112,6 +112,9 @@ fn main() -> ! { // block any rx requests forever fido_listener = Some(msg); } + Some(Opcode::IsSocCompatible) => msg_blocking_scalar_unpack!(msg, _, _, _, _, { + xous::return_scalar(msg.sender, 0).expect("couldn't return compatibility status") + }), Some(Opcode::Quit) => { break; } @@ -200,6 +203,9 @@ fn main() -> ! { usbmgmt.xous_resume(); lockstatus_force_update = true; // notify the status bar that yes, it does need to redraw the lock status, even if the value hasn't changed since the last read }), + Some(Opcode::IsSocCompatible) => msg_blocking_scalar_unpack!(msg, _, _, _, _, { + xous::return_scalar(msg.sender, 1).expect("couldn't return compatibility status") + }), Some(Opcode::U2fRxDeferred) => { if fido_listener_pid.is_none() { fido_listener_pid = msg.sender.pid();