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 | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 17 | use crate::descriptor::{Digest, HashDescriptors}; |
Alice Wang | f275286 | 2023-01-18 11:51:25 +0000 | [diff] [blame] | 18 | use crate::error::AvbSlotVerifyError; |
Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 19 | use crate::ops::{Ops, Payload}; |
Alice Wang | 8077a86 | 2023-01-18 16:06:37 +0000 | [diff] [blame] | 20 | use crate::partition::PartitionName; |
Alice Wang | f275286 | 2023-01-18 11:51:25 +0000 | [diff] [blame] | 21 | use avb_bindgen::{AvbPartitionData, AvbVBMetaData}; |
| 22 | use core::ffi::c_char; |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 23 | |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 24 | /// Verified data returned when the payload verification succeeds. |
Pierre-Clément Tosi | 81ca080 | 2023-02-14 10:41:38 +0000 | [diff] [blame] | 25 | #[derive(Debug, PartialEq, Eq)] |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame^] | 26 | pub struct VerifiedBootData<'a> { |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 27 | /// DebugLevel of the VM. |
| 28 | pub debug_level: DebugLevel, |
| 29 | /// Kernel digest. |
| 30 | pub kernel_digest: Digest, |
| 31 | /// Initrd digest if initrd exists. |
| 32 | pub initrd_digest: Option<Digest>, |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame^] | 33 | /// Trusted public key. |
| 34 | pub public_key: &'a [u8], |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Alice Wang | 5c1a756 | 2023-01-13 17:19:57 +0000 | [diff] [blame] | 37 | /// This enum corresponds to the `DebugLevel` in `VirtualMachineConfig`. |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 38 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
Alice Wang | 5c1a756 | 2023-01-13 17:19:57 +0000 | [diff] [blame] | 39 | pub enum DebugLevel { |
| 40 | /// Not debuggable at all. |
| 41 | None, |
| 42 | /// Fully debuggable. |
| 43 | Full, |
| 44 | } |
| 45 | |
Alice Wang | d3f28ae | 2023-01-25 10:41:40 +0000 | [diff] [blame] | 46 | fn verify_only_one_vbmeta_exists( |
| 47 | vbmeta_images: &[AvbVBMetaData], |
| 48 | ) -> Result<(), AvbSlotVerifyError> { |
| 49 | if vbmeta_images.len() == 1 { |
| 50 | Ok(()) |
| 51 | } else { |
| 52 | Err(AvbSlotVerifyError::InvalidMetadata) |
| 53 | } |
| 54 | } |
| 55 | |
Alice Wang | 9dfb296 | 2023-01-18 10:01:34 +0000 | [diff] [blame] | 56 | fn verify_vbmeta_is_from_kernel_partition( |
| 57 | vbmeta_image: &AvbVBMetaData, |
| 58 | ) -> Result<(), AvbSlotVerifyError> { |
| 59 | match (vbmeta_image.partition_name as *const c_char).try_into() { |
| 60 | Ok(PartitionName::Kernel) => Ok(()), |
| 61 | _ => Err(AvbSlotVerifyError::InvalidMetadata), |
| 62 | } |
| 63 | } |
| 64 | |
Alice Wang | f275286 | 2023-01-18 11:51:25 +0000 | [diff] [blame] | 65 | fn verify_vbmeta_has_only_one_hash_descriptor( |
| 66 | hash_descriptors: &HashDescriptors, |
| 67 | ) -> Result<(), AvbSlotVerifyError> { |
| 68 | if hash_descriptors.len() == 1 { |
| 69 | Ok(()) |
| 70 | } else { |
| 71 | Err(AvbSlotVerifyError::InvalidMetadata) |
| 72 | } |
| 73 | } |
| 74 | |
Alice Wang | 75d0563 | 2023-01-25 13:31:18 +0000 | [diff] [blame] | 75 | fn verify_loaded_partition_has_expected_length( |
| 76 | loaded_partitions: &[AvbPartitionData], |
| 77 | partition_name: PartitionName, |
| 78 | expected_len: usize, |
| 79 | ) -> Result<(), AvbSlotVerifyError> { |
| 80 | if loaded_partitions.len() != 1 { |
| 81 | // Only one partition should be loaded in each verify result. |
| 82 | return Err(AvbSlotVerifyError::Io); |
| 83 | } |
| 84 | let loaded_partition = loaded_partitions[0]; |
| 85 | if !PartitionName::try_from(loaded_partition.partition_name as *const c_char) |
| 86 | .map_or(false, |p| p == partition_name) |
| 87 | { |
| 88 | // Only the requested partition should be loaded. |
| 89 | return Err(AvbSlotVerifyError::Io); |
| 90 | } |
| 91 | if loaded_partition.data_size == expected_len { |
| 92 | Ok(()) |
| 93 | } else { |
| 94 | Err(AvbSlotVerifyError::Verification) |
| 95 | } |
| 96 | } |
| 97 | |
Alice Wang | f3d96b1 | 2022-12-15 13:10:47 +0000 | [diff] [blame] | 98 | /// 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^] | 99 | pub fn verify_payload<'a>( |
Alice Wang | 6b486f1 | 2023-01-06 13:12:16 +0000 | [diff] [blame] | 100 | kernel: &[u8], |
| 101 | initrd: Option<&[u8]>, |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame^] | 102 | trusted_public_key: &'a [u8], |
| 103 | ) -> Result<VerifiedBootData<'a>, AvbSlotVerifyError> { |
Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 104 | let mut payload = Payload::new(kernel, initrd, trusted_public_key); |
| 105 | let mut ops = Ops::from(&mut payload); |
| 106 | let kernel_verify_result = ops.verify_partition(PartitionName::Kernel.as_cstr())?; |
Alice Wang | 75d0563 | 2023-01-25 13:31:18 +0000 | [diff] [blame] | 107 | |
Alice Wang | 86383df | 2023-01-11 10:03:56 +0000 | [diff] [blame] | 108 | let vbmeta_images = kernel_verify_result.vbmeta_images()?; |
Alice Wang | d3f28ae | 2023-01-25 10:41:40 +0000 | [diff] [blame] | 109 | verify_only_one_vbmeta_exists(vbmeta_images)?; |
Alice Wang | 9dfb296 | 2023-01-18 10:01:34 +0000 | [diff] [blame] | 110 | let vbmeta_image = vbmeta_images[0]; |
| 111 | verify_vbmeta_is_from_kernel_partition(&vbmeta_image)?; |
Alice Wang | f275286 | 2023-01-18 11:51:25 +0000 | [diff] [blame] | 112 | // SAFETY: It is safe because the `vbmeta_image` is collected from `AvbSlotVerifyData`, |
| 113 | // which is returned by `avb_slot_verify()` when the verification succeeds. It is |
| 114 | // guaranteed by libavb to be non-null and to point to a valid VBMeta structure. |
| 115 | let hash_descriptors = unsafe { HashDescriptors::from_vbmeta(vbmeta_image)? }; |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 116 | let kernel_descriptor = hash_descriptors.find(PartitionName::Kernel)?; |
Alice Wang | d3f28ae | 2023-01-25 10:41:40 +0000 | [diff] [blame] | 117 | |
Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 118 | if initrd.is_none() { |
Alice Wang | f275286 | 2023-01-18 11:51:25 +0000 | [diff] [blame] | 119 | verify_vbmeta_has_only_one_hash_descriptor(&hash_descriptors)?; |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 120 | return Ok(VerifiedBootData { |
| 121 | debug_level: DebugLevel::None, |
| 122 | kernel_digest: kernel_descriptor.digest, |
| 123 | initrd_digest: None, |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame^] | 124 | public_key: trusted_public_key, |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 125 | }); |
Alice Wang | 86383df | 2023-01-11 10:03:56 +0000 | [diff] [blame] | 126 | } |
Alice Wang | 5c1a756 | 2023-01-13 17:19:57 +0000 | [diff] [blame] | 127 | |
Alice Wang | 75d0563 | 2023-01-25 13:31:18 +0000 | [diff] [blame] | 128 | let initrd = initrd.unwrap(); |
| 129 | let (debug_level, initrd_verify_result, initrd_partition_name) = |
| 130 | if let Ok(result) = ops.verify_partition(PartitionName::InitrdNormal.as_cstr()) { |
| 131 | (DebugLevel::None, result, PartitionName::InitrdNormal) |
| 132 | } else if let Ok(result) = ops.verify_partition(PartitionName::InitrdDebug.as_cstr()) { |
| 133 | (DebugLevel::Full, result, PartitionName::InitrdDebug) |
| 134 | } else { |
| 135 | return Err(AvbSlotVerifyError::Verification); |
| 136 | }; |
| 137 | let loaded_partitions = initrd_verify_result.loaded_partitions()?; |
| 138 | verify_loaded_partition_has_expected_length( |
| 139 | loaded_partitions, |
| 140 | initrd_partition_name, |
| 141 | initrd.len(), |
| 142 | )?; |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 143 | let initrd_descriptor = hash_descriptors.find(initrd_partition_name)?; |
| 144 | Ok(VerifiedBootData { |
| 145 | debug_level, |
| 146 | kernel_digest: kernel_descriptor.digest, |
| 147 | initrd_digest: Some(initrd_descriptor.digest), |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame^] | 148 | public_key: trusted_public_key, |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 149 | }) |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 150 | } |