From 42a1193522a5503acb480409751479f584b32db9 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 26 Jul 2021 18:12:13 +0800 Subject: [PATCH] add volatile_clear() to xous_ipc::String for password fields Call this on Drop() to ensure that the string is cleared. Written using volatile pointers and compiler fences to ensure the clear is not optimized out or re-ordered. --- xous-ipc/src/string.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/xous-ipc/src/string.rs b/xous-ipc/src/string.rs index d9050f9c9..afc5c79f0 100644 --- a/xous-ipc/src/string.rs +++ b/xous-ipc/src/string.rs @@ -20,6 +20,19 @@ impl String { } } + // use a volatile write to ensure a clear operation is not optimized out + // for ensuring that a string is cleared, e.g. at the exit of a function + pub fn volatile_clear(&mut self) { + let b = self.bytes.as_mut_ptr(); + for i in 0..N { + unsafe{b.add(i).write_volatile(core::mem::zeroed());} + } + // Ensure the compiler doesn't re-order the clear. + // We use `SeqCst`, because `Acquire` only prevents later accesses from being reordered before + // *reads*, but this method only *writes* to the locations. + core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst); + } + pub fn from_str(src: T) -> String where T: AsRef { let src = src.as_ref(); let mut s = Self::new();