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 | |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 115 | if (cancelled_) |
| 116 | return; |
| 117 | if (code == ErrorCode::kSuccess && HasOutputPipe()) |
| 118 | SetOutputObject(install_plan_); |
Kelvin Zhang | 70eef23 | 2020-06-12 20:32:40 +0000 | [diff] [blame] | 119 | UpdateProgress(1.0); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 120 | processor_->ActionComplete(this, code); |
| 121 | } |
| 122 | |
Kelvin Zhang | 70eef23 | 2020-06-12 20:32:40 +0000 | [diff] [blame] | 123 | void FilesystemVerifierAction::UpdateProgress(double progress) { |
| 124 | if (delegate_ != nullptr) { |
| 125 | delegate_->OnVerifyProgressUpdate(progress); |
| 126 | } |
| 127 | } |
| 128 | |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 129 | bool FilesystemVerifierAction::InitializeFdVABC() { |
| 130 | const InstallPlan::Partition& partition = |
| 131 | install_plan_.partitions[partition_index_]; |
| 132 | |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 133 | // FilesystemVerifierAction need the read_fd_. |
| 134 | partition_fd_ = |
Kelvin Zhang | 21a4991 | 2021-03-12 14:28:33 -0500 | [diff] [blame] | 135 | dynamic_control_->OpenCowFd(partition.name, partition.source_path, true); |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 136 | if (!partition_fd_) { |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 137 | LOG(ERROR) << "OpenCowReader(" << partition.name << ", " |
| 138 | << partition.source_path << ") failed."; |
| 139 | return false; |
| 140 | } |
| 141 | partition_size_ = partition.target_size; |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 142 | return true; |
| 143 | } |
| 144 | |
| 145 | bool FilesystemVerifierAction::InitializeFd(const std::string& part_path) { |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 146 | partition_fd_ = FileDescriptorPtr(new EintrSafeFileDescriptor()); |
| 147 | const bool write_verity = ShouldWriteVerity(); |
| 148 | int flags = write_verity ? O_RDWR : O_RDONLY; |
| 149 | if (!utils::SetBlockDeviceReadOnly(part_path, !write_verity)) { |
| 150 | LOG(WARNING) << "Failed to set block device " << part_path << " as " |
| 151 | << (write_verity ? "writable" : "readonly"); |
| 152 | } |
| 153 | if (!partition_fd_->Open(part_path.c_str(), flags)) { |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 154 | LOG(ERROR) << "Unable to open " << part_path << " for reading."; |
| 155 | return false; |
| 156 | } |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 157 | return true; |
| 158 | } |
| 159 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 160 | void FilesystemVerifierAction::StartPartitionHashing() { |
| 161 | if (partition_index_ == install_plan_.partitions.size()) { |
Tianjie | 24f9609 | 2020-06-30 12:26:25 -0700 | [diff] [blame] | 162 | if (!install_plan_.untouched_dynamic_partitions.empty()) { |
| 163 | LOG(INFO) << "Verifying extents of untouched dynamic partitions [" |
| 164 | << base::JoinString(install_plan_.untouched_dynamic_partitions, |
| 165 | ", ") |
| 166 | << "]"; |
| 167 | if (!dynamic_control_->VerifyExtentsForUntouchedPartitions( |
| 168 | install_plan_.source_slot, |
| 169 | install_plan_.target_slot, |
| 170 | install_plan_.untouched_dynamic_partitions)) { |
| 171 | Cleanup(ErrorCode::kFilesystemVerifierError); |
| 172 | return; |
| 173 | } |
| 174 | } |
| 175 | |
Sen Jiang | a35896c | 2016-05-25 11:08:41 -0700 | [diff] [blame] | 176 | Cleanup(ErrorCode::kSuccess); |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 177 | return; |
| 178 | } |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 179 | const InstallPlan::Partition& partition = |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 180 | install_plan_.partitions[partition_index_]; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 181 | string part_path; |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 182 | switch (verifier_step_) { |
| 183 | case VerifierStep::kVerifySourceHash: |
Sen Jiang | e6e4bb9 | 2016-04-05 14:59:12 -0700 | [diff] [blame] | 184 | part_path = partition.source_path; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 185 | partition_size_ = partition.source_size; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 186 | break; |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 187 | case VerifierStep::kVerifyTargetHash: |
Sen Jiang | e6e4bb9 | 2016-04-05 14:59:12 -0700 | [diff] [blame] | 188 | part_path = partition.target_path; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 189 | partition_size_ = partition.target_size; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 190 | break; |
| 191 | } |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 192 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 193 | LOG(INFO) << "Hashing partition " << partition_index_ << " (" |
| 194 | << partition.name << ") on device " << part_path; |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 195 | auto success = false; |
Kelvin Zhang | 0618835 | 2021-02-10 13:21:47 -0500 | [diff] [blame] | 196 | if (dynamic_control_->UpdateUsesSnapshotCompression() && |
Kelvin Zhang | ebd115e | 2021-03-08 16:10:25 -0500 | [diff] [blame] | 197 | verifier_step_ == VerifierStep::kVerifyTargetHash && |
| 198 | dynamic_control_->IsDynamicPartition(partition.name, |
| 199 | install_plan_.target_slot)) { |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 200 | success = InitializeFdVABC(); |
| 201 | } else { |
| 202 | if (part_path.empty()) { |
| 203 | if (partition_size_ == 0) { |
| 204 | LOG(INFO) << "Skip hashing partition " << partition_index_ << " (" |
| 205 | << partition.name << ") because size is 0."; |
| 206 | partition_index_++; |
| 207 | StartPartitionHashing(); |
| 208 | return; |
| 209 | } |
| 210 | LOG(ERROR) << "Cannot hash partition " << partition_index_ << " (" |
| 211 | << partition.name |
| 212 | << ") because its device path cannot be determined."; |
| 213 | Cleanup(ErrorCode::kFilesystemVerifierError); |
| 214 | return; |
| 215 | } |
| 216 | success = InitializeFd(part_path); |
| 217 | } |
| 218 | if (!success) { |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 219 | Cleanup(ErrorCode::kFilesystemVerifierError); |
| 220 | return; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 221 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 222 | buffer_.resize(kReadFileBufferSize); |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 223 | hasher_ = std::make_unique<HashCalculator>(); |
| 224 | |
| 225 | offset_ = 0; |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 226 | filesystem_data_end_ = partition_size_; |
| 227 | CHECK_LE(partition.hash_tree_offset, partition.fec_offset) |
| 228 | << " Hash tree is expected to come before FEC data"; |
| 229 | if (partition.hash_tree_offset != 0) { |
| 230 | filesystem_data_end_ = partition.hash_tree_offset; |
| 231 | } else if (partition.fec_offset != 0) { |
| 232 | filesystem_data_end_ = partition.fec_offset; |
| 233 | } |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 234 | if (ShouldWriteVerity()) { |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 235 | if (!verity_writer_->Init(partition)) { |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 236 | LOG(INFO) << "Verity writes enabled on partition " << partition.name; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 237 | Cleanup(ErrorCode::kVerityCalculationError); |
| 238 | return; |
| 239 | } |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 240 | } else { |
| 241 | LOG(INFO) << "Verity writes disabled on partition " << partition.name; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 242 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 243 | |
| 244 | // Start the first read. |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 245 | ScheduleFileSystemRead(); |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 248 | bool FilesystemVerifierAction::ShouldWriteVerity() { |
| 249 | const InstallPlan::Partition& partition = |
| 250 | install_plan_.partitions[partition_index_]; |
| 251 | return verifier_step_ == VerifierStep::kVerifyTargetHash && |
| 252 | install_plan_.write_verity && |
| 253 | (partition.hash_tree_size > 0 || partition.fec_size > 0); |
| 254 | } |
| 255 | |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 256 | void FilesystemVerifierAction::ReadVerityAndFooter() { |
| 257 | if (ShouldWriteVerity()) { |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 258 | if (!verity_writer_->Finalize(partition_fd_, partition_fd_)) { |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 259 | LOG(ERROR) << "Failed to write hashtree/FEC data."; |
| 260 | Cleanup(ErrorCode::kFilesystemVerifierError); |
| 261 | return; |
| 262 | } |
| 263 | } |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 264 | // Since we handed our |read_fd_| to verity_writer_ during |Finalize()| |
| 265 | // call, fd's position could have been changed. Re-seek. |
| 266 | partition_fd_->Seek(filesystem_data_end_, SEEK_SET); |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 267 | auto bytes_to_read = partition_size_ - filesystem_data_end_; |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 268 | while (bytes_to_read > 0) { |
| 269 | 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] | 270 | auto bytes_read = partition_fd_->Read(buffer_.data(), read_size); |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 271 | if (bytes_read <= 0) { |
| 272 | PLOG(ERROR) << "Failed to read hash tree " << bytes_read; |
| 273 | Cleanup(ErrorCode::kFilesystemVerifierError); |
| 274 | return; |
| 275 | } |
| 276 | if (!hasher_->Update(buffer_.data(), bytes_read)) { |
| 277 | LOG(ERROR) << "Unable to update the hash."; |
| 278 | Cleanup(ErrorCode::kError); |
| 279 | return; |
| 280 | } |
| 281 | bytes_to_read -= bytes_read; |
| 282 | } |
| 283 | FinishPartitionHashing(); |
| 284 | } |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 285 | |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 286 | void FilesystemVerifierAction::ScheduleFileSystemRead() { |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 287 | // We can only start reading anything past |hash_tree_offset| after we have |
| 288 | // already read all the data blocks that the hash tree covers. The same |
| 289 | // applies to FEC. |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 290 | |
| 291 | size_t bytes_to_read = std::min(static_cast<uint64_t>(buffer_.size()), |
| 292 | filesystem_data_end_ - offset_); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 293 | if (!bytes_to_read) { |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 294 | ReadVerityAndFooter(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 295 | return; |
| 296 | } |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 297 | partition_fd_->Seek(offset_, SEEK_SET); |
| 298 | auto bytes_read = partition_fd_->Read(buffer_.data(), bytes_to_read); |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 299 | if (bytes_read < 0) { |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 300 | LOG(ERROR) << "Unable to schedule an asynchronous read from the stream. " |
| 301 | << bytes_read; |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 302 | Cleanup(ErrorCode::kError); |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 303 | } else { |
| 304 | // We could just invoke |OnReadDoneCallback()|, it works. But |PostTask| |
| 305 | // is used so that users can cancel updates. |
| 306 | pending_task_id_ = brillo::MessageLoop::current()->PostTask( |
| 307 | base::Bind(&FilesystemVerifierAction::OnReadDone, |
| 308 | base::Unretained(this), |
| 309 | bytes_read)); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 313 | void FilesystemVerifierAction::OnReadDone(size_t bytes_read) { |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 314 | if (cancelled_) { |
| 315 | Cleanup(ErrorCode::kError); |
| 316 | return; |
| 317 | } |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 318 | if (bytes_read == 0) { |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 319 | LOG(ERROR) << "Failed to read the remaining " << partition_size_ - offset_ |
| 320 | << " bytes from partition " |
| 321 | << install_plan_.partitions[partition_index_].name; |
| 322 | Cleanup(ErrorCode::kFilesystemVerifierError); |
| 323 | return; |
| 324 | } |
| 325 | |
| 326 | if (!hasher_->Update(buffer_.data(), bytes_read)) { |
| 327 | LOG(ERROR) << "Unable to update the hash."; |
| 328 | Cleanup(ErrorCode::kError); |
| 329 | return; |
| 330 | } |
| 331 | |
Kelvin Zhang | 70eef23 | 2020-06-12 20:32:40 +0000 | [diff] [blame] | 332 | // WE don't consider sizes of each partition. Every partition |
| 333 | // has the same length on progress bar. |
| 334 | // TODO(zhangkelvin) Take sizes of each partition into account |
| 335 | |
| 336 | UpdateProgress( |
| 337 | (static_cast<double>(offset_) / partition_size_ + partition_index_) / |
| 338 | install_plan_.partitions.size()); |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 339 | if (ShouldWriteVerity()) { |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 340 | if (!verity_writer_->Update(offset_, buffer_.data(), bytes_read)) { |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 341 | LOG(ERROR) << "Unable to update verity"; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 342 | Cleanup(ErrorCode::kVerityCalculationError); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 343 | return; |
| 344 | } |
| 345 | } |
| 346 | |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 347 | offset_ += bytes_read; |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 348 | if (offset_ == filesystem_data_end_) { |
| 349 | ReadVerityAndFooter(); |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 350 | return; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 351 | } |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 352 | |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 353 | ScheduleFileSystemRead(); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 356 | void FilesystemVerifierAction::FinishPartitionHashing() { |
| 357 | if (!hasher_->Finalize()) { |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 358 | LOG(ERROR) << "Unable to finalize the hash."; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 359 | Cleanup(ErrorCode::kError); |
| 360 | return; |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 361 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 362 | InstallPlan::Partition& partition = |
| 363 | install_plan_.partitions[partition_index_]; |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 364 | LOG(INFO) << "Hash of " << partition.name << ": " |
| 365 | << Base64Encode(hasher_->raw_hash()); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 366 | |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 367 | switch (verifier_step_) { |
| 368 | case VerifierStep::kVerifyTargetHash: |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 369 | if (partition.target_hash != hasher_->raw_hash()) { |
| 370 | LOG(ERROR) << "New '" << partition.name |
| 371 | << "' partition verification failed."; |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 372 | if (partition.source_hash.empty()) { |
| 373 | // No need to verify source if it is a full payload. |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 374 | Cleanup(ErrorCode::kNewRootfsVerificationError); |
| 375 | return; |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 376 | } |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 377 | // If we have not verified source partition yet, now that the target |
Sen Jiang | 65566a3 | 2016-04-06 13:35:36 -0700 | [diff] [blame] | 378 | // 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] | 379 | // switch to kVerifySourceHash step to check if it's because the |
| 380 | // source partition does not match either. |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 381 | verifier_step_ = VerifierStep::kVerifySourceHash; |
Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame] | 382 | } else { |
| 383 | partition_index_++; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 384 | } |
| 385 | break; |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 386 | case VerifierStep::kVerifySourceHash: |
Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame] | 387 | if (partition.source_hash != hasher_->raw_hash()) { |
| 388 | LOG(ERROR) << "Old '" << partition.name |
| 389 | << "' partition verification failed."; |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 390 | LOG(ERROR) << "This is a server-side error due to mismatched delta" |
| 391 | << " update image!"; |
| 392 | LOG(ERROR) << "The delta I've been given contains a " << partition.name |
| 393 | << " delta update that must be applied over a " |
| 394 | << partition.name << " with a specific checksum, but the " |
| 395 | << partition.name |
| 396 | << " we're starting with doesn't have that checksum! This" |
| 397 | " means that the delta I've been given doesn't match my" |
| 398 | " existing system. The " |
| 399 | << partition.name << " partition I have has hash: " |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 400 | << Base64Encode(hasher_->raw_hash()) |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 401 | << " but the update expected me to have " |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 402 | << Base64Encode(partition.source_hash) << " ."; |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 403 | LOG(INFO) << "To get the checksum of the " << partition.name |
| 404 | << " partition run this command: dd if=" |
| 405 | << partition.source_path |
| 406 | << " bs=1M count=" << partition.source_size |
| 407 | << " iflag=count_bytes 2>/dev/null | openssl dgst -sha256 " |
| 408 | "-binary | openssl base64"; |
| 409 | LOG(INFO) << "To get the checksum of partitions in a bin file, " |
| 410 | << "run: .../src/scripts/sha256_partitions.sh .../file.bin"; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 411 | Cleanup(ErrorCode::kDownloadStateInitializationError); |
| 412 | return; |
Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame] | 413 | } |
Sen Jiang | a35896c | 2016-05-25 11:08:41 -0700 | [diff] [blame] | 414 | // The action will skip kVerifySourceHash step if target partition hash |
| 415 | // 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] | 416 | // and now that the source partition hash matches, we should set the |
| 417 | // error code to reflect the error in target partition. We only need to |
| 418 | // verify the source partition which the target hash does not match, the |
| 419 | // rest of the partitions don't matter. |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 420 | Cleanup(ErrorCode::kNewRootfsVerificationError); |
| 421 | return; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 422 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 423 | // Start hashing the next partition, if any. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 424 | hasher_.reset(); |
| 425 | buffer_.clear(); |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 426 | if (partition_fd_) { |
| 427 | partition_fd_.reset(); |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 428 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 429 | StartPartitionHashing(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | } // namespace chromeos_update_engine |