From d3970eaa80420f9e056874a2dc4b899f9cbbbdf6 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 18 May 2026 12:54:17 +0800 Subject: [PATCH] remove potential race condition in sending U2F packets over USB The hardware handler structure passes through an interrupt request boundary that breaks assumptions around the borrow handler due to the unsafe transmutation of the record pointer at the interrupt handler entry. This means that records shared between the userspace and machine handler space are not part of the borrow-checker's review. This patch adds an AtomicBool flag that indicates if the ISR has completed running, and forces the userspace to wait until the ISR has completed before returning. This does potentially slow down the rate of packet transmission but it ensures that the `fido_tx_queue` structure does not have concurrency issues in the case where the following happens: 1. U2fTx opcode encountered 2. fido_tx_queue has a u2f_msg put on it 3. ISR triggered 4. in the process of handling the ISR, a page fault is hit, so the ISR doesn't start for some time. 5. Before the ISR returns, another U2FTx opcode is encountered 6. fido_tx_queue is being modified by userspace 7. The ISR is able to resume 8. You have a simultaneous modification to the fido_tx_queue which is UB. I think it's maybe possible that this scenario above can't structurally happen due to an ISR page fault not allowing the system to resume through to "normal" code running (iirc the swapper will handle a page fault in ISR and resume directly into the ISR routine) but this patch guards against incorrectness in that implementation. for what it's worth, later tests show that this bug was not actually at the root cause of the behavior I was chasing down, so in fact this was not happening. However I think it's still a good idea to fix this potential problem. --- services/usb-bao1x/src/hw.rs | 3 +++ services/usb-bao1x/src/main.rs | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/services/usb-bao1x/src/hw.rs b/services/usb-bao1x/src/hw.rs index c09748a66..42841dd71 100644 --- a/services/usb-bao1x/src/hw.rs +++ b/services/usb-bao1x/src/hw.rs @@ -57,6 +57,7 @@ pub struct Bao1xUsb<'a> { // from the interrupt handler. pub double_lock: AtomicBool, pub led_state: KeyboardLedsReport, + pub irq_serviced: AtomicBool, } impl<'a> Bao1xUsb<'a> { @@ -122,6 +123,7 @@ impl<'a> Bao1xUsb<'a> { serial_rx: [0u8; SERIAL_MAX_PACKET_SIZE], double_lock: AtomicBool::new(false), led_state: KeyboardLedsReport::default(), + irq_serviced: AtomicBool::new(false), } } @@ -355,6 +357,7 @@ pub(crate) fn composite_handler(_irq_no: usize, arg: *mut usize) { while let Some(u2f_msg) = usb.fido_tx_queue.borrow_mut().pop_front() { u2f.write_report(&u2f_msg).ok(); } + usb.irq_serviced.store(true, Ordering::SeqCst); } Some(UsbIrqReq::KbdTx) => { let keyboard = composite.device::, _>(); diff --git a/services/usb-bao1x/src/main.rs b/services/usb-bao1x/src/main.rs index 0161066ac..0e3265095 100644 --- a/services/usb-bao1x/src/main.rs +++ b/services/usb-bao1x/src/main.rs @@ -441,6 +441,7 @@ pub(crate) fn main_hw() -> ! { unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut u2f_ipc = buffer.to_original::().unwrap(); if cu.device.state() != usb_device::device::UsbDeviceState::Configured { + log::warn!("U2fTx: HANGUP"); u2f_ipc.code = U2fCode::Hangup; buffer.replace(u2f_ipc).unwrap(); continue; @@ -454,6 +455,15 @@ pub(crate) fn main_hw() -> ! { cu.fido_tx_queue.borrow_mut().push_back(u2f_msg); } cu.sw_irq(UsbIrqReq::FidoTx); + // ensure that the interrupt has happened, because the interrupt can create + // concurrency issues on the fido_tx_queue if we've re-entered this function + // before the interrupt has processed + while !cu.irq_serviced.load(Ordering::SeqCst) { + xous::yield_slice(); + } + // clear the serviced flag here - this is OK because this is the only other thread + // that can access the variable. + cu.irq_serviced.store(false, Ordering::SeqCst); log::debug!("enqueued U2F packet {:x?}", u2f_ipc.data); u2f_ipc.code = U2fCode::TxAck; } else {