avb_cmdline.c 15 KB

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