1*d289c2baSAndroid Build Coastguard Worker /*
2*d289c2baSAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project
3*d289c2baSAndroid Build Coastguard Worker *
4*d289c2baSAndroid Build Coastguard Worker * Permission is hereby granted, free of charge, to any person
5*d289c2baSAndroid Build Coastguard Worker * obtaining a copy of this software and associated documentation
6*d289c2baSAndroid Build Coastguard Worker * files (the "Software"), to deal in the Software without
7*d289c2baSAndroid Build Coastguard Worker * restriction, including without limitation the rights to use, copy,
8*d289c2baSAndroid Build Coastguard Worker * modify, merge, publish, distribute, sublicense, and/or sell copies
9*d289c2baSAndroid Build Coastguard Worker * of the Software, and to permit persons to whom the Software is
10*d289c2baSAndroid Build Coastguard Worker * furnished to do so, subject to the following conditions:
11*d289c2baSAndroid Build Coastguard Worker *
12*d289c2baSAndroid Build Coastguard Worker * The above copyright notice and this permission notice shall be
13*d289c2baSAndroid Build Coastguard Worker * included in all copies or substantial portions of the Software.
14*d289c2baSAndroid Build Coastguard Worker *
15*d289c2baSAndroid Build Coastguard Worker * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*d289c2baSAndroid Build Coastguard Worker * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*d289c2baSAndroid Build Coastguard Worker * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18*d289c2baSAndroid Build Coastguard Worker * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19*d289c2baSAndroid Build Coastguard Worker * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20*d289c2baSAndroid Build Coastguard Worker * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21*d289c2baSAndroid Build Coastguard Worker * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*d289c2baSAndroid Build Coastguard Worker * SOFTWARE.
23*d289c2baSAndroid Build Coastguard Worker */
24*d289c2baSAndroid Build Coastguard Worker
25*d289c2baSAndroid Build Coastguard Worker #include "avb_slot_verify.h"
26*d289c2baSAndroid Build Coastguard Worker #include "avb_chain_partition_descriptor.h"
27*d289c2baSAndroid Build Coastguard Worker #include "avb_cmdline.h"
28*d289c2baSAndroid Build Coastguard Worker #include "avb_footer.h"
29*d289c2baSAndroid Build Coastguard Worker #include "avb_hash_descriptor.h"
30*d289c2baSAndroid Build Coastguard Worker #include "avb_hashtree_descriptor.h"
31*d289c2baSAndroid Build Coastguard Worker #include "avb_kernel_cmdline_descriptor.h"
32*d289c2baSAndroid Build Coastguard Worker #include "avb_sha.h"
33*d289c2baSAndroid Build Coastguard Worker #include "avb_util.h"
34*d289c2baSAndroid Build Coastguard Worker #include "avb_vbmeta_image.h"
35*d289c2baSAndroid Build Coastguard Worker #include "avb_version.h"
36*d289c2baSAndroid Build Coastguard Worker
37*d289c2baSAndroid Build Coastguard Worker /* Maximum number of partitions that can be loaded with avb_slot_verify(). */
38*d289c2baSAndroid Build Coastguard Worker #define MAX_NUMBER_OF_LOADED_PARTITIONS 32
39*d289c2baSAndroid Build Coastguard Worker
40*d289c2baSAndroid Build Coastguard Worker /* Maximum number of vbmeta images that can be loaded with avb_slot_verify(). */
41*d289c2baSAndroid Build Coastguard Worker #define MAX_NUMBER_OF_VBMETA_IMAGES 32
42*d289c2baSAndroid Build Coastguard Worker
43*d289c2baSAndroid Build Coastguard Worker /* Maximum size of a vbmeta image - 64 KiB. */
44*d289c2baSAndroid Build Coastguard Worker #define VBMETA_MAX_SIZE (64 * 1024)
45*d289c2baSAndroid Build Coastguard Worker
46*d289c2baSAndroid Build Coastguard Worker /* Test buffer used to check the existence of a partition. */
47*d289c2baSAndroid Build Coastguard Worker #define TEST_BUFFER_SIZE 1
48*d289c2baSAndroid Build Coastguard Worker
49*d289c2baSAndroid Build Coastguard Worker static AvbSlotVerifyResult initialize_persistent_digest(
50*d289c2baSAndroid Build Coastguard Worker AvbOps* ops,
51*d289c2baSAndroid Build Coastguard Worker const char* part_name,
52*d289c2baSAndroid Build Coastguard Worker const char* persistent_value_name,
53*d289c2baSAndroid Build Coastguard Worker size_t digest_size,
54*d289c2baSAndroid Build Coastguard Worker const uint8_t* initial_digest,
55*d289c2baSAndroid Build Coastguard Worker uint8_t* out_digest);
56*d289c2baSAndroid Build Coastguard Worker
57*d289c2baSAndroid Build Coastguard Worker /* Helper function to see if we should continue with verification in
58*d289c2baSAndroid Build Coastguard Worker * allow_verification_error=true mode if something goes wrong. See the
59*d289c2baSAndroid Build Coastguard Worker * comments for the avb_slot_verify() function for more information.
60*d289c2baSAndroid Build Coastguard Worker */
result_should_continue(AvbSlotVerifyResult result)61*d289c2baSAndroid Build Coastguard Worker static inline bool result_should_continue(AvbSlotVerifyResult result) {
62*d289c2baSAndroid Build Coastguard Worker switch (result) {
63*d289c2baSAndroid Build Coastguard Worker case AVB_SLOT_VERIFY_RESULT_ERROR_OOM:
64*d289c2baSAndroid Build Coastguard Worker case AVB_SLOT_VERIFY_RESULT_ERROR_IO:
65*d289c2baSAndroid Build Coastguard Worker case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA:
66*d289c2baSAndroid Build Coastguard Worker case AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION:
67*d289c2baSAndroid Build Coastguard Worker case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT:
68*d289c2baSAndroid Build Coastguard Worker return false;
69*d289c2baSAndroid Build Coastguard Worker
70*d289c2baSAndroid Build Coastguard Worker case AVB_SLOT_VERIFY_RESULT_OK:
71*d289c2baSAndroid Build Coastguard Worker case AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION:
72*d289c2baSAndroid Build Coastguard Worker case AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX:
73*d289c2baSAndroid Build Coastguard Worker case AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED:
74*d289c2baSAndroid Build Coastguard Worker return true;
75*d289c2baSAndroid Build Coastguard Worker }
76*d289c2baSAndroid Build Coastguard Worker
77*d289c2baSAndroid Build Coastguard Worker return false;
78*d289c2baSAndroid Build Coastguard Worker }
79*d289c2baSAndroid Build Coastguard Worker
load_full_partition(AvbOps * ops,const char * part_name,uint64_t image_size,uint8_t ** out_image_buf,bool * out_image_preloaded)80*d289c2baSAndroid Build Coastguard Worker static AvbSlotVerifyResult load_full_partition(AvbOps* ops,
81*d289c2baSAndroid Build Coastguard Worker const char* part_name,
82*d289c2baSAndroid Build Coastguard Worker uint64_t image_size,
83*d289c2baSAndroid Build Coastguard Worker uint8_t** out_image_buf,
84*d289c2baSAndroid Build Coastguard Worker bool* out_image_preloaded) {
85*d289c2baSAndroid Build Coastguard Worker size_t part_num_read;
86*d289c2baSAndroid Build Coastguard Worker AvbIOResult io_ret;
87*d289c2baSAndroid Build Coastguard Worker
88*d289c2baSAndroid Build Coastguard Worker /* Make sure that we do not overwrite existing data. */
89*d289c2baSAndroid Build Coastguard Worker avb_assert(*out_image_buf == NULL);
90*d289c2baSAndroid Build Coastguard Worker avb_assert(!*out_image_preloaded);
91*d289c2baSAndroid Build Coastguard Worker
92*d289c2baSAndroid Build Coastguard Worker /* We are going to implicitly cast image_size from uint64_t to size_t in the
93*d289c2baSAndroid Build Coastguard Worker * following code, so we need to make sure that the cast is safe. */
94*d289c2baSAndroid Build Coastguard Worker if (image_size != (size_t)(image_size)) {
95*d289c2baSAndroid Build Coastguard Worker avb_error(part_name, ": Partition size too large to load.\n");
96*d289c2baSAndroid Build Coastguard Worker return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
97*d289c2baSAndroid Build Coastguard Worker }
98*d289c2baSAndroid Build Coastguard Worker
99*d289c2baSAndroid Build Coastguard Worker /* Try use a preloaded one. */
100*d289c2baSAndroid Build Coastguard Worker if (ops->get_preloaded_partition != NULL) {
101*d289c2baSAndroid Build Coastguard Worker io_ret = ops->get_preloaded_partition(
102*d289c2baSAndroid Build Coastguard Worker ops, part_name, image_size, out_image_buf, &part_num_read);
103*d289c2baSAndroid Build Coastguard Worker if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
104*d289c2baSAndroid Build Coastguard Worker return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
105*d289c2baSAndroid Build Coastguard Worker } else if (io_ret != AVB_IO_RESULT_OK) {
106*d289c2baSAndroid Build Coastguard Worker avb_error(part_name, ": Error loading data from partition.\n");
107*d289c2baSAndroid Build Coastguard Worker return AVB_SLOT_VERIFY_RESULT_ERROR_IO;
108*d289c2baSAndroid Build Coastguard Worker }
109*d289c2baSAndroid Build Coastguard Worker
110*d289c2baSAndroid Build Coastguard Worker if (*out_image_buf != NULL) {
111*d289c2baSAndroid Build Coastguard Worker *out_image_preloaded = true;
112*d289c2baSAndroid Build Coastguard Worker if (part_num_read != image_size) {
113*d289c2baSAndroid Build Coastguard Worker avb_error(part_name, ": Read incorrect number of bytes.\n");
114*d289c2baSAndroid Build Coastguard Worker return AVB_SLOT_VERIFY_RESULT_ERROR_IO;
115*d289c2baSAndroid Build Coastguard Worker }
116*d289c2baSAndroid Build Coastguard Worker }
117*d289c2baSAndroid Build Coastguard Worker }
118*d289c2baSAndroid Build Coastguard Worker
119*d289c2baSAndroid Build Coastguard Worker /* Allocate and copy the partition. */
120*d289c2baSAndroid Build Coastguard Worker if (!*out_image_preloaded) {
121*d289c2baSAndroid Build Coastguard Worker *out_image_buf = avb_malloc(image_size);
122*d289c2baSAndroid Build Coastguard Worker if (*out_image_buf == NULL) {
123*d289c2baSAndroid Build Coastguard Worker return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
124*d289c2baSAndroid Build Coastguard Worker }
125*d289c2baSAndroid Build Coastguard Worker
126*d289c2baSAndroid Build Coastguard Worker io_ret = ops->read_from_partition(ops,
127*d289c2baSAndroid Build Coastguard Worker part_name,
128*d289c2baSAndroid Build Coastguard Worker 0 /* offset */,
129*d289c2baSAndroid Build Coastguard Worker image_size,
130*d289c2baSAndroid Build Coastguard Worker *out_image_buf,
131*d289c2baSAndroid Build Coastguard Worker &part_num_read);
132*d289c2baSAndroid Build Coastguard Worker if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
133*d289c2baSAndroid Build Coastguard Worker return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
134*d289c2baSAndroid Build Coastguard Worker } else if (io_ret != AVB_IO_RESULT_OK) {
135*d289c2baSAndroid Build Coastguard Worker avb_error(part_name, ": Error loading data from partition.\n");
136*d289c2baSAndroid Build Coastguard Worker return AVB_SLOT_VERIFY_RESULT_ERROR_IO;
137*d289c2baSAndroid Build Coastguard Worker }
138*d289c2baSAndroid Build Coastguard Worker if (part_num_read != image_size) {
139*d289c2baSAndroid Build Coastguard Worker avb_error(part_name, ": Read incorrect number of bytes.\n");
140*d289c2baSAndroid Build Coastguard Worker return AVB_SLOT_VERIFY_RESULT_ERROR_IO;
141*d289c2baSAndroid Build Coastguard Worker }
142*d289c2baSAndroid Build Coastguard Worker }
143*d289c2baSAndroid Build Coastguard Worker
144*d289c2baSAndroid Build Coastguard Worker return AVB_SLOT_VERIFY_RESULT_OK;
145*d289c2baSAndroid Build Coastguard Worker }
146*d289c2baSAndroid Build Coastguard Worker
147*d289c2baSAndroid Build Coastguard Worker /* Reads a persistent digest stored as a named persistent value corresponding to
148*d289c2baSAndroid Build Coastguard Worker * the given |part_name|. The value is returned in |out_digest| which must point
149*d289c2baSAndroid Build Coastguard Worker * to |expected_digest_size| bytes. If there is no digest stored for |part_name|
150*d289c2baSAndroid Build Coastguard Worker * it can be initialized by providing a non-NULL |initial_digest| of length
151*d289c2baSAndroid Build Coastguard Worker * |expected_digest_size|. This automatic initialization will only occur if the
152*d289c2baSAndroid Build Coastguard Worker * device is currently locked. The |initial_digest| may be NULL.
153*d289c2baSAndroid Build Coastguard Worker *
154*d289c2baSAndroid Build Coastguard Worker * Returns AVB_SLOT_VERIFY_RESULT_OK on success, otherwise returns an
155*d289c2baSAndroid Build Coastguard Worker * AVB_SLOT_VERIFY_RESULT_ERROR_* error code.
156*d289c2baSAndroid Build Coastguard Worker *
157*d289c2baSAndroid Build Coastguard Worker * If the value does not exist, is not supported, or is not populated, and
158*d289c2baSAndroid Build Coastguard Worker * |initial_digest| is NULL, returns
159*d289c2baSAndroid Build Coastguard Worker * AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA. If |expected_digest_size| does
160*d289c2baSAndroid Build Coastguard Worker * not match the stored digest size, also returns
161*d289c2baSAndroid Build Coastguard Worker * AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA.
162*d289c2baSAndroid Build Coastguard Worker */
read_persistent_digest(AvbOps * ops,const char * part_name,size_t expected_digest_size,const uint8_t * initial_digest,uint8_t * out_digest)163*d289c2baSAndroid Build Coastguard Worker static AvbSlotVerifyResult read_persistent_digest(AvbOps* ops,
164*d289c2baSAndroid Build Coastguard Worker const char* part_name,
165*d289c2baSAndroid Build Coastguard Worker size_t expected_digest_size,
166*d289c2baSAndroid Build Coastguard Worker const uint8_t* initial_digest,
167*d289c2baSAndroid Build Coastguard Worker uint8_t* out_digest) {
168*d289c2baSAndroid Build Coastguard Worker char* persistent_value_name = NULL;
169*d289c2baSAndroid Build Coastguard Worker AvbIOResult io_ret = AVB_IO_RESULT_OK;
170*d289c2baSAndroid Build Coastguard Worker size_t stored_digest_size = 0;
171*d289c2baSAndroid Build Coastguard Worker
172*d289c2baSAndroid Build Coastguard Worker if (ops->read_persistent_value == NULL) {
173*d289c2baSAndroid Build Coastguard Worker avb_error(part_name, ": Persistent values are not implemented.\n");
174*d289c2baSAndroid Build Coastguard Worker return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
175*d289c2baSAndroid Build Coastguard Worker }
176*d289c2baSAndroid Build Coastguard Worker persistent_value_name =
177*d289c2baSAndroid Build Coastguard Worker avb_strdupv(AVB_NPV_PERSISTENT_DIGEST_PREFIX, part_name, NULL);
178*d289c2baSAndroid Build Coastguard Worker if (persistent_value_name == NULL) {
179*d289c2baSAndroid Build Coastguard Worker return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
180*d289c2baSAndroid Build Coastguard Worker }
181*d289c2baSAndroid Build Coastguard Worker
182*d289c2baSAndroid Build Coastguard Worker io_ret = ops->read_persistent_value(ops,
183*d289c2baSAndroid Build Coastguard Worker persistent_value_name,
184*d289c2baSAndroid Build Coastguard Worker expected_digest_size,
185*d289c2baSAndroid Build Coastguard Worker out_digest,
186*d289c2baSAndroid Build Coastguard Worker &stored_digest_size);
187*d289c2baSAndroid Build Coastguard Worker
188*d289c2baSAndroid Build Coastguard Worker // If no such named persistent value exists and an initial digest value was
189*d289c2baSAndroid Build Coastguard Worker // given, initialize the named persistent value with the given digest. If
190*d289c2baSAndroid Build Coastguard Worker // initialized successfully, this will recurse into this function but with a
191*d289c2baSAndroid Build Coastguard Worker // NULL initial_digest.
192*d289c2baSAndroid Build Coastguard Worker if (io_ret == AVB_IO_RESULT_ERROR_NO_SUCH_VALUE && initial_digest) {
193*d289c2baSAndroid Build Coastguard Worker AvbSlotVerifyResult ret =
194*d289c2baSAndroid Build Coastguard Worker initialize_persistent_digest(ops,
195*d289c2baSAndroid Build Coastguard Worker part_name,
196*d289c2baSAndroid Build Coastguard Worker persistent_value_name,
197*d289c2baSAndroid Build Coastguard Worker expected_digest_size,
198*d289c2baSAndroid Build Coastguard Worker initial_digest,
199*d289c2baSAndroid Build Coastguard Worker out_digest);
200*d289c2baSAndroid Build Coastguard Worker avb_free(persistent_value_name);
201*d289c2baSAndroid Build Coastguard Worker return ret;
202*d289c2baSAndroid Build Coastguard Worker }
203*d289c2baSAndroid Build Coastguard Worker avb_free(persistent_value_name);
204*d289c2baSAndroid Build Coastguard Worker
205*d289c2baSAndroid Build Coastguard Worker if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
206*d289c2baSAndroid Build Coastguard Worker return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
207*d289c2baSAndroid Build Coastguard Worker } else if (io_ret == AVB_IO_RESULT_ERROR_NO_SUCH_VALUE) {
208*d289c2baSAndroid Build Coastguard Worker // Treat a missing persistent value as a verification error, which is
209*d289c2baSAndroid Build Coastguard Worker // ignoreable, rather than a metadata error which is not.
210*d289c2baSAndroid Build Coastguard Worker avb_error(part_name, ": Persistent digest does not exist.\n");
211*d289c2baSAndroid Build Coastguard Worker return AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION;
212*d289c2baSAndroid Build Coastguard Worker } else if (io_ret == AVB_IO_RESULT_ERROR_INVALID_VALUE_SIZE ||
213*d289c2baSAndroid Build Coastguard Worker io_ret == AVB_IO_RESULT_ERROR_INSUFFICIENT_SPACE) {
214*d289c2baSAndroid Build Coastguard Worker avb_error(part_name, ": Persistent digest is not of expected size.\n");
215*d289c2baSAndroid Build Coastguard Worker return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
216*d289c2baSAndroid Build Coastguard Worker } else if (io_ret != AVB_IO_RESULT_OK) {
217*d289c2baSAndroid Build Coastguard Worker avb_error(part_name, ": Error reading persistent digest.\n");
218*d289c2baSAndroid Build Coastguard Worker return AVB_SLOT_VERIFY_RESULT_ERROR_IO;
219*d289c2baSAndroid Build Coastguard Worker } else if (expected_digest_size != stored_digest_size) {
220*d289c2baSAndroid Build Coastguard Worker avb_error(part_name, ": Persistent digest is not of expected size.\n");
221*d289c2baSAndroid Build Coastguard Worker return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
222*d289c2baSAndroid Build Coastguard Worker }
223*d289c2baSAndroid Build Coastguard Worker return AVB_SLOT_VERIFY_RESULT_OK;
224*d289c2baSAndroid Build Coastguard Worker }
225*d289c2baSAndroid Build Coastguard Worker
initialize_persistent_digest(AvbOps * ops,const char * part_name,const char * persistent_value_name,size_t digest_size,const uint8_t * initial_digest,uint8_t * out_digest)226*d289c2baSAndroid Build Coastguard Worker static AvbSlotVerifyResult initialize_persistent_digest(
227*d289c2baSAndroid Build Coastguard Worker AvbOps* ops,
228*d289c2baSAndroid Build Coastguard Worker const char* part_name,
229*d289c2baSAndroid Build Coastguard Worker const char* persistent_value_name,
230*d289c2baSAndroid Build Coastguard Worker size_t digest_size,
231*d289c2baSAndroid Build Coastguard Worker const uint8_t* initial_digest,
232*d289c2baSAndroid Build Coastguard Worker uint8_t* out_digest) {
233*d289c2baSAndroid Build Coastguard Worker AvbSlotVerifyResult ret;
234*d289c2baSAndroid Build Coastguard Worker AvbIOResult io_ret = AVB_IO_RESULT_OK;
235*d289c2baSAndroid Build Coastguard Worker bool is_device_unlocked = true;
236*d289c2baSAndroid Build Coastguard Worker
237*d289c2baSAndroid Build Coastguard Worker io_ret = ops->read_is_device_unlocked(ops, &is_device_unlocked);
238*d289c2baSAndroid Build Coastguard Worker if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
239*d289c2baSAndroid Build Coastguard Worker return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
240*d289c2baSAndroid Build Coastguard Worker } else if (io_ret != AVB_IO_RESULT_OK) {
241*d289c2baSAndroid Build Coastguard Worker avb_error("Error getting device lock state.\n");
242*d289c2baSAndroid Build Coastguard Worker return AVB_SLOT_VERIFY_RESULT_ERROR_IO;
243*d289c2baSAndroid Build Coastguard Worker }
244*d289c2baSAndroid Build Coastguard Worker
245*d289c2baSAndroid Build Coastguard Worker if (is_device_unlocked) {
246*d289c2baSAndroid Build Coastguard Worker avb_debug(part_name,
247*d289c2baSAndroid Build Coastguard Worker ": Digest does not exist, device unlocked so not initializing "
248*d289c2baSAndroid Build Coastguard Worker "digest.\n");
249*d289c2baSAndroid Build Coastguard Worker return AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION;
250*d289c2baSAndroid Build Coastguard Worker }
251*d289c2baSAndroid Build Coastguard Worker
252*d289c2baSAndroid Build Coastguard Worker // Device locked; initialize digest with given initial value.
253*d289c2baSAndroid Build Coastguard Worker avb_debug(part_name,
254*d289c2baSAndroid Build Coastguard Worker ": Digest does not exist, initializing persistent digest.\n");
255*d289c2baSAndroid Build Coastguard Worker io_ret = ops->write_persistent_value(
256*d289c2baSAndroid Build Coastguard Worker ops, persistent_value_name, digest_size, initial_digest);
257*d289c2baSAndroid Build Coastguard Worker if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
258*d289c2baSAndroid Build Coastguard Worker return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
259*d289c2baSAndroid Build Coastguard Worker } else if (io_ret != AVB_IO_RESULT_OK) {
260*d289c2baSAndroid Build Coastguard Worker avb_error(part_name, ": Error initializing persistent digest.\n");
261*d289c2baSAndroid Build Coastguard Worker return AVB_SLOT_VERIFY_RESULT_ERROR_IO;
262*d289c2baSAndroid Build Coastguard Worker }
263*d289c2baSAndroid Build Coastguard Worker
264*d289c2baSAndroid Build Coastguard Worker // To ensure that the digest value was written successfully - and avoid a
265*d289c2baSAndroid Build Coastguard Worker // scenario where the digest is simply 'initialized' on every verify - recurse
266*d289c2baSAndroid Build Coastguard Worker // into read_persistent_digest to read back the written value. The NULL
267*d289c2baSAndroid Build Coastguard Worker // initial_digest ensures that this will not recurse again.
268*d289c2baSAndroid Build Coastguard Worker ret = read_persistent_digest(ops, part_name, digest_size, NULL, out_digest);
269*d289c2baSAndroid Build Coastguard Worker if (ret != AVB_SLOT_VERIFY_RESULT_OK) {
270*d289c2baSAndroid Build Coastguard Worker avb_error(part_name,
271*d289c2baSAndroid Build Coastguard Worker ": Reading back initialized persistent digest failed!\n");
272*d289c2baSAndroid Build Coastguard Worker }
273*d289c2baSAndroid Build Coastguard Worker return ret;
274*d289c2baSAndroid Build Coastguard Worker }
275*d289c2baSAndroid Build Coastguard Worker
load_and_verify_hash_partition(AvbOps * ops,const char * const * requested_partitions,const char * ab_suffix,bool allow_verification_error,const AvbDescriptor * descriptor,AvbSlotVerifyData * slot_data)276*d289c2baSAndroid Build Coastguard Worker static AvbSlotVerifyResult load_and_verify_hash_partition(
277*d289c2baSAndroid Build Coastguard Worker AvbOps* ops,
278*d289c2baSAndroid Build Coastguard Worker const char* const* requested_partitions,
279*d289c2baSAndroid Build Coastguard Worker const char* ab_suffix,
280*d289c2baSAndroid Build Coastguard Worker bool allow_verification_error,
281*d289c2baSAndroid Build Coastguard Worker const AvbDescriptor* descriptor,
282*d289c2baSAndroid Build Coastguard Worker AvbSlotVerifyData* slot_data) {
283*d289c2baSAndroid Build Coastguard Worker AvbHashDescriptor hash_desc;
284*d289c2baSAndroid Build Coastguard Worker const uint8_t* desc_partition_name = NULL;
285*d289c2baSAndroid Build Coastguard Worker const uint8_t* desc_salt;
286*d289c2baSAndroid Build Coastguard Worker const uint8_t* desc_digest;
287*d289c2baSAndroid Build Coastguard Worker char part_name[AVB_PART_NAME_MAX_SIZE];
288*d289c2baSAndroid Build Coastguard Worker AvbSlotVerifyResult ret;
289*d289c2baSAndroid Build Coastguard Worker AvbIOResult io_ret;
290*d289c2baSAndroid Build Coastguard Worker uint8_t* image_buf = NULL;
291*d289c2baSAndroid Build Coastguard Worker bool image_preloaded = false;
292*d289c2baSAndroid Build Coastguard Worker uint8_t* digest;
293*d289c2baSAndroid Build Coastguard Worker size_t digest_len;
294*d289c2baSAndroid Build Coastguard Worker const char* found;
295*d289c2baSAndroid Build Coastguard Worker uint64_t image_size;
296*d289c2baSAndroid Build Coastguard Worker size_t expected_digest_len = 0;
297*d289c2baSAndroid Build Coastguard Worker uint8_t expected_digest_buf[AVB_SHA512_DIGEST_SIZE];
298*d289c2baSAndroid Build Coastguard Worker const uint8_t* expected_digest = NULL;
299*d289c2baSAndroid Build Coastguard Worker
300*d289c2baSAndroid Build Coastguard Worker if (!avb_hash_descriptor_validate_and_byteswap(
301*d289c2baSAndroid Build Coastguard Worker (const AvbHashDescriptor*)descriptor, &hash_desc)) {
302*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
303*d289c2baSAndroid Build Coastguard Worker goto out;
304*d289c2baSAndroid Build Coastguard Worker }
305*d289c2baSAndroid Build Coastguard Worker
306*d289c2baSAndroid Build Coastguard Worker desc_partition_name =
307*d289c2baSAndroid Build Coastguard Worker ((const uint8_t*)descriptor) + sizeof(AvbHashDescriptor);
308*d289c2baSAndroid Build Coastguard Worker desc_salt = desc_partition_name + hash_desc.partition_name_len;
309*d289c2baSAndroid Build Coastguard Worker desc_digest = desc_salt + hash_desc.salt_len;
310*d289c2baSAndroid Build Coastguard Worker
311*d289c2baSAndroid Build Coastguard Worker if (!avb_validate_utf8(desc_partition_name, hash_desc.partition_name_len)) {
312*d289c2baSAndroid Build Coastguard Worker avb_error("Partition name is not valid UTF-8.\n");
313*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
314*d289c2baSAndroid Build Coastguard Worker goto out;
315*d289c2baSAndroid Build Coastguard Worker }
316*d289c2baSAndroid Build Coastguard Worker
317*d289c2baSAndroid Build Coastguard Worker /* Don't bother loading or validating unless the partition was
318*d289c2baSAndroid Build Coastguard Worker * requested in the first place.
319*d289c2baSAndroid Build Coastguard Worker */
320*d289c2baSAndroid Build Coastguard Worker found = avb_strv_find_str(requested_partitions,
321*d289c2baSAndroid Build Coastguard Worker (const char*)desc_partition_name,
322*d289c2baSAndroid Build Coastguard Worker hash_desc.partition_name_len);
323*d289c2baSAndroid Build Coastguard Worker if (found == NULL) {
324*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_OK;
325*d289c2baSAndroid Build Coastguard Worker goto out;
326*d289c2baSAndroid Build Coastguard Worker }
327*d289c2baSAndroid Build Coastguard Worker
328*d289c2baSAndroid Build Coastguard Worker if ((hash_desc.flags & AVB_HASH_DESCRIPTOR_FLAGS_DO_NOT_USE_AB) != 0) {
329*d289c2baSAndroid Build Coastguard Worker /* No ab_suffix, just copy the partition name as is. */
330*d289c2baSAndroid Build Coastguard Worker if (hash_desc.partition_name_len >= AVB_PART_NAME_MAX_SIZE) {
331*d289c2baSAndroid Build Coastguard Worker avb_error("Partition name does not fit.\n");
332*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
333*d289c2baSAndroid Build Coastguard Worker goto out;
334*d289c2baSAndroid Build Coastguard Worker }
335*d289c2baSAndroid Build Coastguard Worker avb_memcpy(part_name, desc_partition_name, hash_desc.partition_name_len);
336*d289c2baSAndroid Build Coastguard Worker part_name[hash_desc.partition_name_len] = '\0';
337*d289c2baSAndroid Build Coastguard Worker } else if (hash_desc.digest_len == 0 && avb_strlen(ab_suffix) != 0) {
338*d289c2baSAndroid Build Coastguard Worker /* No ab_suffix allowed for partitions without a digest in the descriptor
339*d289c2baSAndroid Build Coastguard Worker * because these partitions hold data unique to this device and are not
340*d289c2baSAndroid Build Coastguard Worker * updated using an A/B scheme.
341*d289c2baSAndroid Build Coastguard Worker */
342*d289c2baSAndroid Build Coastguard Worker avb_error("Cannot use A/B with a persistent digest.\n");
343*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
344*d289c2baSAndroid Build Coastguard Worker goto out;
345*d289c2baSAndroid Build Coastguard Worker } else {
346*d289c2baSAndroid Build Coastguard Worker /* Add ab_suffix to the partition name. */
347*d289c2baSAndroid Build Coastguard Worker if (!avb_str_concat(part_name,
348*d289c2baSAndroid Build Coastguard Worker sizeof part_name,
349*d289c2baSAndroid Build Coastguard Worker (const char*)desc_partition_name,
350*d289c2baSAndroid Build Coastguard Worker hash_desc.partition_name_len,
351*d289c2baSAndroid Build Coastguard Worker ab_suffix,
352*d289c2baSAndroid Build Coastguard Worker avb_strlen(ab_suffix))) {
353*d289c2baSAndroid Build Coastguard Worker avb_error("Partition name and suffix does not fit.\n");
354*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
355*d289c2baSAndroid Build Coastguard Worker goto out;
356*d289c2baSAndroid Build Coastguard Worker }
357*d289c2baSAndroid Build Coastguard Worker }
358*d289c2baSAndroid Build Coastguard Worker
359*d289c2baSAndroid Build Coastguard Worker /* If we're allowing verification errors then hash_desc.image_size
360*d289c2baSAndroid Build Coastguard Worker * may no longer match what's in the partition... so in this case
361*d289c2baSAndroid Build Coastguard Worker * just load the entire partition.
362*d289c2baSAndroid Build Coastguard Worker *
363*d289c2baSAndroid Build Coastguard Worker * For example, this can happen if a developer does 'fastboot flash
364*d289c2baSAndroid Build Coastguard Worker * boot /path/to/new/and/bigger/boot.img'. We want this to work
365*d289c2baSAndroid Build Coastguard Worker * since it's such a common workflow.
366*d289c2baSAndroid Build Coastguard Worker */
367*d289c2baSAndroid Build Coastguard Worker image_size = hash_desc.image_size;
368*d289c2baSAndroid Build Coastguard Worker if (allow_verification_error) {
369*d289c2baSAndroid Build Coastguard Worker io_ret = ops->get_size_of_partition(ops, part_name, &image_size);
370*d289c2baSAndroid Build Coastguard Worker if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
371*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
372*d289c2baSAndroid Build Coastguard Worker goto out;
373*d289c2baSAndroid Build Coastguard Worker } else if (io_ret != AVB_IO_RESULT_OK) {
374*d289c2baSAndroid Build Coastguard Worker avb_error(part_name, ": Error determining partition size.\n");
375*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
376*d289c2baSAndroid Build Coastguard Worker goto out;
377*d289c2baSAndroid Build Coastguard Worker }
378*d289c2baSAndroid Build Coastguard Worker avb_debug(part_name, ": Loading entire partition.\n");
379*d289c2baSAndroid Build Coastguard Worker }
380*d289c2baSAndroid Build Coastguard Worker
381*d289c2baSAndroid Build Coastguard Worker ret = load_full_partition(
382*d289c2baSAndroid Build Coastguard Worker ops, part_name, image_size, &image_buf, &image_preloaded);
383*d289c2baSAndroid Build Coastguard Worker if (ret != AVB_SLOT_VERIFY_RESULT_OK) {
384*d289c2baSAndroid Build Coastguard Worker goto out;
385*d289c2baSAndroid Build Coastguard Worker }
386*d289c2baSAndroid Build Coastguard Worker // Although only one of the type might be used, we have to defined the
387*d289c2baSAndroid Build Coastguard Worker // structure here so that they would live outside the 'if/else' scope to be
388*d289c2baSAndroid Build Coastguard Worker // used later.
389*d289c2baSAndroid Build Coastguard Worker AvbSHA256Ctx sha256_ctx;
390*d289c2baSAndroid Build Coastguard Worker AvbSHA512Ctx sha512_ctx;
391*d289c2baSAndroid Build Coastguard Worker size_t image_size_to_hash = hash_desc.image_size;
392*d289c2baSAndroid Build Coastguard Worker // If we allow verification error and the whole partition is smaller than
393*d289c2baSAndroid Build Coastguard Worker // image size in hash descriptor, we just hash the whole partition.
394*d289c2baSAndroid Build Coastguard Worker if (image_size_to_hash > image_size) {
395*d289c2baSAndroid Build Coastguard Worker image_size_to_hash = image_size;
396*d289c2baSAndroid Build Coastguard Worker }
397*d289c2baSAndroid Build Coastguard Worker if (avb_strcmp((const char*)hash_desc.hash_algorithm, "sha256") == 0) {
398*d289c2baSAndroid Build Coastguard Worker avb_sha256_init(&sha256_ctx);
399*d289c2baSAndroid Build Coastguard Worker avb_sha256_update(&sha256_ctx, desc_salt, hash_desc.salt_len);
400*d289c2baSAndroid Build Coastguard Worker avb_sha256_update(&sha256_ctx, image_buf, image_size_to_hash);
401*d289c2baSAndroid Build Coastguard Worker digest = avb_sha256_final(&sha256_ctx);
402*d289c2baSAndroid Build Coastguard Worker digest_len = AVB_SHA256_DIGEST_SIZE;
403*d289c2baSAndroid Build Coastguard Worker } else if (avb_strcmp((const char*)hash_desc.hash_algorithm, "sha512") == 0) {
404*d289c2baSAndroid Build Coastguard Worker avb_sha512_init(&sha512_ctx);
405*d289c2baSAndroid Build Coastguard Worker avb_sha512_update(&sha512_ctx, desc_salt, hash_desc.salt_len);
406*d289c2baSAndroid Build Coastguard Worker avb_sha512_update(&sha512_ctx, image_buf, image_size_to_hash);
407*d289c2baSAndroid Build Coastguard Worker digest = avb_sha512_final(&sha512_ctx);
408*d289c2baSAndroid Build Coastguard Worker digest_len = AVB_SHA512_DIGEST_SIZE;
409*d289c2baSAndroid Build Coastguard Worker } else {
410*d289c2baSAndroid Build Coastguard Worker avb_error(part_name, ": Unsupported hash algorithm.\n");
411*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
412*d289c2baSAndroid Build Coastguard Worker goto out;
413*d289c2baSAndroid Build Coastguard Worker }
414*d289c2baSAndroid Build Coastguard Worker
415*d289c2baSAndroid Build Coastguard Worker if (hash_desc.digest_len == 0) {
416*d289c2baSAndroid Build Coastguard Worker /* Expect a match to a persistent digest. */
417*d289c2baSAndroid Build Coastguard Worker avb_debug(part_name, ": No digest, using persistent digest.\n");
418*d289c2baSAndroid Build Coastguard Worker expected_digest_len = digest_len;
419*d289c2baSAndroid Build Coastguard Worker expected_digest = expected_digest_buf;
420*d289c2baSAndroid Build Coastguard Worker avb_assert(expected_digest_len <= sizeof(expected_digest_buf));
421*d289c2baSAndroid Build Coastguard Worker /* Pass |digest| as the |initial_digest| so devices not yet initialized get
422*d289c2baSAndroid Build Coastguard Worker * initialized to the current partition digest.
423*d289c2baSAndroid Build Coastguard Worker */
424*d289c2baSAndroid Build Coastguard Worker ret = read_persistent_digest(
425*d289c2baSAndroid Build Coastguard Worker ops, part_name, digest_len, digest, expected_digest_buf);
426*d289c2baSAndroid Build Coastguard Worker if (ret != AVB_SLOT_VERIFY_RESULT_OK) {
427*d289c2baSAndroid Build Coastguard Worker goto out;
428*d289c2baSAndroid Build Coastguard Worker }
429*d289c2baSAndroid Build Coastguard Worker } else {
430*d289c2baSAndroid Build Coastguard Worker /* Expect a match to the digest in the descriptor. */
431*d289c2baSAndroid Build Coastguard Worker expected_digest_len = hash_desc.digest_len;
432*d289c2baSAndroid Build Coastguard Worker expected_digest = desc_digest;
433*d289c2baSAndroid Build Coastguard Worker }
434*d289c2baSAndroid Build Coastguard Worker
435*d289c2baSAndroid Build Coastguard Worker if (digest_len != expected_digest_len) {
436*d289c2baSAndroid Build Coastguard Worker avb_error(part_name, ": Digest in descriptor not of expected size.\n");
437*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
438*d289c2baSAndroid Build Coastguard Worker goto out;
439*d289c2baSAndroid Build Coastguard Worker }
440*d289c2baSAndroid Build Coastguard Worker
441*d289c2baSAndroid Build Coastguard Worker if (avb_safe_memcmp(digest, expected_digest, digest_len) != 0) {
442*d289c2baSAndroid Build Coastguard Worker avb_error(part_name,
443*d289c2baSAndroid Build Coastguard Worker ": Hash of data does not match digest in descriptor.\n");
444*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION;
445*d289c2baSAndroid Build Coastguard Worker goto out;
446*d289c2baSAndroid Build Coastguard Worker }
447*d289c2baSAndroid Build Coastguard Worker
448*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_OK;
449*d289c2baSAndroid Build Coastguard Worker
450*d289c2baSAndroid Build Coastguard Worker out:
451*d289c2baSAndroid Build Coastguard Worker
452*d289c2baSAndroid Build Coastguard Worker /* If it worked and something was loaded, copy to slot_data. */
453*d289c2baSAndroid Build Coastguard Worker if ((ret == AVB_SLOT_VERIFY_RESULT_OK || result_should_continue(ret)) &&
454*d289c2baSAndroid Build Coastguard Worker image_buf != NULL) {
455*d289c2baSAndroid Build Coastguard Worker AvbPartitionData* loaded_partition;
456*d289c2baSAndroid Build Coastguard Worker if (slot_data->num_loaded_partitions == MAX_NUMBER_OF_LOADED_PARTITIONS) {
457*d289c2baSAndroid Build Coastguard Worker avb_error(part_name, ": Too many loaded partitions.\n");
458*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
459*d289c2baSAndroid Build Coastguard Worker goto fail;
460*d289c2baSAndroid Build Coastguard Worker }
461*d289c2baSAndroid Build Coastguard Worker loaded_partition =
462*d289c2baSAndroid Build Coastguard Worker &slot_data->loaded_partitions[slot_data->num_loaded_partitions++];
463*d289c2baSAndroid Build Coastguard Worker loaded_partition->partition_name = avb_strdup(found);
464*d289c2baSAndroid Build Coastguard Worker loaded_partition->data_size = image_size;
465*d289c2baSAndroid Build Coastguard Worker loaded_partition->data = image_buf;
466*d289c2baSAndroid Build Coastguard Worker loaded_partition->preloaded = image_preloaded;
467*d289c2baSAndroid Build Coastguard Worker loaded_partition->verify_result = ret;
468*d289c2baSAndroid Build Coastguard Worker image_buf = NULL;
469*d289c2baSAndroid Build Coastguard Worker }
470*d289c2baSAndroid Build Coastguard Worker
471*d289c2baSAndroid Build Coastguard Worker fail:
472*d289c2baSAndroid Build Coastguard Worker if (image_buf != NULL && !image_preloaded) {
473*d289c2baSAndroid Build Coastguard Worker avb_free(image_buf);
474*d289c2baSAndroid Build Coastguard Worker }
475*d289c2baSAndroid Build Coastguard Worker return ret;
476*d289c2baSAndroid Build Coastguard Worker }
477*d289c2baSAndroid Build Coastguard Worker
load_requested_partitions(AvbOps * ops,const char * const * requested_partitions,const char * ab_suffix,AvbSlotVerifyData * slot_data)478*d289c2baSAndroid Build Coastguard Worker static AvbSlotVerifyResult load_requested_partitions(
479*d289c2baSAndroid Build Coastguard Worker AvbOps* ops,
480*d289c2baSAndroid Build Coastguard Worker const char* const* requested_partitions,
481*d289c2baSAndroid Build Coastguard Worker const char* ab_suffix,
482*d289c2baSAndroid Build Coastguard Worker AvbSlotVerifyData* slot_data) {
483*d289c2baSAndroid Build Coastguard Worker AvbSlotVerifyResult ret;
484*d289c2baSAndroid Build Coastguard Worker uint8_t* image_buf = NULL;
485*d289c2baSAndroid Build Coastguard Worker bool image_preloaded = false;
486*d289c2baSAndroid Build Coastguard Worker size_t n;
487*d289c2baSAndroid Build Coastguard Worker
488*d289c2baSAndroid Build Coastguard Worker for (n = 0; requested_partitions[n] != NULL; n++) {
489*d289c2baSAndroid Build Coastguard Worker char part_name[AVB_PART_NAME_MAX_SIZE];
490*d289c2baSAndroid Build Coastguard Worker AvbIOResult io_ret;
491*d289c2baSAndroid Build Coastguard Worker uint64_t image_size;
492*d289c2baSAndroid Build Coastguard Worker AvbPartitionData* loaded_partition;
493*d289c2baSAndroid Build Coastguard Worker
494*d289c2baSAndroid Build Coastguard Worker if (!avb_str_concat(part_name,
495*d289c2baSAndroid Build Coastguard Worker sizeof part_name,
496*d289c2baSAndroid Build Coastguard Worker requested_partitions[n],
497*d289c2baSAndroid Build Coastguard Worker avb_strlen(requested_partitions[n]),
498*d289c2baSAndroid Build Coastguard Worker ab_suffix,
499*d289c2baSAndroid Build Coastguard Worker avb_strlen(ab_suffix))) {
500*d289c2baSAndroid Build Coastguard Worker avb_error("Partition name and suffix does not fit.\n");
501*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
502*d289c2baSAndroid Build Coastguard Worker goto out;
503*d289c2baSAndroid Build Coastguard Worker }
504*d289c2baSAndroid Build Coastguard Worker
505*d289c2baSAndroid Build Coastguard Worker io_ret = ops->get_size_of_partition(ops, part_name, &image_size);
506*d289c2baSAndroid Build Coastguard Worker if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
507*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
508*d289c2baSAndroid Build Coastguard Worker goto out;
509*d289c2baSAndroid Build Coastguard Worker } else if (io_ret != AVB_IO_RESULT_OK) {
510*d289c2baSAndroid Build Coastguard Worker avb_error(part_name, ": Error determining partition size.\n");
511*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
512*d289c2baSAndroid Build Coastguard Worker goto out;
513*d289c2baSAndroid Build Coastguard Worker }
514*d289c2baSAndroid Build Coastguard Worker avb_debug(part_name, ": Loading entire partition.\n");
515*d289c2baSAndroid Build Coastguard Worker
516*d289c2baSAndroid Build Coastguard Worker ret = load_full_partition(
517*d289c2baSAndroid Build Coastguard Worker ops, part_name, image_size, &image_buf, &image_preloaded);
518*d289c2baSAndroid Build Coastguard Worker if (ret != AVB_SLOT_VERIFY_RESULT_OK) {
519*d289c2baSAndroid Build Coastguard Worker goto out;
520*d289c2baSAndroid Build Coastguard Worker }
521*d289c2baSAndroid Build Coastguard Worker
522*d289c2baSAndroid Build Coastguard Worker /* Move to slot_data. */
523*d289c2baSAndroid Build Coastguard Worker if (slot_data->num_loaded_partitions == MAX_NUMBER_OF_LOADED_PARTITIONS) {
524*d289c2baSAndroid Build Coastguard Worker avb_error(part_name, ": Too many loaded partitions.\n");
525*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
526*d289c2baSAndroid Build Coastguard Worker goto out;
527*d289c2baSAndroid Build Coastguard Worker }
528*d289c2baSAndroid Build Coastguard Worker loaded_partition =
529*d289c2baSAndroid Build Coastguard Worker &slot_data->loaded_partitions[slot_data->num_loaded_partitions++];
530*d289c2baSAndroid Build Coastguard Worker loaded_partition->partition_name = avb_strdup(requested_partitions[n]);
531*d289c2baSAndroid Build Coastguard Worker if (loaded_partition->partition_name == NULL) {
532*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
533*d289c2baSAndroid Build Coastguard Worker goto out;
534*d289c2baSAndroid Build Coastguard Worker }
535*d289c2baSAndroid Build Coastguard Worker loaded_partition->data_size = image_size;
536*d289c2baSAndroid Build Coastguard Worker loaded_partition->data = image_buf; /* Transferring the owner. */
537*d289c2baSAndroid Build Coastguard Worker loaded_partition->preloaded = image_preloaded;
538*d289c2baSAndroid Build Coastguard Worker image_buf = NULL;
539*d289c2baSAndroid Build Coastguard Worker image_preloaded = false;
540*d289c2baSAndroid Build Coastguard Worker }
541*d289c2baSAndroid Build Coastguard Worker
542*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_OK;
543*d289c2baSAndroid Build Coastguard Worker
544*d289c2baSAndroid Build Coastguard Worker out:
545*d289c2baSAndroid Build Coastguard Worker /* Free the current buffer if any. */
546*d289c2baSAndroid Build Coastguard Worker if (image_buf != NULL && !image_preloaded) {
547*d289c2baSAndroid Build Coastguard Worker avb_free(image_buf);
548*d289c2baSAndroid Build Coastguard Worker }
549*d289c2baSAndroid Build Coastguard Worker /* Buffers that are already saved in slot_data will be handled by the caller
550*d289c2baSAndroid Build Coastguard Worker * even on failure. */
551*d289c2baSAndroid Build Coastguard Worker return ret;
552*d289c2baSAndroid Build Coastguard Worker }
553*d289c2baSAndroid Build Coastguard Worker
load_and_verify_vbmeta(AvbOps * ops,const char * const * requested_partitions,const char * ab_suffix,AvbSlotVerifyFlags flags,bool allow_verification_error,AvbVBMetaImageFlags toplevel_vbmeta_flags,uint32_t rollback_index_location,const char * partition_name,size_t partition_name_len,const uint8_t * expected_public_key,size_t expected_public_key_length,AvbSlotVerifyData * slot_data,AvbAlgorithmType * out_algorithm_type,AvbCmdlineSubstList * out_additional_cmdline_subst,bool use_ab_suffix)554*d289c2baSAndroid Build Coastguard Worker static AvbSlotVerifyResult load_and_verify_vbmeta(
555*d289c2baSAndroid Build Coastguard Worker AvbOps* ops,
556*d289c2baSAndroid Build Coastguard Worker const char* const* requested_partitions,
557*d289c2baSAndroid Build Coastguard Worker const char* ab_suffix,
558*d289c2baSAndroid Build Coastguard Worker AvbSlotVerifyFlags flags,
559*d289c2baSAndroid Build Coastguard Worker bool allow_verification_error,
560*d289c2baSAndroid Build Coastguard Worker AvbVBMetaImageFlags toplevel_vbmeta_flags,
561*d289c2baSAndroid Build Coastguard Worker uint32_t rollback_index_location,
562*d289c2baSAndroid Build Coastguard Worker const char* partition_name,
563*d289c2baSAndroid Build Coastguard Worker size_t partition_name_len,
564*d289c2baSAndroid Build Coastguard Worker const uint8_t* expected_public_key,
565*d289c2baSAndroid Build Coastguard Worker size_t expected_public_key_length,
566*d289c2baSAndroid Build Coastguard Worker AvbSlotVerifyData* slot_data,
567*d289c2baSAndroid Build Coastguard Worker AvbAlgorithmType* out_algorithm_type,
568*d289c2baSAndroid Build Coastguard Worker AvbCmdlineSubstList* out_additional_cmdline_subst,
569*d289c2baSAndroid Build Coastguard Worker bool use_ab_suffix) {
570*d289c2baSAndroid Build Coastguard Worker char full_partition_name[AVB_PART_NAME_MAX_SIZE];
571*d289c2baSAndroid Build Coastguard Worker AvbSlotVerifyResult ret;
572*d289c2baSAndroid Build Coastguard Worker AvbIOResult io_ret;
573*d289c2baSAndroid Build Coastguard Worker uint64_t vbmeta_offset;
574*d289c2baSAndroid Build Coastguard Worker size_t vbmeta_size;
575*d289c2baSAndroid Build Coastguard Worker uint8_t* vbmeta_buf = NULL;
576*d289c2baSAndroid Build Coastguard Worker size_t vbmeta_num_read;
577*d289c2baSAndroid Build Coastguard Worker AvbVBMetaVerifyResult vbmeta_ret;
578*d289c2baSAndroid Build Coastguard Worker const uint8_t* pk_data;
579*d289c2baSAndroid Build Coastguard Worker size_t pk_len;
580*d289c2baSAndroid Build Coastguard Worker AvbVBMetaImageHeader vbmeta_header;
581*d289c2baSAndroid Build Coastguard Worker uint64_t stored_rollback_index;
582*d289c2baSAndroid Build Coastguard Worker const AvbDescriptor** descriptors = NULL;
583*d289c2baSAndroid Build Coastguard Worker size_t num_descriptors;
584*d289c2baSAndroid Build Coastguard Worker size_t n;
585*d289c2baSAndroid Build Coastguard Worker bool is_main_vbmeta;
586*d289c2baSAndroid Build Coastguard Worker bool look_for_vbmeta_footer;
587*d289c2baSAndroid Build Coastguard Worker AvbVBMetaData* vbmeta_image_data = NULL;
588*d289c2baSAndroid Build Coastguard Worker
589*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_OK;
590*d289c2baSAndroid Build Coastguard Worker
591*d289c2baSAndroid Build Coastguard Worker avb_assert(slot_data != NULL);
592*d289c2baSAndroid Build Coastguard Worker
593*d289c2baSAndroid Build Coastguard Worker /* Since we allow top-level vbmeta in 'boot', use
594*d289c2baSAndroid Build Coastguard Worker * rollback_index_location to determine whether we're the main
595*d289c2baSAndroid Build Coastguard Worker * vbmeta struct.
596*d289c2baSAndroid Build Coastguard Worker */
597*d289c2baSAndroid Build Coastguard Worker is_main_vbmeta = false;
598*d289c2baSAndroid Build Coastguard Worker if (rollback_index_location == 0) {
599*d289c2baSAndroid Build Coastguard Worker if ((flags & AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION) == 0) {
600*d289c2baSAndroid Build Coastguard Worker is_main_vbmeta = true;
601*d289c2baSAndroid Build Coastguard Worker }
602*d289c2baSAndroid Build Coastguard Worker }
603*d289c2baSAndroid Build Coastguard Worker
604*d289c2baSAndroid Build Coastguard Worker /* Don't use footers for vbmeta partitions ('vbmeta' or
605*d289c2baSAndroid Build Coastguard Worker * 'vbmeta_<partition_name>').
606*d289c2baSAndroid Build Coastguard Worker */
607*d289c2baSAndroid Build Coastguard Worker look_for_vbmeta_footer = true;
608*d289c2baSAndroid Build Coastguard Worker if (avb_strncmp(partition_name, "vbmeta", avb_strlen("vbmeta")) == 0) {
609*d289c2baSAndroid Build Coastguard Worker look_for_vbmeta_footer = false;
610*d289c2baSAndroid Build Coastguard Worker }
611*d289c2baSAndroid Build Coastguard Worker
612*d289c2baSAndroid Build Coastguard Worker if (!avb_validate_utf8((const uint8_t*)partition_name, partition_name_len)) {
613*d289c2baSAndroid Build Coastguard Worker avb_error("Partition name is not valid UTF-8.\n");
614*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
615*d289c2baSAndroid Build Coastguard Worker goto out;
616*d289c2baSAndroid Build Coastguard Worker }
617*d289c2baSAndroid Build Coastguard Worker if (!use_ab_suffix) {
618*d289c2baSAndroid Build Coastguard Worker /*No ab_suffix, just copy the partition name as is.*/
619*d289c2baSAndroid Build Coastguard Worker if (partition_name_len >= AVB_PART_NAME_MAX_SIZE) {
620*d289c2baSAndroid Build Coastguard Worker avb_error("Partition name does not fit.\n");
621*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
622*d289c2baSAndroid Build Coastguard Worker goto out;
623*d289c2baSAndroid Build Coastguard Worker }
624*d289c2baSAndroid Build Coastguard Worker avb_memcpy(full_partition_name, partition_name, partition_name_len);
625*d289c2baSAndroid Build Coastguard Worker full_partition_name[partition_name_len] = '\0';
626*d289c2baSAndroid Build Coastguard Worker }else{
627*d289c2baSAndroid Build Coastguard Worker /* Construct full partition name e.g. system_a. */
628*d289c2baSAndroid Build Coastguard Worker if (!avb_str_concat(full_partition_name,
629*d289c2baSAndroid Build Coastguard Worker sizeof full_partition_name,
630*d289c2baSAndroid Build Coastguard Worker partition_name,
631*d289c2baSAndroid Build Coastguard Worker partition_name_len,
632*d289c2baSAndroid Build Coastguard Worker ab_suffix,
633*d289c2baSAndroid Build Coastguard Worker avb_strlen(ab_suffix))) {
634*d289c2baSAndroid Build Coastguard Worker avb_error("Partition name and suffix does not fit.\n");
635*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
636*d289c2baSAndroid Build Coastguard Worker goto out;
637*d289c2baSAndroid Build Coastguard Worker }
638*d289c2baSAndroid Build Coastguard Worker }
639*d289c2baSAndroid Build Coastguard Worker
640*d289c2baSAndroid Build Coastguard Worker /* If we're loading from the main vbmeta partition, the vbmeta struct is in
641*d289c2baSAndroid Build Coastguard Worker * the beginning. Otherwise we may have to locate it via a footer... if no
642*d289c2baSAndroid Build Coastguard Worker * footer is found, we look in the beginning to support e.g. vbmeta_<org>
643*d289c2baSAndroid Build Coastguard Worker * partitions holding data for e.g. super partitions (b/80195851 for
644*d289c2baSAndroid Build Coastguard Worker * rationale).
645*d289c2baSAndroid Build Coastguard Worker */
646*d289c2baSAndroid Build Coastguard Worker vbmeta_offset = 0;
647*d289c2baSAndroid Build Coastguard Worker vbmeta_size = VBMETA_MAX_SIZE;
648*d289c2baSAndroid Build Coastguard Worker if (look_for_vbmeta_footer) {
649*d289c2baSAndroid Build Coastguard Worker uint8_t footer_buf[AVB_FOOTER_SIZE];
650*d289c2baSAndroid Build Coastguard Worker size_t footer_num_read;
651*d289c2baSAndroid Build Coastguard Worker AvbFooter footer;
652*d289c2baSAndroid Build Coastguard Worker
653*d289c2baSAndroid Build Coastguard Worker io_ret = ops->read_from_partition(ops,
654*d289c2baSAndroid Build Coastguard Worker full_partition_name,
655*d289c2baSAndroid Build Coastguard Worker -AVB_FOOTER_SIZE,
656*d289c2baSAndroid Build Coastguard Worker AVB_FOOTER_SIZE,
657*d289c2baSAndroid Build Coastguard Worker footer_buf,
658*d289c2baSAndroid Build Coastguard Worker &footer_num_read);
659*d289c2baSAndroid Build Coastguard Worker if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
660*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
661*d289c2baSAndroid Build Coastguard Worker goto out;
662*d289c2baSAndroid Build Coastguard Worker } else if (io_ret != AVB_IO_RESULT_OK) {
663*d289c2baSAndroid Build Coastguard Worker avb_error(full_partition_name, ": Error loading footer.\n");
664*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
665*d289c2baSAndroid Build Coastguard Worker goto out;
666*d289c2baSAndroid Build Coastguard Worker }
667*d289c2baSAndroid Build Coastguard Worker avb_assert(footer_num_read == AVB_FOOTER_SIZE);
668*d289c2baSAndroid Build Coastguard Worker
669*d289c2baSAndroid Build Coastguard Worker if (!avb_footer_validate_and_byteswap((const AvbFooter*)footer_buf,
670*d289c2baSAndroid Build Coastguard Worker &footer)) {
671*d289c2baSAndroid Build Coastguard Worker avb_debug(full_partition_name, ": No footer detected.\n");
672*d289c2baSAndroid Build Coastguard Worker } else {
673*d289c2baSAndroid Build Coastguard Worker /* Basic footer sanity check since the data is untrusted. */
674*d289c2baSAndroid Build Coastguard Worker if (footer.vbmeta_size > VBMETA_MAX_SIZE) {
675*d289c2baSAndroid Build Coastguard Worker avb_error(full_partition_name, ": Invalid vbmeta size in footer.\n");
676*d289c2baSAndroid Build Coastguard Worker } else {
677*d289c2baSAndroid Build Coastguard Worker vbmeta_offset = footer.vbmeta_offset;
678*d289c2baSAndroid Build Coastguard Worker vbmeta_size = footer.vbmeta_size;
679*d289c2baSAndroid Build Coastguard Worker }
680*d289c2baSAndroid Build Coastguard Worker }
681*d289c2baSAndroid Build Coastguard Worker } else {
682*d289c2baSAndroid Build Coastguard Worker uint64_t partition_size = 0;
683*d289c2baSAndroid Build Coastguard Worker io_ret =
684*d289c2baSAndroid Build Coastguard Worker ops->get_size_of_partition(ops, full_partition_name, &partition_size);
685*d289c2baSAndroid Build Coastguard Worker if (io_ret == AVB_IO_RESULT_OK) {
686*d289c2baSAndroid Build Coastguard Worker if (partition_size < vbmeta_size && partition_size > 0) {
687*d289c2baSAndroid Build Coastguard Worker avb_debug(full_partition_name,
688*d289c2baSAndroid Build Coastguard Worker ": Using partition size as vbmeta size\n");
689*d289c2baSAndroid Build Coastguard Worker vbmeta_size = partition_size;
690*d289c2baSAndroid Build Coastguard Worker }
691*d289c2baSAndroid Build Coastguard Worker } else {
692*d289c2baSAndroid Build Coastguard Worker avb_debug(full_partition_name, ": Failed to get partition size\n");
693*d289c2baSAndroid Build Coastguard Worker // libavb might fall back to other partitions if current vbmeta partition
694*d289c2baSAndroid Build Coastguard Worker // isn't found. So AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION is recoverable,
695*d289c2baSAndroid Build Coastguard Worker // but other errors are not.
696*d289c2baSAndroid Build Coastguard Worker if (io_ret != AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION) {
697*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
698*d289c2baSAndroid Build Coastguard Worker goto out;
699*d289c2baSAndroid Build Coastguard Worker }
700*d289c2baSAndroid Build Coastguard Worker }
701*d289c2baSAndroid Build Coastguard Worker }
702*d289c2baSAndroid Build Coastguard Worker
703*d289c2baSAndroid Build Coastguard Worker /* Use result from previous I/O operation to check the existence of the
704*d289c2baSAndroid Build Coastguard Worker * partition before allocating the big chunk of memory on heap
705*d289c2baSAndroid Build Coastguard Worker * for vbmeta later. `io_ret` will be used later to decide whether
706*d289c2baSAndroid Build Coastguard Worker * to fallback on the `boot` partition.
707*d289c2baSAndroid Build Coastguard Worker */
708*d289c2baSAndroid Build Coastguard Worker if (io_ret != AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION) {
709*d289c2baSAndroid Build Coastguard Worker vbmeta_buf = avb_malloc(vbmeta_size);
710*d289c2baSAndroid Build Coastguard Worker if (vbmeta_buf == NULL) {
711*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
712*d289c2baSAndroid Build Coastguard Worker goto out;
713*d289c2baSAndroid Build Coastguard Worker }
714*d289c2baSAndroid Build Coastguard Worker
715*d289c2baSAndroid Build Coastguard Worker if (vbmeta_offset != 0) {
716*d289c2baSAndroid Build Coastguard Worker avb_debug("Loading vbmeta struct in footer from partition '",
717*d289c2baSAndroid Build Coastguard Worker full_partition_name,
718*d289c2baSAndroid Build Coastguard Worker "'.\n");
719*d289c2baSAndroid Build Coastguard Worker } else {
720*d289c2baSAndroid Build Coastguard Worker avb_debug("Loading vbmeta struct from partition '",
721*d289c2baSAndroid Build Coastguard Worker full_partition_name,
722*d289c2baSAndroid Build Coastguard Worker "'.\n");
723*d289c2baSAndroid Build Coastguard Worker }
724*d289c2baSAndroid Build Coastguard Worker
725*d289c2baSAndroid Build Coastguard Worker io_ret = ops->read_from_partition(ops,
726*d289c2baSAndroid Build Coastguard Worker full_partition_name,
727*d289c2baSAndroid Build Coastguard Worker vbmeta_offset,
728*d289c2baSAndroid Build Coastguard Worker vbmeta_size,
729*d289c2baSAndroid Build Coastguard Worker vbmeta_buf,
730*d289c2baSAndroid Build Coastguard Worker &vbmeta_num_read);
731*d289c2baSAndroid Build Coastguard Worker }
732*d289c2baSAndroid Build Coastguard Worker if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
733*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
734*d289c2baSAndroid Build Coastguard Worker goto out;
735*d289c2baSAndroid Build Coastguard Worker } else if (io_ret != AVB_IO_RESULT_OK) {
736*d289c2baSAndroid Build Coastguard Worker /* If we're looking for 'vbmeta' but there is no such partition,
737*d289c2baSAndroid Build Coastguard Worker * go try to get it from the boot partition instead.
738*d289c2baSAndroid Build Coastguard Worker */
739*d289c2baSAndroid Build Coastguard Worker if (is_main_vbmeta && io_ret == AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION &&
740*d289c2baSAndroid Build Coastguard Worker !look_for_vbmeta_footer) {
741*d289c2baSAndroid Build Coastguard Worker avb_debug(full_partition_name,
742*d289c2baSAndroid Build Coastguard Worker ": No such partition. Trying 'boot' instead.\n");
743*d289c2baSAndroid Build Coastguard Worker ret = load_and_verify_vbmeta(ops,
744*d289c2baSAndroid Build Coastguard Worker requested_partitions,
745*d289c2baSAndroid Build Coastguard Worker ab_suffix,
746*d289c2baSAndroid Build Coastguard Worker flags,
747*d289c2baSAndroid Build Coastguard Worker allow_verification_error,
748*d289c2baSAndroid Build Coastguard Worker 0 /* toplevel_vbmeta_flags */,
749*d289c2baSAndroid Build Coastguard Worker 0 /* rollback_index_location */,
750*d289c2baSAndroid Build Coastguard Worker "boot",
751*d289c2baSAndroid Build Coastguard Worker avb_strlen("boot"),
752*d289c2baSAndroid Build Coastguard Worker NULL /* expected_public_key */,
753*d289c2baSAndroid Build Coastguard Worker 0 /* expected_public_key_length */,
754*d289c2baSAndroid Build Coastguard Worker slot_data,
755*d289c2baSAndroid Build Coastguard Worker out_algorithm_type,
756*d289c2baSAndroid Build Coastguard Worker out_additional_cmdline_subst,
757*d289c2baSAndroid Build Coastguard Worker use_ab_suffix);
758*d289c2baSAndroid Build Coastguard Worker goto out;
759*d289c2baSAndroid Build Coastguard Worker } else {
760*d289c2baSAndroid Build Coastguard Worker avb_error(full_partition_name, ": Error loading vbmeta data.\n");
761*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
762*d289c2baSAndroid Build Coastguard Worker goto out;
763*d289c2baSAndroid Build Coastguard Worker }
764*d289c2baSAndroid Build Coastguard Worker }
765*d289c2baSAndroid Build Coastguard Worker avb_assert(vbmeta_num_read <= vbmeta_size);
766*d289c2baSAndroid Build Coastguard Worker
767*d289c2baSAndroid Build Coastguard Worker /* Check if the image is properly signed and get the public key used
768*d289c2baSAndroid Build Coastguard Worker * to sign the image.
769*d289c2baSAndroid Build Coastguard Worker */
770*d289c2baSAndroid Build Coastguard Worker vbmeta_ret =
771*d289c2baSAndroid Build Coastguard Worker avb_vbmeta_image_verify(vbmeta_buf, vbmeta_num_read, &pk_data, &pk_len);
772*d289c2baSAndroid Build Coastguard Worker switch (vbmeta_ret) {
773*d289c2baSAndroid Build Coastguard Worker case AVB_VBMETA_VERIFY_RESULT_OK:
774*d289c2baSAndroid Build Coastguard Worker avb_assert(pk_data != NULL && pk_len > 0);
775*d289c2baSAndroid Build Coastguard Worker break;
776*d289c2baSAndroid Build Coastguard Worker
777*d289c2baSAndroid Build Coastguard Worker case AVB_VBMETA_VERIFY_RESULT_OK_NOT_SIGNED:
778*d289c2baSAndroid Build Coastguard Worker case AVB_VBMETA_VERIFY_RESULT_HASH_MISMATCH:
779*d289c2baSAndroid Build Coastguard Worker case AVB_VBMETA_VERIFY_RESULT_SIGNATURE_MISMATCH:
780*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION;
781*d289c2baSAndroid Build Coastguard Worker avb_error(full_partition_name,
782*d289c2baSAndroid Build Coastguard Worker ": Error verifying vbmeta image: ",
783*d289c2baSAndroid Build Coastguard Worker avb_vbmeta_verify_result_to_string(vbmeta_ret),
784*d289c2baSAndroid Build Coastguard Worker "\n");
785*d289c2baSAndroid Build Coastguard Worker if (!allow_verification_error) {
786*d289c2baSAndroid Build Coastguard Worker goto out;
787*d289c2baSAndroid Build Coastguard Worker }
788*d289c2baSAndroid Build Coastguard Worker break;
789*d289c2baSAndroid Build Coastguard Worker
790*d289c2baSAndroid Build Coastguard Worker case AVB_VBMETA_VERIFY_RESULT_INVALID_VBMETA_HEADER:
791*d289c2baSAndroid Build Coastguard Worker /* No way to continue this case. */
792*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
793*d289c2baSAndroid Build Coastguard Worker avb_error(full_partition_name,
794*d289c2baSAndroid Build Coastguard Worker ": Error verifying vbmeta image: invalid vbmeta header\n");
795*d289c2baSAndroid Build Coastguard Worker goto out;
796*d289c2baSAndroid Build Coastguard Worker
797*d289c2baSAndroid Build Coastguard Worker case AVB_VBMETA_VERIFY_RESULT_UNSUPPORTED_VERSION:
798*d289c2baSAndroid Build Coastguard Worker /* No way to continue this case. */
799*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION;
800*d289c2baSAndroid Build Coastguard Worker avb_error(full_partition_name,
801*d289c2baSAndroid Build Coastguard Worker ": Error verifying vbmeta image: unsupported AVB version\n");
802*d289c2baSAndroid Build Coastguard Worker goto out;
803*d289c2baSAndroid Build Coastguard Worker }
804*d289c2baSAndroid Build Coastguard Worker
805*d289c2baSAndroid Build Coastguard Worker /* Byteswap the header. */
806*d289c2baSAndroid Build Coastguard Worker avb_vbmeta_image_header_to_host_byte_order((AvbVBMetaImageHeader*)vbmeta_buf,
807*d289c2baSAndroid Build Coastguard Worker &vbmeta_header);
808*d289c2baSAndroid Build Coastguard Worker
809*d289c2baSAndroid Build Coastguard Worker /* If we're the toplevel, assign flags so they'll be passed down. */
810*d289c2baSAndroid Build Coastguard Worker if (is_main_vbmeta) {
811*d289c2baSAndroid Build Coastguard Worker toplevel_vbmeta_flags = (AvbVBMetaImageFlags)vbmeta_header.flags;
812*d289c2baSAndroid Build Coastguard Worker } else {
813*d289c2baSAndroid Build Coastguard Worker if (vbmeta_header.flags != 0) {
814*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
815*d289c2baSAndroid Build Coastguard Worker avb_error(full_partition_name,
816*d289c2baSAndroid Build Coastguard Worker ": chained vbmeta image has non-zero flags\n");
817*d289c2baSAndroid Build Coastguard Worker goto out;
818*d289c2baSAndroid Build Coastguard Worker }
819*d289c2baSAndroid Build Coastguard Worker }
820*d289c2baSAndroid Build Coastguard Worker
821*d289c2baSAndroid Build Coastguard Worker uint32_t rollback_index_location_to_use = rollback_index_location;
822*d289c2baSAndroid Build Coastguard Worker if (is_main_vbmeta) {
823*d289c2baSAndroid Build Coastguard Worker rollback_index_location_to_use = vbmeta_header.rollback_index_location;
824*d289c2baSAndroid Build Coastguard Worker }
825*d289c2baSAndroid Build Coastguard Worker
826*d289c2baSAndroid Build Coastguard Worker /* Check if key used to make signature matches what is expected. */
827*d289c2baSAndroid Build Coastguard Worker if (pk_data != NULL) {
828*d289c2baSAndroid Build Coastguard Worker if (expected_public_key != NULL) {
829*d289c2baSAndroid Build Coastguard Worker avb_assert(!is_main_vbmeta);
830*d289c2baSAndroid Build Coastguard Worker if (expected_public_key_length != pk_len ||
831*d289c2baSAndroid Build Coastguard Worker avb_safe_memcmp(expected_public_key, pk_data, pk_len) != 0) {
832*d289c2baSAndroid Build Coastguard Worker avb_error(full_partition_name,
833*d289c2baSAndroid Build Coastguard Worker ": Public key used to sign data does not match key in chain "
834*d289c2baSAndroid Build Coastguard Worker "partition descriptor.\n");
835*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED;
836*d289c2baSAndroid Build Coastguard Worker if (!allow_verification_error) {
837*d289c2baSAndroid Build Coastguard Worker goto out;
838*d289c2baSAndroid Build Coastguard Worker }
839*d289c2baSAndroid Build Coastguard Worker }
840*d289c2baSAndroid Build Coastguard Worker } else {
841*d289c2baSAndroid Build Coastguard Worker bool key_is_trusted = false;
842*d289c2baSAndroid Build Coastguard Worker const uint8_t* pk_metadata = NULL;
843*d289c2baSAndroid Build Coastguard Worker size_t pk_metadata_len = 0;
844*d289c2baSAndroid Build Coastguard Worker
845*d289c2baSAndroid Build Coastguard Worker if (vbmeta_header.public_key_metadata_size > 0) {
846*d289c2baSAndroid Build Coastguard Worker pk_metadata = vbmeta_buf + sizeof(AvbVBMetaImageHeader) +
847*d289c2baSAndroid Build Coastguard Worker vbmeta_header.authentication_data_block_size +
848*d289c2baSAndroid Build Coastguard Worker vbmeta_header.public_key_metadata_offset;
849*d289c2baSAndroid Build Coastguard Worker pk_metadata_len = vbmeta_header.public_key_metadata_size;
850*d289c2baSAndroid Build Coastguard Worker }
851*d289c2baSAndroid Build Coastguard Worker
852*d289c2baSAndroid Build Coastguard Worker // If we're not using a vbmeta partition, need to use another AvbOps...
853*d289c2baSAndroid Build Coastguard Worker if (flags & AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION) {
854*d289c2baSAndroid Build Coastguard Worker io_ret = ops->validate_public_key_for_partition(
855*d289c2baSAndroid Build Coastguard Worker ops,
856*d289c2baSAndroid Build Coastguard Worker full_partition_name,
857*d289c2baSAndroid Build Coastguard Worker pk_data,
858*d289c2baSAndroid Build Coastguard Worker pk_len,
859*d289c2baSAndroid Build Coastguard Worker pk_metadata,
860*d289c2baSAndroid Build Coastguard Worker pk_metadata_len,
861*d289c2baSAndroid Build Coastguard Worker &key_is_trusted,
862*d289c2baSAndroid Build Coastguard Worker &rollback_index_location_to_use);
863*d289c2baSAndroid Build Coastguard Worker } else {
864*d289c2baSAndroid Build Coastguard Worker avb_assert(is_main_vbmeta);
865*d289c2baSAndroid Build Coastguard Worker io_ret = ops->validate_vbmeta_public_key(ops,
866*d289c2baSAndroid Build Coastguard Worker pk_data,
867*d289c2baSAndroid Build Coastguard Worker pk_len,
868*d289c2baSAndroid Build Coastguard Worker pk_metadata,
869*d289c2baSAndroid Build Coastguard Worker pk_metadata_len,
870*d289c2baSAndroid Build Coastguard Worker &key_is_trusted);
871*d289c2baSAndroid Build Coastguard Worker }
872*d289c2baSAndroid Build Coastguard Worker
873*d289c2baSAndroid Build Coastguard Worker if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
874*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
875*d289c2baSAndroid Build Coastguard Worker goto out;
876*d289c2baSAndroid Build Coastguard Worker } else if (io_ret != AVB_IO_RESULT_OK) {
877*d289c2baSAndroid Build Coastguard Worker avb_error(full_partition_name,
878*d289c2baSAndroid Build Coastguard Worker ": Error while checking public key used to sign data.\n");
879*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
880*d289c2baSAndroid Build Coastguard Worker goto out;
881*d289c2baSAndroid Build Coastguard Worker }
882*d289c2baSAndroid Build Coastguard Worker if (!key_is_trusted) {
883*d289c2baSAndroid Build Coastguard Worker avb_error(full_partition_name,
884*d289c2baSAndroid Build Coastguard Worker ": Public key used to sign data rejected.\n");
885*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED;
886*d289c2baSAndroid Build Coastguard Worker if (!allow_verification_error) {
887*d289c2baSAndroid Build Coastguard Worker goto out;
888*d289c2baSAndroid Build Coastguard Worker }
889*d289c2baSAndroid Build Coastguard Worker }
890*d289c2baSAndroid Build Coastguard Worker }
891*d289c2baSAndroid Build Coastguard Worker }
892*d289c2baSAndroid Build Coastguard Worker
893*d289c2baSAndroid Build Coastguard Worker /* Check rollback index. */
894*d289c2baSAndroid Build Coastguard Worker io_ret = ops->read_rollback_index(
895*d289c2baSAndroid Build Coastguard Worker ops, rollback_index_location_to_use, &stored_rollback_index);
896*d289c2baSAndroid Build Coastguard Worker if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
897*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
898*d289c2baSAndroid Build Coastguard Worker goto out;
899*d289c2baSAndroid Build Coastguard Worker } else if (io_ret != AVB_IO_RESULT_OK) {
900*d289c2baSAndroid Build Coastguard Worker avb_error(full_partition_name,
901*d289c2baSAndroid Build Coastguard Worker ": Error getting rollback index for location.\n");
902*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
903*d289c2baSAndroid Build Coastguard Worker goto out;
904*d289c2baSAndroid Build Coastguard Worker }
905*d289c2baSAndroid Build Coastguard Worker if (vbmeta_header.rollback_index < stored_rollback_index) {
906*d289c2baSAndroid Build Coastguard Worker avb_error(
907*d289c2baSAndroid Build Coastguard Worker full_partition_name,
908*d289c2baSAndroid Build Coastguard Worker ": Image rollback index is less than the stored rollback index.\n");
909*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX;
910*d289c2baSAndroid Build Coastguard Worker if (!allow_verification_error) {
911*d289c2baSAndroid Build Coastguard Worker goto out;
912*d289c2baSAndroid Build Coastguard Worker }
913*d289c2baSAndroid Build Coastguard Worker }
914*d289c2baSAndroid Build Coastguard Worker
915*d289c2baSAndroid Build Coastguard Worker /* Copy vbmeta to vbmeta_images before recursing. */
916*d289c2baSAndroid Build Coastguard Worker if (is_main_vbmeta) {
917*d289c2baSAndroid Build Coastguard Worker avb_assert(slot_data->num_vbmeta_images == 0);
918*d289c2baSAndroid Build Coastguard Worker } else {
919*d289c2baSAndroid Build Coastguard Worker if (!(flags & AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION)) {
920*d289c2baSAndroid Build Coastguard Worker avb_assert(slot_data->num_vbmeta_images > 0);
921*d289c2baSAndroid Build Coastguard Worker }
922*d289c2baSAndroid Build Coastguard Worker }
923*d289c2baSAndroid Build Coastguard Worker if (slot_data->num_vbmeta_images == MAX_NUMBER_OF_VBMETA_IMAGES) {
924*d289c2baSAndroid Build Coastguard Worker avb_error(full_partition_name, ": Too many vbmeta images.\n");
925*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
926*d289c2baSAndroid Build Coastguard Worker goto out;
927*d289c2baSAndroid Build Coastguard Worker }
928*d289c2baSAndroid Build Coastguard Worker vbmeta_image_data = &slot_data->vbmeta_images[slot_data->num_vbmeta_images++];
929*d289c2baSAndroid Build Coastguard Worker vbmeta_image_data->partition_name = avb_strdup(partition_name);
930*d289c2baSAndroid Build Coastguard Worker vbmeta_image_data->vbmeta_data = vbmeta_buf;
931*d289c2baSAndroid Build Coastguard Worker /* Note that |vbmeta_buf| is actually |vbmeta_num_read| bytes long
932*d289c2baSAndroid Build Coastguard Worker * and this includes data past the end of the image. Pass the
933*d289c2baSAndroid Build Coastguard Worker * actual size of the vbmeta image. Also, no need to use
934*d289c2baSAndroid Build Coastguard Worker * avb_safe_add() since the header has already been verified.
935*d289c2baSAndroid Build Coastguard Worker */
936*d289c2baSAndroid Build Coastguard Worker vbmeta_image_data->vbmeta_size =
937*d289c2baSAndroid Build Coastguard Worker sizeof(AvbVBMetaImageHeader) +
938*d289c2baSAndroid Build Coastguard Worker vbmeta_header.authentication_data_block_size +
939*d289c2baSAndroid Build Coastguard Worker vbmeta_header.auxiliary_data_block_size;
940*d289c2baSAndroid Build Coastguard Worker vbmeta_image_data->verify_result = vbmeta_ret;
941*d289c2baSAndroid Build Coastguard Worker
942*d289c2baSAndroid Build Coastguard Worker /* If verification has been disabled by setting a bit in the image,
943*d289c2baSAndroid Build Coastguard Worker * we're done... except that we need to load the entirety of the
944*d289c2baSAndroid Build Coastguard Worker * requested partitions.
945*d289c2baSAndroid Build Coastguard Worker */
946*d289c2baSAndroid Build Coastguard Worker if (vbmeta_header.flags & AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED) {
947*d289c2baSAndroid Build Coastguard Worker AvbSlotVerifyResult sub_ret;
948*d289c2baSAndroid Build Coastguard Worker avb_debug(full_partition_name, ": VERIFICATION_DISABLED bit is set.\n");
949*d289c2baSAndroid Build Coastguard Worker /* If load_requested_partitions() fail it is always a fatal
950*d289c2baSAndroid Build Coastguard Worker * failure (e.g. ERROR_INVALID_ARGUMENT, ERROR_OOM, etc.) rather
951*d289c2baSAndroid Build Coastguard Worker * than recoverable (e.g. one where result_should_continue()
952*d289c2baSAndroid Build Coastguard Worker * returns true) and we want to convey that error.
953*d289c2baSAndroid Build Coastguard Worker */
954*d289c2baSAndroid Build Coastguard Worker sub_ret = load_requested_partitions(
955*d289c2baSAndroid Build Coastguard Worker ops, requested_partitions, ab_suffix, slot_data);
956*d289c2baSAndroid Build Coastguard Worker if (sub_ret != AVB_SLOT_VERIFY_RESULT_OK) {
957*d289c2baSAndroid Build Coastguard Worker ret = sub_ret;
958*d289c2baSAndroid Build Coastguard Worker }
959*d289c2baSAndroid Build Coastguard Worker goto out;
960*d289c2baSAndroid Build Coastguard Worker }
961*d289c2baSAndroid Build Coastguard Worker
962*d289c2baSAndroid Build Coastguard Worker /* Now go through all descriptors and take the appropriate action:
963*d289c2baSAndroid Build Coastguard Worker *
964*d289c2baSAndroid Build Coastguard Worker * - hash descriptor: Load data from partition, calculate hash, and
965*d289c2baSAndroid Build Coastguard Worker * checks that it matches what's in the hash descriptor.
966*d289c2baSAndroid Build Coastguard Worker *
967*d289c2baSAndroid Build Coastguard Worker * - hashtree descriptor: Do nothing since verification happens
968*d289c2baSAndroid Build Coastguard Worker * on-the-fly from within the OS. (Unless the descriptor uses a
969*d289c2baSAndroid Build Coastguard Worker * persistent digest, in which case we need to find it).
970*d289c2baSAndroid Build Coastguard Worker *
971*d289c2baSAndroid Build Coastguard Worker * - chained partition descriptor: Load the footer, load the vbmeta
972*d289c2baSAndroid Build Coastguard Worker * image, verify vbmeta image (includes rollback checks, hash
973*d289c2baSAndroid Build Coastguard Worker * checks, bail on chained partitions).
974*d289c2baSAndroid Build Coastguard Worker */
975*d289c2baSAndroid Build Coastguard Worker descriptors =
976*d289c2baSAndroid Build Coastguard Worker avb_descriptor_get_all(vbmeta_buf, vbmeta_num_read, &num_descriptors);
977*d289c2baSAndroid Build Coastguard Worker for (n = 0; n < num_descriptors; n++) {
978*d289c2baSAndroid Build Coastguard Worker AvbDescriptor desc;
979*d289c2baSAndroid Build Coastguard Worker
980*d289c2baSAndroid Build Coastguard Worker if (!avb_descriptor_validate_and_byteswap(descriptors[n], &desc)) {
981*d289c2baSAndroid Build Coastguard Worker avb_error(full_partition_name, ": Descriptor is invalid.\n");
982*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
983*d289c2baSAndroid Build Coastguard Worker goto out;
984*d289c2baSAndroid Build Coastguard Worker }
985*d289c2baSAndroid Build Coastguard Worker
986*d289c2baSAndroid Build Coastguard Worker switch (desc.tag) {
987*d289c2baSAndroid Build Coastguard Worker case AVB_DESCRIPTOR_TAG_HASH: {
988*d289c2baSAndroid Build Coastguard Worker AvbSlotVerifyResult sub_ret;
989*d289c2baSAndroid Build Coastguard Worker sub_ret = load_and_verify_hash_partition(ops,
990*d289c2baSAndroid Build Coastguard Worker requested_partitions,
991*d289c2baSAndroid Build Coastguard Worker ab_suffix,
992*d289c2baSAndroid Build Coastguard Worker allow_verification_error,
993*d289c2baSAndroid Build Coastguard Worker descriptors[n],
994*d289c2baSAndroid Build Coastguard Worker slot_data);
995*d289c2baSAndroid Build Coastguard Worker if (sub_ret != AVB_SLOT_VERIFY_RESULT_OK) {
996*d289c2baSAndroid Build Coastguard Worker ret = sub_ret;
997*d289c2baSAndroid Build Coastguard Worker if (!allow_verification_error || !result_should_continue(ret)) {
998*d289c2baSAndroid Build Coastguard Worker goto out;
999*d289c2baSAndroid Build Coastguard Worker }
1000*d289c2baSAndroid Build Coastguard Worker }
1001*d289c2baSAndroid Build Coastguard Worker } break;
1002*d289c2baSAndroid Build Coastguard Worker
1003*d289c2baSAndroid Build Coastguard Worker case AVB_DESCRIPTOR_TAG_CHAIN_PARTITION: {
1004*d289c2baSAndroid Build Coastguard Worker AvbSlotVerifyResult sub_ret;
1005*d289c2baSAndroid Build Coastguard Worker AvbChainPartitionDescriptor chain_desc;
1006*d289c2baSAndroid Build Coastguard Worker const uint8_t* chain_partition_name;
1007*d289c2baSAndroid Build Coastguard Worker const uint8_t* chain_public_key;
1008*d289c2baSAndroid Build Coastguard Worker
1009*d289c2baSAndroid Build Coastguard Worker /* Only allow CHAIN_PARTITION descriptors in the main vbmeta image. */
1010*d289c2baSAndroid Build Coastguard Worker if (!is_main_vbmeta) {
1011*d289c2baSAndroid Build Coastguard Worker avb_error(full_partition_name,
1012*d289c2baSAndroid Build Coastguard Worker ": Encountered chain descriptor not in main image.\n");
1013*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
1014*d289c2baSAndroid Build Coastguard Worker goto out;
1015*d289c2baSAndroid Build Coastguard Worker }
1016*d289c2baSAndroid Build Coastguard Worker
1017*d289c2baSAndroid Build Coastguard Worker if (!avb_chain_partition_descriptor_validate_and_byteswap(
1018*d289c2baSAndroid Build Coastguard Worker (AvbChainPartitionDescriptor*)descriptors[n], &chain_desc)) {
1019*d289c2baSAndroid Build Coastguard Worker avb_error(full_partition_name,
1020*d289c2baSAndroid Build Coastguard Worker ": Chain partition descriptor is invalid.\n");
1021*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
1022*d289c2baSAndroid Build Coastguard Worker goto out;
1023*d289c2baSAndroid Build Coastguard Worker }
1024*d289c2baSAndroid Build Coastguard Worker
1025*d289c2baSAndroid Build Coastguard Worker if (chain_desc.rollback_index_location == 0) {
1026*d289c2baSAndroid Build Coastguard Worker avb_error(full_partition_name,
1027*d289c2baSAndroid Build Coastguard Worker ": Chain partition has invalid "
1028*d289c2baSAndroid Build Coastguard Worker "rollback_index_location field.\n");
1029*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
1030*d289c2baSAndroid Build Coastguard Worker goto out;
1031*d289c2baSAndroid Build Coastguard Worker }
1032*d289c2baSAndroid Build Coastguard Worker if ((chain_desc.flags & AVB_HASH_DESCRIPTOR_FLAGS_DO_NOT_USE_AB) != 0){
1033*d289c2baSAndroid Build Coastguard Worker use_ab_suffix = false;
1034*d289c2baSAndroid Build Coastguard Worker }else{
1035*d289c2baSAndroid Build Coastguard Worker use_ab_suffix = true;
1036*d289c2baSAndroid Build Coastguard Worker }
1037*d289c2baSAndroid Build Coastguard Worker chain_partition_name = ((const uint8_t*)descriptors[n]) +
1038*d289c2baSAndroid Build Coastguard Worker sizeof(AvbChainPartitionDescriptor);
1039*d289c2baSAndroid Build Coastguard Worker chain_public_key = chain_partition_name + chain_desc.partition_name_len;
1040*d289c2baSAndroid Build Coastguard Worker
1041*d289c2baSAndroid Build Coastguard Worker sub_ret =
1042*d289c2baSAndroid Build Coastguard Worker load_and_verify_vbmeta(ops,
1043*d289c2baSAndroid Build Coastguard Worker requested_partitions,
1044*d289c2baSAndroid Build Coastguard Worker ab_suffix,
1045*d289c2baSAndroid Build Coastguard Worker flags,
1046*d289c2baSAndroid Build Coastguard Worker allow_verification_error,
1047*d289c2baSAndroid Build Coastguard Worker toplevel_vbmeta_flags,
1048*d289c2baSAndroid Build Coastguard Worker chain_desc.rollback_index_location,
1049*d289c2baSAndroid Build Coastguard Worker (const char*)chain_partition_name,
1050*d289c2baSAndroid Build Coastguard Worker chain_desc.partition_name_len,
1051*d289c2baSAndroid Build Coastguard Worker chain_public_key,
1052*d289c2baSAndroid Build Coastguard Worker chain_desc.public_key_len,
1053*d289c2baSAndroid Build Coastguard Worker slot_data,
1054*d289c2baSAndroid Build Coastguard Worker NULL, /* out_algorithm_type */
1055*d289c2baSAndroid Build Coastguard Worker NULL, /* out_additional_cmdline_subst */
1056*d289c2baSAndroid Build Coastguard Worker use_ab_suffix);
1057*d289c2baSAndroid Build Coastguard Worker if (sub_ret != AVB_SLOT_VERIFY_RESULT_OK) {
1058*d289c2baSAndroid Build Coastguard Worker ret = sub_ret;
1059*d289c2baSAndroid Build Coastguard Worker if (!result_should_continue(ret)) {
1060*d289c2baSAndroid Build Coastguard Worker goto out;
1061*d289c2baSAndroid Build Coastguard Worker }
1062*d289c2baSAndroid Build Coastguard Worker }
1063*d289c2baSAndroid Build Coastguard Worker } break;
1064*d289c2baSAndroid Build Coastguard Worker
1065*d289c2baSAndroid Build Coastguard Worker case AVB_DESCRIPTOR_TAG_KERNEL_CMDLINE: {
1066*d289c2baSAndroid Build Coastguard Worker const uint8_t* kernel_cmdline;
1067*d289c2baSAndroid Build Coastguard Worker AvbKernelCmdlineDescriptor kernel_cmdline_desc;
1068*d289c2baSAndroid Build Coastguard Worker bool apply_cmdline;
1069*d289c2baSAndroid Build Coastguard Worker
1070*d289c2baSAndroid Build Coastguard Worker if (!avb_kernel_cmdline_descriptor_validate_and_byteswap(
1071*d289c2baSAndroid Build Coastguard Worker (AvbKernelCmdlineDescriptor*)descriptors[n],
1072*d289c2baSAndroid Build Coastguard Worker &kernel_cmdline_desc)) {
1073*d289c2baSAndroid Build Coastguard Worker avb_error(full_partition_name,
1074*d289c2baSAndroid Build Coastguard Worker ": Kernel cmdline descriptor is invalid.\n");
1075*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
1076*d289c2baSAndroid Build Coastguard Worker goto out;
1077*d289c2baSAndroid Build Coastguard Worker }
1078*d289c2baSAndroid Build Coastguard Worker
1079*d289c2baSAndroid Build Coastguard Worker kernel_cmdline = ((const uint8_t*)descriptors[n]) +
1080*d289c2baSAndroid Build Coastguard Worker sizeof(AvbKernelCmdlineDescriptor);
1081*d289c2baSAndroid Build Coastguard Worker
1082*d289c2baSAndroid Build Coastguard Worker if (!avb_validate_utf8(kernel_cmdline,
1083*d289c2baSAndroid Build Coastguard Worker kernel_cmdline_desc.kernel_cmdline_length)) {
1084*d289c2baSAndroid Build Coastguard Worker avb_error(full_partition_name,
1085*d289c2baSAndroid Build Coastguard Worker ": Kernel cmdline is not valid UTF-8.\n");
1086*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
1087*d289c2baSAndroid Build Coastguard Worker goto out;
1088*d289c2baSAndroid Build Coastguard Worker }
1089*d289c2baSAndroid Build Coastguard Worker
1090*d289c2baSAndroid Build Coastguard Worker /* Compare the flags for top-level VBMeta struct with flags in
1091*d289c2baSAndroid Build Coastguard Worker * the command-line descriptor so command-line snippets only
1092*d289c2baSAndroid Build Coastguard Worker * intended for a certain mode (dm-verity enabled/disabled)
1093*d289c2baSAndroid Build Coastguard Worker * are skipped if applicable.
1094*d289c2baSAndroid Build Coastguard Worker */
1095*d289c2baSAndroid Build Coastguard Worker apply_cmdline = true;
1096*d289c2baSAndroid Build Coastguard Worker if (toplevel_vbmeta_flags & AVB_VBMETA_IMAGE_FLAGS_HASHTREE_DISABLED) {
1097*d289c2baSAndroid Build Coastguard Worker if (kernel_cmdline_desc.flags &
1098*d289c2baSAndroid Build Coastguard Worker AVB_KERNEL_CMDLINE_FLAGS_USE_ONLY_IF_HASHTREE_NOT_DISABLED) {
1099*d289c2baSAndroid Build Coastguard Worker apply_cmdline = false;
1100*d289c2baSAndroid Build Coastguard Worker }
1101*d289c2baSAndroid Build Coastguard Worker } else {
1102*d289c2baSAndroid Build Coastguard Worker if (kernel_cmdline_desc.flags &
1103*d289c2baSAndroid Build Coastguard Worker AVB_KERNEL_CMDLINE_FLAGS_USE_ONLY_IF_HASHTREE_DISABLED) {
1104*d289c2baSAndroid Build Coastguard Worker apply_cmdline = false;
1105*d289c2baSAndroid Build Coastguard Worker }
1106*d289c2baSAndroid Build Coastguard Worker }
1107*d289c2baSAndroid Build Coastguard Worker
1108*d289c2baSAndroid Build Coastguard Worker if (apply_cmdline) {
1109*d289c2baSAndroid Build Coastguard Worker if (slot_data->cmdline == NULL) {
1110*d289c2baSAndroid Build Coastguard Worker slot_data->cmdline =
1111*d289c2baSAndroid Build Coastguard Worker avb_calloc(kernel_cmdline_desc.kernel_cmdline_length + 1);
1112*d289c2baSAndroid Build Coastguard Worker if (slot_data->cmdline == NULL) {
1113*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1114*d289c2baSAndroid Build Coastguard Worker goto out;
1115*d289c2baSAndroid Build Coastguard Worker }
1116*d289c2baSAndroid Build Coastguard Worker avb_memcpy(slot_data->cmdline,
1117*d289c2baSAndroid Build Coastguard Worker kernel_cmdline,
1118*d289c2baSAndroid Build Coastguard Worker kernel_cmdline_desc.kernel_cmdline_length);
1119*d289c2baSAndroid Build Coastguard Worker } else {
1120*d289c2baSAndroid Build Coastguard Worker /* new cmdline is: <existing_cmdline> + ' ' + <newcmdline> + '\0' */
1121*d289c2baSAndroid Build Coastguard Worker size_t orig_size = avb_strlen(slot_data->cmdline);
1122*d289c2baSAndroid Build Coastguard Worker size_t new_size =
1123*d289c2baSAndroid Build Coastguard Worker orig_size + 1 + kernel_cmdline_desc.kernel_cmdline_length + 1;
1124*d289c2baSAndroid Build Coastguard Worker char* new_cmdline = avb_calloc(new_size);
1125*d289c2baSAndroid Build Coastguard Worker if (new_cmdline == NULL) {
1126*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1127*d289c2baSAndroid Build Coastguard Worker goto out;
1128*d289c2baSAndroid Build Coastguard Worker }
1129*d289c2baSAndroid Build Coastguard Worker avb_memcpy(new_cmdline, slot_data->cmdline, orig_size);
1130*d289c2baSAndroid Build Coastguard Worker new_cmdline[orig_size] = ' ';
1131*d289c2baSAndroid Build Coastguard Worker avb_memcpy(new_cmdline + orig_size + 1,
1132*d289c2baSAndroid Build Coastguard Worker kernel_cmdline,
1133*d289c2baSAndroid Build Coastguard Worker kernel_cmdline_desc.kernel_cmdline_length);
1134*d289c2baSAndroid Build Coastguard Worker avb_free(slot_data->cmdline);
1135*d289c2baSAndroid Build Coastguard Worker slot_data->cmdline = new_cmdline;
1136*d289c2baSAndroid Build Coastguard Worker }
1137*d289c2baSAndroid Build Coastguard Worker }
1138*d289c2baSAndroid Build Coastguard Worker } break;
1139*d289c2baSAndroid Build Coastguard Worker
1140*d289c2baSAndroid Build Coastguard Worker case AVB_DESCRIPTOR_TAG_HASHTREE: {
1141*d289c2baSAndroid Build Coastguard Worker AvbHashtreeDescriptor hashtree_desc;
1142*d289c2baSAndroid Build Coastguard Worker
1143*d289c2baSAndroid Build Coastguard Worker if (!avb_hashtree_descriptor_validate_and_byteswap(
1144*d289c2baSAndroid Build Coastguard Worker (AvbHashtreeDescriptor*)descriptors[n], &hashtree_desc)) {
1145*d289c2baSAndroid Build Coastguard Worker avb_error(full_partition_name, ": Hashtree descriptor is invalid.\n");
1146*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
1147*d289c2baSAndroid Build Coastguard Worker goto out;
1148*d289c2baSAndroid Build Coastguard Worker }
1149*d289c2baSAndroid Build Coastguard Worker
1150*d289c2baSAndroid Build Coastguard Worker /* We only need to continue when there is no digest in the descriptor.
1151*d289c2baSAndroid Build Coastguard Worker * This is because the only processing here is to find the digest and
1152*d289c2baSAndroid Build Coastguard Worker * make it available on the kernel command line.
1153*d289c2baSAndroid Build Coastguard Worker */
1154*d289c2baSAndroid Build Coastguard Worker if (hashtree_desc.root_digest_len == 0) {
1155*d289c2baSAndroid Build Coastguard Worker char part_name[AVB_PART_NAME_MAX_SIZE];
1156*d289c2baSAndroid Build Coastguard Worker size_t digest_len = 0;
1157*d289c2baSAndroid Build Coastguard Worker uint8_t digest_buf[AVB_SHA512_DIGEST_SIZE];
1158*d289c2baSAndroid Build Coastguard Worker const uint8_t* desc_partition_name =
1159*d289c2baSAndroid Build Coastguard Worker ((const uint8_t*)descriptors[n]) + sizeof(AvbHashtreeDescriptor);
1160*d289c2baSAndroid Build Coastguard Worker
1161*d289c2baSAndroid Build Coastguard Worker if (!avb_validate_utf8(desc_partition_name,
1162*d289c2baSAndroid Build Coastguard Worker hashtree_desc.partition_name_len)) {
1163*d289c2baSAndroid Build Coastguard Worker avb_error("Partition name is not valid UTF-8.\n");
1164*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
1165*d289c2baSAndroid Build Coastguard Worker goto out;
1166*d289c2baSAndroid Build Coastguard Worker }
1167*d289c2baSAndroid Build Coastguard Worker
1168*d289c2baSAndroid Build Coastguard Worker /* No ab_suffix for partitions without a digest in the descriptor
1169*d289c2baSAndroid Build Coastguard Worker * because these partitions hold data unique to this device and are
1170*d289c2baSAndroid Build Coastguard Worker * not updated using an A/B scheme.
1171*d289c2baSAndroid Build Coastguard Worker */
1172*d289c2baSAndroid Build Coastguard Worker if ((hashtree_desc.flags &
1173*d289c2baSAndroid Build Coastguard Worker AVB_HASHTREE_DESCRIPTOR_FLAGS_DO_NOT_USE_AB) == 0 &&
1174*d289c2baSAndroid Build Coastguard Worker avb_strlen(ab_suffix) != 0) {
1175*d289c2baSAndroid Build Coastguard Worker avb_error("Cannot use A/B with a persistent root digest.\n");
1176*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
1177*d289c2baSAndroid Build Coastguard Worker goto out;
1178*d289c2baSAndroid Build Coastguard Worker }
1179*d289c2baSAndroid Build Coastguard Worker if (hashtree_desc.partition_name_len >= AVB_PART_NAME_MAX_SIZE) {
1180*d289c2baSAndroid Build Coastguard Worker avb_error("Partition name does not fit.\n");
1181*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
1182*d289c2baSAndroid Build Coastguard Worker goto out;
1183*d289c2baSAndroid Build Coastguard Worker }
1184*d289c2baSAndroid Build Coastguard Worker avb_memcpy(
1185*d289c2baSAndroid Build Coastguard Worker part_name, desc_partition_name, hashtree_desc.partition_name_len);
1186*d289c2baSAndroid Build Coastguard Worker part_name[hashtree_desc.partition_name_len] = '\0';
1187*d289c2baSAndroid Build Coastguard Worker
1188*d289c2baSAndroid Build Coastguard Worker /* Determine the expected digest size from the hash algorithm. */
1189*d289c2baSAndroid Build Coastguard Worker if (avb_strcmp((const char*)hashtree_desc.hash_algorithm, "sha1") ==
1190*d289c2baSAndroid Build Coastguard Worker 0) {
1191*d289c2baSAndroid Build Coastguard Worker digest_len = AVB_SHA1_DIGEST_SIZE;
1192*d289c2baSAndroid Build Coastguard Worker } else if (avb_strcmp((const char*)hashtree_desc.hash_algorithm,
1193*d289c2baSAndroid Build Coastguard Worker "sha256") == 0) {
1194*d289c2baSAndroid Build Coastguard Worker digest_len = AVB_SHA256_DIGEST_SIZE;
1195*d289c2baSAndroid Build Coastguard Worker } else if (avb_strcmp((const char*)hashtree_desc.hash_algorithm,
1196*d289c2baSAndroid Build Coastguard Worker "sha512") == 0) {
1197*d289c2baSAndroid Build Coastguard Worker digest_len = AVB_SHA512_DIGEST_SIZE;
1198*d289c2baSAndroid Build Coastguard Worker } else {
1199*d289c2baSAndroid Build Coastguard Worker avb_error(part_name, ": Unsupported hash algorithm.\n");
1200*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
1201*d289c2baSAndroid Build Coastguard Worker goto out;
1202*d289c2baSAndroid Build Coastguard Worker }
1203*d289c2baSAndroid Build Coastguard Worker
1204*d289c2baSAndroid Build Coastguard Worker ret = read_persistent_digest(ops,
1205*d289c2baSAndroid Build Coastguard Worker part_name,
1206*d289c2baSAndroid Build Coastguard Worker digest_len,
1207*d289c2baSAndroid Build Coastguard Worker NULL /* initial_digest */,
1208*d289c2baSAndroid Build Coastguard Worker digest_buf);
1209*d289c2baSAndroid Build Coastguard Worker if (ret != AVB_SLOT_VERIFY_RESULT_OK) {
1210*d289c2baSAndroid Build Coastguard Worker goto out;
1211*d289c2baSAndroid Build Coastguard Worker }
1212*d289c2baSAndroid Build Coastguard Worker
1213*d289c2baSAndroid Build Coastguard Worker if (out_additional_cmdline_subst) {
1214*d289c2baSAndroid Build Coastguard Worker ret =
1215*d289c2baSAndroid Build Coastguard Worker avb_add_root_digest_substitution(part_name,
1216*d289c2baSAndroid Build Coastguard Worker digest_buf,
1217*d289c2baSAndroid Build Coastguard Worker digest_len,
1218*d289c2baSAndroid Build Coastguard Worker out_additional_cmdline_subst);
1219*d289c2baSAndroid Build Coastguard Worker if (ret != AVB_SLOT_VERIFY_RESULT_OK) {
1220*d289c2baSAndroid Build Coastguard Worker goto out;
1221*d289c2baSAndroid Build Coastguard Worker }
1222*d289c2baSAndroid Build Coastguard Worker }
1223*d289c2baSAndroid Build Coastguard Worker }
1224*d289c2baSAndroid Build Coastguard Worker } break;
1225*d289c2baSAndroid Build Coastguard Worker
1226*d289c2baSAndroid Build Coastguard Worker case AVB_DESCRIPTOR_TAG_PROPERTY:
1227*d289c2baSAndroid Build Coastguard Worker /* Do nothing. */
1228*d289c2baSAndroid Build Coastguard Worker break;
1229*d289c2baSAndroid Build Coastguard Worker }
1230*d289c2baSAndroid Build Coastguard Worker }
1231*d289c2baSAndroid Build Coastguard Worker
1232*d289c2baSAndroid Build Coastguard Worker if (rollback_index_location_to_use >=
1233*d289c2baSAndroid Build Coastguard Worker AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS) {
1234*d289c2baSAndroid Build Coastguard Worker avb_error(full_partition_name, ": Invalid rollback_index_location.\n");
1235*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
1236*d289c2baSAndroid Build Coastguard Worker goto out;
1237*d289c2baSAndroid Build Coastguard Worker }
1238*d289c2baSAndroid Build Coastguard Worker
1239*d289c2baSAndroid Build Coastguard Worker slot_data->rollback_indexes[rollback_index_location_to_use] =
1240*d289c2baSAndroid Build Coastguard Worker vbmeta_header.rollback_index;
1241*d289c2baSAndroid Build Coastguard Worker
1242*d289c2baSAndroid Build Coastguard Worker if (out_algorithm_type != NULL) {
1243*d289c2baSAndroid Build Coastguard Worker *out_algorithm_type = (AvbAlgorithmType)vbmeta_header.algorithm_type;
1244*d289c2baSAndroid Build Coastguard Worker }
1245*d289c2baSAndroid Build Coastguard Worker
1246*d289c2baSAndroid Build Coastguard Worker out:
1247*d289c2baSAndroid Build Coastguard Worker /* If |vbmeta_image_data| isn't NULL it means that it adopted
1248*d289c2baSAndroid Build Coastguard Worker * |vbmeta_buf| so in that case don't free it here.
1249*d289c2baSAndroid Build Coastguard Worker */
1250*d289c2baSAndroid Build Coastguard Worker if (vbmeta_image_data == NULL) {
1251*d289c2baSAndroid Build Coastguard Worker if (vbmeta_buf != NULL) {
1252*d289c2baSAndroid Build Coastguard Worker avb_free(vbmeta_buf);
1253*d289c2baSAndroid Build Coastguard Worker }
1254*d289c2baSAndroid Build Coastguard Worker }
1255*d289c2baSAndroid Build Coastguard Worker if (descriptors != NULL) {
1256*d289c2baSAndroid Build Coastguard Worker avb_free(descriptors);
1257*d289c2baSAndroid Build Coastguard Worker }
1258*d289c2baSAndroid Build Coastguard Worker return ret;
1259*d289c2baSAndroid Build Coastguard Worker }
1260*d289c2baSAndroid Build Coastguard Worker
avb_manage_hashtree_error_mode(AvbOps * ops,AvbSlotVerifyFlags flags,AvbSlotVerifyData * data,AvbHashtreeErrorMode * out_hashtree_error_mode)1261*d289c2baSAndroid Build Coastguard Worker static AvbIOResult avb_manage_hashtree_error_mode(
1262*d289c2baSAndroid Build Coastguard Worker AvbOps* ops,
1263*d289c2baSAndroid Build Coastguard Worker AvbSlotVerifyFlags flags,
1264*d289c2baSAndroid Build Coastguard Worker AvbSlotVerifyData* data,
1265*d289c2baSAndroid Build Coastguard Worker AvbHashtreeErrorMode* out_hashtree_error_mode) {
1266*d289c2baSAndroid Build Coastguard Worker AvbHashtreeErrorMode ret = AVB_HASHTREE_ERROR_MODE_RESTART;
1267*d289c2baSAndroid Build Coastguard Worker AvbIOResult io_ret = AVB_IO_RESULT_OK;
1268*d289c2baSAndroid Build Coastguard Worker uint8_t vbmeta_digest_sha256[AVB_SHA256_DIGEST_SIZE];
1269*d289c2baSAndroid Build Coastguard Worker uint8_t stored_vbmeta_digest_sha256[AVB_SHA256_DIGEST_SIZE];
1270*d289c2baSAndroid Build Coastguard Worker size_t num_bytes_read;
1271*d289c2baSAndroid Build Coastguard Worker
1272*d289c2baSAndroid Build Coastguard Worker avb_assert(out_hashtree_error_mode != NULL);
1273*d289c2baSAndroid Build Coastguard Worker avb_assert(ops->read_persistent_value != NULL);
1274*d289c2baSAndroid Build Coastguard Worker avb_assert(ops->write_persistent_value != NULL);
1275*d289c2baSAndroid Build Coastguard Worker
1276*d289c2baSAndroid Build Coastguard Worker // If we're rebooting because of dm-verity corruption, make a note of
1277*d289c2baSAndroid Build Coastguard Worker // the vbmeta hash so we can stay in 'eio' mode until things change.
1278*d289c2baSAndroid Build Coastguard Worker if (flags & AVB_SLOT_VERIFY_FLAGS_RESTART_CAUSED_BY_HASHTREE_CORRUPTION) {
1279*d289c2baSAndroid Build Coastguard Worker avb_debug(
1280*d289c2baSAndroid Build Coastguard Worker "Rebooting because of dm-verity corruption - "
1281*d289c2baSAndroid Build Coastguard Worker "recording OS instance and using 'eio' mode.\n");
1282*d289c2baSAndroid Build Coastguard Worker avb_slot_verify_data_calculate_vbmeta_digest(
1283*d289c2baSAndroid Build Coastguard Worker data, AVB_DIGEST_TYPE_SHA256, vbmeta_digest_sha256);
1284*d289c2baSAndroid Build Coastguard Worker io_ret = ops->write_persistent_value(ops,
1285*d289c2baSAndroid Build Coastguard Worker AVB_NPV_MANAGED_VERITY_MODE,
1286*d289c2baSAndroid Build Coastguard Worker AVB_SHA256_DIGEST_SIZE,
1287*d289c2baSAndroid Build Coastguard Worker vbmeta_digest_sha256);
1288*d289c2baSAndroid Build Coastguard Worker if (io_ret != AVB_IO_RESULT_OK) {
1289*d289c2baSAndroid Build Coastguard Worker avb_error("Error writing to " AVB_NPV_MANAGED_VERITY_MODE ".\n");
1290*d289c2baSAndroid Build Coastguard Worker goto out;
1291*d289c2baSAndroid Build Coastguard Worker }
1292*d289c2baSAndroid Build Coastguard Worker ret = AVB_HASHTREE_ERROR_MODE_EIO;
1293*d289c2baSAndroid Build Coastguard Worker io_ret = AVB_IO_RESULT_OK;
1294*d289c2baSAndroid Build Coastguard Worker goto out;
1295*d289c2baSAndroid Build Coastguard Worker }
1296*d289c2baSAndroid Build Coastguard Worker
1297*d289c2baSAndroid Build Coastguard Worker // See if we're in 'eio' mode.
1298*d289c2baSAndroid Build Coastguard Worker io_ret = ops->read_persistent_value(ops,
1299*d289c2baSAndroid Build Coastguard Worker AVB_NPV_MANAGED_VERITY_MODE,
1300*d289c2baSAndroid Build Coastguard Worker AVB_SHA256_DIGEST_SIZE,
1301*d289c2baSAndroid Build Coastguard Worker stored_vbmeta_digest_sha256,
1302*d289c2baSAndroid Build Coastguard Worker &num_bytes_read);
1303*d289c2baSAndroid Build Coastguard Worker if (io_ret == AVB_IO_RESULT_ERROR_NO_SUCH_VALUE ||
1304*d289c2baSAndroid Build Coastguard Worker (io_ret == AVB_IO_RESULT_OK && num_bytes_read == 0)) {
1305*d289c2baSAndroid Build Coastguard Worker // This is the usual case ('eio' mode not set).
1306*d289c2baSAndroid Build Coastguard Worker avb_debug("No dm-verity corruption - using in 'restart' mode.\n");
1307*d289c2baSAndroid Build Coastguard Worker ret = AVB_HASHTREE_ERROR_MODE_RESTART;
1308*d289c2baSAndroid Build Coastguard Worker io_ret = AVB_IO_RESULT_OK;
1309*d289c2baSAndroid Build Coastguard Worker goto out;
1310*d289c2baSAndroid Build Coastguard Worker } else if (io_ret != AVB_IO_RESULT_OK) {
1311*d289c2baSAndroid Build Coastguard Worker avb_error("Error reading from " AVB_NPV_MANAGED_VERITY_MODE ".\n");
1312*d289c2baSAndroid Build Coastguard Worker goto out;
1313*d289c2baSAndroid Build Coastguard Worker }
1314*d289c2baSAndroid Build Coastguard Worker if (num_bytes_read != AVB_SHA256_DIGEST_SIZE) {
1315*d289c2baSAndroid Build Coastguard Worker avb_error(
1316*d289c2baSAndroid Build Coastguard Worker "Unexpected number of bytes read from " AVB_NPV_MANAGED_VERITY_MODE
1317*d289c2baSAndroid Build Coastguard Worker ".\n");
1318*d289c2baSAndroid Build Coastguard Worker io_ret = AVB_IO_RESULT_ERROR_IO;
1319*d289c2baSAndroid Build Coastguard Worker goto out;
1320*d289c2baSAndroid Build Coastguard Worker }
1321*d289c2baSAndroid Build Coastguard Worker
1322*d289c2baSAndroid Build Coastguard Worker // OK, so we're currently in 'eio' mode and the vbmeta digest of the OS
1323*d289c2baSAndroid Build Coastguard Worker // that caused this is in |stored_vbmeta_digest_sha256| ... now see if
1324*d289c2baSAndroid Build Coastguard Worker // the OS we're dealing with now is the same.
1325*d289c2baSAndroid Build Coastguard Worker avb_slot_verify_data_calculate_vbmeta_digest(
1326*d289c2baSAndroid Build Coastguard Worker data, AVB_DIGEST_TYPE_SHA256, vbmeta_digest_sha256);
1327*d289c2baSAndroid Build Coastguard Worker if (avb_memcmp(vbmeta_digest_sha256,
1328*d289c2baSAndroid Build Coastguard Worker stored_vbmeta_digest_sha256,
1329*d289c2baSAndroid Build Coastguard Worker AVB_SHA256_DIGEST_SIZE) == 0) {
1330*d289c2baSAndroid Build Coastguard Worker // It's the same so we're still in 'eio' mode.
1331*d289c2baSAndroid Build Coastguard Worker avb_debug("Same OS instance detected - staying in 'eio' mode.\n");
1332*d289c2baSAndroid Build Coastguard Worker ret = AVB_HASHTREE_ERROR_MODE_EIO;
1333*d289c2baSAndroid Build Coastguard Worker io_ret = AVB_IO_RESULT_OK;
1334*d289c2baSAndroid Build Coastguard Worker } else {
1335*d289c2baSAndroid Build Coastguard Worker // It did change!
1336*d289c2baSAndroid Build Coastguard Worker avb_debug(
1337*d289c2baSAndroid Build Coastguard Worker "New OS instance detected - changing from 'eio' to 'restart' mode.\n");
1338*d289c2baSAndroid Build Coastguard Worker io_ret =
1339*d289c2baSAndroid Build Coastguard Worker ops->write_persistent_value(ops,
1340*d289c2baSAndroid Build Coastguard Worker AVB_NPV_MANAGED_VERITY_MODE,
1341*d289c2baSAndroid Build Coastguard Worker 0, // This clears the persistent property.
1342*d289c2baSAndroid Build Coastguard Worker vbmeta_digest_sha256);
1343*d289c2baSAndroid Build Coastguard Worker if (io_ret != AVB_IO_RESULT_OK) {
1344*d289c2baSAndroid Build Coastguard Worker avb_error("Error clearing " AVB_NPV_MANAGED_VERITY_MODE ".\n");
1345*d289c2baSAndroid Build Coastguard Worker goto out;
1346*d289c2baSAndroid Build Coastguard Worker }
1347*d289c2baSAndroid Build Coastguard Worker ret = AVB_HASHTREE_ERROR_MODE_RESTART;
1348*d289c2baSAndroid Build Coastguard Worker io_ret = AVB_IO_RESULT_OK;
1349*d289c2baSAndroid Build Coastguard Worker }
1350*d289c2baSAndroid Build Coastguard Worker
1351*d289c2baSAndroid Build Coastguard Worker out:
1352*d289c2baSAndroid Build Coastguard Worker *out_hashtree_error_mode = ret;
1353*d289c2baSAndroid Build Coastguard Worker return io_ret;
1354*d289c2baSAndroid Build Coastguard Worker }
1355*d289c2baSAndroid Build Coastguard Worker
has_system_partition(AvbOps * ops,const char * ab_suffix)1356*d289c2baSAndroid Build Coastguard Worker static bool has_system_partition(AvbOps* ops, const char* ab_suffix) {
1357*d289c2baSAndroid Build Coastguard Worker char part_name[AVB_PART_NAME_MAX_SIZE];
1358*d289c2baSAndroid Build Coastguard Worker const char* system_part_name = "system";
1359*d289c2baSAndroid Build Coastguard Worker char guid_buf[37];
1360*d289c2baSAndroid Build Coastguard Worker AvbIOResult io_ret;
1361*d289c2baSAndroid Build Coastguard Worker
1362*d289c2baSAndroid Build Coastguard Worker if (!avb_str_concat(part_name,
1363*d289c2baSAndroid Build Coastguard Worker sizeof part_name,
1364*d289c2baSAndroid Build Coastguard Worker system_part_name,
1365*d289c2baSAndroid Build Coastguard Worker avb_strlen(system_part_name),
1366*d289c2baSAndroid Build Coastguard Worker ab_suffix,
1367*d289c2baSAndroid Build Coastguard Worker avb_strlen(ab_suffix))) {
1368*d289c2baSAndroid Build Coastguard Worker avb_error("System partition name and suffix does not fit.\n");
1369*d289c2baSAndroid Build Coastguard Worker return false;
1370*d289c2baSAndroid Build Coastguard Worker }
1371*d289c2baSAndroid Build Coastguard Worker
1372*d289c2baSAndroid Build Coastguard Worker io_ret = ops->get_unique_guid_for_partition(
1373*d289c2baSAndroid Build Coastguard Worker ops, part_name, guid_buf, sizeof guid_buf);
1374*d289c2baSAndroid Build Coastguard Worker if (io_ret == AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION) {
1375*d289c2baSAndroid Build Coastguard Worker avb_debug("No system partition.\n");
1376*d289c2baSAndroid Build Coastguard Worker return false;
1377*d289c2baSAndroid Build Coastguard Worker } else if (io_ret != AVB_IO_RESULT_OK) {
1378*d289c2baSAndroid Build Coastguard Worker avb_error("Error getting unique GUID for system partition.\n");
1379*d289c2baSAndroid Build Coastguard Worker return false;
1380*d289c2baSAndroid Build Coastguard Worker }
1381*d289c2baSAndroid Build Coastguard Worker
1382*d289c2baSAndroid Build Coastguard Worker return true;
1383*d289c2baSAndroid Build Coastguard Worker }
1384*d289c2baSAndroid Build Coastguard Worker
avb_slot_verify(AvbOps * ops,const char * const * requested_partitions,const char * ab_suffix,AvbSlotVerifyFlags flags,AvbHashtreeErrorMode hashtree_error_mode,AvbSlotVerifyData ** out_data)1385*d289c2baSAndroid Build Coastguard Worker AvbSlotVerifyResult avb_slot_verify(AvbOps* ops,
1386*d289c2baSAndroid Build Coastguard Worker const char* const* requested_partitions,
1387*d289c2baSAndroid Build Coastguard Worker const char* ab_suffix,
1388*d289c2baSAndroid Build Coastguard Worker AvbSlotVerifyFlags flags,
1389*d289c2baSAndroid Build Coastguard Worker AvbHashtreeErrorMode hashtree_error_mode,
1390*d289c2baSAndroid Build Coastguard Worker AvbSlotVerifyData** out_data) {
1391*d289c2baSAndroid Build Coastguard Worker AvbSlotVerifyResult ret;
1392*d289c2baSAndroid Build Coastguard Worker AvbSlotVerifyData* slot_data = NULL;
1393*d289c2baSAndroid Build Coastguard Worker AvbAlgorithmType algorithm_type = AVB_ALGORITHM_TYPE_NONE;
1394*d289c2baSAndroid Build Coastguard Worker bool using_boot_for_vbmeta = false;
1395*d289c2baSAndroid Build Coastguard Worker AvbVBMetaImageHeader toplevel_vbmeta;
1396*d289c2baSAndroid Build Coastguard Worker bool allow_verification_error =
1397*d289c2baSAndroid Build Coastguard Worker (flags & AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR);
1398*d289c2baSAndroid Build Coastguard Worker AvbCmdlineSubstList* additional_cmdline_subst = NULL;
1399*d289c2baSAndroid Build Coastguard Worker
1400*d289c2baSAndroid Build Coastguard Worker /* Fail early if we're missing the AvbOps needed for slot verification. */
1401*d289c2baSAndroid Build Coastguard Worker avb_assert(ops->read_is_device_unlocked != NULL);
1402*d289c2baSAndroid Build Coastguard Worker avb_assert(ops->read_from_partition != NULL);
1403*d289c2baSAndroid Build Coastguard Worker avb_assert(ops->get_size_of_partition != NULL);
1404*d289c2baSAndroid Build Coastguard Worker avb_assert(ops->read_rollback_index != NULL);
1405*d289c2baSAndroid Build Coastguard Worker avb_assert(ops->get_unique_guid_for_partition != NULL);
1406*d289c2baSAndroid Build Coastguard Worker
1407*d289c2baSAndroid Build Coastguard Worker if (out_data != NULL) {
1408*d289c2baSAndroid Build Coastguard Worker *out_data = NULL;
1409*d289c2baSAndroid Build Coastguard Worker }
1410*d289c2baSAndroid Build Coastguard Worker
1411*d289c2baSAndroid Build Coastguard Worker /* Allowing dm-verity errors defeats the purpose of verified boot so
1412*d289c2baSAndroid Build Coastguard Worker * only allow this if set up to allow verification errors
1413*d289c2baSAndroid Build Coastguard Worker * (e.g. typically only UNLOCKED mode).
1414*d289c2baSAndroid Build Coastguard Worker */
1415*d289c2baSAndroid Build Coastguard Worker if (hashtree_error_mode == AVB_HASHTREE_ERROR_MODE_LOGGING &&
1416*d289c2baSAndroid Build Coastguard Worker !allow_verification_error) {
1417*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT;
1418*d289c2baSAndroid Build Coastguard Worker goto fail;
1419*d289c2baSAndroid Build Coastguard Worker }
1420*d289c2baSAndroid Build Coastguard Worker
1421*d289c2baSAndroid Build Coastguard Worker /* Make sure passed-in AvbOps support persistent values if
1422*d289c2baSAndroid Build Coastguard Worker * asking for libavb to manage verity state.
1423*d289c2baSAndroid Build Coastguard Worker */
1424*d289c2baSAndroid Build Coastguard Worker if (hashtree_error_mode == AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO) {
1425*d289c2baSAndroid Build Coastguard Worker if (ops->read_persistent_value == NULL ||
1426*d289c2baSAndroid Build Coastguard Worker ops->write_persistent_value == NULL) {
1427*d289c2baSAndroid Build Coastguard Worker avb_error(
1428*d289c2baSAndroid Build Coastguard Worker "Persistent values required for "
1429*d289c2baSAndroid Build Coastguard Worker "AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO "
1430*d289c2baSAndroid Build Coastguard Worker "but are not implemented in given AvbOps.\n");
1431*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT;
1432*d289c2baSAndroid Build Coastguard Worker goto fail;
1433*d289c2baSAndroid Build Coastguard Worker }
1434*d289c2baSAndroid Build Coastguard Worker }
1435*d289c2baSAndroid Build Coastguard Worker
1436*d289c2baSAndroid Build Coastguard Worker /* Make sure passed-in AvbOps support verifying public keys and getting
1437*d289c2baSAndroid Build Coastguard Worker * rollback index location if not using a vbmeta partition.
1438*d289c2baSAndroid Build Coastguard Worker */
1439*d289c2baSAndroid Build Coastguard Worker if (flags & AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION) {
1440*d289c2baSAndroid Build Coastguard Worker if (ops->validate_public_key_for_partition == NULL) {
1441*d289c2baSAndroid Build Coastguard Worker avb_error(
1442*d289c2baSAndroid Build Coastguard Worker "AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION was passed but the "
1443*d289c2baSAndroid Build Coastguard Worker "validate_public_key_for_partition() operation isn't implemented.\n");
1444*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT;
1445*d289c2baSAndroid Build Coastguard Worker goto fail;
1446*d289c2baSAndroid Build Coastguard Worker }
1447*d289c2baSAndroid Build Coastguard Worker } else {
1448*d289c2baSAndroid Build Coastguard Worker avb_assert(ops->validate_vbmeta_public_key != NULL);
1449*d289c2baSAndroid Build Coastguard Worker }
1450*d289c2baSAndroid Build Coastguard Worker
1451*d289c2baSAndroid Build Coastguard Worker slot_data = avb_calloc(sizeof(AvbSlotVerifyData));
1452*d289c2baSAndroid Build Coastguard Worker if (slot_data == NULL) {
1453*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1454*d289c2baSAndroid Build Coastguard Worker goto fail;
1455*d289c2baSAndroid Build Coastguard Worker }
1456*d289c2baSAndroid Build Coastguard Worker slot_data->vbmeta_images =
1457*d289c2baSAndroid Build Coastguard Worker avb_calloc(sizeof(AvbVBMetaData) * MAX_NUMBER_OF_VBMETA_IMAGES);
1458*d289c2baSAndroid Build Coastguard Worker if (slot_data->vbmeta_images == NULL) {
1459*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1460*d289c2baSAndroid Build Coastguard Worker goto fail;
1461*d289c2baSAndroid Build Coastguard Worker }
1462*d289c2baSAndroid Build Coastguard Worker slot_data->loaded_partitions =
1463*d289c2baSAndroid Build Coastguard Worker avb_calloc(sizeof(AvbPartitionData) * MAX_NUMBER_OF_LOADED_PARTITIONS);
1464*d289c2baSAndroid Build Coastguard Worker if (slot_data->loaded_partitions == NULL) {
1465*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1466*d289c2baSAndroid Build Coastguard Worker goto fail;
1467*d289c2baSAndroid Build Coastguard Worker }
1468*d289c2baSAndroid Build Coastguard Worker
1469*d289c2baSAndroid Build Coastguard Worker additional_cmdline_subst = avb_new_cmdline_subst_list();
1470*d289c2baSAndroid Build Coastguard Worker if (additional_cmdline_subst == NULL) {
1471*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1472*d289c2baSAndroid Build Coastguard Worker goto fail;
1473*d289c2baSAndroid Build Coastguard Worker }
1474*d289c2baSAndroid Build Coastguard Worker
1475*d289c2baSAndroid Build Coastguard Worker if (flags & AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION) {
1476*d289c2baSAndroid Build Coastguard Worker if (requested_partitions == NULL || requested_partitions[0] == NULL) {
1477*d289c2baSAndroid Build Coastguard Worker avb_fatal(
1478*d289c2baSAndroid Build Coastguard Worker "Requested partitions cannot be empty when using "
1479*d289c2baSAndroid Build Coastguard Worker "AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION");
1480*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT;
1481*d289c2baSAndroid Build Coastguard Worker goto fail;
1482*d289c2baSAndroid Build Coastguard Worker }
1483*d289c2baSAndroid Build Coastguard Worker
1484*d289c2baSAndroid Build Coastguard Worker /* No vbmeta partition, go through each of the requested partitions... */
1485*d289c2baSAndroid Build Coastguard Worker for (size_t n = 0; requested_partitions[n] != NULL; n++) {
1486*d289c2baSAndroid Build Coastguard Worker ret = load_and_verify_vbmeta(ops,
1487*d289c2baSAndroid Build Coastguard Worker requested_partitions,
1488*d289c2baSAndroid Build Coastguard Worker ab_suffix,
1489*d289c2baSAndroid Build Coastguard Worker flags,
1490*d289c2baSAndroid Build Coastguard Worker allow_verification_error,
1491*d289c2baSAndroid Build Coastguard Worker 0 /* toplevel_vbmeta_flags */,
1492*d289c2baSAndroid Build Coastguard Worker 0 /* rollback_index_location */,
1493*d289c2baSAndroid Build Coastguard Worker requested_partitions[n],
1494*d289c2baSAndroid Build Coastguard Worker avb_strlen(requested_partitions[n]),
1495*d289c2baSAndroid Build Coastguard Worker NULL /* expected_public_key */,
1496*d289c2baSAndroid Build Coastguard Worker 0 /* expected_public_key_length */,
1497*d289c2baSAndroid Build Coastguard Worker slot_data,
1498*d289c2baSAndroid Build Coastguard Worker &algorithm_type,
1499*d289c2baSAndroid Build Coastguard Worker additional_cmdline_subst,
1500*d289c2baSAndroid Build Coastguard Worker true /*use_ab_suffix*/);
1501*d289c2baSAndroid Build Coastguard Worker if (!allow_verification_error && ret != AVB_SLOT_VERIFY_RESULT_OK) {
1502*d289c2baSAndroid Build Coastguard Worker goto fail;
1503*d289c2baSAndroid Build Coastguard Worker }
1504*d289c2baSAndroid Build Coastguard Worker }
1505*d289c2baSAndroid Build Coastguard Worker
1506*d289c2baSAndroid Build Coastguard Worker } else {
1507*d289c2baSAndroid Build Coastguard Worker /* Usual path, load "vbmeta"... */
1508*d289c2baSAndroid Build Coastguard Worker ret = load_and_verify_vbmeta(ops,
1509*d289c2baSAndroid Build Coastguard Worker requested_partitions,
1510*d289c2baSAndroid Build Coastguard Worker ab_suffix,
1511*d289c2baSAndroid Build Coastguard Worker flags,
1512*d289c2baSAndroid Build Coastguard Worker allow_verification_error,
1513*d289c2baSAndroid Build Coastguard Worker 0 /* toplevel_vbmeta_flags */,
1514*d289c2baSAndroid Build Coastguard Worker 0 /* rollback_index_location */,
1515*d289c2baSAndroid Build Coastguard Worker "vbmeta",
1516*d289c2baSAndroid Build Coastguard Worker avb_strlen("vbmeta"),
1517*d289c2baSAndroid Build Coastguard Worker NULL /* expected_public_key */,
1518*d289c2baSAndroid Build Coastguard Worker 0 /* expected_public_key_length */,
1519*d289c2baSAndroid Build Coastguard Worker slot_data,
1520*d289c2baSAndroid Build Coastguard Worker &algorithm_type,
1521*d289c2baSAndroid Build Coastguard Worker additional_cmdline_subst,
1522*d289c2baSAndroid Build Coastguard Worker true /*use_ab_suffix*/);
1523*d289c2baSAndroid Build Coastguard Worker if (!allow_verification_error && ret != AVB_SLOT_VERIFY_RESULT_OK) {
1524*d289c2baSAndroid Build Coastguard Worker goto fail;
1525*d289c2baSAndroid Build Coastguard Worker }
1526*d289c2baSAndroid Build Coastguard Worker }
1527*d289c2baSAndroid Build Coastguard Worker
1528*d289c2baSAndroid Build Coastguard Worker if (!result_should_continue(ret)) {
1529*d289c2baSAndroid Build Coastguard Worker goto fail;
1530*d289c2baSAndroid Build Coastguard Worker }
1531*d289c2baSAndroid Build Coastguard Worker
1532*d289c2baSAndroid Build Coastguard Worker /* If things check out, mangle the kernel command-line as needed. */
1533*d289c2baSAndroid Build Coastguard Worker if (!(flags & AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION)) {
1534*d289c2baSAndroid Build Coastguard Worker if (avb_strcmp(slot_data->vbmeta_images[0].partition_name, "vbmeta") != 0) {
1535*d289c2baSAndroid Build Coastguard Worker avb_assert(
1536*d289c2baSAndroid Build Coastguard Worker avb_strcmp(slot_data->vbmeta_images[0].partition_name, "boot") == 0);
1537*d289c2baSAndroid Build Coastguard Worker using_boot_for_vbmeta = true;
1538*d289c2baSAndroid Build Coastguard Worker }
1539*d289c2baSAndroid Build Coastguard Worker }
1540*d289c2baSAndroid Build Coastguard Worker
1541*d289c2baSAndroid Build Coastguard Worker /* Byteswap top-level vbmeta header since we'll need it below. */
1542*d289c2baSAndroid Build Coastguard Worker avb_vbmeta_image_header_to_host_byte_order(
1543*d289c2baSAndroid Build Coastguard Worker (const AvbVBMetaImageHeader*)slot_data->vbmeta_images[0].vbmeta_data,
1544*d289c2baSAndroid Build Coastguard Worker &toplevel_vbmeta);
1545*d289c2baSAndroid Build Coastguard Worker
1546*d289c2baSAndroid Build Coastguard Worker /* Fill in |ab_suffix| field. */
1547*d289c2baSAndroid Build Coastguard Worker slot_data->ab_suffix = avb_strdup(ab_suffix);
1548*d289c2baSAndroid Build Coastguard Worker if (slot_data->ab_suffix == NULL) {
1549*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1550*d289c2baSAndroid Build Coastguard Worker goto fail;
1551*d289c2baSAndroid Build Coastguard Worker }
1552*d289c2baSAndroid Build Coastguard Worker
1553*d289c2baSAndroid Build Coastguard Worker /* If verification is disabled, we are done ... we specifically
1554*d289c2baSAndroid Build Coastguard Worker * don't want to add any androidboot.* options since verification
1555*d289c2baSAndroid Build Coastguard Worker * is disabled.
1556*d289c2baSAndroid Build Coastguard Worker */
1557*d289c2baSAndroid Build Coastguard Worker if (toplevel_vbmeta.flags & AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED) {
1558*d289c2baSAndroid Build Coastguard Worker /* Since verification is disabled we didn't process any
1559*d289c2baSAndroid Build Coastguard Worker * descriptors and thus there's no cmdline... so set root= such
1560*d289c2baSAndroid Build Coastguard Worker * that the system partition is mounted.
1561*d289c2baSAndroid Build Coastguard Worker */
1562*d289c2baSAndroid Build Coastguard Worker avb_assert(slot_data->cmdline == NULL);
1563*d289c2baSAndroid Build Coastguard Worker // Devices with dynamic partitions won't have system partition.
1564*d289c2baSAndroid Build Coastguard Worker // Instead, it has a large super partition to accommodate *.img files.
1565*d289c2baSAndroid Build Coastguard Worker // See b/119551429 for details.
1566*d289c2baSAndroid Build Coastguard Worker if (has_system_partition(ops, ab_suffix)) {
1567*d289c2baSAndroid Build Coastguard Worker slot_data->cmdline =
1568*d289c2baSAndroid Build Coastguard Worker avb_strdup("root=PARTUUID=$(ANDROID_SYSTEM_PARTUUID)");
1569*d289c2baSAndroid Build Coastguard Worker } else {
1570*d289c2baSAndroid Build Coastguard Worker // The |cmdline| field should be a NUL-terminated string.
1571*d289c2baSAndroid Build Coastguard Worker slot_data->cmdline = avb_strdup("");
1572*d289c2baSAndroid Build Coastguard Worker }
1573*d289c2baSAndroid Build Coastguard Worker if (slot_data->cmdline == NULL) {
1574*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1575*d289c2baSAndroid Build Coastguard Worker goto fail;
1576*d289c2baSAndroid Build Coastguard Worker }
1577*d289c2baSAndroid Build Coastguard Worker } else {
1578*d289c2baSAndroid Build Coastguard Worker /* If requested, manage dm-verity mode... */
1579*d289c2baSAndroid Build Coastguard Worker AvbHashtreeErrorMode resolved_hashtree_error_mode = hashtree_error_mode;
1580*d289c2baSAndroid Build Coastguard Worker if (hashtree_error_mode ==
1581*d289c2baSAndroid Build Coastguard Worker AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO) {
1582*d289c2baSAndroid Build Coastguard Worker AvbIOResult io_ret;
1583*d289c2baSAndroid Build Coastguard Worker io_ret = avb_manage_hashtree_error_mode(
1584*d289c2baSAndroid Build Coastguard Worker ops, flags, slot_data, &resolved_hashtree_error_mode);
1585*d289c2baSAndroid Build Coastguard Worker if (io_ret != AVB_IO_RESULT_OK) {
1586*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
1587*d289c2baSAndroid Build Coastguard Worker if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
1588*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1589*d289c2baSAndroid Build Coastguard Worker }
1590*d289c2baSAndroid Build Coastguard Worker goto fail;
1591*d289c2baSAndroid Build Coastguard Worker }
1592*d289c2baSAndroid Build Coastguard Worker }
1593*d289c2baSAndroid Build Coastguard Worker slot_data->resolved_hashtree_error_mode = resolved_hashtree_error_mode;
1594*d289c2baSAndroid Build Coastguard Worker
1595*d289c2baSAndroid Build Coastguard Worker /* Add options... */
1596*d289c2baSAndroid Build Coastguard Worker AvbSlotVerifyResult sub_ret;
1597*d289c2baSAndroid Build Coastguard Worker sub_ret = avb_append_options(ops,
1598*d289c2baSAndroid Build Coastguard Worker flags,
1599*d289c2baSAndroid Build Coastguard Worker slot_data,
1600*d289c2baSAndroid Build Coastguard Worker &toplevel_vbmeta,
1601*d289c2baSAndroid Build Coastguard Worker algorithm_type,
1602*d289c2baSAndroid Build Coastguard Worker hashtree_error_mode,
1603*d289c2baSAndroid Build Coastguard Worker resolved_hashtree_error_mode);
1604*d289c2baSAndroid Build Coastguard Worker if (sub_ret != AVB_SLOT_VERIFY_RESULT_OK) {
1605*d289c2baSAndroid Build Coastguard Worker ret = sub_ret;
1606*d289c2baSAndroid Build Coastguard Worker goto fail;
1607*d289c2baSAndroid Build Coastguard Worker }
1608*d289c2baSAndroid Build Coastguard Worker }
1609*d289c2baSAndroid Build Coastguard Worker
1610*d289c2baSAndroid Build Coastguard Worker /* Substitute $(ANDROID_SYSTEM_PARTUUID) and friends. */
1611*d289c2baSAndroid Build Coastguard Worker if (slot_data->cmdline != NULL && avb_strlen(slot_data->cmdline) != 0) {
1612*d289c2baSAndroid Build Coastguard Worker char* new_cmdline;
1613*d289c2baSAndroid Build Coastguard Worker new_cmdline = avb_sub_cmdline(ops,
1614*d289c2baSAndroid Build Coastguard Worker slot_data->cmdline,
1615*d289c2baSAndroid Build Coastguard Worker ab_suffix,
1616*d289c2baSAndroid Build Coastguard Worker using_boot_for_vbmeta,
1617*d289c2baSAndroid Build Coastguard Worker additional_cmdline_subst);
1618*d289c2baSAndroid Build Coastguard Worker if (new_cmdline != slot_data->cmdline) {
1619*d289c2baSAndroid Build Coastguard Worker if (new_cmdline == NULL) {
1620*d289c2baSAndroid Build Coastguard Worker ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1621*d289c2baSAndroid Build Coastguard Worker goto fail;
1622*d289c2baSAndroid Build Coastguard Worker }
1623*d289c2baSAndroid Build Coastguard Worker avb_free(slot_data->cmdline);
1624*d289c2baSAndroid Build Coastguard Worker slot_data->cmdline = new_cmdline;
1625*d289c2baSAndroid Build Coastguard Worker }
1626*d289c2baSAndroid Build Coastguard Worker }
1627*d289c2baSAndroid Build Coastguard Worker
1628*d289c2baSAndroid Build Coastguard Worker if (out_data != NULL) {
1629*d289c2baSAndroid Build Coastguard Worker *out_data = slot_data;
1630*d289c2baSAndroid Build Coastguard Worker } else {
1631*d289c2baSAndroid Build Coastguard Worker avb_slot_verify_data_free(slot_data);
1632*d289c2baSAndroid Build Coastguard Worker }
1633*d289c2baSAndroid Build Coastguard Worker
1634*d289c2baSAndroid Build Coastguard Worker avb_free_cmdline_subst_list(additional_cmdline_subst);
1635*d289c2baSAndroid Build Coastguard Worker additional_cmdline_subst = NULL;
1636*d289c2baSAndroid Build Coastguard Worker
1637*d289c2baSAndroid Build Coastguard Worker if (!allow_verification_error) {
1638*d289c2baSAndroid Build Coastguard Worker avb_assert(ret == AVB_SLOT_VERIFY_RESULT_OK);
1639*d289c2baSAndroid Build Coastguard Worker }
1640*d289c2baSAndroid Build Coastguard Worker
1641*d289c2baSAndroid Build Coastguard Worker return ret;
1642*d289c2baSAndroid Build Coastguard Worker
1643*d289c2baSAndroid Build Coastguard Worker fail:
1644*d289c2baSAndroid Build Coastguard Worker if (slot_data != NULL) {
1645*d289c2baSAndroid Build Coastguard Worker avb_slot_verify_data_free(slot_data);
1646*d289c2baSAndroid Build Coastguard Worker }
1647*d289c2baSAndroid Build Coastguard Worker if (additional_cmdline_subst != NULL) {
1648*d289c2baSAndroid Build Coastguard Worker avb_free_cmdline_subst_list(additional_cmdline_subst);
1649*d289c2baSAndroid Build Coastguard Worker }
1650*d289c2baSAndroid Build Coastguard Worker return ret;
1651*d289c2baSAndroid Build Coastguard Worker }
1652*d289c2baSAndroid Build Coastguard Worker
avb_slot_verify_data_free(AvbSlotVerifyData * data)1653*d289c2baSAndroid Build Coastguard Worker void avb_slot_verify_data_free(AvbSlotVerifyData* data) {
1654*d289c2baSAndroid Build Coastguard Worker if (data->ab_suffix != NULL) {
1655*d289c2baSAndroid Build Coastguard Worker avb_free(data->ab_suffix);
1656*d289c2baSAndroid Build Coastguard Worker }
1657*d289c2baSAndroid Build Coastguard Worker if (data->cmdline != NULL) {
1658*d289c2baSAndroid Build Coastguard Worker avb_free(data->cmdline);
1659*d289c2baSAndroid Build Coastguard Worker }
1660*d289c2baSAndroid Build Coastguard Worker if (data->vbmeta_images != NULL) {
1661*d289c2baSAndroid Build Coastguard Worker size_t n;
1662*d289c2baSAndroid Build Coastguard Worker for (n = 0; n < data->num_vbmeta_images; n++) {
1663*d289c2baSAndroid Build Coastguard Worker AvbVBMetaData* vbmeta_image = &data->vbmeta_images[n];
1664*d289c2baSAndroid Build Coastguard Worker if (vbmeta_image->partition_name != NULL) {
1665*d289c2baSAndroid Build Coastguard Worker avb_free(vbmeta_image->partition_name);
1666*d289c2baSAndroid Build Coastguard Worker }
1667*d289c2baSAndroid Build Coastguard Worker if (vbmeta_image->vbmeta_data != NULL) {
1668*d289c2baSAndroid Build Coastguard Worker avb_free(vbmeta_image->vbmeta_data);
1669*d289c2baSAndroid Build Coastguard Worker }
1670*d289c2baSAndroid Build Coastguard Worker }
1671*d289c2baSAndroid Build Coastguard Worker avb_free(data->vbmeta_images);
1672*d289c2baSAndroid Build Coastguard Worker }
1673*d289c2baSAndroid Build Coastguard Worker if (data->loaded_partitions != NULL) {
1674*d289c2baSAndroid Build Coastguard Worker size_t n;
1675*d289c2baSAndroid Build Coastguard Worker for (n = 0; n < data->num_loaded_partitions; n++) {
1676*d289c2baSAndroid Build Coastguard Worker AvbPartitionData* loaded_partition = &data->loaded_partitions[n];
1677*d289c2baSAndroid Build Coastguard Worker if (loaded_partition->partition_name != NULL) {
1678*d289c2baSAndroid Build Coastguard Worker avb_free(loaded_partition->partition_name);
1679*d289c2baSAndroid Build Coastguard Worker }
1680*d289c2baSAndroid Build Coastguard Worker if (loaded_partition->data != NULL && !loaded_partition->preloaded) {
1681*d289c2baSAndroid Build Coastguard Worker avb_free(loaded_partition->data);
1682*d289c2baSAndroid Build Coastguard Worker }
1683*d289c2baSAndroid Build Coastguard Worker }
1684*d289c2baSAndroid Build Coastguard Worker avb_free(data->loaded_partitions);
1685*d289c2baSAndroid Build Coastguard Worker }
1686*d289c2baSAndroid Build Coastguard Worker avb_free(data);
1687*d289c2baSAndroid Build Coastguard Worker }
1688*d289c2baSAndroid Build Coastguard Worker
avb_slot_verify_result_to_string(AvbSlotVerifyResult result)1689*d289c2baSAndroid Build Coastguard Worker const char* avb_slot_verify_result_to_string(AvbSlotVerifyResult result) {
1690*d289c2baSAndroid Build Coastguard Worker const char* ret = NULL;
1691*d289c2baSAndroid Build Coastguard Worker
1692*d289c2baSAndroid Build Coastguard Worker switch (result) {
1693*d289c2baSAndroid Build Coastguard Worker case AVB_SLOT_VERIFY_RESULT_OK:
1694*d289c2baSAndroid Build Coastguard Worker ret = "OK";
1695*d289c2baSAndroid Build Coastguard Worker break;
1696*d289c2baSAndroid Build Coastguard Worker case AVB_SLOT_VERIFY_RESULT_ERROR_OOM:
1697*d289c2baSAndroid Build Coastguard Worker ret = "ERROR_OOM";
1698*d289c2baSAndroid Build Coastguard Worker break;
1699*d289c2baSAndroid Build Coastguard Worker case AVB_SLOT_VERIFY_RESULT_ERROR_IO:
1700*d289c2baSAndroid Build Coastguard Worker ret = "ERROR_IO";
1701*d289c2baSAndroid Build Coastguard Worker break;
1702*d289c2baSAndroid Build Coastguard Worker case AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION:
1703*d289c2baSAndroid Build Coastguard Worker ret = "ERROR_VERIFICATION";
1704*d289c2baSAndroid Build Coastguard Worker break;
1705*d289c2baSAndroid Build Coastguard Worker case AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX:
1706*d289c2baSAndroid Build Coastguard Worker ret = "ERROR_ROLLBACK_INDEX";
1707*d289c2baSAndroid Build Coastguard Worker break;
1708*d289c2baSAndroid Build Coastguard Worker case AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED:
1709*d289c2baSAndroid Build Coastguard Worker ret = "ERROR_PUBLIC_KEY_REJECTED";
1710*d289c2baSAndroid Build Coastguard Worker break;
1711*d289c2baSAndroid Build Coastguard Worker case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA:
1712*d289c2baSAndroid Build Coastguard Worker ret = "ERROR_INVALID_METADATA";
1713*d289c2baSAndroid Build Coastguard Worker break;
1714*d289c2baSAndroid Build Coastguard Worker case AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION:
1715*d289c2baSAndroid Build Coastguard Worker ret = "ERROR_UNSUPPORTED_VERSION";
1716*d289c2baSAndroid Build Coastguard Worker break;
1717*d289c2baSAndroid Build Coastguard Worker case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT:
1718*d289c2baSAndroid Build Coastguard Worker ret = "ERROR_INVALID_ARGUMENT";
1719*d289c2baSAndroid Build Coastguard Worker break;
1720*d289c2baSAndroid Build Coastguard Worker /* Do not add a 'default:' case here because of -Wswitch. */
1721*d289c2baSAndroid Build Coastguard Worker }
1722*d289c2baSAndroid Build Coastguard Worker
1723*d289c2baSAndroid Build Coastguard Worker if (ret == NULL) {
1724*d289c2baSAndroid Build Coastguard Worker avb_error("Unknown AvbSlotVerifyResult value.\n");
1725*d289c2baSAndroid Build Coastguard Worker ret = "(unknown)";
1726*d289c2baSAndroid Build Coastguard Worker }
1727*d289c2baSAndroid Build Coastguard Worker
1728*d289c2baSAndroid Build Coastguard Worker return ret;
1729*d289c2baSAndroid Build Coastguard Worker }
1730*d289c2baSAndroid Build Coastguard Worker
avb_slot_verify_data_calculate_vbmeta_digest(const AvbSlotVerifyData * data,AvbDigestType digest_type,uint8_t * out_digest)1731*d289c2baSAndroid Build Coastguard Worker void avb_slot_verify_data_calculate_vbmeta_digest(const AvbSlotVerifyData* data,
1732*d289c2baSAndroid Build Coastguard Worker AvbDigestType digest_type,
1733*d289c2baSAndroid Build Coastguard Worker uint8_t* out_digest) {
1734*d289c2baSAndroid Build Coastguard Worker bool ret = false;
1735*d289c2baSAndroid Build Coastguard Worker size_t n;
1736*d289c2baSAndroid Build Coastguard Worker
1737*d289c2baSAndroid Build Coastguard Worker switch (digest_type) {
1738*d289c2baSAndroid Build Coastguard Worker case AVB_DIGEST_TYPE_SHA256: {
1739*d289c2baSAndroid Build Coastguard Worker AvbSHA256Ctx ctx;
1740*d289c2baSAndroid Build Coastguard Worker avb_sha256_init(&ctx);
1741*d289c2baSAndroid Build Coastguard Worker for (n = 0; n < data->num_vbmeta_images; n++) {
1742*d289c2baSAndroid Build Coastguard Worker avb_sha256_update(&ctx,
1743*d289c2baSAndroid Build Coastguard Worker data->vbmeta_images[n].vbmeta_data,
1744*d289c2baSAndroid Build Coastguard Worker data->vbmeta_images[n].vbmeta_size);
1745*d289c2baSAndroid Build Coastguard Worker }
1746*d289c2baSAndroid Build Coastguard Worker avb_memcpy(out_digest, avb_sha256_final(&ctx), AVB_SHA256_DIGEST_SIZE);
1747*d289c2baSAndroid Build Coastguard Worker ret = true;
1748*d289c2baSAndroid Build Coastguard Worker } break;
1749*d289c2baSAndroid Build Coastguard Worker
1750*d289c2baSAndroid Build Coastguard Worker case AVB_DIGEST_TYPE_SHA512: {
1751*d289c2baSAndroid Build Coastguard Worker AvbSHA512Ctx ctx;
1752*d289c2baSAndroid Build Coastguard Worker avb_sha512_init(&ctx);
1753*d289c2baSAndroid Build Coastguard Worker for (n = 0; n < data->num_vbmeta_images; n++) {
1754*d289c2baSAndroid Build Coastguard Worker avb_sha512_update(&ctx,
1755*d289c2baSAndroid Build Coastguard Worker data->vbmeta_images[n].vbmeta_data,
1756*d289c2baSAndroid Build Coastguard Worker data->vbmeta_images[n].vbmeta_size);
1757*d289c2baSAndroid Build Coastguard Worker }
1758*d289c2baSAndroid Build Coastguard Worker avb_memcpy(out_digest, avb_sha512_final(&ctx), AVB_SHA512_DIGEST_SIZE);
1759*d289c2baSAndroid Build Coastguard Worker ret = true;
1760*d289c2baSAndroid Build Coastguard Worker } break;
1761*d289c2baSAndroid Build Coastguard Worker
1762*d289c2baSAndroid Build Coastguard Worker /* Do not add a 'default:' case here because of -Wswitch. */
1763*d289c2baSAndroid Build Coastguard Worker }
1764*d289c2baSAndroid Build Coastguard Worker
1765*d289c2baSAndroid Build Coastguard Worker if (!ret) {
1766*d289c2baSAndroid Build Coastguard Worker avb_fatal("Unknown digest type");
1767*d289c2baSAndroid Build Coastguard Worker }
1768*d289c2baSAndroid Build Coastguard Worker }
1769