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