1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * SPDX-License-Identifier: MIT 5 */ 6 7 #include "avb_chain_partition_descriptor.h" 8 #include "avb_util.h" 9 10 bool avb_chain_partition_descriptor_validate_and_byteswap( 11 const AvbChainPartitionDescriptor* src, AvbChainPartitionDescriptor* dest) { 12 uint64_t expected_size; 13 14 avb_memcpy(dest, src, sizeof(AvbChainPartitionDescriptor)); 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_CHAIN_PARTITION) { 21 avb_error("Invalid tag for chain partition descriptor.\n"); 22 return false; 23 } 24 25 dest->rollback_index_location = avb_be32toh(dest->rollback_index_location); 26 dest->partition_name_len = avb_be32toh(dest->partition_name_len); 27 dest->public_key_len = avb_be32toh(dest->public_key_len); 28 29 if (dest->rollback_index_location < 1) { 30 avb_error("Invalid rollback index location value.\n"); 31 return false; 32 } 33 34 /* Check that partition_name and public_key are fully contained. */ 35 expected_size = sizeof(AvbChainPartitionDescriptor) - sizeof(AvbDescriptor); 36 if (!avb_safe_add_to(&expected_size, dest->partition_name_len) || 37 !avb_safe_add_to(&expected_size, dest->public_key_len)) { 38 avb_error("Overflow while adding up sizes.\n"); 39 return false; 40 } 41 if (expected_size > dest->parent_descriptor.num_bytes_following) { 42 avb_error("Descriptor payload size overflow.\n"); 43 return false; 44 } 45 return true; 46 } 47