blob: 56a3a91fc23d166cfbcef4060ab4e9faed5073d2 [file] [log] [blame]
Tom Rini897a1d92018-06-19 11:21:44 -04001// SPDX-License-Identifier: MIT
Igor Opaniukd8f9d2a2018-06-03 21:56:36 +03002/*
3 * Copyright (C) 2016 The Android Open Source Project
Igor Opaniukd8f9d2a2018-06-03 21:56:36 +03004 */
5
6#include "avb_descriptor.h"
7#include "avb_util.h"
8#include "avb_vbmeta_image.h"
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070010#include <malloc.h>
Igor Opaniukd8f9d2a2018-06-03 21:56:36 +030011
12bool avb_descriptor_validate_and_byteswap(const AvbDescriptor* src,
13 AvbDescriptor* dest) {
14 dest->tag = avb_be64toh(src->tag);
15 dest->num_bytes_following = avb_be64toh(src->num_bytes_following);
16
17 if ((dest->num_bytes_following & 0x07) != 0) {
18 avb_error("Descriptor size is not divisible by 8.\n");
19 return false;
20 }
21 return true;
22}
23
24bool avb_descriptor_foreach(const uint8_t* image_data,
25 size_t image_size,
26 AvbDescriptorForeachFunc foreach_func,
27 void* user_data) {
28 const AvbVBMetaImageHeader* header = NULL;
29 bool ret = false;
30 const uint8_t* image_end;
31 const uint8_t* desc_start;
32 const uint8_t* desc_end;
33 const uint8_t* p;
34
35 if (image_data == NULL) {
36 avb_error("image_data is NULL\n.");
37 goto out;
38 }
39
40 if (foreach_func == NULL) {
41 avb_error("foreach_func is NULL\n.");
42 goto out;
43 }
44
45 if (image_size < sizeof(AvbVBMetaImageHeader)) {
46 avb_error("Length is smaller than header.\n");
47 goto out;
48 }
49
50 /* Ensure magic is correct. */
51 if (avb_memcmp(image_data, AVB_MAGIC, AVB_MAGIC_LEN) != 0) {
52 avb_error("Magic is incorrect.\n");
53 goto out;
54 }
55
56 /* Careful, not byteswapped - also ensure it's aligned properly. */
57 avb_assert_aligned(image_data);
58 header = (const AvbVBMetaImageHeader*)image_data;
59 image_end = image_data + image_size;
60
61 desc_start = image_data + sizeof(AvbVBMetaImageHeader) +
62 avb_be64toh(header->authentication_data_block_size) +
63 avb_be64toh(header->descriptors_offset);
64
65 desc_end = desc_start + avb_be64toh(header->descriptors_size);
66
67 if (desc_start < image_data || desc_start > image_end ||
68 desc_end < image_data || desc_end > image_end || desc_end < desc_start) {
69 avb_error("Descriptors not inside passed-in data.\n");
70 goto out;
71 }
72
73 for (p = desc_start; p < desc_end;) {
74 const AvbDescriptor* dh = (const AvbDescriptor*)p;
75 avb_assert_aligned(dh);
76 uint64_t nb_following = avb_be64toh(dh->num_bytes_following);
Sam Protsenko4d579a42019-08-15 23:04:02 +030077 uint64_t nb_total = 0;
78 if (!avb_safe_add(&nb_total, sizeof(AvbDescriptor), nb_following)) {
79 avb_error("Invalid descriptor length.\n");
80 goto out;
81 }
Igor Opaniukd8f9d2a2018-06-03 21:56:36 +030082
83 if ((nb_total & 7) != 0) {
84 avb_error("Invalid descriptor length.\n");
85 goto out;
86 }
87
88 if (nb_total + p < desc_start || nb_total + p > desc_end) {
89 avb_error("Invalid data in descriptors array.\n");
90 goto out;
91 }
92
93 if (foreach_func(dh, user_data) == 0) {
94 goto out;
95 }
96
Sam Protsenko4d579a42019-08-15 23:04:02 +030097 if (!avb_safe_add_to((uint64_t*)(&p), nb_total)) {
98 avb_error("Invalid descriptor length.\n");
99 goto out;
100 }
Igor Opaniukd8f9d2a2018-06-03 21:56:36 +0300101 }
102
103 ret = true;
104
105out:
106 return ret;
107}
108
109static bool count_descriptors(const AvbDescriptor* descriptor,
110 void* user_data) {
111 size_t* num_descriptors = user_data;
112 *num_descriptors += 1;
113 return true;
114}
115
116typedef struct {
117 size_t descriptor_number;
118 const AvbDescriptor** descriptors;
119} SetDescriptorData;
120
121static bool set_descriptors(const AvbDescriptor* descriptor, void* user_data) {
122 SetDescriptorData* data = user_data;
123 data->descriptors[data->descriptor_number++] = descriptor;
124 return true;
125}
126
127const AvbDescriptor** avb_descriptor_get_all(const uint8_t* image_data,
128 size_t image_size,
129 size_t* out_num_descriptors) {
130 size_t num_descriptors = 0;
131 SetDescriptorData data;
132
133 avb_descriptor_foreach(
134 image_data, image_size, count_descriptors, &num_descriptors);
135
136 data.descriptor_number = 0;
137 data.descriptors =
138 avb_calloc(sizeof(const AvbDescriptor*) * (num_descriptors + 1));
139 if (data.descriptors == NULL) {
140 return NULL;
141 }
142 avb_descriptor_foreach(image_data, image_size, set_descriptors, &data);
143 avb_assert(data.descriptor_number == num_descriptors);
144
145 if (out_num_descriptors != NULL) {
146 *out_num_descriptors = num_descriptors;
147 }
148
149 return data.descriptors;
150}