• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   avb_assert(ret != NULL);
101 
102   /* Replace any additional substitutions. */
103   if (additional_substitutions != NULL) {
104     for (n = 0; n < additional_substitutions->size; ++n) {
105       char* new_ret = avb_replace(ret,
106                                   additional_substitutions->tokens[n],
107                                   additional_substitutions->values[n]);
108       avb_free(ret);
109       ret = new_ret;
110       if (ret == NULL) {
111         goto fail;
112       }
113     }
114   }
115 
116   return ret;
117 
118 fail:
119   if (ret != NULL) {
120     avb_free(ret);
121   }
122   return NULL;
123 }
124 
cmdline_append_option(AvbSlotVerifyData * slot_data,const char * key,const char * value)125 static int cmdline_append_option(AvbSlotVerifyData* slot_data,
126                                  const char* key,
127                                  const char* value) {
128   size_t offset, key_len, value_len;
129   char* new_cmdline;
130 
131   key_len = avb_strlen(key);
132   value_len = avb_strlen(value);
133 
134   offset = 0;
135   if (slot_data->cmdline != NULL) {
136     offset = avb_strlen(slot_data->cmdline);
137     if (offset > 0) {
138       offset += 1;
139     }
140   }
141 
142   new_cmdline = avb_calloc(offset + key_len + value_len + 2);
143   if (new_cmdline == NULL) {
144     return 0;
145   }
146   if (offset > 0) {
147     avb_memcpy(new_cmdline, slot_data->cmdline, offset - 1);
148     new_cmdline[offset - 1] = ' ';
149   }
150   avb_memcpy(new_cmdline + offset, key, key_len);
151   new_cmdline[offset + key_len] = '=';
152   avb_memcpy(new_cmdline + offset + key_len + 1, value, value_len);
153   if (slot_data->cmdline != NULL) {
154     avb_free(slot_data->cmdline);
155   }
156   slot_data->cmdline = new_cmdline;
157 
158   return 1;
159 }
160 
161 #define AVB_MAX_DIGITS_UINT64 32
162 
163 /* Writes |value| to |digits| in base 10 followed by a NUL byte.
164  * Returns number of characters written excluding the NUL byte.
165  */
uint64_to_base10(uint64_t value,char digits[AVB_MAX_DIGITS_UINT64])166 static size_t uint64_to_base10(uint64_t value,
167                                char digits[AVB_MAX_DIGITS_UINT64]) {
168   char rev_digits[AVB_MAX_DIGITS_UINT64];
169   size_t n, num_digits;
170 
171   for (num_digits = 0; num_digits < AVB_MAX_DIGITS_UINT64 - 1;) {
172     rev_digits[num_digits++] = avb_div_by_10(&value) + '0';
173     if (value == 0) {
174       break;
175     }
176   }
177 
178   for (n = 0; n < num_digits; n++) {
179     digits[n] = rev_digits[num_digits - 1 - n];
180   }
181   digits[n] = '\0';
182   return n;
183 }
184 
cmdline_append_version(AvbSlotVerifyData * slot_data,const char * key,uint64_t major_version,uint64_t minor_version)185 static int cmdline_append_version(AvbSlotVerifyData* slot_data,
186                                   const char* key,
187                                   uint64_t major_version,
188                                   uint64_t minor_version) {
189   char major_digits[AVB_MAX_DIGITS_UINT64];
190   char minor_digits[AVB_MAX_DIGITS_UINT64];
191   char combined[AVB_MAX_DIGITS_UINT64 * 2 + 1];
192   size_t num_major_digits, num_minor_digits;
193 
194   num_major_digits = uint64_to_base10(major_version, major_digits);
195   num_minor_digits = uint64_to_base10(minor_version, minor_digits);
196   avb_memcpy(combined, major_digits, num_major_digits);
197   combined[num_major_digits] = '.';
198   avb_memcpy(combined + num_major_digits + 1, minor_digits, num_minor_digits);
199   combined[num_major_digits + 1 + num_minor_digits] = '\0';
200 
201   return cmdline_append_option(slot_data, key, combined);
202 }
203 
cmdline_append_uint64_base10(AvbSlotVerifyData * slot_data,const char * key,uint64_t value)204 static int cmdline_append_uint64_base10(AvbSlotVerifyData* slot_data,
205                                         const char* key,
206                                         uint64_t value) {
207   char digits[AVB_MAX_DIGITS_UINT64];
208   uint64_to_base10(value, digits);
209   return cmdline_append_option(slot_data, key, digits);
210 }
211 
cmdline_append_hex(AvbSlotVerifyData * slot_data,const char * key,const uint8_t * data,size_t data_len)212 static int cmdline_append_hex(AvbSlotVerifyData* slot_data,
213                               const char* key,
214                               const uint8_t* data,
215                               size_t data_len) {
216   int ret;
217   char* hex_data = avb_bin2hex(data, data_len);
218   if (hex_data == NULL) {
219     return 0;
220   }
221   ret = cmdline_append_option(slot_data, key, hex_data);
222   avb_free(hex_data);
223   return ret;
224 }
225 
avb_append_options(AvbOps * ops,AvbSlotVerifyData * slot_data,AvbVBMetaImageHeader * toplevel_vbmeta,AvbAlgorithmType algorithm_type,AvbHashtreeErrorMode hashtree_error_mode,AvbHashtreeErrorMode resolved_hashtree_error_mode)226 AvbSlotVerifyResult avb_append_options(
227     AvbOps* ops,
228     AvbSlotVerifyData* slot_data,
229     AvbVBMetaImageHeader* toplevel_vbmeta,
230     AvbAlgorithmType algorithm_type,
231     AvbHashtreeErrorMode hashtree_error_mode,
232     AvbHashtreeErrorMode resolved_hashtree_error_mode) {
233   AvbSlotVerifyResult ret;
234   const char* verity_mode;
235   bool is_device_unlocked;
236   AvbIOResult io_ret;
237 
238   /* Add androidboot.vbmeta.device option. */
239   if (!cmdline_append_option(slot_data,
240                              "androidboot.vbmeta.device",
241                              "PARTUUID=$(ANDROID_VBMETA_PARTUUID)")) {
242     ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
243     goto out;
244   }
245 
246   /* Add androidboot.vbmeta.avb_version option. */
247   if (!cmdline_append_version(slot_data,
248                               "androidboot.vbmeta.avb_version",
249                               AVB_VERSION_MAJOR,
250                               AVB_VERSION_MINOR)) {
251     ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
252     goto out;
253   }
254 
255   /* Set androidboot.avb.device_state to "locked" or "unlocked". */
256   io_ret = ops->read_is_device_unlocked(ops, &is_device_unlocked);
257   if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
258     ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
259     goto out;
260   } else if (io_ret != AVB_IO_RESULT_OK) {
261     avb_error("Error getting device state.\n");
262     ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
263     goto out;
264   }
265   if (!cmdline_append_option(slot_data,
266                              "androidboot.vbmeta.device_state",
267                              is_device_unlocked ? "unlocked" : "locked")) {
268     ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
269     goto out;
270   }
271 
272   /* Set androidboot.vbmeta.{hash_alg, size, digest} - use same hash
273    * function as is used to sign vbmeta.
274    */
275   switch (algorithm_type) {
276     /* Explicit fallthrough. */
277     case AVB_ALGORITHM_TYPE_NONE:
278     case AVB_ALGORITHM_TYPE_SHA256_RSA2048:
279     case AVB_ALGORITHM_TYPE_SHA256_RSA4096:
280     case AVB_ALGORITHM_TYPE_SHA256_RSA8192: {
281       size_t n, total_size = 0;
282       uint8_t vbmeta_digest[AVB_SHA256_DIGEST_SIZE];
283       avb_slot_verify_data_calculate_vbmeta_digest(
284           slot_data, AVB_DIGEST_TYPE_SHA256, vbmeta_digest);
285       for (n = 0; n < slot_data->num_vbmeta_images; n++) {
286         total_size += slot_data->vbmeta_images[n].vbmeta_size;
287       }
288       if (!cmdline_append_option(
289               slot_data, "androidboot.vbmeta.hash_alg", "sha256") ||
290           !cmdline_append_uint64_base10(
291               slot_data, "androidboot.vbmeta.size", total_size) ||
292           !cmdline_append_hex(slot_data,
293                               "androidboot.vbmeta.digest",
294                               vbmeta_digest,
295                               AVB_SHA256_DIGEST_SIZE)) {
296         ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
297         goto out;
298       }
299     } break;
300     /* Explicit fallthrough. */
301     case AVB_ALGORITHM_TYPE_SHA512_RSA2048:
302     case AVB_ALGORITHM_TYPE_SHA512_RSA4096:
303     case AVB_ALGORITHM_TYPE_SHA512_RSA8192: {
304       size_t n, total_size = 0;
305       uint8_t vbmeta_digest[AVB_SHA512_DIGEST_SIZE];
306       avb_slot_verify_data_calculate_vbmeta_digest(
307           slot_data, AVB_DIGEST_TYPE_SHA512, vbmeta_digest);
308       for (n = 0; n < slot_data->num_vbmeta_images; n++) {
309         total_size += slot_data->vbmeta_images[n].vbmeta_size;
310       }
311       if (!cmdline_append_option(
312               slot_data, "androidboot.vbmeta.hash_alg", "sha512") ||
313           !cmdline_append_uint64_base10(
314               slot_data, "androidboot.vbmeta.size", total_size) ||
315           !cmdline_append_hex(slot_data,
316                               "androidboot.vbmeta.digest",
317                               vbmeta_digest,
318                               AVB_SHA512_DIGEST_SIZE)) {
319         ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
320         goto out;
321       }
322     } break;
323     case _AVB_ALGORITHM_NUM_TYPES:
324       avb_assert_not_reached();
325       break;
326   }
327 
328   /* Set androidboot.veritymode and androidboot.vbmeta.invalidate_on_error */
329   if (toplevel_vbmeta->flags & AVB_VBMETA_IMAGE_FLAGS_HASHTREE_DISABLED) {
330     verity_mode = "disabled";
331   } else {
332     const char* dm_verity_mode;
333     char* new_ret;
334 
335     switch (resolved_hashtree_error_mode) {
336       case AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE:
337         if (!cmdline_append_option(
338                 slot_data, "androidboot.vbmeta.invalidate_on_error", "yes")) {
339           ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
340           goto out;
341         }
342         verity_mode = "enforcing";
343         dm_verity_mode = "restart_on_corruption";
344         break;
345       case AVB_HASHTREE_ERROR_MODE_RESTART:
346         verity_mode = "enforcing";
347         dm_verity_mode = "restart_on_corruption";
348         break;
349       case AVB_HASHTREE_ERROR_MODE_EIO:
350         verity_mode = "eio";
351         /* For now there's no option to specify the EIO mode. So
352          * just use 'ignore_zero_blocks' since that's already set
353          * and dm-verity-target.c supports specifying this multiple
354          * times.
355          */
356         dm_verity_mode = "ignore_zero_blocks";
357         break;
358       case AVB_HASHTREE_ERROR_MODE_LOGGING:
359         verity_mode = "logging";
360         dm_verity_mode = "ignore_corruption";
361         break;
362       case AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO:
363         // Should never get here because MANAGED_RESTART_AND_EIO is
364         // remapped by avb_manage_hashtree_error_mode().
365         avb_assert_not_reached();
366         break;
367     }
368     new_ret = avb_replace(
369         slot_data->cmdline, "$(ANDROID_VERITY_MODE)", dm_verity_mode);
370     avb_free(slot_data->cmdline);
371     slot_data->cmdline = new_ret;
372     if (slot_data->cmdline == NULL) {
373       ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
374       goto out;
375     }
376   }
377   if (!cmdline_append_option(
378           slot_data, "androidboot.veritymode", verity_mode)) {
379     ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
380     goto out;
381   }
382   if (hashtree_error_mode == AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO) {
383     if (!cmdline_append_option(
384             slot_data, "androidboot.veritymode.managed", "yes")) {
385       ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
386       goto out;
387     }
388   }
389 
390   ret = AVB_SLOT_VERIFY_RESULT_OK;
391 
392 out:
393 
394   return ret;
395 }
396 
avb_new_cmdline_subst_list()397 AvbCmdlineSubstList* avb_new_cmdline_subst_list() {
398   return (AvbCmdlineSubstList*)avb_calloc(sizeof(AvbCmdlineSubstList));
399 }
400 
avb_free_cmdline_subst_list(AvbCmdlineSubstList * cmdline_subst)401 void avb_free_cmdline_subst_list(AvbCmdlineSubstList* cmdline_subst) {
402   size_t i;
403   for (i = 0; i < cmdline_subst->size; ++i) {
404     avb_free(cmdline_subst->tokens[i]);
405     avb_free(cmdline_subst->values[i]);
406   }
407   cmdline_subst->size = 0;
408   avb_free(cmdline_subst);
409 }
410 
avb_add_root_digest_substitution(const char * part_name,const uint8_t * digest,size_t digest_size,AvbCmdlineSubstList * out_cmdline_subst)411 AvbSlotVerifyResult avb_add_root_digest_substitution(
412     const char* part_name,
413     const uint8_t* digest,
414     size_t digest_size,
415     AvbCmdlineSubstList* out_cmdline_subst) {
416   const char* kDigestSubPrefix = "$(AVB_";
417   const char* kDigestSubSuffix = "_ROOT_DIGEST)";
418   size_t part_name_len = avb_strlen(part_name);
419   size_t list_index = out_cmdline_subst->size;
420 
421   avb_assert(part_name_len < AVB_PART_NAME_MAX_SIZE);
422   avb_assert(digest_size <= AVB_SHA512_DIGEST_SIZE);
423   if (part_name_len >= AVB_PART_NAME_MAX_SIZE ||
424       digest_size > AVB_SHA512_DIGEST_SIZE) {
425     return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
426   }
427 
428   if (out_cmdline_subst->size >= AVB_MAX_NUM_CMDLINE_SUBST) {
429     /* The list is full. Currently dynamic growth of this list is not supported.
430      */
431     return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
432   }
433 
434   /* Construct the token to replace in the command line based on the partition
435    * name. For partition 'foo', this will be '$(AVB_FOO_ROOT_DIGEST)'.
436    */
437   out_cmdline_subst->tokens[list_index] =
438       avb_strdupv(kDigestSubPrefix, part_name, kDigestSubSuffix, NULL);
439   if (out_cmdline_subst->tokens[list_index] == NULL) {
440     goto fail;
441   }
442   avb_uppercase(out_cmdline_subst->tokens[list_index]);
443 
444   /* The digest value is hex encoded when inserted in the command line. */
445   out_cmdline_subst->values[list_index] = avb_bin2hex(digest, digest_size);
446   if (out_cmdline_subst->values[list_index] == NULL) {
447     goto fail;
448   }
449 
450   out_cmdline_subst->size++;
451   return AVB_SLOT_VERIFY_RESULT_OK;
452 
453 fail:
454   if (out_cmdline_subst->tokens[list_index]) {
455     avb_free(out_cmdline_subst->tokens[list_index]);
456   }
457   if (out_cmdline_subst->values[list_index]) {
458     avb_free(out_cmdline_subst->values[list_index]);
459   }
460   return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
461 }
462