blob: 1a16f9d00fd87401cc20b72b9d05529bdf583b69 [file] [log] [blame]
Alice Wang28cbcf12022-12-01 07:58:28 +00001// 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 Wangf3d96b12022-12-15 13:10:47 +000015//! This module handles the pvmfw payload verification.
Alice Wang28cbcf12022-12-01 07:58:28 +000016
Alice Wang79985512023-05-22 08:15:36 +000017use crate::descriptor::{Descriptors, Digest};
Alice Wang167ab3f2023-01-23 13:39:25 +000018use crate::ops::{Ops, Payload};
Alice Wang8077a862023-01-18 16:06:37 +000019use crate::partition::PartitionName;
David Pursella7c727b2023-08-14 16:24:40 -070020use crate::PvmfwVerifyError;
Alice Wangab0d0202023-05-17 08:07:41 +000021use alloc::vec;
22use alloc::vec::Vec;
Alice Wangf2752862023-01-18 11:51:25 +000023use avb_bindgen::{AvbPartitionData, AvbVBMetaData};
24use core::ffi::c_char;
Alice Wang28cbcf12022-12-01 07:58:28 +000025
Shikha Panwara26f16a2023-09-27 09:39:00 +000026// We use this for the rollback_index field if AvbSlotVerifyDataWrap has empty rollback_indexes
27const DEFAULT_ROLLBACK_INDEX: u64 = 0;
28
Alice Wang1f0add02023-01-23 16:22:53 +000029/// Verified data returned when the payload verification succeeds.
Pierre-Clément Tosi81ca0802023-02-14 10:41:38 +000030#[derive(Debug, PartialEq, Eq)]
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +000031pub struct VerifiedBootData<'a> {
Alice Wang1f0add02023-01-23 16:22:53 +000032 /// DebugLevel of the VM.
33 pub debug_level: DebugLevel,
34 /// Kernel digest.
35 pub kernel_digest: Digest,
36 /// Initrd digest if initrd exists.
37 pub initrd_digest: Option<Digest>,
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +000038 /// Trusted public key.
39 pub public_key: &'a [u8],
Alice Wangab0d0202023-05-17 08:07:41 +000040 /// VM capabilities.
41 pub capabilities: Vec<Capability>,
Shikha Panwara26f16a2023-09-27 09:39:00 +000042 /// Rollback index of kernel.
43 pub rollback_index: u64,
Alice Wang1f0add02023-01-23 16:22:53 +000044}
45
Alice Wang5c1a7562023-01-13 17:19:57 +000046/// This enum corresponds to the `DebugLevel` in `VirtualMachineConfig`.
Alice Wang1f0add02023-01-23 16:22:53 +000047#[derive(Clone, Copy, Debug, PartialEq, Eq)]
Alice Wang5c1a7562023-01-13 17:19:57 +000048pub enum DebugLevel {
49 /// Not debuggable at all.
50 None,
51 /// Fully debuggable.
52 Full,
53}
54
Alice Wangab0d0202023-05-17 08:07:41 +000055/// VM Capability.
56#[derive(Debug, PartialEq, Eq)]
57pub enum Capability {
58 /// Remote attestation.
59 RemoteAttest,
60}
61
62impl Capability {
63 const KEY: &[u8] = b"com.android.virt.cap";
64 const REMOTE_ATTEST: &[u8] = b"remote_attest";
65 const SEPARATOR: u8 = b'|';
66
David Pursella7c727b2023-08-14 16:24:40 -070067 fn get_capabilities(property_value: &[u8]) -> Result<Vec<Self>, PvmfwVerifyError> {
Alice Wangab0d0202023-05-17 08:07:41 +000068 let mut res = Vec::new();
69
70 for v in property_value.split(|b| *b == Self::SEPARATOR) {
71 let cap = match v {
72 Self::REMOTE_ATTEST => Self::RemoteAttest,
David Pursella7c727b2023-08-14 16:24:40 -070073 _ => return Err(PvmfwVerifyError::UnknownVbmetaProperty),
Alice Wangab0d0202023-05-17 08:07:41 +000074 };
75 if res.contains(&cap) {
David Pursella7c727b2023-08-14 16:24:40 -070076 return Err(avb::SlotVerifyError::InvalidMetadata.into());
Alice Wangab0d0202023-05-17 08:07:41 +000077 }
78 res.push(cap);
79 }
80 Ok(res)
81 }
82}
83
Alice Wangd3f28ae2023-01-25 10:41:40 +000084fn verify_only_one_vbmeta_exists(
85 vbmeta_images: &[AvbVBMetaData],
David Pursella7c727b2023-08-14 16:24:40 -070086) -> Result<(), avb::SlotVerifyError> {
Alice Wangd3f28ae2023-01-25 10:41:40 +000087 if vbmeta_images.len() == 1 {
88 Ok(())
89 } else {
David Pursella7c727b2023-08-14 16:24:40 -070090 Err(avb::SlotVerifyError::InvalidMetadata)
Alice Wangd3f28ae2023-01-25 10:41:40 +000091 }
92}
93
Alice Wang9dfb2962023-01-18 10:01:34 +000094fn verify_vbmeta_is_from_kernel_partition(
95 vbmeta_image: &AvbVBMetaData,
David Pursella7c727b2023-08-14 16:24:40 -070096) -> Result<(), avb::SlotVerifyError> {
Alice Wang9dfb2962023-01-18 10:01:34 +000097 match (vbmeta_image.partition_name as *const c_char).try_into() {
98 Ok(PartitionName::Kernel) => Ok(()),
David Pursella7c727b2023-08-14 16:24:40 -070099 _ => Err(avb::SlotVerifyError::InvalidMetadata),
Alice Wang9dfb2962023-01-18 10:01:34 +0000100 }
101}
102
Alice Wangf2752862023-01-18 11:51:25 +0000103fn verify_vbmeta_has_only_one_hash_descriptor(
Alice Wang79985512023-05-22 08:15:36 +0000104 descriptors: &Descriptors,
David Pursella7c727b2023-08-14 16:24:40 -0700105) -> Result<(), avb::SlotVerifyError> {
Alice Wang79985512023-05-22 08:15:36 +0000106 if descriptors.num_hash_descriptor() == 1 {
Alice Wangf2752862023-01-18 11:51:25 +0000107 Ok(())
108 } else {
David Pursella7c727b2023-08-14 16:24:40 -0700109 Err(avb::SlotVerifyError::InvalidMetadata)
Alice Wangf2752862023-01-18 11:51:25 +0000110 }
111}
112
Alice Wang75d05632023-01-25 13:31:18 +0000113fn verify_loaded_partition_has_expected_length(
114 loaded_partitions: &[AvbPartitionData],
115 partition_name: PartitionName,
116 expected_len: usize,
David Pursella7c727b2023-08-14 16:24:40 -0700117) -> Result<(), avb::SlotVerifyError> {
Alice Wang75d05632023-01-25 13:31:18 +0000118 if loaded_partitions.len() != 1 {
119 // Only one partition should be loaded in each verify result.
David Pursella7c727b2023-08-14 16:24:40 -0700120 return Err(avb::SlotVerifyError::Io);
Alice Wang75d05632023-01-25 13:31:18 +0000121 }
122 let loaded_partition = loaded_partitions[0];
123 if !PartitionName::try_from(loaded_partition.partition_name as *const c_char)
124 .map_or(false, |p| p == partition_name)
125 {
126 // Only the requested partition should be loaded.
David Pursella7c727b2023-08-14 16:24:40 -0700127 return Err(avb::SlotVerifyError::Io);
Alice Wang75d05632023-01-25 13:31:18 +0000128 }
129 if loaded_partition.data_size == expected_len {
130 Ok(())
131 } else {
David Pursella7c727b2023-08-14 16:24:40 -0700132 Err(avb::SlotVerifyError::Verification)
Alice Wang75d05632023-01-25 13:31:18 +0000133 }
134}
135
Alice Wangab0d0202023-05-17 08:07:41 +0000136/// Verifies that the vbmeta contains at most one property descriptor and it indicates the
137/// vm type is service VM.
138fn verify_property_and_get_capabilities(
139 descriptors: &Descriptors,
David Pursella7c727b2023-08-14 16:24:40 -0700140) -> Result<Vec<Capability>, PvmfwVerifyError> {
Alice Wangab0d0202023-05-17 08:07:41 +0000141 if !descriptors.has_property_descriptor() {
142 return Ok(vec![]);
143 }
144 descriptors
145 .find_property_value(Capability::KEY)
David Pursella7c727b2023-08-14 16:24:40 -0700146 .ok_or(PvmfwVerifyError::UnknownVbmetaProperty)
Alice Wangab0d0202023-05-17 08:07:41 +0000147 .and_then(Capability::get_capabilities)
148}
149
Alice Wangf3d96b12022-12-15 13:10:47 +0000150/// Verifies the payload (signed kernel + initrd) against the trusted public key.
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +0000151pub fn verify_payload<'a>(
Alice Wang6b486f12023-01-06 13:12:16 +0000152 kernel: &[u8],
153 initrd: Option<&[u8]>,
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +0000154 trusted_public_key: &'a [u8],
David Pursella7c727b2023-08-14 16:24:40 -0700155) -> Result<VerifiedBootData<'a>, PvmfwVerifyError> {
Alice Wang167ab3f2023-01-23 13:39:25 +0000156 let mut payload = Payload::new(kernel, initrd, trusted_public_key);
157 let mut ops = Ops::from(&mut payload);
158 let kernel_verify_result = ops.verify_partition(PartitionName::Kernel.as_cstr())?;
Alice Wang75d05632023-01-25 13:31:18 +0000159
Alice Wang86383df2023-01-11 10:03:56 +0000160 let vbmeta_images = kernel_verify_result.vbmeta_images()?;
Shikha Panwara26f16a2023-09-27 09:39:00 +0000161 // TODO(b/302093437): Use explicit rollback_index_location instead of default
162 // location (first element).
163 let rollback_index =
164 *kernel_verify_result.rollback_indexes().first().unwrap_or(&DEFAULT_ROLLBACK_INDEX);
Alice Wangd3f28ae2023-01-25 10:41:40 +0000165 verify_only_one_vbmeta_exists(vbmeta_images)?;
Alice Wang9dfb2962023-01-18 10:01:34 +0000166 let vbmeta_image = vbmeta_images[0];
167 verify_vbmeta_is_from_kernel_partition(&vbmeta_image)?;
Alice Wangf2752862023-01-18 11:51:25 +0000168 // SAFETY: It is safe because the `vbmeta_image` is collected from `AvbSlotVerifyData`,
169 // which is returned by `avb_slot_verify()` when the verification succeeds. It is
170 // guaranteed by libavb to be non-null and to point to a valid VBMeta structure.
Alice Wang79985512023-05-22 08:15:36 +0000171 let descriptors = unsafe { Descriptors::from_vbmeta(vbmeta_image)? };
Alice Wangab0d0202023-05-17 08:07:41 +0000172 let capabilities = verify_property_and_get_capabilities(&descriptors)?;
Alice Wang79985512023-05-22 08:15:36 +0000173 let kernel_descriptor = descriptors.find_hash_descriptor(PartitionName::Kernel)?;
Alice Wangd3f28ae2023-01-25 10:41:40 +0000174
Alice Wang167ab3f2023-01-23 13:39:25 +0000175 if initrd.is_none() {
Alice Wang79985512023-05-22 08:15:36 +0000176 verify_vbmeta_has_only_one_hash_descriptor(&descriptors)?;
Alice Wang1f0add02023-01-23 16:22:53 +0000177 return Ok(VerifiedBootData {
178 debug_level: DebugLevel::None,
Alice Wang91509482023-05-22 13:10:32 +0000179 kernel_digest: *kernel_descriptor.digest,
Alice Wang1f0add02023-01-23 16:22:53 +0000180 initrd_digest: None,
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +0000181 public_key: trusted_public_key,
Alice Wangab0d0202023-05-17 08:07:41 +0000182 capabilities,
Shikha Panwara26f16a2023-09-27 09:39:00 +0000183 rollback_index,
Alice Wang1f0add02023-01-23 16:22:53 +0000184 });
Alice Wang86383df2023-01-11 10:03:56 +0000185 }
Alice Wang5c1a7562023-01-13 17:19:57 +0000186
Alice Wang75d05632023-01-25 13:31:18 +0000187 let initrd = initrd.unwrap();
188 let (debug_level, initrd_verify_result, initrd_partition_name) =
189 if let Ok(result) = ops.verify_partition(PartitionName::InitrdNormal.as_cstr()) {
190 (DebugLevel::None, result, PartitionName::InitrdNormal)
191 } else if let Ok(result) = ops.verify_partition(PartitionName::InitrdDebug.as_cstr()) {
192 (DebugLevel::Full, result, PartitionName::InitrdDebug)
193 } else {
David Pursella7c727b2023-08-14 16:24:40 -0700194 return Err(avb::SlotVerifyError::Verification.into());
Alice Wang75d05632023-01-25 13:31:18 +0000195 };
196 let loaded_partitions = initrd_verify_result.loaded_partitions()?;
197 verify_loaded_partition_has_expected_length(
198 loaded_partitions,
199 initrd_partition_name,
200 initrd.len(),
201 )?;
Alice Wang79985512023-05-22 08:15:36 +0000202 let initrd_descriptor = descriptors.find_hash_descriptor(initrd_partition_name)?;
Alice Wang1f0add02023-01-23 16:22:53 +0000203 Ok(VerifiedBootData {
204 debug_level,
Alice Wang91509482023-05-22 13:10:32 +0000205 kernel_digest: *kernel_descriptor.digest,
206 initrd_digest: Some(*initrd_descriptor.digest),
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +0000207 public_key: trusted_public_key,
Alice Wangab0d0202023-05-17 08:07:41 +0000208 capabilities,
Shikha Panwara26f16a2023-09-27 09:39:00 +0000209 rollback_index,
Alice Wang1f0add02023-01-23 16:22:53 +0000210 })
Alice Wang28cbcf12022-12-01 07:58:28 +0000211}