blob: 261d8a8718776244acf9e69343191ff9a588cffe [file] [log] [blame]
Alice Wangbf7fadd2023-01-13 12:18:24 +00001/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Alice Wang2925b0a2023-01-19 10:44:24 +000017mod utils;
Alice Wangbf7fadd2023-01-13 12:18:24 +000018
Alice Wang1f0add02023-01-23 16:22:53 +000019use anyhow::{anyhow, Result};
Alice Wang2925b0a2023-01-19 10:44:24 +000020use avb_bindgen::{AvbFooter, AvbVBMetaImageHeader};
Alice Wang1f0add02023-01-23 16:22:53 +000021use pvmfw_avb::{verify_payload, AvbSlotVerifyError, DebugLevel};
Alice Wang2925b0a2023-01-19 10:44:24 +000022use std::{fs, mem::size_of, ptr};
23use utils::*;
24
Alice Wangbf7fadd2023-01-13 12:18:24 +000025const TEST_IMG_WITH_ONE_HASHDESC_PATH: &str = "test_image_with_one_hashdesc.img";
Alice Wang86383df2023-01-11 10:03:56 +000026const TEST_IMG_WITH_PROP_DESC_PATH: &str = "test_image_with_prop_desc.img";
27const TEST_IMG_WITH_NON_INITRD_HASHDESC_PATH: &str = "test_image_with_non_initrd_hashdesc.img";
Alice Wangf2752862023-01-18 11:51:25 +000028const TEST_IMG_WITH_INITRD_AND_NON_INITRD_DESC_PATH: &str =
29 "test_image_with_initrd_and_non_initrd_desc.img";
Alice Wangbf7fadd2023-01-13 12:18:24 +000030const UNSIGNED_TEST_IMG_PATH: &str = "unsigned_test.img";
31
Alice Wangbf7fadd2023-01-13 12:18:24 +000032const RANDOM_FOOTER_POS: usize = 30;
33
34/// This test uses the Microdroid payload compiled on the fly to check that
35/// the latest payload can be verified successfully.
36#[test]
Alice Wang4e55dd92023-01-11 10:17:01 +000037fn latest_normal_payload_passes_verification() -> Result<()> {
Alice Wang1f0add02023-01-23 16:22:53 +000038 assert_latest_payload_verification_passes(
Alice Wang4e55dd92023-01-11 10:17:01 +000039 &load_latest_initrd_normal()?,
Alice Wang1f0add02023-01-23 16:22:53 +000040 b"initrd_normal",
41 DebugLevel::None,
Alice Wang4e55dd92023-01-11 10:17:01 +000042 )
43}
Alice Wangbf7fadd2023-01-13 12:18:24 +000044
Alice Wang4e55dd92023-01-11 10:17:01 +000045#[test]
46fn latest_debug_payload_passes_verification() -> Result<()> {
Alice Wang1f0add02023-01-23 16:22:53 +000047 assert_latest_payload_verification_passes(
Alice Wang4e55dd92023-01-11 10:17:01 +000048 &load_latest_initrd_debug()?,
Alice Wang1f0add02023-01-23 16:22:53 +000049 b"initrd_debug",
50 DebugLevel::Full,
Alice Wang4e55dd92023-01-11 10:17:01 +000051 )
Alice Wangbf7fadd2023-01-13 12:18:24 +000052}
53
54#[test]
55fn payload_expecting_no_initrd_passes_verification_with_no_initrd() -> Result<()> {
Alice Wang1f0add02023-01-23 16:22:53 +000056 let verified_boot_data = verify_payload(
Alice Wang86383df2023-01-11 10:03:56 +000057 &fs::read(TEST_IMG_WITH_ONE_HASHDESC_PATH)?,
Alice Wang2925b0a2023-01-19 10:44:24 +000058 /*initrd=*/ None,
Alice Wang86383df2023-01-11 10:03:56 +000059 &load_trusted_public_key()?,
Alice Wang86383df2023-01-11 10:03:56 +000060 )
Alice Wang1f0add02023-01-23 16:22:53 +000061 .map_err(|e| anyhow!("Verification failed. Error: {}", e))?;
62
63 assert_eq!(DebugLevel::None, verified_boot_data.debug_level);
64 let digest = hash(&[&hex::decode("1111")?, &fs::read(UNSIGNED_TEST_IMG_PATH)?]);
65 assert_eq!(digest, verified_boot_data.kernel_digest);
66 assert!(verified_boot_data.initrd_digest.is_none());
67 Ok(())
Alice Wangbf7fadd2023-01-13 12:18:24 +000068}
69
Alice Wang86383df2023-01-11 10:03:56 +000070#[test]
Alice Wangf2752862023-01-18 11:51:25 +000071fn payload_with_non_initrd_descriptor_fails_verification_with_no_initrd() -> Result<()> {
Alice Wang1f0add02023-01-23 16:22:53 +000072 assert_payload_verification_fails(
Alice Wang86383df2023-01-11 10:03:56 +000073 &fs::read(TEST_IMG_WITH_NON_INITRD_HASHDESC_PATH)?,
Alice Wang2925b0a2023-01-19 10:44:24 +000074 /*initrd=*/ None,
Alice Wang86383df2023-01-11 10:03:56 +000075 &load_trusted_public_key()?,
Alice Wang1f0add02023-01-23 16:22:53 +000076 AvbSlotVerifyError::InvalidMetadata,
Alice Wangf2752862023-01-18 11:51:25 +000077 )
78}
79
80#[test]
81fn payload_with_non_initrd_descriptor_fails_verification_with_initrd() -> Result<()> {
Alice Wang1f0add02023-01-23 16:22:53 +000082 assert_payload_verification_with_initrd_fails(
Alice Wangf2752862023-01-18 11:51:25 +000083 &fs::read(TEST_IMG_WITH_INITRD_AND_NON_INITRD_DESC_PATH)?,
84 &load_latest_initrd_normal()?,
85 &load_trusted_public_key()?,
Alice Wang1f0add02023-01-23 16:22:53 +000086 AvbSlotVerifyError::InvalidMetadata,
Alice Wang86383df2023-01-11 10:03:56 +000087 )
88}
89
90#[test]
91fn payload_with_prop_descriptor_fails_verification_with_no_initrd() -> Result<()> {
Alice Wang1f0add02023-01-23 16:22:53 +000092 assert_payload_verification_fails(
Alice Wang86383df2023-01-11 10:03:56 +000093 &fs::read(TEST_IMG_WITH_PROP_DESC_PATH)?,
Alice Wang2925b0a2023-01-19 10:44:24 +000094 /*initrd=*/ None,
Alice Wang86383df2023-01-11 10:03:56 +000095 &load_trusted_public_key()?,
Alice Wang1f0add02023-01-23 16:22:53 +000096 AvbSlotVerifyError::InvalidMetadata,
Alice Wang86383df2023-01-11 10:03:56 +000097 )
98}
99
100#[test]
101fn payload_expecting_initrd_fails_verification_with_no_initrd() -> Result<()> {
Alice Wang1f0add02023-01-23 16:22:53 +0000102 assert_payload_verification_fails(
Alice Wang86383df2023-01-11 10:03:56 +0000103 &load_latest_signed_kernel()?,
Alice Wang2925b0a2023-01-19 10:44:24 +0000104 /*initrd=*/ None,
Alice Wang86383df2023-01-11 10:03:56 +0000105 &load_trusted_public_key()?,
Alice Wang1f0add02023-01-23 16:22:53 +0000106 AvbSlotVerifyError::InvalidMetadata,
Alice Wang86383df2023-01-11 10:03:56 +0000107 )
108}
Alice Wangbf7fadd2023-01-13 12:18:24 +0000109
110#[test]
111fn payload_with_empty_public_key_fails_verification() -> Result<()> {
Alice Wang1f0add02023-01-23 16:22:53 +0000112 assert_payload_verification_with_initrd_fails(
Alice Wangbf7fadd2023-01-13 12:18:24 +0000113 &load_latest_signed_kernel()?,
114 &load_latest_initrd_normal()?,
115 /*trusted_public_key=*/ &[0u8; 0],
Alice Wang1f0add02023-01-23 16:22:53 +0000116 AvbSlotVerifyError::PublicKeyRejected,
Alice Wangbf7fadd2023-01-13 12:18:24 +0000117 )
118}
119
120#[test]
121fn payload_with_an_invalid_public_key_fails_verification() -> Result<()> {
Alice Wang1f0add02023-01-23 16:22:53 +0000122 assert_payload_verification_with_initrd_fails(
Alice Wangbf7fadd2023-01-13 12:18:24 +0000123 &load_latest_signed_kernel()?,
124 &load_latest_initrd_normal()?,
125 /*trusted_public_key=*/ &[0u8; 512],
Alice Wang1f0add02023-01-23 16:22:53 +0000126 AvbSlotVerifyError::PublicKeyRejected,
Alice Wangbf7fadd2023-01-13 12:18:24 +0000127 )
128}
129
130#[test]
131fn payload_with_a_different_valid_public_key_fails_verification() -> Result<()> {
Alice Wang1f0add02023-01-23 16:22:53 +0000132 assert_payload_verification_with_initrd_fails(
Alice Wangbf7fadd2023-01-13 12:18:24 +0000133 &load_latest_signed_kernel()?,
134 &load_latest_initrd_normal()?,
135 &fs::read(PUBLIC_KEY_RSA2048_PATH)?,
Alice Wang1f0add02023-01-23 16:22:53 +0000136 AvbSlotVerifyError::PublicKeyRejected,
Alice Wangbf7fadd2023-01-13 12:18:24 +0000137 )
138}
139
140#[test]
Alice Wang5c1a7562023-01-13 17:19:57 +0000141fn payload_with_an_invalid_initrd_fails_verification() -> Result<()> {
Alice Wang1f0add02023-01-23 16:22:53 +0000142 assert_payload_verification_with_initrd_fails(
Alice Wang5c1a7562023-01-13 17:19:57 +0000143 &load_latest_signed_kernel()?,
144 /*initrd=*/ &fs::read(UNSIGNED_TEST_IMG_PATH)?,
145 &load_trusted_public_key()?,
Alice Wang1f0add02023-01-23 16:22:53 +0000146 AvbSlotVerifyError::Verification,
Alice Wang5c1a7562023-01-13 17:19:57 +0000147 )
148}
149
150#[test]
Alice Wangbf7fadd2023-01-13 12:18:24 +0000151fn unsigned_kernel_fails_verification() -> Result<()> {
Alice Wang1f0add02023-01-23 16:22:53 +0000152 assert_payload_verification_with_initrd_fails(
Alice Wangbf7fadd2023-01-13 12:18:24 +0000153 &fs::read(UNSIGNED_TEST_IMG_PATH)?,
154 &load_latest_initrd_normal()?,
Alice Wang4e55dd92023-01-11 10:17:01 +0000155 &load_trusted_public_key()?,
Alice Wang1f0add02023-01-23 16:22:53 +0000156 AvbSlotVerifyError::Io,
Alice Wangbf7fadd2023-01-13 12:18:24 +0000157 )
158}
159
160#[test]
161fn tampered_kernel_fails_verification() -> Result<()> {
162 let mut kernel = load_latest_signed_kernel()?;
163 kernel[1] = !kernel[1]; // Flip the bits
164
Alice Wang1f0add02023-01-23 16:22:53 +0000165 assert_payload_verification_with_initrd_fails(
Alice Wangbf7fadd2023-01-13 12:18:24 +0000166 &kernel,
167 &load_latest_initrd_normal()?,
Alice Wang4e55dd92023-01-11 10:17:01 +0000168 &load_trusted_public_key()?,
Alice Wang1f0add02023-01-23 16:22:53 +0000169 AvbSlotVerifyError::Verification,
Alice Wangbf7fadd2023-01-13 12:18:24 +0000170 )
171}
172
173#[test]
Alice Wangfaceff42023-01-19 09:54:38 +0000174fn kernel_footer_with_vbmeta_offset_overwritten_fails_verification() -> Result<()> {
175 // Arrange.
176 let mut kernel = load_latest_signed_kernel()?;
177 let total_len = kernel.len() as u64;
178 let footer = extract_avb_footer(&kernel)?;
179 assert!(footer.vbmeta_offset < total_len);
180 let vbmeta_offset_addr = ptr::addr_of!(footer.vbmeta_offset) as *const u8;
181 // SAFETY: It is safe as both raw pointers `vbmeta_offset_addr` and `footer` are not null.
182 let vbmeta_offset_start =
183 unsafe { vbmeta_offset_addr.offset_from(ptr::addr_of!(footer) as *const u8) };
184 let footer_start = kernel.len() - size_of::<AvbFooter>();
185 let vbmeta_offset_start = footer_start + usize::try_from(vbmeta_offset_start)?;
186
187 let wrong_offsets = [total_len, u64::MAX];
188 for &wrong_offset in wrong_offsets.iter() {
189 // Act.
190 kernel[vbmeta_offset_start..(vbmeta_offset_start + size_of::<u64>())]
191 .copy_from_slice(&wrong_offset.to_be_bytes());
192
193 // Assert.
Inseob Kim8ebf1da2023-01-27 18:12:57 +0900194 let footer = extract_avb_footer(&kernel)?;
195 // footer is unaligned; copy vbmeta_offset to local variable
196 let vbmeta_offset = footer.vbmeta_offset;
197 assert_eq!(wrong_offset, vbmeta_offset);
Alice Wang1f0add02023-01-23 16:22:53 +0000198 assert_payload_verification_with_initrd_fails(
Alice Wangfaceff42023-01-19 09:54:38 +0000199 &kernel,
200 &load_latest_initrd_normal()?,
201 &load_trusted_public_key()?,
Alice Wang1f0add02023-01-23 16:22:53 +0000202 AvbSlotVerifyError::Io,
Alice Wangfaceff42023-01-19 09:54:38 +0000203 )?;
204 }
205 Ok(())
206}
207
208#[test]
Alice Wangbf7fadd2023-01-13 12:18:24 +0000209fn tampered_kernel_footer_fails_verification() -> Result<()> {
210 let mut kernel = load_latest_signed_kernel()?;
211 let avb_footer_index = kernel.len() - size_of::<AvbFooter>() + RANDOM_FOOTER_POS;
212 kernel[avb_footer_index] = !kernel[avb_footer_index];
213
Alice Wang1f0add02023-01-23 16:22:53 +0000214 assert_payload_verification_with_initrd_fails(
Alice Wangbf7fadd2023-01-13 12:18:24 +0000215 &kernel,
216 &load_latest_initrd_normal()?,
Alice Wang4e55dd92023-01-11 10:17:01 +0000217 &load_trusted_public_key()?,
Alice Wang1f0add02023-01-23 16:22:53 +0000218 AvbSlotVerifyError::InvalidMetadata,
Alice Wangbf7fadd2023-01-13 12:18:24 +0000219 )
220}
221
Alice Wang58dac082023-01-13 13:03:59 +0000222#[test]
Alice Wang75d05632023-01-25 13:31:18 +0000223fn extended_initrd_fails_verification() -> Result<()> {
224 let mut initrd = load_latest_initrd_normal()?;
225 initrd.extend(b"androidboot.vbmeta.digest=1111");
226
Alice Wang1f0add02023-01-23 16:22:53 +0000227 assert_payload_verification_with_initrd_fails(
Alice Wang75d05632023-01-25 13:31:18 +0000228 &load_latest_signed_kernel()?,
229 &initrd,
230 &load_trusted_public_key()?,
Alice Wang1f0add02023-01-23 16:22:53 +0000231 AvbSlotVerifyError::Verification,
Alice Wang75d05632023-01-25 13:31:18 +0000232 )
233}
234
235#[test]
Alice Wang58dac082023-01-13 13:03:59 +0000236fn tampered_vbmeta_fails_verification() -> Result<()> {
237 let mut kernel = load_latest_signed_kernel()?;
238 let footer = extract_avb_footer(&kernel)?;
239 let vbmeta_index: usize = (footer.vbmeta_offset + 1).try_into()?;
240
241 kernel[vbmeta_index] = !kernel[vbmeta_index]; // Flip the bits
242
Alice Wang1f0add02023-01-23 16:22:53 +0000243 assert_payload_verification_with_initrd_fails(
Alice Wang58dac082023-01-13 13:03:59 +0000244 &kernel,
245 &load_latest_initrd_normal()?,
246 &load_trusted_public_key()?,
Alice Wang1f0add02023-01-23 16:22:53 +0000247 AvbSlotVerifyError::InvalidMetadata,
Alice Wang58dac082023-01-13 13:03:59 +0000248 )
249}
250
251#[test]
252fn vbmeta_with_public_key_overwritten_fails_verification() -> Result<()> {
253 let mut kernel = load_latest_signed_kernel()?;
254 let footer = extract_avb_footer(&kernel)?;
255 let vbmeta_header = extract_vbmeta_header(&kernel, &footer)?;
256 let public_key_offset = footer.vbmeta_offset as usize
257 + size_of::<AvbVBMetaImageHeader>()
258 + vbmeta_header.authentication_data_block_size as usize
259 + vbmeta_header.public_key_offset as usize;
260 let public_key_size: usize = vbmeta_header.public_key_size.try_into()?;
261 let empty_public_key = vec![0u8; public_key_size];
262
263 kernel[public_key_offset..(public_key_offset + public_key_size)]
264 .copy_from_slice(&empty_public_key);
265
Alice Wang1f0add02023-01-23 16:22:53 +0000266 assert_payload_verification_with_initrd_fails(
Alice Wang58dac082023-01-13 13:03:59 +0000267 &kernel,
268 &load_latest_initrd_normal()?,
269 &empty_public_key,
Alice Wang1f0add02023-01-23 16:22:53 +0000270 AvbSlotVerifyError::Verification,
Alice Wang58dac082023-01-13 13:03:59 +0000271 )?;
Alice Wang1f0add02023-01-23 16:22:53 +0000272 assert_payload_verification_with_initrd_fails(
Alice Wang58dac082023-01-13 13:03:59 +0000273 &kernel,
274 &load_latest_initrd_normal()?,
275 &load_trusted_public_key()?,
Alice Wang1f0add02023-01-23 16:22:53 +0000276 AvbSlotVerifyError::Verification,
Alice Wang58dac082023-01-13 13:03:59 +0000277 )
278}
279
Alice Wangf06bfd72023-01-19 09:24:21 +0000280#[test]
281fn vbmeta_with_verification_flag_disabled_fails_verification() -> Result<()> {
282 // From external/avb/libavb/avb_vbmeta_image.h
283 const AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED: u32 = 2;
284
285 // Arrange.
286 let mut kernel = load_latest_signed_kernel()?;
287 let footer = extract_avb_footer(&kernel)?;
288 let vbmeta_header = extract_vbmeta_header(&kernel, &footer)?;
Inseob Kim8ebf1da2023-01-27 18:12:57 +0900289
290 // vbmeta_header is unaligned; copy flags to local variable
291 let vbmeta_header_flags = vbmeta_header.flags;
292 assert_eq!(0, vbmeta_header_flags, "The disable flag should not be set in the latest kernel.");
Alice Wangf06bfd72023-01-19 09:24:21 +0000293 let flags_addr = ptr::addr_of!(vbmeta_header.flags) as *const u8;
294 // SAFETY: It is safe as both raw pointers `flags_addr` and `vbmeta_header` are not null.
295 let flags_offset = unsafe { flags_addr.offset_from(ptr::addr_of!(vbmeta_header) as *const u8) };
296 let flags_offset = usize::try_from(footer.vbmeta_offset)? + usize::try_from(flags_offset)?;
297
298 // Act.
299 kernel[flags_offset..(flags_offset + size_of::<u32>())]
300 .copy_from_slice(&AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED.to_be_bytes());
301
302 // Assert.
Inseob Kim8ebf1da2023-01-27 18:12:57 +0900303 let vbmeta_header = extract_vbmeta_header(&kernel, &footer)?;
304 // vbmeta_header is unaligned; copy flags to local variable
305 let vbmeta_header_flags = vbmeta_header.flags;
306 assert_eq!(
307 AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED, vbmeta_header_flags,
308 "VBMeta verification flag should be disabled now."
309 );
Alice Wang1f0add02023-01-23 16:22:53 +0000310 assert_payload_verification_with_initrd_fails(
Alice Wangf06bfd72023-01-19 09:24:21 +0000311 &kernel,
312 &load_latest_initrd_normal()?,
313 &load_trusted_public_key()?,
Alice Wang1f0add02023-01-23 16:22:53 +0000314 AvbSlotVerifyError::Verification,
Alice Wangf06bfd72023-01-19 09:24:21 +0000315 )
316}