blob: 9221caa8da83a7ed7288c8996089b72bb5b39200 [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 Zhangebd115e2021-03-08 16:10:25 -0500171 verifier_step_ == VerifierStep::kVerifyTargetHash &&
172 dynamic_control_->IsDynamicPartition(partition.name,
173 install_plan_.target_slot)) {
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400174 success = InitializeFdVABC();
175 } else {
176 if (part_path.empty()) {
177 if (partition_size_ == 0) {
178 LOG(INFO) << "Skip hashing partition " << partition_index_ << " ("
179 << partition.name << ") because size is 0.";
180 partition_index_++;
181 StartPartitionHashing();
182 return;
183 }
184 LOG(ERROR) << "Cannot hash partition " << partition_index_ << " ("
185 << partition.name
186 << ") because its device path cannot be determined.";
187 Cleanup(ErrorCode::kFilesystemVerifierError);
188 return;
189 }
190 success = InitializeFd(part_path);
191 }
192 if (!success) {
Sen Jiang57f91802017-11-14 17:42:13 -0800193 Cleanup(ErrorCode::kFilesystemVerifierError);
194 return;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700195 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700196 buffer_.resize(kReadFileBufferSize);
Sen Jiang57f91802017-11-14 17:42:13 -0800197 hasher_ = std::make_unique<HashCalculator>();
198
199 offset_ = 0;
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400200 if (ShouldWriteVerity()) {
201 if (!verity_writer_->Init(partition, read_fd_, write_fd_)) {
Sen Jiang57f91802017-11-14 17:42:13 -0800202 Cleanup(ErrorCode::kVerityCalculationError);
203 return;
204 }
205 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700206
207 // Start the first read.
208 ScheduleRead();
209}
210
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400211bool FilesystemVerifierAction::ShouldWriteVerity() {
212 const InstallPlan::Partition& partition =
213 install_plan_.partitions[partition_index_];
214 return verifier_step_ == VerifierStep::kVerifyTargetHash &&
215 install_plan_.write_verity &&
216 (partition.hash_tree_size > 0 || partition.fec_size > 0);
217}
218
Alex Deymob9e8e262015-08-03 20:23:03 -0700219void FilesystemVerifierAction::ScheduleRead() {
Sen Jiang57f91802017-11-14 17:42:13 -0800220 const InstallPlan::Partition& partition =
221 install_plan_.partitions[partition_index_];
222
223 // We can only start reading anything past |hash_tree_offset| after we have
224 // already read all the data blocks that the hash tree covers. The same
225 // applies to FEC.
226 uint64_t read_end = partition_size_;
227 if (partition.hash_tree_size != 0 &&
228 offset_ < partition.hash_tree_data_offset + partition.hash_tree_data_size)
229 read_end = std::min(read_end, partition.hash_tree_offset);
230 if (partition.fec_size != 0 &&
231 offset_ < partition.fec_data_offset + partition.fec_data_size)
232 read_end = std::min(read_end, partition.fec_offset);
233 size_t bytes_to_read =
234 std::min(static_cast<uint64_t>(buffer_.size()), read_end - offset_);
Alex Deymob9e8e262015-08-03 20:23:03 -0700235 if (!bytes_to_read) {
Sen Jiang57f91802017-11-14 17:42:13 -0800236 FinishPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700237 return;
238 }
239
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400240 auto bytes_read = read_fd_->Read(buffer_.data(), bytes_to_read);
241 if (bytes_read < 0) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700242 LOG(ERROR) << "Unable to schedule an asynchronous read from the stream.";
243 Cleanup(ErrorCode::kError);
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400244 } else {
245 // We could just invoke |OnReadDoneCallback()|, it works. But |PostTask|
246 // is used so that users can cancel updates.
247 pending_task_id_ = brillo::MessageLoop::current()->PostTask(
248 base::Bind(&FilesystemVerifierAction::OnReadDone,
249 base::Unretained(this),
250 bytes_read));
Allie Woodeb9e6d82015-04-17 13:55:30 -0700251 }
252}
253
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400254void FilesystemVerifierAction::OnReadDone(size_t bytes_read) {
Sen Jiang57f91802017-11-14 17:42:13 -0800255 if (cancelled_) {
256 Cleanup(ErrorCode::kError);
257 return;
258 }
Alex Deymob9e8e262015-08-03 20:23:03 -0700259 if (bytes_read == 0) {
Sen Jiang57f91802017-11-14 17:42:13 -0800260 LOG(ERROR) << "Failed to read the remaining " << partition_size_ - offset_
261 << " bytes from partition "
262 << install_plan_.partitions[partition_index_].name;
263 Cleanup(ErrorCode::kFilesystemVerifierError);
264 return;
265 }
266
267 if (!hasher_->Update(buffer_.data(), bytes_read)) {
268 LOG(ERROR) << "Unable to update the hash.";
269 Cleanup(ErrorCode::kError);
270 return;
271 }
272
Kelvin Zhang70eef232020-06-12 20:32:40 +0000273 // WE don't consider sizes of each partition. Every partition
274 // has the same length on progress bar.
275 // TODO(zhangkelvin) Take sizes of each partition into account
276
277 UpdateProgress(
278 (static_cast<double>(offset_) / partition_size_ + partition_index_) /
279 install_plan_.partitions.size());
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400280 if (ShouldWriteVerity()) {
Sen Jiang57f91802017-11-14 17:42:13 -0800281 if (!verity_writer_->Update(offset_, buffer_.data(), bytes_read)) {
282 Cleanup(ErrorCode::kVerityCalculationError);
Alex Deymob9e8e262015-08-03 20:23:03 -0700283 return;
284 }
285 }
286
Sen Jiang57f91802017-11-14 17:42:13 -0800287 offset_ += bytes_read;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700288
Sen Jiang57f91802017-11-14 17:42:13 -0800289 if (offset_ == partition_size_) {
290 FinishPartitionHashing();
291 return;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700292 }
Sen Jiang57f91802017-11-14 17:42:13 -0800293
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700294 ScheduleRead();
Alex Deymob9e8e262015-08-03 20:23:03 -0700295}
296
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700297void FilesystemVerifierAction::FinishPartitionHashing() {
298 if (!hasher_->Finalize()) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700299 LOG(ERROR) << "Unable to finalize the hash.";
Sen Jiang57f91802017-11-14 17:42:13 -0800300 Cleanup(ErrorCode::kError);
301 return;
Alex Deymob9e8e262015-08-03 20:23:03 -0700302 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700303 InstallPlan::Partition& partition =
304 install_plan_.partitions[partition_index_];
Sen Jiang2703ef42017-03-16 13:36:21 -0700305 LOG(INFO) << "Hash of " << partition.name << ": "
306 << Base64Encode(hasher_->raw_hash());
Alex Deymob9e8e262015-08-03 20:23:03 -0700307
Sen Jiangfef85fd2016-03-25 15:32:49 -0700308 switch (verifier_step_) {
309 case VerifierStep::kVerifyTargetHash:
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700310 if (partition.target_hash != hasher_->raw_hash()) {
311 LOG(ERROR) << "New '" << partition.name
312 << "' partition verification failed.";
Sen Jiangcdd52062017-05-18 15:33:10 -0700313 if (partition.source_hash.empty()) {
314 // No need to verify source if it is a full payload.
Sen Jiang57f91802017-11-14 17:42:13 -0800315 Cleanup(ErrorCode::kNewRootfsVerificationError);
316 return;
Sen Jiangcdd52062017-05-18 15:33:10 -0700317 }
Sen Jiangfef85fd2016-03-25 15:32:49 -0700318 // If we have not verified source partition yet, now that the target
Sen Jiang65566a32016-04-06 13:35:36 -0700319 // partition does not match, and it's not a full payload, we need to
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400320 // switch to kVerifySourceHash step to check if it's because the
321 // source partition does not match either.
Sen Jiangfef85fd2016-03-25 15:32:49 -0700322 verifier_step_ = VerifierStep::kVerifySourceHash;
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800323 } else {
324 partition_index_++;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700325 }
326 break;
Sen Jiangfef85fd2016-03-25 15:32:49 -0700327 case VerifierStep::kVerifySourceHash:
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800328 if (partition.source_hash != hasher_->raw_hash()) {
329 LOG(ERROR) << "Old '" << partition.name
330 << "' partition verification failed.";
Sen Jiangfef85fd2016-03-25 15:32:49 -0700331 LOG(ERROR) << "This is a server-side error due to mismatched delta"
332 << " update image!";
333 LOG(ERROR) << "The delta I've been given contains a " << partition.name
334 << " delta update that must be applied over a "
335 << partition.name << " with a specific checksum, but the "
336 << partition.name
337 << " we're starting with doesn't have that checksum! This"
338 " means that the delta I've been given doesn't match my"
339 " existing system. The "
340 << partition.name << " partition I have has hash: "
Sen Jiang2703ef42017-03-16 13:36:21 -0700341 << Base64Encode(hasher_->raw_hash())
Sen Jiangfef85fd2016-03-25 15:32:49 -0700342 << " but the update expected me to have "
Sen Jiang2703ef42017-03-16 13:36:21 -0700343 << Base64Encode(partition.source_hash) << " .";
Sen Jiangfef85fd2016-03-25 15:32:49 -0700344 LOG(INFO) << "To get the checksum of the " << partition.name
345 << " partition run this command: dd if="
346 << partition.source_path
347 << " bs=1M count=" << partition.source_size
348 << " iflag=count_bytes 2>/dev/null | openssl dgst -sha256 "
349 "-binary | openssl base64";
350 LOG(INFO) << "To get the checksum of partitions in a bin file, "
351 << "run: .../src/scripts/sha256_partitions.sh .../file.bin";
Sen Jiang57f91802017-11-14 17:42:13 -0800352 Cleanup(ErrorCode::kDownloadStateInitializationError);
353 return;
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800354 }
Sen Jianga35896c2016-05-25 11:08:41 -0700355 // The action will skip kVerifySourceHash step if target partition hash
356 // matches, if we are in this step, it means target hash does not match,
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400357 // and now that the source partition hash matches, we should set the
358 // error code to reflect the error in target partition. We only need to
359 // verify the source partition which the target hash does not match, the
360 // rest of the partitions don't matter.
Sen Jiang57f91802017-11-14 17:42:13 -0800361 Cleanup(ErrorCode::kNewRootfsVerificationError);
362 return;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700363 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700364 // Start hashing the next partition, if any.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700365 hasher_.reset();
366 buffer_.clear();
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400367 if (read_fd_) {
368 read_fd_.reset();
369 }
370 if (write_fd_) {
371 write_fd_.reset();
372 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700373 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700374}
375
376} // namespace chromeos_update_engine