Janis Danisevskis | 9d90b81 | 2020-11-25 21:02:11 -0800 | [diff] [blame] | 1 | // Copyright 2020, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #![allow(dead_code)] |
| 16 | |
| 17 | use crate::error::Error; |
| 18 | use nix::sys::mman::{mlock, munlock}; |
| 19 | use std::convert::TryFrom; |
| 20 | use std::fmt; |
| 21 | use std::ops::{Deref, DerefMut}; |
| 22 | use std::ptr::write_volatile; |
| 23 | |
Joel Galenson | 0591458 | 2021-01-08 09:30:41 -0800 | [diff] [blame^] | 24 | /// A semi fixed size u8 vector that is zeroed when dropped. It can shrink in |
| 25 | /// size but cannot grow larger than the original size (and if it shrinks it |
| 26 | /// still owns the entire buffer). Also the data is pinned in memory with |
| 27 | /// mlock. |
Janis Danisevskis | 9d90b81 | 2020-11-25 21:02:11 -0800 | [diff] [blame] | 28 | #[derive(Default, Eq, PartialEq)] |
Joel Galenson | 0591458 | 2021-01-08 09:30:41 -0800 | [diff] [blame^] | 29 | pub struct ZVec { |
| 30 | elems: Box<[u8]>, |
| 31 | len: usize, |
| 32 | } |
Janis Danisevskis | 9d90b81 | 2020-11-25 21:02:11 -0800 | [diff] [blame] | 33 | |
| 34 | impl ZVec { |
| 35 | /// Create a ZVec with the given size. |
| 36 | pub fn new(size: usize) -> Result<Self, Error> { |
| 37 | let v: Vec<u8> = vec![0; size]; |
| 38 | let b = v.into_boxed_slice(); |
| 39 | if size > 0 { |
| 40 | unsafe { mlock(b.as_ptr() as *const std::ffi::c_void, b.len()) }?; |
| 41 | } |
Joel Galenson | 0591458 | 2021-01-08 09:30:41 -0800 | [diff] [blame^] | 42 | Ok(Self { elems: b, len: size }) |
| 43 | } |
| 44 | |
| 45 | /// Reduce the length to the given value. Does nothing if that length is |
| 46 | /// greater than the length of the vector. Note that it still owns the |
| 47 | /// original allocation even if the length is reduced. |
| 48 | pub fn reduce_len(&mut self, len: usize) { |
| 49 | if len <= self.elems.len() { |
| 50 | self.len = len; |
| 51 | } |
Janis Danisevskis | 9d90b81 | 2020-11-25 21:02:11 -0800 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
| 55 | impl Drop for ZVec { |
| 56 | fn drop(&mut self) { |
Joel Galenson | 0591458 | 2021-01-08 09:30:41 -0800 | [diff] [blame^] | 57 | for i in 0..self.elems.len() { |
| 58 | unsafe { write_volatile(self.elems.as_mut_ptr().add(i), 0) }; |
Janis Danisevskis | 9d90b81 | 2020-11-25 21:02:11 -0800 | [diff] [blame] | 59 | } |
Joel Galenson | 0591458 | 2021-01-08 09:30:41 -0800 | [diff] [blame^] | 60 | if !self.elems.is_empty() { |
Janis Danisevskis | 9d90b81 | 2020-11-25 21:02:11 -0800 | [diff] [blame] | 61 | if let Err(e) = |
Joel Galenson | 0591458 | 2021-01-08 09:30:41 -0800 | [diff] [blame^] | 62 | unsafe { munlock(self.elems.as_ptr() as *const std::ffi::c_void, self.elems.len()) } |
Janis Danisevskis | 9d90b81 | 2020-11-25 21:02:11 -0800 | [diff] [blame] | 63 | { |
| 64 | log::error!("In ZVec::drop: `munlock` failed: {:?}.", e); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | impl Deref for ZVec { |
| 71 | type Target = [u8]; |
| 72 | |
| 73 | fn deref(&self) -> &Self::Target { |
Joel Galenson | 0591458 | 2021-01-08 09:30:41 -0800 | [diff] [blame^] | 74 | &self.elems[0..self.len] |
Janis Danisevskis | 9d90b81 | 2020-11-25 21:02:11 -0800 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | impl DerefMut for ZVec { |
| 79 | fn deref_mut(&mut self) -> &mut Self::Target { |
Joel Galenson | 0591458 | 2021-01-08 09:30:41 -0800 | [diff] [blame^] | 80 | &mut self.elems[0..self.len] |
Janis Danisevskis | 9d90b81 | 2020-11-25 21:02:11 -0800 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | impl fmt::Debug for ZVec { |
| 85 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Joel Galenson | 0591458 | 2021-01-08 09:30:41 -0800 | [diff] [blame^] | 86 | if self.elems.is_empty() { |
Janis Danisevskis | 9d90b81 | 2020-11-25 21:02:11 -0800 | [diff] [blame] | 87 | write!(f, "Zvec empty") |
| 88 | } else { |
Joel Galenson | 0591458 | 2021-01-08 09:30:41 -0800 | [diff] [blame^] | 89 | write!(f, "Zvec size: {} [ Sensitive information redacted ]", self.len) |
Janis Danisevskis | 9d90b81 | 2020-11-25 21:02:11 -0800 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | impl TryFrom<&[u8]> for ZVec { |
| 95 | type Error = Error; |
| 96 | |
| 97 | fn try_from(v: &[u8]) -> Result<Self, Self::Error> { |
| 98 | let mut z = ZVec::new(v.len())?; |
| 99 | if !v.is_empty() { |
| 100 | z.clone_from_slice(v); |
| 101 | } |
| 102 | Ok(z) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | impl TryFrom<Vec<u8>> for ZVec { |
| 107 | type Error = Error; |
| 108 | |
| 109 | fn try_from(v: Vec<u8>) -> Result<Self, Self::Error> { |
| 110 | let b = v.into_boxed_slice(); |
| 111 | if !b.is_empty() { |
| 112 | unsafe { mlock(b.as_ptr() as *const std::ffi::c_void, b.len()) }?; |
| 113 | } |
Joel Galenson | 0591458 | 2021-01-08 09:30:41 -0800 | [diff] [blame^] | 114 | let len = b.len(); |
| 115 | Ok(Self { elems: b, len }) |
Janis Danisevskis | 9d90b81 | 2020-11-25 21:02:11 -0800 | [diff] [blame] | 116 | } |
| 117 | } |