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