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