blob: 8c21673ab1e7fb74eed6ec65a9243bffdc7290b7 [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>
Kelvin Zhangec205cf2020-09-28 13:23:40 -040034#include <brillo/data_encoding.h>
35#include <brillo/message_loops/message_loop.h>
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -050036#include <brillo/secure_blob.h>
Kelvin Zhangec205cf2020-09-28 13:23:40 -040037#include <brillo/streams/file_stream.h>
Allie Woodeb9e6d82015-04-17 13:55:30 -070038
Kelvin Zhangab15beb2022-12-05 10:19:52 -080039#include "update_engine/common/error_code.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080040#include "update_engine/common/utils.h"
Kelvin Zhangec205cf2020-09-28 13:23:40 -040041#include "update_engine/payload_consumer/file_descriptor.h"
Kelvin Zhangab15beb2022-12-05 10:19:52 -080042#include "update_engine/payload_consumer/install_plan.h"
Allie Woodeb9e6d82015-04-17 13:55:30 -070043
Sen Jiang2703ef42017-03-16 13:36:21 -070044using brillo::data_encoding::Base64Encode;
Allie Woodeb9e6d82015-04-17 13:55:30 -070045using std::string;
46
Kelvin Zhang7f925672021-03-15 13:37:40 -040047// On a partition with verity enabled, we expect to see the following format:
48// ===================================================
49// Normal Filesystem Data
50// (this should take most of the space, like over 90%)
51// ===================================================
52// Hash tree
53// ~0.8% (e.g. 16M for 2GB image)
54// ===================================================
55// FEC data
56// ~0.8%
57// ===================================================
58// Footer
59// 4K
60// ===================================================
61
62// For OTA that doesn't do on device verity computation, hash tree and fec data
63// are written during DownloadAction as a regular InstallOp, so no special
64// handling needed, we can just read the entire partition in 1 go.
65
66// Verity enabled case: Only Normal FS data is written during download action.
67// When hasing the entire partition, we will need to build the hash tree, write
68// it to disk, then build FEC, and write it to disk. Therefore, it is important
69// that we finish writing hash tree before we attempt to read & hash it. The
70// same principal applies to FEC data.
71
72// |verity_writer_| handles building and
73// writing of FEC/HashTree, we just need to be careful when reading.
74// Specifically, we must stop at beginning of Hash tree, let |verity_writer_|
75// write both hash tree and FEC, then continue reading the remaining part of
76// partition.
77
Allie Woodeb9e6d82015-04-17 13:55:30 -070078namespace chromeos_update_engine {
79
80namespace {
Alex Deymo20c99202015-07-09 16:14:16 -070081const off_t kReadFileBufferSize = 128 * 1024;
Daniel Zheng3efb3f22022-12-19 22:14:20 +000082constexpr float kVerityProgressPercent = 0.3;
83constexpr float kEncodeFECPercent = 0.3;
84
Allie Woodeb9e6d82015-04-17 13:55:30 -070085} // namespace
86
Allie Woodeb9e6d82015-04-17 13:55:30 -070087void FilesystemVerifierAction::PerformAction() {
88 // Will tell the ActionProcessor we've failed if we return.
89 ScopedActionCompleter abort_action_completer(processor_, this);
90
91 if (!HasInputObject()) {
92 LOG(ERROR) << "FilesystemVerifierAction missing input object.";
93 return;
94 }
95 install_plan_ = GetInputObject();
96
Alex Deymoe5e5fe92015-10-05 09:28:19 -070097 if (install_plan_.partitions.empty()) {
Kelvin Zhang430852d2023-05-25 17:00:24 -070098 LOG(ERROR) << "No partitions to verify.";
Allie Woodeb9e6d82015-04-17 13:55:30 -070099 if (HasOutputPipe())
100 SetOutputObject(install_plan_);
Kelvin Zhang430852d2023-05-25 17:00:24 -0700101 abort_action_completer.set_code(ErrorCode::kFilesystemVerifierError);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700102 return;
103 }
Kelvin Zhang5b145822022-07-26 10:56:09 -0700104 // partition_weight_[i] = total size of partitions before index i.
105 partition_weight_.clear();
106 partition_weight_.reserve(install_plan_.partitions.size() + 1);
107 partition_weight_.push_back(0);
108 for (const auto& part : install_plan_.partitions) {
109 partition_weight_.push_back(part.target_size);
110 }
111 std::partial_sum(partition_weight_.begin(),
112 partition_weight_.end(),
113 partition_weight_.begin(),
liuqiangf1a360b2024-09-25 17:44:58 +0800114 std::plus<uint64_t>());
Kelvin Zhang5b145822022-07-26 10:56:09 -0700115
Jae Hoon Kim50504d62020-04-23 14:32:38 -0700116 install_plan_.Dump();
Kelvin Zhangab15beb2022-12-05 10:19:52 -0800117 // If we are not writing verity, just map all partitions once at the
118 // beginning.
119 // No need to re-map for each partition, because we are not writing any new
120 // COW data.
121 if (dynamic_control_->UpdateUsesSnapshotCompression() &&
122 !install_plan_.write_verity) {
123 dynamic_control_->MapAllPartitions();
124 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700125 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700126 abort_action_completer.set_should_complete(false);
127}
128
129void FilesystemVerifierAction::TerminateProcessing() {
Alex Deymo20c99202015-07-09 16:14:16 -0700130 cancelled_ = true;
131 Cleanup(ErrorCode::kSuccess); // error code is ignored if canceled_ is true.
Allie Woodeb9e6d82015-04-17 13:55:30 -0700132}
133
Allie Woodeb9e6d82015-04-17 13:55:30 -0700134void FilesystemVerifierAction::Cleanup(ErrorCode code) {
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500135 partition_fd_.reset();
Alex Deymo20c99202015-07-09 16:14:16 -0700136 // This memory is not used anymore.
137 buffer_.clear();
138
Kelvin Zhang9105f4b2021-04-26 13:44:49 -0400139 // If we didn't write verity, partitions were maped. Releaase resource now.
140 if (!install_plan_.write_verity &&
141 dynamic_control_->UpdateUsesSnapshotCompression()) {
142 LOG(INFO) << "Not writing verity and VABC is enabled, unmapping all "
143 "partitions";
144 dynamic_control_->UnmapAllPartitions();
145 }
146
Allie Woodeb9e6d82015-04-17 13:55:30 -0700147 if (cancelled_)
148 return;
149 if (code == ErrorCode::kSuccess && HasOutputPipe())
150 SetOutputObject(install_plan_);
Kelvin Zhang70eef232020-06-12 20:32:40 +0000151 UpdateProgress(1.0);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700152 processor_->ActionComplete(this, code);
153}
154
Kelvin Zhang70eef232020-06-12 20:32:40 +0000155void FilesystemVerifierAction::UpdateProgress(double progress) {
156 if (delegate_ != nullptr) {
157 delegate_->OnVerifyProgressUpdate(progress);
158 }
159}
160
Kelvin Zhang8704c832021-05-10 17:53:14 -0400161void FilesystemVerifierAction::UpdatePartitionProgress(double progress) {
Kelvin Zhang5b145822022-07-26 10:56:09 -0700162 UpdateProgress((partition_weight_[partition_index_] * (1 - progress) +
163 partition_weight_[partition_index_ + 1] * progress) /
164 partition_weight_.back());
Kelvin Zhang8704c832021-05-10 17:53:14 -0400165}
166
167bool FilesystemVerifierAction::InitializeFdVABC(bool should_write_verity) {
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400168 const InstallPlan::Partition& partition =
169 install_plan_.partitions[partition_index_];
170
Kelvin Zhang8704c832021-05-10 17:53:14 -0400171 if (!should_write_verity) {
172 // In VABC, we cannot map/unmap partitions w/o first closing ALL fds first.
173 // Since this function might be called inside a ScheduledTask, the closure
174 // might have a copy of partition_fd_ when executing this function. Which
175 // means even if we do |partition_fd_.reset()| here, there's a chance that
176 // underlying fd isn't closed until we return. This is unacceptable, we need
177 // to close |partition_fd| right away.
178 if (partition_fd_) {
179 partition_fd_->Close();
180 partition_fd_.reset();
181 }
Kelvin Zhang9105f4b2021-04-26 13:44:49 -0400182 // In VABC, if we are not writing verity, just map all partitions,
183 // and read using regular fd on |postinstall_mount_device| .
184 // All read will go through snapuserd, which provides a consistent
185 // view: device will use snapuserd to read partition during boot.
186 // b/186196758
187 // Call UnmapAllPartitions() first, because if we wrote verity before, these
188 // writes won't be visible to previously opened snapuserd daemon. To ensure
189 // that we will see the most up to date data from partitions, call Unmap()
190 // then Map() to re-spin daemon.
Kelvin Zhangab15beb2022-12-05 10:19:52 -0800191 if (install_plan_.write_verity) {
192 dynamic_control_->UnmapAllPartitions();
193 dynamic_control_->MapAllPartitions();
194 }
Kelvin Zhang9105f4b2021-04-26 13:44:49 -0400195 return InitializeFd(partition.readonly_target_path);
196 }
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500197 partition_fd_ =
Kelvin Zhang21a49912021-03-12 14:28:33 -0500198 dynamic_control_->OpenCowFd(partition.name, partition.source_path, true);
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500199 if (!partition_fd_) {
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400200 LOG(ERROR) << "OpenCowReader(" << partition.name << ", "
201 << partition.source_path << ") failed.";
202 return false;
203 }
204 partition_size_ = partition.target_size;
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400205 return true;
206}
207
208bool FilesystemVerifierAction::InitializeFd(const std::string& part_path) {
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800209 partition_fd_ = std::make_unique<EintrSafeFileDescriptor>();
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500210 const bool write_verity = ShouldWriteVerity();
211 int flags = write_verity ? O_RDWR : O_RDONLY;
212 if (!utils::SetBlockDeviceReadOnly(part_path, !write_verity)) {
213 LOG(WARNING) << "Failed to set block device " << part_path << " as "
214 << (write_verity ? "writable" : "readonly");
215 }
216 if (!partition_fd_->Open(part_path.c_str(), flags)) {
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400217 LOG(ERROR) << "Unable to open " << part_path << " for reading.";
218 return false;
219 }
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400220 return true;
221}
222
Daniel Zhengbd16b012022-09-28 23:40:05 +0000223void FilesystemVerifierAction::WriteVerityData(FileDescriptor* fd,
224 void* buffer,
225 const size_t buffer_size) {
226 if (verity_writer_->FECFinished()) {
227 LOG(INFO) << "EncodeFEC is completed. Resuming other tasks";
228 if (dynamic_control_->UpdateUsesSnapshotCompression()) {
229 // Spin up snapuserd to read fs.
230 if (!InitializeFdVABC(false)) {
231 LOG(ERROR) << "Failed to map all partitions";
232 Cleanup(ErrorCode::kFilesystemVerifierError);
233 return;
234 }
235 }
236 HashPartition(0, partition_size_, buffer, buffer_size);
237 return;
238 }
239 if (!verity_writer_->IncrementalFinalize(fd, fd)) {
240 LOG(ERROR) << "Failed to write verity data";
241 Cleanup(ErrorCode::kVerityCalculationError);
242 }
Daniel Zheng3efb3f22022-12-19 22:14:20 +0000243 UpdatePartitionProgress(kVerityProgressPercent +
244 verity_writer_->GetProgress() * kEncodeFECPercent);
Daniel Zhengbd16b012022-09-28 23:40:05 +0000245 CHECK(pending_task_id_.PostTask(
246 FROM_HERE,
247 base::BindOnce(&FilesystemVerifierAction::WriteVerityData,
248 base::Unretained(this),
249 fd,
250 buffer,
251 buffer_size)));
252}
253
Kelvin Zhang8704c832021-05-10 17:53:14 -0400254void FilesystemVerifierAction::WriteVerityAndHashPartition(
Kelvin Zhang8704c832021-05-10 17:53:14 -0400255 const off64_t start_offset,
256 const off64_t end_offset,
257 void* buffer,
258 const size_t buffer_size) {
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800259 auto fd = partition_fd_.get();
260 TEST_AND_RETURN(fd != nullptr);
Kelvin Zhang8704c832021-05-10 17:53:14 -0400261 if (start_offset >= end_offset) {
262 LOG_IF(WARNING, start_offset > end_offset)
263 << "start_offset is greater than end_offset : " << start_offset << " > "
264 << end_offset;
Daniel Zhengbd16b012022-09-28 23:40:05 +0000265 WriteVerityData(fd, buffer, buffer_size);
Kelvin Zhang8704c832021-05-10 17:53:14 -0400266 return;
267 }
268 const auto cur_offset = fd->Seek(start_offset, SEEK_SET);
269 if (cur_offset != start_offset) {
270 PLOG(ERROR) << "Failed to seek to offset: " << start_offset;
271 Cleanup(ErrorCode::kVerityCalculationError);
272 return;
273 }
274 const auto read_size =
Daniel Zhengda3a49b2024-08-16 13:23:28 -0700275 std::min<uint64_t>(buffer_size, end_offset - start_offset);
Kelvin Zhang8704c832021-05-10 17:53:14 -0400276 const auto bytes_read = fd->Read(buffer, read_size);
277 if (bytes_read < 0 || static_cast<size_t>(bytes_read) != read_size) {
278 PLOG(ERROR) << "Failed to read offset " << start_offset << " expected "
279 << read_size << " bytes, actual: " << bytes_read;
280 Cleanup(ErrorCode::kVerityCalculationError);
281 return;
282 }
283 if (!verity_writer_->Update(
284 start_offset, static_cast<const uint8_t*>(buffer), read_size)) {
285 LOG(ERROR) << "VerityWriter::Update() failed";
286 Cleanup(ErrorCode::kVerityCalculationError);
287 return;
288 }
289 UpdatePartitionProgress((start_offset + bytes_read) * 1.0f / partition_size_ *
Kelvin Zhang1d99ae12021-05-12 13:29:27 -0400290 kVerityProgressPercent);
Kelvin Zhang8704c832021-05-10 17:53:14 -0400291 CHECK(pending_task_id_.PostTask(
292 FROM_HERE,
293 base::BindOnce(&FilesystemVerifierAction::WriteVerityAndHashPartition,
294 base::Unretained(this),
Kelvin Zhang8704c832021-05-10 17:53:14 -0400295 start_offset + bytes_read,
296 end_offset,
297 buffer,
298 buffer_size)));
299}
300
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800301void FilesystemVerifierAction::HashPartition(const off64_t start_offset,
Kelvin Zhang8704c832021-05-10 17:53:14 -0400302 const off64_t end_offset,
303 void* buffer,
304 const size_t buffer_size) {
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800305 auto fd = partition_fd_.get();
306 TEST_AND_RETURN(fd != nullptr);
Kelvin Zhang8704c832021-05-10 17:53:14 -0400307 if (start_offset >= end_offset) {
308 LOG_IF(WARNING, start_offset > end_offset)
309 << "start_offset is greater than end_offset : " << start_offset << " > "
310 << end_offset;
311 FinishPartitionHashing();
312 return;
313 }
314 const auto cur_offset = fd->Seek(start_offset, SEEK_SET);
315 if (cur_offset != start_offset) {
316 PLOG(ERROR) << "Failed to seek to offset: " << start_offset;
317 Cleanup(ErrorCode::kFilesystemVerifierError);
318 return;
319 }
320 const auto read_size =
Daniel Zheng6963a922024-08-21 01:09:28 +0000321 std::min<uint64_t>(buffer_size, end_offset - start_offset);
Kelvin Zhang8704c832021-05-10 17:53:14 -0400322 const auto bytes_read = fd->Read(buffer, read_size);
323 if (bytes_read < 0 || static_cast<size_t>(bytes_read) != read_size) {
324 PLOG(ERROR) << "Failed to read offset " << start_offset << " expected "
325 << read_size << " bytes, actual: " << bytes_read;
326 Cleanup(ErrorCode::kFilesystemVerifierError);
327 return;
328 }
329 if (!hasher_->Update(buffer, read_size)) {
330 LOG(ERROR) << "Hasher updated failed on offset" << start_offset;
331 Cleanup(ErrorCode::kFilesystemVerifierError);
332 return;
333 }
334 const auto progress = (start_offset + bytes_read) * 1.0f / partition_size_;
Kelvin Zhang5b145822022-07-26 10:56:09 -0700335 // If we are writing verity, then the progress bar will be split between
336 // verity writes and partition hashing. Otherwise, the entire progress bar is
337 // dedicated to partition hashing for smooth progress.
338 if (ShouldWriteVerity()) {
Daniel Zheng3efb3f22022-12-19 22:14:20 +0000339 UpdatePartitionProgress(
340 progress * (1 - (kVerityProgressPercent + kEncodeFECPercent)) +
341 kVerityProgressPercent + kEncodeFECPercent);
Kelvin Zhang5b145822022-07-26 10:56:09 -0700342 } else {
343 UpdatePartitionProgress(progress);
344 }
Kelvin Zhang8704c832021-05-10 17:53:14 -0400345 CHECK(pending_task_id_.PostTask(
346 FROM_HERE,
347 base::BindOnce(&FilesystemVerifierAction::HashPartition,
348 base::Unretained(this),
Kelvin Zhang8704c832021-05-10 17:53:14 -0400349 start_offset + bytes_read,
350 end_offset,
351 buffer,
352 buffer_size)));
353}
354
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700355void FilesystemVerifierAction::StartPartitionHashing() {
356 if (partition_index_ == install_plan_.partitions.size()) {
Tianjie24f96092020-06-30 12:26:25 -0700357 if (!install_plan_.untouched_dynamic_partitions.empty()) {
358 LOG(INFO) << "Verifying extents of untouched dynamic partitions ["
Kelvin Zhang0c184242024-10-25 11:19:27 -0700359 << android::base::Join(
360 install_plan_.untouched_dynamic_partitions, ", ")
Tianjie24f96092020-06-30 12:26:25 -0700361 << "]";
362 if (!dynamic_control_->VerifyExtentsForUntouchedPartitions(
363 install_plan_.source_slot,
364 install_plan_.target_slot,
365 install_plan_.untouched_dynamic_partitions)) {
366 Cleanup(ErrorCode::kFilesystemVerifierError);
367 return;
368 }
369 }
370
Sen Jianga35896c2016-05-25 11:08:41 -0700371 Cleanup(ErrorCode::kSuccess);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700372 return;
373 }
Sen Jiang57f91802017-11-14 17:42:13 -0800374 const InstallPlan::Partition& partition =
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700375 install_plan_.partitions[partition_index_];
Kelvin Zhange012f652021-05-10 16:00:31 -0400376 const auto& part_path = GetPartitionPath();
377 partition_size_ = GetPartitionSize();
Yifan Hong537802d2018-08-15 13:15:42 -0700378
Yifan Hong537802d2018-08-15 13:15:42 -0700379 LOG(INFO) << "Hashing partition " << partition_index_ << " ("
380 << partition.name << ") on device " << part_path;
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400381 auto success = false;
Kelvin Zhange012f652021-05-10 16:00:31 -0400382 if (IsVABC(partition)) {
Kelvin Zhang8704c832021-05-10 17:53:14 -0400383 success = InitializeFdVABC(ShouldWriteVerity());
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400384 } else {
385 if (part_path.empty()) {
386 if (partition_size_ == 0) {
387 LOG(INFO) << "Skip hashing partition " << partition_index_ << " ("
388 << partition.name << ") because size is 0.";
389 partition_index_++;
390 StartPartitionHashing();
391 return;
392 }
393 LOG(ERROR) << "Cannot hash partition " << partition_index_ << " ("
394 << partition.name
395 << ") because its device path cannot be determined.";
396 Cleanup(ErrorCode::kFilesystemVerifierError);
397 return;
398 }
399 success = InitializeFd(part_path);
400 }
401 if (!success) {
Sen Jiang57f91802017-11-14 17:42:13 -0800402 Cleanup(ErrorCode::kFilesystemVerifierError);
403 return;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700404 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700405 buffer_.resize(kReadFileBufferSize);
Sen Jiang57f91802017-11-14 17:42:13 -0800406 hasher_ = std::make_unique<HashCalculator>();
407
Kelvin Zhang7f925672021-03-15 13:37:40 -0400408 filesystem_data_end_ = partition_size_;
Kelvin Zhang5e5ad392021-07-26 21:42:06 -0400409 if (partition.fec_offset > 0) {
410 CHECK_LE(partition.hash_tree_offset, partition.fec_offset)
411 << " Hash tree is expected to come before FEC data";
412 }
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800413 CHECK_NE(partition_fd_, nullptr);
Kelvin Zhang7f925672021-03-15 13:37:40 -0400414 if (partition.hash_tree_offset != 0) {
415 filesystem_data_end_ = partition.hash_tree_offset;
416 } else if (partition.fec_offset != 0) {
417 filesystem_data_end_ = partition.fec_offset;
418 }
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400419 if (ShouldWriteVerity()) {
Kelvin Zhang8704c832021-05-10 17:53:14 -0400420 LOG(INFO) << "Verity writes enabled on partition " << partition.name;
Kelvin Zhang7f925672021-03-15 13:37:40 -0400421 if (!verity_writer_->Init(partition)) {
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500422 LOG(INFO) << "Verity writes enabled on partition " << partition.name;
Sen Jiang57f91802017-11-14 17:42:13 -0800423 Cleanup(ErrorCode::kVerityCalculationError);
424 return;
425 }
Kelvin Zhang8704c832021-05-10 17:53:14 -0400426 WriteVerityAndHashPartition(
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800427 0, filesystem_data_end_, buffer_.data(), buffer_.size());
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500428 } else {
429 LOG(INFO) << "Verity writes disabled on partition " << partition.name;
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800430 HashPartition(0, partition_size_, buffer_.data(), buffer_.size());
Sen Jiang57f91802017-11-14 17:42:13 -0800431 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700432}
433
Kelvin Zhange012f652021-05-10 16:00:31 -0400434bool FilesystemVerifierAction::IsVABC(
435 const InstallPlan::Partition& partition) const {
436 return dynamic_control_->UpdateUsesSnapshotCompression() &&
437 verifier_step_ == VerifierStep::kVerifyTargetHash &&
438 dynamic_control_->IsDynamicPartition(partition.name,
439 install_plan_.target_slot);
440}
441
442const std::string& FilesystemVerifierAction::GetPartitionPath() const {
443 const InstallPlan::Partition& partition =
444 install_plan_.partitions[partition_index_];
445 switch (verifier_step_) {
446 case VerifierStep::kVerifySourceHash:
447 return partition.source_path;
448 case VerifierStep::kVerifyTargetHash:
449 if (IsVABC(partition)) {
450 return partition.readonly_target_path;
451 } else {
452 return partition.target_path;
453 }
454 }
455}
456
Daniel Zheng6db5f792024-08-28 18:13:05 +0000457uint64_t FilesystemVerifierAction::GetPartitionSize() const {
Kelvin Zhange012f652021-05-10 16:00:31 -0400458 const InstallPlan::Partition& partition =
459 install_plan_.partitions[partition_index_];
460 switch (verifier_step_) {
461 case VerifierStep::kVerifySourceHash:
462 return partition.source_size;
463 case VerifierStep::kVerifyTargetHash:
464 return partition.target_size;
465 }
466}
467
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400468bool FilesystemVerifierAction::ShouldWriteVerity() {
469 const InstallPlan::Partition& partition =
470 install_plan_.partitions[partition_index_];
471 return verifier_step_ == VerifierStep::kVerifyTargetHash &&
472 install_plan_.write_verity &&
473 (partition.hash_tree_size > 0 || partition.fec_size > 0);
474}
475
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700476void FilesystemVerifierAction::FinishPartitionHashing() {
477 if (!hasher_->Finalize()) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700478 LOG(ERROR) << "Unable to finalize the hash.";
Sen Jiang57f91802017-11-14 17:42:13 -0800479 Cleanup(ErrorCode::kError);
480 return;
Alex Deymob9e8e262015-08-03 20:23:03 -0700481 }
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800482 const InstallPlan::Partition& partition =
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700483 install_plan_.partitions[partition_index_];
Sen Jiang2703ef42017-03-16 13:36:21 -0700484 LOG(INFO) << "Hash of " << partition.name << ": "
Kelvin Zhang3fe49642021-10-04 15:35:02 -0700485 << HexEncode(hasher_->raw_hash());
Alex Deymob9e8e262015-08-03 20:23:03 -0700486
Sen Jiangfef85fd2016-03-25 15:32:49 -0700487 switch (verifier_step_) {
488 case VerifierStep::kVerifyTargetHash:
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700489 if (partition.target_hash != hasher_->raw_hash()) {
490 LOG(ERROR) << "New '" << partition.name
491 << "' partition verification failed.";
Sen Jiangcdd52062017-05-18 15:33:10 -0700492 if (partition.source_hash.empty()) {
493 // No need to verify source if it is a full payload.
Sen Jiang57f91802017-11-14 17:42:13 -0800494 Cleanup(ErrorCode::kNewRootfsVerificationError);
495 return;
Sen Jiangcdd52062017-05-18 15:33:10 -0700496 }
Sen Jiangfef85fd2016-03-25 15:32:49 -0700497 // If we have not verified source partition yet, now that the target
Sen Jiang65566a32016-04-06 13:35:36 -0700498 // partition does not match, and it's not a full payload, we need to
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400499 // switch to kVerifySourceHash step to check if it's because the
500 // source partition does not match either.
Sen Jiangfef85fd2016-03-25 15:32:49 -0700501 verifier_step_ = VerifierStep::kVerifySourceHash;
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800502 } else {
503 partition_index_++;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700504 }
505 break;
Sen Jiangfef85fd2016-03-25 15:32:49 -0700506 case VerifierStep::kVerifySourceHash:
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800507 if (partition.source_hash != hasher_->raw_hash()) {
508 LOG(ERROR) << "Old '" << partition.name
509 << "' partition verification failed.";
Sen Jiangfef85fd2016-03-25 15:32:49 -0700510 LOG(ERROR) << "This is a server-side error due to mismatched delta"
511 << " update image!";
512 LOG(ERROR) << "The delta I've been given contains a " << partition.name
513 << " delta update that must be applied over a "
514 << partition.name << " with a specific checksum, but the "
515 << partition.name
516 << " we're starting with doesn't have that checksum! This"
517 " means that the delta I've been given doesn't match my"
518 " existing system. The "
519 << partition.name << " partition I have has hash: "
Sen Jiang2703ef42017-03-16 13:36:21 -0700520 << Base64Encode(hasher_->raw_hash())
Sen Jiangfef85fd2016-03-25 15:32:49 -0700521 << " but the update expected me to have "
Sen Jiang2703ef42017-03-16 13:36:21 -0700522 << Base64Encode(partition.source_hash) << " .";
Sen Jiangfef85fd2016-03-25 15:32:49 -0700523 LOG(INFO) << "To get the checksum of the " << partition.name
524 << " partition run this command: dd if="
525 << partition.source_path
526 << " bs=1M count=" << partition.source_size
527 << " iflag=count_bytes 2>/dev/null | openssl dgst -sha256 "
528 "-binary | openssl base64";
529 LOG(INFO) << "To get the checksum of partitions in a bin file, "
530 << "run: .../src/scripts/sha256_partitions.sh .../file.bin";
Sen Jiang57f91802017-11-14 17:42:13 -0800531 Cleanup(ErrorCode::kDownloadStateInitializationError);
532 return;
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800533 }
Sen Jianga35896c2016-05-25 11:08:41 -0700534 // The action will skip kVerifySourceHash step if target partition hash
535 // matches, if we are in this step, it means target hash does not match,
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400536 // and now that the source partition hash matches, we should set the
537 // error code to reflect the error in target partition. We only need to
538 // verify the source partition which the target hash does not match, the
539 // rest of the partitions don't matter.
Sen Jiang57f91802017-11-14 17:42:13 -0800540 Cleanup(ErrorCode::kNewRootfsVerificationError);
541 return;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700542 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700543 // Start hashing the next partition, if any.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700544 buffer_.clear();
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500545 if (partition_fd_) {
Kelvin Zhang8704c832021-05-10 17:53:14 -0400546 partition_fd_->Close();
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500547 partition_fd_.reset();
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400548 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700549 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700550}
551
552} // namespace chromeos_update_engine