avb_slot_verify.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * SPDX-License-Identifier: MIT
  5. */
  6. #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION)
  7. #error "Never include this file directly, include libavb.h instead."
  8. #endif
  9. #ifndef AVB_SLOT_VERIFY_H_
  10. #define AVB_SLOT_VERIFY_H_
  11. #include "avb_ops.h"
  12. #include "avb_vbmeta_image.h"
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. /* Return codes used in avb_slot_verify(), see that function for
  17. * documentation for each field.
  18. *
  19. * Use avb_slot_verify_result_to_string() to get a textual
  20. * representation usable for error/debug output.
  21. */
  22. typedef enum {
  23. AVB_SLOT_VERIFY_RESULT_OK,
  24. AVB_SLOT_VERIFY_RESULT_ERROR_OOM,
  25. AVB_SLOT_VERIFY_RESULT_ERROR_IO,
  26. AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION,
  27. AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX,
  28. AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED,
  29. AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA,
  30. AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION,
  31. AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT
  32. } AvbSlotVerifyResult;
  33. /* Various error handling modes for when verification fails using a
  34. * hashtree at runtime inside the HLOS.
  35. *
  36. * AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE means that the OS
  37. * will invalidate the current slot and restart.
  38. *
  39. * AVB_HASHTREE_ERROR_MODE_RESTART means that the OS will restart.
  40. *
  41. * AVB_HASHTREE_ERROR_MODE_EIO means that an EIO error will be
  42. * returned to applications.
  43. *
  44. * AVB_HASHTREE_ERROR_MODE_LOGGING means that errors will be logged
  45. * and corrupt data may be returned to applications. This mode should
  46. * be used ONLY for diagnostics and debugging. It cannot be used
  47. * unless AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR is also
  48. * used.
  49. */
  50. typedef enum {
  51. AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE,
  52. AVB_HASHTREE_ERROR_MODE_RESTART,
  53. AVB_HASHTREE_ERROR_MODE_EIO,
  54. AVB_HASHTREE_ERROR_MODE_LOGGING
  55. } AvbHashtreeErrorMode;
  56. /* Flags that influence how avb_slot_verify() works.
  57. *
  58. * If AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR is NOT set then
  59. * avb_slot_verify() will bail out as soon as an error is encountered
  60. * and |out_data| is set only if AVB_SLOT_VERIFY_RESULT_OK is
  61. * returned.
  62. *
  63. * Otherwise if AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR is set
  64. * avb_slot_verify() will continue verification efforts and |out_data|
  65. * is also set if AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED,
  66. * AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION, or
  67. * AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX is returned. It is
  68. * undefined which error is returned if more than one distinct error
  69. * is encountered. It is guaranteed that AVB_SLOT_VERIFY_RESULT_OK is
  70. * returned if, and only if, there are no errors. This mode is needed
  71. * to boot valid but unverified slots when the device is unlocked.
  72. *
  73. * Also, if AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR is set the
  74. * contents loaded from |requested_partition| will be the contents of
  75. * the entire partition instead of just the size specified in the hash
  76. * descriptor.
  77. */
  78. typedef enum {
  79. AVB_SLOT_VERIFY_FLAGS_NONE = 0,
  80. AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR = (1 << 0)
  81. } AvbSlotVerifyFlags;
  82. /* Get a textual representation of |result|. */
  83. const char* avb_slot_verify_result_to_string(AvbSlotVerifyResult result);
  84. /* Maximum number of rollback index locations supported. */
  85. #define AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS 32
  86. /* AvbPartitionData contains data loaded from partitions when using
  87. * avb_slot_verify(). The |partition_name| field contains the name of
  88. * the partition (without A/B suffix), |data| points to the loaded
  89. * data which is |data_size| bytes long. If |preloaded| is set to true,
  90. * this structure dose not own |data|. The caller of |avb_slot_verify|
  91. * needs to make sure that the preloaded data outlives this
  92. * |AvbPartitionData| structure.
  93. *
  94. * Note that this is strictly less than the partition size - it's only
  95. * the image stored there, not the entire partition nor any of the
  96. * metadata.
  97. */
  98. typedef struct {
  99. char* partition_name;
  100. uint8_t* data;
  101. size_t data_size;
  102. bool preloaded;
  103. } AvbPartitionData;
  104. /* AvbVBMetaData contains a vbmeta struct loaded from a partition when
  105. * using avb_slot_verify(). The |partition_name| field contains the
  106. * name of the partition (without A/B suffix), |vbmeta_data| points to
  107. * the loaded data which is |vbmeta_size| bytes long.
  108. *
  109. * The |verify_result| field contains the result of
  110. * avb_vbmeta_image_verify() on the data. This is guaranteed to be
  111. * AVB_VBMETA_VERIFY_RESULT_OK for all vbmeta images if
  112. * avb_slot_verify() returns AVB_SLOT_VERIFY_RESULT_OK.
  113. *
  114. * You can use avb_descriptor_get_all(), avb_descriptor_foreach(), and
  115. * avb_vbmeta_image_header_to_host_byte_order() with this data.
  116. */
  117. typedef struct {
  118. char* partition_name;
  119. uint8_t* vbmeta_data;
  120. size_t vbmeta_size;
  121. AvbVBMetaVerifyResult verify_result;
  122. } AvbVBMetaData;
  123. /* AvbSlotVerifyData contains data needed to boot a particular slot
  124. * and is returned by avb_slot_verify() if partitions in a slot are
  125. * successfully verified.
  126. *
  127. * All data pointed to by this struct - including data in each item in
  128. * the |partitions| array - will be freed when the
  129. * avb_slot_verify_data_free() function is called.
  130. *
  131. * The |ab_suffix| field is the copy of the of |ab_suffix| field
  132. * passed to avb_slot_verify(). It is the A/B suffix of the slot. This
  133. * value includes the leading underscore - typical values are "" (if
  134. * no slots are in use), "_a" (for the first slot), and "_b" (for the
  135. * second slot).
  136. *
  137. * The VBMeta images that were checked are available in the
  138. * |vbmeta_images| field. The field |num_vbmeta_images| contains the
  139. * number of elements in this array. The first element -
  140. * vbmeta_images[0] - is guaranteed to be from the partition with the
  141. * top-level vbmeta struct. This is usually the "vbmeta" partition in
  142. * the requested slot but if there is no "vbmeta" partition it can
  143. * also be the "boot" partition.
  144. *
  145. * The partitions loaded and verified from from the slot are
  146. * accessible in the |loaded_partitions| array. The field
  147. * |num_loaded_partitions| contains the number of elements in this
  148. * array. The order of partitions in this array may not necessarily be
  149. * the same order as in the passed-in |requested_partitions| array.
  150. *
  151. * Rollback indexes for the verified slot are stored in the
  152. * |rollback_indexes| field. Note that avb_slot_verify() will NEVER
  153. * modify stored_rollback_index[n] locations e.g. it will never use
  154. * the write_rollback_index() AvbOps operation. Instead it is the job
  155. * of the caller of avb_slot_verify() to do this based on e.g. A/B
  156. * policy and other factors. See libavb_ab/avb_ab_flow.c for an
  157. * example of how to do this.
  158. *
  159. * The |cmdline| field is a NUL-terminated string in UTF-8 resulting
  160. * from concatenating all |AvbKernelCmdlineDescriptor| and then
  161. * performing proper substitution of the variables
  162. * $(ANDROID_SYSTEM_PARTUUID), $(ANDROID_BOOT_PARTUUID), and
  163. * $(ANDROID_VBMETA_PARTUUID) using the
  164. * get_unique_guid_for_partition() operation in |AvbOps|. Additionally
  165. * $(ANDROID_VERITY_MODE) will be replaced with the proper dm-verity
  166. * option depending on the value of |hashtree_error_mode|.
  167. *
  168. * Additionally, the |cmdline| field will have the following kernel
  169. * command-line options set (unless verification is disabled, see
  170. * below):
  171. *
  172. * androidboot.veritymode: This is set to 'disabled' if the
  173. * AVB_VBMETA_IMAGE_FLAGS_HASHTREE_DISABLED flag is set in top-level
  174. * vbmeta struct. Otherwise it is set to 'enforcing' if the
  175. * passed-in hashtree error mode is AVB_HASHTREE_ERROR_MODE_RESTART
  176. * or AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE, 'eio' if it's
  177. * set to AVB_HASHTREE_ERROR_MODE_EIO, and 'logging' if it's set to
  178. * AVB_HASHTREE_ERROR_MODE_LOGGING.
  179. *
  180. * androidboot.vbmeta.invalidate_on_error: This is set to 'yes' only
  181. * if hashtree validation isn't disabled and the passed-in hashtree
  182. * error mode is AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE.
  183. *
  184. * androidboot.vbmeta.device_state: set to "locked" or "unlocked"
  185. * depending on the result of the result of AvbOps's
  186. * read_is_unlocked() function.
  187. *
  188. * androidboot.vbmeta.{hash_alg, size, digest}: Will be set to
  189. * the digest of all images in |vbmeta_images|.
  190. *
  191. * androidboot.vbmeta.device: This is set to the value
  192. * PARTUUID=$(ANDROID_VBMETA_PARTUUID) before substitution so it
  193. * will end up pointing to the vbmeta partition for the verified
  194. * slot. If there is no vbmeta partition it will point to the boot
  195. * partition of the verified slot.
  196. *
  197. * androidboot.vbmeta.avb_version: This is set to the decimal value
  198. * of AVB_VERSION_MAJOR followed by a dot followed by the decimal
  199. * value of AVB_VERSION_MINOR, for example "1.0" or "1.4". This
  200. * version number represents the vbmeta file format version
  201. * supported by libavb copy used in the boot loader. This is not
  202. * necessarily the same version number of the on-disk metadata for
  203. * the slot that was verified.
  204. *
  205. * Note that androidboot.slot_suffix is not set in the |cmdline| field
  206. * in |AvbSlotVerifyData| - you will have to set this yourself.
  207. *
  208. * If the |AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED| flag is set
  209. * in the top-level vbmeta struct then only the top-level vbmeta
  210. * struct is verified and descriptors will not processed. The return
  211. * value will be set accordingly (if this flag is set via 'avbctl
  212. * disable-verification' then the return value will be
  213. * |AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION|) and
  214. * |AvbSlotVerifyData| is returned. Additionally all partitions in the
  215. * |requested_partitions| are loaded and the |cmdline| field is set to
  216. * "root=PARTUUID=$(ANDROID_SYSTEM_PARTUUID)" and the GUID for the
  217. * appropriate system partition is substituted in. Note that none of
  218. * the androidboot.* options mentioned above will be set.
  219. *
  220. * This struct may grow in the future without it being considered an
  221. * ABI break.
  222. */
  223. typedef struct {
  224. char* ab_suffix;
  225. AvbVBMetaData* vbmeta_images;
  226. size_t num_vbmeta_images;
  227. AvbPartitionData* loaded_partitions;
  228. size_t num_loaded_partitions;
  229. char* cmdline;
  230. uint64_t rollback_indexes[AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS];
  231. } AvbSlotVerifyData;
  232. /* Calculates a digest of all vbmeta images in |data| using
  233. * the digest indicated by |digest_type|. Stores the result
  234. * in |out_digest| which must be large enough to hold a digest
  235. * of the requested type.
  236. */
  237. void avb_slot_verify_data_calculate_vbmeta_digest(AvbSlotVerifyData* data,
  238. AvbDigestType digest_type,
  239. uint8_t* out_digest);
  240. /* Frees a |AvbSlotVerifyData| including all data it points to. */
  241. void avb_slot_verify_data_free(AvbSlotVerifyData* data);
  242. /* Performs a full verification of the slot identified by |ab_suffix|
  243. * and load and verify the contents of the partitions whose name is in
  244. * the NULL-terminated string array |requested_partitions| (each
  245. * partition must use hash verification). If not using A/B, pass an
  246. * empty string (e.g. "", not NULL) for |ab_suffix|. This parameter
  247. * must include the leading underscore, for example "_a" should be
  248. * used to refer to the first slot.
  249. *
  250. * Typically the |requested_partitions| array only contains a single
  251. * item for the boot partition, 'boot'.
  252. *
  253. * Verification includes loading and verifying data from the 'vbmeta',
  254. * the requested hash partitions, and possibly other partitions (with
  255. * |ab_suffix| appended), inspecting rollback indexes, and checking if
  256. * the public key used to sign the data is acceptable. The functions
  257. * in |ops| will be used to do this.
  258. *
  259. * If |out_data| is not NULL, it will be set to a newly allocated
  260. * |AvbSlotVerifyData| struct containing all the data needed to
  261. * actually boot the slot. This data structure should be freed with
  262. * avb_slot_verify_data_free() when you are done with it. See below
  263. * for when this is returned.
  264. *
  265. * The |flags| parameter is used to influence the semantics of
  266. * avb_slot_verify() - for example the
  267. * AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR flag can be used to
  268. * ignore verification errors which is something needed in the
  269. * UNLOCKED state. See the AvbSlotVerifyFlags enumeration for details.
  270. *
  271. * The |hashtree_error_mode| parameter should be set to the desired
  272. * error handling mode when hashtree validation fails inside the
  273. * HLOS. This value isn't used by libavb per se - it is forwarded to
  274. * the HLOS through the androidboot.veritymode and
  275. * androidboot.vbmeta.invalidate_on_error cmdline parameters. See the
  276. * AvbHashtreeErrorMode enumeration for details.
  277. *
  278. * Also note that |out_data| is never set if
  279. * AVB_SLOT_VERIFY_RESULT_ERROR_OOM, AVB_SLOT_VERIFY_RESULT_ERROR_IO,
  280. * or AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA is returned.
  281. *
  282. * AVB_SLOT_VERIFY_RESULT_OK is returned if everything is verified
  283. * correctly and all public keys are accepted.
  284. *
  285. * AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED is returned if
  286. * everything is verified correctly out but one or more public keys
  287. * are not accepted. This includes the case where integrity data is
  288. * not signed.
  289. *
  290. * AVB_SLOT_VERIFY_RESULT_ERROR_OOM is returned if unable to
  291. * allocate memory.
  292. *
  293. * AVB_SLOT_VERIFY_RESULT_ERROR_IO is returned if an I/O error
  294. * occurred while trying to load data or get a rollback index.
  295. *
  296. * AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION is returned if the data
  297. * did not verify, e.g. the digest didn't match or signature checks
  298. * failed.
  299. *
  300. * AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX is returned if a
  301. * rollback index was less than its stored value.
  302. *
  303. * AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA is returned if some
  304. * of the metadata is invalid or inconsistent.
  305. *
  306. * AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION is returned if
  307. * some of the metadata requires a newer version of libavb than what
  308. * is in use.
  309. *
  310. * AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT is returned if the
  311. * caller passed invalid parameters, for example trying to use
  312. * AVB_HASHTREE_ERROR_MODE_LOGGING without
  313. * AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR.
  314. */
  315. AvbSlotVerifyResult avb_slot_verify(AvbOps* ops,
  316. const char* const* requested_partitions,
  317. const char* ab_suffix,
  318. AvbSlotVerifyFlags flags,
  319. AvbHashtreeErrorMode hashtree_error_mode,
  320. AvbSlotVerifyData** out_data);
  321. #ifdef __cplusplus
  322. }
  323. #endif
  324. #endif /* AVB_SLOT_VERIFY_H_ */