rust: add zeroizing str to cstr util function
What changed, and why it matters
This commit adds a safer helper function for converting Rust text into C-style strings used to unlock the BitBox02 hardware wallet. The new version ensures the temporary copy holding the device password is automatically wiped from memory after use, instead of potentially leaving leftover copies behind. It is a defensive hardening change rather than a fix for a known active attack.
Treat as a routine security-hardening commit. Review whether other FFI call sites that pass passwords or seeds still use the non-zeroizing str_to_cstr_vec() and consider migrating them. No urgent response is indicated by the commit itself.
Security signals we found
Sensitive-data handling: password converted to C string for FFI call
Memory-safety hardening: zeroization wrapper used to clear temporary buffer
Author-stated concern about unzeroed copies from CString reallocation
Defensive patch: no CVE, exploit, or incident described in commit
Evidence from the diff
The patch introduces str_to_cstr_vec_zeroizing(), which allocates a single zero-initialized Vec
Changed components
src/rust/bitbox02/src/util.rssrc/rust/bitbox02/src/keystore.rskeystore_unlock FFI call in _unlock()Inspect captured patch +33 / −2
diff --git a/src/rust/bitbox02/src/keystore.rs b/src/rust/bitbox02/src/keystore.rs
index 0b549a6..8b5b9ff 100644
--- a/src/rust/bitbox02/src/keystore.rs
+++ b/src/rust/bitbox02/src/keystore.rs
@@ -75,7 +75,7 @@ pub fn _unlock(password: &str) -> Result<zeroize::Zeroizing<Vec<u8>>, Error> {
let mut seed_len: usize = 0;
match unsafe {
bitbox02_sys::keystore_unlock(
- crate::util::str_to_cstr_vec(password)
+ crate::util::str_to_cstr_vec_zeroizing(password)
.unwrap()
.as_ptr()
.cast(),
diff --git a/src/rust/bitbox02/src/util.rs b/src/rust/bitbox02/src/util.rs
index d6497be..60202b5 100644
--- a/src/rust/bitbox02/src/util.rs
+++ b/src/rust/bitbox02/src/util.rs
@@ -42,7 +42,7 @@ pub fn truncate_str(s: &str, len: usize) -> &str {
}
/// Converts a Rust string to a null terminated C string by appending a null
-/// terminator. Returns `Err(())` if the input already contians a null byte.
+/// terminator. Returns `Err(())` if the input already contains a null byte.
pub fn str_to_cstr_vec(input: &str) -> Result<Vec<core::ffi::c_char>, ()> {
let cstr = alloc::ffi::CString::new(input)
.or(Err(()))?
@@ -53,6 +53,23 @@ pub fn str_to_cstr_vec(input: &str) -> Result<Vec<core::ffi::c_char>, ()> {
Ok(cstr.into_iter().map(|c| c as _).collect())
}
+/// Converts a Rust string to a null terminated C string by appending a null
+/// terminator. Returns `Err(())` if the input already contains a null byte.
+pub fn str_to_cstr_vec_zeroizing(
+ input: &str,
+) -> Result<zeroize::Zeroizing<Vec<core::ffi::c_char>>, ()> {
+ let bytes = input.as_bytes();
+ if bytes.contains(&0) {
+ return Err(());
+ }
+ let mut result: zeroize::Zeroizing<Vec<core::ffi::c_char>> =
+ zeroize::Zeroizing::new(vec![0; bytes.len() + 1]);
+ for (i, b) in bytes.iter().enumerate() {
+ result[i] = *b as _;
+ }
+ Ok(result)
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -134,4 +151,18 @@ mod tests {
);
assert_eq!(str_to_cstr_vec("te\0st"), Err(()));
}
+
+ #[test]
+ fn test_str_to_cstr_vec_zeroizing() {
+ assert_eq!(str_to_cstr_vec_zeroizing("").unwrap().as_slice(), &[0]);
+ assert_eq!(
+ str_to_cstr_vec_zeroizing("test").unwrap(),
+ b"test\0"
+ .iter()
+ .map(|c| *c as _)
+ .collect::<Vec<core::ffi::c_char>>()
+ .into(),
+ );
+ assert_eq!(str_to_cstr_vec_zeroizing("te\0st"), Err(()));
+ }
}
Why this scored 35/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.