What changed, and why it matters
This commit is a routine internal code cleanup: it rewrites a three-button user-interface helper so it uses Rust's modern async/await style instead of an older callback-and-shared-state pattern. There is no claim or visible change that fixes a security bug, and the visible behavior of the device should stay the same.
No security action required; review as normal code-quality refactor. If auditing, verify that the new waker storage and C callback interaction do not introduce race conditions or missed wake-ups in the async runtime, but this is a robustness concern rather than an exploit.
Security signals we found
No security-relevant keywords in commit title or message
No CVE, advisory, or bug-bounty attribution in commit or supplied references
Change is an architectural refactor (callback -> async/await) with equivalent functional outcome
No input validation, buffer handling, cryptography, or privilege changes visible
Memory management changes (boxed callback removed, waker stored) but no obvious use-after-free or leak introduced
Evidence from the diff
The patch ports trinary_choice from a synchronous-style callback API (trinary_choice_create + option_no_screensaver polling a RefCell<Option<TrinaryChoice>>) to an async function that uses core::future::poll_fn, stores a Waker, and is woken by the C callback. It also removes the old TrinaryChoiceCb boxed callback and the explicit drop logic. The change touches the real UI implementation and two stub/test variants. No security-relevant bug is described or directly observable in the diff.
Changed components
src/rust/bitbox02-rust/src/workflow/trinary_choice.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 +64 / −52
diff --git a/src/rust/bitbox02-rust/src/workflow/trinary_choice.rs b/src/rust/bitbox02-rust/src/workflow/trinary_choice.rs
index 93d81e5..dda1d7d 100644
--- a/src/rust/bitbox02-rust/src/workflow/trinary_choice.rs
+++ b/src/rust/bitbox02-rust/src/workflow/trinary_choice.rs
@@ -1,12 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
-use crate::bb02_async::option_no_screensaver;
-use core::cell::RefCell;
-
-use alloc::boxed::Box;
-
pub use bitbox02::ui::TrinaryChoice;
-use bitbox02::ui::trinary_choice_create;
+use bitbox02::ui::trinary_choice;
pub async fn choose(
message: &str,
@@ -14,17 +9,5 @@ pub async fn choose(
label_middle: Option<&str>,
label_right: Option<&str>,
) -> TrinaryChoice {
- let result = RefCell::new(None as Option<TrinaryChoice>);
-
- let mut component = trinary_choice_create(
- message,
- label_left,
- label_middle,
- label_right,
- Box::new(|choice| {
- *result.borrow_mut() = Some(choice);
- }),
- );
- component.screen_stack_push();
- option_no_screensaver(&result).await
+ trinary_choice(message, label_left, label_middle, label_right).await
}
diff --git a/src/rust/bitbox02/src/ui/ui.rs b/src/rust/bitbox02/src/ui/ui.rs
index 8cb6b59..bbfb4d2 100644
--- a/src/rust/bitbox02/src/ui/ui.rs
+++ b/src/rust/bitbox02/src/ui/ui.rs
@@ -475,19 +475,35 @@ pub async fn menu(params: MenuParams<'_>) -> MenuResponse {
}
}
-pub fn trinary_choice_create<'a>(
- message: &'a str,
- label_left: Option<&'a str>,
- label_middle: Option<&'a str>,
- label_right: Option<&'a str>,
- chosen_callback: TrinaryChoiceCb,
-) -> Component<'a> {
- unsafe extern "C" fn c_chosen_cb(choice: TrinaryChoice, user_data: *mut c_void) {
- let callback = user_data as *mut TrinaryChoiceCb;
- unsafe { (*callback)(choice) };
+pub async fn trinary_choice(
+ message: &str,
+ label_left: Option<&str>,
+ label_middle: Option<&str>,
+ label_right: Option<&str>,
+) -> TrinaryChoice {
+ let _no_screensaver = crate::screen_saver::ScreensaverInhibitor::new();
+
+ // Shared between the async context and the c callback
+ struct SharedState {
+ waker: Option<Waker>,
+ result: Option<TrinaryChoice>,
}
+ 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;
- let chosen_user_data = Box::into_raw(Box::new(chosen_callback)) as *mut c_void;
+ unsafe extern "C" fn callback(choice: TrinaryChoice, user_data: *mut c_void) {
+ let shared_state = unsafe { &*(user_data as *mut RefCell<SharedState>) };
+ let mut shared_state = shared_state.borrow_mut();
+ if shared_state.result.is_none() {
+ shared_state.result = Some(choice);
+ if let Some(waker) = shared_state.waker.as_ref() {
+ waker.wake_by_ref();
+ }
+ }
+ }
let label_left = label_left.map(|label| util::strings::str_to_cstr_vec(label).unwrap());
let label_middle = label_middle.map(|label| util::strings::str_to_cstr_vec(label).unwrap());
@@ -508,20 +524,35 @@ pub fn trinary_choice_create<'a>(
label_right
.as_ref()
.map_or_else(core::ptr::null, |label| label.as_ptr()),
- Some(c_chosen_cb as _),
- chosen_user_data,
+ Some(callback),
+ shared_state_ptr, // passed to callback as `user_data`.
core::ptr::null_mut(), // parent component, there is no parent.
)
};
- Component {
+
+ let mut component = Component {
component,
is_pushed: false,
- on_drop: Some(Box::new(move || unsafe {
- // Drop all callbacks.
- drop(Box::from_raw(chosen_user_data as *mut TrinaryChoiceCb));
- })),
+ 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 fn confirm_transaction_address_create<'a, 'b>(
diff --git a/src/rust/bitbox02/src/ui/ui_stub.rs b/src/rust/bitbox02/src/ui/ui_stub.rs
index 5e89f6d..a2ba588 100644
--- a/src/rust/bitbox02/src/ui/ui_stub.rs
+++ b/src/rust/bitbox02/src/ui/ui_stub.rs
@@ -64,13 +64,12 @@ pub async fn menu(_params: MenuParams<'_>) -> MenuResponse {
panic!("not used");
}
-pub fn trinary_choice_create<'a>(
- _message: &'a str,
- _label_left: Option<&'a str>,
- _label_middle: Option<&'a str>,
- _label_right: Option<&'a str>,
- _chosen_callback: TrinaryChoiceCb,
-) -> Component<'a> {
+pub async fn trinary_choice(
+ _message: &str,
+ _label_left: Option<&str>,
+ _label_middle: Option<&str>,
+ _label_right: Option<&str>,
+) -> TrinaryChoice {
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 ae03ff6..cdbbb90 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
@@ -78,13 +78,12 @@ pub async fn menu(_params: MenuParams<'_>) -> MenuResponse {
panic!("not implemented");
}
-pub fn trinary_choice_create<'a>(
- _message: &'a str,
- _label_left: Option<&'a str>,
- _label_middle: Option<&'a str>,
- _label_right: Option<&'a str>,
- _chosen_callback: TrinaryChoiceCb,
-) -> Component<'a> {
+pub async fn trinary_choice(
+ _message: &str,
+ _label_left: Option<&str>,
+ _label_middle: Option<&str>,
+ _label_right: Option<&str>,
+) -> TrinaryChoice {
panic!("not implemented")
}
Why this scored 17/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.