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