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 | 45d98fa | 2023-01-11 09:39:45 +0000 | [diff] [blame] | 17 | use crate::error::{slot_verify_result_to_verify_payload_result, AvbSlotVerifyError}; |
| 18 | use avb_bindgen::{avb_slot_verify, AvbHashtreeErrorMode, AvbIOResult, AvbOps, AvbSlotVerifyFlags}; |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 19 | use core::{ |
| 20 | ffi::{c_char, c_void, CStr}, |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 21 | ptr::{self, NonNull}, |
| 22 | slice, |
| 23 | }; |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 24 | |
Alice Wang | 6b486f1 | 2023-01-06 13:12:16 +0000 | [diff] [blame] | 25 | static NULL_BYTE: &[u8] = b"\0"; |
| 26 | |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 27 | enum AvbIOError { |
| 28 | /// AVB_IO_RESULT_ERROR_OOM, |
| 29 | #[allow(dead_code)] |
| 30 | Oom, |
| 31 | /// AVB_IO_RESULT_ERROR_IO, |
| 32 | #[allow(dead_code)] |
| 33 | Io, |
| 34 | /// AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION, |
| 35 | NoSuchPartition, |
| 36 | /// AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION, |
| 37 | RangeOutsidePartition, |
| 38 | /// AVB_IO_RESULT_ERROR_NO_SUCH_VALUE, |
| 39 | NoSuchValue, |
| 40 | /// AVB_IO_RESULT_ERROR_INVALID_VALUE_SIZE, |
| 41 | InvalidValueSize, |
| 42 | /// AVB_IO_RESULT_ERROR_INSUFFICIENT_SPACE, |
| 43 | #[allow(dead_code)] |
| 44 | InsufficientSpace, |
| 45 | } |
| 46 | |
| 47 | impl From<AvbIOError> for AvbIOResult { |
| 48 | fn from(error: AvbIOError) -> Self { |
| 49 | match error { |
| 50 | AvbIOError::Oom => AvbIOResult::AVB_IO_RESULT_ERROR_OOM, |
| 51 | AvbIOError::Io => AvbIOResult::AVB_IO_RESULT_ERROR_IO, |
| 52 | AvbIOError::NoSuchPartition => AvbIOResult::AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION, |
| 53 | AvbIOError::RangeOutsidePartition => { |
| 54 | AvbIOResult::AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION |
| 55 | } |
| 56 | AvbIOError::NoSuchValue => AvbIOResult::AVB_IO_RESULT_ERROR_NO_SUCH_VALUE, |
| 57 | AvbIOError::InvalidValueSize => AvbIOResult::AVB_IO_RESULT_ERROR_INVALID_VALUE_SIZE, |
| 58 | AvbIOError::InsufficientSpace => AvbIOResult::AVB_IO_RESULT_ERROR_INSUFFICIENT_SPACE, |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | fn to_avb_io_result(result: Result<(), AvbIOError>) -> AvbIOResult { |
| 64 | result.map_or_else(|e| e.into(), |_| AvbIOResult::AVB_IO_RESULT_OK) |
| 65 | } |
| 66 | |
| 67 | extern "C" fn read_is_device_unlocked( |
| 68 | _ops: *mut AvbOps, |
| 69 | out_is_unlocked: *mut bool, |
| 70 | ) -> AvbIOResult { |
Alice Wang | fd21766 | 2023-01-13 12:07:53 +0000 | [diff] [blame^] | 71 | to_avb_io_result(write(out_is_unlocked, false)) |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Alice Wang | 563663d | 2023-01-10 08:22:29 +0000 | [diff] [blame] | 74 | unsafe extern "C" fn get_preloaded_partition( |
| 75 | ops: *mut AvbOps, |
| 76 | partition: *const c_char, |
| 77 | num_bytes: usize, |
| 78 | out_pointer: *mut *mut u8, |
| 79 | out_num_bytes_preloaded: *mut usize, |
| 80 | ) -> AvbIOResult { |
| 81 | to_avb_io_result(try_get_preloaded_partition( |
| 82 | ops, |
| 83 | partition, |
| 84 | num_bytes, |
| 85 | out_pointer, |
| 86 | out_num_bytes_preloaded, |
| 87 | )) |
| 88 | } |
| 89 | |
| 90 | fn try_get_preloaded_partition( |
| 91 | ops: *mut AvbOps, |
| 92 | partition: *const c_char, |
| 93 | num_bytes: usize, |
| 94 | out_pointer: *mut *mut u8, |
| 95 | out_num_bytes_preloaded: *mut usize, |
| 96 | ) -> Result<(), AvbIOError> { |
Alice Wang | f0fe705 | 2023-01-11 13:15:57 +0000 | [diff] [blame] | 97 | let ops = as_ref(ops)?; |
Alice Wang | 563663d | 2023-01-10 08:22:29 +0000 | [diff] [blame] | 98 | let partition = ops.as_ref().get_partition(partition)?; |
Alice Wang | fd21766 | 2023-01-13 12:07:53 +0000 | [diff] [blame^] | 99 | write(out_pointer, partition.as_ptr() as *mut u8)?; |
| 100 | write(out_num_bytes_preloaded, partition.len().min(num_bytes)) |
Alice Wang | 563663d | 2023-01-10 08:22:29 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 103 | extern "C" fn read_from_partition( |
| 104 | ops: *mut AvbOps, |
| 105 | partition: *const c_char, |
| 106 | offset: i64, |
| 107 | num_bytes: usize, |
| 108 | buffer: *mut c_void, |
| 109 | out_num_read: *mut usize, |
| 110 | ) -> AvbIOResult { |
| 111 | to_avb_io_result(try_read_from_partition( |
| 112 | ops, |
| 113 | partition, |
| 114 | offset, |
| 115 | num_bytes, |
| 116 | buffer, |
| 117 | out_num_read, |
| 118 | )) |
| 119 | } |
| 120 | |
| 121 | fn try_read_from_partition( |
| 122 | ops: *mut AvbOps, |
| 123 | partition: *const c_char, |
| 124 | offset: i64, |
| 125 | num_bytes: usize, |
| 126 | buffer: *mut c_void, |
| 127 | out_num_read: *mut usize, |
| 128 | ) -> Result<(), AvbIOError> { |
Alice Wang | f0fe705 | 2023-01-11 13:15:57 +0000 | [diff] [blame] | 129 | let ops = as_ref(ops)?; |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 130 | let partition = ops.as_ref().get_partition(partition)?; |
| 131 | let buffer = to_nonnull(buffer)?; |
| 132 | // SAFETY: It is safe to copy the requested number of bytes to `buffer` as `buffer` |
| 133 | // is created to point to the `num_bytes` of bytes in memory. |
| 134 | let buffer_slice = unsafe { slice::from_raw_parts_mut(buffer.as_ptr() as *mut u8, num_bytes) }; |
| 135 | copy_data_to_dst(partition, offset, buffer_slice)?; |
Alice Wang | fd21766 | 2023-01-13 12:07:53 +0000 | [diff] [blame^] | 136 | write(out_num_read, buffer_slice.len()) |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | fn copy_data_to_dst(src: &[u8], offset: i64, dst: &mut [u8]) -> Result<(), AvbIOError> { |
| 140 | let start = to_copy_start(offset, src.len()).ok_or(AvbIOError::InvalidValueSize)?; |
| 141 | let end = start.checked_add(dst.len()).ok_or(AvbIOError::InvalidValueSize)?; |
| 142 | dst.copy_from_slice(src.get(start..end).ok_or(AvbIOError::RangeOutsidePartition)?); |
| 143 | Ok(()) |
| 144 | } |
| 145 | |
| 146 | fn to_copy_start(offset: i64, len: usize) -> Option<usize> { |
| 147 | usize::try_from(offset) |
| 148 | .ok() |
| 149 | .or_else(|| isize::try_from(offset).ok().and_then(|v| len.checked_add_signed(v))) |
| 150 | } |
| 151 | |
| 152 | extern "C" fn get_size_of_partition( |
| 153 | ops: *mut AvbOps, |
| 154 | partition: *const c_char, |
| 155 | out_size_num_bytes: *mut u64, |
| 156 | ) -> AvbIOResult { |
| 157 | to_avb_io_result(try_get_size_of_partition(ops, partition, out_size_num_bytes)) |
| 158 | } |
| 159 | |
| 160 | fn try_get_size_of_partition( |
| 161 | ops: *mut AvbOps, |
| 162 | partition: *const c_char, |
| 163 | out_size_num_bytes: *mut u64, |
| 164 | ) -> Result<(), AvbIOError> { |
Alice Wang | f0fe705 | 2023-01-11 13:15:57 +0000 | [diff] [blame] | 165 | let ops = as_ref(ops)?; |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 166 | let partition = ops.as_ref().get_partition(partition)?; |
| 167 | let partition_size = |
| 168 | u64::try_from(partition.len()).map_err(|_| AvbIOError::InvalidValueSize)?; |
Alice Wang | fd21766 | 2023-01-13 12:07:53 +0000 | [diff] [blame^] | 169 | write(out_size_num_bytes, partition_size) |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | extern "C" fn read_rollback_index( |
| 173 | _ops: *mut AvbOps, |
| 174 | _rollback_index_location: usize, |
| 175 | _out_rollback_index: *mut u64, |
| 176 | ) -> AvbIOResult { |
| 177 | // Rollback protection is not yet implemented, but |
| 178 | // this method is required by `avb_slot_verify()`. |
| 179 | AvbIOResult::AVB_IO_RESULT_OK |
| 180 | } |
| 181 | |
| 182 | extern "C" fn get_unique_guid_for_partition( |
| 183 | _ops: *mut AvbOps, |
| 184 | _partition: *const c_char, |
| 185 | _guid_buf: *mut c_char, |
| 186 | _guid_buf_size: usize, |
| 187 | ) -> AvbIOResult { |
| 188 | // This method is required by `avb_slot_verify()`. |
| 189 | AvbIOResult::AVB_IO_RESULT_OK |
| 190 | } |
| 191 | |
| 192 | extern "C" fn validate_public_key_for_partition( |
| 193 | ops: *mut AvbOps, |
| 194 | partition: *const c_char, |
| 195 | public_key_data: *const u8, |
| 196 | public_key_length: usize, |
| 197 | public_key_metadata: *const u8, |
| 198 | public_key_metadata_length: usize, |
| 199 | out_is_trusted: *mut bool, |
| 200 | out_rollback_index_location: *mut u32, |
| 201 | ) -> AvbIOResult { |
| 202 | to_avb_io_result(try_validate_public_key_for_partition( |
| 203 | ops, |
| 204 | partition, |
| 205 | public_key_data, |
| 206 | public_key_length, |
| 207 | public_key_metadata, |
| 208 | public_key_metadata_length, |
| 209 | out_is_trusted, |
| 210 | out_rollback_index_location, |
| 211 | )) |
| 212 | } |
| 213 | |
| 214 | #[allow(clippy::too_many_arguments)] |
| 215 | fn try_validate_public_key_for_partition( |
| 216 | ops: *mut AvbOps, |
| 217 | partition: *const c_char, |
| 218 | public_key_data: *const u8, |
| 219 | public_key_length: usize, |
| 220 | _public_key_metadata: *const u8, |
| 221 | _public_key_metadata_length: usize, |
| 222 | out_is_trusted: *mut bool, |
| 223 | _out_rollback_index_location: *mut u32, |
| 224 | ) -> Result<(), AvbIOError> { |
| 225 | is_not_null(public_key_data)?; |
| 226 | // SAFETY: It is safe to create a slice with the given pointer and length as |
| 227 | // `public_key_data` is a valid pointer and it points to an array of length |
| 228 | // `public_key_length`. |
| 229 | let public_key = unsafe { slice::from_raw_parts(public_key_data, public_key_length) }; |
Alice Wang | f0fe705 | 2023-01-11 13:15:57 +0000 | [diff] [blame] | 230 | let ops = as_ref(ops)?; |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 231 | // Verifies the public key for the known partitions only. |
| 232 | ops.as_ref().get_partition(partition)?; |
| 233 | let trusted_public_key = ops.as_ref().trusted_public_key; |
Alice Wang | fd21766 | 2023-01-13 12:07:53 +0000 | [diff] [blame^] | 234 | write(out_is_trusted, public_key == trusted_public_key) |
| 235 | } |
| 236 | |
| 237 | fn write<T>(ptr: *mut T, value: T) -> Result<(), AvbIOError> { |
| 238 | let ptr = to_nonnull(ptr)?; |
| 239 | // SAFETY: It is safe as the raw pointer `ptr` is a nonnull pointer. |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 240 | unsafe { |
Alice Wang | fd21766 | 2023-01-13 12:07:53 +0000 | [diff] [blame^] | 241 | *ptr.as_ptr() = value; |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 242 | } |
| 243 | Ok(()) |
| 244 | } |
| 245 | |
Alice Wang | f0fe705 | 2023-01-11 13:15:57 +0000 | [diff] [blame] | 246 | fn as_ref<'a, T>(ptr: *mut T) -> Result<&'a T, AvbIOError> { |
| 247 | let ptr = to_nonnull(ptr)?; |
| 248 | // SAFETY: It is safe as the raw pointer `ptr` is a nonnull pointer. |
| 249 | unsafe { Ok(ptr.as_ref()) } |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Alice Wang | f0fe705 | 2023-01-11 13:15:57 +0000 | [diff] [blame] | 252 | fn to_nonnull<T>(ptr: *mut T) -> Result<NonNull<T>, AvbIOError> { |
| 253 | NonNull::new(ptr).ok_or(AvbIOError::NoSuchValue) |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | fn is_not_null<T>(ptr: *const T) -> Result<(), AvbIOError> { |
| 257 | if ptr.is_null() { |
| 258 | Err(AvbIOError::NoSuchValue) |
| 259 | } else { |
| 260 | Ok(()) |
| 261 | } |
| 262 | } |
| 263 | |
Alice Wang | 951de3a | 2023-01-11 14:13:29 +0000 | [diff] [blame] | 264 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 265 | enum PartitionName { |
| 266 | Kernel, |
| 267 | InitrdNormal, |
| 268 | InitrdDebug, |
| 269 | } |
| 270 | |
| 271 | impl PartitionName { |
Alice Wang | 8aa3cb1 | 2023-01-11 09:04:04 +0000 | [diff] [blame] | 272 | const KERNEL_PARTITION_NAME: &[u8] = b"boot\0"; |
Alice Wang | 951de3a | 2023-01-11 14:13:29 +0000 | [diff] [blame] | 273 | const INITRD_NORMAL_PARTITION_NAME: &[u8] = b"initrd_normal\0"; |
| 274 | const INITRD_DEBUG_PARTITION_NAME: &[u8] = b"initrd_debug\0"; |
| 275 | |
| 276 | fn as_cstr(&self) -> &CStr { |
| 277 | let partition_name = match self { |
| 278 | Self::Kernel => Self::KERNEL_PARTITION_NAME, |
| 279 | Self::InitrdNormal => Self::INITRD_NORMAL_PARTITION_NAME, |
| 280 | Self::InitrdDebug => Self::INITRD_DEBUG_PARTITION_NAME, |
| 281 | }; |
| 282 | CStr::from_bytes_with_nul(partition_name).unwrap() |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | impl TryFrom<&CStr> for PartitionName { |
| 287 | type Error = AvbIOError; |
| 288 | |
| 289 | fn try_from(partition_name: &CStr) -> Result<Self, Self::Error> { |
| 290 | match partition_name.to_bytes_with_nul() { |
| 291 | Self::KERNEL_PARTITION_NAME => Ok(Self::Kernel), |
| 292 | Self::INITRD_NORMAL_PARTITION_NAME => Ok(Self::InitrdNormal), |
| 293 | Self::INITRD_DEBUG_PARTITION_NAME => Ok(Self::InitrdDebug), |
| 294 | _ => Err(AvbIOError::NoSuchPartition), |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 299 | struct Payload<'a> { |
| 300 | kernel: &'a [u8], |
Alice Wang | 6b486f1 | 2023-01-06 13:12:16 +0000 | [diff] [blame] | 301 | initrd: Option<&'a [u8]>, |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 302 | trusted_public_key: &'a [u8], |
| 303 | } |
| 304 | |
| 305 | impl<'a> AsRef<Payload<'a>> for AvbOps { |
| 306 | fn as_ref(&self) -> &Payload<'a> { |
| 307 | let payload = self.user_data as *const Payload; |
| 308 | // SAFETY: It is safe to cast the `AvbOps.user_data` to Payload as we have saved a |
| 309 | // pointer to a valid value of Payload in user_data when creating AvbOps, and |
| 310 | // assume that the Payload isn't used beyond the lifetime of the AvbOps that it |
| 311 | // belongs to. |
| 312 | unsafe { &*payload } |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | impl<'a> Payload<'a> { |
Alice Wang | 6b486f1 | 2023-01-06 13:12:16 +0000 | [diff] [blame] | 317 | const MAX_NUM_OF_HASH_DESCRIPTORS: usize = 3; |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 318 | |
| 319 | fn get_partition(&self, partition_name: *const c_char) -> Result<&[u8], AvbIOError> { |
| 320 | is_not_null(partition_name)?; |
| 321 | // SAFETY: It is safe as the raw pointer `partition_name` is a nonnull pointer. |
| 322 | let partition_name = unsafe { CStr::from_ptr(partition_name) }; |
Alice Wang | 951de3a | 2023-01-11 14:13:29 +0000 | [diff] [blame] | 323 | match partition_name.try_into()? { |
| 324 | PartitionName::Kernel => Ok(self.kernel), |
| 325 | PartitionName::InitrdNormal | PartitionName::InitrdDebug => { |
Alice Wang | 6b486f1 | 2023-01-06 13:12:16 +0000 | [diff] [blame] | 326 | self.initrd.ok_or(AvbIOError::NoSuchPartition) |
| 327 | } |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 328 | } |
| 329 | } |
Alice Wang | 6b486f1 | 2023-01-06 13:12:16 +0000 | [diff] [blame] | 330 | |
Alice Wang | 45d98fa | 2023-01-11 09:39:45 +0000 | [diff] [blame] | 331 | fn verify_partitions(&mut self, partition_names: &[&CStr]) -> Result<(), AvbSlotVerifyError> { |
Alice Wang | 6b486f1 | 2023-01-06 13:12:16 +0000 | [diff] [blame] | 332 | if partition_names.len() > Self::MAX_NUM_OF_HASH_DESCRIPTORS { |
Alice Wang | 45d98fa | 2023-01-11 09:39:45 +0000 | [diff] [blame] | 333 | return Err(AvbSlotVerifyError::InvalidArgument); |
Alice Wang | 6b486f1 | 2023-01-06 13:12:16 +0000 | [diff] [blame] | 334 | } |
| 335 | let mut requested_partitions = [ptr::null(); Self::MAX_NUM_OF_HASH_DESCRIPTORS + 1]; |
| 336 | partition_names |
| 337 | .iter() |
| 338 | .enumerate() |
| 339 | .for_each(|(i, name)| requested_partitions[i] = name.as_ptr()); |
| 340 | |
| 341 | let mut avb_ops = AvbOps { |
| 342 | user_data: self as *mut _ as *mut c_void, |
| 343 | ab_ops: ptr::null_mut(), |
| 344 | atx_ops: ptr::null_mut(), |
| 345 | read_from_partition: Some(read_from_partition), |
| 346 | get_preloaded_partition: Some(get_preloaded_partition), |
| 347 | write_to_partition: None, |
| 348 | validate_vbmeta_public_key: None, |
| 349 | read_rollback_index: Some(read_rollback_index), |
| 350 | write_rollback_index: None, |
| 351 | read_is_device_unlocked: Some(read_is_device_unlocked), |
| 352 | get_unique_guid_for_partition: Some(get_unique_guid_for_partition), |
| 353 | get_size_of_partition: Some(get_size_of_partition), |
| 354 | read_persistent_value: None, |
| 355 | write_persistent_value: None, |
| 356 | validate_public_key_for_partition: Some(validate_public_key_for_partition), |
| 357 | }; |
| 358 | let ab_suffix = CStr::from_bytes_with_nul(NULL_BYTE).unwrap(); |
| 359 | let out_data = ptr::null_mut(); |
| 360 | // SAFETY: It is safe to call `avb_slot_verify()` as the pointer arguments (`ops`, |
| 361 | // `requested_partitions` and `ab_suffix`) passed to the method are all valid and |
| 362 | // initialized. The last argument `out_data` is allowed to be null so that nothing |
| 363 | // will be written to it. |
| 364 | let result = unsafe { |
| 365 | avb_slot_verify( |
| 366 | &mut avb_ops, |
| 367 | requested_partitions.as_ptr(), |
| 368 | ab_suffix.as_ptr(), |
| 369 | AvbSlotVerifyFlags::AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION, |
| 370 | AvbHashtreeErrorMode::AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE, |
| 371 | out_data, |
| 372 | ) |
| 373 | }; |
Alice Wang | 45d98fa | 2023-01-11 09:39:45 +0000 | [diff] [blame] | 374 | slot_verify_result_to_verify_payload_result(result) |
Alice Wang | 6b486f1 | 2023-01-06 13:12:16 +0000 | [diff] [blame] | 375 | } |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Alice Wang | f3d96b1 | 2022-12-15 13:10:47 +0000 | [diff] [blame] | 378 | /// Verifies the payload (signed kernel + initrd) against the trusted public key. |
Alice Wang | 6b486f1 | 2023-01-06 13:12:16 +0000 | [diff] [blame] | 379 | pub fn verify_payload( |
| 380 | kernel: &[u8], |
| 381 | initrd: Option<&[u8]>, |
| 382 | trusted_public_key: &[u8], |
Alice Wang | 45d98fa | 2023-01-11 09:39:45 +0000 | [diff] [blame] | 383 | ) -> Result<(), AvbSlotVerifyError> { |
Alice Wang | 6b486f1 | 2023-01-06 13:12:16 +0000 | [diff] [blame] | 384 | let mut payload = Payload { kernel, initrd, trusted_public_key }; |
Alice Wang | 951de3a | 2023-01-11 14:13:29 +0000 | [diff] [blame] | 385 | let requested_partitions = [PartitionName::Kernel.as_cstr()]; |
Alice Wang | 6b486f1 | 2023-01-06 13:12:16 +0000 | [diff] [blame] | 386 | payload.verify_partitions(&requested_partitions) |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Alice Wang | f3d96b1 | 2022-12-15 13:10:47 +0000 | [diff] [blame] | 389 | #[cfg(test)] |
| 390 | mod tests { |
| 391 | use super::*; |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 392 | use anyhow::Result; |
Alice Wang | 2af2fac | 2023-01-05 16:31:04 +0000 | [diff] [blame] | 393 | use avb_bindgen::AvbFooter; |
| 394 | use std::{fs, mem::size_of}; |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 395 | |
Alice Wang | 6b486f1 | 2023-01-06 13:12:16 +0000 | [diff] [blame] | 396 | const MICRODROID_KERNEL_IMG_PATH: &str = "microdroid_kernel"; |
| 397 | const INITRD_NORMAL_IMG_PATH: &str = "microdroid_initrd_normal.img"; |
| 398 | const TEST_IMG_WITH_ONE_HASHDESC_PATH: &str = "test_image_with_one_hashdesc.img"; |
| 399 | const UNSIGNED_TEST_IMG_PATH: &str = "unsigned_test.img"; |
| 400 | |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 401 | const PUBLIC_KEY_RSA2048_PATH: &str = "data/testkey_rsa2048_pub.bin"; |
| 402 | const PUBLIC_KEY_RSA4096_PATH: &str = "data/testkey_rsa4096_pub.bin"; |
Alice Wang | 2af2fac | 2023-01-05 16:31:04 +0000 | [diff] [blame] | 403 | const RANDOM_FOOTER_POS: usize = 30; |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 404 | |
| 405 | /// This test uses the Microdroid payload compiled on the fly to check that |
| 406 | /// the latest payload can be verified successfully. |
Alice Wang | f3d96b1 | 2022-12-15 13:10:47 +0000 | [diff] [blame] | 407 | #[test] |
Alice Wang | 6b486f1 | 2023-01-06 13:12:16 +0000 | [diff] [blame] | 408 | fn latest_valid_payload_passes_verification() -> Result<()> { |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 409 | let kernel = load_latest_signed_kernel()?; |
Alice Wang | 6b486f1 | 2023-01-06 13:12:16 +0000 | [diff] [blame] | 410 | let initrd_normal = fs::read(INITRD_NORMAL_IMG_PATH)?; |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 411 | let public_key = fs::read(PUBLIC_KEY_RSA4096_PATH)?; |
| 412 | |
Alice Wang | 6b486f1 | 2023-01-06 13:12:16 +0000 | [diff] [blame] | 413 | assert_eq!(Ok(()), verify_payload(&kernel, Some(&initrd_normal[..]), &public_key)); |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 414 | Ok(()) |
| 415 | } |
| 416 | |
| 417 | #[test] |
Alice Wang | 6b486f1 | 2023-01-06 13:12:16 +0000 | [diff] [blame] | 418 | fn payload_expecting_no_initrd_passes_verification_with_no_initrd() -> Result<()> { |
| 419 | let kernel = fs::read(TEST_IMG_WITH_ONE_HASHDESC_PATH)?; |
| 420 | let public_key = fs::read(PUBLIC_KEY_RSA4096_PATH)?; |
| 421 | |
| 422 | assert_eq!(Ok(()), verify_payload(&kernel, None, &public_key)); |
| 423 | Ok(()) |
| 424 | } |
| 425 | |
| 426 | // TODO(b/256148034): Test that kernel with two hashdesc and no initrd fails verification. |
| 427 | // e.g. payload_expecting_initrd_fails_verification_with_no_initrd |
| 428 | |
| 429 | #[test] |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 430 | fn payload_with_empty_public_key_fails_verification() -> Result<()> { |
| 431 | assert_payload_verification_fails( |
| 432 | &load_latest_signed_kernel()?, |
Alice Wang | 6b486f1 | 2023-01-06 13:12:16 +0000 | [diff] [blame] | 433 | &load_latest_initrd_normal()?, |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 434 | /*trusted_public_key=*/ &[0u8; 0], |
Alice Wang | 45d98fa | 2023-01-11 09:39:45 +0000 | [diff] [blame] | 435 | AvbSlotVerifyError::PublicKeyRejected, |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 436 | ) |
| 437 | } |
| 438 | |
| 439 | #[test] |
| 440 | fn payload_with_an_invalid_public_key_fails_verification() -> Result<()> { |
| 441 | assert_payload_verification_fails( |
| 442 | &load_latest_signed_kernel()?, |
Alice Wang | 6b486f1 | 2023-01-06 13:12:16 +0000 | [diff] [blame] | 443 | &load_latest_initrd_normal()?, |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 444 | /*trusted_public_key=*/ &[0u8; 512], |
Alice Wang | 45d98fa | 2023-01-11 09:39:45 +0000 | [diff] [blame] | 445 | AvbSlotVerifyError::PublicKeyRejected, |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 446 | ) |
| 447 | } |
| 448 | |
| 449 | #[test] |
| 450 | fn payload_with_a_different_valid_public_key_fails_verification() -> Result<()> { |
| 451 | assert_payload_verification_fails( |
| 452 | &load_latest_signed_kernel()?, |
Alice Wang | 6b486f1 | 2023-01-06 13:12:16 +0000 | [diff] [blame] | 453 | &load_latest_initrd_normal()?, |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 454 | &fs::read(PUBLIC_KEY_RSA2048_PATH)?, |
Alice Wang | 45d98fa | 2023-01-11 09:39:45 +0000 | [diff] [blame] | 455 | AvbSlotVerifyError::PublicKeyRejected, |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 456 | ) |
| 457 | } |
| 458 | |
| 459 | #[test] |
| 460 | fn unsigned_kernel_fails_verification() -> Result<()> { |
| 461 | assert_payload_verification_fails( |
Alice Wang | 6b486f1 | 2023-01-06 13:12:16 +0000 | [diff] [blame] | 462 | &fs::read(UNSIGNED_TEST_IMG_PATH)?, |
| 463 | &load_latest_initrd_normal()?, |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 464 | &fs::read(PUBLIC_KEY_RSA4096_PATH)?, |
Alice Wang | 45d98fa | 2023-01-11 09:39:45 +0000 | [diff] [blame] | 465 | AvbSlotVerifyError::Io, |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 466 | ) |
| 467 | } |
| 468 | |
| 469 | #[test] |
| 470 | fn tampered_kernel_fails_verification() -> Result<()> { |
| 471 | let mut kernel = load_latest_signed_kernel()?; |
| 472 | kernel[1] = !kernel[1]; // Flip the bits |
| 473 | |
| 474 | assert_payload_verification_fails( |
| 475 | &kernel, |
Alice Wang | 6b486f1 | 2023-01-06 13:12:16 +0000 | [diff] [blame] | 476 | &load_latest_initrd_normal()?, |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 477 | &fs::read(PUBLIC_KEY_RSA4096_PATH)?, |
Alice Wang | 45d98fa | 2023-01-11 09:39:45 +0000 | [diff] [blame] | 478 | AvbSlotVerifyError::Verification, |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 479 | ) |
| 480 | } |
| 481 | |
Alice Wang | 2af2fac | 2023-01-05 16:31:04 +0000 | [diff] [blame] | 482 | #[test] |
| 483 | fn tampered_kernel_footer_fails_verification() -> Result<()> { |
| 484 | let mut kernel = load_latest_signed_kernel()?; |
| 485 | let avb_footer_index = kernel.len() - size_of::<AvbFooter>() + RANDOM_FOOTER_POS; |
| 486 | kernel[avb_footer_index] = !kernel[avb_footer_index]; |
| 487 | |
| 488 | assert_payload_verification_fails( |
| 489 | &kernel, |
Alice Wang | 6b486f1 | 2023-01-06 13:12:16 +0000 | [diff] [blame] | 490 | &load_latest_initrd_normal()?, |
Alice Wang | 2af2fac | 2023-01-05 16:31:04 +0000 | [diff] [blame] | 491 | &fs::read(PUBLIC_KEY_RSA4096_PATH)?, |
Alice Wang | 45d98fa | 2023-01-11 09:39:45 +0000 | [diff] [blame] | 492 | AvbSlotVerifyError::InvalidMetadata, |
Alice Wang | 2af2fac | 2023-01-05 16:31:04 +0000 | [diff] [blame] | 493 | ) |
| 494 | } |
| 495 | |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 496 | fn assert_payload_verification_fails( |
| 497 | kernel: &[u8], |
Alice Wang | 6b486f1 | 2023-01-06 13:12:16 +0000 | [diff] [blame] | 498 | initrd: &[u8], |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 499 | trusted_public_key: &[u8], |
Alice Wang | 45d98fa | 2023-01-11 09:39:45 +0000 | [diff] [blame] | 500 | expected_error: AvbSlotVerifyError, |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 501 | ) -> Result<()> { |
Alice Wang | 6b486f1 | 2023-01-06 13:12:16 +0000 | [diff] [blame] | 502 | assert_eq!(Err(expected_error), verify_payload(kernel, Some(initrd), trusted_public_key)); |
Alice Wang | a78279c | 2022-12-16 12:41:19 +0000 | [diff] [blame] | 503 | Ok(()) |
| 504 | } |
| 505 | |
| 506 | fn load_latest_signed_kernel() -> Result<Vec<u8>> { |
Alice Wang | 6b486f1 | 2023-01-06 13:12:16 +0000 | [diff] [blame] | 507 | Ok(fs::read(MICRODROID_KERNEL_IMG_PATH)?) |
| 508 | } |
| 509 | |
| 510 | fn load_latest_initrd_normal() -> Result<Vec<u8>> { |
| 511 | Ok(fs::read(INITRD_NORMAL_IMG_PATH)?) |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 512 | } |
| 513 | } |