blob: 22c8e0bdc3bc110a9cce76bdb7e14d70ed5cabd0 [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>
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -050035#include <brillo/secure_blob.h>
Kelvin Zhangec205cf2020-09-28 13:23:40 -040036#include <brillo/streams/file_stream.h>
Allie Woodeb9e6d82015-04-17 13:55:30 -070037
Kelvin Zhang8704c832021-05-10 17:53:14 -040038#include "common/error_code.h"
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -050039#include "payload_generator/delta_diff_generator.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080040#include "update_engine/common/utils.h"
Kelvin Zhangec205cf2020-09-28 13:23:40 -040041#include "update_engine/payload_consumer/file_descriptor.h"
Allie Woodeb9e6d82015-04-17 13:55:30 -070042
Sen Jiang2703ef42017-03-16 13:36:21 -070043using brillo::data_encoding::Base64Encode;
Allie Woodeb9e6d82015-04-17 13:55:30 -070044using std::string;
45
Kelvin Zhang7f925672021-03-15 13:37:40 -040046// On a partition with verity enabled, we expect to see the following format:
47// ===================================================
48// Normal Filesystem Data
49// (this should take most of the space, like over 90%)
50// ===================================================
51// Hash tree
52// ~0.8% (e.g. 16M for 2GB image)
53// ===================================================
54// FEC data
55// ~0.8%
56// ===================================================
57// Footer
58// 4K
59// ===================================================
60
61// For OTA that doesn't do on device verity computation, hash tree and fec data
62// are written during DownloadAction as a regular InstallOp, so no special
63// handling needed, we can just read the entire partition in 1 go.
64
65// Verity enabled case: Only Normal FS data is written during download action.
66// When hasing the entire partition, we will need to build the hash tree, write
67// it to disk, then build FEC, and write it to disk. Therefore, it is important
68// that we finish writing hash tree before we attempt to read & hash it. The
69// same principal applies to FEC data.
70
71// |verity_writer_| handles building and
72// writing of FEC/HashTree, we just need to be careful when reading.
73// Specifically, we must stop at beginning of Hash tree, let |verity_writer_|
74// write both hash tree and FEC, then continue reading the remaining part of
75// partition.
76
Allie Woodeb9e6d82015-04-17 13:55:30 -070077namespace chromeos_update_engine {
78
79namespace {
Alex Deymo20c99202015-07-09 16:14:16 -070080const off_t kReadFileBufferSize = 128 * 1024;
Kelvin Zhang1d99ae12021-05-12 13:29:27 -040081constexpr float kVerityProgressPercent = 0.6;
Allie Woodeb9e6d82015-04-17 13:55:30 -070082} // namespace
83
Allie Woodeb9e6d82015-04-17 13:55:30 -070084void FilesystemVerifierAction::PerformAction() {
85 // Will tell the ActionProcessor we've failed if we return.
86 ScopedActionCompleter abort_action_completer(processor_, this);
87
88 if (!HasInputObject()) {
89 LOG(ERROR) << "FilesystemVerifierAction missing input object.";
90 return;
91 }
92 install_plan_ = GetInputObject();
93
Alex Deymoe5e5fe92015-10-05 09:28:19 -070094 if (install_plan_.partitions.empty()) {
95 LOG(INFO) << "No partitions to verify.";
Allie Woodeb9e6d82015-04-17 13:55:30 -070096 if (HasOutputPipe())
97 SetOutputObject(install_plan_);
98 abort_action_completer.set_code(ErrorCode::kSuccess);
99 return;
100 }
Jae Hoon Kim50504d62020-04-23 14:32:38 -0700101 install_plan_.Dump();
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700102 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700103 abort_action_completer.set_should_complete(false);
104}
105
106void FilesystemVerifierAction::TerminateProcessing() {
Alex Deymo20c99202015-07-09 16:14:16 -0700107 cancelled_ = true;
108 Cleanup(ErrorCode::kSuccess); // error code is ignored if canceled_ is true.
Allie Woodeb9e6d82015-04-17 13:55:30 -0700109}
110
Allie Woodeb9e6d82015-04-17 13:55:30 -0700111void FilesystemVerifierAction::Cleanup(ErrorCode code) {
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500112 partition_fd_.reset();
Alex Deymo20c99202015-07-09 16:14:16 -0700113 // This memory is not used anymore.
114 buffer_.clear();
115
Kelvin Zhang9105f4b2021-04-26 13:44:49 -0400116 // If we didn't write verity, partitions were maped. Releaase resource now.
117 if (!install_plan_.write_verity &&
118 dynamic_control_->UpdateUsesSnapshotCompression()) {
119 LOG(INFO) << "Not writing verity and VABC is enabled, unmapping all "
120 "partitions";
121 dynamic_control_->UnmapAllPartitions();
122 }
123
Allie Woodeb9e6d82015-04-17 13:55:30 -0700124 if (cancelled_)
125 return;
126 if (code == ErrorCode::kSuccess && HasOutputPipe())
127 SetOutputObject(install_plan_);
Kelvin Zhang70eef232020-06-12 20:32:40 +0000128 UpdateProgress(1.0);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700129 processor_->ActionComplete(this, code);
130}
131
Kelvin Zhang70eef232020-06-12 20:32:40 +0000132void FilesystemVerifierAction::UpdateProgress(double progress) {
133 if (delegate_ != nullptr) {
134 delegate_->OnVerifyProgressUpdate(progress);
135 }
136}
137
Kelvin Zhang8704c832021-05-10 17:53:14 -0400138void FilesystemVerifierAction::UpdatePartitionProgress(double progress) {
Kelvin Zhang1d99ae12021-05-12 13:29:27 -0400139 // We don't consider sizes of each partition. Every partition
Kelvin Zhang8704c832021-05-10 17:53:14 -0400140 // has the same length on progress bar.
Kelvin Zhang1d99ae12021-05-12 13:29:27 -0400141 // TODO(b/186087589): Take sizes of each partition into account.
Kelvin Zhang8704c832021-05-10 17:53:14 -0400142 UpdateProgress((progress + partition_index_) /
143 install_plan_.partitions.size());
144}
145
146bool FilesystemVerifierAction::InitializeFdVABC(bool should_write_verity) {
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400147 const InstallPlan::Partition& partition =
148 install_plan_.partitions[partition_index_];
149
Kelvin Zhang8704c832021-05-10 17:53:14 -0400150 if (!should_write_verity) {
151 // In VABC, we cannot map/unmap partitions w/o first closing ALL fds first.
152 // Since this function might be called inside a ScheduledTask, the closure
153 // might have a copy of partition_fd_ when executing this function. Which
154 // means even if we do |partition_fd_.reset()| here, there's a chance that
155 // underlying fd isn't closed until we return. This is unacceptable, we need
156 // to close |partition_fd| right away.
157 if (partition_fd_) {
158 partition_fd_->Close();
159 partition_fd_.reset();
160 }
Kelvin Zhang9105f4b2021-04-26 13:44:49 -0400161 // In VABC, if we are not writing verity, just map all partitions,
162 // and read using regular fd on |postinstall_mount_device| .
163 // All read will go through snapuserd, which provides a consistent
164 // view: device will use snapuserd to read partition during boot.
165 // b/186196758
166 // Call UnmapAllPartitions() first, because if we wrote verity before, these
167 // writes won't be visible to previously opened snapuserd daemon. To ensure
168 // that we will see the most up to date data from partitions, call Unmap()
169 // then Map() to re-spin daemon.
170 dynamic_control_->UnmapAllPartitions();
171 dynamic_control_->MapAllPartitions();
172 return InitializeFd(partition.readonly_target_path);
173 }
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500174 partition_fd_ =
Kelvin Zhang21a49912021-03-12 14:28:33 -0500175 dynamic_control_->OpenCowFd(partition.name, partition.source_path, true);
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500176 if (!partition_fd_) {
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400177 LOG(ERROR) << "OpenCowReader(" << partition.name << ", "
178 << partition.source_path << ") failed.";
179 return false;
180 }
181 partition_size_ = partition.target_size;
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400182 return true;
183}
184
185bool FilesystemVerifierAction::InitializeFd(const std::string& part_path) {
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500186 partition_fd_ = FileDescriptorPtr(new EintrSafeFileDescriptor());
187 const bool write_verity = ShouldWriteVerity();
188 int flags = write_verity ? O_RDWR : O_RDONLY;
189 if (!utils::SetBlockDeviceReadOnly(part_path, !write_verity)) {
190 LOG(WARNING) << "Failed to set block device " << part_path << " as "
191 << (write_verity ? "writable" : "readonly");
192 }
193 if (!partition_fd_->Open(part_path.c_str(), flags)) {
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400194 LOG(ERROR) << "Unable to open " << part_path << " for reading.";
195 return false;
196 }
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400197 return true;
198}
199
Kelvin Zhang8704c832021-05-10 17:53:14 -0400200void FilesystemVerifierAction::WriteVerityAndHashPartition(
201 FileDescriptorPtr fd,
202 const off64_t start_offset,
203 const off64_t end_offset,
204 void* buffer,
205 const size_t buffer_size) {
206 if (start_offset >= end_offset) {
207 LOG_IF(WARNING, start_offset > end_offset)
208 << "start_offset is greater than end_offset : " << start_offset << " > "
209 << end_offset;
210 if (!verity_writer_->Finalize(fd, fd)) {
211 LOG(ERROR) << "Failed to write verity data";
212 Cleanup(ErrorCode::kVerityCalculationError);
213 return;
214 }
215 if (dynamic_control_->UpdateUsesSnapshotCompression()) {
Kelvin Zhang1d99ae12021-05-12 13:29:27 -0400216 // Spin up snapuserd to read fs.
Kelvin Zhang8704c832021-05-10 17:53:14 -0400217 if (!InitializeFdVABC(false)) {
218 LOG(ERROR) << "Failed to map all partitions";
219 Cleanup(ErrorCode::kFilesystemVerifierError);
220 return;
221 }
222 }
223 HashPartition(partition_fd_, 0, partition_size_, buffer, buffer_size);
224 return;
225 }
226 const auto cur_offset = fd->Seek(start_offset, SEEK_SET);
227 if (cur_offset != start_offset) {
228 PLOG(ERROR) << "Failed to seek to offset: " << start_offset;
229 Cleanup(ErrorCode::kVerityCalculationError);
230 return;
231 }
232 const auto read_size =
233 std::min<size_t>(buffer_size, end_offset - start_offset);
234 const auto bytes_read = fd->Read(buffer, read_size);
235 if (bytes_read < 0 || static_cast<size_t>(bytes_read) != read_size) {
236 PLOG(ERROR) << "Failed to read offset " << start_offset << " expected "
237 << read_size << " bytes, actual: " << bytes_read;
238 Cleanup(ErrorCode::kVerityCalculationError);
239 return;
240 }
241 if (!verity_writer_->Update(
242 start_offset, static_cast<const uint8_t*>(buffer), read_size)) {
243 LOG(ERROR) << "VerityWriter::Update() failed";
244 Cleanup(ErrorCode::kVerityCalculationError);
245 return;
246 }
247 UpdatePartitionProgress((start_offset + bytes_read) * 1.0f / partition_size_ *
Kelvin Zhang1d99ae12021-05-12 13:29:27 -0400248 kVerityProgressPercent);
Kelvin Zhang8704c832021-05-10 17:53:14 -0400249 CHECK(pending_task_id_.PostTask(
250 FROM_HERE,
251 base::BindOnce(&FilesystemVerifierAction::WriteVerityAndHashPartition,
252 base::Unretained(this),
253 fd,
254 start_offset + bytes_read,
255 end_offset,
256 buffer,
257 buffer_size)));
258}
259
260void FilesystemVerifierAction::HashPartition(FileDescriptorPtr fd,
261 const off64_t start_offset,
262 const off64_t end_offset,
263 void* buffer,
264 const size_t buffer_size) {
265 if (start_offset >= end_offset) {
266 LOG_IF(WARNING, start_offset > end_offset)
267 << "start_offset is greater than end_offset : " << start_offset << " > "
268 << end_offset;
269 FinishPartitionHashing();
270 return;
271 }
272 const auto cur_offset = fd->Seek(start_offset, SEEK_SET);
273 if (cur_offset != start_offset) {
274 PLOG(ERROR) << "Failed to seek to offset: " << start_offset;
275 Cleanup(ErrorCode::kFilesystemVerifierError);
276 return;
277 }
278 const auto read_size =
279 std::min<size_t>(buffer_size, end_offset - start_offset);
280 const auto bytes_read = fd->Read(buffer, read_size);
281 if (bytes_read < 0 || static_cast<size_t>(bytes_read) != read_size) {
282 PLOG(ERROR) << "Failed to read offset " << start_offset << " expected "
283 << read_size << " bytes, actual: " << bytes_read;
284 Cleanup(ErrorCode::kFilesystemVerifierError);
285 return;
286 }
287 if (!hasher_->Update(buffer, read_size)) {
288 LOG(ERROR) << "Hasher updated failed on offset" << start_offset;
289 Cleanup(ErrorCode::kFilesystemVerifierError);
290 return;
291 }
292 const auto progress = (start_offset + bytes_read) * 1.0f / partition_size_;
Kelvin Zhang1d99ae12021-05-12 13:29:27 -0400293 UpdatePartitionProgress(progress * (1 - kVerityProgressPercent) +
294 kVerityProgressPercent);
Kelvin Zhang8704c832021-05-10 17:53:14 -0400295 CHECK(pending_task_id_.PostTask(
296 FROM_HERE,
297 base::BindOnce(&FilesystemVerifierAction::HashPartition,
298 base::Unretained(this),
299 fd,
300 start_offset + bytes_read,
301 end_offset,
302 buffer,
303 buffer_size)));
304}
305
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700306void FilesystemVerifierAction::StartPartitionHashing() {
307 if (partition_index_ == install_plan_.partitions.size()) {
Tianjie24f96092020-06-30 12:26:25 -0700308 if (!install_plan_.untouched_dynamic_partitions.empty()) {
309 LOG(INFO) << "Verifying extents of untouched dynamic partitions ["
310 << base::JoinString(install_plan_.untouched_dynamic_partitions,
311 ", ")
312 << "]";
313 if (!dynamic_control_->VerifyExtentsForUntouchedPartitions(
314 install_plan_.source_slot,
315 install_plan_.target_slot,
316 install_plan_.untouched_dynamic_partitions)) {
317 Cleanup(ErrorCode::kFilesystemVerifierError);
318 return;
319 }
320 }
321
Sen Jianga35896c2016-05-25 11:08:41 -0700322 Cleanup(ErrorCode::kSuccess);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700323 return;
324 }
Sen Jiang57f91802017-11-14 17:42:13 -0800325 const InstallPlan::Partition& partition =
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700326 install_plan_.partitions[partition_index_];
Kelvin Zhange012f652021-05-10 16:00:31 -0400327 const auto& part_path = GetPartitionPath();
328 partition_size_ = GetPartitionSize();
Yifan Hong537802d2018-08-15 13:15:42 -0700329
Yifan Hong537802d2018-08-15 13:15:42 -0700330 LOG(INFO) << "Hashing partition " << partition_index_ << " ("
331 << partition.name << ") on device " << part_path;
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400332 auto success = false;
Kelvin Zhange012f652021-05-10 16:00:31 -0400333 if (IsVABC(partition)) {
Kelvin Zhang8704c832021-05-10 17:53:14 -0400334 success = InitializeFdVABC(ShouldWriteVerity());
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400335 } else {
336 if (part_path.empty()) {
337 if (partition_size_ == 0) {
338 LOG(INFO) << "Skip hashing partition " << partition_index_ << " ("
339 << partition.name << ") because size is 0.";
340 partition_index_++;
341 StartPartitionHashing();
342 return;
343 }
344 LOG(ERROR) << "Cannot hash partition " << partition_index_ << " ("
345 << partition.name
346 << ") because its device path cannot be determined.";
347 Cleanup(ErrorCode::kFilesystemVerifierError);
348 return;
349 }
350 success = InitializeFd(part_path);
351 }
352 if (!success) {
Sen Jiang57f91802017-11-14 17:42:13 -0800353 Cleanup(ErrorCode::kFilesystemVerifierError);
354 return;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700355 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700356 buffer_.resize(kReadFileBufferSize);
Sen Jiang57f91802017-11-14 17:42:13 -0800357 hasher_ = std::make_unique<HashCalculator>();
358
359 offset_ = 0;
Kelvin Zhang7f925672021-03-15 13:37:40 -0400360 filesystem_data_end_ = partition_size_;
361 CHECK_LE(partition.hash_tree_offset, partition.fec_offset)
362 << " Hash tree is expected to come before FEC data";
363 if (partition.hash_tree_offset != 0) {
364 filesystem_data_end_ = partition.hash_tree_offset;
365 } else if (partition.fec_offset != 0) {
366 filesystem_data_end_ = partition.fec_offset;
367 }
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400368 if (ShouldWriteVerity()) {
Kelvin Zhang8704c832021-05-10 17:53:14 -0400369 LOG(INFO) << "Verity writes enabled on partition " << partition.name;
Kelvin Zhang7f925672021-03-15 13:37:40 -0400370 if (!verity_writer_->Init(partition)) {
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500371 LOG(INFO) << "Verity writes enabled on partition " << partition.name;
Sen Jiang57f91802017-11-14 17:42:13 -0800372 Cleanup(ErrorCode::kVerityCalculationError);
373 return;
374 }
Kelvin Zhang8704c832021-05-10 17:53:14 -0400375 WriteVerityAndHashPartition(
376 partition_fd_, 0, filesystem_data_end_, buffer_.data(), buffer_.size());
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500377 } else {
378 LOG(INFO) << "Verity writes disabled on partition " << partition.name;
Kelvin Zhang8704c832021-05-10 17:53:14 -0400379 HashPartition(
380 partition_fd_, 0, partition_size_, buffer_.data(), buffer_.size());
Sen Jiang57f91802017-11-14 17:42:13 -0800381 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700382}
383
Kelvin Zhange012f652021-05-10 16:00:31 -0400384bool FilesystemVerifierAction::IsVABC(
385 const InstallPlan::Partition& partition) const {
386 return dynamic_control_->UpdateUsesSnapshotCompression() &&
387 verifier_step_ == VerifierStep::kVerifyTargetHash &&
388 dynamic_control_->IsDynamicPartition(partition.name,
389 install_plan_.target_slot);
390}
391
392const std::string& FilesystemVerifierAction::GetPartitionPath() const {
393 const InstallPlan::Partition& partition =
394 install_plan_.partitions[partition_index_];
395 switch (verifier_step_) {
396 case VerifierStep::kVerifySourceHash:
397 return partition.source_path;
398 case VerifierStep::kVerifyTargetHash:
399 if (IsVABC(partition)) {
400 return partition.readonly_target_path;
401 } else {
402 return partition.target_path;
403 }
404 }
405}
406
407size_t FilesystemVerifierAction::GetPartitionSize() const {
408 const InstallPlan::Partition& partition =
409 install_plan_.partitions[partition_index_];
410 switch (verifier_step_) {
411 case VerifierStep::kVerifySourceHash:
412 return partition.source_size;
413 case VerifierStep::kVerifyTargetHash:
414 return partition.target_size;
415 }
416}
417
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400418bool FilesystemVerifierAction::ShouldWriteVerity() {
419 const InstallPlan::Partition& partition =
420 install_plan_.partitions[partition_index_];
421 return verifier_step_ == VerifierStep::kVerifyTargetHash &&
422 install_plan_.write_verity &&
423 (partition.hash_tree_size > 0 || partition.fec_size > 0);
424}
425
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700426void FilesystemVerifierAction::FinishPartitionHashing() {
427 if (!hasher_->Finalize()) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700428 LOG(ERROR) << "Unable to finalize the hash.";
Sen Jiang57f91802017-11-14 17:42:13 -0800429 Cleanup(ErrorCode::kError);
430 return;
Alex Deymob9e8e262015-08-03 20:23:03 -0700431 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700432 InstallPlan::Partition& partition =
433 install_plan_.partitions[partition_index_];
Sen Jiang2703ef42017-03-16 13:36:21 -0700434 LOG(INFO) << "Hash of " << partition.name << ": "
435 << Base64Encode(hasher_->raw_hash());
Alex Deymob9e8e262015-08-03 20:23:03 -0700436
Sen Jiangfef85fd2016-03-25 15:32:49 -0700437 switch (verifier_step_) {
438 case VerifierStep::kVerifyTargetHash:
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700439 if (partition.target_hash != hasher_->raw_hash()) {
440 LOG(ERROR) << "New '" << partition.name
441 << "' partition verification failed.";
Sen Jiangcdd52062017-05-18 15:33:10 -0700442 if (partition.source_hash.empty()) {
443 // No need to verify source if it is a full payload.
Sen Jiang57f91802017-11-14 17:42:13 -0800444 Cleanup(ErrorCode::kNewRootfsVerificationError);
445 return;
Sen Jiangcdd52062017-05-18 15:33:10 -0700446 }
Sen Jiangfef85fd2016-03-25 15:32:49 -0700447 // If we have not verified source partition yet, now that the target
Sen Jiang65566a32016-04-06 13:35:36 -0700448 // partition does not match, and it's not a full payload, we need to
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400449 // switch to kVerifySourceHash step to check if it's because the
450 // source partition does not match either.
Sen Jiangfef85fd2016-03-25 15:32:49 -0700451 verifier_step_ = VerifierStep::kVerifySourceHash;
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800452 } else {
453 partition_index_++;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700454 }
455 break;
Sen Jiangfef85fd2016-03-25 15:32:49 -0700456 case VerifierStep::kVerifySourceHash:
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800457 if (partition.source_hash != hasher_->raw_hash()) {
458 LOG(ERROR) << "Old '" << partition.name
459 << "' partition verification failed.";
Sen Jiangfef85fd2016-03-25 15:32:49 -0700460 LOG(ERROR) << "This is a server-side error due to mismatched delta"
461 << " update image!";
462 LOG(ERROR) << "The delta I've been given contains a " << partition.name
463 << " delta update that must be applied over a "
464 << partition.name << " with a specific checksum, but the "
465 << partition.name
466 << " we're starting with doesn't have that checksum! This"
467 " means that the delta I've been given doesn't match my"
468 " existing system. The "
469 << partition.name << " partition I have has hash: "
Sen Jiang2703ef42017-03-16 13:36:21 -0700470 << Base64Encode(hasher_->raw_hash())
Sen Jiangfef85fd2016-03-25 15:32:49 -0700471 << " but the update expected me to have "
Sen Jiang2703ef42017-03-16 13:36:21 -0700472 << Base64Encode(partition.source_hash) << " .";
Sen Jiangfef85fd2016-03-25 15:32:49 -0700473 LOG(INFO) << "To get the checksum of the " << partition.name
474 << " partition run this command: dd if="
475 << partition.source_path
476 << " bs=1M count=" << partition.source_size
477 << " iflag=count_bytes 2>/dev/null | openssl dgst -sha256 "
478 "-binary | openssl base64";
479 LOG(INFO) << "To get the checksum of partitions in a bin file, "
480 << "run: .../src/scripts/sha256_partitions.sh .../file.bin";
Sen Jiang57f91802017-11-14 17:42:13 -0800481 Cleanup(ErrorCode::kDownloadStateInitializationError);
482 return;
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800483 }
Sen Jianga35896c2016-05-25 11:08:41 -0700484 // The action will skip kVerifySourceHash step if target partition hash
485 // matches, if we are in this step, it means target hash does not match,
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400486 // and now that the source partition hash matches, we should set the
487 // error code to reflect the error in target partition. We only need to
488 // verify the source partition which the target hash does not match, the
489 // rest of the partitions don't matter.
Sen Jiang57f91802017-11-14 17:42:13 -0800490 Cleanup(ErrorCode::kNewRootfsVerificationError);
491 return;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700492 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700493 // Start hashing the next partition, if any.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700494 hasher_.reset();
495 buffer_.clear();
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500496 if (partition_fd_) {
Kelvin Zhang8704c832021-05-10 17:53:14 -0400497 partition_fd_->Close();
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500498 partition_fd_.reset();
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400499 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700500 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700501}
502
503} // namespace chromeos_update_engine