| 1 | //! Routines for "secure" memory operations; i.e. data that we need to send to Linux-PAM and don't |
| 2 | //! want any copies to leak (that we would then need to zeroize). |
| 3 | use std::{ |
| 4 | alloc::{self, Layout}, |
| 5 | ptr::NonNull, |
| 6 | slice, |
| 7 | }; |
| 8 | |
| 9 | const SIZE: usize = super::sys::PAM_MAX_RESP_SIZE as usize; |
| 10 | |
| 11 | pub struct PamBuffer(NonNull<[u8; SIZE]>); |
| 12 | |
| 13 | const LAYOUT: Layout = match Layout::from_size_align(SIZE, 1) { |
| 14 | Ok(layout) => layout, |
| 15 | Err(_) => unreachable!(), |
| 16 | }; |
| 17 | |
| 18 | impl PamBuffer { |
| 19 | // consume this buffer and return its internal pointer |
| 20 | // (ending the type-level security, but guaranteeing you need unsafe code to access the data) |
| 21 | pub fn leak(self) -> NonNull<u8> { |
| 22 | let result = self.0; |
| 23 | std::mem::forget(self); |
| 24 | |
| 25 | result.cast() |
| 26 | } |
| 27 | |
| 28 | // initialize the buffer with already existing data (otherwise populating it is a bit hairy) |
| 29 | // this is inferior to placing the data into the securebuffer directly |
| 30 | #[cfg(test)] |
| 31 | pub fn new(mut src: impl AsMut<[u8]>) -> Self { |
| 32 | let mut buffer = PamBuffer::default(); |
| 33 | let src = src.as_mut(); |
| 34 | buffer[..src.len()].copy_from_slice(src); |
| 35 | wipe_memory(src); |
| 36 | |
| 37 | buffer |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | impl Default for PamBuffer { |
| 42 | fn default() -> Self { |
| 43 | // SAFETY: `calloc` returns either a cleared, allocated chunk of `SIZE` bytes |
| 44 | // or NULL to indicate that the allocation request failed |
| 45 | let res = unsafe { libc::calloc(1, SIZE) }; |
| 46 | if let Some(nn) = NonNull::new(res) { |
| 47 | PamBuffer(nn.cast()) |
| 48 | } else { |
| 49 | alloc::handle_alloc_error(LAYOUT) |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | impl std::ops::Deref for PamBuffer { |
| 55 | type Target = [u8]; |
| 56 | |
| 57 | fn deref(&self) -> &[u8] { |
| 58 | // SAFETY: `self.0.as_ptr()` is non-null, aligned, and initialized, and points to `SIZE` bytes. |
| 59 | // The lifetime of the slice does not exceed that of `self`. |
| 60 | // |
| 61 | // We make the slice one less in size to guarantee the existence of a terminating NUL. |
| 62 | unsafe { slice::from_raw_parts(self.0.as_ptr().cast(), SIZE - 1) } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | impl std::ops::DerefMut for PamBuffer { |
| 67 | fn deref_mut(&mut self) -> &mut [u8] { |
| 68 | // SAFETY: see above |
| 69 | unsafe { slice::from_raw_parts_mut(self.0.as_ptr().cast(), SIZE - 1) } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | impl Drop for PamBuffer { |
| 74 | fn drop(&mut self) { |
| 75 | // SAFETY: same as for `deref()` and `deref_mut()` |
| 76 | wipe_memory(unsafe { self.0.as_mut() }); |
| 77 | // SAFETY: `self.0.as_ptr()` was obtained via `calloc`, so calling `free` is proper. |
| 78 | unsafe { libc::free(self.0.as_ptr().cast()) } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /// Used to zero out memory and protect sensitive data from leaking; inspired by Conrad Kleinespel's |
| 83 | /// Rustatic rtoolbox::SafeString, <https://crates.io/crates/rtoolbox/0.0.1> |
| 84 | fn wipe_memory(memory: &mut [u8]) { |
| 85 | use std::sync::atomic; |
| 86 | |
| 87 | let nonsense: u8 = 0x55; |
| 88 | for c in memory { |
| 89 | // SAFETY: `c` is safe for writes (it comes from a &mut reference) |
| 90 | unsafe { std::ptr::write_volatile(c, nonsense) }; |
| 91 | } |
| 92 | |
| 93 | atomic::fence(atomic::Ordering::SeqCst); |
| 94 | atomic::compiler_fence(atomic::Ordering::SeqCst); |
| 95 | } |
| 96 | |
| 97 | #[allow(clippy::undocumented_unsafe_blocks)] |
| 98 | #[cfg(test)] |
| 99 | mod test { |
| 100 | use super::PamBuffer; |
| 101 | |
| 102 | #[test] |
| 103 | fn miri_test_leaky_cstring() { |
| 104 | let test = |text: &str| unsafe { |
| 105 | let buf = PamBuffer::new(text.to_string().as_bytes_mut()); |
| 106 | assert_eq!(&buf[..text.len()], text.as_bytes()); |
| 107 | let nn = buf.leak(); |
| 108 | let result = crate::cutils::string_from_ptr(nn.as_ptr().cast()); |
| 109 | libc::free(nn.as_ptr().cast()); |
| 110 | result |
| 111 | }; |
| 112 | assert_eq!(test(""), ""); |
| 113 | assert_eq!(test("hello"), "hello"); |
| 114 | } |
| 115 | |
| 116 | #[test] |
| 117 | fn miri_test_wipe() { |
| 118 | let mut memory: [u8; 3] = [1, 2, 3]; |
| 119 | let fix = PamBuffer::new(&mut memory); |
| 120 | assert_eq!(memory, [0x55, 0x55, 0x55]); |
| 121 | assert_eq!(fix[0..=2], [1, 2, 3]); |
| 122 | assert!(fix[3..].iter().all(|&x| x == 0)); |
| 123 | std::mem::drop(fix); |
| 124 | } |
| 125 | } |