1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
#![allow(missing_debug_implementations)]
#![allow(unsafe_code)]
use crate::ffi::sodium;
use crate::traits::*;
use std::borrow::BorrowMut;
use std::fmt::{self, Debug, Formatter};
use std::ops::{Deref, DerefMut};
use std::thread;
/// A type for protecting secrets allocated on the stack.
///
/// Stack-allocated secrets have distinct security needs from
/// heap-allocated secrets, and should be preferred when possible. They
/// provide the following guarantees:
///
/// * [`mlock(2)`][mlock] is called on the underlying memory
/// * [`munlock(2)`][mlock] is called on the underlying memory when no longer in use
/// * the underlying memory is zeroed out when no longer in use
/// * they are borrowed for their entire lifespan, so cannot be moved
/// * they are best-effort compared in constant time
/// * they are best-effort prevented from being printed by [`Debug`]
/// * they are best-effort prevented from being [`Clone`]d
///
/// To fulfill these guarantees, [`Secret`]s are constructed in an
/// atypical pattern. Rather than having [`new`](Secret::new) return a
/// newly-created instance, [`new`](Secret::new) accepts a callback
/// argument that is provided with a mutably borrowed wrapper around the
/// data in question. This wrapper [`Deref`]s into the desired type,
/// with replacement implementations of [`Debug`], [`PartialEq`], and
/// [`Eq`] to prevent accidental misuse.
///
/// Users *must* take care when dereferencing secrets as this will
/// provide direct access to the underlying type. If the bare type
/// implements traits like [`Clone`], [`Debug`], and [`PartialEq`],
/// those methods can be called directly and will not benefit from the
/// protections provided by this wrapper.
///
/// Users *must* also take care to avoid unintentionally invoking
/// [`Copy`] on the underlying data, as doing so will result in
/// secret data being copied out of the [`Secret`], thus losing the
/// protections provided by this library. Be careful not to invoke
/// methods that take ownership of `self` or functions that move
/// parameters with secret data, since doing so will implicitly create
/// copies.
///
/// # Example: generate a cryptographically-random 128-bit [`Secret`]
///
/// Initialize a [`Secret`] with cryptographically random data:
///
/// ```
/// # use secrets::Secret;
/// Secret::<[u8; 16]>::random(|s| {
/// // use `s` as if it were a `&mut [u8; 16]`
/// });
/// ```
///
/// # Example: move mutable data into a [`Secret`]
///
/// Existing data can be moved into a [`Secret`]. When doing so, we make
/// a best-effort attempt to zero out the data in the original location.
/// Any prior copies will be unaffected, so please exercise as much
/// caution as possible when handling data before it can be protected.
///
/// ```
/// # use secrets::Secret;
/// let mut value = [1u8, 2, 3, 4];
///
/// // the contents of `value` will be copied into the Secret before
/// // being zeroed out
/// Secret::from(&mut value, |s| {
/// assert_eq!(*s, [1, 2, 3, 4]);
/// });
///
/// // the contents of `value` have been zeroed
/// assert_eq!(value, [0, 0, 0, 0]);
/// ```
///
/// [mlock]: http://man7.org/linux/man-pages/man2/mlock.2.html
pub struct Secret<T: Bytes> {
/// The internal protected memory for the [`Secret`].
data: T,
}
/// A mutable [`Deref`]-wrapper around a [`Secret`]'s internal
/// contents that intercepts calls like [`Clone::clone`] and
/// [`Debug::fmt`] that are likely to result in the inadvertent
/// disclosure of secret data.
pub struct RefMut<'a, T: Bytes> {
/// a reference to the underlying secret data that will be derefed
data: &'a mut T,
}
impl<T: Bytes> Secret<T> {
/// Creates a new [`Secret`] and invokes the provided callback with
/// a wrapper to the protected memory. This memory will be filled
/// with a well-defined, arbitrary byte pattern, and should be
/// initialized to something meaningful before actual use.
///
/// # Panics
///
/// This function will panic if the underlying call to `mlock(2)`
/// (`VirtualLock` on windows) fails.
///
/// ```
/// # use secrets::Secret;
/// use std::fs::File;
/// use std::io::prelude::*;
///
/// Secret::<[u8; 32]>::new(|mut s| {
/// File::open("/dev/urandom")?.read_exact(&mut s[..])
/// });
/// ```
#[cfg_attr(feature = "cargo-clippy", allow(clippy::new_ret_no_self))]
pub fn new<F, U>(f: F) -> U
where
F: FnOnce(RefMut<'_, T>) -> U,
{
tested!(std::mem::size_of::<T>() == 0);
let mut secret = Self {
data: T::uninitialized(),
};
assert!(
unsafe { sodium::mlock(&mut secret.data) },
"secrets: unable to mlock memory for a Secret"
);
f(RefMut::new(&mut secret.data))
}
}
impl<T: Bytes + Zeroable> Secret<T> {
/// Creates a new [`Secret`] filled with zeroed bytes and invokes the
/// callback with a wrapper to the protected memory.
///
/// ```
/// # use secrets::Secret;
/// Secret::<u8>::zero(|s| {
/// assert_eq!(*s, 0);
/// });
/// ```
pub fn zero<F, U>(f: F) -> U
where
F: FnOnce(RefMut<'_, T>) -> U,
{
Self::new(|mut s| {
s.zero();
f(s)
})
}
/// Creates a new [`Secret`] from existing, unprotected data, and
/// immediately zeroes out the memory of the data being moved in.
/// Invokes the callback with a wrapper to the protected memory.
///
/// ```
/// # use secrets::Secret;
/// let mut bytes : [u32; 4] = [1, 2, 3, 4];
///
/// Secret::from(&mut bytes, |s| {
/// assert_eq!(*s, [1, 2, 3, 4]);
/// });
///
/// assert_eq!(bytes, [0, 0, 0, 0]);
/// ```
pub fn from<F, U>(v: &mut T, f: F) -> U
where
F: FnOnce(RefMut<'_, T>) -> U,
{
Self::new(|mut s| {
let _ = &v; // ensure the entirety of `v` is closed over
unsafe { v.transfer(s.borrow_mut()) };
f(s)
})
}
}
impl<T: Bytes + Randomizable> Secret<T> {
/// Creates a new [`Secret`] filled with random bytes and invokes
/// the callback with a wrapper to the protected memory.
///
/// ```
/// # use secrets::Secret;
/// Secret::<u128>::random(|s| {
/// // s is filled with random bytes
/// })
/// ```
pub fn random<F, U>(f: F) -> U
where
F: FnOnce(RefMut<'_, T>) -> U,
{
Self::new(|mut s| {
s.randomize();
f(s)
})
}
}
impl<T: Bytes> Drop for Secret<T> {
/// Ensures that the [`Secret`]'s underlying memory is `munlock`ed
/// and zeroed when it leaves scope.
fn drop(&mut self) {
// When we call sodium_munlock on some data, it actually unlocks the entire page that
// contains the memory. If two locked items were on the same page, then the second one
// fails because it was already unlocked. On Linux, this does now throw an error. On
// Windows, it does. We'll ignore it for now, and provide a better fix later.
if unsafe { !sodium::munlock(&mut self.data) }
&& !(cfg!(target_family = "windows")
&& std::io::Error::last_os_error().raw_os_error().map_or(false, |c| c == 158)) {
// [`Drop::drop`] is called during stack unwinding, so we
// may be in a panic already.
assert!(
thread::panicking(),
"secrets: unable to munlock memory for a Secret"
);
};
}
}
impl<'a, T: Bytes> RefMut<'a, T> {
/// Instantiates a new `RefMut`.
pub(crate) fn new(data: &'a mut T) -> Self {
Self { data }
}
}
impl<T: Bytes + Clone> Clone for RefMut<'_, T> {
fn clone(&self) -> Self {
panic!("secrets: a Secret may not be cloned")
}
}
impl<T: Bytes> Debug for RefMut<'_, T> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{{ {} bytes redacted }}", self.data.size())
}
}
impl<T: Bytes> Deref for RefMut<'_, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.data
}
}
impl<T: Bytes> DerefMut for RefMut<'_, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.data
}
}
impl<T: Bytes> PartialEq for RefMut<'_, T> {
fn eq(&self, rhs: &Self) -> bool {
self.data.constant_eq(rhs.data)
}
}
impl<T: Bytes> Eq for RefMut<'_, T> {}
// LCOV_EXCL_START
#[cfg(test)]
mod tests {
use super::*;
use std::ptr;
#[test]
fn it_defaults_to_garbage_data() {
Secret::<u16>::new(|s| assert_eq!(*s, 0xdbdb));
}
#[test]
fn it_zeroes_when_leaving_scope() {
unsafe {
let mut ptr: *const _ = ptr::null();
Secret::<u128>::new(|mut s| {
// Funnily enough, this test also fails (in release
// mode) if we set `s` to since the Rust compiler
// rightly determines that this entire block does
// nothing and can be optimized away.
//
// So we use `sodium::memrandom` which `rustc` doesn't
// get to perform analysis on to force the compiler to
// not optimize this whole thing away.
sodium::memrandom(s.as_mut_bytes());
// Assign to a pointer that outlives this, which is
// totally undefined behavior but there's no real other
// way to test that this works.
ptr = &*s;
});
// This is extremely brittle. It works with integers because
// they compare equality directly but it doesn't work with
// arrays since they compare using a function call which
// clobbers the value of `ptr` since it's pointing to the
// stack.
//
// Still, a test here is better than no test here. It would
// just be nice if we could also test with arrays, but the
// logic should work regardless. This was spot-checked in a
// debugger as well.
assert_eq!(*ptr, 0);
}
}
#[test]
fn it_initializes_from_values() {
Secret::from(&mut 5, |s| assert_eq!(*s, 5_u8));
}
#[test]
fn it_zeroes_values_when_initializing_from() {
let mut value = 5_u8;
Secret::from(&mut value, |_| {});
assert_eq!(value, 0);
}
#[test]
fn it_compares_equality() {
Secret::<u32>::from(&mut 0x0123_4567, |a| {
Secret::<u32>::from(&mut 0x0123_4567, |b| {
assert_eq!(a, b);
});
});
}
#[test]
fn it_compares_inequality() {
Secret::<[u64; 4]>::random(|a| {
Secret::<[u64; 4]>::random(|b| {
assert_ne!(a, b);
});
});
}
#[test]
fn it_preserves_secrecy() {
Secret::<[u64; 2]>::zero(|s| {
assert_eq!(
format!("{{ {} bytes redacted }}", 16),
format!("{:?}", s),
);
});
}
#[test]
#[should_panic(expected = "secrets: a Secret may not be cloned")]
fn it_panics_when_cloned() {
#[cfg_attr(feature = "cargo-clippy", allow(clippy::redundant_clone))]
Secret::<u16>::zero(|s| {
let _ = s.clone();
});
}
#[test]
#[should_panic(expected = "secrets: unable to mlock memory for a Secret")]
fn it_detects_sodium_mlock_failure() {
sodium::fail();
Secret::<u8>::zero(|_| {});
}
#[test]
#[should_panic(expected = "secrets: unable to munlock memory for a Secret")]
fn it_detects_sodium_munlock_failure() {
Secret::<u8>::zero(|_| sodium::fail());
}
}
// LCOV_EXCL_STOP