blob: 8d496a61d5bc50f93b6a86efec0c44b2cdb9bfc5 [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 Zhang4f28a6c2021-01-14 14:04:57 -050038#include "payload_generator/delta_diff_generator.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080039#include "update_engine/common/utils.h"
Kelvin Zhangec205cf2020-09-28 13:23:40 -040040#include "update_engine/payload_consumer/file_descriptor.h"
Allie Woodeb9e6d82015-04-17 13:55:30 -070041
Sen Jiang2703ef42017-03-16 13:36:21 -070042using brillo::data_encoding::Base64Encode;
Allie Woodeb9e6d82015-04-17 13:55:30 -070043using std::string;
44
Kelvin Zhang7f925672021-03-15 13:37:40 -040045// On a partition with verity enabled, we expect to see the following format:
46// ===================================================
47// Normal Filesystem Data
48// (this should take most of the space, like over 90%)
49// ===================================================
50// Hash tree
51// ~0.8% (e.g. 16M for 2GB image)
52// ===================================================
53// FEC data
54// ~0.8%
55// ===================================================
56// Footer
57// 4K
58// ===================================================
59
60// For OTA that doesn't do on device verity computation, hash tree and fec data
61// are written during DownloadAction as a regular InstallOp, so no special
62// handling needed, we can just read the entire partition in 1 go.
63
64// Verity enabled case: Only Normal FS data is written during download action.
65// When hasing the entire partition, we will need to build the hash tree, write
66// it to disk, then build FEC, and write it to disk. Therefore, it is important
67// that we finish writing hash tree before we attempt to read & hash it. The
68// same principal applies to FEC data.
69
70// |verity_writer_| handles building and
71// writing of FEC/HashTree, we just need to be careful when reading.
72// Specifically, we must stop at beginning of Hash tree, let |verity_writer_|
73// write both hash tree and FEC, then continue reading the remaining part of
74// partition.
75
Allie Woodeb9e6d82015-04-17 13:55:30 -070076namespace chromeos_update_engine {
77
78namespace {
Alex Deymo20c99202015-07-09 16:14:16 -070079const off_t kReadFileBufferSize = 128 * 1024;
Allie Woodeb9e6d82015-04-17 13:55:30 -070080} // namespace
81
Allie Woodeb9e6d82015-04-17 13:55:30 -070082void FilesystemVerifierAction::PerformAction() {
83 // Will tell the ActionProcessor we've failed if we return.
84 ScopedActionCompleter abort_action_completer(processor_, this);
85
86 if (!HasInputObject()) {
87 LOG(ERROR) << "FilesystemVerifierAction missing input object.";
88 return;
89 }
90 install_plan_ = GetInputObject();
91
Alex Deymoe5e5fe92015-10-05 09:28:19 -070092 if (install_plan_.partitions.empty()) {
93 LOG(INFO) << "No partitions to verify.";
Allie Woodeb9e6d82015-04-17 13:55:30 -070094 if (HasOutputPipe())
95 SetOutputObject(install_plan_);
96 abort_action_completer.set_code(ErrorCode::kSuccess);
97 return;
98 }
Jae Hoon Kim50504d62020-04-23 14:32:38 -070099 install_plan_.Dump();
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700100 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700101 abort_action_completer.set_should_complete(false);
102}
103
104void FilesystemVerifierAction::TerminateProcessing() {
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400105 brillo::MessageLoop::current()->CancelTask(pending_task_id_);
Alex Deymo20c99202015-07-09 16:14:16 -0700106 cancelled_ = true;
107 Cleanup(ErrorCode::kSuccess); // error code is ignored if canceled_ is true.
Allie Woodeb9e6d82015-04-17 13:55:30 -0700108}
109
Allie Woodeb9e6d82015-04-17 13:55:30 -0700110void FilesystemVerifierAction::Cleanup(ErrorCode code) {
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500111 partition_fd_.reset();
Alex Deymo20c99202015-07-09 16:14:16 -0700112 // This memory is not used anymore.
113 buffer_.clear();
114
Kelvin Zhang9105f4b2021-04-26 13:44:49 -0400115 // If we didn't write verity, partitions were maped. Releaase resource now.
116 if (!install_plan_.write_verity &&
117 dynamic_control_->UpdateUsesSnapshotCompression()) {
118 LOG(INFO) << "Not writing verity and VABC is enabled, unmapping all "
119 "partitions";
120 dynamic_control_->UnmapAllPartitions();
121 }
122
Allie Woodeb9e6d82015-04-17 13:55:30 -0700123 if (cancelled_)
124 return;
125 if (code == ErrorCode::kSuccess && HasOutputPipe())
126 SetOutputObject(install_plan_);
Kelvin Zhang70eef232020-06-12 20:32:40 +0000127 UpdateProgress(1.0);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700128 processor_->ActionComplete(this, code);
129}
130
Kelvin Zhang70eef232020-06-12 20:32:40 +0000131void FilesystemVerifierAction::UpdateProgress(double progress) {
132 if (delegate_ != nullptr) {
133 delegate_->OnVerifyProgressUpdate(progress);
134 }
135}
136
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400137bool FilesystemVerifierAction::InitializeFdVABC() {
138 const InstallPlan::Partition& partition =
139 install_plan_.partitions[partition_index_];
140
Kelvin Zhang9105f4b2021-04-26 13:44:49 -0400141 if (!ShouldWriteVerity()) {
142 // In VABC, if we are not writing verity, just map all partitions,
143 // and read using regular fd on |postinstall_mount_device| .
144 // All read will go through snapuserd, which provides a consistent
145 // view: device will use snapuserd to read partition during boot.
146 // b/186196758
147 // Call UnmapAllPartitions() first, because if we wrote verity before, these
148 // writes won't be visible to previously opened snapuserd daemon. To ensure
149 // that we will see the most up to date data from partitions, call Unmap()
150 // then Map() to re-spin daemon.
151 dynamic_control_->UnmapAllPartitions();
152 dynamic_control_->MapAllPartitions();
153 return InitializeFd(partition.readonly_target_path);
154 }
155
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500156 // FilesystemVerifierAction need the read_fd_.
157 partition_fd_ =
Kelvin Zhang21a49912021-03-12 14:28:33 -0500158 dynamic_control_->OpenCowFd(partition.name, partition.source_path, true);
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500159 if (!partition_fd_) {
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400160 LOG(ERROR) << "OpenCowReader(" << partition.name << ", "
161 << partition.source_path << ") failed.";
162 return false;
163 }
164 partition_size_ = partition.target_size;
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400165 return true;
166}
167
168bool FilesystemVerifierAction::InitializeFd(const std::string& part_path) {
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500169 partition_fd_ = FileDescriptorPtr(new EintrSafeFileDescriptor());
170 const bool write_verity = ShouldWriteVerity();
171 int flags = write_verity ? O_RDWR : O_RDONLY;
172 if (!utils::SetBlockDeviceReadOnly(part_path, !write_verity)) {
173 LOG(WARNING) << "Failed to set block device " << part_path << " as "
174 << (write_verity ? "writable" : "readonly");
175 }
176 if (!partition_fd_->Open(part_path.c_str(), flags)) {
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400177 LOG(ERROR) << "Unable to open " << part_path << " for reading.";
178 return false;
179 }
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400180 return true;
181}
182
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700183void FilesystemVerifierAction::StartPartitionHashing() {
184 if (partition_index_ == install_plan_.partitions.size()) {
Tianjie24f96092020-06-30 12:26:25 -0700185 if (!install_plan_.untouched_dynamic_partitions.empty()) {
186 LOG(INFO) << "Verifying extents of untouched dynamic partitions ["
187 << base::JoinString(install_plan_.untouched_dynamic_partitions,
188 ", ")
189 << "]";
190 if (!dynamic_control_->VerifyExtentsForUntouchedPartitions(
191 install_plan_.source_slot,
192 install_plan_.target_slot,
193 install_plan_.untouched_dynamic_partitions)) {
194 Cleanup(ErrorCode::kFilesystemVerifierError);
195 return;
196 }
197 }
198
Sen Jianga35896c2016-05-25 11:08:41 -0700199 Cleanup(ErrorCode::kSuccess);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700200 return;
201 }
Sen Jiang57f91802017-11-14 17:42:13 -0800202 const InstallPlan::Partition& partition =
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700203 install_plan_.partitions[partition_index_];
Kelvin Zhange012f652021-05-10 16:00:31 -0400204 const auto& part_path = GetPartitionPath();
205 partition_size_ = GetPartitionSize();
Yifan Hong537802d2018-08-15 13:15:42 -0700206
Yifan Hong537802d2018-08-15 13:15:42 -0700207 LOG(INFO) << "Hashing partition " << partition_index_ << " ("
208 << partition.name << ") on device " << part_path;
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400209 auto success = false;
Kelvin Zhange012f652021-05-10 16:00:31 -0400210 if (IsVABC(partition)) {
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400211 success = InitializeFdVABC();
212 } else {
213 if (part_path.empty()) {
214 if (partition_size_ == 0) {
215 LOG(INFO) << "Skip hashing partition " << partition_index_ << " ("
216 << partition.name << ") because size is 0.";
217 partition_index_++;
218 StartPartitionHashing();
219 return;
220 }
221 LOG(ERROR) << "Cannot hash partition " << partition_index_ << " ("
222 << partition.name
223 << ") because its device path cannot be determined.";
224 Cleanup(ErrorCode::kFilesystemVerifierError);
225 return;
226 }
227 success = InitializeFd(part_path);
228 }
229 if (!success) {
Sen Jiang57f91802017-11-14 17:42:13 -0800230 Cleanup(ErrorCode::kFilesystemVerifierError);
231 return;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700232 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700233 buffer_.resize(kReadFileBufferSize);
Sen Jiang57f91802017-11-14 17:42:13 -0800234 hasher_ = std::make_unique<HashCalculator>();
235
236 offset_ = 0;
Kelvin Zhang7f925672021-03-15 13:37:40 -0400237 filesystem_data_end_ = partition_size_;
238 CHECK_LE(partition.hash_tree_offset, partition.fec_offset)
239 << " Hash tree is expected to come before FEC data";
240 if (partition.hash_tree_offset != 0) {
241 filesystem_data_end_ = partition.hash_tree_offset;
242 } else if (partition.fec_offset != 0) {
243 filesystem_data_end_ = partition.fec_offset;
244 }
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400245 if (ShouldWriteVerity()) {
Kelvin Zhang7f925672021-03-15 13:37:40 -0400246 if (!verity_writer_->Init(partition)) {
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500247 LOG(INFO) << "Verity writes enabled on partition " << partition.name;
Sen Jiang57f91802017-11-14 17:42:13 -0800248 Cleanup(ErrorCode::kVerityCalculationError);
249 return;
250 }
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500251 } else {
252 LOG(INFO) << "Verity writes disabled on partition " << partition.name;
Sen Jiang57f91802017-11-14 17:42:13 -0800253 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700254
255 // Start the first read.
Kelvin Zhang7f925672021-03-15 13:37:40 -0400256 ScheduleFileSystemRead();
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700257}
258
Kelvin Zhange012f652021-05-10 16:00:31 -0400259bool FilesystemVerifierAction::IsVABC(
260 const InstallPlan::Partition& partition) const {
261 return dynamic_control_->UpdateUsesSnapshotCompression() &&
262 verifier_step_ == VerifierStep::kVerifyTargetHash &&
263 dynamic_control_->IsDynamicPartition(partition.name,
264 install_plan_.target_slot);
265}
266
267const std::string& FilesystemVerifierAction::GetPartitionPath() const {
268 const InstallPlan::Partition& partition =
269 install_plan_.partitions[partition_index_];
270 switch (verifier_step_) {
271 case VerifierStep::kVerifySourceHash:
272 return partition.source_path;
273 case VerifierStep::kVerifyTargetHash:
274 if (IsVABC(partition)) {
275 return partition.readonly_target_path;
276 } else {
277 return partition.target_path;
278 }
279 }
280}
281
282size_t FilesystemVerifierAction::GetPartitionSize() const {
283 const InstallPlan::Partition& partition =
284 install_plan_.partitions[partition_index_];
285 switch (verifier_step_) {
286 case VerifierStep::kVerifySourceHash:
287 return partition.source_size;
288 case VerifierStep::kVerifyTargetHash:
289 return partition.target_size;
290 }
291}
292
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400293bool FilesystemVerifierAction::ShouldWriteVerity() {
294 const InstallPlan::Partition& partition =
295 install_plan_.partitions[partition_index_];
296 return verifier_step_ == VerifierStep::kVerifyTargetHash &&
297 install_plan_.write_verity &&
298 (partition.hash_tree_size > 0 || partition.fec_size > 0);
299}
300
Kelvin Zhang7f925672021-03-15 13:37:40 -0400301void FilesystemVerifierAction::ReadVerityAndFooter() {
302 if (ShouldWriteVerity()) {
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500303 if (!verity_writer_->Finalize(partition_fd_, partition_fd_)) {
Kelvin Zhang7f925672021-03-15 13:37:40 -0400304 LOG(ERROR) << "Failed to write hashtree/FEC data.";
305 Cleanup(ErrorCode::kFilesystemVerifierError);
306 return;
307 }
308 }
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500309 // Since we handed our |read_fd_| to verity_writer_ during |Finalize()|
310 // call, fd's position could have been changed. Re-seek.
311 partition_fd_->Seek(filesystem_data_end_, SEEK_SET);
Kelvin Zhang7f925672021-03-15 13:37:40 -0400312 auto bytes_to_read = partition_size_ - filesystem_data_end_;
Kelvin Zhang7f925672021-03-15 13:37:40 -0400313 while (bytes_to_read > 0) {
314 const auto read_size = std::min<size_t>(buffer_.size(), bytes_to_read);
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500315 auto bytes_read = partition_fd_->Read(buffer_.data(), read_size);
Kelvin Zhang7f925672021-03-15 13:37:40 -0400316 if (bytes_read <= 0) {
317 PLOG(ERROR) << "Failed to read hash tree " << bytes_read;
318 Cleanup(ErrorCode::kFilesystemVerifierError);
319 return;
320 }
321 if (!hasher_->Update(buffer_.data(), bytes_read)) {
322 LOG(ERROR) << "Unable to update the hash.";
323 Cleanup(ErrorCode::kError);
324 return;
325 }
326 bytes_to_read -= bytes_read;
327 }
328 FinishPartitionHashing();
329}
Sen Jiang57f91802017-11-14 17:42:13 -0800330
Kelvin Zhang7f925672021-03-15 13:37:40 -0400331void FilesystemVerifierAction::ScheduleFileSystemRead() {
Sen Jiang57f91802017-11-14 17:42:13 -0800332 // We can only start reading anything past |hash_tree_offset| after we have
333 // already read all the data blocks that the hash tree covers. The same
334 // applies to FEC.
Kelvin Zhang7f925672021-03-15 13:37:40 -0400335
336 size_t bytes_to_read = std::min(static_cast<uint64_t>(buffer_.size()),
337 filesystem_data_end_ - offset_);
Alex Deymob9e8e262015-08-03 20:23:03 -0700338 if (!bytes_to_read) {
Kelvin Zhang7f925672021-03-15 13:37:40 -0400339 ReadVerityAndFooter();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700340 return;
341 }
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500342 partition_fd_->Seek(offset_, SEEK_SET);
343 auto bytes_read = partition_fd_->Read(buffer_.data(), bytes_to_read);
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400344 if (bytes_read < 0) {
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500345 LOG(ERROR) << "Unable to schedule an asynchronous read from the stream. "
346 << bytes_read;
Alex Deymob9e8e262015-08-03 20:23:03 -0700347 Cleanup(ErrorCode::kError);
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400348 } else {
349 // We could just invoke |OnReadDoneCallback()|, it works. But |PostTask|
350 // is used so that users can cancel updates.
351 pending_task_id_ = brillo::MessageLoop::current()->PostTask(
352 base::Bind(&FilesystemVerifierAction::OnReadDone,
353 base::Unretained(this),
354 bytes_read));
Allie Woodeb9e6d82015-04-17 13:55:30 -0700355 }
356}
357
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400358void FilesystemVerifierAction::OnReadDone(size_t bytes_read) {
Sen Jiang57f91802017-11-14 17:42:13 -0800359 if (cancelled_) {
360 Cleanup(ErrorCode::kError);
361 return;
362 }
Alex Deymob9e8e262015-08-03 20:23:03 -0700363 if (bytes_read == 0) {
Sen Jiang57f91802017-11-14 17:42:13 -0800364 LOG(ERROR) << "Failed to read the remaining " << partition_size_ - offset_
365 << " bytes from partition "
366 << install_plan_.partitions[partition_index_].name;
367 Cleanup(ErrorCode::kFilesystemVerifierError);
368 return;
369 }
370
371 if (!hasher_->Update(buffer_.data(), bytes_read)) {
372 LOG(ERROR) << "Unable to update the hash.";
373 Cleanup(ErrorCode::kError);
374 return;
375 }
376
Kelvin Zhang70eef232020-06-12 20:32:40 +0000377 // WE don't consider sizes of each partition. Every partition
378 // has the same length on progress bar.
379 // TODO(zhangkelvin) Take sizes of each partition into account
380
381 UpdateProgress(
382 (static_cast<double>(offset_) / partition_size_ + partition_index_) /
383 install_plan_.partitions.size());
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400384 if (ShouldWriteVerity()) {
Sen Jiang57f91802017-11-14 17:42:13 -0800385 if (!verity_writer_->Update(offset_, buffer_.data(), bytes_read)) {
Kelvin Zhang7f925672021-03-15 13:37:40 -0400386 LOG(ERROR) << "Unable to update verity";
Sen Jiang57f91802017-11-14 17:42:13 -0800387 Cleanup(ErrorCode::kVerityCalculationError);
Alex Deymob9e8e262015-08-03 20:23:03 -0700388 return;
389 }
390 }
391
Sen Jiang57f91802017-11-14 17:42:13 -0800392 offset_ += bytes_read;
Kelvin Zhang7f925672021-03-15 13:37:40 -0400393 if (offset_ == filesystem_data_end_) {
394 ReadVerityAndFooter();
Sen Jiang57f91802017-11-14 17:42:13 -0800395 return;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700396 }
Sen Jiang57f91802017-11-14 17:42:13 -0800397
Kelvin Zhang7f925672021-03-15 13:37:40 -0400398 ScheduleFileSystemRead();
Alex Deymob9e8e262015-08-03 20:23:03 -0700399}
400
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700401void FilesystemVerifierAction::FinishPartitionHashing() {
402 if (!hasher_->Finalize()) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700403 LOG(ERROR) << "Unable to finalize the hash.";
Sen Jiang57f91802017-11-14 17:42:13 -0800404 Cleanup(ErrorCode::kError);
405 return;
Alex Deymob9e8e262015-08-03 20:23:03 -0700406 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700407 InstallPlan::Partition& partition =
408 install_plan_.partitions[partition_index_];
Sen Jiang2703ef42017-03-16 13:36:21 -0700409 LOG(INFO) << "Hash of " << partition.name << ": "
410 << Base64Encode(hasher_->raw_hash());
Alex Deymob9e8e262015-08-03 20:23:03 -0700411
Sen Jiangfef85fd2016-03-25 15:32:49 -0700412 switch (verifier_step_) {
413 case VerifierStep::kVerifyTargetHash:
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700414 if (partition.target_hash != hasher_->raw_hash()) {
415 LOG(ERROR) << "New '" << partition.name
416 << "' partition verification failed.";
Sen Jiangcdd52062017-05-18 15:33:10 -0700417 if (partition.source_hash.empty()) {
418 // No need to verify source if it is a full payload.
Sen Jiang57f91802017-11-14 17:42:13 -0800419 Cleanup(ErrorCode::kNewRootfsVerificationError);
420 return;
Sen Jiangcdd52062017-05-18 15:33:10 -0700421 }
Sen Jiangfef85fd2016-03-25 15:32:49 -0700422 // If we have not verified source partition yet, now that the target
Sen Jiang65566a32016-04-06 13:35:36 -0700423 // partition does not match, and it's not a full payload, we need to
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400424 // switch to kVerifySourceHash step to check if it's because the
425 // source partition does not match either.
Sen Jiangfef85fd2016-03-25 15:32:49 -0700426 verifier_step_ = VerifierStep::kVerifySourceHash;
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800427 } else {
428 partition_index_++;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700429 }
430 break;
Sen Jiangfef85fd2016-03-25 15:32:49 -0700431 case VerifierStep::kVerifySourceHash:
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800432 if (partition.source_hash != hasher_->raw_hash()) {
433 LOG(ERROR) << "Old '" << partition.name
434 << "' partition verification failed.";
Sen Jiangfef85fd2016-03-25 15:32:49 -0700435 LOG(ERROR) << "This is a server-side error due to mismatched delta"
436 << " update image!";
437 LOG(ERROR) << "The delta I've been given contains a " << partition.name
438 << " delta update that must be applied over a "
439 << partition.name << " with a specific checksum, but the "
440 << partition.name
441 << " we're starting with doesn't have that checksum! This"
442 " means that the delta I've been given doesn't match my"
443 " existing system. The "
444 << partition.name << " partition I have has hash: "
Sen Jiang2703ef42017-03-16 13:36:21 -0700445 << Base64Encode(hasher_->raw_hash())
Sen Jiangfef85fd2016-03-25 15:32:49 -0700446 << " but the update expected me to have "
Sen Jiang2703ef42017-03-16 13:36:21 -0700447 << Base64Encode(partition.source_hash) << " .";
Sen Jiangfef85fd2016-03-25 15:32:49 -0700448 LOG(INFO) << "To get the checksum of the " << partition.name
449 << " partition run this command: dd if="
450 << partition.source_path
451 << " bs=1M count=" << partition.source_size
452 << " iflag=count_bytes 2>/dev/null | openssl dgst -sha256 "
453 "-binary | openssl base64";
454 LOG(INFO) << "To get the checksum of partitions in a bin file, "
455 << "run: .../src/scripts/sha256_partitions.sh .../file.bin";
Sen Jiang57f91802017-11-14 17:42:13 -0800456 Cleanup(ErrorCode::kDownloadStateInitializationError);
457 return;
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800458 }
Sen Jianga35896c2016-05-25 11:08:41 -0700459 // The action will skip kVerifySourceHash step if target partition hash
460 // matches, if we are in this step, it means target hash does not match,
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400461 // and now that the source partition hash matches, we should set the
462 // error code to reflect the error in target partition. We only need to
463 // verify the source partition which the target hash does not match, the
464 // rest of the partitions don't matter.
Sen Jiang57f91802017-11-14 17:42:13 -0800465 Cleanup(ErrorCode::kNewRootfsVerificationError);
466 return;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700467 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700468 // Start hashing the next partition, if any.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700469 hasher_.reset();
470 buffer_.clear();
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500471 if (partition_fd_) {
472 partition_fd_.reset();
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400473 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700474 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700475}
476
477} // namespace chromeos_update_engine