Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 1 | // Copyright 2022, 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 | |
Alice Wang | f3d96b1 | 2022-12-15 13:10:47 +0000 | [diff] [blame] | 15 | //! This module handles the pvmfw payload verification. |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 16 | |
Alice Wang | 7998551 | 2023-05-22 08:15:36 +0000 | [diff] [blame] | 17 | use crate::descriptor::{Descriptors, Digest}; |
Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 18 | use crate::ops::{Ops, Payload}; |
Alice Wang | 8077a86 | 2023-01-18 16:06:37 +0000 | [diff] [blame] | 19 | use crate::partition::PartitionName; |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame] | 20 | use crate::PvmfwVerifyError; |
Alice Wang | ab0d020 | 2023-05-17 08:07:41 +0000 | [diff] [blame] | 21 | use alloc::vec; |
| 22 | use alloc::vec::Vec; |
David Pursell | b59bcc4 | 2023-11-10 16:59:19 -0800 | [diff] [blame] | 23 | use avb::{PartitionData, SlotVerifyError, SlotVerifyNoDataResult, VbmetaData}; |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 24 | |
David Pursell | b59bcc4 | 2023-11-10 16:59:19 -0800 | [diff] [blame] | 25 | // We use this for the rollback_index field if SlotVerifyData has empty rollback_indexes |
Shikha Panwar | a26f16a | 2023-09-27 09:39:00 +0000 | [diff] [blame] | 26 | const DEFAULT_ROLLBACK_INDEX: u64 = 0; |
| 27 | |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 28 | /// Verified data returned when the payload verification succeeds. |
Pierre-Clément Tosi | 81ca080 | 2023-02-14 10:41:38 +0000 | [diff] [blame] | 29 | #[derive(Debug, PartialEq, Eq)] |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame] | 30 | pub struct VerifiedBootData<'a> { |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 31 | /// DebugLevel of the VM. |
| 32 | pub debug_level: DebugLevel, |
| 33 | /// Kernel digest. |
| 34 | pub kernel_digest: Digest, |
| 35 | /// Initrd digest if initrd exists. |
| 36 | pub initrd_digest: Option<Digest>, |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame] | 37 | /// Trusted public key. |
| 38 | pub public_key: &'a [u8], |
Alice Wang | ab0d020 | 2023-05-17 08:07:41 +0000 | [diff] [blame] | 39 | /// VM capabilities. |
| 40 | pub capabilities: Vec<Capability>, |
Shikha Panwar | a26f16a | 2023-09-27 09:39:00 +0000 | [diff] [blame] | 41 | /// Rollback index of kernel. |
| 42 | pub rollback_index: u64, |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Shikha Panwar | 4a0651d | 2023-09-28 13:06:13 +0000 | [diff] [blame] | 45 | impl VerifiedBootData<'_> { |
| 46 | /// Returns whether the kernel have the given capability |
| 47 | pub fn has_capability(&self, cap: Capability) -> bool { |
| 48 | self.capabilities.contains(&cap) |
| 49 | } |
| 50 | } |
| 51 | |
Alice Wang | 5c1a756 | 2023-01-13 17:19:57 +0000 | [diff] [blame] | 52 | /// This enum corresponds to the `DebugLevel` in `VirtualMachineConfig`. |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 53 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
Alice Wang | 5c1a756 | 2023-01-13 17:19:57 +0000 | [diff] [blame] | 54 | pub enum DebugLevel { |
| 55 | /// Not debuggable at all. |
| 56 | None, |
| 57 | /// Fully debuggable. |
| 58 | Full, |
| 59 | } |
| 60 | |
Alice Wang | ab0d020 | 2023-05-17 08:07:41 +0000 | [diff] [blame] | 61 | /// VM Capability. |
Shikha Panwar | 4a0651d | 2023-09-28 13:06:13 +0000 | [diff] [blame] | 62 | #[derive(Copy, Clone, Debug, PartialEq, Eq)] |
Alice Wang | ab0d020 | 2023-05-17 08:07:41 +0000 | [diff] [blame] | 63 | pub enum Capability { |
| 64 | /// Remote attestation. |
| 65 | RemoteAttest, |
Shikha Panwar | 4a0651d | 2023-09-28 13:06:13 +0000 | [diff] [blame] | 66 | /// Secretkeeper protected secrets. |
| 67 | SecretkeeperProtection, |
Alice Wang | ab0d020 | 2023-05-17 08:07:41 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | impl Capability { |
Chris Wailes | 98f1823 | 2023-12-07 12:04:21 -0800 | [diff] [blame^] | 71 | const KEY: &'static [u8] = b"com.android.virt.cap"; |
| 72 | const REMOTE_ATTEST: &'static [u8] = b"remote_attest"; |
| 73 | const SECRETKEEPER_PROTECTION: &'static [u8] = b"secretkeeper_protection"; |
Alice Wang | ab0d020 | 2023-05-17 08:07:41 +0000 | [diff] [blame] | 74 | const SEPARATOR: u8 = b'|'; |
| 75 | |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame] | 76 | fn get_capabilities(property_value: &[u8]) -> Result<Vec<Self>, PvmfwVerifyError> { |
Alice Wang | ab0d020 | 2023-05-17 08:07:41 +0000 | [diff] [blame] | 77 | let mut res = Vec::new(); |
| 78 | |
| 79 | for v in property_value.split(|b| *b == Self::SEPARATOR) { |
| 80 | let cap = match v { |
| 81 | Self::REMOTE_ATTEST => Self::RemoteAttest, |
Shikha Panwar | 4a0651d | 2023-09-28 13:06:13 +0000 | [diff] [blame] | 82 | Self::SECRETKEEPER_PROTECTION => Self::SecretkeeperProtection, |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame] | 83 | _ => return Err(PvmfwVerifyError::UnknownVbmetaProperty), |
Alice Wang | ab0d020 | 2023-05-17 08:07:41 +0000 | [diff] [blame] | 84 | }; |
| 85 | if res.contains(&cap) { |
David Pursell | b59bcc4 | 2023-11-10 16:59:19 -0800 | [diff] [blame] | 86 | return Err(SlotVerifyError::InvalidMetadata.into()); |
Alice Wang | ab0d020 | 2023-05-17 08:07:41 +0000 | [diff] [blame] | 87 | } |
| 88 | res.push(cap); |
| 89 | } |
| 90 | Ok(res) |
| 91 | } |
| 92 | } |
| 93 | |
David Pursell | b59bcc4 | 2023-11-10 16:59:19 -0800 | [diff] [blame] | 94 | fn verify_only_one_vbmeta_exists(vbmeta_data: &[VbmetaData]) -> SlotVerifyNoDataResult<()> { |
| 95 | if vbmeta_data.len() == 1 { |
Alice Wang | d3f28ae | 2023-01-25 10:41:40 +0000 | [diff] [blame] | 96 | Ok(()) |
| 97 | } else { |
David Pursell | b59bcc4 | 2023-11-10 16:59:19 -0800 | [diff] [blame] | 98 | Err(SlotVerifyError::InvalidMetadata) |
Alice Wang | d3f28ae | 2023-01-25 10:41:40 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
David Pursell | b59bcc4 | 2023-11-10 16:59:19 -0800 | [diff] [blame] | 102 | fn verify_vbmeta_is_from_kernel_partition(vbmeta_image: &VbmetaData) -> SlotVerifyNoDataResult<()> { |
| 103 | match vbmeta_image.partition_name().try_into() { |
Alice Wang | 9dfb296 | 2023-01-18 10:01:34 +0000 | [diff] [blame] | 104 | Ok(PartitionName::Kernel) => Ok(()), |
David Pursell | b59bcc4 | 2023-11-10 16:59:19 -0800 | [diff] [blame] | 105 | _ => Err(SlotVerifyError::InvalidMetadata), |
Alice Wang | 9dfb296 | 2023-01-18 10:01:34 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
Alice Wang | f275286 | 2023-01-18 11:51:25 +0000 | [diff] [blame] | 109 | fn verify_vbmeta_has_only_one_hash_descriptor( |
Alice Wang | 7998551 | 2023-05-22 08:15:36 +0000 | [diff] [blame] | 110 | descriptors: &Descriptors, |
David Pursell | b59bcc4 | 2023-11-10 16:59:19 -0800 | [diff] [blame] | 111 | ) -> SlotVerifyNoDataResult<()> { |
Alice Wang | 7998551 | 2023-05-22 08:15:36 +0000 | [diff] [blame] | 112 | if descriptors.num_hash_descriptor() == 1 { |
Alice Wang | f275286 | 2023-01-18 11:51:25 +0000 | [diff] [blame] | 113 | Ok(()) |
| 114 | } else { |
David Pursell | b59bcc4 | 2023-11-10 16:59:19 -0800 | [diff] [blame] | 115 | Err(SlotVerifyError::InvalidMetadata) |
Alice Wang | f275286 | 2023-01-18 11:51:25 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
Alice Wang | 75d0563 | 2023-01-25 13:31:18 +0000 | [diff] [blame] | 119 | fn verify_loaded_partition_has_expected_length( |
David Pursell | b59bcc4 | 2023-11-10 16:59:19 -0800 | [diff] [blame] | 120 | loaded_partitions: &[PartitionData], |
Alice Wang | 75d0563 | 2023-01-25 13:31:18 +0000 | [diff] [blame] | 121 | partition_name: PartitionName, |
| 122 | expected_len: usize, |
David Pursell | b59bcc4 | 2023-11-10 16:59:19 -0800 | [diff] [blame] | 123 | ) -> SlotVerifyNoDataResult<()> { |
Alice Wang | 75d0563 | 2023-01-25 13:31:18 +0000 | [diff] [blame] | 124 | if loaded_partitions.len() != 1 { |
| 125 | // Only one partition should be loaded in each verify result. |
David Pursell | b59bcc4 | 2023-11-10 16:59:19 -0800 | [diff] [blame] | 126 | return Err(SlotVerifyError::Io); |
Alice Wang | 75d0563 | 2023-01-25 13:31:18 +0000 | [diff] [blame] | 127 | } |
David Pursell | b59bcc4 | 2023-11-10 16:59:19 -0800 | [diff] [blame] | 128 | let loaded_partition = &loaded_partitions[0]; |
| 129 | if !PartitionName::try_from(loaded_partition.partition_name()) |
Alice Wang | 75d0563 | 2023-01-25 13:31:18 +0000 | [diff] [blame] | 130 | .map_or(false, |p| p == partition_name) |
| 131 | { |
| 132 | // Only the requested partition should be loaded. |
David Pursell | b59bcc4 | 2023-11-10 16:59:19 -0800 | [diff] [blame] | 133 | return Err(SlotVerifyError::Io); |
Alice Wang | 75d0563 | 2023-01-25 13:31:18 +0000 | [diff] [blame] | 134 | } |
David Pursell | b59bcc4 | 2023-11-10 16:59:19 -0800 | [diff] [blame] | 135 | if loaded_partition.data().len() == expected_len { |
Alice Wang | 75d0563 | 2023-01-25 13:31:18 +0000 | [diff] [blame] | 136 | Ok(()) |
| 137 | } else { |
David Pursell | b59bcc4 | 2023-11-10 16:59:19 -0800 | [diff] [blame] | 138 | Err(SlotVerifyError::Verification(None)) |
Alice Wang | 75d0563 | 2023-01-25 13:31:18 +0000 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Alice Wang | ab0d020 | 2023-05-17 08:07:41 +0000 | [diff] [blame] | 142 | /// Verifies that the vbmeta contains at most one property descriptor and it indicates the |
| 143 | /// vm type is service VM. |
| 144 | fn verify_property_and_get_capabilities( |
| 145 | descriptors: &Descriptors, |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame] | 146 | ) -> Result<Vec<Capability>, PvmfwVerifyError> { |
Alice Wang | ab0d020 | 2023-05-17 08:07:41 +0000 | [diff] [blame] | 147 | if !descriptors.has_property_descriptor() { |
| 148 | return Ok(vec![]); |
| 149 | } |
| 150 | descriptors |
| 151 | .find_property_value(Capability::KEY) |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame] | 152 | .ok_or(PvmfwVerifyError::UnknownVbmetaProperty) |
Alice Wang | ab0d020 | 2023-05-17 08:07:41 +0000 | [diff] [blame] | 153 | .and_then(Capability::get_capabilities) |
| 154 | } |
| 155 | |
David Pursell | b59bcc4 | 2023-11-10 16:59:19 -0800 | [diff] [blame] | 156 | /// Verifies the given initrd partition, and checks that the resulting contents looks like expected. |
| 157 | fn verify_initrd( |
| 158 | ops: &mut Ops, |
| 159 | partition_name: PartitionName, |
| 160 | expected_initrd: &[u8], |
| 161 | ) -> SlotVerifyNoDataResult<()> { |
| 162 | let result = |
| 163 | ops.verify_partition(partition_name.as_cstr()).map_err(|e| e.without_verify_data())?; |
| 164 | verify_loaded_partition_has_expected_length( |
| 165 | result.partition_data(), |
| 166 | partition_name, |
| 167 | expected_initrd.len(), |
| 168 | ) |
| 169 | } |
| 170 | |
Alice Wang | f3d96b1 | 2022-12-15 13:10:47 +0000 | [diff] [blame] | 171 | /// Verifies the payload (signed kernel + initrd) against the trusted public key. |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame] | 172 | pub fn verify_payload<'a>( |
Alice Wang | 6b486f1 | 2023-01-06 13:12:16 +0000 | [diff] [blame] | 173 | kernel: &[u8], |
| 174 | initrd: Option<&[u8]>, |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame] | 175 | trusted_public_key: &'a [u8], |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame] | 176 | ) -> Result<VerifiedBootData<'a>, PvmfwVerifyError> { |
David Pursell | b59bcc4 | 2023-11-10 16:59:19 -0800 | [diff] [blame] | 177 | let payload = Payload::new(kernel, initrd, trusted_public_key); |
| 178 | let mut ops = Ops::new(&payload); |
Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 179 | let kernel_verify_result = ops.verify_partition(PartitionName::Kernel.as_cstr())?; |
Alice Wang | 75d0563 | 2023-01-25 13:31:18 +0000 | [diff] [blame] | 180 | |
David Pursell | b59bcc4 | 2023-11-10 16:59:19 -0800 | [diff] [blame] | 181 | let vbmeta_images = kernel_verify_result.vbmeta_data(); |
Shikha Panwar | a26f16a | 2023-09-27 09:39:00 +0000 | [diff] [blame] | 182 | // TODO(b/302093437): Use explicit rollback_index_location instead of default |
| 183 | // location (first element). |
| 184 | let rollback_index = |
| 185 | *kernel_verify_result.rollback_indexes().first().unwrap_or(&DEFAULT_ROLLBACK_INDEX); |
Alice Wang | d3f28ae | 2023-01-25 10:41:40 +0000 | [diff] [blame] | 186 | verify_only_one_vbmeta_exists(vbmeta_images)?; |
David Pursell | b59bcc4 | 2023-11-10 16:59:19 -0800 | [diff] [blame] | 187 | let vbmeta_image = &vbmeta_images[0]; |
| 188 | verify_vbmeta_is_from_kernel_partition(vbmeta_image)?; |
| 189 | let descriptors = Descriptors::from_vbmeta(vbmeta_image)?; |
Alice Wang | ab0d020 | 2023-05-17 08:07:41 +0000 | [diff] [blame] | 190 | let capabilities = verify_property_and_get_capabilities(&descriptors)?; |
Alice Wang | 7998551 | 2023-05-22 08:15:36 +0000 | [diff] [blame] | 191 | let kernel_descriptor = descriptors.find_hash_descriptor(PartitionName::Kernel)?; |
Alice Wang | d3f28ae | 2023-01-25 10:41:40 +0000 | [diff] [blame] | 192 | |
Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 193 | if initrd.is_none() { |
Alice Wang | 7998551 | 2023-05-22 08:15:36 +0000 | [diff] [blame] | 194 | verify_vbmeta_has_only_one_hash_descriptor(&descriptors)?; |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 195 | return Ok(VerifiedBootData { |
| 196 | debug_level: DebugLevel::None, |
Alice Wang | 9150948 | 2023-05-22 13:10:32 +0000 | [diff] [blame] | 197 | kernel_digest: *kernel_descriptor.digest, |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 198 | initrd_digest: None, |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame] | 199 | public_key: trusted_public_key, |
Alice Wang | ab0d020 | 2023-05-17 08:07:41 +0000 | [diff] [blame] | 200 | capabilities, |
Shikha Panwar | a26f16a | 2023-09-27 09:39:00 +0000 | [diff] [blame] | 201 | rollback_index, |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 202 | }); |
Alice Wang | 86383df | 2023-01-11 10:03:56 +0000 | [diff] [blame] | 203 | } |
Alice Wang | 5c1a756 | 2023-01-13 17:19:57 +0000 | [diff] [blame] | 204 | |
Alice Wang | 75d0563 | 2023-01-25 13:31:18 +0000 | [diff] [blame] | 205 | let initrd = initrd.unwrap(); |
David Pursell | b59bcc4 | 2023-11-10 16:59:19 -0800 | [diff] [blame] | 206 | let mut initrd_ops = Ops::new(&payload); |
| 207 | let (debug_level, initrd_partition_name) = |
| 208 | if verify_initrd(&mut initrd_ops, PartitionName::InitrdNormal, initrd).is_ok() { |
| 209 | (DebugLevel::None, PartitionName::InitrdNormal) |
| 210 | } else if verify_initrd(&mut initrd_ops, PartitionName::InitrdDebug, initrd).is_ok() { |
| 211 | (DebugLevel::Full, PartitionName::InitrdDebug) |
Alice Wang | 75d0563 | 2023-01-25 13:31:18 +0000 | [diff] [blame] | 212 | } else { |
David Pursell | b59bcc4 | 2023-11-10 16:59:19 -0800 | [diff] [blame] | 213 | return Err(SlotVerifyError::Verification(None).into()); |
Alice Wang | 75d0563 | 2023-01-25 13:31:18 +0000 | [diff] [blame] | 214 | }; |
Alice Wang | 7998551 | 2023-05-22 08:15:36 +0000 | [diff] [blame] | 215 | let initrd_descriptor = descriptors.find_hash_descriptor(initrd_partition_name)?; |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 216 | Ok(VerifiedBootData { |
| 217 | debug_level, |
Alice Wang | 9150948 | 2023-05-22 13:10:32 +0000 | [diff] [blame] | 218 | kernel_digest: *kernel_descriptor.digest, |
| 219 | initrd_digest: Some(*initrd_descriptor.digest), |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame] | 220 | public_key: trusted_public_key, |
Alice Wang | ab0d020 | 2023-05-17 08:07:41 +0000 | [diff] [blame] | 221 | capabilities, |
Shikha Panwar | a26f16a | 2023-09-27 09:39:00 +0000 | [diff] [blame] | 222 | rollback_index, |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 223 | }) |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 224 | } |