Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 1 | // Copyright 2023, 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 | |
| 15 | //! Structs and functions relating to `AvbOps`. |
| 16 | |
Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 17 | use crate::partition::PartitionName; |
| 18 | use crate::utils::{self, as_ref, is_not_null, to_nonnull, write}; |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame] | 19 | use avb::internal::{result_to_io_enum, slot_verify_enum_to_result}; |
Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 20 | use avb_bindgen::{ |
| 21 | avb_slot_verify, avb_slot_verify_data_free, AvbHashtreeErrorMode, AvbIOResult, AvbOps, |
Alice Wang | 75d0563 | 2023-01-25 13:31:18 +0000 | [diff] [blame] | 22 | AvbPartitionData, AvbSlotVerifyData, AvbSlotVerifyFlags, AvbVBMetaData, |
Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 23 | }; |
| 24 | use core::{ |
| 25 | ffi::{c_char, c_void, CStr}, |
| 26 | mem::MaybeUninit, |
| 27 | ptr, slice, |
| 28 | }; |
| 29 | |
| 30 | const NULL_BYTE: &[u8] = b"\0"; |
| 31 | |
| 32 | pub(crate) struct Payload<'a> { |
| 33 | kernel: &'a [u8], |
| 34 | initrd: Option<&'a [u8]>, |
| 35 | trusted_public_key: &'a [u8], |
| 36 | } |
| 37 | |
| 38 | impl<'a> AsRef<Payload<'a>> for AvbOps { |
| 39 | fn as_ref(&self) -> &Payload<'a> { |
| 40 | let payload = self.user_data as *const Payload; |
| 41 | // SAFETY: It is safe to cast the `AvbOps.user_data` to Payload as we have saved a |
| 42 | // pointer to a valid value of Payload in user_data when creating AvbOps. |
| 43 | unsafe { &*payload } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | impl<'a> Payload<'a> { |
| 48 | pub(crate) fn new( |
| 49 | kernel: &'a [u8], |
| 50 | initrd: Option<&'a [u8]>, |
| 51 | trusted_public_key: &'a [u8], |
| 52 | ) -> Self { |
| 53 | Self { kernel, initrd, trusted_public_key } |
| 54 | } |
| 55 | |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame] | 56 | fn get_partition(&self, partition_name: *const c_char) -> Result<&[u8], avb::IoError> { |
Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 57 | match partition_name.try_into()? { |
| 58 | PartitionName::Kernel => Ok(self.kernel), |
| 59 | PartitionName::InitrdNormal | PartitionName::InitrdDebug => { |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame] | 60 | self.initrd.ok_or(avb::IoError::NoSuchPartition) |
Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /// `Ops` wraps the class `AvbOps` in libavb. It provides pvmfw customized |
| 67 | /// operations used in the verification. |
| 68 | pub(crate) struct Ops(AvbOps); |
| 69 | |
| 70 | impl<'a> From<&mut Payload<'a>> for Ops { |
| 71 | fn from(payload: &mut Payload<'a>) -> Self { |
| 72 | let avb_ops = AvbOps { |
| 73 | user_data: payload as *mut _ as *mut c_void, |
| 74 | ab_ops: ptr::null_mut(), |
| 75 | atx_ops: ptr::null_mut(), |
| 76 | read_from_partition: Some(read_from_partition), |
| 77 | get_preloaded_partition: Some(get_preloaded_partition), |
| 78 | write_to_partition: None, |
| 79 | validate_vbmeta_public_key: Some(validate_vbmeta_public_key), |
| 80 | read_rollback_index: Some(read_rollback_index), |
| 81 | write_rollback_index: None, |
| 82 | read_is_device_unlocked: Some(read_is_device_unlocked), |
| 83 | get_unique_guid_for_partition: Some(get_unique_guid_for_partition), |
| 84 | get_size_of_partition: Some(get_size_of_partition), |
| 85 | read_persistent_value: None, |
| 86 | write_persistent_value: None, |
| 87 | validate_public_key_for_partition: None, |
| 88 | }; |
| 89 | Self(avb_ops) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | impl Ops { |
| 94 | pub(crate) fn verify_partition( |
| 95 | &mut self, |
| 96 | partition_name: &CStr, |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame] | 97 | ) -> Result<AvbSlotVerifyDataWrap, avb::SlotVerifyError> { |
Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 98 | let requested_partitions = [partition_name.as_ptr(), ptr::null()]; |
| 99 | let ab_suffix = CStr::from_bytes_with_nul(NULL_BYTE).unwrap(); |
| 100 | let mut out_data = MaybeUninit::uninit(); |
| 101 | // SAFETY: It is safe to call `avb_slot_verify()` as the pointer arguments (`ops`, |
| 102 | // `requested_partitions` and `ab_suffix`) passed to the method are all valid and |
| 103 | // initialized. |
| 104 | let result = unsafe { |
| 105 | avb_slot_verify( |
| 106 | &mut self.0, |
| 107 | requested_partitions.as_ptr(), |
| 108 | ab_suffix.as_ptr(), |
| 109 | AvbSlotVerifyFlags::AVB_SLOT_VERIFY_FLAGS_NONE, |
| 110 | AvbHashtreeErrorMode::AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE, |
| 111 | out_data.as_mut_ptr(), |
| 112 | ) |
| 113 | }; |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame] | 114 | slot_verify_enum_to_result(result)?; |
Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 115 | // SAFETY: This is safe because `out_data` has been properly initialized after |
| 116 | // calling `avb_slot_verify` and it returns OK. |
| 117 | let out_data = unsafe { out_data.assume_init() }; |
| 118 | out_data.try_into() |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | extern "C" fn read_is_device_unlocked( |
| 123 | _ops: *mut AvbOps, |
| 124 | out_is_unlocked: *mut bool, |
| 125 | ) -> AvbIOResult { |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame] | 126 | result_to_io_enum(write(out_is_unlocked, false)) |
Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | extern "C" fn get_preloaded_partition( |
| 130 | ops: *mut AvbOps, |
| 131 | partition: *const c_char, |
| 132 | num_bytes: usize, |
| 133 | out_pointer: *mut *mut u8, |
| 134 | out_num_bytes_preloaded: *mut usize, |
| 135 | ) -> AvbIOResult { |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame] | 136 | result_to_io_enum(try_get_preloaded_partition( |
Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 137 | ops, |
| 138 | partition, |
| 139 | num_bytes, |
| 140 | out_pointer, |
| 141 | out_num_bytes_preloaded, |
| 142 | )) |
| 143 | } |
| 144 | |
| 145 | fn try_get_preloaded_partition( |
| 146 | ops: *mut AvbOps, |
| 147 | partition: *const c_char, |
| 148 | num_bytes: usize, |
| 149 | out_pointer: *mut *mut u8, |
| 150 | out_num_bytes_preloaded: *mut usize, |
| 151 | ) -> utils::Result<()> { |
| 152 | let ops = as_ref(ops)?; |
| 153 | let partition = ops.as_ref().get_partition(partition)?; |
| 154 | write(out_pointer, partition.as_ptr() as *mut u8)?; |
| 155 | write(out_num_bytes_preloaded, partition.len().min(num_bytes)) |
| 156 | } |
| 157 | |
| 158 | extern "C" fn read_from_partition( |
| 159 | ops: *mut AvbOps, |
| 160 | partition: *const c_char, |
| 161 | offset: i64, |
| 162 | num_bytes: usize, |
| 163 | buffer: *mut c_void, |
| 164 | out_num_read: *mut usize, |
| 165 | ) -> AvbIOResult { |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame] | 166 | result_to_io_enum(try_read_from_partition( |
Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 167 | ops, |
| 168 | partition, |
| 169 | offset, |
| 170 | num_bytes, |
| 171 | buffer, |
| 172 | out_num_read, |
| 173 | )) |
| 174 | } |
| 175 | |
| 176 | fn try_read_from_partition( |
| 177 | ops: *mut AvbOps, |
| 178 | partition: *const c_char, |
| 179 | offset: i64, |
| 180 | num_bytes: usize, |
| 181 | buffer: *mut c_void, |
| 182 | out_num_read: *mut usize, |
| 183 | ) -> utils::Result<()> { |
| 184 | let ops = as_ref(ops)?; |
| 185 | let partition = ops.as_ref().get_partition(partition)?; |
| 186 | let buffer = to_nonnull(buffer)?; |
| 187 | // SAFETY: It is safe to copy the requested number of bytes to `buffer` as `buffer` |
| 188 | // is created to point to the `num_bytes` of bytes in memory. |
| 189 | let buffer_slice = unsafe { slice::from_raw_parts_mut(buffer.as_ptr() as *mut u8, num_bytes) }; |
| 190 | copy_data_to_dst(partition, offset, buffer_slice)?; |
| 191 | write(out_num_read, buffer_slice.len()) |
| 192 | } |
| 193 | |
| 194 | fn copy_data_to_dst(src: &[u8], offset: i64, dst: &mut [u8]) -> utils::Result<()> { |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame] | 195 | let start = to_copy_start(offset, src.len()).ok_or(avb::IoError::InvalidValueSize)?; |
| 196 | let end = start.checked_add(dst.len()).ok_or(avb::IoError::InvalidValueSize)?; |
| 197 | dst.copy_from_slice(src.get(start..end).ok_or(avb::IoError::RangeOutsidePartition)?); |
Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 198 | Ok(()) |
| 199 | } |
| 200 | |
| 201 | fn to_copy_start(offset: i64, len: usize) -> Option<usize> { |
| 202 | usize::try_from(offset) |
| 203 | .ok() |
| 204 | .or_else(|| isize::try_from(offset).ok().and_then(|v| len.checked_add_signed(v))) |
| 205 | } |
| 206 | |
| 207 | extern "C" fn get_size_of_partition( |
| 208 | ops: *mut AvbOps, |
| 209 | partition: *const c_char, |
| 210 | out_size_num_bytes: *mut u64, |
| 211 | ) -> AvbIOResult { |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame] | 212 | result_to_io_enum(try_get_size_of_partition(ops, partition, out_size_num_bytes)) |
Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | fn try_get_size_of_partition( |
| 216 | ops: *mut AvbOps, |
| 217 | partition: *const c_char, |
| 218 | out_size_num_bytes: *mut u64, |
| 219 | ) -> utils::Result<()> { |
| 220 | let ops = as_ref(ops)?; |
| 221 | let partition = ops.as_ref().get_partition(partition)?; |
| 222 | let partition_size = |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame] | 223 | u64::try_from(partition.len()).map_err(|_| avb::IoError::InvalidValueSize)?; |
Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 224 | write(out_size_num_bytes, partition_size) |
| 225 | } |
| 226 | |
| 227 | extern "C" fn read_rollback_index( |
| 228 | _ops: *mut AvbOps, |
| 229 | _rollback_index_location: usize, |
Alice Wang | 209f2c5 | 2023-01-25 10:33:13 +0000 | [diff] [blame] | 230 | out_rollback_index: *mut u64, |
Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 231 | ) -> AvbIOResult { |
Shikha Panwar | a26f16a | 2023-09-27 09:39:00 +0000 | [diff] [blame] | 232 | // This method is used by `avb_slot_verify()` to read the stored_rollback_index at |
| 233 | // rollback_index_location. |
| 234 | |
| 235 | // TODO(291213394) : Refine this comment once capability for rollback protection is defined. |
| 236 | // pvmfw does not compare stored_rollback_index with rollback_index for Antirollback protection |
| 237 | // Hence, we set `out_rollback_index` to 0 to ensure that the |
| 238 | // rollback_index (including default: 0) is never smaller than it, |
| 239 | // thus the rollback index check will pass. |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame] | 240 | result_to_io_enum(write(out_rollback_index, 0)) |
Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | extern "C" fn get_unique_guid_for_partition( |
| 244 | _ops: *mut AvbOps, |
| 245 | _partition: *const c_char, |
| 246 | _guid_buf: *mut c_char, |
| 247 | _guid_buf_size: usize, |
| 248 | ) -> AvbIOResult { |
| 249 | // TODO(b/256148034): Check if it's possible to throw an error here instead of having |
| 250 | // an empty method. |
| 251 | // This method is required by `avb_slot_verify()`. |
| 252 | AvbIOResult::AVB_IO_RESULT_OK |
| 253 | } |
| 254 | |
| 255 | extern "C" fn validate_vbmeta_public_key( |
| 256 | ops: *mut AvbOps, |
| 257 | public_key_data: *const u8, |
| 258 | public_key_length: usize, |
| 259 | public_key_metadata: *const u8, |
| 260 | public_key_metadata_length: usize, |
| 261 | out_is_trusted: *mut bool, |
| 262 | ) -> AvbIOResult { |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame] | 263 | result_to_io_enum(try_validate_vbmeta_public_key( |
Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 264 | ops, |
| 265 | public_key_data, |
| 266 | public_key_length, |
| 267 | public_key_metadata, |
| 268 | public_key_metadata_length, |
| 269 | out_is_trusted, |
| 270 | )) |
| 271 | } |
| 272 | |
| 273 | fn try_validate_vbmeta_public_key( |
| 274 | ops: *mut AvbOps, |
| 275 | public_key_data: *const u8, |
| 276 | public_key_length: usize, |
| 277 | _public_key_metadata: *const u8, |
| 278 | _public_key_metadata_length: usize, |
| 279 | out_is_trusted: *mut bool, |
| 280 | ) -> utils::Result<()> { |
| 281 | // The public key metadata is not used when we build the VBMeta. |
| 282 | is_not_null(public_key_data)?; |
| 283 | // SAFETY: It is safe to create a slice with the given pointer and length as |
| 284 | // `public_key_data` is a valid pointer and it points to an array of length |
| 285 | // `public_key_length`. |
| 286 | let public_key = unsafe { slice::from_raw_parts(public_key_data, public_key_length) }; |
| 287 | let ops = as_ref(ops)?; |
| 288 | let trusted_public_key = ops.as_ref().trusted_public_key; |
| 289 | write(out_is_trusted, public_key == trusted_public_key) |
| 290 | } |
| 291 | |
| 292 | pub(crate) struct AvbSlotVerifyDataWrap(*mut AvbSlotVerifyData); |
| 293 | |
| 294 | impl TryFrom<*mut AvbSlotVerifyData> for AvbSlotVerifyDataWrap { |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame] | 295 | type Error = avb::SlotVerifyError; |
Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 296 | |
| 297 | fn try_from(data: *mut AvbSlotVerifyData) -> Result<Self, Self::Error> { |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame] | 298 | is_not_null(data).map_err(|_| avb::SlotVerifyError::Io)?; |
Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 299 | Ok(Self(data)) |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | impl Drop for AvbSlotVerifyDataWrap { |
| 304 | fn drop(&mut self) { |
| 305 | // SAFETY: This is safe because `self.0` is checked nonnull when the |
| 306 | // instance is created. We can free this pointer when the instance is |
| 307 | // no longer needed. |
| 308 | unsafe { |
| 309 | avb_slot_verify_data_free(self.0); |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | impl AsRef<AvbSlotVerifyData> for AvbSlotVerifyDataWrap { |
| 315 | fn as_ref(&self) -> &AvbSlotVerifyData { |
| 316 | // This is safe because `self.0` is checked nonnull when the instance is created. |
| 317 | as_ref(self.0).unwrap() |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | impl AvbSlotVerifyDataWrap { |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame] | 322 | pub(crate) fn vbmeta_images(&self) -> Result<&[AvbVBMetaData], avb::SlotVerifyError> { |
Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 323 | let data = self.as_ref(); |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame] | 324 | is_not_null(data.vbmeta_images).map_err(|_| avb::SlotVerifyError::Io)?; |
Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 325 | let vbmeta_images = |
Andrew Walbran | 20bb4e4 | 2023-07-07 13:55:55 +0100 | [diff] [blame] | 326 | // SAFETY: It is safe as the raw pointer `data.vbmeta_images` is a nonnull pointer. |
Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 327 | unsafe { slice::from_raw_parts(data.vbmeta_images, data.num_vbmeta_images) }; |
| 328 | Ok(vbmeta_images) |
| 329 | } |
Alice Wang | 75d0563 | 2023-01-25 13:31:18 +0000 | [diff] [blame] | 330 | |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame] | 331 | pub(crate) fn loaded_partitions(&self) -> Result<&[AvbPartitionData], avb::SlotVerifyError> { |
Alice Wang | 75d0563 | 2023-01-25 13:31:18 +0000 | [diff] [blame] | 332 | let data = self.as_ref(); |
David Pursell | a7c727b | 2023-08-14 16:24:40 -0700 | [diff] [blame] | 333 | is_not_null(data.loaded_partitions).map_err(|_| avb::SlotVerifyError::Io)?; |
Andrew Walbran | 20bb4e4 | 2023-07-07 13:55:55 +0100 | [diff] [blame] | 334 | let loaded_partitions = |
Alice Wang | 75d0563 | 2023-01-25 13:31:18 +0000 | [diff] [blame] | 335 | // SAFETY: It is safe as the raw pointer `data.loaded_partitions` is a nonnull pointer and |
| 336 | // is guaranteed by libavb to point to a valid `AvbPartitionData` array as part of the |
| 337 | // `AvbSlotVerifyData` struct. |
Alice Wang | 75d0563 | 2023-01-25 13:31:18 +0000 | [diff] [blame] | 338 | unsafe { slice::from_raw_parts(data.loaded_partitions, data.num_loaded_partitions) }; |
| 339 | Ok(loaded_partitions) |
| 340 | } |
Shikha Panwar | a26f16a | 2023-09-27 09:39:00 +0000 | [diff] [blame] | 341 | |
| 342 | pub(crate) fn rollback_indexes(&self) -> &[u64] { |
| 343 | &self.as_ref().rollback_indexes |
| 344 | } |
Alice Wang | 167ab3f | 2023-01-23 13:39:25 +0000 | [diff] [blame] | 345 | } |