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