1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Permission is hereby granted, free of charge, to any person 5 * obtaining a copy of this software and associated documentation 6 * files (the "Software"), to deal in the Software without 7 * restriction, including without limitation the rights to use, copy, 8 * modify, merge, publish, distribute, sublicense, and/or sell copies 9 * of the Software, and to permit persons to whom the Software is 10 * furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be 13 * included in all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 * SOFTWARE. 23 */ 24 #include <common.h> 25 #include <sysmem.h> 26 #include <android_avb/avb_slot_verify.h> 27 #include <android_avb/avb_chain_partition_descriptor.h> 28 #include <android_avb/avb_cmdline.h> 29 #include <android_avb/avb_footer.h> 30 #include <android_avb/avb_hash_descriptor.h> 31 #include <android_avb/avb_hashtree_descriptor.h> 32 #include <android_avb/avb_kernel_cmdline_descriptor.h> 33 #include <android_avb/avb_sha.h> 34 #include <android_avb/avb_util.h> 35 #include <android_avb/avb_vbmeta_image.h> 36 #include <android_avb/avb_version.h> 37 38 /* Maximum number of partitions that can be loaded with avb_slot_verify(). */ 39 #define MAX_NUMBER_OF_LOADED_PARTITIONS 32 40 41 /* Maximum number of vbmeta images that can be loaded with avb_slot_verify(). */ 42 #define MAX_NUMBER_OF_VBMETA_IMAGES 32 43 44 /* Maximum size of a vbmeta image - 64 KiB. */ 45 #define VBMETA_MAX_SIZE (64 * 1024) 46 47 static AvbSlotVerifyResult initialize_persistent_digest( 48 AvbOps* ops, 49 const char* part_name, 50 const char* persistent_value_name, 51 size_t digest_size, 52 const uint8_t* initial_digest, 53 uint8_t* out_digest); 54 55 /* Helper function to see if we should continue with verification in 56 * allow_verification_error=true mode if something goes wrong. See the 57 * comments for the avb_slot_verify() function for more information. 58 */ 59 static inline bool result_should_continue(AvbSlotVerifyResult result) { 60 switch (result) { 61 case AVB_SLOT_VERIFY_RESULT_ERROR_OOM: 62 case AVB_SLOT_VERIFY_RESULT_ERROR_IO: 63 case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA: 64 case AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION: 65 case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT: 66 return false; 67 68 case AVB_SLOT_VERIFY_RESULT_OK: 69 case AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION: 70 case AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX: 71 case AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED: 72 return true; 73 } 74 75 return false; 76 } 77 78 static AvbSlotVerifyResult load_full_partition(AvbOps* ops, 79 const char* part_name, 80 uint64_t image_size, 81 uint8_t** out_image_buf, 82 bool* out_image_preloaded) { 83 size_t part_num_read; 84 AvbIOResult io_ret; 85 86 /* Make sure that we do not overwrite existing data. */ 87 avb_assert(*out_image_buf == NULL); 88 avb_assert(!*out_image_preloaded); 89 90 /* We are going to implicitly cast image_size from uint64_t to size_t in the 91 * following code, so we need to make sure that the cast is safe. */ 92 if (image_size != (size_t)(image_size)) { 93 avb_errorv(part_name, ": Partition size too large to load.\n", NULL); 94 return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA; 95 } 96 97 /* Try use a preloaded one. */ 98 if (ops->get_preloaded_partition != NULL) { 99 io_ret = ops->get_preloaded_partition( 100 ops, part_name, image_size, out_image_buf, &part_num_read); 101 if (io_ret == AVB_IO_RESULT_ERROR_OOM) { 102 return AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 103 } else if (io_ret != AVB_IO_RESULT_OK) { 104 avb_errorv(part_name, ": Error loading data from partition.\n", NULL); 105 return AVB_SLOT_VERIFY_RESULT_ERROR_IO; 106 } 107 108 if (*out_image_buf != NULL) { 109 if (part_num_read != image_size) { 110 avb_errorv(part_name, ": Read incorrect number of bytes.\n", NULL); 111 return AVB_SLOT_VERIFY_RESULT_ERROR_IO; 112 } 113 *out_image_preloaded = true; 114 } 115 } 116 117 /* Allocate and copy the partition. */ 118 if (!*out_image_preloaded) { 119 *out_image_buf = sysmem_alloc(MEMBLK_ID_AVB_ANDROID, image_size); 120 if (*out_image_buf == NULL) { 121 return AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 122 } 123 124 io_ret = ops->read_from_partition(ops, 125 part_name, 126 0 /* offset */, 127 image_size, 128 *out_image_buf, 129 &part_num_read); 130 if (io_ret == AVB_IO_RESULT_ERROR_OOM) { 131 return AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 132 } else if (io_ret != AVB_IO_RESULT_OK) { 133 avb_errorv(part_name, ": Error loading data from partition.\n", NULL); 134 return AVB_SLOT_VERIFY_RESULT_ERROR_IO; 135 } 136 if (part_num_read != image_size) { 137 avb_errorv(part_name, ": Read incorrect number of bytes.\n", NULL); 138 return AVB_SLOT_VERIFY_RESULT_ERROR_IO; 139 } 140 } 141 142 return AVB_SLOT_VERIFY_RESULT_OK; 143 } 144 145 /* Reads a persistent digest stored as a named persistent value corresponding to 146 * the given |part_name|. The value is returned in |out_digest| which must point 147 * to |expected_digest_size| bytes. If there is no digest stored for |part_name| 148 * it can be initialized by providing a non-NULL |initial_digest| of length 149 * |expected_digest_size|. This automatic initialization will only occur if the 150 * device is currently locked. The |initial_digest| may be NULL. 151 * 152 * Returns AVB_SLOT_VERIFY_RESULT_OK on success, otherwise returns an 153 * AVB_SLOT_VERIFY_RESULT_ERROR_* error code. 154 * 155 * If the value does not exist, is not supported, or is not populated, and 156 * |initial_digest| is NULL, returns 157 * AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA. If |expected_digest_size| does 158 * not match the stored digest size, also returns 159 * AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA. 160 */ 161 static AvbSlotVerifyResult read_persistent_digest(AvbOps* ops, 162 const char* part_name, 163 size_t expected_digest_size, 164 const uint8_t* initial_digest, 165 uint8_t* out_digest) { 166 char* persistent_value_name = NULL; 167 AvbIOResult io_ret = AVB_IO_RESULT_OK; 168 size_t stored_digest_size = 0; 169 170 if (ops->read_persistent_value == NULL) { 171 avb_errorv(part_name, ": Persistent values are not implemented.\n", NULL); 172 return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA; 173 } 174 persistent_value_name = 175 avb_strdupv(AVB_NPV_PERSISTENT_DIGEST_PREFIX, part_name, NULL); 176 if (persistent_value_name == NULL) { 177 return AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 178 } 179 180 io_ret = ops->read_persistent_value(ops, 181 persistent_value_name, 182 expected_digest_size, 183 out_digest, 184 &stored_digest_size); 185 186 // If no such named persistent value exists and an initial digest value was 187 // given, initialize the named persistent value with the given digest. If 188 // initialized successfully, this will recurse into this function but with a 189 // NULL initial_digest. 190 if (io_ret == AVB_IO_RESULT_ERROR_NO_SUCH_VALUE && initial_digest) { 191 AvbSlotVerifyResult ret = 192 initialize_persistent_digest(ops, 193 part_name, 194 persistent_value_name, 195 expected_digest_size, 196 initial_digest, 197 out_digest); 198 avb_free(persistent_value_name); 199 return ret; 200 } 201 avb_free(persistent_value_name); 202 203 if (io_ret == AVB_IO_RESULT_ERROR_OOM) { 204 return AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 205 } else if (io_ret == AVB_IO_RESULT_ERROR_NO_SUCH_VALUE) { 206 // Treat a missing persistent value as a verification error, which is 207 // ignoreable, rather than a metadata error which is not. 208 avb_errorv(part_name, ": Persistent digest does not exist.\n", NULL); 209 return AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION; 210 } else if (io_ret == AVB_IO_RESULT_ERROR_INVALID_VALUE_SIZE || 211 io_ret == AVB_IO_RESULT_ERROR_INSUFFICIENT_SPACE) { 212 avb_errorv( 213 part_name, ": Persistent digest is not of expected size.\n", NULL); 214 return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA; 215 } else if (io_ret != AVB_IO_RESULT_OK) { 216 avb_errorv(part_name, ": Error reading persistent digest.\n", NULL); 217 return AVB_SLOT_VERIFY_RESULT_ERROR_IO; 218 } else if (expected_digest_size != stored_digest_size) { 219 avb_errorv( 220 part_name, ": Persistent digest is not of expected size.\n", NULL); 221 return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA; 222 } 223 return AVB_SLOT_VERIFY_RESULT_OK; 224 } 225 226 static AvbSlotVerifyResult initialize_persistent_digest( 227 AvbOps* ops, 228 const char* part_name, 229 const char* persistent_value_name, 230 size_t digest_size, 231 const uint8_t* initial_digest, 232 uint8_t* out_digest) { 233 AvbSlotVerifyResult ret; 234 AvbIOResult io_ret = AVB_IO_RESULT_OK; 235 bool is_device_unlocked = true; 236 237 io_ret = ops->read_is_device_unlocked(ops, &is_device_unlocked); 238 if (io_ret == AVB_IO_RESULT_ERROR_OOM) { 239 return AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 240 } else if (io_ret != AVB_IO_RESULT_OK) { 241 avb_error("Error getting device lock state.\n"); 242 return AVB_SLOT_VERIFY_RESULT_ERROR_IO; 243 } 244 245 if (is_device_unlocked) { 246 avb_debugv(part_name, 247 ": Digest does not exist, device unlocked so not initializing " 248 "digest.\n", 249 NULL); 250 return AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION; 251 } 252 253 // Device locked; initialize digest with given initial value. 254 avb_debugv(part_name, 255 ": Digest does not exist, initializing persistent digest.\n", 256 NULL); 257 io_ret = ops->write_persistent_value( 258 ops, persistent_value_name, digest_size, initial_digest); 259 if (io_ret == AVB_IO_RESULT_ERROR_OOM) { 260 return AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 261 } else if (io_ret != AVB_IO_RESULT_OK) { 262 avb_errorv(part_name, ": Error initializing persistent digest.\n", NULL); 263 return AVB_SLOT_VERIFY_RESULT_ERROR_IO; 264 } 265 266 // To ensure that the digest value was written successfully - and avoid a 267 // scenario where the digest is simply 'initialized' on every verify - recurse 268 // into read_persistent_digest to read back the written value. The NULL 269 // initial_digest ensures that this will not recurse again. 270 ret = read_persistent_digest(ops, part_name, digest_size, NULL, out_digest); 271 if (ret != AVB_SLOT_VERIFY_RESULT_OK) { 272 avb_errorv(part_name, 273 ": Reading back initialized persistent digest failed!\n", 274 NULL); 275 } 276 return ret; 277 } 278 279 static AvbSlotVerifyResult load_and_verify_hash_partition( 280 AvbOps* ops, 281 const char* const* requested_partitions, 282 const char* ab_suffix, 283 bool allow_verification_error, 284 const AvbDescriptor* descriptor, 285 AvbSlotVerifyData* slot_data) { 286 AvbHashDescriptor hash_desc; 287 const uint8_t* desc_partition_name = NULL; 288 const uint8_t* desc_salt; 289 const uint8_t* desc_digest; 290 char part_name[AVB_PART_NAME_MAX_SIZE]; 291 AvbSlotVerifyResult ret; 292 AvbIOResult io_ret; 293 uint8_t* image_buf = NULL; 294 bool image_preloaded = false; 295 uint8_t* digest; 296 size_t digest_len; 297 const char* found; 298 uint64_t image_size; 299 size_t expected_digest_len = 0; 300 uint8_t expected_digest_buf[AVB_SHA512_DIGEST_SIZE]; 301 const uint8_t* expected_digest = NULL; 302 303 if (!avb_hash_descriptor_validate_and_byteswap( 304 (const AvbHashDescriptor*)descriptor, &hash_desc)) { 305 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA; 306 goto out; 307 } 308 309 desc_partition_name = 310 ((const uint8_t*)descriptor) + sizeof(AvbHashDescriptor); 311 desc_salt = desc_partition_name + hash_desc.partition_name_len; 312 desc_digest = desc_salt + hash_desc.salt_len; 313 314 if (!avb_validate_utf8(desc_partition_name, hash_desc.partition_name_len)) { 315 avb_error("Partition name is not valid UTF-8.\n"); 316 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA; 317 goto out; 318 } 319 320 /* Don't bother loading or validating unless the partition was 321 * requested in the first place. 322 */ 323 found = avb_strv_find_str(requested_partitions, 324 (const char*)desc_partition_name, 325 hash_desc.partition_name_len); 326 if (found == NULL) { 327 ret = AVB_SLOT_VERIFY_RESULT_OK; 328 goto out; 329 } 330 331 if ((hash_desc.flags & AVB_HASH_DESCRIPTOR_FLAGS_DO_NOT_USE_AB) != 0) { 332 /* No ab_suffix, just copy the partition name as is. */ 333 if (hash_desc.partition_name_len >= AVB_PART_NAME_MAX_SIZE) { 334 avb_error("Partition name does not fit.\n"); 335 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA; 336 goto out; 337 } 338 avb_memcpy(part_name, desc_partition_name, hash_desc.partition_name_len); 339 part_name[hash_desc.partition_name_len] = '\0'; 340 } else if (hash_desc.digest_len == 0 && avb_strlen(ab_suffix) != 0) { 341 /* No ab_suffix allowed for partitions without a digest in the descriptor 342 * because these partitions hold data unique to this device and are not 343 * updated using an A/B scheme. 344 */ 345 avb_error("Cannot use A/B with a persistent digest.\n"); 346 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA; 347 goto out; 348 } else { 349 /* Add ab_suffix to the partition name. */ 350 if (!avb_str_concat(part_name, 351 sizeof part_name, 352 (const char*)desc_partition_name, 353 hash_desc.partition_name_len, 354 ab_suffix, 355 avb_strlen(ab_suffix))) { 356 avb_error("Partition name and suffix does not fit.\n"); 357 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA; 358 goto out; 359 } 360 } 361 362 /* If we're allowing verification errors then hash_desc.image_size 363 * may no longer match what's in the partition... so in this case 364 * just load the entire partition. 365 * 366 * For example, this can happen if a developer does 'fastboot flash 367 * boot /path/to/new/and/bigger/boot.img'. We want this to work 368 * since it's such a common workflow. 369 */ 370 image_size = hash_desc.image_size; 371 if (allow_verification_error) { 372 io_ret = ops->get_size_of_partition(ops, part_name, &image_size); 373 if (io_ret == AVB_IO_RESULT_ERROR_OOM) { 374 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 375 goto out; 376 } else if (io_ret != AVB_IO_RESULT_OK) { 377 avb_errorv(part_name, ": Error determining partition size.\n", NULL); 378 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO; 379 goto out; 380 } 381 avb_debugv(part_name, ": Loading entire partition.\n", NULL); 382 } 383 384 ret = load_full_partition( 385 ops, part_name, image_size, &image_buf, &image_preloaded); 386 if (ret != AVB_SLOT_VERIFY_RESULT_OK) { 387 goto out; 388 } 389 // Although only one of the type might be used, we have to defined the 390 // structure here so that they would live outside the 'if/else' scope to be 391 // used later. 392 AvbSHA256Ctx sha256_ctx; 393 AvbSHA512Ctx sha512_ctx; 394 size_t image_size_to_hash = hash_desc.image_size; 395 // If we allow verification error and the whole partition is smaller than 396 // image size in hash descriptor, we just hash the whole partition. 397 if (image_size_to_hash > image_size) { 398 image_size_to_hash = image_size; 399 } 400 if (avb_strcmp((const char*)hash_desc.hash_algorithm, "sha256") == 0) { 401 avb_sha256_init(&sha256_ctx); 402 avb_sha256_update(&sha256_ctx, desc_salt, hash_desc.salt_len); 403 avb_sha256_update(&sha256_ctx, image_buf, image_size_to_hash); 404 digest = avb_sha256_final(&sha256_ctx); 405 digest_len = AVB_SHA256_DIGEST_SIZE; 406 } else if (avb_strcmp((const char*)hash_desc.hash_algorithm, "sha512") == 0) { 407 avb_sha512_init(&sha512_ctx); 408 avb_sha512_update(&sha512_ctx, desc_salt, hash_desc.salt_len); 409 avb_sha512_update(&sha512_ctx, image_buf, image_size_to_hash); 410 digest = avb_sha512_final(&sha512_ctx); 411 digest_len = AVB_SHA512_DIGEST_SIZE; 412 } else { 413 avb_errorv(part_name, ": Unsupported hash algorithm.\n", NULL); 414 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA; 415 goto out; 416 } 417 418 if (hash_desc.digest_len == 0) { 419 /* Expect a match to a persistent digest. */ 420 avb_debugv(part_name, ": No digest, using persistent digest.\n", NULL); 421 expected_digest_len = digest_len; 422 expected_digest = expected_digest_buf; 423 avb_assert(expected_digest_len <= sizeof(expected_digest_buf)); 424 /* Pass |digest| as the |initial_digest| so devices not yet initialized get 425 * initialized to the current partition digest. 426 */ 427 ret = read_persistent_digest( 428 ops, part_name, digest_len, digest, expected_digest_buf); 429 if (ret != AVB_SLOT_VERIFY_RESULT_OK) { 430 goto out; 431 } 432 } else { 433 /* Expect a match to the digest in the descriptor. */ 434 expected_digest_len = hash_desc.digest_len; 435 expected_digest = desc_digest; 436 } 437 438 if (digest_len != expected_digest_len) { 439 avb_errorv( 440 part_name, ": Digest in descriptor not of expected size.\n", NULL); 441 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA; 442 goto out; 443 } 444 445 if (avb_safe_memcmp(digest, expected_digest, digest_len) != 0) { 446 avb_errorv(part_name, 447 ": Hash of data does not match digest in descriptor.\n", 448 NULL); 449 ret = AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION; 450 goto out; 451 } 452 453 ret = AVB_SLOT_VERIFY_RESULT_OK; 454 455 out: 456 457 /* If it worked and something was loaded, copy to slot_data. */ 458 if ((ret == AVB_SLOT_VERIFY_RESULT_OK || result_should_continue(ret)) && 459 image_buf != NULL) { 460 AvbPartitionData* loaded_partition; 461 if (slot_data->num_loaded_partitions == MAX_NUMBER_OF_LOADED_PARTITIONS) { 462 avb_errorv(part_name, ": Too many loaded partitions.\n", NULL); 463 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 464 goto fail; 465 } 466 loaded_partition = 467 &slot_data->loaded_partitions[slot_data->num_loaded_partitions++]; 468 loaded_partition->partition_name = avb_strdup(found); 469 loaded_partition->data_size = image_size; 470 loaded_partition->data = image_buf; 471 loaded_partition->preloaded = image_preloaded; 472 image_buf = NULL; 473 } 474 475 fail: 476 if (image_buf != NULL && !image_preloaded) { 477 sysmem_free((phys_addr_t)image_buf); 478 } 479 return ret; 480 } 481 482 static AvbSlotVerifyResult load_requested_partitions( 483 AvbOps* ops, 484 const char* const* requested_partitions, 485 const char* ab_suffix, 486 AvbSlotVerifyData* slot_data) { 487 AvbSlotVerifyResult ret; 488 uint8_t* image_buf = NULL; 489 bool image_preloaded = false; 490 size_t n; 491 492 for (n = 0; requested_partitions[n] != NULL; n++) { 493 char part_name[AVB_PART_NAME_MAX_SIZE]; 494 AvbIOResult io_ret; 495 uint64_t image_size; 496 AvbPartitionData* loaded_partition; 497 498 if (!avb_str_concat(part_name, 499 sizeof part_name, 500 requested_partitions[n], 501 avb_strlen(requested_partitions[n]), 502 ab_suffix, 503 avb_strlen(ab_suffix))) { 504 avb_error("Partition name and suffix does not fit.\n"); 505 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA; 506 goto out; 507 } 508 509 io_ret = ops->get_size_of_partition(ops, part_name, &image_size); 510 if (io_ret == AVB_IO_RESULT_ERROR_OOM) { 511 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 512 goto out; 513 } else if (io_ret != AVB_IO_RESULT_OK) { 514 avb_errorv(part_name, ": Error determining partition size.\n", NULL); 515 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO; 516 goto out; 517 } 518 avb_debugv(part_name, ": Loading entire partition.\n", NULL); 519 520 ret = load_full_partition( 521 ops, part_name, image_size, &image_buf, &image_preloaded); 522 if (ret != AVB_SLOT_VERIFY_RESULT_OK) { 523 goto out; 524 } 525 526 /* Move to slot_data. */ 527 if (slot_data->num_loaded_partitions == MAX_NUMBER_OF_LOADED_PARTITIONS) { 528 avb_errorv(part_name, ": Too many loaded partitions.\n", NULL); 529 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 530 goto out; 531 } 532 loaded_partition = 533 &slot_data->loaded_partitions[slot_data->num_loaded_partitions++]; 534 loaded_partition->partition_name = avb_strdup(requested_partitions[n]); 535 if (loaded_partition->partition_name == NULL) { 536 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 537 goto out; 538 } 539 loaded_partition->data_size = image_size; 540 loaded_partition->data = image_buf; /* Transferring the owner. */ 541 loaded_partition->preloaded = image_preloaded; 542 image_buf = NULL; 543 image_preloaded = false; 544 } 545 546 ret = AVB_SLOT_VERIFY_RESULT_OK; 547 548 out: 549 /* Free the current buffer if any. */ 550 if (image_buf != NULL && !image_preloaded) { 551 sysmem_free((phys_addr_t)image_buf); 552 } 553 /* Buffers that are already saved in slot_data will be handled by the caller 554 * even on failure. */ 555 return ret; 556 } 557 558 static AvbSlotVerifyResult load_and_verify_vbmeta( 559 AvbOps* ops, 560 const char* const* requested_partitions, 561 const char* ab_suffix, 562 AvbSlotVerifyFlags flags, 563 bool allow_verification_error, 564 AvbVBMetaImageFlags toplevel_vbmeta_flags, 565 int rollback_index_location, 566 const char* partition_name, 567 size_t partition_name_len, 568 const uint8_t* expected_public_key, 569 size_t expected_public_key_length, 570 AvbSlotVerifyData* slot_data, 571 AvbAlgorithmType* out_algorithm_type, 572 AvbCmdlineSubstList* out_additional_cmdline_subst) { 573 char full_partition_name[AVB_PART_NAME_MAX_SIZE]; 574 AvbSlotVerifyResult ret; 575 AvbIOResult io_ret; 576 uint64_t vbmeta_offset; 577 size_t vbmeta_size; 578 uint8_t* vbmeta_buf = NULL; 579 size_t vbmeta_num_read; 580 AvbVBMetaVerifyResult vbmeta_ret; 581 const uint8_t* pk_data; 582 size_t pk_len; 583 AvbVBMetaImageHeader vbmeta_header; 584 uint64_t stored_rollback_index; 585 const AvbDescriptor** descriptors = NULL; 586 size_t num_descriptors; 587 size_t n; 588 bool is_main_vbmeta; 589 bool look_for_vbmeta_footer; 590 AvbVBMetaData* vbmeta_image_data = NULL; 591 592 ret = AVB_SLOT_VERIFY_RESULT_OK; 593 594 avb_assert(slot_data != NULL); 595 596 /* Since we allow top-level vbmeta in 'boot', use 597 * rollback_index_location to determine whether we're the main 598 * vbmeta struct. 599 */ 600 is_main_vbmeta = false; 601 if (rollback_index_location == 0) { 602 if ((flags & AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION) == 0) { 603 is_main_vbmeta = true; 604 } 605 } 606 607 /* Don't use footers for vbmeta partitions ('vbmeta' or 608 * 'vbmeta_<partition_name>'). 609 */ 610 look_for_vbmeta_footer = true; 611 if (avb_strncmp(partition_name, "vbmeta", avb_strlen("vbmeta")) == 0) { 612 look_for_vbmeta_footer = false; 613 } 614 615 if (!avb_validate_utf8((const uint8_t*)partition_name, partition_name_len)) { 616 avb_error("Partition name is not valid UTF-8.\n"); 617 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA; 618 goto out; 619 } 620 621 /* Construct full partition name e.g. system_a. */ 622 if (!avb_str_concat(full_partition_name, 623 sizeof full_partition_name, 624 partition_name, 625 partition_name_len, 626 ab_suffix, 627 avb_strlen(ab_suffix))) { 628 avb_error("Partition name and suffix does not fit.\n"); 629 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA; 630 goto out; 631 } 632 633 /* If we're loading from the main vbmeta partition, the vbmeta struct is in 634 * the beginning. Otherwise we may have to locate it via a footer... if no 635 * footer is found, we look in the beginning to support e.g. vbmeta_<org> 636 * partitions holding data for e.g. super partitions (b/80195851 for 637 * rationale). 638 */ 639 vbmeta_offset = 0; 640 vbmeta_size = VBMETA_MAX_SIZE; 641 if (look_for_vbmeta_footer) { 642 uint8_t footer_buf[AVB_FOOTER_SIZE]; 643 size_t footer_num_read; 644 AvbFooter footer; 645 646 io_ret = ops->read_from_partition(ops, 647 full_partition_name, 648 -AVB_FOOTER_SIZE, 649 AVB_FOOTER_SIZE, 650 footer_buf, 651 &footer_num_read); 652 if (io_ret == AVB_IO_RESULT_ERROR_OOM) { 653 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 654 goto out; 655 } else if (io_ret != AVB_IO_RESULT_OK) { 656 avb_errorv(full_partition_name, ": Error loading footer.\n", NULL); 657 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO; 658 goto out; 659 } 660 avb_assert(footer_num_read == AVB_FOOTER_SIZE); 661 662 if (!avb_footer_validate_and_byteswap((const AvbFooter*)footer_buf, 663 &footer)) { 664 avb_debugv(full_partition_name, ": No footer detected.\n", NULL); 665 } else { 666 /* Basic footer sanity check since the data is untrusted. */ 667 if (footer.vbmeta_size > VBMETA_MAX_SIZE) { 668 avb_errorv( 669 full_partition_name, ": Invalid vbmeta size in footer.\n", NULL); 670 } else { 671 vbmeta_offset = footer.vbmeta_offset; 672 vbmeta_size = footer.vbmeta_size; 673 } 674 } 675 } 676 677 vbmeta_buf = avb_malloc(vbmeta_size); 678 if (vbmeta_buf == NULL) { 679 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 680 goto out; 681 } 682 683 if (vbmeta_offset != 0) { 684 avb_debugv("Loading vbmeta struct in footer from partition '", 685 full_partition_name, 686 "'.\n", 687 NULL); 688 } else { 689 avb_debugv("Loading vbmeta struct from partition '", 690 full_partition_name, 691 "'.\n", 692 NULL); 693 } 694 695 io_ret = ops->read_from_partition(ops, 696 full_partition_name, 697 vbmeta_offset, 698 vbmeta_size, 699 vbmeta_buf, 700 &vbmeta_num_read); 701 if (io_ret == AVB_IO_RESULT_ERROR_OOM) { 702 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 703 goto out; 704 } else if (io_ret != AVB_IO_RESULT_OK) { 705 /* If we're looking for 'vbmeta' but there is no such partition, 706 * go try to get it from the boot partition instead. 707 */ 708 if (is_main_vbmeta && io_ret == AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION && 709 !look_for_vbmeta_footer) { 710 avb_debugv(full_partition_name, 711 ": No such partition. Trying 'boot' instead.\n", 712 NULL); 713 ret = load_and_verify_vbmeta(ops, 714 requested_partitions, 715 ab_suffix, 716 flags, 717 allow_verification_error, 718 0 /* toplevel_vbmeta_flags */, 719 0 /* rollback_index_location */, 720 "boot", 721 avb_strlen("boot"), 722 NULL /* expected_public_key */, 723 0 /* expected_public_key_length */, 724 slot_data, 725 out_algorithm_type, 726 out_additional_cmdline_subst); 727 goto out; 728 } else { 729 avb_errorv(full_partition_name, ": Error loading vbmeta data.\n", NULL); 730 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO; 731 goto out; 732 } 733 } 734 avb_assert(vbmeta_num_read <= vbmeta_size); 735 736 /* Check if the image is properly signed and get the public key used 737 * to sign the image. 738 */ 739 vbmeta_ret = 740 avb_vbmeta_image_verify(vbmeta_buf, vbmeta_num_read, &pk_data, &pk_len); 741 switch (vbmeta_ret) { 742 case AVB_VBMETA_VERIFY_RESULT_OK: 743 avb_assert(pk_data != NULL && pk_len > 0); 744 break; 745 746 case AVB_VBMETA_VERIFY_RESULT_OK_NOT_SIGNED: 747 case AVB_VBMETA_VERIFY_RESULT_HASH_MISMATCH: 748 case AVB_VBMETA_VERIFY_RESULT_SIGNATURE_MISMATCH: 749 ret = AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION; 750 avb_errorv(full_partition_name, 751 ": Error verifying vbmeta image: ", 752 avb_vbmeta_verify_result_to_string(vbmeta_ret), 753 "\n", 754 NULL); 755 if (!allow_verification_error) { 756 goto out; 757 } 758 break; 759 760 case AVB_VBMETA_VERIFY_RESULT_INVALID_VBMETA_HEADER: 761 /* No way to continue this case. */ 762 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA; 763 avb_errorv(full_partition_name, 764 ": Error verifying vbmeta image: invalid vbmeta header\n", 765 NULL); 766 goto out; 767 768 case AVB_VBMETA_VERIFY_RESULT_UNSUPPORTED_VERSION: 769 /* No way to continue this case. */ 770 ret = AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION; 771 avb_errorv(full_partition_name, 772 ": Error verifying vbmeta image: unsupported AVB version\n", 773 NULL); 774 goto out; 775 } 776 777 /* Byteswap the header. */ 778 avb_vbmeta_image_header_to_host_byte_order((AvbVBMetaImageHeader*)vbmeta_buf, 779 &vbmeta_header); 780 781 /* If we're the toplevel, assign flags so they'll be passed down. */ 782 if (is_main_vbmeta) { 783 toplevel_vbmeta_flags = (AvbVBMetaImageFlags)vbmeta_header.flags; 784 } else { 785 if (vbmeta_header.flags != 0) { 786 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA; 787 avb_errorv(full_partition_name, 788 ": chained vbmeta image has non-zero flags\n", 789 NULL); 790 goto out; 791 } 792 } 793 794 uint32_t rollback_index_location_to_use = rollback_index_location; 795 796 /* Check if key used to make signature matches what is expected. */ 797 if (pk_data != NULL) { 798 if (expected_public_key != NULL) { 799 avb_assert(!is_main_vbmeta); 800 if (expected_public_key_length != pk_len || 801 avb_safe_memcmp(expected_public_key, pk_data, pk_len) != 0) { 802 avb_errorv(full_partition_name, 803 ": Public key used to sign data does not match key in chain " 804 "partition descriptor.\n", 805 NULL); 806 ret = AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED; 807 if (!allow_verification_error) { 808 goto out; 809 } 810 } 811 } else { 812 bool key_is_trusted = false; 813 const uint8_t* pk_metadata = NULL; 814 size_t pk_metadata_len = 0; 815 816 if (vbmeta_header.public_key_metadata_size > 0) { 817 pk_metadata = vbmeta_buf + sizeof(AvbVBMetaImageHeader) + 818 vbmeta_header.authentication_data_block_size + 819 vbmeta_header.public_key_metadata_offset; 820 pk_metadata_len = vbmeta_header.public_key_metadata_size; 821 } 822 823 // If we're not using a vbmeta partition, need to use another AvbOps... 824 if (flags & AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION) { 825 io_ret = ops->validate_public_key_for_partition( 826 ops, 827 full_partition_name, 828 pk_data, 829 pk_len, 830 pk_metadata, 831 pk_metadata_len, 832 &key_is_trusted, 833 &rollback_index_location_to_use); 834 } else { 835 avb_assert(is_main_vbmeta); 836 io_ret = ops->validate_vbmeta_public_key(ops, 837 pk_data, 838 pk_len, 839 pk_metadata, 840 pk_metadata_len, 841 &key_is_trusted); 842 } 843 844 if (io_ret == AVB_IO_RESULT_ERROR_OOM) { 845 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 846 goto out; 847 } else if (io_ret != AVB_IO_RESULT_OK) { 848 avb_errorv(full_partition_name, 849 ": Error while checking public key used to sign data.\n", 850 NULL); 851 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO; 852 goto out; 853 } 854 if (!key_is_trusted) { 855 avb_errorv(full_partition_name, 856 ": Public key used to sign data rejected.\n", 857 NULL); 858 ret = AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED; 859 if (!allow_verification_error) { 860 goto out; 861 } 862 } 863 } 864 } 865 866 /* Check rollback index. */ 867 io_ret = ops->read_rollback_index( 868 ops, rollback_index_location_to_use, &stored_rollback_index); 869 if (io_ret == AVB_IO_RESULT_ERROR_OOM) { 870 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 871 goto out; 872 } else if (io_ret != AVB_IO_RESULT_OK) { 873 avb_errorv(full_partition_name, 874 ": Error getting rollback index for location.\n", 875 NULL); 876 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO; 877 goto out; 878 } 879 if (vbmeta_header.rollback_index < stored_rollback_index) { 880 avb_errorv( 881 full_partition_name, 882 ": Image rollback index is less than the stored rollback index.\n", 883 NULL); 884 ret = AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX; 885 if (!allow_verification_error) { 886 goto out; 887 } 888 } 889 890 /* Copy vbmeta to vbmeta_images before recursing. */ 891 if (is_main_vbmeta) { 892 avb_assert(slot_data->num_vbmeta_images == 0); 893 } else { 894 if (!(flags & AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION)) { 895 avb_assert(slot_data->num_vbmeta_images > 0); 896 } 897 } 898 if (slot_data->num_vbmeta_images == MAX_NUMBER_OF_VBMETA_IMAGES) { 899 avb_errorv(full_partition_name, ": Too many vbmeta images.\n", NULL); 900 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 901 goto out; 902 } 903 vbmeta_image_data = &slot_data->vbmeta_images[slot_data->num_vbmeta_images++]; 904 vbmeta_image_data->partition_name = avb_strdup(partition_name); 905 vbmeta_image_data->vbmeta_data = vbmeta_buf; 906 /* Note that |vbmeta_buf| is actually |vbmeta_num_read| bytes long 907 * and this includes data past the end of the image. Pass the 908 * actual size of the vbmeta image. Also, no need to use 909 * avb_safe_add() since the header has already been verified. 910 */ 911 vbmeta_image_data->vbmeta_size = 912 sizeof(AvbVBMetaImageHeader) + 913 vbmeta_header.authentication_data_block_size + 914 vbmeta_header.auxiliary_data_block_size; 915 vbmeta_image_data->verify_result = vbmeta_ret; 916 917 /* If verification has been disabled by setting a bit in the image, 918 * we're done... except that we need to load the entirety of the 919 * requested partitions. 920 */ 921 if (vbmeta_header.flags & AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED) { 922 AvbSlotVerifyResult sub_ret; 923 avb_debugv( 924 full_partition_name, ": VERIFICATION_DISABLED bit is set.\n", NULL); 925 /* If load_requested_partitions() fail it is always a fatal 926 * failure (e.g. ERROR_INVALID_ARGUMENT, ERROR_OOM, etc.) rather 927 * than recoverable (e.g. one where result_should_continue() 928 * returns true) and we want to convey that error. 929 */ 930 sub_ret = load_requested_partitions( 931 ops, requested_partitions, ab_suffix, slot_data); 932 if (sub_ret != AVB_SLOT_VERIFY_RESULT_OK) { 933 ret = sub_ret; 934 } 935 goto out; 936 } 937 938 /* Now go through all descriptors and take the appropriate action: 939 * 940 * - hash descriptor: Load data from partition, calculate hash, and 941 * checks that it matches what's in the hash descriptor. 942 * 943 * - hashtree descriptor: Do nothing since verification happens 944 * on-the-fly from within the OS. (Unless the descriptor uses a 945 * persistent digest, in which case we need to find it). 946 * 947 * - chained partition descriptor: Load the footer, load the vbmeta 948 * image, verify vbmeta image (includes rollback checks, hash 949 * checks, bail on chained partitions). 950 */ 951 descriptors = 952 avb_descriptor_get_all(vbmeta_buf, vbmeta_num_read, &num_descriptors); 953 for (n = 0; n < num_descriptors; n++) { 954 AvbDescriptor desc; 955 956 if (!avb_descriptor_validate_and_byteswap(descriptors[n], &desc)) { 957 avb_errorv(full_partition_name, ": Descriptor is invalid.\n", NULL); 958 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA; 959 goto out; 960 } 961 962 switch (desc.tag) { 963 case AVB_DESCRIPTOR_TAG_HASH: { 964 AvbSlotVerifyResult sub_ret; 965 sub_ret = load_and_verify_hash_partition(ops, 966 requested_partitions, 967 ab_suffix, 968 allow_verification_error, 969 descriptors[n], 970 slot_data); 971 if (sub_ret != AVB_SLOT_VERIFY_RESULT_OK) { 972 ret = sub_ret; 973 if (!allow_verification_error || !result_should_continue(ret)) { 974 goto out; 975 } 976 } 977 } break; 978 979 case AVB_DESCRIPTOR_TAG_CHAIN_PARTITION: { 980 AvbSlotVerifyResult sub_ret; 981 AvbChainPartitionDescriptor chain_desc; 982 const uint8_t* chain_partition_name; 983 const uint8_t* chain_public_key; 984 985 /* Only allow CHAIN_PARTITION descriptors in the main vbmeta image. */ 986 if (!is_main_vbmeta) { 987 avb_errorv(full_partition_name, 988 ": Encountered chain descriptor not in main image.\n", 989 NULL); 990 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA; 991 goto out; 992 } 993 994 if (!avb_chain_partition_descriptor_validate_and_byteswap( 995 (AvbChainPartitionDescriptor*)descriptors[n], &chain_desc)) { 996 avb_errorv(full_partition_name, 997 ": Chain partition descriptor is invalid.\n", 998 NULL); 999 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA; 1000 goto out; 1001 } 1002 1003 if (chain_desc.rollback_index_location == 0) { 1004 avb_errorv(full_partition_name, 1005 ": Chain partition has invalid " 1006 "rollback_index_location field.\n", 1007 NULL); 1008 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA; 1009 goto out; 1010 } 1011 1012 chain_partition_name = ((const uint8_t*)descriptors[n]) + 1013 sizeof(AvbChainPartitionDescriptor); 1014 chain_public_key = chain_partition_name + chain_desc.partition_name_len; 1015 1016 sub_ret = 1017 load_and_verify_vbmeta(ops, 1018 requested_partitions, 1019 ab_suffix, 1020 flags, 1021 allow_verification_error, 1022 toplevel_vbmeta_flags, 1023 chain_desc.rollback_index_location, 1024 (const char*)chain_partition_name, 1025 chain_desc.partition_name_len, 1026 chain_public_key, 1027 chain_desc.public_key_len, 1028 slot_data, 1029 NULL, /* out_algorithm_type */ 1030 NULL /* out_additional_cmdline_subst */); 1031 if (sub_ret != AVB_SLOT_VERIFY_RESULT_OK) { 1032 ret = sub_ret; 1033 if (!result_should_continue(ret)) { 1034 goto out; 1035 } 1036 } 1037 } break; 1038 1039 case AVB_DESCRIPTOR_TAG_KERNEL_CMDLINE: { 1040 const uint8_t* kernel_cmdline; 1041 AvbKernelCmdlineDescriptor kernel_cmdline_desc; 1042 bool apply_cmdline; 1043 1044 if (!avb_kernel_cmdline_descriptor_validate_and_byteswap( 1045 (AvbKernelCmdlineDescriptor*)descriptors[n], 1046 &kernel_cmdline_desc)) { 1047 avb_errorv(full_partition_name, 1048 ": Kernel cmdline descriptor is invalid.\n", 1049 NULL); 1050 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA; 1051 goto out; 1052 } 1053 1054 kernel_cmdline = ((const uint8_t*)descriptors[n]) + 1055 sizeof(AvbKernelCmdlineDescriptor); 1056 1057 if (!avb_validate_utf8(kernel_cmdline, 1058 kernel_cmdline_desc.kernel_cmdline_length)) { 1059 avb_errorv(full_partition_name, 1060 ": Kernel cmdline is not valid UTF-8.\n", 1061 NULL); 1062 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA; 1063 goto out; 1064 } 1065 1066 /* Compare the flags for top-level VBMeta struct with flags in 1067 * the command-line descriptor so command-line snippets only 1068 * intended for a certain mode (dm-verity enabled/disabled) 1069 * are skipped if applicable. 1070 */ 1071 apply_cmdline = true; 1072 if (toplevel_vbmeta_flags & AVB_VBMETA_IMAGE_FLAGS_HASHTREE_DISABLED) { 1073 if (kernel_cmdline_desc.flags & 1074 AVB_KERNEL_CMDLINE_FLAGS_USE_ONLY_IF_HASHTREE_NOT_DISABLED) { 1075 apply_cmdline = false; 1076 } 1077 } else { 1078 if (kernel_cmdline_desc.flags & 1079 AVB_KERNEL_CMDLINE_FLAGS_USE_ONLY_IF_HASHTREE_DISABLED) { 1080 apply_cmdline = false; 1081 } 1082 } 1083 1084 if (apply_cmdline) { 1085 if (slot_data->cmdline == NULL) { 1086 slot_data->cmdline = 1087 avb_calloc(kernel_cmdline_desc.kernel_cmdline_length + 1); 1088 if (slot_data->cmdline == NULL) { 1089 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 1090 goto out; 1091 } 1092 avb_memcpy(slot_data->cmdline, 1093 kernel_cmdline, 1094 kernel_cmdline_desc.kernel_cmdline_length); 1095 } else { 1096 /* new cmdline is: <existing_cmdline> + ' ' + <newcmdline> + '\0' */ 1097 size_t orig_size = avb_strlen(slot_data->cmdline); 1098 size_t new_size = 1099 orig_size + 1 + kernel_cmdline_desc.kernel_cmdline_length + 1; 1100 char* new_cmdline = avb_calloc(new_size); 1101 if (new_cmdline == NULL) { 1102 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 1103 goto out; 1104 } 1105 avb_memcpy(new_cmdline, slot_data->cmdline, orig_size); 1106 new_cmdline[orig_size] = ' '; 1107 avb_memcpy(new_cmdline + orig_size + 1, 1108 kernel_cmdline, 1109 kernel_cmdline_desc.kernel_cmdline_length); 1110 avb_free(slot_data->cmdline); 1111 slot_data->cmdline = new_cmdline; 1112 } 1113 } 1114 } break; 1115 1116 case AVB_DESCRIPTOR_TAG_HASHTREE: { 1117 AvbHashtreeDescriptor hashtree_desc; 1118 1119 if (!avb_hashtree_descriptor_validate_and_byteswap( 1120 (AvbHashtreeDescriptor*)descriptors[n], &hashtree_desc)) { 1121 avb_errorv( 1122 full_partition_name, ": Hashtree descriptor is invalid.\n", NULL); 1123 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA; 1124 goto out; 1125 } 1126 1127 /* We only need to continue when there is no digest in the descriptor. 1128 * This is because the only processing here is to find the digest and 1129 * make it available on the kernel command line. 1130 */ 1131 if (hashtree_desc.root_digest_len == 0) { 1132 char part_name[AVB_PART_NAME_MAX_SIZE]; 1133 size_t digest_len = 0; 1134 uint8_t digest_buf[AVB_SHA512_DIGEST_SIZE]; 1135 const uint8_t* desc_partition_name = 1136 ((const uint8_t*)descriptors[n]) + sizeof(AvbHashtreeDescriptor); 1137 1138 if (!avb_validate_utf8(desc_partition_name, 1139 hashtree_desc.partition_name_len)) { 1140 avb_error("Partition name is not valid UTF-8.\n"); 1141 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA; 1142 goto out; 1143 } 1144 1145 /* No ab_suffix for partitions without a digest in the descriptor 1146 * because these partitions hold data unique to this device and are 1147 * not updated using an A/B scheme. 1148 */ 1149 if ((hashtree_desc.flags & 1150 AVB_HASHTREE_DESCRIPTOR_FLAGS_DO_NOT_USE_AB) == 0 && 1151 avb_strlen(ab_suffix) != 0) { 1152 avb_error("Cannot use A/B with a persistent root digest.\n"); 1153 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA; 1154 goto out; 1155 } 1156 if (hashtree_desc.partition_name_len >= AVB_PART_NAME_MAX_SIZE) { 1157 avb_error("Partition name does not fit.\n"); 1158 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA; 1159 goto out; 1160 } 1161 avb_memcpy( 1162 part_name, desc_partition_name, hashtree_desc.partition_name_len); 1163 part_name[hashtree_desc.partition_name_len] = '\0'; 1164 1165 /* Determine the expected digest size from the hash algorithm. */ 1166 if (avb_strcmp((const char*)hashtree_desc.hash_algorithm, "sha1") == 1167 0) { 1168 digest_len = AVB_SHA1_DIGEST_SIZE; 1169 } else if (avb_strcmp((const char*)hashtree_desc.hash_algorithm, 1170 "sha256") == 0) { 1171 digest_len = AVB_SHA256_DIGEST_SIZE; 1172 } else if (avb_strcmp((const char*)hashtree_desc.hash_algorithm, 1173 "sha512") == 0) { 1174 digest_len = AVB_SHA512_DIGEST_SIZE; 1175 } else { 1176 avb_errorv(part_name, ": Unsupported hash algorithm.\n", NULL); 1177 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA; 1178 goto out; 1179 } 1180 1181 ret = read_persistent_digest(ops, 1182 part_name, 1183 digest_len, 1184 NULL /* initial_digest */, 1185 digest_buf); 1186 if (ret != AVB_SLOT_VERIFY_RESULT_OK) { 1187 goto out; 1188 } 1189 1190 if (out_additional_cmdline_subst) { 1191 ret = 1192 avb_add_root_digest_substitution(part_name, 1193 digest_buf, 1194 digest_len, 1195 out_additional_cmdline_subst); 1196 if (ret != AVB_SLOT_VERIFY_RESULT_OK) { 1197 goto out; 1198 } 1199 } 1200 } 1201 } break; 1202 1203 case AVB_DESCRIPTOR_TAG_PROPERTY: 1204 /* Do nothing. */ 1205 break; 1206 } 1207 } 1208 1209 if (rollback_index_location < 0 || 1210 rollback_index_location >= AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS) { 1211 avb_errorv( 1212 full_partition_name, ": Invalid rollback_index_location.\n", NULL); 1213 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA; 1214 goto out; 1215 } 1216 1217 slot_data->rollback_indexes[rollback_index_location] = 1218 vbmeta_header.rollback_index; 1219 1220 if (out_algorithm_type != NULL) { 1221 *out_algorithm_type = (AvbAlgorithmType)vbmeta_header.algorithm_type; 1222 } 1223 1224 out: 1225 /* If |vbmeta_image_data| isn't NULL it means that it adopted 1226 * |vbmeta_buf| so in that case don't free it here. 1227 */ 1228 if (vbmeta_image_data == NULL) { 1229 if (vbmeta_buf != NULL) { 1230 avb_free(vbmeta_buf); 1231 } 1232 } 1233 if (descriptors != NULL) { 1234 avb_free(descriptors); 1235 } 1236 return ret; 1237 } 1238 1239 static AvbIOResult avb_manage_hashtree_error_mode( 1240 AvbOps* ops, 1241 AvbSlotVerifyFlags flags, 1242 AvbSlotVerifyData* data, 1243 AvbHashtreeErrorMode* out_hashtree_error_mode) { 1244 AvbHashtreeErrorMode ret = AVB_HASHTREE_ERROR_MODE_RESTART; 1245 AvbIOResult io_ret = AVB_IO_RESULT_OK; 1246 uint8_t vbmeta_digest_sha256[AVB_SHA256_DIGEST_SIZE]; 1247 uint8_t stored_vbmeta_digest_sha256[AVB_SHA256_DIGEST_SIZE]; 1248 size_t num_bytes_read; 1249 1250 avb_assert(out_hashtree_error_mode != NULL); 1251 avb_assert(ops->read_persistent_value != NULL); 1252 avb_assert(ops->write_persistent_value != NULL); 1253 1254 // If we're rebooting because of dm-verity corruption, make a note of 1255 // the vbmeta hash so we can stay in 'eio' mode until things change. 1256 if (flags & AVB_SLOT_VERIFY_FLAGS_RESTART_CAUSED_BY_HASHTREE_CORRUPTION) { 1257 avb_debug( 1258 "Rebooting because of dm-verity corruption - " 1259 "recording OS instance and using 'eio' mode.\n"); 1260 avb_slot_verify_data_calculate_vbmeta_digest( 1261 data, AVB_DIGEST_TYPE_SHA256, vbmeta_digest_sha256); 1262 io_ret = ops->write_persistent_value(ops, 1263 AVB_NPV_MANAGED_VERITY_MODE, 1264 AVB_SHA256_DIGEST_SIZE, 1265 vbmeta_digest_sha256); 1266 if (io_ret != AVB_IO_RESULT_OK) { 1267 avb_error("Error writing to " AVB_NPV_MANAGED_VERITY_MODE ".\n"); 1268 goto out; 1269 } 1270 ret = AVB_HASHTREE_ERROR_MODE_EIO; 1271 io_ret = AVB_IO_RESULT_OK; 1272 goto out; 1273 } 1274 1275 // See if we're in 'eio' mode. 1276 io_ret = ops->read_persistent_value(ops, 1277 AVB_NPV_MANAGED_VERITY_MODE, 1278 AVB_SHA256_DIGEST_SIZE, 1279 stored_vbmeta_digest_sha256, 1280 &num_bytes_read); 1281 if (io_ret == AVB_IO_RESULT_ERROR_NO_SUCH_VALUE || 1282 (io_ret == AVB_IO_RESULT_OK && num_bytes_read == 0)) { 1283 // This is the usual case ('eio' mode not set). 1284 avb_debug("No dm-verity corruption - using in 'restart' mode.\n"); 1285 ret = AVB_HASHTREE_ERROR_MODE_RESTART; 1286 io_ret = AVB_IO_RESULT_OK; 1287 goto out; 1288 } else if (io_ret != AVB_IO_RESULT_OK) { 1289 avb_error("Error reading from " AVB_NPV_MANAGED_VERITY_MODE ".\n"); 1290 goto out; 1291 } 1292 if (num_bytes_read != AVB_SHA256_DIGEST_SIZE) { 1293 avb_error( 1294 "Unexpected number of bytes read from " AVB_NPV_MANAGED_VERITY_MODE 1295 ".\n"); 1296 io_ret = AVB_IO_RESULT_ERROR_IO; 1297 goto out; 1298 } 1299 1300 // OK, so we're currently in 'eio' mode and the vbmeta digest of the OS 1301 // that caused this is in |stored_vbmeta_digest_sha256| ... now see if 1302 // the OS we're dealing with now is the same. 1303 avb_slot_verify_data_calculate_vbmeta_digest( 1304 data, AVB_DIGEST_TYPE_SHA256, vbmeta_digest_sha256); 1305 if (avb_memcmp(vbmeta_digest_sha256, 1306 stored_vbmeta_digest_sha256, 1307 AVB_SHA256_DIGEST_SIZE) == 0) { 1308 // It's the same so we're still in 'eio' mode. 1309 avb_debug("Same OS instance detected - staying in 'eio' mode.\n"); 1310 ret = AVB_HASHTREE_ERROR_MODE_EIO; 1311 io_ret = AVB_IO_RESULT_OK; 1312 } else { 1313 // It did change! 1314 avb_debug( 1315 "New OS instance detected - changing from 'eio' to 'restart' mode.\n"); 1316 io_ret = 1317 ops->write_persistent_value(ops, 1318 AVB_NPV_MANAGED_VERITY_MODE, 1319 0, // This clears the persistent property. 1320 vbmeta_digest_sha256); 1321 if (io_ret != AVB_IO_RESULT_OK) { 1322 avb_error("Error clearing " AVB_NPV_MANAGED_VERITY_MODE ".\n"); 1323 goto out; 1324 } 1325 ret = AVB_HASHTREE_ERROR_MODE_RESTART; 1326 io_ret = AVB_IO_RESULT_OK; 1327 } 1328 1329 out: 1330 *out_hashtree_error_mode = ret; 1331 return io_ret; 1332 } 1333 1334 static bool has_system_partition(AvbOps* ops, const char* ab_suffix) { 1335 char part_name[AVB_PART_NAME_MAX_SIZE]; 1336 char* system_part_name = "system"; 1337 char guid_buf[37]; 1338 AvbIOResult io_ret; 1339 1340 if (!avb_str_concat(part_name, 1341 sizeof part_name, 1342 system_part_name, 1343 avb_strlen(system_part_name), 1344 ab_suffix, 1345 avb_strlen(ab_suffix))) { 1346 avb_error("System partition name and suffix does not fit.\n"); 1347 return false; 1348 } 1349 1350 io_ret = ops->get_unique_guid_for_partition( 1351 ops, part_name, guid_buf, sizeof guid_buf); 1352 if (io_ret == AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION) { 1353 avb_debug("No system partition.\n"); 1354 return false; 1355 } else if (io_ret != AVB_IO_RESULT_OK) { 1356 avb_error("Error getting unique GUID for system partition.\n"); 1357 return false; 1358 } 1359 1360 return true; 1361 } 1362 1363 AvbSlotVerifyResult avb_slot_verify(AvbOps* ops, 1364 const char* const* requested_partitions, 1365 const char* ab_suffix, 1366 AvbSlotVerifyFlags flags, 1367 AvbHashtreeErrorMode hashtree_error_mode, 1368 AvbSlotVerifyData** out_data) { 1369 AvbSlotVerifyResult ret = 0; 1370 AvbSlotVerifyData* slot_data = NULL; 1371 AvbAlgorithmType algorithm_type = AVB_ALGORITHM_TYPE_NONE; 1372 bool using_boot_for_vbmeta = false; 1373 AvbVBMetaImageHeader toplevel_vbmeta; 1374 bool allow_verification_error = 1375 (flags & AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR); 1376 AvbCmdlineSubstList* additional_cmdline_subst = NULL; 1377 1378 /* Fail early if we're missing the AvbOps needed for slot verification. */ 1379 avb_assert(ops->read_is_device_unlocked != NULL); 1380 avb_assert(ops->read_from_partition != NULL); 1381 avb_assert(ops->get_size_of_partition != NULL); 1382 avb_assert(ops->read_rollback_index != NULL); 1383 avb_assert(ops->get_unique_guid_for_partition != NULL); 1384 1385 if (out_data != NULL) { 1386 *out_data = NULL; 1387 } 1388 1389 /* Allowing dm-verity errors defeats the purpose of verified boot so 1390 * only allow this if set up to allow verification errors 1391 * (e.g. typically only UNLOCKED mode). 1392 */ 1393 if (hashtree_error_mode == AVB_HASHTREE_ERROR_MODE_LOGGING && 1394 !allow_verification_error) { 1395 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT; 1396 goto fail; 1397 } 1398 1399 /* Make sure passed-in AvbOps support persistent values if 1400 * asking for libavb to manage verity state. 1401 */ 1402 if (hashtree_error_mode == AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO) { 1403 if (ops->read_persistent_value == NULL || 1404 ops->write_persistent_value == NULL) { 1405 avb_error( 1406 "Persistent values required for " 1407 "AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO " 1408 "but are not implemented in given AvbOps.\n"); 1409 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT; 1410 goto fail; 1411 } 1412 } 1413 1414 /* Make sure passed-in AvbOps support verifying public keys and getting 1415 * rollback index location if not using a vbmeta partition. 1416 */ 1417 if (flags & AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION) { 1418 if (ops->validate_public_key_for_partition == NULL) { 1419 avb_error( 1420 "AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION was passed but the " 1421 "validate_public_key_for_partition() operation isn't implemented.\n"); 1422 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT; 1423 goto fail; 1424 } 1425 } else { 1426 avb_assert(ops->validate_vbmeta_public_key != NULL); 1427 } 1428 1429 slot_data = avb_calloc(sizeof(AvbSlotVerifyData)); 1430 if (slot_data == NULL) { 1431 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 1432 goto fail; 1433 } 1434 slot_data->vbmeta_images = 1435 avb_calloc(sizeof(AvbVBMetaData) * MAX_NUMBER_OF_VBMETA_IMAGES); 1436 if (slot_data->vbmeta_images == NULL) { 1437 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 1438 goto fail; 1439 } 1440 slot_data->loaded_partitions = 1441 avb_calloc(sizeof(AvbPartitionData) * MAX_NUMBER_OF_LOADED_PARTITIONS); 1442 if (slot_data->loaded_partitions == NULL) { 1443 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 1444 goto fail; 1445 } 1446 1447 additional_cmdline_subst = avb_new_cmdline_subst_list(); 1448 if (additional_cmdline_subst == NULL) { 1449 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 1450 goto fail; 1451 } 1452 1453 if (flags & AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION) { 1454 if (requested_partitions == NULL || requested_partitions[0] == NULL) { 1455 avb_fatal( 1456 "Requested partitions cannot be empty when using " 1457 "AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION"); 1458 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT; 1459 goto fail; 1460 } 1461 1462 /* No vbmeta partition, go through each of the requested partitions... */ 1463 for (size_t n = 0; requested_partitions[n] != NULL; n++) { 1464 ret = load_and_verify_vbmeta(ops, 1465 requested_partitions, 1466 ab_suffix, 1467 flags, 1468 allow_verification_error, 1469 0 /* toplevel_vbmeta_flags */, 1470 0 /* rollback_index_location */, 1471 requested_partitions[n], 1472 avb_strlen(requested_partitions[n]), 1473 NULL /* expected_public_key */, 1474 0 /* expected_public_key_length */, 1475 slot_data, 1476 &algorithm_type, 1477 additional_cmdline_subst); 1478 if (!allow_verification_error && ret != AVB_SLOT_VERIFY_RESULT_OK) { 1479 goto fail; 1480 } 1481 } 1482 1483 } else { 1484 /* Usual path, load "vbmeta"... */ 1485 ret = load_and_verify_vbmeta(ops, 1486 requested_partitions, 1487 ab_suffix, 1488 flags, 1489 allow_verification_error, 1490 0 /* toplevel_vbmeta_flags */, 1491 0 /* rollback_index_location */, 1492 "vbmeta", 1493 avb_strlen("vbmeta"), 1494 NULL /* expected_public_key */, 1495 0 /* expected_public_key_length */, 1496 slot_data, 1497 &algorithm_type, 1498 additional_cmdline_subst); 1499 if (!allow_verification_error && ret != AVB_SLOT_VERIFY_RESULT_OK) { 1500 goto fail; 1501 } 1502 } 1503 1504 if (!result_should_continue(ret)) { 1505 goto fail; 1506 } 1507 1508 /* If things check out, mangle the kernel command-line as needed. */ 1509 if (!(flags & AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION)) { 1510 if (avb_strcmp(slot_data->vbmeta_images[0].partition_name, "vbmeta") != 0) { 1511 avb_assert( 1512 avb_strcmp(slot_data->vbmeta_images[0].partition_name, "boot") == 0); 1513 using_boot_for_vbmeta = true; 1514 } 1515 } 1516 1517 /* Byteswap top-level vbmeta header since we'll need it below. */ 1518 avb_vbmeta_image_header_to_host_byte_order( 1519 (const AvbVBMetaImageHeader*)slot_data->vbmeta_images[0].vbmeta_data, 1520 &toplevel_vbmeta); 1521 1522 /* Fill in |ab_suffix| field. */ 1523 slot_data->ab_suffix = avb_strdup(ab_suffix); 1524 if (slot_data->ab_suffix == NULL) { 1525 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 1526 goto fail; 1527 } 1528 1529 /* If verification is disabled, we are done ... we specifically 1530 * don't want to add any androidboot.* options since verification 1531 * is disabled. 1532 */ 1533 if (toplevel_vbmeta.flags & AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED) { 1534 /* Since verification is disabled we didn't process any 1535 * descriptors and thus there's no cmdline... so set root= such 1536 * that the system partition is mounted. 1537 */ 1538 avb_assert(slot_data->cmdline == NULL); 1539 // Devices with dynamic partitions won't have system partition. 1540 // Instead, it has a large super partition to accommodate *.img files. 1541 // See b/119551429 for details. 1542 if (has_system_partition(ops, ab_suffix)) { 1543 slot_data->cmdline = 1544 avb_strdup("root=PARTUUID=$(ANDROID_SYSTEM_PARTUUID)"); 1545 } else { 1546 // The |cmdline| field should be a NUL-terminated string. 1547 slot_data->cmdline = avb_strdup(""); 1548 } 1549 if (slot_data->cmdline == NULL) { 1550 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 1551 goto fail; 1552 } 1553 } else { 1554 /* If requested, manage dm-verity mode... */ 1555 AvbHashtreeErrorMode resolved_hashtree_error_mode = hashtree_error_mode; 1556 if (hashtree_error_mode == 1557 AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO) { 1558 AvbIOResult io_ret; 1559 io_ret = avb_manage_hashtree_error_mode( 1560 ops, flags, slot_data, &resolved_hashtree_error_mode); 1561 if (io_ret != AVB_IO_RESULT_OK) { 1562 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO; 1563 if (io_ret == AVB_IO_RESULT_ERROR_OOM) { 1564 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 1565 } 1566 goto fail; 1567 } 1568 } 1569 slot_data->resolved_hashtree_error_mode = resolved_hashtree_error_mode; 1570 1571 /* Add options... */ 1572 AvbSlotVerifyResult sub_ret; 1573 sub_ret = avb_append_options(ops, 1574 flags, 1575 slot_data, 1576 &toplevel_vbmeta, 1577 algorithm_type, 1578 hashtree_error_mode, 1579 resolved_hashtree_error_mode); 1580 if (sub_ret != AVB_SLOT_VERIFY_RESULT_OK) { 1581 ret = sub_ret; 1582 goto fail; 1583 } 1584 } 1585 1586 /* Substitute $(ANDROID_SYSTEM_PARTUUID) and friends. */ 1587 if (slot_data->cmdline != NULL && avb_strlen(slot_data->cmdline) != 0) { 1588 char* new_cmdline; 1589 new_cmdline = avb_sub_cmdline(ops, 1590 slot_data->cmdline, 1591 ab_suffix, 1592 using_boot_for_vbmeta, 1593 additional_cmdline_subst); 1594 if (new_cmdline != slot_data->cmdline) { 1595 if (new_cmdline == NULL) { 1596 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 1597 goto fail; 1598 } 1599 avb_free(slot_data->cmdline); 1600 slot_data->cmdline = new_cmdline; 1601 } 1602 } 1603 1604 if (out_data != NULL) { 1605 *out_data = slot_data; 1606 } else { 1607 avb_slot_verify_data_free(slot_data); 1608 } 1609 1610 avb_free_cmdline_subst_list(additional_cmdline_subst); 1611 additional_cmdline_subst = NULL; 1612 1613 if (!allow_verification_error) { 1614 avb_assert(ret == AVB_SLOT_VERIFY_RESULT_OK); 1615 } 1616 1617 return ret; 1618 1619 fail: 1620 if (slot_data != NULL) { 1621 avb_slot_verify_data_free(slot_data); 1622 } 1623 if (additional_cmdline_subst != NULL) { 1624 avb_free_cmdline_subst_list(additional_cmdline_subst); 1625 } 1626 return ret; 1627 } 1628 1629 void avb_slot_verify_data_free(AvbSlotVerifyData* data) { 1630 if (data->ab_suffix != NULL) { 1631 avb_free(data->ab_suffix); 1632 } 1633 if (data->cmdline != NULL) { 1634 avb_free(data->cmdline); 1635 } 1636 if (data->vbmeta_images != NULL) { 1637 size_t n; 1638 for (n = 0; n < data->num_vbmeta_images; n++) { 1639 AvbVBMetaData* vbmeta_image = &data->vbmeta_images[n]; 1640 if (vbmeta_image->partition_name != NULL) { 1641 avb_free(vbmeta_image->partition_name); 1642 } 1643 if (vbmeta_image->vbmeta_data != NULL) { 1644 avb_free(vbmeta_image->vbmeta_data); 1645 } 1646 } 1647 avb_free(data->vbmeta_images); 1648 } 1649 if (data->loaded_partitions != NULL) { 1650 size_t n; 1651 for (n = 0; n < data->num_loaded_partitions; n++) { 1652 AvbPartitionData* loaded_partition = &data->loaded_partitions[n]; 1653 if (loaded_partition->partition_name != NULL) { 1654 avb_free(loaded_partition->partition_name); 1655 } 1656 if (loaded_partition->data != NULL && !loaded_partition->preloaded) { 1657 sysmem_free((phys_addr_t)loaded_partition->data); 1658 } 1659 } 1660 avb_free(data->loaded_partitions); 1661 } 1662 avb_free(data); 1663 } 1664 1665 const char* avb_slot_verify_result_to_string(AvbSlotVerifyResult result) { 1666 const char* ret = NULL; 1667 1668 switch (result) { 1669 case AVB_SLOT_VERIFY_RESULT_OK: 1670 ret = "OK"; 1671 break; 1672 case AVB_SLOT_VERIFY_RESULT_ERROR_OOM: 1673 ret = "ERROR_OOM"; 1674 break; 1675 case AVB_SLOT_VERIFY_RESULT_ERROR_IO: 1676 ret = "ERROR_IO"; 1677 break; 1678 case AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION: 1679 ret = "ERROR_VERIFICATION"; 1680 break; 1681 case AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX: 1682 ret = "ERROR_ROLLBACK_INDEX"; 1683 break; 1684 case AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED: 1685 ret = "ERROR_PUBLIC_KEY_REJECTED"; 1686 break; 1687 case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA: 1688 ret = "ERROR_INVALID_METADATA"; 1689 break; 1690 case AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION: 1691 ret = "ERROR_UNSUPPORTED_VERSION"; 1692 break; 1693 case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT: 1694 ret = "ERROR_INVALID_ARGUMENT"; 1695 break; 1696 /* Do not add a 'default:' case here because of -Wswitch. */ 1697 } 1698 1699 if (ret == NULL) { 1700 avb_error("Unknown AvbSlotVerifyResult value.\n"); 1701 ret = "(unknown)"; 1702 } 1703 1704 return ret; 1705 } 1706 1707 void avb_slot_verify_data_calculate_vbmeta_digest(AvbSlotVerifyData* data, 1708 AvbDigestType digest_type, 1709 uint8_t* out_digest) { 1710 bool ret = false; 1711 size_t n; 1712 1713 switch (digest_type) { 1714 case AVB_DIGEST_TYPE_SHA256: { 1715 AvbSHA256Ctx ctx; 1716 avb_sha256_init(&ctx); 1717 for (n = 0; n < data->num_vbmeta_images; n++) { 1718 avb_sha256_update(&ctx, 1719 data->vbmeta_images[n].vbmeta_data, 1720 data->vbmeta_images[n].vbmeta_size); 1721 } 1722 avb_memcpy(out_digest, avb_sha256_final(&ctx), AVB_SHA256_DIGEST_SIZE); 1723 ret = true; 1724 } break; 1725 1726 case AVB_DIGEST_TYPE_SHA512: { 1727 AvbSHA512Ctx ctx; 1728 avb_sha512_init(&ctx); 1729 for (n = 0; n < data->num_vbmeta_images; n++) { 1730 avb_sha512_update(&ctx, 1731 data->vbmeta_images[n].vbmeta_data, 1732 data->vbmeta_images[n].vbmeta_size); 1733 } 1734 avb_memcpy(out_digest, avb_sha512_final(&ctx), AVB_SHA512_DIGEST_SIZE); 1735 ret = true; 1736 } break; 1737 1738 /* Do not add a 'default:' case here because of -Wswitch. */ 1739 } 1740 1741 if (!ret) { 1742 avb_fatal("Unknown digest type"); 1743 } 1744 } 1745