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 | 03ca853 | 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. |
| 31 | * |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 32 | */ |
| 33 | |
Tianjie Xu | 03ca853 | 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 | 03ca853 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 39 | #include <string> |
| 40 | #include <vector> |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 41 | |
Tianjie Xu | 03ca853 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 42 | #include <android-base/file.h> |
| 43 | #include <android-base/parseint.h> |
| 44 | #include <android-base/strings.h> |
| 45 | #include <android-base/unique_fd.h> |
| 46 | #include <cutils/properties.h> |
| 47 | #include <hardware/boot_control.h> |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 48 | #define LOG_TAG "update_verifier" |
Tao Bao | 740e01e | 2015-12-07 17:04:58 -0800 | [diff] [blame] | 49 | #include <log/log.h> |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 50 | |
Tianjie Xu | 03ca853 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 51 | constexpr auto CARE_MAP_FILE = "/data/ota_package/care_map.txt"; |
| 52 | constexpr int BLOCKSIZE = 4096; |
| 53 | |
| 54 | static bool read_blocks(const std::string& blk_device_prefix, const std::string& range_str) { |
| 55 | char slot_suffix[PROPERTY_VALUE_MAX]; |
| 56 | property_get("ro.boot.slot_suffix", slot_suffix, ""); |
| 57 | std::string blk_device = blk_device_prefix + std::string(slot_suffix); |
| 58 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(blk_device.c_str(), O_RDONLY))); |
| 59 | if (fd.get() == -1) { |
| 60 | SLOGE("Error reading partition %s: %s\n", blk_device.c_str(), strerror(errno)); |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | // For block range string, first integer 'count' equals 2 * total number of valid ranges, |
| 65 | // followed by 'count' number comma separated integers. Every two integers reprensent a |
| 66 | // block range with the first number included in range but second number not included. |
| 67 | // For example '4,64536,65343,74149,74150' represents: [64536,65343) and [74149,74150). |
| 68 | std::vector<std::string> ranges = android::base::Split(range_str, ","); |
| 69 | size_t range_count; |
| 70 | bool status = android::base::ParseUint(ranges[0].c_str(), &range_count); |
| 71 | if (!status || (range_count == 0) || (range_count % 2 != 0) || |
| 72 | (range_count != ranges.size()-1)) { |
| 73 | SLOGE("Error in parsing range string.\n"); |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | size_t blk_count = 0; |
| 78 | for (size_t i = 1; i < ranges.size(); i += 2) { |
| 79 | unsigned int range_start, range_end; |
| 80 | bool parse_status = android::base::ParseUint(ranges[i].c_str(), &range_start); |
| 81 | parse_status = parse_status && android::base::ParseUint(ranges[i+1].c_str(), &range_end); |
| 82 | if (!parse_status || range_start >= range_end) { |
| 83 | SLOGE("Invalid range pair %s, %s.\n", ranges[i].c_str(), ranges[i+1].c_str()); |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | if (lseek64(fd.get(), static_cast<off64_t>(range_start) * BLOCKSIZE, SEEK_SET) == -1) { |
| 88 | SLOGE("lseek to %u failed: %s.\n", range_start, strerror(errno)); |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | size_t size = (range_end - range_start) * BLOCKSIZE; |
| 93 | std::vector<uint8_t> buf(size); |
| 94 | if (!android::base::ReadFully(fd.get(), buf.data(), size)) { |
| 95 | SLOGE("Failed to read blocks %u to %u: %s.\n", range_start, range_end, |
| 96 | strerror(errno)); |
| 97 | return false; |
| 98 | } |
| 99 | blk_count += (range_end - range_start); |
| 100 | } |
| 101 | |
| 102 | SLOGI("Finished reading %zu blocks on %s.\n", blk_count, blk_device.c_str()); |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | static bool verify_image(const std::string& care_map_name) { |
| 107 | android::base::unique_fd care_map_fd(TEMP_FAILURE_RETRY(open(care_map_name.c_str(), O_RDONLY))); |
Tianjie Xu | da654af | 2016-07-15 16:15:42 -0700 | [diff] [blame] | 108 | // If the device is flashed before the current boot, it may not have care_map.txt |
| 109 | // in /data/ota_package. To allow the device to continue booting in this situation, |
| 110 | // we should print a warning and skip the block verification. |
Tianjie Xu | 03ca853 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 111 | if (care_map_fd.get() == -1) { |
Tianjie Xu | da654af | 2016-07-15 16:15:42 -0700 | [diff] [blame] | 112 | SLOGI("Warning: care map %s not found.\n", care_map_name.c_str()); |
| 113 | return true; |
Tianjie Xu | 03ca853 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 114 | } |
| 115 | // Care map file has four lines (two lines if vendor partition is not present): |
| 116 | // First line has the block device name, e.g./dev/block/.../by-name/system. |
| 117 | // Second line holds all ranges of blocks to verify. |
| 118 | // The next two lines have the same format but for vendor partition. |
| 119 | std::string file_content; |
| 120 | if (!android::base::ReadFdToString(care_map_fd.get(), &file_content)) { |
| 121 | SLOGE("Error reading care map contents to string.\n"); |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | std::vector<std::string> lines; |
| 126 | lines = android::base::Split(android::base::Trim(file_content), "\n"); |
| 127 | if (lines.size() != 2 && lines.size() != 4) { |
| 128 | SLOGE("Invalid lines in care_map: found %zu lines, expecting 2 or 4 lines.\n", |
| 129 | lines.size()); |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | for (size_t i = 0; i < lines.size(); i += 2) { |
| 134 | if (!read_blocks(lines[i], lines[i+1])) { |
| 135 | return false; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | return true; |
| 140 | } |
| 141 | |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 142 | int main(int argc, char** argv) { |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 143 | for (int i = 1; i < argc; i++) { |
Tao Bao | 740e01e | 2015-12-07 17:04:58 -0800 | [diff] [blame] | 144 | SLOGI("Started with arg %d: %s\n", i, argv[i]); |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | const hw_module_t* hw_module; |
| 148 | if (hw_get_module("bootctrl", &hw_module) != 0) { |
Tao Bao | 740e01e | 2015-12-07 17:04:58 -0800 | [diff] [blame] | 149 | SLOGE("Error getting bootctrl module.\n"); |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 150 | return -1; |
| 151 | } |
| 152 | |
| 153 | boot_control_module_t* module = reinterpret_cast<boot_control_module_t*>( |
| 154 | const_cast<hw_module_t*>(hw_module)); |
| 155 | module->init(module); |
| 156 | |
| 157 | unsigned current_slot = module->getCurrentSlot(module); |
Tao Bao | 612161e | 2015-12-09 14:41:40 -0800 | [diff] [blame] | 158 | int is_successful= module->isSlotMarkedSuccessful(module, current_slot); |
| 159 | SLOGI("Booting slot %u: isSlotMarkedSuccessful=%d\n", current_slot, is_successful); |
Tao Bao | 612161e | 2015-12-09 14:41:40 -0800 | [diff] [blame] | 160 | if (is_successful == 0) { |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 161 | // The current slot has not booted successfully. |
Tianjie Xu | 03ca853 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 162 | char verity_mode[PROPERTY_VALUE_MAX]; |
| 163 | if (property_get("ro.boot.veritymode", verity_mode, "") == -1) { |
| 164 | SLOGE("Failed to get dm-verity mode"); |
| 165 | return -1; |
Tianjie Xu | 4bbe0c9 | 2016-07-14 16:00:36 -0700 | [diff] [blame] | 166 | } else if (strcasecmp(verity_mode, "eio") == 0) { |
Tianjie Xu | 03ca853 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 167 | // We shouldn't see verity in EIO mode if the current slot hasn't booted |
Tianjie Xu | 4bbe0c9 | 2016-07-14 16:00:36 -0700 | [diff] [blame] | 168 | // successfully before. Therefore, fail the verification when veritymode=eio. |
Tianjie Xu | 03ca853 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 169 | SLOGE("Found dm-verity in EIO mode, skip verification."); |
| 170 | return -1; |
| 171 | } else if (strcmp(verity_mode, "enforcing") != 0) { |
| 172 | SLOGE("Unexpected dm-verity mode : %s, expecting enforcing.", verity_mode); |
| 173 | return -1; |
| 174 | } else if (!verify_image(CARE_MAP_FILE)) { |
| 175 | SLOGE("Failed to verify all blocks in care map file.\n"); |
| 176 | return -1; |
| 177 | } |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 178 | |
| 179 | int ret = module->markBootSuccessful(module); |
| 180 | if (ret != 0) { |
Tao Bao | 740e01e | 2015-12-07 17:04:58 -0800 | [diff] [blame] | 181 | SLOGE("Error marking booted successfully: %s\n", strerror(-ret)); |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 182 | return -1; |
| 183 | } |
Tao Bao | 740e01e | 2015-12-07 17:04:58 -0800 | [diff] [blame] | 184 | SLOGI("Marked slot %u as booted successfully.\n", current_slot); |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Tao Bao | 740e01e | 2015-12-07 17:04:58 -0800 | [diff] [blame] | 187 | SLOGI("Leaving update_verifier.\n"); |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 188 | return 0; |
| 189 | } |