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