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