blob: 956f90b08d78c20cfe6ebb0df8340243ea4332b1 [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
Allie Woodeb9e6d82015-04-17 13:55:30 -070019#include <fcntl.h>
20#include <sys/stat.h>
21#include <sys/types.h>
Kelvin Zhangec205cf2020-09-28 13:23:40 -040022#include <unistd.h>
Allie Woodeb9e6d82015-04-17 13:55:30 -070023
24#include <algorithm>
25#include <cstdlib>
Daniel Zhengbd16b012022-09-28 23:40:05 +000026#include <functional>
Kelvin Zhangec205cf2020-09-28 13:23:40 -040027#include <memory>
Kelvin Zhang5b145822022-07-26 10:56:09 -070028#include <numeric>
Allie Woodeb9e6d82015-04-17 13:55:30 -070029#include <string>
30
Alex Deymo20c99202015-07-09 16:14:16 -070031#include <base/bind.h>
Kelvin Zhangec205cf2020-09-28 13:23:40 -040032#include <brillo/data_encoding.h>
33#include <brillo/message_loops/message_loop.h>
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -050034#include <brillo/secure_blob.h>
Kelvin Zhangec205cf2020-09-28 13:23:40 -040035#include <brillo/streams/file_stream.h>
Allie Woodeb9e6d82015-04-17 13:55:30 -070036
Kelvin Zhangab15beb2022-12-05 10:19:52 -080037#include "update_engine/common/error_code.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080038#include "update_engine/common/utils.h"
Kelvin Zhangec205cf2020-09-28 13:23:40 -040039#include "update_engine/payload_consumer/file_descriptor.h"
Kelvin Zhangab15beb2022-12-05 10:19:52 -080040#include "update_engine/payload_consumer/install_plan.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;
Daniel Zheng3efb3f22022-12-19 22:14:20 +000080constexpr float kVerityProgressPercent = 0.3;
81constexpr float kEncodeFECPercent = 0.3;
82
Allie Woodeb9e6d82015-04-17 13:55:30 -070083} // namespace
84
Allie Woodeb9e6d82015-04-17 13:55:30 -070085void FilesystemVerifierAction::PerformAction() {
86 // Will tell the ActionProcessor we've failed if we return.
87 ScopedActionCompleter abort_action_completer(processor_, this);
88
89 if (!HasInputObject()) {
90 LOG(ERROR) << "FilesystemVerifierAction missing input object.";
91 return;
92 }
93 install_plan_ = GetInputObject();
94
Alex Deymoe5e5fe92015-10-05 09:28:19 -070095 if (install_plan_.partitions.empty()) {
Kelvin Zhang430852d2023-05-25 17:00:24 -070096 LOG(ERROR) << "No partitions to verify.";
Allie Woodeb9e6d82015-04-17 13:55:30 -070097 if (HasOutputPipe())
98 SetOutputObject(install_plan_);
Kelvin Zhang430852d2023-05-25 17:00:24 -070099 abort_action_completer.set_code(ErrorCode::kFilesystemVerifierError);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700100 return;
101 }
Kelvin Zhang5b145822022-07-26 10:56:09 -0700102 // partition_weight_[i] = total size of partitions before index i.
103 partition_weight_.clear();
104 partition_weight_.reserve(install_plan_.partitions.size() + 1);
105 partition_weight_.push_back(0);
106 for (const auto& part : install_plan_.partitions) {
107 partition_weight_.push_back(part.target_size);
108 }
109 std::partial_sum(partition_weight_.begin(),
110 partition_weight_.end(),
111 partition_weight_.begin(),
liuqiangf1a360b2024-09-25 17:44:58 +0800112 std::plus<uint64_t>());
Kelvin Zhang5b145822022-07-26 10:56:09 -0700113
Jae Hoon Kim50504d62020-04-23 14:32:38 -0700114 install_plan_.Dump();
Kelvin Zhangab15beb2022-12-05 10:19:52 -0800115 // If we are not writing verity, just map all partitions once at the
116 // beginning.
117 // No need to re-map for each partition, because we are not writing any new
118 // COW data.
119 if (dynamic_control_->UpdateUsesSnapshotCompression() &&
120 !install_plan_.write_verity) {
121 dynamic_control_->MapAllPartitions();
122 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700123 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700124 abort_action_completer.set_should_complete(false);
125}
126
127void FilesystemVerifierAction::TerminateProcessing() {
Alex Deymo20c99202015-07-09 16:14:16 -0700128 cancelled_ = true;
129 Cleanup(ErrorCode::kSuccess); // error code is ignored if canceled_ is true.
Allie Woodeb9e6d82015-04-17 13:55:30 -0700130}
131
Allie Woodeb9e6d82015-04-17 13:55:30 -0700132void FilesystemVerifierAction::Cleanup(ErrorCode code) {
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500133 partition_fd_.reset();
Alex Deymo20c99202015-07-09 16:14:16 -0700134 // This memory is not used anymore.
135 buffer_.clear();
136
Kelvin Zhang9105f4b2021-04-26 13:44:49 -0400137 // If we didn't write verity, partitions were maped. Releaase resource now.
138 if (!install_plan_.write_verity &&
139 dynamic_control_->UpdateUsesSnapshotCompression()) {
140 LOG(INFO) << "Not writing verity and VABC is enabled, unmapping all "
141 "partitions";
142 dynamic_control_->UnmapAllPartitions();
143 }
144
Allie Woodeb9e6d82015-04-17 13:55:30 -0700145 if (cancelled_)
146 return;
147 if (code == ErrorCode::kSuccess && HasOutputPipe())
148 SetOutputObject(install_plan_);
Kelvin Zhang70eef232020-06-12 20:32:40 +0000149 UpdateProgress(1.0);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700150 processor_->ActionComplete(this, code);
151}
152
Kelvin Zhang70eef232020-06-12 20:32:40 +0000153void FilesystemVerifierAction::UpdateProgress(double progress) {
154 if (delegate_ != nullptr) {
155 delegate_->OnVerifyProgressUpdate(progress);
156 }
157}
158
Kelvin Zhang8704c832021-05-10 17:53:14 -0400159void FilesystemVerifierAction::UpdatePartitionProgress(double progress) {
Kelvin Zhang5b145822022-07-26 10:56:09 -0700160 UpdateProgress((partition_weight_[partition_index_] * (1 - progress) +
161 partition_weight_[partition_index_ + 1] * progress) /
162 partition_weight_.back());
Kelvin Zhang8704c832021-05-10 17:53:14 -0400163}
164
165bool FilesystemVerifierAction::InitializeFdVABC(bool should_write_verity) {
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400166 const InstallPlan::Partition& partition =
167 install_plan_.partitions[partition_index_];
168
Kelvin Zhang8704c832021-05-10 17:53:14 -0400169 if (!should_write_verity) {
170 // In VABC, we cannot map/unmap partitions w/o first closing ALL fds first.
171 // Since this function might be called inside a ScheduledTask, the closure
172 // might have a copy of partition_fd_ when executing this function. Which
173 // means even if we do |partition_fd_.reset()| here, there's a chance that
174 // underlying fd isn't closed until we return. This is unacceptable, we need
175 // to close |partition_fd| right away.
176 if (partition_fd_) {
177 partition_fd_->Close();
178 partition_fd_.reset();
179 }
Kelvin Zhang9105f4b2021-04-26 13:44:49 -0400180 // In VABC, if we are not writing verity, just map all partitions,
181 // and read using regular fd on |postinstall_mount_device| .
182 // All read will go through snapuserd, which provides a consistent
183 // view: device will use snapuserd to read partition during boot.
184 // b/186196758
185 // Call UnmapAllPartitions() first, because if we wrote verity before, these
186 // writes won't be visible to previously opened snapuserd daemon. To ensure
187 // that we will see the most up to date data from partitions, call Unmap()
188 // then Map() to re-spin daemon.
Kelvin Zhangab15beb2022-12-05 10:19:52 -0800189 if (install_plan_.write_verity) {
190 dynamic_control_->UnmapAllPartitions();
191 dynamic_control_->MapAllPartitions();
192 }
Kelvin Zhang9105f4b2021-04-26 13:44:49 -0400193 return InitializeFd(partition.readonly_target_path);
194 }
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500195 partition_fd_ =
Kelvin Zhang21a49912021-03-12 14:28:33 -0500196 dynamic_control_->OpenCowFd(partition.name, partition.source_path, true);
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500197 if (!partition_fd_) {
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400198 LOG(ERROR) << "OpenCowReader(" << partition.name << ", "
199 << partition.source_path << ") failed.";
200 return false;
201 }
202 partition_size_ = partition.target_size;
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400203 return true;
204}
205
206bool FilesystemVerifierAction::InitializeFd(const std::string& part_path) {
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800207 partition_fd_ = std::make_unique<EintrSafeFileDescriptor>();
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500208 const bool write_verity = ShouldWriteVerity();
209 int flags = write_verity ? O_RDWR : O_RDONLY;
210 if (!utils::SetBlockDeviceReadOnly(part_path, !write_verity)) {
211 LOG(WARNING) << "Failed to set block device " << part_path << " as "
212 << (write_verity ? "writable" : "readonly");
213 }
214 if (!partition_fd_->Open(part_path.c_str(), flags)) {
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400215 LOG(ERROR) << "Unable to open " << part_path << " for reading.";
216 return false;
217 }
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400218 return true;
219}
220
Daniel Zhengbd16b012022-09-28 23:40:05 +0000221void FilesystemVerifierAction::WriteVerityData(FileDescriptor* fd,
222 void* buffer,
223 const size_t buffer_size) {
224 if (verity_writer_->FECFinished()) {
225 LOG(INFO) << "EncodeFEC is completed. Resuming other tasks";
226 if (dynamic_control_->UpdateUsesSnapshotCompression()) {
227 // Spin up snapuserd to read fs.
228 if (!InitializeFdVABC(false)) {
229 LOG(ERROR) << "Failed to map all partitions";
230 Cleanup(ErrorCode::kFilesystemVerifierError);
231 return;
232 }
233 }
234 HashPartition(0, partition_size_, buffer, buffer_size);
235 return;
236 }
237 if (!verity_writer_->IncrementalFinalize(fd, fd)) {
238 LOG(ERROR) << "Failed to write verity data";
239 Cleanup(ErrorCode::kVerityCalculationError);
240 }
Daniel Zheng3efb3f22022-12-19 22:14:20 +0000241 UpdatePartitionProgress(kVerityProgressPercent +
242 verity_writer_->GetProgress() * kEncodeFECPercent);
Daniel Zhengbd16b012022-09-28 23:40:05 +0000243 CHECK(pending_task_id_.PostTask(
244 FROM_HERE,
245 base::BindOnce(&FilesystemVerifierAction::WriteVerityData,
246 base::Unretained(this),
247 fd,
248 buffer,
249 buffer_size)));
250}
251
Kelvin Zhang8704c832021-05-10 17:53:14 -0400252void FilesystemVerifierAction::WriteVerityAndHashPartition(
Kelvin Zhang8704c832021-05-10 17:53:14 -0400253 const off64_t start_offset,
254 const off64_t end_offset,
255 void* buffer,
256 const size_t buffer_size) {
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800257 auto fd = partition_fd_.get();
258 TEST_AND_RETURN(fd != nullptr);
Kelvin Zhang8704c832021-05-10 17:53:14 -0400259 if (start_offset >= end_offset) {
260 LOG_IF(WARNING, start_offset > end_offset)
261 << "start_offset is greater than end_offset : " << start_offset << " > "
262 << end_offset;
Daniel Zhengbd16b012022-09-28 23:40:05 +0000263 WriteVerityData(fd, buffer, buffer_size);
Kelvin Zhang8704c832021-05-10 17:53:14 -0400264 return;
265 }
266 const auto cur_offset = fd->Seek(start_offset, SEEK_SET);
267 if (cur_offset != start_offset) {
268 PLOG(ERROR) << "Failed to seek to offset: " << start_offset;
269 Cleanup(ErrorCode::kVerityCalculationError);
270 return;
271 }
272 const auto read_size =
Daniel Zhengda3a49b2024-08-16 13:23:28 -0700273 std::min<uint64_t>(buffer_size, end_offset - start_offset);
Kelvin Zhang8704c832021-05-10 17:53:14 -0400274 const auto bytes_read = fd->Read(buffer, read_size);
275 if (bytes_read < 0 || static_cast<size_t>(bytes_read) != read_size) {
276 PLOG(ERROR) << "Failed to read offset " << start_offset << " expected "
277 << read_size << " bytes, actual: " << bytes_read;
278 Cleanup(ErrorCode::kVerityCalculationError);
279 return;
280 }
281 if (!verity_writer_->Update(
282 start_offset, static_cast<const uint8_t*>(buffer), read_size)) {
283 LOG(ERROR) << "VerityWriter::Update() failed";
284 Cleanup(ErrorCode::kVerityCalculationError);
285 return;
286 }
287 UpdatePartitionProgress((start_offset + bytes_read) * 1.0f / partition_size_ *
Kelvin Zhang1d99ae12021-05-12 13:29:27 -0400288 kVerityProgressPercent);
Kelvin Zhang8704c832021-05-10 17:53:14 -0400289 CHECK(pending_task_id_.PostTask(
290 FROM_HERE,
291 base::BindOnce(&FilesystemVerifierAction::WriteVerityAndHashPartition,
292 base::Unretained(this),
Kelvin Zhang8704c832021-05-10 17:53:14 -0400293 start_offset + bytes_read,
294 end_offset,
295 buffer,
296 buffer_size)));
297}
298
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800299void FilesystemVerifierAction::HashPartition(const off64_t start_offset,
Kelvin Zhang8704c832021-05-10 17:53:14 -0400300 const off64_t end_offset,
301 void* buffer,
302 const size_t buffer_size) {
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800303 auto fd = partition_fd_.get();
304 TEST_AND_RETURN(fd != nullptr);
Kelvin Zhang8704c832021-05-10 17:53:14 -0400305 if (start_offset >= end_offset) {
306 LOG_IF(WARNING, start_offset > end_offset)
307 << "start_offset is greater than end_offset : " << start_offset << " > "
308 << end_offset;
309 FinishPartitionHashing();
310 return;
311 }
312 const auto cur_offset = fd->Seek(start_offset, SEEK_SET);
313 if (cur_offset != start_offset) {
314 PLOG(ERROR) << "Failed to seek to offset: " << start_offset;
315 Cleanup(ErrorCode::kFilesystemVerifierError);
316 return;
317 }
318 const auto read_size =
Daniel Zheng6963a922024-08-21 01:09:28 +0000319 std::min<uint64_t>(buffer_size, end_offset - start_offset);
Kelvin Zhang8704c832021-05-10 17:53:14 -0400320 const auto bytes_read = fd->Read(buffer, read_size);
321 if (bytes_read < 0 || static_cast<size_t>(bytes_read) != read_size) {
322 PLOG(ERROR) << "Failed to read offset " << start_offset << " expected "
323 << read_size << " bytes, actual: " << bytes_read;
324 Cleanup(ErrorCode::kFilesystemVerifierError);
325 return;
326 }
327 if (!hasher_->Update(buffer, read_size)) {
328 LOG(ERROR) << "Hasher updated failed on offset" << start_offset;
329 Cleanup(ErrorCode::kFilesystemVerifierError);
330 return;
331 }
332 const auto progress = (start_offset + bytes_read) * 1.0f / partition_size_;
Kelvin Zhang5b145822022-07-26 10:56:09 -0700333 // If we are writing verity, then the progress bar will be split between
334 // verity writes and partition hashing. Otherwise, the entire progress bar is
335 // dedicated to partition hashing for smooth progress.
336 if (ShouldWriteVerity()) {
Daniel Zheng3efb3f22022-12-19 22:14:20 +0000337 UpdatePartitionProgress(
338 progress * (1 - (kVerityProgressPercent + kEncodeFECPercent)) +
339 kVerityProgressPercent + kEncodeFECPercent);
Kelvin Zhang5b145822022-07-26 10:56:09 -0700340 } else {
341 UpdatePartitionProgress(progress);
342 }
Kelvin Zhang8704c832021-05-10 17:53:14 -0400343 CHECK(pending_task_id_.PostTask(
344 FROM_HERE,
345 base::BindOnce(&FilesystemVerifierAction::HashPartition,
346 base::Unretained(this),
Kelvin Zhang8704c832021-05-10 17:53:14 -0400347 start_offset + bytes_read,
348 end_offset,
349 buffer,
350 buffer_size)));
351}
352
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700353void FilesystemVerifierAction::StartPartitionHashing() {
354 if (partition_index_ == install_plan_.partitions.size()) {
Tianjie24f96092020-06-30 12:26:25 -0700355 if (!install_plan_.untouched_dynamic_partitions.empty()) {
356 LOG(INFO) << "Verifying extents of untouched dynamic partitions ["
Kelvin Zhang0c184242024-10-25 11:19:27 -0700357 << android::base::Join(
358 install_plan_.untouched_dynamic_partitions, ", ")
Tianjie24f96092020-06-30 12:26:25 -0700359 << "]";
360 if (!dynamic_control_->VerifyExtentsForUntouchedPartitions(
361 install_plan_.source_slot,
362 install_plan_.target_slot,
363 install_plan_.untouched_dynamic_partitions)) {
364 Cleanup(ErrorCode::kFilesystemVerifierError);
365 return;
366 }
367 }
368
Sen Jianga35896c2016-05-25 11:08:41 -0700369 Cleanup(ErrorCode::kSuccess);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700370 return;
371 }
Sen Jiang57f91802017-11-14 17:42:13 -0800372 const InstallPlan::Partition& partition =
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700373 install_plan_.partitions[partition_index_];
Kelvin Zhange012f652021-05-10 16:00:31 -0400374 const auto& part_path = GetPartitionPath();
375 partition_size_ = GetPartitionSize();
Yifan Hong537802d2018-08-15 13:15:42 -0700376
Yifan Hong537802d2018-08-15 13:15:42 -0700377 LOG(INFO) << "Hashing partition " << partition_index_ << " ("
378 << partition.name << ") on device " << part_path;
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400379 auto success = false;
Kelvin Zhange012f652021-05-10 16:00:31 -0400380 if (IsVABC(partition)) {
Kelvin Zhang8704c832021-05-10 17:53:14 -0400381 success = InitializeFdVABC(ShouldWriteVerity());
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400382 } else {
383 if (part_path.empty()) {
384 if (partition_size_ == 0) {
385 LOG(INFO) << "Skip hashing partition " << partition_index_ << " ("
386 << partition.name << ") because size is 0.";
387 partition_index_++;
388 StartPartitionHashing();
389 return;
390 }
391 LOG(ERROR) << "Cannot hash partition " << partition_index_ << " ("
392 << partition.name
393 << ") because its device path cannot be determined.";
394 Cleanup(ErrorCode::kFilesystemVerifierError);
395 return;
396 }
397 success = InitializeFd(part_path);
398 }
399 if (!success) {
Sen Jiang57f91802017-11-14 17:42:13 -0800400 Cleanup(ErrorCode::kFilesystemVerifierError);
401 return;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700402 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700403 buffer_.resize(kReadFileBufferSize);
Sen Jiang57f91802017-11-14 17:42:13 -0800404 hasher_ = std::make_unique<HashCalculator>();
405
Kelvin Zhang7f925672021-03-15 13:37:40 -0400406 filesystem_data_end_ = partition_size_;
Kelvin Zhang5e5ad392021-07-26 21:42:06 -0400407 if (partition.fec_offset > 0) {
408 CHECK_LE(partition.hash_tree_offset, partition.fec_offset)
409 << " Hash tree is expected to come before FEC data";
410 }
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800411 CHECK_NE(partition_fd_, nullptr);
Kelvin Zhang7f925672021-03-15 13:37:40 -0400412 if (partition.hash_tree_offset != 0) {
413 filesystem_data_end_ = partition.hash_tree_offset;
414 } else if (partition.fec_offset != 0) {
415 filesystem_data_end_ = partition.fec_offset;
416 }
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400417 if (ShouldWriteVerity()) {
Kelvin Zhang8704c832021-05-10 17:53:14 -0400418 LOG(INFO) << "Verity writes enabled on partition " << partition.name;
Kelvin Zhang7f925672021-03-15 13:37:40 -0400419 if (!verity_writer_->Init(partition)) {
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500420 LOG(INFO) << "Verity writes enabled on partition " << partition.name;
Sen Jiang57f91802017-11-14 17:42:13 -0800421 Cleanup(ErrorCode::kVerityCalculationError);
422 return;
423 }
Kelvin Zhang8704c832021-05-10 17:53:14 -0400424 WriteVerityAndHashPartition(
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800425 0, filesystem_data_end_, buffer_.data(), buffer_.size());
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500426 } else {
427 LOG(INFO) << "Verity writes disabled on partition " << partition.name;
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800428 HashPartition(0, partition_size_, buffer_.data(), buffer_.size());
Sen Jiang57f91802017-11-14 17:42:13 -0800429 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700430}
431
Kelvin Zhange012f652021-05-10 16:00:31 -0400432bool FilesystemVerifierAction::IsVABC(
433 const InstallPlan::Partition& partition) const {
434 return dynamic_control_->UpdateUsesSnapshotCompression() &&
435 verifier_step_ == VerifierStep::kVerifyTargetHash &&
436 dynamic_control_->IsDynamicPartition(partition.name,
437 install_plan_.target_slot);
438}
439
440const std::string& FilesystemVerifierAction::GetPartitionPath() const {
441 const InstallPlan::Partition& partition =
442 install_plan_.partitions[partition_index_];
443 switch (verifier_step_) {
444 case VerifierStep::kVerifySourceHash:
445 return partition.source_path;
446 case VerifierStep::kVerifyTargetHash:
447 if (IsVABC(partition)) {
448 return partition.readonly_target_path;
449 } else {
450 return partition.target_path;
451 }
452 }
453}
454
Daniel Zheng6db5f792024-08-28 18:13:05 +0000455uint64_t FilesystemVerifierAction::GetPartitionSize() const {
Kelvin Zhange012f652021-05-10 16:00:31 -0400456 const InstallPlan::Partition& partition =
457 install_plan_.partitions[partition_index_];
458 switch (verifier_step_) {
459 case VerifierStep::kVerifySourceHash:
460 return partition.source_size;
461 case VerifierStep::kVerifyTargetHash:
462 return partition.target_size;
463 }
464}
465
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400466bool FilesystemVerifierAction::ShouldWriteVerity() {
467 const InstallPlan::Partition& partition =
468 install_plan_.partitions[partition_index_];
469 return verifier_step_ == VerifierStep::kVerifyTargetHash &&
470 install_plan_.write_verity &&
471 (partition.hash_tree_size > 0 || partition.fec_size > 0);
472}
473
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700474void FilesystemVerifierAction::FinishPartitionHashing() {
475 if (!hasher_->Finalize()) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700476 LOG(ERROR) << "Unable to finalize the hash.";
Sen Jiang57f91802017-11-14 17:42:13 -0800477 Cleanup(ErrorCode::kError);
478 return;
Alex Deymob9e8e262015-08-03 20:23:03 -0700479 }
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800480 const InstallPlan::Partition& partition =
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700481 install_plan_.partitions[partition_index_];
Sen Jiang2703ef42017-03-16 13:36:21 -0700482 LOG(INFO) << "Hash of " << partition.name << ": "
Kelvin Zhang3fe49642021-10-04 15:35:02 -0700483 << HexEncode(hasher_->raw_hash());
Alex Deymob9e8e262015-08-03 20:23:03 -0700484
Sen Jiangfef85fd2016-03-25 15:32:49 -0700485 switch (verifier_step_) {
486 case VerifierStep::kVerifyTargetHash:
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700487 if (partition.target_hash != hasher_->raw_hash()) {
488 LOG(ERROR) << "New '" << partition.name
489 << "' partition verification failed.";
Sen Jiangcdd52062017-05-18 15:33:10 -0700490 if (partition.source_hash.empty()) {
491 // No need to verify source if it is a full payload.
Sen Jiang57f91802017-11-14 17:42:13 -0800492 Cleanup(ErrorCode::kNewRootfsVerificationError);
493 return;
Sen Jiangcdd52062017-05-18 15:33:10 -0700494 }
Sen Jiangfef85fd2016-03-25 15:32:49 -0700495 // If we have not verified source partition yet, now that the target
Sen Jiang65566a32016-04-06 13:35:36 -0700496 // partition does not match, and it's not a full payload, we need to
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400497 // switch to kVerifySourceHash step to check if it's because the
498 // source partition does not match either.
Sen Jiangfef85fd2016-03-25 15:32:49 -0700499 verifier_step_ = VerifierStep::kVerifySourceHash;
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800500 } else {
501 partition_index_++;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700502 }
503 break;
Sen Jiangfef85fd2016-03-25 15:32:49 -0700504 case VerifierStep::kVerifySourceHash:
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800505 if (partition.source_hash != hasher_->raw_hash()) {
506 LOG(ERROR) << "Old '" << partition.name
507 << "' partition verification failed.";
Sen Jiangfef85fd2016-03-25 15:32:49 -0700508 LOG(ERROR) << "This is a server-side error due to mismatched delta"
509 << " update image!";
510 LOG(ERROR) << "The delta I've been given contains a " << partition.name
511 << " delta update that must be applied over a "
512 << partition.name << " with a specific checksum, but the "
513 << partition.name
514 << " we're starting with doesn't have that checksum! This"
515 " means that the delta I've been given doesn't match my"
516 " existing system. The "
517 << partition.name << " partition I have has hash: "
Sen Jiang2703ef42017-03-16 13:36:21 -0700518 << Base64Encode(hasher_->raw_hash())
Sen Jiangfef85fd2016-03-25 15:32:49 -0700519 << " but the update expected me to have "
Sen Jiang2703ef42017-03-16 13:36:21 -0700520 << Base64Encode(partition.source_hash) << " .";
Sen Jiangfef85fd2016-03-25 15:32:49 -0700521 LOG(INFO) << "To get the checksum of the " << partition.name
522 << " partition run this command: dd if="
523 << partition.source_path
524 << " bs=1M count=" << partition.source_size
525 << " iflag=count_bytes 2>/dev/null | openssl dgst -sha256 "
526 "-binary | openssl base64";
527 LOG(INFO) << "To get the checksum of partitions in a bin file, "
528 << "run: .../src/scripts/sha256_partitions.sh .../file.bin";
Sen Jiang57f91802017-11-14 17:42:13 -0800529 Cleanup(ErrorCode::kDownloadStateInitializationError);
530 return;
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800531 }
Sen Jianga35896c2016-05-25 11:08:41 -0700532 // The action will skip kVerifySourceHash step if target partition hash
533 // matches, if we are in this step, it means target hash does not match,
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400534 // and now that the source partition hash matches, we should set the
535 // error code to reflect the error in target partition. We only need to
536 // verify the source partition which the target hash does not match, the
537 // rest of the partitions don't matter.
Sen Jiang57f91802017-11-14 17:42:13 -0800538 Cleanup(ErrorCode::kNewRootfsVerificationError);
539 return;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700540 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700541 // Start hashing the next partition, if any.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700542 buffer_.clear();
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500543 if (partition_fd_) {
Kelvin Zhang8704c832021-05-10 17:53:14 -0400544 partition_fd_->Close();
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500545 partition_fd_.reset();
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400546 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700547 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700548}
549
550} // namespace chromeos_update_engine