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