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
25 #include "avb_descriptor.h"
26 #include "avb_util.h"
27 #include "avb_vbmeta_image.h"
28
avb_descriptor_validate_and_byteswap(const AvbDescriptor * src,AvbDescriptor * dest)29 bool avb_descriptor_validate_and_byteswap(const AvbDescriptor* src,
30 AvbDescriptor* dest) {
31 dest->tag = avb_be64toh(src->tag);
32 dest->num_bytes_following = avb_be64toh(src->num_bytes_following);
33
34 if ((dest->num_bytes_following & 0x07) != 0) {
35 avb_error("Descriptor size is not divisible by 8.\n");
36 return false;
37 }
38 return true;
39 }
40
avb_descriptor_foreach(const uint8_t * image_data,size_t image_size,AvbDescriptorForeachFunc foreach_func,void * user_data)41 bool avb_descriptor_foreach(const uint8_t* image_data,
42 size_t image_size,
43 AvbDescriptorForeachFunc foreach_func,
44 void* user_data) {
45 const AvbVBMetaImageHeader* header = NULL;
46 bool ret = false;
47 const uint8_t* image_end;
48 const uint8_t* desc_start;
49 const uint8_t* desc_end;
50 const uint8_t* p;
51 uint64_t desc_offset = 0;
52 uint64_t desc_size = 0;
53
54 if (image_data == NULL) {
55 avb_error("image_data is NULL\n.");
56 goto out;
57 }
58
59 if (foreach_func == NULL) {
60 avb_error("foreach_func is NULL\n.");
61 goto out;
62 }
63
64 /* The data is supposed to have been cryptographically verified at this point,
65 * This check is just adding defense in depth.
66 */
67 if (image_size < sizeof(AvbVBMetaImageHeader)) {
68 avb_error("Length is smaller than header.\n");
69 goto out;
70 }
71
72 /* Ensure magic is correct. */
73 if (avb_memcmp(image_data, AVB_MAGIC, AVB_MAGIC_LEN) != 0) {
74 avb_error("Magic is incorrect.\n");
75 goto out;
76 }
77
78 /* Careful, not byteswapped - also ensure it's aligned properly. */
79 avb_assert_aligned(image_data);
80 header = (const AvbVBMetaImageHeader*)image_data;
81 image_end = image_data + image_size;
82
83 /* Since the data is supposed to have been cryptographically verified at this
84 * point, being overflow-safe is just for defense.
85 * The following lines are overflow-safe version of:
86 * desc_offset = sizeof(AvbVBMetaImageHeader) +
87 * avb_be64toh(header->authentication_data_block_size)) +
88 * avb_be64toh(header->descriptors_offset)
89 */
90 if (!avb_safe_add(&desc_offset,
91 sizeof(AvbVBMetaImageHeader),
92 avb_be64toh(header->authentication_data_block_size))) {
93 avb_error("Invalid authentication data block size.\n");
94 goto out;
95 }
96 if (!avb_safe_add_to(&desc_offset, avb_be64toh(header->descriptors_offset))) {
97 avb_error("Invalid descriptors offset.\n");
98 goto out;
99 }
100
101 if (desc_offset > (uint64_t)(image_end - image_data)) {
102 avb_error("Descriptors not inside passed-in data.\n");
103 goto out;
104 }
105
106 desc_start = image_data + desc_offset;
107
108 desc_size = avb_be64toh(header->descriptors_size);
109 if (desc_size > (uint64_t)(image_end - desc_start)) {
110 avb_error("Descriptors not inside passed-in data.\n");
111 goto out;
112 }
113
114 desc_end = desc_start + desc_size;
115
116 for (p = desc_start; p < desc_end;) {
117 uint64_t nb_following;
118 uint64_t nb_total = 0;
119 const AvbDescriptor* dh;
120
121 if (sizeof(AvbDescriptor) > (size_t)(desc_end - p)) {
122 avb_error("Invalid descriptor length.\n");
123 goto out;
124 }
125
126 dh = (const AvbDescriptor*)p;
127 avb_assert_aligned(dh);
128 nb_following = avb_be64toh(dh->num_bytes_following);
129
130 if (!avb_safe_add(&nb_total, sizeof(AvbDescriptor), nb_following)) {
131 avb_error("Invalid descriptor length.\n");
132 goto out;
133 }
134
135 if ((nb_total & 7) != 0) {
136 avb_error("Invalid descriptor length.\n");
137 goto out;
138 }
139
140 if (nb_total > (uint64_t)(desc_end - p)) {
141 avb_error("Invalid data in descriptors array.\n");
142 goto out;
143 }
144
145 if (foreach_func(dh, user_data) == 0) {
146 goto out;
147 }
148
149 p += nb_total;
150 }
151
152 ret = true;
153
154 out:
155 return ret;
156 }
157
count_descriptors(const AvbDescriptor * descriptor,void * user_data)158 static bool count_descriptors(const AvbDescriptor* descriptor,
159 void* user_data) {
160 size_t* num_descriptors = user_data;
161 *num_descriptors += 1;
162 return true;
163 }
164
165 typedef struct {
166 size_t descriptor_number;
167 const AvbDescriptor** descriptors;
168 } SetDescriptorData;
169
set_descriptors(const AvbDescriptor * descriptor,void * user_data)170 static bool set_descriptors(const AvbDescriptor* descriptor, void* user_data) {
171 SetDescriptorData* data = user_data;
172 data->descriptors[data->descriptor_number++] = descriptor;
173 return true;
174 }
175
avb_descriptor_get_all(const uint8_t * image_data,size_t image_size,size_t * out_num_descriptors)176 const AvbDescriptor** avb_descriptor_get_all(const uint8_t* image_data,
177 size_t image_size,
178 size_t* out_num_descriptors) {
179 size_t num_descriptors = 0;
180 SetDescriptorData data;
181
182 avb_descriptor_foreach(
183 image_data, image_size, count_descriptors, &num_descriptors);
184
185 data.descriptor_number = 0;
186 data.descriptors =
187 avb_calloc(sizeof(const AvbDescriptor*) * (num_descriptors + 1));
188 if (data.descriptors == NULL) {
189 return NULL;
190 }
191 avb_descriptor_foreach(image_data, image_size, set_descriptors, &data);
192 avb_assert(data.descriptor_number == num_descriptors);
193
194 if (out_num_descriptors != NULL) {
195 *out_num_descriptors = num_descriptors;
196 }
197
198 return data.descriptors;
199 }
200