What changed, and why it matters
This commit rewrites a small user-interface flow so it uses Rust's modern async/await style instead of an older callback-and-shared-state style. It changes how the device waits for the user to insert or cancel the SD card screen, but does not add or remove security checks. There is no indication this fixes a vulnerability or introduces a new one.
No security action required; treat as routine code-quality/async modernization. Standard code review for memory safety of the new C/Rust boundary (waker lifetime, shared_state ownership, callback single-fire guarantee) is advisable, but the diff itself does not indicate a vulnerability.
Security signals we found
No security-relevant behavior change identified in the diff
Refactoring from callback-based to async/await pattern
New ScreensaverInhibitor usage prevents screen saver during SD card prompt
Evidence from the diff
The patch ports the sdcard UI component from a callback-based API (sdcard_create + FnMut(bool) closure stored in a RefCell
Changed components
src/rust/bitbox02-rust/src/workflow/sdcard.rssrc/rust/bitbox02/src/ui/types.rssrc/rust/bitbox02/src/ui/ui.rssrc/rust/bitbox02/src/ui/ui_stub.rssrc/rust/bitbox02/src/ui/ui_stub_c_unit_tests.rsInspect captured patch +68 / −50
diff --git a/src/rust/bitbox02-rust/src/workflow/sdcard.rs b/src/rust/bitbox02-rust/src/workflow/sdcard.rs
index 4505b31..c0b9bbf 100644
--- a/src/rust/bitbox02-rust/src/workflow/sdcard.rs
+++ b/src/rust/bitbox02-rust/src/workflow/sdcard.rs
@@ -1,19 +1,10 @@
// SPDX-License-Identifier: Apache-2.0
-use crate::bb02_async::option_no_screensaver;
-use core::cell::RefCell;
-
pub struct UserAbort;
pub async fn sdcard() -> Result<(), UserAbort> {
- let result = RefCell::new(None as Option<Result<(), UserAbort>>);
- let mut component = bitbox02::ui::sdcard_create(|sd_done| {
- *result.borrow_mut() = if sd_done {
- Some(Ok(()))
- } else {
- Some(Err(UserAbort))
- };
- });
- component.screen_stack_push();
- option_no_screensaver(&result).await
+ match bitbox02::ui::sdcard().await {
+ bitbox02::ui::SdcardResponse::Inserted => Ok(()),
+ bitbox02::ui::SdcardResponse::Cancelled => Err(UserAbort),
+ }
}
diff --git a/src/rust/bitbox02/src/ui/types.rs b/src/rust/bitbox02/src/ui/types.rs
index 40b2093..240bc41 100644
--- a/src/rust/bitbox02/src/ui/types.rs
+++ b/src/rust/bitbox02/src/ui/types.rs
@@ -28,6 +28,11 @@ impl Font {
}
}
+pub enum SdcardResponse {
+ Inserted,
+ Cancelled,
+}
+
#[derive(Default)]
pub struct ConfirmParams<'a> {
/// The confirmation title of the screen. Max 200 chars, otherwise **panic**.
diff --git a/src/rust/bitbox02/src/ui/ui.rs b/src/rust/bitbox02/src/ui/ui.rs
index ad4c329..8cb6b59 100644
--- a/src/rust/bitbox02/src/ui/ui.rs
+++ b/src/rust/bitbox02/src/ui/ui.rs
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: Apache-2.0
pub use super::types::{
- AcceptRejectCb, ConfirmParams, ContinueCancelCb, Font, MenuParams, MenuResponse, SelectWordCb,
- TrinaryChoice, TrinaryChoiceCb, TrinaryInputStringParams,
+ AcceptRejectCb, ConfirmParams, ContinueCancelCb, Font, MenuParams, MenuResponse,
+ SdcardResponse, SelectWordCb, TrinaryChoice, TrinaryChoiceCb, TrinaryInputStringParams,
};
use core::ffi::{c_char, c_void};
@@ -269,34 +269,66 @@ pub fn status_create<'a>(text: &str, status_success: bool) -> Component<'a> {
}
}
-pub fn sdcard_create<'a, F>(callback: F) -> Component<'a>
-where
- // Callback must outlive component.
- F: FnMut(bool) + 'a,
-{
- unsafe extern "C" fn c_callback<F2>(sd_done: bool, user_data: *mut c_void)
- where
- F2: FnMut(bool),
- {
- // The callback is dropped afterwards. This is safe because
- // this C callback is guaranteed to be called only once.
- let mut callback = unsafe { Box::from_raw(user_data as *mut F2) };
- callback(sd_done);
+pub async fn sdcard() -> SdcardResponse {
+ let _no_screensaver = crate::screen_saver::ScreensaverInhibitor::new();
+
+ // Shared between the async context and the c callback
+ struct SharedState {
+ waker: Option<Waker>,
+ result: Option<SdcardResponse>,
+ }
+ let shared_state = Box::new(RefCell::new(SharedState {
+ waker: None,
+ result: None,
+ }));
+ let shared_state_ptr = shared_state.as_ref() as *const RefCell<SharedState> as *mut c_void;
+
+ unsafe extern "C" fn callback(result: bool, user_data: *mut c_void) {
+ let shared_state = unsafe { &*(user_data as *mut RefCell<SharedState>) };
+ let mut shared_state = shared_state.borrow_mut();
+ let response = if result {
+ SdcardResponse::Inserted
+ } else {
+ SdcardResponse::Cancelled
+ };
+ if shared_state.result.is_none() {
+ shared_state.result = Some(response);
+ if let Some(waker) = shared_state.waker.as_ref() {
+ waker.wake_by_ref();
+ }
+ }
}
let component = unsafe {
bitbox02_sys::sdcard_create(
- Some(c_callback::<F>),
- // passed to the C callback as `user_data`
- Box::into_raw(Box::new(callback)) as *mut _,
+ Some(callback),
+ shared_state_ptr, // passed to callback as `user_data`.
)
};
- Component {
+
+ let mut component = Component {
component,
is_pushed: false,
on_drop: None,
_p: PhantomData,
- }
+ };
+ component.screen_stack_push();
+
+ core::future::poll_fn({
+ let shared_state = &shared_state;
+ move |cx| {
+ let mut shared_state = shared_state.borrow_mut();
+
+ if let Some(result) = shared_state.result.take() {
+ Poll::Ready(result)
+ } else {
+ // Store the waker so the callback can wake up this task
+ shared_state.waker = Some(cx.waker().clone());
+ Poll::Pending
+ }
+ }
+ })
+ .await
}
pub async fn menu(params: MenuParams<'_>) -> MenuResponse {
diff --git a/src/rust/bitbox02/src/ui/ui_stub.rs b/src/rust/bitbox02/src/ui/ui_stub.rs
index 746dc01..5e89f6d 100644
--- a/src/rust/bitbox02/src/ui/ui_stub.rs
+++ b/src/rust/bitbox02/src/ui/ui_stub.rs
@@ -6,8 +6,8 @@
//! workflows now.
pub use super::types::{
- AcceptRejectCb, ConfirmParams, ContinueCancelCb, Font, MenuParams, MenuResponse, SelectWordCb,
- TrinaryChoice, TrinaryChoiceCb, TrinaryInputStringParams,
+ AcceptRejectCb, ConfirmParams, ContinueCancelCb, Font, MenuParams, MenuResponse,
+ SdcardResponse, SelectWordCb, TrinaryChoice, TrinaryChoiceCb, TrinaryInputStringParams,
};
use core::marker::PhantomData;
@@ -56,10 +56,7 @@ pub fn status_create<'a>(_text: &str, _status_success: bool) -> Component<'a> {
panic!("not used");
}
-pub fn sdcard_create<'a, F>(_callback: F) -> Component<'a>
-where
- F: FnMut(bool) + 'a,
-{
+pub async fn sdcard() -> SdcardResponse {
panic!("not used");
}
diff --git a/src/rust/bitbox02/src/ui/ui_stub_c_unit_tests.rs b/src/rust/bitbox02/src/ui/ui_stub_c_unit_tests.rs
index 496bba9..ae03ff6 100644
--- a/src/rust/bitbox02/src/ui/ui_stub_c_unit_tests.rs
+++ b/src/rust/bitbox02/src/ui/ui_stub_c_unit_tests.rs
@@ -3,8 +3,8 @@
//! Stubs for the Bitbox02 simulator and also C unit-tests.
pub use super::types::{
- AcceptRejectCb, ConfirmParams, ContinueCancelCb, Font, MenuParams, MenuResponse, SelectWordCb,
- TrinaryChoice, TrinaryChoiceCb, TrinaryInputStringParams,
+ AcceptRejectCb, ConfirmParams, ContinueCancelCb, Font, MenuParams, MenuResponse,
+ SdcardResponse, SelectWordCb, TrinaryChoice, TrinaryChoiceCb, TrinaryInputStringParams,
};
use core::marker::PhantomData;
@@ -70,15 +70,8 @@ pub fn status_create<'a>(text: &str, _status_success: bool) -> Component<'a> {
}
}
-pub fn sdcard_create<'a, F>(mut callback: F) -> Component<'a>
-where
- F: FnMut(bool) + 'a,
-{
- callback(true);
- Component {
- is_pushed: false,
- _p: PhantomData,
- }
+pub async fn sdcard() -> SdcardResponse {
+ SdcardResponse::Inserted
}
pub async fn menu(_params: MenuParams<'_>) -> MenuResponse {
Why this scored 11/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.