blob: b6affc3c6749d162faac25fad187a449485a7185 [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/utils.h"
Allie Woodeb9e6d82015-04-17 13:55:30 -070033
Sen Jiang2703ef42017-03-16 13:36:21 -070034using brillo::data_encoding::Base64Encode;
Allie Woodeb9e6d82015-04-17 13:55:30 -070035using std::string;
36
37namespace chromeos_update_engine {
38
39namespace {
Alex Deymo20c99202015-07-09 16:14:16 -070040const off_t kReadFileBufferSize = 128 * 1024;
Allie Woodeb9e6d82015-04-17 13:55:30 -070041} // namespace
42
Allie Woodeb9e6d82015-04-17 13:55:30 -070043void FilesystemVerifierAction::PerformAction() {
44 // Will tell the ActionProcessor we've failed if we return.
45 ScopedActionCompleter abort_action_completer(processor_, this);
46
47 if (!HasInputObject()) {
48 LOG(ERROR) << "FilesystemVerifierAction missing input object.";
49 return;
50 }
51 install_plan_ = GetInputObject();
52
Alex Deymoe5e5fe92015-10-05 09:28:19 -070053 if (install_plan_.partitions.empty()) {
54 LOG(INFO) << "No partitions to verify.";
Allie Woodeb9e6d82015-04-17 13:55:30 -070055 if (HasOutputPipe())
56 SetOutputObject(install_plan_);
57 abort_action_completer.set_code(ErrorCode::kSuccess);
58 return;
59 }
60
Alex Deymoe5e5fe92015-10-05 09:28:19 -070061 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -070062 abort_action_completer.set_should_complete(false);
63}
64
65void FilesystemVerifierAction::TerminateProcessing() {
Alex Deymo20c99202015-07-09 16:14:16 -070066 cancelled_ = true;
67 Cleanup(ErrorCode::kSuccess); // error code is ignored if canceled_ is true.
Allie Woodeb9e6d82015-04-17 13:55:30 -070068}
69
Allie Woodeb9e6d82015-04-17 13:55:30 -070070void FilesystemVerifierAction::Cleanup(ErrorCode code) {
Alex Deymob9e8e262015-08-03 20:23:03 -070071 src_stream_.reset();
Alex Deymo20c99202015-07-09 16:14:16 -070072 // This memory is not used anymore.
73 buffer_.clear();
74
Allie Woodeb9e6d82015-04-17 13:55:30 -070075 if (cancelled_)
76 return;
77 if (code == ErrorCode::kSuccess && HasOutputPipe())
78 SetOutputObject(install_plan_);
Kelvin Zhang70eef232020-06-12 20:32:40 +000079 UpdateProgress(1.0);
Allie Woodeb9e6d82015-04-17 13:55:30 -070080 processor_->ActionComplete(this, code);
81}
82
Kelvin Zhang70eef232020-06-12 20:32:40 +000083void FilesystemVerifierAction::UpdateProgress(double progress) {
84 if (delegate_ != nullptr) {
85 delegate_->OnVerifyProgressUpdate(progress);
86 }
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 }
Sen Jiang57f91802017-11-14 17:42:13 -080094 const InstallPlan::Partition& partition =
Alex Deymoe5e5fe92015-10-05 09:28:19 -070095 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;
Sen Jiang57f91802017-11-14 17:42:13 -0800101 partition_size_ = partition.source_size;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700102 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;
Sen Jiang57f91802017-11-14 17:42:13 -0800105 partition_size_ = partition.target_size;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700106 break;
107 }
Yifan Hong537802d2018-08-15 13:15:42 -0700108
Sen Jiang57f91802017-11-14 17:42:13 -0800109 if (part_path.empty()) {
Yifan Hong537802d2018-08-15 13:15:42 -0700110 if (partition_size_ == 0) {
111 LOG(INFO) << "Skip hashing partition " << partition_index_ << " ("
112 << partition.name << ") because size is 0.";
113 partition_index_++;
114 StartPartitionHashing();
115 return;
116 }
117 LOG(ERROR) << "Cannot hash partition " << partition_index_ << " ("
118 << partition.name
119 << ") because its device path cannot be determined.";
Sen Jiang57f91802017-11-14 17:42:13 -0800120 Cleanup(ErrorCode::kFilesystemVerifierError);
121 return;
122 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700123
Yifan Hong537802d2018-08-15 13:15:42 -0700124 LOG(INFO) << "Hashing partition " << partition_index_ << " ("
125 << partition.name << ") on device " << part_path;
126
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700127 brillo::ErrorPtr error;
Amin Hassani008c4582019-01-13 16:22:47 -0800128 src_stream_ =
129 brillo::FileStream::Open(base::FilePath(part_path),
130 brillo::Stream::AccessMode::READ,
131 brillo::FileStream::Disposition::OPEN_EXISTING,
132 &error);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700133
134 if (!src_stream_) {
135 LOG(ERROR) << "Unable to open " << part_path << " for reading";
Sen Jiang57f91802017-11-14 17:42:13 -0800136 Cleanup(ErrorCode::kFilesystemVerifierError);
137 return;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700138 }
139
140 buffer_.resize(kReadFileBufferSize);
Sen Jiang57f91802017-11-14 17:42:13 -0800141 hasher_ = std::make_unique<HashCalculator>();
142
143 offset_ = 0;
Sen Jiang3eeaf7d2018-10-11 13:55:32 -0700144 if (verifier_step_ == VerifierStep::kVerifyTargetHash &&
145 install_plan_.write_verity) {
Sen Jiang57f91802017-11-14 17:42:13 -0800146 if (!verity_writer_->Init(partition)) {
147 Cleanup(ErrorCode::kVerityCalculationError);
148 return;
149 }
150 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700151
152 // Start the first read.
153 ScheduleRead();
154}
155
Alex Deymob9e8e262015-08-03 20:23:03 -0700156void FilesystemVerifierAction::ScheduleRead() {
Sen Jiang57f91802017-11-14 17:42:13 -0800157 const InstallPlan::Partition& partition =
158 install_plan_.partitions[partition_index_];
159
160 // We can only start reading anything past |hash_tree_offset| after we have
161 // already read all the data blocks that the hash tree covers. The same
162 // applies to FEC.
163 uint64_t read_end = partition_size_;
164 if (partition.hash_tree_size != 0 &&
165 offset_ < partition.hash_tree_data_offset + partition.hash_tree_data_size)
166 read_end = std::min(read_end, partition.hash_tree_offset);
167 if (partition.fec_size != 0 &&
168 offset_ < partition.fec_data_offset + partition.fec_data_size)
169 read_end = std::min(read_end, partition.fec_offset);
170 size_t bytes_to_read =
171 std::min(static_cast<uint64_t>(buffer_.size()), read_end - offset_);
Alex Deymob9e8e262015-08-03 20:23:03 -0700172 if (!bytes_to_read) {
Sen Jiang57f91802017-11-14 17:42:13 -0800173 FinishPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700174 return;
175 }
176
Alex Deymob9e8e262015-08-03 20:23:03 -0700177 bool read_async_ok = src_stream_->ReadAsync(
Sen Jiang57f91802017-11-14 17:42:13 -0800178 buffer_.data(),
179 bytes_to_read,
180 base::Bind(&FilesystemVerifierAction::OnReadDoneCallback,
181 base::Unretained(this)),
182 base::Bind(&FilesystemVerifierAction::OnReadErrorCallback,
183 base::Unretained(this)),
184 nullptr);
Alex Deymob9e8e262015-08-03 20:23:03 -0700185
186 if (!read_async_ok) {
187 LOG(ERROR) << "Unable to schedule an asynchronous read from the stream.";
188 Cleanup(ErrorCode::kError);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700189 }
190}
191
Alex Deymob9e8e262015-08-03 20:23:03 -0700192void FilesystemVerifierAction::OnReadDoneCallback(size_t bytes_read) {
Sen Jiang57f91802017-11-14 17:42:13 -0800193 if (cancelled_) {
194 Cleanup(ErrorCode::kError);
195 return;
196 }
Alex Deymob9e8e262015-08-03 20:23:03 -0700197 if (bytes_read == 0) {
Sen Jiang57f91802017-11-14 17:42:13 -0800198 LOG(ERROR) << "Failed to read the remaining " << partition_size_ - offset_
199 << " bytes from partition "
200 << install_plan_.partitions[partition_index_].name;
201 Cleanup(ErrorCode::kFilesystemVerifierError);
202 return;
203 }
204
205 if (!hasher_->Update(buffer_.data(), bytes_read)) {
206 LOG(ERROR) << "Unable to update the hash.";
207 Cleanup(ErrorCode::kError);
208 return;
209 }
210
Kelvin Zhang70eef232020-06-12 20:32:40 +0000211 // WE don't consider sizes of each partition. Every partition
212 // has the same length on progress bar.
213 // TODO(zhangkelvin) Take sizes of each partition into account
214
215 UpdateProgress(
216 (static_cast<double>(offset_) / partition_size_ + partition_index_) /
217 install_plan_.partitions.size());
Sen Jiang3eeaf7d2018-10-11 13:55:32 -0700218 if (verifier_step_ == VerifierStep::kVerifyTargetHash &&
219 install_plan_.write_verity) {
Sen Jiang57f91802017-11-14 17:42:13 -0800220 if (!verity_writer_->Update(offset_, buffer_.data(), bytes_read)) {
221 Cleanup(ErrorCode::kVerityCalculationError);
Alex Deymob9e8e262015-08-03 20:23:03 -0700222 return;
223 }
224 }
225
Sen Jiang57f91802017-11-14 17:42:13 -0800226 offset_ += bytes_read;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700227
Sen Jiang57f91802017-11-14 17:42:13 -0800228 if (offset_ == partition_size_) {
229 FinishPartitionHashing();
230 return;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700231 }
Sen Jiang57f91802017-11-14 17:42:13 -0800232
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700233 ScheduleRead();
Alex Deymob9e8e262015-08-03 20:23:03 -0700234}
235
Amin Hassani008c4582019-01-13 16:22:47 -0800236void FilesystemVerifierAction::OnReadErrorCallback(const brillo::Error* error) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700237 // TODO(deymo): Transform the read-error into an specific ErrorCode.
238 LOG(ERROR) << "Asynchronous read failed.";
239 Cleanup(ErrorCode::kError);
240}
241
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700242void FilesystemVerifierAction::FinishPartitionHashing() {
243 if (!hasher_->Finalize()) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700244 LOG(ERROR) << "Unable to finalize the hash.";
Sen Jiang57f91802017-11-14 17:42:13 -0800245 Cleanup(ErrorCode::kError);
246 return;
Alex Deymob9e8e262015-08-03 20:23:03 -0700247 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700248 InstallPlan::Partition& partition =
249 install_plan_.partitions[partition_index_];
Sen Jiang2703ef42017-03-16 13:36:21 -0700250 LOG(INFO) << "Hash of " << partition.name << ": "
251 << Base64Encode(hasher_->raw_hash());
Alex Deymob9e8e262015-08-03 20:23:03 -0700252
Sen Jiangfef85fd2016-03-25 15:32:49 -0700253 switch (verifier_step_) {
254 case VerifierStep::kVerifyTargetHash:
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700255 if (partition.target_hash != hasher_->raw_hash()) {
256 LOG(ERROR) << "New '" << partition.name
257 << "' partition verification failed.";
Sen Jiangcdd52062017-05-18 15:33:10 -0700258 if (partition.source_hash.empty()) {
259 // No need to verify source if it is a full payload.
Sen Jiang57f91802017-11-14 17:42:13 -0800260 Cleanup(ErrorCode::kNewRootfsVerificationError);
261 return;
Sen Jiangcdd52062017-05-18 15:33:10 -0700262 }
Sen Jiangfef85fd2016-03-25 15:32:49 -0700263 // If we have not verified source partition yet, now that the target
Sen Jiang65566a32016-04-06 13:35:36 -0700264 // partition does not match, and it's not a full payload, we need to
265 // switch to kVerifySourceHash step to check if it's because the source
266 // partition does not match either.
Sen Jiangfef85fd2016-03-25 15:32:49 -0700267 verifier_step_ = VerifierStep::kVerifySourceHash;
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800268 } else {
269 partition_index_++;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700270 }
271 break;
Sen Jiangfef85fd2016-03-25 15:32:49 -0700272 case VerifierStep::kVerifySourceHash:
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800273 if (partition.source_hash != hasher_->raw_hash()) {
274 LOG(ERROR) << "Old '" << partition.name
275 << "' partition verification failed.";
Sen Jiangfef85fd2016-03-25 15:32:49 -0700276 LOG(ERROR) << "This is a server-side error due to mismatched delta"
277 << " update image!";
278 LOG(ERROR) << "The delta I've been given contains a " << partition.name
279 << " delta update that must be applied over a "
280 << partition.name << " with a specific checksum, but the "
281 << partition.name
282 << " we're starting with doesn't have that checksum! This"
283 " means that the delta I've been given doesn't match my"
284 " existing system. The "
285 << partition.name << " partition I have has hash: "
Sen Jiang2703ef42017-03-16 13:36:21 -0700286 << Base64Encode(hasher_->raw_hash())
Sen Jiangfef85fd2016-03-25 15:32:49 -0700287 << " but the update expected me to have "
Sen Jiang2703ef42017-03-16 13:36:21 -0700288 << Base64Encode(partition.source_hash) << " .";
Sen Jiangfef85fd2016-03-25 15:32:49 -0700289 LOG(INFO) << "To get the checksum of the " << partition.name
290 << " partition run this command: dd if="
291 << partition.source_path
292 << " bs=1M count=" << partition.source_size
293 << " iflag=count_bytes 2>/dev/null | openssl dgst -sha256 "
294 "-binary | openssl base64";
295 LOG(INFO) << "To get the checksum of partitions in a bin file, "
296 << "run: .../src/scripts/sha256_partitions.sh .../file.bin";
Sen Jiang57f91802017-11-14 17:42:13 -0800297 Cleanup(ErrorCode::kDownloadStateInitializationError);
298 return;
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800299 }
Sen Jianga35896c2016-05-25 11:08:41 -0700300 // The action will skip kVerifySourceHash step if target partition hash
301 // matches, if we are in this step, it means target hash does not match,
302 // and now that the source partition hash matches, we should set the error
303 // code to reflect the error in target partition.
304 // We only need to verify the source partition which the target hash does
305 // not match, the rest of the partitions don't matter.
Sen Jiang57f91802017-11-14 17:42:13 -0800306 Cleanup(ErrorCode::kNewRootfsVerificationError);
307 return;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700308 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700309 // Start hashing the next partition, if any.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700310 hasher_.reset();
311 buffer_.clear();
312 src_stream_->CloseBlocking(nullptr);
313 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700314}
315
316} // namespace chromeos_update_engine