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> |
Daniel Zheng | bd16b01 | 2022-09-28 23:40:05 +0000 | [diff] [blame] | 27 | #include <functional> |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 28 | #include <memory> |
Kelvin Zhang | 5b14582 | 2022-07-26 10:56:09 -0700 | [diff] [blame] | 29 | #include <numeric> |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 30 | #include <string> |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 31 | #include <utility> |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 32 | |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 33 | #include <base/bind.h> |
Tianjie | 24f9609 | 2020-06-30 12:26:25 -0700 | [diff] [blame] | 34 | #include <base/strings/string_util.h> |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 35 | #include <brillo/data_encoding.h> |
| 36 | #include <brillo/message_loops/message_loop.h> |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 37 | #include <brillo/secure_blob.h> |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 38 | #include <brillo/streams/file_stream.h> |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 39 | |
Kelvin Zhang | ab15beb | 2022-12-05 10:19:52 -0800 | [diff] [blame] | 40 | #include "update_engine/common/error_code.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 41 | #include "update_engine/common/utils.h" |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 42 | #include "update_engine/payload_consumer/file_descriptor.h" |
Kelvin Zhang | ab15beb | 2022-12-05 10:19:52 -0800 | [diff] [blame] | 43 | #include "update_engine/payload_consumer/install_plan.h" |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 44 | |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 45 | using brillo::data_encoding::Base64Encode; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 46 | using std::string; |
| 47 | |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 48 | // On a partition with verity enabled, we expect to see the following format: |
| 49 | // =================================================== |
| 50 | // Normal Filesystem Data |
| 51 | // (this should take most of the space, like over 90%) |
| 52 | // =================================================== |
| 53 | // Hash tree |
| 54 | // ~0.8% (e.g. 16M for 2GB image) |
| 55 | // =================================================== |
| 56 | // FEC data |
| 57 | // ~0.8% |
| 58 | // =================================================== |
| 59 | // Footer |
| 60 | // 4K |
| 61 | // =================================================== |
| 62 | |
| 63 | // For OTA that doesn't do on device verity computation, hash tree and fec data |
| 64 | // are written during DownloadAction as a regular InstallOp, so no special |
| 65 | // handling needed, we can just read the entire partition in 1 go. |
| 66 | |
| 67 | // Verity enabled case: Only Normal FS data is written during download action. |
| 68 | // When hasing the entire partition, we will need to build the hash tree, write |
| 69 | // it to disk, then build FEC, and write it to disk. Therefore, it is important |
| 70 | // that we finish writing hash tree before we attempt to read & hash it. The |
| 71 | // same principal applies to FEC data. |
| 72 | |
| 73 | // |verity_writer_| handles building and |
| 74 | // writing of FEC/HashTree, we just need to be careful when reading. |
| 75 | // Specifically, we must stop at beginning of Hash tree, let |verity_writer_| |
| 76 | // write both hash tree and FEC, then continue reading the remaining part of |
| 77 | // partition. |
| 78 | |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 79 | namespace chromeos_update_engine { |
| 80 | |
| 81 | namespace { |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 82 | const off_t kReadFileBufferSize = 128 * 1024; |
Kelvin Zhang | 1d99ae1 | 2021-05-12 13:29:27 -0400 | [diff] [blame] | 83 | constexpr float kVerityProgressPercent = 0.6; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 84 | } // namespace |
| 85 | |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 86 | void FilesystemVerifierAction::PerformAction() { |
| 87 | // Will tell the ActionProcessor we've failed if we return. |
| 88 | ScopedActionCompleter abort_action_completer(processor_, this); |
| 89 | |
| 90 | if (!HasInputObject()) { |
| 91 | LOG(ERROR) << "FilesystemVerifierAction missing input object."; |
| 92 | return; |
| 93 | } |
| 94 | install_plan_ = GetInputObject(); |
| 95 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 96 | if (install_plan_.partitions.empty()) { |
| 97 | LOG(INFO) << "No partitions to verify."; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 98 | if (HasOutputPipe()) |
| 99 | SetOutputObject(install_plan_); |
| 100 | abort_action_completer.set_code(ErrorCode::kSuccess); |
| 101 | return; |
| 102 | } |
Kelvin Zhang | 5b14582 | 2022-07-26 10:56:09 -0700 | [diff] [blame] | 103 | // partition_weight_[i] = total size of partitions before index i. |
| 104 | partition_weight_.clear(); |
| 105 | partition_weight_.reserve(install_plan_.partitions.size() + 1); |
| 106 | partition_weight_.push_back(0); |
| 107 | for (const auto& part : install_plan_.partitions) { |
| 108 | partition_weight_.push_back(part.target_size); |
| 109 | } |
| 110 | std::partial_sum(partition_weight_.begin(), |
| 111 | partition_weight_.end(), |
| 112 | partition_weight_.begin(), |
| 113 | std::plus<size_t>()); |
| 114 | |
Jae Hoon Kim | 50504d6 | 2020-04-23 14:32:38 -0700 | [diff] [blame] | 115 | install_plan_.Dump(); |
Kelvin Zhang | ab15beb | 2022-12-05 10:19:52 -0800 | [diff] [blame] | 116 | // If we are not writing verity, just map all partitions once at the |
| 117 | // beginning. |
| 118 | // No need to re-map for each partition, because we are not writing any new |
| 119 | // COW data. |
| 120 | if (dynamic_control_->UpdateUsesSnapshotCompression() && |
| 121 | !install_plan_.write_verity) { |
| 122 | dynamic_control_->MapAllPartitions(); |
| 123 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 124 | StartPartitionHashing(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 125 | abort_action_completer.set_should_complete(false); |
| 126 | } |
| 127 | |
| 128 | void FilesystemVerifierAction::TerminateProcessing() { |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 129 | cancelled_ = true; |
| 130 | Cleanup(ErrorCode::kSuccess); // error code is ignored if canceled_ is true. |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 133 | void FilesystemVerifierAction::Cleanup(ErrorCode code) { |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 134 | partition_fd_.reset(); |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 135 | // This memory is not used anymore. |
| 136 | buffer_.clear(); |
| 137 | |
Kelvin Zhang | 9105f4b | 2021-04-26 13:44:49 -0400 | [diff] [blame] | 138 | // If we didn't write verity, partitions were maped. Releaase resource now. |
| 139 | if (!install_plan_.write_verity && |
| 140 | dynamic_control_->UpdateUsesSnapshotCompression()) { |
| 141 | LOG(INFO) << "Not writing verity and VABC is enabled, unmapping all " |
| 142 | "partitions"; |
| 143 | dynamic_control_->UnmapAllPartitions(); |
| 144 | } |
| 145 | |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 146 | if (cancelled_) |
| 147 | return; |
| 148 | if (code == ErrorCode::kSuccess && HasOutputPipe()) |
| 149 | SetOutputObject(install_plan_); |
Kelvin Zhang | 70eef23 | 2020-06-12 20:32:40 +0000 | [diff] [blame] | 150 | UpdateProgress(1.0); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 151 | processor_->ActionComplete(this, code); |
| 152 | } |
| 153 | |
Kelvin Zhang | 70eef23 | 2020-06-12 20:32:40 +0000 | [diff] [blame] | 154 | void FilesystemVerifierAction::UpdateProgress(double progress) { |
| 155 | if (delegate_ != nullptr) { |
| 156 | delegate_->OnVerifyProgressUpdate(progress); |
| 157 | } |
| 158 | } |
| 159 | |
Kelvin Zhang | 8704c83 | 2021-05-10 17:53:14 -0400 | [diff] [blame] | 160 | void FilesystemVerifierAction::UpdatePartitionProgress(double progress) { |
Kelvin Zhang | 5b14582 | 2022-07-26 10:56:09 -0700 | [diff] [blame] | 161 | UpdateProgress((partition_weight_[partition_index_] * (1 - progress) + |
| 162 | partition_weight_[partition_index_ + 1] * progress) / |
| 163 | partition_weight_.back()); |
Kelvin Zhang | 8704c83 | 2021-05-10 17:53:14 -0400 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | bool FilesystemVerifierAction::InitializeFdVABC(bool should_write_verity) { |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 167 | const InstallPlan::Partition& partition = |
| 168 | install_plan_.partitions[partition_index_]; |
| 169 | |
Kelvin Zhang | 8704c83 | 2021-05-10 17:53:14 -0400 | [diff] [blame] | 170 | if (!should_write_verity) { |
| 171 | // In VABC, we cannot map/unmap partitions w/o first closing ALL fds first. |
| 172 | // Since this function might be called inside a ScheduledTask, the closure |
| 173 | // might have a copy of partition_fd_ when executing this function. Which |
| 174 | // means even if we do |partition_fd_.reset()| here, there's a chance that |
| 175 | // underlying fd isn't closed until we return. This is unacceptable, we need |
| 176 | // to close |partition_fd| right away. |
| 177 | if (partition_fd_) { |
| 178 | partition_fd_->Close(); |
| 179 | partition_fd_.reset(); |
| 180 | } |
Kelvin Zhang | 9105f4b | 2021-04-26 13:44:49 -0400 | [diff] [blame] | 181 | // In VABC, if we are not writing verity, just map all partitions, |
| 182 | // and read using regular fd on |postinstall_mount_device| . |
| 183 | // All read will go through snapuserd, which provides a consistent |
| 184 | // view: device will use snapuserd to read partition during boot. |
| 185 | // b/186196758 |
| 186 | // Call UnmapAllPartitions() first, because if we wrote verity before, these |
| 187 | // writes won't be visible to previously opened snapuserd daemon. To ensure |
| 188 | // that we will see the most up to date data from partitions, call Unmap() |
| 189 | // then Map() to re-spin daemon. |
Kelvin Zhang | ab15beb | 2022-12-05 10:19:52 -0800 | [diff] [blame] | 190 | if (install_plan_.write_verity) { |
| 191 | dynamic_control_->UnmapAllPartitions(); |
| 192 | dynamic_control_->MapAllPartitions(); |
| 193 | } |
Kelvin Zhang | 9105f4b | 2021-04-26 13:44:49 -0400 | [diff] [blame] | 194 | return InitializeFd(partition.readonly_target_path); |
| 195 | } |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 196 | partition_fd_ = |
Kelvin Zhang | 21a4991 | 2021-03-12 14:28:33 -0500 | [diff] [blame] | 197 | dynamic_control_->OpenCowFd(partition.name, partition.source_path, true); |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 198 | if (!partition_fd_) { |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 199 | LOG(ERROR) << "OpenCowReader(" << partition.name << ", " |
| 200 | << partition.source_path << ") failed."; |
| 201 | return false; |
| 202 | } |
| 203 | partition_size_ = partition.target_size; |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 204 | return true; |
| 205 | } |
| 206 | |
| 207 | bool FilesystemVerifierAction::InitializeFd(const std::string& part_path) { |
Kelvin Zhang | 1a0ed71 | 2022-01-26 16:09:05 -0800 | [diff] [blame] | 208 | partition_fd_ = std::make_unique<EintrSafeFileDescriptor>(); |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 209 | const bool write_verity = ShouldWriteVerity(); |
| 210 | int flags = write_verity ? O_RDWR : O_RDONLY; |
| 211 | if (!utils::SetBlockDeviceReadOnly(part_path, !write_verity)) { |
| 212 | LOG(WARNING) << "Failed to set block device " << part_path << " as " |
| 213 | << (write_verity ? "writable" : "readonly"); |
| 214 | } |
| 215 | if (!partition_fd_->Open(part_path.c_str(), flags)) { |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 216 | LOG(ERROR) << "Unable to open " << part_path << " for reading."; |
| 217 | return false; |
| 218 | } |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 219 | return true; |
| 220 | } |
| 221 | |
Daniel Zheng | bd16b01 | 2022-09-28 23:40:05 +0000 | [diff] [blame] | 222 | void FilesystemVerifierAction::WriteVerityData(FileDescriptor* fd, |
| 223 | void* buffer, |
| 224 | const size_t buffer_size) { |
| 225 | if (verity_writer_->FECFinished()) { |
| 226 | LOG(INFO) << "EncodeFEC is completed. Resuming other tasks"; |
| 227 | if (dynamic_control_->UpdateUsesSnapshotCompression()) { |
| 228 | // Spin up snapuserd to read fs. |
| 229 | if (!InitializeFdVABC(false)) { |
| 230 | LOG(ERROR) << "Failed to map all partitions"; |
| 231 | Cleanup(ErrorCode::kFilesystemVerifierError); |
| 232 | return; |
| 233 | } |
| 234 | } |
| 235 | HashPartition(0, partition_size_, buffer, buffer_size); |
| 236 | return; |
| 237 | } |
| 238 | if (!verity_writer_->IncrementalFinalize(fd, fd)) { |
| 239 | LOG(ERROR) << "Failed to write verity data"; |
| 240 | Cleanup(ErrorCode::kVerityCalculationError); |
| 241 | } |
| 242 | CHECK(pending_task_id_.PostTask( |
| 243 | FROM_HERE, |
| 244 | base::BindOnce(&FilesystemVerifierAction::WriteVerityData, |
| 245 | base::Unretained(this), |
| 246 | fd, |
| 247 | buffer, |
| 248 | buffer_size))); |
| 249 | } |
| 250 | |
Kelvin Zhang | 8704c83 | 2021-05-10 17:53:14 -0400 | [diff] [blame] | 251 | void FilesystemVerifierAction::WriteVerityAndHashPartition( |
Kelvin Zhang | 8704c83 | 2021-05-10 17:53:14 -0400 | [diff] [blame] | 252 | const off64_t start_offset, |
| 253 | const off64_t end_offset, |
| 254 | void* buffer, |
| 255 | const size_t buffer_size) { |
Kelvin Zhang | 1a0ed71 | 2022-01-26 16:09:05 -0800 | [diff] [blame] | 256 | auto fd = partition_fd_.get(); |
| 257 | TEST_AND_RETURN(fd != nullptr); |
Kelvin Zhang | 8704c83 | 2021-05-10 17:53:14 -0400 | [diff] [blame] | 258 | if (start_offset >= end_offset) { |
| 259 | LOG_IF(WARNING, start_offset > end_offset) |
| 260 | << "start_offset is greater than end_offset : " << start_offset << " > " |
| 261 | << end_offset; |
Daniel Zheng | bd16b01 | 2022-09-28 23:40:05 +0000 | [diff] [blame] | 262 | WriteVerityData(fd, buffer, buffer_size); |
Kelvin Zhang | 8704c83 | 2021-05-10 17:53:14 -0400 | [diff] [blame] | 263 | return; |
| 264 | } |
| 265 | const auto cur_offset = fd->Seek(start_offset, SEEK_SET); |
| 266 | if (cur_offset != start_offset) { |
| 267 | PLOG(ERROR) << "Failed to seek to offset: " << start_offset; |
| 268 | Cleanup(ErrorCode::kVerityCalculationError); |
| 269 | return; |
| 270 | } |
| 271 | const auto read_size = |
| 272 | std::min<size_t>(buffer_size, end_offset - start_offset); |
| 273 | const auto bytes_read = fd->Read(buffer, read_size); |
| 274 | if (bytes_read < 0 || static_cast<size_t>(bytes_read) != read_size) { |
| 275 | PLOG(ERROR) << "Failed to read offset " << start_offset << " expected " |
| 276 | << read_size << " bytes, actual: " << bytes_read; |
| 277 | Cleanup(ErrorCode::kVerityCalculationError); |
| 278 | return; |
| 279 | } |
| 280 | if (!verity_writer_->Update( |
| 281 | start_offset, static_cast<const uint8_t*>(buffer), read_size)) { |
| 282 | LOG(ERROR) << "VerityWriter::Update() failed"; |
| 283 | Cleanup(ErrorCode::kVerityCalculationError); |
| 284 | return; |
| 285 | } |
| 286 | UpdatePartitionProgress((start_offset + bytes_read) * 1.0f / partition_size_ * |
Kelvin Zhang | 1d99ae1 | 2021-05-12 13:29:27 -0400 | [diff] [blame] | 287 | kVerityProgressPercent); |
Kelvin Zhang | 8704c83 | 2021-05-10 17:53:14 -0400 | [diff] [blame] | 288 | CHECK(pending_task_id_.PostTask( |
| 289 | FROM_HERE, |
| 290 | base::BindOnce(&FilesystemVerifierAction::WriteVerityAndHashPartition, |
| 291 | base::Unretained(this), |
Kelvin Zhang | 8704c83 | 2021-05-10 17:53:14 -0400 | [diff] [blame] | 292 | start_offset + bytes_read, |
| 293 | end_offset, |
| 294 | buffer, |
| 295 | buffer_size))); |
| 296 | } |
| 297 | |
Kelvin Zhang | 1a0ed71 | 2022-01-26 16:09:05 -0800 | [diff] [blame] | 298 | void FilesystemVerifierAction::HashPartition(const off64_t start_offset, |
Kelvin Zhang | 8704c83 | 2021-05-10 17:53:14 -0400 | [diff] [blame] | 299 | const off64_t end_offset, |
| 300 | void* buffer, |
| 301 | const size_t buffer_size) { |
Kelvin Zhang | 1a0ed71 | 2022-01-26 16:09:05 -0800 | [diff] [blame] | 302 | auto fd = partition_fd_.get(); |
| 303 | TEST_AND_RETURN(fd != nullptr); |
Kelvin Zhang | 8704c83 | 2021-05-10 17:53:14 -0400 | [diff] [blame] | 304 | if (start_offset >= end_offset) { |
| 305 | LOG_IF(WARNING, start_offset > end_offset) |
| 306 | << "start_offset is greater than end_offset : " << start_offset << " > " |
| 307 | << end_offset; |
| 308 | FinishPartitionHashing(); |
| 309 | return; |
| 310 | } |
| 311 | const auto cur_offset = fd->Seek(start_offset, SEEK_SET); |
| 312 | if (cur_offset != start_offset) { |
| 313 | PLOG(ERROR) << "Failed to seek to offset: " << start_offset; |
| 314 | Cleanup(ErrorCode::kFilesystemVerifierError); |
| 315 | return; |
| 316 | } |
| 317 | const auto read_size = |
| 318 | std::min<size_t>(buffer_size, end_offset - start_offset); |
| 319 | const auto bytes_read = fd->Read(buffer, read_size); |
| 320 | if (bytes_read < 0 || static_cast<size_t>(bytes_read) != read_size) { |
| 321 | PLOG(ERROR) << "Failed to read offset " << start_offset << " expected " |
| 322 | << read_size << " bytes, actual: " << bytes_read; |
| 323 | Cleanup(ErrorCode::kFilesystemVerifierError); |
| 324 | return; |
| 325 | } |
| 326 | if (!hasher_->Update(buffer, read_size)) { |
| 327 | LOG(ERROR) << "Hasher updated failed on offset" << start_offset; |
| 328 | Cleanup(ErrorCode::kFilesystemVerifierError); |
| 329 | return; |
| 330 | } |
| 331 | const auto progress = (start_offset + bytes_read) * 1.0f / partition_size_; |
Kelvin Zhang | 5b14582 | 2022-07-26 10:56:09 -0700 | [diff] [blame] | 332 | // If we are writing verity, then the progress bar will be split between |
| 333 | // verity writes and partition hashing. Otherwise, the entire progress bar is |
| 334 | // dedicated to partition hashing for smooth progress. |
| 335 | if (ShouldWriteVerity()) { |
| 336 | UpdatePartitionProgress(progress * (1 - kVerityProgressPercent) + |
| 337 | kVerityProgressPercent); |
| 338 | } else { |
| 339 | UpdatePartitionProgress(progress); |
| 340 | } |
Kelvin Zhang | 8704c83 | 2021-05-10 17:53:14 -0400 | [diff] [blame] | 341 | CHECK(pending_task_id_.PostTask( |
| 342 | FROM_HERE, |
| 343 | base::BindOnce(&FilesystemVerifierAction::HashPartition, |
| 344 | base::Unretained(this), |
Kelvin Zhang | 8704c83 | 2021-05-10 17:53:14 -0400 | [diff] [blame] | 345 | start_offset + bytes_read, |
| 346 | end_offset, |
| 347 | buffer, |
| 348 | buffer_size))); |
| 349 | } |
| 350 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 351 | void FilesystemVerifierAction::StartPartitionHashing() { |
| 352 | if (partition_index_ == install_plan_.partitions.size()) { |
Tianjie | 24f9609 | 2020-06-30 12:26:25 -0700 | [diff] [blame] | 353 | if (!install_plan_.untouched_dynamic_partitions.empty()) { |
| 354 | LOG(INFO) << "Verifying extents of untouched dynamic partitions [" |
| 355 | << base::JoinString(install_plan_.untouched_dynamic_partitions, |
| 356 | ", ") |
| 357 | << "]"; |
| 358 | if (!dynamic_control_->VerifyExtentsForUntouchedPartitions( |
| 359 | install_plan_.source_slot, |
| 360 | install_plan_.target_slot, |
| 361 | install_plan_.untouched_dynamic_partitions)) { |
| 362 | Cleanup(ErrorCode::kFilesystemVerifierError); |
| 363 | return; |
| 364 | } |
| 365 | } |
| 366 | |
Sen Jiang | a35896c | 2016-05-25 11:08:41 -0700 | [diff] [blame] | 367 | Cleanup(ErrorCode::kSuccess); |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 368 | return; |
| 369 | } |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 370 | const InstallPlan::Partition& partition = |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 371 | install_plan_.partitions[partition_index_]; |
Kelvin Zhang | e012f65 | 2021-05-10 16:00:31 -0400 | [diff] [blame] | 372 | const auto& part_path = GetPartitionPath(); |
| 373 | partition_size_ = GetPartitionSize(); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 374 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 375 | LOG(INFO) << "Hashing partition " << partition_index_ << " (" |
| 376 | << partition.name << ") on device " << part_path; |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 377 | auto success = false; |
Kelvin Zhang | e012f65 | 2021-05-10 16:00:31 -0400 | [diff] [blame] | 378 | if (IsVABC(partition)) { |
Kelvin Zhang | 8704c83 | 2021-05-10 17:53:14 -0400 | [diff] [blame] | 379 | success = InitializeFdVABC(ShouldWriteVerity()); |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 380 | } else { |
| 381 | if (part_path.empty()) { |
| 382 | if (partition_size_ == 0) { |
| 383 | LOG(INFO) << "Skip hashing partition " << partition_index_ << " (" |
| 384 | << partition.name << ") because size is 0."; |
| 385 | partition_index_++; |
| 386 | StartPartitionHashing(); |
| 387 | return; |
| 388 | } |
| 389 | LOG(ERROR) << "Cannot hash partition " << partition_index_ << " (" |
| 390 | << partition.name |
| 391 | << ") because its device path cannot be determined."; |
| 392 | Cleanup(ErrorCode::kFilesystemVerifierError); |
| 393 | return; |
| 394 | } |
| 395 | success = InitializeFd(part_path); |
| 396 | } |
| 397 | if (!success) { |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 398 | Cleanup(ErrorCode::kFilesystemVerifierError); |
| 399 | return; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 400 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 401 | buffer_.resize(kReadFileBufferSize); |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 402 | hasher_ = std::make_unique<HashCalculator>(); |
| 403 | |
| 404 | offset_ = 0; |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 405 | filesystem_data_end_ = partition_size_; |
Kelvin Zhang | 5e5ad39 | 2021-07-26 21:42:06 -0400 | [diff] [blame] | 406 | if (partition.fec_offset > 0) { |
| 407 | CHECK_LE(partition.hash_tree_offset, partition.fec_offset) |
| 408 | << " Hash tree is expected to come before FEC data"; |
| 409 | } |
Kelvin Zhang | 1a0ed71 | 2022-01-26 16:09:05 -0800 | [diff] [blame] | 410 | CHECK_NE(partition_fd_, nullptr); |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 411 | if (partition.hash_tree_offset != 0) { |
| 412 | filesystem_data_end_ = partition.hash_tree_offset; |
| 413 | } else if (partition.fec_offset != 0) { |
| 414 | filesystem_data_end_ = partition.fec_offset; |
| 415 | } |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 416 | if (ShouldWriteVerity()) { |
Kelvin Zhang | 8704c83 | 2021-05-10 17:53:14 -0400 | [diff] [blame] | 417 | LOG(INFO) << "Verity writes enabled on partition " << partition.name; |
Kelvin Zhang | 7f92567 | 2021-03-15 13:37:40 -0400 | [diff] [blame] | 418 | if (!verity_writer_->Init(partition)) { |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 419 | LOG(INFO) << "Verity writes enabled on partition " << partition.name; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 420 | Cleanup(ErrorCode::kVerityCalculationError); |
| 421 | return; |
| 422 | } |
Kelvin Zhang | 8704c83 | 2021-05-10 17:53:14 -0400 | [diff] [blame] | 423 | WriteVerityAndHashPartition( |
Kelvin Zhang | 1a0ed71 | 2022-01-26 16:09:05 -0800 | [diff] [blame] | 424 | 0, filesystem_data_end_, buffer_.data(), buffer_.size()); |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 425 | } else { |
| 426 | LOG(INFO) << "Verity writes disabled on partition " << partition.name; |
Kelvin Zhang | 1a0ed71 | 2022-01-26 16:09:05 -0800 | [diff] [blame] | 427 | HashPartition(0, partition_size_, buffer_.data(), buffer_.size()); |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 428 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Kelvin Zhang | e012f65 | 2021-05-10 16:00:31 -0400 | [diff] [blame] | 431 | bool FilesystemVerifierAction::IsVABC( |
| 432 | const InstallPlan::Partition& partition) const { |
| 433 | return dynamic_control_->UpdateUsesSnapshotCompression() && |
| 434 | verifier_step_ == VerifierStep::kVerifyTargetHash && |
| 435 | dynamic_control_->IsDynamicPartition(partition.name, |
| 436 | install_plan_.target_slot); |
| 437 | } |
| 438 | |
| 439 | const std::string& FilesystemVerifierAction::GetPartitionPath() const { |
| 440 | const InstallPlan::Partition& partition = |
| 441 | install_plan_.partitions[partition_index_]; |
| 442 | switch (verifier_step_) { |
| 443 | case VerifierStep::kVerifySourceHash: |
| 444 | return partition.source_path; |
| 445 | case VerifierStep::kVerifyTargetHash: |
| 446 | if (IsVABC(partition)) { |
| 447 | return partition.readonly_target_path; |
| 448 | } else { |
| 449 | return partition.target_path; |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | size_t FilesystemVerifierAction::GetPartitionSize() const { |
| 455 | const InstallPlan::Partition& partition = |
| 456 | install_plan_.partitions[partition_index_]; |
| 457 | switch (verifier_step_) { |
| 458 | case VerifierStep::kVerifySourceHash: |
| 459 | return partition.source_size; |
| 460 | case VerifierStep::kVerifyTargetHash: |
| 461 | return partition.target_size; |
| 462 | } |
| 463 | } |
| 464 | |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 465 | bool FilesystemVerifierAction::ShouldWriteVerity() { |
| 466 | const InstallPlan::Partition& partition = |
| 467 | install_plan_.partitions[partition_index_]; |
| 468 | return verifier_step_ == VerifierStep::kVerifyTargetHash && |
| 469 | install_plan_.write_verity && |
| 470 | (partition.hash_tree_size > 0 || partition.fec_size > 0); |
| 471 | } |
| 472 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 473 | void FilesystemVerifierAction::FinishPartitionHashing() { |
| 474 | if (!hasher_->Finalize()) { |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 475 | LOG(ERROR) << "Unable to finalize the hash."; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 476 | Cleanup(ErrorCode::kError); |
| 477 | return; |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 478 | } |
Kelvin Zhang | 1a0ed71 | 2022-01-26 16:09:05 -0800 | [diff] [blame] | 479 | const InstallPlan::Partition& partition = |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 480 | install_plan_.partitions[partition_index_]; |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 481 | LOG(INFO) << "Hash of " << partition.name << ": " |
Kelvin Zhang | 3fe4964 | 2021-10-04 15:35:02 -0700 | [diff] [blame] | 482 | << HexEncode(hasher_->raw_hash()); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 483 | |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 484 | switch (verifier_step_) { |
| 485 | case VerifierStep::kVerifyTargetHash: |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 486 | if (partition.target_hash != hasher_->raw_hash()) { |
| 487 | LOG(ERROR) << "New '" << partition.name |
| 488 | << "' partition verification failed."; |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 489 | if (partition.source_hash.empty()) { |
| 490 | // No need to verify source if it is a full payload. |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 491 | Cleanup(ErrorCode::kNewRootfsVerificationError); |
| 492 | return; |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 493 | } |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 494 | // If we have not verified source partition yet, now that the target |
Sen Jiang | 65566a3 | 2016-04-06 13:35:36 -0700 | [diff] [blame] | 495 | // 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] | 496 | // switch to kVerifySourceHash step to check if it's because the |
| 497 | // source partition does not match either. |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 498 | verifier_step_ = VerifierStep::kVerifySourceHash; |
Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame] | 499 | } else { |
| 500 | partition_index_++; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 501 | } |
| 502 | break; |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 503 | case VerifierStep::kVerifySourceHash: |
Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame] | 504 | if (partition.source_hash != hasher_->raw_hash()) { |
| 505 | LOG(ERROR) << "Old '" << partition.name |
| 506 | << "' partition verification failed."; |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 507 | LOG(ERROR) << "This is a server-side error due to mismatched delta" |
| 508 | << " update image!"; |
| 509 | LOG(ERROR) << "The delta I've been given contains a " << partition.name |
| 510 | << " delta update that must be applied over a " |
| 511 | << partition.name << " with a specific checksum, but the " |
| 512 | << partition.name |
| 513 | << " we're starting with doesn't have that checksum! This" |
| 514 | " means that the delta I've been given doesn't match my" |
| 515 | " existing system. The " |
| 516 | << partition.name << " partition I have has hash: " |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 517 | << Base64Encode(hasher_->raw_hash()) |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 518 | << " but the update expected me to have " |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 519 | << Base64Encode(partition.source_hash) << " ."; |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 520 | LOG(INFO) << "To get the checksum of the " << partition.name |
| 521 | << " partition run this command: dd if=" |
| 522 | << partition.source_path |
| 523 | << " bs=1M count=" << partition.source_size |
| 524 | << " iflag=count_bytes 2>/dev/null | openssl dgst -sha256 " |
| 525 | "-binary | openssl base64"; |
| 526 | LOG(INFO) << "To get the checksum of partitions in a bin file, " |
| 527 | << "run: .../src/scripts/sha256_partitions.sh .../file.bin"; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 528 | Cleanup(ErrorCode::kDownloadStateInitializationError); |
| 529 | return; |
Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame] | 530 | } |
Sen Jiang | a35896c | 2016-05-25 11:08:41 -0700 | [diff] [blame] | 531 | // The action will skip kVerifySourceHash step if target partition hash |
| 532 | // 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] | 533 | // and now that the source partition hash matches, we should set the |
| 534 | // error code to reflect the error in target partition. We only need to |
| 535 | // verify the source partition which the target hash does not match, the |
| 536 | // rest of the partitions don't matter. |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 537 | Cleanup(ErrorCode::kNewRootfsVerificationError); |
| 538 | return; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 539 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 540 | // Start hashing the next partition, if any. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 541 | buffer_.clear(); |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 542 | if (partition_fd_) { |
Kelvin Zhang | 8704c83 | 2021-05-10 17:53:14 -0400 | [diff] [blame] | 543 | partition_fd_->Close(); |
Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 544 | partition_fd_.reset(); |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 545 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 546 | StartPartitionHashing(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | } // namespace chromeos_update_engine |