blob: 1a6e421e91082d9f0db308b42dc3cbdee5b0202f [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>
Daniel Zhengbd16b012022-09-28 23:40:05 +000027#include <functional>
Kelvin Zhangec205cf2020-09-28 13:23:40 -040028#include <memory>
Kelvin Zhang5b145822022-07-26 10:56:09 -070029#include <numeric>
Allie Woodeb9e6d82015-04-17 13:55:30 -070030#include <string>
Kelvin Zhangec205cf2020-09-28 13:23:40 -040031#include <utility>
Allie Woodeb9e6d82015-04-17 13:55:30 -070032
Alex Deymo20c99202015-07-09 16:14:16 -070033#include <base/bind.h>
Tianjie24f96092020-06-30 12:26:25 -070034#include <base/strings/string_util.h>
Kelvin Zhangec205cf2020-09-28 13:23:40 -040035#include <brillo/data_encoding.h>
36#include <brillo/message_loops/message_loop.h>
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -050037#include <brillo/secure_blob.h>
Kelvin Zhangec205cf2020-09-28 13:23:40 -040038#include <brillo/streams/file_stream.h>
Allie Woodeb9e6d82015-04-17 13:55:30 -070039
Kelvin Zhang8704c832021-05-10 17:53:14 -040040#include "common/error_code.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080041#include "update_engine/common/utils.h"
Kelvin Zhangec205cf2020-09-28 13:23:40 -040042#include "update_engine/payload_consumer/file_descriptor.h"
Allie Woodeb9e6d82015-04-17 13:55:30 -070043
Sen Jiang2703ef42017-03-16 13:36:21 -070044using brillo::data_encoding::Base64Encode;
Allie Woodeb9e6d82015-04-17 13:55:30 -070045using std::string;
46
Kelvin Zhang7f925672021-03-15 13:37:40 -040047// On a partition with verity enabled, we expect to see the following format:
48// ===================================================
49// Normal Filesystem Data
50// (this should take most of the space, like over 90%)
51// ===================================================
52// Hash tree
53// ~0.8% (e.g. 16M for 2GB image)
54// ===================================================
55// FEC data
56// ~0.8%
57// ===================================================
58// Footer
59// 4K
60// ===================================================
61
62// For OTA that doesn't do on device verity computation, hash tree and fec data
63// are written during DownloadAction as a regular InstallOp, so no special
64// handling needed, we can just read the entire partition in 1 go.
65
66// Verity enabled case: Only Normal FS data is written during download action.
67// When hasing the entire partition, we will need to build the hash tree, write
68// it to disk, then build FEC, and write it to disk. Therefore, it is important
69// that we finish writing hash tree before we attempt to read & hash it. The
70// same principal applies to FEC data.
71
72// |verity_writer_| handles building and
73// writing of FEC/HashTree, we just need to be careful when reading.
74// Specifically, we must stop at beginning of Hash tree, let |verity_writer_|
75// write both hash tree and FEC, then continue reading the remaining part of
76// partition.
77
Allie Woodeb9e6d82015-04-17 13:55:30 -070078namespace chromeos_update_engine {
79
80namespace {
Alex Deymo20c99202015-07-09 16:14:16 -070081const off_t kReadFileBufferSize = 128 * 1024;
Kelvin Zhang1d99ae12021-05-12 13:29:27 -040082constexpr float kVerityProgressPercent = 0.6;
Allie Woodeb9e6d82015-04-17 13:55:30 -070083} // namespace
84
Allie Woodeb9e6d82015-04-17 13:55:30 -070085void FilesystemVerifierAction::PerformAction() {
86 // Will tell the ActionProcessor we've failed if we return.
87 ScopedActionCompleter abort_action_completer(processor_, this);
88
89 if (!HasInputObject()) {
90 LOG(ERROR) << "FilesystemVerifierAction missing input object.";
91 return;
92 }
93 install_plan_ = GetInputObject();
94
Alex Deymoe5e5fe92015-10-05 09:28:19 -070095 if (install_plan_.partitions.empty()) {
96 LOG(INFO) << "No partitions to verify.";
Allie Woodeb9e6d82015-04-17 13:55:30 -070097 if (HasOutputPipe())
98 SetOutputObject(install_plan_);
99 abort_action_completer.set_code(ErrorCode::kSuccess);
100 return;
101 }
Kelvin Zhang5b145822022-07-26 10:56:09 -0700102 // partition_weight_[i] = total size of partitions before index i.
103 partition_weight_.clear();
104 partition_weight_.reserve(install_plan_.partitions.size() + 1);
105 partition_weight_.push_back(0);
106 for (const auto& part : install_plan_.partitions) {
107 partition_weight_.push_back(part.target_size);
108 }
109 std::partial_sum(partition_weight_.begin(),
110 partition_weight_.end(),
111 partition_weight_.begin(),
112 std::plus<size_t>());
113
Jae Hoon Kim50504d62020-04-23 14:32:38 -0700114 install_plan_.Dump();
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700115 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700116 abort_action_completer.set_should_complete(false);
117}
118
119void FilesystemVerifierAction::TerminateProcessing() {
Alex Deymo20c99202015-07-09 16:14:16 -0700120 cancelled_ = true;
121 Cleanup(ErrorCode::kSuccess); // error code is ignored if canceled_ is true.
Allie Woodeb9e6d82015-04-17 13:55:30 -0700122}
123
Allie Woodeb9e6d82015-04-17 13:55:30 -0700124void FilesystemVerifierAction::Cleanup(ErrorCode code) {
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500125 partition_fd_.reset();
Alex Deymo20c99202015-07-09 16:14:16 -0700126 // This memory is not used anymore.
127 buffer_.clear();
128
Kelvin Zhang9105f4b2021-04-26 13:44:49 -0400129 // If we didn't write verity, partitions were maped. Releaase resource now.
130 if (!install_plan_.write_verity &&
131 dynamic_control_->UpdateUsesSnapshotCompression()) {
132 LOG(INFO) << "Not writing verity and VABC is enabled, unmapping all "
133 "partitions";
134 dynamic_control_->UnmapAllPartitions();
135 }
136
Allie Woodeb9e6d82015-04-17 13:55:30 -0700137 if (cancelled_)
138 return;
139 if (code == ErrorCode::kSuccess && HasOutputPipe())
140 SetOutputObject(install_plan_);
Kelvin Zhang70eef232020-06-12 20:32:40 +0000141 UpdateProgress(1.0);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700142 processor_->ActionComplete(this, code);
143}
144
Kelvin Zhang70eef232020-06-12 20:32:40 +0000145void FilesystemVerifierAction::UpdateProgress(double progress) {
146 if (delegate_ != nullptr) {
147 delegate_->OnVerifyProgressUpdate(progress);
148 }
149}
150
Kelvin Zhang8704c832021-05-10 17:53:14 -0400151void FilesystemVerifierAction::UpdatePartitionProgress(double progress) {
Kelvin Zhang5b145822022-07-26 10:56:09 -0700152 UpdateProgress((partition_weight_[partition_index_] * (1 - progress) +
153 partition_weight_[partition_index_ + 1] * progress) /
154 partition_weight_.back());
Kelvin Zhang8704c832021-05-10 17:53:14 -0400155}
156
157bool FilesystemVerifierAction::InitializeFdVABC(bool should_write_verity) {
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400158 const InstallPlan::Partition& partition =
159 install_plan_.partitions[partition_index_];
160
Kelvin Zhang8704c832021-05-10 17:53:14 -0400161 if (!should_write_verity) {
162 // In VABC, we cannot map/unmap partitions w/o first closing ALL fds first.
163 // Since this function might be called inside a ScheduledTask, the closure
164 // might have a copy of partition_fd_ when executing this function. Which
165 // means even if we do |partition_fd_.reset()| here, there's a chance that
166 // underlying fd isn't closed until we return. This is unacceptable, we need
167 // to close |partition_fd| right away.
168 if (partition_fd_) {
169 partition_fd_->Close();
170 partition_fd_.reset();
171 }
Kelvin Zhang9105f4b2021-04-26 13:44:49 -0400172 // In VABC, if we are not writing verity, just map all partitions,
173 // and read using regular fd on |postinstall_mount_device| .
174 // All read will go through snapuserd, which provides a consistent
175 // view: device will use snapuserd to read partition during boot.
176 // b/186196758
177 // Call UnmapAllPartitions() first, because if we wrote verity before, these
178 // writes won't be visible to previously opened snapuserd daemon. To ensure
179 // that we will see the most up to date data from partitions, call Unmap()
180 // then Map() to re-spin daemon.
181 dynamic_control_->UnmapAllPartitions();
182 dynamic_control_->MapAllPartitions();
183 return InitializeFd(partition.readonly_target_path);
184 }
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500185 partition_fd_ =
Kelvin Zhang21a49912021-03-12 14:28:33 -0500186 dynamic_control_->OpenCowFd(partition.name, partition.source_path, true);
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500187 if (!partition_fd_) {
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400188 LOG(ERROR) << "OpenCowReader(" << partition.name << ", "
189 << partition.source_path << ") failed.";
190 return false;
191 }
192 partition_size_ = partition.target_size;
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400193 return true;
194}
195
196bool FilesystemVerifierAction::InitializeFd(const std::string& part_path) {
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800197 partition_fd_ = std::make_unique<EintrSafeFileDescriptor>();
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500198 const bool write_verity = ShouldWriteVerity();
199 int flags = write_verity ? O_RDWR : O_RDONLY;
200 if (!utils::SetBlockDeviceReadOnly(part_path, !write_verity)) {
201 LOG(WARNING) << "Failed to set block device " << part_path << " as "
202 << (write_verity ? "writable" : "readonly");
203 }
204 if (!partition_fd_->Open(part_path.c_str(), flags)) {
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400205 LOG(ERROR) << "Unable to open " << part_path << " for reading.";
206 return false;
207 }
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400208 return true;
209}
210
Daniel Zhengbd16b012022-09-28 23:40:05 +0000211void FilesystemVerifierAction::WriteVerityData(FileDescriptor* fd,
212 void* buffer,
213 const size_t buffer_size) {
214 if (verity_writer_->FECFinished()) {
215 LOG(INFO) << "EncodeFEC is completed. Resuming other tasks";
216 if (dynamic_control_->UpdateUsesSnapshotCompression()) {
217 // Spin up snapuserd to read fs.
218 if (!InitializeFdVABC(false)) {
219 LOG(ERROR) << "Failed to map all partitions";
220 Cleanup(ErrorCode::kFilesystemVerifierError);
221 return;
222 }
223 }
224 HashPartition(0, partition_size_, buffer, buffer_size);
225 return;
226 }
227 if (!verity_writer_->IncrementalFinalize(fd, fd)) {
228 LOG(ERROR) << "Failed to write verity data";
229 Cleanup(ErrorCode::kVerityCalculationError);
230 }
231 CHECK(pending_task_id_.PostTask(
232 FROM_HERE,
233 base::BindOnce(&FilesystemVerifierAction::WriteVerityData,
234 base::Unretained(this),
235 fd,
236 buffer,
237 buffer_size)));
238}
239
Kelvin Zhang8704c832021-05-10 17:53:14 -0400240void FilesystemVerifierAction::WriteVerityAndHashPartition(
Kelvin Zhang8704c832021-05-10 17:53:14 -0400241 const off64_t start_offset,
242 const off64_t end_offset,
243 void* buffer,
244 const size_t buffer_size) {
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800245 auto fd = partition_fd_.get();
246 TEST_AND_RETURN(fd != nullptr);
Kelvin Zhang8704c832021-05-10 17:53:14 -0400247 if (start_offset >= end_offset) {
248 LOG_IF(WARNING, start_offset > end_offset)
249 << "start_offset is greater than end_offset : " << start_offset << " > "
250 << end_offset;
Daniel Zhengbd16b012022-09-28 23:40:05 +0000251 WriteVerityData(fd, buffer, buffer_size);
Kelvin Zhang8704c832021-05-10 17:53:14 -0400252 return;
253 }
254 const auto cur_offset = fd->Seek(start_offset, SEEK_SET);
255 if (cur_offset != start_offset) {
256 PLOG(ERROR) << "Failed to seek to offset: " << start_offset;
257 Cleanup(ErrorCode::kVerityCalculationError);
258 return;
259 }
260 const auto read_size =
261 std::min<size_t>(buffer_size, end_offset - start_offset);
262 const auto bytes_read = fd->Read(buffer, read_size);
263 if (bytes_read < 0 || static_cast<size_t>(bytes_read) != read_size) {
264 PLOG(ERROR) << "Failed to read offset " << start_offset << " expected "
265 << read_size << " bytes, actual: " << bytes_read;
266 Cleanup(ErrorCode::kVerityCalculationError);
267 return;
268 }
269 if (!verity_writer_->Update(
270 start_offset, static_cast<const uint8_t*>(buffer), read_size)) {
271 LOG(ERROR) << "VerityWriter::Update() failed";
272 Cleanup(ErrorCode::kVerityCalculationError);
273 return;
274 }
275 UpdatePartitionProgress((start_offset + bytes_read) * 1.0f / partition_size_ *
Kelvin Zhang1d99ae12021-05-12 13:29:27 -0400276 kVerityProgressPercent);
Kelvin Zhang8704c832021-05-10 17:53:14 -0400277 CHECK(pending_task_id_.PostTask(
278 FROM_HERE,
279 base::BindOnce(&FilesystemVerifierAction::WriteVerityAndHashPartition,
280 base::Unretained(this),
Kelvin Zhang8704c832021-05-10 17:53:14 -0400281 start_offset + bytes_read,
282 end_offset,
283 buffer,
284 buffer_size)));
285}
286
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800287void FilesystemVerifierAction::HashPartition(const off64_t start_offset,
Kelvin Zhang8704c832021-05-10 17:53:14 -0400288 const off64_t end_offset,
289 void* buffer,
290 const size_t buffer_size) {
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800291 auto fd = partition_fd_.get();
292 TEST_AND_RETURN(fd != nullptr);
Kelvin Zhang8704c832021-05-10 17:53:14 -0400293 if (start_offset >= end_offset) {
294 LOG_IF(WARNING, start_offset > end_offset)
295 << "start_offset is greater than end_offset : " << start_offset << " > "
296 << end_offset;
297 FinishPartitionHashing();
298 return;
299 }
300 const auto cur_offset = fd->Seek(start_offset, SEEK_SET);
301 if (cur_offset != start_offset) {
302 PLOG(ERROR) << "Failed to seek to offset: " << start_offset;
303 Cleanup(ErrorCode::kFilesystemVerifierError);
304 return;
305 }
306 const auto read_size =
307 std::min<size_t>(buffer_size, end_offset - start_offset);
308 const auto bytes_read = fd->Read(buffer, read_size);
309 if (bytes_read < 0 || static_cast<size_t>(bytes_read) != read_size) {
310 PLOG(ERROR) << "Failed to read offset " << start_offset << " expected "
311 << read_size << " bytes, actual: " << bytes_read;
312 Cleanup(ErrorCode::kFilesystemVerifierError);
313 return;
314 }
315 if (!hasher_->Update(buffer, read_size)) {
316 LOG(ERROR) << "Hasher updated failed on offset" << start_offset;
317 Cleanup(ErrorCode::kFilesystemVerifierError);
318 return;
319 }
320 const auto progress = (start_offset + bytes_read) * 1.0f / partition_size_;
Kelvin Zhang5b145822022-07-26 10:56:09 -0700321 // If we are writing verity, then the progress bar will be split between
322 // verity writes and partition hashing. Otherwise, the entire progress bar is
323 // dedicated to partition hashing for smooth progress.
324 if (ShouldWriteVerity()) {
325 UpdatePartitionProgress(progress * (1 - kVerityProgressPercent) +
326 kVerityProgressPercent);
327 } else {
328 UpdatePartitionProgress(progress);
329 }
Kelvin Zhang8704c832021-05-10 17:53:14 -0400330 CHECK(pending_task_id_.PostTask(
331 FROM_HERE,
332 base::BindOnce(&FilesystemVerifierAction::HashPartition,
333 base::Unretained(this),
Kelvin Zhang8704c832021-05-10 17:53:14 -0400334 start_offset + bytes_read,
335 end_offset,
336 buffer,
337 buffer_size)));
338}
339
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700340void FilesystemVerifierAction::StartPartitionHashing() {
341 if (partition_index_ == install_plan_.partitions.size()) {
Tianjie24f96092020-06-30 12:26:25 -0700342 if (!install_plan_.untouched_dynamic_partitions.empty()) {
343 LOG(INFO) << "Verifying extents of untouched dynamic partitions ["
344 << base::JoinString(install_plan_.untouched_dynamic_partitions,
345 ", ")
346 << "]";
347 if (!dynamic_control_->VerifyExtentsForUntouchedPartitions(
348 install_plan_.source_slot,
349 install_plan_.target_slot,
350 install_plan_.untouched_dynamic_partitions)) {
351 Cleanup(ErrorCode::kFilesystemVerifierError);
352 return;
353 }
354 }
355
Sen Jianga35896c2016-05-25 11:08:41 -0700356 Cleanup(ErrorCode::kSuccess);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700357 return;
358 }
Sen Jiang57f91802017-11-14 17:42:13 -0800359 const InstallPlan::Partition& partition =
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700360 install_plan_.partitions[partition_index_];
Kelvin Zhange012f652021-05-10 16:00:31 -0400361 const auto& part_path = GetPartitionPath();
362 partition_size_ = GetPartitionSize();
Yifan Hong537802d2018-08-15 13:15:42 -0700363
Yifan Hong537802d2018-08-15 13:15:42 -0700364 LOG(INFO) << "Hashing partition " << partition_index_ << " ("
365 << partition.name << ") on device " << part_path;
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400366 auto success = false;
Kelvin Zhange012f652021-05-10 16:00:31 -0400367 if (IsVABC(partition)) {
Kelvin Zhang8704c832021-05-10 17:53:14 -0400368 success = InitializeFdVABC(ShouldWriteVerity());
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400369 } else {
370 if (part_path.empty()) {
371 if (partition_size_ == 0) {
372 LOG(INFO) << "Skip hashing partition " << partition_index_ << " ("
373 << partition.name << ") because size is 0.";
374 partition_index_++;
375 StartPartitionHashing();
376 return;
377 }
378 LOG(ERROR) << "Cannot hash partition " << partition_index_ << " ("
379 << partition.name
380 << ") because its device path cannot be determined.";
381 Cleanup(ErrorCode::kFilesystemVerifierError);
382 return;
383 }
384 success = InitializeFd(part_path);
385 }
386 if (!success) {
Sen Jiang57f91802017-11-14 17:42:13 -0800387 Cleanup(ErrorCode::kFilesystemVerifierError);
388 return;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700389 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700390 buffer_.resize(kReadFileBufferSize);
Sen Jiang57f91802017-11-14 17:42:13 -0800391 hasher_ = std::make_unique<HashCalculator>();
392
393 offset_ = 0;
Kelvin Zhang7f925672021-03-15 13:37:40 -0400394 filesystem_data_end_ = partition_size_;
Kelvin Zhang5e5ad392021-07-26 21:42:06 -0400395 if (partition.fec_offset > 0) {
396 CHECK_LE(partition.hash_tree_offset, partition.fec_offset)
397 << " Hash tree is expected to come before FEC data";
398 }
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800399 CHECK_NE(partition_fd_, nullptr);
Kelvin Zhang7f925672021-03-15 13:37:40 -0400400 if (partition.hash_tree_offset != 0) {
401 filesystem_data_end_ = partition.hash_tree_offset;
402 } else if (partition.fec_offset != 0) {
403 filesystem_data_end_ = partition.fec_offset;
404 }
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400405 if (ShouldWriteVerity()) {
Kelvin Zhang8704c832021-05-10 17:53:14 -0400406 LOG(INFO) << "Verity writes enabled on partition " << partition.name;
Kelvin Zhang7f925672021-03-15 13:37:40 -0400407 if (!verity_writer_->Init(partition)) {
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500408 LOG(INFO) << "Verity writes enabled on partition " << partition.name;
Sen Jiang57f91802017-11-14 17:42:13 -0800409 Cleanup(ErrorCode::kVerityCalculationError);
410 return;
411 }
Kelvin Zhang8704c832021-05-10 17:53:14 -0400412 WriteVerityAndHashPartition(
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800413 0, filesystem_data_end_, buffer_.data(), buffer_.size());
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500414 } else {
415 LOG(INFO) << "Verity writes disabled on partition " << partition.name;
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800416 HashPartition(0, partition_size_, buffer_.data(), buffer_.size());
Sen Jiang57f91802017-11-14 17:42:13 -0800417 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700418}
419
Kelvin Zhange012f652021-05-10 16:00:31 -0400420bool FilesystemVerifierAction::IsVABC(
421 const InstallPlan::Partition& partition) const {
422 return dynamic_control_->UpdateUsesSnapshotCompression() &&
423 verifier_step_ == VerifierStep::kVerifyTargetHash &&
424 dynamic_control_->IsDynamicPartition(partition.name,
425 install_plan_.target_slot);
426}
427
428const std::string& FilesystemVerifierAction::GetPartitionPath() const {
429 const InstallPlan::Partition& partition =
430 install_plan_.partitions[partition_index_];
431 switch (verifier_step_) {
432 case VerifierStep::kVerifySourceHash:
433 return partition.source_path;
434 case VerifierStep::kVerifyTargetHash:
435 if (IsVABC(partition)) {
436 return partition.readonly_target_path;
437 } else {
438 return partition.target_path;
439 }
440 }
441}
442
443size_t FilesystemVerifierAction::GetPartitionSize() const {
444 const InstallPlan::Partition& partition =
445 install_plan_.partitions[partition_index_];
446 switch (verifier_step_) {
447 case VerifierStep::kVerifySourceHash:
448 return partition.source_size;
449 case VerifierStep::kVerifyTargetHash:
450 return partition.target_size;
451 }
452}
453
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400454bool FilesystemVerifierAction::ShouldWriteVerity() {
455 const InstallPlan::Partition& partition =
456 install_plan_.partitions[partition_index_];
457 return verifier_step_ == VerifierStep::kVerifyTargetHash &&
458 install_plan_.write_verity &&
459 (partition.hash_tree_size > 0 || partition.fec_size > 0);
460}
461
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700462void FilesystemVerifierAction::FinishPartitionHashing() {
463 if (!hasher_->Finalize()) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700464 LOG(ERROR) << "Unable to finalize the hash.";
Sen Jiang57f91802017-11-14 17:42:13 -0800465 Cleanup(ErrorCode::kError);
466 return;
Alex Deymob9e8e262015-08-03 20:23:03 -0700467 }
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800468 const InstallPlan::Partition& partition =
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700469 install_plan_.partitions[partition_index_];
Sen Jiang2703ef42017-03-16 13:36:21 -0700470 LOG(INFO) << "Hash of " << partition.name << ": "
Kelvin Zhang3fe49642021-10-04 15:35:02 -0700471 << HexEncode(hasher_->raw_hash());
Alex Deymob9e8e262015-08-03 20:23:03 -0700472
Sen Jiangfef85fd2016-03-25 15:32:49 -0700473 switch (verifier_step_) {
474 case VerifierStep::kVerifyTargetHash:
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700475 if (partition.target_hash != hasher_->raw_hash()) {
476 LOG(ERROR) << "New '" << partition.name
477 << "' partition verification failed.";
Sen Jiangcdd52062017-05-18 15:33:10 -0700478 if (partition.source_hash.empty()) {
479 // No need to verify source if it is a full payload.
Sen Jiang57f91802017-11-14 17:42:13 -0800480 Cleanup(ErrorCode::kNewRootfsVerificationError);
481 return;
Sen Jiangcdd52062017-05-18 15:33:10 -0700482 }
Sen Jiangfef85fd2016-03-25 15:32:49 -0700483 // If we have not verified source partition yet, now that the target
Sen Jiang65566a32016-04-06 13:35:36 -0700484 // partition does not match, and it's not a full payload, we need to
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400485 // switch to kVerifySourceHash step to check if it's because the
486 // source partition does not match either.
Sen Jiangfef85fd2016-03-25 15:32:49 -0700487 verifier_step_ = VerifierStep::kVerifySourceHash;
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800488 } else {
489 partition_index_++;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700490 }
491 break;
Sen Jiangfef85fd2016-03-25 15:32:49 -0700492 case VerifierStep::kVerifySourceHash:
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800493 if (partition.source_hash != hasher_->raw_hash()) {
494 LOG(ERROR) << "Old '" << partition.name
495 << "' partition verification failed.";
Sen Jiangfef85fd2016-03-25 15:32:49 -0700496 LOG(ERROR) << "This is a server-side error due to mismatched delta"
497 << " update image!";
498 LOG(ERROR) << "The delta I've been given contains a " << partition.name
499 << " delta update that must be applied over a "
500 << partition.name << " with a specific checksum, but the "
501 << partition.name
502 << " we're starting with doesn't have that checksum! This"
503 " means that the delta I've been given doesn't match my"
504 " existing system. The "
505 << partition.name << " partition I have has hash: "
Sen Jiang2703ef42017-03-16 13:36:21 -0700506 << Base64Encode(hasher_->raw_hash())
Sen Jiangfef85fd2016-03-25 15:32:49 -0700507 << " but the update expected me to have "
Sen Jiang2703ef42017-03-16 13:36:21 -0700508 << Base64Encode(partition.source_hash) << " .";
Sen Jiangfef85fd2016-03-25 15:32:49 -0700509 LOG(INFO) << "To get the checksum of the " << partition.name
510 << " partition run this command: dd if="
511 << partition.source_path
512 << " bs=1M count=" << partition.source_size
513 << " iflag=count_bytes 2>/dev/null | openssl dgst -sha256 "
514 "-binary | openssl base64";
515 LOG(INFO) << "To get the checksum of partitions in a bin file, "
516 << "run: .../src/scripts/sha256_partitions.sh .../file.bin";
Sen Jiang57f91802017-11-14 17:42:13 -0800517 Cleanup(ErrorCode::kDownloadStateInitializationError);
518 return;
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800519 }
Sen Jianga35896c2016-05-25 11:08:41 -0700520 // The action will skip kVerifySourceHash step if target partition hash
521 // matches, if we are in this step, it means target hash does not match,
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400522 // and now that the source partition hash matches, we should set the
523 // error code to reflect the error in target partition. We only need to
524 // verify the source partition which the target hash does not match, the
525 // rest of the partitions don't matter.
Sen Jiang57f91802017-11-14 17:42:13 -0800526 Cleanup(ErrorCode::kNewRootfsVerificationError);
527 return;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700528 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700529 // Start hashing the next partition, if any.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700530 buffer_.clear();
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500531 if (partition_fd_) {
Kelvin Zhang8704c832021-05-10 17:53:14 -0400532 partition_fd_->Close();
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500533 partition_fd_.reset();
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400534 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700535 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700536}
537
538} // namespace chromeos_update_engine