blob: d24669927203fbd219e357cfac2830fb9e9088fe [file] [log] [blame]
Tom Rini897a1d92018-06-19 11:21:44 -04001// SPDX-License-Identifier: MIT
Igor Opaniukd8f9d2a2018-06-03 21:56:36 +03002/*
3 * Copyright (C) 2016 The Android Open Source Project
Igor Opaniukd8f9d2a2018-06-03 21:56:36 +03004 */
5
6#include "avb_cmdline.h"
7#include "avb_sha.h"
8#include "avb_util.h"
9#include "avb_version.h"
10
11#define NUM_GUIDS 3
12
13/* Substitutes all variables (e.g. $(ANDROID_SYSTEM_PARTUUID)) with
14 * values. Returns NULL on OOM, otherwise the cmdline with values
15 * replaced.
16 */
17char* avb_sub_cmdline(AvbOps* ops,
18 const char* cmdline,
19 const char* ab_suffix,
20 bool using_boot_for_vbmeta,
21 const AvbCmdlineSubstList* additional_substitutions) {
22 const char* part_name_str[NUM_GUIDS] = {"system", "boot", "vbmeta"};
23 const char* replace_str[NUM_GUIDS] = {"$(ANDROID_SYSTEM_PARTUUID)",
24 "$(ANDROID_BOOT_PARTUUID)",
25 "$(ANDROID_VBMETA_PARTUUID)"};
26 char* ret = NULL;
27 AvbIOResult io_ret;
28 size_t n;
29
30 /* Special-case for when the top-level vbmeta struct is in the boot
31 * partition.
32 */
33 if (using_boot_for_vbmeta) {
34 part_name_str[2] = "boot";
35 }
36
37 /* Replace unique partition GUIDs */
38 for (n = 0; n < NUM_GUIDS; n++) {
39 char part_name[AVB_PART_NAME_MAX_SIZE];
40 char guid_buf[37];
41
42 if (!avb_str_concat(part_name,
43 sizeof part_name,
44 part_name_str[n],
45 avb_strlen(part_name_str[n]),
46 ab_suffix,
47 avb_strlen(ab_suffix))) {
48 avb_error("Partition name and suffix does not fit.\n");
49 goto fail;
50 }
51
52 io_ret = ops->get_unique_guid_for_partition(
53 ops, part_name, guid_buf, sizeof guid_buf);
54 if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
55 goto fail;
56 } else if (io_ret != AVB_IO_RESULT_OK) {
57 avb_error("Error getting unique GUID for partition.\n");
58 goto fail;
59 }
60
61 if (ret == NULL) {
62 ret = avb_replace(cmdline, replace_str[n], guid_buf);
63 } else {
64 char* new_ret = avb_replace(ret, replace_str[n], guid_buf);
65 avb_free(ret);
66 ret = new_ret;
67 }
68 if (ret == NULL) {
69 goto fail;
70 }
71 }
72
73 avb_assert(ret != NULL);
74
75 /* Replace any additional substitutions. */
76 if (additional_substitutions != NULL) {
77 for (n = 0; n < additional_substitutions->size; ++n) {
78 char* new_ret = avb_replace(ret,
79 additional_substitutions->tokens[n],
80 additional_substitutions->values[n]);
81 avb_free(ret);
82 ret = new_ret;
83 if (ret == NULL) {
84 goto fail;
85 }
86 }
87 }
88
89 return ret;
90
91fail:
92 if (ret != NULL) {
93 avb_free(ret);
94 }
95 return NULL;
96}
97
98static int cmdline_append_option(AvbSlotVerifyData* slot_data,
99 const char* key,
100 const char* value) {
101 size_t offset, key_len, value_len;
102 char* new_cmdline;
103
104 key_len = avb_strlen(key);
105 value_len = avb_strlen(value);
106
107 offset = 0;
108 if (slot_data->cmdline != NULL) {
109 offset = avb_strlen(slot_data->cmdline);
110 if (offset > 0) {
111 offset += 1;
112 }
113 }
114
115 new_cmdline = avb_calloc(offset + key_len + value_len + 2);
116 if (new_cmdline == NULL) {
117 return 0;
118 }
119 if (offset > 0) {
120 avb_memcpy(new_cmdline, slot_data->cmdline, offset - 1);
121 new_cmdline[offset - 1] = ' ';
122 }
123 avb_memcpy(new_cmdline + offset, key, key_len);
124 new_cmdline[offset + key_len] = '=';
125 avb_memcpy(new_cmdline + offset + key_len + 1, value, value_len);
126 if (slot_data->cmdline != NULL) {
127 avb_free(slot_data->cmdline);
128 }
129 slot_data->cmdline = new_cmdline;
130
131 return 1;
132}
133
134#define AVB_MAX_DIGITS_UINT64 32
135
136/* Writes |value| to |digits| in base 10 followed by a NUL byte.
137 * Returns number of characters written excluding the NUL byte.
138 */
139static size_t uint64_to_base10(uint64_t value,
140 char digits[AVB_MAX_DIGITS_UINT64]) {
141 char rev_digits[AVB_MAX_DIGITS_UINT64];
142 size_t n, num_digits;
143
144 for (num_digits = 0; num_digits < AVB_MAX_DIGITS_UINT64 - 1;) {
145 rev_digits[num_digits++] = avb_div_by_10(&value) + '0';
146 if (value == 0) {
147 break;
148 }
149 }
150
151 for (n = 0; n < num_digits; n++) {
152 digits[n] = rev_digits[num_digits - 1 - n];
153 }
154 digits[n] = '\0';
155 return n;
156}
157
158static int cmdline_append_version(AvbSlotVerifyData* slot_data,
159 const char* key,
160 uint64_t major_version,
161 uint64_t minor_version) {
162 char major_digits[AVB_MAX_DIGITS_UINT64];
163 char minor_digits[AVB_MAX_DIGITS_UINT64];
164 char combined[AVB_MAX_DIGITS_UINT64 * 2 + 1];
165 size_t num_major_digits, num_minor_digits;
166
167 num_major_digits = uint64_to_base10(major_version, major_digits);
168 num_minor_digits = uint64_to_base10(minor_version, minor_digits);
169 avb_memcpy(combined, major_digits, num_major_digits);
170 combined[num_major_digits] = '.';
171 avb_memcpy(combined + num_major_digits + 1, minor_digits, num_minor_digits);
172 combined[num_major_digits + 1 + num_minor_digits] = '\0';
173
174 return cmdline_append_option(slot_data, key, combined);
175}
176
177static int cmdline_append_uint64_base10(AvbSlotVerifyData* slot_data,
178 const char* key,
179 uint64_t value) {
180 char digits[AVB_MAX_DIGITS_UINT64];
181 uint64_to_base10(value, digits);
182 return cmdline_append_option(slot_data, key, digits);
183}
184
185static int cmdline_append_hex(AvbSlotVerifyData* slot_data,
186 const char* key,
187 const uint8_t* data,
188 size_t data_len) {
189 int ret;
190 char* hex_data = avb_bin2hex(data, data_len);
191 if (hex_data == NULL) {
192 return 0;
193 }
194 ret = cmdline_append_option(slot_data, key, hex_data);
195 avb_free(hex_data);
196 return ret;
197}
198
199AvbSlotVerifyResult avb_append_options(
200 AvbOps* ops,
201 AvbSlotVerifyData* slot_data,
202 AvbVBMetaImageHeader* toplevel_vbmeta,
203 AvbAlgorithmType algorithm_type,
204 AvbHashtreeErrorMode hashtree_error_mode) {
205 AvbSlotVerifyResult ret;
206 const char* verity_mode;
207 bool is_device_unlocked;
208 AvbIOResult io_ret;
209
210 /* Add androidboot.vbmeta.device option. */
211 if (!cmdline_append_option(slot_data,
212 "androidboot.vbmeta.device",
213 "PARTUUID=$(ANDROID_VBMETA_PARTUUID)")) {
214 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
215 goto out;
216 }
217
218 /* Add androidboot.vbmeta.avb_version option. */
219 if (!cmdline_append_version(slot_data,
220 "androidboot.vbmeta.avb_version",
221 AVB_VERSION_MAJOR,
222 AVB_VERSION_MINOR)) {
223 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
224 goto out;
225 }
226
227 /* Set androidboot.avb.device_state to "locked" or "unlocked". */
228 io_ret = ops->read_is_device_unlocked(ops, &is_device_unlocked);
229 if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
230 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
231 goto out;
232 } else if (io_ret != AVB_IO_RESULT_OK) {
233 avb_error("Error getting device state.\n");
234 ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
235 goto out;
236 }
237 if (!cmdline_append_option(slot_data,
238 "androidboot.vbmeta.device_state",
239 is_device_unlocked ? "unlocked" : "locked")) {
240 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
241 goto out;
242 }
243
244 /* Set androidboot.vbmeta.{hash_alg, size, digest} - use same hash
245 * function as is used to sign vbmeta.
246 */
247 switch (algorithm_type) {
248 /* Explicit fallthrough. */
249 case AVB_ALGORITHM_TYPE_NONE:
250 case AVB_ALGORITHM_TYPE_SHA256_RSA2048:
251 case AVB_ALGORITHM_TYPE_SHA256_RSA4096:
252 case AVB_ALGORITHM_TYPE_SHA256_RSA8192: {
253 size_t n, total_size = 0;
254 uint8_t vbmeta_digest[AVB_SHA256_DIGEST_SIZE];
255 avb_slot_verify_data_calculate_vbmeta_digest(
256 slot_data, AVB_DIGEST_TYPE_SHA256, vbmeta_digest);
257 for (n = 0; n < slot_data->num_vbmeta_images; n++) {
258 total_size += slot_data->vbmeta_images[n].vbmeta_size;
259 }
260 if (!cmdline_append_option(
261 slot_data, "androidboot.vbmeta.hash_alg", "sha256") ||
262 !cmdline_append_uint64_base10(
263 slot_data, "androidboot.vbmeta.size", total_size) ||
264 !cmdline_append_hex(slot_data,
265 "androidboot.vbmeta.digest",
266 vbmeta_digest,
267 AVB_SHA256_DIGEST_SIZE)) {
268 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
269 goto out;
270 }
271 } break;
272 /* Explicit fallthrough. */
273 case AVB_ALGORITHM_TYPE_SHA512_RSA2048:
274 case AVB_ALGORITHM_TYPE_SHA512_RSA4096:
275 case AVB_ALGORITHM_TYPE_SHA512_RSA8192: {
276 size_t n, total_size = 0;
277 uint8_t vbmeta_digest[AVB_SHA512_DIGEST_SIZE];
278 avb_slot_verify_data_calculate_vbmeta_digest(
279 slot_data, AVB_DIGEST_TYPE_SHA512, vbmeta_digest);
280 for (n = 0; n < slot_data->num_vbmeta_images; n++) {
281 total_size += slot_data->vbmeta_images[n].vbmeta_size;
282 }
283 if (!cmdline_append_option(
284 slot_data, "androidboot.vbmeta.hash_alg", "sha512") ||
285 !cmdline_append_uint64_base10(
286 slot_data, "androidboot.vbmeta.size", total_size) ||
287 !cmdline_append_hex(slot_data,
288 "androidboot.vbmeta.digest",
289 vbmeta_digest,
290 AVB_SHA512_DIGEST_SIZE)) {
291 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
292 goto out;
293 }
294 } break;
295 case _AVB_ALGORITHM_NUM_TYPES:
296 avb_assert_not_reached();
297 break;
298 }
299
300 /* Set androidboot.veritymode and androidboot.vbmeta.invalidate_on_error */
301 if (toplevel_vbmeta->flags & AVB_VBMETA_IMAGE_FLAGS_HASHTREE_DISABLED) {
302 verity_mode = "disabled";
303 } else {
304 const char* dm_verity_mode;
305 char* new_ret;
306
307 switch (hashtree_error_mode) {
308 case AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE:
309 if (!cmdline_append_option(
310 slot_data, "androidboot.vbmeta.invalidate_on_error", "yes")) {
311 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
312 goto out;
313 }
314 verity_mode = "enforcing";
315 dm_verity_mode = "restart_on_corruption";
316 break;
317 case AVB_HASHTREE_ERROR_MODE_RESTART:
318 verity_mode = "enforcing";
319 dm_verity_mode = "restart_on_corruption";
320 break;
321 case AVB_HASHTREE_ERROR_MODE_EIO:
322 verity_mode = "eio";
323 /* For now there's no option to specify the EIO mode. So
324 * just use 'ignore_zero_blocks' since that's already set
325 * and dm-verity-target.c supports specifying this multiple
326 * times.
327 */
328 dm_verity_mode = "ignore_zero_blocks";
329 break;
330 case AVB_HASHTREE_ERROR_MODE_LOGGING:
331 verity_mode = "logging";
332 dm_verity_mode = "ignore_corruption";
333 break;
Ievgen Maliarenkoecc6f6b2018-08-14 02:43:03 +0200334 default:
335 ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT;
336 goto out;
Igor Opaniukd8f9d2a2018-06-03 21:56:36 +0300337 }
338 new_ret = avb_replace(
339 slot_data->cmdline, "$(ANDROID_VERITY_MODE)", dm_verity_mode);
340 avb_free(slot_data->cmdline);
341 slot_data->cmdline = new_ret;
342 if (slot_data->cmdline == NULL) {
343 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
344 goto out;
345 }
346 }
347 if (!cmdline_append_option(
348 slot_data, "androidboot.veritymode", verity_mode)) {
349 ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
350 goto out;
351 }
352
353 ret = AVB_SLOT_VERIFY_RESULT_OK;
354
355out:
356
357 return ret;
358}
359
360AvbCmdlineSubstList* avb_new_cmdline_subst_list() {
361 return (AvbCmdlineSubstList*)avb_calloc(sizeof(AvbCmdlineSubstList));
362}
363
364void avb_free_cmdline_subst_list(AvbCmdlineSubstList* cmdline_subst) {
365 size_t i;
366 for (i = 0; i < cmdline_subst->size; ++i) {
367 avb_free(cmdline_subst->tokens[i]);
368 avb_free(cmdline_subst->values[i]);
369 }
370 cmdline_subst->size = 0;
371 avb_free(cmdline_subst);
372}
373
374AvbSlotVerifyResult avb_add_root_digest_substitution(
375 const char* part_name,
376 const uint8_t* digest,
377 size_t digest_size,
378 AvbCmdlineSubstList* out_cmdline_subst) {
379 const char* kDigestSubPrefix = "$(AVB_";
380 const char* kDigestSubSuffix = "_ROOT_DIGEST)";
381 size_t part_name_len = avb_strlen(part_name);
382 size_t list_index = out_cmdline_subst->size;
383
384 avb_assert(part_name_len < AVB_PART_NAME_MAX_SIZE);
385 avb_assert(digest_size <= AVB_SHA512_DIGEST_SIZE);
386 if (part_name_len >= AVB_PART_NAME_MAX_SIZE ||
387 digest_size > AVB_SHA512_DIGEST_SIZE) {
388 return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
389 }
390
391 if (out_cmdline_subst->size >= AVB_MAX_NUM_CMDLINE_SUBST) {
392 /* The list is full. Currently dynamic growth of this list is not supported.
393 */
394 return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
395 }
396
397 /* Construct the token to replace in the command line based on the partition
398 * name. For partition 'foo', this will be '$(AVB_FOO_ROOT_DIGEST)'.
399 */
400 out_cmdline_subst->tokens[list_index] =
401 avb_strdupv(kDigestSubPrefix, part_name, kDigestSubSuffix, NULL);
402 if (out_cmdline_subst->tokens[list_index] == NULL) {
403 goto fail;
404 }
405 avb_uppercase(out_cmdline_subst->tokens[list_index]);
406
407 /* The digest value is hex encoded when inserted in the command line. */
408 out_cmdline_subst->values[list_index] = avb_bin2hex(digest, digest_size);
409 if (out_cmdline_subst->values[list_index] == NULL) {
410 goto fail;
411 }
412
413 out_cmdline_subst->size++;
414 return AVB_SLOT_VERIFY_RESULT_OK;
415
416fail:
417 if (out_cmdline_subst->tokens[list_index]) {
418 avb_free(out_cmdline_subst->tokens[list_index]);
419 }
420 if (out_cmdline_subst->values[list_index]) {
421 avb_free(out_cmdline_subst->values[list_index]);
422 }
423 return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
424}