avb_cmdline.c 16 KB

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