blob: e3a3e347bfd1981fc6cd0af99b49f4c409bc2354 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 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//
Allie Woodeb9e6d82015-04-17 13:55:30 -070016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/payload_consumer/filesystem_verifier_action.h"
Allie Woodeb9e6d82015-04-17 13:55:30 -070018
19#include <errno.h>
20#include <fcntl.h>
21#include <sys/stat.h>
22#include <sys/types.h>
Kelvin Zhangec205cf2020-09-28 13:23:40 -040023#include <unistd.h>
Allie Woodeb9e6d82015-04-17 13:55:30 -070024
25#include <algorithm>
26#include <cstdlib>
Kelvin Zhangec205cf2020-09-28 13:23:40 -040027#include <memory>
Allie Woodeb9e6d82015-04-17 13:55:30 -070028#include <string>
Kelvin Zhangec205cf2020-09-28 13:23:40 -040029#include <utility>
Allie Woodeb9e6d82015-04-17 13:55:30 -070030
Alex Deymo20c99202015-07-09 16:14:16 -070031#include <base/bind.h>
Tianjie24f96092020-06-30 12:26:25 -070032#include <base/strings/string_util.h>
Kelvin Zhangec205cf2020-09-28 13:23:40 -040033#include <brillo/data_encoding.h>
34#include <brillo/message_loops/message_loop.h>
35#include <brillo/streams/file_stream.h>
Allie Woodeb9e6d82015-04-17 13:55:30 -070036
Alex Deymo39910dc2015-11-09 17:04:30 -080037#include "update_engine/common/utils.h"
Kelvin Zhangec205cf2020-09-28 13:23:40 -040038#include "update_engine/payload_consumer/file_descriptor.h"
Allie Woodeb9e6d82015-04-17 13:55:30 -070039
Sen Jiang2703ef42017-03-16 13:36:21 -070040using brillo::data_encoding::Base64Encode;
Allie Woodeb9e6d82015-04-17 13:55:30 -070041using std::string;
42
43namespace chromeos_update_engine {
44
45namespace {
Alex Deymo20c99202015-07-09 16:14:16 -070046const off_t kReadFileBufferSize = 128 * 1024;
Allie Woodeb9e6d82015-04-17 13:55:30 -070047} // namespace
48
Allie Woodeb9e6d82015-04-17 13:55:30 -070049void FilesystemVerifierAction::PerformAction() {
50 // Will tell the ActionProcessor we've failed if we return.
51 ScopedActionCompleter abort_action_completer(processor_, this);
52
53 if (!HasInputObject()) {
54 LOG(ERROR) << "FilesystemVerifierAction missing input object.";
55 return;
56 }
57 install_plan_ = GetInputObject();
58
Alex Deymoe5e5fe92015-10-05 09:28:19 -070059 if (install_plan_.partitions.empty()) {
60 LOG(INFO) << "No partitions to verify.";
Allie Woodeb9e6d82015-04-17 13:55:30 -070061 if (HasOutputPipe())
62 SetOutputObject(install_plan_);
63 abort_action_completer.set_code(ErrorCode::kSuccess);
64 return;
65 }
Jae Hoon Kim50504d62020-04-23 14:32:38 -070066 install_plan_.Dump();
Alex Deymoe5e5fe92015-10-05 09:28:19 -070067 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -070068 abort_action_completer.set_should_complete(false);
69}
70
71void FilesystemVerifierAction::TerminateProcessing() {
Kelvin Zhangec205cf2020-09-28 13:23:40 -040072 brillo::MessageLoop::current()->CancelTask(pending_task_id_);
Alex Deymo20c99202015-07-09 16:14:16 -070073 cancelled_ = true;
74 Cleanup(ErrorCode::kSuccess); // error code is ignored if canceled_ is true.
Allie Woodeb9e6d82015-04-17 13:55:30 -070075}
76
Allie Woodeb9e6d82015-04-17 13:55:30 -070077void FilesystemVerifierAction::Cleanup(ErrorCode code) {
Kelvin Zhangec205cf2020-09-28 13:23:40 -040078 read_fd_.reset();
79 write_fd_.reset();
Alex Deymo20c99202015-07-09 16:14:16 -070080 // This memory is not used anymore.
81 buffer_.clear();
82
Allie Woodeb9e6d82015-04-17 13:55:30 -070083 if (cancelled_)
84 return;
85 if (code == ErrorCode::kSuccess && HasOutputPipe())
86 SetOutputObject(install_plan_);
Kelvin Zhang70eef232020-06-12 20:32:40 +000087 UpdateProgress(1.0);
Allie Woodeb9e6d82015-04-17 13:55:30 -070088 processor_->ActionComplete(this, code);
89}
90
Kelvin Zhang70eef232020-06-12 20:32:40 +000091void FilesystemVerifierAction::UpdateProgress(double progress) {
92 if (delegate_ != nullptr) {
93 delegate_->OnVerifyProgressUpdate(progress);
94 }
95}
96
Kelvin Zhangec205cf2020-09-28 13:23:40 -040097bool FilesystemVerifierAction::InitializeFdVABC() {
98 const InstallPlan::Partition& partition =
99 install_plan_.partitions[partition_index_];
100
101 read_fd_ = dynamic_control_->OpenCowReader(
102 partition.name, partition.source_path, true);
103 if (!read_fd_) {
104 LOG(ERROR) << "OpenCowReader(" << partition.name << ", "
105 << partition.source_path << ") failed.";
106 return false;
107 }
108 partition_size_ = partition.target_size;
109 // TODO(b/173432386): Support Verity writes for VABC.
110 CHECK_EQ(partition.fec_size, 0U);
111 CHECK_EQ(partition.hash_tree_size, 0U);
112 return true;
113}
114
115bool FilesystemVerifierAction::InitializeFd(const std::string& part_path) {
116 read_fd_ = FileDescriptorPtr(new EintrSafeFileDescriptor());
117 if (!read_fd_->Open(part_path.c_str(), O_RDONLY)) {
118 LOG(ERROR) << "Unable to open " << part_path << " for reading.";
119 return false;
120 }
121
122 // Can't re-use |read_fd_|, as verity writer may call `seek` to modify state
123 // of a file descriptor.
124 if (ShouldWriteVerity()) {
125 write_fd_ = FileDescriptorPtr(new EintrSafeFileDescriptor());
126 if (!write_fd_->Open(part_path.c_str(), O_RDWR)) {
127 LOG(ERROR) << "Unable to open " << part_path << " for Read/Write.";
128 return false;
129 }
130 }
131 return true;
132}
133
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700134void FilesystemVerifierAction::StartPartitionHashing() {
135 if (partition_index_ == install_plan_.partitions.size()) {
Tianjie24f96092020-06-30 12:26:25 -0700136 if (!install_plan_.untouched_dynamic_partitions.empty()) {
137 LOG(INFO) << "Verifying extents of untouched dynamic partitions ["
138 << base::JoinString(install_plan_.untouched_dynamic_partitions,
139 ", ")
140 << "]";
141 if (!dynamic_control_->VerifyExtentsForUntouchedPartitions(
142 install_plan_.source_slot,
143 install_plan_.target_slot,
144 install_plan_.untouched_dynamic_partitions)) {
145 Cleanup(ErrorCode::kFilesystemVerifierError);
146 return;
147 }
148 }
149
Sen Jianga35896c2016-05-25 11:08:41 -0700150 Cleanup(ErrorCode::kSuccess);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700151 return;
152 }
Sen Jiang57f91802017-11-14 17:42:13 -0800153 const InstallPlan::Partition& partition =
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700154 install_plan_.partitions[partition_index_];
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700155 string part_path;
Sen Jiangfef85fd2016-03-25 15:32:49 -0700156 switch (verifier_step_) {
157 case VerifierStep::kVerifySourceHash:
Sen Jiange6e4bb92016-04-05 14:59:12 -0700158 part_path = partition.source_path;
Sen Jiang57f91802017-11-14 17:42:13 -0800159 partition_size_ = partition.source_size;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700160 break;
Sen Jiangfef85fd2016-03-25 15:32:49 -0700161 case VerifierStep::kVerifyTargetHash:
Sen Jiange6e4bb92016-04-05 14:59:12 -0700162 part_path = partition.target_path;
Sen Jiang57f91802017-11-14 17:42:13 -0800163 partition_size_ = partition.target_size;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700164 break;
165 }
Yifan Hong537802d2018-08-15 13:15:42 -0700166
Yifan Hong537802d2018-08-15 13:15:42 -0700167 LOG(INFO) << "Hashing partition " << partition_index_ << " ("
168 << partition.name << ") on device " << part_path;
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400169 auto success = false;
Kelvin Zhang06188352021-02-10 13:21:47 -0500170 if (dynamic_control_->UpdateUsesSnapshotCompression() &&
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400171 dynamic_control_->IsDynamicPartition(partition.name) &&
172 verifier_step_ == VerifierStep::kVerifyTargetHash) {
173 success = InitializeFdVABC();
174 } else {
175 if (part_path.empty()) {
176 if (partition_size_ == 0) {
177 LOG(INFO) << "Skip hashing partition " << partition_index_ << " ("
178 << partition.name << ") because size is 0.";
179 partition_index_++;
180 StartPartitionHashing();
181 return;
182 }
183 LOG(ERROR) << "Cannot hash partition " << partition_index_ << " ("
184 << partition.name
185 << ") because its device path cannot be determined.";
186 Cleanup(ErrorCode::kFilesystemVerifierError);
187 return;
188 }
189 success = InitializeFd(part_path);
190 }
191 if (!success) {
Sen Jiang57f91802017-11-14 17:42:13 -0800192 Cleanup(ErrorCode::kFilesystemVerifierError);
193 return;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700194 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700195 buffer_.resize(kReadFileBufferSize);
Sen Jiang57f91802017-11-14 17:42:13 -0800196 hasher_ = std::make_unique<HashCalculator>();
197
198 offset_ = 0;
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400199 if (ShouldWriteVerity()) {
200 if (!verity_writer_->Init(partition, read_fd_, write_fd_)) {
Sen Jiang57f91802017-11-14 17:42:13 -0800201 Cleanup(ErrorCode::kVerityCalculationError);
202 return;
203 }
204 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700205
206 // Start the first read.
207 ScheduleRead();
208}
209
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400210bool FilesystemVerifierAction::ShouldWriteVerity() {
211 const InstallPlan::Partition& partition =
212 install_plan_.partitions[partition_index_];
213 return verifier_step_ == VerifierStep::kVerifyTargetHash &&
214 install_plan_.write_verity &&
215 (partition.hash_tree_size > 0 || partition.fec_size > 0);
216}
217
Alex Deymob9e8e262015-08-03 20:23:03 -0700218void FilesystemVerifierAction::ScheduleRead() {
Sen Jiang57f91802017-11-14 17:42:13 -0800219 const InstallPlan::Partition& partition =
220 install_plan_.partitions[partition_index_];
221
222 // We can only start reading anything past |hash_tree_offset| after we have
223 // already read all the data blocks that the hash tree covers. The same
224 // applies to FEC.
225 uint64_t read_end = partition_size_;
226 if (partition.hash_tree_size != 0 &&
227 offset_ < partition.hash_tree_data_offset + partition.hash_tree_data_size)
228 read_end = std::min(read_end, partition.hash_tree_offset);
229 if (partition.fec_size != 0 &&
230 offset_ < partition.fec_data_offset + partition.fec_data_size)
231 read_end = std::min(read_end, partition.fec_offset);
232 size_t bytes_to_read =
233 std::min(static_cast<uint64_t>(buffer_.size()), read_end - offset_);
Alex Deymob9e8e262015-08-03 20:23:03 -0700234 if (!bytes_to_read) {
Sen Jiang57f91802017-11-14 17:42:13 -0800235 FinishPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700236 return;
237 }
238
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400239 auto bytes_read = read_fd_->Read(buffer_.data(), bytes_to_read);
240 if (bytes_read < 0) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700241 LOG(ERROR) << "Unable to schedule an asynchronous read from the stream.";
242 Cleanup(ErrorCode::kError);
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400243 } else {
244 // We could just invoke |OnReadDoneCallback()|, it works. But |PostTask|
245 // is used so that users can cancel updates.
246 pending_task_id_ = brillo::MessageLoop::current()->PostTask(
247 base::Bind(&FilesystemVerifierAction::OnReadDone,
248 base::Unretained(this),
249 bytes_read));
Allie Woodeb9e6d82015-04-17 13:55:30 -0700250 }
251}
252
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400253void FilesystemVerifierAction::OnReadDone(size_t bytes_read) {
Sen Jiang57f91802017-11-14 17:42:13 -0800254 if (cancelled_) {
255 Cleanup(ErrorCode::kError);
256 return;
257 }
Alex Deymob9e8e262015-08-03 20:23:03 -0700258 if (bytes_read == 0) {
Sen Jiang57f91802017-11-14 17:42:13 -0800259 LOG(ERROR) << "Failed to read the remaining " << partition_size_ - offset_
260 << " bytes from partition "
261 << install_plan_.partitions[partition_index_].name;
262 Cleanup(ErrorCode::kFilesystemVerifierError);
263 return;
264 }
265
266 if (!hasher_->Update(buffer_.data(), bytes_read)) {
267 LOG(ERROR) << "Unable to update the hash.";
268 Cleanup(ErrorCode::kError);
269 return;
270 }
271
Kelvin Zhang70eef232020-06-12 20:32:40 +0000272 // WE don't consider sizes of each partition. Every partition
273 // has the same length on progress bar.
274 // TODO(zhangkelvin) Take sizes of each partition into account
275
276 UpdateProgress(
277 (static_cast<double>(offset_) / partition_size_ + partition_index_) /
278 install_plan_.partitions.size());
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400279 if (ShouldWriteVerity()) {
Sen Jiang57f91802017-11-14 17:42:13 -0800280 if (!verity_writer_->Update(offset_, buffer_.data(), bytes_read)) {
281 Cleanup(ErrorCode::kVerityCalculationError);
Alex Deymob9e8e262015-08-03 20:23:03 -0700282 return;
283 }
284 }
285
Sen Jiang57f91802017-11-14 17:42:13 -0800286 offset_ += bytes_read;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700287
Sen Jiang57f91802017-11-14 17:42:13 -0800288 if (offset_ == partition_size_) {
289 FinishPartitionHashing();
290 return;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700291 }
Sen Jiang57f91802017-11-14 17:42:13 -0800292
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700293 ScheduleRead();
Alex Deymob9e8e262015-08-03 20:23:03 -0700294}
295
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700296void FilesystemVerifierAction::FinishPartitionHashing() {
297 if (!hasher_->Finalize()) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700298 LOG(ERROR) << "Unable to finalize the hash.";
Sen Jiang57f91802017-11-14 17:42:13 -0800299 Cleanup(ErrorCode::kError);
300 return;
Alex Deymob9e8e262015-08-03 20:23:03 -0700301 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700302 InstallPlan::Partition& partition =
303 install_plan_.partitions[partition_index_];
Sen Jiang2703ef42017-03-16 13:36:21 -0700304 LOG(INFO) << "Hash of " << partition.name << ": "
305 << Base64Encode(hasher_->raw_hash());
Alex Deymob9e8e262015-08-03 20:23:03 -0700306
Sen Jiangfef85fd2016-03-25 15:32:49 -0700307 switch (verifier_step_) {
308 case VerifierStep::kVerifyTargetHash:
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700309 if (partition.target_hash != hasher_->raw_hash()) {
310 LOG(ERROR) << "New '" << partition.name
311 << "' partition verification failed.";
Sen Jiangcdd52062017-05-18 15:33:10 -0700312 if (partition.source_hash.empty()) {
313 // No need to verify source if it is a full payload.
Sen Jiang57f91802017-11-14 17:42:13 -0800314 Cleanup(ErrorCode::kNewRootfsVerificationError);
315 return;
Sen Jiangcdd52062017-05-18 15:33:10 -0700316 }
Sen Jiangfef85fd2016-03-25 15:32:49 -0700317 // If we have not verified source partition yet, now that the target
Sen Jiang65566a32016-04-06 13:35:36 -0700318 // partition does not match, and it's not a full payload, we need to
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400319 // switch to kVerifySourceHash step to check if it's because the
320 // source partition does not match either.
Sen Jiangfef85fd2016-03-25 15:32:49 -0700321 verifier_step_ = VerifierStep::kVerifySourceHash;
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800322 } else {
323 partition_index_++;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700324 }
325 break;
Sen Jiangfef85fd2016-03-25 15:32:49 -0700326 case VerifierStep::kVerifySourceHash:
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800327 if (partition.source_hash != hasher_->raw_hash()) {
328 LOG(ERROR) << "Old '" << partition.name
329 << "' partition verification failed.";
Sen Jiangfef85fd2016-03-25 15:32:49 -0700330 LOG(ERROR) << "This is a server-side error due to mismatched delta"
331 << " update image!";
332 LOG(ERROR) << "The delta I've been given contains a " << partition.name
333 << " delta update that must be applied over a "
334 << partition.name << " with a specific checksum, but the "
335 << partition.name
336 << " we're starting with doesn't have that checksum! This"
337 " means that the delta I've been given doesn't match my"
338 " existing system. The "
339 << partition.name << " partition I have has hash: "
Sen Jiang2703ef42017-03-16 13:36:21 -0700340 << Base64Encode(hasher_->raw_hash())
Sen Jiangfef85fd2016-03-25 15:32:49 -0700341 << " but the update expected me to have "
Sen Jiang2703ef42017-03-16 13:36:21 -0700342 << Base64Encode(partition.source_hash) << " .";
Sen Jiangfef85fd2016-03-25 15:32:49 -0700343 LOG(INFO) << "To get the checksum of the " << partition.name
344 << " partition run this command: dd if="
345 << partition.source_path
346 << " bs=1M count=" << partition.source_size
347 << " iflag=count_bytes 2>/dev/null | openssl dgst -sha256 "
348 "-binary | openssl base64";
349 LOG(INFO) << "To get the checksum of partitions in a bin file, "
350 << "run: .../src/scripts/sha256_partitions.sh .../file.bin";
Sen Jiang57f91802017-11-14 17:42:13 -0800351 Cleanup(ErrorCode::kDownloadStateInitializationError);
352 return;
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800353 }
Sen Jianga35896c2016-05-25 11:08:41 -0700354 // The action will skip kVerifySourceHash step if target partition hash
355 // matches, if we are in this step, it means target hash does not match,
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400356 // and now that the source partition hash matches, we should set the
357 // error code to reflect the error in target partition. We only need to
358 // verify the source partition which the target hash does not match, the
359 // rest of the partitions don't matter.
Sen Jiang57f91802017-11-14 17:42:13 -0800360 Cleanup(ErrorCode::kNewRootfsVerificationError);
361 return;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700362 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700363 // Start hashing the next partition, if any.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700364 hasher_.reset();
365 buffer_.clear();
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400366 if (read_fd_) {
367 read_fd_.reset();
368 }
369 if (write_fd_) {
370 write_fd_.reset();
371 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700372 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700373}
374
375} // namespace chromeos_update_engine