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