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 | * |
Tianjie Xu | 253e223 | 2017-03-31 22:27:37 +0000 | [diff] [blame^] | 22 | * Update_verifier relies on dm-verity to capture any corruption on the partitions |
| 23 | * being verified. And its behavior varies depending on the dm-verity mode. |
| 24 | * Upon detection of failures: |
| 25 | * enforcing mode: dm-verity reboots the device |
| 26 | * eio mode: dm-verity fails the read and update_verifier reboots the device |
| 27 | * other mode: not supported and update_verifier reboots the device |
| 28 | * |
| 29 | * After a predefined number of failing boot attempts, the bootloader should |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 30 | * mark the slot as unbootable and stops trying. Other dm-verity modes ( |
| 31 | * for example, veritymode=EIO) are not accepted and simply lead to a |
| 32 | * verification failure. |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 33 | * |
| 34 | * The current slot will be marked as having booted successfully if the |
| 35 | * verifier reaches the end after the verification. |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 36 | */ |
| 37 | |
Tianjie Xu | b0ac872 | 2017-01-18 18:41:53 -0800 | [diff] [blame] | 38 | #include <dirent.h> |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 39 | #include <errno.h> |
| 40 | #include <fcntl.h> |
| 41 | #include <stdio.h> |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 42 | #include <string.h> |
Tianjie Xu | 253e223 | 2017-03-31 22:27:37 +0000 | [diff] [blame^] | 43 | #include <unistd.h> |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 44 | |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 45 | #include <string> |
| 46 | #include <vector> |
| 47 | |
| 48 | #include <android-base/file.h> |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 49 | #include <android-base/logging.h> |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 50 | #include <android-base/parseint.h> |
Tao Bao | 4f8d217 | 2017-01-13 15:36:32 -0800 | [diff] [blame] | 51 | #include <android-base/properties.h> |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 52 | #include <android-base/strings.h> |
| 53 | #include <android-base/unique_fd.h> |
Connor O'Brien | ad43d2d | 2016-11-21 12:47:33 -0800 | [diff] [blame] | 54 | #include <android/hardware/boot/1.0/IBootControl.h> |
Tianjie Xu | 253e223 | 2017-03-31 22:27:37 +0000 | [diff] [blame^] | 55 | #include <cutils/android_reboot.h> |
Connor O'Brien | ad43d2d | 2016-11-21 12:47:33 -0800 | [diff] [blame] | 56 | |
| 57 | using android::sp; |
| 58 | using android::hardware::boot::V1_0::IBootControl; |
| 59 | using android::hardware::boot::V1_0::BoolResult; |
| 60 | using android::hardware::boot::V1_0::CommandResult; |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 61 | |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 62 | constexpr auto CARE_MAP_FILE = "/data/ota_package/care_map.txt"; |
Tianjie Xu | b0ac872 | 2017-01-18 18:41:53 -0800 | [diff] [blame] | 63 | constexpr auto DM_PATH_PREFIX = "/sys/block/"; |
| 64 | constexpr auto DM_PATH_SUFFIX = "/dm/name"; |
| 65 | constexpr auto DEV_PATH = "/dev/block/"; |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 66 | constexpr int BLOCKSIZE = 4096; |
| 67 | |
Tianjie Xu | b0ac872 | 2017-01-18 18:41:53 -0800 | [diff] [blame] | 68 | // Find directories in format of "/sys/block/dm-X". |
| 69 | static int dm_name_filter(const dirent* de) { |
| 70 | if (android::base::StartsWith(de->d_name, "dm-")) { |
| 71 | return 1; |
| 72 | } |
| 73 | return 0; |
| 74 | } |
| 75 | |
Tianjie Xu | 253e223 | 2017-03-31 22:27:37 +0000 | [diff] [blame^] | 76 | static bool read_blocks(const std::string& partition, const std::string& range_str) { |
| 77 | CHECK(partition == "system" || partition == "vendor") |
| 78 | << "partition name should be system or vendor" << partition; |
Tianjie Xu | b0ac872 | 2017-01-18 18:41:53 -0800 | [diff] [blame] | 79 | |
| 80 | // Iterate the content of "/sys/block/dm-X/dm/name". If it matches "system" |
| 81 | // (or "vendor"), then dm-X is a dm-wrapped system/vendor partition. |
| 82 | // Afterwards, update_verifier will read every block on the care_map_file of |
| 83 | // "/dev/block/dm-X" to ensure the partition's integrity. |
| 84 | dirent** namelist; |
| 85 | int n = scandir(DM_PATH_PREFIX, &namelist, dm_name_filter, alphasort); |
| 86 | if (n == -1) { |
| 87 | PLOG(ERROR) << "Failed to scan dir " << DM_PATH_PREFIX; |
| 88 | return false; |
| 89 | } |
| 90 | if (n == 0) { |
| 91 | LOG(ERROR) << "dm block device not found for " << partition; |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | std::string dm_block_device; |
| 96 | while (n--) { |
| 97 | std::string path = DM_PATH_PREFIX + std::string(namelist[n]->d_name) + DM_PATH_SUFFIX; |
| 98 | std::string content; |
| 99 | if (!android::base::ReadFileToString(path, &content)) { |
| 100 | PLOG(WARNING) << "Failed to read " << path; |
| 101 | } else if (android::base::Trim(content) == partition) { |
| 102 | dm_block_device = DEV_PATH + std::string(namelist[n]->d_name); |
| 103 | while (n--) { |
| 104 | free(namelist[n]); |
| 105 | } |
| 106 | break; |
| 107 | } |
| 108 | free(namelist[n]); |
| 109 | } |
| 110 | free(namelist); |
| 111 | |
| 112 | if (dm_block_device.empty()) { |
| 113 | LOG(ERROR) << "Failed to find dm block device for " << partition; |
| 114 | return false; |
| 115 | } |
| 116 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(dm_block_device.c_str(), O_RDONLY))); |
Tao Bao | 4f8d217 | 2017-01-13 15:36:32 -0800 | [diff] [blame] | 117 | if (fd.get() == -1) { |
Tianjie Xu | b0ac872 | 2017-01-18 18:41:53 -0800 | [diff] [blame] | 118 | PLOG(ERROR) << "Error reading " << dm_block_device << " for partition " << partition; |
Tao Bao | 4f8d217 | 2017-01-13 15:36:32 -0800 | [diff] [blame] | 119 | return false; |
| 120 | } |
| 121 | |
| 122 | // For block range string, first integer 'count' equals 2 * total number of valid ranges, |
| 123 | // followed by 'count' number comma separated integers. Every two integers reprensent a |
| 124 | // block range with the first number included in range but second number not included. |
| 125 | // For example '4,64536,65343,74149,74150' represents: [64536,65343) and [74149,74150). |
| 126 | std::vector<std::string> ranges = android::base::Split(range_str, ","); |
| 127 | size_t range_count; |
| 128 | bool status = android::base::ParseUint(ranges[0], &range_count); |
| 129 | if (!status || (range_count == 0) || (range_count % 2 != 0) || |
| 130 | (range_count != ranges.size() - 1)) { |
| 131 | LOG(ERROR) << "Error in parsing range string."; |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | size_t blk_count = 0; |
| 136 | for (size_t i = 1; i < ranges.size(); i += 2) { |
| 137 | unsigned int range_start, range_end; |
| 138 | bool parse_status = android::base::ParseUint(ranges[i], &range_start); |
| 139 | parse_status = parse_status && android::base::ParseUint(ranges[i + 1], &range_end); |
| 140 | if (!parse_status || range_start >= range_end) { |
| 141 | LOG(ERROR) << "Invalid range pair " << ranges[i] << ", " << ranges[i + 1]; |
| 142 | return false; |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Tao Bao | 4f8d217 | 2017-01-13 15:36:32 -0800 | [diff] [blame] | 145 | if (lseek64(fd.get(), static_cast<off64_t>(range_start) * BLOCKSIZE, SEEK_SET) == -1) { |
| 146 | PLOG(ERROR) << "lseek to " << range_start << " failed"; |
| 147 | return false; |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Tao Bao | 4f8d217 | 2017-01-13 15:36:32 -0800 | [diff] [blame] | 150 | size_t size = (range_end - range_start) * BLOCKSIZE; |
| 151 | std::vector<uint8_t> buf(size); |
| 152 | if (!android::base::ReadFully(fd.get(), buf.data(), size)) { |
| 153 | PLOG(ERROR) << "Failed to read blocks " << range_start << " to " << range_end; |
| 154 | return false; |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 155 | } |
Tao Bao | 4f8d217 | 2017-01-13 15:36:32 -0800 | [diff] [blame] | 156 | blk_count += (range_end - range_start); |
| 157 | } |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 158 | |
Tianjie Xu | b0ac872 | 2017-01-18 18:41:53 -0800 | [diff] [blame] | 159 | LOG(INFO) << "Finished reading " << blk_count << " blocks on " << dm_block_device; |
Tao Bao | 4f8d217 | 2017-01-13 15:36:32 -0800 | [diff] [blame] | 160 | return true; |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static bool verify_image(const std::string& care_map_name) { |
| 164 | android::base::unique_fd care_map_fd(TEMP_FAILURE_RETRY(open(care_map_name.c_str(), O_RDONLY))); |
| 165 | // If the device is flashed before the current boot, it may not have care_map.txt |
| 166 | // in /data/ota_package. To allow the device to continue booting in this situation, |
| 167 | // we should print a warning and skip the block verification. |
| 168 | if (care_map_fd.get() == -1) { |
| 169 | LOG(WARNING) << "Warning: care map " << care_map_name << " not found."; |
| 170 | return true; |
| 171 | } |
| 172 | // Care map file has four lines (two lines if vendor partition is not present): |
Tianjie Xu | 253e223 | 2017-03-31 22:27:37 +0000 | [diff] [blame^] | 173 | // First line has the block partition name (system/vendor). |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 174 | // Second line holds all ranges of blocks to verify. |
| 175 | // The next two lines have the same format but for vendor partition. |
| 176 | std::string file_content; |
| 177 | if (!android::base::ReadFdToString(care_map_fd.get(), &file_content)) { |
| 178 | LOG(ERROR) << "Error reading care map contents to string."; |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | std::vector<std::string> lines; |
| 183 | lines = android::base::Split(android::base::Trim(file_content), "\n"); |
| 184 | if (lines.size() != 2 && lines.size() != 4) { |
| 185 | LOG(ERROR) << "Invalid lines in care_map: found " << lines.size() |
| 186 | << " lines, expecting 2 or 4 lines."; |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | for (size_t i = 0; i < lines.size(); i += 2) { |
| 191 | if (!read_blocks(lines[i], lines[i+1])) { |
| 192 | return false; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | return true; |
| 197 | } |
| 198 | |
Tianjie Xu | 253e223 | 2017-03-31 22:27:37 +0000 | [diff] [blame^] | 199 | static int reboot_device() { |
| 200 | if (android_reboot(ANDROID_RB_RESTART2, 0, nullptr) == -1) { |
| 201 | LOG(ERROR) << "Failed to reboot."; |
| 202 | return -1; |
| 203 | } |
| 204 | while (true) pause(); |
| 205 | } |
| 206 | |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 207 | int main(int argc, char** argv) { |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 208 | for (int i = 1; i < argc; i++) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 209 | LOG(INFO) << "Started with arg " << i << ": " << argv[i]; |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Chris Phoenix | 0157c78 | 2017-01-20 11:34:00 -0800 | [diff] [blame] | 212 | sp<IBootControl> module = IBootControl::getService(); |
Connor O'Brien | ad43d2d | 2016-11-21 12:47:33 -0800 | [diff] [blame] | 213 | if (module == nullptr) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 214 | LOG(ERROR) << "Error getting bootctrl module."; |
Tianjie Xu | 253e223 | 2017-03-31 22:27:37 +0000 | [diff] [blame^] | 215 | return reboot_device(); |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Connor O'Brien | ad43d2d | 2016-11-21 12:47:33 -0800 | [diff] [blame] | 218 | uint32_t current_slot = module->getCurrentSlot(); |
| 219 | BoolResult is_successful = module->isSlotMarkedSuccessful(current_slot); |
| 220 | LOG(INFO) << "Booting slot " << current_slot << ": isSlotMarkedSuccessful=" |
| 221 | << static_cast<int32_t>(is_successful); |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 222 | |
Connor O'Brien | ad43d2d | 2016-11-21 12:47:33 -0800 | [diff] [blame] | 223 | if (is_successful == BoolResult::FALSE) { |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 224 | // The current slot has not booted successfully. |
Tao Bao | db57f0d | 2017-03-10 14:21:25 -0800 | [diff] [blame] | 225 | |
| 226 | #ifdef PRODUCT_SUPPORTS_VERITY |
Tao Bao | 4f8d217 | 2017-01-13 15:36:32 -0800 | [diff] [blame] | 227 | std::string verity_mode = android::base::GetProperty("ro.boot.veritymode", ""); |
| 228 | if (verity_mode.empty()) { |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 229 | LOG(ERROR) << "Failed to get dm-verity mode."; |
Tianjie Xu | 253e223 | 2017-03-31 22:27:37 +0000 | [diff] [blame^] | 230 | return reboot_device(); |
Tao Bao | 4f8d217 | 2017-01-13 15:36:32 -0800 | [diff] [blame] | 231 | } else if (android::base::EqualsIgnoreCase(verity_mode, "eio")) { |
Tianjie Xu | 253e223 | 2017-03-31 22:27:37 +0000 | [diff] [blame^] | 232 | // We shouldn't see verity in EIO mode if the current slot hasn't booted successfully before. |
| 233 | // Continue the verification until we fail to read some blocks. |
| 234 | LOG(WARNING) << "Found dm-verity in EIO mode."; |
Tao Bao | 4f8d217 | 2017-01-13 15:36:32 -0800 | [diff] [blame] | 235 | } else if (verity_mode != "enforcing") { |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 236 | LOG(ERROR) << "Unexpected dm-verity mode : " << verity_mode << ", expecting enforcing."; |
Tianjie Xu | 253e223 | 2017-03-31 22:27:37 +0000 | [diff] [blame^] | 237 | return reboot_device(); |
| 238 | } |
| 239 | |
| 240 | if (!verify_image(CARE_MAP_FILE)) { |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 241 | LOG(ERROR) << "Failed to verify all blocks in care map file."; |
Tianjie Xu | 253e223 | 2017-03-31 22:27:37 +0000 | [diff] [blame^] | 242 | return reboot_device(); |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 243 | } |
Tao Bao | db57f0d | 2017-03-10 14:21:25 -0800 | [diff] [blame] | 244 | #else |
| 245 | LOG(WARNING) << "dm-verity not enabled; marking without verification."; |
| 246 | #endif |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 247 | |
Connor O'Brien | ad43d2d | 2016-11-21 12:47:33 -0800 | [diff] [blame] | 248 | CommandResult cr; |
| 249 | module->markBootSuccessful([&cr](CommandResult result) { cr = result; }); |
| 250 | if (!cr.success) { |
| 251 | LOG(ERROR) << "Error marking booted successfully: " << cr.errMsg; |
Tianjie Xu | 253e223 | 2017-03-31 22:27:37 +0000 | [diff] [blame^] | 252 | return reboot_device(); |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 253 | } |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 254 | LOG(INFO) << "Marked slot " << current_slot << " as booted successfully."; |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 257 | LOG(INFO) << "Leaving update_verifier."; |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 258 | return 0; |
| 259 | } |