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> |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 29 | #include <brillo/data_encoding.h> |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 30 | #include <brillo/streams/file_stream.h> |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 31 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 32 | #include "update_engine/common/boot_control_interface.h" |
| 33 | #include "update_engine/common/utils.h" |
Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame] | 34 | #include "update_engine/payload_consumer/delta_performer.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 35 | #include "update_engine/payload_consumer/payload_constants.h" |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 36 | |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 37 | using brillo::data_encoding::Base64Encode; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 38 | using std::string; |
| 39 | |
| 40 | namespace chromeos_update_engine { |
| 41 | |
| 42 | namespace { |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 43 | const off_t kReadFileBufferSize = 128 * 1024; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 44 | } // namespace |
| 45 | |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 46 | void FilesystemVerifierAction::PerformAction() { |
| 47 | // Will tell the ActionProcessor we've failed if we return. |
| 48 | ScopedActionCompleter abort_action_completer(processor_, this); |
| 49 | |
| 50 | if (!HasInputObject()) { |
| 51 | LOG(ERROR) << "FilesystemVerifierAction missing input object."; |
| 52 | return; |
| 53 | } |
| 54 | install_plan_ = GetInputObject(); |
| 55 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 56 | if (install_plan_.partitions.empty()) { |
| 57 | LOG(INFO) << "No partitions to verify."; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 58 | if (HasOutputPipe()) |
| 59 | SetOutputObject(install_plan_); |
| 60 | abort_action_completer.set_code(ErrorCode::kSuccess); |
| 61 | return; |
| 62 | } |
| 63 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 64 | StartPartitionHashing(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 65 | abort_action_completer.set_should_complete(false); |
| 66 | } |
| 67 | |
| 68 | void FilesystemVerifierAction::TerminateProcessing() { |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 69 | cancelled_ = true; |
| 70 | Cleanup(ErrorCode::kSuccess); // error code is ignored if canceled_ is true. |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 73 | void FilesystemVerifierAction::Cleanup(ErrorCode code) { |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 74 | src_stream_.reset(); |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 75 | // This memory is not used anymore. |
| 76 | buffer_.clear(); |
| 77 | |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 78 | if (cancelled_) |
| 79 | return; |
| 80 | if (code == ErrorCode::kSuccess && HasOutputPipe()) |
| 81 | SetOutputObject(install_plan_); |
| 82 | processor_->ActionComplete(this, code); |
| 83 | } |
| 84 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 85 | void FilesystemVerifierAction::StartPartitionHashing() { |
| 86 | if (partition_index_ == install_plan_.partitions.size()) { |
Sen Jiang | a35896c | 2016-05-25 11:08:41 -0700 | [diff] [blame] | 87 | Cleanup(ErrorCode::kSuccess); |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 88 | return; |
| 89 | } |
| 90 | InstallPlan::Partition& partition = |
| 91 | install_plan_.partitions[partition_index_]; |
| 92 | |
| 93 | string part_path; |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 94 | switch (verifier_step_) { |
| 95 | case VerifierStep::kVerifySourceHash: |
Sen Jiang | e6e4bb9 | 2016-04-05 14:59:12 -0700 | [diff] [blame] | 96 | part_path = partition.source_path; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 97 | remaining_size_ = partition.source_size; |
| 98 | break; |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 99 | case VerifierStep::kVerifyTargetHash: |
Sen Jiang | e6e4bb9 | 2016-04-05 14:59:12 -0700 | [diff] [blame] | 100 | part_path = partition.target_path; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 101 | remaining_size_ = partition.target_size; |
| 102 | break; |
| 103 | } |
| 104 | LOG(INFO) << "Hashing partition " << partition_index_ << " (" |
| 105 | << partition.name << ") on device " << part_path; |
| 106 | if (part_path.empty()) |
| 107 | return Cleanup(ErrorCode::kFilesystemVerifierError); |
| 108 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 109 | brillo::ErrorPtr error; |
| 110 | src_stream_ = brillo::FileStream::Open( |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 111 | base::FilePath(part_path), |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 112 | brillo::Stream::AccessMode::READ, |
| 113 | brillo::FileStream::Disposition::OPEN_EXISTING, |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 114 | &error); |
| 115 | |
| 116 | if (!src_stream_) { |
| 117 | LOG(ERROR) << "Unable to open " << part_path << " for reading"; |
| 118 | return Cleanup(ErrorCode::kFilesystemVerifierError); |
| 119 | } |
| 120 | |
| 121 | buffer_.resize(kReadFileBufferSize); |
| 122 | read_done_ = false; |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 123 | hasher_.reset(new HashCalculator()); |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 124 | |
| 125 | // Start the first read. |
| 126 | ScheduleRead(); |
| 127 | } |
| 128 | |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 129 | void FilesystemVerifierAction::ScheduleRead() { |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 130 | size_t bytes_to_read = std::min(static_cast<int64_t>(buffer_.size()), |
| 131 | remaining_size_); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 132 | if (!bytes_to_read) { |
| 133 | OnReadDoneCallback(0); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 134 | return; |
| 135 | } |
| 136 | |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 137 | bool read_async_ok = src_stream_->ReadAsync( |
| 138 | buffer_.data(), |
| 139 | bytes_to_read, |
| 140 | base::Bind(&FilesystemVerifierAction::OnReadDoneCallback, |
| 141 | base::Unretained(this)), |
| 142 | base::Bind(&FilesystemVerifierAction::OnReadErrorCallback, |
| 143 | base::Unretained(this)), |
| 144 | nullptr); |
| 145 | |
| 146 | if (!read_async_ok) { |
| 147 | LOG(ERROR) << "Unable to schedule an asynchronous read from the stream."; |
| 148 | Cleanup(ErrorCode::kError); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 152 | void FilesystemVerifierAction::OnReadDoneCallback(size_t bytes_read) { |
| 153 | if (bytes_read == 0) { |
| 154 | read_done_ = true; |
| 155 | } else { |
| 156 | remaining_size_ -= bytes_read; |
| 157 | CHECK(!read_done_); |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 158 | if (!hasher_->Update(buffer_.data(), bytes_read)) { |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 159 | LOG(ERROR) << "Unable to update the hash."; |
| 160 | Cleanup(ErrorCode::kError); |
| 161 | return; |
| 162 | } |
| 163 | } |
| 164 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 165 | // We either terminate the current partition or have more data to read. |
| 166 | if (cancelled_) |
| 167 | return Cleanup(ErrorCode::kError); |
| 168 | |
| 169 | if (read_done_ || remaining_size_ == 0) { |
| 170 | if (remaining_size_ != 0) { |
| 171 | LOG(ERROR) << "Failed to read the remaining " << remaining_size_ |
| 172 | << " bytes from partition " |
| 173 | << install_plan_.partitions[partition_index_].name; |
| 174 | return Cleanup(ErrorCode::kFilesystemVerifierError); |
| 175 | } |
| 176 | return FinishPartitionHashing(); |
| 177 | } |
| 178 | ScheduleRead(); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | void FilesystemVerifierAction::OnReadErrorCallback( |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 182 | const brillo::Error* error) { |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 183 | // TODO(deymo): Transform the read-error into an specific ErrorCode. |
| 184 | LOG(ERROR) << "Asynchronous read failed."; |
| 185 | Cleanup(ErrorCode::kError); |
| 186 | } |
| 187 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 188 | void FilesystemVerifierAction::FinishPartitionHashing() { |
| 189 | if (!hasher_->Finalize()) { |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 190 | LOG(ERROR) << "Unable to finalize the hash."; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 191 | return Cleanup(ErrorCode::kError); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 192 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 193 | InstallPlan::Partition& partition = |
| 194 | install_plan_.partitions[partition_index_]; |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 195 | LOG(INFO) << "Hash of " << partition.name << ": " |
| 196 | << Base64Encode(hasher_->raw_hash()); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 197 | |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 198 | switch (verifier_step_) { |
| 199 | case VerifierStep::kVerifyTargetHash: |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 200 | if (partition.target_hash != hasher_->raw_hash()) { |
| 201 | LOG(ERROR) << "New '" << partition.name |
| 202 | << "' partition verification failed."; |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 203 | if (partition.source_hash.empty()) { |
| 204 | // No need to verify source if it is a full payload. |
Sen Jiang | 65566a3 | 2016-04-06 13:35:36 -0700 | [diff] [blame] | 205 | return Cleanup(ErrorCode::kNewRootfsVerificationError); |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 206 | } |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 207 | // If we have not verified source partition yet, now that the target |
Sen Jiang | 65566a3 | 2016-04-06 13:35:36 -0700 | [diff] [blame] | 208 | // partition does not match, and it's not a full payload, we need to |
| 209 | // switch to kVerifySourceHash step to check if it's because the source |
| 210 | // partition does not match either. |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 211 | verifier_step_ = VerifierStep::kVerifySourceHash; |
Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame] | 212 | } else { |
| 213 | partition_index_++; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 214 | } |
| 215 | break; |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 216 | case VerifierStep::kVerifySourceHash: |
Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame] | 217 | if (partition.source_hash != hasher_->raw_hash()) { |
| 218 | LOG(ERROR) << "Old '" << partition.name |
| 219 | << "' partition verification failed."; |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 220 | LOG(ERROR) << "This is a server-side error due to mismatched delta" |
| 221 | << " update image!"; |
| 222 | LOG(ERROR) << "The delta I've been given contains a " << partition.name |
| 223 | << " delta update that must be applied over a " |
| 224 | << partition.name << " with a specific checksum, but the " |
| 225 | << partition.name |
| 226 | << " we're starting with doesn't have that checksum! This" |
| 227 | " means that the delta I've been given doesn't match my" |
| 228 | " existing system. The " |
| 229 | << partition.name << " partition I have has hash: " |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 230 | << Base64Encode(hasher_->raw_hash()) |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 231 | << " but the update expected me to have " |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 232 | << Base64Encode(partition.source_hash) << " ."; |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 233 | LOG(INFO) << "To get the checksum of the " << partition.name |
| 234 | << " partition run this command: dd if=" |
| 235 | << partition.source_path |
| 236 | << " bs=1M count=" << partition.source_size |
| 237 | << " iflag=count_bytes 2>/dev/null | openssl dgst -sha256 " |
| 238 | "-binary | openssl base64"; |
| 239 | LOG(INFO) << "To get the checksum of partitions in a bin file, " |
| 240 | << "run: .../src/scripts/sha256_partitions.sh .../file.bin"; |
Sen Jiang | 1ad42ad | 2015-11-17 15:04:02 -0800 | [diff] [blame] | 241 | return Cleanup(ErrorCode::kDownloadStateInitializationError); |
| 242 | } |
Sen Jiang | a35896c | 2016-05-25 11:08:41 -0700 | [diff] [blame] | 243 | // The action will skip kVerifySourceHash step if target partition hash |
| 244 | // matches, if we are in this step, it means target hash does not match, |
| 245 | // and now that the source partition hash matches, we should set the error |
| 246 | // code to reflect the error in target partition. |
| 247 | // We only need to verify the source partition which the target hash does |
| 248 | // not match, the rest of the partitions don't matter. |
| 249 | return Cleanup(ErrorCode::kNewRootfsVerificationError); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 250 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 251 | // Start hashing the next partition, if any. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 252 | hasher_.reset(); |
| 253 | buffer_.clear(); |
| 254 | src_stream_->CloseBlocking(nullptr); |
| 255 | StartPartitionHashing(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | } // namespace chromeos_update_engine |