Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 1 | // Copyright 2023, 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 | //! Support for reading and writing to the instance.img. |
| 16 | |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 17 | use crate::crypto; |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 18 | use crate::crypto::AeadCtx; |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 19 | use crate::dice::PartialInputs; |
| 20 | use crate::gpt; |
| 21 | use crate::gpt::Partition; |
| 22 | use crate::gpt::Partitions; |
Alice Wang | 947f3f7 | 2023-09-29 09:04:07 +0000 | [diff] [blame] | 23 | use bssl_avf::{self, hkdf, Digester}; |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 24 | use core::fmt; |
| 25 | use core::mem::size_of; |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 26 | use diced_open_dice::DiceMode; |
| 27 | use diced_open_dice::Hash; |
| 28 | use diced_open_dice::Hidden; |
| 29 | use log::trace; |
| 30 | use uuid::Uuid; |
Alice Wang | 0e08623 | 2023-06-12 13:47:40 +0000 | [diff] [blame] | 31 | use virtio_drivers::transport::{pci::bus::PciRoot, DeviceType, Transport}; |
Pierre-Clément Tosi | 3e3d733 | 2023-06-22 10:44:29 +0000 | [diff] [blame] | 32 | use vmbase::rand; |
Alice Wang | eacb738 | 2023-06-05 12:53:54 +0000 | [diff] [blame] | 33 | use vmbase::util::ceiling_div; |
Alice Wang | 0e08623 | 2023-06-12 13:47:40 +0000 | [diff] [blame] | 34 | use vmbase::virtio::pci::{PciTransportIterator, VirtIOBlk}; |
Alice Wang | 7c55c7d | 2023-07-05 14:51:40 +0000 | [diff] [blame] | 35 | use vmbase::virtio::HalImpl; |
Pierre-Clément Tosi | 8ad980f | 2023-04-25 18:23:11 +0100 | [diff] [blame] | 36 | use zerocopy::AsBytes; |
| 37 | use zerocopy::FromBytes; |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 38 | |
| 39 | pub enum Error { |
| 40 | /// Unexpected I/O error while accessing the underlying disk. |
| 41 | FailedIo(gpt::Error), |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 42 | /// Failed to decrypt the entry. |
| 43 | FailedOpen(crypto::ErrorIterator), |
Pierre-Clément Tosi | a59103d | 2023-02-02 14:46:55 +0000 | [diff] [blame] | 44 | /// Failed to generate a random salt to be stored. |
| 45 | FailedSaltGeneration(rand::Error), |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 46 | /// Failed to encrypt the entry. |
| 47 | FailedSeal(crypto::ErrorIterator), |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 48 | /// Impossible to create a new instance.img entry. |
| 49 | InstanceImageFull, |
| 50 | /// Badly formatted instance.img header block. |
| 51 | InvalidInstanceImageHeader, |
| 52 | /// No instance.img ("vm-instance") partition found. |
| 53 | MissingInstanceImage, |
| 54 | /// The instance.img doesn't contain a header. |
| 55 | MissingInstanceImageHeader, |
| 56 | /// Authority hash found in the pvmfw instance.img entry doesn't match the trusted public key. |
| 57 | RecordedAuthHashMismatch, |
| 58 | /// Code hash found in the pvmfw instance.img entry doesn't match the inputs. |
| 59 | RecordedCodeHashMismatch, |
| 60 | /// DICE mode found in the pvmfw instance.img entry doesn't match the current one. |
| 61 | RecordedDiceModeMismatch, |
| 62 | /// Size of the instance.img entry being read or written is not supported. |
| 63 | UnsupportedEntrySize(usize), |
Alice Wang | 0e08623 | 2023-06-12 13:47:40 +0000 | [diff] [blame] | 64 | /// Failed to create VirtIO Block device. |
| 65 | VirtIOBlkCreationFailed(virtio_drivers::Error), |
Alice Wang | 947f3f7 | 2023-09-29 09:04:07 +0000 | [diff] [blame] | 66 | /// An error happened during the interaction with BoringSSL. |
| 67 | BoringSslFailed(bssl_avf::Error), |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | impl fmt::Display for Error { |
| 71 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 72 | match self { |
| 73 | Self::FailedIo(e) => write!(f, "Failed I/O to disk: {e}"), |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 74 | Self::FailedOpen(e_iter) => { |
| 75 | writeln!(f, "Failed to open the instance.img partition:")?; |
| 76 | for e in *e_iter { |
| 77 | writeln!(f, "\t{e}")?; |
| 78 | } |
| 79 | Ok(()) |
| 80 | } |
Pierre-Clément Tosi | a59103d | 2023-02-02 14:46:55 +0000 | [diff] [blame] | 81 | Self::FailedSaltGeneration(e) => write!(f, "Failed to generate salt: {e}"), |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 82 | Self::FailedSeal(e_iter) => { |
| 83 | writeln!(f, "Failed to seal the instance.img partition:")?; |
| 84 | for e in *e_iter { |
| 85 | writeln!(f, "\t{e}")?; |
| 86 | } |
| 87 | Ok(()) |
| 88 | } |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 89 | Self::InstanceImageFull => write!(f, "Failed to obtain a free instance.img partition"), |
| 90 | Self::InvalidInstanceImageHeader => write!(f, "instance.img header is invalid"), |
| 91 | Self::MissingInstanceImage => write!(f, "Failed to find the instance.img partition"), |
| 92 | Self::MissingInstanceImageHeader => write!(f, "instance.img header is missing"), |
| 93 | Self::RecordedAuthHashMismatch => write!(f, "Recorded authority hash doesn't match"), |
| 94 | Self::RecordedCodeHashMismatch => write!(f, "Recorded code hash doesn't match"), |
| 95 | Self::RecordedDiceModeMismatch => write!(f, "Recorded DICE mode doesn't match"), |
| 96 | Self::UnsupportedEntrySize(sz) => write!(f, "Invalid entry size: {sz}"), |
Alice Wang | 0e08623 | 2023-06-12 13:47:40 +0000 | [diff] [blame] | 97 | Self::VirtIOBlkCreationFailed(e) => { |
| 98 | write!(f, "Failed to create VirtIO Block device: {e}") |
| 99 | } |
Alice Wang | 947f3f7 | 2023-09-29 09:04:07 +0000 | [diff] [blame] | 100 | Self::BoringSslFailed(e) => { |
| 101 | write!(f, "An error happened during the interaction with BoringSSL: {e}") |
| 102 | } |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
Alice Wang | 947f3f7 | 2023-09-29 09:04:07 +0000 | [diff] [blame] | 107 | impl From<bssl_avf::Error> for Error { |
| 108 | fn from(e: bssl_avf::Error) -> Self { |
| 109 | Self::BoringSslFailed(e) |
| 110 | } |
| 111 | } |
| 112 | |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 113 | pub type Result<T> = core::result::Result<T, Error>; |
| 114 | |
| 115 | pub fn get_or_generate_instance_salt( |
| 116 | pci_root: &mut PciRoot, |
| 117 | dice_inputs: &PartialInputs, |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 118 | secret: &[u8], |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 119 | ) -> Result<(bool, Hidden)> { |
| 120 | let mut instance_img = find_instance_img(pci_root)?; |
| 121 | |
| 122 | let entry = locate_entry(&mut instance_img)?; |
| 123 | trace!("Found pvmfw instance.img entry: {entry:?}"); |
| 124 | |
Alice Wang | 947f3f7 | 2023-09-29 09:04:07 +0000 | [diff] [blame] | 125 | let key = hkdf::<32>(secret, /* salt= */ &[], b"vm-instance", Digester::sha512())?; |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 126 | let mut blk = [0; BLK_SIZE]; |
| 127 | match entry { |
| 128 | PvmfwEntry::Existing { header_index, payload_size } => { |
| 129 | if payload_size > blk.len() { |
| 130 | // We currently only support single-blk entries. |
| 131 | return Err(Error::UnsupportedEntrySize(payload_size)); |
| 132 | } |
| 133 | let payload_index = header_index + 1; |
| 134 | instance_img.read_block(payload_index, &mut blk).map_err(Error::FailedIo)?; |
| 135 | |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 136 | let payload = &blk[..payload_size]; |
| 137 | let mut entry = [0; size_of::<EntryBody>()]; |
Alice Wang | ccc52e5 | 2023-10-03 13:14:09 +0000 | [diff] [blame^] | 138 | let aead = |
| 139 | AeadCtx::new_aes_256_gcm_randnonce(key.as_slice()).map_err(Error::FailedOpen)?; |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 140 | let decrypted = aead.open(&mut entry, payload).map_err(Error::FailedOpen)?; |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 141 | |
Pierre-Clément Tosi | 8ad980f | 2023-04-25 18:23:11 +0100 | [diff] [blame] | 142 | let body = EntryBody::read_from(decrypted).unwrap(); |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 143 | if body.code_hash != dice_inputs.code_hash { |
| 144 | Err(Error::RecordedCodeHashMismatch) |
| 145 | } else if body.auth_hash != dice_inputs.auth_hash { |
| 146 | Err(Error::RecordedAuthHashMismatch) |
| 147 | } else if body.mode() != dice_inputs.mode { |
| 148 | Err(Error::RecordedDiceModeMismatch) |
| 149 | } else { |
| 150 | Ok((false, body.salt)) |
| 151 | } |
| 152 | } |
| 153 | PvmfwEntry::New { header_index } => { |
Pierre-Clément Tosi | a59103d | 2023-02-02 14:46:55 +0000 | [diff] [blame] | 154 | let salt = rand::random_array().map_err(Error::FailedSaltGeneration)?; |
Pierre-Clément Tosi | 8ad980f | 2023-04-25 18:23:11 +0100 | [diff] [blame] | 155 | let body = EntryBody::new(dice_inputs, &salt); |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 156 | |
Alice Wang | ccc52e5 | 2023-10-03 13:14:09 +0000 | [diff] [blame^] | 157 | let aead = |
| 158 | AeadCtx::new_aes_256_gcm_randnonce(key.as_slice()).map_err(Error::FailedSeal)?; |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 159 | // We currently only support single-blk entries. |
Pierre-Clément Tosi | 8ad980f | 2023-04-25 18:23:11 +0100 | [diff] [blame] | 160 | let plaintext = body.as_bytes(); |
| 161 | assert!(plaintext.len() + aead.aead().unwrap().max_overhead() < blk.len()); |
| 162 | let encrypted = aead.seal(&mut blk, plaintext).map_err(Error::FailedSeal)?; |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 163 | let payload_size = encrypted.len(); |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 164 | let payload_index = header_index + 1; |
| 165 | instance_img.write_block(payload_index, &blk).map_err(Error::FailedIo)?; |
| 166 | |
| 167 | let header = EntryHeader::new(PvmfwEntry::UUID, payload_size); |
Pierre-Clément Tosi | 8ad980f | 2023-04-25 18:23:11 +0100 | [diff] [blame] | 168 | header.write_to_prefix(blk.as_mut_slice()).unwrap(); |
| 169 | blk[header.as_bytes().len()..].fill(0); |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 170 | instance_img.write_block(header_index, &blk).map_err(Error::FailedIo)?; |
| 171 | |
| 172 | Ok((true, salt)) |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
Pierre-Clément Tosi | 8ad980f | 2023-04-25 18:23:11 +0100 | [diff] [blame] | 177 | #[derive(FromBytes)] |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 178 | #[repr(C, packed)] |
| 179 | struct Header { |
| 180 | magic: [u8; Header::MAGIC.len()], |
| 181 | version: u16, |
| 182 | } |
| 183 | |
| 184 | impl Header { |
| 185 | const MAGIC: &[u8] = b"Android-VM-instance"; |
| 186 | const VERSION_1: u16 = 1; |
| 187 | |
| 188 | pub fn is_valid(&self) -> bool { |
| 189 | self.magic == Self::MAGIC && self.version() == Self::VERSION_1 |
| 190 | } |
| 191 | |
| 192 | fn version(&self) -> u16 { |
| 193 | u16::from_le(self.version) |
| 194 | } |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | fn find_instance_img(pci_root: &mut PciRoot) -> Result<Partition> { |
Alice Wang | 7c55c7d | 2023-07-05 14:51:40 +0000 | [diff] [blame] | 198 | for transport in PciTransportIterator::<HalImpl>::new(pci_root) |
| 199 | .filter(|t| DeviceType::Block == t.device_type()) |
Alice Wang | 0e08623 | 2023-06-12 13:47:40 +0000 | [diff] [blame] | 200 | { |
Alice Wang | 7c55c7d | 2023-07-05 14:51:40 +0000 | [diff] [blame] | 201 | let device = |
| 202 | VirtIOBlk::<HalImpl>::new(transport).map_err(Error::VirtIOBlkCreationFailed)?; |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 203 | match Partition::get_by_name(device, "vm-instance") { |
| 204 | Ok(Some(p)) => return Ok(p), |
| 205 | Ok(None) => {} |
| 206 | Err(e) => log::warn!("error while reading from disk: {e}"), |
| 207 | }; |
| 208 | } |
| 209 | |
| 210 | Err(Error::MissingInstanceImage) |
| 211 | } |
| 212 | |
| 213 | #[derive(Debug)] |
| 214 | enum PvmfwEntry { |
| 215 | Existing { header_index: usize, payload_size: usize }, |
| 216 | New { header_index: usize }, |
| 217 | } |
| 218 | |
| 219 | const BLK_SIZE: usize = Partitions::LBA_SIZE; |
| 220 | |
| 221 | impl PvmfwEntry { |
| 222 | const UUID: Uuid = Uuid::from_u128(0x90d2174a038a4bc6adf3824848fc5825); |
| 223 | } |
| 224 | |
| 225 | fn locate_entry(partition: &mut Partition) -> Result<PvmfwEntry> { |
| 226 | let mut blk = [0; BLK_SIZE]; |
| 227 | let mut indices = partition.indices(); |
| 228 | let header_index = indices.next().ok_or(Error::MissingInstanceImageHeader)?; |
| 229 | partition.read_block(header_index, &mut blk).map_err(Error::FailedIo)?; |
| 230 | // The instance.img header is only used for discovery/validation. |
Pierre-Clément Tosi | 8ad980f | 2023-04-25 18:23:11 +0100 | [diff] [blame] | 231 | let header = Header::read_from_prefix(blk.as_slice()).unwrap(); |
| 232 | if !header.is_valid() { |
| 233 | return Err(Error::InvalidInstanceImageHeader); |
| 234 | } |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 235 | |
| 236 | while let Some(header_index) = indices.next() { |
| 237 | partition.read_block(header_index, &mut blk).map_err(Error::FailedIo)?; |
| 238 | |
Pierre-Clément Tosi | 8ad980f | 2023-04-25 18:23:11 +0100 | [diff] [blame] | 239 | let header = EntryHeader::read_from_prefix(blk.as_slice()).unwrap(); |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 240 | match (header.uuid(), header.payload_size()) { |
| 241 | (uuid, _) if uuid.is_nil() => return Ok(PvmfwEntry::New { header_index }), |
| 242 | (PvmfwEntry::UUID, payload_size) => { |
| 243 | return Ok(PvmfwEntry::Existing { header_index, payload_size }) |
| 244 | } |
| 245 | (uuid, payload_size) => { |
| 246 | trace!("Skipping instance.img entry {uuid}: {payload_size:?} bytes"); |
| 247 | let n = ceiling_div(payload_size, BLK_SIZE).unwrap(); |
| 248 | if n > 0 { |
| 249 | let _ = indices.nth(n - 1); // consume |
| 250 | } |
| 251 | } |
| 252 | }; |
| 253 | } |
| 254 | |
| 255 | Err(Error::InstanceImageFull) |
| 256 | } |
| 257 | |
| 258 | /// Marks the start of an instance.img entry. |
| 259 | /// |
| 260 | /// Note: Virtualization/microdroid_manager/src/instance.rs uses the name "partition". |
Pierre-Clément Tosi | 8ad980f | 2023-04-25 18:23:11 +0100 | [diff] [blame] | 261 | #[derive(AsBytes, FromBytes)] |
| 262 | #[repr(C, packed)] |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 263 | struct EntryHeader { |
| 264 | uuid: u128, |
| 265 | payload_size: u64, |
| 266 | } |
| 267 | |
| 268 | impl EntryHeader { |
| 269 | fn new(uuid: Uuid, payload_size: usize) -> Self { |
Pierre-Clément Tosi | afb126a | 2023-03-29 14:42:19 +0100 | [diff] [blame] | 270 | Self { uuid: uuid.to_u128_le(), payload_size: u64::try_from(payload_size).unwrap().to_le() } |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | fn uuid(&self) -> Uuid { |
Pierre-Clément Tosi | afb126a | 2023-03-29 14:42:19 +0100 | [diff] [blame] | 274 | Uuid::from_u128_le(self.uuid) |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | fn payload_size(&self) -> usize { |
| 278 | usize::try_from(u64::from_le(self.payload_size)).unwrap() |
| 279 | } |
| 280 | } |
| 281 | |
Pierre-Clément Tosi | 8ad980f | 2023-04-25 18:23:11 +0100 | [diff] [blame] | 282 | #[derive(AsBytes, FromBytes)] |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 283 | #[repr(C)] |
| 284 | struct EntryBody { |
| 285 | code_hash: Hash, |
| 286 | auth_hash: Hash, |
| 287 | salt: Hidden, |
| 288 | mode: u8, |
| 289 | } |
| 290 | |
| 291 | impl EntryBody { |
| 292 | fn new(dice_inputs: &PartialInputs, salt: &Hidden) -> Self { |
| 293 | let mode = match dice_inputs.mode { |
| 294 | DiceMode::kDiceModeNotInitialized => 0, |
| 295 | DiceMode::kDiceModeNormal => 1, |
| 296 | DiceMode::kDiceModeDebug => 2, |
| 297 | DiceMode::kDiceModeMaintenance => 3, |
| 298 | }; |
| 299 | |
| 300 | Self { |
| 301 | code_hash: dice_inputs.code_hash, |
| 302 | auth_hash: dice_inputs.auth_hash, |
| 303 | salt: *salt, |
| 304 | mode, |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | fn mode(&self) -> DiceMode { |
| 309 | match self.mode { |
| 310 | 1 => DiceMode::kDiceModeNormal, |
| 311 | 2 => DiceMode::kDiceModeDebug, |
| 312 | 3 => DiceMode::kDiceModeMaintenance, |
| 313 | _ => DiceMode::kDiceModeNotInitialized, |
| 314 | } |
| 315 | } |
| 316 | } |