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; |
Alice Wang | f275286 | 2023-01-18 11:51:25 +0000 | [diff] [blame] | 23 | use avb_bindgen::{AvbPartitionData, AvbVBMetaData}; |
| 24 | use core::ffi::c_char; |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 25 | |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 26 | /// Verified data returned when the payload verification succeeds. |
Pierre-Clément Tosi | 81ca080 | 2023-02-14 10:41:38 +0000 | [diff] [blame] | 27 | #[derive(Debug, PartialEq, Eq)] |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame] | 28 | pub struct VerifiedBootData<'a> { |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 29 | /// DebugLevel of the VM. |
| 30 | pub debug_level: DebugLevel, |
| 31 | /// Kernel digest. |
| 32 | pub kernel_digest: Digest, |
| 33 | /// Initrd digest if initrd exists. |
| 34 | pub initrd_digest: Option<Digest>, |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame] | 35 | /// Trusted public key. |
| 36 | pub public_key: &'a [u8], |
Alice Wang | ab0d020 | 2023-05-17 08:07:41 +0000 | [diff] [blame] | 37 | /// VM capabilities. |
| 38 | pub capabilities: Vec<Capability>, |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Alice Wang | 5c1a756 | 2023-01-13 17:19:57 +0000 | [diff] [blame] | 41 | /// This enum corresponds to the `DebugLevel` in `VirtualMachineConfig`. |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 42 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
Alice Wang | 5c1a756 | 2023-01-13 17:19:57 +0000 | [diff] [blame] | 43 | pub enum DebugLevel { |
| 44 | /// Not debuggable at all. |
| 45 | None, |
| 46 | /// Fully debuggable. |
| 47 | Full, |
| 48 | } |
| 49 | |
Alice Wang | ab0d020 | 2023-05-17 08:07:41 +0000 | [diff] [blame] | 50 | /// VM Capability. |
| 51 | #[derive(Debug, PartialEq, Eq)] |
| 52 | pub enum Capability { |
| 53 | /// Remote attestation. |
| 54 | RemoteAttest, |
| 55 | } |
| 56 | |
| 57 | impl Capability { |
| 58 | const KEY: &[u8] = b"com.android.virt.cap"; |
| 59 | const REMOTE_ATTEST: &[u8] = b"remote_attest"; |
| 60 | const SEPARATOR: u8 = b'|'; |
| 61 | |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame^] | 62 | fn get_capabilities(property_value: &[u8]) -> Result<Vec<Self>, PvmfwVerifyError> { |
Alice Wang | ab0d020 | 2023-05-17 08:07:41 +0000 | [diff] [blame] | 63 | let mut res = Vec::new(); |
| 64 | |
| 65 | for v in property_value.split(|b| *b == Self::SEPARATOR) { |
| 66 | let cap = match v { |
| 67 | Self::REMOTE_ATTEST => Self::RemoteAttest, |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame^] | 68 | _ => return Err(PvmfwVerifyError::UnknownVbmetaProperty), |
Alice Wang | ab0d020 | 2023-05-17 08:07:41 +0000 | [diff] [blame] | 69 | }; |
| 70 | if res.contains(&cap) { |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame^] | 71 | return Err(avb::SlotVerifyError::InvalidMetadata.into()); |
Alice Wang | ab0d020 | 2023-05-17 08:07:41 +0000 | [diff] [blame] | 72 | } |
| 73 | res.push(cap); |
| 74 | } |
| 75 | Ok(res) |
| 76 | } |
| 77 | } |
| 78 | |
Alice Wang | d3f28ae | 2023-01-25 10:41:40 +0000 | [diff] [blame] | 79 | fn verify_only_one_vbmeta_exists( |
| 80 | vbmeta_images: &[AvbVBMetaData], |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame^] | 81 | ) -> Result<(), avb::SlotVerifyError> { |
Alice Wang | d3f28ae | 2023-01-25 10:41:40 +0000 | [diff] [blame] | 82 | if vbmeta_images.len() == 1 { |
| 83 | Ok(()) |
| 84 | } else { |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame^] | 85 | Err(avb::SlotVerifyError::InvalidMetadata) |
Alice Wang | d3f28ae | 2023-01-25 10:41:40 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
Alice Wang | 9dfb296 | 2023-01-18 10:01:34 +0000 | [diff] [blame] | 89 | fn verify_vbmeta_is_from_kernel_partition( |
| 90 | vbmeta_image: &AvbVBMetaData, |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame^] | 91 | ) -> Result<(), avb::SlotVerifyError> { |
Alice Wang | 9dfb296 | 2023-01-18 10:01:34 +0000 | [diff] [blame] | 92 | match (vbmeta_image.partition_name as *const c_char).try_into() { |
| 93 | Ok(PartitionName::Kernel) => Ok(()), |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame^] | 94 | _ => Err(avb::SlotVerifyError::InvalidMetadata), |
Alice Wang | 9dfb296 | 2023-01-18 10:01:34 +0000 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
Alice Wang | f275286 | 2023-01-18 11:51:25 +0000 | [diff] [blame] | 98 | fn verify_vbmeta_has_only_one_hash_descriptor( |
Alice Wang | 7998551 | 2023-05-22 08:15:36 +0000 | [diff] [blame] | 99 | descriptors: &Descriptors, |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame^] | 100 | ) -> Result<(), avb::SlotVerifyError> { |
Alice Wang | 7998551 | 2023-05-22 08:15:36 +0000 | [diff] [blame] | 101 | if descriptors.num_hash_descriptor() == 1 { |
Alice Wang | f275286 | 2023-01-18 11:51:25 +0000 | [diff] [blame] | 102 | Ok(()) |
| 103 | } else { |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame^] | 104 | Err(avb::SlotVerifyError::InvalidMetadata) |
Alice Wang | f275286 | 2023-01-18 11:51:25 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
Alice Wang | 75d0563 | 2023-01-25 13:31:18 +0000 | [diff] [blame] | 108 | fn verify_loaded_partition_has_expected_length( |
| 109 | loaded_partitions: &[AvbPartitionData], |
| 110 | partition_name: PartitionName, |
| 111 | expected_len: usize, |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame^] | 112 | ) -> Result<(), avb::SlotVerifyError> { |
Alice Wang | 75d0563 | 2023-01-25 13:31:18 +0000 | [diff] [blame] | 113 | if loaded_partitions.len() != 1 { |
| 114 | // Only one partition should be loaded in each verify result. |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame^] | 115 | return Err(avb::SlotVerifyError::Io); |
Alice Wang | 75d0563 | 2023-01-25 13:31:18 +0000 | [diff] [blame] | 116 | } |
| 117 | let loaded_partition = loaded_partitions[0]; |
| 118 | if !PartitionName::try_from(loaded_partition.partition_name as *const c_char) |
| 119 | .map_or(false, |p| p == partition_name) |
| 120 | { |
| 121 | // Only the requested partition should be loaded. |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame^] | 122 | return Err(avb::SlotVerifyError::Io); |
Alice Wang | 75d0563 | 2023-01-25 13:31:18 +0000 | [diff] [blame] | 123 | } |
| 124 | if loaded_partition.data_size == expected_len { |
| 125 | Ok(()) |
| 126 | } else { |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame^] | 127 | Err(avb::SlotVerifyError::Verification) |
Alice Wang | 75d0563 | 2023-01-25 13:31:18 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Alice Wang | ab0d020 | 2023-05-17 08:07:41 +0000 | [diff] [blame] | 131 | /// Verifies that the vbmeta contains at most one property descriptor and it indicates the |
| 132 | /// vm type is service VM. |
| 133 | fn verify_property_and_get_capabilities( |
| 134 | descriptors: &Descriptors, |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame^] | 135 | ) -> Result<Vec<Capability>, PvmfwVerifyError> { |
Alice Wang | ab0d020 | 2023-05-17 08:07:41 +0000 | [diff] [blame] | 136 | if !descriptors.has_property_descriptor() { |
| 137 | return Ok(vec![]); |
| 138 | } |
| 139 | descriptors |
| 140 | .find_property_value(Capability::KEY) |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame^] | 141 | .ok_or(PvmfwVerifyError::UnknownVbmetaProperty) |
Alice Wang | ab0d020 | 2023-05-17 08:07:41 +0000 | [diff] [blame] | 142 | .and_then(Capability::get_capabilities) |
| 143 | } |
| 144 | |
Alice Wang | f3d96b1 | 2022-12-15 13:10:47 +0000 | [diff] [blame] | 145 | /// 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] | 146 | pub fn verify_payload<'a>( |
Alice Wang | 6b486f1 | 2023-01-06 13:12:16 +0000 | [diff] [blame] | 147 | kernel: &[u8], |
| 148 | initrd: Option<&[u8]>, |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame] | 149 | trusted_public_key: &'a [u8], |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame^] | 150 | ) -> Result<VerifiedBootData<'a>, PvmfwVerifyError> { |
Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 151 | let mut payload = Payload::new(kernel, initrd, trusted_public_key); |
| 152 | let mut ops = Ops::from(&mut payload); |
| 153 | let kernel_verify_result = ops.verify_partition(PartitionName::Kernel.as_cstr())?; |
Alice Wang | 75d0563 | 2023-01-25 13:31:18 +0000 | [diff] [blame] | 154 | |
Alice Wang | 86383df | 2023-01-11 10:03:56 +0000 | [diff] [blame] | 155 | let vbmeta_images = kernel_verify_result.vbmeta_images()?; |
Alice Wang | d3f28ae | 2023-01-25 10:41:40 +0000 | [diff] [blame] | 156 | verify_only_one_vbmeta_exists(vbmeta_images)?; |
Alice Wang | 9dfb296 | 2023-01-18 10:01:34 +0000 | [diff] [blame] | 157 | let vbmeta_image = vbmeta_images[0]; |
| 158 | verify_vbmeta_is_from_kernel_partition(&vbmeta_image)?; |
Alice Wang | f275286 | 2023-01-18 11:51:25 +0000 | [diff] [blame] | 159 | // SAFETY: It is safe because the `vbmeta_image` is collected from `AvbSlotVerifyData`, |
| 160 | // which is returned by `avb_slot_verify()` when the verification succeeds. It is |
| 161 | // guaranteed by libavb to be non-null and to point to a valid VBMeta structure. |
Alice Wang | 7998551 | 2023-05-22 08:15:36 +0000 | [diff] [blame] | 162 | let descriptors = unsafe { Descriptors::from_vbmeta(vbmeta_image)? }; |
Alice Wang | ab0d020 | 2023-05-17 08:07:41 +0000 | [diff] [blame] | 163 | let capabilities = verify_property_and_get_capabilities(&descriptors)?; |
Alice Wang | 7998551 | 2023-05-22 08:15:36 +0000 | [diff] [blame] | 164 | let kernel_descriptor = descriptors.find_hash_descriptor(PartitionName::Kernel)?; |
Alice Wang | d3f28ae | 2023-01-25 10:41:40 +0000 | [diff] [blame] | 165 | |
Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 166 | if initrd.is_none() { |
Alice Wang | 7998551 | 2023-05-22 08:15:36 +0000 | [diff] [blame] | 167 | verify_vbmeta_has_only_one_hash_descriptor(&descriptors)?; |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 168 | return Ok(VerifiedBootData { |
| 169 | debug_level: DebugLevel::None, |
Alice Wang | 9150948 | 2023-05-22 13:10:32 +0000 | [diff] [blame] | 170 | kernel_digest: *kernel_descriptor.digest, |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 171 | initrd_digest: None, |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame] | 172 | public_key: trusted_public_key, |
Alice Wang | ab0d020 | 2023-05-17 08:07:41 +0000 | [diff] [blame] | 173 | capabilities, |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 174 | }); |
Alice Wang | 86383df | 2023-01-11 10:03:56 +0000 | [diff] [blame] | 175 | } |
Alice Wang | 5c1a756 | 2023-01-13 17:19:57 +0000 | [diff] [blame] | 176 | |
Alice Wang | 75d0563 | 2023-01-25 13:31:18 +0000 | [diff] [blame] | 177 | let initrd = initrd.unwrap(); |
| 178 | let (debug_level, initrd_verify_result, initrd_partition_name) = |
| 179 | if let Ok(result) = ops.verify_partition(PartitionName::InitrdNormal.as_cstr()) { |
| 180 | (DebugLevel::None, result, PartitionName::InitrdNormal) |
| 181 | } else if let Ok(result) = ops.verify_partition(PartitionName::InitrdDebug.as_cstr()) { |
| 182 | (DebugLevel::Full, result, PartitionName::InitrdDebug) |
| 183 | } else { |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame^] | 184 | return Err(avb::SlotVerifyError::Verification.into()); |
Alice Wang | 75d0563 | 2023-01-25 13:31:18 +0000 | [diff] [blame] | 185 | }; |
| 186 | let loaded_partitions = initrd_verify_result.loaded_partitions()?; |
| 187 | verify_loaded_partition_has_expected_length( |
| 188 | loaded_partitions, |
| 189 | initrd_partition_name, |
| 190 | initrd.len(), |
| 191 | )?; |
Alice Wang | 7998551 | 2023-05-22 08:15:36 +0000 | [diff] [blame] | 192 | let initrd_descriptor = descriptors.find_hash_descriptor(initrd_partition_name)?; |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 193 | Ok(VerifiedBootData { |
| 194 | debug_level, |
Alice Wang | 9150948 | 2023-05-22 13:10:32 +0000 | [diff] [blame] | 195 | kernel_digest: *kernel_descriptor.digest, |
| 196 | initrd_digest: Some(*initrd_descriptor.digest), |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame] | 197 | public_key: trusted_public_key, |
Alice Wang | ab0d020 | 2023-05-17 08:07:41 +0000 | [diff] [blame] | 198 | capabilities, |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 199 | }) |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 200 | } |