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