1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* This program verifies the integrity of the partitions after an A/B OTA
* update. It gets invoked by init, and will only perform the verification if
* it's the first boot post an A/B OTA update.
*
* It relies on dm-verity to capture any corruption on the partitions being
* verified. dm-verity must be in enforcing mode, so that it will reboot the
* device on dm-verity failures. When that happens, the bootloader should
* mark the slot as unbootable and stops trying. Other dm-verity modes (
* for example, veritymode=EIO) are not accepted and simply lead to a
* verification failure.
*
* The current slot will be marked as having booted successfully if the
* verifier reaches the end after the verification.
*/
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/parseint.h>
#include <android-base/properties.h>
#include <android-base/strings.h>
#include <android-base/unique_fd.h>
#include <android/hardware/boot/1.0/IBootControl.h>
using android::sp;
using android::hardware::boot::V1_0::IBootControl;
using android::hardware::boot::V1_0::BoolResult;
using android::hardware::boot::V1_0::CommandResult;
constexpr auto CARE_MAP_FILE = "/data/ota_package/care_map.txt";
constexpr auto DM_PATH_PREFIX = "/sys/block/";
constexpr auto DM_PATH_SUFFIX = "/dm/name";
constexpr auto DEV_PATH = "/dev/block/";
constexpr int BLOCKSIZE = 4096;
// Find directories in format of "/sys/block/dm-X".
static int dm_name_filter(const dirent* de) {
if (android::base::StartsWith(de->d_name, "dm-")) {
return 1;
}
return 0;
}
static bool read_blocks(const std::string& blk_device, const std::string& range_str) {
// Parse the partition in the end of the block_device string.
// Here is one example: "/dev/block/bootdevice/by-name/system"
std::string partition;
if (android::base::EndsWith(blk_device, "system")) {
partition = "system";
} else if (android::base::EndsWith(blk_device, "vendor")) {
partition = "vendor";
} else {
LOG(ERROR) << "Failed to parse partition string in " << blk_device;
return false;
}
// Iterate the content of "/sys/block/dm-X/dm/name". If it matches "system"
// (or "vendor"), then dm-X is a dm-wrapped system/vendor partition.
// Afterwards, update_verifier will read every block on the care_map_file of
// "/dev/block/dm-X" to ensure the partition's integrity.
dirent** namelist;
int n = scandir(DM_PATH_PREFIX, &namelist, dm_name_filter, alphasort);
if (n == -1) {
PLOG(ERROR) << "Failed to scan dir " << DM_PATH_PREFIX;
return false;
}
if (n == 0) {
LOG(ERROR) << "dm block device not found for " << partition;
return false;
}
std::string dm_block_device;
while (n--) {
std::string path = DM_PATH_PREFIX + std::string(namelist[n]->d_name) + DM_PATH_SUFFIX;
std::string content;
if (!android::base::ReadFileToString(path, &content)) {
PLOG(WARNING) << "Failed to read " << path;
} else if (android::base::Trim(content) == partition) {
dm_block_device = DEV_PATH + std::string(namelist[n]->d_name);
while (n--) {
free(namelist[n]);
}
break;
}
free(namelist[n]);
}
free(namelist);
if (dm_block_device.empty()) {
LOG(ERROR) << "Failed to find dm block device for " << partition;
return false;
}
android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(dm_block_device.c_str(), O_RDONLY)));
if (fd.get() == -1) {
PLOG(ERROR) << "Error reading " << dm_block_device << " for partition " << partition;
return false;
}
// For block range string, first integer 'count' equals 2 * total number of valid ranges,
// followed by 'count' number comma separated integers. Every two integers reprensent a
// block range with the first number included in range but second number not included.
// For example '4,64536,65343,74149,74150' represents: [64536,65343) and [74149,74150).
std::vector<std::string> ranges = android::base::Split(range_str, ",");
size_t range_count;
bool status = android::base::ParseUint(ranges[0], &range_count);
if (!status || (range_count == 0) || (range_count % 2 != 0) ||
(range_count != ranges.size() - 1)) {
LOG(ERROR) << "Error in parsing range string.";
return false;
}
size_t blk_count = 0;
for (size_t i = 1; i < ranges.size(); i += 2) {
unsigned int range_start, range_end;
bool parse_status = android::base::ParseUint(ranges[i], &range_start);
parse_status = parse_status && android::base::ParseUint(ranges[i + 1], &range_end);
if (!parse_status || range_start >= range_end) {
LOG(ERROR) << "Invalid range pair " << ranges[i] << ", " << ranges[i + 1];
return false;
}
if (lseek64(fd.get(), static_cast<off64_t>(range_start) * BLOCKSIZE, SEEK_SET) == -1) {
PLOG(ERROR) << "lseek to " << range_start << " failed";
return false;
}
size_t size = (range_end - range_start) * BLOCKSIZE;
std::vector<uint8_t> buf(size);
if (!android::base::ReadFully(fd.get(), buf.data(), size)) {
PLOG(ERROR) << "Failed to read blocks " << range_start << " to " << range_end;
return false;
}
blk_count += (range_end - range_start);
}
LOG(INFO) << "Finished reading " << blk_count << " blocks on " << dm_block_device;
return true;
}
static bool verify_image(const std::string& care_map_name) {
android::base::unique_fd care_map_fd(TEMP_FAILURE_RETRY(open(care_map_name.c_str(), O_RDONLY)));
// If the device is flashed before the current boot, it may not have care_map.txt
// in /data/ota_package. To allow the device to continue booting in this situation,
// we should print a warning and skip the block verification.
if (care_map_fd.get() == -1) {
LOG(WARNING) << "Warning: care map " << care_map_name << " not found.";
return true;
}
// Care map file has four lines (two lines if vendor partition is not present):
// First line has the block device name, e.g./dev/block/.../by-name/system.
// Second line holds all ranges of blocks to verify.
// The next two lines have the same format but for vendor partition.
std::string file_content;
if (!android::base::ReadFdToString(care_map_fd.get(), &file_content)) {
LOG(ERROR) << "Error reading care map contents to string.";
return false;
}
std::vector<std::string> lines;
lines = android::base::Split(android::base::Trim(file_content), "\n");
if (lines.size() != 2 && lines.size() != 4) {
LOG(ERROR) << "Invalid lines in care_map: found " << lines.size()
<< " lines, expecting 2 or 4 lines.";
return false;
}
for (size_t i = 0; i < lines.size(); i += 2) {
if (!read_blocks(lines[i], lines[i+1])) {
return false;
}
}
return true;
}
int main(int argc, char** argv) {
for (int i = 1; i < argc; i++) {
LOG(INFO) << "Started with arg " << i << ": " << argv[i];
}
sp<IBootControl> module = IBootControl::getService();
if (module == nullptr) {
LOG(ERROR) << "Error getting bootctrl module.";
return -1;
}
uint32_t current_slot = module->getCurrentSlot();
BoolResult is_successful = module->isSlotMarkedSuccessful(current_slot);
LOG(INFO) << "Booting slot " << current_slot << ": isSlotMarkedSuccessful="
<< static_cast<int32_t>(is_successful);
if (is_successful == BoolResult::FALSE) {
// The current slot has not booted successfully.
#ifdef PRODUCT_SUPPORTS_VERITY
std::string verity_mode = android::base::GetProperty("ro.boot.veritymode", "");
if (verity_mode.empty()) {
LOG(ERROR) << "Failed to get dm-verity mode.";
return -1;
} else if (android::base::EqualsIgnoreCase(verity_mode, "eio")) {
// We shouldn't see verity in EIO mode if the current slot hasn't booted
// successfully before. Therefore, fail the verification when veritymode=eio.
LOG(ERROR) << "Found dm-verity in EIO mode, skip verification.";
return -1;
} else if (verity_mode != "enforcing") {
LOG(ERROR) << "Unexpected dm-verity mode : " << verity_mode << ", expecting enforcing.";
return -1;
} else if (!verify_image(CARE_MAP_FILE)) {
LOG(ERROR) << "Failed to verify all blocks in care map file.";
return -1;
}
#else
LOG(WARNING) << "dm-verity not enabled; marking without verification.";
#endif
CommandResult cr;
module->markBootSuccessful([&cr](CommandResult result) { cr = result; });
if (!cr.success) {
LOG(ERROR) << "Error marking booted successfully: " << cr.errMsg;
return -1;
}
LOG(INFO) << "Marked slot " << current_slot << " as booted successfully.";
}
LOG(INFO) << "Leaving update_verifier.";
return 0;
}
|