aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / zeroize / tests / zeroize.rs
| 1 | //! zeroize integration tests. |
| 2 | |
| 3 | #![allow( |
| 4 | clippy::missing_safety_doc, |
| 5 | clippy::std_instead_of_alloc, |
| 6 | clippy::undocumented_unsafe_blocks |
| 7 | )] |
| 8 | |
| 9 | use core::{ |
| 10 | marker::{PhantomData, PhantomPinned}, |
| 11 | mem::{MaybeUninit, size_of}, |
| 12 | num::*, |
| 13 | ptr, |
| 14 | }; |
| 15 | use std::sync::Arc; |
| 16 | use zeroize::*; |
| 17 | |
| 18 | #[cfg(feature = "std")] |
| 19 | use std::ffi::CString; |
| 20 | |
| 21 | #[derive(Clone, Debug, PartialEq)] |
| 22 | struct ZeroizedOnDrop(u64); |
| 23 | |
| 24 | impl Drop for ZeroizedOnDrop { |
| 25 | fn drop(&mut self) { |
| 26 | self.0.zeroize(); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | #[test] |
| 31 | fn non_zero() { |
| 32 | macro_rules! non_zero_test { |
| 33 | ($($type:ty),+) => { |
| 34 | $(let mut value = <$type>::new(42).unwrap(); |
| 35 | value.zeroize(); |
| 36 | assert_eq!(value.get(), 1);)+ |
| 37 | }; |
| 38 | } |
| 39 | |
| 40 | non_zero_test!( |
| 41 | NonZeroI8, |
| 42 | NonZeroI16, |
| 43 | NonZeroI32, |
| 44 | NonZeroI64, |
| 45 | NonZeroI128, |
| 46 | NonZeroIsize, |
| 47 | NonZeroU8, |
| 48 | NonZeroU16, |
| 49 | NonZeroU32, |
| 50 | NonZeroU64, |
| 51 | NonZeroU128, |
| 52 | NonZeroUsize |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | #[test] |
| 57 | fn zeroize_byte_arrays() { |
| 58 | let mut arr = [42u8; 137]; |
| 59 | arr.zeroize(); |
| 60 | assert_eq!(arr.as_ref(), [0u8; 137].as_ref()); |
| 61 | } |
| 62 | |
| 63 | #[test] |
| 64 | fn zeroize_on_drop_byte_arrays() { |
| 65 | let mut arr = [ZeroizedOnDrop(42); 1]; |
| 66 | unsafe { ptr::drop_in_place(&raw mut arr) }; |
| 67 | assert_eq!(arr.as_ref(), [ZeroizedOnDrop(0); 1].as_ref()); |
| 68 | } |
| 69 | |
| 70 | #[test] |
| 71 | fn zeroize_maybeuninit_byte_arrays() { |
| 72 | let mut arr = [MaybeUninit::new(42u64); 64]; |
| 73 | arr.zeroize(); |
| 74 | let arr_init: [u64; 64] = unsafe { core::mem::transmute(arr) }; |
| 75 | assert_eq!(arr_init, [0u64; 64]); |
| 76 | } |
| 77 | |
| 78 | #[test] |
| 79 | fn zeroize_check_zerosize_types() { |
| 80 | // Since we assume these types have zero size, we test this holds for |
| 81 | // the current version of Rust. |
| 82 | assert_eq!(size_of::<()>(), 0); |
| 83 | assert_eq!(size_of::<PhantomPinned>(), 0); |
| 84 | assert_eq!(size_of::<PhantomData<usize>>(), 0); |
| 85 | } |
| 86 | |
| 87 | #[test] |
| 88 | fn zeroize_check_tuple() { |
| 89 | let mut tup1 = (42u8,); |
| 90 | tup1.zeroize(); |
| 91 | assert_eq!(tup1, (0u8,)); |
| 92 | |
| 93 | let mut tup2 = (42u8, 42u8); |
| 94 | tup2.zeroize(); |
| 95 | assert_eq!(tup2, (0u8, 0u8)); |
| 96 | } |
| 97 | |
| 98 | #[test] |
| 99 | fn zeroize_on_drop_check_tuple() { |
| 100 | let mut tup1 = (ZeroizedOnDrop(42),); |
| 101 | unsafe { ptr::drop_in_place(&raw mut tup1) }; |
| 102 | assert_eq!(tup1, (ZeroizedOnDrop(0),)); |
| 103 | |
| 104 | let mut tup2 = (ZeroizedOnDrop(42), ZeroizedOnDrop(42)); |
| 105 | unsafe { ptr::drop_in_place(&raw mut tup2) }; |
| 106 | assert_eq!(tup2, (ZeroizedOnDrop(0), ZeroizedOnDrop(0))); |
| 107 | } |
| 108 | |
| 109 | #[cfg(feature = "alloc")] |
| 110 | #[test] |
| 111 | fn zeroize_vec() { |
| 112 | let mut vec = vec![42; 3]; |
| 113 | vec.zeroize(); |
| 114 | assert!(vec.is_empty()); |
| 115 | } |
| 116 | |
| 117 | #[cfg(feature = "alloc")] |
| 118 | #[test] |
| 119 | fn zeroize_vec_entire_capacity() { |
| 120 | #[derive(Clone)] |
| 121 | struct PanicOnNonZeroDrop(u64); |
| 122 | |
| 123 | impl Zeroize for PanicOnNonZeroDrop { |
| 124 | fn zeroize(&mut self) { |
| 125 | self.0 = 0; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | impl Drop for PanicOnNonZeroDrop { |
| 130 | fn drop(&mut self) { |
| 131 | assert!(self.0 == 0, "dropped non-zeroized data"); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // Ensure that the entire capacity of the vec is zeroized and that no unitinialized data |
| 136 | // is ever interpreted as initialized |
| 137 | let mut vec = vec![PanicOnNonZeroDrop(42); 2]; |
| 138 | |
| 139 | unsafe { |
| 140 | vec.set_len(1); |
| 141 | } |
| 142 | |
| 143 | vec.zeroize(); |
| 144 | |
| 145 | unsafe { |
| 146 | vec.set_len(2); |
| 147 | } |
| 148 | |
| 149 | drop(vec); |
| 150 | } |
| 151 | |
| 152 | #[cfg(feature = "alloc")] |
| 153 | #[test] |
| 154 | fn zeroize_string() { |
| 155 | let mut string = String::from("Hello, world!"); |
| 156 | string.zeroize(); |
| 157 | assert!(string.is_empty()); |
| 158 | } |
| 159 | |
| 160 | #[cfg(feature = "alloc")] |
| 161 | #[test] |
| 162 | fn zeroize_string_entire_capacity() { |
| 163 | let mut string = String::from("Hello, world!"); |
| 164 | string.truncate(5); |
| 165 | |
| 166 | string.zeroize(); |
| 167 | |
| 168 | // convert the string to a vec to easily access the unused capacity |
| 169 | let mut as_vec = string.into_bytes(); |
| 170 | unsafe { as_vec.set_len(as_vec.capacity()) }; |
| 171 | |
| 172 | assert!(as_vec.iter().all(|byte| *byte == 0)); |
| 173 | } |
| 174 | |
| 175 | // TODO(tarcieri): debug flaky test (with potential UB?) See: RustCrypto/utils#774 |
| 176 | #[cfg(feature = "std")] |
| 177 | #[ignore] |
| 178 | #[test] |
| 179 | fn zeroize_c_string() { |
| 180 | let mut cstring = CString::new("Hello, world!").expect("CString::new failed"); |
| 181 | let orig_len = cstring.as_bytes().len(); |
| 182 | let orig_ptr = cstring.as_bytes().as_ptr(); |
| 183 | cstring.zeroize(); |
| 184 | // This doesn't quite test that the original memory has been cleared, but only that |
| 185 | // cstring now owns an empty buffer |
| 186 | assert!(cstring.as_bytes().is_empty()); |
| 187 | for i in 0..orig_len { |
| 188 | unsafe { |
| 189 | // Using a simple deref, only one iteration of the loop is performed |
| 190 | // presumably because after zeroize, the internal buffer has a length of one/ |
| 191 | // `read_volatile` seems to "fix" this |
| 192 | // Note that this is very likely UB |
| 193 | assert_eq!(orig_ptr.add(i).read_volatile(), 0); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | #[cfg(feature = "alloc")] |
| 199 | #[test] |
| 200 | fn zeroize_box() { |
| 201 | let mut boxed_arr = Box::new([42u8; 3]); |
| 202 | boxed_arr.zeroize(); |
| 203 | assert_eq!(boxed_arr.as_ref(), &[0u8; 3]); |
| 204 | } |
| 205 | |
| 206 | #[cfg(feature = "alloc")] |
| 207 | #[test] |
| 208 | fn asref() { |
| 209 | let mut buffer: Zeroizing<Vec<u8>> = Default::default(); |
| 210 | let _asmut: &mut [u8] = buffer.as_mut(); |
| 211 | let _asref: &[u8] = buffer.as_ref(); |
| 212 | |
| 213 | let mut buffer: Zeroizing<Box<[u8]>> = Default::default(); |
| 214 | let _asmut: &mut [u8] = buffer.as_mut(); |
| 215 | let _asref: &[u8] = buffer.as_ref(); |
| 216 | } |
| 217 | |
| 218 | #[cfg(feature = "alloc")] |
| 219 | #[test] |
| 220 | fn box_unsized_zeroizing() { |
| 221 | let mut b: Box<Zeroizing<[u8]>> = Box::new(Zeroizing::new([1, 2, 3, 4])); |
| 222 | { |
| 223 | let s: &[u8] = &b; |
| 224 | assert_eq!(s, &[1, 2, 3, 4]); |
| 225 | |
| 226 | let s: &[u8] = b.as_ref(); |
| 227 | assert_eq!(s, &[1, 2, 3, 4]); |
| 228 | |
| 229 | let s: &mut [u8] = b.as_mut(); |
| 230 | assert_eq!(s, &[1, 2, 3, 4]); |
| 231 | } |
| 232 | |
| 233 | unsafe { |
| 234 | ptr::drop_in_place(&raw mut *b); |
| 235 | } |
| 236 | |
| 237 | let s: &[u8] = &b; |
| 238 | assert_eq!(s, &[0, 0, 0, 0]); |
| 239 | } |
| 240 | |
| 241 | #[cfg(feature = "alloc")] |
| 242 | #[test] |
| 243 | fn arc_unsized_zeroizing() { |
| 244 | let mut arc: Arc<Zeroizing<[u8]>> = Arc::new(Zeroizing::new([1, 2, 3, 4])); |
| 245 | { |
| 246 | let s: &[u8] = &arc; |
| 247 | assert_eq!(s, &[1, 2, 3, 4]); |
| 248 | |
| 249 | let s: &[u8] = arc.as_ref(); |
| 250 | assert_eq!(s, &[1, 2, 3, 4]); |
| 251 | } |
| 252 | |
| 253 | unsafe { |
| 254 | let inner = Arc::get_mut(&mut arc).unwrap(); |
| 255 | ptr::drop_in_place(inner); |
| 256 | } |
| 257 | |
| 258 | let s: &[u8] = &arc; |
| 259 | assert_eq!(s, &[0, 0, 0, 0]); |
| 260 | } |
| 261 | |
| 262 | // This is a weird way to use zeroizing, but it's technically allowed b/c |
| 263 | // unsized types can be stored inside Zeroizing, so make sure it works as |
| 264 | // expected. |
| 265 | #[test] |
| 266 | fn zeroizing_dyn_trait() { |
| 267 | trait TestTrait: Zeroize { |
| 268 | fn data(&self) -> &[u8]; |
| 269 | } |
| 270 | |
| 271 | struct TestStruct { |
| 272 | data: [u8; 4], |
| 273 | } |
| 274 | |
| 275 | impl Zeroize for TestStruct { |
| 276 | fn zeroize(&mut self) { |
| 277 | self.data.zeroize(); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | impl Drop for TestStruct { |
| 282 | fn drop(&mut self) { |
| 283 | self.zeroize(); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | impl TestTrait for TestStruct { |
| 288 | fn data(&self) -> &[u8] { |
| 289 | &self.data |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | let mut b: Box<Zeroizing<dyn TestTrait>> = |
| 294 | Box::new(Zeroizing::new(TestStruct { data: [1, 2, 3, 4] })); |
| 295 | |
| 296 | unsafe { |
| 297 | ptr::drop_in_place(&raw mut *b); |
| 298 | } |
| 299 | |
| 300 | let inner: &Zeroizing<dyn TestTrait> = &b; |
| 301 | let inner: &dyn TestTrait = core::ops::Deref::deref(inner); |
| 302 | |
| 303 | assert_eq!(inner.data(), &[0, 0, 0, 0]); |
| 304 | } |