1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "avb_util.h"
18
19 #include <unistd.h>
20
21 #include <array>
22 #include <sstream>
23
24 #include <android-base/file.h>
25 #include <android-base/strings.h>
26 #include <android-base/unique_fd.h>
27
28 #include "util.h"
29
30 using android::base::Basename;
31 using android::base::ReadFileToString;
32 using android::base::StartsWith;
33 using android::base::unique_fd;
34
35 namespace android {
36 namespace fs_mgr {
37
38 // Constructs dm-verity arguments for sending DM_TABLE_LOAD ioctl to kernel.
39 // See the following link for more details:
40 // https://gitlab.com/cryptsetup/cryptsetup/wikis/DMVerity
ConstructVerityTable(const FsAvbHashtreeDescriptor & hashtree_desc,const std::string & blk_device,android::dm::DmTable * table)41 bool ConstructVerityTable(const FsAvbHashtreeDescriptor& hashtree_desc,
42 const std::string& blk_device, android::dm::DmTable* table) {
43 // Loads androidboot.veritymode from kernel cmdline.
44 std::string verity_mode;
45 if (!fs_mgr_get_boot_config("veritymode", &verity_mode)) {
46 verity_mode = "enforcing"; // Defaults to enforcing when it's absent.
47 }
48
49 // Converts veritymode to the format used in kernel.
50 std::string dm_verity_mode;
51 if (verity_mode == "panicking") {
52 dm_verity_mode = "panic_on_corruption";
53 } else if (verity_mode == "enforcing") {
54 dm_verity_mode = "restart_on_corruption";
55 } else if (verity_mode == "logging") {
56 dm_verity_mode = "ignore_corruption";
57 } else if (verity_mode != "eio") { // Default dm_verity_mode is eio.
58 LERROR << "Unknown androidboot.veritymode: " << verity_mode;
59 return false;
60 }
61
62 std::ostringstream hash_algorithm;
63 hash_algorithm << hashtree_desc.hash_algorithm;
64
65 android::dm::DmTargetVerity target(
66 0, hashtree_desc.image_size / 512, hashtree_desc.dm_verity_version, blk_device,
67 blk_device, hashtree_desc.data_block_size, hashtree_desc.hash_block_size,
68 hashtree_desc.image_size / hashtree_desc.data_block_size,
69 hashtree_desc.tree_offset / hashtree_desc.hash_block_size, hash_algorithm.str(),
70 hashtree_desc.root_digest, hashtree_desc.salt);
71 if (hashtree_desc.fec_size > 0) {
72 target.UseFec(blk_device, hashtree_desc.fec_num_roots,
73 hashtree_desc.fec_offset / hashtree_desc.data_block_size,
74 hashtree_desc.fec_offset / hashtree_desc.data_block_size);
75 }
76 if (!dm_verity_mode.empty()) {
77 target.SetVerityMode(dm_verity_mode);
78 }
79 // Always use ignore_zero_blocks.
80 target.IgnoreZeroBlocks();
81
82 if (hashtree_desc.flags & AVB_HASHTREE_DESCRIPTOR_FLAGS_CHECK_AT_MOST_ONCE) {
83 target.CheckAtMostOnce();
84 }
85
86 LINFO << "Built verity table: '" << target.GetParameterString() << "'";
87
88 return table->AddTarget(std::make_unique<android::dm::DmTargetVerity>(target));
89 }
90
HashtreeDmVeritySetup(FstabEntry * fstab_entry,const FsAvbHashtreeDescriptor & hashtree_desc,bool wait_for_verity_dev)91 bool HashtreeDmVeritySetup(FstabEntry* fstab_entry, const FsAvbHashtreeDescriptor& hashtree_desc,
92 bool wait_for_verity_dev) {
93 android::dm::DmTable table;
94 if (!ConstructVerityTable(hashtree_desc, fstab_entry->blk_device, &table) || !table.valid()) {
95 LERROR << "Failed to construct verity table.";
96 return false;
97 }
98 table.set_readonly(true);
99
100 std::chrono::milliseconds timeout = {};
101 if (wait_for_verity_dev) timeout = 1s;
102
103 std::string dev_path;
104 const std::string device_name(GetVerityDeviceName(*fstab_entry));
105 android::dm::DeviceMapper& dm = android::dm::DeviceMapper::Instance();
106 if (!dm.CreateDevice(device_name, table, &dev_path, timeout)) {
107 LERROR << "Couldn't create verity device!";
108 return false;
109 }
110
111 // Marks the underlying block device as read-only.
112 SetBlockDeviceReadOnly(fstab_entry->blk_device);
113
114 // Updates fstab_rec->blk_device to verity device name.
115 fstab_entry->blk_device = dev_path;
116 return true;
117 }
118
GetHashtreeDescriptor(const std::string & partition_name,const std::vector<VBMetaData> & vbmeta_images)119 std::unique_ptr<FsAvbHashtreeDescriptor> GetHashtreeDescriptor(
120 const std::string& partition_name, const std::vector<VBMetaData>& vbmeta_images) {
121 bool found = false;
122 const uint8_t* desc_partition_name;
123 auto hashtree_desc = std::make_unique<FsAvbHashtreeDescriptor>();
124
125 for (const auto& vbmeta : vbmeta_images) {
126 size_t num_descriptors;
127 std::unique_ptr<const AvbDescriptor* [], decltype(&avb_free)> descriptors(
128 avb_descriptor_get_all(vbmeta.data(), vbmeta.size(), &num_descriptors), avb_free);
129
130 if (!descriptors || num_descriptors < 1) {
131 continue;
132 }
133
134 for (size_t n = 0; n < num_descriptors && !found; n++) {
135 AvbDescriptor desc;
136 if (!avb_descriptor_validate_and_byteswap(descriptors[n], &desc)) {
137 LWARNING << "Descriptor[" << n << "] is invalid";
138 continue;
139 }
140 if (desc.tag == AVB_DESCRIPTOR_TAG_HASHTREE) {
141 desc_partition_name =
142 (const uint8_t*)descriptors[n] + sizeof(AvbHashtreeDescriptor);
143 if (!avb_hashtree_descriptor_validate_and_byteswap(
144 (AvbHashtreeDescriptor*)descriptors[n], hashtree_desc.get())) {
145 continue;
146 }
147 if (hashtree_desc->partition_name_len != partition_name.length()) {
148 continue;
149 }
150 // Notes that desc_partition_name is not NUL-terminated.
151 std::string hashtree_partition_name((const char*)desc_partition_name,
152 hashtree_desc->partition_name_len);
153 if (hashtree_partition_name == partition_name) {
154 found = true;
155 }
156 }
157 }
158
159 if (found) break;
160 }
161
162 if (!found) {
163 LERROR << "Hashtree descriptor not found: " << partition_name;
164 return nullptr;
165 }
166
167 hashtree_desc->partition_name = partition_name;
168
169 const uint8_t* desc_salt = desc_partition_name + hashtree_desc->partition_name_len;
170 hashtree_desc->salt = BytesToHex(desc_salt, hashtree_desc->salt_len);
171
172 const uint8_t* desc_digest = desc_salt + hashtree_desc->salt_len;
173 hashtree_desc->root_digest = BytesToHex(desc_digest, hashtree_desc->root_digest_len);
174
175 return hashtree_desc;
176 }
177
LoadAvbHashtreeToEnableVerity(FstabEntry * fstab_entry,bool wait_for_verity_dev,const std::vector<VBMetaData> & vbmeta_images,const std::string & ab_suffix,const std::string & ab_other_suffix)178 bool LoadAvbHashtreeToEnableVerity(FstabEntry* fstab_entry, bool wait_for_verity_dev,
179 const std::vector<VBMetaData>& vbmeta_images,
180 const std::string& ab_suffix,
181 const std::string& ab_other_suffix) {
182 // Derives partition_name from blk_device to query the corresponding AVB HASHTREE descriptor
183 // to setup dm-verity. The partition_names in AVB descriptors are without A/B suffix.
184 std::string partition_name = DeriveAvbPartitionName(*fstab_entry, ab_suffix, ab_other_suffix);
185
186 if (partition_name.empty()) {
187 LERROR << "partition name is empty, cannot lookup AVB descriptors";
188 return false;
189 }
190
191 std::unique_ptr<FsAvbHashtreeDescriptor> hashtree_descriptor =
192 GetHashtreeDescriptor(partition_name, vbmeta_images);
193 if (!hashtree_descriptor) {
194 return false;
195 }
196
197 // Converts HASHTREE descriptor to verity table to load into kernel.
198 // When success, the new device path will be returned, e.g., /dev/block/dm-2.
199 return HashtreeDmVeritySetup(fstab_entry, *hashtree_descriptor, wait_for_verity_dev);
200 }
201
202 // Converts a AVB partition_name (without A/B suffix) to a device partition name.
203 // e.g., "system" => "system_a",
204 // "system_other" => "system_b".
205 //
206 // If the device is non-A/B, converts it to a partition name without suffix.
207 // e.g., "system" => "system",
208 // "system_other" => "system".
AvbPartitionToDevicePatition(const std::string & avb_partition_name,const std::string & ab_suffix,const std::string & ab_other_suffix)209 std::string AvbPartitionToDevicePatition(const std::string& avb_partition_name,
210 const std::string& ab_suffix,
211 const std::string& ab_other_suffix) {
212 bool is_other_slot = false;
213 std::string sanitized_partition_name(avb_partition_name);
214
215 auto other_suffix = sanitized_partition_name.rfind("_other");
216 if (other_suffix != std::string::npos) {
217 sanitized_partition_name.erase(other_suffix); // converts system_other => system
218 is_other_slot = true;
219 }
220
221 auto append_suffix = is_other_slot ? ab_other_suffix : ab_suffix;
222 return sanitized_partition_name + append_suffix;
223 }
224
225 // Converts fstab_entry.blk_device (with ab_suffix) to a AVB partition name.
226 // e.g., "/dev/block/by-name/system_a", slot_select => "system",
227 // "/dev/block/by-name/system_b", slot_select_other => "system_other".
228 //
229 // Or for a logical partition (with ab_suffix):
230 // e.g., "system_a", slot_select => "system",
231 // "system_b", slot_select_other => "system_other".
DeriveAvbPartitionName(const FstabEntry & fstab_entry,const std::string & ab_suffix,const std::string & ab_other_suffix)232 std::string DeriveAvbPartitionName(const FstabEntry& fstab_entry, const std::string& ab_suffix,
233 const std::string& ab_other_suffix) {
234 std::string partition_name;
235 if (fstab_entry.fs_mgr_flags.logical) {
236 partition_name = fstab_entry.logical_partition_name;
237 } else {
238 partition_name = Basename(fstab_entry.blk_device);
239 }
240
241 if (fstab_entry.fs_mgr_flags.slot_select) {
242 auto found = partition_name.rfind(ab_suffix);
243 if (found != std::string::npos) {
244 partition_name.erase(found); // converts system_a => system
245 }
246 } else if (fstab_entry.fs_mgr_flags.slot_select_other) {
247 auto found = partition_name.rfind(ab_other_suffix);
248 if (found != std::string::npos) {
249 partition_name.erase(found); // converts system_b => system
250 }
251 partition_name += "_other"; // converts system => system_other
252 }
253
254 return partition_name;
255 }
256
GetTotalSize(int fd)257 off64_t GetTotalSize(int fd) {
258 off64_t saved_current = lseek64(fd, 0, SEEK_CUR);
259 if (saved_current == -1) {
260 PERROR << "Failed to get current position";
261 return -1;
262 }
263
264 // lseek64() returns the resulting offset location from the beginning of the file.
265 off64_t total_size = lseek64(fd, 0, SEEK_END);
266 if (total_size == -1) {
267 PERROR << "Failed to lseek64 to end of the partition";
268 return -1;
269 }
270
271 // Restores the original offset.
272 if (lseek64(fd, saved_current, SEEK_SET) == -1) {
273 PERROR << "Failed to lseek64 to the original offset: " << saved_current;
274 }
275
276 return total_size;
277 }
278
GetAvbFooter(int fd)279 std::unique_ptr<AvbFooter> GetAvbFooter(int fd) {
280 std::array<uint8_t, AVB_FOOTER_SIZE> footer_buf;
281 auto footer = std::make_unique<AvbFooter>();
282
283 off64_t footer_offset = GetTotalSize(fd) - AVB_FOOTER_SIZE;
284
285 ssize_t num_read =
286 TEMP_FAILURE_RETRY(pread64(fd, footer_buf.data(), AVB_FOOTER_SIZE, footer_offset));
287 if (num_read < 0 || num_read != AVB_FOOTER_SIZE) {
288 PERROR << "Failed to read AVB footer at offset: " << footer_offset;
289 return nullptr;
290 }
291
292 if (!avb_footer_validate_and_byteswap((const AvbFooter*)footer_buf.data(), footer.get())) {
293 PERROR << "AVB footer verification failed at offset " << footer_offset;
294 return nullptr;
295 }
296
297 return footer;
298 }
299
ValidatePublicKeyBlob(const uint8_t * key,size_t length,const std::string & expected_key_blob)300 bool ValidatePublicKeyBlob(const uint8_t* key, size_t length,
301 const std::string& expected_key_blob) {
302 if (expected_key_blob.empty()) { // no expectation of the key, return true.
303 return true;
304 }
305 if (expected_key_blob.size() != length) {
306 return false;
307 }
308 if (0 == memcmp(key, expected_key_blob.data(), length)) {
309 return true;
310 }
311 return false;
312 }
313
ValidatePublicKeyBlob(const std::string & key_blob_to_validate,const std::vector<std::string> & allowed_key_paths)314 bool ValidatePublicKeyBlob(const std::string& key_blob_to_validate,
315 const std::vector<std::string>& allowed_key_paths) {
316 std::string allowed_key_blob;
317 if (key_blob_to_validate.empty()) {
318 LWARNING << "Failed to validate an empty key";
319 return false;
320 }
321 for (const auto& path : allowed_key_paths) {
322 if (ReadFileToString(path, &allowed_key_blob)) {
323 if (key_blob_to_validate == allowed_key_blob) return true;
324 }
325 }
326 return false;
327 }
328
VerifyVBMetaSignature(const VBMetaData & vbmeta,const std::string & expected_public_key_blob,std::string * out_public_key_data)329 VBMetaVerifyResult VerifyVBMetaSignature(const VBMetaData& vbmeta,
330 const std::string& expected_public_key_blob,
331 std::string* out_public_key_data) {
332 const uint8_t* pk_data;
333 size_t pk_len;
334 ::AvbVBMetaVerifyResult vbmeta_ret;
335
336 vbmeta_ret = avb_vbmeta_image_verify(vbmeta.data(), vbmeta.size(), &pk_data, &pk_len);
337
338 if (out_public_key_data != nullptr) {
339 out_public_key_data->clear();
340 if (pk_len > 0) {
341 out_public_key_data->append(reinterpret_cast<const char*>(pk_data), pk_len);
342 }
343 }
344
345 switch (vbmeta_ret) {
346 case AVB_VBMETA_VERIFY_RESULT_OK:
347 if (pk_data == nullptr || pk_len <= 0) {
348 LERROR << vbmeta.partition()
349 << ": Error verifying vbmeta image: failed to get public key";
350 return VBMetaVerifyResult::kError;
351 }
352 if (!ValidatePublicKeyBlob(pk_data, pk_len, expected_public_key_blob)) {
353 LERROR << vbmeta.partition() << ": Error verifying vbmeta image: public key used to"
354 << " sign data does not match key in chain descriptor";
355 return VBMetaVerifyResult::kErrorVerification;
356 }
357 return VBMetaVerifyResult::kSuccess;
358 case AVB_VBMETA_VERIFY_RESULT_OK_NOT_SIGNED:
359 case AVB_VBMETA_VERIFY_RESULT_HASH_MISMATCH:
360 case AVB_VBMETA_VERIFY_RESULT_SIGNATURE_MISMATCH:
361 LERROR << vbmeta.partition() << ": Error verifying vbmeta image: "
362 << avb_vbmeta_verify_result_to_string(vbmeta_ret);
363 return VBMetaVerifyResult::kErrorVerification;
364 case AVB_VBMETA_VERIFY_RESULT_INVALID_VBMETA_HEADER:
365 // No way to continue this case.
366 LERROR << vbmeta.partition() << ": Error verifying vbmeta image: invalid vbmeta header";
367 break;
368 case AVB_VBMETA_VERIFY_RESULT_UNSUPPORTED_VERSION:
369 // No way to continue this case.
370 LERROR << vbmeta.partition()
371 << ": Error verifying vbmeta image: unsupported AVB version";
372 break;
373 default:
374 LERROR << "Unknown vbmeta image verify return value: " << vbmeta_ret;
375 break;
376 }
377
378 return VBMetaVerifyResult::kError;
379 }
380
VerifyVBMetaData(int fd,const std::string & partition_name,const std::string & expected_public_key_blob,std::string * out_public_key_data,VBMetaVerifyResult * out_verify_result)381 std::unique_ptr<VBMetaData> VerifyVBMetaData(int fd, const std::string& partition_name,
382 const std::string& expected_public_key_blob,
383 std::string* out_public_key_data,
384 VBMetaVerifyResult* out_verify_result) {
385 uint64_t vbmeta_offset = 0;
386 uint64_t vbmeta_size = VBMetaData::kMaxVBMetaSize;
387 bool is_vbmeta_partition = StartsWith(partition_name, "vbmeta");
388
389 if (out_verify_result) {
390 *out_verify_result = VBMetaVerifyResult::kError;
391 }
392
393 if (!is_vbmeta_partition) {
394 std::unique_ptr<AvbFooter> footer = GetAvbFooter(fd);
395 if (!footer) {
396 return nullptr;
397 }
398 vbmeta_offset = footer->vbmeta_offset;
399 vbmeta_size = footer->vbmeta_size;
400 }
401
402 if (vbmeta_size > VBMetaData::kMaxVBMetaSize) {
403 LERROR << "VbMeta size in footer exceeds kMaxVBMetaSize";
404 return nullptr;
405 }
406
407 auto vbmeta = std::make_unique<VBMetaData>(vbmeta_size, partition_name);
408 ssize_t num_read = TEMP_FAILURE_RETRY(pread64(fd, vbmeta->data(), vbmeta_size, vbmeta_offset));
409 // Allows partial read for vbmeta partition, because its vbmeta_size is kMaxVBMetaSize.
410 if (num_read < 0 || (!is_vbmeta_partition && static_cast<uint64_t>(num_read) != vbmeta_size)) {
411 PERROR << partition_name << ": Failed to read vbmeta at offset " << vbmeta_offset
412 << " with size " << vbmeta_size;
413 return nullptr;
414 }
415
416 auto verify_result =
417 VerifyVBMetaSignature(*vbmeta, expected_public_key_blob, out_public_key_data);
418
419 if (out_verify_result != nullptr) {
420 *out_verify_result = verify_result;
421 }
422
423 if (verify_result == VBMetaVerifyResult::kSuccess ||
424 verify_result == VBMetaVerifyResult::kErrorVerification) {
425 return vbmeta;
426 }
427
428 return nullptr;
429 }
430
RollbackDetected(const std::string & partition_name ATTRIBUTE_UNUSED,uint64_t rollback_index ATTRIBUTE_UNUSED)431 bool RollbackDetected(const std::string& partition_name ATTRIBUTE_UNUSED,
432 uint64_t rollback_index ATTRIBUTE_UNUSED) {
433 // TODO(bowgotsai): Support rollback protection.
434 return false;
435 }
436
GetChainPartitionInfo(const VBMetaData & vbmeta,bool * fatal_error)437 std::vector<ChainInfo> GetChainPartitionInfo(const VBMetaData& vbmeta, bool* fatal_error) {
438 CHECK(fatal_error != nullptr);
439 std::vector<ChainInfo> chain_partitions;
440
441 size_t num_descriptors;
442 std::unique_ptr<const AvbDescriptor* [], decltype(&avb_free)> descriptors(
443 avb_descriptor_get_all(vbmeta.data(), vbmeta.size(), &num_descriptors), avb_free);
444
445 if (!descriptors || num_descriptors < 1) {
446 return {};
447 }
448
449 for (size_t i = 0; i < num_descriptors; i++) {
450 AvbDescriptor desc;
451 if (!avb_descriptor_validate_and_byteswap(descriptors[i], &desc)) {
452 LERROR << "Descriptor[" << i << "] is invalid in vbmeta: " << vbmeta.partition();
453 *fatal_error = true;
454 return {};
455 }
456 if (desc.tag == AVB_DESCRIPTOR_TAG_CHAIN_PARTITION) {
457 AvbChainPartitionDescriptor chain_desc;
458 if (!avb_chain_partition_descriptor_validate_and_byteswap(
459 (AvbChainPartitionDescriptor*)descriptors[i], &chain_desc)) {
460 LERROR << "Chain descriptor[" << i
461 << "] is invalid in vbmeta: " << vbmeta.partition();
462 *fatal_error = true;
463 return {};
464 }
465 const char* chain_partition_name =
466 ((const char*)descriptors[i]) + sizeof(AvbChainPartitionDescriptor);
467 const char* chain_public_key_blob =
468 chain_partition_name + chain_desc.partition_name_len;
469 chain_partitions.emplace_back(
470 std::string(chain_partition_name, chain_desc.partition_name_len),
471 std::string(chain_public_key_blob, chain_desc.public_key_len));
472 }
473 }
474
475 return chain_partitions;
476 }
477
478 // Loads the vbmeta from a given path.
LoadAndVerifyVbmetaByPath(const std::string & image_path,const std::string & partition_name,const std::string & expected_public_key_blob,bool allow_verification_error,bool rollback_protection,bool is_chained_vbmeta,std::string * out_public_key_data,bool * out_verification_disabled,VBMetaVerifyResult * out_verify_result)479 std::unique_ptr<VBMetaData> LoadAndVerifyVbmetaByPath(
480 const std::string& image_path, const std::string& partition_name,
481 const std::string& expected_public_key_blob, bool allow_verification_error,
482 bool rollback_protection, bool is_chained_vbmeta, std::string* out_public_key_data,
483 bool* out_verification_disabled, VBMetaVerifyResult* out_verify_result) {
484 if (out_verify_result) {
485 *out_verify_result = VBMetaVerifyResult::kError;
486 }
487
488 // Ensures the device path (might be a symlink created by init) is ready to access.
489 if (!WaitForFile(image_path, 1s)) {
490 PERROR << "No such path: " << image_path;
491 return nullptr;
492 }
493
494 unique_fd fd(TEMP_FAILURE_RETRY(open(image_path.c_str(), O_RDONLY | O_CLOEXEC)));
495 if (fd < 0) {
496 PERROR << "Failed to open: " << image_path;
497 return nullptr;
498 }
499
500 VBMetaVerifyResult verify_result;
501 std::unique_ptr<VBMetaData> vbmeta = VerifyVBMetaData(
502 fd, partition_name, expected_public_key_blob, out_public_key_data, &verify_result);
503 if (!vbmeta) {
504 LERROR << partition_name << ": Failed to load vbmeta, result: " << verify_result;
505 return nullptr;
506 }
507 vbmeta->set_vbmeta_path(image_path);
508
509 if (!allow_verification_error && verify_result == VBMetaVerifyResult::kErrorVerification) {
510 LERROR << partition_name << ": allow verification error is not allowed";
511 return nullptr;
512 }
513
514 std::unique_ptr<AvbVBMetaImageHeader> vbmeta_header =
515 vbmeta->GetVBMetaHeader(true /* update_vbmeta_size */);
516 if (!vbmeta_header) {
517 LERROR << partition_name << ": Failed to get vbmeta header";
518 return nullptr;
519 }
520
521 if (rollback_protection && RollbackDetected(partition_name, vbmeta_header->rollback_index)) {
522 return nullptr;
523 }
524
525 // vbmeta flags can only be set by the top-level vbmeta image.
526 if (is_chained_vbmeta && vbmeta_header->flags != 0) {
527 LERROR << partition_name << ": chained vbmeta image has non-zero flags";
528 return nullptr;
529 }
530
531 // Checks if verification has been disabled by setting a bit in the image.
532 if (out_verification_disabled) {
533 if (vbmeta_header->flags & AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED) {
534 LWARNING << "VERIFICATION_DISABLED bit is set for partition: " << partition_name;
535 *out_verification_disabled = true;
536 } else {
537 *out_verification_disabled = false;
538 }
539 }
540
541 if (out_verify_result) {
542 *out_verify_result = verify_result;
543 }
544
545 return vbmeta;
546 }
547
LoadAndVerifyVbmetaByPartition(const std::string & partition_name,const std::string & ab_suffix,const std::string & ab_other_suffix,const std::string & expected_public_key_blob,bool allow_verification_error,bool load_chained_vbmeta,bool rollback_protection,std::function<std::string (const std::string &)> device_path_constructor,bool is_chained_vbmeta,std::vector<VBMetaData> * out_vbmeta_images)548 VBMetaVerifyResult LoadAndVerifyVbmetaByPartition(
549 const std::string& partition_name, const std::string& ab_suffix,
550 const std::string& ab_other_suffix, const std::string& expected_public_key_blob,
551 bool allow_verification_error, bool load_chained_vbmeta, bool rollback_protection,
552 std::function<std::string(const std::string&)> device_path_constructor, bool is_chained_vbmeta,
553 std::vector<VBMetaData>* out_vbmeta_images) {
554 auto image_path = device_path_constructor(
555 AvbPartitionToDevicePatition(partition_name, ab_suffix, ab_other_suffix));
556
557 bool verification_disabled = false;
558 VBMetaVerifyResult verify_result;
559 auto vbmeta = LoadAndVerifyVbmetaByPath(image_path, partition_name, expected_public_key_blob,
560 allow_verification_error, rollback_protection,
561 is_chained_vbmeta, nullptr /* out_public_key_data */,
562 &verification_disabled, &verify_result);
563
564 if (!vbmeta) {
565 return VBMetaVerifyResult::kError;
566 }
567 if (out_vbmeta_images) {
568 out_vbmeta_images->emplace_back(std::move(*vbmeta));
569 }
570
571 // Only loads chained vbmeta if AVB verification is NOT disabled.
572 if (!verification_disabled && load_chained_vbmeta) {
573 bool fatal_error = false;
574 auto chain_partitions = GetChainPartitionInfo(*out_vbmeta_images->rbegin(), &fatal_error);
575 if (fatal_error) {
576 return VBMetaVerifyResult::kError;
577 }
578 for (auto& chain : chain_partitions) {
579 auto sub_ret = LoadAndVerifyVbmetaByPartition(
580 chain.partition_name, ab_suffix, ab_other_suffix, chain.public_key_blob,
581 allow_verification_error, load_chained_vbmeta, rollback_protection,
582 device_path_constructor, true, /* is_chained_vbmeta */
583 out_vbmeta_images);
584 if (sub_ret != VBMetaVerifyResult::kSuccess) {
585 verify_result = sub_ret; // might be 'ERROR' or 'ERROR VERIFICATION'.
586 if (verify_result == VBMetaVerifyResult::kError) {
587 return verify_result; // stop here if we got an 'ERROR'.
588 }
589 }
590 }
591 }
592
593 return verify_result;
594 }
595
596 } // namespace fs_mgr
597 } // namespace android
598