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> |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 35 | #include <brillo/secure_blob.h> |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 36 | #include <brillo/streams/file_stream.h> |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 37 | |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 38 | #include "payload_generator/delta_diff_generator.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 39 | #include "update_engine/common/utils.h" |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 40 | #include "update_engine/payload_consumer/file_descriptor.h" |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 41 | |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 42 | using brillo::data_encoding::Base64Encode; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 43 | using std::string; |
| 44 | |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 45 | // 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 Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 76 | namespace chromeos_update_engine { |
| 77 | |
| 78 | namespace { |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 79 | const off_t kReadFileBufferSize = 128 * 1024; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 80 | } // namespace |
| 81 | |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 82 | void FilesystemVerifierAction::PerformAction() { |
| 83 | // Will tell the ActionProcessor we've failed if we return. |
| 84 | ScopedActionCompleter abort_action_completer(processor_, this); |
| 85 | |
| 86 | if (!HasInputObject()) { |
| 87 | LOG(ERROR) << "FilesystemVerifierAction missing input object."; |
| 88 | return; |
| 89 | } |
| 90 | install_plan_ = GetInputObject(); |
| 91 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 92 | if (install_plan_.partitions.empty()) { |
| 93 | LOG(INFO) << "No partitions to verify."; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 94 | if (HasOutputPipe()) |
| 95 | SetOutputObject(install_plan_); |
| 96 | abort_action_completer.set_code(ErrorCode::kSuccess); |
| 97 | return; |
| 98 | } |
Jae Hoon Kim | 50504d6 | 2020-04-23 14:32:38 -0700 | [diff] [blame] | 99 | install_plan_.Dump(); |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 100 | StartPartitionHashing(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 101 | abort_action_completer.set_should_complete(false); |
| 102 | } |
| 103 | |
| 104 | void FilesystemVerifierAction::TerminateProcessing() { |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 105 | brillo::MessageLoop::current()->CancelTask(pending_task_id_); |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 106 | cancelled_ = true; |
| 107 | Cleanup(ErrorCode::kSuccess); // error code is ignored if canceled_ is true. |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 110 | void FilesystemVerifierAction::Cleanup(ErrorCode code) { |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 111 | partition_fd_.reset(); |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 112 | // This memory is not used anymore. |
| 113 | buffer_.clear(); |
| 114 | |
Kelvin Zhang | 9105f4b | 2021-04-26 13:44:49 -0400 | [diff] [blame] | 115 | // If we didn't write verity, partitions were maped. Releaase resource now. |
| 116 | if (!install_plan_.write_verity && |
| 117 | dynamic_control_->UpdateUsesSnapshotCompression()) { |
| 118 | LOG(INFO) << "Not writing verity and VABC is enabled, unmapping all " |
| 119 | "partitions"; |
| 120 | dynamic_control_->UnmapAllPartitions(); |
| 121 | } |
| 122 | |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 123 | if (cancelled_) |
| 124 | return; |
| 125 | if (code == ErrorCode::kSuccess && HasOutputPipe()) |
| 126 | SetOutputObject(install_plan_); |
Kelvin Zhang | 70eef23 | 2020-06-12 20:32:40 +0000 | [diff] [blame] | 127 | UpdateProgress(1.0); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 128 | processor_->ActionComplete(this, code); |
| 129 | } |
| 130 | |
Kelvin Zhang | 70eef23 | 2020-06-12 20:32:40 +0000 | [diff] [blame] | 131 | void FilesystemVerifierAction::UpdateProgress(double progress) { |
| 132 | if (delegate_ != nullptr) { |
| 133 | delegate_->OnVerifyProgressUpdate(progress); |
| 134 | } |
| 135 | } |
| 136 | |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 137 | bool FilesystemVerifierAction::InitializeFdVABC() { |
| 138 | const InstallPlan::Partition& partition = |
| 139 | install_plan_.partitions[partition_index_]; |
| 140 | |
Kelvin Zhang | 9105f4b | 2021-04-26 13:44:49 -0400 | [diff] [blame] | 141 | if (!ShouldWriteVerity()) { |
| 142 | // In VABC, if we are not writing verity, just map all partitions, |
| 143 | // and read using regular fd on |postinstall_mount_device| . |
| 144 | // All read will go through snapuserd, which provides a consistent |
| 145 | // view: device will use snapuserd to read partition during boot. |
| 146 | // b/186196758 |
| 147 | // Call UnmapAllPartitions() first, because if we wrote verity before, these |
| 148 | // writes won't be visible to previously opened snapuserd daemon. To ensure |
| 149 | // that we will see the most up to date data from partitions, call Unmap() |
| 150 | // then Map() to re-spin daemon. |
| 151 | dynamic_control_->UnmapAllPartitions(); |
| 152 | dynamic_control_->MapAllPartitions(); |
| 153 | return InitializeFd(partition.readonly_target_path); |
| 154 | } |
| 155 | |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 156 | // FilesystemVerifierAction need the read_fd_. |
| 157 | partition_fd_ = |
Kelvin Zhang | 21a4991 | 2021-03-12 14:28:33 -0500 | [diff] [blame] | 158 | dynamic_control_->OpenCowFd(partition.name, partition.source_path, true); |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 159 | if (!partition_fd_) { |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 160 | LOG(ERROR) << "OpenCowReader(" << partition.name << ", " |
| 161 | << partition.source_path << ") failed."; |
| 162 | return false; |
| 163 | } |
| 164 | partition_size_ = partition.target_size; |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 165 | return true; |
| 166 | } |
| 167 | |
| 168 | bool FilesystemVerifierAction::InitializeFd(const std::string& part_path) { |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 169 | partition_fd_ = FileDescriptorPtr(new EintrSafeFileDescriptor()); |
| 170 | const bool write_verity = ShouldWriteVerity(); |
| 171 | int flags = write_verity ? O_RDWR : O_RDONLY; |
| 172 | if (!utils::SetBlockDeviceReadOnly(part_path, !write_verity)) { |
| 173 | LOG(WARNING) << "Failed to set block device " << part_path << " as " |
| 174 | << (write_verity ? "writable" : "readonly"); |
| 175 | } |
| 176 | if (!partition_fd_->Open(part_path.c_str(), flags)) { |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 177 | LOG(ERROR) << "Unable to open " << part_path << " for reading."; |
| 178 | return false; |
| 179 | } |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 180 | return true; |
| 181 | } |
| 182 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 183 | void FilesystemVerifierAction::StartPartitionHashing() { |
| 184 | if (partition_index_ == install_plan_.partitions.size()) { |
Tianjie | 24f9609 | 2020-06-30 12:26:25 -0700 | [diff] [blame] | 185 | if (!install_plan_.untouched_dynamic_partitions.empty()) { |
| 186 | LOG(INFO) << "Verifying extents of untouched dynamic partitions [" |
| 187 | << base::JoinString(install_plan_.untouched_dynamic_partitions, |
| 188 | ", ") |
| 189 | << "]"; |
| 190 | if (!dynamic_control_->VerifyExtentsForUntouchedPartitions( |
| 191 | install_plan_.source_slot, |
| 192 | install_plan_.target_slot, |
| 193 | install_plan_.untouched_dynamic_partitions)) { |
| 194 | Cleanup(ErrorCode::kFilesystemVerifierError); |
| 195 | return; |
| 196 | } |
| 197 | } |
| 198 | |
Sen Jiang | a35896c | 2016-05-25 11:08:41 -0700 | [diff] [blame] | 199 | Cleanup(ErrorCode::kSuccess); |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 200 | return; |
| 201 | } |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 202 | const InstallPlan::Partition& partition = |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 203 | install_plan_.partitions[partition_index_]; |
Kelvin Zhang | e012f65 | 2021-05-10 16:00:31 -0400 | [diff] [blame^] | 204 | const auto& part_path = GetPartitionPath(); |
| 205 | partition_size_ = GetPartitionSize(); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 206 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 207 | LOG(INFO) << "Hashing partition " << partition_index_ << " (" |
| 208 | << partition.name << ") on device " << part_path; |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 209 | auto success = false; |
Kelvin Zhang | e012f65 | 2021-05-10 16:00:31 -0400 | [diff] [blame^] | 210 | if (IsVABC(partition)) { |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 211 | success = InitializeFdVABC(); |
| 212 | } else { |
| 213 | if (part_path.empty()) { |
| 214 | if (partition_size_ == 0) { |
| 215 | LOG(INFO) << "Skip hashing partition " << partition_index_ << " (" |
| 216 | << partition.name << ") because size is 0."; |
| 217 | partition_index_++; |
| 218 | StartPartitionHashing(); |
| 219 | return; |
| 220 | } |
| 221 | LOG(ERROR) << "Cannot hash partition " << partition_index_ << " (" |
| 222 | << partition.name |
| 223 | << ") because its device path cannot be determined."; |
| 224 | Cleanup(ErrorCode::kFilesystemVerifierError); |
| 225 | return; |
| 226 | } |
| 227 | success = InitializeFd(part_path); |
| 228 | } |
| 229 | if (!success) { |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 230 | Cleanup(ErrorCode::kFilesystemVerifierError); |
| 231 | return; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 232 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 233 | buffer_.resize(kReadFileBufferSize); |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 234 | hasher_ = std::make_unique<HashCalculator>(); |
| 235 | |
| 236 | offset_ = 0; |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 237 | filesystem_data_end_ = partition_size_; |
| 238 | CHECK_LE(partition.hash_tree_offset, partition.fec_offset) |
| 239 | << " Hash tree is expected to come before FEC data"; |
| 240 | if (partition.hash_tree_offset != 0) { |
| 241 | filesystem_data_end_ = partition.hash_tree_offset; |
| 242 | } else if (partition.fec_offset != 0) { |
| 243 | filesystem_data_end_ = partition.fec_offset; |
| 244 | } |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 245 | if (ShouldWriteVerity()) { |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 246 | if (!verity_writer_->Init(partition)) { |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 247 | LOG(INFO) << "Verity writes enabled on partition " << partition.name; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 248 | Cleanup(ErrorCode::kVerityCalculationError); |
| 249 | return; |
| 250 | } |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 251 | } else { |
| 252 | LOG(INFO) << "Verity writes disabled on partition " << partition.name; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 253 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 254 | |
| 255 | // Start the first read. |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 256 | ScheduleFileSystemRead(); |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Kelvin Zhang | e012f65 | 2021-05-10 16:00:31 -0400 | [diff] [blame^] | 259 | bool FilesystemVerifierAction::IsVABC( |
| 260 | const InstallPlan::Partition& partition) const { |
| 261 | return dynamic_control_->UpdateUsesSnapshotCompression() && |
| 262 | verifier_step_ == VerifierStep::kVerifyTargetHash && |
| 263 | dynamic_control_->IsDynamicPartition(partition.name, |
| 264 | install_plan_.target_slot); |
| 265 | } |
| 266 | |
| 267 | const std::string& FilesystemVerifierAction::GetPartitionPath() const { |
| 268 | const InstallPlan::Partition& partition = |
| 269 | install_plan_.partitions[partition_index_]; |
| 270 | switch (verifier_step_) { |
| 271 | case VerifierStep::kVerifySourceHash: |
| 272 | return partition.source_path; |
| 273 | case VerifierStep::kVerifyTargetHash: |
| 274 | if (IsVABC(partition)) { |
| 275 | return partition.readonly_target_path; |
| 276 | } else { |
| 277 | return partition.target_path; |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | size_t FilesystemVerifierAction::GetPartitionSize() const { |
| 283 | const InstallPlan::Partition& partition = |
| 284 | install_plan_.partitions[partition_index_]; |
| 285 | switch (verifier_step_) { |
| 286 | case VerifierStep::kVerifySourceHash: |
| 287 | return partition.source_size; |
| 288 | case VerifierStep::kVerifyTargetHash: |
| 289 | return partition.target_size; |
| 290 | } |
| 291 | } |
| 292 | |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 293 | bool FilesystemVerifierAction::ShouldWriteVerity() { |
| 294 | const InstallPlan::Partition& partition = |
| 295 | install_plan_.partitions[partition_index_]; |
| 296 | return verifier_step_ == VerifierStep::kVerifyTargetHash && |
| 297 | install_plan_.write_verity && |
| 298 | (partition.hash_tree_size > 0 || partition.fec_size > 0); |
| 299 | } |
| 300 | |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 301 | void FilesystemVerifierAction::ReadVerityAndFooter() { |
| 302 | if (ShouldWriteVerity()) { |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 303 | if (!verity_writer_->Finalize(partition_fd_, partition_fd_)) { |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 304 | LOG(ERROR) << "Failed to write hashtree/FEC data."; |
| 305 | Cleanup(ErrorCode::kFilesystemVerifierError); |
| 306 | return; |
| 307 | } |
| 308 | } |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 309 | // Since we handed our |read_fd_| to verity_writer_ during |Finalize()| |
| 310 | // call, fd's position could have been changed. Re-seek. |
| 311 | partition_fd_->Seek(filesystem_data_end_, SEEK_SET); |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 312 | auto bytes_to_read = partition_size_ - filesystem_data_end_; |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 313 | while (bytes_to_read > 0) { |
| 314 | const auto read_size = std::min<size_t>(buffer_.size(), bytes_to_read); |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 315 | auto bytes_read = partition_fd_->Read(buffer_.data(), read_size); |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 316 | if (bytes_read <= 0) { |
| 317 | PLOG(ERROR) << "Failed to read hash tree " << bytes_read; |
| 318 | Cleanup(ErrorCode::kFilesystemVerifierError); |
| 319 | return; |
| 320 | } |
| 321 | if (!hasher_->Update(buffer_.data(), bytes_read)) { |
| 322 | LOG(ERROR) << "Unable to update the hash."; |
| 323 | Cleanup(ErrorCode::kError); |
| 324 | return; |
| 325 | } |
| 326 | bytes_to_read -= bytes_read; |
| 327 | } |
| 328 | FinishPartitionHashing(); |
| 329 | } |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 330 | |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 331 | void FilesystemVerifierAction::ScheduleFileSystemRead() { |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 332 | // We can only start reading anything past |hash_tree_offset| after we have |
| 333 | // already read all the data blocks that the hash tree covers. The same |
| 334 | // applies to FEC. |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 335 | |
| 336 | size_t bytes_to_read = std::min(static_cast<uint64_t>(buffer_.size()), |
| 337 | filesystem_data_end_ - offset_); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 338 | if (!bytes_to_read) { |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 339 | ReadVerityAndFooter(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 340 | return; |
| 341 | } |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 342 | partition_fd_->Seek(offset_, SEEK_SET); |
| 343 | auto bytes_read = partition_fd_->Read(buffer_.data(), bytes_to_read); |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 344 | if (bytes_read < 0) { |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 345 | LOG(ERROR) << "Unable to schedule an asynchronous read from the stream. " |
| 346 | << bytes_read; |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 347 | Cleanup(ErrorCode::kError); |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 348 | } else { |
| 349 | // We could just invoke |OnReadDoneCallback()|, it works. But |PostTask| |
| 350 | // is used so that users can cancel updates. |
| 351 | pending_task_id_ = brillo::MessageLoop::current()->PostTask( |
| 352 | base::Bind(&FilesystemVerifierAction::OnReadDone, |
| 353 | base::Unretained(this), |
| 354 | bytes_read)); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 358 | void FilesystemVerifierAction::OnReadDone(size_t bytes_read) { |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 359 | if (cancelled_) { |
| 360 | Cleanup(ErrorCode::kError); |
| 361 | return; |
| 362 | } |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 363 | if (bytes_read == 0) { |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 364 | LOG(ERROR) << "Failed to read the remaining " << partition_size_ - offset_ |
| 365 | << " bytes from partition " |
| 366 | << install_plan_.partitions[partition_index_].name; |
| 367 | Cleanup(ErrorCode::kFilesystemVerifierError); |
| 368 | return; |
| 369 | } |
| 370 | |
| 371 | if (!hasher_->Update(buffer_.data(), bytes_read)) { |
| 372 | LOG(ERROR) << "Unable to update the hash."; |
| 373 | Cleanup(ErrorCode::kError); |
| 374 | return; |
| 375 | } |
| 376 | |
Kelvin Zhang | 70eef23 | 2020-06-12 20:32:40 +0000 | [diff] [blame] | 377 | // WE don't consider sizes of each partition. Every partition |
| 378 | // has the same length on progress bar. |
| 379 | // TODO(zhangkelvin) Take sizes of each partition into account |
| 380 | |
| 381 | UpdateProgress( |
| 382 | (static_cast<double>(offset_) / partition_size_ + partition_index_) / |
| 383 | install_plan_.partitions.size()); |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 384 | if (ShouldWriteVerity()) { |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 385 | if (!verity_writer_->Update(offset_, buffer_.data(), bytes_read)) { |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 386 | LOG(ERROR) << "Unable to update verity"; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 387 | Cleanup(ErrorCode::kVerityCalculationError); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 388 | return; |
| 389 | } |
| 390 | } |
| 391 | |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 392 | offset_ += bytes_read; |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 393 | if (offset_ == filesystem_data_end_) { |
| 394 | ReadVerityAndFooter(); |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 395 | return; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 396 | } |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 397 | |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 398 | ScheduleFileSystemRead(); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 401 | void FilesystemVerifierAction::FinishPartitionHashing() { |
| 402 | if (!hasher_->Finalize()) { |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 403 | LOG(ERROR) << "Unable to finalize the hash."; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 404 | Cleanup(ErrorCode::kError); |
| 405 | return; |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 406 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 407 | InstallPlan::Partition& partition = |
| 408 | install_plan_.partitions[partition_index_]; |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 409 | LOG(INFO) << "Hash of " << partition.name << ": " |
| 410 | << Base64Encode(hasher_->raw_hash()); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 411 | |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 412 | switch (verifier_step_) { |
| 413 | case VerifierStep::kVerifyTargetHash: |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 414 | if (partition.target_hash != hasher_->raw_hash()) { |
| 415 | LOG(ERROR) << "New '" << partition.name |
| 416 | << "' partition verification failed."; |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 417 | if (partition.source_hash.empty()) { |
| 418 | // No need to verify source if it is a full payload. |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 419 | Cleanup(ErrorCode::kNewRootfsVerificationError); |
| 420 | return; |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 421 | } |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 422 | // If we have not verified source partition yet, now that the target |
Sen Jiang | 65566a3 | 2016-04-06 13:35:36 -0700 | [diff] [blame] | 423 | // 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] | 424 | // switch to kVerifySourceHash step to check if it's because the |
| 425 | // source partition does not match either. |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 426 | verifier_step_ = VerifierStep::kVerifySourceHash; |
Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame] | 427 | } else { |
| 428 | partition_index_++; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 429 | } |
| 430 | break; |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 431 | case VerifierStep::kVerifySourceHash: |
Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame] | 432 | if (partition.source_hash != hasher_->raw_hash()) { |
| 433 | LOG(ERROR) << "Old '" << partition.name |
| 434 | << "' partition verification failed."; |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 435 | LOG(ERROR) << "This is a server-side error due to mismatched delta" |
| 436 | << " update image!"; |
| 437 | LOG(ERROR) << "The delta I've been given contains a " << partition.name |
| 438 | << " delta update that must be applied over a " |
| 439 | << partition.name << " with a specific checksum, but the " |
| 440 | << partition.name |
| 441 | << " we're starting with doesn't have that checksum! This" |
| 442 | " means that the delta I've been given doesn't match my" |
| 443 | " existing system. The " |
| 444 | << partition.name << " partition I have has hash: " |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 445 | << Base64Encode(hasher_->raw_hash()) |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 446 | << " but the update expected me to have " |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 447 | << Base64Encode(partition.source_hash) << " ."; |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 448 | LOG(INFO) << "To get the checksum of the " << partition.name |
| 449 | << " partition run this command: dd if=" |
| 450 | << partition.source_path |
| 451 | << " bs=1M count=" << partition.source_size |
| 452 | << " iflag=count_bytes 2>/dev/null | openssl dgst -sha256 " |
| 453 | "-binary | openssl base64"; |
| 454 | LOG(INFO) << "To get the checksum of partitions in a bin file, " |
| 455 | << "run: .../src/scripts/sha256_partitions.sh .../file.bin"; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 456 | Cleanup(ErrorCode::kDownloadStateInitializationError); |
| 457 | return; |
Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame] | 458 | } |
Sen Jiang | a35896c | 2016-05-25 11:08:41 -0700 | [diff] [blame] | 459 | // The action will skip kVerifySourceHash step if target partition hash |
| 460 | // 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] | 461 | // and now that the source partition hash matches, we should set the |
| 462 | // error code to reflect the error in target partition. We only need to |
| 463 | // verify the source partition which the target hash does not match, the |
| 464 | // rest of the partitions don't matter. |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 465 | Cleanup(ErrorCode::kNewRootfsVerificationError); |
| 466 | return; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 467 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 468 | // Start hashing the next partition, if any. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 469 | hasher_.reset(); |
| 470 | buffer_.clear(); |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 471 | if (partition_fd_) { |
| 472 | partition_fd_.reset(); |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 473 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 474 | StartPartitionHashing(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | } // namespace chromeos_update_engine |