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> |
| 23 | |
| 24 | #include <algorithm> |
| 25 | #include <cstdlib> |
| 26 | #include <string> |
| 27 | |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 28 | #include <base/bind.h> |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 29 | #include <brillo/streams/file_stream.h> |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 30 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 31 | #include "update_engine/common/boot_control_interface.h" |
| 32 | #include "update_engine/common/utils.h" |
Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame] | 33 | #include "update_engine/payload_consumer/delta_performer.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 34 | #include "update_engine/payload_consumer/payload_constants.h" |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 35 | |
| 36 | using std::string; |
| 37 | |
| 38 | namespace chromeos_update_engine { |
| 39 | |
| 40 | namespace { |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 41 | const off_t kReadFileBufferSize = 128 * 1024; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 42 | } // namespace |
| 43 | |
| 44 | FilesystemVerifierAction::FilesystemVerifierAction( |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 45 | const BootControlInterface* boot_control, |
| 46 | VerifierMode verifier_mode) |
| 47 | : verifier_mode_(verifier_mode), |
| 48 | boot_control_(boot_control) {} |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 49 | |
| 50 | void FilesystemVerifierAction::PerformAction() { |
| 51 | // Will tell the ActionProcessor we've failed if we return. |
| 52 | ScopedActionCompleter abort_action_completer(processor_, this); |
| 53 | |
| 54 | if (!HasInputObject()) { |
| 55 | LOG(ERROR) << "FilesystemVerifierAction missing input object."; |
| 56 | return; |
| 57 | } |
| 58 | install_plan_ = GetInputObject(); |
| 59 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 60 | if (install_plan_.partitions.empty()) { |
| 61 | LOG(INFO) << "No partitions to verify."; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 62 | if (HasOutputPipe()) |
| 63 | SetOutputObject(install_plan_); |
| 64 | abort_action_completer.set_code(ErrorCode::kSuccess); |
| 65 | return; |
| 66 | } |
| 67 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 68 | StartPartitionHashing(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 69 | abort_action_completer.set_should_complete(false); |
| 70 | } |
| 71 | |
| 72 | void FilesystemVerifierAction::TerminateProcessing() { |
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 | |
| 77 | bool FilesystemVerifierAction::IsCleanupPending() const { |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 78 | return src_stream_ != nullptr; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void FilesystemVerifierAction::Cleanup(ErrorCode code) { |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 82 | src_stream_.reset(); |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 83 | // This memory is not used anymore. |
| 84 | buffer_.clear(); |
| 85 | |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 86 | if (cancelled_) |
| 87 | return; |
| 88 | if (code == ErrorCode::kSuccess && HasOutputPipe()) |
| 89 | SetOutputObject(install_plan_); |
| 90 | processor_->ActionComplete(this, code); |
| 91 | } |
| 92 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 93 | void FilesystemVerifierAction::StartPartitionHashing() { |
| 94 | if (partition_index_ == install_plan_.partitions.size()) { |
Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame] | 95 | // We never called this action with kVerifySourceHash directly, if we are in |
| 96 | // this mode, it means the target partition verification has failed, so we |
| 97 | // should set the error code to reflect the error in target. |
| 98 | if (verifier_mode_ == VerifierMode::kVerifySourceHash) |
| 99 | Cleanup(ErrorCode::kNewRootfsVerificationError); |
| 100 | else |
| 101 | Cleanup(ErrorCode::kSuccess); |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 102 | return; |
| 103 | } |
| 104 | InstallPlan::Partition& partition = |
| 105 | install_plan_.partitions[partition_index_]; |
| 106 | |
| 107 | string part_path; |
| 108 | switch (verifier_mode_) { |
| 109 | case VerifierMode::kComputeSourceHash: |
Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame] | 110 | case VerifierMode::kVerifySourceHash: |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 111 | boot_control_->GetPartitionDevice( |
| 112 | partition.name, install_plan_.source_slot, &part_path); |
| 113 | remaining_size_ = partition.source_size; |
| 114 | break; |
| 115 | case VerifierMode::kVerifyTargetHash: |
| 116 | boot_control_->GetPartitionDevice( |
| 117 | partition.name, install_plan_.target_slot, &part_path); |
| 118 | remaining_size_ = partition.target_size; |
| 119 | break; |
| 120 | } |
| 121 | LOG(INFO) << "Hashing partition " << partition_index_ << " (" |
| 122 | << partition.name << ") on device " << part_path; |
| 123 | if (part_path.empty()) |
| 124 | return Cleanup(ErrorCode::kFilesystemVerifierError); |
| 125 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 126 | brillo::ErrorPtr error; |
| 127 | src_stream_ = brillo::FileStream::Open( |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 128 | base::FilePath(part_path), |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 129 | brillo::Stream::AccessMode::READ, |
| 130 | brillo::FileStream::Disposition::OPEN_EXISTING, |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 131 | &error); |
| 132 | |
| 133 | if (!src_stream_) { |
| 134 | LOG(ERROR) << "Unable to open " << part_path << " for reading"; |
| 135 | return Cleanup(ErrorCode::kFilesystemVerifierError); |
| 136 | } |
| 137 | |
| 138 | buffer_.resize(kReadFileBufferSize); |
| 139 | read_done_ = false; |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 140 | hasher_.reset(new HashCalculator()); |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 141 | |
| 142 | // Start the first read. |
| 143 | ScheduleRead(); |
| 144 | } |
| 145 | |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 146 | void FilesystemVerifierAction::ScheduleRead() { |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 147 | size_t bytes_to_read = std::min(static_cast<int64_t>(buffer_.size()), |
| 148 | remaining_size_); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 149 | if (!bytes_to_read) { |
| 150 | OnReadDoneCallback(0); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 151 | return; |
| 152 | } |
| 153 | |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 154 | bool read_async_ok = src_stream_->ReadAsync( |
| 155 | buffer_.data(), |
| 156 | bytes_to_read, |
| 157 | base::Bind(&FilesystemVerifierAction::OnReadDoneCallback, |
| 158 | base::Unretained(this)), |
| 159 | base::Bind(&FilesystemVerifierAction::OnReadErrorCallback, |
| 160 | base::Unretained(this)), |
| 161 | nullptr); |
| 162 | |
| 163 | if (!read_async_ok) { |
| 164 | LOG(ERROR) << "Unable to schedule an asynchronous read from the stream."; |
| 165 | Cleanup(ErrorCode::kError); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 169 | void FilesystemVerifierAction::OnReadDoneCallback(size_t bytes_read) { |
| 170 | if (bytes_read == 0) { |
| 171 | read_done_ = true; |
| 172 | } else { |
| 173 | remaining_size_ -= bytes_read; |
| 174 | CHECK(!read_done_); |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 175 | if (!hasher_->Update(buffer_.data(), bytes_read)) { |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 176 | LOG(ERROR) << "Unable to update the hash."; |
| 177 | Cleanup(ErrorCode::kError); |
| 178 | return; |
| 179 | } |
| 180 | } |
| 181 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 182 | // We either terminate the current partition or have more data to read. |
| 183 | if (cancelled_) |
| 184 | return Cleanup(ErrorCode::kError); |
| 185 | |
| 186 | if (read_done_ || remaining_size_ == 0) { |
| 187 | if (remaining_size_ != 0) { |
| 188 | LOG(ERROR) << "Failed to read the remaining " << remaining_size_ |
| 189 | << " bytes from partition " |
| 190 | << install_plan_.partitions[partition_index_].name; |
| 191 | return Cleanup(ErrorCode::kFilesystemVerifierError); |
| 192 | } |
| 193 | return FinishPartitionHashing(); |
| 194 | } |
| 195 | ScheduleRead(); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void FilesystemVerifierAction::OnReadErrorCallback( |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 199 | const brillo::Error* error) { |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 200 | // TODO(deymo): Transform the read-error into an specific ErrorCode. |
| 201 | LOG(ERROR) << "Asynchronous read failed."; |
| 202 | Cleanup(ErrorCode::kError); |
| 203 | } |
| 204 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 205 | void FilesystemVerifierAction::FinishPartitionHashing() { |
| 206 | if (!hasher_->Finalize()) { |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 207 | LOG(ERROR) << "Unable to finalize the hash."; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 208 | return Cleanup(ErrorCode::kError); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 209 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 210 | InstallPlan::Partition& partition = |
| 211 | install_plan_.partitions[partition_index_]; |
| 212 | LOG(INFO) << "Hash of " << partition.name << ": " << hasher_->hash(); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 213 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 214 | switch (verifier_mode_) { |
| 215 | case VerifierMode::kComputeSourceHash: |
| 216 | partition.source_hash = hasher_->raw_hash(); |
Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame] | 217 | partition_index_++; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 218 | break; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 219 | case VerifierMode::kVerifyTargetHash: |
| 220 | if (partition.target_hash != hasher_->raw_hash()) { |
| 221 | LOG(ERROR) << "New '" << partition.name |
| 222 | << "' partition verification failed."; |
Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame] | 223 | if (DeltaPerformer::kSupportedMinorPayloadVersion < |
| 224 | kOpSrcHashMinorPayloadVersion) |
| 225 | return Cleanup(ErrorCode::kNewRootfsVerificationError); |
| 226 | // If we support per-operation source hash, then we skipped source |
| 227 | // filesystem verification, now that the target partition does not |
| 228 | // match, we need to switch to kVerifySourceHash mode to check if it's |
| 229 | // because the source partition does not match either. |
| 230 | verifier_mode_ = VerifierMode::kVerifySourceHash; |
| 231 | partition_index_ = 0; |
| 232 | } else { |
| 233 | partition_index_++; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 234 | } |
| 235 | break; |
Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame] | 236 | case VerifierMode::kVerifySourceHash: |
| 237 | if (partition.source_hash != hasher_->raw_hash()) { |
| 238 | LOG(ERROR) << "Old '" << partition.name |
| 239 | << "' partition verification failed."; |
| 240 | return Cleanup(ErrorCode::kDownloadStateInitializationError); |
| 241 | } |
| 242 | partition_index_++; |
| 243 | break; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 244 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 245 | // Start hashing the next partition, if any. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 246 | hasher_.reset(); |
| 247 | buffer_.clear(); |
| 248 | src_stream_->CloseBlocking(nullptr); |
| 249 | StartPartitionHashing(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | } // namespace chromeos_update_engine |