avb_cmdline.c 15 KB

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