Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | /* |
| 18 | * This program verifies the integrity of the partitions after an A/B OTA |
| 19 | * update. It gets invoked by init, and will only perform the verification if |
| 20 | * it's the first boot post an A/B OTA update. |
| 21 | * |
| 22 | * It relies on dm-verity to capture any corruption on the partitions being |
| 23 | * verified. dm-verity must be in enforcing mode, so that it will reboot the |
| 24 | * device on dm-verity failures. When that happens, the bootloader should |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 25 | * mark the slot as unbootable and stops trying. Other dm-verity modes ( |
| 26 | * for example, veritymode=EIO) are not accepted and simply lead to a |
| 27 | * verification failure. |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 28 | * |
| 29 | * The current slot will be marked as having booted successfully if the |
| 30 | * verifier reaches the end after the verification. |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 31 | */ |
| 32 | |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 33 | #include <errno.h> |
| 34 | #include <fcntl.h> |
| 35 | #include <stdio.h> |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 36 | #include <string.h> |
| 37 | |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 38 | #include <string> |
| 39 | #include <vector> |
| 40 | |
| 41 | #include <android-base/file.h> |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 42 | #include <android-base/logging.h> |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 43 | #include <android-base/parseint.h> |
Tao Bao | 4f8d217 | 2017-01-13 15:36:32 -0800 | [diff] [blame] | 44 | #include <android-base/properties.h> |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 45 | #include <android-base/strings.h> |
| 46 | #include <android-base/unique_fd.h> |
Connor O'Brien | ad43d2d | 2016-11-21 12:47:33 -0800 | [diff] [blame] | 47 | #include <android/hardware/boot/1.0/IBootControl.h> |
| 48 | |
| 49 | using android::sp; |
| 50 | using android::hardware::boot::V1_0::IBootControl; |
| 51 | using android::hardware::boot::V1_0::BoolResult; |
| 52 | using android::hardware::boot::V1_0::CommandResult; |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 53 | |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 54 | constexpr auto CARE_MAP_FILE = "/data/ota_package/care_map.txt"; |
| 55 | constexpr int BLOCKSIZE = 4096; |
| 56 | |
| 57 | static bool read_blocks(const std::string& blk_device_prefix, const std::string& range_str) { |
Tao Bao | 4f8d217 | 2017-01-13 15:36:32 -0800 | [diff] [blame] | 58 | std::string slot_suffix = android::base::GetProperty("ro.boot.slot_suffix", ""); |
| 59 | std::string blk_device = blk_device_prefix + slot_suffix; |
| 60 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(blk_device.c_str(), O_RDONLY))); |
| 61 | if (fd.get() == -1) { |
| 62 | PLOG(ERROR) << "Error reading partition " << blk_device; |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | // For block range string, first integer 'count' equals 2 * total number of valid ranges, |
| 67 | // followed by 'count' number comma separated integers. Every two integers reprensent a |
| 68 | // block range with the first number included in range but second number not included. |
| 69 | // For example '4,64536,65343,74149,74150' represents: [64536,65343) and [74149,74150). |
| 70 | std::vector<std::string> ranges = android::base::Split(range_str, ","); |
| 71 | size_t range_count; |
| 72 | bool status = android::base::ParseUint(ranges[0], &range_count); |
| 73 | if (!status || (range_count == 0) || (range_count % 2 != 0) || |
| 74 | (range_count != ranges.size() - 1)) { |
| 75 | LOG(ERROR) << "Error in parsing range string."; |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | size_t blk_count = 0; |
| 80 | for (size_t i = 1; i < ranges.size(); i += 2) { |
| 81 | unsigned int range_start, range_end; |
| 82 | bool parse_status = android::base::ParseUint(ranges[i], &range_start); |
| 83 | parse_status = parse_status && android::base::ParseUint(ranges[i + 1], &range_end); |
| 84 | if (!parse_status || range_start >= range_end) { |
| 85 | LOG(ERROR) << "Invalid range pair " << ranges[i] << ", " << ranges[i + 1]; |
| 86 | return false; |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Tao Bao | 4f8d217 | 2017-01-13 15:36:32 -0800 | [diff] [blame] | 89 | if (lseek64(fd.get(), static_cast<off64_t>(range_start) * BLOCKSIZE, SEEK_SET) == -1) { |
| 90 | PLOG(ERROR) << "lseek to " << range_start << " failed"; |
| 91 | return false; |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Tao Bao | 4f8d217 | 2017-01-13 15:36:32 -0800 | [diff] [blame] | 94 | size_t size = (range_end - range_start) * BLOCKSIZE; |
| 95 | std::vector<uint8_t> buf(size); |
| 96 | if (!android::base::ReadFully(fd.get(), buf.data(), size)) { |
| 97 | PLOG(ERROR) << "Failed to read blocks " << range_start << " to " << range_end; |
| 98 | return false; |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 99 | } |
Tao Bao | 4f8d217 | 2017-01-13 15:36:32 -0800 | [diff] [blame] | 100 | blk_count += (range_end - range_start); |
| 101 | } |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 102 | |
Tao Bao | 4f8d217 | 2017-01-13 15:36:32 -0800 | [diff] [blame] | 103 | LOG(INFO) << "Finished reading " << blk_count << " blocks on " << blk_device; |
| 104 | return true; |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static bool verify_image(const std::string& care_map_name) { |
| 108 | android::base::unique_fd care_map_fd(TEMP_FAILURE_RETRY(open(care_map_name.c_str(), O_RDONLY))); |
| 109 | // If the device is flashed before the current boot, it may not have care_map.txt |
| 110 | // in /data/ota_package. To allow the device to continue booting in this situation, |
| 111 | // we should print a warning and skip the block verification. |
| 112 | if (care_map_fd.get() == -1) { |
| 113 | LOG(WARNING) << "Warning: care map " << care_map_name << " not found."; |
| 114 | return true; |
| 115 | } |
| 116 | // Care map file has four lines (two lines if vendor partition is not present): |
| 117 | // First line has the block device name, e.g./dev/block/.../by-name/system. |
| 118 | // Second line holds all ranges of blocks to verify. |
| 119 | // The next two lines have the same format but for vendor partition. |
| 120 | std::string file_content; |
| 121 | if (!android::base::ReadFdToString(care_map_fd.get(), &file_content)) { |
| 122 | LOG(ERROR) << "Error reading care map contents to string."; |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | std::vector<std::string> lines; |
| 127 | lines = android::base::Split(android::base::Trim(file_content), "\n"); |
| 128 | if (lines.size() != 2 && lines.size() != 4) { |
| 129 | LOG(ERROR) << "Invalid lines in care_map: found " << lines.size() |
| 130 | << " lines, expecting 2 or 4 lines."; |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | for (size_t i = 0; i < lines.size(); i += 2) { |
| 135 | if (!read_blocks(lines[i], lines[i+1])) { |
| 136 | return false; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return true; |
| 141 | } |
| 142 | |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 143 | int main(int argc, char** argv) { |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 144 | for (int i = 1; i < argc; i++) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 145 | LOG(INFO) << "Started with arg " << i << ": " << argv[i]; |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Chris Phoenix | 0157c78 | 2017-01-20 11:34:00 -0800 | [diff] [blame] | 148 | sp<IBootControl> module = IBootControl::getService(); |
Connor O'Brien | ad43d2d | 2016-11-21 12:47:33 -0800 | [diff] [blame] | 149 | if (module == nullptr) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 150 | LOG(ERROR) << "Error getting bootctrl module."; |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 151 | return -1; |
| 152 | } |
| 153 | |
Connor O'Brien | ad43d2d | 2016-11-21 12:47:33 -0800 | [diff] [blame] | 154 | uint32_t current_slot = module->getCurrentSlot(); |
| 155 | BoolResult is_successful = module->isSlotMarkedSuccessful(current_slot); |
| 156 | LOG(INFO) << "Booting slot " << current_slot << ": isSlotMarkedSuccessful=" |
| 157 | << static_cast<int32_t>(is_successful); |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 158 | |
Connor O'Brien | ad43d2d | 2016-11-21 12:47:33 -0800 | [diff] [blame] | 159 | if (is_successful == BoolResult::FALSE) { |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 160 | // The current slot has not booted successfully. |
Tao Bao | 4f8d217 | 2017-01-13 15:36:32 -0800 | [diff] [blame] | 161 | std::string verity_mode = android::base::GetProperty("ro.boot.veritymode", ""); |
| 162 | if (verity_mode.empty()) { |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 163 | LOG(ERROR) << "Failed to get dm-verity mode."; |
| 164 | return -1; |
Tao Bao | 4f8d217 | 2017-01-13 15:36:32 -0800 | [diff] [blame] | 165 | } else if (android::base::EqualsIgnoreCase(verity_mode, "eio")) { |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 166 | // We shouldn't see verity in EIO mode if the current slot hasn't booted |
| 167 | // successfully before. Therefore, fail the verification when veritymode=eio. |
| 168 | LOG(ERROR) << "Found dm-verity in EIO mode, skip verification."; |
| 169 | return -1; |
Tao Bao | 4f8d217 | 2017-01-13 15:36:32 -0800 | [diff] [blame] | 170 | } else if (verity_mode != "enforcing") { |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 171 | LOG(ERROR) << "Unexpected dm-verity mode : " << verity_mode << ", expecting enforcing."; |
| 172 | return -1; |
| 173 | } else if (!verify_image(CARE_MAP_FILE)) { |
| 174 | LOG(ERROR) << "Failed to verify all blocks in care map file."; |
| 175 | return -1; |
| 176 | } |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 177 | |
Connor O'Brien | ad43d2d | 2016-11-21 12:47:33 -0800 | [diff] [blame] | 178 | CommandResult cr; |
| 179 | module->markBootSuccessful([&cr](CommandResult result) { cr = result; }); |
| 180 | if (!cr.success) { |
| 181 | LOG(ERROR) << "Error marking booted successfully: " << cr.errMsg; |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 182 | return -1; |
| 183 | } |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 184 | LOG(INFO) << "Marked slot " << current_slot << " as booted successfully."; |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 187 | LOG(INFO) << "Leaving update_verifier."; |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 188 | return 0; |
| 189 | } |