blob: 5edde9ea27caf87f10e1e1ea018550b178266802 [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
Sen Jiang2703ef42017-03-16 13:36:21 -070037using brillo::data_encoding::Base64Encode;
Allie Woodeb9e6d82015-04-17 13:55:30 -070038using std::string;
39
40namespace chromeos_update_engine {
41
42namespace {
Alex Deymo20c99202015-07-09 16:14:16 -070043const off_t kReadFileBufferSize = 128 * 1024;
Allie Woodeb9e6d82015-04-17 13:55:30 -070044} // namespace
45
Allie Woodeb9e6d82015-04-17 13:55:30 -070046void 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 Deymoe5e5fe92015-10-05 09:28:19 -070056 if (install_plan_.partitions.empty()) {
57 LOG(INFO) << "No partitions to verify.";
Allie Woodeb9e6d82015-04-17 13:55:30 -070058 if (HasOutputPipe())
59 SetOutputObject(install_plan_);
60 abort_action_completer.set_code(ErrorCode::kSuccess);
61 return;
62 }
63
Alex Deymoe5e5fe92015-10-05 09:28:19 -070064 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -070065 abort_action_completer.set_should_complete(false);
66}
67
68void FilesystemVerifierAction::TerminateProcessing() {
Alex Deymo20c99202015-07-09 16:14:16 -070069 cancelled_ = true;
70 Cleanup(ErrorCode::kSuccess); // error code is ignored if canceled_ is true.
Allie Woodeb9e6d82015-04-17 13:55:30 -070071}
72
73bool FilesystemVerifierAction::IsCleanupPending() const {
Alex Deymob9e8e262015-08-03 20:23:03 -070074 return src_stream_ != nullptr;
Allie Woodeb9e6d82015-04-17 13:55:30 -070075}
76
77void FilesystemVerifierAction::Cleanup(ErrorCode code) {
Alex Deymob9e8e262015-08-03 20:23:03 -070078 src_stream_.reset();
Alex Deymo20c99202015-07-09 16:14:16 -070079 // This memory is not used anymore.
80 buffer_.clear();
81
Allie Woodeb9e6d82015-04-17 13:55:30 -070082 if (cancelled_)
83 return;
84 if (code == ErrorCode::kSuccess && HasOutputPipe())
85 SetOutputObject(install_plan_);
86 processor_->ActionComplete(this, code);
87}
88
Alex Deymoe5e5fe92015-10-05 09:28:19 -070089void FilesystemVerifierAction::StartPartitionHashing() {
90 if (partition_index_ == install_plan_.partitions.size()) {
Sen Jianga35896c2016-05-25 11:08:41 -070091 Cleanup(ErrorCode::kSuccess);
Alex Deymoe5e5fe92015-10-05 09:28:19 -070092 return;
93 }
94 InstallPlan::Partition& partition =
95 install_plan_.partitions[partition_index_];
96
97 string part_path;
Sen Jiangfef85fd2016-03-25 15:32:49 -070098 switch (verifier_step_) {
99 case VerifierStep::kVerifySourceHash:
Sen Jiange6e4bb92016-04-05 14:59:12 -0700100 part_path = partition.source_path;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700101 remaining_size_ = partition.source_size;
102 break;
Sen Jiangfef85fd2016-03-25 15:32:49 -0700103 case VerifierStep::kVerifyTargetHash:
Sen Jiange6e4bb92016-04-05 14:59:12 -0700104 part_path = partition.target_path;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700105 remaining_size_ = partition.target_size;
106 break;
107 }
108 LOG(INFO) << "Hashing partition " << partition_index_ << " ("
109 << partition.name << ") on device " << part_path;
110 if (part_path.empty())
111 return Cleanup(ErrorCode::kFilesystemVerifierError);
112
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700113 brillo::ErrorPtr error;
114 src_stream_ = brillo::FileStream::Open(
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700115 base::FilePath(part_path),
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700116 brillo::Stream::AccessMode::READ,
117 brillo::FileStream::Disposition::OPEN_EXISTING,
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700118 &error);
119
120 if (!src_stream_) {
121 LOG(ERROR) << "Unable to open " << part_path << " for reading";
122 return Cleanup(ErrorCode::kFilesystemVerifierError);
123 }
124
125 buffer_.resize(kReadFileBufferSize);
126 read_done_ = false;
Alex Deymo39910dc2015-11-09 17:04:30 -0800127 hasher_.reset(new HashCalculator());
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700128
129 // Start the first read.
130 ScheduleRead();
131}
132
Alex Deymob9e8e262015-08-03 20:23:03 -0700133void FilesystemVerifierAction::ScheduleRead() {
Alex Deymo20c99202015-07-09 16:14:16 -0700134 size_t bytes_to_read = std::min(static_cast<int64_t>(buffer_.size()),
135 remaining_size_);
Alex Deymob9e8e262015-08-03 20:23:03 -0700136 if (!bytes_to_read) {
137 OnReadDoneCallback(0);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700138 return;
139 }
140
Alex Deymob9e8e262015-08-03 20:23:03 -0700141 bool read_async_ok = src_stream_->ReadAsync(
142 buffer_.data(),
143 bytes_to_read,
144 base::Bind(&FilesystemVerifierAction::OnReadDoneCallback,
145 base::Unretained(this)),
146 base::Bind(&FilesystemVerifierAction::OnReadErrorCallback,
147 base::Unretained(this)),
148 nullptr);
149
150 if (!read_async_ok) {
151 LOG(ERROR) << "Unable to schedule an asynchronous read from the stream.";
152 Cleanup(ErrorCode::kError);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700153 }
154}
155
Alex Deymob9e8e262015-08-03 20:23:03 -0700156void FilesystemVerifierAction::OnReadDoneCallback(size_t bytes_read) {
157 if (bytes_read == 0) {
158 read_done_ = true;
159 } else {
160 remaining_size_ -= bytes_read;
161 CHECK(!read_done_);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700162 if (!hasher_->Update(buffer_.data(), bytes_read)) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700163 LOG(ERROR) << "Unable to update the hash.";
164 Cleanup(ErrorCode::kError);
165 return;
166 }
167 }
168
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700169 // We either terminate the current partition or have more data to read.
170 if (cancelled_)
171 return Cleanup(ErrorCode::kError);
172
173 if (read_done_ || remaining_size_ == 0) {
174 if (remaining_size_ != 0) {
175 LOG(ERROR) << "Failed to read the remaining " << remaining_size_
176 << " bytes from partition "
177 << install_plan_.partitions[partition_index_].name;
178 return Cleanup(ErrorCode::kFilesystemVerifierError);
179 }
180 return FinishPartitionHashing();
181 }
182 ScheduleRead();
Alex Deymob9e8e262015-08-03 20:23:03 -0700183}
184
185void FilesystemVerifierAction::OnReadErrorCallback(
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700186 const brillo::Error* error) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700187 // TODO(deymo): Transform the read-error into an specific ErrorCode.
188 LOG(ERROR) << "Asynchronous read failed.";
189 Cleanup(ErrorCode::kError);
190}
191
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700192void FilesystemVerifierAction::FinishPartitionHashing() {
193 if (!hasher_->Finalize()) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700194 LOG(ERROR) << "Unable to finalize the hash.";
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700195 return Cleanup(ErrorCode::kError);
Alex Deymob9e8e262015-08-03 20:23:03 -0700196 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700197 InstallPlan::Partition& partition =
198 install_plan_.partitions[partition_index_];
Sen Jiang2703ef42017-03-16 13:36:21 -0700199 LOG(INFO) << "Hash of " << partition.name << ": "
200 << Base64Encode(hasher_->raw_hash());
Alex Deymob9e8e262015-08-03 20:23:03 -0700201
Sen Jiangfef85fd2016-03-25 15:32:49 -0700202 switch (verifier_step_) {
203 case VerifierStep::kVerifyTargetHash:
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700204 if (partition.target_hash != hasher_->raw_hash()) {
205 LOG(ERROR) << "New '" << partition.name
206 << "' partition verification failed.";
Sen Jiangcdd52062017-05-18 15:33:10 -0700207 if (partition.source_hash.empty()) {
208 // No need to verify source if it is a full payload.
Sen Jiang65566a32016-04-06 13:35:36 -0700209 return Cleanup(ErrorCode::kNewRootfsVerificationError);
Sen Jiangcdd52062017-05-18 15:33:10 -0700210 }
Sen Jiangfef85fd2016-03-25 15:32:49 -0700211 // If we have not verified source partition yet, now that the target
Sen Jiang65566a32016-04-06 13:35:36 -0700212 // partition does not match, and it's not a full payload, we need to
213 // switch to kVerifySourceHash step to check if it's because the source
214 // partition does not match either.
Sen Jiangfef85fd2016-03-25 15:32:49 -0700215 verifier_step_ = VerifierStep::kVerifySourceHash;
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800216 } else {
217 partition_index_++;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700218 }
219 break;
Sen Jiangfef85fd2016-03-25 15:32:49 -0700220 case VerifierStep::kVerifySourceHash:
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800221 if (partition.source_hash != hasher_->raw_hash()) {
222 LOG(ERROR) << "Old '" << partition.name
223 << "' partition verification failed.";
Sen Jiangfef85fd2016-03-25 15:32:49 -0700224 LOG(ERROR) << "This is a server-side error due to mismatched delta"
225 << " update image!";
226 LOG(ERROR) << "The delta I've been given contains a " << partition.name
227 << " delta update that must be applied over a "
228 << partition.name << " with a specific checksum, but the "
229 << partition.name
230 << " we're starting with doesn't have that checksum! This"
231 " means that the delta I've been given doesn't match my"
232 " existing system. The "
233 << partition.name << " partition I have has hash: "
Sen Jiang2703ef42017-03-16 13:36:21 -0700234 << Base64Encode(hasher_->raw_hash())
Sen Jiangfef85fd2016-03-25 15:32:49 -0700235 << " but the update expected me to have "
Sen Jiang2703ef42017-03-16 13:36:21 -0700236 << Base64Encode(partition.source_hash) << " .";
Sen Jiangfef85fd2016-03-25 15:32:49 -0700237 LOG(INFO) << "To get the checksum of the " << partition.name
238 << " partition run this command: dd if="
239 << partition.source_path
240 << " bs=1M count=" << partition.source_size
241 << " iflag=count_bytes 2>/dev/null | openssl dgst -sha256 "
242 "-binary | openssl base64";
243 LOG(INFO) << "To get the checksum of partitions in a bin file, "
244 << "run: .../src/scripts/sha256_partitions.sh .../file.bin";
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800245 return Cleanup(ErrorCode::kDownloadStateInitializationError);
246 }
Sen Jianga35896c2016-05-25 11:08:41 -0700247 // The action will skip kVerifySourceHash step if target partition hash
248 // matches, if we are in this step, it means target hash does not match,
249 // and now that the source partition hash matches, we should set the error
250 // code to reflect the error in target partition.
251 // We only need to verify the source partition which the target hash does
252 // not match, the rest of the partitions don't matter.
253 return Cleanup(ErrorCode::kNewRootfsVerificationError);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700254 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700255 // Start hashing the next partition, if any.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700256 hasher_.reset();
257 buffer_.clear();
258 src_stream_->CloseBlocking(nullptr);
259 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700260}
261
262} // namespace chromeos_update_engine