mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
security-relevant: clear_volatile() API missing on String
xous_ipc::<String> had a clear-volatile() API for zeroizing sensitive strings. This is now missing in String, so it has been manually implemented here by casting the String to a slice-u8 and then iterating through it using raw pointers and volatile writes with a compiler barrier to hopefully avoid all of the landmines I know of for zeroizing a string... commit is called out separately because this one is one of the more "dangerous" security-related changes in the API migration.
This commit is contained in:
parent
1fd59f0fc9
commit
f1d263359d
1 changed files with 103 additions and 66 deletions
|
|
@ -5,14 +5,14 @@ use std::cell::RefCell;
|
||||||
use graphics_server::api::GlyphStyle;
|
use graphics_server::api::GlyphStyle;
|
||||||
use graphics_server::api::*;
|
use graphics_server::api::*;
|
||||||
use num_traits::*;
|
use num_traits::*;
|
||||||
use xous_ipc::{Buffer, String};
|
use xous_ipc::Buffer;
|
||||||
|
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
|
||||||
// TODO: figure out this, do we really have to limit ourselves to 10?
|
// TODO: figure out this, do we really have to limit ourselves to 10?
|
||||||
const MAX_FIELDS: i16 = 10;
|
const MAX_FIELDS: i16 = 10;
|
||||||
|
|
||||||
pub type ValidatorErr = xous_ipc::String<256>;
|
pub type ValidatorErr = String;
|
||||||
|
|
||||||
pub type Payloads = [TextEntryPayload; MAX_FIELDS as usize];
|
pub type Payloads = [TextEntryPayload; MAX_FIELDS as usize];
|
||||||
|
|
||||||
|
|
@ -102,18 +102,14 @@ impl TextEntry {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reset_action_payloads(
|
pub fn reset_action_payloads(&mut self, fields: u32, placeholders: Option<[Option<(String, bool)>; 10]>) {
|
||||||
&mut self,
|
|
||||||
fields: u32,
|
|
||||||
placeholders: Option<[Option<(xous_ipc::String<256>, bool)>; 10]>,
|
|
||||||
) {
|
|
||||||
let mut payload = vec![TextEntryPayload::default(); fields as usize];
|
let mut payload = vec![TextEntryPayload::default(); fields as usize];
|
||||||
|
|
||||||
if let Some(placeholders) = placeholders {
|
if let Some(placeholders) = placeholders {
|
||||||
for (index, element) in payload.iter_mut().enumerate() {
|
for (index, element) in payload.iter_mut().enumerate() {
|
||||||
if let Some((p, persist)) = placeholders[index] {
|
if let Some((p, persist)) = &placeholders[index] {
|
||||||
element.placeholder = Some(p);
|
element.placeholder = Some(p.to_string());
|
||||||
element.placeholder_persist = persist;
|
element.placeholder_persist = *persist;
|
||||||
} else {
|
} else {
|
||||||
element.placeholder = None;
|
element.placeholder = None;
|
||||||
element.placeholder_persist = false;
|
element.placeholder_persist = false;
|
||||||
|
|
@ -213,7 +209,7 @@ impl ActionApi for TextEntry {
|
||||||
tv.text.clear();
|
tv.text.clear();
|
||||||
let content = {
|
let content = {
|
||||||
if payload.placeholder.is_some() && payload.content.len().is_zero() {
|
if payload.placeholder.is_some() && payload.content.len().is_zero() {
|
||||||
let placeholder_content = payload.placeholder.unwrap();
|
let placeholder_content = payload.placeholder.as_deref().unwrap();
|
||||||
placeholder_content.to_string()
|
placeholder_content.to_string()
|
||||||
} else {
|
} else {
|
||||||
payload.content.to_string()
|
payload.content.to_string()
|
||||||
|
|
@ -346,7 +342,7 @@ impl ActionApi for TextEntry {
|
||||||
Some(payload.content.len() as i32)
|
Some(payload.content.len() as i32)
|
||||||
};
|
};
|
||||||
tv.text.clear(); // make sure this is blank
|
tv.text.clear(); // make sure this is blank
|
||||||
let payload_chars = payload.content.as_str().unwrap().chars().count();
|
let payload_chars = payload.content.as_str().chars().count();
|
||||||
// TODO: condense the "above MAX_CHARS" chars length path a bit -- written out "the dumb way" just
|
// TODO: condense the "above MAX_CHARS" chars length path a bit -- written out "the dumb way" just
|
||||||
// to reason out the logic a bit
|
// to reason out the logic a bit
|
||||||
match self.visibility {
|
match self.visibility {
|
||||||
|
|
@ -356,7 +352,7 @@ impl ActionApi for TextEntry {
|
||||||
&& payload.content.len().is_zero()
|
&& payload.content.len().is_zero()
|
||||||
&& !self.keys_hit[index]
|
&& !self.keys_hit[index]
|
||||||
{
|
{
|
||||||
let placeholder_content = payload.placeholder.unwrap();
|
let placeholder_content = payload.placeholder.as_ref().unwrap();
|
||||||
placeholder_content.to_string()
|
placeholder_content.to_string()
|
||||||
} else {
|
} else {
|
||||||
payload.content.to_string()
|
payload.content.to_string()
|
||||||
|
|
@ -387,52 +383,50 @@ impl ActionApi for TextEntry {
|
||||||
}
|
}
|
||||||
TextEntryVisibility::Hidden => {
|
TextEntryVisibility::Hidden => {
|
||||||
if payload_chars < MAX_CHARS {
|
if payload_chars < MAX_CHARS {
|
||||||
for _char in payload.content.as_str().unwrap().chars() {
|
for _char in payload.content.as_str().chars() {
|
||||||
tv.text.push('*').expect("text field too long");
|
tv.text.push('*');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// just render a pure dummy string
|
// just render a pure dummy string
|
||||||
tv.text.push('.').unwrap();
|
tv.text.push('.');
|
||||||
tv.text.push('.').unwrap();
|
tv.text.push('.');
|
||||||
tv.text.push('.').unwrap();
|
tv.text.push('.');
|
||||||
for _ in 0..(MAX_CHARS - 3) {
|
for _ in 0..(MAX_CHARS - 3) {
|
||||||
tv.text.push('*').expect("text field too long");
|
tv.text.push('*');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
modal.gam.post_textview(&mut tv).expect("couldn't post textview");
|
modal.gam.post_textview(&mut tv).expect("couldn't post textview");
|
||||||
}
|
}
|
||||||
TextEntryVisibility::LastChars => {
|
TextEntryVisibility::LastChars => {
|
||||||
if payload_chars < MAX_CHARS {
|
if payload_chars < MAX_CHARS {
|
||||||
let hide_to = if payload.content.as_str().unwrap().chars().count() >= 2 {
|
let hide_to = if payload.content.as_str().chars().count() >= 2 {
|
||||||
payload.content.as_str().unwrap().chars().count() - 2
|
payload.content.as_str().chars().count() - 2
|
||||||
} else {
|
} else {
|
||||||
0
|
0
|
||||||
};
|
};
|
||||||
for (index, ch) in payload.content.as_str().unwrap().chars().enumerate() {
|
for (index, ch) in payload.content.as_str().chars().enumerate() {
|
||||||
if index < hide_to {
|
if index < hide_to {
|
||||||
tv.text.push('*').expect("text field too long");
|
tv.text.push('*');
|
||||||
} else {
|
} else {
|
||||||
tv.text.push(ch).expect("text field too long");
|
tv.text.push(ch);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
tv.text.push('.').unwrap();
|
tv.text.push('.');
|
||||||
tv.text.push('.').unwrap();
|
tv.text.push('.');
|
||||||
tv.text.push('.').unwrap();
|
tv.text.push('.');
|
||||||
let hide_to = if payload.content.as_str().unwrap().chars().count() >= 2 {
|
let hide_to = if payload.content.as_str().chars().count() >= 2 {
|
||||||
payload.content.as_str().unwrap().chars().count() - 2
|
payload.content.as_str().chars().count() - 2
|
||||||
} else {
|
} else {
|
||||||
0
|
0
|
||||||
};
|
};
|
||||||
for (index, ch) in payload.content.as_str().unwrap()
|
for (index, ch) in
|
||||||
[payload_chars - (MAX_CHARS - 3)..]
|
payload.content.as_str()[payload_chars - (MAX_CHARS - 3)..].chars().enumerate()
|
||||||
.chars()
|
|
||||||
.enumerate()
|
|
||||||
{
|
{
|
||||||
if index + payload_chars - (MAX_CHARS - 3) < hide_to {
|
if index + payload_chars - (MAX_CHARS - 3) < hide_to {
|
||||||
tv.text.push('*').expect("text field too long");
|
tv.text.push('*');
|
||||||
} else {
|
} else {
|
||||||
tv.text.push(ch).expect("text field too long");
|
tv.text.push(ch);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -583,8 +577,8 @@ impl ActionApi for TextEntry {
|
||||||
}
|
}
|
||||||
} else if !self.is_password {
|
} else if !self.is_password {
|
||||||
if payload.content.len() == 0 {
|
if payload.content.len() == 0 {
|
||||||
if let Some(placeholder) = payload.placeholder {
|
if let Some(placeholder) = payload.placeholder.as_ref() {
|
||||||
payload.content.append(placeholder.to_str()).ok();
|
payload.content.push_str(placeholder);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if let Some(index) = payload.insertion_point {
|
if let Some(index) = payload.insertion_point {
|
||||||
|
|
@ -608,8 +602,8 @@ impl ActionApi for TextEntry {
|
||||||
}
|
}
|
||||||
} else if !self.is_password {
|
} else if !self.is_password {
|
||||||
if payload.content.len() == 0 {
|
if payload.content.len() == 0 {
|
||||||
if let Some(placeholder) = payload.placeholder {
|
if let Some(placeholder) = payload.placeholder.as_ref() {
|
||||||
payload.content.append(placeholder.to_str()).ok();
|
payload.content.push_str(placeholder);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if let Some(index) = payload.insertion_point.take() {
|
if let Some(index) = payload.insertion_point.take() {
|
||||||
|
|
@ -624,8 +618,8 @@ impl ActionApi for TextEntry {
|
||||||
}
|
}
|
||||||
'∴' | '\u{d}' => {
|
'∴' | '\u{d}' => {
|
||||||
if payload.content.len() == 0 && !self.keys_hit[self.selected_field as usize] {
|
if payload.content.len() == 0 && !self.keys_hit[self.selected_field as usize] {
|
||||||
if let Some(placeholder) = payload.placeholder {
|
if let Some(placeholder) = payload.placeholder.as_ref() {
|
||||||
payload.content.append(placeholder.to_str()).ok();
|
payload.content.push_str(placeholder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(validator) = self.validator {
|
if let Some(validator) = self.validator {
|
||||||
|
|
@ -640,8 +634,8 @@ impl ActionApi for TextEntry {
|
||||||
self.action_payloads[..self.max_field_amount as usize].iter_mut().enumerate()
|
self.action_payloads[..self.max_field_amount as usize].iter_mut().enumerate()
|
||||||
{
|
{
|
||||||
if raw_payload.content.len() == 0 && !self.keys_hit[index] {
|
if raw_payload.content.len() == 0 && !self.keys_hit[index] {
|
||||||
if let Some(placeholder) = raw_payload.placeholder {
|
if let Some(placeholder) = raw_payload.placeholder.as_ref() {
|
||||||
raw_payload.content.append(placeholder.to_str()).ok();
|
raw_payload.content.push_str(placeholder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -691,7 +685,7 @@ impl ActionApi for TextEntry {
|
||||||
if payload.placeholder_persist && payload.placeholder.is_some() && payload.content.len() == 0
|
if payload.placeholder_persist && payload.placeholder.is_some() && payload.content.len() == 0
|
||||||
{
|
{
|
||||||
// copy the placeholder into the content string before processing the backspace
|
// copy the placeholder into the content string before processing the backspace
|
||||||
payload.content.append(payload.placeholder.unwrap().to_str()).ok();
|
payload.content.push_str(payload.placeholder.as_ref().unwrap());
|
||||||
}
|
}
|
||||||
if let Some(insertion_point) = payload.insertion_point {
|
if let Some(insertion_point) = payload.insertion_point {
|
||||||
if insertion_point > 0 {
|
if insertion_point > 0 {
|
||||||
|
|
@ -699,20 +693,20 @@ impl ActionApi for TextEntry {
|
||||||
insertion_point < payload.content.len(),
|
insertion_point < payload.content.len(),
|
||||||
"insertion point beyond content length!"
|
"insertion point beyond content length!"
|
||||||
);
|
);
|
||||||
let new_len = payload.content.as_str().unwrap().chars().count() - 1;
|
let new_len = payload.content.as_str().chars().count() - 1;
|
||||||
|
|
||||||
// have to use a temporary string because index operators are not implemented on Xous
|
// have to use a temporary string because index operators are not implemented on Xous
|
||||||
// strings
|
// strings
|
||||||
let mut temp_str = String::<256>::new();
|
let mut temp_str = String::new();
|
||||||
let mut original = payload.content.as_str().unwrap().chars().enumerate().peekable();
|
let mut original = payload.content.as_str().chars().enumerate().peekable();
|
||||||
// copy the data over, skipping the character that was deleted
|
// copy the data over, skipping the character that was deleted
|
||||||
loop {
|
loop {
|
||||||
if let Some((index, c)) = original.next() {
|
if let Some((index, c)) = original.next() {
|
||||||
if index < insertion_point.saturating_sub(1) {
|
if index < insertion_point.saturating_sub(1) {
|
||||||
temp_str.push(c).ok();
|
temp_str.push(c);
|
||||||
} else {
|
} else {
|
||||||
if let Some((_, next_char)) = original.peek() {
|
if let Some((_, next_char)) = original.peek() {
|
||||||
temp_str.push(*next_char).ok();
|
temp_str.push(*next_char);
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -722,13 +716,28 @@ impl ActionApi for TextEntry {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// now copy the data back into the original string
|
// now copy the data back into the original string
|
||||||
let mut c_iter = temp_str.as_str().unwrap().chars();
|
let mut c_iter = temp_str.as_str().chars();
|
||||||
payload.content.clear();
|
payload.content.clear();
|
||||||
for _ in 0..new_len {
|
for _ in 0..new_len {
|
||||||
payload.content.push(c_iter.next().unwrap()).unwrap();
|
payload.content.push(c_iter.next().unwrap());
|
||||||
}
|
}
|
||||||
// clear the temp string
|
// clear the temp string
|
||||||
temp_str.volatile_clear();
|
// safety: we are going to turn the underlying pointer into NULL bytes, which is valid
|
||||||
|
// UTF-8
|
||||||
|
let temp_bytes = unsafe { temp_str.as_bytes_mut() };
|
||||||
|
let len = temp_bytes.len();
|
||||||
|
let ptr = temp_bytes.as_mut_ptr();
|
||||||
|
for i in 0..len {
|
||||||
|
// safety: the bounds were derived from a valid len; the pointer is aligned
|
||||||
|
// because it's derived from a slice. We use the
|
||||||
|
// "unsafe" version of this to force a zeroize
|
||||||
|
// of contents and avoid optimizations that could otherwise cause this operation
|
||||||
|
// to be skipped (e.g. dropping and de-allocating
|
||||||
|
// without scrubbing)
|
||||||
|
unsafe { ptr.add(i).write_volatile(0) };
|
||||||
|
}
|
||||||
|
// force all the writes to finish if they were re-ordered
|
||||||
|
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
|
||||||
// bring the insertion point back one index
|
// bring the insertion point back one index
|
||||||
payload.insertion_point = Some(insertion_point.saturating_sub(1));
|
payload.insertion_point = Some(insertion_point.saturating_sub(1));
|
||||||
}
|
}
|
||||||
|
|
@ -737,14 +746,27 @@ impl ActionApi for TextEntry {
|
||||||
// plaintext on the stack
|
// plaintext on the stack
|
||||||
if payload.content.len() > 0 {
|
if payload.content.len() > 0 {
|
||||||
// don't backspace if we have no string.
|
// don't backspace if we have no string.
|
||||||
let mut temp_str = String::<256>::from_str(payload.content.as_str().unwrap());
|
let mut temp_str = String::from(payload.content.as_str());
|
||||||
let cur_len = temp_str.as_str().unwrap().chars().count();
|
let cur_len = temp_str.as_str().chars().count();
|
||||||
let mut c_iter = temp_str.as_str().unwrap().chars();
|
let mut c_iter = temp_str.as_str().chars();
|
||||||
payload.content.clear();
|
payload.content.clear();
|
||||||
for _ in 0..cur_len - 1 {
|
for _ in 0..cur_len - 1 {
|
||||||
payload.content.push(c_iter.next().unwrap()).unwrap();
|
payload.content.push(c_iter.next().unwrap());
|
||||||
}
|
}
|
||||||
temp_str.volatile_clear();
|
let temp_bytes = unsafe { temp_str.as_bytes_mut() };
|
||||||
|
let len = temp_bytes.len();
|
||||||
|
let ptr = temp_bytes.as_mut_ptr();
|
||||||
|
for i in 0..len {
|
||||||
|
// safety: the bounds were derived from a valid len; the pointer is aligned
|
||||||
|
// because it's derived from a slice. We use the
|
||||||
|
// "unsafe" version of this to force a zeroize
|
||||||
|
// of contents and avoid optimizations that could otherwise cause this operation
|
||||||
|
// to be skipped (e.g. dropping and de-allocating
|
||||||
|
// without scrubbing)
|
||||||
|
unsafe { ptr.add(i).write_volatile(0) };
|
||||||
|
}
|
||||||
|
// force all the writes to finish if they were re-ordered
|
||||||
|
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -756,7 +778,7 @@ impl ActionApi for TextEntry {
|
||||||
&& payload.content.len() == 0
|
&& payload.content.len() == 0
|
||||||
{
|
{
|
||||||
// copy the placeholder into the content string before processing the backspace
|
// copy the placeholder into the content string before processing the backspace
|
||||||
payload.content.append(payload.placeholder.unwrap().to_str()).ok();
|
payload.content.push_str(payload.placeholder.as_ref().unwrap());
|
||||||
}
|
}
|
||||||
self.keys_hit[self.selected_field as usize] = true;
|
self.keys_hit[self.selected_field as usize] = true;
|
||||||
#[cfg(feature = "tts")]
|
#[cfg(feature = "tts")]
|
||||||
|
|
@ -770,26 +792,41 @@ impl ActionApi for TextEntry {
|
||||||
_ => {
|
_ => {
|
||||||
if let Some(insertion_point) = payload.insertion_point {
|
if let Some(insertion_point) = payload.insertion_point {
|
||||||
if insertion_point >= payload.content.len() {
|
if insertion_point >= payload.content.len() {
|
||||||
payload.content.push(k).expect("ran out of space storing password");
|
payload.content.push(k);
|
||||||
payload.insertion_point = None;
|
payload.insertion_point = None;
|
||||||
} else {
|
} else {
|
||||||
// have to use a temporary string because index operators are not implemented
|
// have to use a temporary string because index operators are not implemented
|
||||||
// on Xous strings
|
// on Xous strings
|
||||||
let mut temp_str = String::<256>::from_str(payload.content.as_str().unwrap());
|
let mut temp_str = String::from(payload.content.as_str());
|
||||||
let cur_len = temp_str.as_str().unwrap().chars().count();
|
let cur_len = temp_str.as_str().chars().count();
|
||||||
let mut c_iter = temp_str.as_str().unwrap().chars();
|
let mut c_iter = temp_str.as_str().chars();
|
||||||
payload.content.clear();
|
payload.content.clear();
|
||||||
for i in 0..cur_len {
|
for i in 0..cur_len {
|
||||||
if i == insertion_point {
|
if i == insertion_point {
|
||||||
payload.content.push(k).ok(); // don't panic if we type too much, just silently drop the character
|
payload.content.push(k); // don't panic if we type too much, just silently drop the character
|
||||||
}
|
}
|
||||||
payload.content.push(c_iter.next().unwrap()).unwrap();
|
payload.content.push(c_iter.next().unwrap());
|
||||||
}
|
}
|
||||||
payload.insertion_point = Some(insertion_point + 1);
|
payload.insertion_point = Some(insertion_point + 1);
|
||||||
temp_str.volatile_clear();
|
let temp_bytes = unsafe { temp_str.as_bytes_mut() };
|
||||||
|
let len = temp_bytes.len();
|
||||||
|
let ptr = temp_bytes.as_mut_ptr();
|
||||||
|
for i in 0..len {
|
||||||
|
// safety: the bounds were derived from a valid len; the pointer is
|
||||||
|
// aligned because it's derived from a
|
||||||
|
// slice. We use the "unsafe" version
|
||||||
|
// of this to force a zeroize
|
||||||
|
// of contents and avoid optimizations that could otherwise cause this
|
||||||
|
// operation to be skipped (e.g.
|
||||||
|
// dropping and de-allocating
|
||||||
|
// without scrubbing)
|
||||||
|
unsafe { ptr.add(i).write_volatile(0) };
|
||||||
|
}
|
||||||
|
// force all the writes to finish if they were re-ordered
|
||||||
|
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
payload.content.push(k).ok(); // don't panic if we type too much, just silently drop the character
|
payload.content.push(k); // don't panic if we type too much, just silently drop the character
|
||||||
}
|
}
|
||||||
log::trace!("****update payload: {}", payload.content);
|
log::trace!("****update payload: {}", payload.content);
|
||||||
payload.dirty = true;
|
payload.dirty = true;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue