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