avb_slot_verify.h 17 KB

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