blob: d3a5185b88c6cefa62cd1e01cb9fd6a49ab04492 [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/*
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 Xu3958a952017-03-01 15:31:25 -080022 * 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 Xud007cf22016-02-29 16:08:06 -080030 * 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 Bao7197ee02015-12-05 21:21:27 -080033 *
34 * The current slot will be marked as having booted successfully if the
35 * verifier reaches the end after the verification.
Tao Bao7197ee02015-12-05 21:21:27 -080036 */
37
Tao Bao83b07802017-04-26 14:30:56 -070038#include "update_verifier/update_verifier.h"
39
Tianjie Xub0ac8722017-01-18 18:41:53 -080040#include <dirent.h>
Tianjie Xud007cf22016-02-29 16:08:06 -080041#include <errno.h>
42#include <fcntl.h>
43#include <stdio.h>
Tao Bao7197ee02015-12-05 21:21:27 -080044#include <string.h>
Tianjie Xu3958a952017-03-01 15:31:25 -080045#include <unistd.h>
Tao Bao7197ee02015-12-05 21:21:27 -080046
Tianjie Xu8fa8f0b2017-04-27 11:47:35 -070047#include <algorithm>
Tianjie Xud007cf22016-02-29 16:08:06 -080048#include <string>
49#include <vector>
50
51#include <android-base/file.h>
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070052#include <android-base/logging.h>
Tianjie Xud007cf22016-02-29 16:08:06 -080053#include <android-base/parseint.h>
Tao Bao4f8d2172017-01-13 15:36:32 -080054#include <android-base/properties.h>
Tianjie Xud007cf22016-02-29 16:08:06 -080055#include <android-base/strings.h>
56#include <android-base/unique_fd.h>
Connor O'Brienad43d2d2016-11-21 12:47:33 -080057#include <android/hardware/boot/1.0/IBootControl.h>
Tianjie Xu3958a952017-03-01 15:31:25 -080058#include <cutils/android_reboot.h>
Connor O'Brienad43d2d2016-11-21 12:47:33 -080059
60using android::sp;
61using android::hardware::boot::V1_0::IBootControl;
62using android::hardware::boot::V1_0::BoolResult;
63using android::hardware::boot::V1_0::CommandResult;
Tao Bao7197ee02015-12-05 21:21:27 -080064
Tianjie Xub0ac8722017-01-18 18:41:53 -080065// Find directories in format of "/sys/block/dm-X".
66static 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 Xu3958a952017-03-01 15:31:25 -080073static bool read_blocks(const std::string& partition, const std::string& range_str) {
Tianjie Xu5a176c02017-03-31 16:36:12 -070074 if (partition != "system" && partition != "vendor") {
75 LOG(ERROR) << "partition name must be system or vendor: " << partition;
76 return false;
77 }
Tianjie Xub0ac8722017-01-18 18:41:53 -080078 // 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 Bao83b07802017-04-26 14:30:56 -070082 static constexpr auto DM_PATH_PREFIX = "/sys/block/";
Tianjie Xub0ac8722017-01-18 18:41:53 -080083 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 Bao83b07802017-04-26 14:30:56 -070094 static constexpr auto DM_PATH_SUFFIX = "/dm/name";
95 static constexpr auto DEV_PATH = "/dev/block/";
Tianjie Xub0ac8722017-01-18 18:41:53 -080096 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;
David Zeuthen336cbce2017-05-08 13:41:28 -0400102 } else {
103 std::string dm_block_name = android::base::Trim(content);
104#ifdef BOARD_AVB_ENABLE
105 // AVB is using 'vroot' for the root block device but we're expecting 'system'.
106 if (dm_block_name == "vroot") {
107 dm_block_name = "system";
Tianjie Xub0ac8722017-01-18 18:41:53 -0800108 }
David Zeuthen336cbce2017-05-08 13:41:28 -0400109#endif
110 if (dm_block_name == partition) {
111 dm_block_device = DEV_PATH + std::string(namelist[n]->d_name);
112 while (n--) {
113 free(namelist[n]);
114 }
115 break;
116 }
Tianjie Xub0ac8722017-01-18 18:41:53 -0800117 }
118 free(namelist[n]);
119 }
120 free(namelist);
121
122 if (dm_block_device.empty()) {
123 LOG(ERROR) << "Failed to find dm block device for " << partition;
124 return false;
125 }
126 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(dm_block_device.c_str(), O_RDONLY)));
Tao Bao4f8d2172017-01-13 15:36:32 -0800127 if (fd.get() == -1) {
Tianjie Xub0ac8722017-01-18 18:41:53 -0800128 PLOG(ERROR) << "Error reading " << dm_block_device << " for partition " << partition;
Tao Bao4f8d2172017-01-13 15:36:32 -0800129 return false;
130 }
131
132 // For block range string, first integer 'count' equals 2 * total number of valid ranges,
133 // followed by 'count' number comma separated integers. Every two integers reprensent a
134 // block range with the first number included in range but second number not included.
135 // For example '4,64536,65343,74149,74150' represents: [64536,65343) and [74149,74150).
136 std::vector<std::string> ranges = android::base::Split(range_str, ",");
137 size_t range_count;
138 bool status = android::base::ParseUint(ranges[0], &range_count);
139 if (!status || (range_count == 0) || (range_count % 2 != 0) ||
140 (range_count != ranges.size() - 1)) {
141 LOG(ERROR) << "Error in parsing range string.";
142 return false;
143 }
144
145 size_t blk_count = 0;
146 for (size_t i = 1; i < ranges.size(); i += 2) {
147 unsigned int range_start, range_end;
148 bool parse_status = android::base::ParseUint(ranges[i], &range_start);
149 parse_status = parse_status && android::base::ParseUint(ranges[i + 1], &range_end);
150 if (!parse_status || range_start >= range_end) {
151 LOG(ERROR) << "Invalid range pair " << ranges[i] << ", " << ranges[i + 1];
152 return false;
Tianjie Xud007cf22016-02-29 16:08:06 -0800153 }
154
Tianjie Xu8fa8f0b2017-04-27 11:47:35 -0700155 static constexpr size_t BLOCKSIZE = 4096;
Tao Bao4f8d2172017-01-13 15:36:32 -0800156 if (lseek64(fd.get(), static_cast<off64_t>(range_start) * BLOCKSIZE, SEEK_SET) == -1) {
157 PLOG(ERROR) << "lseek to " << range_start << " failed";
158 return false;
Tianjie Xud007cf22016-02-29 16:08:06 -0800159 }
160
Tianjie Xu8fa8f0b2017-04-27 11:47:35 -0700161 size_t remain = (range_end - range_start) * BLOCKSIZE;
162 while (remain > 0) {
163 size_t to_read = std::min(remain, 1024 * BLOCKSIZE);
164 std::vector<uint8_t> buf(to_read);
165 if (!android::base::ReadFully(fd.get(), buf.data(), to_read)) {
166 PLOG(ERROR) << "Failed to read blocks " << range_start << " to " << range_end;
167 return false;
168 }
169 remain -= to_read;
Tianjie Xud007cf22016-02-29 16:08:06 -0800170 }
Tao Bao4f8d2172017-01-13 15:36:32 -0800171 blk_count += (range_end - range_start);
172 }
Tianjie Xud007cf22016-02-29 16:08:06 -0800173
Tianjie Xub0ac8722017-01-18 18:41:53 -0800174 LOG(INFO) << "Finished reading " << blk_count << " blocks on " << dm_block_device;
Tao Bao4f8d2172017-01-13 15:36:32 -0800175 return true;
Tianjie Xud007cf22016-02-29 16:08:06 -0800176}
177
Tao Bao83b07802017-04-26 14:30:56 -0700178bool verify_image(const std::string& care_map_name) {
Tianjie Xud007cf22016-02-29 16:08:06 -0800179 android::base::unique_fd care_map_fd(TEMP_FAILURE_RETRY(open(care_map_name.c_str(), O_RDONLY)));
180 // If the device is flashed before the current boot, it may not have care_map.txt
181 // in /data/ota_package. To allow the device to continue booting in this situation,
182 // we should print a warning and skip the block verification.
183 if (care_map_fd.get() == -1) {
Tom Cherry3a8002f2017-03-31 17:17:34 -0700184 PLOG(WARNING) << "Failed to open " << care_map_name;
Tianjie Xud007cf22016-02-29 16:08:06 -0800185 return true;
186 }
187 // Care map file has four lines (two lines if vendor partition is not present):
Tianjie Xu3958a952017-03-01 15:31:25 -0800188 // First line has the block partition name (system/vendor).
Tianjie Xud007cf22016-02-29 16:08:06 -0800189 // Second line holds all ranges of blocks to verify.
190 // The next two lines have the same format but for vendor partition.
191 std::string file_content;
192 if (!android::base::ReadFdToString(care_map_fd.get(), &file_content)) {
193 LOG(ERROR) << "Error reading care map contents to string.";
194 return false;
195 }
196
197 std::vector<std::string> lines;
198 lines = android::base::Split(android::base::Trim(file_content), "\n");
199 if (lines.size() != 2 && lines.size() != 4) {
200 LOG(ERROR) << "Invalid lines in care_map: found " << lines.size()
201 << " lines, expecting 2 or 4 lines.";
202 return false;
203 }
204
205 for (size_t i = 0; i < lines.size(); i += 2) {
206 if (!read_blocks(lines[i], lines[i+1])) {
207 return false;
208 }
209 }
210
211 return true;
212}
213
Tianjie Xu3958a952017-03-01 15:31:25 -0800214static int reboot_device() {
215 if (android_reboot(ANDROID_RB_RESTART2, 0, nullptr) == -1) {
216 LOG(ERROR) << "Failed to reboot.";
217 return -1;
218 }
219 while (true) pause();
220}
221
Tao Bao83b07802017-04-26 14:30:56 -0700222int update_verifier(int argc, char** argv) {
Tao Bao7197ee02015-12-05 21:21:27 -0800223 for (int i = 1; i < argc; i++) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700224 LOG(INFO) << "Started with arg " << i << ": " << argv[i];
Tao Bao7197ee02015-12-05 21:21:27 -0800225 }
226
Chris Phoenix0157c782017-01-20 11:34:00 -0800227 sp<IBootControl> module = IBootControl::getService();
Connor O'Brienad43d2d2016-11-21 12:47:33 -0800228 if (module == nullptr) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700229 LOG(ERROR) << "Error getting bootctrl module.";
Tianjie Xu3958a952017-03-01 15:31:25 -0800230 return reboot_device();
Tao Bao7197ee02015-12-05 21:21:27 -0800231 }
232
Connor O'Brienad43d2d2016-11-21 12:47:33 -0800233 uint32_t current_slot = module->getCurrentSlot();
234 BoolResult is_successful = module->isSlotMarkedSuccessful(current_slot);
235 LOG(INFO) << "Booting slot " << current_slot << ": isSlotMarkedSuccessful="
236 << static_cast<int32_t>(is_successful);
Tao Bao7197ee02015-12-05 21:21:27 -0800237
Connor O'Brienad43d2d2016-11-21 12:47:33 -0800238 if (is_successful == BoolResult::FALSE) {
Tao Bao7197ee02015-12-05 21:21:27 -0800239 // The current slot has not booted successfully.
Tao Baodb57f0d2017-03-10 14:21:25 -0800240
David Zeuthen336cbce2017-05-08 13:41:28 -0400241#if defined(PRODUCT_SUPPORTS_VERITY) || defined(BOARD_AVB_ENABLE)
Tao Bao4f8d2172017-01-13 15:36:32 -0800242 std::string verity_mode = android::base::GetProperty("ro.boot.veritymode", "");
243 if (verity_mode.empty()) {
Tianjie Xud007cf22016-02-29 16:08:06 -0800244 LOG(ERROR) << "Failed to get dm-verity mode.";
Tianjie Xu3958a952017-03-01 15:31:25 -0800245 return reboot_device();
Tao Bao4f8d2172017-01-13 15:36:32 -0800246 } else if (android::base::EqualsIgnoreCase(verity_mode, "eio")) {
Tianjie Xu3958a952017-03-01 15:31:25 -0800247 // We shouldn't see verity in EIO mode if the current slot hasn't booted successfully before.
248 // Continue the verification until we fail to read some blocks.
249 LOG(WARNING) << "Found dm-verity in EIO mode.";
Tao Bao4f8d2172017-01-13 15:36:32 -0800250 } else if (verity_mode != "enforcing") {
Tianjie Xud007cf22016-02-29 16:08:06 -0800251 LOG(ERROR) << "Unexpected dm-verity mode : " << verity_mode << ", expecting enforcing.";
Tianjie Xu3958a952017-03-01 15:31:25 -0800252 return reboot_device();
253 }
254
Tao Bao83b07802017-04-26 14:30:56 -0700255 static constexpr auto CARE_MAP_FILE = "/data/ota_package/care_map.txt";
Tianjie Xu3958a952017-03-01 15:31:25 -0800256 if (!verify_image(CARE_MAP_FILE)) {
Tianjie Xud007cf22016-02-29 16:08:06 -0800257 LOG(ERROR) << "Failed to verify all blocks in care map file.";
Tianjie Xu3958a952017-03-01 15:31:25 -0800258 return reboot_device();
Tianjie Xud007cf22016-02-29 16:08:06 -0800259 }
Tao Baodb57f0d2017-03-10 14:21:25 -0800260#else
261 LOG(WARNING) << "dm-verity not enabled; marking without verification.";
262#endif
Tao Bao7197ee02015-12-05 21:21:27 -0800263
Connor O'Brienad43d2d2016-11-21 12:47:33 -0800264 CommandResult cr;
265 module->markBootSuccessful([&cr](CommandResult result) { cr = result; });
266 if (!cr.success) {
267 LOG(ERROR) << "Error marking booted successfully: " << cr.errMsg;
Tianjie Xu3958a952017-03-01 15:31:25 -0800268 return reboot_device();
Tao Bao7197ee02015-12-05 21:21:27 -0800269 }
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700270 LOG(INFO) << "Marked slot " << current_slot << " as booted successfully.";
Tao Bao7197ee02015-12-05 21:21:27 -0800271 }
272
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700273 LOG(INFO) << "Leaving update_verifier.";
Tao Bao7197ee02015-12-05 21:21:27 -0800274 return 0;
275}