blob: 21b10196e9a213ca6a86ba74f1541465f2715936 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
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 */
36char *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{
42 const char *part_name_str[NUM_GUIDS] = {ROOTFS_PART_NAME, BOOT_PART_NAME, "vbmeta"};
43#ifdef BOOT_DEV_NAND
44 const char *replace_str[NUM_GUIDS] = {"PARTUUID=$(ANDROID_SYSTEM_PARTUUID)",
45 "$(ANDROID_BOOT_PARTUUID)",
46 "PARTUUID=$(ANDROID_VBMETA_PARTUUID)"
47 };
48#else
49 const char *replace_str[NUM_GUIDS] = {"$(ANDROID_SYSTEM_PARTUUID)",
50 "$(ANDROID_BOOT_PARTUUID)",
51 "$(ANDROID_VBMETA_PARTUUID)"
52 };
53#endif
54
55 char *ret = NULL;
56 AvbIOResult io_ret;
57 size_t n;
58
59 /* Special-case for when the top-level vbmeta struct is in the boot
60 * partition.
61 */
62 if (using_boot_for_vbmeta) {
63 part_name_str[2] = BOOT_PART_NAME;
64 }
65
66 /* Replace unique partition GUIDs */
67 for (n = 0; n < NUM_GUIDS; n++) {
68 char part_name[AVB_PART_NAME_MAX_SIZE];
69 char guid_buf[37];
70
71 /* Don't attempt to query the partition guid unless its search string is
72 * present in the command line. Note: the original cmdline is used here,
73 * not the replaced one. See b/116010959.
74 */
75 if (avb_strstr(cmdline, replace_str[n]) == NULL) {
76 continue;
77 }
78
79 if (!avb_str_concat(part_name,
80 sizeof part_name,
81 part_name_str[n],
82 avb_strlen(part_name_str[n]),
83 ab_suffix,
84 avb_strlen(ab_suffix))) {
85 avb_error("Partition name and suffix does not fit.\n");
86 goto fail;
87 }
88
89 io_ret = ops->get_unique_guid_for_partition(
90 ops, part_name, guid_buf, sizeof guid_buf);
91 if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
92 goto fail;
93 } else if (io_ret != AVB_IO_RESULT_OK) {
94 avb_error("Error getting unique GUID for partition.\n");
95 goto fail;
96 }
97
98 if (ret == NULL) {
99 ret = avb_replace(cmdline, replace_str[n], guid_buf);
100 } else {
101 char *new_ret = avb_replace(ret, replace_str[n], guid_buf);
102 avb_free(ret);
103 ret = new_ret;
104 }
105 if (ret == NULL) {
106 goto fail;
107 }
108 }
109
110 avb_assert(ret != NULL);
111
112 /* Replace any additional substitutions. */
113 if (additional_substitutions != NULL) {
114 for (n = 0; n < additional_substitutions->size; ++n) {
115 char *new_ret = avb_replace(ret,
116 additional_substitutions->tokens[n],
117 additional_substitutions->values[n]);
118 avb_free(ret);
119 ret = new_ret;
120 if (ret == NULL) {
121 goto fail;
122 }
123 }
124 }
125
126 return ret;
127
128fail:
129 if (ret != NULL) {
130 avb_free(ret);
131 }
132 return NULL;
133}
134
135static int cmdline_append_option(AvbSlotVerifyData *slot_data,
136 const char *key,
137 const char *value)
138{
139 size_t offset, key_len, value_len;
140 char *new_cmdline;
141
142 key_len = avb_strlen(key);
143 value_len = avb_strlen(value);
144
145 offset = 0;
146 if (slot_data->cmdline != NULL) {
147 offset = avb_strlen(slot_data->cmdline);
148 if (offset > 0) {
149 offset += 1;
150 }
151 }
152
153 new_cmdline = avb_calloc(offset + key_len + value_len + 2);
154 if (new_cmdline == NULL) {
155 return 0;
156 }
157 if (offset > 0) {
158 avb_memcpy(new_cmdline, slot_data->cmdline, offset - 1);
159 new_cmdline[offset - 1] = ' ';
160 }
161 avb_memcpy(new_cmdline + offset, key, key_len);
162 new_cmdline[offset + key_len] = '=';
163 avb_memcpy(new_cmdline + offset + key_len + 1, value, value_len);
164 if (slot_data->cmdline != NULL) {
165 avb_free(slot_data->cmdline);
166 }
167 slot_data->cmdline = new_cmdline;
168
169 return 1;
170}
171
172#define AVB_MAX_DIGITS_UINT64 32
173
174/* Writes |value| to |digits| in base 10 followed by a NUL byte.
175 * Returns number of characters written excluding the NUL byte.
176 */
177static size_t uint64_to_base10(uint64_t value,
178 char digits[AVB_MAX_DIGITS_UINT64])
179{
180 char rev_digits[AVB_MAX_DIGITS_UINT64];
181 size_t n, num_digits;
182
183 for (num_digits = 0; num_digits < AVB_MAX_DIGITS_UINT64 - 1;) {
184 rev_digits[num_digits++] = avb_div_by_10(&value) + '0';
185 if (value == 0) {
186 break;
187 }
188 }
189
190 for (n = 0; n < num_digits; n++) {
191 digits[n] = rev_digits[num_digits - 1 - n];
192 }
193 digits[n] = '\0';
194 return n;
195}
196
197static int cmdline_append_version(AvbSlotVerifyData *slot_data,
198 const char *key,
199 uint64_t major_version,
200 uint64_t minor_version)
201{
202 char major_digits[AVB_MAX_DIGITS_UINT64];
203 char minor_digits[AVB_MAX_DIGITS_UINT64];
204 char combined[AVB_MAX_DIGITS_UINT64 * 2 + 1];
205 size_t num_major_digits, num_minor_digits;
206
207 num_major_digits = uint64_to_base10(major_version, major_digits);
208 num_minor_digits = uint64_to_base10(minor_version, minor_digits);
209 avb_memcpy(combined, major_digits, num_major_digits);
210 combined[num_major_digits] = '.';
211 avb_memcpy(combined + num_major_digits + 1, minor_digits, num_minor_digits);
212 combined[num_major_digits + 1 + num_minor_digits] = '\0';
213
214 return cmdline_append_option(slot_data, key, combined);
215}
216
217static int cmdline_append_uint64_base10(AvbSlotVerifyData *slot_data,
218 const char *key,
219 uint64_t value)
220{
221 char digits[AVB_MAX_DIGITS_UINT64];
222 uint64_to_base10(value, digits);
223 return cmdline_append_option(slot_data, key, digits);
224}
225
226static int cmdline_append_hex(AvbSlotVerifyData *slot_data,
227 const char *key,
228 const uint8_t *data,
229 size_t data_len)
230{
231 int ret;
232 char *hex_data = avb_bin2hex(data, data_len);
233 if (hex_data == NULL) {
234 return 0;
235 }
236 ret = cmdline_append_option(slot_data, key, hex_data);
237 avb_free(hex_data);
238 return ret;
239}
240
241AvbSlotVerifyResult avb_append_options(
242 AvbOps *ops,
243 AvbSlotVerifyData *slot_data,
244 AvbVBMetaImageHeader *toplevel_vbmeta,
245 AvbAlgorithmType algorithm_type,
246 AvbHashtreeErrorMode hashtree_error_mode)
247{
248 AvbSlotVerifyResult ret;
249 const char *verity_mode;
250 bool is_device_unlocked;
251 AvbIOResult io_ret;
252
253 /* Add androidboot.vbmeta.device option. */
254 if (!cmdline_append_option(slot_data,
255 "androidboot.vbmeta.device",
256 "PARTUUID=$(ANDROID_VBMETA_PARTUUID)")) {
257 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
258 goto out;
259 }
260
261 /* Add androidboot.vbmeta.avb_version option. */
262 if (!cmdline_append_version(slot_data,
263 "androidboot.vbmeta.avb_version",
264 AVB_VERSION_MAJOR,
265 AVB_VERSION_MINOR)) {
266 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
267 goto out;
268 }
269
270 /* Set androidboot.avb.device_state to "locked" or "unlocked". */
271 io_ret = ops->read_is_device_unlocked(ops, &is_device_unlocked);
272 if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
273 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
274 goto out;
275 } else if (io_ret != AVB_IO_RESULT_OK) {
276 avb_error("Error getting device state.\n");
277 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
278 goto out;
279 }
280 if (!cmdline_append_option(slot_data,
281 "androidboot.vbmeta.device_state",
282 is_device_unlocked ? "unlocked" : "locked")) {
283 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
284 goto out;
285 }
286 if (!cmdline_append_option(slot_data,
287 "androidboot.verifiedbootstate",
288 is_device_unlocked ? "orange" : "green")) {
289 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
290 goto out;
291 }
292
293 /* Set androidboot.vbmeta.{hash_alg, size, digest} - use same hash
294 * function as is used to sign vbmeta.
295 */
296 switch (algorithm_type) {
297 /* Explicit fallthrough. */
298 case AVB_ALGORITHM_TYPE_NONE:
299 case AVB_ALGORITHM_TYPE_SHA256_RSA2048:
300 case AVB_ALGORITHM_TYPE_SHA256_RSA4096:
301 case AVB_ALGORITHM_TYPE_SHA256_RSA8192: {
302 AvbSHA256Ctx ctx;
303 size_t n, total_size = 0;
304 avb_sha256_init(&ctx);
305 for (n = 0; n < slot_data->num_vbmeta_images; n++) {
306 avb_sha256_update(&ctx,
307 slot_data->vbmeta_images[n].vbmeta_data,
308 slot_data->vbmeta_images[n].vbmeta_size);
309 total_size += slot_data->vbmeta_images[n].vbmeta_size;
310 }
311 if (!cmdline_append_option(
312 slot_data, "androidboot.vbmeta.hash_alg", "sha256") ||
313 !cmdline_append_uint64_base10(
314 slot_data, "androidboot.vbmeta.size", total_size) ||
315 !cmdline_append_hex(slot_data,
316 "androidboot.vbmeta.digest",
317 avb_sha256_final(&ctx),
318 AVB_SHA256_DIGEST_SIZE)) {
319 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
320 goto out;
321 }
322 }
323 break;
324 /* Explicit fallthrough. */
325 case AVB_ALGORITHM_TYPE_SHA512_RSA2048:
326 case AVB_ALGORITHM_TYPE_SHA512_RSA4096:
327 case AVB_ALGORITHM_TYPE_SHA512_RSA8192: {
328 AvbSHA512Ctx ctx;
329 size_t n, total_size = 0;
330 avb_sha512_init(&ctx);
331 for (n = 0; n < slot_data->num_vbmeta_images; n++) {
332 avb_sha512_update(&ctx,
333 slot_data->vbmeta_images[n].vbmeta_data,
334 slot_data->vbmeta_images[n].vbmeta_size);
335 total_size += slot_data->vbmeta_images[n].vbmeta_size;
336 }
337 if (!cmdline_append_option(
338 slot_data, "androidboot.vbmeta.hash_alg", "sha512") ||
339 !cmdline_append_uint64_base10(
340 slot_data, "androidboot.vbmeta.size", total_size) ||
341 !cmdline_append_hex(slot_data,
342 "androidboot.vbmeta.digest",
343 avb_sha512_final(&ctx),
344 AVB_SHA512_DIGEST_SIZE)) {
345 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
346 goto out;
347 }
348 }
349 break;
350 case _AVB_ALGORITHM_NUM_TYPES:
351 avb_assert_not_reached();
352 break;
353 }
354
355 /* Set androidboot.veritymode and androidboot.vbmeta.invalidate_on_error */
356 if (toplevel_vbmeta->flags & AVB_VBMETA_IMAGE_FLAGS_HASHTREE_DISABLED) {
357 verity_mode = "disabled";
358 } else {
359 const char *dm_verity_mode;
360 char *new_ret;
361
362 switch (hashtree_error_mode) {
363 case AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE:
364 if (!cmdline_append_option(
365 slot_data, "androidboot.vbmeta.invalidate_on_error", "yes")) {
366 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
367 goto out;
368 }
369 verity_mode = "enforcing";
370 dm_verity_mode = "restart_on_corruption";
371 break;
372 case AVB_HASHTREE_ERROR_MODE_RESTART:
373 verity_mode = "enforcing";
374 dm_verity_mode = "restart_on_corruption";
375 break;
376 case AVB_HASHTREE_ERROR_MODE_EIO:
377 verity_mode = "eio";
378 /* For now there's no option to specify the EIO mode. So
379 * just use 'ignore_zero_blocks' since that's already set
380 * and dm-verity-target.c supports specifying this multiple
381 * times.
382 */
383 dm_verity_mode = "ignore_zero_blocks";
384 break;
385 case AVB_HASHTREE_ERROR_MODE_LOGGING:
386 verity_mode = "logging";
387 dm_verity_mode = "ignore_corruption";
388 break;
389 }
390 new_ret = avb_replace(
391 slot_data->cmdline, "$(ANDROID_VERITY_MODE)", dm_verity_mode);
392 avb_free(slot_data->cmdline);
393 slot_data->cmdline = new_ret;
394 if (slot_data->cmdline == NULL) {
395 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
396 goto out;
397 }
398 }
399 if (!cmdline_append_option(
400 slot_data, "androidboot.veritymode", verity_mode)) {
401 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
402 goto out;
403 }
404
405 ret = AVB_SLOT_VERIFY_RESULT_OK;
406
407out:
408
409 return ret;
410}
411
412AvbCmdlineSubstList *avb_new_cmdline_subst_list()
413{
414 return (AvbCmdlineSubstList *)avb_calloc(sizeof(AvbCmdlineSubstList));
415}
416
417void avb_free_cmdline_subst_list(AvbCmdlineSubstList *cmdline_subst)
418{
419 size_t i;
420 for (i = 0; i < cmdline_subst->size; ++i) {
421 avb_free(cmdline_subst->tokens[i]);
422 avb_free(cmdline_subst->values[i]);
423 }
424 cmdline_subst->size = 0;
425 avb_free(cmdline_subst);
426}
427
428AvbSlotVerifyResult avb_add_root_digest_substitution(
429 const char *part_name,
430 const uint8_t *digest,
431 size_t digest_size,
432 AvbCmdlineSubstList *out_cmdline_subst)
433{
434 const char *kDigestSubPrefix = "$(AVB_";
435 const char *kDigestSubSuffix = "_ROOT_DIGEST)";
436 size_t part_name_len = avb_strlen(part_name);
437 size_t list_index = out_cmdline_subst->size;
438
439 avb_assert(part_name_len < AVB_PART_NAME_MAX_SIZE);
440 avb_assert(digest_size <= AVB_SHA512_DIGEST_SIZE);
441 if (part_name_len >= AVB_PART_NAME_MAX_SIZE ||
442 digest_size > AVB_SHA512_DIGEST_SIZE) {
443 return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
444 }
445
446 if (out_cmdline_subst->size >= AVB_MAX_NUM_CMDLINE_SUBST) {
447 /* The list is full. Currently dynamic growth of this list is not supported.
448 */
449 return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
450 }
451
452 /* Construct the token to replace in the command line based on the partition
453 * name. For partition 'foo', this will be '$(AVB_FOO_ROOT_DIGEST)'.
454 */
455 out_cmdline_subst->tokens[list_index] =
456 avb_strdupv(kDigestSubPrefix, part_name, kDigestSubSuffix, NULL);
457 if (out_cmdline_subst->tokens[list_index] == NULL) {
458 goto fail;
459 }
460 avb_uppercase(out_cmdline_subst->tokens[list_index]);
461
462 /* The digest value is hex encoded when inserted in the command line. */
463 out_cmdline_subst->values[list_index] = avb_bin2hex(digest, digest_size);
464 if (out_cmdline_subst->values[list_index] == NULL) {
465 goto fail;
466 }
467
468 out_cmdline_subst->size++;
469 return AVB_SLOT_VERIFY_RESULT_OK;
470
471fail:
472 if (out_cmdline_subst->tokens[list_index]) {
473 avb_free(out_cmdline_subst->tokens[list_index]);
474 }
475 if (out_cmdline_subst->values[list_index]) {
476 avb_free(out_cmdline_subst->values[list_index]);
477 }
478 return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
479}