| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2012 The Android Open Source Project | 
|  | 3 | // | 
|  | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | // you may not use this file except in compliance with the License. | 
|  | 6 | // You may obtain a copy of the License at | 
|  | 7 | // | 
|  | 8 | //      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | // | 
|  | 10 | // Unless required by applicable law or agreed to in writing, software | 
|  | 11 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | // See the License for the specific language governing permissions and | 
|  | 14 | // limitations under the License. | 
|  | 15 | // | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 16 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/payload_consumer/delta_performer.h" | 
| Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 18 |  | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 19 | #include <errno.h> | 
| Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 20 | #include <linux/fs.h> | 
| Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 21 |  | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 22 | #include <algorithm> | 
|  | 23 | #include <cstring> | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 24 | #include <map> | 
| Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 25 | #include <memory> | 
| Tianjie | d3865d1 | 2020-06-03 15:25:17 -0700 | [diff] [blame] | 26 | #include <set> | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 27 | #include <string> | 
| Amin Hassani | d87304c | 2017-08-29 11:40:03 -0700 | [diff] [blame] | 28 | #include <utility> | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 29 | #include <vector> | 
|  | 30 |  | 
| Ben Chan | 06c76a4 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 31 | #include <base/files/file_util.h> | 
| Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 32 | #include <base/format_macros.h> | 
| Lann Martin | 39f5714 | 2017-07-14 09:18:42 -0600 | [diff] [blame] | 33 | #include <base/metrics/histogram_macros.h> | 
| Alex Deymo | 6714084 | 2016-06-15 13:28:59 -0700 | [diff] [blame] | 34 | #include <base/strings/string_number_conversions.h> | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 35 | #include <base/strings/string_util.h> | 
|  | 36 | #include <base/strings/stringprintf.h> | 
| Lann Martin | 39f5714 | 2017-07-14 09:18:42 -0600 | [diff] [blame] | 37 | #include <base/time/time.h> | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 38 | #include <brillo/data_encoding.h> | 
| Alex Deymo | 6765a68 | 2017-05-19 13:13:54 -0700 | [diff] [blame] | 39 | #include <bsdiff/bspatch.h> | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 40 | #include <google/protobuf/repeated_field.h> | 
| Amin Hassani | 02855c2 | 2017-09-06 22:34:50 -0700 | [diff] [blame] | 41 | #include <puffin/puffpatch.h> | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 42 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 43 | #include "update_engine/common/constants.h" | 
| Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 44 | #include "update_engine/common/download_action.h" | 
| Yifan Hong | 8702933 | 2020-09-01 17:20:08 -0700 | [diff] [blame] | 45 | #include "update_engine/common/error_code.h" | 
|  | 46 | #include "update_engine/common/error_code_utils.h" | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 47 | #include "update_engine/common/hardware_interface.h" | 
| Alex Deymo | 542c19b | 2015-12-03 07:43:31 -0300 | [diff] [blame] | 48 | #include "update_engine/common/prefs_interface.h" | 
|  | 49 | #include "update_engine/common/subprocess.h" | 
|  | 50 | #include "update_engine/common/terminator.h" | 
| Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 51 | #include "update_engine/common/utils.h" | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 52 | #include "update_engine/payload_consumer/bzip_extent_writer.h" | 
| Amin Hassani | a3fc20a | 2017-09-19 17:19:34 -0700 | [diff] [blame] | 53 | #include "update_engine/payload_consumer/cached_file_descriptor.h" | 
| Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 54 | #include "update_engine/payload_consumer/certificate_parser_interface.h" | 
| Amin Hassani | db56be9 | 2017-09-06 12:41:23 -0700 | [diff] [blame] | 55 | #include "update_engine/payload_consumer/extent_reader.h" | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 56 | #include "update_engine/payload_consumer/extent_writer.h" | 
| Tianjie | d3865d1 | 2020-06-03 15:25:17 -0700 | [diff] [blame] | 57 | #include "update_engine/payload_consumer/partition_update_generator_interface.h" | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 58 | #include "update_engine/payload_consumer/partition_writer.h" | 
| Alex Deymo | 51c264e | 2016-11-04 15:49:53 -0700 | [diff] [blame] | 59 | #if USE_FEC | 
|  | 60 | #include "update_engine/payload_consumer/fec_file_descriptor.h" | 
|  | 61 | #endif  // USE_FEC | 
| Alex Deymo | a48f630 | 2016-11-04 15:49:53 -0700 | [diff] [blame] | 62 | #include "update_engine/payload_consumer/file_descriptor_utils.h" | 
| HÃ¥kan Kvist | 4e13cf4 | 2018-01-23 12:57:55 +0100 | [diff] [blame] | 63 | #include "update_engine/payload_consumer/mount_history.h" | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 64 | #include "update_engine/payload_consumer/payload_constants.h" | 
|  | 65 | #include "update_engine/payload_consumer/payload_verifier.h" | 
|  | 66 | #include "update_engine/payload_consumer/xz_extent_writer.h" | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 67 |  | 
| Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 68 | using google::protobuf::RepeatedPtrField; | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 69 | using std::min; | 
|  | 70 | using std::string; | 
|  | 71 | using std::vector; | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 72 |  | 
|  | 73 | namespace chromeos_update_engine { | 
| Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 74 | const unsigned DeltaPerformer::kProgressLogMaxChunks = 10; | 
|  | 75 | const unsigned DeltaPerformer::kProgressLogTimeoutSeconds = 30; | 
|  | 76 | const unsigned DeltaPerformer::kProgressDownloadWeight = 50; | 
|  | 77 | const unsigned DeltaPerformer::kProgressOperationsWeight = 50; | 
| Colin Howes | 0e452c9 | 2018-11-02 13:18:44 -0700 | [diff] [blame] | 78 | const uint64_t DeltaPerformer::kCheckpointFrequencySeconds = 1; | 
| Darin Petkov | abc7bc0 | 2011-02-23 14:39:43 -0800 | [diff] [blame] | 79 |  | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 80 | namespace { | 
| Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 81 | const int kUpdateStateOperationInvalid = -1; | 
| Darin Petkov | 6142614 | 2010-10-08 11:04:55 -0700 | [diff] [blame] | 82 | const int kMaxResumedUpdateFailures = 10; | 
| Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 83 |  | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 84 | }  // namespace | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 85 |  | 
| Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 86 | // Computes the ratio of |part| and |total|, scaled to |norm|, using integer | 
|  | 87 | // arithmetic. | 
|  | 88 | static uint64_t IntRatio(uint64_t part, uint64_t total, uint64_t norm) { | 
|  | 89 | return part * norm / total; | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | void DeltaPerformer::LogProgress(const char* message_prefix) { | 
|  | 93 | // Format operations total count and percentage. | 
|  | 94 | string total_operations_str("?"); | 
|  | 95 | string completed_percentage_str(""); | 
|  | 96 | if (num_total_operations_) { | 
| Alex Deymo | c00c98a | 2015-03-17 17:38:00 -0700 | [diff] [blame] | 97 | total_operations_str = std::to_string(num_total_operations_); | 
| Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 98 | // Upcasting to 64-bit to avoid overflow, back to size_t for formatting. | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 99 | completed_percentage_str = base::StringPrintf( | 
|  | 100 | " (%" PRIu64 "%%)", | 
|  | 101 | IntRatio(next_operation_num_, num_total_operations_, 100)); | 
| Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 102 | } | 
|  | 103 |  | 
|  | 104 | // Format download total count and percentage. | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 105 | size_t payload_size = payload_->size; | 
| Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 106 | string payload_size_str("?"); | 
|  | 107 | string downloaded_percentage_str(""); | 
|  | 108 | if (payload_size) { | 
| Alex Deymo | c00c98a | 2015-03-17 17:38:00 -0700 | [diff] [blame] | 109 | payload_size_str = std::to_string(payload_size); | 
| Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 110 | // Upcasting to 64-bit to avoid overflow, back to size_t for formatting. | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 111 | downloaded_percentage_str = base::StringPrintf( | 
|  | 112 | " (%" PRIu64 "%%)", IntRatio(total_bytes_received_, payload_size, 100)); | 
| Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 113 | } | 
|  | 114 |  | 
|  | 115 | LOG(INFO) << (message_prefix ? message_prefix : "") << next_operation_num_ | 
|  | 116 | << "/" << total_operations_str << " operations" | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 117 | << completed_percentage_str << ", " << total_bytes_received_ << "/" | 
|  | 118 | << payload_size_str << " bytes downloaded" | 
| Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 119 | << downloaded_percentage_str << ", overall progress " | 
|  | 120 | << overall_progress_ << "%"; | 
|  | 121 | } | 
|  | 122 |  | 
|  | 123 | void DeltaPerformer::UpdateOverallProgress(bool force_log, | 
|  | 124 | const char* message_prefix) { | 
|  | 125 | // Compute our download and overall progress. | 
|  | 126 | unsigned new_overall_progress = 0; | 
| Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 127 | static_assert(kProgressDownloadWeight + kProgressOperationsWeight == 100, | 
|  | 128 | "Progress weights don't add up"); | 
| Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 129 | // Only consider download progress if its total size is known; otherwise | 
|  | 130 | // adjust the operations weight to compensate for the absence of download | 
|  | 131 | // progress. Also, make sure to cap the download portion at | 
|  | 132 | // kProgressDownloadWeight, in case we end up downloading more than we | 
|  | 133 | // initially expected (this indicates a problem, but could generally happen). | 
|  | 134 | // TODO(garnold) the correction of operations weight when we do not have the | 
|  | 135 | // total payload size, as well as the conditional guard below, should both be | 
|  | 136 | // eliminated once we ensure that the payload_size in the install plan is | 
|  | 137 | // always given and is non-zero. This currently isn't the case during unit | 
|  | 138 | // tests (see chromium-os:37969). | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 139 | size_t payload_size = payload_->size; | 
| Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 140 | unsigned actual_operations_weight = kProgressOperationsWeight; | 
|  | 141 | if (payload_size) | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 142 | new_overall_progress += | 
|  | 143 | min(static_cast<unsigned>(IntRatio( | 
|  | 144 | total_bytes_received_, payload_size, kProgressDownloadWeight)), | 
|  | 145 | kProgressDownloadWeight); | 
| Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 146 | else | 
|  | 147 | actual_operations_weight += kProgressDownloadWeight; | 
|  | 148 |  | 
|  | 149 | // Only add completed operations if their total number is known; we definitely | 
|  | 150 | // expect an update to have at least one operation, so the expectation is that | 
|  | 151 | // this will eventually reach |actual_operations_weight|. | 
|  | 152 | if (num_total_operations_) | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 153 | new_overall_progress += IntRatio( | 
|  | 154 | next_operation_num_, num_total_operations_, actual_operations_weight); | 
| Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 155 |  | 
|  | 156 | // Progress ratio cannot recede, unless our assumptions about the total | 
|  | 157 | // payload size, total number of operations, or the monotonicity of progress | 
|  | 158 | // is breached. | 
|  | 159 | if (new_overall_progress < overall_progress_) { | 
|  | 160 | LOG(WARNING) << "progress counter receded from " << overall_progress_ | 
|  | 161 | << "% down to " << new_overall_progress << "%; this is a bug"; | 
|  | 162 | force_log = true; | 
|  | 163 | } | 
|  | 164 | overall_progress_ = new_overall_progress; | 
|  | 165 |  | 
|  | 166 | // Update chunk index, log as needed: if forced by called, or we completed a | 
|  | 167 | // progress chunk, or a timeout has expired. | 
| Sen Jiang | 53c2ec5 | 2019-01-10 15:27:59 -0800 | [diff] [blame] | 168 | base::TimeTicks curr_time = base::TimeTicks::Now(); | 
| Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 169 | unsigned curr_progress_chunk = | 
|  | 170 | overall_progress_ * kProgressLogMaxChunks / 100; | 
|  | 171 | if (force_log || curr_progress_chunk > last_progress_chunk_ || | 
|  | 172 | curr_time > forced_progress_log_time_) { | 
|  | 173 | forced_progress_log_time_ = curr_time + forced_progress_log_wait_; | 
|  | 174 | LogProgress(message_prefix); | 
|  | 175 | } | 
|  | 176 | last_progress_chunk_ = curr_progress_chunk; | 
|  | 177 | } | 
|  | 178 |  | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 179 | size_t DeltaPerformer::CopyDataToBuffer(const char** bytes_p, | 
|  | 180 | size_t* count_p, | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 181 | size_t max) { | 
|  | 182 | const size_t count = *count_p; | 
|  | 183 | if (!count) | 
|  | 184 | return 0;  // Special case shortcut. | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 185 | size_t read_len = min(count, max - buffer_.size()); | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 186 | const char* bytes_start = *bytes_p; | 
|  | 187 | const char* bytes_end = bytes_start + read_len; | 
| Sen Jiang | be19a24 | 2017-11-17 11:48:17 -0800 | [diff] [blame] | 188 | buffer_.reserve(max); | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 189 | buffer_.insert(buffer_.end(), bytes_start, bytes_end); | 
|  | 190 | *bytes_p = bytes_end; | 
|  | 191 | *count_p = count - read_len; | 
|  | 192 | return read_len; | 
|  | 193 | } | 
|  | 194 |  | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 195 | bool DeltaPerformer::HandleOpResult(bool op_result, | 
|  | 196 | const char* op_type_name, | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 197 | ErrorCode* error) { | 
|  | 198 | if (op_result) | 
|  | 199 | return true; | 
|  | 200 |  | 
|  | 201 | LOG(ERROR) << "Failed to perform " << op_type_name << " operation " | 
| Alex Deymo | 3d00906 | 2016-05-13 15:51:25 -0700 | [diff] [blame] | 202 | << next_operation_num_ << ", which is the operation " | 
| Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 203 | << GetPartitionOperationNum() << " in partition \"" | 
| Alex Deymo | 3d00906 | 2016-05-13 15:51:25 -0700 | [diff] [blame] | 204 | << partitions_[current_partition_].partition_name() << "\""; | 
| Sen Jiang | be2c47b | 2016-06-15 14:09:27 -0700 | [diff] [blame] | 205 | if (*error == ErrorCode::kSuccess) | 
|  | 206 | *error = ErrorCode::kDownloadOperationExecutionError; | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 207 | return false; | 
|  | 208 | } | 
|  | 209 |  | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 210 | int DeltaPerformer::Close() { | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 211 | int err = -CloseCurrentPartition(); | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 212 | LOG_IF(ERROR, | 
|  | 213 | !payload_hash_calculator_.Finalize() || | 
|  | 214 | !signed_hash_calculator_.Finalize()) | 
| Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 215 | << "Unable to finalize the hash."; | 
| Darin Petkov | 934bb41 | 2010-11-18 11:21:35 -0800 | [diff] [blame] | 216 | if (!buffer_.empty()) { | 
| Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 217 | LOG(INFO) << "Discarding " << buffer_.size() << " unused downloaded bytes"; | 
|  | 218 | if (err >= 0) | 
| Darin Petkov | 934bb41 | 2010-11-18 11:21:35 -0800 | [diff] [blame] | 219 | err = 1; | 
| Darin Petkov | 934bb41 | 2010-11-18 11:21:35 -0800 | [diff] [blame] | 220 | } | 
| Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 221 | return -err; | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 222 | } | 
|  | 223 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 224 | int DeltaPerformer::CloseCurrentPartition() { | 
| Kelvin Zhang | 9bd519d | 2020-09-23 12:55:19 -0400 | [diff] [blame] | 225 | if (!partition_writer_) { | 
|  | 226 | return 0; | 
|  | 227 | } | 
|  | 228 | int err = partition_writer_->Close(); | 
|  | 229 | partition_writer_ = nullptr; | 
|  | 230 | return err; | 
|  | 231 | } | 
|  | 232 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 233 | bool DeltaPerformer::OpenCurrentPartition() { | 
|  | 234 | if (current_partition_ >= partitions_.size()) | 
|  | 235 | return false; | 
|  | 236 |  | 
|  | 237 | const PartitionUpdate& partition = partitions_[current_partition_]; | 
| Sen Jiang | d2ff2a0 | 2017-04-06 14:53:31 -0700 | [diff] [blame] | 238 | size_t num_previous_partitions = | 
|  | 239 | install_plan_->partitions.size() - partitions_.size(); | 
|  | 240 | const InstallPlan::Partition& install_part = | 
|  | 241 | install_plan_->partitions[num_previous_partitions + current_partition_]; | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 242 | auto dynamic_control = boot_control_->GetDynamicPartitionControl(); | 
| Kelvin Zhang | ebd115e | 2021-03-08 16:10:25 -0500 | [diff] [blame] | 243 | partition_writer_ = CreatePartitionWriter( | 
|  | 244 | partition, | 
|  | 245 | install_part, | 
|  | 246 | dynamic_control, | 
|  | 247 | block_size_, | 
|  | 248 | interactive_, | 
|  | 249 | IsDynamicPartition(install_part.name, install_plan_->target_slot)); | 
| Tianjie | 55abd3c | 2020-06-19 00:22:59 -0700 | [diff] [blame] | 250 | // Open source fds if we have a delta payload, or for partitions in the | 
|  | 251 | // partial update. | 
| Tianjie | d3865d1 | 2020-06-03 15:25:17 -0700 | [diff] [blame] | 252 | bool source_may_exist = manifest_.partial_update() || | 
| Tianjie | 55abd3c | 2020-06-19 00:22:59 -0700 | [diff] [blame] | 253 | payload_->type == InstallPayloadType::kDelta; | 
| Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 254 | const size_t partition_operation_num = GetPartitionOperationNum(); | 
|  | 255 |  | 
|  | 256 | TEST_AND_RETURN_FALSE(partition_writer_->Init( | 
|  | 257 | install_plan_, source_may_exist, partition_operation_num)); | 
|  | 258 | CheckpointUpdateProgress(true); | 
|  | 259 | return true; | 
|  | 260 | } | 
|  | 261 |  | 
|  | 262 | size_t DeltaPerformer::GetPartitionOperationNum() { | 
|  | 263 | return next_operation_num_ - | 
|  | 264 | (current_partition_ ? acc_num_operations_[current_partition_ - 1] : 0); | 
| Kelvin Zhang | 9bd519d | 2020-09-23 12:55:19 -0400 | [diff] [blame] | 265 | } | 
|  | 266 |  | 
| Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame] | 267 | namespace { | 
|  | 268 |  | 
|  | 269 | void LogPartitionInfoHash(const PartitionInfo& info, const string& tag) { | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 270 | string sha256 = brillo::data_encoding::Base64Encode(info.hash()); | 
| Alex Vakulenko | 981a9fb | 2015-02-09 12:51:24 -0800 | [diff] [blame] | 271 | LOG(INFO) << "PartitionInfo " << tag << " sha256: " << sha256 | 
|  | 272 | << " size: " << info.size(); | 
| Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame] | 273 | } | 
|  | 274 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 275 | void LogPartitionInfo(const vector<PartitionUpdate>& partitions) { | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 276 | for (const PartitionUpdate& partition : partitions) { | 
| Sen Jiang | 7b9a587 | 2018-01-17 13:25:06 -0800 | [diff] [blame] | 277 | if (partition.has_old_partition_info()) { | 
|  | 278 | LogPartitionInfoHash(partition.old_partition_info(), | 
|  | 279 | "old " + partition.partition_name()); | 
|  | 280 | } | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 281 | LogPartitionInfoHash(partition.new_partition_info(), | 
|  | 282 | "new " + partition.partition_name()); | 
|  | 283 | } | 
| Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame] | 284 | } | 
|  | 285 |  | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 286 | }  // namespace | 
| Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame] | 287 |  | 
| Sen Jiang | b8060e4 | 2015-09-24 17:30:50 -0700 | [diff] [blame] | 288 | bool DeltaPerformer::IsHeaderParsed() const { | 
|  | 289 | return metadata_size_ != 0; | 
|  | 290 | } | 
| Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 291 |  | 
| Sen Jiang | 9c89e84 | 2018-02-02 13:51:21 -0800 | [diff] [blame] | 292 | MetadataParseResult DeltaPerformer::ParsePayloadMetadata( | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 293 | const brillo::Blob& payload, ErrorCode* error) { | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 294 | *error = ErrorCode::kSuccess; | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 295 |  | 
| Sen Jiang | b8060e4 | 2015-09-24 17:30:50 -0700 | [diff] [blame] | 296 | if (!IsHeaderParsed()) { | 
| Sen Jiang | f123663 | 2018-05-11 16:03:23 -0700 | [diff] [blame] | 297 | MetadataParseResult result = | 
|  | 298 | payload_metadata_.ParsePayloadHeader(payload, error); | 
| Sen Jiang | 9c89e84 | 2018-02-02 13:51:21 -0800 | [diff] [blame] | 299 | if (result != MetadataParseResult::kSuccess) | 
|  | 300 | return result; | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 301 |  | 
| Sen Jiang | 9c89e84 | 2018-02-02 13:51:21 -0800 | [diff] [blame] | 302 | metadata_size_ = payload_metadata_.GetMetadataSize(); | 
|  | 303 | metadata_signature_size_ = payload_metadata_.GetMetadataSignatureSize(); | 
|  | 304 | major_payload_version_ = payload_metadata_.GetMajorVersion(); | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 305 |  | 
|  | 306 | // If the metadata size is present in install plan, check for it immediately | 
|  | 307 | // even before waiting for that many number of bytes to be downloaded in the | 
|  | 308 | // payload. This will prevent any attack which relies on us downloading data | 
|  | 309 | // beyond the expected metadata size. | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 310 | if (install_plan_->hash_checks_mandatory) { | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 311 | if (payload_->metadata_size != metadata_size_) { | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 312 | LOG(ERROR) << "Mandatory metadata size in Omaha response (" | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 313 | << payload_->metadata_size | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 314 | << ") is missing/incorrect, actual = " << metadata_size_; | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 315 | *error = ErrorCode::kDownloadInvalidMetadataSize; | 
| Sen Jiang | 9c89e84 | 2018-02-02 13:51:21 -0800 | [diff] [blame] | 316 | return MetadataParseResult::kError; | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 317 | } | 
|  | 318 | } | 
| Andrew | 3a7dc26 | 2019-12-19 11:38:08 -0800 | [diff] [blame] | 319 |  | 
|  | 320 | // Check that the |metadata signature size_| and |metadata_size_| are not | 
|  | 321 | // very big numbers. This is necessary since |update_engine| needs to write | 
|  | 322 | // these values into the buffer before being able to use them, and if an | 
|  | 323 | // attacker sets these values to a very big number, the buffer will overflow | 
|  | 324 | // and |update_engine| will crash. A simple way of solving this is to check | 
|  | 325 | // that the size of both values is smaller than the payload itself. | 
|  | 326 | if (metadata_size_ + metadata_signature_size_ > payload_->size) { | 
|  | 327 | LOG(ERROR) << "The size of the metadata_size(" << metadata_size_ << ")" | 
|  | 328 | << " or metadata signature(" << metadata_signature_size_ << ")" | 
|  | 329 | << " is greater than the size of the payload" | 
|  | 330 | << "(" << payload_->size << ")"; | 
|  | 331 | *error = ErrorCode::kDownloadInvalidMetadataSize; | 
|  | 332 | return MetadataParseResult::kError; | 
|  | 333 | } | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 334 | } | 
|  | 335 |  | 
|  | 336 | // Now that we have validated the metadata size, we should wait for the full | 
| Sen Jiang | 76bfa74 | 2015-10-12 17:13:26 -0700 | [diff] [blame] | 337 | // metadata and its signature (if exist) to be read in before we can parse it. | 
|  | 338 | if (payload.size() < metadata_size_ + metadata_signature_size_) | 
| Sen Jiang | 9c89e84 | 2018-02-02 13:51:21 -0800 | [diff] [blame] | 339 | return MetadataParseResult::kInsufficientData; | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 340 |  | 
|  | 341 | // Log whether we validated the size or simply trusting what's in the payload | 
| Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 342 | // here. This is logged here (after we received the full metadata data) so | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 343 | // that we just log once (instead of logging n times) if it takes n | 
|  | 344 | // DeltaPerformer::Write calls to download the full manifest. | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 345 | if (payload_->metadata_size == metadata_size_) { | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 346 | LOG(INFO) << "Manifest size in payload matches expected value from Omaha"; | 
| Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 347 | } else { | 
|  | 348 | // For mandatory-cases, we'd have already returned a kMetadataParseError | 
|  | 349 | // above. We'll be here only for non-mandatory cases. Just send a UMA stat. | 
|  | 350 | LOG(WARNING) << "Ignoring missing/incorrect metadata size (" | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 351 | << payload_->metadata_size | 
| Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 352 | << ") in Omaha response as validation is not mandatory. " | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 353 | << "Trusting metadata size in payload = " << metadata_size_; | 
| Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 354 | } | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 355 |  | 
| Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 356 | // NOLINTNEXTLINE(whitespace/braces) | 
| Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 357 | auto [payload_verifier, perform_verification] = CreatePayloadVerifier(); | 
|  | 358 | if (!payload_verifier) { | 
|  | 359 | LOG(ERROR) << "Failed to create payload verifier."; | 
| Sen Jiang | 08c6da1 | 2019-01-07 18:28:56 -0800 | [diff] [blame] | 360 | *error = ErrorCode::kDownloadMetadataSignatureVerificationError; | 
| Tianjie Xu | 6c70b93 | 2019-10-22 16:46:00 -0700 | [diff] [blame] | 361 | if (perform_verification) { | 
|  | 362 | return MetadataParseResult::kError; | 
|  | 363 | } | 
|  | 364 | } else { | 
|  | 365 | // We have the full metadata in |payload|. Verify its integrity | 
|  | 366 | // and authenticity based on the information we have in Omaha response. | 
|  | 367 | *error = payload_metadata_.ValidateMetadataSignature( | 
|  | 368 | payload, payload_->metadata_signature, *payload_verifier); | 
| Sen Jiang | 08c6da1 | 2019-01-07 18:28:56 -0800 | [diff] [blame] | 369 | } | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 370 | if (*error != ErrorCode::kSuccess) { | 
| Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 371 | if (install_plan_->hash_checks_mandatory) { | 
| David Zeuthen | bc27aac | 2013-11-26 11:17:48 -0800 | [diff] [blame] | 372 | // The autoupdate_CatchBadSignatures test checks for this string | 
|  | 373 | // in log-files. Keep in sync. | 
| Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 374 | LOG(ERROR) << "Mandatory metadata signature validation failed"; | 
| Sen Jiang | 9c89e84 | 2018-02-02 13:51:21 -0800 | [diff] [blame] | 375 | return MetadataParseResult::kError; | 
| Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 376 | } | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 377 |  | 
| Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 378 | // For non-mandatory cases, just send a UMA stat. | 
|  | 379 | LOG(WARNING) << "Ignoring metadata signature validation failures"; | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 380 | *error = ErrorCode::kSuccess; | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 381 | } | 
|  | 382 |  | 
| Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 383 | // The payload metadata is deemed valid, it's safe to parse the protobuf. | 
| Sen Jiang | 9c89e84 | 2018-02-02 13:51:21 -0800 | [diff] [blame] | 384 | if (!payload_metadata_.GetManifest(payload, &manifest_)) { | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 385 | LOG(ERROR) << "Unable to parse manifest in update file."; | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 386 | *error = ErrorCode::kDownloadManifestParseError; | 
| Sen Jiang | 9c89e84 | 2018-02-02 13:51:21 -0800 | [diff] [blame] | 387 | return MetadataParseResult::kError; | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 388 | } | 
| Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 389 |  | 
|  | 390 | manifest_parsed_ = true; | 
| Sen Jiang | 9c89e84 | 2018-02-02 13:51:21 -0800 | [diff] [blame] | 391 | return MetadataParseResult::kSuccess; | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 392 | } | 
|  | 393 |  | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 394 | #define OP_DURATION_HISTOGRAM(_op_name, _start_time)                         \ | 
|  | 395 | LOCAL_HISTOGRAM_CUSTOM_TIMES(                                              \ | 
|  | 396 | "UpdateEngine.DownloadAction.InstallOperation::" _op_name ".Duration", \ | 
| Andrew | 9d5a61d | 2020-03-26 13:40:37 -0700 | [diff] [blame] | 397 | (base::TimeTicks::Now() - _start_time),                                \ | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 398 | base::TimeDelta::FromMilliseconds(10),                                 \ | 
|  | 399 | base::TimeDelta::FromMinutes(5),                                       \ | 
|  | 400 | 20); | 
| Lann Martin | 39f5714 | 2017-07-14 09:18:42 -0600 | [diff] [blame] | 401 |  | 
| Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 402 | // Wrapper around write. Returns true if all requested bytes | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 403 | // were written, or false on any error, regardless of progress | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 404 | // and stores an action exit code in |error|. | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 405 | bool DeltaPerformer::Write(const void* bytes, size_t count, ErrorCode* error) { | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 406 | *error = ErrorCode::kSuccess; | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 407 | const char* c_bytes = reinterpret_cast<const char*>(bytes); | 
| Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 408 |  | 
|  | 409 | // Update the total byte downloaded count and the progress logs. | 
|  | 410 | total_bytes_received_ += count; | 
|  | 411 | UpdateOverallProgress(false, "Completed "); | 
|  | 412 |  | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 413 | while (!manifest_valid_) { | 
| Sen Jiang | b8060e4 | 2015-09-24 17:30:50 -0700 | [diff] [blame] | 414 | // Read data up to the needed limit; this is either maximium payload header | 
|  | 415 | // size, or the full metadata size (once it becomes known). | 
|  | 416 | const bool do_read_header = !IsHeaderParsed(); | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 417 | CopyDataToBuffer( | 
|  | 418 | &c_bytes, | 
|  | 419 | &count, | 
|  | 420 | (do_read_header ? kMaxPayloadHeaderSize | 
|  | 421 | : metadata_size_ + metadata_signature_size_)); | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 422 |  | 
| Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 423 | MetadataParseResult result = ParsePayloadMetadata(buffer_, error); | 
| Sen Jiang | 9c89e84 | 2018-02-02 13:51:21 -0800 | [diff] [blame] | 424 | if (result == MetadataParseResult::kError) | 
| Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 425 | return false; | 
| Sen Jiang | 9c89e84 | 2018-02-02 13:51:21 -0800 | [diff] [blame] | 426 | if (result == MetadataParseResult::kInsufficientData) { | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 427 | // If we just processed the header, make an attempt on the manifest. | 
| Sen Jiang | b8060e4 | 2015-09-24 17:30:50 -0700 | [diff] [blame] | 428 | if (do_read_header && IsHeaderParsed()) | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 429 | continue; | 
|  | 430 |  | 
| Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 431 | return true; | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 432 | } | 
| Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 433 |  | 
|  | 434 | // Checks the integrity of the payload manifest. | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 435 | if ((*error = ValidateManifest()) != ErrorCode::kSuccess) | 
| Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 436 | return false; | 
| Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 437 | manifest_valid_ = true; | 
| Kelvin Zhang | cc011d3 | 2020-07-10 18:20:08 -0400 | [diff] [blame] | 438 | if (!install_plan_->is_resume) { | 
| Kelvin Zhang | cf4600e | 2020-10-27 15:50:33 -0400 | [diff] [blame] | 439 | auto begin = reinterpret_cast<const char*>(buffer_.data()); | 
|  | 440 | prefs_->SetString(kPrefsManifestBytes, {begin, buffer_.size()}); | 
| Kelvin Zhang | cc011d3 | 2020-07-10 18:20:08 -0400 | [diff] [blame] | 441 | } | 
| Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 442 |  | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 443 | // Clear the download buffer. | 
| Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 444 | DiscardBuffer(false, metadata_size_); | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 445 |  | 
| Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 446 | block_size_ = manifest_.block_size(); | 
|  | 447 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 448 | // This populates |partitions_| and the |install_plan.partitions| with the | 
|  | 449 | // list of partitions from the manifest. | 
|  | 450 | if (!ParseManifestPartitions(error)) | 
|  | 451 | return false; | 
|  | 452 |  | 
| Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 453 | // |install_plan.partitions| was filled in, nothing need to be done here if | 
|  | 454 | // the payload was already applied, returns false to terminate http fetcher, | 
|  | 455 | // but keep |error| as ErrorCode::kSuccess. | 
|  | 456 | if (payload_->already_applied) | 
|  | 457 | return false; | 
|  | 458 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 459 | num_total_operations_ = 0; | 
|  | 460 | for (const auto& partition : partitions_) { | 
|  | 461 | num_total_operations_ += partition.operations_size(); | 
|  | 462 | acc_num_operations_.push_back(num_total_operations_); | 
|  | 463 | } | 
|  | 464 |  | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 465 | LOG_IF(WARNING, | 
|  | 466 | !prefs_->SetInt64(kPrefsManifestMetadataSize, metadata_size_)) | 
| Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 467 | << "Unable to save the manifest metadata size."; | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 468 | LOG_IF(WARNING, | 
|  | 469 | !prefs_->SetInt64(kPrefsManifestSignatureSize, | 
|  | 470 | metadata_signature_size_)) | 
| Alex Deymo | f25eb49 | 2016-02-26 00:20:08 -0800 | [diff] [blame] | 471 | << "Unable to save the manifest signature size."; | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 472 |  | 
| Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 473 | if (!PrimeUpdateState()) { | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 474 | *error = ErrorCode::kDownloadStateInitializationError; | 
| Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 475 | LOG(ERROR) << "Unable to prime the update state."; | 
| Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 476 | return false; | 
| Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 477 | } | 
| Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 478 |  | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 479 | if (next_operation_num_ < acc_num_operations_[current_partition_]) { | 
|  | 480 | if (!OpenCurrentPartition()) { | 
|  | 481 | *error = ErrorCode::kInstallDeviceOpenError; | 
|  | 482 | return false; | 
|  | 483 | } | 
| Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 484 | } | 
|  | 485 |  | 
| Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 486 | if (next_operation_num_ > 0) | 
|  | 487 | UpdateOverallProgress(true, "Resuming after "); | 
|  | 488 | LOG(INFO) << "Starting to apply update payload operations"; | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 489 | } | 
| Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 490 |  | 
|  | 491 | while (next_operation_num_ < num_total_operations_) { | 
| Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 492 | // Check if we should cancel the current attempt for any reason. | 
|  | 493 | // In this case, *error will have already been populated with the reason | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 494 | // why we're canceling. | 
| Alex Deymo | 542c19b | 2015-12-03 07:43:31 -0300 | [diff] [blame] | 495 | if (download_delegate_ && download_delegate_->ShouldCancel(error)) | 
| Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 496 | return false; | 
|  | 497 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 498 | // We know there are more operations to perform because we didn't reach the | 
|  | 499 | // |num_total_operations_| limit yet. | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 500 | if (next_operation_num_ >= acc_num_operations_[current_partition_]) { | 
| Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 501 | if (partition_writer_) { | 
|  | 502 | TEST_AND_RETURN_FALSE(partition_writer_->FinishedInstallOps()); | 
|  | 503 | } | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 504 | CloseCurrentPartition(); | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 505 | // Skip until there are operations for current_partition_. | 
|  | 506 | while (next_operation_num_ >= acc_num_operations_[current_partition_]) { | 
|  | 507 | current_partition_++; | 
|  | 508 | } | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 509 | if (!OpenCurrentPartition()) { | 
|  | 510 | *error = ErrorCode::kInstallDeviceOpenError; | 
|  | 511 | return false; | 
|  | 512 | } | 
|  | 513 | } | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 514 |  | 
| Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 515 | const InstallOperation& op = | 
| Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 516 | partitions_[current_partition_].operations(GetPartitionOperationNum()); | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 517 |  | 
|  | 518 | CopyDataToBuffer(&c_bytes, &count, op.data_length()); | 
|  | 519 |  | 
|  | 520 | // Check whether we received all of the next operation's data payload. | 
|  | 521 | if (!CanPerformInstallOperation(op)) | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 522 | return true; | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 523 |  | 
| Tianjie | 1205ea6 | 2020-07-09 17:04:28 -0700 | [diff] [blame] | 524 | // Validate the operation unconditionally. This helps prevent the | 
|  | 525 | // exploitation of vulnerabilities in the patching libraries, e.g. bspatch. | 
|  | 526 | // The hash of the patch data for a given operation is embedded in the | 
|  | 527 | // payload metadata; and thus has been verified against the public key on | 
|  | 528 | // device. | 
|  | 529 | // Note: Validate must be called only if CanPerformInstallOperation is | 
|  | 530 | // called. Otherwise, we might be failing operations before even if there | 
|  | 531 | // isn't sufficient data to compute the proper hash. | 
|  | 532 | *error = ValidateOperationHash(op); | 
|  | 533 | if (*error != ErrorCode::kSuccess) { | 
|  | 534 | if (install_plan_->hash_checks_mandatory) { | 
|  | 535 | LOG(ERROR) << "Mandatory operation hash check failed"; | 
|  | 536 | return false; | 
| Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 537 | } | 
| Tianjie | 1205ea6 | 2020-07-09 17:04:28 -0700 | [diff] [blame] | 538 |  | 
|  | 539 | // For non-mandatory cases, just send a UMA stat. | 
|  | 540 | LOG(WARNING) << "Ignoring operation validation errors"; | 
|  | 541 | *error = ErrorCode::kSuccess; | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 542 | } | 
|  | 543 |  | 
| Darin Petkov | 45580e4 | 2010-10-08 14:02:40 -0700 | [diff] [blame] | 544 | // Makes sure we unblock exit when this operation completes. | 
| Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 545 | ScopedTerminatorExitUnblocker exit_unblocker = | 
|  | 546 | ScopedTerminatorExitUnblocker();  // Avoids a compiler unused var bug. | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 547 |  | 
| Lann Martin | 39f5714 | 2017-07-14 09:18:42 -0600 | [diff] [blame] | 548 | base::TimeTicks op_start_time = base::TimeTicks::Now(); | 
|  | 549 |  | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 550 | bool op_result; | 
| Alex Deymo | 2d621a3 | 2015-10-01 11:09:01 -0700 | [diff] [blame] | 551 | switch (op.type()) { | 
|  | 552 | case InstallOperation::REPLACE: | 
|  | 553 | case InstallOperation::REPLACE_BZ: | 
|  | 554 | case InstallOperation::REPLACE_XZ: | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 555 | op_result = PerformReplaceOperation(op); | 
| Lann Martin | 39f5714 | 2017-07-14 09:18:42 -0600 | [diff] [blame] | 556 | OP_DURATION_HISTOGRAM("REPLACE", op_start_time); | 
| Alex Deymo | 2d621a3 | 2015-10-01 11:09:01 -0700 | [diff] [blame] | 557 | break; | 
| Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 558 | case InstallOperation::ZERO: | 
|  | 559 | case InstallOperation::DISCARD: | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 560 | op_result = PerformZeroOrDiscardOperation(op); | 
| Lann Martin | 39f5714 | 2017-07-14 09:18:42 -0600 | [diff] [blame] | 561 | OP_DURATION_HISTOGRAM("ZERO_OR_DISCARD", op_start_time); | 
| Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 562 | break; | 
| Alex Deymo | 2d621a3 | 2015-10-01 11:09:01 -0700 | [diff] [blame] | 563 | case InstallOperation::SOURCE_COPY: | 
| Sen Jiang | be2c47b | 2016-06-15 14:09:27 -0700 | [diff] [blame] | 564 | op_result = PerformSourceCopyOperation(op, error); | 
| Lann Martin | 39f5714 | 2017-07-14 09:18:42 -0600 | [diff] [blame] | 565 | OP_DURATION_HISTOGRAM("SOURCE_COPY", op_start_time); | 
| Alex Deymo | 2d621a3 | 2015-10-01 11:09:01 -0700 | [diff] [blame] | 566 | break; | 
|  | 567 | case InstallOperation::SOURCE_BSDIFF: | 
| Amin Hassani | efa62d9 | 2017-11-09 13:46:56 -0800 | [diff] [blame] | 568 | case InstallOperation::BROTLI_BSDIFF: | 
| Sen Jiang | be2c47b | 2016-06-15 14:09:27 -0700 | [diff] [blame] | 569 | op_result = PerformSourceBsdiffOperation(op, error); | 
| Lann Martin | 39f5714 | 2017-07-14 09:18:42 -0600 | [diff] [blame] | 570 | OP_DURATION_HISTOGRAM("SOURCE_BSDIFF", op_start_time); | 
| Alex Deymo | 2d621a3 | 2015-10-01 11:09:01 -0700 | [diff] [blame] | 571 | break; | 
| Amin Hassani | 49fdb09 | 2017-08-04 13:10:59 -0700 | [diff] [blame] | 572 | case InstallOperation::PUFFDIFF: | 
| Amin Hassani | 02855c2 | 2017-09-06 22:34:50 -0700 | [diff] [blame] | 573 | op_result = PerformPuffDiffOperation(op, error); | 
|  | 574 | OP_DURATION_HISTOGRAM("PUFFDIFF", op_start_time); | 
| Sen Jiang | bc3e6b0 | 2016-01-19 18:39:26 +0800 | [diff] [blame] | 575 | break; | 
| Alex Deymo | 2d621a3 | 2015-10-01 11:09:01 -0700 | [diff] [blame] | 576 | default: | 
| Alex Deymo | eecb0a5 | 2017-05-19 15:15:08 -0700 | [diff] [blame] | 577 | op_result = false; | 
| Alex Deymo | 2d621a3 | 2015-10-01 11:09:01 -0700 | [diff] [blame] | 578 | } | 
|  | 579 | if (!HandleOpResult(op_result, InstallOperationTypeName(op.type()), error)) | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 580 | return false; | 
| Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 581 |  | 
| Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 582 | next_operation_num_++; | 
| Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 583 | UpdateOverallProgress(false, "Completed "); | 
| Sen Jiang | 3a25dc2 | 2019-01-10 14:14:41 -0800 | [diff] [blame] | 584 | CheckpointUpdateProgress(false); | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 585 | } | 
| Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 586 |  | 
| Kelvin Zhang | 4f28a6c | 2021-01-14 14:04:57 -0500 | [diff] [blame] | 587 | if (partition_writer_) { | 
|  | 588 | TEST_AND_RETURN_FALSE(partition_writer_->FinishedInstallOps()); | 
|  | 589 | } | 
|  | 590 | CloseCurrentPartition(); | 
|  | 591 |  | 
| Saint Chou | 0a92a62 | 2020-07-29 14:25:35 +0000 | [diff] [blame] | 592 | // In major version 2, we don't add unused operation to the payload. | 
| Alex Deymo | f25eb49 | 2016-02-26 00:20:08 -0800 | [diff] [blame] | 593 | // If we already extracted the signature we should skip this step. | 
| Amin Hassani | 822d485 | 2020-03-10 01:50:42 +0000 | [diff] [blame] | 594 | if (manifest_.has_signatures_offset() && manifest_.has_signatures_size() && | 
| Alex Deymo | f25eb49 | 2016-02-26 00:20:08 -0800 | [diff] [blame] | 595 | signatures_message_data_.empty()) { | 
| Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 596 | if (manifest_.signatures_offset() != buffer_offset_) { | 
|  | 597 | LOG(ERROR) << "Payload signatures offset points to blob offset " | 
|  | 598 | << manifest_.signatures_offset() | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 599 | << " but signatures are expected at offset " << buffer_offset_; | 
| Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 600 | *error = ErrorCode::kDownloadPayloadVerificationError; | 
|  | 601 | return false; | 
|  | 602 | } | 
|  | 603 | CopyDataToBuffer(&c_bytes, &count, manifest_.signatures_size()); | 
|  | 604 | // Needs more data to cover entire signature. | 
|  | 605 | if (buffer_.size() < manifest_.signatures_size()) | 
|  | 606 | return true; | 
|  | 607 | if (!ExtractSignatureMessage()) { | 
|  | 608 | LOG(ERROR) << "Extract payload signature failed."; | 
|  | 609 | *error = ErrorCode::kDownloadPayloadVerificationError; | 
|  | 610 | return false; | 
|  | 611 | } | 
|  | 612 | DiscardBuffer(true, 0); | 
| Alex Deymo | f25eb49 | 2016-02-26 00:20:08 -0800 | [diff] [blame] | 613 | // Since we extracted the SignatureMessage we need to advance the | 
|  | 614 | // checkpoint, otherwise we would reload the signature and try to extract | 
|  | 615 | // it again. | 
| Sen Jiang | 3a25dc2 | 2019-01-10 14:14:41 -0800 | [diff] [blame] | 616 | // This is the last checkpoint for an update, force this checkpoint to be | 
|  | 617 | // saved. | 
|  | 618 | CheckpointUpdateProgress(true); | 
| Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 619 | } | 
|  | 620 |  | 
| Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 621 | return true; | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 622 | } | 
|  | 623 |  | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 624 | bool DeltaPerformer::IsManifestValid() { | 
|  | 625 | return manifest_valid_; | 
|  | 626 | } | 
|  | 627 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 628 | bool DeltaPerformer::ParseManifestPartitions(ErrorCode* error) { | 
| Amin Hassani | 822d485 | 2020-03-10 01:50:42 +0000 | [diff] [blame] | 629 | partitions_.clear(); | 
|  | 630 | for (const PartitionUpdate& partition : manifest_.partitions()) { | 
|  | 631 | partitions_.push_back(partition); | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 632 | } | 
|  | 633 |  | 
| Tianjie | d3865d1 | 2020-06-03 15:25:17 -0700 | [diff] [blame] | 634 | // For VAB and partial updates, the partition preparation will copy the | 
|  | 635 | // dynamic partitions metadata to the target metadata slot, and rename the | 
|  | 636 | // slot suffix of the partitions in the metadata. | 
|  | 637 | if (install_plan_->target_slot != BootControlInterface::kInvalidSlot) { | 
|  | 638 | uint64_t required_size = 0; | 
|  | 639 | if (!PreparePartitionsForUpdate(&required_size)) { | 
|  | 640 | if (required_size > 0) { | 
|  | 641 | *error = ErrorCode::kNotEnoughSpace; | 
|  | 642 | } else { | 
|  | 643 | *error = ErrorCode::kInstallDeviceOpenError; | 
|  | 644 | } | 
|  | 645 | return false; | 
|  | 646 | } | 
|  | 647 | } | 
|  | 648 |  | 
| Tianjie | 55abd3c | 2020-06-19 00:22:59 -0700 | [diff] [blame] | 649 | // Partitions in manifest are no longer needed after preparing partitions. | 
| Amin Hassani | 822d485 | 2020-03-10 01:50:42 +0000 | [diff] [blame] | 650 | manifest_.clear_partitions(); | 
| Tianjie | d3865d1 | 2020-06-03 15:25:17 -0700 | [diff] [blame] | 651 | // TODO(xunchang) TBD: allow partial update only on devices with dynamic | 
|  | 652 | // partition. | 
|  | 653 | if (manifest_.partial_update()) { | 
|  | 654 | std::set<std::string> touched_partitions; | 
|  | 655 | for (const auto& partition_update : partitions_) { | 
|  | 656 | touched_partitions.insert(partition_update.partition_name()); | 
|  | 657 | } | 
|  | 658 |  | 
| Tianjie | 99d570d | 2020-06-04 14:57:19 -0700 | [diff] [blame] | 659 | auto generator = partition_update_generator::Create(boot_control_, | 
|  | 660 | manifest_.block_size()); | 
| Tianjie | 24f9609 | 2020-06-30 12:26:25 -0700 | [diff] [blame] | 661 | std::vector<PartitionUpdate> untouched_static_partitions; | 
| Tianjie | d3865d1 | 2020-06-03 15:25:17 -0700 | [diff] [blame] | 662 | TEST_AND_RETURN_FALSE( | 
|  | 663 | generator->GenerateOperationsForPartitionsNotInPayload( | 
|  | 664 | install_plan_->source_slot, | 
|  | 665 | install_plan_->target_slot, | 
|  | 666 | touched_partitions, | 
| Tianjie | 24f9609 | 2020-06-30 12:26:25 -0700 | [diff] [blame] | 667 | &untouched_static_partitions)); | 
|  | 668 | partitions_.insert(partitions_.end(), | 
|  | 669 | untouched_static_partitions.begin(), | 
|  | 670 | untouched_static_partitions.end()); | 
|  | 671 |  | 
|  | 672 | // Save the untouched dynamic partitions in install plan. | 
|  | 673 | std::vector<std::string> dynamic_partitions; | 
|  | 674 | if (!boot_control_->GetDynamicPartitionControl() | 
|  | 675 | ->ListDynamicPartitionsForSlot(install_plan_->source_slot, | 
| Tianjie | 3a55fc2 | 2021-02-13 16:02:22 -0800 | [diff] [blame] | 676 | boot_control_->GetCurrentSlot(), | 
| Tianjie | 24f9609 | 2020-06-30 12:26:25 -0700 | [diff] [blame] | 677 | &dynamic_partitions)) { | 
|  | 678 | LOG(ERROR) << "Failed to load dynamic partitions from slot " | 
|  | 679 | << install_plan_->source_slot; | 
|  | 680 | return false; | 
|  | 681 | } | 
|  | 682 | install_plan_->untouched_dynamic_partitions.clear(); | 
|  | 683 | for (const auto& name : dynamic_partitions) { | 
|  | 684 | if (touched_partitions.find(name) == touched_partitions.end()) { | 
|  | 685 | install_plan_->untouched_dynamic_partitions.push_back(name); | 
|  | 686 | } | 
|  | 687 | } | 
| Tianjie | d3865d1 | 2020-06-03 15:25:17 -0700 | [diff] [blame] | 688 | } | 
|  | 689 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 690 | // Fill in the InstallPlan::partitions based on the partitions from the | 
|  | 691 | // payload. | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 692 | for (const auto& partition : partitions_) { | 
|  | 693 | InstallPlan::Partition install_part; | 
|  | 694 | install_part.name = partition.partition_name(); | 
|  | 695 | install_part.run_postinstall = | 
|  | 696 | partition.has_run_postinstall() && partition.run_postinstall(); | 
| Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 697 | if (install_part.run_postinstall) { | 
|  | 698 | install_part.postinstall_path = | 
|  | 699 | (partition.has_postinstall_path() ? partition.postinstall_path() | 
|  | 700 | : kPostinstallDefaultScript); | 
|  | 701 | install_part.filesystem_type = partition.filesystem_type(); | 
| Alex Deymo | 5b91c6b | 2016-08-04 20:33:36 -0700 | [diff] [blame] | 702 | install_part.postinstall_optional = partition.postinstall_optional(); | 
| Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 703 | } | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 704 |  | 
|  | 705 | if (partition.has_old_partition_info()) { | 
|  | 706 | const PartitionInfo& info = partition.old_partition_info(); | 
|  | 707 | install_part.source_size = info.size(); | 
|  | 708 | install_part.source_hash.assign(info.hash().begin(), info.hash().end()); | 
|  | 709 | } | 
|  | 710 |  | 
|  | 711 | if (!partition.has_new_partition_info()) { | 
|  | 712 | LOG(ERROR) << "Unable to get new partition hash info on partition " | 
|  | 713 | << install_part.name << "."; | 
|  | 714 | *error = ErrorCode::kDownloadNewPartitionInfoError; | 
|  | 715 | return false; | 
|  | 716 | } | 
|  | 717 | const PartitionInfo& info = partition.new_partition_info(); | 
|  | 718 | install_part.target_size = info.size(); | 
|  | 719 | install_part.target_hash.assign(info.hash().begin(), info.hash().end()); | 
|  | 720 |  | 
| Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 721 | install_part.block_size = block_size_; | 
|  | 722 | if (partition.has_hash_tree_extent()) { | 
|  | 723 | Extent extent = partition.hash_tree_data_extent(); | 
|  | 724 | install_part.hash_tree_data_offset = extent.start_block() * block_size_; | 
|  | 725 | install_part.hash_tree_data_size = extent.num_blocks() * block_size_; | 
|  | 726 | extent = partition.hash_tree_extent(); | 
|  | 727 | install_part.hash_tree_offset = extent.start_block() * block_size_; | 
|  | 728 | install_part.hash_tree_size = extent.num_blocks() * block_size_; | 
|  | 729 | uint64_t hash_tree_data_end = | 
|  | 730 | install_part.hash_tree_data_offset + install_part.hash_tree_data_size; | 
|  | 731 | if (install_part.hash_tree_offset < hash_tree_data_end) { | 
|  | 732 | LOG(ERROR) << "Invalid hash tree extents, hash tree data ends at " | 
|  | 733 | << hash_tree_data_end << ", but hash tree starts at " | 
|  | 734 | << install_part.hash_tree_offset; | 
|  | 735 | *error = ErrorCode::kDownloadNewPartitionInfoError; | 
|  | 736 | return false; | 
|  | 737 | } | 
|  | 738 | install_part.hash_tree_algorithm = partition.hash_tree_algorithm(); | 
|  | 739 | install_part.hash_tree_salt.assign(partition.hash_tree_salt().begin(), | 
|  | 740 | partition.hash_tree_salt().end()); | 
|  | 741 | } | 
|  | 742 | if (partition.has_fec_extent()) { | 
|  | 743 | Extent extent = partition.fec_data_extent(); | 
|  | 744 | install_part.fec_data_offset = extent.start_block() * block_size_; | 
|  | 745 | install_part.fec_data_size = extent.num_blocks() * block_size_; | 
|  | 746 | extent = partition.fec_extent(); | 
|  | 747 | install_part.fec_offset = extent.start_block() * block_size_; | 
|  | 748 | install_part.fec_size = extent.num_blocks() * block_size_; | 
|  | 749 | uint64_t fec_data_end = | 
|  | 750 | install_part.fec_data_offset + install_part.fec_data_size; | 
|  | 751 | if (install_part.fec_offset < fec_data_end) { | 
|  | 752 | LOG(ERROR) << "Invalid fec extents, fec data ends at " << fec_data_end | 
|  | 753 | << ", but fec starts at " << install_part.fec_offset; | 
|  | 754 | *error = ErrorCode::kDownloadNewPartitionInfoError; | 
|  | 755 | return false; | 
|  | 756 | } | 
|  | 757 | install_part.fec_roots = partition.fec_roots(); | 
|  | 758 | } | 
|  | 759 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 760 | install_plan_->partitions.push_back(install_part); | 
|  | 761 | } | 
|  | 762 |  | 
| Tianjie | d3865d1 | 2020-06-03 15:25:17 -0700 | [diff] [blame] | 763 | // TODO(xunchang) only need to load the partitions for those in payload. | 
|  | 764 | // Because we have already loaded the other once when generating SOURCE_COPY | 
|  | 765 | // operations. | 
| Alex Deymo | 542c19b | 2015-12-03 07:43:31 -0300 | [diff] [blame] | 766 | if (!install_plan_->LoadPartitionsFromSlots(boot_control_)) { | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 767 | LOG(ERROR) << "Unable to determine all the partition devices."; | 
|  | 768 | *error = ErrorCode::kInstallDeviceOpenError; | 
|  | 769 | return false; | 
|  | 770 | } | 
|  | 771 | LogPartitionInfo(partitions_); | 
|  | 772 | return true; | 
|  | 773 | } | 
|  | 774 |  | 
| Yifan Hong | b9d6357 | 2020-01-09 17:50:46 -0800 | [diff] [blame] | 775 | bool DeltaPerformer::PreparePartitionsForUpdate(uint64_t* required_size) { | 
|  | 776 | // Call static PreparePartitionsForUpdate with hash from | 
|  | 777 | // kPrefsUpdateCheckResponseHash to ensure hash of payload that space is | 
|  | 778 | // preallocated for is the same as the hash of payload being applied. | 
|  | 779 | string update_check_response_hash; | 
|  | 780 | ignore_result(prefs_->GetString(kPrefsUpdateCheckResponseHash, | 
|  | 781 | &update_check_response_hash)); | 
|  | 782 | return PreparePartitionsForUpdate(prefs_, | 
|  | 783 | boot_control_, | 
|  | 784 | install_plan_->target_slot, | 
|  | 785 | manifest_, | 
|  | 786 | update_check_response_hash, | 
|  | 787 | required_size); | 
|  | 788 | } | 
|  | 789 |  | 
|  | 790 | bool DeltaPerformer::PreparePartitionsForUpdate( | 
|  | 791 | PrefsInterface* prefs, | 
|  | 792 | BootControlInterface* boot_control, | 
|  | 793 | BootControlInterface::Slot target_slot, | 
|  | 794 | const DeltaArchiveManifest& manifest, | 
|  | 795 | const std::string& update_check_response_hash, | 
|  | 796 | uint64_t* required_size) { | 
|  | 797 | string last_hash; | 
|  | 798 | ignore_result( | 
|  | 799 | prefs->GetString(kPrefsDynamicPartitionMetadataUpdated, &last_hash)); | 
|  | 800 |  | 
|  | 801 | bool is_resume = !update_check_response_hash.empty() && | 
|  | 802 | last_hash == update_check_response_hash; | 
|  | 803 |  | 
|  | 804 | if (is_resume) { | 
|  | 805 | LOG(INFO) << "Using previously prepared partitions for update. hash = " | 
|  | 806 | << last_hash; | 
|  | 807 | } else { | 
|  | 808 | LOG(INFO) << "Preparing partitions for new update. last hash = " | 
|  | 809 | << last_hash << ", new hash = " << update_check_response_hash; | 
| Kelvin Zhang | 51e08b9 | 2021-02-19 14:54:21 -0500 | [diff] [blame] | 810 | ResetUpdateProgress(prefs, false); | 
| Yifan Hong | b9d6357 | 2020-01-09 17:50:46 -0800 | [diff] [blame] | 811 | } | 
|  | 812 |  | 
|  | 813 | if (!boot_control->GetDynamicPartitionControl()->PreparePartitionsForUpdate( | 
|  | 814 | boot_control->GetCurrentSlot(), | 
|  | 815 | target_slot, | 
|  | 816 | manifest, | 
|  | 817 | !is_resume /* should update */, | 
|  | 818 | required_size)) { | 
| Yifan Hong | 9acd9cb | 2018-10-19 14:52:45 -0700 | [diff] [blame] | 819 | LOG(ERROR) << "Unable to initialize partition metadata for slot " | 
| Yifan Hong | b9d6357 | 2020-01-09 17:50:46 -0800 | [diff] [blame] | 820 | << BootControlInterface::SlotName(target_slot); | 
| Yifan Hong | 9acd9cb | 2018-10-19 14:52:45 -0700 | [diff] [blame] | 821 | return false; | 
|  | 822 | } | 
| Yifan Hong | b9d6357 | 2020-01-09 17:50:46 -0800 | [diff] [blame] | 823 |  | 
|  | 824 | TEST_AND_RETURN_FALSE(prefs->SetString(kPrefsDynamicPartitionMetadataUpdated, | 
|  | 825 | update_check_response_hash)); | 
| Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 826 | LOG(INFO) << "PreparePartitionsForUpdate done."; | 
| Yifan Hong | 9acd9cb | 2018-10-19 14:52:45 -0700 | [diff] [blame] | 827 |  | 
|  | 828 | return true; | 
|  | 829 | } | 
|  | 830 |  | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 831 | bool DeltaPerformer::CanPerformInstallOperation( | 
| Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 832 | const chromeos_update_engine::InstallOperation& operation) { | 
| Alex Deymo | 0497d05 | 2016-03-23 09:16:59 -0700 | [diff] [blame] | 833 | // If we don't have a data blob we can apply it right away. | 
|  | 834 | if (!operation.has_data_offset() && !operation.has_data_length()) | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 835 | return true; | 
|  | 836 |  | 
|  | 837 | // See if we have the entire data blob in the buffer | 
|  | 838 | if (operation.data_offset() < buffer_offset_) { | 
|  | 839 | LOG(ERROR) << "we threw away data it seems?"; | 
|  | 840 | return false; | 
|  | 841 | } | 
| Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 842 |  | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 843 | return (operation.data_offset() + operation.data_length() <= | 
|  | 844 | buffer_offset_ + buffer_.size()); | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 845 | } | 
|  | 846 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 847 | bool DeltaPerformer::PerformReplaceOperation( | 
|  | 848 | const InstallOperation& operation) { | 
| Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 849 | CHECK(operation.type() == InstallOperation::REPLACE || | 
| Alex Deymo | 2d621a3 | 2015-10-01 11:09:01 -0700 | [diff] [blame] | 850 | operation.type() == InstallOperation::REPLACE_BZ || | 
|  | 851 | operation.type() == InstallOperation::REPLACE_XZ); | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 852 |  | 
|  | 853 | // Since we delete data off the beginning of the buffer as we use it, | 
|  | 854 | // the data we need should be exactly at the beginning of the buffer. | 
| Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 855 | TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length()); | 
| Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 856 |  | 
| Kelvin Zhang | 9bd519d | 2020-09-23 12:55:19 -0400 | [diff] [blame] | 857 | TEST_AND_RETURN_FALSE(partition_writer_->PerformReplaceOperation( | 
|  | 858 | operation, buffer_.data(), buffer_.size())); | 
|  | 859 | // Update buffer | 
|  | 860 | DiscardBuffer(true, buffer_.size()); | 
|  | 861 | return true; | 
|  | 862 | } | 
|  | 863 |  | 
| Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 864 | bool DeltaPerformer::PerformZeroOrDiscardOperation( | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 865 | const InstallOperation& operation) { | 
| Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 866 | CHECK(operation.type() == InstallOperation::DISCARD || | 
|  | 867 | operation.type() == InstallOperation::ZERO); | 
|  | 868 |  | 
|  | 869 | // These operations have no blob. | 
|  | 870 | TEST_AND_RETURN_FALSE(!operation.has_data_offset()); | 
|  | 871 | TEST_AND_RETURN_FALSE(!operation.has_data_length()); | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 872 |  | 
| Kelvin Zhang | 9bd519d | 2020-09-23 12:55:19 -0400 | [diff] [blame] | 873 | return partition_writer_->PerformZeroOrDiscardOperation(operation); | 
|  | 874 | } | 
| Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 875 |  | 
| Kelvin Zhang | 9bd519d | 2020-09-23 12:55:19 -0400 | [diff] [blame] | 876 | bool PartitionWriter::ValidateSourceHash(const brillo::Blob& calculated_hash, | 
|  | 877 | const InstallOperation& operation, | 
|  | 878 | const FileDescriptorPtr source_fd, | 
|  | 879 | ErrorCode* error) { | 
| Sen Jiang | 2ec4aab | 2015-11-13 15:04:03 -0800 | [diff] [blame] | 880 | brillo::Blob expected_source_hash(operation.src_sha256_hash().begin(), | 
|  | 881 | operation.src_sha256_hash().end()); | 
|  | 882 | if (calculated_hash != expected_source_hash) { | 
| Alex Deymo | 6714084 | 2016-06-15 13:28:59 -0700 | [diff] [blame] | 883 | LOG(ERROR) << "The hash of the source data on disk for this operation " | 
|  | 884 | << "doesn't match the expected value. This could mean that the " | 
|  | 885 | << "delta update payload was targeted for another version, or " | 
|  | 886 | << "that the source partition was modified after it was " | 
|  | 887 | << "installed, for example, by mounting a filesystem."; | 
|  | 888 | LOG(ERROR) << "Expected:   sha256|hex = " | 
|  | 889 | << base::HexEncode(expected_source_hash.data(), | 
|  | 890 | expected_source_hash.size()); | 
|  | 891 | LOG(ERROR) << "Calculated: sha256|hex = " | 
|  | 892 | << base::HexEncode(calculated_hash.data(), | 
|  | 893 | calculated_hash.size()); | 
|  | 894 |  | 
|  | 895 | vector<string> source_extents; | 
|  | 896 | for (const Extent& ext : operation.src_extents()) { | 
| Tamas Berghammer | 9d66d76 | 2016-07-15 14:19:04 +0100 | [diff] [blame] | 897 | source_extents.push_back( | 
|  | 898 | base::StringPrintf("%" PRIu64 ":%" PRIu64, | 
|  | 899 | static_cast<uint64_t>(ext.start_block()), | 
|  | 900 | static_cast<uint64_t>(ext.num_blocks()))); | 
| Alex Deymo | 6714084 | 2016-06-15 13:28:59 -0700 | [diff] [blame] | 901 | } | 
|  | 902 | LOG(ERROR) << "Operation source (offset:size) in blocks: " | 
|  | 903 | << base::JoinString(source_extents, ","); | 
| Sen Jiang | be2c47b | 2016-06-15 14:09:27 -0700 | [diff] [blame] | 904 |  | 
| HÃ¥kan Kvist | 4e13cf4 | 2018-01-23 12:57:55 +0100 | [diff] [blame] | 905 | // Log remount history if this device is an ext4 partition. | 
|  | 906 | LogMountHistory(source_fd); | 
|  | 907 |  | 
| Sen Jiang | be2c47b | 2016-06-15 14:09:27 -0700 | [diff] [blame] | 908 | *error = ErrorCode::kDownloadStateInitializationError; | 
| Sen Jiang | 2ec4aab | 2015-11-13 15:04:03 -0800 | [diff] [blame] | 909 | return false; | 
|  | 910 | } | 
|  | 911 | return true; | 
|  | 912 | } | 
|  | 913 |  | 
| Allie Wood | 9f6f0a5 | 2015-03-30 11:25:47 -0700 | [diff] [blame] | 914 | bool DeltaPerformer::PerformSourceCopyOperation( | 
| Sen Jiang | be2c47b | 2016-06-15 14:09:27 -0700 | [diff] [blame] | 915 | const InstallOperation& operation, ErrorCode* error) { | 
| Allie Wood | 9f6f0a5 | 2015-03-30 11:25:47 -0700 | [diff] [blame] | 916 | if (operation.has_src_length()) | 
|  | 917 | TEST_AND_RETURN_FALSE(operation.src_length() % block_size_ == 0); | 
|  | 918 | if (operation.has_dst_length()) | 
|  | 919 | TEST_AND_RETURN_FALSE(operation.dst_length() % block_size_ == 0); | 
| Kelvin Zhang | 9bd519d | 2020-09-23 12:55:19 -0400 | [diff] [blame] | 920 | return partition_writer_->PerformSourceCopyOperation(operation, error); | 
|  | 921 | } | 
| Allie Wood | 9f6f0a5 | 2015-03-30 11:25:47 -0700 | [diff] [blame] | 922 |  | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 923 | bool DeltaPerformer::ExtentsToBsdiffPositionsString( | 
|  | 924 | const RepeatedPtrField<Extent>& extents, | 
|  | 925 | uint64_t block_size, | 
|  | 926 | uint64_t full_length, | 
|  | 927 | string* positions_string) { | 
|  | 928 | string ret; | 
|  | 929 | uint64_t length = 0; | 
| Chih-Hung Hsieh | 5c6bb1d | 2016-07-27 13:33:15 -0700 | [diff] [blame] | 930 | for (const Extent& extent : extents) { | 
| Allie Wood | 5687345 | 2015-03-27 17:48:40 -0700 | [diff] [blame] | 931 | int64_t start = extent.start_block() * block_size; | 
| Tamas Berghammer | 9d66d76 | 2016-07-15 14:19:04 +0100 | [diff] [blame] | 932 | uint64_t this_length = | 
|  | 933 | min(full_length - length, | 
|  | 934 | static_cast<uint64_t>(extent.num_blocks()) * block_size); | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 935 | ret += base::StringPrintf("%" PRIi64 ":%" PRIu64 ",", start, this_length); | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 936 | length += this_length; | 
|  | 937 | } | 
|  | 938 | TEST_AND_RETURN_FALSE(length == full_length); | 
|  | 939 | if (!ret.empty()) | 
|  | 940 | ret.resize(ret.size() - 1);  // Strip trailing comma off | 
|  | 941 | *positions_string = ret; | 
|  | 942 | return true; | 
|  | 943 | } | 
|  | 944 |  | 
| Allie Wood | 9f6f0a5 | 2015-03-30 11:25:47 -0700 | [diff] [blame] | 945 | bool DeltaPerformer::PerformSourceBsdiffOperation( | 
| Sen Jiang | be2c47b | 2016-06-15 14:09:27 -0700 | [diff] [blame] | 946 | const InstallOperation& operation, ErrorCode* error) { | 
| Allie Wood | 9f6f0a5 | 2015-03-30 11:25:47 -0700 | [diff] [blame] | 947 | // Since we delete data off the beginning of the buffer as we use it, | 
|  | 948 | // the data we need should be exactly at the beginning of the buffer. | 
|  | 949 | TEST_AND_RETURN_FALSE(buffer_offset_ == operation.data_offset()); | 
|  | 950 | TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length()); | 
|  | 951 | if (operation.has_src_length()) | 
|  | 952 | TEST_AND_RETURN_FALSE(operation.src_length() % block_size_ == 0); | 
|  | 953 | if (operation.has_dst_length()) | 
|  | 954 | TEST_AND_RETURN_FALSE(operation.dst_length() % block_size_ == 0); | 
|  | 955 |  | 
| Kelvin Zhang | 9bd519d | 2020-09-23 12:55:19 -0400 | [diff] [blame] | 956 | TEST_AND_RETURN_FALSE(partition_writer_->PerformSourceBsdiffOperation( | 
|  | 957 | operation, error, buffer_.data(), buffer_.size())); | 
|  | 958 | DiscardBuffer(true, buffer_.size()); | 
|  | 959 | return true; | 
|  | 960 | } | 
|  | 961 |  | 
| Amin Hassani | 02855c2 | 2017-09-06 22:34:50 -0700 | [diff] [blame] | 962 | bool DeltaPerformer::PerformPuffDiffOperation(const InstallOperation& operation, | 
|  | 963 | ErrorCode* error) { | 
|  | 964 | // Since we delete data off the beginning of the buffer as we use it, | 
|  | 965 | // the data we need should be exactly at the beginning of the buffer. | 
|  | 966 | TEST_AND_RETURN_FALSE(buffer_offset_ == operation.data_offset()); | 
|  | 967 | TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length()); | 
| Kelvin Zhang | 9bd519d | 2020-09-23 12:55:19 -0400 | [diff] [blame] | 968 | TEST_AND_RETURN_FALSE(partition_writer_->PerformPuffDiffOperation( | 
|  | 969 | operation, error, buffer_.data(), buffer_.size())); | 
|  | 970 | DiscardBuffer(true, buffer_.size()); | 
|  | 971 | return true; | 
|  | 972 | } | 
| Amin Hassani | 02855c2 | 2017-09-06 22:34:50 -0700 | [diff] [blame] | 973 |  | 
| Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 974 | bool DeltaPerformer::ExtractSignatureMessage() { | 
| Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 975 | TEST_AND_RETURN_FALSE(signatures_message_data_.empty()); | 
|  | 976 | TEST_AND_RETURN_FALSE(buffer_offset_ == manifest_.signatures_offset()); | 
|  | 977 | TEST_AND_RETURN_FALSE(buffer_.size() >= manifest_.signatures_size()); | 
| Darin Petkov | 4f0a07b | 2011-05-25 16:47:20 -0700 | [diff] [blame] | 978 | signatures_message_data_.assign( | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 979 | buffer_.begin(), buffer_.begin() + manifest_.signatures_size()); | 
| Darin Petkov | 4f0a07b | 2011-05-25 16:47:20 -0700 | [diff] [blame] | 980 |  | 
| Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 981 | LOG(INFO) << "Extracted signature data of size " | 
|  | 982 | << manifest_.signatures_size() << " at " | 
|  | 983 | << manifest_.signatures_offset(); | 
|  | 984 | return true; | 
|  | 985 | } | 
|  | 986 |  | 
| Sen Jiang | 08c6da1 | 2019-01-07 18:28:56 -0800 | [diff] [blame] | 987 | bool DeltaPerformer::GetPublicKey(string* out_public_key) { | 
|  | 988 | out_public_key->clear(); | 
| David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 989 |  | 
| Sen Jiang | 08c6da1 | 2019-01-07 18:28:56 -0800 | [diff] [blame] | 990 | if (utils::FileExists(public_key_path_.c_str())) { | 
|  | 991 | LOG(INFO) << "Verifying using public key: " << public_key_path_; | 
|  | 992 | return utils::ReadFile(public_key_path_, out_public_key); | 
|  | 993 | } | 
|  | 994 |  | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 995 | // If this is an official build then we are not allowed to use public key | 
|  | 996 | // from Omaha response. | 
| Sen Jiang | 08c6da1 | 2019-01-07 18:28:56 -0800 | [diff] [blame] | 997 | if (!hardware_->IsOfficialBuild() && !install_plan_->public_key_rsa.empty()) { | 
|  | 998 | LOG(INFO) << "Verifying using public key from Omaha response."; | 
|  | 999 | return brillo::data_encoding::Base64Decode(install_plan_->public_key_rsa, | 
|  | 1000 | out_public_key); | 
|  | 1001 | } | 
| Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 1002 | LOG(INFO) << "No public keys found for verification."; | 
| David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 1003 | return true; | 
|  | 1004 | } | 
|  | 1005 |  | 
| Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 1006 | std::pair<std::unique_ptr<PayloadVerifier>, bool> | 
|  | 1007 | DeltaPerformer::CreatePayloadVerifier() { | 
|  | 1008 | if (utils::FileExists(update_certificates_path_.c_str())) { | 
|  | 1009 | LOG(INFO) << "Verifying using certificates: " << update_certificates_path_; | 
|  | 1010 | return { | 
|  | 1011 | PayloadVerifier::CreateInstanceFromZipPath(update_certificates_path_), | 
|  | 1012 | true}; | 
|  | 1013 | } | 
|  | 1014 |  | 
|  | 1015 | string public_key; | 
|  | 1016 | if (!GetPublicKey(&public_key)) { | 
|  | 1017 | LOG(ERROR) << "Failed to read public key"; | 
|  | 1018 | return {nullptr, true}; | 
|  | 1019 | } | 
|  | 1020 |  | 
|  | 1021 | // Skips the verification if the public key is empty. | 
|  | 1022 | if (public_key.empty()) { | 
|  | 1023 | return {nullptr, false}; | 
|  | 1024 | } | 
|  | 1025 | return {PayloadVerifier::CreateInstance(public_key), true}; | 
|  | 1026 | } | 
|  | 1027 |  | 
| Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 1028 | ErrorCode DeltaPerformer::ValidateManifest() { | 
| Saint Chou | 0a92a62 | 2020-07-29 14:25:35 +0000 | [diff] [blame] | 1029 | // Perform assorted checks to validation check the manifest, make sure it | 
| Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 1030 | // matches data from other sources, and that it is a supported version. | 
| Amin Hassani | 822d485 | 2020-03-10 01:50:42 +0000 | [diff] [blame] | 1031 | bool has_old_fields = std::any_of(manifest_.partitions().begin(), | 
|  | 1032 | manifest_.partitions().end(), | 
|  | 1033 | [](const PartitionUpdate& partition) { | 
|  | 1034 | return partition.has_old_partition_info(); | 
|  | 1035 | }); | 
| Sen Jiang | c8f6b7a | 2015-10-21 11:09:59 -0700 | [diff] [blame] | 1036 |  | 
| Alex Deymo | 64d9878 | 2016-02-05 18:03:48 -0800 | [diff] [blame] | 1037 | // The presence of an old partition hash is the sole indicator for a delta | 
| Tianjie | f5baff4 | 2020-07-17 21:43:22 -0700 | [diff] [blame] | 1038 | // update. Also, always treat the partial update as delta so that we can | 
|  | 1039 | // perform the minor version check correctly. | 
| Alex Deymo | 64d9878 | 2016-02-05 18:03:48 -0800 | [diff] [blame] | 1040 | InstallPayloadType actual_payload_type = | 
| Tianjie | f5baff4 | 2020-07-17 21:43:22 -0700 | [diff] [blame] | 1041 | (has_old_fields || manifest_.partial_update()) | 
|  | 1042 | ? InstallPayloadType::kDelta | 
|  | 1043 | : InstallPayloadType::kFull; | 
| Alex Deymo | 64d9878 | 2016-02-05 18:03:48 -0800 | [diff] [blame] | 1044 |  | 
| Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 1045 | if (payload_->type == InstallPayloadType::kUnknown) { | 
| Alex Deymo | 64d9878 | 2016-02-05 18:03:48 -0800 | [diff] [blame] | 1046 | LOG(INFO) << "Detected a '" | 
|  | 1047 | << InstallPayloadTypeToString(actual_payload_type) | 
|  | 1048 | << "' payload."; | 
| Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 1049 | payload_->type = actual_payload_type; | 
|  | 1050 | } else if (payload_->type != actual_payload_type) { | 
| Alex Deymo | 64d9878 | 2016-02-05 18:03:48 -0800 | [diff] [blame] | 1051 | LOG(ERROR) << "InstallPlan expected a '" | 
| Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 1052 | << InstallPayloadTypeToString(payload_->type) | 
| Alex Deymo | 64d9878 | 2016-02-05 18:03:48 -0800 | [diff] [blame] | 1053 | << "' payload but the downloaded manifest contains a '" | 
|  | 1054 | << InstallPayloadTypeToString(actual_payload_type) | 
|  | 1055 | << "' payload."; | 
|  | 1056 | return ErrorCode::kPayloadMismatchedType; | 
|  | 1057 | } | 
| Alex Deymo | 64d9878 | 2016-02-05 18:03:48 -0800 | [diff] [blame] | 1058 | // Check that the minor version is compatible. | 
| Tianjie | d3865d1 | 2020-06-03 15:25:17 -0700 | [diff] [blame] | 1059 | // TODO(xunchang) increment minor version & add check for partial update | 
| Alex Deymo | 64d9878 | 2016-02-05 18:03:48 -0800 | [diff] [blame] | 1060 | if (actual_payload_type == InstallPayloadType::kFull) { | 
| Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 1061 | if (manifest_.minor_version() != kFullPayloadMinorVersion) { | 
|  | 1062 | LOG(ERROR) << "Manifest contains minor version " | 
|  | 1063 | << manifest_.minor_version() | 
|  | 1064 | << ", but all full payloads should have version " | 
|  | 1065 | << kFullPayloadMinorVersion << "."; | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1066 | return ErrorCode::kUnsupportedMinorPayloadVersion; | 
| Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 1067 | } | 
|  | 1068 | } else { | 
| Sen Jiang | f123663 | 2018-05-11 16:03:23 -0700 | [diff] [blame] | 1069 | if (manifest_.minor_version() < kMinSupportedMinorPayloadVersion || | 
|  | 1070 | manifest_.minor_version() > kMaxSupportedMinorPayloadVersion) { | 
| Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 1071 | LOG(ERROR) << "Manifest contains minor version " | 
|  | 1072 | << manifest_.minor_version() | 
| Sen Jiang | f123663 | 2018-05-11 16:03:23 -0700 | [diff] [blame] | 1073 | << " not in the range of supported minor versions [" | 
|  | 1074 | << kMinSupportedMinorPayloadVersion << ", " | 
|  | 1075 | << kMaxSupportedMinorPayloadVersion << "]."; | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1076 | return ErrorCode::kUnsupportedMinorPayloadVersion; | 
| Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 1077 | } | 
| Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 1078 | } | 
|  | 1079 |  | 
| Yifan Hong | 8702933 | 2020-09-01 17:20:08 -0700 | [diff] [blame] | 1080 | ErrorCode error_code = CheckTimestampError(); | 
|  | 1081 | if (error_code != ErrorCode::kSuccess) { | 
|  | 1082 | if (error_code == ErrorCode::kPayloadTimestampError) { | 
|  | 1083 | if (!hardware_->AllowDowngrade()) { | 
|  | 1084 | return ErrorCode::kPayloadTimestampError; | 
|  | 1085 | } | 
|  | 1086 | LOG(INFO) << "The current OS build allows downgrade, continuing to apply" | 
|  | 1087 | " the payload with an older timestamp."; | 
|  | 1088 | } else { | 
|  | 1089 | LOG(ERROR) << "Timestamp check returned " | 
|  | 1090 | << utils::ErrorCodeToString(error_code); | 
|  | 1091 | return error_code; | 
| Tianjie Xu | 4ad3af6 | 2019-10-30 11:59:45 -0700 | [diff] [blame] | 1092 | } | 
| Sen Jiang | 8e768e9 | 2017-06-28 17:13:19 -0700 | [diff] [blame] | 1093 | } | 
|  | 1094 |  | 
| Amin Hassani | 55c7541 | 2019-10-07 11:20:39 -0700 | [diff] [blame] | 1095 | // TODO(crbug.com/37661) we should be adding more and more manifest checks, | 
|  | 1096 | // such as partition boundaries, etc. | 
| Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 1097 |  | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1098 | return ErrorCode::kSuccess; | 
| Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 1099 | } | 
|  | 1100 |  | 
| Yifan Hong | 8702933 | 2020-09-01 17:20:08 -0700 | [diff] [blame] | 1101 | ErrorCode DeltaPerformer::CheckTimestampError() const { | 
| Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 1102 | bool is_partial_update = | 
|  | 1103 | manifest_.has_partial_update() && manifest_.partial_update(); | 
|  | 1104 | const auto& partitions = manifest_.partitions(); | 
| Yifan Hong | 8702933 | 2020-09-01 17:20:08 -0700 | [diff] [blame] | 1105 |  | 
|  | 1106 | // Check version field for a given PartitionUpdate object. If an error | 
|  | 1107 | // is encountered, set |error_code| accordingly. If downgrade is detected, | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 1108 | // |downgrade_detected| is set. Return true if the program should continue | 
|  | 1109 | // to check the next partition or not, or false if it should exit early due | 
|  | 1110 | // to errors. | 
| Yifan Hong | 8702933 | 2020-09-01 17:20:08 -0700 | [diff] [blame] | 1111 | auto&& timestamp_valid = [this](const PartitionUpdate& partition, | 
|  | 1112 | bool allow_empty_version, | 
|  | 1113 | bool* downgrade_detected) -> ErrorCode { | 
| Tianjie | 19e5529 | 2020-10-19 21:49:37 -0700 | [diff] [blame] | 1114 | const auto& partition_name = partition.partition_name(); | 
| Yifan Hong | 8702933 | 2020-09-01 17:20:08 -0700 | [diff] [blame] | 1115 | if (!partition.has_version()) { | 
| Tianjie | 19e5529 | 2020-10-19 21:49:37 -0700 | [diff] [blame] | 1116 | if (hardware_->GetVersionForLogging(partition_name).empty()) { | 
|  | 1117 | LOG(INFO) << partition_name << " does't have version, skipping " | 
|  | 1118 | << "downgrade check."; | 
|  | 1119 | return ErrorCode::kSuccess; | 
|  | 1120 | } | 
|  | 1121 |  | 
| Yifan Hong | 8702933 | 2020-09-01 17:20:08 -0700 | [diff] [blame] | 1122 | if (allow_empty_version) { | 
|  | 1123 | return ErrorCode::kSuccess; | 
|  | 1124 | } | 
|  | 1125 | LOG(ERROR) | 
| Tianjie | 19e5529 | 2020-10-19 21:49:37 -0700 | [diff] [blame] | 1126 | << "PartitionUpdate " << partition_name | 
|  | 1127 | << " doesn't have a version field. Not allowed in partial updates."; | 
| Yifan Hong | 8702933 | 2020-09-01 17:20:08 -0700 | [diff] [blame] | 1128 | return ErrorCode::kDownloadManifestParseError; | 
|  | 1129 | } | 
|  | 1130 |  | 
| Tianjie | 19e5529 | 2020-10-19 21:49:37 -0700 | [diff] [blame] | 1131 | auto error_code = | 
|  | 1132 | hardware_->IsPartitionUpdateValid(partition_name, partition.version()); | 
| Yifan Hong | 8702933 | 2020-09-01 17:20:08 -0700 | [diff] [blame] | 1133 | switch (error_code) { | 
|  | 1134 | case ErrorCode::kSuccess: | 
|  | 1135 | break; | 
|  | 1136 | case ErrorCode::kPayloadTimestampError: | 
|  | 1137 | *downgrade_detected = true; | 
| Tianjie | 19e5529 | 2020-10-19 21:49:37 -0700 | [diff] [blame] | 1138 | LOG(WARNING) << "PartitionUpdate " << partition_name | 
| Yifan Hong | 8702933 | 2020-09-01 17:20:08 -0700 | [diff] [blame] | 1139 | << " has an older version than partition on device."; | 
|  | 1140 | break; | 
|  | 1141 | default: | 
| Tianjie | 19e5529 | 2020-10-19 21:49:37 -0700 | [diff] [blame] | 1142 | LOG(ERROR) << "IsPartitionUpdateValid(" << partition_name | 
| Yifan Hong | 8702933 | 2020-09-01 17:20:08 -0700 | [diff] [blame] | 1143 | << ") returned" << utils::ErrorCodeToString(error_code); | 
|  | 1144 | break; | 
|  | 1145 | } | 
|  | 1146 | return error_code; | 
| Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 1147 | }; | 
| Yifan Hong | 8702933 | 2020-09-01 17:20:08 -0700 | [diff] [blame] | 1148 |  | 
|  | 1149 | bool downgrade_detected = false; | 
|  | 1150 |  | 
| Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 1151 | if (is_partial_update) { | 
|  | 1152 | // for partial updates, all partition MUST have valid timestamps | 
|  | 1153 | // But max_timestamp can be empty | 
|  | 1154 | for (const auto& partition : partitions) { | 
| Yifan Hong | 8702933 | 2020-09-01 17:20:08 -0700 | [diff] [blame] | 1155 | auto error_code = timestamp_valid( | 
|  | 1156 | partition, false /* allow_empty_version */, &downgrade_detected); | 
|  | 1157 | if (error_code != ErrorCode::kSuccess && | 
|  | 1158 | error_code != ErrorCode::kPayloadTimestampError) { | 
|  | 1159 | return error_code; | 
| Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 1160 | } | 
|  | 1161 | } | 
| Yifan Hong | 8702933 | 2020-09-01 17:20:08 -0700 | [diff] [blame] | 1162 | if (downgrade_detected) { | 
|  | 1163 | return ErrorCode::kPayloadTimestampError; | 
|  | 1164 | } | 
|  | 1165 | return ErrorCode::kSuccess; | 
| Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 1166 | } | 
| Yifan Hong | 8702933 | 2020-09-01 17:20:08 -0700 | [diff] [blame] | 1167 |  | 
|  | 1168 | // For non-partial updates, check max_timestamp first. | 
| Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 1169 | if (manifest_.max_timestamp() < hardware_->GetBuildTimestamp()) { | 
|  | 1170 | LOG(ERROR) << "The current OS build timestamp (" | 
|  | 1171 | << hardware_->GetBuildTimestamp() | 
|  | 1172 | << ") is newer than the maximum timestamp in the manifest (" | 
|  | 1173 | << manifest_.max_timestamp() << ")"; | 
| Yifan Hong | 8702933 | 2020-09-01 17:20:08 -0700 | [diff] [blame] | 1174 | return ErrorCode::kPayloadTimestampError; | 
| Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 1175 | } | 
|  | 1176 | // Otherwise... partitions can have empty timestamps. | 
|  | 1177 | for (const auto& partition : partitions) { | 
| Yifan Hong | 8702933 | 2020-09-01 17:20:08 -0700 | [diff] [blame] | 1178 | auto error_code = timestamp_valid( | 
|  | 1179 | partition, true /* allow_empty_version */, &downgrade_detected); | 
|  | 1180 | if (error_code != ErrorCode::kSuccess && | 
|  | 1181 | error_code != ErrorCode::kPayloadTimestampError) { | 
|  | 1182 | return error_code; | 
| Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 1183 | } | 
|  | 1184 | } | 
| Yifan Hong | 8702933 | 2020-09-01 17:20:08 -0700 | [diff] [blame] | 1185 | if (downgrade_detected) { | 
|  | 1186 | return ErrorCode::kPayloadTimestampError; | 
|  | 1187 | } | 
|  | 1188 | return ErrorCode::kSuccess; | 
| Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 1189 | } | 
|  | 1190 |  | 
| David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1191 | ErrorCode DeltaPerformer::ValidateOperationHash( | 
| Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 1192 | const InstallOperation& operation) { | 
| Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1193 | if (!operation.data_sha256_hash().size()) { | 
|  | 1194 | if (!operation.data_length()) { | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 1195 | // Operations that do not have any data blob won't have any operation | 
|  | 1196 | // hash either. So, these operations are always considered validated | 
|  | 1197 | // since the metadata that contains all the non-data-blob portions of | 
|  | 1198 | // the operation has already been validated. This is true for both HTTP | 
|  | 1199 | // and HTTPS cases. | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1200 | return ErrorCode::kSuccess; | 
| Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1201 | } | 
|  | 1202 |  | 
| Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 1203 | // No hash is present for an operation that has data blobs. This shouldn't | 
|  | 1204 | // happen normally for any client that has this code, because the | 
| Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1205 | // corresponding update should have been produced with the operation | 
| Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 1206 | // hashes. So if it happens it means either we've turned operation hash | 
|  | 1207 | // generation off in DeltaDiffGenerator or it's a regression of some sort. | 
| Saint Chou | 0a92a62 | 2020-07-29 14:25:35 +0000 | [diff] [blame] | 1208 | // One caveat though: The last operation is a unused signature operation | 
| Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1209 | // that doesn't have a hash at the time the manifest is created. So we | 
|  | 1210 | // should not complaint about that operation. This operation can be | 
|  | 1211 | // recognized by the fact that it's offset is mentioned in the manifest. | 
|  | 1212 | if (manifest_.signatures_offset() && | 
|  | 1213 | manifest_.signatures_offset() == operation.data_offset()) { | 
|  | 1214 | LOG(INFO) << "Skipping hash verification for signature operation " | 
|  | 1215 | << next_operation_num_ + 1; | 
|  | 1216 | } else { | 
| Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 1217 | if (install_plan_->hash_checks_mandatory) { | 
|  | 1218 | LOG(ERROR) << "Missing mandatory operation hash for operation " | 
|  | 1219 | << next_operation_num_ + 1; | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1220 | return ErrorCode::kDownloadOperationHashMissingError; | 
| Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 1221 | } | 
|  | 1222 |  | 
| Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 1223 | LOG(WARNING) << "Cannot validate operation " << next_operation_num_ + 1 | 
|  | 1224 | << " as there's no operation hash in manifest"; | 
| Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1225 | } | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1226 | return ErrorCode::kSuccess; | 
| Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1227 | } | 
|  | 1228 |  | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1229 | brillo::Blob expected_op_hash; | 
| Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1230 | expected_op_hash.assign(operation.data_sha256_hash().data(), | 
|  | 1231 | (operation.data_sha256_hash().data() + | 
|  | 1232 | operation.data_sha256_hash().size())); | 
|  | 1233 |  | 
| Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 1234 | brillo::Blob calculated_op_hash; | 
|  | 1235 | if (!HashCalculator::RawHashOfBytes( | 
|  | 1236 | buffer_.data(), operation.data_length(), &calculated_op_hash)) { | 
| Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1237 | LOG(ERROR) << "Unable to compute actual hash of operation " | 
|  | 1238 | << next_operation_num_; | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1239 | return ErrorCode::kDownloadOperationHashVerificationError; | 
| Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1240 | } | 
|  | 1241 |  | 
| Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1242 | if (calculated_op_hash != expected_op_hash) { | 
|  | 1243 | LOG(ERROR) << "Hash verification failed for operation " | 
|  | 1244 | << next_operation_num_ << ". Expected hash = "; | 
|  | 1245 | utils::HexDumpVector(expected_op_hash); | 
|  | 1246 | LOG(ERROR) << "Calculated hash over " << operation.data_length() | 
|  | 1247 | << " bytes at offset: " << operation.data_offset() << " = "; | 
|  | 1248 | utils::HexDumpVector(calculated_op_hash); | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1249 | return ErrorCode::kDownloadOperationHashMismatch; | 
| Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1250 | } | 
|  | 1251 |  | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1252 | return ErrorCode::kSuccess; | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 1253 | } | 
|  | 1254 |  | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 1255 | #define TEST_AND_RETURN_VAL(_retval, _condition)              \ | 
|  | 1256 | do {                                                        \ | 
|  | 1257 | if (!(_condition)) {                                      \ | 
|  | 1258 | LOG(ERROR) << "VerifyPayload failure: " << #_condition; \ | 
|  | 1259 | return _retval;                                         \ | 
|  | 1260 | }                                                         \ | 
| Andrew de los Reyes | 771e1bd | 2011-08-30 14:47:23 -0700 | [diff] [blame] | 1261 | } while (0); | 
| Andrew de los Reyes | fb830ba | 2011-04-04 11:42:43 -0700 | [diff] [blame] | 1262 |  | 
| David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1263 | ErrorCode DeltaPerformer::VerifyPayload( | 
| Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 1264 | const brillo::Blob& update_check_response_hash, | 
| Andrew de los Reyes | 771e1bd | 2011-08-30 14:47:23 -0700 | [diff] [blame] | 1265 | const uint64_t update_check_response_size) { | 
| Jay Srinivasan | 0d8fb40 | 2012-05-07 19:19:38 -0700 | [diff] [blame] | 1266 | // Verifies the download size. | 
| Sen Jiang | 3a25dc2 | 2019-01-10 14:14:41 -0800 | [diff] [blame] | 1267 | if (update_check_response_size != | 
|  | 1268 | metadata_size_ + metadata_signature_size_ + buffer_offset_) { | 
|  | 1269 | LOG(ERROR) << "update_check_response_size (" << update_check_response_size | 
|  | 1270 | << ") doesn't match metadata_size (" << metadata_size_ | 
|  | 1271 | << ") + metadata_signature_size (" << metadata_signature_size_ | 
|  | 1272 | << ") + buffer_offset (" << buffer_offset_ << ")."; | 
|  | 1273 | return ErrorCode::kPayloadSizeMismatchError; | 
|  | 1274 | } | 
| Jay Srinivasan | 0d8fb40 | 2012-05-07 19:19:38 -0700 | [diff] [blame] | 1275 |  | 
| Amin Hassani | e331f5a | 2020-10-06 15:53:29 -0700 | [diff] [blame] | 1276 | // Verifies the payload hash. | 
|  | 1277 | TEST_AND_RETURN_VAL(ErrorCode::kDownloadPayloadVerificationError, | 
|  | 1278 | !payload_hash_calculator_.raw_hash().empty()); | 
|  | 1279 | TEST_AND_RETURN_VAL( | 
|  | 1280 | ErrorCode::kPayloadHashMismatchError, | 
|  | 1281 | payload_hash_calculator_.raw_hash() == update_check_response_hash); | 
|  | 1282 |  | 
| Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 1283 | // NOLINTNEXTLINE(whitespace/braces) | 
| Amin Hassani | f624e11 | 2020-09-15 15:30:08 -0700 | [diff] [blame] | 1284 | auto [payload_verifier, perform_verification] = CreatePayloadVerifier(); | 
|  | 1285 | if (!perform_verification) { | 
|  | 1286 | LOG(WARNING) << "Not verifying signed delta payload -- missing public key."; | 
|  | 1287 | return ErrorCode::kSuccess; | 
|  | 1288 | } | 
|  | 1289 | if (!payload_verifier) { | 
|  | 1290 | LOG(ERROR) << "Failed to create the payload verifier."; | 
|  | 1291 | return ErrorCode::kDownloadPayloadPubKeyVerificationError; | 
|  | 1292 | } | 
|  | 1293 |  | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1294 | TEST_AND_RETURN_VAL(ErrorCode::kSignedDeltaPayloadExpectedError, | 
| Andrew de los Reyes | 771e1bd | 2011-08-30 14:47:23 -0700 | [diff] [blame] | 1295 | !signatures_message_data_.empty()); | 
| Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 1296 | brillo::Blob hash_data = signed_hash_calculator_.raw_hash(); | 
| Alex Deymo | b552a68 | 2015-09-30 09:36:49 -0700 | [diff] [blame] | 1297 | TEST_AND_RETURN_VAL(ErrorCode::kDownloadPayloadPubKeyVerificationError, | 
| xunchang | cda3c03 | 2019-03-26 15:41:14 -0700 | [diff] [blame] | 1298 | hash_data.size() == kSHA256Size); | 
| Alex Deymo | b552a68 | 2015-09-30 09:36:49 -0700 | [diff] [blame] | 1299 |  | 
| Tianjie Xu | 6cf830b | 2019-09-30 11:31:49 -0700 | [diff] [blame] | 1300 | if (!payload_verifier->VerifySignature(signatures_message_data_, hash_data)) { | 
| David Zeuthen | bc27aac | 2013-11-26 11:17:48 -0800 | [diff] [blame] | 1301 | // The autoupdate_CatchBadSignatures test checks for this string | 
|  | 1302 | // in log-files. Keep in sync. | 
| Alex Deymo | b552a68 | 2015-09-30 09:36:49 -0700 | [diff] [blame] | 1303 | LOG(ERROR) << "Public key verification failed, thus update failed."; | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1304 | return ErrorCode::kDownloadPayloadPubKeyVerificationError; | 
| Andrew de los Reyes | fb830ba | 2011-04-04 11:42:43 -0700 | [diff] [blame] | 1305 | } | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 1306 |  | 
| David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 1307 | LOG(INFO) << "Payload hash matches value in payload."; | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1308 | return ErrorCode::kSuccess; | 
| Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 1309 | } | 
|  | 1310 |  | 
| Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 1311 | void DeltaPerformer::DiscardBuffer(bool do_advance_offset, | 
|  | 1312 | size_t signed_hash_buffer_size) { | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 1313 | // Update the buffer offset. | 
| Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 1314 | if (do_advance_offset) | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 1315 | buffer_offset_ += buffer_.size(); | 
|  | 1316 |  | 
|  | 1317 | // Hash the content. | 
| Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 1318 | payload_hash_calculator_.Update(buffer_.data(), buffer_.size()); | 
|  | 1319 | signed_hash_calculator_.Update(buffer_.data(), signed_hash_buffer_size); | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 1320 |  | 
|  | 1321 | // Swap content with an empty vector to ensure that all memory is released. | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1322 | brillo::Blob().swap(buffer_); | 
| Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 1323 | } | 
|  | 1324 |  | 
| Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1325 | bool DeltaPerformer::CanResumeUpdate(PrefsInterface* prefs, | 
| Chih-Hung Hsieh | 5c6bb1d | 2016-07-27 13:33:15 -0700 | [diff] [blame] | 1326 | const string& update_check_response_hash) { | 
| Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1327 | int64_t next_operation = kUpdateStateOperationInvalid; | 
| Alex Deymo | 3310b22 | 2015-03-30 15:59:07 -0700 | [diff] [blame] | 1328 | if (!(prefs->GetInt64(kPrefsUpdateStateNextOperation, &next_operation) && | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 1329 | next_operation != kUpdateStateOperationInvalid && next_operation > 0)) | 
| Alex Deymo | 3310b22 | 2015-03-30 15:59:07 -0700 | [diff] [blame] | 1330 | return false; | 
| Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1331 |  | 
|  | 1332 | string interrupted_hash; | 
| Alex Deymo | 3310b22 | 2015-03-30 15:59:07 -0700 | [diff] [blame] | 1333 | if (!(prefs->GetString(kPrefsUpdateCheckResponseHash, &interrupted_hash) && | 
|  | 1334 | !interrupted_hash.empty() && | 
|  | 1335 | interrupted_hash == update_check_response_hash)) | 
|  | 1336 | return false; | 
| Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1337 |  | 
| Darin Petkov | 6142614 | 2010-10-08 11:04:55 -0700 | [diff] [blame] | 1338 | int64_t resumed_update_failures; | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 1339 | // Note that storing this value is optional, but if it is there it should | 
|  | 1340 | // not be more than the limit. | 
| Alex Deymo | f25eb49 | 2016-02-26 00:20:08 -0800 | [diff] [blame] | 1341 | if (prefs->GetInt64(kPrefsResumedUpdateFailures, &resumed_update_failures) && | 
|  | 1342 | resumed_update_failures > kMaxResumedUpdateFailures) | 
| Alex Deymo | 3310b22 | 2015-03-30 15:59:07 -0700 | [diff] [blame] | 1343 | return false; | 
| Darin Petkov | 6142614 | 2010-10-08 11:04:55 -0700 | [diff] [blame] | 1344 |  | 
| Saint Chou | 0a92a62 | 2020-07-29 14:25:35 +0000 | [diff] [blame] | 1345 | // Validation check the rest. | 
| Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1346 | int64_t next_data_offset = -1; | 
| Alex Deymo | 3310b22 | 2015-03-30 15:59:07 -0700 | [diff] [blame] | 1347 | if (!(prefs->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset) && | 
|  | 1348 | next_data_offset >= 0)) | 
|  | 1349 | return false; | 
| Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1350 |  | 
| Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 1351 | string sha256_context; | 
| Alex Deymo | 3310b22 | 2015-03-30 15:59:07 -0700 | [diff] [blame] | 1352 | if (!(prefs->GetString(kPrefsUpdateStateSHA256Context, &sha256_context) && | 
|  | 1353 | !sha256_context.empty())) | 
|  | 1354 | return false; | 
| Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1355 |  | 
|  | 1356 | int64_t manifest_metadata_size = 0; | 
| Alex Deymo | 3310b22 | 2015-03-30 15:59:07 -0700 | [diff] [blame] | 1357 | if (!(prefs->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size) && | 
|  | 1358 | manifest_metadata_size > 0)) | 
|  | 1359 | return false; | 
| Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1360 |  | 
| Alex Deymo | f25eb49 | 2016-02-26 00:20:08 -0800 | [diff] [blame] | 1361 | int64_t manifest_signature_size = 0; | 
|  | 1362 | if (!(prefs->GetInt64(kPrefsManifestSignatureSize, | 
|  | 1363 | &manifest_signature_size) && | 
|  | 1364 | manifest_signature_size >= 0)) | 
|  | 1365 | return false; | 
|  | 1366 |  | 
| Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1367 | return true; | 
|  | 1368 | } | 
|  | 1369 |  | 
| Yifan Hong | 55c2bfe | 2020-01-13 17:01:19 -0800 | [diff] [blame] | 1370 | bool DeltaPerformer::ResetUpdateProgress( | 
|  | 1371 | PrefsInterface* prefs, | 
|  | 1372 | bool quick, | 
|  | 1373 | bool skip_dynamic_partititon_metadata_updated) { | 
| Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1374 | TEST_AND_RETURN_FALSE(prefs->SetInt64(kPrefsUpdateStateNextOperation, | 
|  | 1375 | kUpdateStateOperationInvalid)); | 
| Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1376 | if (!quick) { | 
| Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1377 | prefs->SetInt64(kPrefsUpdateStateNextDataOffset, -1); | 
| David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 1378 | prefs->SetInt64(kPrefsUpdateStateNextDataLength, 0); | 
| Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1379 | prefs->SetString(kPrefsUpdateStateSHA256Context, ""); | 
|  | 1380 | prefs->SetString(kPrefsUpdateStateSignedSHA256Context, ""); | 
| Darin Petkov | 4f0a07b | 2011-05-25 16:47:20 -0700 | [diff] [blame] | 1381 | prefs->SetString(kPrefsUpdateStateSignatureBlob, ""); | 
| Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1382 | prefs->SetInt64(kPrefsManifestMetadataSize, -1); | 
| Alex Deymo | f25eb49 | 2016-02-26 00:20:08 -0800 | [diff] [blame] | 1383 | prefs->SetInt64(kPrefsManifestSignatureSize, -1); | 
| Darin Petkov | 6142614 | 2010-10-08 11:04:55 -0700 | [diff] [blame] | 1384 | prefs->SetInt64(kPrefsResumedUpdateFailures, 0); | 
| Sen Jiang | fe52282 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 1385 | prefs->Delete(kPrefsPostInstallSucceeded); | 
| Sen Jiang | 3eeaf7d | 2018-10-11 13:55:32 -0700 | [diff] [blame] | 1386 | prefs->Delete(kPrefsVerityWritten); | 
| Yifan Hong | 55c2bfe | 2020-01-13 17:01:19 -0800 | [diff] [blame] | 1387 |  | 
|  | 1388 | if (!skip_dynamic_partititon_metadata_updated) { | 
|  | 1389 | LOG(INFO) << "Resetting recorded hash for prepared partitions."; | 
|  | 1390 | prefs->Delete(kPrefsDynamicPartitionMetadataUpdated); | 
|  | 1391 | } | 
| Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1392 | } | 
| Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 1393 | return true; | 
|  | 1394 | } | 
|  | 1395 |  | 
| Kelvin Zhang | cfe694f | 2020-11-13 13:10:42 -0500 | [diff] [blame] | 1396 | bool DeltaPerformer::ShouldCheckpoint() { | 
| Sen Jiang | 53c2ec5 | 2019-01-10 15:27:59 -0800 | [diff] [blame] | 1397 | base::TimeTicks curr_time = base::TimeTicks::Now(); | 
| Kelvin Zhang | cfe694f | 2020-11-13 13:10:42 -0500 | [diff] [blame] | 1398 | if (curr_time > update_checkpoint_time_) { | 
| Colin Howes | 0e452c9 | 2018-11-02 13:18:44 -0700 | [diff] [blame] | 1399 | update_checkpoint_time_ = curr_time + update_checkpoint_wait_; | 
| Kelvin Zhang | cfe694f | 2020-11-13 13:10:42 -0500 | [diff] [blame] | 1400 | return true; | 
|  | 1401 | } | 
|  | 1402 | return false; | 
|  | 1403 | } | 
|  | 1404 |  | 
|  | 1405 | bool DeltaPerformer::CheckpointUpdateProgress(bool force) { | 
|  | 1406 | if (!force && !ShouldCheckpoint()) { | 
| Colin Howes | 0e452c9 | 2018-11-02 13:18:44 -0700 | [diff] [blame] | 1407 | return false; | 
|  | 1408 | } | 
| Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 1409 | Terminator::set_exit_blocked(true); | 
| Kelvin Zhang | 3265b31 | 2020-12-16 15:51:30 -0500 | [diff] [blame] | 1410 | if (last_updated_operation_num_ != next_operation_num_ || force) { | 
| Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 1411 | // Resets the progress in case we die in the middle of the state update. | 
| Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1412 | ResetUpdateProgress(prefs_, true); | 
| Kelvin Zhang | bb8e999 | 2020-12-28 10:28:48 -0500 | [diff] [blame] | 1413 | if (!signatures_message_data_.empty()) { | 
|  | 1414 | // Save the signature blob because if the update is interrupted after the | 
|  | 1415 | // download phase we don't go through this path anymore. Some alternatives | 
|  | 1416 | // to consider: | 
|  | 1417 | // | 
|  | 1418 | // 1. On resume, re-download the signature blob from the server and | 
|  | 1419 | // re-verify it. | 
|  | 1420 | // | 
|  | 1421 | // 2. Verify the signature as soon as it's received and don't checkpoint | 
|  | 1422 | // the blob and the signed sha-256 context. | 
|  | 1423 | LOG_IF(WARNING, | 
|  | 1424 | !prefs_->SetString(kPrefsUpdateStateSignatureBlob, | 
|  | 1425 | signatures_message_data_)) | 
|  | 1426 | << "Unable to store the signature blob."; | 
|  | 1427 | } | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 1428 | TEST_AND_RETURN_FALSE(prefs_->SetString( | 
|  | 1429 | kPrefsUpdateStateSHA256Context, payload_hash_calculator_.GetContext())); | 
| Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 1430 | TEST_AND_RETURN_FALSE( | 
|  | 1431 | prefs_->SetString(kPrefsUpdateStateSignedSHA256Context, | 
|  | 1432 | signed_hash_calculator_.GetContext())); | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 1433 | TEST_AND_RETURN_FALSE( | 
|  | 1434 | prefs_->SetInt64(kPrefsUpdateStateNextDataOffset, buffer_offset_)); | 
| Kelvin Zhang | cfe694f | 2020-11-13 13:10:42 -0500 | [diff] [blame] | 1435 | last_updated_operation_num_ = next_operation_num_; | 
| David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 1436 |  | 
|  | 1437 | if (next_operation_num_ < num_total_operations_) { | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 1438 | size_t partition_index = current_partition_; | 
| Kelvin Zhang | cfe694f | 2020-11-13 13:10:42 -0500 | [diff] [blame] | 1439 | while (next_operation_num_ >= acc_num_operations_[partition_index]) { | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 1440 | partition_index++; | 
| Kelvin Zhang | cfe694f | 2020-11-13 13:10:42 -0500 | [diff] [blame] | 1441 | } | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 1442 | const size_t partition_operation_num = | 
|  | 1443 | next_operation_num_ - | 
|  | 1444 | (partition_index ? acc_num_operations_[partition_index - 1] : 0); | 
| Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 1445 | const InstallOperation& op = | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 1446 | partitions_[partition_index].operations(partition_operation_num); | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 1447 | TEST_AND_RETURN_FALSE( | 
|  | 1448 | prefs_->SetInt64(kPrefsUpdateStateNextDataLength, op.data_length())); | 
| David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 1449 | } else { | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 1450 | TEST_AND_RETURN_FALSE( | 
|  | 1451 | prefs_->SetInt64(kPrefsUpdateStateNextDataLength, 0)); | 
| David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 1452 | } | 
| Kelvin Zhang | e4235b0 | 2020-11-23 13:57:51 -0500 | [diff] [blame] | 1453 | if (partition_writer_) { | 
|  | 1454 | partition_writer_->CheckpointUpdateProgress(GetPartitionOperationNum()); | 
|  | 1455 | } else { | 
|  | 1456 | CHECK_EQ(next_operation_num_, num_total_operations_) | 
|  | 1457 | << "Partition writer is null, we are expected to finish all " | 
|  | 1458 | "operations: " | 
|  | 1459 | << next_operation_num_ << "/" << num_total_operations_; | 
|  | 1460 | } | 
| Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1461 | } | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 1462 | TEST_AND_RETURN_FALSE( | 
|  | 1463 | prefs_->SetInt64(kPrefsUpdateStateNextOperation, next_operation_num_)); | 
| Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 1464 | return true; | 
|  | 1465 | } | 
|  | 1466 |  | 
| Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1467 | bool DeltaPerformer::PrimeUpdateState() { | 
|  | 1468 | CHECK(manifest_valid_); | 
| Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1469 |  | 
|  | 1470 | int64_t next_operation = kUpdateStateOperationInvalid; | 
|  | 1471 | if (!prefs_->GetInt64(kPrefsUpdateStateNextOperation, &next_operation) || | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 1472 | next_operation == kUpdateStateOperationInvalid || next_operation <= 0) { | 
| Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1473 | // Initiating a new update, no more state needs to be initialized. | 
|  | 1474 | return true; | 
|  | 1475 | } | 
|  | 1476 | next_operation_num_ = next_operation; | 
|  | 1477 |  | 
|  | 1478 | // Resuming an update -- load the rest of the update state. | 
|  | 1479 | int64_t next_data_offset = -1; | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 1480 | TEST_AND_RETURN_FALSE( | 
|  | 1481 | prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset) && | 
|  | 1482 | next_data_offset >= 0); | 
| Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1483 | buffer_offset_ = next_data_offset; | 
|  | 1484 |  | 
| Darin Petkov | 4f0a07b | 2011-05-25 16:47:20 -0700 | [diff] [blame] | 1485 | // The signed hash context and the signature blob may be empty if the | 
|  | 1486 | // interrupted update didn't reach the signature. | 
| Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 1487 | string signed_hash_context; | 
|  | 1488 | if (prefs_->GetString(kPrefsUpdateStateSignedSHA256Context, | 
|  | 1489 | &signed_hash_context)) { | 
|  | 1490 | TEST_AND_RETURN_FALSE( | 
|  | 1491 | signed_hash_calculator_.SetContext(signed_hash_context)); | 
|  | 1492 | } | 
|  | 1493 |  | 
| Sen Jiang | 9b2f178 | 2019-01-24 14:27:50 -0800 | [diff] [blame] | 1494 | prefs_->GetString(kPrefsUpdateStateSignatureBlob, &signatures_message_data_); | 
| Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1495 |  | 
|  | 1496 | string hash_context; | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 1497 | TEST_AND_RETURN_FALSE( | 
|  | 1498 | prefs_->GetString(kPrefsUpdateStateSHA256Context, &hash_context) && | 
|  | 1499 | payload_hash_calculator_.SetContext(hash_context)); | 
| Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1500 |  | 
|  | 1501 | int64_t manifest_metadata_size = 0; | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 1502 | TEST_AND_RETURN_FALSE( | 
|  | 1503 | prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size) && | 
|  | 1504 | manifest_metadata_size > 0); | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 1505 | metadata_size_ = manifest_metadata_size; | 
| Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1506 |  | 
| Alex Deymo | f25eb49 | 2016-02-26 00:20:08 -0800 | [diff] [blame] | 1507 | int64_t manifest_signature_size = 0; | 
|  | 1508 | TEST_AND_RETURN_FALSE( | 
|  | 1509 | prefs_->GetInt64(kPrefsManifestSignatureSize, &manifest_signature_size) && | 
|  | 1510 | manifest_signature_size >= 0); | 
|  | 1511 | metadata_signature_size_ = manifest_signature_size; | 
|  | 1512 |  | 
| Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 1513 | // Advance the download progress to reflect what doesn't need to be | 
|  | 1514 | // re-downloaded. | 
|  | 1515 | total_bytes_received_ += buffer_offset_; | 
|  | 1516 |  | 
| Darin Petkov | 6142614 | 2010-10-08 11:04:55 -0700 | [diff] [blame] | 1517 | // Speculatively count the resume as a failure. | 
|  | 1518 | int64_t resumed_update_failures; | 
|  | 1519 | if (prefs_->GetInt64(kPrefsResumedUpdateFailures, &resumed_update_failures)) { | 
|  | 1520 | resumed_update_failures++; | 
|  | 1521 | } else { | 
|  | 1522 | resumed_update_failures = 1; | 
|  | 1523 | } | 
|  | 1524 | prefs_->SetInt64(kPrefsResumedUpdateFailures, resumed_update_failures); | 
| Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1525 | return true; | 
|  | 1526 | } | 
|  | 1527 |  | 
| Kelvin Zhang | ebd115e | 2021-03-08 16:10:25 -0500 | [diff] [blame] | 1528 | bool DeltaPerformer::IsDynamicPartition(const std::string& part_name, | 
|  | 1529 | uint32_t slot) { | 
| Tianjie | 3a55fc2 | 2021-02-13 16:02:22 -0800 | [diff] [blame] | 1530 | return boot_control_->GetDynamicPartitionControl()->IsDynamicPartition( | 
| Kelvin Zhang | ebd115e | 2021-03-08 16:10:25 -0500 | [diff] [blame] | 1531 | part_name, slot); | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 1532 | } | 
|  | 1533 |  | 
| Kelvin Zhang | cfe694f | 2020-11-13 13:10:42 -0500 | [diff] [blame] | 1534 | std::unique_ptr<PartitionWriter> DeltaPerformer::CreatePartitionWriter( | 
|  | 1535 | const PartitionUpdate& partition_update, | 
|  | 1536 | const InstallPlan::Partition& install_part, | 
|  | 1537 | DynamicPartitionControlInterface* dynamic_control, | 
|  | 1538 | size_t block_size, | 
|  | 1539 | bool is_interactive, | 
|  | 1540 | bool is_dynamic_partition) { | 
|  | 1541 | return partition_writer::CreatePartitionWriter( | 
|  | 1542 | partition_update, | 
|  | 1543 | install_part, | 
|  | 1544 | dynamic_control, | 
|  | 1545 | block_size_, | 
|  | 1546 | interactive_, | 
| Kelvin Zhang | ebd115e | 2021-03-08 16:10:25 -0500 | [diff] [blame] | 1547 | IsDynamicPartition(install_part.name, install_plan_->target_slot)); | 
| Kelvin Zhang | cfe694f | 2020-11-13 13:10:42 -0500 | [diff] [blame] | 1548 | } | 
|  | 1549 |  | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 1550 | }  // namespace chromeos_update_engine |