1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * SPDX-License-Identifier: MIT 5 */ 6 7 #include "avb_hashtree_descriptor.h" 8 #include "avb_util.h" 9 10 bool avb_hashtree_descriptor_validate_and_byteswap( 11 const AvbHashtreeDescriptor* src, AvbHashtreeDescriptor* dest) { 12 uint64_t expected_size; 13 14 avb_memcpy(dest, src, sizeof(AvbHashtreeDescriptor)); 15 16 if (!avb_descriptor_validate_and_byteswap((const AvbDescriptor*)src, 17 (AvbDescriptor*)dest)) 18 return false; 19 20 if (dest->parent_descriptor.tag != AVB_DESCRIPTOR_TAG_HASHTREE) { 21 avb_error("Invalid tag for hashtree descriptor.\n"); 22 return false; 23 } 24 25 dest->dm_verity_version = avb_be32toh(dest->dm_verity_version); 26 dest->image_size = avb_be64toh(dest->image_size); 27 dest->tree_offset = avb_be64toh(dest->tree_offset); 28 dest->tree_size = avb_be64toh(dest->tree_size); 29 dest->data_block_size = avb_be32toh(dest->data_block_size); 30 dest->hash_block_size = avb_be32toh(dest->hash_block_size); 31 dest->fec_num_roots = avb_be32toh(dest->fec_num_roots); 32 dest->fec_offset = avb_be64toh(dest->fec_offset); 33 dest->fec_size = avb_be64toh(dest->fec_size); 34 dest->partition_name_len = avb_be32toh(dest->partition_name_len); 35 dest->salt_len = avb_be32toh(dest->salt_len); 36 dest->root_digest_len = avb_be32toh(dest->root_digest_len); 37 dest->flags = avb_be32toh(dest->flags); 38 39 /* Check that partition_name, salt, and root_digest are fully contained. */ 40 expected_size = sizeof(AvbHashtreeDescriptor) - sizeof(AvbDescriptor); 41 if (!avb_safe_add_to(&expected_size, dest->partition_name_len) || 42 !avb_safe_add_to(&expected_size, dest->salt_len) || 43 !avb_safe_add_to(&expected_size, dest->root_digest_len)) { 44 avb_error("Overflow while adding up sizes.\n"); 45 return false; 46 } 47 if (expected_size > dest->parent_descriptor.num_bytes_following) { 48 avb_error("Descriptor payload size overflow.\n"); 49 return false; 50 } 51 return true; 52 } 53