blob: 31b4f1d3b7f56db47bf37a023ef69ee70e50e916 [file] [log] [blame]
Monika Singhb15747d2017-09-25 14:01:13 +05301/*
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_slot_verify.h"
26#include "avb_chain_partition_descriptor.h"
27#include "avb_footer.h"
28#include "avb_hash_descriptor.h"
29#include "avb_kernel_cmdline_descriptor.h"
30#include "avb_sha.h"
31#include "avb_util.h"
32#include "avb_vbmeta_image.h"
33#include "avb_version.h"
34
35/* Maximum allow length (in bytes) of a partition name, including
36 * ab_suffix.
37 */
38#define PART_NAME_MAX_SIZE 32
39
40/* Maximum number of partitions that can be loaded with avb_slot_verify(). */
41#define MAX_NUMBER_OF_LOADED_PARTITIONS 32
42
43/* Maximum number of vbmeta images that can be loaded with avb_slot_verify(). */
44#define MAX_NUMBER_OF_VBMETA_IMAGES 32
45
46/* Maximum size of a vbmeta image - 64 KiB. */
47#define VBMETA_MAX_SIZE (64 * 1024)
48
49/* Helper function to see if we should continue with verification in
50 * allow_verification_error=true mode if something goes wrong. See the
51 * comments for the avb_slot_verify() function for more information.
52 */
53static inline bool result_should_continue(AvbSlotVerifyResult result) {
54 switch (result) {
55 case AVB_SLOT_VERIFY_RESULT_ERROR_OOM:
56 case AVB_SLOT_VERIFY_RESULT_ERROR_IO:
57 case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA:
58 case AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION:
59 case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT:
60 return false;
61
62 case AVB_SLOT_VERIFY_RESULT_OK:
63 case AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION:
64 case AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX:
65 case AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED:
66 return true;
67 }
68
69 return false;
70}
71
72static AvbSlotVerifyResult load_and_verify_hash_partition(
73 AvbOps* ops,
74 const char* const* requested_partitions,
75 const char* ab_suffix,
76 bool allow_verification_error,
77 const AvbDescriptor* descriptor,
78 AvbSlotVerifyData* slot_data) {
79 AvbHashDescriptor hash_desc;
80 const uint8_t* desc_partition_name = NULL;
81 const uint8_t* desc_salt;
82 const uint8_t* desc_digest;
83 char part_name[PART_NAME_MAX_SIZE];
84 AvbSlotVerifyResult ret;
85 AvbIOResult io_ret;
86 uint8_t* image_buf = NULL;
87 size_t part_num_read;
88 uint8_t* digest = NULL;
89 size_t digest_len;
90 const char* found = NULL;
91 uint64_t image_size;
92
93 if (!avb_hash_descriptor_validate_and_byteswap(
94 (const AvbHashDescriptor*)descriptor, &hash_desc)) {
95 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
96 goto out;
97 }
98 desc_partition_name =
99 ((const uint8_t*)descriptor) + sizeof(AvbHashDescriptor);
100 desc_salt = desc_partition_name + hash_desc.partition_name_len;
101 desc_digest = desc_salt + hash_desc.salt_len;
102
103 if (!avb_validate_utf8(desc_partition_name, hash_desc.partition_name_len)) {
104 avb_error("Partition name is not valid UTF-8.\n");
105 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
106 goto out;
107 }
108
109 /* Don't bother loading or validating unless the partition was
110 * requested in the first place.
111 */
112 found = avb_strv_find_str(requested_partitions,
113 (const char*)desc_partition_name,
114 hash_desc.partition_name_len);
115 if (found == NULL) {
116 ret = AVB_SLOT_VERIFY_RESULT_OK;
117 goto out;
118 }
119
120 if (!avb_str_concat(part_name,
121 sizeof part_name,
122 (const char*)desc_partition_name,
123 hash_desc.partition_name_len,
124 ab_suffix,
125 avb_strlen(ab_suffix))) {
126 avb_error("Partition name and suffix does not fit.\n");
127 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
128 goto out;
129 }
130
131 /* If we're allowing verification errors then hash_desc.image_size
132 * may no longer match what's in the partition... so in this case
133 * just load the entire partition.
134 *
135 * For example, this can happen if a developer does 'fastboot flash
136 * boot /path/to/new/and/bigger/boot.img'. We want this to work
137 * since it's such a common workflow.
138 */
139 image_size = hash_desc.image_size;
140 if (allow_verification_error) {
141 if (ops->get_size_of_partition == NULL) {
142 avb_errorv(part_name,
143 ": The get_size_of_partition() operation is "
144 "not implemented so we may not load the entire partition. "
145 "Please implement.",
146 NULL);
147 } else {
148 io_ret = ops->get_size_of_partition(ops, part_name, &image_size);
149 if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
150 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
151 goto out;
152 } else if (io_ret != AVB_IO_RESULT_OK) {
153 avb_errorv(part_name, ": Error determining partition size.\n", NULL);
154 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
155 goto out;
156 }
157 avb_debugv(part_name, ": Loading entire partition.\n", NULL);
158 }
159 }
160
Monika Singh7a5c2ed2018-08-30 15:57:24 +0530161 if (!strncmp(part_name, "boot", strlen("boot"))) {
162 image_size = hash_desc.image_size;
163 }
164
165 io_ret = ops->read_from_partition(
166 ops, part_name, 0 /* offset */, image_size, &image_buf, &part_num_read);
167
Monika Singhb15747d2017-09-25 14:01:13 +0530168 if (image_buf == NULL) {
169 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
170 goto out;
171 }
Monika Singhb15747d2017-09-25 14:01:13 +0530172 if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
173 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
174 goto out;
175 } else if (io_ret != AVB_IO_RESULT_OK) {
176 avb_errorv(part_name, ": Error loading data from partition.\n", NULL);
177 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
178 goto out;
179 }
Monika Singh7a5c2ed2018-08-30 15:57:24 +0530180 if (part_num_read < image_size) {
Monika Singhb15747d2017-09-25 14:01:13 +0530181 avb_errorv(part_name, ": Read fewer than requested bytes.\n", NULL);
182 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
183 goto out;
184 }
185
186 if (avb_strcmp((const char*)hash_desc.hash_algorithm, "sha256") == 0) {
187 uint32_t complete_len = hash_desc.salt_len + hash_desc.image_size;
Monika Singhb15747d2017-09-25 14:01:13 +0530188 digest = avb_malloc(AVB_SHA256_DIGEST_SIZE);
Monika Singh08d26892018-09-10 15:58:07 +0530189 if(digest == NULL || hash_desc.salt_len > SALT_BUFF_OFFSET )
Monika Singh0f7bfc82018-04-16 23:14:29 +0530190 {
191 avb_errorv(part_name, ": Failed to allocate memory\n", NULL);
192 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
193 goto out;
194 }
Monika Singh08d26892018-09-10 15:58:07 +0530195 image_buf = ADD_SALT_BUFF_OFFSET(image_buf) - hash_desc.salt_len;
196 avb_memcpy(image_buf, desc_salt, hash_desc.salt_len);
197 hash_find(image_buf, complete_len, digest, CRYPTO_AUTH_ALG_SHA256);
Monika Singhb15747d2017-09-25 14:01:13 +0530198 digest_len = AVB_SHA256_DIGEST_SIZE;
199 } else if (avb_strcmp((const char*)hash_desc.hash_algorithm, "sha512") == 0) {
200 AvbSHA512Ctx sha512_ctx;
201 uint8_t *dig;
202 digest = avb_malloc(AVB_SHA512_DIGEST_SIZE);
Monika Singh0f7bfc82018-04-16 23:14:29 +0530203 if(digest == NULL)
204 {
205 avb_errorv(part_name, ": Failed to allocate memory\n", NULL);
206 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
207 goto out;
208 }
Monika Singhb15747d2017-09-25 14:01:13 +0530209 avb_sha512_init(&sha512_ctx);
210 avb_sha512_update(&sha512_ctx, desc_salt, hash_desc.salt_len);
211 avb_sha512_update(&sha512_ctx, image_buf, hash_desc.image_size);
212 dig = avb_sha512_final(&sha512_ctx);
213 digest_len = AVB_SHA512_DIGEST_SIZE;
214 avb_memcpy(digest, dig, AVB_SHA512_DIGEST_SIZE);
215 } else {
216 avb_errorv(part_name, ": Unsupported hash algorithm.\n", NULL);
217 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
218 goto out;
219 }
220
221 if (digest_len != hash_desc.digest_len) {
222 avb_errorv(
223 part_name, ": Digest in descriptor not of expected size.\n", NULL);
224 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
225 goto out;
226 }
227
228 if (avb_safe_memcmp(digest, desc_digest, digest_len) != 0) {
229 avb_errorv(part_name,
230 ": Hash of data does not match digest in descriptor.\n",
231 NULL);
232 ret = AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION;
233 goto out;
234 }
235
236 ret = AVB_SLOT_VERIFY_RESULT_OK;
237
238out:
239 if (digest)
240 avb_free(digest);
241 /* If it worked and something was loaded, copy to slot_data. */
242 if ((ret == AVB_SLOT_VERIFY_RESULT_OK || result_should_continue(ret)) &&
243 image_buf != NULL) {
244 AvbPartitionData* loaded_partition;
245 if (slot_data->num_loaded_partitions == MAX_NUMBER_OF_LOADED_PARTITIONS) {
246 avb_errorv(part_name, ": Too many loaded partitions.\n", NULL);
247 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
248 goto fail;
249 }
250 loaded_partition =
251 &slot_data->loaded_partitions[slot_data->num_loaded_partitions++];
252 loaded_partition->partition_name = avb_strdup(found);
253 loaded_partition->data_size = image_size;
254 loaded_partition->data = image_buf;
255 image_buf = NULL;
256 }
257
258fail:
259 //remove avb_free() as memory allocated from scratch region
260 return ret;
261}
262
263static AvbSlotVerifyResult load_requested_partitions(
264 AvbOps* ops,
265 const char* const* requested_partitions,
266 const char* ab_suffix,
267 AvbSlotVerifyData* slot_data) {
268 AvbSlotVerifyResult ret;
269 uint8_t* image_buf = NULL;
270 size_t n;
271
272 if (ops->get_size_of_partition == NULL) {
273 avb_error("get_size_of_partition() not implemented.\n");
274 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT;
275 goto out;
276 }
277
278 for (n = 0; requested_partitions[n] != NULL; n++) {
279 char part_name[PART_NAME_MAX_SIZE];
280 AvbIOResult io_ret;
281 uint64_t image_size;
282 size_t part_num_read;
283 AvbPartitionData* loaded_partition;
284
285 if (!avb_str_concat(part_name,
286 sizeof part_name,
287 requested_partitions[n],
288 avb_strlen(requested_partitions[n]),
289 ab_suffix,
290 avb_strlen(ab_suffix))) {
291 avb_error("Partition name and suffix does not fit.\n");
292 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
293 goto out;
294 }
295
296 io_ret = ops->get_size_of_partition(ops, part_name, &image_size);
297 if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
298 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
299 goto out;
300 } else if (io_ret != AVB_IO_RESULT_OK) {
301 avb_errorv(part_name, ": Error determining partition size.\n", NULL);
302 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
303 goto out;
304 }
305 avb_debugv(part_name, ": Loading entire partition.\n", NULL);
306
307 image_buf = avb_malloc(image_size);
308 if (image_buf == NULL) {
309 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
310 goto out;
311 }
312
313 io_ret = ops->read_from_partition(
314 ops, part_name, 0 /* offset */, image_size, image_buf, &part_num_read);
315 if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
316 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
317 goto out;
318 } else if (io_ret != AVB_IO_RESULT_OK) {
319 avb_errorv(part_name, ": Error loading data from partition.\n", NULL);
320 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
321 goto out;
322 }
323 if (part_num_read != image_size) {
324 avb_errorv(part_name, ": Read fewer than requested bytes.\n", NULL);
325 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
326 goto out;
327 }
328
329 /* Move to slot_data. */
330 if (slot_data->num_loaded_partitions == MAX_NUMBER_OF_LOADED_PARTITIONS) {
331 avb_errorv(part_name, ": Too many loaded partitions.\n", NULL);
332 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
333 goto out;
334 }
335 loaded_partition =
336 &slot_data->loaded_partitions[slot_data->num_loaded_partitions++];
337 loaded_partition->partition_name = avb_strdup(requested_partitions[n]);
338 if (loaded_partition->partition_name == NULL) {
339 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
340 goto out;
341 }
342 loaded_partition->data_size = image_size;
343 loaded_partition->data = image_buf;
344 image_buf = NULL;
345 }
346
347 ret = AVB_SLOT_VERIFY_RESULT_OK;
348
349out:
350 if (image_buf != NULL) {
351 avb_free(image_buf);
352 }
353 return ret;
354}
355
356static AvbSlotVerifyResult load_and_verify_vbmeta(
357 AvbOps* ops,
358 const char* const* requested_partitions,
359 const char* ab_suffix,
360 bool allow_verification_error,
361 AvbVBMetaImageFlags toplevel_vbmeta_flags,
362 int rollback_index_location,
363 const char* partition_name,
364 size_t partition_name_len,
365 const uint8_t* expected_public_key,
366 size_t expected_public_key_length,
367 AvbSlotVerifyData* slot_data,
368 AvbAlgorithmType* out_algorithm_type) {
369 char full_partition_name[PART_NAME_MAX_SIZE];
370 AvbSlotVerifyResult ret;
371 AvbIOResult io_ret;
Monika Singh5c317dc2018-09-25 11:09:55 +0530372 UINTN vbmeta_offset;
Monika Singhb15747d2017-09-25 14:01:13 +0530373 size_t vbmeta_size;
374 uint8_t* vbmeta_buf = NULL;
375 size_t vbmeta_num_read;
376 AvbVBMetaVerifyResult vbmeta_ret;
377 const uint8_t* pk_data;
378 size_t pk_len;
379 AvbVBMetaImageHeader vbmeta_header;
380 uint64_t stored_rollback_index;
381 const AvbDescriptor** descriptors = NULL;
382 size_t num_descriptors;
383 size_t n;
384 bool is_main_vbmeta;
385 bool is_vbmeta_partition;
386 AvbVBMetaData* vbmeta_image_data = NULL;
387
388 ret = AVB_SLOT_VERIFY_RESULT_OK;
389
390 avb_assert(slot_data != NULL);
391
392 /* Since we allow top-level vbmeta in 'boot', use
393 * rollback_index_location to determine whether we're the main
394 * vbmeta struct.
395 */
396 is_main_vbmeta = (rollback_index_location == 0);
397 is_vbmeta_partition = (avb_strcmp(partition_name, "vbmeta") == 0);
398
399 if (!avb_validate_utf8((const uint8_t*)partition_name, partition_name_len)) {
400 avb_error("Partition name is not valid UTF-8.\n");
401 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
402 goto out;
403 }
404
405 /* Construct full partition name. */
406 if (!avb_str_concat(full_partition_name,
407 sizeof full_partition_name,
408 partition_name,
409 partition_name_len,
410 ab_suffix,
411 avb_strlen(ab_suffix))) {
412 avb_error("Partition name and suffix does not fit.\n");
413 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
414 goto out;
415 }
416
417 avb_debugv("Loading vbmeta struct from partition '",
418 full_partition_name,
419 "'.\n",
420 NULL);
421
422 /* If we're loading from the main vbmeta partition, the vbmeta
423 * struct is in the beginning. Otherwise we have to locate it via a
424 * footer.
425 */
426 if (is_vbmeta_partition) {
427 vbmeta_offset = 0;
428 vbmeta_size = VBMETA_MAX_SIZE;
429 } else {
430 uint8_t footer_buf[AVB_FOOTER_SIZE];
431 size_t footer_num_read;
432 AvbFooter footer;
433
434 io_ret = ops->read_from_partition(ops,
435 full_partition_name,
436 -AVB_FOOTER_SIZE,
437 AVB_FOOTER_SIZE,
438 footer_buf,
439 &footer_num_read);
440 if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
441 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
442 goto out;
443 } else if (io_ret != AVB_IO_RESULT_OK) {
444 avb_errorv(full_partition_name, ": Error loading footer.\n", NULL);
445 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
446 goto out;
447 }
448 avb_assert(footer_num_read == AVB_FOOTER_SIZE);
449
450 if (!avb_footer_validate_and_byteswap((const AvbFooter*)footer_buf,
451 &footer)) {
452 avb_errorv(full_partition_name, ": Error validating footer.\n", NULL);
453 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
454 goto out;
455 }
456
457 /* Basic footer sanity check since the data is untrusted. */
458 if (footer.vbmeta_size > VBMETA_MAX_SIZE) {
459 avb_errorv(
460 full_partition_name, ": Invalid vbmeta size in footer.\n", NULL);
461 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
462 goto out;
463 }
Monika Singhb15747d2017-09-25 14:01:13 +0530464 vbmeta_offset = footer.vbmeta_offset;
465 vbmeta_size = footer.vbmeta_size;
466 }
467
Monika Singh5c317dc2018-09-25 11:09:55 +0530468 if (avb_strcmp(full_partition_name, "vbmeta") == 0) {
469 io_ret = ops->read_from_partition(ops,
Monika Singh7a5c2ed2018-08-30 15:57:24 +0530470 full_partition_name,
471 vbmeta_offset,
472 vbmeta_size,
473 &vbmeta_buf,
474 &vbmeta_num_read);
Monika Singh5c317dc2018-09-25 11:09:55 +0530475 } else { // for chain partitions
476 vbmeta_buf = avb_malloc(vbmeta_size);
477 if (vbmeta_buf == NULL) {
478 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
479 goto out;
480 }
481 io_ret = ops->read_from_partition(ops,
482 full_partition_name,
483 vbmeta_offset,
484 vbmeta_size,
485 vbmeta_buf,
486 &vbmeta_num_read);
487 }
Monika Singhb15747d2017-09-25 14:01:13 +0530488 if (vbmeta_buf == NULL) {
489 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
490 goto out;
491 }
492
Monika Singhb15747d2017-09-25 14:01:13 +0530493 if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
494 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
495 goto out;
496 } else if (io_ret != AVB_IO_RESULT_OK) {
497 /* If we're looking for 'vbmeta' but there is no such partition,
498 * go try to get it from the boot partition instead.
499 */
500 if (is_main_vbmeta && io_ret == AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION &&
501 is_vbmeta_partition) {
502 avb_debugv(full_partition_name,
503 ": No such partition. Trying 'boot' instead.\n",
504 NULL);
505 ret = load_and_verify_vbmeta(ops,
506 requested_partitions,
507 ab_suffix,
508 allow_verification_error,
509 0 /* toplevel_vbmeta_flags */,
510 0 /* rollback_index_location */,
511 "boot",
512 avb_strlen("boot"),
513 NULL /* expected_public_key */,
514 0 /* expected_public_key_length */,
515 slot_data,
516 out_algorithm_type);
517 goto out;
518 } else {
519 avb_errorv(full_partition_name, ": Error loading vbmeta data.\n", NULL);
520 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
521 goto out;
522 }
523 }
524 avb_assert(vbmeta_num_read <= vbmeta_size);
525
526 /* Check if the image is properly signed and get the public key used
527 * to sign the image.
528 */
529 vbmeta_ret =
530 avb_vbmeta_image_verify(vbmeta_buf, vbmeta_num_read, &pk_data, &pk_len);
531 switch (vbmeta_ret) {
532 case AVB_VBMETA_VERIFY_RESULT_OK:
533 avb_assert(pk_data != NULL && pk_len > 0);
534 break;
535
536 case AVB_VBMETA_VERIFY_RESULT_OK_NOT_SIGNED:
537 case AVB_VBMETA_VERIFY_RESULT_HASH_MISMATCH:
538 case AVB_VBMETA_VERIFY_RESULT_SIGNATURE_MISMATCH:
539 ret = AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION;
540 avb_errorv(full_partition_name,
541 ": Error verifying vbmeta image: ",
542 avb_vbmeta_verify_result_to_string(vbmeta_ret),
543 "\n",
544 NULL);
545 if (!allow_verification_error) {
546 goto out;
547 }
548 break;
549
550 case AVB_VBMETA_VERIFY_RESULT_INVALID_VBMETA_HEADER:
551 /* No way to continue this case. */
552 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
553 avb_errorv(full_partition_name,
554 ": Error verifying vbmeta image: invalid vbmeta header\n",
555 NULL);
556 goto out;
557
558 case AVB_VBMETA_VERIFY_RESULT_UNSUPPORTED_VERSION:
559 /* No way to continue this case. */
560 ret = AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION;
561 avb_errorv(full_partition_name,
562 ": Error verifying vbmeta image: unsupported AVB version\n",
563 NULL);
564 goto out;
565 }
566
567 /* Byteswap the header. */
568 avb_vbmeta_image_header_to_host_byte_order((AvbVBMetaImageHeader*)vbmeta_buf,
569 &vbmeta_header);
570
571 /* If we're the toplevel, assign flags so they'll be passed down. */
572 if (is_main_vbmeta) {
573 toplevel_vbmeta_flags = (AvbVBMetaImageFlags)vbmeta_header.flags;
574 } else {
575 if (vbmeta_header.flags != 0) {
576 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
577 avb_errorv(full_partition_name,
578 ": chained vbmeta image has non-zero flags\n",
579 NULL);
580 goto out;
581 }
582 }
583
584 /* Check if key used to make signature matches what is expected. */
585 if (pk_data != NULL) {
586 if (expected_public_key != NULL) {
587 avb_assert(!is_main_vbmeta);
588 if (expected_public_key_length != pk_len ||
589 avb_safe_memcmp(expected_public_key, pk_data, pk_len) != 0) {
590 avb_errorv(full_partition_name,
591 ": Public key used to sign data does not match key in chain "
592 "partition descriptor.\n",
593 NULL);
594 ret = AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED;
595 if (!allow_verification_error) {
596 goto out;
597 }
598 }
599 } else {
600 bool key_is_trusted = false;
601 const uint8_t* pk_metadata = NULL;
602 size_t pk_metadata_len = 0;
603
604 if (vbmeta_header.public_key_metadata_size > 0) {
605 pk_metadata = vbmeta_buf + sizeof(AvbVBMetaImageHeader) +
606 vbmeta_header.authentication_data_block_size +
607 vbmeta_header.public_key_metadata_offset;
608 pk_metadata_len = vbmeta_header.public_key_metadata_size;
609 }
610
611 avb_assert(is_main_vbmeta);
612 io_ret = ops->validate_vbmeta_public_key(
613 ops, pk_data, pk_len, pk_metadata, pk_metadata_len, &key_is_trusted);
614 if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
615 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
616 goto out;
617 } else if (io_ret != AVB_IO_RESULT_OK) {
618 avb_errorv(full_partition_name,
619 ": Error while checking public key used to sign data.\n",
620 NULL);
621 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
622 goto out;
623 }
624 if (!key_is_trusted) {
625 avb_errorv(full_partition_name,
626 ": Public key used to sign data rejected.\n",
627 NULL);
628 ret = AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED;
629 if (!allow_verification_error) {
630 goto out;
631 }
632 }
633 }
634 }
635
636 /* Check rollback index. */
637 io_ret = ops->read_rollback_index(
638 ops, rollback_index_location, &stored_rollback_index);
639 if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
640 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
641 goto out;
642 } else if (io_ret != AVB_IO_RESULT_OK) {
643 avb_errorv(full_partition_name,
644 ": Error getting rollback index for location.\n",
645 NULL);
646 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
647 goto out;
648 }
649 if (vbmeta_header.rollback_index < stored_rollback_index) {
650 avb_errorv(
651 full_partition_name,
652 ": Image rollback index is less than the stored rollback index.\n",
653 NULL);
654 ret = AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX;
655 if (!allow_verification_error) {
656 goto out;
657 }
658 }
659
Monika Singh2c9d2b82018-05-01 12:24:59 +0530660 if (stored_rollback_index < vbmeta_header.rollback_index) {
661 io_ret = ops->write_rollback_index(
662 ops, rollback_index_location, vbmeta_header.rollback_index);
663 if (io_ret != AVB_IO_RESULT_OK) {
664 avb_errorv(full_partition_name,
665 ": Error storing rollback index for location.\n",
666 NULL);
667 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
668 goto out;
669 }
670 }
671
Monika Singhb15747d2017-09-25 14:01:13 +0530672 /* Copy vbmeta to vbmeta_images before recursing. */
673 if (is_main_vbmeta) {
674 avb_assert(slot_data->num_vbmeta_images == 0);
675 } else {
676 avb_assert(slot_data->num_vbmeta_images > 0);
677 }
678 if (slot_data->num_vbmeta_images == MAX_NUMBER_OF_VBMETA_IMAGES) {
679 avb_errorv(full_partition_name, ": Too many vbmeta images.\n", NULL);
680 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
681 goto out;
682 }
683 vbmeta_image_data = &slot_data->vbmeta_images[slot_data->num_vbmeta_images++];
684 vbmeta_image_data->partition_name = avb_strdup(partition_name);
685 vbmeta_image_data->vbmeta_data = vbmeta_buf;
686 /* Note that |vbmeta_buf| is actually |vbmeta_num_read| bytes long
687 * and this includes data past the end of the image. Pass the
688 * actual size of the vbmeta image. Also, no need to use
689 * avb_safe_add() since the header has already been verified.
690 */
691 vbmeta_image_data->vbmeta_size =
692 sizeof(AvbVBMetaImageHeader) +
693 vbmeta_header.authentication_data_block_size +
694 vbmeta_header.auxiliary_data_block_size;
695 vbmeta_image_data->verify_result = vbmeta_ret;
696
697 /* If verification has been disabled by setting a bit in the image,
698 * we're done... except that we need to load the entirety of the
699 * requested partitions.
700 */
701 if (vbmeta_header.flags & AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED) {
702 AvbSlotVerifyResult sub_ret;
703 avb_debugv(
704 full_partition_name, ": VERIFICATION_DISABLED bit is set.\n", NULL);
705 /* If load_requested_partitions() fail it is always a fatal
706 * failure (e.g. ERROR_INVALID_ARGUMENT, ERROR_OOM, etc.) rather
707 * than recoverable (e.g. one where result_should_continue()
708 * returns true) and we want to convey that error.
709 */
710 sub_ret = load_requested_partitions(
711 ops, requested_partitions, ab_suffix, slot_data);
712 if (sub_ret != AVB_SLOT_VERIFY_RESULT_OK) {
713 ret = sub_ret;
714 }
715 goto out;
716 }
717
718 /* Now go through all descriptors and take the appropriate action:
719 *
720 * - hash descriptor: Load data from partition, calculate hash, and
721 * checks that it matches what's in the hash descriptor.
722 *
723 * - hashtree descriptor: Do nothing since verification happens
724 * on-the-fly from within the OS.
725 *
726 * - chained partition descriptor: Load the footer, load the vbmeta
727 * image, verify vbmeta image (includes rollback checks, hash
728 * checks, bail on chained partitions).
729 */
730 descriptors =
731 avb_descriptor_get_all(vbmeta_buf, vbmeta_num_read, &num_descriptors);
Firoz Khan6f6d59e2018-09-19 14:24:13 +0530732 if (descriptors == NULL) {
733 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
734 goto out;
735 }
736
Monika Singhb15747d2017-09-25 14:01:13 +0530737 for (n = 0; n < num_descriptors; n++) {
738 AvbDescriptor desc;
739
740 if (!avb_descriptor_validate_and_byteswap(descriptors[n], &desc)) {
741 avb_errorv(full_partition_name, ": Descriptor is invalid.\n", NULL);
742 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
743 goto out;
744 }
745
746 switch (desc.tag) {
747 case AVB_DESCRIPTOR_TAG_HASH: {
748 AvbSlotVerifyResult sub_ret;
749 sub_ret = load_and_verify_hash_partition(ops,
750 requested_partitions,
751 ab_suffix,
752 allow_verification_error,
753 descriptors[n],
754 slot_data);
755 if (sub_ret != AVB_SLOT_VERIFY_RESULT_OK) {
756 ret = sub_ret;
757 if (!allow_verification_error || !result_should_continue(ret)) {
758 goto out;
759 }
760 }
761 } break;
762
763 case AVB_DESCRIPTOR_TAG_CHAIN_PARTITION: {
764 AvbSlotVerifyResult sub_ret;
765 AvbChainPartitionDescriptor chain_desc;
766 const uint8_t* chain_partition_name;
767 const uint8_t* chain_public_key;
768
769 /* Only allow CHAIN_PARTITION descriptors in the main vbmeta image. */
770 if (!is_main_vbmeta) {
771 avb_errorv(full_partition_name,
772 ": Encountered chain descriptor not in main image.\n",
773 NULL);
774 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
775 goto out;
776 }
777
778 if (!avb_chain_partition_descriptor_validate_and_byteswap(
779 (AvbChainPartitionDescriptor*)descriptors[n], &chain_desc)) {
780 avb_errorv(full_partition_name,
781 ": Chain partition descriptor is invalid.\n",
782 NULL);
783 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
784 goto out;
785 }
786
787 if (chain_desc.rollback_index_location == 0) {
788 avb_errorv(full_partition_name,
789 ": Chain partition has invalid "
790 "rollback_index_location field.\n",
791 NULL);
792 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
793 goto out;
794 }
795
796 chain_partition_name = ((const uint8_t*)descriptors[n]) +
797 sizeof(AvbChainPartitionDescriptor);
798 chain_public_key = chain_partition_name + chain_desc.partition_name_len;
799
800 sub_ret = load_and_verify_vbmeta(ops,
801 requested_partitions,
802 ab_suffix,
803 allow_verification_error,
804 toplevel_vbmeta_flags,
805 chain_desc.rollback_index_location,
806 (const char*)chain_partition_name,
807 chain_desc.partition_name_len,
808 chain_public_key,
809 chain_desc.public_key_len,
810 slot_data,
811 NULL /* out_algorithm_type */);
812 if (sub_ret != AVB_SLOT_VERIFY_RESULT_OK) {
813 ret = sub_ret;
814 if (!result_should_continue(ret)) {
815 goto out;
816 }
817 }
818 } break;
819
820 case AVB_DESCRIPTOR_TAG_KERNEL_CMDLINE: {
821 const uint8_t* kernel_cmdline;
822 AvbKernelCmdlineDescriptor kernel_cmdline_desc;
823 bool apply_cmdline;
824
825 if (!avb_kernel_cmdline_descriptor_validate_and_byteswap(
826 (AvbKernelCmdlineDescriptor*)descriptors[n],
827 &kernel_cmdline_desc)) {
828 avb_errorv(full_partition_name,
829 ": Kernel cmdline descriptor is invalid.\n",
830 NULL);
831 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
832 goto out;
833 }
834
835 kernel_cmdline = ((const uint8_t*)descriptors[n]) +
836 sizeof(AvbKernelCmdlineDescriptor);
837
838 if (!avb_validate_utf8(kernel_cmdline,
839 kernel_cmdline_desc.kernel_cmdline_length)) {
840 avb_errorv(full_partition_name,
841 ": Kernel cmdline is not valid UTF-8.\n",
842 NULL);
843 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
844 goto out;
845 }
846
847 /* Compare the flags for top-level VBMeta struct with flags in
848 * the command-line descriptor so command-line snippets only
849 * intended for a certain mode (dm-verity enabled/disabled)
850 * are skipped if applicable.
851 */
852 apply_cmdline = true;
853 if (toplevel_vbmeta_flags & AVB_VBMETA_IMAGE_FLAGS_HASHTREE_DISABLED) {
854 if (kernel_cmdline_desc.flags &
855 AVB_KERNEL_CMDLINE_FLAGS_USE_ONLY_IF_HASHTREE_NOT_DISABLED) {
856 apply_cmdline = false;
857 }
858 } else {
859 if (kernel_cmdline_desc.flags &
860 AVB_KERNEL_CMDLINE_FLAGS_USE_ONLY_IF_HASHTREE_DISABLED) {
861 apply_cmdline = false;
862 }
863 }
864
865 if (apply_cmdline) {
866 if (slot_data->cmdline == NULL) {
867 slot_data->cmdline =
868 avb_calloc(kernel_cmdline_desc.kernel_cmdline_length + 1);
869 if (slot_data->cmdline == NULL) {
870 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
871 goto out;
872 }
873 avb_memcpy(slot_data->cmdline,
874 kernel_cmdline,
875 kernel_cmdline_desc.kernel_cmdline_length);
876 } else {
877 /* new cmdline is: <existing_cmdline> + ' ' + <newcmdline> + '\0' */
878 size_t orig_size = avb_strlen(slot_data->cmdline);
879 size_t new_size =
880 orig_size + 1 + kernel_cmdline_desc.kernel_cmdline_length + 1;
881 char* new_cmdline = avb_calloc(new_size);
882 if (new_cmdline == NULL) {
883 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
884 goto out;
885 }
886 avb_memcpy(new_cmdline, slot_data->cmdline, orig_size);
887 new_cmdline[orig_size] = ' ';
888 avb_memcpy(new_cmdline + orig_size + 1,
889 kernel_cmdline,
890 kernel_cmdline_desc.kernel_cmdline_length);
891 avb_free(slot_data->cmdline);
892 slot_data->cmdline = new_cmdline;
893 }
894 }
895 } break;
896
897 /* Explicit fall-through */
898 case AVB_DESCRIPTOR_TAG_PROPERTY:
899 case AVB_DESCRIPTOR_TAG_HASHTREE:
900 /* Do nothing. */
901 break;
902 }
903 }
904
905 if (rollback_index_location >= AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS) {
906 avb_errorv(
907 full_partition_name, ": Invalid rollback_index_location.\n", NULL);
908 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
909 goto out;
910 }
911
912 slot_data->rollback_indexes[rollback_index_location] =
913 vbmeta_header.rollback_index;
914
915 if (out_algorithm_type != NULL) {
916 *out_algorithm_type = (AvbAlgorithmType)vbmeta_header.algorithm_type;
917 }
918
919out:
920 /* If |vbmeta_image_data| isn't NULL it means that it adopted
921 * |vbmeta_buf| so in that case don't free it here.
922 */
923
924 /* remove avb_free() as memory allocated from scratch region */
925
926 if (descriptors != NULL) {
927 avb_free(descriptors);
928 }
929 return ret;
930}
931
932#define NUM_GUIDS 3
933
934/* Substitutes all variables (e.g. $(ANDROID_SYSTEM_PARTUUID)) with
935 * values. Returns NULL on OOM, otherwise the cmdline with values
936 * replaced.
937 */
938static char* sub_cmdline(AvbOps* ops,
939 const char* cmdline,
940 const char* ab_suffix,
941 bool using_boot_for_vbmeta) {
942 const char* part_name_str[NUM_GUIDS] = {"system", "boot", "vbmeta"};
943 const char* replace_str[NUM_GUIDS] = {"$(ANDROID_SYSTEM_PARTUUID)",
944 "$(ANDROID_BOOT_PARTUUID)",
945 "$(ANDROID_VBMETA_PARTUUID)"};
946 char* ret = NULL;
947 AvbIOResult io_ret;
948
949 /* Special-case for when the top-level vbmeta struct is in the boot
950 * partition.
951 */
952 if (using_boot_for_vbmeta) {
953 part_name_str[2] = "boot";
954 }
955
956 /* Replace unique partition GUIDs */
957 for (size_t n = 0; n < NUM_GUIDS; n++) {
958 char part_name[PART_NAME_MAX_SIZE];
959 char guid_buf[37];
960
961 if (!avb_str_concat(part_name,
962 sizeof part_name,
963 part_name_str[n],
964 avb_strlen(part_name_str[n]),
965 ab_suffix,
966 avb_strlen(ab_suffix))) {
967 avb_error("Partition name and suffix does not fit.\n");
968 goto fail;
969 }
970
971 io_ret = ops->get_unique_guid_for_partition(
972 ops, part_name, guid_buf, sizeof guid_buf);
973 if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
974 return NULL;
975 } else if (io_ret != AVB_IO_RESULT_OK) {
976 avb_error("Error getting unique GUID for partition.\n");
977 goto fail;
978 }
979
980 if (ret == NULL) {
981 ret = avb_replace(cmdline, replace_str[n], guid_buf);
982 } else {
983 char* new_ret = avb_replace(ret, replace_str[n], guid_buf);
984 avb_free(ret);
985 ret = new_ret;
986 }
987 if (ret == NULL) {
988 goto fail;
989 }
990 }
991
992 return ret;
993
994fail:
995 if (ret != NULL) {
996 avb_free(ret);
997 }
998 return NULL;
999}
1000
1001static int cmdline_append_option(AvbSlotVerifyData* slot_data,
1002 const char* key,
1003 const char* value) {
1004 size_t offset, key_len, value_len;
1005 char* new_cmdline;
1006
1007 key_len = avb_strlen(key);
1008 value_len = avb_strlen(value);
1009
1010 offset = 0;
1011 if (slot_data->cmdline != NULL) {
1012 offset = avb_strlen(slot_data->cmdline);
1013 if (offset > 0) {
1014 offset += 1;
1015 }
1016 }
1017
1018 new_cmdline = avb_calloc(offset + key_len + value_len + 2);
1019 if (new_cmdline == NULL) {
1020 return 0;
1021 }
1022 if (offset > 0) {
1023 avb_memcpy(new_cmdline, slot_data->cmdline, offset - 1);
1024 new_cmdline[offset - 1] = ' ';
1025 }
1026 avb_memcpy(new_cmdline + offset, key, key_len);
1027 new_cmdline[offset + key_len] = '=';
1028 avb_memcpy(new_cmdline + offset + key_len + 1, value, value_len);
1029 if (slot_data->cmdline != NULL) {
1030 avb_free(slot_data->cmdline);
1031 }
1032 slot_data->cmdline = new_cmdline;
1033
1034 return 1;
1035}
1036
1037#define AVB_MAX_DIGITS_UINT64 32
1038
1039/* Writes |value| to |digits| in base 10 followed by a NUL byte.
1040 * Returns number of characters written excluding the NUL byte.
1041 */
1042static size_t uint64_to_base10(uint64_t value,
1043 char digits[AVB_MAX_DIGITS_UINT64]) {
1044 char rev_digits[AVB_MAX_DIGITS_UINT64];
1045 size_t n, num_digits;
1046
1047 for (num_digits = 0; num_digits < AVB_MAX_DIGITS_UINT64 - 1;) {
1048 rev_digits[num_digits++] = (value % 10) + '0';
1049 value /= 10;
1050 if (value == 0) {
1051 break;
1052 }
1053 }
1054
1055 for (n = 0; n < num_digits; n++) {
1056 digits[n] = rev_digits[num_digits - 1 - n];
1057 }
1058 digits[n] = '\0';
1059 return n;
1060}
1061
1062static int cmdline_append_version(AvbSlotVerifyData* slot_data,
1063 const char* key,
1064 uint64_t major_version,
1065 uint64_t minor_version) {
1066 char major_digits[AVB_MAX_DIGITS_UINT64];
1067 char minor_digits[AVB_MAX_DIGITS_UINT64];
1068 char combined[AVB_MAX_DIGITS_UINT64 * 2 + 1];
1069 size_t num_major_digits, num_minor_digits;
1070
1071 num_major_digits = uint64_to_base10(major_version, major_digits);
1072 num_minor_digits = uint64_to_base10(minor_version, minor_digits);
1073 avb_memcpy(combined, major_digits, num_major_digits);
1074 combined[num_major_digits] = '.';
1075 avb_memcpy(combined + num_major_digits + 1, minor_digits, num_minor_digits);
1076 combined[num_major_digits + 1 + num_minor_digits] = '\0';
1077
1078 return cmdline_append_option(slot_data, key, combined);
1079}
1080
1081static int cmdline_append_uint64_base10(AvbSlotVerifyData* slot_data,
1082 const char* key,
1083 uint64_t value) {
1084 char digits[AVB_MAX_DIGITS_UINT64];
1085 uint64_to_base10(value, digits);
1086 return cmdline_append_option(slot_data, key, digits);
1087}
1088
1089static int cmdline_append_hex(AvbSlotVerifyData* slot_data,
1090 const char* key,
1091 const uint8_t* data,
1092 size_t data_len) {
1093 char hex_digits[17] = "0123456789abcdef";
1094 char* hex_data;
1095 int ret;
1096 size_t n;
1097
1098 hex_data = avb_malloc(data_len * 2 + 1);
1099 if (hex_data == NULL) {
1100 return 0;
1101 }
1102
1103 for (n = 0; n < data_len; n++) {
1104 hex_data[n * 2] = hex_digits[data[n] >> 4];
1105 hex_data[n * 2 + 1] = hex_digits[data[n] & 0x0f];
1106 }
1107 hex_data[n * 2] = '\0';
1108
1109 ret = cmdline_append_option(slot_data, key, hex_data);
1110 avb_free(hex_data);
1111 return ret;
1112}
1113
1114static AvbSlotVerifyResult append_options(
1115 AvbOps* ops,
1116 AvbSlotVerifyData* slot_data,
1117 AvbVBMetaImageHeader* toplevel_vbmeta,
1118 AvbAlgorithmType algorithm_type,
1119 AvbHashtreeErrorMode hashtree_error_mode) {
1120 AvbSlotVerifyResult ret;
1121 const char* verity_mode = NULL;
1122 bool is_device_unlocked;
1123 AvbIOResult io_ret;
1124
1125 /* Add androidboot.vbmeta.device option. */
1126 if (!cmdline_append_option(slot_data,
1127 "androidboot.vbmeta.device",
1128 "PARTUUID=$(ANDROID_VBMETA_PARTUUID)")) {
1129 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1130 goto out;
1131 }
1132
1133 /* Add androidboot.vbmeta.avb_version option. */
1134 if (!cmdline_append_version(slot_data,
1135 "androidboot.vbmeta.avb_version",
1136 AVB_VERSION_MAJOR,
1137 AVB_VERSION_MINOR)) {
1138 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1139 goto out;
1140 }
1141
1142 /* Set androidboot.avb.device_state to "locked" or "unlocked". */
1143 io_ret = ops->read_is_device_unlocked(ops, &is_device_unlocked);
1144 if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
1145 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1146 goto out;
1147 } else if (io_ret != AVB_IO_RESULT_OK) {
1148 avb_error("Error getting device state.\n");
1149 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
1150 goto out;
1151 }
1152 if (!cmdline_append_option(slot_data,
1153 "androidboot.vbmeta.device_state",
1154 is_device_unlocked ? "unlocked" : "locked")) {
1155 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1156 goto out;
1157 }
1158
1159 /* Set androidboot.vbmeta.{hash_alg, size, digest} - use same hash
1160 * function as is used to sign vbmeta.
1161 */
1162 switch (algorithm_type) {
1163 /* Explicit fallthrough. */
1164 case AVB_ALGORITHM_TYPE_NONE:
1165 case AVB_ALGORITHM_TYPE_SHA256_RSA2048:
1166 case AVB_ALGORITHM_TYPE_SHA256_RSA4096:
1167 case AVB_ALGORITHM_TYPE_SHA256_RSA8192: {
1168 size_t n, prev_sz = 0, total_size = 0;
1169 uint8_t* digest = NULL;
1170 uint8_t* tbuf = NULL;
1171
1172 digest = avb_malloc(AVB_SHA256_DIGEST_SIZE);
Monika Singh0f7bfc82018-04-16 23:14:29 +05301173 if(digest == NULL)
1174 {
1175 avb_error("Failed to allocate memory\n");
1176 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
1177 goto out;
1178 }
Monika Singhb15747d2017-09-25 14:01:13 +05301179 for (n = 0; n < slot_data->num_vbmeta_images; n++) {
1180 total_size += slot_data->vbmeta_images[n].vbmeta_size;
1181 }
1182 tbuf = avb_malloc(total_size);
1183
1184 for (n = 0; n < slot_data->num_vbmeta_images; n++) {
1185 avb_memcpy(tbuf + prev_sz, slot_data->vbmeta_images[n].vbmeta_data,
1186 slot_data->vbmeta_images[n].vbmeta_size);
1187 prev_sz = slot_data->vbmeta_images[n].vbmeta_size;
1188 }
1189 hash_find((unsigned char *)tbuf, total_size, digest, CRYPTO_AUTH_ALG_SHA256);
1190 avb_free(tbuf);
1191
1192 if (!cmdline_append_option(
1193 slot_data, "androidboot.vbmeta.hash_alg", "sha256") ||
1194 !cmdline_append_uint64_base10(
1195 slot_data, "androidboot.vbmeta.size", total_size) ||
1196 !cmdline_append_hex(slot_data,
1197 "androidboot.vbmeta.digest",
1198 digest,
1199 AVB_SHA256_DIGEST_SIZE)) {
1200 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1201 if (digest)
1202 avb_free(digest);
1203 goto out;
1204 }
1205 if (digest)
1206 avb_free(digest);
1207 } break;
1208 /* Explicit fallthrough. */
1209 case AVB_ALGORITHM_TYPE_SHA512_RSA2048:
1210 case AVB_ALGORITHM_TYPE_SHA512_RSA4096:
1211 case AVB_ALGORITHM_TYPE_SHA512_RSA8192: {
1212 AvbSHA512Ctx ctx;
1213 size_t n, total_size = 0;
1214 avb_sha512_init(&ctx);
1215 for (n = 0; n < slot_data->num_vbmeta_images; n++) {
1216 avb_sha512_update(&ctx,
1217 slot_data->vbmeta_images[n].vbmeta_data,
1218 slot_data->vbmeta_images[n].vbmeta_size);
1219 total_size += slot_data->vbmeta_images[n].vbmeta_size;
1220 }
1221 if (!cmdline_append_option(
1222 slot_data, "androidboot.vbmeta.hash_alg", "sha512") ||
1223 !cmdline_append_uint64_base10(
1224 slot_data, "androidboot.vbmeta.size", total_size) ||
1225 !cmdline_append_hex(slot_data,
1226 "androidboot.vbmeta.digest",
1227 avb_sha512_final(&ctx),
1228 AVB_SHA512_DIGEST_SIZE)) {
1229 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1230 goto out;
1231 }
1232 } break;
1233 case _AVB_ALGORITHM_NUM_TYPES:
1234 avb_assert_not_reached();
1235 break;
1236 }
1237
1238 /* Set androidboot.veritymode and androidboot.vbmeta.invalidate_on_error */
1239 if (toplevel_vbmeta->flags & AVB_VBMETA_IMAGE_FLAGS_HASHTREE_DISABLED) {
1240 verity_mode = "disabled";
1241 } else {
1242 const char* dm_verity_mode = NULL;
1243 char* new_ret;
1244
1245 switch (hashtree_error_mode) {
1246 case AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE:
1247 if (!cmdline_append_option(
1248 slot_data, "androidboot.vbmeta.invalidate_on_error", "yes")) {
1249 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1250 goto out;
1251 }
1252 verity_mode = "enforcing";
1253 dm_verity_mode = "restart_on_corruption";
1254 break;
1255 case AVB_HASHTREE_ERROR_MODE_RESTART:
1256 verity_mode = "enforcing";
1257 dm_verity_mode = "restart_on_corruption";
1258 break;
1259 case AVB_HASHTREE_ERROR_MODE_EIO:
1260 verity_mode = "eio";
1261 /* For now there's no option to specify the EIO mode. So
1262 * just use 'ignore_zero_blocks' since that's already set
1263 * and dm-verity-target.c supports specifying this multiple
1264 * times.
1265 */
1266 dm_verity_mode = "ignore_zero_blocks";
1267 break;
1268 case AVB_HASHTREE_ERROR_MODE_LOGGING:
1269 verity_mode = "logging";
1270 dm_verity_mode = "ignore_corruption";
1271 break;
1272 }
1273 new_ret = avb_replace(
1274 slot_data->cmdline, "$(ANDROID_VERITY_MODE)", dm_verity_mode);
1275 avb_free(slot_data->cmdline);
1276 slot_data->cmdline = new_ret;
1277 if (slot_data->cmdline == NULL) {
1278 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1279 goto out;
1280 }
1281 }
1282 if (!cmdline_append_option(
1283 slot_data, "androidboot.veritymode", verity_mode)) {
1284 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1285 goto out;
1286 }
1287
1288 ret = AVB_SLOT_VERIFY_RESULT_OK;
1289
1290out:
1291
1292 return ret;
1293}
1294
1295AvbSlotVerifyResult avb_slot_verify(AvbOps* ops,
1296 const char* const* requested_partitions,
1297 const char* ab_suffix,
1298 AvbSlotVerifyFlags flags,
1299 AvbHashtreeErrorMode hashtree_error_mode,
1300 AvbSlotVerifyData** out_data) {
1301 AvbSlotVerifyResult ret;
1302 AvbSlotVerifyData* slot_data = NULL;
1303 AvbAlgorithmType algorithm_type = AVB_ALGORITHM_TYPE_NONE;
1304 bool using_boot_for_vbmeta = false;
1305 AvbVBMetaImageHeader toplevel_vbmeta;
1306 bool allow_verification_error =
1307 (flags & AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR);
1308
1309 /* Fail early if we're missing the AvbOps needed for slot verification.
1310 *
1311 * For now, handle get_size_of_partition() not being implemented. In
1312 * a later release we may change that.
1313 */
1314 avb_assert(ops->read_is_device_unlocked != NULL);
1315 avb_assert(ops->read_from_partition != NULL);
1316 avb_assert(ops->validate_vbmeta_public_key != NULL);
1317 avb_assert(ops->read_rollback_index != NULL);
1318 avb_assert(ops->get_unique_guid_for_partition != NULL);
1319 /* avb_assert(ops->get_size_of_partition != NULL); */
1320
1321 if (out_data != NULL) {
1322 *out_data = NULL;
1323 }
1324
1325 /* Allowing dm-verity errors defeats the purpose of verified boot so
1326 * only allow this if set up to allow verification errors
1327 * (e.g. typically only UNLOCKED mode).
1328 */
1329 if (hashtree_error_mode == AVB_HASHTREE_ERROR_MODE_LOGGING &&
1330 !allow_verification_error) {
1331 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT;
1332 goto fail;
1333 }
1334
1335 slot_data = avb_calloc(sizeof(AvbSlotVerifyData));
1336 if (slot_data == NULL) {
1337 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1338 goto fail;
1339 }
1340 slot_data->vbmeta_images =
1341 avb_calloc(sizeof(AvbVBMetaData) * MAX_NUMBER_OF_VBMETA_IMAGES);
1342 if (slot_data->vbmeta_images == NULL) {
1343 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1344 goto fail;
1345 }
1346 slot_data->loaded_partitions =
1347 avb_calloc(sizeof(AvbPartitionData) * MAX_NUMBER_OF_LOADED_PARTITIONS);
1348 if (slot_data->loaded_partitions == NULL) {
1349 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1350 goto fail;
1351 }
1352
1353 ret = load_and_verify_vbmeta(ops,
1354 requested_partitions,
1355 ab_suffix,
1356 allow_verification_error,
1357 0 /* toplevel_vbmeta_flags */,
1358 0 /* rollback_index_location */,
1359 "vbmeta",
1360 avb_strlen("vbmeta"),
1361 NULL /* expected_public_key */,
1362 0 /* expected_public_key_length */,
1363 slot_data,
1364 &algorithm_type);
1365 if (!allow_verification_error && ret != AVB_SLOT_VERIFY_RESULT_OK) {
1366 goto fail;
1367 }
1368
1369 /* If things check out, mangle the kernel command-line as needed. */
1370 if (result_should_continue(ret)) {
1371 if (avb_strcmp(slot_data->vbmeta_images[0].partition_name, "vbmeta") != 0) {
1372 avb_assert(
1373 avb_strcmp(slot_data->vbmeta_images[0].partition_name, "boot") == 0);
1374 using_boot_for_vbmeta = true;
1375 }
1376
1377 /* Byteswap top-level vbmeta header since we'll need it below. */
1378 avb_vbmeta_image_header_to_host_byte_order(
1379 (const AvbVBMetaImageHeader*)slot_data->vbmeta_images[0].vbmeta_data,
1380 &toplevel_vbmeta);
1381
1382 /* Fill in |ab_suffix| field. */
1383 slot_data->ab_suffix = avb_strdup(ab_suffix);
1384 if (slot_data->ab_suffix == NULL) {
1385 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1386 goto fail;
1387 }
1388
1389 /* If verification is disabled, we are done ... we specifically
1390 * don't want to add any androidboot.* options since verification
1391 * is disabled.
1392 */
1393 if (toplevel_vbmeta.flags & AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED) {
1394 /* Since verification is disabled we didn't process any
1395 * descriptors and thus there's no cmdline... so set root= such
1396 * that the system partition is mounted.
1397 */
1398 avb_assert(slot_data->cmdline == NULL);
1399 slot_data->cmdline =
1400 avb_strdup("root=PARTUUID=$(ANDROID_SYSTEM_PARTUUID)");
1401 if (slot_data->cmdline == NULL) {
1402 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1403 goto fail;
1404 }
1405 } else {
1406 /* Add options - any failure in append_options() is either an
1407 * I/O or OOM error.
1408 */
1409 AvbSlotVerifyResult sub_ret = append_options(ops,
1410 slot_data,
1411 &toplevel_vbmeta,
1412 algorithm_type,
1413 hashtree_error_mode);
1414 if (sub_ret != AVB_SLOT_VERIFY_RESULT_OK) {
1415 ret = sub_ret;
1416 goto fail;
1417 }
1418 }
1419
1420 /* Substitute $(ANDROID_SYSTEM_PARTUUID) and friends. */
1421 if (slot_data->cmdline != NULL) {
1422 char* new_cmdline;
1423 new_cmdline = sub_cmdline(
1424 ops, slot_data->cmdline, ab_suffix, using_boot_for_vbmeta);
1425 if (new_cmdline == NULL) {
1426 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1427 goto fail;
1428 }
1429 avb_free(slot_data->cmdline);
1430 slot_data->cmdline = new_cmdline;
1431 }
1432
1433 if (out_data != NULL) {
1434 *out_data = slot_data;
1435 } else {
1436 avb_slot_verify_data_free(slot_data);
1437 }
1438 }
1439
1440 if (!allow_verification_error) {
1441 avb_assert(ret == AVB_SLOT_VERIFY_RESULT_OK);
1442 }
1443
1444 return ret;
1445
1446fail:
1447 if (slot_data != NULL) {
1448 avb_slot_verify_data_free(slot_data);
1449 }
1450 return ret;
1451}
1452
1453void avb_slot_verify_data_free(AvbSlotVerifyData* data) {
1454 if (data->ab_suffix != NULL) {
1455 avb_free(data->ab_suffix);
1456 }
1457 if (data->cmdline != NULL) {
1458 avb_free(data->cmdline);
1459 }
1460 if (data->vbmeta_images != NULL) {
1461 size_t n;
1462 for (n = 0; n < data->num_vbmeta_images; n++) {
1463 AvbVBMetaData* vbmeta_image = &data->vbmeta_images[n];
1464 if (vbmeta_image->partition_name != NULL) {
1465 avb_free(vbmeta_image->partition_name);
1466 }
1467 if (vbmeta_image->vbmeta_data != NULL) {
1468 vbmeta_image->vbmeta_data = NULL;
1469 }
1470 }
1471 avb_free(data->vbmeta_images);
1472 }
1473 if (data->loaded_partitions != NULL) {
1474 size_t n;
1475 for (n = 0; n < data->num_loaded_partitions; n++) {
1476 AvbPartitionData* loaded_partition = &data->loaded_partitions[n];
1477 if (loaded_partition->partition_name != NULL) {
1478 avb_free(loaded_partition->partition_name);
1479 }
1480 if (loaded_partition->data != NULL) {
1481 if (data->num_loaded_partitions == 1)
1482 loaded_partition->data = NULL;
1483 }
1484 }
1485 avb_free(data->loaded_partitions);
1486 }
1487 avb_free(data);
1488}
1489
1490const char* avb_slot_verify_result_to_string(AvbSlotVerifyResult result) {
1491 const char* ret = NULL;
1492
1493 switch (result) {
1494 case AVB_SLOT_VERIFY_RESULT_OK:
1495 ret = "OK";
1496 break;
1497 case AVB_SLOT_VERIFY_RESULT_ERROR_OOM:
1498 ret = "ERROR_OOM";
1499 break;
1500 case AVB_SLOT_VERIFY_RESULT_ERROR_IO:
1501 ret = "ERROR_IO";
1502 break;
1503 case AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION:
1504 ret = "ERROR_VERIFICATION";
1505 break;
1506 case AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX:
1507 ret = "ERROR_ROLLBACK_INDEX";
1508 break;
1509 case AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED:
1510 ret = "ERROR_PUBLIC_KEY_REJECTED";
1511 break;
1512 case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA:
1513 ret = "ERROR_INVALID_METADATA";
1514 break;
1515 case AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION:
1516 ret = "ERROR_UNSUPPORTED_VERSION";
1517 break;
1518 case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT:
1519 ret = "ERROR_INVALID_ARGUMENT";
1520 break;
1521 /* Do not add a 'default:' case here because of -Wswitch. */
1522 }
1523
1524 if (ret == NULL) {
1525 avb_error("Unknown AvbSlotVerifyResult value.\n");
1526 ret = "(unknown)";
1527 }
1528
1529 return ret;
1530}