blob: d2ed24ab0631d19afd969306951530bc6dcecac1 [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//
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/payload_consumer/delta_performer.h"
Darin Petkovd7061ab2010-10-06 14:37:09 -070018
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070019#include <errno.h>
Alex Deymo79715ad2015-10-02 14:27:53 -070020#include <linux/fs.h>
Darin Petkovd7061ab2010-10-06 14:37:09 -070021
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070022#include <algorithm>
23#include <cstring>
Yifan Hong537802d2018-08-15 13:15:42 -070024#include <map>
Ben Chan02f7c1d2014-10-18 15:18:02 -070025#include <memory>
Tianjied3865d12020-06-03 15:25:17 -070026#include <set>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070027#include <string>
Amin Hassanid87304c2017-08-29 11:40:03 -070028#include <utility>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070029#include <vector>
30
Ben Chan06c76a42014-09-05 08:21:06 -070031#include <base/files/file_util.h>
Alex Deymo161c4a12014-05-16 15:56:21 -070032#include <base/format_macros.h>
Lann Martin39f57142017-07-14 09:18:42 -060033#include <base/metrics/histogram_macros.h>
Alex Deymo67140842016-06-15 13:28:59 -070034#include <base/strings/string_number_conversions.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070035#include <base/strings/string_util.h>
36#include <base/strings/stringprintf.h>
Lann Martin39f57142017-07-14 09:18:42 -060037#include <base/time/time.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070038#include <brillo/data_encoding.h>
Alex Deymo6765a682017-05-19 13:13:54 -070039#include <bsdiff/bspatch.h>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070040#include <google/protobuf/repeated_field.h>
Amin Hassani02855c22017-09-06 22:34:50 -070041#include <puffin/puffpatch.h>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070042
Alex Deymo39910dc2015-11-09 17:04:30 -080043#include "update_engine/common/constants.h"
Yifan Hong87029332020-09-01 17:20:08 -070044#include "update_engine/common/error_code.h"
45#include "update_engine/common/error_code_utils.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080046#include "update_engine/common/hardware_interface.h"
Alex Deymo542c19b2015-12-03 07:43:31 -030047#include "update_engine/common/prefs_interface.h"
48#include "update_engine/common/subprocess.h"
49#include "update_engine/common/terminator.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080050#include "update_engine/payload_consumer/bzip_extent_writer.h"
Amin Hassania3fc20a2017-09-19 17:19:34 -070051#include "update_engine/payload_consumer/cached_file_descriptor.h"
Tianjie Xu7a78d632019-10-08 16:32:39 -070052#include "update_engine/payload_consumer/certificate_parser_interface.h"
Alex Deymo542c19b2015-12-03 07:43:31 -030053#include "update_engine/payload_consumer/download_action.h"
Amin Hassanidb56be92017-09-06 12:41:23 -070054#include "update_engine/payload_consumer/extent_reader.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080055#include "update_engine/payload_consumer/extent_writer.h"
Tianjied3865d12020-06-03 15:25:17 -070056#include "update_engine/payload_consumer/partition_update_generator_interface.h"
Alex Deymo51c264e2016-11-04 15:49:53 -070057#if USE_FEC
58#include "update_engine/payload_consumer/fec_file_descriptor.h"
59#endif // USE_FEC
Alex Deymoa48f6302016-11-04 15:49:53 -070060#include "update_engine/payload_consumer/file_descriptor_utils.h"
HÃ¥kan Kvist4e13cf42018-01-23 12:57:55 +010061#include "update_engine/payload_consumer/mount_history.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080062#include "update_engine/payload_consumer/payload_constants.h"
63#include "update_engine/payload_consumer/payload_verifier.h"
64#include "update_engine/payload_consumer/xz_extent_writer.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070065
Alex Deymo161c4a12014-05-16 15:56:21 -070066using google::protobuf::RepeatedPtrField;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070067using std::min;
68using std::string;
69using std::vector;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070070
71namespace chromeos_update_engine {
Gilad Arnold8a86fa52013-01-15 12:35:05 -080072const unsigned DeltaPerformer::kProgressLogMaxChunks = 10;
73const unsigned DeltaPerformer::kProgressLogTimeoutSeconds = 30;
74const unsigned DeltaPerformer::kProgressDownloadWeight = 50;
75const unsigned DeltaPerformer::kProgressOperationsWeight = 50;
Colin Howes0e452c92018-11-02 13:18:44 -070076const uint64_t DeltaPerformer::kCheckpointFrequencySeconds = 1;
Darin Petkovabc7bc02011-02-23 14:39:43 -080077
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070078namespace {
Darin Petkov73058b42010-10-06 16:32:19 -070079const int kUpdateStateOperationInvalid = -1;
Darin Petkov61426142010-10-08 11:04:55 -070080const int kMaxResumedUpdateFailures = 10;
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080081
Amin Hassania3fc20a2017-09-19 17:19:34 -070082const uint64_t kCacheSize = 1024 * 1024; // 1MB
83
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080084// Opens path for read/write. On success returns an open FileDescriptor
85// and sets *err to 0. On failure, sets *err to errno and returns nullptr.
Amin Hassania3fc20a2017-09-19 17:19:34 -070086FileDescriptorPtr OpenFile(const char* path,
87 int mode,
88 bool cache_writes,
89 int* err) {
Alex Deymo5fb356c2016-03-25 18:48:22 -070090 // Try to mark the block device read-only based on the mode. Ignore any
91 // failure since this won't work when passing regular files.
Amin Hassania3fc20a2017-09-19 17:19:34 -070092 bool read_only = (mode & O_ACCMODE) == O_RDONLY;
93 utils::SetBlockDeviceReadOnly(path, read_only);
Alex Deymo5fb356c2016-03-25 18:48:22 -070094
Brian Norris7b428f52019-06-26 10:03:35 -070095 FileDescriptorPtr fd(new EintrSafeFileDescriptor());
Amin Hassania3fc20a2017-09-19 17:19:34 -070096 if (cache_writes && !read_only) {
97 fd = FileDescriptorPtr(new CachedFileDescriptor(fd, kCacheSize));
98 LOG(INFO) << "Caching writes.";
99 }
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800100 if (!fd->Open(path, mode, 000)) {
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -0800101 *err = errno;
102 PLOG(ERROR) << "Unable to open file " << path;
103 return nullptr;
104 }
105 *err = 0;
106 return fd;
107}
Alex Deymob86787c2016-05-12 18:46:25 -0700108
109// Discard the tail of the block device referenced by |fd|, from the offset
110// |data_size| until the end of the block device. Returns whether the data was
111// discarded.
Chih-Hung Hsieh5c6bb1d2016-07-27 13:33:15 -0700112bool DiscardPartitionTail(const FileDescriptorPtr& fd, uint64_t data_size) {
Alex Deymob86787c2016-05-12 18:46:25 -0700113 uint64_t part_size = fd->BlockDevSize();
114 if (!part_size || part_size <= data_size)
115 return false;
116
Alex Deymo72a68d82016-06-15 16:31:04 -0700117 struct blkioctl_request {
118 int number;
119 const char* name;
120 };
121 const vector<blkioctl_request> blkioctl_requests = {
Alex Deymo72a68d82016-06-15 16:31:04 -0700122 {BLKDISCARD, "BLKDISCARD"},
Amin Hassani41555202017-06-23 12:42:00 -0700123 {BLKSECDISCARD, "BLKSECDISCARD"},
Alex Deymob86787c2016-05-12 18:46:25 -0700124#ifdef BLKZEROOUT
Alex Deymo72a68d82016-06-15 16:31:04 -0700125 {BLKZEROOUT, "BLKZEROOUT"},
Alex Deymob86787c2016-05-12 18:46:25 -0700126#endif
127 };
Alex Deymo72a68d82016-06-15 16:31:04 -0700128 for (const auto& req : blkioctl_requests) {
Alex Deymob86787c2016-05-12 18:46:25 -0700129 int error = 0;
Alex Deymo72a68d82016-06-15 16:31:04 -0700130 if (fd->BlkIoctl(req.number, data_size, part_size - data_size, &error) &&
Alex Deymob86787c2016-05-12 18:46:25 -0700131 error == 0) {
132 return true;
133 }
134 LOG(WARNING) << "Error discarding the last "
135 << (part_size - data_size) / 1024 << " KiB using ioctl("
Alex Deymo72a68d82016-06-15 16:31:04 -0700136 << req.name << ")";
Alex Deymob86787c2016-05-12 18:46:25 -0700137 }
138 return false;
139}
140
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700141} // namespace
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700142
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800143// Computes the ratio of |part| and |total|, scaled to |norm|, using integer
144// arithmetic.
145static uint64_t IntRatio(uint64_t part, uint64_t total, uint64_t norm) {
146 return part * norm / total;
147}
148
149void DeltaPerformer::LogProgress(const char* message_prefix) {
150 // Format operations total count and percentage.
151 string total_operations_str("?");
152 string completed_percentage_str("");
153 if (num_total_operations_) {
Alex Deymoc00c98a2015-03-17 17:38:00 -0700154 total_operations_str = std::to_string(num_total_operations_);
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800155 // Upcasting to 64-bit to avoid overflow, back to size_t for formatting.
Amin Hassani008c4582019-01-13 16:22:47 -0800156 completed_percentage_str = base::StringPrintf(
157 " (%" PRIu64 "%%)",
158 IntRatio(next_operation_num_, num_total_operations_, 100));
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800159 }
160
161 // Format download total count and percentage.
Sen Jiang0affc2c2017-02-10 15:55:05 -0800162 size_t payload_size = payload_->size;
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800163 string payload_size_str("?");
164 string downloaded_percentage_str("");
165 if (payload_size) {
Alex Deymoc00c98a2015-03-17 17:38:00 -0700166 payload_size_str = std::to_string(payload_size);
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800167 // Upcasting to 64-bit to avoid overflow, back to size_t for formatting.
Amin Hassani008c4582019-01-13 16:22:47 -0800168 downloaded_percentage_str = base::StringPrintf(
169 " (%" PRIu64 "%%)", IntRatio(total_bytes_received_, payload_size, 100));
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800170 }
171
172 LOG(INFO) << (message_prefix ? message_prefix : "") << next_operation_num_
173 << "/" << total_operations_str << " operations"
Amin Hassani008c4582019-01-13 16:22:47 -0800174 << completed_percentage_str << ", " << total_bytes_received_ << "/"
175 << payload_size_str << " bytes downloaded"
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800176 << downloaded_percentage_str << ", overall progress "
177 << overall_progress_ << "%";
178}
179
180void DeltaPerformer::UpdateOverallProgress(bool force_log,
181 const char* message_prefix) {
182 // Compute our download and overall progress.
183 unsigned new_overall_progress = 0;
Alex Vakulenko0103c362016-01-20 07:56:15 -0800184 static_assert(kProgressDownloadWeight + kProgressOperationsWeight == 100,
185 "Progress weights don't add up");
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800186 // Only consider download progress if its total size is known; otherwise
187 // adjust the operations weight to compensate for the absence of download
188 // progress. Also, make sure to cap the download portion at
189 // kProgressDownloadWeight, in case we end up downloading more than we
190 // initially expected (this indicates a problem, but could generally happen).
191 // TODO(garnold) the correction of operations weight when we do not have the
192 // total payload size, as well as the conditional guard below, should both be
193 // eliminated once we ensure that the payload_size in the install plan is
194 // always given and is non-zero. This currently isn't the case during unit
195 // tests (see chromium-os:37969).
Sen Jiang0affc2c2017-02-10 15:55:05 -0800196 size_t payload_size = payload_->size;
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800197 unsigned actual_operations_weight = kProgressOperationsWeight;
198 if (payload_size)
Amin Hassani008c4582019-01-13 16:22:47 -0800199 new_overall_progress +=
200 min(static_cast<unsigned>(IntRatio(
201 total_bytes_received_, payload_size, kProgressDownloadWeight)),
202 kProgressDownloadWeight);
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800203 else
204 actual_operations_weight += kProgressDownloadWeight;
205
206 // Only add completed operations if their total number is known; we definitely
207 // expect an update to have at least one operation, so the expectation is that
208 // this will eventually reach |actual_operations_weight|.
209 if (num_total_operations_)
Amin Hassani008c4582019-01-13 16:22:47 -0800210 new_overall_progress += IntRatio(
211 next_operation_num_, num_total_operations_, actual_operations_weight);
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800212
213 // Progress ratio cannot recede, unless our assumptions about the total
214 // payload size, total number of operations, or the monotonicity of progress
215 // is breached.
216 if (new_overall_progress < overall_progress_) {
217 LOG(WARNING) << "progress counter receded from " << overall_progress_
218 << "% down to " << new_overall_progress << "%; this is a bug";
219 force_log = true;
220 }
221 overall_progress_ = new_overall_progress;
222
223 // Update chunk index, log as needed: if forced by called, or we completed a
224 // progress chunk, or a timeout has expired.
Sen Jiang53c2ec52019-01-10 15:27:59 -0800225 base::TimeTicks curr_time = base::TimeTicks::Now();
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800226 unsigned curr_progress_chunk =
227 overall_progress_ * kProgressLogMaxChunks / 100;
228 if (force_log || curr_progress_chunk > last_progress_chunk_ ||
229 curr_time > forced_progress_log_time_) {
230 forced_progress_log_time_ = curr_time + forced_progress_log_wait_;
231 LogProgress(message_prefix);
232 }
233 last_progress_chunk_ = curr_progress_chunk;
234}
235
Amin Hassani008c4582019-01-13 16:22:47 -0800236size_t DeltaPerformer::CopyDataToBuffer(const char** bytes_p,
237 size_t* count_p,
Gilad Arnoldfe133932014-01-14 12:25:50 -0800238 size_t max) {
239 const size_t count = *count_p;
240 if (!count)
241 return 0; // Special case shortcut.
Alex Deymof329b932014-10-30 01:37:48 -0700242 size_t read_len = min(count, max - buffer_.size());
Gilad Arnoldfe133932014-01-14 12:25:50 -0800243 const char* bytes_start = *bytes_p;
244 const char* bytes_end = bytes_start + read_len;
Sen Jiangbe19a242017-11-17 11:48:17 -0800245 buffer_.reserve(max);
Gilad Arnoldfe133932014-01-14 12:25:50 -0800246 buffer_.insert(buffer_.end(), bytes_start, bytes_end);
247 *bytes_p = bytes_end;
248 *count_p = count - read_len;
249 return read_len;
250}
251
Amin Hassani008c4582019-01-13 16:22:47 -0800252bool DeltaPerformer::HandleOpResult(bool op_result,
253 const char* op_type_name,
Gilad Arnoldfe133932014-01-14 12:25:50 -0800254 ErrorCode* error) {
255 if (op_result)
256 return true;
257
Alex Deymo3d009062016-05-13 15:51:25 -0700258 size_t partition_first_op_num =
259 current_partition_ ? acc_num_operations_[current_partition_ - 1] : 0;
Gilad Arnoldfe133932014-01-14 12:25:50 -0800260 LOG(ERROR) << "Failed to perform " << op_type_name << " operation "
Alex Deymo3d009062016-05-13 15:51:25 -0700261 << next_operation_num_ << ", which is the operation "
262 << next_operation_num_ - partition_first_op_num
263 << " in partition \""
264 << partitions_[current_partition_].partition_name() << "\"";
Sen Jiangbe2c47b2016-06-15 14:09:27 -0700265 if (*error == ErrorCode::kSuccess)
266 *error = ErrorCode::kDownloadOperationExecutionError;
Gilad Arnoldfe133932014-01-14 12:25:50 -0800267 return false;
268}
269
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700270int DeltaPerformer::Close() {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700271 int err = -CloseCurrentPartition();
Amin Hassani008c4582019-01-13 16:22:47 -0800272 LOG_IF(ERROR,
273 !payload_hash_calculator_.Finalize() ||
274 !signed_hash_calculator_.Finalize())
Sen Jiangf6813802015-11-03 21:27:29 -0800275 << "Unable to finalize the hash.";
Darin Petkov934bb412010-11-18 11:21:35 -0800276 if (!buffer_.empty()) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700277 LOG(INFO) << "Discarding " << buffer_.size() << " unused downloaded bytes";
278 if (err >= 0)
Darin Petkov934bb412010-11-18 11:21:35 -0800279 err = 1;
Darin Petkov934bb412010-11-18 11:21:35 -0800280 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700281 return -err;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700282}
283
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700284int DeltaPerformer::CloseCurrentPartition() {
285 int err = 0;
286 if (source_fd_ && !source_fd_->Close()) {
287 err = errno;
288 PLOG(ERROR) << "Error closing source partition";
289 if (!err)
290 err = 1;
291 }
292 source_fd_.reset();
Alex Deymo51c264e2016-11-04 15:49:53 -0700293 if (source_ecc_fd_ && !source_ecc_fd_->Close()) {
294 err = errno;
295 PLOG(ERROR) << "Error closing ECC source partition";
296 if (!err)
297 err = 1;
298 }
299 source_ecc_fd_.reset();
300 source_ecc_open_failure_ = false;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700301 source_path_.clear();
302
303 if (target_fd_ && !target_fd_->Close()) {
304 err = errno;
305 PLOG(ERROR) << "Error closing target partition";
306 if (!err)
307 err = 1;
308 }
309 target_fd_.reset();
310 target_path_.clear();
311 return -err;
312}
313
314bool DeltaPerformer::OpenCurrentPartition() {
315 if (current_partition_ >= partitions_.size())
316 return false;
317
318 const PartitionUpdate& partition = partitions_[current_partition_];
Sen Jiangd2ff2a02017-04-06 14:53:31 -0700319 size_t num_previous_partitions =
320 install_plan_->partitions.size() - partitions_.size();
321 const InstallPlan::Partition& install_part =
322 install_plan_->partitions[num_previous_partitions + current_partition_];
Tianjie55abd3c2020-06-19 00:22:59 -0700323 // Open source fds if we have a delta payload, or for partitions in the
324 // partial update.
Tianjied3865d12020-06-03 15:25:17 -0700325 bool source_may_exist = manifest_.partial_update() ||
Tianjie55abd3c2020-06-19 00:22:59 -0700326 payload_->type == InstallPayloadType::kDelta;
Tianjied3865d12020-06-03 15:25:17 -0700327 // We shouldn't open the source partition in certain cases, e.g. some dynamic
328 // partitions in delta payload, partitions included in the full payload for
329 // partial updates. Use the source size as the indicator.
330 if (source_may_exist && install_part.source_size > 0) {
Sen Jiangd2ff2a02017-04-06 14:53:31 -0700331 source_path_ = install_part.source_path;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700332 int err;
Amin Hassania3fc20a2017-09-19 17:19:34 -0700333 source_fd_ = OpenFile(source_path_.c_str(), O_RDONLY, false, &err);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700334 if (!source_fd_) {
335 LOG(ERROR) << "Unable to open source partition "
336 << partition.partition_name() << " on slot "
337 << BootControlInterface::SlotName(install_plan_->source_slot)
338 << ", file " << source_path_;
339 return false;
340 }
341 }
342
Sen Jiangd2ff2a02017-04-06 14:53:31 -0700343 target_path_ = install_part.target_path;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700344 int err;
Grant Grundlera763c2d2017-05-10 11:12:52 -0700345
Amin Hassani7ecda262017-07-11 17:10:50 -0700346 int flags = O_RDWR;
Amin Hassanied37d682018-04-06 13:22:00 -0700347 if (!interactive_)
Amin Hassani7ecda262017-07-11 17:10:50 -0700348 flags |= O_DSYNC;
349
350 LOG(INFO) << "Opening " << target_path_ << " partition with"
Amin Hassanied37d682018-04-06 13:22:00 -0700351 << (interactive_ ? "out" : "") << " O_DSYNC";
Amin Hassani7ecda262017-07-11 17:10:50 -0700352
Amin Hassania3fc20a2017-09-19 17:19:34 -0700353 target_fd_ = OpenFile(target_path_.c_str(), flags, true, &err);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700354 if (!target_fd_) {
355 LOG(ERROR) << "Unable to open target partition "
356 << partition.partition_name() << " on slot "
357 << BootControlInterface::SlotName(install_plan_->target_slot)
358 << ", file " << target_path_;
359 return false;
360 }
Alex Deymob86787c2016-05-12 18:46:25 -0700361
362 LOG(INFO) << "Applying " << partition.operations().size()
363 << " operations to partition \"" << partition.partition_name()
364 << "\"";
365
366 // Discard the end of the partition, but ignore failures.
Sen Jiangd2ff2a02017-04-06 14:53:31 -0700367 DiscardPartitionTail(target_fd_, install_part.target_size);
Alex Deymob86787c2016-05-12 18:46:25 -0700368
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700369 return true;
370}
371
Alex Deymo51c264e2016-11-04 15:49:53 -0700372bool DeltaPerformer::OpenCurrentECCPartition() {
373 if (source_ecc_fd_)
374 return true;
375
376 if (source_ecc_open_failure_)
377 return false;
378
379 if (current_partition_ >= partitions_.size())
380 return false;
381
Amin Hassani0f59a9a2019-09-27 10:24:31 -0700382 // No support for ECC for full payloads.
383 if (payload_->type == InstallPayloadType::kFull)
Alex Deymo51c264e2016-11-04 15:49:53 -0700384 return false;
385
386#if USE_FEC
387 const PartitionUpdate& partition = partitions_[current_partition_];
388 size_t num_previous_partitions =
389 install_plan_->partitions.size() - partitions_.size();
390 const InstallPlan::Partition& install_part =
391 install_plan_->partitions[num_previous_partitions + current_partition_];
392 string path = install_part.source_path;
393 FileDescriptorPtr fd(new FecFileDescriptor());
394 if (!fd->Open(path.c_str(), O_RDONLY, 0)) {
395 PLOG(ERROR) << "Unable to open ECC source partition "
396 << partition.partition_name() << " on slot "
397 << BootControlInterface::SlotName(install_plan_->source_slot)
398 << ", file " << path;
399 source_ecc_open_failure_ = true;
400 return false;
401 }
402 source_ecc_fd_ = fd;
403#else
404 // No support for ECC compiled.
405 source_ecc_open_failure_ = true;
406#endif // USE_FEC
407
408 return !source_ecc_open_failure_;
409}
410
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700411namespace {
412
413void LogPartitionInfoHash(const PartitionInfo& info, const string& tag) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700414 string sha256 = brillo::data_encoding::Base64Encode(info.hash());
Alex Vakulenko981a9fb2015-02-09 12:51:24 -0800415 LOG(INFO) << "PartitionInfo " << tag << " sha256: " << sha256
416 << " size: " << info.size();
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700417}
418
Alex Deymo39910dc2015-11-09 17:04:30 -0800419void LogPartitionInfo(const vector<PartitionUpdate>& partitions) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700420 for (const PartitionUpdate& partition : partitions) {
Sen Jiang7b9a5872018-01-17 13:25:06 -0800421 if (partition.has_old_partition_info()) {
422 LogPartitionInfoHash(partition.old_partition_info(),
423 "old " + partition.partition_name());
424 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700425 LogPartitionInfoHash(partition.new_partition_info(),
426 "new " + partition.partition_name());
427 }
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700428}
429
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700430} // namespace
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700431
Sen Jiangb8060e42015-09-24 17:30:50 -0700432bool DeltaPerformer::IsHeaderParsed() const {
433 return metadata_size_ != 0;
434}
Jay Srinivasanf4318702012-09-24 11:56:24 -0700435
Sen Jiang9c89e842018-02-02 13:51:21 -0800436MetadataParseResult DeltaPerformer::ParsePayloadMetadata(
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700437 const brillo::Blob& payload, ErrorCode* error) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700438 *error = ErrorCode::kSuccess;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700439
Sen Jiangb8060e42015-09-24 17:30:50 -0700440 if (!IsHeaderParsed()) {
Sen Jiangf1236632018-05-11 16:03:23 -0700441 MetadataParseResult result =
Amin Hassani822d4852020-03-10 01:50:42 +0000442 payload_metadata_.ParsePayloadHeader(payload, error);
Sen Jiang9c89e842018-02-02 13:51:21 -0800443 if (result != MetadataParseResult::kSuccess)
444 return result;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700445
Sen Jiang9c89e842018-02-02 13:51:21 -0800446 metadata_size_ = payload_metadata_.GetMetadataSize();
447 metadata_signature_size_ = payload_metadata_.GetMetadataSignatureSize();
448 major_payload_version_ = payload_metadata_.GetMajorVersion();
Gilad Arnoldfe133932014-01-14 12:25:50 -0800449
450 // If the metadata size is present in install plan, check for it immediately
451 // even before waiting for that many number of bytes to be downloaded in the
452 // payload. This will prevent any attack which relies on us downloading data
453 // beyond the expected metadata size.
Gilad Arnoldfe133932014-01-14 12:25:50 -0800454 if (install_plan_->hash_checks_mandatory) {
Sen Jiang0affc2c2017-02-10 15:55:05 -0800455 if (payload_->metadata_size != metadata_size_) {
Gilad Arnoldfe133932014-01-14 12:25:50 -0800456 LOG(ERROR) << "Mandatory metadata size in Omaha response ("
Sen Jiang0affc2c2017-02-10 15:55:05 -0800457 << payload_->metadata_size
Gilad Arnoldfe133932014-01-14 12:25:50 -0800458 << ") is missing/incorrect, actual = " << metadata_size_;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700459 *error = ErrorCode::kDownloadInvalidMetadataSize;
Sen Jiang9c89e842018-02-02 13:51:21 -0800460 return MetadataParseResult::kError;
Gilad Arnoldfe133932014-01-14 12:25:50 -0800461 }
462 }
Andrew3a7dc262019-12-19 11:38:08 -0800463
464 // Check that the |metadata signature size_| and |metadata_size_| are not
465 // very big numbers. This is necessary since |update_engine| needs to write
466 // these values into the buffer before being able to use them, and if an
467 // attacker sets these values to a very big number, the buffer will overflow
468 // and |update_engine| will crash. A simple way of solving this is to check
469 // that the size of both values is smaller than the payload itself.
470 if (metadata_size_ + metadata_signature_size_ > payload_->size) {
471 LOG(ERROR) << "The size of the metadata_size(" << metadata_size_ << ")"
472 << " or metadata signature(" << metadata_signature_size_ << ")"
473 << " is greater than the size of the payload"
474 << "(" << payload_->size << ")";
475 *error = ErrorCode::kDownloadInvalidMetadataSize;
476 return MetadataParseResult::kError;
477 }
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700478 }
479
480 // Now that we have validated the metadata size, we should wait for the full
Sen Jiang76bfa742015-10-12 17:13:26 -0700481 // metadata and its signature (if exist) to be read in before we can parse it.
482 if (payload.size() < metadata_size_ + metadata_signature_size_)
Sen Jiang9c89e842018-02-02 13:51:21 -0800483 return MetadataParseResult::kInsufficientData;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700484
485 // Log whether we validated the size or simply trusting what's in the payload
Jay Srinivasanf4318702012-09-24 11:56:24 -0700486 // here. This is logged here (after we received the full metadata data) so
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700487 // that we just log once (instead of logging n times) if it takes n
488 // DeltaPerformer::Write calls to download the full manifest.
Sen Jiang0affc2c2017-02-10 15:55:05 -0800489 if (payload_->metadata_size == metadata_size_) {
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700490 LOG(INFO) << "Manifest size in payload matches expected value from Omaha";
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800491 } else {
492 // For mandatory-cases, we'd have already returned a kMetadataParseError
493 // above. We'll be here only for non-mandatory cases. Just send a UMA stat.
494 LOG(WARNING) << "Ignoring missing/incorrect metadata size ("
Sen Jiang0affc2c2017-02-10 15:55:05 -0800495 << payload_->metadata_size
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800496 << ") in Omaha response as validation is not mandatory. "
Gilad Arnoldfe133932014-01-14 12:25:50 -0800497 << "Trusting metadata size in payload = " << metadata_size_;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800498 }
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700499
Tianjie Xu7a78d632019-10-08 16:32:39 -0700500 auto [payload_verifier, perform_verification] = CreatePayloadVerifier();
501 if (!payload_verifier) {
502 LOG(ERROR) << "Failed to create payload verifier.";
Sen Jiang08c6da12019-01-07 18:28:56 -0800503 *error = ErrorCode::kDownloadMetadataSignatureVerificationError;
Tianjie Xu6c70b932019-10-22 16:46:00 -0700504 if (perform_verification) {
505 return MetadataParseResult::kError;
506 }
507 } else {
508 // We have the full metadata in |payload|. Verify its integrity
509 // and authenticity based on the information we have in Omaha response.
510 *error = payload_metadata_.ValidateMetadataSignature(
511 payload, payload_->metadata_signature, *payload_verifier);
Sen Jiang08c6da12019-01-07 18:28:56 -0800512 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700513 if (*error != ErrorCode::kSuccess) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800514 if (install_plan_->hash_checks_mandatory) {
David Zeuthenbc27aac2013-11-26 11:17:48 -0800515 // The autoupdate_CatchBadSignatures test checks for this string
516 // in log-files. Keep in sync.
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800517 LOG(ERROR) << "Mandatory metadata signature validation failed";
Sen Jiang9c89e842018-02-02 13:51:21 -0800518 return MetadataParseResult::kError;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800519 }
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700520
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800521 // For non-mandatory cases, just send a UMA stat.
522 LOG(WARNING) << "Ignoring metadata signature validation failures";
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700523 *error = ErrorCode::kSuccess;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700524 }
525
Gilad Arnolddaa27402014-01-23 11:56:17 -0800526 // The payload metadata is deemed valid, it's safe to parse the protobuf.
Sen Jiang9c89e842018-02-02 13:51:21 -0800527 if (!payload_metadata_.GetManifest(payload, &manifest_)) {
Darin Petkov9574f7e2011-01-13 10:48:12 -0800528 LOG(ERROR) << "Unable to parse manifest in update file.";
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700529 *error = ErrorCode::kDownloadManifestParseError;
Sen Jiang9c89e842018-02-02 13:51:21 -0800530 return MetadataParseResult::kError;
Darin Petkov9574f7e2011-01-13 10:48:12 -0800531 }
Gilad Arnolddaa27402014-01-23 11:56:17 -0800532
533 manifest_parsed_ = true;
Sen Jiang9c89e842018-02-02 13:51:21 -0800534 return MetadataParseResult::kSuccess;
Darin Petkov9574f7e2011-01-13 10:48:12 -0800535}
536
Amin Hassani008c4582019-01-13 16:22:47 -0800537#define OP_DURATION_HISTOGRAM(_op_name, _start_time) \
538 LOCAL_HISTOGRAM_CUSTOM_TIMES( \
539 "UpdateEngine.DownloadAction.InstallOperation::" _op_name ".Duration", \
Andrew9d5a61d2020-03-26 13:40:37 -0700540 (base::TimeTicks::Now() - _start_time), \
Amin Hassani008c4582019-01-13 16:22:47 -0800541 base::TimeDelta::FromMilliseconds(10), \
542 base::TimeDelta::FromMinutes(5), \
543 20);
Lann Martin39f57142017-07-14 09:18:42 -0600544
Don Garrette410e0f2011-11-10 15:39:01 -0800545// Wrapper around write. Returns true if all requested bytes
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800546// were written, or false on any error, regardless of progress
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700547// and stores an action exit code in |error|.
Amin Hassani008c4582019-01-13 16:22:47 -0800548bool DeltaPerformer::Write(const void* bytes, size_t count, ErrorCode* error) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700549 *error = ErrorCode::kSuccess;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700550 const char* c_bytes = reinterpret_cast<const char*>(bytes);
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800551
552 // Update the total byte downloaded count and the progress logs.
553 total_bytes_received_ += count;
554 UpdateOverallProgress(false, "Completed ");
555
Gilad Arnoldfe133932014-01-14 12:25:50 -0800556 while (!manifest_valid_) {
Sen Jiangb8060e42015-09-24 17:30:50 -0700557 // Read data up to the needed limit; this is either maximium payload header
558 // size, or the full metadata size (once it becomes known).
559 const bool do_read_header = !IsHeaderParsed();
Amin Hassani008c4582019-01-13 16:22:47 -0800560 CopyDataToBuffer(
561 &c_bytes,
562 &count,
563 (do_read_header ? kMaxPayloadHeaderSize
564 : metadata_size_ + metadata_signature_size_));
Gilad Arnoldfe133932014-01-14 12:25:50 -0800565
Gilad Arnolddaa27402014-01-23 11:56:17 -0800566 MetadataParseResult result = ParsePayloadMetadata(buffer_, error);
Sen Jiang9c89e842018-02-02 13:51:21 -0800567 if (result == MetadataParseResult::kError)
Don Garrette410e0f2011-11-10 15:39:01 -0800568 return false;
Sen Jiang9c89e842018-02-02 13:51:21 -0800569 if (result == MetadataParseResult::kInsufficientData) {
Gilad Arnoldfe133932014-01-14 12:25:50 -0800570 // If we just processed the header, make an attempt on the manifest.
Sen Jiangb8060e42015-09-24 17:30:50 -0700571 if (do_read_header && IsHeaderParsed())
Gilad Arnoldfe133932014-01-14 12:25:50 -0800572 continue;
573
Don Garrette410e0f2011-11-10 15:39:01 -0800574 return true;
Gilad Arnoldfe133932014-01-14 12:25:50 -0800575 }
Gilad Arnold21504f02013-05-24 08:51:22 -0700576
577 // Checks the integrity of the payload manifest.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700578 if ((*error = ValidateManifest()) != ErrorCode::kSuccess)
Gilad Arnold21504f02013-05-24 08:51:22 -0700579 return false;
Gilad Arnolddaa27402014-01-23 11:56:17 -0800580 manifest_valid_ = true;
Kelvin Zhangcc011d32020-07-10 18:20:08 -0400581 if (!install_plan_->is_resume) {
582 prefs_->SetString(kPrefsManifestBytes, {buffer_.begin(), buffer_.end()});
583 }
Gilad Arnold21504f02013-05-24 08:51:22 -0700584
Gilad Arnoldfe133932014-01-14 12:25:50 -0800585 // Clear the download buffer.
Sen Jiangf6813802015-11-03 21:27:29 -0800586 DiscardBuffer(false, metadata_size_);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700587
Sen Jiang57f91802017-11-14 17:42:13 -0800588 block_size_ = manifest_.block_size();
589
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700590 // This populates |partitions_| and the |install_plan.partitions| with the
591 // list of partitions from the manifest.
592 if (!ParseManifestPartitions(error))
593 return false;
594
Sen Jiang5ae865b2017-04-18 14:24:40 -0700595 // |install_plan.partitions| was filled in, nothing need to be done here if
596 // the payload was already applied, returns false to terminate http fetcher,
597 // but keep |error| as ErrorCode::kSuccess.
598 if (payload_->already_applied)
599 return false;
600
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700601 num_total_operations_ = 0;
602 for (const auto& partition : partitions_) {
603 num_total_operations_ += partition.operations_size();
604 acc_num_operations_.push_back(num_total_operations_);
605 }
606
Amin Hassani008c4582019-01-13 16:22:47 -0800607 LOG_IF(WARNING,
608 !prefs_->SetInt64(kPrefsManifestMetadataSize, metadata_size_))
Darin Petkov73058b42010-10-06 16:32:19 -0700609 << "Unable to save the manifest metadata size.";
Amin Hassani008c4582019-01-13 16:22:47 -0800610 LOG_IF(WARNING,
611 !prefs_->SetInt64(kPrefsManifestSignatureSize,
612 metadata_signature_size_))
Alex Deymof25eb492016-02-26 00:20:08 -0800613 << "Unable to save the manifest signature size.";
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700614
Darin Petkov9b230572010-10-08 10:20:09 -0700615 if (!PrimeUpdateState()) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700616 *error = ErrorCode::kDownloadStateInitializationError;
Darin Petkov9b230572010-10-08 10:20:09 -0700617 LOG(ERROR) << "Unable to prime the update state.";
Don Garrette410e0f2011-11-10 15:39:01 -0800618 return false;
Darin Petkov9b230572010-10-08 10:20:09 -0700619 }
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800620
Yifan Hong537802d2018-08-15 13:15:42 -0700621 if (next_operation_num_ < acc_num_operations_[current_partition_]) {
622 if (!OpenCurrentPartition()) {
623 *error = ErrorCode::kInstallDeviceOpenError;
624 return false;
625 }
Allie Woodfdf00512015-03-02 13:34:55 -0800626 }
627
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800628 if (next_operation_num_ > 0)
629 UpdateOverallProgress(true, "Resuming after ");
630 LOG(INFO) << "Starting to apply update payload operations";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700631 }
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800632
633 while (next_operation_num_ < num_total_operations_) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700634 // Check if we should cancel the current attempt for any reason.
635 // In this case, *error will have already been populated with the reason
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700636 // why we're canceling.
Alex Deymo542c19b2015-12-03 07:43:31 -0300637 if (download_delegate_ && download_delegate_->ShouldCancel(error))
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700638 return false;
639
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700640 // We know there are more operations to perform because we didn't reach the
641 // |num_total_operations_| limit yet.
Yifan Hong537802d2018-08-15 13:15:42 -0700642 if (next_operation_num_ >= acc_num_operations_[current_partition_]) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700643 CloseCurrentPartition();
Yifan Hong537802d2018-08-15 13:15:42 -0700644 // Skip until there are operations for current_partition_.
645 while (next_operation_num_ >= acc_num_operations_[current_partition_]) {
646 current_partition_++;
647 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700648 if (!OpenCurrentPartition()) {
649 *error = ErrorCode::kInstallDeviceOpenError;
650 return false;
651 }
652 }
Amin Hassani008c4582019-01-13 16:22:47 -0800653 const size_t partition_operation_num =
654 next_operation_num_ -
655 (current_partition_ ? acc_num_operations_[current_partition_ - 1] : 0);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700656
Alex Deymoa12ee112015-08-12 22:19:32 -0700657 const InstallOperation& op =
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700658 partitions_[current_partition_].operations(partition_operation_num);
Gilad Arnoldfe133932014-01-14 12:25:50 -0800659
660 CopyDataToBuffer(&c_bytes, &count, op.data_length());
661
662 // Check whether we received all of the next operation's data payload.
663 if (!CanPerformInstallOperation(op))
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700664 return true;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700665
Jay Srinivasanf4318702012-09-24 11:56:24 -0700666 // Validate the operation only if the metadata signature is present.
Jay Srinivasan00f76b62012-09-17 18:48:36 -0700667 // Otherwise, keep the old behavior. This serves as a knob to disable
668 // the validation logic in case we find some regression after rollout.
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800669 // NOTE: If hash checks are mandatory and if metadata_signature is empty,
670 // we would have already failed in ParsePayloadMetadata method and thus not
671 // even be here. So no need to handle that case again here.
Sen Jiang0affc2c2017-02-10 15:55:05 -0800672 if (!payload_->metadata_signature.empty()) {
Jay Srinivasan00f76b62012-09-17 18:48:36 -0700673 // Note: Validate must be called only if CanPerformInstallOperation is
674 // called. Otherwise, we might be failing operations before even if there
675 // isn't sufficient data to compute the proper hash.
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800676 *error = ValidateOperationHash(op);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700677 if (*error != ErrorCode::kSuccess) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800678 if (install_plan_->hash_checks_mandatory) {
679 LOG(ERROR) << "Mandatory operation hash check failed";
680 return false;
681 }
Jay Srinivasanf0572052012-10-23 18:12:56 -0700682
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800683 // For non-mandatory cases, just send a UMA stat.
684 LOG(WARNING) << "Ignoring operation validation errors";
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700685 *error = ErrorCode::kSuccess;
Jay Srinivasan00f76b62012-09-17 18:48:36 -0700686 }
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700687 }
688
Darin Petkov45580e42010-10-08 14:02:40 -0700689 // Makes sure we unblock exit when this operation completes.
Darin Petkov9c0baf82010-10-07 13:44:48 -0700690 ScopedTerminatorExitUnblocker exit_unblocker =
691 ScopedTerminatorExitUnblocker(); // Avoids a compiler unused var bug.
Gilad Arnoldfe133932014-01-14 12:25:50 -0800692
Lann Martin39f57142017-07-14 09:18:42 -0600693 base::TimeTicks op_start_time = base::TimeTicks::Now();
694
Gilad Arnoldfe133932014-01-14 12:25:50 -0800695 bool op_result;
Alex Deymo2d621a32015-10-01 11:09:01 -0700696 switch (op.type()) {
697 case InstallOperation::REPLACE:
698 case InstallOperation::REPLACE_BZ:
699 case InstallOperation::REPLACE_XZ:
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700700 op_result = PerformReplaceOperation(op);
Lann Martin39f57142017-07-14 09:18:42 -0600701 OP_DURATION_HISTOGRAM("REPLACE", op_start_time);
Alex Deymo2d621a32015-10-01 11:09:01 -0700702 break;
Alex Deymo79715ad2015-10-02 14:27:53 -0700703 case InstallOperation::ZERO:
704 case InstallOperation::DISCARD:
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700705 op_result = PerformZeroOrDiscardOperation(op);
Lann Martin39f57142017-07-14 09:18:42 -0600706 OP_DURATION_HISTOGRAM("ZERO_OR_DISCARD", op_start_time);
Alex Deymo79715ad2015-10-02 14:27:53 -0700707 break;
Alex Deymo2d621a32015-10-01 11:09:01 -0700708 case InstallOperation::SOURCE_COPY:
Sen Jiangbe2c47b2016-06-15 14:09:27 -0700709 op_result = PerformSourceCopyOperation(op, error);
Lann Martin39f57142017-07-14 09:18:42 -0600710 OP_DURATION_HISTOGRAM("SOURCE_COPY", op_start_time);
Alex Deymo2d621a32015-10-01 11:09:01 -0700711 break;
712 case InstallOperation::SOURCE_BSDIFF:
Amin Hassaniefa62d92017-11-09 13:46:56 -0800713 case InstallOperation::BROTLI_BSDIFF:
Sen Jiangbe2c47b2016-06-15 14:09:27 -0700714 op_result = PerformSourceBsdiffOperation(op, error);
Lann Martin39f57142017-07-14 09:18:42 -0600715 OP_DURATION_HISTOGRAM("SOURCE_BSDIFF", op_start_time);
Alex Deymo2d621a32015-10-01 11:09:01 -0700716 break;
Amin Hassani49fdb092017-08-04 13:10:59 -0700717 case InstallOperation::PUFFDIFF:
Amin Hassani02855c22017-09-06 22:34:50 -0700718 op_result = PerformPuffDiffOperation(op, error);
719 OP_DURATION_HISTOGRAM("PUFFDIFF", op_start_time);
Sen Jiangbc3e6b02016-01-19 18:39:26 +0800720 break;
Alex Deymo2d621a32015-10-01 11:09:01 -0700721 default:
Alex Deymoeecb0a52017-05-19 15:15:08 -0700722 op_result = false;
Alex Deymo2d621a32015-10-01 11:09:01 -0700723 }
724 if (!HandleOpResult(op_result, InstallOperationTypeName(op.type()), error))
Gilad Arnoldfe133932014-01-14 12:25:50 -0800725 return false;
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800726
Amin Hassani5192fe52017-08-28 10:28:46 -0700727 if (!target_fd_->Flush()) {
728 return false;
729 }
730
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700731 next_operation_num_++;
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800732 UpdateOverallProgress(false, "Completed ");
Sen Jiang3a25dc22019-01-10 14:14:41 -0800733 CheckpointUpdateProgress(false);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700734 }
Sen Jiangf6813802015-11-03 21:27:29 -0800735
Saint Chou0a92a622020-07-29 14:25:35 +0000736 // In major version 2, we don't add unused operation to the payload.
Alex Deymof25eb492016-02-26 00:20:08 -0800737 // If we already extracted the signature we should skip this step.
Amin Hassani822d4852020-03-10 01:50:42 +0000738 if (manifest_.has_signatures_offset() && manifest_.has_signatures_size() &&
Alex Deymof25eb492016-02-26 00:20:08 -0800739 signatures_message_data_.empty()) {
Sen Jiangf6813802015-11-03 21:27:29 -0800740 if (manifest_.signatures_offset() != buffer_offset_) {
741 LOG(ERROR) << "Payload signatures offset points to blob offset "
742 << manifest_.signatures_offset()
Amin Hassani008c4582019-01-13 16:22:47 -0800743 << " but signatures are expected at offset " << buffer_offset_;
Sen Jiangf6813802015-11-03 21:27:29 -0800744 *error = ErrorCode::kDownloadPayloadVerificationError;
745 return false;
746 }
747 CopyDataToBuffer(&c_bytes, &count, manifest_.signatures_size());
748 // Needs more data to cover entire signature.
749 if (buffer_.size() < manifest_.signatures_size())
750 return true;
751 if (!ExtractSignatureMessage()) {
752 LOG(ERROR) << "Extract payload signature failed.";
753 *error = ErrorCode::kDownloadPayloadVerificationError;
754 return false;
755 }
756 DiscardBuffer(true, 0);
Alex Deymof25eb492016-02-26 00:20:08 -0800757 // Since we extracted the SignatureMessage we need to advance the
758 // checkpoint, otherwise we would reload the signature and try to extract
759 // it again.
Sen Jiang3a25dc22019-01-10 14:14:41 -0800760 // This is the last checkpoint for an update, force this checkpoint to be
761 // saved.
762 CheckpointUpdateProgress(true);
Sen Jiangf6813802015-11-03 21:27:29 -0800763 }
764
Don Garrette410e0f2011-11-10 15:39:01 -0800765 return true;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700766}
767
David Zeuthen8f191b22013-08-06 12:27:50 -0700768bool DeltaPerformer::IsManifestValid() {
769 return manifest_valid_;
770}
771
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700772bool DeltaPerformer::ParseManifestPartitions(ErrorCode* error) {
Amin Hassani822d4852020-03-10 01:50:42 +0000773 partitions_.clear();
774 for (const PartitionUpdate& partition : manifest_.partitions()) {
775 partitions_.push_back(partition);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700776 }
777
Tianjied3865d12020-06-03 15:25:17 -0700778 // For VAB and partial updates, the partition preparation will copy the
779 // dynamic partitions metadata to the target metadata slot, and rename the
780 // slot suffix of the partitions in the metadata.
781 if (install_plan_->target_slot != BootControlInterface::kInvalidSlot) {
782 uint64_t required_size = 0;
783 if (!PreparePartitionsForUpdate(&required_size)) {
784 if (required_size > 0) {
785 *error = ErrorCode::kNotEnoughSpace;
786 } else {
787 *error = ErrorCode::kInstallDeviceOpenError;
788 }
789 return false;
790 }
791 }
792
Tianjie55abd3c2020-06-19 00:22:59 -0700793 // Partitions in manifest are no longer needed after preparing partitions.
Amin Hassani822d4852020-03-10 01:50:42 +0000794 manifest_.clear_partitions();
Tianjied3865d12020-06-03 15:25:17 -0700795 // TODO(xunchang) TBD: allow partial update only on devices with dynamic
796 // partition.
797 if (manifest_.partial_update()) {
798 std::set<std::string> touched_partitions;
799 for (const auto& partition_update : partitions_) {
800 touched_partitions.insert(partition_update.partition_name());
801 }
802
Tianjie99d570d2020-06-04 14:57:19 -0700803 auto generator = partition_update_generator::Create(boot_control_,
804 manifest_.block_size());
Tianjie24f96092020-06-30 12:26:25 -0700805 std::vector<PartitionUpdate> untouched_static_partitions;
Tianjied3865d12020-06-03 15:25:17 -0700806 TEST_AND_RETURN_FALSE(
807 generator->GenerateOperationsForPartitionsNotInPayload(
808 install_plan_->source_slot,
809 install_plan_->target_slot,
810 touched_partitions,
Tianjie24f96092020-06-30 12:26:25 -0700811 &untouched_static_partitions));
812 partitions_.insert(partitions_.end(),
813 untouched_static_partitions.begin(),
814 untouched_static_partitions.end());
815
816 // Save the untouched dynamic partitions in install plan.
817 std::vector<std::string> dynamic_partitions;
818 if (!boot_control_->GetDynamicPartitionControl()
819 ->ListDynamicPartitionsForSlot(install_plan_->source_slot,
820 &dynamic_partitions)) {
821 LOG(ERROR) << "Failed to load dynamic partitions from slot "
822 << install_plan_->source_slot;
823 return false;
824 }
825 install_plan_->untouched_dynamic_partitions.clear();
826 for (const auto& name : dynamic_partitions) {
827 if (touched_partitions.find(name) == touched_partitions.end()) {
828 install_plan_->untouched_dynamic_partitions.push_back(name);
829 }
830 }
Tianjied3865d12020-06-03 15:25:17 -0700831 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700832
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700833 // Fill in the InstallPlan::partitions based on the partitions from the
834 // payload.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700835 for (const auto& partition : partitions_) {
836 InstallPlan::Partition install_part;
837 install_part.name = partition.partition_name();
838 install_part.run_postinstall =
839 partition.has_run_postinstall() && partition.run_postinstall();
Alex Deymo390efed2016-02-18 11:00:40 -0800840 if (install_part.run_postinstall) {
841 install_part.postinstall_path =
842 (partition.has_postinstall_path() ? partition.postinstall_path()
843 : kPostinstallDefaultScript);
844 install_part.filesystem_type = partition.filesystem_type();
Alex Deymo5b91c6b2016-08-04 20:33:36 -0700845 install_part.postinstall_optional = partition.postinstall_optional();
Alex Deymo390efed2016-02-18 11:00:40 -0800846 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700847
848 if (partition.has_old_partition_info()) {
849 const PartitionInfo& info = partition.old_partition_info();
850 install_part.source_size = info.size();
851 install_part.source_hash.assign(info.hash().begin(), info.hash().end());
852 }
853
854 if (!partition.has_new_partition_info()) {
855 LOG(ERROR) << "Unable to get new partition hash info on partition "
856 << install_part.name << ".";
857 *error = ErrorCode::kDownloadNewPartitionInfoError;
858 return false;
859 }
860 const PartitionInfo& info = partition.new_partition_info();
861 install_part.target_size = info.size();
862 install_part.target_hash.assign(info.hash().begin(), info.hash().end());
863
Sen Jiang57f91802017-11-14 17:42:13 -0800864 install_part.block_size = block_size_;
865 if (partition.has_hash_tree_extent()) {
866 Extent extent = partition.hash_tree_data_extent();
867 install_part.hash_tree_data_offset = extent.start_block() * block_size_;
868 install_part.hash_tree_data_size = extent.num_blocks() * block_size_;
869 extent = partition.hash_tree_extent();
870 install_part.hash_tree_offset = extent.start_block() * block_size_;
871 install_part.hash_tree_size = extent.num_blocks() * block_size_;
872 uint64_t hash_tree_data_end =
873 install_part.hash_tree_data_offset + install_part.hash_tree_data_size;
874 if (install_part.hash_tree_offset < hash_tree_data_end) {
875 LOG(ERROR) << "Invalid hash tree extents, hash tree data ends at "
876 << hash_tree_data_end << ", but hash tree starts at "
877 << install_part.hash_tree_offset;
878 *error = ErrorCode::kDownloadNewPartitionInfoError;
879 return false;
880 }
881 install_part.hash_tree_algorithm = partition.hash_tree_algorithm();
882 install_part.hash_tree_salt.assign(partition.hash_tree_salt().begin(),
883 partition.hash_tree_salt().end());
884 }
885 if (partition.has_fec_extent()) {
886 Extent extent = partition.fec_data_extent();
887 install_part.fec_data_offset = extent.start_block() * block_size_;
888 install_part.fec_data_size = extent.num_blocks() * block_size_;
889 extent = partition.fec_extent();
890 install_part.fec_offset = extent.start_block() * block_size_;
891 install_part.fec_size = extent.num_blocks() * block_size_;
892 uint64_t fec_data_end =
893 install_part.fec_data_offset + install_part.fec_data_size;
894 if (install_part.fec_offset < fec_data_end) {
895 LOG(ERROR) << "Invalid fec extents, fec data ends at " << fec_data_end
896 << ", but fec starts at " << install_part.fec_offset;
897 *error = ErrorCode::kDownloadNewPartitionInfoError;
898 return false;
899 }
900 install_part.fec_roots = partition.fec_roots();
901 }
902
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700903 install_plan_->partitions.push_back(install_part);
904 }
905
Tianjied3865d12020-06-03 15:25:17 -0700906 // TODO(xunchang) only need to load the partitions for those in payload.
907 // Because we have already loaded the other once when generating SOURCE_COPY
908 // operations.
Alex Deymo542c19b2015-12-03 07:43:31 -0300909 if (!install_plan_->LoadPartitionsFromSlots(boot_control_)) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700910 LOG(ERROR) << "Unable to determine all the partition devices.";
911 *error = ErrorCode::kInstallDeviceOpenError;
912 return false;
913 }
914 LogPartitionInfo(partitions_);
915 return true;
916}
917
Yifan Hongb9d63572020-01-09 17:50:46 -0800918bool DeltaPerformer::PreparePartitionsForUpdate(uint64_t* required_size) {
919 // Call static PreparePartitionsForUpdate with hash from
920 // kPrefsUpdateCheckResponseHash to ensure hash of payload that space is
921 // preallocated for is the same as the hash of payload being applied.
922 string update_check_response_hash;
923 ignore_result(prefs_->GetString(kPrefsUpdateCheckResponseHash,
924 &update_check_response_hash));
925 return PreparePartitionsForUpdate(prefs_,
926 boot_control_,
927 install_plan_->target_slot,
928 manifest_,
929 update_check_response_hash,
930 required_size);
931}
932
933bool DeltaPerformer::PreparePartitionsForUpdate(
934 PrefsInterface* prefs,
935 BootControlInterface* boot_control,
936 BootControlInterface::Slot target_slot,
937 const DeltaArchiveManifest& manifest,
938 const std::string& update_check_response_hash,
939 uint64_t* required_size) {
940 string last_hash;
941 ignore_result(
942 prefs->GetString(kPrefsDynamicPartitionMetadataUpdated, &last_hash));
943
944 bool is_resume = !update_check_response_hash.empty() &&
945 last_hash == update_check_response_hash;
946
947 if (is_resume) {
948 LOG(INFO) << "Using previously prepared partitions for update. hash = "
949 << last_hash;
950 } else {
951 LOG(INFO) << "Preparing partitions for new update. last hash = "
952 << last_hash << ", new hash = " << update_check_response_hash;
Yifan Hong9acd9cb2018-10-19 14:52:45 -0700953 }
954
Yifan Hongb9d63572020-01-09 17:50:46 -0800955 if (!boot_control->GetDynamicPartitionControl()->PreparePartitionsForUpdate(
956 boot_control->GetCurrentSlot(),
957 target_slot,
958 manifest,
959 !is_resume /* should update */,
960 required_size)) {
Yifan Hong9acd9cb2018-10-19 14:52:45 -0700961 LOG(ERROR) << "Unable to initialize partition metadata for slot "
Yifan Hongb9d63572020-01-09 17:50:46 -0800962 << BootControlInterface::SlotName(target_slot);
Yifan Hong9acd9cb2018-10-19 14:52:45 -0700963 return false;
964 }
Yifan Hongb9d63572020-01-09 17:50:46 -0800965
966 TEST_AND_RETURN_FALSE(prefs->SetString(kPrefsDynamicPartitionMetadataUpdated,
967 update_check_response_hash));
Yifan Hong13d41cb2019-09-16 13:18:22 -0700968 LOG(INFO) << "PreparePartitionsForUpdate done.";
Yifan Hong9acd9cb2018-10-19 14:52:45 -0700969
970 return true;
971}
972
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700973bool DeltaPerformer::CanPerformInstallOperation(
Alex Deymoa12ee112015-08-12 22:19:32 -0700974 const chromeos_update_engine::InstallOperation& operation) {
Alex Deymo0497d052016-03-23 09:16:59 -0700975 // If we don't have a data blob we can apply it right away.
976 if (!operation.has_data_offset() && !operation.has_data_length())
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700977 return true;
978
979 // See if we have the entire data blob in the buffer
980 if (operation.data_offset() < buffer_offset_) {
981 LOG(ERROR) << "we threw away data it seems?";
982 return false;
983 }
Darin Petkovd7061ab2010-10-06 14:37:09 -0700984
Gilad Arnoldfe133932014-01-14 12:25:50 -0800985 return (operation.data_offset() + operation.data_length() <=
986 buffer_offset_ + buffer_.size());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700987}
988
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700989bool DeltaPerformer::PerformReplaceOperation(
990 const InstallOperation& operation) {
Alex Deymoa12ee112015-08-12 22:19:32 -0700991 CHECK(operation.type() == InstallOperation::REPLACE ||
Alex Deymo2d621a32015-10-01 11:09:01 -0700992 operation.type() == InstallOperation::REPLACE_BZ ||
993 operation.type() == InstallOperation::REPLACE_XZ);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700994
995 // Since we delete data off the beginning of the buffer as we use it,
996 // the data we need should be exactly at the beginning of the buffer.
Darin Petkov9b230572010-10-08 10:20:09 -0700997 TEST_AND_RETURN_FALSE(buffer_offset_ == operation.data_offset());
998 TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length());
Darin Petkovd7061ab2010-10-06 14:37:09 -0700999
Alex Deymo05322872015-09-30 09:50:24 -07001000 // Setup the ExtentWriter stack based on the operation type.
Sen Jiang5e1af982018-11-01 15:01:45 -07001001 std::unique_ptr<ExtentWriter> writer = std::make_unique<DirectExtentWriter>();
Darin Petkovd7061ab2010-10-06 14:37:09 -07001002
Alex Deymo2d621a32015-10-01 11:09:01 -07001003 if (operation.type() == InstallOperation::REPLACE_BZ) {
Alex Deymo05322872015-09-30 09:50:24 -07001004 writer.reset(new BzipExtentWriter(std::move(writer)));
Alex Deymo2d621a32015-10-01 11:09:01 -07001005 } else if (operation.type() == InstallOperation::REPLACE_XZ) {
1006 writer.reset(new XzExtentWriter(std::move(writer)));
1007 }
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001008
Amin Hassanicd7edbe2017-09-18 17:05:02 -07001009 TEST_AND_RETURN_FALSE(
1010 writer->Init(target_fd_, operation.dst_extents(), block_size_));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001011 TEST_AND_RETURN_FALSE(writer->Write(buffer_.data(), operation.data_length()));
Darin Petkovd7061ab2010-10-06 14:37:09 -07001012
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001013 // Update buffer
Sen Jiangf6813802015-11-03 21:27:29 -08001014 DiscardBuffer(true, buffer_.size());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001015 return true;
1016}
1017
Alex Deymo79715ad2015-10-02 14:27:53 -07001018bool DeltaPerformer::PerformZeroOrDiscardOperation(
Alex Deymoe5e5fe92015-10-05 09:28:19 -07001019 const InstallOperation& operation) {
Alex Deymo79715ad2015-10-02 14:27:53 -07001020 CHECK(operation.type() == InstallOperation::DISCARD ||
1021 operation.type() == InstallOperation::ZERO);
1022
1023 // These operations have no blob.
1024 TEST_AND_RETURN_FALSE(!operation.has_data_offset());
1025 TEST_AND_RETURN_FALSE(!operation.has_data_length());
1026
Alex Deymo05e0e382015-12-07 20:18:16 -08001027#ifdef BLKZEROOUT
1028 bool attempt_ioctl = true;
Alex Deymo79715ad2015-10-02 14:27:53 -07001029 int request =
1030 (operation.type() == InstallOperation::ZERO ? BLKZEROOUT : BLKDISCARD);
Amin Hassani008c4582019-01-13 16:22:47 -08001031#else // !defined(BLKZEROOUT)
Alex Deymo05e0e382015-12-07 20:18:16 -08001032 bool attempt_ioctl = false;
1033 int request = 0;
1034#endif // !defined(BLKZEROOUT)
Alex Deymo79715ad2015-10-02 14:27:53 -07001035
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001036 brillo::Blob zeros;
Chih-Hung Hsieh5c6bb1d2016-07-27 13:33:15 -07001037 for (const Extent& extent : operation.dst_extents()) {
Alex Deymo79715ad2015-10-02 14:27:53 -07001038 const uint64_t start = extent.start_block() * block_size_;
1039 const uint64_t length = extent.num_blocks() * block_size_;
1040 if (attempt_ioctl) {
1041 int result = 0;
Alex Deymoe5e5fe92015-10-05 09:28:19 -07001042 if (target_fd_->BlkIoctl(request, start, length, &result) && result == 0)
Alex Deymo79715ad2015-10-02 14:27:53 -07001043 continue;
1044 attempt_ioctl = false;
Alex Deymo79715ad2015-10-02 14:27:53 -07001045 }
1046 // In case of failure, we fall back to writing 0 to the selected region.
Tianjie Xu06bbe492018-01-12 12:37:48 -08001047 zeros.resize(16 * block_size_);
Alex Deymo79715ad2015-10-02 14:27:53 -07001048 for (uint64_t offset = 0; offset < length; offset += zeros.size()) {
Amin Hassani008c4582019-01-13 16:22:47 -08001049 uint64_t chunk_length =
1050 min(length - offset, static_cast<uint64_t>(zeros.size()));
Amin Hassanid87304c2017-08-29 11:40:03 -07001051 TEST_AND_RETURN_FALSE(utils::PWriteAll(
1052 target_fd_, zeros.data(), chunk_length, start + offset));
Alex Deymo79715ad2015-10-02 14:27:53 -07001053 }
1054 }
1055 return true;
1056}
1057
Sen Jiang8371c1c2018-02-01 13:46:39 -08001058bool DeltaPerformer::ValidateSourceHash(const brillo::Blob& calculated_hash,
1059 const InstallOperation& operation,
1060 const FileDescriptorPtr source_fd,
1061 ErrorCode* error) {
Sen Jiang2ec4aab2015-11-13 15:04:03 -08001062 brillo::Blob expected_source_hash(operation.src_sha256_hash().begin(),
1063 operation.src_sha256_hash().end());
1064 if (calculated_hash != expected_source_hash) {
Alex Deymo67140842016-06-15 13:28:59 -07001065 LOG(ERROR) << "The hash of the source data on disk for this operation "
1066 << "doesn't match the expected value. This could mean that the "
1067 << "delta update payload was targeted for another version, or "
1068 << "that the source partition was modified after it was "
1069 << "installed, for example, by mounting a filesystem.";
1070 LOG(ERROR) << "Expected: sha256|hex = "
1071 << base::HexEncode(expected_source_hash.data(),
1072 expected_source_hash.size());
1073 LOG(ERROR) << "Calculated: sha256|hex = "
1074 << base::HexEncode(calculated_hash.data(),
1075 calculated_hash.size());
1076
1077 vector<string> source_extents;
1078 for (const Extent& ext : operation.src_extents()) {
Tamas Berghammer9d66d762016-07-15 14:19:04 +01001079 source_extents.push_back(
1080 base::StringPrintf("%" PRIu64 ":%" PRIu64,
1081 static_cast<uint64_t>(ext.start_block()),
1082 static_cast<uint64_t>(ext.num_blocks())));
Alex Deymo67140842016-06-15 13:28:59 -07001083 }
1084 LOG(ERROR) << "Operation source (offset:size) in blocks: "
1085 << base::JoinString(source_extents, ",");
Sen Jiangbe2c47b2016-06-15 14:09:27 -07001086
HÃ¥kan Kvist4e13cf42018-01-23 12:57:55 +01001087 // Log remount history if this device is an ext4 partition.
1088 LogMountHistory(source_fd);
1089
Sen Jiangbe2c47b2016-06-15 14:09:27 -07001090 *error = ErrorCode::kDownloadStateInitializationError;
Sen Jiang2ec4aab2015-11-13 15:04:03 -08001091 return false;
1092 }
1093 return true;
1094}
1095
Allie Wood9f6f0a52015-03-30 11:25:47 -07001096bool DeltaPerformer::PerformSourceCopyOperation(
Sen Jiangbe2c47b2016-06-15 14:09:27 -07001097 const InstallOperation& operation, ErrorCode* error) {
Allie Wood9f6f0a52015-03-30 11:25:47 -07001098 if (operation.has_src_length())
1099 TEST_AND_RETURN_FALSE(operation.src_length() % block_size_ == 0);
1100 if (operation.has_dst_length())
1101 TEST_AND_RETURN_FALSE(operation.dst_length() % block_size_ == 0);
1102
Hridya Valsarajue69ca5f2019-02-25 22:33:56 -08001103 TEST_AND_RETURN_FALSE(source_fd_ != nullptr);
1104
Alessio Balsini8d384312019-11-26 11:46:33 +00001105 // The device may optimize the SOURCE_COPY operation.
1106 // Being this a device-specific optimization let DynamicPartitionController
1107 // decide it the operation should be skipped.
Yifan Hong6eec9952019-12-04 13:12:01 -08001108 const PartitionUpdate& partition = partitions_[current_partition_];
Alessio Balsini8d384312019-11-26 11:46:33 +00001109 const auto& partition_control = boot_control_->GetDynamicPartitionControl();
Yifan Hongf5261562020-03-10 10:28:10 -07001110
1111 InstallOperation buf;
1112 bool should_optimize = partition_control->OptimizeOperation(
1113 partition.partition_name(), operation, &buf);
1114 const InstallOperation& optimized = should_optimize ? buf : operation;
Alessio Balsini8d384312019-11-26 11:46:33 +00001115
Sen Jiang2ec4aab2015-11-13 15:04:03 -08001116 if (operation.has_src_sha256_hash()) {
Alessio Balsini8d384312019-11-26 11:46:33 +00001117 bool read_ok;
Alex Deymo51c264e2016-11-04 15:49:53 -07001118 brillo::Blob source_hash;
1119 brillo::Blob expected_source_hash(operation.src_sha256_hash().begin(),
1120 operation.src_sha256_hash().end());
Sen Jiang2ec4aab2015-11-13 15:04:03 -08001121
Alex Deymo51c264e2016-11-04 15:49:53 -07001122 // We fall back to use the error corrected device if the hash of the raw
1123 // device doesn't match or there was an error reading the source partition.
1124 // Note that this code will also fall back if writing the target partition
1125 // fails.
Yifan Hongf5261562020-03-10 10:28:10 -07001126 if (should_optimize) {
1127 // Hash operation.src_extents(), then copy optimized.src_extents to
1128 // optimized.dst_extents.
1129 read_ok =
1130 fd_utils::ReadAndHashExtents(
1131 source_fd_, operation.src_extents(), block_size_, &source_hash) &&
1132 fd_utils::CopyAndHashExtents(source_fd_,
1133 optimized.src_extents(),
1134 target_fd_,
1135 optimized.dst_extents(),
1136 block_size_,
1137 nullptr /* skip hashing */);
Alessio Balsini8d384312019-11-26 11:46:33 +00001138 } else {
1139 read_ok = fd_utils::CopyAndHashExtents(source_fd_,
1140 operation.src_extents(),
1141 target_fd_,
1142 operation.dst_extents(),
1143 block_size_,
1144 &source_hash);
1145 }
Alex Deymo51c264e2016-11-04 15:49:53 -07001146 if (read_ok && expected_source_hash == source_hash)
1147 return true;
Kelvin Zhang88d10692020-07-06 10:54:03 -04001148 LOG(WARNING) << "Source hash from RAW device mismatched, attempting to "
1149 "correct using ECC";
Alex Deymo51c264e2016-11-04 15:49:53 -07001150 if (!OpenCurrentECCPartition()) {
1151 // The following function call will return false since the source hash
1152 // mismatches, but we still want to call it so it prints the appropriate
1153 // log message.
1154 return ValidateSourceHash(source_hash, operation, source_fd_, error);
1155 }
1156
1157 LOG(WARNING) << "Source hash from RAW device mismatched: found "
1158 << base::HexEncode(source_hash.data(), source_hash.size())
1159 << ", expected "
1160 << base::HexEncode(expected_source_hash.data(),
1161 expected_source_hash.size());
Yifan Hongf5261562020-03-10 10:28:10 -07001162 if (should_optimize) {
Alessio Balsini8d384312019-11-26 11:46:33 +00001163 TEST_AND_RETURN_FALSE(fd_utils::ReadAndHashExtents(
1164 source_ecc_fd_, operation.src_extents(), block_size_, &source_hash));
Yifan Hongf5261562020-03-10 10:28:10 -07001165 TEST_AND_RETURN_FALSE(
1166 fd_utils::CopyAndHashExtents(source_ecc_fd_,
1167 optimized.src_extents(),
1168 target_fd_,
1169 optimized.dst_extents(),
1170 block_size_,
1171 nullptr /* skip hashing */));
Alessio Balsini8d384312019-11-26 11:46:33 +00001172 } else {
1173 TEST_AND_RETURN_FALSE(
1174 fd_utils::CopyAndHashExtents(source_ecc_fd_,
1175 operation.src_extents(),
1176 target_fd_,
1177 operation.dst_extents(),
1178 block_size_,
1179 &source_hash));
1180 }
Alex Deymo51c264e2016-11-04 15:49:53 -07001181 TEST_AND_RETURN_FALSE(
1182 ValidateSourceHash(source_hash, operation, source_ecc_fd_, error));
1183 // At this point reading from the the error corrected device worked, but
1184 // reading from the raw device failed, so this is considered a recovered
1185 // failure.
1186 source_ecc_recovered_failures_++;
1187 } else {
1188 // When the operation doesn't include a source hash, we attempt the error
1189 // corrected device first since we can't verify the block in the raw device
1190 // at this point, but we fall back to the raw device since the error
1191 // corrected device can be shorter or not available.
Alessio Balsini8d384312019-11-26 11:46:33 +00001192
Alex Deymo51c264e2016-11-04 15:49:53 -07001193 if (OpenCurrentECCPartition() &&
1194 fd_utils::CopyAndHashExtents(source_ecc_fd_,
Yifan Hongf5261562020-03-10 10:28:10 -07001195 optimized.src_extents(),
Alex Deymo51c264e2016-11-04 15:49:53 -07001196 target_fd_,
Yifan Hongf5261562020-03-10 10:28:10 -07001197 optimized.dst_extents(),
Alex Deymo51c264e2016-11-04 15:49:53 -07001198 block_size_,
1199 nullptr)) {
1200 return true;
1201 }
1202 TEST_AND_RETURN_FALSE(fd_utils::CopyAndHashExtents(source_fd_,
Yifan Hongf5261562020-03-10 10:28:10 -07001203 optimized.src_extents(),
Alex Deymo51c264e2016-11-04 15:49:53 -07001204 target_fd_,
Yifan Hongf5261562020-03-10 10:28:10 -07001205 optimized.dst_extents(),
Alex Deymo51c264e2016-11-04 15:49:53 -07001206 block_size_,
1207 nullptr));
1208 }
Allie Wood9f6f0a52015-03-30 11:25:47 -07001209 return true;
1210}
1211
Sen Jiang77ab7bd2018-05-22 17:24:23 -07001212FileDescriptorPtr DeltaPerformer::ChooseSourceFD(
1213 const InstallOperation& operation, ErrorCode* error) {
Hridya Valsarajue69ca5f2019-02-25 22:33:56 -08001214 if (source_fd_ == nullptr) {
1215 LOG(ERROR) << "ChooseSourceFD fail: source_fd_ == nullptr";
1216 return nullptr;
1217 }
1218
Sen Jiang77ab7bd2018-05-22 17:24:23 -07001219 if (!operation.has_src_sha256_hash()) {
1220 // When the operation doesn't include a source hash, we attempt the error
1221 // corrected device first since we can't verify the block in the raw device
1222 // at this point, but we first need to make sure all extents are readable
1223 // since the error corrected device can be shorter or not available.
1224 if (OpenCurrentECCPartition() &&
1225 fd_utils::ReadAndHashExtents(
1226 source_ecc_fd_, operation.src_extents(), block_size_, nullptr)) {
1227 return source_ecc_fd_;
1228 }
1229 return source_fd_;
1230 }
1231
1232 brillo::Blob source_hash;
1233 brillo::Blob expected_source_hash(operation.src_sha256_hash().begin(),
1234 operation.src_sha256_hash().end());
1235 if (fd_utils::ReadAndHashExtents(
1236 source_fd_, operation.src_extents(), block_size_, &source_hash) &&
1237 source_hash == expected_source_hash) {
1238 return source_fd_;
1239 }
1240 // We fall back to use the error corrected device if the hash of the raw
1241 // device doesn't match or there was an error reading the source partition.
1242 if (!OpenCurrentECCPartition()) {
1243 // The following function call will return false since the source hash
1244 // mismatches, but we still want to call it so it prints the appropriate
1245 // log message.
1246 ValidateSourceHash(source_hash, operation, source_fd_, error);
1247 return nullptr;
1248 }
1249 LOG(WARNING) << "Source hash from RAW device mismatched: found "
1250 << base::HexEncode(source_hash.data(), source_hash.size())
1251 << ", expected "
1252 << base::HexEncode(expected_source_hash.data(),
1253 expected_source_hash.size());
1254
1255 if (fd_utils::ReadAndHashExtents(
1256 source_ecc_fd_, operation.src_extents(), block_size_, &source_hash) &&
1257 ValidateSourceHash(source_hash, operation, source_ecc_fd_, error)) {
1258 // At this point reading from the the error corrected device worked, but
1259 // reading from the raw device failed, so this is considered a recovered
1260 // failure.
1261 source_ecc_recovered_failures_++;
1262 return source_ecc_fd_;
1263 }
1264 return nullptr;
1265}
1266
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001267bool DeltaPerformer::ExtentsToBsdiffPositionsString(
1268 const RepeatedPtrField<Extent>& extents,
1269 uint64_t block_size,
1270 uint64_t full_length,
1271 string* positions_string) {
1272 string ret;
1273 uint64_t length = 0;
Chih-Hung Hsieh5c6bb1d2016-07-27 13:33:15 -07001274 for (const Extent& extent : extents) {
Allie Wood56873452015-03-27 17:48:40 -07001275 int64_t start = extent.start_block() * block_size;
Tamas Berghammer9d66d762016-07-15 14:19:04 +01001276 uint64_t this_length =
1277 min(full_length - length,
1278 static_cast<uint64_t>(extent.num_blocks()) * block_size);
Alex Vakulenko75039d72014-03-25 12:36:28 -07001279 ret += base::StringPrintf("%" PRIi64 ":%" PRIu64 ",", start, this_length);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001280 length += this_length;
1281 }
1282 TEST_AND_RETURN_FALSE(length == full_length);
1283 if (!ret.empty())
1284 ret.resize(ret.size() - 1); // Strip trailing comma off
1285 *positions_string = ret;
1286 return true;
1287}
1288
Amin Hassanid9cb2902017-09-20 12:11:39 -07001289namespace {
1290
1291class BsdiffExtentFile : public bsdiff::FileInterface {
1292 public:
1293 BsdiffExtentFile(std::unique_ptr<ExtentReader> reader, size_t size)
1294 : BsdiffExtentFile(std::move(reader), nullptr, size) {}
1295 BsdiffExtentFile(std::unique_ptr<ExtentWriter> writer, size_t size)
1296 : BsdiffExtentFile(nullptr, std::move(writer), size) {}
1297
1298 ~BsdiffExtentFile() override = default;
1299
1300 bool Read(void* buf, size_t count, size_t* bytes_read) override {
1301 TEST_AND_RETURN_FALSE(reader_->Read(buf, count));
1302 *bytes_read = count;
1303 offset_ += count;
1304 return true;
1305 }
1306
1307 bool Write(const void* buf, size_t count, size_t* bytes_written) override {
1308 TEST_AND_RETURN_FALSE(writer_->Write(buf, count));
1309 *bytes_written = count;
1310 offset_ += count;
1311 return true;
1312 }
1313
1314 bool Seek(off_t pos) override {
1315 if (reader_ != nullptr) {
1316 TEST_AND_RETURN_FALSE(reader_->Seek(pos));
1317 offset_ = pos;
1318 } else {
1319 // For writes technically there should be no change of position, or it
1320 // should be equivalent of current offset.
1321 TEST_AND_RETURN_FALSE(offset_ == static_cast<uint64_t>(pos));
1322 }
1323 return true;
1324 }
1325
Sen Jiang5e1af982018-11-01 15:01:45 -07001326 bool Close() override { return true; }
Amin Hassanid9cb2902017-09-20 12:11:39 -07001327
1328 bool GetSize(uint64_t* size) override {
1329 *size = size_;
1330 return true;
1331 }
1332
1333 private:
1334 BsdiffExtentFile(std::unique_ptr<ExtentReader> reader,
1335 std::unique_ptr<ExtentWriter> writer,
1336 size_t size)
1337 : reader_(std::move(reader)),
1338 writer_(std::move(writer)),
1339 size_(size),
1340 offset_(0) {}
1341
1342 std::unique_ptr<ExtentReader> reader_;
1343 std::unique_ptr<ExtentWriter> writer_;
1344 uint64_t size_;
1345 uint64_t offset_;
1346
1347 DISALLOW_COPY_AND_ASSIGN(BsdiffExtentFile);
1348};
1349
1350} // namespace
1351
Allie Wood9f6f0a52015-03-30 11:25:47 -07001352bool DeltaPerformer::PerformSourceBsdiffOperation(
Sen Jiangbe2c47b2016-06-15 14:09:27 -07001353 const InstallOperation& operation, ErrorCode* error) {
Allie Wood9f6f0a52015-03-30 11:25:47 -07001354 // Since we delete data off the beginning of the buffer as we use it,
1355 // the data we need should be exactly at the beginning of the buffer.
1356 TEST_AND_RETURN_FALSE(buffer_offset_ == operation.data_offset());
1357 TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length());
1358 if (operation.has_src_length())
1359 TEST_AND_RETURN_FALSE(operation.src_length() % block_size_ == 0);
1360 if (operation.has_dst_length())
1361 TEST_AND_RETURN_FALSE(operation.dst_length() % block_size_ == 0);
1362
Sen Jiang77ab7bd2018-05-22 17:24:23 -07001363 FileDescriptorPtr source_fd = ChooseSourceFD(operation, error);
1364 TEST_AND_RETURN_FALSE(source_fd != nullptr);
Sen Jiang2ec4aab2015-11-13 15:04:03 -08001365
Amin Hassanid9cb2902017-09-20 12:11:39 -07001366 auto reader = std::make_unique<DirectExtentReader>();
1367 TEST_AND_RETURN_FALSE(
Sen Jiang77ab7bd2018-05-22 17:24:23 -07001368 reader->Init(source_fd, operation.src_extents(), block_size_));
Amin Hassanid9cb2902017-09-20 12:11:39 -07001369 auto src_file = std::make_unique<BsdiffExtentFile>(
1370 std::move(reader),
1371 utils::BlocksInExtents(operation.src_extents()) * block_size_);
Allie Wood9f6f0a52015-03-30 11:25:47 -07001372
Amin Hassanid9cb2902017-09-20 12:11:39 -07001373 auto writer = std::make_unique<DirectExtentWriter>();
1374 TEST_AND_RETURN_FALSE(
1375 writer->Init(target_fd_, operation.dst_extents(), block_size_));
1376 auto dst_file = std::make_unique<BsdiffExtentFile>(
1377 std::move(writer),
1378 utils::BlocksInExtents(operation.dst_extents()) * block_size_);
1379
1380 TEST_AND_RETURN_FALSE(bsdiff::bspatch(std::move(src_file),
1381 std::move(dst_file),
Sen Jiang7a659092016-05-10 15:31:46 -07001382 buffer_.data(),
Amin Hassanid9cb2902017-09-20 12:11:39 -07001383 buffer_.size()) == 0);
Sen Jiangbc3e6b02016-01-19 18:39:26 +08001384 DiscardBuffer(true, buffer_.size());
1385 return true;
1386}
1387
Amin Hassani02855c22017-09-06 22:34:50 -07001388namespace {
1389
1390// A class to be passed to |puffpatch| for reading from |source_fd_| and writing
1391// into |target_fd_|.
1392class PuffinExtentStream : public puffin::StreamInterface {
1393 public:
1394 // Constructor for creating a stream for reading from an |ExtentReader|.
Amin Hassani678a68f2018-02-28 11:54:47 -08001395 PuffinExtentStream(std::unique_ptr<ExtentReader> reader, uint64_t size)
Amin Hassani02855c22017-09-06 22:34:50 -07001396 : PuffinExtentStream(std::move(reader), nullptr, size) {}
1397
1398 // Constructor for creating a stream for writing to an |ExtentWriter|.
Amin Hassani678a68f2018-02-28 11:54:47 -08001399 PuffinExtentStream(std::unique_ptr<ExtentWriter> writer, uint64_t size)
Amin Hassani02855c22017-09-06 22:34:50 -07001400 : PuffinExtentStream(nullptr, std::move(writer), size) {}
1401
1402 ~PuffinExtentStream() override = default;
1403
Amin Hassani678a68f2018-02-28 11:54:47 -08001404 bool GetSize(uint64_t* size) const override {
Amin Hassani02855c22017-09-06 22:34:50 -07001405 *size = size_;
1406 return true;
1407 }
1408
Amin Hassani678a68f2018-02-28 11:54:47 -08001409 bool GetOffset(uint64_t* offset) const override {
Amin Hassani02855c22017-09-06 22:34:50 -07001410 *offset = offset_;
1411 return true;
1412 }
1413
Amin Hassani678a68f2018-02-28 11:54:47 -08001414 bool Seek(uint64_t offset) override {
Amin Hassani02855c22017-09-06 22:34:50 -07001415 if (is_read_) {
1416 TEST_AND_RETURN_FALSE(reader_->Seek(offset));
1417 offset_ = offset;
1418 } else {
1419 // For writes technically there should be no change of position, or it
1420 // should equivalent of current offset.
1421 TEST_AND_RETURN_FALSE(offset_ == offset);
1422 }
1423 return true;
1424 }
1425
1426 bool Read(void* buffer, size_t count) override {
1427 TEST_AND_RETURN_FALSE(is_read_);
1428 TEST_AND_RETURN_FALSE(reader_->Read(buffer, count));
1429 offset_ += count;
1430 return true;
1431 }
1432
1433 bool Write(const void* buffer, size_t count) override {
1434 TEST_AND_RETURN_FALSE(!is_read_);
1435 TEST_AND_RETURN_FALSE(writer_->Write(buffer, count));
1436 offset_ += count;
1437 return true;
1438 }
1439
Sen Jiang5e1af982018-11-01 15:01:45 -07001440 bool Close() override { return true; }
Amin Hassani02855c22017-09-06 22:34:50 -07001441
1442 private:
1443 PuffinExtentStream(std::unique_ptr<ExtentReader> reader,
1444 std::unique_ptr<ExtentWriter> writer,
Amin Hassani678a68f2018-02-28 11:54:47 -08001445 uint64_t size)
Amin Hassani02855c22017-09-06 22:34:50 -07001446 : reader_(std::move(reader)),
1447 writer_(std::move(writer)),
1448 size_(size),
1449 offset_(0),
1450 is_read_(reader_ ? true : false) {}
1451
1452 std::unique_ptr<ExtentReader> reader_;
1453 std::unique_ptr<ExtentWriter> writer_;
1454 uint64_t size_;
1455 uint64_t offset_;
1456 bool is_read_;
1457
1458 DISALLOW_COPY_AND_ASSIGN(PuffinExtentStream);
1459};
1460
1461} // namespace
1462
1463bool DeltaPerformer::PerformPuffDiffOperation(const InstallOperation& operation,
1464 ErrorCode* error) {
1465 // Since we delete data off the beginning of the buffer as we use it,
1466 // the data we need should be exactly at the beginning of the buffer.
1467 TEST_AND_RETURN_FALSE(buffer_offset_ == operation.data_offset());
1468 TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length());
1469
Sen Jiang77ab7bd2018-05-22 17:24:23 -07001470 FileDescriptorPtr source_fd = ChooseSourceFD(operation, error);
1471 TEST_AND_RETURN_FALSE(source_fd != nullptr);
Amin Hassani02855c22017-09-06 22:34:50 -07001472
1473 auto reader = std::make_unique<DirectExtentReader>();
1474 TEST_AND_RETURN_FALSE(
Sen Jiang77ab7bd2018-05-22 17:24:23 -07001475 reader->Init(source_fd, operation.src_extents(), block_size_));
Amin Hassani02855c22017-09-06 22:34:50 -07001476 puffin::UniqueStreamPtr src_stream(new PuffinExtentStream(
1477 std::move(reader),
1478 utils::BlocksInExtents(operation.src_extents()) * block_size_));
1479
1480 auto writer = std::make_unique<DirectExtentWriter>();
1481 TEST_AND_RETURN_FALSE(
1482 writer->Init(target_fd_, operation.dst_extents(), block_size_));
1483 puffin::UniqueStreamPtr dst_stream(new PuffinExtentStream(
1484 std::move(writer),
1485 utils::BlocksInExtents(operation.dst_extents()) * block_size_));
1486
1487 const size_t kMaxCacheSize = 5 * 1024 * 1024; // Total 5MB cache.
1488 TEST_AND_RETURN_FALSE(puffin::PuffPatch(std::move(src_stream),
1489 std::move(dst_stream),
1490 buffer_.data(),
1491 buffer_.size(),
1492 kMaxCacheSize));
Allie Wood9f6f0a52015-03-30 11:25:47 -07001493 DiscardBuffer(true, buffer_.size());
Alex Deymoa12ee112015-08-12 22:19:32 -07001494 return true;
1495}
Darin Petkovd7061ab2010-10-06 14:37:09 -07001496
Sen Jiangf6813802015-11-03 21:27:29 -08001497bool DeltaPerformer::ExtractSignatureMessage() {
Darin Petkovd7061ab2010-10-06 14:37:09 -07001498 TEST_AND_RETURN_FALSE(signatures_message_data_.empty());
1499 TEST_AND_RETURN_FALSE(buffer_offset_ == manifest_.signatures_offset());
1500 TEST_AND_RETURN_FALSE(buffer_.size() >= manifest_.signatures_size());
Darin Petkov4f0a07b2011-05-25 16:47:20 -07001501 signatures_message_data_.assign(
Amin Hassani008c4582019-01-13 16:22:47 -08001502 buffer_.begin(), buffer_.begin() + manifest_.signatures_size());
Darin Petkov4f0a07b2011-05-25 16:47:20 -07001503
1504 // Save the signature blob because if the update is interrupted after the
1505 // download phase we don't go through this path anymore. Some alternatives to
1506 // consider:
1507 //
1508 // 1. On resume, re-download the signature blob from the server and re-verify
1509 // it.
1510 //
1511 // 2. Verify the signature as soon as it's received and don't checkpoint the
1512 // blob and the signed sha-256 context.
Amin Hassani008c4582019-01-13 16:22:47 -08001513 LOG_IF(WARNING,
1514 !prefs_->SetString(kPrefsUpdateStateSignatureBlob,
Sen Jiang9b2f1782019-01-24 14:27:50 -08001515 signatures_message_data_))
Darin Petkov4f0a07b2011-05-25 16:47:20 -07001516 << "Unable to store the signature blob.";
Sen Jiangf6813802015-11-03 21:27:29 -08001517
Darin Petkovd7061ab2010-10-06 14:37:09 -07001518 LOG(INFO) << "Extracted signature data of size "
1519 << manifest_.signatures_size() << " at "
1520 << manifest_.signatures_offset();
1521 return true;
1522}
1523
Sen Jiang08c6da12019-01-07 18:28:56 -08001524bool DeltaPerformer::GetPublicKey(string* out_public_key) {
1525 out_public_key->clear();
David Zeuthene7f89172013-10-31 10:21:04 -07001526
Sen Jiang08c6da12019-01-07 18:28:56 -08001527 if (utils::FileExists(public_key_path_.c_str())) {
1528 LOG(INFO) << "Verifying using public key: " << public_key_path_;
1529 return utils::ReadFile(public_key_path_, out_public_key);
1530 }
1531
1532 // If this is an official build then we are not allowed to use public key from
1533 // Omaha response.
1534 if (!hardware_->IsOfficialBuild() && !install_plan_->public_key_rsa.empty()) {
1535 LOG(INFO) << "Verifying using public key from Omaha response.";
1536 return brillo::data_encoding::Base64Decode(install_plan_->public_key_rsa,
1537 out_public_key);
1538 }
Tianjie Xu7a78d632019-10-08 16:32:39 -07001539 LOG(INFO) << "No public keys found for verification.";
David Zeuthene7f89172013-10-31 10:21:04 -07001540 return true;
1541}
1542
Tianjie Xu7a78d632019-10-08 16:32:39 -07001543std::pair<std::unique_ptr<PayloadVerifier>, bool>
1544DeltaPerformer::CreatePayloadVerifier() {
1545 if (utils::FileExists(update_certificates_path_.c_str())) {
1546 LOG(INFO) << "Verifying using certificates: " << update_certificates_path_;
1547 return {
1548 PayloadVerifier::CreateInstanceFromZipPath(update_certificates_path_),
1549 true};
1550 }
Don Garrettb8dd1d92013-11-22 17:40:02 -08001551
Tianjie Xu7a78d632019-10-08 16:32:39 -07001552 string public_key;
1553 if (!GetPublicKey(&public_key)) {
1554 LOG(ERROR) << "Failed to read public key";
1555 return {nullptr, true};
1556 }
1557
1558 // Skips the verification if the public key is empty.
1559 if (public_key.empty()) {
1560 return {nullptr, false};
1561 }
1562 return {PayloadVerifier::CreateInstance(public_key), true};
1563}
1564
Don Garrettb8dd1d92013-11-22 17:40:02 -08001565ErrorCode DeltaPerformer::ValidateManifest() {
Saint Chou0a92a622020-07-29 14:25:35 +00001566 // Perform assorted checks to validation check the manifest, make sure it
Don Garrettb8dd1d92013-11-22 17:40:02 -08001567 // matches data from other sources, and that it is a supported version.
Amin Hassani822d4852020-03-10 01:50:42 +00001568 bool has_old_fields = std::any_of(manifest_.partitions().begin(),
1569 manifest_.partitions().end(),
1570 [](const PartitionUpdate& partition) {
1571 return partition.has_old_partition_info();
1572 });
Sen Jiangc8f6b7a2015-10-21 11:09:59 -07001573
Alex Deymo64d98782016-02-05 18:03:48 -08001574 // The presence of an old partition hash is the sole indicator for a delta
Tianjief5baff42020-07-17 21:43:22 -07001575 // update. Also, always treat the partial update as delta so that we can
1576 // perform the minor version check correctly.
Alex Deymo64d98782016-02-05 18:03:48 -08001577 InstallPayloadType actual_payload_type =
Tianjief5baff42020-07-17 21:43:22 -07001578 (has_old_fields || manifest_.partial_update())
1579 ? InstallPayloadType::kDelta
1580 : InstallPayloadType::kFull;
Alex Deymo64d98782016-02-05 18:03:48 -08001581
Sen Jiangcdd52062017-05-18 15:33:10 -07001582 if (payload_->type == InstallPayloadType::kUnknown) {
Alex Deymo64d98782016-02-05 18:03:48 -08001583 LOG(INFO) << "Detected a '"
1584 << InstallPayloadTypeToString(actual_payload_type)
1585 << "' payload.";
Sen Jiangcdd52062017-05-18 15:33:10 -07001586 payload_->type = actual_payload_type;
1587 } else if (payload_->type != actual_payload_type) {
Alex Deymo64d98782016-02-05 18:03:48 -08001588 LOG(ERROR) << "InstallPlan expected a '"
Sen Jiangcdd52062017-05-18 15:33:10 -07001589 << InstallPayloadTypeToString(payload_->type)
Alex Deymo64d98782016-02-05 18:03:48 -08001590 << "' payload but the downloaded manifest contains a '"
1591 << InstallPayloadTypeToString(actual_payload_type)
1592 << "' payload.";
1593 return ErrorCode::kPayloadMismatchedType;
1594 }
Alex Deymo64d98782016-02-05 18:03:48 -08001595 // Check that the minor version is compatible.
Tianjied3865d12020-06-03 15:25:17 -07001596 // TODO(xunchang) increment minor version & add check for partial update
Alex Deymo64d98782016-02-05 18:03:48 -08001597 if (actual_payload_type == InstallPayloadType::kFull) {
Don Garrettb8dd1d92013-11-22 17:40:02 -08001598 if (manifest_.minor_version() != kFullPayloadMinorVersion) {
1599 LOG(ERROR) << "Manifest contains minor version "
1600 << manifest_.minor_version()
1601 << ", but all full payloads should have version "
1602 << kFullPayloadMinorVersion << ".";
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001603 return ErrorCode::kUnsupportedMinorPayloadVersion;
Don Garrettb8dd1d92013-11-22 17:40:02 -08001604 }
1605 } else {
Sen Jiangf1236632018-05-11 16:03:23 -07001606 if (manifest_.minor_version() < kMinSupportedMinorPayloadVersion ||
1607 manifest_.minor_version() > kMaxSupportedMinorPayloadVersion) {
Don Garrettb8dd1d92013-11-22 17:40:02 -08001608 LOG(ERROR) << "Manifest contains minor version "
1609 << manifest_.minor_version()
Sen Jiangf1236632018-05-11 16:03:23 -07001610 << " not in the range of supported minor versions ["
1611 << kMinSupportedMinorPayloadVersion << ", "
1612 << kMaxSupportedMinorPayloadVersion << "].";
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001613 return ErrorCode::kUnsupportedMinorPayloadVersion;
Don Garrettb8dd1d92013-11-22 17:40:02 -08001614 }
Gilad Arnold21504f02013-05-24 08:51:22 -07001615 }
1616
Yifan Hong87029332020-09-01 17:20:08 -07001617 ErrorCode error_code = CheckTimestampError();
1618 if (error_code != ErrorCode::kSuccess) {
1619 if (error_code == ErrorCode::kPayloadTimestampError) {
1620 if (!hardware_->AllowDowngrade()) {
1621 return ErrorCode::kPayloadTimestampError;
1622 }
1623 LOG(INFO) << "The current OS build allows downgrade, continuing to apply"
1624 " the payload with an older timestamp.";
1625 } else {
1626 LOG(ERROR) << "Timestamp check returned "
1627 << utils::ErrorCodeToString(error_code);
1628 return error_code;
Tianjie Xu4ad3af62019-10-30 11:59:45 -07001629 }
Sen Jiang8e768e92017-06-28 17:13:19 -07001630 }
1631
Amin Hassani55c75412019-10-07 11:20:39 -07001632 // TODO(crbug.com/37661) we should be adding more and more manifest checks,
1633 // such as partition boundaries, etc.
Gilad Arnold21504f02013-05-24 08:51:22 -07001634
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001635 return ErrorCode::kSuccess;
Gilad Arnold21504f02013-05-24 08:51:22 -07001636}
1637
Yifan Hong87029332020-09-01 17:20:08 -07001638ErrorCode DeltaPerformer::CheckTimestampError() const {
Kelvin Zhangd7191032020-08-11 10:48:16 -04001639 bool is_partial_update =
1640 manifest_.has_partial_update() && manifest_.partial_update();
1641 const auto& partitions = manifest_.partitions();
Yifan Hong87029332020-09-01 17:20:08 -07001642
1643 // Check version field for a given PartitionUpdate object. If an error
1644 // is encountered, set |error_code| accordingly. If downgrade is detected,
1645 // |downgrade_detected| is set. Return true if the program should continue to
1646 // check the next partition or not, or false if it should exit early due to
1647 // errors.
1648 auto&& timestamp_valid = [this](const PartitionUpdate& partition,
1649 bool allow_empty_version,
1650 bool* downgrade_detected) -> ErrorCode {
1651 if (!partition.has_version()) {
1652 if (allow_empty_version) {
1653 return ErrorCode::kSuccess;
1654 }
1655 LOG(ERROR)
1656 << "PartitionUpdate " << partition.partition_name()
1657 << " does ot have a version field. Not allowed in partial updates.";
1658 return ErrorCode::kDownloadManifestParseError;
1659 }
1660
1661 auto error_code = hardware_->IsPartitionUpdateValid(
1662 partition.partition_name(), partition.version());
1663 switch (error_code) {
1664 case ErrorCode::kSuccess:
1665 break;
1666 case ErrorCode::kPayloadTimestampError:
1667 *downgrade_detected = true;
1668 LOG(WARNING) << "PartitionUpdate " << partition.partition_name()
1669 << " has an older version than partition on device.";
1670 break;
1671 default:
1672 LOG(ERROR) << "IsPartitionUpdateValid(" << partition.partition_name()
1673 << ") returned" << utils::ErrorCodeToString(error_code);
1674 break;
1675 }
1676 return error_code;
Kelvin Zhangd7191032020-08-11 10:48:16 -04001677 };
Yifan Hong87029332020-09-01 17:20:08 -07001678
1679 bool downgrade_detected = false;
1680
Kelvin Zhangd7191032020-08-11 10:48:16 -04001681 if (is_partial_update) {
1682 // for partial updates, all partition MUST have valid timestamps
1683 // But max_timestamp can be empty
1684 for (const auto& partition : partitions) {
Yifan Hong87029332020-09-01 17:20:08 -07001685 auto error_code = timestamp_valid(
1686 partition, false /* allow_empty_version */, &downgrade_detected);
1687 if (error_code != ErrorCode::kSuccess &&
1688 error_code != ErrorCode::kPayloadTimestampError) {
1689 return error_code;
Kelvin Zhangd7191032020-08-11 10:48:16 -04001690 }
1691 }
Yifan Hong87029332020-09-01 17:20:08 -07001692 if (downgrade_detected) {
1693 return ErrorCode::kPayloadTimestampError;
1694 }
1695 return ErrorCode::kSuccess;
Kelvin Zhangd7191032020-08-11 10:48:16 -04001696 }
Yifan Hong87029332020-09-01 17:20:08 -07001697
1698 // For non-partial updates, check max_timestamp first.
Jay Srinivasan738fdf32012-12-07 17:40:54 -08001699 if (manifest_.max_timestamp() < hardware_->GetBuildTimestamp()) {
1700 LOG(ERROR) << "The current OS build timestamp ("
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001701 << hardware_->GetBuildTimestamp()
Jay Srinivasan738fdf32012-12-07 17:40:54 -08001702 << ") is newer than the maximum timestamp in the manifest ("
1703 << manifest_.max_timestamp() << ")";
1704 return ErrorCode::kPayloadTimestampError;
1705 }
Kelvin Zhangd7191032020-08-11 10:48:16 -04001706 // Otherwise... partitions can have empty timestamps.
1707 for (const auto& partition : partitions) {
Yifan Hong87029332020-09-01 17:20:08 -07001708 auto error_code = timestamp_valid(
1709 partition, true /* allow_empty_version */, &downgrade_detected);
1710 if (error_code != ErrorCode::kSuccess &&
1711 error_code != ErrorCode::kPayloadTimestampError) {
1712 return error_code;
Kelvin Zhangd7191032020-08-11 10:48:16 -04001713 }
1714 }
Yifan Hong87029332020-09-01 17:20:08 -07001715 if (downgrade_detected) {
1716 return ErrorCode::kPayloadTimestampError;
1717 }
Jay Srinivasan738fdf32012-12-07 17:40:54 -08001718 return ErrorCode::kSuccess;
1719}
1720
1721ErrorCode DeltaPerformer::ValidateOperationHash(
Alex Deymoa12ee112015-08-12 22:19:32 -07001722 const InstallOperation& operation) {
Jay Srinivasan00f76b62012-09-17 18:48:36 -07001723 if (!operation.data_sha256_hash().size()) {
1724 if (!operation.data_length()) {
1725 // Operations that do not have any data blob won't have any operation hash
1726 // either. So, these operations are always considered validated since the
Jay Srinivasanf0572052012-10-23 18:12:56 -07001727 // metadata that contains all the non-data-blob portions of the operation
1728 // has already been validated. This is true for both HTTP and HTTPS cases.
Jay Srinivasanedce2832012-10-24 18:57:47 -07001729 return ErrorCode::kSuccess;
Jay Srinivasanf0572052012-10-23 18:12:56 -07001730 }
Jay Srinivasan00f76b62012-09-17 18:48:36 -07001731
1732 // No hash is present for an operation that has data blobs. This shouldn't
1733 // happen normally for any client that has this code, because the
1734 // corresponding update should have been produced with the operation
1735 // hashes. So if it happens it means either we've turned operation hash
Jay Srinivasanedce2832012-10-24 18:57:47 -07001736 // generation off in DeltaDiffGenerator or it's a regression of some sort.
Saint Chou0a92a622020-07-29 14:25:35 +00001737 // One caveat though: The last operation is a unused signature operation
Jay Srinivasan00f76b62012-09-17 18:48:36 -07001738 // that doesn't have a hash at the time the manifest is created. So we
1739 // should not complaint about that operation. This operation can be
1740 // recognized by the fact that it's offset is mentioned in the manifest.
1741 if (manifest_.signatures_offset() &&
1742 manifest_.signatures_offset() == operation.data_offset()) {
1743 LOG(INFO) << "Skipping hash verification for signature operation "
1744 << next_operation_num_ + 1;
1745 } else {
Jay Srinivasan738fdf32012-12-07 17:40:54 -08001746 if (install_plan_->hash_checks_mandatory) {
1747 LOG(ERROR) << "Missing mandatory operation hash for operation "
1748 << next_operation_num_ + 1;
1749 return ErrorCode::kDownloadOperationHashMissingError;
1750 }
1751
Jay Srinivasan738fdf32012-12-07 17:40:54 -08001752 LOG(WARNING) << "Cannot validate operation " << next_operation_num_ + 1
1753 << " as there's no operation hash in manifest";
Jay Srinivasan00f76b62012-09-17 18:48:36 -07001754 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001755 return ErrorCode::kSuccess;
Jay Srinivasan00f76b62012-09-17 18:48:36 -07001756 }
1757
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001758 brillo::Blob expected_op_hash;
Jay Srinivasan00f76b62012-09-17 18:48:36 -07001759 expected_op_hash.assign(operation.data_sha256_hash().data(),
1760 (operation.data_sha256_hash().data() +
1761 operation.data_sha256_hash().size()));
1762
Sen Jiang2703ef42017-03-16 13:36:21 -07001763 brillo::Blob calculated_op_hash;
1764 if (!HashCalculator::RawHashOfBytes(
1765 buffer_.data(), operation.data_length(), &calculated_op_hash)) {
Jay Srinivasan00f76b62012-09-17 18:48:36 -07001766 LOG(ERROR) << "Unable to compute actual hash of operation "
1767 << next_operation_num_;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001768 return ErrorCode::kDownloadOperationHashVerificationError;
Jay Srinivasan00f76b62012-09-17 18:48:36 -07001769 }
1770
Jay Srinivasan00f76b62012-09-17 18:48:36 -07001771 if (calculated_op_hash != expected_op_hash) {
1772 LOG(ERROR) << "Hash verification failed for operation "
1773 << next_operation_num_ << ". Expected hash = ";
1774 utils::HexDumpVector(expected_op_hash);
1775 LOG(ERROR) << "Calculated hash over " << operation.data_length()
1776 << " bytes at offset: " << operation.data_offset() << " = ";
1777 utils::HexDumpVector(calculated_op_hash);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001778 return ErrorCode::kDownloadOperationHashMismatch;
Jay Srinivasan00f76b62012-09-17 18:48:36 -07001779 }
1780
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001781 return ErrorCode::kSuccess;
Jay Srinivasan51dcf262012-09-13 17:24:32 -07001782}
1783
Amin Hassani008c4582019-01-13 16:22:47 -08001784#define TEST_AND_RETURN_VAL(_retval, _condition) \
1785 do { \
1786 if (!(_condition)) { \
1787 LOG(ERROR) << "VerifyPayload failure: " << #_condition; \
1788 return _retval; \
1789 } \
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -07001790 } while (0);
Andrew de los Reyesfb830ba2011-04-04 11:42:43 -07001791
David Zeuthena99981f2013-04-29 13:42:47 -07001792ErrorCode DeltaPerformer::VerifyPayload(
Sen Jiang2703ef42017-03-16 13:36:21 -07001793 const brillo::Blob& update_check_response_hash,
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -07001794 const uint64_t update_check_response_size) {
Jay Srinivasan0d8fb402012-05-07 19:19:38 -07001795 // Verifies the download size.
Sen Jiang3a25dc22019-01-10 14:14:41 -08001796 if (update_check_response_size !=
1797 metadata_size_ + metadata_signature_size_ + buffer_offset_) {
1798 LOG(ERROR) << "update_check_response_size (" << update_check_response_size
1799 << ") doesn't match metadata_size (" << metadata_size_
1800 << ") + metadata_signature_size (" << metadata_signature_size_
1801 << ") + buffer_offset (" << buffer_offset_ << ").";
1802 return ErrorCode::kPayloadSizeMismatchError;
1803 }
Jay Srinivasan0d8fb402012-05-07 19:19:38 -07001804
Amin Hassanie331f5a2020-10-06 15:53:29 -07001805 // Verifies the payload hash.
1806 TEST_AND_RETURN_VAL(ErrorCode::kDownloadPayloadVerificationError,
1807 !payload_hash_calculator_.raw_hash().empty());
1808 TEST_AND_RETURN_VAL(
1809 ErrorCode::kPayloadHashMismatchError,
1810 payload_hash_calculator_.raw_hash() == update_check_response_hash);
1811
Amin Hassanif624e112020-09-15 15:30:08 -07001812 auto [payload_verifier, perform_verification] = CreatePayloadVerifier();
1813 if (!perform_verification) {
1814 LOG(WARNING) << "Not verifying signed delta payload -- missing public key.";
1815 return ErrorCode::kSuccess;
1816 }
1817 if (!payload_verifier) {
1818 LOG(ERROR) << "Failed to create the payload verifier.";
1819 return ErrorCode::kDownloadPayloadPubKeyVerificationError;
1820 }
1821
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001822 TEST_AND_RETURN_VAL(ErrorCode::kSignedDeltaPayloadExpectedError,
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -07001823 !signatures_message_data_.empty());
Sen Jiangf6813802015-11-03 21:27:29 -08001824 brillo::Blob hash_data = signed_hash_calculator_.raw_hash();
Alex Deymob552a682015-09-30 09:36:49 -07001825 TEST_AND_RETURN_VAL(ErrorCode::kDownloadPayloadPubKeyVerificationError,
xunchangcda3c032019-03-26 15:41:14 -07001826 hash_data.size() == kSHA256Size);
Alex Deymob552a682015-09-30 09:36:49 -07001827
Tianjie Xu6cf830b2019-09-30 11:31:49 -07001828 if (!payload_verifier->VerifySignature(signatures_message_data_, hash_data)) {
David Zeuthenbc27aac2013-11-26 11:17:48 -08001829 // The autoupdate_CatchBadSignatures test checks for this string
1830 // in log-files. Keep in sync.
Alex Deymob552a682015-09-30 09:36:49 -07001831 LOG(ERROR) << "Public key verification failed, thus update failed.";
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001832 return ErrorCode::kDownloadPayloadPubKeyVerificationError;
Andrew de los Reyesfb830ba2011-04-04 11:42:43 -07001833 }
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -08001834
David Zeuthene7f89172013-10-31 10:21:04 -07001835 LOG(INFO) << "Payload hash matches value in payload.";
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001836 return ErrorCode::kSuccess;
Darin Petkovd7061ab2010-10-06 14:37:09 -07001837}
1838
Sen Jiangf6813802015-11-03 21:27:29 -08001839void DeltaPerformer::DiscardBuffer(bool do_advance_offset,
1840 size_t signed_hash_buffer_size) {
Gilad Arnoldfe133932014-01-14 12:25:50 -08001841 // Update the buffer offset.
Gilad Arnolddaa27402014-01-23 11:56:17 -08001842 if (do_advance_offset)
Gilad Arnoldfe133932014-01-14 12:25:50 -08001843 buffer_offset_ += buffer_.size();
1844
1845 // Hash the content.
Sen Jiangf6813802015-11-03 21:27:29 -08001846 payload_hash_calculator_.Update(buffer_.data(), buffer_.size());
1847 signed_hash_calculator_.Update(buffer_.data(), signed_hash_buffer_size);
Gilad Arnoldfe133932014-01-14 12:25:50 -08001848
1849 // Swap content with an empty vector to ensure that all memory is released.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001850 brillo::Blob().swap(buffer_);
Darin Petkovd7061ab2010-10-06 14:37:09 -07001851}
1852
Darin Petkov0406e402010-10-06 21:33:11 -07001853bool DeltaPerformer::CanResumeUpdate(PrefsInterface* prefs,
Chih-Hung Hsieh5c6bb1d2016-07-27 13:33:15 -07001854 const string& update_check_response_hash) {
Darin Petkov0406e402010-10-06 21:33:11 -07001855 int64_t next_operation = kUpdateStateOperationInvalid;
Alex Deymo3310b222015-03-30 15:59:07 -07001856 if (!(prefs->GetInt64(kPrefsUpdateStateNextOperation, &next_operation) &&
Amin Hassani008c4582019-01-13 16:22:47 -08001857 next_operation != kUpdateStateOperationInvalid && next_operation > 0))
Alex Deymo3310b222015-03-30 15:59:07 -07001858 return false;
Darin Petkov0406e402010-10-06 21:33:11 -07001859
1860 string interrupted_hash;
Alex Deymo3310b222015-03-30 15:59:07 -07001861 if (!(prefs->GetString(kPrefsUpdateCheckResponseHash, &interrupted_hash) &&
1862 !interrupted_hash.empty() &&
1863 interrupted_hash == update_check_response_hash))
1864 return false;
Darin Petkov0406e402010-10-06 21:33:11 -07001865
Darin Petkov61426142010-10-08 11:04:55 -07001866 int64_t resumed_update_failures;
Alex Deymof25eb492016-02-26 00:20:08 -08001867 // Note that storing this value is optional, but if it is there it should not
1868 // be more than the limit.
1869 if (prefs->GetInt64(kPrefsResumedUpdateFailures, &resumed_update_failures) &&
1870 resumed_update_failures > kMaxResumedUpdateFailures)
Alex Deymo3310b222015-03-30 15:59:07 -07001871 return false;
Darin Petkov61426142010-10-08 11:04:55 -07001872
Saint Chou0a92a622020-07-29 14:25:35 +00001873 // Validation check the rest.
Darin Petkov0406e402010-10-06 21:33:11 -07001874 int64_t next_data_offset = -1;
Alex Deymo3310b222015-03-30 15:59:07 -07001875 if (!(prefs->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset) &&
1876 next_data_offset >= 0))
1877 return false;
Darin Petkov0406e402010-10-06 21:33:11 -07001878
Darin Petkov437adc42010-10-07 13:12:24 -07001879 string sha256_context;
Alex Deymo3310b222015-03-30 15:59:07 -07001880 if (!(prefs->GetString(kPrefsUpdateStateSHA256Context, &sha256_context) &&
1881 !sha256_context.empty()))
1882 return false;
Darin Petkov0406e402010-10-06 21:33:11 -07001883
1884 int64_t manifest_metadata_size = 0;
Alex Deymo3310b222015-03-30 15:59:07 -07001885 if (!(prefs->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size) &&
1886 manifest_metadata_size > 0))
1887 return false;
Darin Petkov0406e402010-10-06 21:33:11 -07001888
Alex Deymof25eb492016-02-26 00:20:08 -08001889 int64_t manifest_signature_size = 0;
1890 if (!(prefs->GetInt64(kPrefsManifestSignatureSize,
1891 &manifest_signature_size) &&
1892 manifest_signature_size >= 0))
1893 return false;
1894
Darin Petkov0406e402010-10-06 21:33:11 -07001895 return true;
1896}
1897
Yifan Hong55c2bfe2020-01-13 17:01:19 -08001898bool DeltaPerformer::ResetUpdateProgress(
1899 PrefsInterface* prefs,
1900 bool quick,
1901 bool skip_dynamic_partititon_metadata_updated) {
Darin Petkov0406e402010-10-06 21:33:11 -07001902 TEST_AND_RETURN_FALSE(prefs->SetInt64(kPrefsUpdateStateNextOperation,
1903 kUpdateStateOperationInvalid));
Darin Petkov9b230572010-10-08 10:20:09 -07001904 if (!quick) {
Darin Petkov9b230572010-10-08 10:20:09 -07001905 prefs->SetInt64(kPrefsUpdateStateNextDataOffset, -1);
David Zeuthen41996ad2013-09-24 15:43:24 -07001906 prefs->SetInt64(kPrefsUpdateStateNextDataLength, 0);
Darin Petkov9b230572010-10-08 10:20:09 -07001907 prefs->SetString(kPrefsUpdateStateSHA256Context, "");
1908 prefs->SetString(kPrefsUpdateStateSignedSHA256Context, "");
Darin Petkov4f0a07b2011-05-25 16:47:20 -07001909 prefs->SetString(kPrefsUpdateStateSignatureBlob, "");
Darin Petkov9b230572010-10-08 10:20:09 -07001910 prefs->SetInt64(kPrefsManifestMetadataSize, -1);
Alex Deymof25eb492016-02-26 00:20:08 -08001911 prefs->SetInt64(kPrefsManifestSignatureSize, -1);
Darin Petkov61426142010-10-08 11:04:55 -07001912 prefs->SetInt64(kPrefsResumedUpdateFailures, 0);
Sen Jiangfe522822017-10-31 15:14:11 -07001913 prefs->Delete(kPrefsPostInstallSucceeded);
Sen Jiang3eeaf7d2018-10-11 13:55:32 -07001914 prefs->Delete(kPrefsVerityWritten);
Yifan Hong55c2bfe2020-01-13 17:01:19 -08001915
1916 if (!skip_dynamic_partititon_metadata_updated) {
1917 LOG(INFO) << "Resetting recorded hash for prepared partitions.";
1918 prefs->Delete(kPrefsDynamicPartitionMetadataUpdated);
1919 }
Darin Petkov9b230572010-10-08 10:20:09 -07001920 }
Darin Petkov73058b42010-10-06 16:32:19 -07001921 return true;
1922}
1923
Sen Jiang3a25dc22019-01-10 14:14:41 -08001924bool DeltaPerformer::CheckpointUpdateProgress(bool force) {
Sen Jiang53c2ec52019-01-10 15:27:59 -08001925 base::TimeTicks curr_time = base::TimeTicks::Now();
Sen Jiang3a25dc22019-01-10 14:14:41 -08001926 if (force || curr_time > update_checkpoint_time_) {
Colin Howes0e452c92018-11-02 13:18:44 -07001927 update_checkpoint_time_ = curr_time + update_checkpoint_wait_;
1928 } else {
1929 return false;
1930 }
1931
Darin Petkov9c0baf82010-10-07 13:44:48 -07001932 Terminator::set_exit_blocked(true);
Darin Petkov0406e402010-10-06 21:33:11 -07001933 if (last_updated_buffer_offset_ != buffer_offset_) {
Darin Petkov9c0baf82010-10-07 13:44:48 -07001934 // Resets the progress in case we die in the middle of the state update.
Darin Petkov9b230572010-10-08 10:20:09 -07001935 ResetUpdateProgress(prefs_, true);
Amin Hassani008c4582019-01-13 16:22:47 -08001936 TEST_AND_RETURN_FALSE(prefs_->SetString(
1937 kPrefsUpdateStateSHA256Context, payload_hash_calculator_.GetContext()));
Sen Jiangf6813802015-11-03 21:27:29 -08001938 TEST_AND_RETURN_FALSE(
1939 prefs_->SetString(kPrefsUpdateStateSignedSHA256Context,
1940 signed_hash_calculator_.GetContext()));
Amin Hassani008c4582019-01-13 16:22:47 -08001941 TEST_AND_RETURN_FALSE(
1942 prefs_->SetInt64(kPrefsUpdateStateNextDataOffset, buffer_offset_));
Darin Petkov0406e402010-10-06 21:33:11 -07001943 last_updated_buffer_offset_ = buffer_offset_;
David Zeuthen41996ad2013-09-24 15:43:24 -07001944
1945 if (next_operation_num_ < num_total_operations_) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -07001946 size_t partition_index = current_partition_;
1947 while (next_operation_num_ >= acc_num_operations_[partition_index])
1948 partition_index++;
Amin Hassani008c4582019-01-13 16:22:47 -08001949 const size_t partition_operation_num =
1950 next_operation_num_ -
1951 (partition_index ? acc_num_operations_[partition_index - 1] : 0);
Alex Deymoa12ee112015-08-12 22:19:32 -07001952 const InstallOperation& op =
Alex Deymoe5e5fe92015-10-05 09:28:19 -07001953 partitions_[partition_index].operations(partition_operation_num);
Amin Hassani008c4582019-01-13 16:22:47 -08001954 TEST_AND_RETURN_FALSE(
1955 prefs_->SetInt64(kPrefsUpdateStateNextDataLength, op.data_length()));
David Zeuthen41996ad2013-09-24 15:43:24 -07001956 } else {
Amin Hassani008c4582019-01-13 16:22:47 -08001957 TEST_AND_RETURN_FALSE(
1958 prefs_->SetInt64(kPrefsUpdateStateNextDataLength, 0));
David Zeuthen41996ad2013-09-24 15:43:24 -07001959 }
Darin Petkov0406e402010-10-06 21:33:11 -07001960 }
Amin Hassani008c4582019-01-13 16:22:47 -08001961 TEST_AND_RETURN_FALSE(
1962 prefs_->SetInt64(kPrefsUpdateStateNextOperation, next_operation_num_));
Darin Petkov73058b42010-10-06 16:32:19 -07001963 return true;
1964}
1965
Darin Petkov9b230572010-10-08 10:20:09 -07001966bool DeltaPerformer::PrimeUpdateState() {
1967 CHECK(manifest_valid_);
Darin Petkov9b230572010-10-08 10:20:09 -07001968
1969 int64_t next_operation = kUpdateStateOperationInvalid;
1970 if (!prefs_->GetInt64(kPrefsUpdateStateNextOperation, &next_operation) ||
Amin Hassani008c4582019-01-13 16:22:47 -08001971 next_operation == kUpdateStateOperationInvalid || next_operation <= 0) {
Darin Petkov9b230572010-10-08 10:20:09 -07001972 // Initiating a new update, no more state needs to be initialized.
1973 return true;
1974 }
1975 next_operation_num_ = next_operation;
1976
1977 // Resuming an update -- load the rest of the update state.
1978 int64_t next_data_offset = -1;
Amin Hassani008c4582019-01-13 16:22:47 -08001979 TEST_AND_RETURN_FALSE(
1980 prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset) &&
1981 next_data_offset >= 0);
Darin Petkov9b230572010-10-08 10:20:09 -07001982 buffer_offset_ = next_data_offset;
1983
Darin Petkov4f0a07b2011-05-25 16:47:20 -07001984 // The signed hash context and the signature blob may be empty if the
1985 // interrupted update didn't reach the signature.
Sen Jiangf6813802015-11-03 21:27:29 -08001986 string signed_hash_context;
1987 if (prefs_->GetString(kPrefsUpdateStateSignedSHA256Context,
1988 &signed_hash_context)) {
1989 TEST_AND_RETURN_FALSE(
1990 signed_hash_calculator_.SetContext(signed_hash_context));
1991 }
1992
Sen Jiang9b2f1782019-01-24 14:27:50 -08001993 prefs_->GetString(kPrefsUpdateStateSignatureBlob, &signatures_message_data_);
Darin Petkov9b230572010-10-08 10:20:09 -07001994
1995 string hash_context;
Amin Hassani008c4582019-01-13 16:22:47 -08001996 TEST_AND_RETURN_FALSE(
1997 prefs_->GetString(kPrefsUpdateStateSHA256Context, &hash_context) &&
1998 payload_hash_calculator_.SetContext(hash_context));
Darin Petkov9b230572010-10-08 10:20:09 -07001999
2000 int64_t manifest_metadata_size = 0;
Amin Hassani008c4582019-01-13 16:22:47 -08002001 TEST_AND_RETURN_FALSE(
2002 prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size) &&
2003 manifest_metadata_size > 0);
Gilad Arnoldfe133932014-01-14 12:25:50 -08002004 metadata_size_ = manifest_metadata_size;
Darin Petkov9b230572010-10-08 10:20:09 -07002005
Alex Deymof25eb492016-02-26 00:20:08 -08002006 int64_t manifest_signature_size = 0;
2007 TEST_AND_RETURN_FALSE(
2008 prefs_->GetInt64(kPrefsManifestSignatureSize, &manifest_signature_size) &&
2009 manifest_signature_size >= 0);
2010 metadata_signature_size_ = manifest_signature_size;
2011
Gilad Arnold8a86fa52013-01-15 12:35:05 -08002012 // Advance the download progress to reflect what doesn't need to be
2013 // re-downloaded.
2014 total_bytes_received_ += buffer_offset_;
2015
Darin Petkov61426142010-10-08 11:04:55 -07002016 // Speculatively count the resume as a failure.
2017 int64_t resumed_update_failures;
2018 if (prefs_->GetInt64(kPrefsResumedUpdateFailures, &resumed_update_failures)) {
2019 resumed_update_failures++;
2020 } else {
2021 resumed_update_failures = 1;
2022 }
2023 prefs_->SetInt64(kPrefsResumedUpdateFailures, resumed_update_failures);
Darin Petkov9b230572010-10-08 10:20:09 -07002024 return true;
2025}
2026
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07002027} // namespace chromeos_update_engine