blob: a8b9269660972669cc0df95e772889cdb744b51f [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"
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;
Kelvin Zhang1d99ae12021-05-12 13:29:27 -040080constexpr float kVerityProgressPercent = 0.6;
Allie Woodeb9e6d82015-04-17 13:55:30 -070081} // namespace
82
Allie Woodeb9e6d82015-04-17 13:55:30 -070083void FilesystemVerifierAction::PerformAction() {
84 // Will tell the ActionProcessor we've failed if we return.
85 ScopedActionCompleter abort_action_completer(processor_, this);
86
87 if (!HasInputObject()) {
88 LOG(ERROR) << "FilesystemVerifierAction missing input object.";
89 return;
90 }
91 install_plan_ = GetInputObject();
92
Alex Deymoe5e5fe92015-10-05 09:28:19 -070093 if (install_plan_.partitions.empty()) {
94 LOG(INFO) << "No partitions to verify.";
Allie Woodeb9e6d82015-04-17 13:55:30 -070095 if (HasOutputPipe())
96 SetOutputObject(install_plan_);
97 abort_action_completer.set_code(ErrorCode::kSuccess);
98 return;
99 }
Jae Hoon Kim50504d62020-04-23 14:32:38 -0700100 install_plan_.Dump();
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700101 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700102 abort_action_completer.set_should_complete(false);
103}
104
105void FilesystemVerifierAction::TerminateProcessing() {
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 Zhang8704c832021-05-10 17:53:14 -0400137void FilesystemVerifierAction::UpdatePartitionProgress(double progress) {
Kelvin Zhang1d99ae12021-05-12 13:29:27 -0400138 // We don't consider sizes of each partition. Every partition
Kelvin Zhang8704c832021-05-10 17:53:14 -0400139 // has the same length on progress bar.
Kelvin Zhang1d99ae12021-05-12 13:29:27 -0400140 // TODO(b/186087589): Take sizes of each partition into account.
Kelvin Zhang8704c832021-05-10 17:53:14 -0400141 UpdateProgress((progress + partition_index_) /
142 install_plan_.partitions.size());
143}
144
145bool FilesystemVerifierAction::InitializeFdVABC(bool should_write_verity) {
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400146 const InstallPlan::Partition& partition =
147 install_plan_.partitions[partition_index_];
148
Kelvin Zhang8704c832021-05-10 17:53:14 -0400149 if (!should_write_verity) {
150 // In VABC, we cannot map/unmap partitions w/o first closing ALL fds first.
151 // Since this function might be called inside a ScheduledTask, the closure
152 // might have a copy of partition_fd_ when executing this function. Which
153 // means even if we do |partition_fd_.reset()| here, there's a chance that
154 // underlying fd isn't closed until we return. This is unacceptable, we need
155 // to close |partition_fd| right away.
156 if (partition_fd_) {
157 partition_fd_->Close();
158 partition_fd_.reset();
159 }
Kelvin Zhang9105f4b2021-04-26 13:44:49 -0400160 // In VABC, if we are not writing verity, just map all partitions,
161 // and read using regular fd on |postinstall_mount_device| .
162 // All read will go through snapuserd, which provides a consistent
163 // view: device will use snapuserd to read partition during boot.
164 // b/186196758
165 // Call UnmapAllPartitions() first, because if we wrote verity before, these
166 // writes won't be visible to previously opened snapuserd daemon. To ensure
167 // that we will see the most up to date data from partitions, call Unmap()
168 // then Map() to re-spin daemon.
169 dynamic_control_->UnmapAllPartitions();
170 dynamic_control_->MapAllPartitions();
171 return InitializeFd(partition.readonly_target_path);
172 }
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500173 partition_fd_ =
Kelvin Zhang21a49912021-03-12 14:28:33 -0500174 dynamic_control_->OpenCowFd(partition.name, partition.source_path, true);
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500175 if (!partition_fd_) {
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400176 LOG(ERROR) << "OpenCowReader(" << partition.name << ", "
177 << partition.source_path << ") failed.";
178 return false;
179 }
180 partition_size_ = partition.target_size;
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400181 return true;
182}
183
184bool FilesystemVerifierAction::InitializeFd(const std::string& part_path) {
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500185 partition_fd_ = FileDescriptorPtr(new EintrSafeFileDescriptor());
186 const bool write_verity = ShouldWriteVerity();
187 int flags = write_verity ? O_RDWR : O_RDONLY;
188 if (!utils::SetBlockDeviceReadOnly(part_path, !write_verity)) {
189 LOG(WARNING) << "Failed to set block device " << part_path << " as "
190 << (write_verity ? "writable" : "readonly");
191 }
192 if (!partition_fd_->Open(part_path.c_str(), flags)) {
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400193 LOG(ERROR) << "Unable to open " << part_path << " for reading.";
194 return false;
195 }
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400196 return true;
197}
198
Kelvin Zhang8704c832021-05-10 17:53:14 -0400199void FilesystemVerifierAction::WriteVerityAndHashPartition(
200 FileDescriptorPtr fd,
201 const off64_t start_offset,
202 const off64_t end_offset,
203 void* buffer,
204 const size_t buffer_size) {
205 if (start_offset >= end_offset) {
206 LOG_IF(WARNING, start_offset > end_offset)
207 << "start_offset is greater than end_offset : " << start_offset << " > "
208 << end_offset;
209 if (!verity_writer_->Finalize(fd, fd)) {
210 LOG(ERROR) << "Failed to write verity data";
211 Cleanup(ErrorCode::kVerityCalculationError);
212 return;
213 }
214 if (dynamic_control_->UpdateUsesSnapshotCompression()) {
Kelvin Zhang1d99ae12021-05-12 13:29:27 -0400215 // Spin up snapuserd to read fs.
Kelvin Zhang8704c832021-05-10 17:53:14 -0400216 if (!InitializeFdVABC(false)) {
217 LOG(ERROR) << "Failed to map all partitions";
218 Cleanup(ErrorCode::kFilesystemVerifierError);
219 return;
220 }
221 }
222 HashPartition(partition_fd_, 0, partition_size_, buffer, buffer_size);
223 return;
224 }
225 const auto cur_offset = fd->Seek(start_offset, SEEK_SET);
226 if (cur_offset != start_offset) {
227 PLOG(ERROR) << "Failed to seek to offset: " << start_offset;
228 Cleanup(ErrorCode::kVerityCalculationError);
229 return;
230 }
231 const auto read_size =
232 std::min<size_t>(buffer_size, end_offset - start_offset);
233 const auto bytes_read = fd->Read(buffer, read_size);
234 if (bytes_read < 0 || static_cast<size_t>(bytes_read) != read_size) {
235 PLOG(ERROR) << "Failed to read offset " << start_offset << " expected "
236 << read_size << " bytes, actual: " << bytes_read;
237 Cleanup(ErrorCode::kVerityCalculationError);
238 return;
239 }
240 if (!verity_writer_->Update(
241 start_offset, static_cast<const uint8_t*>(buffer), read_size)) {
242 LOG(ERROR) << "VerityWriter::Update() failed";
243 Cleanup(ErrorCode::kVerityCalculationError);
244 return;
245 }
246 UpdatePartitionProgress((start_offset + bytes_read) * 1.0f / partition_size_ *
Kelvin Zhang1d99ae12021-05-12 13:29:27 -0400247 kVerityProgressPercent);
Kelvin Zhang8704c832021-05-10 17:53:14 -0400248 CHECK(pending_task_id_.PostTask(
249 FROM_HERE,
250 base::BindOnce(&FilesystemVerifierAction::WriteVerityAndHashPartition,
251 base::Unretained(this),
252 fd,
253 start_offset + bytes_read,
254 end_offset,
255 buffer,
256 buffer_size)));
257}
258
259void FilesystemVerifierAction::HashPartition(FileDescriptorPtr fd,
260 const off64_t start_offset,
261 const off64_t end_offset,
262 void* buffer,
263 const size_t buffer_size) {
264 if (start_offset >= end_offset) {
265 LOG_IF(WARNING, start_offset > end_offset)
266 << "start_offset is greater than end_offset : " << start_offset << " > "
267 << end_offset;
268 FinishPartitionHashing();
269 return;
270 }
271 const auto cur_offset = fd->Seek(start_offset, SEEK_SET);
272 if (cur_offset != start_offset) {
273 PLOG(ERROR) << "Failed to seek to offset: " << start_offset;
274 Cleanup(ErrorCode::kFilesystemVerifierError);
275 return;
276 }
277 const auto read_size =
278 std::min<size_t>(buffer_size, end_offset - start_offset);
279 const auto bytes_read = fd->Read(buffer, read_size);
280 if (bytes_read < 0 || static_cast<size_t>(bytes_read) != read_size) {
281 PLOG(ERROR) << "Failed to read offset " << start_offset << " expected "
282 << read_size << " bytes, actual: " << bytes_read;
283 Cleanup(ErrorCode::kFilesystemVerifierError);
284 return;
285 }
286 if (!hasher_->Update(buffer, read_size)) {
287 LOG(ERROR) << "Hasher updated failed on offset" << start_offset;
288 Cleanup(ErrorCode::kFilesystemVerifierError);
289 return;
290 }
291 const auto progress = (start_offset + bytes_read) * 1.0f / partition_size_;
Kelvin Zhang1d99ae12021-05-12 13:29:27 -0400292 UpdatePartitionProgress(progress * (1 - kVerityProgressPercent) +
293 kVerityProgressPercent);
Kelvin Zhang8704c832021-05-10 17:53:14 -0400294 CHECK(pending_task_id_.PostTask(
295 FROM_HERE,
296 base::BindOnce(&FilesystemVerifierAction::HashPartition,
297 base::Unretained(this),
298 fd,
299 start_offset + bytes_read,
300 end_offset,
301 buffer,
302 buffer_size)));
303}
304
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700305void FilesystemVerifierAction::StartPartitionHashing() {
306 if (partition_index_ == install_plan_.partitions.size()) {
Tianjie24f96092020-06-30 12:26:25 -0700307 if (!install_plan_.untouched_dynamic_partitions.empty()) {
308 LOG(INFO) << "Verifying extents of untouched dynamic partitions ["
309 << base::JoinString(install_plan_.untouched_dynamic_partitions,
310 ", ")
311 << "]";
312 if (!dynamic_control_->VerifyExtentsForUntouchedPartitions(
313 install_plan_.source_slot,
314 install_plan_.target_slot,
315 install_plan_.untouched_dynamic_partitions)) {
316 Cleanup(ErrorCode::kFilesystemVerifierError);
317 return;
318 }
319 }
320
Sen Jianga35896c2016-05-25 11:08:41 -0700321 Cleanup(ErrorCode::kSuccess);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700322 return;
323 }
Sen Jiang57f91802017-11-14 17:42:13 -0800324 const InstallPlan::Partition& partition =
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700325 install_plan_.partitions[partition_index_];
Kelvin Zhange012f652021-05-10 16:00:31 -0400326 const auto& part_path = GetPartitionPath();
327 partition_size_ = GetPartitionSize();
Yifan Hong537802d2018-08-15 13:15:42 -0700328
Yifan Hong537802d2018-08-15 13:15:42 -0700329 LOG(INFO) << "Hashing partition " << partition_index_ << " ("
330 << partition.name << ") on device " << part_path;
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400331 auto success = false;
Kelvin Zhange012f652021-05-10 16:00:31 -0400332 if (IsVABC(partition)) {
Kelvin Zhang8704c832021-05-10 17:53:14 -0400333 success = InitializeFdVABC(ShouldWriteVerity());
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400334 } else {
335 if (part_path.empty()) {
336 if (partition_size_ == 0) {
337 LOG(INFO) << "Skip hashing partition " << partition_index_ << " ("
338 << partition.name << ") because size is 0.";
339 partition_index_++;
340 StartPartitionHashing();
341 return;
342 }
343 LOG(ERROR) << "Cannot hash partition " << partition_index_ << " ("
344 << partition.name
345 << ") because its device path cannot be determined.";
346 Cleanup(ErrorCode::kFilesystemVerifierError);
347 return;
348 }
349 success = InitializeFd(part_path);
350 }
351 if (!success) {
Sen Jiang57f91802017-11-14 17:42:13 -0800352 Cleanup(ErrorCode::kFilesystemVerifierError);
353 return;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700354 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700355 buffer_.resize(kReadFileBufferSize);
Sen Jiang57f91802017-11-14 17:42:13 -0800356 hasher_ = std::make_unique<HashCalculator>();
357
358 offset_ = 0;
Kelvin Zhang7f925672021-03-15 13:37:40 -0400359 filesystem_data_end_ = partition_size_;
Kelvin Zhang5e5ad392021-07-26 21:42:06 -0400360 if (partition.fec_offset > 0) {
361 CHECK_LE(partition.hash_tree_offset, partition.fec_offset)
362 << " Hash tree is expected to come before FEC data";
363 }
Kelvin Zhang7f925672021-03-15 13:37:40 -0400364 if (partition.hash_tree_offset != 0) {
365 filesystem_data_end_ = partition.hash_tree_offset;
366 } else if (partition.fec_offset != 0) {
367 filesystem_data_end_ = partition.fec_offset;
368 }
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400369 if (ShouldWriteVerity()) {
Kelvin Zhang8704c832021-05-10 17:53:14 -0400370 LOG(INFO) << "Verity writes enabled on partition " << partition.name;
Kelvin Zhang7f925672021-03-15 13:37:40 -0400371 if (!verity_writer_->Init(partition)) {
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500372 LOG(INFO) << "Verity writes enabled on partition " << partition.name;
Sen Jiang57f91802017-11-14 17:42:13 -0800373 Cleanup(ErrorCode::kVerityCalculationError);
374 return;
375 }
Kelvin Zhang8704c832021-05-10 17:53:14 -0400376 WriteVerityAndHashPartition(
377 partition_fd_, 0, filesystem_data_end_, buffer_.data(), buffer_.size());
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500378 } else {
379 LOG(INFO) << "Verity writes disabled on partition " << partition.name;
Kelvin Zhang8704c832021-05-10 17:53:14 -0400380 HashPartition(
381 partition_fd_, 0, partition_size_, buffer_.data(), buffer_.size());
Sen Jiang57f91802017-11-14 17:42:13 -0800382 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700383}
384
Kelvin Zhange012f652021-05-10 16:00:31 -0400385bool FilesystemVerifierAction::IsVABC(
386 const InstallPlan::Partition& partition) const {
387 return dynamic_control_->UpdateUsesSnapshotCompression() &&
388 verifier_step_ == VerifierStep::kVerifyTargetHash &&
389 dynamic_control_->IsDynamicPartition(partition.name,
390 install_plan_.target_slot);
391}
392
393const std::string& FilesystemVerifierAction::GetPartitionPath() const {
394 const InstallPlan::Partition& partition =
395 install_plan_.partitions[partition_index_];
396 switch (verifier_step_) {
397 case VerifierStep::kVerifySourceHash:
398 return partition.source_path;
399 case VerifierStep::kVerifyTargetHash:
400 if (IsVABC(partition)) {
401 return partition.readonly_target_path;
402 } else {
403 return partition.target_path;
404 }
405 }
406}
407
408size_t FilesystemVerifierAction::GetPartitionSize() const {
409 const InstallPlan::Partition& partition =
410 install_plan_.partitions[partition_index_];
411 switch (verifier_step_) {
412 case VerifierStep::kVerifySourceHash:
413 return partition.source_size;
414 case VerifierStep::kVerifyTargetHash:
415 return partition.target_size;
416 }
417}
418
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400419bool FilesystemVerifierAction::ShouldWriteVerity() {
420 const InstallPlan::Partition& partition =
421 install_plan_.partitions[partition_index_];
422 return verifier_step_ == VerifierStep::kVerifyTargetHash &&
423 install_plan_.write_verity &&
424 (partition.hash_tree_size > 0 || partition.fec_size > 0);
425}
426
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700427void FilesystemVerifierAction::FinishPartitionHashing() {
428 if (!hasher_->Finalize()) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700429 LOG(ERROR) << "Unable to finalize the hash.";
Sen Jiang57f91802017-11-14 17:42:13 -0800430 Cleanup(ErrorCode::kError);
431 return;
Alex Deymob9e8e262015-08-03 20:23:03 -0700432 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700433 InstallPlan::Partition& partition =
434 install_plan_.partitions[partition_index_];
Sen Jiang2703ef42017-03-16 13:36:21 -0700435 LOG(INFO) << "Hash of " << partition.name << ": "
Kelvin Zhang3fe49642021-10-04 15:35:02 -0700436 << HexEncode(hasher_->raw_hash());
Alex Deymob9e8e262015-08-03 20:23:03 -0700437
Sen Jiangfef85fd2016-03-25 15:32:49 -0700438 switch (verifier_step_) {
439 case VerifierStep::kVerifyTargetHash:
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700440 if (partition.target_hash != hasher_->raw_hash()) {
441 LOG(ERROR) << "New '" << partition.name
442 << "' partition verification failed.";
Sen Jiangcdd52062017-05-18 15:33:10 -0700443 if (partition.source_hash.empty()) {
444 // No need to verify source if it is a full payload.
Sen Jiang57f91802017-11-14 17:42:13 -0800445 Cleanup(ErrorCode::kNewRootfsVerificationError);
446 return;
Sen Jiangcdd52062017-05-18 15:33:10 -0700447 }
Sen Jiangfef85fd2016-03-25 15:32:49 -0700448 // If we have not verified source partition yet, now that the target
Sen Jiang65566a32016-04-06 13:35:36 -0700449 // partition does not match, and it's not a full payload, we need to
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400450 // switch to kVerifySourceHash step to check if it's because the
451 // source partition does not match either.
Sen Jiangfef85fd2016-03-25 15:32:49 -0700452 verifier_step_ = VerifierStep::kVerifySourceHash;
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800453 } else {
454 partition_index_++;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700455 }
456 break;
Sen Jiangfef85fd2016-03-25 15:32:49 -0700457 case VerifierStep::kVerifySourceHash:
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800458 if (partition.source_hash != hasher_->raw_hash()) {
459 LOG(ERROR) << "Old '" << partition.name
460 << "' partition verification failed.";
Sen Jiangfef85fd2016-03-25 15:32:49 -0700461 LOG(ERROR) << "This is a server-side error due to mismatched delta"
462 << " update image!";
463 LOG(ERROR) << "The delta I've been given contains a " << partition.name
464 << " delta update that must be applied over a "
465 << partition.name << " with a specific checksum, but the "
466 << partition.name
467 << " we're starting with doesn't have that checksum! This"
468 " means that the delta I've been given doesn't match my"
469 " existing system. The "
470 << partition.name << " partition I have has hash: "
Sen Jiang2703ef42017-03-16 13:36:21 -0700471 << Base64Encode(hasher_->raw_hash())
Sen Jiangfef85fd2016-03-25 15:32:49 -0700472 << " but the update expected me to have "
Sen Jiang2703ef42017-03-16 13:36:21 -0700473 << Base64Encode(partition.source_hash) << " .";
Sen Jiangfef85fd2016-03-25 15:32:49 -0700474 LOG(INFO) << "To get the checksum of the " << partition.name
475 << " partition run this command: dd if="
476 << partition.source_path
477 << " bs=1M count=" << partition.source_size
478 << " iflag=count_bytes 2>/dev/null | openssl dgst -sha256 "
479 "-binary | openssl base64";
480 LOG(INFO) << "To get the checksum of partitions in a bin file, "
481 << "run: .../src/scripts/sha256_partitions.sh .../file.bin";
Sen Jiang57f91802017-11-14 17:42:13 -0800482 Cleanup(ErrorCode::kDownloadStateInitializationError);
483 return;
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800484 }
Sen Jianga35896c2016-05-25 11:08:41 -0700485 // The action will skip kVerifySourceHash step if target partition hash
486 // matches, if we are in this step, it means target hash does not match,
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400487 // and now that the source partition hash matches, we should set the
488 // error code to reflect the error in target partition. We only need to
489 // verify the source partition which the target hash does not match, the
490 // rest of the partitions don't matter.
Sen Jiang57f91802017-11-14 17:42:13 -0800491 Cleanup(ErrorCode::kNewRootfsVerificationError);
492 return;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700493 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700494 // Start hashing the next partition, if any.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700495 hasher_.reset();
496 buffer_.clear();
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500497 if (partition_fd_) {
Kelvin Zhang8704c832021-05-10 17:53:14 -0400498 partition_fd_->Close();
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500499 partition_fd_.reset();
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400500 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700501 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700502}
503
504} // namespace chromeos_update_engine