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