avb_slot_verify.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. * If the AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION flag is set then
  115. * data won't be loaded from the "vbmeta" partition and the
  116. * |validate_vbmeta_public_key| operation is never called. Instead, the
  117. * vbmeta structs in |requested_partitions| are loaded and processed and the
  118. * |validate_public_key_for_partition| operation is called for each of these
  119. * vbmeta structs. This flag is useful when booting into recovery on a device
  120. * not using A/B - see section "Booting into recovery" in README.md for
  121. * more information.
  122. */
  123. typedef enum {
  124. AVB_SLOT_VERIFY_FLAGS_NONE = 0,
  125. AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR = (1 << 0),
  126. AVB_SLOT_VERIFY_FLAGS_RESTART_CAUSED_BY_HASHTREE_CORRUPTION = (1 << 1),
  127. AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION = (1 << 2),
  128. } AvbSlotVerifyFlags;
  129. /* Get a textual representation of |result|. */
  130. const char* avb_slot_verify_result_to_string(AvbSlotVerifyResult result);
  131. #define AVB_VER_INITIAL_VALUE 0xFFFFFFFF
  132. /* Maximum number of rollback index locations supported. */
  133. #define AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS 32
  134. /* AvbPartitionData contains data loaded from partitions when using
  135. * avb_slot_verify(). The |partition_name| field contains the name of
  136. * the partition (without A/B suffix), |data| points to the loaded
  137. * data which is |data_size| bytes long. If |preloaded| is set to true,
  138. * this structure dose not own |data|. The caller of |avb_slot_verify|
  139. * needs to make sure that the preloaded data outlives this
  140. * |AvbPartitionData| structure.
  141. *
  142. * Note that this is strictly less than the partition size - it's only
  143. * the image stored there, not the entire partition nor any of the
  144. * metadata.
  145. */
  146. typedef struct {
  147. char* partition_name;
  148. uint8_t* data;
  149. size_t data_size;
  150. bool preloaded;
  151. } AvbPartitionData;
  152. /* AvbVBMetaData contains a vbmeta struct loaded from a partition when
  153. * using avb_slot_verify(). The |partition_name| field contains the
  154. * name of the partition (without A/B suffix), |vbmeta_data| points to
  155. * the loaded data which is |vbmeta_size| bytes long.
  156. *
  157. * The |verify_result| field contains the result of
  158. * avb_vbmeta_image_verify() on the data. This is guaranteed to be
  159. * AVB_VBMETA_VERIFY_RESULT_OK for all vbmeta images if
  160. * avb_slot_verify() returns AVB_SLOT_VERIFY_RESULT_OK.
  161. *
  162. * You can use avb_descriptor_get_all(), avb_descriptor_foreach(), and
  163. * avb_vbmeta_image_header_to_host_byte_order() with this data.
  164. */
  165. typedef struct {
  166. char* partition_name;
  167. uint8_t* vbmeta_data;
  168. size_t vbmeta_size;
  169. AvbVBMetaVerifyResult verify_result;
  170. } AvbVBMetaData;
  171. /* AvbSlotVerifyData contains data needed to boot a particular slot
  172. * and is returned by avb_slot_verify() if partitions in a slot are
  173. * successfully verified.
  174. *
  175. * All data pointed to by this struct - including data in each item in
  176. * the |partitions| array - will be freed when the
  177. * avb_slot_verify_data_free() function is called.
  178. *
  179. * The |ab_suffix| field is the copy of the of |ab_suffix| field
  180. * passed to avb_slot_verify(). It is the A/B suffix of the slot. This
  181. * value includes the leading underscore - typical values are "" (if
  182. * no slots are in use), "_a" (for the first slot), and "_b" (for the
  183. * second slot).
  184. *
  185. * The VBMeta images that were checked are available in the
  186. * |vbmeta_images| field. The field |num_vbmeta_images| contains the
  187. * number of elements in this array. The first element -
  188. * vbmeta_images[0] - is guaranteed to be from the partition with the
  189. * top-level vbmeta struct. This is usually the "vbmeta" partition in
  190. * the requested slot but if there is no "vbmeta" partition it can
  191. * also be the "boot" partition.
  192. *
  193. * The partitions loaded and verified from from the slot are
  194. * accessible in the |loaded_partitions| array. The field
  195. * |num_loaded_partitions| contains the number of elements in this
  196. * array. The order of partitions in this array may not necessarily be
  197. * the same order as in the passed-in |requested_partitions| array.
  198. *
  199. * Rollback indexes for the verified slot are stored in the
  200. * |rollback_indexes| field. Note that avb_slot_verify() will NEVER
  201. * modify stored_rollback_index[n] locations e.g. it will never use
  202. * the write_rollback_index() AvbOps operation. Instead it is the job
  203. * of the caller of avb_slot_verify() to do this based on e.g. A/B
  204. * policy and other factors. See libavb_ab/avb_ab_flow.c for an
  205. * example of how to do this.
  206. *
  207. * The |cmdline| field is a NUL-terminated string in UTF-8 resulting
  208. * from concatenating all |AvbKernelCmdlineDescriptor| and then
  209. * performing proper substitution of the variables
  210. * $(ANDROID_SYSTEM_PARTUUID), $(ANDROID_BOOT_PARTUUID), and
  211. * $(ANDROID_VBMETA_PARTUUID) using the
  212. * get_unique_guid_for_partition() operation in |AvbOps|. Additionally
  213. * $(ANDROID_VERITY_MODE) will be replaced with the proper dm-verity
  214. * option depending on the value of |hashtree_error_mode|.
  215. *
  216. * Additionally, the |cmdline| field will have the following kernel
  217. * command-line options set (unless verification is disabled, see
  218. * below):
  219. *
  220. * androidboot.veritymode: This is set to 'disabled' if the
  221. * AVB_VBMETA_IMAGE_FLAGS_HASHTREE_DISABLED flag is set in top-level
  222. * vbmeta struct. Otherwise it is set to 'enforcing' if the
  223. * passed-in hashtree error mode is AVB_HASHTREE_ERROR_MODE_RESTART
  224. * or AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE, 'eio' if it's
  225. * set to AVB_HASHTREE_ERROR_MODE_EIO, and 'logging' if it's set to
  226. * AVB_HASHTREE_ERROR_MODE_LOGGING.
  227. *
  228. * androidboot.veritymode.managed: This is set to 'yes' only
  229. * if hashtree validation isn't disabled and the passed-in hashtree
  230. * error mode is AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO.
  231. *
  232. * androidboot.vbmeta.invalidate_on_error: This is set to 'yes' only
  233. * if hashtree validation isn't disabled and the passed-in hashtree
  234. * error mode is AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE.
  235. *
  236. * androidboot.vbmeta.device_state: set to "locked" or "unlocked"
  237. * depending on the result of the result of AvbOps's
  238. * read_is_unlocked() function.
  239. *
  240. * androidboot.vbmeta.{hash_alg, size, digest}: Will be set to
  241. * the digest of all images in |vbmeta_images|.
  242. *
  243. * androidboot.vbmeta.device: This is set to the value
  244. * PARTUUID=$(ANDROID_VBMETA_PARTUUID) before substitution so it
  245. * will end up pointing to the vbmeta partition for the verified
  246. * slot. If there is no vbmeta partition it will point to the boot
  247. * partition of the verified slot. If the flag
  248. * AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION is used, this is not
  249. * set.
  250. *
  251. * androidboot.vbmeta.avb_version: This is set to the decimal value
  252. * of AVB_VERSION_MAJOR followed by a dot followed by the decimal
  253. * value of AVB_VERSION_MINOR, for example "1.0" or "1.4". This
  254. * version number represents the vbmeta file format version
  255. * supported by libavb copy used in the boot loader. This is not
  256. * necessarily the same version number of the on-disk metadata for
  257. * the slot that was verified.
  258. *
  259. * Note that androidboot.slot_suffix is not set in the |cmdline| field
  260. * in |AvbSlotVerifyData| - you will have to set this yourself.
  261. *
  262. * If the |AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED| flag is set
  263. * in the top-level vbmeta struct then only the top-level vbmeta
  264. * struct is verified and descriptors will not processed. The return
  265. * value will be set accordingly (if this flag is set via 'avbctl
  266. * disable-verification' then the return value will be
  267. * |AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION|) and
  268. * |AvbSlotVerifyData| is returned. Additionally all partitions in the
  269. * |requested_partitions| are loaded and the |cmdline| field is set to
  270. * "root=PARTUUID=$(ANDROID_SYSTEM_PARTUUID)" and the GUID for the
  271. * appropriate system partition is substituted in. Note that none of
  272. * the androidboot.* options mentioned above will be set.
  273. *
  274. * The |resolved_hashtree_error_mode| is the the value of the passed
  275. * avb_slot_verify()'s |hashtree_error_mode| parameter except that it never has
  276. * the value AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO. If this value was
  277. * passed in, then the restart/eio state machine is used resulting in
  278. * |resolved_hashtree_error_mode| being set to either
  279. * AVB_HASHTREE_ERROR_MODE_RESTART or AVB_HASHTREE_ERROR_MODE_EIO. If set to
  280. * AVB_HASHTREE_ERROR_MODE_EIO the boot loader should present a RED warning
  281. * screen for the user to click through before continuing to boot.
  282. *
  283. * This struct may grow in the future without it being considered an
  284. * ABI break.
  285. */
  286. typedef struct {
  287. char* ab_suffix;
  288. AvbVBMetaData* vbmeta_images;
  289. size_t num_vbmeta_images;
  290. AvbPartitionData* loaded_partitions;
  291. size_t num_loaded_partitions;
  292. char* cmdline;
  293. uint64_t rollback_indexes[AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS];
  294. AvbHashtreeErrorMode resolved_hashtree_error_mode;
  295. } AvbSlotVerifyData;
  296. /* Calculates a digest of all vbmeta images in |data| using
  297. * the digest indicated by |digest_type|. Stores the result
  298. * in |out_digest| which must be large enough to hold a digest
  299. * of the requested type.
  300. */
  301. void avb_slot_verify_data_calculate_vbmeta_digest(AvbSlotVerifyData* data,
  302. AvbDigestType digest_type,
  303. uint8_t* out_digest);
  304. /* Frees a |AvbSlotVerifyData| including all data it points to. */
  305. void avb_slot_verify_data_free(AvbSlotVerifyData* data);
  306. /* Performs a full verification of the slot identified by |ab_suffix|
  307. * and load and verify the contents of the partitions whose name is in
  308. * the NULL-terminated string array |requested_partitions| (each
  309. * partition must use hash verification). If not using A/B, pass an
  310. * empty string (e.g. "", not NULL) for |ab_suffix|. This parameter
  311. * must include the leading underscore, for example "_a" should be
  312. * used to refer to the first slot.
  313. *
  314. * Typically the |requested_partitions| array only contains a single
  315. * item for the boot partition, 'boot'.
  316. *
  317. * Verification includes loading and verifying data from the 'vbmeta',
  318. * the requested hash partitions, and possibly other partitions (with
  319. * |ab_suffix| appended), inspecting rollback indexes, and checking if
  320. * the public key used to sign the data is acceptable. The functions
  321. * in |ops| will be used to do this.
  322. *
  323. * If |out_data| is not NULL, it will be set to a newly allocated
  324. * |AvbSlotVerifyData| struct containing all the data needed to
  325. * actually boot the slot. This data structure should be freed with
  326. * avb_slot_verify_data_free() when you are done with it. See below
  327. * for when this is returned.
  328. *
  329. * The |flags| parameter is used to influence the semantics of
  330. * avb_slot_verify() - for example the
  331. * AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR flag can be used to
  332. * ignore verification errors which is something needed in the
  333. * UNLOCKED state. See the AvbSlotVerifyFlags enumeration for details.
  334. *
  335. * The |hashtree_error_mode| parameter should be set to the desired error
  336. * handling mode. See the AvbHashtreeErrorMode enumeration for details.
  337. *
  338. * Also note that |out_data| is never set if
  339. * AVB_SLOT_VERIFY_RESULT_ERROR_OOM, AVB_SLOT_VERIFY_RESULT_ERROR_IO,
  340. * or AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA is returned.
  341. *
  342. * AVB_SLOT_VERIFY_RESULT_OK is returned if everything is verified
  343. * correctly and all public keys are accepted.
  344. *
  345. * AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED is returned if
  346. * everything is verified correctly out but one or more public keys
  347. * are not accepted. This includes the case where integrity data is
  348. * not signed.
  349. *
  350. * AVB_SLOT_VERIFY_RESULT_ERROR_OOM is returned if unable to
  351. * allocate memory.
  352. *
  353. * AVB_SLOT_VERIFY_RESULT_ERROR_IO is returned if an I/O error
  354. * occurred while trying to load data or get a rollback index.
  355. *
  356. * AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION is returned if the data
  357. * did not verify, e.g. the digest didn't match or signature checks
  358. * failed.
  359. *
  360. * AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX is returned if a
  361. * rollback index was less than its stored value.
  362. *
  363. * AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA is returned if some
  364. * of the metadata is invalid or inconsistent.
  365. *
  366. * AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION is returned if
  367. * some of the metadata requires a newer version of libavb than what
  368. * is in use.
  369. *
  370. * AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT is returned if the
  371. * caller passed invalid parameters, for example trying to use
  372. * AVB_HASHTREE_ERROR_MODE_LOGGING without
  373. * AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR.
  374. */
  375. AvbSlotVerifyResult avb_slot_verify(AvbOps* ops,
  376. const char* const* requested_partitions,
  377. const char* ab_suffix,
  378. AvbSlotVerifyFlags flags,
  379. AvbHashtreeErrorMode hashtree_error_mode,
  380. AvbSlotVerifyData** out_data);
  381. #ifdef __cplusplus
  382. }
  383. #endif
  384. #endif /* AVB_SLOT_VERIFY_H_ */