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