| 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 |  | 
|  | 43 | namespace chromeos_update_engine { | 
|  | 44 |  | 
|  | 45 | namespace { | 
| Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 46 | const off_t kReadFileBufferSize = 128 * 1024; | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 47 | }  // namespace | 
|  | 48 |  | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 49 | void FilesystemVerifierAction::PerformAction() { | 
|  | 50 | // Will tell the ActionProcessor we've failed if we return. | 
|  | 51 | ScopedActionCompleter abort_action_completer(processor_, this); | 
|  | 52 |  | 
|  | 53 | if (!HasInputObject()) { | 
|  | 54 | LOG(ERROR) << "FilesystemVerifierAction missing input object."; | 
|  | 55 | return; | 
|  | 56 | } | 
|  | 57 | install_plan_ = GetInputObject(); | 
|  | 58 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 59 | if (install_plan_.partitions.empty()) { | 
|  | 60 | LOG(INFO) << "No partitions to verify."; | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 61 | if (HasOutputPipe()) | 
|  | 62 | SetOutputObject(install_plan_); | 
|  | 63 | abort_action_completer.set_code(ErrorCode::kSuccess); | 
|  | 64 | return; | 
|  | 65 | } | 
| Jae Hoon Kim | 50504d6 | 2020-04-23 14:32:38 -0700 | [diff] [blame] | 66 | install_plan_.Dump(); | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 67 | StartPartitionHashing(); | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 68 | abort_action_completer.set_should_complete(false); | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | void FilesystemVerifierAction::TerminateProcessing() { | 
| Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 72 | brillo::MessageLoop::current()->CancelTask(pending_task_id_); | 
| Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 73 | cancelled_ = true; | 
|  | 74 | Cleanup(ErrorCode::kSuccess);  // error code is ignored if canceled_ is true. | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 75 | } | 
|  | 76 |  | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 77 | void FilesystemVerifierAction::Cleanup(ErrorCode code) { | 
| Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 78 | read_fd_.reset(); | 
|  | 79 | write_fd_.reset(); | 
| Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 80 | // This memory is not used anymore. | 
|  | 81 | buffer_.clear(); | 
|  | 82 |  | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 83 | if (cancelled_) | 
|  | 84 | return; | 
|  | 85 | if (code == ErrorCode::kSuccess && HasOutputPipe()) | 
|  | 86 | SetOutputObject(install_plan_); | 
| Kelvin Zhang | 70eef23 | 2020-06-12 20:32:40 +0000 | [diff] [blame] | 87 | UpdateProgress(1.0); | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 88 | processor_->ActionComplete(this, code); | 
|  | 89 | } | 
|  | 90 |  | 
| Kelvin Zhang | 70eef23 | 2020-06-12 20:32:40 +0000 | [diff] [blame] | 91 | void FilesystemVerifierAction::UpdateProgress(double progress) { | 
|  | 92 | if (delegate_ != nullptr) { | 
|  | 93 | delegate_->OnVerifyProgressUpdate(progress); | 
|  | 94 | } | 
|  | 95 | } | 
|  | 96 |  | 
| Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 97 | bool FilesystemVerifierAction::InitializeFdVABC() { | 
|  | 98 | const InstallPlan::Partition& partition = | 
|  | 99 | install_plan_.partitions[partition_index_]; | 
|  | 100 |  | 
|  | 101 | read_fd_ = dynamic_control_->OpenCowReader( | 
|  | 102 | partition.name, partition.source_path, true); | 
|  | 103 | if (!read_fd_) { | 
|  | 104 | LOG(ERROR) << "OpenCowReader(" << partition.name << ", " | 
|  | 105 | << partition.source_path << ") failed."; | 
|  | 106 | return false; | 
|  | 107 | } | 
|  | 108 | partition_size_ = partition.target_size; | 
|  | 109 | // TODO(b/173432386): Support Verity writes for VABC. | 
|  | 110 | CHECK_EQ(partition.fec_size, 0U); | 
|  | 111 | CHECK_EQ(partition.hash_tree_size, 0U); | 
|  | 112 | return true; | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | bool FilesystemVerifierAction::InitializeFd(const std::string& part_path) { | 
|  | 116 | read_fd_ = FileDescriptorPtr(new EintrSafeFileDescriptor()); | 
|  | 117 | if (!read_fd_->Open(part_path.c_str(), O_RDONLY)) { | 
|  | 118 | LOG(ERROR) << "Unable to open " << part_path << " for reading."; | 
|  | 119 | return false; | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | // Can't re-use |read_fd_|, as verity writer may call `seek` to modify state | 
|  | 123 | // of a file descriptor. | 
|  | 124 | if (ShouldWriteVerity()) { | 
|  | 125 | write_fd_ = FileDescriptorPtr(new EintrSafeFileDescriptor()); | 
|  | 126 | if (!write_fd_->Open(part_path.c_str(), O_RDWR)) { | 
|  | 127 | LOG(ERROR) << "Unable to open " << part_path << " for Read/Write."; | 
|  | 128 | return false; | 
|  | 129 | } | 
|  | 130 | } | 
|  | 131 | return true; | 
|  | 132 | } | 
|  | 133 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 134 | void FilesystemVerifierAction::StartPartitionHashing() { | 
|  | 135 | if (partition_index_ == install_plan_.partitions.size()) { | 
| Tianjie | 24f9609 | 2020-06-30 12:26:25 -0700 | [diff] [blame] | 136 | if (!install_plan_.untouched_dynamic_partitions.empty()) { | 
|  | 137 | LOG(INFO) << "Verifying extents of untouched dynamic partitions [" | 
|  | 138 | << base::JoinString(install_plan_.untouched_dynamic_partitions, | 
|  | 139 | ", ") | 
|  | 140 | << "]"; | 
|  | 141 | if (!dynamic_control_->VerifyExtentsForUntouchedPartitions( | 
|  | 142 | install_plan_.source_slot, | 
|  | 143 | install_plan_.target_slot, | 
|  | 144 | install_plan_.untouched_dynamic_partitions)) { | 
|  | 145 | Cleanup(ErrorCode::kFilesystemVerifierError); | 
|  | 146 | return; | 
|  | 147 | } | 
|  | 148 | } | 
|  | 149 |  | 
| Sen Jiang | a35896c | 2016-05-25 11:08:41 -0700 | [diff] [blame] | 150 | Cleanup(ErrorCode::kSuccess); | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 151 | return; | 
|  | 152 | } | 
| Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 153 | const InstallPlan::Partition& partition = | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 154 | install_plan_.partitions[partition_index_]; | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 155 | string part_path; | 
| Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 156 | switch (verifier_step_) { | 
|  | 157 | case VerifierStep::kVerifySourceHash: | 
| Sen Jiang | e6e4bb9 | 2016-04-05 14:59:12 -0700 | [diff] [blame] | 158 | part_path = partition.source_path; | 
| Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 159 | partition_size_ = partition.source_size; | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 160 | break; | 
| Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 161 | case VerifierStep::kVerifyTargetHash: | 
| Sen Jiang | e6e4bb9 | 2016-04-05 14:59:12 -0700 | [diff] [blame] | 162 | part_path = partition.target_path; | 
| Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 163 | partition_size_ = partition.target_size; | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 164 | break; | 
|  | 165 | } | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 166 |  | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 167 | LOG(INFO) << "Hashing partition " << partition_index_ << " (" | 
|  | 168 | << partition.name << ") on device " << part_path; | 
| Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 169 | auto success = false; | 
|  | 170 | if (dynamic_control_->GetVirtualAbCompressionFeatureFlag().IsEnabled() && | 
|  | 171 | dynamic_control_->IsDynamicPartition(partition.name) && | 
|  | 172 | verifier_step_ == VerifierStep::kVerifyTargetHash) { | 
|  | 173 | success = InitializeFdVABC(); | 
|  | 174 | } else { | 
|  | 175 | if (part_path.empty()) { | 
|  | 176 | if (partition_size_ == 0) { | 
|  | 177 | LOG(INFO) << "Skip hashing partition " << partition_index_ << " (" | 
|  | 178 | << partition.name << ") because size is 0."; | 
|  | 179 | partition_index_++; | 
|  | 180 | StartPartitionHashing(); | 
|  | 181 | return; | 
|  | 182 | } | 
|  | 183 | LOG(ERROR) << "Cannot hash partition " << partition_index_ << " (" | 
|  | 184 | << partition.name | 
|  | 185 | << ") because its device path cannot be determined."; | 
|  | 186 | Cleanup(ErrorCode::kFilesystemVerifierError); | 
|  | 187 | return; | 
|  | 188 | } | 
|  | 189 | success = InitializeFd(part_path); | 
|  | 190 | } | 
|  | 191 | if (!success) { | 
| Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 192 | Cleanup(ErrorCode::kFilesystemVerifierError); | 
|  | 193 | return; | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 194 | } | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 195 | buffer_.resize(kReadFileBufferSize); | 
| Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 196 | hasher_ = std::make_unique<HashCalculator>(); | 
|  | 197 |  | 
|  | 198 | offset_ = 0; | 
| Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 199 | if (ShouldWriteVerity()) { | 
|  | 200 | if (!verity_writer_->Init(partition, read_fd_, write_fd_)) { | 
| Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 201 | Cleanup(ErrorCode::kVerityCalculationError); | 
|  | 202 | return; | 
|  | 203 | } | 
|  | 204 | } | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 205 |  | 
|  | 206 | // Start the first read. | 
|  | 207 | ScheduleRead(); | 
|  | 208 | } | 
|  | 209 |  | 
| Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 210 | bool FilesystemVerifierAction::ShouldWriteVerity() { | 
|  | 211 | const InstallPlan::Partition& partition = | 
|  | 212 | install_plan_.partitions[partition_index_]; | 
|  | 213 | return verifier_step_ == VerifierStep::kVerifyTargetHash && | 
|  | 214 | install_plan_.write_verity && | 
|  | 215 | (partition.hash_tree_size > 0 || partition.fec_size > 0); | 
|  | 216 | } | 
|  | 217 |  | 
| Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 218 | void FilesystemVerifierAction::ScheduleRead() { | 
| Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 219 | const InstallPlan::Partition& partition = | 
|  | 220 | install_plan_.partitions[partition_index_]; | 
|  | 221 |  | 
|  | 222 | // We can only start reading anything past |hash_tree_offset| after we have | 
|  | 223 | // already read all the data blocks that the hash tree covers. The same | 
|  | 224 | // applies to FEC. | 
|  | 225 | uint64_t read_end = partition_size_; | 
|  | 226 | if (partition.hash_tree_size != 0 && | 
|  | 227 | offset_ < partition.hash_tree_data_offset + partition.hash_tree_data_size) | 
|  | 228 | read_end = std::min(read_end, partition.hash_tree_offset); | 
|  | 229 | if (partition.fec_size != 0 && | 
|  | 230 | offset_ < partition.fec_data_offset + partition.fec_data_size) | 
|  | 231 | read_end = std::min(read_end, partition.fec_offset); | 
|  | 232 | size_t bytes_to_read = | 
|  | 233 | std::min(static_cast<uint64_t>(buffer_.size()), read_end - offset_); | 
| Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 234 | if (!bytes_to_read) { | 
| Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 235 | FinishPartitionHashing(); | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 236 | return; | 
|  | 237 | } | 
|  | 238 |  | 
| Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 239 | auto bytes_read = read_fd_->Read(buffer_.data(), bytes_to_read); | 
|  | 240 | if (bytes_read < 0) { | 
| Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 241 | LOG(ERROR) << "Unable to schedule an asynchronous read from the stream."; | 
|  | 242 | Cleanup(ErrorCode::kError); | 
| Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 243 | } else { | 
|  | 244 | // We could just invoke |OnReadDoneCallback()|, it works. But |PostTask| | 
|  | 245 | // is used so that users can cancel updates. | 
|  | 246 | pending_task_id_ = brillo::MessageLoop::current()->PostTask( | 
|  | 247 | base::Bind(&FilesystemVerifierAction::OnReadDone, | 
|  | 248 | base::Unretained(this), | 
|  | 249 | bytes_read)); | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 250 | } | 
|  | 251 | } | 
|  | 252 |  | 
| Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 253 | void FilesystemVerifierAction::OnReadDone(size_t bytes_read) { | 
| Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 254 | if (cancelled_) { | 
|  | 255 | Cleanup(ErrorCode::kError); | 
|  | 256 | return; | 
|  | 257 | } | 
| Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 258 | if (bytes_read == 0) { | 
| Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 259 | LOG(ERROR) << "Failed to read the remaining " << partition_size_ - offset_ | 
|  | 260 | << " bytes from partition " | 
|  | 261 | << install_plan_.partitions[partition_index_].name; | 
|  | 262 | Cleanup(ErrorCode::kFilesystemVerifierError); | 
|  | 263 | return; | 
|  | 264 | } | 
|  | 265 |  | 
|  | 266 | if (!hasher_->Update(buffer_.data(), bytes_read)) { | 
|  | 267 | LOG(ERROR) << "Unable to update the hash."; | 
|  | 268 | Cleanup(ErrorCode::kError); | 
|  | 269 | return; | 
|  | 270 | } | 
|  | 271 |  | 
| Kelvin Zhang | 70eef23 | 2020-06-12 20:32:40 +0000 | [diff] [blame] | 272 | // WE don't consider sizes of each partition. Every partition | 
|  | 273 | // has the same length on progress bar. | 
|  | 274 | // TODO(zhangkelvin) Take sizes of each partition into account | 
|  | 275 |  | 
|  | 276 | UpdateProgress( | 
|  | 277 | (static_cast<double>(offset_) / partition_size_ + partition_index_) / | 
|  | 278 | install_plan_.partitions.size()); | 
| Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 279 | if (ShouldWriteVerity()) { | 
| Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 280 | if (!verity_writer_->Update(offset_, buffer_.data(), bytes_read)) { | 
|  | 281 | Cleanup(ErrorCode::kVerityCalculationError); | 
| Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 282 | return; | 
|  | 283 | } | 
|  | 284 | } | 
|  | 285 |  | 
| Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 286 | offset_ += bytes_read; | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 287 |  | 
| Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 288 | if (offset_ == partition_size_) { | 
|  | 289 | FinishPartitionHashing(); | 
|  | 290 | return; | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 291 | } | 
| Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 292 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 293 | ScheduleRead(); | 
| Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 294 | } | 
|  | 295 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 296 | void FilesystemVerifierAction::FinishPartitionHashing() { | 
|  | 297 | if (!hasher_->Finalize()) { | 
| Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 298 | LOG(ERROR) << "Unable to finalize the hash."; | 
| Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 299 | Cleanup(ErrorCode::kError); | 
|  | 300 | return; | 
| Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 301 | } | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 302 | InstallPlan::Partition& partition = | 
|  | 303 | install_plan_.partitions[partition_index_]; | 
| Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 304 | LOG(INFO) << "Hash of " << partition.name << ": " | 
|  | 305 | << Base64Encode(hasher_->raw_hash()); | 
| Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 306 |  | 
| Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 307 | switch (verifier_step_) { | 
|  | 308 | case VerifierStep::kVerifyTargetHash: | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 309 | if (partition.target_hash != hasher_->raw_hash()) { | 
|  | 310 | LOG(ERROR) << "New '" << partition.name | 
|  | 311 | << "' partition verification failed."; | 
| Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 312 | if (partition.source_hash.empty()) { | 
|  | 313 | // No need to verify source if it is a full payload. | 
| Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 314 | Cleanup(ErrorCode::kNewRootfsVerificationError); | 
|  | 315 | return; | 
| Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 316 | } | 
| Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 317 | // If we have not verified source partition yet, now that the target | 
| Sen Jiang | 65566a3 | 2016-04-06 13:35:36 -0700 | [diff] [blame] | 318 | // 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] | 319 | // switch to kVerifySourceHash step to check if it's because the | 
|  | 320 | // source partition does not match either. | 
| Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 321 | verifier_step_ = VerifierStep::kVerifySourceHash; | 
| Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame] | 322 | } else { | 
|  | 323 | partition_index_++; | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 324 | } | 
|  | 325 | break; | 
| Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 326 | case VerifierStep::kVerifySourceHash: | 
| Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame] | 327 | if (partition.source_hash != hasher_->raw_hash()) { | 
|  | 328 | LOG(ERROR) << "Old '" << partition.name | 
|  | 329 | << "' partition verification failed."; | 
| Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 330 | LOG(ERROR) << "This is a server-side error due to mismatched delta" | 
|  | 331 | << " update image!"; | 
|  | 332 | LOG(ERROR) << "The delta I've been given contains a " << partition.name | 
|  | 333 | << " delta update that must be applied over a " | 
|  | 334 | << partition.name << " with a specific checksum, but the " | 
|  | 335 | << partition.name | 
|  | 336 | << " we're starting with doesn't have that checksum! This" | 
|  | 337 | " means that the delta I've been given doesn't match my" | 
|  | 338 | " existing system. The " | 
|  | 339 | << partition.name << " partition I have has hash: " | 
| Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 340 | << Base64Encode(hasher_->raw_hash()) | 
| Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 341 | << " but the update expected me to have " | 
| Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 342 | << Base64Encode(partition.source_hash) << " ."; | 
| Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 343 | LOG(INFO) << "To get the checksum of the " << partition.name | 
|  | 344 | << " partition run this command: dd if=" | 
|  | 345 | << partition.source_path | 
|  | 346 | << " bs=1M count=" << partition.source_size | 
|  | 347 | << " iflag=count_bytes 2>/dev/null | openssl dgst -sha256 " | 
|  | 348 | "-binary | openssl base64"; | 
|  | 349 | LOG(INFO) << "To get the checksum of partitions in a bin file, " | 
|  | 350 | << "run: .../src/scripts/sha256_partitions.sh .../file.bin"; | 
| Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 351 | Cleanup(ErrorCode::kDownloadStateInitializationError); | 
|  | 352 | return; | 
| Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame] | 353 | } | 
| Sen Jiang | a35896c | 2016-05-25 11:08:41 -0700 | [diff] [blame] | 354 | // The action will skip kVerifySourceHash step if target partition hash | 
|  | 355 | // 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] | 356 | // and now that the source partition hash matches, we should set the | 
|  | 357 | // error code to reflect the error in target partition. We only need to | 
|  | 358 | // verify the source partition which the target hash does not match, the | 
|  | 359 | // rest of the partitions don't matter. | 
| Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 360 | Cleanup(ErrorCode::kNewRootfsVerificationError); | 
|  | 361 | return; | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 362 | } | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 363 | // Start hashing the next partition, if any. | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 364 | hasher_.reset(); | 
|  | 365 | buffer_.clear(); | 
| Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 366 | if (read_fd_) { | 
|  | 367 | read_fd_.reset(); | 
|  | 368 | } | 
|  | 369 | if (write_fd_) { | 
|  | 370 | write_fd_.reset(); | 
|  | 371 | } | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 372 | StartPartitionHashing(); | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 373 | } | 
|  | 374 |  | 
|  | 375 | }  // namespace chromeos_update_engine |