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 | /* |
Tao Bao | 1cc0351 | 2018-04-18 17:10:49 -0700 | [diff] [blame] | 18 | * update_verifier verifies the integrity of the partitions after an A/B OTA update. It gets invoked |
| 19 | * by init, and will only perform the verification if it's the first boot post an A/B OTA update |
| 20 | * (https://source.android.com/devices/tech/ota/ab/#after_reboot). |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 21 | * |
Tao Bao | 1cc0351 | 2018-04-18 17:10:49 -0700 | [diff] [blame] | 22 | * update_verifier relies on device-mapper-verity (dm-verity) to capture any corruption on the |
| 23 | * partitions being verified (https://source.android.com/security/verifiedboot). The verification |
| 24 | * will be skipped, if dm-verity is not enabled on the device. |
| 25 | * |
| 26 | * Upon detecting verification failures, the device will be rebooted, although the trigger of the |
| 27 | * reboot depends on the dm-verity mode. |
Tianjie Xu | 3958a95 | 2017-03-01 15:31:25 -0800 | [diff] [blame] | 28 | * enforcing mode: dm-verity reboots the device |
| 29 | * eio mode: dm-verity fails the read and update_verifier reboots the device |
| 30 | * other mode: not supported and update_verifier reboots the device |
| 31 | * |
Tao Bao | 1cc0351 | 2018-04-18 17:10:49 -0700 | [diff] [blame] | 32 | * All these reboots prevent the device from booting into a known corrupt state. If the device |
| 33 | * continuously fails to boot into the new slot, the bootloader should mark the slot as unbootable |
| 34 | * and trigger a fallback to the old slot. |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 35 | * |
Tao Bao | 1cc0351 | 2018-04-18 17:10:49 -0700 | [diff] [blame] | 36 | * The current slot will be marked as having booted successfully if the verifier reaches the end |
| 37 | * after the verification. |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 38 | */ |
| 39 | |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 40 | #include "update_verifier/update_verifier.h" |
| 41 | |
Tianjie Xu | b0ac872 | 2017-01-18 18:41:53 -0800 | [diff] [blame] | 42 | #include <dirent.h> |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 43 | #include <errno.h> |
| 44 | #include <fcntl.h> |
Tao Bao | c89c394 | 2019-03-13 15:30:13 -0700 | [diff] [blame] | 45 | #include <stdint.h> |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 46 | #include <stdio.h> |
Tao Bao | c89c394 | 2019-03-13 15:30:13 -0700 | [diff] [blame] | 47 | #include <stdlib.h> |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 48 | #include <string.h> |
Tianjie Xu | 3958a95 | 2017-03-01 15:31:25 -0800 | [diff] [blame] | 49 | #include <unistd.h> |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 50 | |
Tianjie Xu | 8fa8f0b | 2017-04-27 11:47:35 -0700 | [diff] [blame] | 51 | #include <algorithm> |
Wei Wang | 5226f47 | 2017-08-02 10:27:31 -0700 | [diff] [blame] | 52 | #include <future> |
Tao Bao | c89c394 | 2019-03-13 15:30:13 -0700 | [diff] [blame] | 53 | #include <thread> |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 54 | |
Kelvin Zhang | fa554fe | 2022-04-01 14:17:10 -0700 | [diff] [blame^] | 55 | #include <BootControlClient.h> |
Akilesh Kailash | dc1f6da | 2022-03-23 18:53:52 +0000 | [diff] [blame] | 56 | #include <android-base/chrono_utils.h> |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 57 | #include <android-base/file.h> |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 58 | #include <android-base/logging.h> |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 59 | #include <android-base/parseint.h> |
Tao Bao | 4f8d217 | 2017-01-13 15:36:32 -0800 | [diff] [blame] | 60 | #include <android-base/properties.h> |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 61 | #include <android-base/strings.h> |
| 62 | #include <android-base/unique_fd.h> |
Tao Bao | c89c394 | 2019-03-13 15:30:13 -0700 | [diff] [blame] | 63 | #include <android/os/IVold.h> |
Daniel Rosenberg | 15f22bd | 2019-01-22 19:57:28 -0800 | [diff] [blame] | 64 | #include <binder/BinderService.h> |
| 65 | #include <binder/Status.h> |
Tianjie Xu | 3958a95 | 2017-03-01 15:31:25 -0800 | [diff] [blame] | 66 | #include <cutils/android_reboot.h> |
Connor O'Brien | ad43d2d | 2016-11-21 12:47:33 -0800 | [diff] [blame] | 67 | |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 68 | #include "care_map.pb.h" |
Tao Bao | 160514b | 2017-11-04 00:08:08 -0700 | [diff] [blame] | 69 | |
xunchang | aaa6103 | 2019-03-13 14:21:48 -0700 | [diff] [blame] | 70 | // TODO(xunchang) remove the prefix and use a default path instead. |
Tianjie Xu | 9eed65e | 2018-09-14 12:52:02 -0700 | [diff] [blame] | 71 | constexpr const char* kDefaultCareMapPrefix = "/data/ota_package/care_map"; |
| 72 | |
Tianjie Xu | b0ac872 | 2017-01-18 18:41:53 -0800 | [diff] [blame] | 73 | // Find directories in format of "/sys/block/dm-X". |
| 74 | static int dm_name_filter(const dirent* de) { |
| 75 | if (android::base::StartsWith(de->d_name, "dm-")) { |
| 76 | return 1; |
| 77 | } |
| 78 | return 0; |
| 79 | } |
| 80 | |
Tianjie Xu | 9eed65e | 2018-09-14 12:52:02 -0700 | [diff] [blame] | 81 | UpdateVerifier::UpdateVerifier() |
| 82 | : care_map_prefix_(kDefaultCareMapPrefix), |
| 83 | property_reader_([](const std::string& id) { return android::base::GetProperty(id, ""); }) {} |
| 84 | |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 85 | // Iterate the content of "/sys/block/dm-X/dm/name" and find all the dm-wrapped block devices. |
| 86 | // We will later read all the ("cared") blocks from "/dev/block/dm-X" to ensure the target |
| 87 | // partition's integrity. |
| 88 | std::map<std::string, std::string> UpdateVerifier::FindDmPartitions() { |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 89 | static constexpr auto DM_PATH_PREFIX = "/sys/block/"; |
Kelvin Zhang | fa554fe | 2022-04-01 14:17:10 -0700 | [diff] [blame^] | 90 | dirent** namelist = nullptr; |
Tianjie Xu | b0ac872 | 2017-01-18 18:41:53 -0800 | [diff] [blame] | 91 | int n = scandir(DM_PATH_PREFIX, &namelist, dm_name_filter, alphasort); |
| 92 | if (n == -1) { |
| 93 | PLOG(ERROR) << "Failed to scan dir " << DM_PATH_PREFIX; |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 94 | return {}; |
Tianjie Xu | b0ac872 | 2017-01-18 18:41:53 -0800 | [diff] [blame] | 95 | } |
| 96 | if (n == 0) { |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 97 | LOG(ERROR) << "No dm block device found."; |
| 98 | return {}; |
Tianjie Xu | b0ac872 | 2017-01-18 18:41:53 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 101 | static constexpr auto DM_PATH_SUFFIX = "/dm/name"; |
| 102 | static constexpr auto DEV_PATH = "/dev/block/"; |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 103 | std::map<std::string, std::string> dm_block_devices; |
Tianjie Xu | b0ac872 | 2017-01-18 18:41:53 -0800 | [diff] [blame] | 104 | while (n--) { |
| 105 | std::string path = DM_PATH_PREFIX + std::string(namelist[n]->d_name) + DM_PATH_SUFFIX; |
| 106 | std::string content; |
| 107 | if (!android::base::ReadFileToString(path, &content)) { |
| 108 | PLOG(WARNING) << "Failed to read " << path; |
David Zeuthen | 8ed9738 | 2017-05-08 13:41:28 -0400 | [diff] [blame] | 109 | } else { |
| 110 | std::string dm_block_name = android::base::Trim(content); |
David Zeuthen | 8ed9738 | 2017-05-08 13:41:28 -0400 | [diff] [blame] | 111 | // AVB is using 'vroot' for the root block device but we're expecting 'system'. |
| 112 | if (dm_block_name == "vroot") { |
| 113 | dm_block_name = "system"; |
David Anderson | c52663c | 2019-04-01 14:37:33 -0700 | [diff] [blame] | 114 | } else if (android::base::EndsWith(dm_block_name, "-verity")) { |
| 115 | auto npos = dm_block_name.rfind("-verity"); |
| 116 | dm_block_name = dm_block_name.substr(0, npos); |
| 117 | } else if (!android::base::GetProperty("ro.boot.avb_version", "").empty()) { |
| 118 | // Verified Boot 1.0 doesn't add a -verity suffix. On AVB 2 devices, |
| 119 | // if DAP is enabled, then a -verity suffix must be used to |
| 120 | // differentiate between dm-linear and dm-verity devices. If we get |
| 121 | // here, we're AVB 2 and looking at a non-verity partition. |
| 122 | continue; |
Tianjie Xu | b0ac872 | 2017-01-18 18:41:53 -0800 | [diff] [blame] | 123 | } |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 124 | |
| 125 | dm_block_devices.emplace(dm_block_name, DEV_PATH + std::string(namelist[n]->d_name)); |
Tianjie Xu | b0ac872 | 2017-01-18 18:41:53 -0800 | [diff] [blame] | 126 | } |
| 127 | free(namelist[n]); |
| 128 | } |
| 129 | free(namelist); |
| 130 | |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 131 | return dm_block_devices; |
| 132 | } |
Tao Bao | 4f8d217 | 2017-01-13 15:36:32 -0800 | [diff] [blame] | 133 | |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 134 | bool UpdateVerifier::ReadBlocks(const std::string partition_name, |
| 135 | const std::string& dm_block_device, const RangeSet& ranges) { |
Tao Bao | 160514b | 2017-11-04 00:08:08 -0700 | [diff] [blame] | 136 | // RangeSet::Split() splits the ranges into multiple groups with same number of blocks (except for |
| 137 | // the last group). |
| 138 | size_t thread_num = std::thread::hardware_concurrency() ?: 4; |
| 139 | std::vector<RangeSet> groups = ranges.Split(thread_num); |
Tao Bao | 4f8d217 | 2017-01-13 15:36:32 -0800 | [diff] [blame] | 140 | |
Wei Wang | 5226f47 | 2017-08-02 10:27:31 -0700 | [diff] [blame] | 141 | std::vector<std::future<bool>> threads; |
Tao Bao | 160514b | 2017-11-04 00:08:08 -0700 | [diff] [blame] | 142 | for (const auto& group : groups) { |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 143 | auto thread_func = [&group, &dm_block_device, &partition_name]() { |
Wei Wang | 5226f47 | 2017-08-02 10:27:31 -0700 | [diff] [blame] | 144 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(dm_block_device.c_str(), O_RDONLY))); |
| 145 | if (fd.get() == -1) { |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 146 | PLOG(ERROR) << "Error reading " << dm_block_device << " for partition " << partition_name; |
Tianjie Xu | 8fa8f0b | 2017-04-27 11:47:35 -0700 | [diff] [blame] | 147 | return false; |
| 148 | } |
Wei Wang | 5226f47 | 2017-08-02 10:27:31 -0700 | [diff] [blame] | 149 | |
Tao Bao | 160514b | 2017-11-04 00:08:08 -0700 | [diff] [blame] | 150 | static constexpr size_t kBlockSize = 4096; |
| 151 | std::vector<uint8_t> buf(1024 * kBlockSize); |
Wei Wang | 5226f47 | 2017-08-02 10:27:31 -0700 | [diff] [blame] | 152 | |
Tao Bao | 160514b | 2017-11-04 00:08:08 -0700 | [diff] [blame] | 153 | size_t block_count = 0; |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 154 | for (const auto& [range_start, range_end] : group) { |
Wei Wang | 5226f47 | 2017-08-02 10:27:31 -0700 | [diff] [blame] | 155 | if (lseek64(fd.get(), static_cast<off64_t>(range_start) * kBlockSize, SEEK_SET) == -1) { |
| 156 | PLOG(ERROR) << "lseek to " << range_start << " failed"; |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | size_t remain = (range_end - range_start) * kBlockSize; |
| 161 | while (remain > 0) { |
| 162 | size_t to_read = std::min(remain, 1024 * kBlockSize); |
| 163 | if (!android::base::ReadFully(fd.get(), buf.data(), to_read)) { |
| 164 | PLOG(ERROR) << "Failed to read blocks " << range_start << " to " << range_end; |
| 165 | return false; |
| 166 | } |
| 167 | remain -= to_read; |
| 168 | } |
Tao Bao | 160514b | 2017-11-04 00:08:08 -0700 | [diff] [blame] | 169 | block_count += (range_end - range_start); |
Wei Wang | 5226f47 | 2017-08-02 10:27:31 -0700 | [diff] [blame] | 170 | } |
Tao Bao | 160514b | 2017-11-04 00:08:08 -0700 | [diff] [blame] | 171 | LOG(INFO) << "Finished reading " << block_count << " blocks on " << dm_block_device; |
Wei Wang | 5226f47 | 2017-08-02 10:27:31 -0700 | [diff] [blame] | 172 | return true; |
| 173 | }; |
| 174 | |
| 175 | threads.emplace_back(std::async(std::launch::async, thread_func)); |
Tao Bao | 4f8d217 | 2017-01-13 15:36:32 -0800 | [diff] [blame] | 176 | } |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 177 | |
Wei Wang | 5226f47 | 2017-08-02 10:27:31 -0700 | [diff] [blame] | 178 | bool ret = true; |
| 179 | for (auto& t : threads) { |
| 180 | ret = t.get() && ret; |
| 181 | } |
| 182 | LOG(INFO) << "Finished reading blocks on " << dm_block_device << " with " << thread_num |
| 183 | << " threads."; |
| 184 | return ret; |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Akilesh Kailash | dc1f6da | 2022-03-23 18:53:52 +0000 | [diff] [blame] | 187 | bool UpdateVerifier::CheckVerificationStatus() { |
| 188 | auto client = |
| 189 | android::snapshot::SnapuserdClient::Connect(android::snapshot::kSnapuserdSocket, 5s); |
| 190 | if (!client) { |
| 191 | LOG(ERROR) << "Unable to connect to snapuserd"; |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | return client->QueryUpdateVerification(); |
| 196 | } |
| 197 | |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 198 | bool UpdateVerifier::VerifyPartitions() { |
Akilesh Kailash | dc1f6da | 2022-03-23 18:53:52 +0000 | [diff] [blame] | 199 | const bool userspace_snapshots = |
| 200 | android::base::GetBoolProperty("ro.virtual_ab.userspace.snapshots.enabled", false); |
| 201 | |
| 202 | if (userspace_snapshots && CheckVerificationStatus()) { |
| 203 | LOG(INFO) << "Partitions verified by snapuserd daemon"; |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | LOG(INFO) << "Partitions not verified by snapuserd daemon"; |
| 208 | |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 209 | auto dm_block_devices = FindDmPartitions(); |
| 210 | if (dm_block_devices.empty()) { |
| 211 | LOG(ERROR) << "No dm-enabled block device is found."; |
Tao Bao | 5a1dee0 | 2017-07-21 15:15:31 -0700 | [diff] [blame] | 212 | return false; |
| 213 | } |
| 214 | |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 215 | for (const auto& [partition_name, ranges] : partition_map_) { |
| 216 | if (dm_block_devices.find(partition_name) == dm_block_devices.end()) { |
| 217 | LOG(ERROR) << "Failed to find dm block device for " << partition_name; |
| 218 | return false; |
Tao Bao | 5a1dee0 | 2017-07-21 15:15:31 -0700 | [diff] [blame] | 219 | } |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 220 | |
| 221 | if (!ReadBlocks(partition_name, dm_block_devices.at(partition_name), ranges)) { |
Tao Bao | 5a1dee0 | 2017-07-21 15:15:31 -0700 | [diff] [blame] | 222 | return false; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | return true; |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Tianjie Xu | 9eed65e | 2018-09-14 12:52:02 -0700 | [diff] [blame] | 229 | bool UpdateVerifier::ParseCareMap() { |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 230 | partition_map_.clear(); |
| 231 | |
Tianjie Xu | 9eed65e | 2018-09-14 12:52:02 -0700 | [diff] [blame] | 232 | std::string care_map_name = care_map_prefix_ + ".pb"; |
| 233 | if (access(care_map_name.c_str(), R_OK) == -1) { |
xunchang | aaa6103 | 2019-03-13 14:21:48 -0700 | [diff] [blame] | 234 | LOG(ERROR) << care_map_name << " doesn't exist"; |
| 235 | return false; |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 238 | android::base::unique_fd care_map_fd(TEMP_FAILURE_RETRY(open(care_map_name.c_str(), O_RDONLY))); |
| 239 | // If the device is flashed before the current boot, it may not have care_map.txt in |
| 240 | // /data/ota_package. To allow the device to continue booting in this situation, we should |
| 241 | // print a warning and skip the block verification. |
| 242 | if (care_map_fd.get() == -1) { |
| 243 | PLOG(WARNING) << "Failed to open " << care_map_name; |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 244 | return false; |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | std::string file_content; |
| 248 | if (!android::base::ReadFdToString(care_map_fd.get(), &file_content)) { |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 249 | PLOG(WARNING) << "Failed to read " << care_map_name; |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 250 | return false; |
| 251 | } |
| 252 | |
| 253 | if (file_content.empty()) { |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 254 | LOG(WARNING) << "Unexpected empty care map"; |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 255 | return false; |
| 256 | } |
| 257 | |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 258 | recovery_update_verifier::CareMap care_map; |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 259 | if (!care_map.ParseFromString(file_content)) { |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 260 | LOG(WARNING) << "Failed to parse " << care_map_name << " in protobuf format."; |
| 261 | return false; |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | for (const auto& partition : care_map.partitions()) { |
| 265 | if (partition.name().empty()) { |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 266 | LOG(WARNING) << "Unexpected empty partition name."; |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 267 | return false; |
| 268 | } |
| 269 | if (partition.ranges().empty()) { |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 270 | LOG(WARNING) << "Unexpected block ranges for partition " << partition.name(); |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 271 | return false; |
| 272 | } |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 273 | RangeSet ranges = RangeSet::Parse(partition.ranges()); |
| 274 | if (!ranges) { |
| 275 | LOG(WARNING) << "Error parsing RangeSet string " << partition.ranges(); |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 276 | return false; |
| 277 | } |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 278 | |
Tianjie Xu | 9eed65e | 2018-09-14 12:52:02 -0700 | [diff] [blame] | 279 | // Continues to check other partitions if there is a fingerprint mismatch. |
| 280 | if (partition.id().empty() || partition.id() == "unknown") { |
| 281 | LOG(WARNING) << "Skip reading partition " << partition.name() |
| 282 | << ": property_id is not provided to get fingerprint."; |
| 283 | continue; |
| 284 | } |
| 285 | |
| 286 | std::string fingerprint = property_reader_(partition.id()); |
| 287 | if (fingerprint != partition.fingerprint()) { |
| 288 | LOG(WARNING) << "Skip reading partition " << partition.name() << ": fingerprint " |
| 289 | << fingerprint << " doesn't match the expected value " |
| 290 | << partition.fingerprint(); |
| 291 | continue; |
| 292 | } |
| 293 | |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 294 | partition_map_.emplace(partition.name(), ranges); |
| 295 | } |
| 296 | |
| 297 | if (partition_map_.empty()) { |
| 298 | LOG(WARNING) << "No partition to verify"; |
| 299 | return false; |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | return true; |
| 303 | } |
| 304 | |
Tianjie Xu | 9eed65e | 2018-09-14 12:52:02 -0700 | [diff] [blame] | 305 | void UpdateVerifier::set_care_map_prefix(const std::string& prefix) { |
| 306 | care_map_prefix_ = prefix; |
| 307 | } |
| 308 | |
| 309 | void UpdateVerifier::set_property_reader( |
| 310 | const std::function<std::string(const std::string&)>& property_reader) { |
| 311 | property_reader_ = property_reader; |
| 312 | } |
| 313 | |
Tianjie Xu | 3958a95 | 2017-03-01 15:31:25 -0800 | [diff] [blame] | 314 | static int reboot_device() { |
| 315 | if (android_reboot(ANDROID_RB_RESTART2, 0, nullptr) == -1) { |
| 316 | LOG(ERROR) << "Failed to reboot."; |
| 317 | return -1; |
| 318 | } |
| 319 | while (true) pause(); |
| 320 | } |
| 321 | |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 322 | int update_verifier(int argc, char** argv) { |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 323 | for (int i = 1; i < argc; i++) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 324 | LOG(INFO) << "Started with arg " << i << ": " << argv[i]; |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 325 | } |
| 326 | |
Kelvin Zhang | fa554fe | 2022-04-01 14:17:10 -0700 | [diff] [blame^] | 327 | const auto module = android::hal::BootControlClient::WaitForService(); |
Connor O'Brien | ad43d2d | 2016-11-21 12:47:33 -0800 | [diff] [blame] | 328 | if (module == nullptr) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 329 | LOG(ERROR) << "Error getting bootctrl module."; |
Tianjie Xu | 3958a95 | 2017-03-01 15:31:25 -0800 | [diff] [blame] | 330 | return reboot_device(); |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 331 | } |
| 332 | |
Kelvin Zhang | fa554fe | 2022-04-01 14:17:10 -0700 | [diff] [blame^] | 333 | uint32_t current_slot = module->GetCurrentSlot(); |
| 334 | const auto is_successful = module->IsSlotMarkedSuccessful(current_slot); |
| 335 | if (!is_successful.has_value()) { |
| 336 | LOG(INFO) << "Booting slot " << current_slot << " failed"; |
| 337 | } else { |
| 338 | LOG(INFO) << "Booting slot " << current_slot |
| 339 | << ": isSlotMarkedSuccessful=" << is_successful.value(); |
| 340 | } |
| 341 | if (is_successful.has_value() && !is_successful.value()) { |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 342 | // The current slot has not booted successfully. |
Tao Bao | db57f0d | 2017-03-10 14:21:25 -0800 | [diff] [blame] | 343 | |
David Zeuthen | 1a0929c | 2017-08-07 18:47:27 -0400 | [diff] [blame] | 344 | bool skip_verification = false; |
Tao Bao | 4f8d217 | 2017-01-13 15:36:32 -0800 | [diff] [blame] | 345 | std::string verity_mode = android::base::GetProperty("ro.boot.veritymode", ""); |
| 346 | if (verity_mode.empty()) { |
Tao Bao | 1cc0351 | 2018-04-18 17:10:49 -0700 | [diff] [blame] | 347 | // Skip the verification if ro.boot.veritymode property is not set. This could be a result |
| 348 | // that device doesn't support dm-verity, or has disabled that. |
| 349 | LOG(WARNING) << "dm-verity not enabled; marking without verification."; |
David Zeuthen | 1a0929c | 2017-08-07 18:47:27 -0400 | [diff] [blame] | 350 | skip_verification = true; |
Tao Bao | 4f8d217 | 2017-01-13 15:36:32 -0800 | [diff] [blame] | 351 | } else if (android::base::EqualsIgnoreCase(verity_mode, "eio")) { |
Tianjie Xu | 3958a95 | 2017-03-01 15:31:25 -0800 | [diff] [blame] | 352 | // We shouldn't see verity in EIO mode if the current slot hasn't booted successfully before. |
| 353 | // Continue the verification until we fail to read some blocks. |
| 354 | LOG(WARNING) << "Found dm-verity in EIO mode."; |
David Zeuthen | 1a0929c | 2017-08-07 18:47:27 -0400 | [diff] [blame] | 355 | } else if (android::base::EqualsIgnoreCase(verity_mode, "disabled")) { |
| 356 | LOG(WARNING) << "dm-verity in disabled mode; marking without verification."; |
| 357 | skip_verification = true; |
Tao Bao | 4f8d217 | 2017-01-13 15:36:32 -0800 | [diff] [blame] | 358 | } else if (verity_mode != "enforcing") { |
Tao Bao | 1cc0351 | 2018-04-18 17:10:49 -0700 | [diff] [blame] | 359 | LOG(ERROR) << "Unexpected dm-verity mode: " << verity_mode << ", expecting enforcing."; |
Tianjie Xu | 3958a95 | 2017-03-01 15:31:25 -0800 | [diff] [blame] | 360 | return reboot_device(); |
| 361 | } |
| 362 | |
David Zeuthen | 1a0929c | 2017-08-07 18:47:27 -0400 | [diff] [blame] | 363 | if (!skip_verification) { |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 364 | UpdateVerifier verifier; |
| 365 | if (!verifier.ParseCareMap()) { |
| 366 | LOG(WARNING) << "Failed to parse the care map file, skipping verification"; |
| 367 | } else if (!verifier.VerifyPartitions()) { |
David Zeuthen | 1a0929c | 2017-08-07 18:47:27 -0400 | [diff] [blame] | 368 | LOG(ERROR) << "Failed to verify all blocks in care map file."; |
| 369 | return reboot_device(); |
| 370 | } |
Tianjie Xu | d007cf2 | 2016-02-29 16:08:06 -0800 | [diff] [blame] | 371 | } |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 372 | |
Daniel Rosenberg | 15f22bd | 2019-01-22 19:57:28 -0800 | [diff] [blame] | 373 | bool supports_checkpoint = false; |
| 374 | auto sm = android::defaultServiceManager(); |
| 375 | android::sp<android::IBinder> binder = sm->getService(android::String16("vold")); |
| 376 | if (binder) { |
| 377 | auto vold = android::interface_cast<android::os::IVold>(binder); |
| 378 | android::binder::Status status = vold->supportsCheckpoint(&supports_checkpoint); |
| 379 | if (!status.isOk()) { |
| 380 | LOG(ERROR) << "Failed to check if checkpoints supported. Continuing"; |
| 381 | } |
| 382 | } else { |
| 383 | LOG(ERROR) << "Failed to obtain vold Binder. Continuing"; |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 384 | } |
Daniel Rosenberg | 15f22bd | 2019-01-22 19:57:28 -0800 | [diff] [blame] | 385 | |
| 386 | if (!supports_checkpoint) { |
Kelvin Zhang | fa554fe | 2022-04-01 14:17:10 -0700 | [diff] [blame^] | 387 | const auto cr = module->MarkBootSuccessful(); |
Daniel Rosenberg | 15f22bd | 2019-01-22 19:57:28 -0800 | [diff] [blame] | 388 | if (!cr.success) { |
| 389 | LOG(ERROR) << "Error marking booted successfully: " << cr.errMsg; |
| 390 | return reboot_device(); |
| 391 | } |
| 392 | LOG(INFO) << "Marked slot " << current_slot << " as booted successfully."; |
Tianjie Xu | 88bf6d2 | 2019-11-14 15:07:25 -0800 | [diff] [blame] | 393 | // Clears the warm reset flag for next reboot. |
| 394 | if (!android::base::SetProperty("ota.warm_reset", "0")) { |
| 395 | LOG(WARNING) << "Failed to reset the warm reset flag"; |
| 396 | } |
Daniel Rosenberg | 15f22bd | 2019-01-22 19:57:28 -0800 | [diff] [blame] | 397 | } else { |
| 398 | LOG(INFO) << "Deferred marking slot " << current_slot << " as booted successfully."; |
| 399 | } |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 400 | } |
| 401 | |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 402 | LOG(INFO) << "Leaving update_verifier."; |
Tao Bao | 7197ee0 | 2015-12-05 21:21:27 -0800 | [diff] [blame] | 403 | return 0; |
| 404 | } |