blob: 94af2bbdbf09769537b7e146dc020e071298ec3e [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Woodeb9e6d82015-04-17 13:55:30 -070016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/payload_consumer/filesystem_verifier_action.h"
Allie Woodeb9e6d82015-04-17 13:55:30 -070018
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 Deymo20c99202015-07-09 16:14:16 -070028#include <base/bind.h>
Sen Jiangfef85fd2016-03-25 15:32:49 -070029#include <brillo/data_encoding.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070030#include <brillo/streams/file_stream.h>
Allie Woodeb9e6d82015-04-17 13:55:30 -070031
Alex Deymo39910dc2015-11-09 17:04:30 -080032#include "update_engine/common/boot_control_interface.h"
33#include "update_engine/common/utils.h"
Sen Jiang1ad42ad2015-11-17 15:04:02 -080034#include "update_engine/payload_consumer/delta_performer.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080035#include "update_engine/payload_consumer/payload_constants.h"
Allie Woodeb9e6d82015-04-17 13:55:30 -070036
37using std::string;
38
39namespace chromeos_update_engine {
40
41namespace {
Alex Deymo20c99202015-07-09 16:14:16 -070042const off_t kReadFileBufferSize = 128 * 1024;
Sen Jiangfef85fd2016-03-25 15:32:49 -070043
44string StringForHashBytes(const brillo::Blob& hash) {
45 return brillo::data_encoding::Base64Encode(hash.data(), hash.size());
46}
Allie Woodeb9e6d82015-04-17 13:55:30 -070047} // namespace
48
49FilesystemVerifierAction::FilesystemVerifierAction(
Sen Jiangfef85fd2016-03-25 15:32:49 -070050 const BootControlInterface* boot_control)
51 : boot_control_(boot_control) {}
Allie Woodeb9e6d82015-04-17 13:55:30 -070052
53void FilesystemVerifierAction::PerformAction() {
54 // Will tell the ActionProcessor we've failed if we return.
55 ScopedActionCompleter abort_action_completer(processor_, this);
56
57 if (!HasInputObject()) {
58 LOG(ERROR) << "FilesystemVerifierAction missing input object.";
59 return;
60 }
61 install_plan_ = GetInputObject();
62
Alex Deymoe5e5fe92015-10-05 09:28:19 -070063 if (install_plan_.partitions.empty()) {
64 LOG(INFO) << "No partitions to verify.";
Allie Woodeb9e6d82015-04-17 13:55:30 -070065 if (HasOutputPipe())
66 SetOutputObject(install_plan_);
67 abort_action_completer.set_code(ErrorCode::kSuccess);
68 return;
69 }
70
Alex Deymoe5e5fe92015-10-05 09:28:19 -070071 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -070072 abort_action_completer.set_should_complete(false);
73}
74
75void FilesystemVerifierAction::TerminateProcessing() {
Alex Deymo20c99202015-07-09 16:14:16 -070076 cancelled_ = true;
77 Cleanup(ErrorCode::kSuccess); // error code is ignored if canceled_ is true.
Allie Woodeb9e6d82015-04-17 13:55:30 -070078}
79
80bool FilesystemVerifierAction::IsCleanupPending() const {
Alex Deymob9e8e262015-08-03 20:23:03 -070081 return src_stream_ != nullptr;
Allie Woodeb9e6d82015-04-17 13:55:30 -070082}
83
84void FilesystemVerifierAction::Cleanup(ErrorCode code) {
Alex Deymob9e8e262015-08-03 20:23:03 -070085 src_stream_.reset();
Alex Deymo20c99202015-07-09 16:14:16 -070086 // This memory is not used anymore.
87 buffer_.clear();
88
Allie Woodeb9e6d82015-04-17 13:55:30 -070089 if (cancelled_)
90 return;
91 if (code == ErrorCode::kSuccess && HasOutputPipe())
92 SetOutputObject(install_plan_);
93 processor_->ActionComplete(this, code);
94}
95
Alex Deymoe5e5fe92015-10-05 09:28:19 -070096void FilesystemVerifierAction::StartPartitionHashing() {
97 if (partition_index_ == install_plan_.partitions.size()) {
Sen Jiangfef85fd2016-03-25 15:32:49 -070098 switch (verifier_step_) {
99 case VerifierStep::kVerifySourceHash:
100 // The action will skip kVerifySourceHash step if target partition hash
101 // matches, if we are in this step, it means target hash does not match,
102 // and now that the source hash matches, we should set the error code to
103 // reflect the error in target partition.
104 Cleanup(ErrorCode::kNewRootfsVerificationError);
105 break;
106 case VerifierStep::kVerifyTargetHash:
107 Cleanup(ErrorCode::kSuccess);
108 break;
109 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700110 return;
111 }
112 InstallPlan::Partition& partition =
113 install_plan_.partitions[partition_index_];
114
115 string part_path;
Sen Jiangfef85fd2016-03-25 15:32:49 -0700116 switch (verifier_step_) {
117 case VerifierStep::kVerifySourceHash:
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700118 boot_control_->GetPartitionDevice(
119 partition.name, install_plan_.source_slot, &part_path);
120 remaining_size_ = partition.source_size;
121 break;
Sen Jiangfef85fd2016-03-25 15:32:49 -0700122 case VerifierStep::kVerifyTargetHash:
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700123 boot_control_->GetPartitionDevice(
124 partition.name, install_plan_.target_slot, &part_path);
125 remaining_size_ = partition.target_size;
126 break;
127 }
128 LOG(INFO) << "Hashing partition " << partition_index_ << " ("
129 << partition.name << ") on device " << part_path;
130 if (part_path.empty())
131 return Cleanup(ErrorCode::kFilesystemVerifierError);
132
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700133 brillo::ErrorPtr error;
134 src_stream_ = brillo::FileStream::Open(
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700135 base::FilePath(part_path),
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700136 brillo::Stream::AccessMode::READ,
137 brillo::FileStream::Disposition::OPEN_EXISTING,
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700138 &error);
139
140 if (!src_stream_) {
141 LOG(ERROR) << "Unable to open " << part_path << " for reading";
142 return Cleanup(ErrorCode::kFilesystemVerifierError);
143 }
144
145 buffer_.resize(kReadFileBufferSize);
146 read_done_ = false;
Alex Deymo39910dc2015-11-09 17:04:30 -0800147 hasher_.reset(new HashCalculator());
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700148
149 // Start the first read.
150 ScheduleRead();
151}
152
Alex Deymob9e8e262015-08-03 20:23:03 -0700153void FilesystemVerifierAction::ScheduleRead() {
Alex Deymo20c99202015-07-09 16:14:16 -0700154 size_t bytes_to_read = std::min(static_cast<int64_t>(buffer_.size()),
155 remaining_size_);
Alex Deymob9e8e262015-08-03 20:23:03 -0700156 if (!bytes_to_read) {
157 OnReadDoneCallback(0);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700158 return;
159 }
160
Alex Deymob9e8e262015-08-03 20:23:03 -0700161 bool read_async_ok = src_stream_->ReadAsync(
162 buffer_.data(),
163 bytes_to_read,
164 base::Bind(&FilesystemVerifierAction::OnReadDoneCallback,
165 base::Unretained(this)),
166 base::Bind(&FilesystemVerifierAction::OnReadErrorCallback,
167 base::Unretained(this)),
168 nullptr);
169
170 if (!read_async_ok) {
171 LOG(ERROR) << "Unable to schedule an asynchronous read from the stream.";
172 Cleanup(ErrorCode::kError);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700173 }
174}
175
Alex Deymob9e8e262015-08-03 20:23:03 -0700176void FilesystemVerifierAction::OnReadDoneCallback(size_t bytes_read) {
177 if (bytes_read == 0) {
178 read_done_ = true;
179 } else {
180 remaining_size_ -= bytes_read;
181 CHECK(!read_done_);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700182 if (!hasher_->Update(buffer_.data(), bytes_read)) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700183 LOG(ERROR) << "Unable to update the hash.";
184 Cleanup(ErrorCode::kError);
185 return;
186 }
187 }
188
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700189 // We either terminate the current partition or have more data to read.
190 if (cancelled_)
191 return Cleanup(ErrorCode::kError);
192
193 if (read_done_ || remaining_size_ == 0) {
194 if (remaining_size_ != 0) {
195 LOG(ERROR) << "Failed to read the remaining " << remaining_size_
196 << " bytes from partition "
197 << install_plan_.partitions[partition_index_].name;
198 return Cleanup(ErrorCode::kFilesystemVerifierError);
199 }
200 return FinishPartitionHashing();
201 }
202 ScheduleRead();
Alex Deymob9e8e262015-08-03 20:23:03 -0700203}
204
205void FilesystemVerifierAction::OnReadErrorCallback(
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700206 const brillo::Error* error) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700207 // TODO(deymo): Transform the read-error into an specific ErrorCode.
208 LOG(ERROR) << "Asynchronous read failed.";
209 Cleanup(ErrorCode::kError);
210}
211
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700212void FilesystemVerifierAction::FinishPartitionHashing() {
213 if (!hasher_->Finalize()) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700214 LOG(ERROR) << "Unable to finalize the hash.";
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700215 return Cleanup(ErrorCode::kError);
Alex Deymob9e8e262015-08-03 20:23:03 -0700216 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700217 InstallPlan::Partition& partition =
218 install_plan_.partitions[partition_index_];
219 LOG(INFO) << "Hash of " << partition.name << ": " << hasher_->hash();
Alex Deymob9e8e262015-08-03 20:23:03 -0700220
Sen Jiangfef85fd2016-03-25 15:32:49 -0700221 switch (verifier_step_) {
222 case VerifierStep::kVerifyTargetHash:
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700223 if (partition.target_hash != hasher_->raw_hash()) {
224 LOG(ERROR) << "New '" << partition.name
225 << "' partition verification failed.";
Sen Jiangfef85fd2016-03-25 15:32:49 -0700226 // If we have not verified source partition yet, now that the target
227 // partition does not match, we need to switch to kVerifySourceHash step
228 // to check if it's because the source partition does not match either.
229 verifier_step_ = VerifierStep::kVerifySourceHash;
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800230 partition_index_ = 0;
231 } else {
232 partition_index_++;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700233 }
234 break;
Sen Jiangfef85fd2016-03-25 15:32:49 -0700235 case VerifierStep::kVerifySourceHash:
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800236 if (partition.source_hash != hasher_->raw_hash()) {
237 LOG(ERROR) << "Old '" << partition.name
238 << "' partition verification failed.";
Sen Jiangfef85fd2016-03-25 15:32:49 -0700239 LOG(ERROR) << "This is a server-side error due to mismatched delta"
240 << " update image!";
241 LOG(ERROR) << "The delta I've been given contains a " << partition.name
242 << " delta update that must be applied over a "
243 << partition.name << " with a specific checksum, but the "
244 << partition.name
245 << " we're starting with doesn't have that checksum! This"
246 " means that the delta I've been given doesn't match my"
247 " existing system. The "
248 << partition.name << " partition I have has hash: "
249 << StringForHashBytes(hasher_->raw_hash())
250 << " but the update expected me to have "
251 << StringForHashBytes(partition.source_hash) << " .";
252 LOG(INFO) << "To get the checksum of the " << partition.name
253 << " partition run this command: dd if="
254 << partition.source_path
255 << " bs=1M count=" << partition.source_size
256 << " iflag=count_bytes 2>/dev/null | openssl dgst -sha256 "
257 "-binary | openssl base64";
258 LOG(INFO) << "To get the checksum of partitions in a bin file, "
259 << "run: .../src/scripts/sha256_partitions.sh .../file.bin";
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800260 return Cleanup(ErrorCode::kDownloadStateInitializationError);
261 }
262 partition_index_++;
263 break;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700264 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700265 // Start hashing the next partition, if any.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700266 hasher_.reset();
267 buffer_.clear();
268 src_stream_->CloseBlocking(nullptr);
269 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700270}
271
272} // namespace chromeos_update_engine