blob: b14cbc8d037565b17fd274bd55f8c63e7c3a5230 [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_];
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700204 string part_path;
Sen Jiangfef85fd2016-03-25 15:32:49 -0700205 switch (verifier_step_) {
206 case VerifierStep::kVerifySourceHash:
Sen Jiange6e4bb92016-04-05 14:59:12 -0700207 part_path = partition.source_path;
Sen Jiang57f91802017-11-14 17:42:13 -0800208 partition_size_ = partition.source_size;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700209 break;
Sen Jiangfef85fd2016-03-25 15:32:49 -0700210 case VerifierStep::kVerifyTargetHash:
Sen Jiange6e4bb92016-04-05 14:59:12 -0700211 part_path = partition.target_path;
Sen Jiang57f91802017-11-14 17:42:13 -0800212 partition_size_ = partition.target_size;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700213 break;
214 }
Yifan Hong537802d2018-08-15 13:15:42 -0700215
Yifan Hong537802d2018-08-15 13:15:42 -0700216 LOG(INFO) << "Hashing partition " << partition_index_ << " ("
217 << partition.name << ") on device " << part_path;
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400218 auto success = false;
Kelvin Zhang06188352021-02-10 13:21:47 -0500219 if (dynamic_control_->UpdateUsesSnapshotCompression() &&
Kelvin Zhangebd115e2021-03-08 16:10:25 -0500220 verifier_step_ == VerifierStep::kVerifyTargetHash &&
221 dynamic_control_->IsDynamicPartition(partition.name,
222 install_plan_.target_slot)) {
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400223 success = InitializeFdVABC();
224 } else {
225 if (part_path.empty()) {
226 if (partition_size_ == 0) {
227 LOG(INFO) << "Skip hashing partition " << partition_index_ << " ("
228 << partition.name << ") because size is 0.";
229 partition_index_++;
230 StartPartitionHashing();
231 return;
232 }
233 LOG(ERROR) << "Cannot hash partition " << partition_index_ << " ("
234 << partition.name
235 << ") because its device path cannot be determined.";
236 Cleanup(ErrorCode::kFilesystemVerifierError);
237 return;
238 }
239 success = InitializeFd(part_path);
240 }
241 if (!success) {
Sen Jiang57f91802017-11-14 17:42:13 -0800242 Cleanup(ErrorCode::kFilesystemVerifierError);
243 return;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700244 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700245 buffer_.resize(kReadFileBufferSize);
Sen Jiang57f91802017-11-14 17:42:13 -0800246 hasher_ = std::make_unique<HashCalculator>();
247
248 offset_ = 0;
Kelvin Zhang7f925672021-03-15 13:37:40 -0400249 filesystem_data_end_ = partition_size_;
250 CHECK_LE(partition.hash_tree_offset, partition.fec_offset)
251 << " Hash tree is expected to come before FEC data";
252 if (partition.hash_tree_offset != 0) {
253 filesystem_data_end_ = partition.hash_tree_offset;
254 } else if (partition.fec_offset != 0) {
255 filesystem_data_end_ = partition.fec_offset;
256 }
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400257 if (ShouldWriteVerity()) {
Kelvin Zhang7f925672021-03-15 13:37:40 -0400258 if (!verity_writer_->Init(partition)) {
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500259 LOG(INFO) << "Verity writes enabled on partition " << partition.name;
Sen Jiang57f91802017-11-14 17:42:13 -0800260 Cleanup(ErrorCode::kVerityCalculationError);
261 return;
262 }
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500263 } else {
264 LOG(INFO) << "Verity writes disabled on partition " << partition.name;
Sen Jiang57f91802017-11-14 17:42:13 -0800265 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700266
267 // Start the first read.
Kelvin Zhang7f925672021-03-15 13:37:40 -0400268 ScheduleFileSystemRead();
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700269}
270
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400271bool FilesystemVerifierAction::ShouldWriteVerity() {
272 const InstallPlan::Partition& partition =
273 install_plan_.partitions[partition_index_];
274 return verifier_step_ == VerifierStep::kVerifyTargetHash &&
275 install_plan_.write_verity &&
276 (partition.hash_tree_size > 0 || partition.fec_size > 0);
277}
278
Kelvin Zhang7f925672021-03-15 13:37:40 -0400279void FilesystemVerifierAction::ReadVerityAndFooter() {
280 if (ShouldWriteVerity()) {
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500281 if (!verity_writer_->Finalize(partition_fd_, partition_fd_)) {
Kelvin Zhang7f925672021-03-15 13:37:40 -0400282 LOG(ERROR) << "Failed to write hashtree/FEC data.";
283 Cleanup(ErrorCode::kFilesystemVerifierError);
284 return;
285 }
286 }
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500287 // Since we handed our |read_fd_| to verity_writer_ during |Finalize()|
288 // call, fd's position could have been changed. Re-seek.
289 partition_fd_->Seek(filesystem_data_end_, SEEK_SET);
Kelvin Zhang7f925672021-03-15 13:37:40 -0400290 auto bytes_to_read = partition_size_ - filesystem_data_end_;
Kelvin Zhang7f925672021-03-15 13:37:40 -0400291 while (bytes_to_read > 0) {
292 const auto read_size = std::min<size_t>(buffer_.size(), bytes_to_read);
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500293 auto bytes_read = partition_fd_->Read(buffer_.data(), read_size);
Kelvin Zhang7f925672021-03-15 13:37:40 -0400294 if (bytes_read <= 0) {
295 PLOG(ERROR) << "Failed to read hash tree " << bytes_read;
296 Cleanup(ErrorCode::kFilesystemVerifierError);
297 return;
298 }
299 if (!hasher_->Update(buffer_.data(), bytes_read)) {
300 LOG(ERROR) << "Unable to update the hash.";
301 Cleanup(ErrorCode::kError);
302 return;
303 }
304 bytes_to_read -= bytes_read;
305 }
306 FinishPartitionHashing();
307}
Sen Jiang57f91802017-11-14 17:42:13 -0800308
Kelvin Zhang7f925672021-03-15 13:37:40 -0400309void FilesystemVerifierAction::ScheduleFileSystemRead() {
Sen Jiang57f91802017-11-14 17:42:13 -0800310 // We can only start reading anything past |hash_tree_offset| after we have
311 // already read all the data blocks that the hash tree covers. The same
312 // applies to FEC.
Kelvin Zhang7f925672021-03-15 13:37:40 -0400313
314 size_t bytes_to_read = std::min(static_cast<uint64_t>(buffer_.size()),
315 filesystem_data_end_ - offset_);
Alex Deymob9e8e262015-08-03 20:23:03 -0700316 if (!bytes_to_read) {
Kelvin Zhang7f925672021-03-15 13:37:40 -0400317 ReadVerityAndFooter();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700318 return;
319 }
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500320 partition_fd_->Seek(offset_, SEEK_SET);
321 auto bytes_read = partition_fd_->Read(buffer_.data(), bytes_to_read);
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400322 if (bytes_read < 0) {
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500323 LOG(ERROR) << "Unable to schedule an asynchronous read from the stream. "
324 << bytes_read;
Alex Deymob9e8e262015-08-03 20:23:03 -0700325 Cleanup(ErrorCode::kError);
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400326 } else {
327 // We could just invoke |OnReadDoneCallback()|, it works. But |PostTask|
328 // is used so that users can cancel updates.
329 pending_task_id_ = brillo::MessageLoop::current()->PostTask(
330 base::Bind(&FilesystemVerifierAction::OnReadDone,
331 base::Unretained(this),
332 bytes_read));
Allie Woodeb9e6d82015-04-17 13:55:30 -0700333 }
334}
335
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400336void FilesystemVerifierAction::OnReadDone(size_t bytes_read) {
Sen Jiang57f91802017-11-14 17:42:13 -0800337 if (cancelled_) {
338 Cleanup(ErrorCode::kError);
339 return;
340 }
Alex Deymob9e8e262015-08-03 20:23:03 -0700341 if (bytes_read == 0) {
Sen Jiang57f91802017-11-14 17:42:13 -0800342 LOG(ERROR) << "Failed to read the remaining " << partition_size_ - offset_
343 << " bytes from partition "
344 << install_plan_.partitions[partition_index_].name;
345 Cleanup(ErrorCode::kFilesystemVerifierError);
346 return;
347 }
348
349 if (!hasher_->Update(buffer_.data(), bytes_read)) {
350 LOG(ERROR) << "Unable to update the hash.";
351 Cleanup(ErrorCode::kError);
352 return;
353 }
354
Kelvin Zhang70eef232020-06-12 20:32:40 +0000355 // WE don't consider sizes of each partition. Every partition
356 // has the same length on progress bar.
357 // TODO(zhangkelvin) Take sizes of each partition into account
358
359 UpdateProgress(
360 (static_cast<double>(offset_) / partition_size_ + partition_index_) /
361 install_plan_.partitions.size());
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400362 if (ShouldWriteVerity()) {
Sen Jiang57f91802017-11-14 17:42:13 -0800363 if (!verity_writer_->Update(offset_, buffer_.data(), bytes_read)) {
Kelvin Zhang7f925672021-03-15 13:37:40 -0400364 LOG(ERROR) << "Unable to update verity";
Sen Jiang57f91802017-11-14 17:42:13 -0800365 Cleanup(ErrorCode::kVerityCalculationError);
Alex Deymob9e8e262015-08-03 20:23:03 -0700366 return;
367 }
368 }
369
Sen Jiang57f91802017-11-14 17:42:13 -0800370 offset_ += bytes_read;
Kelvin Zhang7f925672021-03-15 13:37:40 -0400371 if (offset_ == filesystem_data_end_) {
372 ReadVerityAndFooter();
Sen Jiang57f91802017-11-14 17:42:13 -0800373 return;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700374 }
Sen Jiang57f91802017-11-14 17:42:13 -0800375
Kelvin Zhang7f925672021-03-15 13:37:40 -0400376 ScheduleFileSystemRead();
Alex Deymob9e8e262015-08-03 20:23:03 -0700377}
378
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700379void FilesystemVerifierAction::FinishPartitionHashing() {
380 if (!hasher_->Finalize()) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700381 LOG(ERROR) << "Unable to finalize the hash.";
Sen Jiang57f91802017-11-14 17:42:13 -0800382 Cleanup(ErrorCode::kError);
383 return;
Alex Deymob9e8e262015-08-03 20:23:03 -0700384 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700385 InstallPlan::Partition& partition =
386 install_plan_.partitions[partition_index_];
Sen Jiang2703ef42017-03-16 13:36:21 -0700387 LOG(INFO) << "Hash of " << partition.name << ": "
388 << Base64Encode(hasher_->raw_hash());
Alex Deymob9e8e262015-08-03 20:23:03 -0700389
Sen Jiangfef85fd2016-03-25 15:32:49 -0700390 switch (verifier_step_) {
391 case VerifierStep::kVerifyTargetHash:
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700392 if (partition.target_hash != hasher_->raw_hash()) {
393 LOG(ERROR) << "New '" << partition.name
394 << "' partition verification failed.";
Sen Jiangcdd52062017-05-18 15:33:10 -0700395 if (partition.source_hash.empty()) {
396 // No need to verify source if it is a full payload.
Sen Jiang57f91802017-11-14 17:42:13 -0800397 Cleanup(ErrorCode::kNewRootfsVerificationError);
398 return;
Sen Jiangcdd52062017-05-18 15:33:10 -0700399 }
Sen Jiangfef85fd2016-03-25 15:32:49 -0700400 // If we have not verified source partition yet, now that the target
Sen Jiang65566a32016-04-06 13:35:36 -0700401 // partition does not match, and it's not a full payload, we need to
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400402 // switch to kVerifySourceHash step to check if it's because the
403 // source partition does not match either.
Sen Jiangfef85fd2016-03-25 15:32:49 -0700404 verifier_step_ = VerifierStep::kVerifySourceHash;
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800405 } else {
406 partition_index_++;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700407 }
408 break;
Sen Jiangfef85fd2016-03-25 15:32:49 -0700409 case VerifierStep::kVerifySourceHash:
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800410 if (partition.source_hash != hasher_->raw_hash()) {
411 LOG(ERROR) << "Old '" << partition.name
412 << "' partition verification failed.";
Sen Jiangfef85fd2016-03-25 15:32:49 -0700413 LOG(ERROR) << "This is a server-side error due to mismatched delta"
414 << " update image!";
415 LOG(ERROR) << "The delta I've been given contains a " << partition.name
416 << " delta update that must be applied over a "
417 << partition.name << " with a specific checksum, but the "
418 << partition.name
419 << " we're starting with doesn't have that checksum! This"
420 " means that the delta I've been given doesn't match my"
421 " existing system. The "
422 << partition.name << " partition I have has hash: "
Sen Jiang2703ef42017-03-16 13:36:21 -0700423 << Base64Encode(hasher_->raw_hash())
Sen Jiangfef85fd2016-03-25 15:32:49 -0700424 << " but the update expected me to have "
Sen Jiang2703ef42017-03-16 13:36:21 -0700425 << Base64Encode(partition.source_hash) << " .";
Sen Jiangfef85fd2016-03-25 15:32:49 -0700426 LOG(INFO) << "To get the checksum of the " << partition.name
427 << " partition run this command: dd if="
428 << partition.source_path
429 << " bs=1M count=" << partition.source_size
430 << " iflag=count_bytes 2>/dev/null | openssl dgst -sha256 "
431 "-binary | openssl base64";
432 LOG(INFO) << "To get the checksum of partitions in a bin file, "
433 << "run: .../src/scripts/sha256_partitions.sh .../file.bin";
Sen Jiang57f91802017-11-14 17:42:13 -0800434 Cleanup(ErrorCode::kDownloadStateInitializationError);
435 return;
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800436 }
Sen Jianga35896c2016-05-25 11:08:41 -0700437 // The action will skip kVerifySourceHash step if target partition hash
438 // matches, if we are in this step, it means target hash does not match,
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400439 // and now that the source partition hash matches, we should set the
440 // error code to reflect the error in target partition. We only need to
441 // verify the source partition which the target hash does not match, the
442 // rest of the partitions don't matter.
Sen Jiang57f91802017-11-14 17:42:13 -0800443 Cleanup(ErrorCode::kNewRootfsVerificationError);
444 return;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700445 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700446 // Start hashing the next partition, if any.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700447 hasher_.reset();
448 buffer_.clear();
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500449 if (partition_fd_) {
450 partition_fd_.reset();
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400451 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700452 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700453}
454
455} // namespace chromeos_update_engine