Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 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 Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 16 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/payload_consumer/filesystem_verifier_action.h" |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 18 | |
| 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <sys/types.h> |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 23 | #include <unistd.h> |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 24 | |
| 25 | #include <algorithm> |
| 26 | #include <cstdlib> |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 27 | #include <memory> |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 28 | #include <string> |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 29 | #include <utility> |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 30 | |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 31 | #include <base/bind.h> |
Tianjie | 24f9609 | 2020-06-30 12:26:25 -0700 | [diff] [blame] | 32 | #include <base/strings/string_util.h> |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 33 | #include <brillo/data_encoding.h> |
| 34 | #include <brillo/message_loops/message_loop.h> |
| 35 | #include <brillo/streams/file_stream.h> |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 36 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 37 | #include "update_engine/common/utils.h" |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 38 | #include "update_engine/payload_consumer/file_descriptor.h" |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 39 | |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 40 | using brillo::data_encoding::Base64Encode; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 41 | using std::string; |
| 42 | |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 43 | // On a partition with verity enabled, we expect to see the following format: |
| 44 | // =================================================== |
| 45 | // Normal Filesystem Data |
| 46 | // (this should take most of the space, like over 90%) |
| 47 | // =================================================== |
| 48 | // Hash tree |
| 49 | // ~0.8% (e.g. 16M for 2GB image) |
| 50 | // =================================================== |
| 51 | // FEC data |
| 52 | // ~0.8% |
| 53 | // =================================================== |
| 54 | // Footer |
| 55 | // 4K |
| 56 | // =================================================== |
| 57 | |
| 58 | // For OTA that doesn't do on device verity computation, hash tree and fec data |
| 59 | // are written during DownloadAction as a regular InstallOp, so no special |
| 60 | // handling needed, we can just read the entire partition in 1 go. |
| 61 | |
| 62 | // Verity enabled case: Only Normal FS data is written during download action. |
| 63 | // When hasing the entire partition, we will need to build the hash tree, write |
| 64 | // it to disk, then build FEC, and write it to disk. Therefore, it is important |
| 65 | // that we finish writing hash tree before we attempt to read & hash it. The |
| 66 | // same principal applies to FEC data. |
| 67 | |
| 68 | // |verity_writer_| handles building and |
| 69 | // writing of FEC/HashTree, we just need to be careful when reading. |
| 70 | // Specifically, we must stop at beginning of Hash tree, let |verity_writer_| |
| 71 | // write both hash tree and FEC, then continue reading the remaining part of |
| 72 | // partition. |
| 73 | |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 74 | namespace chromeos_update_engine { |
| 75 | |
| 76 | namespace { |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 77 | const off_t kReadFileBufferSize = 128 * 1024; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 78 | } // namespace |
| 79 | |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 80 | void FilesystemVerifierAction::PerformAction() { |
| 81 | // Will tell the ActionProcessor we've failed if we return. |
| 82 | ScopedActionCompleter abort_action_completer(processor_, this); |
| 83 | |
| 84 | if (!HasInputObject()) { |
| 85 | LOG(ERROR) << "FilesystemVerifierAction missing input object."; |
| 86 | return; |
| 87 | } |
| 88 | install_plan_ = GetInputObject(); |
| 89 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 90 | if (install_plan_.partitions.empty()) { |
| 91 | LOG(INFO) << "No partitions to verify."; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 92 | if (HasOutputPipe()) |
| 93 | SetOutputObject(install_plan_); |
| 94 | abort_action_completer.set_code(ErrorCode::kSuccess); |
| 95 | return; |
| 96 | } |
Jae Hoon Kim | 50504d6 | 2020-04-23 14:32:38 -0700 | [diff] [blame] | 97 | install_plan_.Dump(); |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 98 | StartPartitionHashing(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 99 | abort_action_completer.set_should_complete(false); |
| 100 | } |
| 101 | |
| 102 | void FilesystemVerifierAction::TerminateProcessing() { |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 103 | brillo::MessageLoop::current()->CancelTask(pending_task_id_); |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 104 | cancelled_ = true; |
| 105 | Cleanup(ErrorCode::kSuccess); // error code is ignored if canceled_ is true. |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 108 | void FilesystemVerifierAction::Cleanup(ErrorCode code) { |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 109 | read_fd_.reset(); |
| 110 | write_fd_.reset(); |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 111 | // This memory is not used anymore. |
| 112 | buffer_.clear(); |
| 113 | |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 114 | if (cancelled_) |
| 115 | return; |
| 116 | if (code == ErrorCode::kSuccess && HasOutputPipe()) |
| 117 | SetOutputObject(install_plan_); |
Kelvin Zhang | 70eef23 | 2020-06-12 20:32:40 +0000 | [diff] [blame] | 118 | UpdateProgress(1.0); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 119 | processor_->ActionComplete(this, code); |
| 120 | } |
| 121 | |
Kelvin Zhang | 70eef23 | 2020-06-12 20:32:40 +0000 | [diff] [blame] | 122 | void FilesystemVerifierAction::UpdateProgress(double progress) { |
| 123 | if (delegate_ != nullptr) { |
| 124 | delegate_->OnVerifyProgressUpdate(progress); |
| 125 | } |
| 126 | } |
| 127 | |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 128 | bool FilesystemVerifierAction::InitializeFdVABC() { |
| 129 | const InstallPlan::Partition& partition = |
| 130 | install_plan_.partitions[partition_index_]; |
| 131 | |
| 132 | read_fd_ = dynamic_control_->OpenCowReader( |
| 133 | partition.name, partition.source_path, true); |
| 134 | if (!read_fd_) { |
| 135 | LOG(ERROR) << "OpenCowReader(" << partition.name << ", " |
| 136 | << partition.source_path << ") failed."; |
| 137 | return false; |
| 138 | } |
| 139 | partition_size_ = partition.target_size; |
| 140 | // TODO(b/173432386): Support Verity writes for VABC. |
| 141 | CHECK_EQ(partition.fec_size, 0U); |
| 142 | CHECK_EQ(partition.hash_tree_size, 0U); |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | bool FilesystemVerifierAction::InitializeFd(const std::string& part_path) { |
| 147 | read_fd_ = FileDescriptorPtr(new EintrSafeFileDescriptor()); |
| 148 | if (!read_fd_->Open(part_path.c_str(), O_RDONLY)) { |
| 149 | LOG(ERROR) << "Unable to open " << part_path << " for reading."; |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | // Can't re-use |read_fd_|, as verity writer may call `seek` to modify state |
| 154 | // of a file descriptor. |
| 155 | if (ShouldWriteVerity()) { |
| 156 | write_fd_ = FileDescriptorPtr(new EintrSafeFileDescriptor()); |
| 157 | if (!write_fd_->Open(part_path.c_str(), O_RDWR)) { |
| 158 | LOG(ERROR) << "Unable to open " << part_path << " for Read/Write."; |
| 159 | return false; |
| 160 | } |
| 161 | } |
| 162 | return true; |
| 163 | } |
| 164 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 165 | void FilesystemVerifierAction::StartPartitionHashing() { |
| 166 | if (partition_index_ == install_plan_.partitions.size()) { |
Tianjie | 24f9609 | 2020-06-30 12:26:25 -0700 | [diff] [blame] | 167 | if (!install_plan_.untouched_dynamic_partitions.empty()) { |
| 168 | LOG(INFO) << "Verifying extents of untouched dynamic partitions [" |
| 169 | << base::JoinString(install_plan_.untouched_dynamic_partitions, |
| 170 | ", ") |
| 171 | << "]"; |
| 172 | if (!dynamic_control_->VerifyExtentsForUntouchedPartitions( |
| 173 | install_plan_.source_slot, |
| 174 | install_plan_.target_slot, |
| 175 | install_plan_.untouched_dynamic_partitions)) { |
| 176 | Cleanup(ErrorCode::kFilesystemVerifierError); |
| 177 | return; |
| 178 | } |
| 179 | } |
| 180 | |
Sen Jiang | a35896c | 2016-05-25 11:08:41 -0700 | [diff] [blame] | 181 | Cleanup(ErrorCode::kSuccess); |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 182 | return; |
| 183 | } |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 184 | const InstallPlan::Partition& partition = |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 185 | install_plan_.partitions[partition_index_]; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 186 | string part_path; |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 187 | switch (verifier_step_) { |
| 188 | case VerifierStep::kVerifySourceHash: |
Sen Jiang | e6e4bb9 | 2016-04-05 14:59:12 -0700 | [diff] [blame] | 189 | part_path = partition.source_path; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 190 | partition_size_ = partition.source_size; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 191 | break; |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 192 | case VerifierStep::kVerifyTargetHash: |
Sen Jiang | e6e4bb9 | 2016-04-05 14:59:12 -0700 | [diff] [blame] | 193 | part_path = partition.target_path; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 194 | partition_size_ = partition.target_size; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 195 | break; |
| 196 | } |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 197 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 198 | LOG(INFO) << "Hashing partition " << partition_index_ << " (" |
| 199 | << partition.name << ") on device " << part_path; |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 200 | auto success = false; |
Kelvin Zhang | 0618835 | 2021-02-10 13:21:47 -0500 | [diff] [blame] | 201 | if (dynamic_control_->UpdateUsesSnapshotCompression() && |
Kelvin Zhang | ebd115e | 2021-03-08 16:10:25 -0500 | [diff] [blame] | 202 | verifier_step_ == VerifierStep::kVerifyTargetHash && |
| 203 | dynamic_control_->IsDynamicPartition(partition.name, |
| 204 | install_plan_.target_slot)) { |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 205 | success = InitializeFdVABC(); |
| 206 | } else { |
| 207 | if (part_path.empty()) { |
| 208 | if (partition_size_ == 0) { |
| 209 | LOG(INFO) << "Skip hashing partition " << partition_index_ << " (" |
| 210 | << partition.name << ") because size is 0."; |
| 211 | partition_index_++; |
| 212 | StartPartitionHashing(); |
| 213 | return; |
| 214 | } |
| 215 | LOG(ERROR) << "Cannot hash partition " << partition_index_ << " (" |
| 216 | << partition.name |
| 217 | << ") because its device path cannot be determined."; |
| 218 | Cleanup(ErrorCode::kFilesystemVerifierError); |
| 219 | return; |
| 220 | } |
| 221 | success = InitializeFd(part_path); |
| 222 | } |
| 223 | if (!success) { |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 224 | Cleanup(ErrorCode::kFilesystemVerifierError); |
| 225 | return; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 226 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 227 | buffer_.resize(kReadFileBufferSize); |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 228 | hasher_ = std::make_unique<HashCalculator>(); |
| 229 | |
| 230 | offset_ = 0; |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 231 | filesystem_data_end_ = partition_size_; |
| 232 | CHECK_LE(partition.hash_tree_offset, partition.fec_offset) |
| 233 | << " Hash tree is expected to come before FEC data"; |
| 234 | if (partition.hash_tree_offset != 0) { |
| 235 | filesystem_data_end_ = partition.hash_tree_offset; |
| 236 | } else if (partition.fec_offset != 0) { |
| 237 | filesystem_data_end_ = partition.fec_offset; |
| 238 | } |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 239 | if (ShouldWriteVerity()) { |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 240 | if (!verity_writer_->Init(partition)) { |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 241 | Cleanup(ErrorCode::kVerityCalculationError); |
| 242 | return; |
| 243 | } |
| 244 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 245 | |
| 246 | // Start the first read. |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 247 | ScheduleFileSystemRead(); |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 250 | bool FilesystemVerifierAction::ShouldWriteVerity() { |
| 251 | const InstallPlan::Partition& partition = |
| 252 | install_plan_.partitions[partition_index_]; |
| 253 | return verifier_step_ == VerifierStep::kVerifyTargetHash && |
| 254 | install_plan_.write_verity && |
| 255 | (partition.hash_tree_size > 0 || partition.fec_size > 0); |
| 256 | } |
| 257 | |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 258 | void FilesystemVerifierAction::ReadVerityAndFooter() { |
| 259 | if (ShouldWriteVerity()) { |
| 260 | if (!verity_writer_->Finalize(read_fd_, write_fd_)) { |
| 261 | LOG(ERROR) << "Failed to write hashtree/FEC data."; |
| 262 | Cleanup(ErrorCode::kFilesystemVerifierError); |
| 263 | return; |
| 264 | } |
| 265 | } |
| 266 | auto bytes_to_read = partition_size_ - filesystem_data_end_; |
| 267 | // Since we handed our |read_fd_| to verity_writer_ during |Finalize()| call, |
| 268 | // fd's position could have been changed. Re-seek. |
| 269 | read_fd_->Seek(filesystem_data_end_, SEEK_SET); |
| 270 | while (bytes_to_read > 0) { |
| 271 | const auto read_size = std::min<size_t>(buffer_.size(), bytes_to_read); |
| 272 | auto bytes_read = read_fd_->Read(buffer_.data(), read_size); |
| 273 | if (bytes_read <= 0) { |
| 274 | PLOG(ERROR) << "Failed to read hash tree " << bytes_read; |
| 275 | Cleanup(ErrorCode::kFilesystemVerifierError); |
| 276 | return; |
| 277 | } |
| 278 | if (!hasher_->Update(buffer_.data(), bytes_read)) { |
| 279 | LOG(ERROR) << "Unable to update the hash."; |
| 280 | Cleanup(ErrorCode::kError); |
| 281 | return; |
| 282 | } |
| 283 | bytes_to_read -= bytes_read; |
| 284 | } |
| 285 | FinishPartitionHashing(); |
| 286 | } |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 287 | |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 288 | void FilesystemVerifierAction::ScheduleFileSystemRead() { |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 289 | // We can only start reading anything past |hash_tree_offset| after we have |
| 290 | // already read all the data blocks that the hash tree covers. The same |
| 291 | // applies to FEC. |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 292 | |
| 293 | size_t bytes_to_read = std::min(static_cast<uint64_t>(buffer_.size()), |
| 294 | filesystem_data_end_ - offset_); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 295 | if (!bytes_to_read) { |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 296 | ReadVerityAndFooter(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 297 | return; |
| 298 | } |
| 299 | |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 300 | auto bytes_read = read_fd_->Read(buffer_.data(), bytes_to_read); |
| 301 | if (bytes_read < 0) { |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 302 | LOG(ERROR) << "Unable to schedule an asynchronous read from the stream."; |
| 303 | Cleanup(ErrorCode::kError); |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 304 | } else { |
| 305 | // We could just invoke |OnReadDoneCallback()|, it works. But |PostTask| |
| 306 | // is used so that users can cancel updates. |
| 307 | pending_task_id_ = brillo::MessageLoop::current()->PostTask( |
| 308 | base::Bind(&FilesystemVerifierAction::OnReadDone, |
| 309 | base::Unretained(this), |
| 310 | bytes_read)); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 314 | void FilesystemVerifierAction::OnReadDone(size_t bytes_read) { |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 315 | if (cancelled_) { |
| 316 | Cleanup(ErrorCode::kError); |
| 317 | return; |
| 318 | } |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 319 | if (bytes_read == 0) { |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 320 | LOG(ERROR) << "Failed to read the remaining " << partition_size_ - offset_ |
| 321 | << " bytes from partition " |
| 322 | << install_plan_.partitions[partition_index_].name; |
| 323 | Cleanup(ErrorCode::kFilesystemVerifierError); |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | if (!hasher_->Update(buffer_.data(), bytes_read)) { |
| 328 | LOG(ERROR) << "Unable to update the hash."; |
| 329 | Cleanup(ErrorCode::kError); |
| 330 | return; |
| 331 | } |
| 332 | |
Kelvin Zhang | 70eef23 | 2020-06-12 20:32:40 +0000 | [diff] [blame] | 333 | // WE don't consider sizes of each partition. Every partition |
| 334 | // has the same length on progress bar. |
| 335 | // TODO(zhangkelvin) Take sizes of each partition into account |
| 336 | |
| 337 | UpdateProgress( |
| 338 | (static_cast<double>(offset_) / partition_size_ + partition_index_) / |
| 339 | install_plan_.partitions.size()); |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 340 | if (ShouldWriteVerity()) { |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 341 | if (!verity_writer_->Update(offset_, buffer_.data(), bytes_read)) { |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 342 | LOG(ERROR) << "Unable to update verity"; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 343 | Cleanup(ErrorCode::kVerityCalculationError); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 344 | return; |
| 345 | } |
| 346 | } |
| 347 | |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 348 | offset_ += bytes_read; |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 349 | if (offset_ == filesystem_data_end_) { |
| 350 | ReadVerityAndFooter(); |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 351 | return; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 352 | } |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 353 | |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 354 | ScheduleFileSystemRead(); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 357 | void FilesystemVerifierAction::FinishPartitionHashing() { |
| 358 | if (!hasher_->Finalize()) { |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 359 | LOG(ERROR) << "Unable to finalize the hash."; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 360 | Cleanup(ErrorCode::kError); |
| 361 | return; |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 362 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 363 | InstallPlan::Partition& partition = |
| 364 | install_plan_.partitions[partition_index_]; |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 365 | LOG(INFO) << "Hash of " << partition.name << ": " |
| 366 | << Base64Encode(hasher_->raw_hash()); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 367 | |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 368 | switch (verifier_step_) { |
| 369 | case VerifierStep::kVerifyTargetHash: |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 370 | if (partition.target_hash != hasher_->raw_hash()) { |
| 371 | LOG(ERROR) << "New '" << partition.name |
| 372 | << "' partition verification failed."; |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 373 | if (partition.source_hash.empty()) { |
| 374 | // No need to verify source if it is a full payload. |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 375 | Cleanup(ErrorCode::kNewRootfsVerificationError); |
| 376 | return; |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 377 | } |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 378 | // If we have not verified source partition yet, now that the target |
Sen Jiang | 65566a3 | 2016-04-06 13:35:36 -0700 | [diff] [blame] | 379 | // partition does not match, and it's not a full payload, we need to |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 380 | // switch to kVerifySourceHash step to check if it's because the |
| 381 | // source partition does not match either. |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 382 | verifier_step_ = VerifierStep::kVerifySourceHash; |
Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame] | 383 | } else { |
| 384 | partition_index_++; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 385 | } |
| 386 | break; |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 387 | case VerifierStep::kVerifySourceHash: |
Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame] | 388 | if (partition.source_hash != hasher_->raw_hash()) { |
| 389 | LOG(ERROR) << "Old '" << partition.name |
| 390 | << "' partition verification failed."; |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 391 | LOG(ERROR) << "This is a server-side error due to mismatched delta" |
| 392 | << " update image!"; |
| 393 | LOG(ERROR) << "The delta I've been given contains a " << partition.name |
| 394 | << " delta update that must be applied over a " |
| 395 | << partition.name << " with a specific checksum, but the " |
| 396 | << partition.name |
| 397 | << " we're starting with doesn't have that checksum! This" |
| 398 | " means that the delta I've been given doesn't match my" |
| 399 | " existing system. The " |
| 400 | << partition.name << " partition I have has hash: " |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 401 | << Base64Encode(hasher_->raw_hash()) |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 402 | << " but the update expected me to have " |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 403 | << Base64Encode(partition.source_hash) << " ."; |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 404 | LOG(INFO) << "To get the checksum of the " << partition.name |
| 405 | << " partition run this command: dd if=" |
| 406 | << partition.source_path |
| 407 | << " bs=1M count=" << partition.source_size |
| 408 | << " iflag=count_bytes 2>/dev/null | openssl dgst -sha256 " |
| 409 | "-binary | openssl base64"; |
| 410 | LOG(INFO) << "To get the checksum of partitions in a bin file, " |
| 411 | << "run: .../src/scripts/sha256_partitions.sh .../file.bin"; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 412 | Cleanup(ErrorCode::kDownloadStateInitializationError); |
| 413 | return; |
Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame] | 414 | } |
Sen Jiang | a35896c | 2016-05-25 11:08:41 -0700 | [diff] [blame] | 415 | // The action will skip kVerifySourceHash step if target partition hash |
| 416 | // matches, if we are in this step, it means target hash does not match, |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 417 | // and now that the source partition hash matches, we should set the |
| 418 | // error code to reflect the error in target partition. We only need to |
| 419 | // verify the source partition which the target hash does not match, the |
| 420 | // rest of the partitions don't matter. |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 421 | Cleanup(ErrorCode::kNewRootfsVerificationError); |
| 422 | return; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 423 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 424 | // Start hashing the next partition, if any. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 425 | hasher_.reset(); |
| 426 | buffer_.clear(); |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 427 | if (read_fd_) { |
| 428 | read_fd_.reset(); |
| 429 | } |
| 430 | if (write_fd_) { |
| 431 | write_fd_.reset(); |
| 432 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 433 | StartPartitionHashing(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | } // namespace chromeos_update_engine |