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