blob: dc72763268035cf80f20111c3e11a86027d25f1c [file] [log] [blame]
Tao Bao7197ee02015-12-05 21:21:27 -08001/*
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 Bao1cc03512018-04-18 17:10:49 -070018 * 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 Bao7197ee02015-12-05 21:21:27 -080021 *
Tao Bao1cc03512018-04-18 17:10:49 -070022 * 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 Xu3958a952017-03-01 15:31:25 -080028 * 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 Bao1cc03512018-04-18 17:10:49 -070032 * 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 Bao7197ee02015-12-05 21:21:27 -080035 *
Tao Bao1cc03512018-04-18 17:10:49 -070036 * The current slot will be marked as having booted successfully if the verifier reaches the end
37 * after the verification.
Tao Bao7197ee02015-12-05 21:21:27 -080038 */
39
Tao Bao83b07802017-04-26 14:30:56 -070040#include "update_verifier/update_verifier.h"
41
Tianjie Xub0ac8722017-01-18 18:41:53 -080042#include <dirent.h>
Tianjie Xud007cf22016-02-29 16:08:06 -080043#include <errno.h>
44#include <fcntl.h>
45#include <stdio.h>
Tao Bao7197ee02015-12-05 21:21:27 -080046#include <string.h>
Tianjie Xu3958a952017-03-01 15:31:25 -080047#include <unistd.h>
Tao Bao7197ee02015-12-05 21:21:27 -080048
Tianjie Xu8fa8f0b2017-04-27 11:47:35 -070049#include <algorithm>
Wei Wang5226f472017-08-02 10:27:31 -070050#include <future>
Tianjie Xud007cf22016-02-29 16:08:06 -080051#include <string>
52#include <vector>
53
54#include <android-base/file.h>
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070055#include <android-base/logging.h>
Tianjie Xud007cf22016-02-29 16:08:06 -080056#include <android-base/parseint.h>
Tao Bao4f8d2172017-01-13 15:36:32 -080057#include <android-base/properties.h>
Tianjie Xud007cf22016-02-29 16:08:06 -080058#include <android-base/strings.h>
59#include <android-base/unique_fd.h>
Connor O'Brienad43d2d2016-11-21 12:47:33 -080060#include <android/hardware/boot/1.0/IBootControl.h>
Tianjie Xu3958a952017-03-01 15:31:25 -080061#include <cutils/android_reboot.h>
Connor O'Brienad43d2d2016-11-21 12:47:33 -080062
Tao Bao160514b2017-11-04 00:08:08 -070063#include "otautil/rangeset.h"
64
Connor O'Brienad43d2d2016-11-21 12:47:33 -080065using android::sp;
66using android::hardware::boot::V1_0::IBootControl;
67using android::hardware::boot::V1_0::BoolResult;
68using android::hardware::boot::V1_0::CommandResult;
Tao Bao7197ee02015-12-05 21:21:27 -080069
Tianjie Xub0ac8722017-01-18 18:41:53 -080070// Find directories in format of "/sys/block/dm-X".
71static int dm_name_filter(const dirent* de) {
72 if (android::base::StartsWith(de->d_name, "dm-")) {
73 return 1;
74 }
75 return 0;
76}
77
Tianjie Xu3958a952017-03-01 15:31:25 -080078static bool read_blocks(const std::string& partition, const std::string& range_str) {
Tao Baoec2e8c62018-03-22 16:07:00 -070079 if (partition != "system" && partition != "vendor" && partition != "product") {
80 LOG(ERROR) << "Invalid partition name \"" << partition << "\"";
Tianjie Xu5a176c02017-03-31 16:36:12 -070081 return false;
82 }
Tao Baoec2e8c62018-03-22 16:07:00 -070083 // Iterate the content of "/sys/block/dm-X/dm/name". If it matches one of "system", "vendor" or
84 // "product", then dm-X is a dm-wrapped device for that target. We will later read all the
85 // ("cared") blocks from "/dev/block/dm-X" to ensure the target partition's integrity.
Tao Bao83b07802017-04-26 14:30:56 -070086 static constexpr auto DM_PATH_PREFIX = "/sys/block/";
Tianjie Xub0ac8722017-01-18 18:41:53 -080087 dirent** namelist;
88 int n = scandir(DM_PATH_PREFIX, &namelist, dm_name_filter, alphasort);
89 if (n == -1) {
90 PLOG(ERROR) << "Failed to scan dir " << DM_PATH_PREFIX;
91 return false;
92 }
93 if (n == 0) {
94 LOG(ERROR) << "dm block device not found for " << partition;
95 return false;
96 }
97
Tao Bao83b07802017-04-26 14:30:56 -070098 static constexpr auto DM_PATH_SUFFIX = "/dm/name";
99 static constexpr auto DEV_PATH = "/dev/block/";
Tianjie Xub0ac8722017-01-18 18:41:53 -0800100 std::string dm_block_device;
101 while (n--) {
102 std::string path = DM_PATH_PREFIX + std::string(namelist[n]->d_name) + DM_PATH_SUFFIX;
103 std::string content;
104 if (!android::base::ReadFileToString(path, &content)) {
105 PLOG(WARNING) << "Failed to read " << path;
David Zeuthen8ed97382017-05-08 13:41:28 -0400106 } else {
107 std::string dm_block_name = android::base::Trim(content);
David Zeuthen8ed97382017-05-08 13:41:28 -0400108 // AVB is using 'vroot' for the root block device but we're expecting 'system'.
109 if (dm_block_name == "vroot") {
110 dm_block_name = "system";
Tianjie Xub0ac8722017-01-18 18:41:53 -0800111 }
David Zeuthen8ed97382017-05-08 13:41:28 -0400112 if (dm_block_name == partition) {
113 dm_block_device = DEV_PATH + std::string(namelist[n]->d_name);
114 while (n--) {
115 free(namelist[n]);
116 }
117 break;
118 }
Tianjie Xub0ac8722017-01-18 18:41:53 -0800119 }
120 free(namelist[n]);
121 }
122 free(namelist);
123
124 if (dm_block_device.empty()) {
125 LOG(ERROR) << "Failed to find dm block device for " << partition;
126 return false;
127 }
Tao Bao4f8d2172017-01-13 15:36:32 -0800128
129 // For block range string, first integer 'count' equals 2 * total number of valid ranges,
130 // followed by 'count' number comma separated integers. Every two integers reprensent a
131 // block range with the first number included in range but second number not included.
132 // For example '4,64536,65343,74149,74150' represents: [64536,65343) and [74149,74150).
Tao Bao160514b2017-11-04 00:08:08 -0700133 RangeSet ranges = RangeSet::Parse(range_str);
134 if (!ranges) {
135 LOG(ERROR) << "Error parsing RangeSet string " << range_str;
Tao Bao4f8d2172017-01-13 15:36:32 -0800136 return false;
137 }
Tao Bao160514b2017-11-04 00:08:08 -0700138
139 // RangeSet::Split() splits the ranges into multiple groups with same number of blocks (except for
140 // the last group).
141 size_t thread_num = std::thread::hardware_concurrency() ?: 4;
142 std::vector<RangeSet> groups = ranges.Split(thread_num);
Tao Bao4f8d2172017-01-13 15:36:32 -0800143
Wei Wang5226f472017-08-02 10:27:31 -0700144 std::vector<std::future<bool>> threads;
Tao Bao160514b2017-11-04 00:08:08 -0700145 for (const auto& group : groups) {
146 auto thread_func = [&group, &dm_block_device, &partition]() {
Wei Wang5226f472017-08-02 10:27:31 -0700147 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(dm_block_device.c_str(), O_RDONLY)));
148 if (fd.get() == -1) {
149 PLOG(ERROR) << "Error reading " << dm_block_device << " for partition " << partition;
Tianjie Xu8fa8f0b2017-04-27 11:47:35 -0700150 return false;
151 }
Wei Wang5226f472017-08-02 10:27:31 -0700152
Tao Bao160514b2017-11-04 00:08:08 -0700153 static constexpr size_t kBlockSize = 4096;
154 std::vector<uint8_t> buf(1024 * kBlockSize);
Wei Wang5226f472017-08-02 10:27:31 -0700155
Tao Bao160514b2017-11-04 00:08:08 -0700156 size_t block_count = 0;
157 for (const auto& range : group) {
158 size_t range_start = range.first;
159 size_t range_end = range.second;
Wei Wang5226f472017-08-02 10:27:31 -0700160 if (lseek64(fd.get(), static_cast<off64_t>(range_start) * kBlockSize, SEEK_SET) == -1) {
161 PLOG(ERROR) << "lseek to " << range_start << " failed";
162 return false;
163 }
164
165 size_t remain = (range_end - range_start) * kBlockSize;
166 while (remain > 0) {
167 size_t to_read = std::min(remain, 1024 * kBlockSize);
168 if (!android::base::ReadFully(fd.get(), buf.data(), to_read)) {
169 PLOG(ERROR) << "Failed to read blocks " << range_start << " to " << range_end;
170 return false;
171 }
172 remain -= to_read;
173 }
Tao Bao160514b2017-11-04 00:08:08 -0700174 block_count += (range_end - range_start);
Wei Wang5226f472017-08-02 10:27:31 -0700175 }
Tao Bao160514b2017-11-04 00:08:08 -0700176 LOG(INFO) << "Finished reading " << block_count << " blocks on " << dm_block_device;
Wei Wang5226f472017-08-02 10:27:31 -0700177 return true;
178 };
179
180 threads.emplace_back(std::async(std::launch::async, thread_func));
Tao Bao4f8d2172017-01-13 15:36:32 -0800181 }
Tianjie Xud007cf22016-02-29 16:08:06 -0800182
Wei Wang5226f472017-08-02 10:27:31 -0700183 bool ret = true;
184 for (auto& t : threads) {
185 ret = t.get() && ret;
186 }
187 LOG(INFO) << "Finished reading blocks on " << dm_block_device << " with " << thread_num
188 << " threads.";
189 return ret;
Tianjie Xud007cf22016-02-29 16:08:06 -0800190}
191
Tao Bao5a1dee02017-07-21 15:15:31 -0700192// Returns true to indicate a passing verification (or the error should be ignored); Otherwise
193// returns false on fatal errors, where we should reject the current boot and trigger a fallback.
194// Note that update_verifier should be backward compatible to not reject care_map.txt from old
195// releases, which could otherwise fail to boot into the new release. For example, we've changed
196// the care_map format between N and O. An O update_verifier would fail to work with N
197// care_map.txt. This could be a result of sideloading an O OTA while the device having a pending N
198// update.
Tao Bao83b07802017-04-26 14:30:56 -0700199bool verify_image(const std::string& care_map_name) {
Tao Bao5a1dee02017-07-21 15:15:31 -0700200 android::base::unique_fd care_map_fd(TEMP_FAILURE_RETRY(open(care_map_name.c_str(), O_RDONLY)));
201 // If the device is flashed before the current boot, it may not have care_map.txt
202 // in /data/ota_package. To allow the device to continue booting in this situation,
203 // we should print a warning and skip the block verification.
204 if (care_map_fd.get() == -1) {
205 PLOG(WARNING) << "Failed to open " << care_map_name;
Tianjie Xud007cf22016-02-29 16:08:06 -0800206 return true;
Tao Bao5a1dee02017-07-21 15:15:31 -0700207 }
Tao Baoec2e8c62018-03-22 16:07:00 -0700208 // care_map file has up to six lines, where every two lines make a pair. Within each pair, the
209 // first line has the partition name (e.g. "system"), while the second line holds the ranges of
210 // all the blocks to verify.
Tao Bao5a1dee02017-07-21 15:15:31 -0700211 std::string file_content;
212 if (!android::base::ReadFdToString(care_map_fd.get(), &file_content)) {
213 LOG(ERROR) << "Error reading care map contents to string.";
214 return false;
215 }
216
217 std::vector<std::string> lines;
218 lines = android::base::Split(android::base::Trim(file_content), "\n");
Tao Baoec2e8c62018-03-22 16:07:00 -0700219 if (lines.size() != 2 && lines.size() != 4 && lines.size() != 6) {
Tao Bao5a1dee02017-07-21 15:15:31 -0700220 LOG(ERROR) << "Invalid lines in care_map: found " << lines.size()
Tao Baoec2e8c62018-03-22 16:07:00 -0700221 << " lines, expecting 2 or 4 or 6 lines.";
Tao Bao5a1dee02017-07-21 15:15:31 -0700222 return false;
223 }
224
225 for (size_t i = 0; i < lines.size(); i += 2) {
226 // We're seeing an N care_map.txt. Skip the verification since it's not compatible with O
227 // update_verifier (the last few metadata blocks can't be read via device mapper).
228 if (android::base::StartsWith(lines[i], "/dev/block/")) {
229 LOG(WARNING) << "Found legacy care_map.txt; skipped.";
230 return true;
231 }
232 if (!read_blocks(lines[i], lines[i+1])) {
233 return false;
234 }
235 }
236
237 return true;
Tianjie Xud007cf22016-02-29 16:08:06 -0800238}
239
Tianjie Xu3958a952017-03-01 15:31:25 -0800240static int reboot_device() {
241 if (android_reboot(ANDROID_RB_RESTART2, 0, nullptr) == -1) {
242 LOG(ERROR) << "Failed to reboot.";
243 return -1;
244 }
245 while (true) pause();
246}
247
Tao Bao83b07802017-04-26 14:30:56 -0700248int update_verifier(int argc, char** argv) {
Tao Bao7197ee02015-12-05 21:21:27 -0800249 for (int i = 1; i < argc; i++) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700250 LOG(INFO) << "Started with arg " << i << ": " << argv[i];
Tao Bao7197ee02015-12-05 21:21:27 -0800251 }
252
Chris Phoenix0157c782017-01-20 11:34:00 -0800253 sp<IBootControl> module = IBootControl::getService();
Connor O'Brienad43d2d2016-11-21 12:47:33 -0800254 if (module == nullptr) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700255 LOG(ERROR) << "Error getting bootctrl module.";
Tianjie Xu3958a952017-03-01 15:31:25 -0800256 return reboot_device();
Tao Bao7197ee02015-12-05 21:21:27 -0800257 }
258
Connor O'Brienad43d2d2016-11-21 12:47:33 -0800259 uint32_t current_slot = module->getCurrentSlot();
260 BoolResult is_successful = module->isSlotMarkedSuccessful(current_slot);
261 LOG(INFO) << "Booting slot " << current_slot << ": isSlotMarkedSuccessful="
262 << static_cast<int32_t>(is_successful);
Tao Bao7197ee02015-12-05 21:21:27 -0800263
Connor O'Brienad43d2d2016-11-21 12:47:33 -0800264 if (is_successful == BoolResult::FALSE) {
Tao Bao7197ee02015-12-05 21:21:27 -0800265 // The current slot has not booted successfully.
Tao Baodb57f0d2017-03-10 14:21:25 -0800266
David Zeuthen1a0929c2017-08-07 18:47:27 -0400267 bool skip_verification = false;
Tao Bao4f8d2172017-01-13 15:36:32 -0800268 std::string verity_mode = android::base::GetProperty("ro.boot.veritymode", "");
269 if (verity_mode.empty()) {
Tao Bao1cc03512018-04-18 17:10:49 -0700270 // Skip the verification if ro.boot.veritymode property is not set. This could be a result
271 // that device doesn't support dm-verity, or has disabled that.
272 LOG(WARNING) << "dm-verity not enabled; marking without verification.";
David Zeuthen1a0929c2017-08-07 18:47:27 -0400273 skip_verification = true;
Tao Bao4f8d2172017-01-13 15:36:32 -0800274 } else if (android::base::EqualsIgnoreCase(verity_mode, "eio")) {
Tianjie Xu3958a952017-03-01 15:31:25 -0800275 // We shouldn't see verity in EIO mode if the current slot hasn't booted successfully before.
276 // Continue the verification until we fail to read some blocks.
277 LOG(WARNING) << "Found dm-verity in EIO mode.";
David Zeuthen1a0929c2017-08-07 18:47:27 -0400278 } else if (android::base::EqualsIgnoreCase(verity_mode, "disabled")) {
279 LOG(WARNING) << "dm-verity in disabled mode; marking without verification.";
280 skip_verification = true;
Tao Bao4f8d2172017-01-13 15:36:32 -0800281 } else if (verity_mode != "enforcing") {
Tao Bao1cc03512018-04-18 17:10:49 -0700282 LOG(ERROR) << "Unexpected dm-verity mode: " << verity_mode << ", expecting enforcing.";
Tianjie Xu3958a952017-03-01 15:31:25 -0800283 return reboot_device();
284 }
285
David Zeuthen1a0929c2017-08-07 18:47:27 -0400286 if (!skip_verification) {
287 static constexpr auto CARE_MAP_FILE = "/data/ota_package/care_map.txt";
288 if (!verify_image(CARE_MAP_FILE)) {
289 LOG(ERROR) << "Failed to verify all blocks in care map file.";
290 return reboot_device();
291 }
Tianjie Xud007cf22016-02-29 16:08:06 -0800292 }
Tao Bao7197ee02015-12-05 21:21:27 -0800293
Connor O'Brienad43d2d2016-11-21 12:47:33 -0800294 CommandResult cr;
295 module->markBootSuccessful([&cr](CommandResult result) { cr = result; });
296 if (!cr.success) {
297 LOG(ERROR) << "Error marking booted successfully: " << cr.errMsg;
Tianjie Xu3958a952017-03-01 15:31:25 -0800298 return reboot_device();
Tao Bao7197ee02015-12-05 21:21:27 -0800299 }
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700300 LOG(INFO) << "Marked slot " << current_slot << " as booted successfully.";
Tao Bao7197ee02015-12-05 21:21:27 -0800301 }
302
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700303 LOG(INFO) << "Leaving update_verifier.";
Tao Bao7197ee02015-12-05 21:21:27 -0800304 return 0;
305}