1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include "avb_cmdline.h"
26 #include "avb_sha.h"
27 #include "avb_util.h"
28 #include "avb_version.h"
29
30 #define NUM_GUIDS 3
31
32 /* Substitutes all variables (e.g. $(ANDROID_SYSTEM_PARTUUID)) with
33 * values. Returns NULL on OOM, otherwise the cmdline with values
34 * replaced.
35 */
avb_sub_cmdline(AvbOps * ops,const char * cmdline,const char * ab_suffix,bool using_boot_for_vbmeta,const AvbCmdlineSubstList * additional_substitutions)36 char* avb_sub_cmdline(AvbOps* ops,
37 const char* cmdline,
38 const char* ab_suffix,
39 bool using_boot_for_vbmeta,
40 const AvbCmdlineSubstList* additional_substitutions) {
41 const char* part_name_str[NUM_GUIDS] = {"system", "boot", "vbmeta"};
42 const char* replace_str[NUM_GUIDS] = {"$(ANDROID_SYSTEM_PARTUUID)",
43 "$(ANDROID_BOOT_PARTUUID)",
44 "$(ANDROID_VBMETA_PARTUUID)"};
45 char* ret = NULL;
46 AvbIOResult io_ret;
47 size_t n;
48
49 /* Special-case for when the top-level vbmeta struct is in the boot
50 * partition.
51 */
52 if (using_boot_for_vbmeta) {
53 part_name_str[2] = "boot";
54 }
55
56 /* Replace unique partition GUIDs */
57 for (n = 0; n < NUM_GUIDS; n++) {
58 char part_name[AVB_PART_NAME_MAX_SIZE];
59 char guid_buf[37];
60
61 /* Don't attempt to query the partition guid unless its search string is
62 * present in the command line. Note: the original cmdline is used here,
63 * not the replaced one. See b/116010959.
64 */
65 if (avb_strstr(cmdline, replace_str[n]) == NULL) {
66 continue;
67 }
68
69 if (!avb_str_concat(part_name,
70 sizeof part_name,
71 part_name_str[n],
72 avb_strlen(part_name_str[n]),
73 ab_suffix,
74 avb_strlen(ab_suffix))) {
75 avb_error("Partition name and suffix does not fit.\n");
76 goto fail;
77 }
78
79 io_ret = ops->get_unique_guid_for_partition(
80 ops, part_name, guid_buf, sizeof guid_buf);
81 if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
82 goto fail;
83 } else if (io_ret != AVB_IO_RESULT_OK) {
84 avb_error("Error getting unique GUID for partition.\n");
85 goto fail;
86 }
87
88 if (ret == NULL) {
89 ret = avb_replace(cmdline, replace_str[n], guid_buf);
90 } else {
91 char* new_ret = avb_replace(ret, replace_str[n], guid_buf);
92 avb_free(ret);
93 ret = new_ret;
94 }
95 if (ret == NULL) {
96 goto fail;
97 }
98 }
99
100 /* It's possible there is no _PARTUUID for replacement above.
101 * Duplicate cmdline to ret for additional substitutions below.
102 */
103 if (ret == NULL) {
104 ret = avb_strdup(cmdline);
105 if (ret == NULL) {
106 goto fail;
107 }
108 }
109
110 /* Replace any additional substitutions. */
111 if (additional_substitutions != NULL) {
112 for (n = 0; n < additional_substitutions->size; ++n) {
113 char* new_ret = avb_replace(ret,
114 additional_substitutions->tokens[n],
115 additional_substitutions->values[n]);
116 avb_free(ret);
117 ret = new_ret;
118 if (ret == NULL) {
119 goto fail;
120 }
121 }
122 }
123
124 return ret;
125
126 fail:
127 if (ret != NULL) {
128 avb_free(ret);
129 }
130 return NULL;
131 }
132
cmdline_append_option(AvbSlotVerifyData * slot_data,const char * key,const char * value)133 static int cmdline_append_option(AvbSlotVerifyData* slot_data,
134 const char* key,
135 const char* value) {
136 size_t offset, key_len, value_len;
137 char* new_cmdline;
138
139 key_len = avb_strlen(key);
140 value_len = avb_strlen(value);
141
142 offset = 0;
143 if (slot_data->cmdline != NULL) {
144 offset = avb_strlen(slot_data->cmdline);
145 if (offset > 0) {
146 offset += 1;
147 }
148 }
149
150 new_cmdline = avb_calloc(offset + key_len + value_len + 2);
151 if (new_cmdline == NULL) {
152 return 0;
153 }
154 if (offset > 0) {
155 avb_memcpy(new_cmdline, slot_data->cmdline, offset - 1);
156 new_cmdline[offset - 1] = ' ';
157 }
158 avb_memcpy(new_cmdline + offset, key, key_len);
159 new_cmdline[offset + key_len] = '=';
160 avb_memcpy(new_cmdline + offset + key_len + 1, value, value_len);
161 if (slot_data->cmdline != NULL) {
162 avb_free(slot_data->cmdline);
163 }
164 slot_data->cmdline = new_cmdline;
165
166 return 1;
167 }
168
cmdline_append_version(AvbSlotVerifyData * slot_data,const char * key,uint64_t major_version,uint64_t minor_version)169 static int cmdline_append_version(AvbSlotVerifyData* slot_data,
170 const char* key,
171 uint64_t major_version,
172 uint64_t minor_version) {
173 char major_digits[AVB_MAX_DIGITS_UINT64];
174 char minor_digits[AVB_MAX_DIGITS_UINT64];
175 char combined[AVB_MAX_DIGITS_UINT64 * 2 + 1];
176 size_t num_major_digits, num_minor_digits;
177
178 num_major_digits = avb_uint64_to_base10(major_version, major_digits);
179 num_minor_digits = avb_uint64_to_base10(minor_version, minor_digits);
180 avb_memcpy(combined, major_digits, num_major_digits);
181 combined[num_major_digits] = '.';
182 avb_memcpy(combined + num_major_digits + 1, minor_digits, num_minor_digits);
183 combined[num_major_digits + 1 + num_minor_digits] = '\0';
184
185 return cmdline_append_option(slot_data, key, combined);
186 }
187
cmdline_append_uint64_base10(AvbSlotVerifyData * slot_data,const char * key,uint64_t value)188 static int cmdline_append_uint64_base10(AvbSlotVerifyData* slot_data,
189 const char* key,
190 uint64_t value) {
191 char digits[AVB_MAX_DIGITS_UINT64];
192 avb_uint64_to_base10(value, digits);
193 return cmdline_append_option(slot_data, key, digits);
194 }
195
cmdline_append_hex(AvbSlotVerifyData * slot_data,const char * key,const uint8_t * data,size_t data_len)196 static int cmdline_append_hex(AvbSlotVerifyData* slot_data,
197 const char* key,
198 const uint8_t* data,
199 size_t data_len) {
200 int ret;
201 char* hex_data = avb_bin2hex(data, data_len);
202 if (hex_data == NULL) {
203 return 0;
204 }
205 ret = cmdline_append_option(slot_data, key, hex_data);
206 avb_free(hex_data);
207 return ret;
208 }
209
cmdline_get_digest_name(AvbDigestType avb_digest_type)210 static const char* cmdline_get_digest_name(AvbDigestType avb_digest_type) {
211 const char* ret = NULL;
212 switch (avb_digest_type) {
213 case AVB_DIGEST_TYPE_SHA256:
214 ret = "sha256";
215 break;
216 case AVB_DIGEST_TYPE_SHA512:
217 ret = "sha512";
218 break;
219 /* Do not add a 'default:' case here because of -Wswitch. */
220 }
221
222 if (ret == NULL) {
223 avb_error("Unknown AvbDigestType.\n");
224 ret = "unknown";
225 }
226
227 return ret;
228 }
229
avb_append_options(AvbOps * ops,AvbSlotVerifyFlags flags,AvbSlotVerifyData * slot_data,AvbVBMetaImageHeader * toplevel_vbmeta,const uint8_t * toplevel_vbmeta_public_key_data,size_t toplevel_vbmeta_public_key_length,AvbAlgorithmType algorithm_type,AvbHashtreeErrorMode hashtree_error_mode,AvbHashtreeErrorMode resolved_hashtree_error_mode)230 AvbSlotVerifyResult avb_append_options(
231 AvbOps* ops,
232 AvbSlotVerifyFlags flags,
233 AvbSlotVerifyData* slot_data,
234 AvbVBMetaImageHeader* toplevel_vbmeta,
235 const uint8_t* toplevel_vbmeta_public_key_data,
236 size_t toplevel_vbmeta_public_key_length,
237 AvbAlgorithmType algorithm_type,
238 AvbHashtreeErrorMode hashtree_error_mode,
239 AvbHashtreeErrorMode resolved_hashtree_error_mode) {
240 AvbSlotVerifyResult ret;
241 const char* verity_mode;
242 bool is_device_unlocked;
243 AvbIOResult io_ret;
244 char* requested_partition_hash_alg = NULL;
245 char* requested_partition_digest = NULL;
246
247 /* Add options that only make sense if there is a vbmeta partition. */
248 if (!(flags & AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION)) {
249 /* Add androidboot.vbmeta.device option. */
250 if (!cmdline_append_option(slot_data,
251 "androidboot.vbmeta.device",
252 "PARTUUID=$(ANDROID_VBMETA_PARTUUID)")) {
253 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
254 goto out;
255 }
256
257 /* Set androidboot.vbmeta.public_key_digest to the SHA-256 hash of the
258 * public key used to verify the vbmeta image. */
259 if (toplevel_vbmeta_public_key_data != NULL &&
260 toplevel_vbmeta_public_key_length > 0) {
261 AvbSHA256Ctx ctx;
262 avb_sha256_init(&ctx);
263 avb_sha256_update(&ctx,
264 toplevel_vbmeta_public_key_data,
265 toplevel_vbmeta_public_key_length);
266 uint8_t* vbmeta_public_key_digest = avb_sha256_final(&ctx);
267 if (!cmdline_append_hex(slot_data,
268 "androidboot.vbmeta.public_key_digest",
269 vbmeta_public_key_digest,
270 AVB_SHA256_DIGEST_SIZE)) {
271 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
272 goto out;
273 }
274 }
275 }
276
277 /* Add androidboot.vbmeta.avb_version option. */
278 if (!cmdline_append_version(slot_data,
279 "androidboot.vbmeta.avb_version",
280 AVB_VERSION_MAJOR,
281 AVB_VERSION_MINOR)) {
282 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
283 goto out;
284 }
285
286 /* Set androidboot.avb.device_state to "locked" or "unlocked". */
287 io_ret = ops->read_is_device_unlocked(ops, &is_device_unlocked);
288 if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
289 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
290 goto out;
291 } else if (io_ret != AVB_IO_RESULT_OK) {
292 avb_error("Error getting device state.\n");
293 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
294 goto out;
295 }
296 if (!cmdline_append_option(slot_data,
297 "androidboot.vbmeta.device_state",
298 is_device_unlocked ? "unlocked" : "locked")) {
299 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
300 goto out;
301 }
302
303 /* Set androidboot.vbmeta.{hash_alg, size, digest} - use same hash
304 * function as is used to sign vbmeta.
305 */
306 switch (algorithm_type) {
307 /* Explicit fallthrough. */
308 case AVB_ALGORITHM_TYPE_NONE:
309 case AVB_ALGORITHM_TYPE_SHA256_RSA2048:
310 case AVB_ALGORITHM_TYPE_SHA256_RSA4096:
311 case AVB_ALGORITHM_TYPE_SHA256_RSA8192: {
312 size_t n, total_size = 0;
313 uint8_t vbmeta_digest[AVB_SHA256_DIGEST_SIZE];
314 avb_slot_verify_data_calculate_vbmeta_digest(
315 slot_data, AVB_DIGEST_TYPE_SHA256, vbmeta_digest);
316 for (n = 0; n < slot_data->num_vbmeta_images; n++) {
317 total_size += slot_data->vbmeta_images[n].vbmeta_size;
318 }
319 if (!cmdline_append_option(
320 slot_data, "androidboot.vbmeta.hash_alg", "sha256") ||
321 !cmdline_append_uint64_base10(
322 slot_data, "androidboot.vbmeta.size", total_size) ||
323 !cmdline_append_hex(slot_data,
324 "androidboot.vbmeta.digest",
325 vbmeta_digest,
326 AVB_SHA256_DIGEST_SIZE)) {
327 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
328 goto out;
329 }
330 } break;
331 /* Explicit fallthrough. */
332 case AVB_ALGORITHM_TYPE_SHA512_RSA2048:
333 case AVB_ALGORITHM_TYPE_SHA512_RSA4096:
334 case AVB_ALGORITHM_TYPE_SHA512_RSA8192: {
335 size_t n, total_size = 0;
336 uint8_t vbmeta_digest[AVB_SHA512_DIGEST_SIZE];
337 avb_slot_verify_data_calculate_vbmeta_digest(
338 slot_data, AVB_DIGEST_TYPE_SHA512, vbmeta_digest);
339 for (n = 0; n < slot_data->num_vbmeta_images; n++) {
340 total_size += slot_data->vbmeta_images[n].vbmeta_size;
341 }
342 if (!cmdline_append_option(
343 slot_data, "androidboot.vbmeta.hash_alg", "sha512") ||
344 !cmdline_append_uint64_base10(
345 slot_data, "androidboot.vbmeta.size", total_size) ||
346 !cmdline_append_hex(slot_data,
347 "androidboot.vbmeta.digest",
348 vbmeta_digest,
349 AVB_SHA512_DIGEST_SIZE)) {
350 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
351 goto out;
352 }
353 } break;
354 case _AVB_ALGORITHM_NUM_TYPES:
355 avb_assert_not_reached();
356 break;
357 }
358
359 /* Set androidboot.veritymode and androidboot.vbmeta.invalidate_on_error */
360 if (toplevel_vbmeta->flags & AVB_VBMETA_IMAGE_FLAGS_HASHTREE_DISABLED) {
361 verity_mode = "disabled";
362 } else {
363 const char* dm_verity_mode;
364 char* new_ret;
365
366 switch (resolved_hashtree_error_mode) {
367 case AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE:
368 if (!cmdline_append_option(
369 slot_data, "androidboot.vbmeta.invalidate_on_error", "yes")) {
370 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
371 goto out;
372 }
373 verity_mode = "enforcing";
374 dm_verity_mode = "restart_on_corruption";
375 break;
376 case AVB_HASHTREE_ERROR_MODE_RESTART:
377 verity_mode = "enforcing";
378 dm_verity_mode = "restart_on_corruption";
379 break;
380 case AVB_HASHTREE_ERROR_MODE_EIO:
381 verity_mode = "eio";
382 /* For now there's no option to specify the EIO mode. So
383 * just use 'ignore_zero_blocks' since that's already set
384 * and dm-verity-target.c supports specifying this multiple
385 * times.
386 */
387 dm_verity_mode = "ignore_zero_blocks";
388 break;
389 case AVB_HASHTREE_ERROR_MODE_LOGGING:
390 verity_mode = "logging";
391 dm_verity_mode = "ignore_corruption";
392 break;
393 case AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO:
394 // Should never get here because MANAGED_RESTART_AND_EIO is
395 // remapped by avb_manage_hashtree_error_mode().
396 avb_assert_not_reached();
397 break;
398 case AVB_HASHTREE_ERROR_MODE_PANIC:
399 verity_mode = "panicking";
400 dm_verity_mode = "panic_on_corruption";
401 break;
402 }
403 new_ret = avb_replace(
404 slot_data->cmdline, "$(ANDROID_VERITY_MODE)", dm_verity_mode);
405 avb_free(slot_data->cmdline);
406 slot_data->cmdline = new_ret;
407 if (slot_data->cmdline == NULL) {
408 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
409 goto out;
410 }
411 }
412 if (!cmdline_append_option(
413 slot_data, "androidboot.veritymode", verity_mode)) {
414 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
415 goto out;
416 }
417 if (hashtree_error_mode == AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO) {
418 if (!cmdline_append_option(
419 slot_data, "androidboot.veritymode.managed", "yes")) {
420 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
421 goto out;
422 }
423 }
424
425 size_t i;
426 for (i = 0; i < slot_data->num_loaded_partitions; i++) {
427 if (slot_data->loaded_partitions[i].partition_name != NULL &&
428 slot_data->loaded_partitions[i].digest != NULL) {
429 requested_partition_hash_alg =
430 avb_strdupv("androidboot.vbmeta.",
431 slot_data->loaded_partitions[i].partition_name,
432 ".hash_alg",
433 NULL);
434 if (requested_partition_hash_alg == NULL) {
435 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
436 goto out;
437 }
438
439 requested_partition_digest =
440 avb_strdupv("androidboot.vbmeta.",
441 slot_data->loaded_partitions[i].partition_name,
442 ".digest",
443 NULL);
444 if (requested_partition_digest == NULL) {
445 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
446 goto out;
447 }
448
449 if (!cmdline_append_option(
450 slot_data,
451 requested_partition_hash_alg,
452 cmdline_get_digest_name(
453 slot_data->loaded_partitions[i].digest_type)) ||
454 !cmdline_append_hex(slot_data,
455 requested_partition_digest,
456 slot_data->loaded_partitions[i].digest,
457 slot_data->loaded_partitions[i].digest_size)) {
458 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
459 goto out;
460 }
461
462 avb_free(requested_partition_hash_alg);
463 requested_partition_hash_alg = NULL;
464 avb_free(requested_partition_digest);
465 requested_partition_digest = NULL;
466 }
467 }
468
469 ret = AVB_SLOT_VERIFY_RESULT_OK;
470
471 out:
472 if (requested_partition_hash_alg != NULL) {
473 avb_free(requested_partition_hash_alg);
474 requested_partition_hash_alg = NULL;
475 }
476 if (requested_partition_digest != NULL) {
477 avb_free(requested_partition_digest);
478 requested_partition_digest = NULL;
479 }
480 return ret;
481 }
482
avb_new_cmdline_subst_list()483 AvbCmdlineSubstList* avb_new_cmdline_subst_list() {
484 return (AvbCmdlineSubstList*)avb_calloc(sizeof(AvbCmdlineSubstList));
485 }
486
avb_free_cmdline_subst_list(AvbCmdlineSubstList * cmdline_subst)487 void avb_free_cmdline_subst_list(AvbCmdlineSubstList* cmdline_subst) {
488 size_t i;
489 for (i = 0; i < cmdline_subst->size; ++i) {
490 avb_free(cmdline_subst->tokens[i]);
491 avb_free(cmdline_subst->values[i]);
492 }
493 cmdline_subst->size = 0;
494 avb_free(cmdline_subst);
495 }
496
avb_add_root_digest_substitution(const char * part_name,const uint8_t * digest,size_t digest_size,AvbCmdlineSubstList * out_cmdline_subst)497 AvbSlotVerifyResult avb_add_root_digest_substitution(
498 const char* part_name,
499 const uint8_t* digest,
500 size_t digest_size,
501 AvbCmdlineSubstList* out_cmdline_subst) {
502 const char* kDigestSubPrefix = "$(AVB_";
503 const char* kDigestSubSuffix = "_ROOT_DIGEST)";
504 size_t part_name_len = avb_strlen(part_name);
505 size_t list_index = out_cmdline_subst->size;
506
507 avb_assert(part_name_len < AVB_PART_NAME_MAX_SIZE);
508 avb_assert(digest_size <= AVB_SHA512_DIGEST_SIZE);
509 if (part_name_len >= AVB_PART_NAME_MAX_SIZE ||
510 digest_size > AVB_SHA512_DIGEST_SIZE) {
511 return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
512 }
513
514 if (out_cmdline_subst->size >= AVB_MAX_NUM_CMDLINE_SUBST) {
515 /* The list is full. Currently dynamic growth of this list is not supported.
516 */
517 return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
518 }
519
520 /* Construct the token to replace in the command line based on the partition
521 * name. For partition 'foo', this will be '$(AVB_FOO_ROOT_DIGEST)'.
522 */
523 out_cmdline_subst->tokens[list_index] =
524 avb_strdupv(kDigestSubPrefix, part_name, kDigestSubSuffix, NULL);
525 if (out_cmdline_subst->tokens[list_index] == NULL) {
526 goto fail;
527 }
528 avb_uppercase(out_cmdline_subst->tokens[list_index]);
529
530 /* The digest value is hex encoded when inserted in the command line. */
531 out_cmdline_subst->values[list_index] = avb_bin2hex(digest, digest_size);
532 if (out_cmdline_subst->values[list_index] == NULL) {
533 goto fail;
534 }
535
536 out_cmdline_subst->size++;
537 return AVB_SLOT_VERIFY_RESULT_OK;
538
539 fail:
540 if (out_cmdline_subst->tokens[list_index]) {
541 avb_free(out_cmdline_subst->tokens[list_index]);
542 }
543 if (out_cmdline_subst->values[list_index]) {
544 avb_free(out_cmdline_subst->values[list_index]);
545 }
546 return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
547 }
548