From f079a1480b6e1451d0041d6931b93d990cf8057f Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 15 Feb 2026 03:36:32 +0800 Subject: [PATCH] fix more bounds checks in USB handler - CBW block only derived after length is validated - write offset to disk uses checked_add, necessary since we're compiling in release mode --- bao1x-boot/boot1/src/platform/bao1x/usb/handlers.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bao1x-boot/boot1/src/platform/bao1x/usb/handlers.rs b/bao1x-boot/boot1/src/platform/bao1x/usb/handlers.rs index 436a2e44b..8b1d378bd 100644 --- a/bao1x-boot/boot1/src/platform/bao1x/usb/handlers.rs +++ b/bao1x-boot/boot1/src/platform/bao1x/usb/handlers.rs @@ -190,11 +190,11 @@ pub fn usb_ep1_bulk_out_complete( udc_pointer_check!(buf_addr, info as usize & 0xFFFF); // safety: the buffer is checked to be in-range for the UDC handler let buf = unsafe { core::slice::from_raw_parts(buf_addr as *const u8, info as usize & 0xFFFF) }; - let mut cbw = Cbw::default(); - cbw.as_mut().copy_from_slice(&buf[..size_of::()]); let mut csw = Csw::derive(); if UmsState::CommandPhase == this.ms_state && (length == 31) { + let mut cbw = Cbw::default(); + cbw.as_mut().copy_from_slice(&buf[..size_of::()]); // CBW if cbw.signature == BULK_CBW_SIG { csw.signature = BULK_CSW_SIG; @@ -383,7 +383,7 @@ pub fn usb_ep1_bulk_out_complete( critical_section::with(|cs| super::glue::SECTOR.borrow(cs).replace(block)); } } - if write_offset + len < disk.len() { + if write_offset.checked_add(len).map_or(false, |end| end < disk.len()) { // update the received data to the disk if it fits within the allocated region disk[write_offset..write_offset + len].copy_from_slice(&app_buf[..len]); }