blob: 4990358e9687a2d86a8c24f45ac8724b0a371f6c [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>
Daniel Zhengbd16b012022-09-28 23:40:05 +000027#include <functional>
Kelvin Zhangec205cf2020-09-28 13:23:40 -040028#include <memory>
Kelvin Zhang5b145822022-07-26 10:56:09 -070029#include <numeric>
Allie Woodeb9e6d82015-04-17 13:55:30 -070030#include <string>
Kelvin Zhangec205cf2020-09-28 13:23:40 -040031#include <utility>
Allie Woodeb9e6d82015-04-17 13:55:30 -070032
Alex Deymo20c99202015-07-09 16:14:16 -070033#include <base/bind.h>
Tianjie24f96092020-06-30 12:26:25 -070034#include <base/strings/string_util.h>
Kelvin Zhangec205cf2020-09-28 13:23:40 -040035#include <brillo/data_encoding.h>
36#include <brillo/message_loops/message_loop.h>
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -050037#include <brillo/secure_blob.h>
Kelvin Zhangec205cf2020-09-28 13:23:40 -040038#include <brillo/streams/file_stream.h>
Allie Woodeb9e6d82015-04-17 13:55:30 -070039
Kelvin Zhangab15beb2022-12-05 10:19:52 -080040#include "update_engine/common/error_code.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080041#include "update_engine/common/utils.h"
Kelvin Zhangec205cf2020-09-28 13:23:40 -040042#include "update_engine/payload_consumer/file_descriptor.h"
Kelvin Zhangab15beb2022-12-05 10:19:52 -080043#include "update_engine/payload_consumer/install_plan.h"
Allie Woodeb9e6d82015-04-17 13:55:30 -070044
Sen Jiang2703ef42017-03-16 13:36:21 -070045using brillo::data_encoding::Base64Encode;
Allie Woodeb9e6d82015-04-17 13:55:30 -070046using std::string;
47
Kelvin Zhang7f925672021-03-15 13:37:40 -040048// On a partition with verity enabled, we expect to see the following format:
49// ===================================================
50// Normal Filesystem Data
51// (this should take most of the space, like over 90%)
52// ===================================================
53// Hash tree
54// ~0.8% (e.g. 16M for 2GB image)
55// ===================================================
56// FEC data
57// ~0.8%
58// ===================================================
59// Footer
60// 4K
61// ===================================================
62
63// For OTA that doesn't do on device verity computation, hash tree and fec data
64// are written during DownloadAction as a regular InstallOp, so no special
65// handling needed, we can just read the entire partition in 1 go.
66
67// Verity enabled case: Only Normal FS data is written during download action.
68// When hasing the entire partition, we will need to build the hash tree, write
69// it to disk, then build FEC, and write it to disk. Therefore, it is important
70// that we finish writing hash tree before we attempt to read & hash it. The
71// same principal applies to FEC data.
72
73// |verity_writer_| handles building and
74// writing of FEC/HashTree, we just need to be careful when reading.
75// Specifically, we must stop at beginning of Hash tree, let |verity_writer_|
76// write both hash tree and FEC, then continue reading the remaining part of
77// partition.
78
Allie Woodeb9e6d82015-04-17 13:55:30 -070079namespace chromeos_update_engine {
80
81namespace {
Alex Deymo20c99202015-07-09 16:14:16 -070082const off_t kReadFileBufferSize = 128 * 1024;
Daniel Zheng3efb3f22022-12-19 22:14:20 +000083constexpr float kVerityProgressPercent = 0.3;
84constexpr float kEncodeFECPercent = 0.3;
85
Allie Woodeb9e6d82015-04-17 13:55:30 -070086} // namespace
87
Allie Woodeb9e6d82015-04-17 13:55:30 -070088void FilesystemVerifierAction::PerformAction() {
89 // Will tell the ActionProcessor we've failed if we return.
90 ScopedActionCompleter abort_action_completer(processor_, this);
91
92 if (!HasInputObject()) {
93 LOG(ERROR) << "FilesystemVerifierAction missing input object.";
94 return;
95 }
96 install_plan_ = GetInputObject();
97
Alex Deymoe5e5fe92015-10-05 09:28:19 -070098 if (install_plan_.partitions.empty()) {
Kelvin Zhang430852d2023-05-25 17:00:24 -070099 LOG(ERROR) << "No partitions to verify.";
Allie Woodeb9e6d82015-04-17 13:55:30 -0700100 if (HasOutputPipe())
101 SetOutputObject(install_plan_);
Kelvin Zhang430852d2023-05-25 17:00:24 -0700102 abort_action_completer.set_code(ErrorCode::kFilesystemVerifierError);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700103 return;
104 }
Kelvin Zhang5b145822022-07-26 10:56:09 -0700105 // partition_weight_[i] = total size of partitions before index i.
106 partition_weight_.clear();
107 partition_weight_.reserve(install_plan_.partitions.size() + 1);
108 partition_weight_.push_back(0);
109 for (const auto& part : install_plan_.partitions) {
110 partition_weight_.push_back(part.target_size);
111 }
112 std::partial_sum(partition_weight_.begin(),
113 partition_weight_.end(),
114 partition_weight_.begin(),
liuqiangf1a360b2024-09-25 17:44:58 +0800115 std::plus<uint64_t>());
Kelvin Zhang5b145822022-07-26 10:56:09 -0700116
Jae Hoon Kim50504d62020-04-23 14:32:38 -0700117 install_plan_.Dump();
Kelvin Zhangab15beb2022-12-05 10:19:52 -0800118 // If we are not writing verity, just map all partitions once at the
119 // beginning.
120 // No need to re-map for each partition, because we are not writing any new
121 // COW data.
122 if (dynamic_control_->UpdateUsesSnapshotCompression() &&
123 !install_plan_.write_verity) {
124 dynamic_control_->MapAllPartitions();
125 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700126 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700127 abort_action_completer.set_should_complete(false);
128}
129
130void FilesystemVerifierAction::TerminateProcessing() {
Alex Deymo20c99202015-07-09 16:14:16 -0700131 cancelled_ = true;
132 Cleanup(ErrorCode::kSuccess); // error code is ignored if canceled_ is true.
Allie Woodeb9e6d82015-04-17 13:55:30 -0700133}
134
Allie Woodeb9e6d82015-04-17 13:55:30 -0700135void FilesystemVerifierAction::Cleanup(ErrorCode code) {
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500136 partition_fd_.reset();
Alex Deymo20c99202015-07-09 16:14:16 -0700137 // This memory is not used anymore.
138 buffer_.clear();
139
Kelvin Zhang9105f4b2021-04-26 13:44:49 -0400140 // If we didn't write verity, partitions were maped. Releaase resource now.
141 if (!install_plan_.write_verity &&
142 dynamic_control_->UpdateUsesSnapshotCompression()) {
143 LOG(INFO) << "Not writing verity and VABC is enabled, unmapping all "
144 "partitions";
145 dynamic_control_->UnmapAllPartitions();
146 }
147
Allie Woodeb9e6d82015-04-17 13:55:30 -0700148 if (cancelled_)
149 return;
150 if (code == ErrorCode::kSuccess && HasOutputPipe())
151 SetOutputObject(install_plan_);
Kelvin Zhang70eef232020-06-12 20:32:40 +0000152 UpdateProgress(1.0);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700153 processor_->ActionComplete(this, code);
154}
155
Kelvin Zhang70eef232020-06-12 20:32:40 +0000156void FilesystemVerifierAction::UpdateProgress(double progress) {
157 if (delegate_ != nullptr) {
158 delegate_->OnVerifyProgressUpdate(progress);
159 }
160}
161
Kelvin Zhang8704c832021-05-10 17:53:14 -0400162void FilesystemVerifierAction::UpdatePartitionProgress(double progress) {
Kelvin Zhang5b145822022-07-26 10:56:09 -0700163 UpdateProgress((partition_weight_[partition_index_] * (1 - progress) +
164 partition_weight_[partition_index_ + 1] * progress) /
165 partition_weight_.back());
Kelvin Zhang8704c832021-05-10 17:53:14 -0400166}
167
168bool FilesystemVerifierAction::InitializeFdVABC(bool should_write_verity) {
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400169 const InstallPlan::Partition& partition =
170 install_plan_.partitions[partition_index_];
171
Kelvin Zhang8704c832021-05-10 17:53:14 -0400172 if (!should_write_verity) {
173 // In VABC, we cannot map/unmap partitions w/o first closing ALL fds first.
174 // Since this function might be called inside a ScheduledTask, the closure
175 // might have a copy of partition_fd_ when executing this function. Which
176 // means even if we do |partition_fd_.reset()| here, there's a chance that
177 // underlying fd isn't closed until we return. This is unacceptable, we need
178 // to close |partition_fd| right away.
179 if (partition_fd_) {
180 partition_fd_->Close();
181 partition_fd_.reset();
182 }
Kelvin Zhang9105f4b2021-04-26 13:44:49 -0400183 // In VABC, if we are not writing verity, just map all partitions,
184 // and read using regular fd on |postinstall_mount_device| .
185 // All read will go through snapuserd, which provides a consistent
186 // view: device will use snapuserd to read partition during boot.
187 // b/186196758
188 // Call UnmapAllPartitions() first, because if we wrote verity before, these
189 // writes won't be visible to previously opened snapuserd daemon. To ensure
190 // that we will see the most up to date data from partitions, call Unmap()
191 // then Map() to re-spin daemon.
Kelvin Zhangab15beb2022-12-05 10:19:52 -0800192 if (install_plan_.write_verity) {
193 dynamic_control_->UnmapAllPartitions();
194 dynamic_control_->MapAllPartitions();
195 }
Kelvin Zhang9105f4b2021-04-26 13:44:49 -0400196 return InitializeFd(partition.readonly_target_path);
197 }
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500198 partition_fd_ =
Kelvin Zhang21a49912021-03-12 14:28:33 -0500199 dynamic_control_->OpenCowFd(partition.name, partition.source_path, true);
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500200 if (!partition_fd_) {
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400201 LOG(ERROR) << "OpenCowReader(" << partition.name << ", "
202 << partition.source_path << ") failed.";
203 return false;
204 }
205 partition_size_ = partition.target_size;
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400206 return true;
207}
208
209bool FilesystemVerifierAction::InitializeFd(const std::string& part_path) {
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800210 partition_fd_ = std::make_unique<EintrSafeFileDescriptor>();
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500211 const bool write_verity = ShouldWriteVerity();
212 int flags = write_verity ? O_RDWR : O_RDONLY;
213 if (!utils::SetBlockDeviceReadOnly(part_path, !write_verity)) {
214 LOG(WARNING) << "Failed to set block device " << part_path << " as "
215 << (write_verity ? "writable" : "readonly");
216 }
217 if (!partition_fd_->Open(part_path.c_str(), flags)) {
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400218 LOG(ERROR) << "Unable to open " << part_path << " for reading.";
219 return false;
220 }
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400221 return true;
222}
223
Daniel Zhengbd16b012022-09-28 23:40:05 +0000224void FilesystemVerifierAction::WriteVerityData(FileDescriptor* fd,
225 void* buffer,
226 const size_t buffer_size) {
227 if (verity_writer_->FECFinished()) {
228 LOG(INFO) << "EncodeFEC is completed. Resuming other tasks";
229 if (dynamic_control_->UpdateUsesSnapshotCompression()) {
230 // Spin up snapuserd to read fs.
231 if (!InitializeFdVABC(false)) {
232 LOG(ERROR) << "Failed to map all partitions";
233 Cleanup(ErrorCode::kFilesystemVerifierError);
234 return;
235 }
236 }
237 HashPartition(0, partition_size_, buffer, buffer_size);
238 return;
239 }
240 if (!verity_writer_->IncrementalFinalize(fd, fd)) {
241 LOG(ERROR) << "Failed to write verity data";
242 Cleanup(ErrorCode::kVerityCalculationError);
243 }
Daniel Zheng3efb3f22022-12-19 22:14:20 +0000244 UpdatePartitionProgress(kVerityProgressPercent +
245 verity_writer_->GetProgress() * kEncodeFECPercent);
Daniel Zhengbd16b012022-09-28 23:40:05 +0000246 CHECK(pending_task_id_.PostTask(
247 FROM_HERE,
248 base::BindOnce(&FilesystemVerifierAction::WriteVerityData,
249 base::Unretained(this),
250 fd,
251 buffer,
252 buffer_size)));
253}
254
Kelvin Zhang8704c832021-05-10 17:53:14 -0400255void FilesystemVerifierAction::WriteVerityAndHashPartition(
Kelvin Zhang8704c832021-05-10 17:53:14 -0400256 const off64_t start_offset,
257 const off64_t end_offset,
258 void* buffer,
259 const size_t buffer_size) {
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800260 auto fd = partition_fd_.get();
261 TEST_AND_RETURN(fd != nullptr);
Kelvin Zhang8704c832021-05-10 17:53:14 -0400262 if (start_offset >= end_offset) {
263 LOG_IF(WARNING, start_offset > end_offset)
264 << "start_offset is greater than end_offset : " << start_offset << " > "
265 << end_offset;
Daniel Zhengbd16b012022-09-28 23:40:05 +0000266 WriteVerityData(fd, buffer, buffer_size);
Kelvin Zhang8704c832021-05-10 17:53:14 -0400267 return;
268 }
269 const auto cur_offset = fd->Seek(start_offset, SEEK_SET);
270 if (cur_offset != start_offset) {
271 PLOG(ERROR) << "Failed to seek to offset: " << start_offset;
272 Cleanup(ErrorCode::kVerityCalculationError);
273 return;
274 }
275 const auto read_size =
Daniel Zhengda3a49b2024-08-16 13:23:28 -0700276 std::min<uint64_t>(buffer_size, end_offset - start_offset);
Kelvin Zhang8704c832021-05-10 17:53:14 -0400277 const auto bytes_read = fd->Read(buffer, read_size);
278 if (bytes_read < 0 || static_cast<size_t>(bytes_read) != read_size) {
279 PLOG(ERROR) << "Failed to read offset " << start_offset << " expected "
280 << read_size << " bytes, actual: " << bytes_read;
281 Cleanup(ErrorCode::kVerityCalculationError);
282 return;
283 }
284 if (!verity_writer_->Update(
285 start_offset, static_cast<const uint8_t*>(buffer), read_size)) {
286 LOG(ERROR) << "VerityWriter::Update() failed";
287 Cleanup(ErrorCode::kVerityCalculationError);
288 return;
289 }
290 UpdatePartitionProgress((start_offset + bytes_read) * 1.0f / partition_size_ *
Kelvin Zhang1d99ae12021-05-12 13:29:27 -0400291 kVerityProgressPercent);
Kelvin Zhang8704c832021-05-10 17:53:14 -0400292 CHECK(pending_task_id_.PostTask(
293 FROM_HERE,
294 base::BindOnce(&FilesystemVerifierAction::WriteVerityAndHashPartition,
295 base::Unretained(this),
Kelvin Zhang8704c832021-05-10 17:53:14 -0400296 start_offset + bytes_read,
297 end_offset,
298 buffer,
299 buffer_size)));
300}
301
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800302void FilesystemVerifierAction::HashPartition(const off64_t start_offset,
Kelvin Zhang8704c832021-05-10 17:53:14 -0400303 const off64_t end_offset,
304 void* buffer,
305 const size_t buffer_size) {
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800306 auto fd = partition_fd_.get();
307 TEST_AND_RETURN(fd != nullptr);
Kelvin Zhang8704c832021-05-10 17:53:14 -0400308 if (start_offset >= end_offset) {
309 LOG_IF(WARNING, start_offset > end_offset)
310 << "start_offset is greater than end_offset : " << start_offset << " > "
311 << end_offset;
312 FinishPartitionHashing();
313 return;
314 }
315 const auto cur_offset = fd->Seek(start_offset, SEEK_SET);
316 if (cur_offset != start_offset) {
317 PLOG(ERROR) << "Failed to seek to offset: " << start_offset;
318 Cleanup(ErrorCode::kFilesystemVerifierError);
319 return;
320 }
321 const auto read_size =
Daniel Zheng6963a922024-08-21 01:09:28 +0000322 std::min<uint64_t>(buffer_size, end_offset - start_offset);
Kelvin Zhang8704c832021-05-10 17:53:14 -0400323 const auto bytes_read = fd->Read(buffer, read_size);
324 if (bytes_read < 0 || static_cast<size_t>(bytes_read) != read_size) {
325 PLOG(ERROR) << "Failed to read offset " << start_offset << " expected "
326 << read_size << " bytes, actual: " << bytes_read;
327 Cleanup(ErrorCode::kFilesystemVerifierError);
328 return;
329 }
330 if (!hasher_->Update(buffer, read_size)) {
331 LOG(ERROR) << "Hasher updated failed on offset" << start_offset;
332 Cleanup(ErrorCode::kFilesystemVerifierError);
333 return;
334 }
335 const auto progress = (start_offset + bytes_read) * 1.0f / partition_size_;
Kelvin Zhang5b145822022-07-26 10:56:09 -0700336 // If we are writing verity, then the progress bar will be split between
337 // verity writes and partition hashing. Otherwise, the entire progress bar is
338 // dedicated to partition hashing for smooth progress.
339 if (ShouldWriteVerity()) {
Daniel Zheng3efb3f22022-12-19 22:14:20 +0000340 UpdatePartitionProgress(
341 progress * (1 - (kVerityProgressPercent + kEncodeFECPercent)) +
342 kVerityProgressPercent + kEncodeFECPercent);
Kelvin Zhang5b145822022-07-26 10:56:09 -0700343 } else {
344 UpdatePartitionProgress(progress);
345 }
Kelvin Zhang8704c832021-05-10 17:53:14 -0400346 CHECK(pending_task_id_.PostTask(
347 FROM_HERE,
348 base::BindOnce(&FilesystemVerifierAction::HashPartition,
349 base::Unretained(this),
Kelvin Zhang8704c832021-05-10 17:53:14 -0400350 start_offset + bytes_read,
351 end_offset,
352 buffer,
353 buffer_size)));
354}
355
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700356void FilesystemVerifierAction::StartPartitionHashing() {
357 if (partition_index_ == install_plan_.partitions.size()) {
Tianjie24f96092020-06-30 12:26:25 -0700358 if (!install_plan_.untouched_dynamic_partitions.empty()) {
359 LOG(INFO) << "Verifying extents of untouched dynamic partitions ["
360 << base::JoinString(install_plan_.untouched_dynamic_partitions,
361 ", ")
362 << "]";
363 if (!dynamic_control_->VerifyExtentsForUntouchedPartitions(
364 install_plan_.source_slot,
365 install_plan_.target_slot,
366 install_plan_.untouched_dynamic_partitions)) {
367 Cleanup(ErrorCode::kFilesystemVerifierError);
368 return;
369 }
370 }
371
Sen Jianga35896c2016-05-25 11:08:41 -0700372 Cleanup(ErrorCode::kSuccess);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700373 return;
374 }
Sen Jiang57f91802017-11-14 17:42:13 -0800375 const InstallPlan::Partition& partition =
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700376 install_plan_.partitions[partition_index_];
Kelvin Zhange012f652021-05-10 16:00:31 -0400377 const auto& part_path = GetPartitionPath();
378 partition_size_ = GetPartitionSize();
Yifan Hong537802d2018-08-15 13:15:42 -0700379
Yifan Hong537802d2018-08-15 13:15:42 -0700380 LOG(INFO) << "Hashing partition " << partition_index_ << " ("
381 << partition.name << ") on device " << part_path;
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400382 auto success = false;
Kelvin Zhange012f652021-05-10 16:00:31 -0400383 if (IsVABC(partition)) {
Kelvin Zhang8704c832021-05-10 17:53:14 -0400384 success = InitializeFdVABC(ShouldWriteVerity());
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400385 } else {
386 if (part_path.empty()) {
387 if (partition_size_ == 0) {
388 LOG(INFO) << "Skip hashing partition " << partition_index_ << " ("
389 << partition.name << ") because size is 0.";
390 partition_index_++;
391 StartPartitionHashing();
392 return;
393 }
394 LOG(ERROR) << "Cannot hash partition " << partition_index_ << " ("
395 << partition.name
396 << ") because its device path cannot be determined.";
397 Cleanup(ErrorCode::kFilesystemVerifierError);
398 return;
399 }
400 success = InitializeFd(part_path);
401 }
402 if (!success) {
Sen Jiang57f91802017-11-14 17:42:13 -0800403 Cleanup(ErrorCode::kFilesystemVerifierError);
404 return;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700405 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700406 buffer_.resize(kReadFileBufferSize);
Sen Jiang57f91802017-11-14 17:42:13 -0800407 hasher_ = std::make_unique<HashCalculator>();
408
Kelvin Zhang7f925672021-03-15 13:37:40 -0400409 filesystem_data_end_ = partition_size_;
Kelvin Zhang5e5ad392021-07-26 21:42:06 -0400410 if (partition.fec_offset > 0) {
411 CHECK_LE(partition.hash_tree_offset, partition.fec_offset)
412 << " Hash tree is expected to come before FEC data";
413 }
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800414 CHECK_NE(partition_fd_, nullptr);
Kelvin Zhang7f925672021-03-15 13:37:40 -0400415 if (partition.hash_tree_offset != 0) {
416 filesystem_data_end_ = partition.hash_tree_offset;
417 } else if (partition.fec_offset != 0) {
418 filesystem_data_end_ = partition.fec_offset;
419 }
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400420 if (ShouldWriteVerity()) {
Kelvin Zhang8704c832021-05-10 17:53:14 -0400421 LOG(INFO) << "Verity writes enabled on partition " << partition.name;
Kelvin Zhang7f925672021-03-15 13:37:40 -0400422 if (!verity_writer_->Init(partition)) {
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500423 LOG(INFO) << "Verity writes enabled on partition " << partition.name;
Sen Jiang57f91802017-11-14 17:42:13 -0800424 Cleanup(ErrorCode::kVerityCalculationError);
425 return;
426 }
Kelvin Zhang8704c832021-05-10 17:53:14 -0400427 WriteVerityAndHashPartition(
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800428 0, filesystem_data_end_, buffer_.data(), buffer_.size());
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500429 } else {
430 LOG(INFO) << "Verity writes disabled on partition " << partition.name;
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800431 HashPartition(0, partition_size_, buffer_.data(), buffer_.size());
Sen Jiang57f91802017-11-14 17:42:13 -0800432 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700433}
434
Kelvin Zhange012f652021-05-10 16:00:31 -0400435bool FilesystemVerifierAction::IsVABC(
436 const InstallPlan::Partition& partition) const {
437 return dynamic_control_->UpdateUsesSnapshotCompression() &&
438 verifier_step_ == VerifierStep::kVerifyTargetHash &&
439 dynamic_control_->IsDynamicPartition(partition.name,
440 install_plan_.target_slot);
441}
442
443const std::string& FilesystemVerifierAction::GetPartitionPath() const {
444 const InstallPlan::Partition& partition =
445 install_plan_.partitions[partition_index_];
446 switch (verifier_step_) {
447 case VerifierStep::kVerifySourceHash:
448 return partition.source_path;
449 case VerifierStep::kVerifyTargetHash:
450 if (IsVABC(partition)) {
451 return partition.readonly_target_path;
452 } else {
453 return partition.target_path;
454 }
455 }
456}
457
Daniel Zheng6db5f792024-08-28 18:13:05 +0000458uint64_t FilesystemVerifierAction::GetPartitionSize() const {
Kelvin Zhange012f652021-05-10 16:00:31 -0400459 const InstallPlan::Partition& partition =
460 install_plan_.partitions[partition_index_];
461 switch (verifier_step_) {
462 case VerifierStep::kVerifySourceHash:
463 return partition.source_size;
464 case VerifierStep::kVerifyTargetHash:
465 return partition.target_size;
466 }
467}
468
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400469bool FilesystemVerifierAction::ShouldWriteVerity() {
470 const InstallPlan::Partition& partition =
471 install_plan_.partitions[partition_index_];
472 return verifier_step_ == VerifierStep::kVerifyTargetHash &&
473 install_plan_.write_verity &&
474 (partition.hash_tree_size > 0 || partition.fec_size > 0);
475}
476
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700477void FilesystemVerifierAction::FinishPartitionHashing() {
478 if (!hasher_->Finalize()) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700479 LOG(ERROR) << "Unable to finalize the hash.";
Sen Jiang57f91802017-11-14 17:42:13 -0800480 Cleanup(ErrorCode::kError);
481 return;
Alex Deymob9e8e262015-08-03 20:23:03 -0700482 }
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800483 const InstallPlan::Partition& partition =
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700484 install_plan_.partitions[partition_index_];
Sen Jiang2703ef42017-03-16 13:36:21 -0700485 LOG(INFO) << "Hash of " << partition.name << ": "
Kelvin Zhang3fe49642021-10-04 15:35:02 -0700486 << HexEncode(hasher_->raw_hash());
Alex Deymob9e8e262015-08-03 20:23:03 -0700487
Sen Jiangfef85fd2016-03-25 15:32:49 -0700488 switch (verifier_step_) {
489 case VerifierStep::kVerifyTargetHash:
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700490 if (partition.target_hash != hasher_->raw_hash()) {
491 LOG(ERROR) << "New '" << partition.name
492 << "' partition verification failed.";
Sen Jiangcdd52062017-05-18 15:33:10 -0700493 if (partition.source_hash.empty()) {
494 // No need to verify source if it is a full payload.
Sen Jiang57f91802017-11-14 17:42:13 -0800495 Cleanup(ErrorCode::kNewRootfsVerificationError);
496 return;
Sen Jiangcdd52062017-05-18 15:33:10 -0700497 }
Sen Jiangfef85fd2016-03-25 15:32:49 -0700498 // If we have not verified source partition yet, now that the target
Sen Jiang65566a32016-04-06 13:35:36 -0700499 // partition does not match, and it's not a full payload, we need to
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400500 // switch to kVerifySourceHash step to check if it's because the
501 // source partition does not match either.
Sen Jiangfef85fd2016-03-25 15:32:49 -0700502 verifier_step_ = VerifierStep::kVerifySourceHash;
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800503 } else {
504 partition_index_++;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700505 }
506 break;
Sen Jiangfef85fd2016-03-25 15:32:49 -0700507 case VerifierStep::kVerifySourceHash:
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800508 if (partition.source_hash != hasher_->raw_hash()) {
509 LOG(ERROR) << "Old '" << partition.name
510 << "' partition verification failed.";
Sen Jiangfef85fd2016-03-25 15:32:49 -0700511 LOG(ERROR) << "This is a server-side error due to mismatched delta"
512 << " update image!";
513 LOG(ERROR) << "The delta I've been given contains a " << partition.name
514 << " delta update that must be applied over a "
515 << partition.name << " with a specific checksum, but the "
516 << partition.name
517 << " we're starting with doesn't have that checksum! This"
518 " means that the delta I've been given doesn't match my"
519 " existing system. The "
520 << partition.name << " partition I have has hash: "
Sen Jiang2703ef42017-03-16 13:36:21 -0700521 << Base64Encode(hasher_->raw_hash())
Sen Jiangfef85fd2016-03-25 15:32:49 -0700522 << " but the update expected me to have "
Sen Jiang2703ef42017-03-16 13:36:21 -0700523 << Base64Encode(partition.source_hash) << " .";
Sen Jiangfef85fd2016-03-25 15:32:49 -0700524 LOG(INFO) << "To get the checksum of the " << partition.name
525 << " partition run this command: dd if="
526 << partition.source_path
527 << " bs=1M count=" << partition.source_size
528 << " iflag=count_bytes 2>/dev/null | openssl dgst -sha256 "
529 "-binary | openssl base64";
530 LOG(INFO) << "To get the checksum of partitions in a bin file, "
531 << "run: .../src/scripts/sha256_partitions.sh .../file.bin";
Sen Jiang57f91802017-11-14 17:42:13 -0800532 Cleanup(ErrorCode::kDownloadStateInitializationError);
533 return;
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800534 }
Sen Jianga35896c2016-05-25 11:08:41 -0700535 // The action will skip kVerifySourceHash step if target partition hash
536 // matches, if we are in this step, it means target hash does not match,
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400537 // and now that the source partition hash matches, we should set the
538 // error code to reflect the error in target partition. We only need to
539 // verify the source partition which the target hash does not match, the
540 // rest of the partitions don't matter.
Sen Jiang57f91802017-11-14 17:42:13 -0800541 Cleanup(ErrorCode::kNewRootfsVerificationError);
542 return;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700543 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700544 // Start hashing the next partition, if any.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700545 buffer_.clear();
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500546 if (partition_fd_) {
Kelvin Zhang8704c832021-05-10 17:53:14 -0400547 partition_fd_->Close();
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500548 partition_fd_.reset();
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400549 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700550 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700551}
552
553} // namespace chromeos_update_engine