avb_cmdline.c 15 KB

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