Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2012 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 16 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/payload_consumer/delta_performer.h" |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 18 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 19 | #include <errno.h> |
Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 20 | #include <linux/fs.h> |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 21 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 22 | #include <algorithm> |
| 23 | #include <cstring> |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 24 | #include <map> |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 25 | #include <memory> |
Tianjie | d3865d1 | 2020-06-03 15:25:17 -0700 | [diff] [blame] | 26 | #include <set> |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 27 | #include <string> |
Amin Hassani | d87304c | 2017-08-29 11:40:03 -0700 | [diff] [blame] | 28 | #include <utility> |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 29 | #include <vector> |
| 30 | |
Ben Chan | 06c76a4 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 31 | #include <base/files/file_util.h> |
Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 32 | #include <base/format_macros.h> |
Lann Martin | 39f5714 | 2017-07-14 09:18:42 -0600 | [diff] [blame] | 33 | #include <base/metrics/histogram_macros.h> |
Alex Deymo | 6714084 | 2016-06-15 13:28:59 -0700 | [diff] [blame] | 34 | #include <base/strings/string_number_conversions.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 35 | #include <base/strings/string_util.h> |
| 36 | #include <base/strings/stringprintf.h> |
Lann Martin | 39f5714 | 2017-07-14 09:18:42 -0600 | [diff] [blame] | 37 | #include <base/time/time.h> |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 38 | #include <brillo/data_encoding.h> |
Alex Deymo | 6765a68 | 2017-05-19 13:13:54 -0700 | [diff] [blame] | 39 | #include <bsdiff/bspatch.h> |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 40 | #include <google/protobuf/repeated_field.h> |
Amin Hassani | 02855c2 | 2017-09-06 22:34:50 -0700 | [diff] [blame] | 41 | #include <puffin/puffpatch.h> |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 42 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 43 | #include "update_engine/common/constants.h" |
| 44 | #include "update_engine/common/hardware_interface.h" |
Alex Deymo | 542c19b | 2015-12-03 07:43:31 -0300 | [diff] [blame] | 45 | #include "update_engine/common/prefs_interface.h" |
| 46 | #include "update_engine/common/subprocess.h" |
| 47 | #include "update_engine/common/terminator.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 48 | #include "update_engine/payload_consumer/bzip_extent_writer.h" |
Amin Hassani | a3fc20a | 2017-09-19 17:19:34 -0700 | [diff] [blame] | 49 | #include "update_engine/payload_consumer/cached_file_descriptor.h" |
Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 50 | #include "update_engine/payload_consumer/certificate_parser_interface.h" |
Alex Deymo | 542c19b | 2015-12-03 07:43:31 -0300 | [diff] [blame] | 51 | #include "update_engine/payload_consumer/download_action.h" |
Amin Hassani | db56be9 | 2017-09-06 12:41:23 -0700 | [diff] [blame] | 52 | #include "update_engine/payload_consumer/extent_reader.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 53 | #include "update_engine/payload_consumer/extent_writer.h" |
Tianjie | d3865d1 | 2020-06-03 15:25:17 -0700 | [diff] [blame] | 54 | #include "update_engine/payload_consumer/partition_update_generator_interface.h" |
Alex Deymo | 51c264e | 2016-11-04 15:49:53 -0700 | [diff] [blame] | 55 | #if USE_FEC |
| 56 | #include "update_engine/payload_consumer/fec_file_descriptor.h" |
| 57 | #endif // USE_FEC |
Alex Deymo | a48f630 | 2016-11-04 15:49:53 -0700 | [diff] [blame] | 58 | #include "update_engine/payload_consumer/file_descriptor_utils.h" |
HÃ¥kan Kvist | 4e13cf4 | 2018-01-23 12:57:55 +0100 | [diff] [blame] | 59 | #include "update_engine/payload_consumer/mount_history.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 60 | #include "update_engine/payload_consumer/payload_constants.h" |
| 61 | #include "update_engine/payload_consumer/payload_verifier.h" |
| 62 | #include "update_engine/payload_consumer/xz_extent_writer.h" |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 63 | |
Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 64 | using google::protobuf::RepeatedPtrField; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 65 | using std::min; |
| 66 | using std::string; |
| 67 | using std::vector; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 68 | |
| 69 | namespace chromeos_update_engine { |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 70 | const unsigned DeltaPerformer::kProgressLogMaxChunks = 10; |
| 71 | const unsigned DeltaPerformer::kProgressLogTimeoutSeconds = 30; |
| 72 | const unsigned DeltaPerformer::kProgressDownloadWeight = 50; |
| 73 | const unsigned DeltaPerformer::kProgressOperationsWeight = 50; |
Colin Howes | 0e452c9 | 2018-11-02 13:18:44 -0700 | [diff] [blame] | 74 | const uint64_t DeltaPerformer::kCheckpointFrequencySeconds = 1; |
Darin Petkov | abc7bc0 | 2011-02-23 14:39:43 -0800 | [diff] [blame] | 75 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 76 | namespace { |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 77 | const int kUpdateStateOperationInvalid = -1; |
Darin Petkov | 6142614 | 2010-10-08 11:04:55 -0700 | [diff] [blame] | 78 | const int kMaxResumedUpdateFailures = 10; |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 79 | |
Amin Hassani | a3fc20a | 2017-09-19 17:19:34 -0700 | [diff] [blame] | 80 | const uint64_t kCacheSize = 1024 * 1024; // 1MB |
| 81 | |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 82 | // Opens path for read/write. On success returns an open FileDescriptor |
| 83 | // and sets *err to 0. On failure, sets *err to errno and returns nullptr. |
Amin Hassani | a3fc20a | 2017-09-19 17:19:34 -0700 | [diff] [blame] | 84 | FileDescriptorPtr OpenFile(const char* path, |
| 85 | int mode, |
| 86 | bool cache_writes, |
| 87 | int* err) { |
Alex Deymo | 5fb356c | 2016-03-25 18:48:22 -0700 | [diff] [blame] | 88 | // Try to mark the block device read-only based on the mode. Ignore any |
| 89 | // failure since this won't work when passing regular files. |
Amin Hassani | a3fc20a | 2017-09-19 17:19:34 -0700 | [diff] [blame] | 90 | bool read_only = (mode & O_ACCMODE) == O_RDONLY; |
| 91 | utils::SetBlockDeviceReadOnly(path, read_only); |
Alex Deymo | 5fb356c | 2016-03-25 18:48:22 -0700 | [diff] [blame] | 92 | |
Brian Norris | 7b428f5 | 2019-06-26 10:03:35 -0700 | [diff] [blame] | 93 | FileDescriptorPtr fd(new EintrSafeFileDescriptor()); |
Amin Hassani | a3fc20a | 2017-09-19 17:19:34 -0700 | [diff] [blame] | 94 | if (cache_writes && !read_only) { |
| 95 | fd = FileDescriptorPtr(new CachedFileDescriptor(fd, kCacheSize)); |
| 96 | LOG(INFO) << "Caching writes."; |
| 97 | } |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 98 | if (!fd->Open(path, mode, 000)) { |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 99 | *err = errno; |
| 100 | PLOG(ERROR) << "Unable to open file " << path; |
| 101 | return nullptr; |
| 102 | } |
| 103 | *err = 0; |
| 104 | return fd; |
| 105 | } |
Alex Deymo | b86787c | 2016-05-12 18:46:25 -0700 | [diff] [blame] | 106 | |
| 107 | // Discard the tail of the block device referenced by |fd|, from the offset |
| 108 | // |data_size| until the end of the block device. Returns whether the data was |
| 109 | // discarded. |
Chih-Hung Hsieh | 5c6bb1d | 2016-07-27 13:33:15 -0700 | [diff] [blame] | 110 | bool DiscardPartitionTail(const FileDescriptorPtr& fd, uint64_t data_size) { |
Alex Deymo | b86787c | 2016-05-12 18:46:25 -0700 | [diff] [blame] | 111 | uint64_t part_size = fd->BlockDevSize(); |
| 112 | if (!part_size || part_size <= data_size) |
| 113 | return false; |
| 114 | |
Alex Deymo | 72a68d8 | 2016-06-15 16:31:04 -0700 | [diff] [blame] | 115 | struct blkioctl_request { |
| 116 | int number; |
| 117 | const char* name; |
| 118 | }; |
| 119 | const vector<blkioctl_request> blkioctl_requests = { |
Alex Deymo | 72a68d8 | 2016-06-15 16:31:04 -0700 | [diff] [blame] | 120 | {BLKDISCARD, "BLKDISCARD"}, |
Amin Hassani | 4155520 | 2017-06-23 12:42:00 -0700 | [diff] [blame] | 121 | {BLKSECDISCARD, "BLKSECDISCARD"}, |
Alex Deymo | b86787c | 2016-05-12 18:46:25 -0700 | [diff] [blame] | 122 | #ifdef BLKZEROOUT |
Alex Deymo | 72a68d8 | 2016-06-15 16:31:04 -0700 | [diff] [blame] | 123 | {BLKZEROOUT, "BLKZEROOUT"}, |
Alex Deymo | b86787c | 2016-05-12 18:46:25 -0700 | [diff] [blame] | 124 | #endif |
| 125 | }; |
Alex Deymo | 72a68d8 | 2016-06-15 16:31:04 -0700 | [diff] [blame] | 126 | for (const auto& req : blkioctl_requests) { |
Alex Deymo | b86787c | 2016-05-12 18:46:25 -0700 | [diff] [blame] | 127 | int error = 0; |
Alex Deymo | 72a68d8 | 2016-06-15 16:31:04 -0700 | [diff] [blame] | 128 | if (fd->BlkIoctl(req.number, data_size, part_size - data_size, &error) && |
Alex Deymo | b86787c | 2016-05-12 18:46:25 -0700 | [diff] [blame] | 129 | error == 0) { |
| 130 | return true; |
| 131 | } |
| 132 | LOG(WARNING) << "Error discarding the last " |
| 133 | << (part_size - data_size) / 1024 << " KiB using ioctl(" |
Alex Deymo | 72a68d8 | 2016-06-15 16:31:04 -0700 | [diff] [blame] | 134 | << req.name << ")"; |
Alex Deymo | b86787c | 2016-05-12 18:46:25 -0700 | [diff] [blame] | 135 | } |
| 136 | return false; |
| 137 | } |
| 138 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 139 | } // namespace |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 140 | |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 141 | // Computes the ratio of |part| and |total|, scaled to |norm|, using integer |
| 142 | // arithmetic. |
| 143 | static uint64_t IntRatio(uint64_t part, uint64_t total, uint64_t norm) { |
| 144 | return part * norm / total; |
| 145 | } |
| 146 | |
| 147 | void DeltaPerformer::LogProgress(const char* message_prefix) { |
| 148 | // Format operations total count and percentage. |
| 149 | string total_operations_str("?"); |
| 150 | string completed_percentage_str(""); |
| 151 | if (num_total_operations_) { |
Alex Deymo | c00c98a | 2015-03-17 17:38:00 -0700 | [diff] [blame] | 152 | total_operations_str = std::to_string(num_total_operations_); |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 153 | // 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] | 154 | completed_percentage_str = base::StringPrintf( |
| 155 | " (%" PRIu64 "%%)", |
| 156 | IntRatio(next_operation_num_, num_total_operations_, 100)); |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // Format download total count and percentage. |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 160 | size_t payload_size = payload_->size; |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 161 | string payload_size_str("?"); |
| 162 | string downloaded_percentage_str(""); |
| 163 | if (payload_size) { |
Alex Deymo | c00c98a | 2015-03-17 17:38:00 -0700 | [diff] [blame] | 164 | payload_size_str = std::to_string(payload_size); |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 165 | // 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] | 166 | downloaded_percentage_str = base::StringPrintf( |
| 167 | " (%" PRIu64 "%%)", IntRatio(total_bytes_received_, payload_size, 100)); |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | LOG(INFO) << (message_prefix ? message_prefix : "") << next_operation_num_ |
| 171 | << "/" << total_operations_str << " operations" |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 172 | << completed_percentage_str << ", " << total_bytes_received_ << "/" |
| 173 | << payload_size_str << " bytes downloaded" |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 174 | << downloaded_percentage_str << ", overall progress " |
| 175 | << overall_progress_ << "%"; |
| 176 | } |
| 177 | |
| 178 | void DeltaPerformer::UpdateOverallProgress(bool force_log, |
| 179 | const char* message_prefix) { |
| 180 | // Compute our download and overall progress. |
| 181 | unsigned new_overall_progress = 0; |
Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 182 | static_assert(kProgressDownloadWeight + kProgressOperationsWeight == 100, |
| 183 | "Progress weights don't add up"); |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 184 | // Only consider download progress if its total size is known; otherwise |
| 185 | // adjust the operations weight to compensate for the absence of download |
| 186 | // progress. Also, make sure to cap the download portion at |
| 187 | // kProgressDownloadWeight, in case we end up downloading more than we |
| 188 | // initially expected (this indicates a problem, but could generally happen). |
| 189 | // TODO(garnold) the correction of operations weight when we do not have the |
| 190 | // total payload size, as well as the conditional guard below, should both be |
| 191 | // eliminated once we ensure that the payload_size in the install plan is |
| 192 | // always given and is non-zero. This currently isn't the case during unit |
| 193 | // tests (see chromium-os:37969). |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 194 | size_t payload_size = payload_->size; |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 195 | unsigned actual_operations_weight = kProgressOperationsWeight; |
| 196 | if (payload_size) |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 197 | new_overall_progress += |
| 198 | min(static_cast<unsigned>(IntRatio( |
| 199 | total_bytes_received_, payload_size, kProgressDownloadWeight)), |
| 200 | kProgressDownloadWeight); |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 201 | else |
| 202 | actual_operations_weight += kProgressDownloadWeight; |
| 203 | |
| 204 | // Only add completed operations if their total number is known; we definitely |
| 205 | // expect an update to have at least one operation, so the expectation is that |
| 206 | // this will eventually reach |actual_operations_weight|. |
| 207 | if (num_total_operations_) |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 208 | new_overall_progress += IntRatio( |
| 209 | next_operation_num_, num_total_operations_, actual_operations_weight); |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 210 | |
| 211 | // Progress ratio cannot recede, unless our assumptions about the total |
| 212 | // payload size, total number of operations, or the monotonicity of progress |
| 213 | // is breached. |
| 214 | if (new_overall_progress < overall_progress_) { |
| 215 | LOG(WARNING) << "progress counter receded from " << overall_progress_ |
| 216 | << "% down to " << new_overall_progress << "%; this is a bug"; |
| 217 | force_log = true; |
| 218 | } |
| 219 | overall_progress_ = new_overall_progress; |
| 220 | |
| 221 | // Update chunk index, log as needed: if forced by called, or we completed a |
| 222 | // progress chunk, or a timeout has expired. |
Sen Jiang | 53c2ec5 | 2019-01-10 15:27:59 -0800 | [diff] [blame] | 223 | base::TimeTicks curr_time = base::TimeTicks::Now(); |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 224 | unsigned curr_progress_chunk = |
| 225 | overall_progress_ * kProgressLogMaxChunks / 100; |
| 226 | if (force_log || curr_progress_chunk > last_progress_chunk_ || |
| 227 | curr_time > forced_progress_log_time_) { |
| 228 | forced_progress_log_time_ = curr_time + forced_progress_log_wait_; |
| 229 | LogProgress(message_prefix); |
| 230 | } |
| 231 | last_progress_chunk_ = curr_progress_chunk; |
| 232 | } |
| 233 | |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 234 | size_t DeltaPerformer::CopyDataToBuffer(const char** bytes_p, |
| 235 | size_t* count_p, |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 236 | size_t max) { |
| 237 | const size_t count = *count_p; |
| 238 | if (!count) |
| 239 | return 0; // Special case shortcut. |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 240 | size_t read_len = min(count, max - buffer_.size()); |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 241 | const char* bytes_start = *bytes_p; |
| 242 | const char* bytes_end = bytes_start + read_len; |
Sen Jiang | be19a24 | 2017-11-17 11:48:17 -0800 | [diff] [blame] | 243 | buffer_.reserve(max); |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 244 | buffer_.insert(buffer_.end(), bytes_start, bytes_end); |
| 245 | *bytes_p = bytes_end; |
| 246 | *count_p = count - read_len; |
| 247 | return read_len; |
| 248 | } |
| 249 | |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 250 | bool DeltaPerformer::HandleOpResult(bool op_result, |
| 251 | const char* op_type_name, |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 252 | ErrorCode* error) { |
| 253 | if (op_result) |
| 254 | return true; |
| 255 | |
Alex Deymo | 3d00906 | 2016-05-13 15:51:25 -0700 | [diff] [blame] | 256 | size_t partition_first_op_num = |
| 257 | current_partition_ ? acc_num_operations_[current_partition_ - 1] : 0; |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 258 | LOG(ERROR) << "Failed to perform " << op_type_name << " operation " |
Alex Deymo | 3d00906 | 2016-05-13 15:51:25 -0700 | [diff] [blame] | 259 | << next_operation_num_ << ", which is the operation " |
| 260 | << next_operation_num_ - partition_first_op_num |
| 261 | << " in partition \"" |
| 262 | << partitions_[current_partition_].partition_name() << "\""; |
Sen Jiang | be2c47b | 2016-06-15 14:09:27 -0700 | [diff] [blame] | 263 | if (*error == ErrorCode::kSuccess) |
| 264 | *error = ErrorCode::kDownloadOperationExecutionError; |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 265 | return false; |
| 266 | } |
| 267 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 268 | int DeltaPerformer::Close() { |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 269 | int err = -CloseCurrentPartition(); |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 270 | LOG_IF(ERROR, |
| 271 | !payload_hash_calculator_.Finalize() || |
| 272 | !signed_hash_calculator_.Finalize()) |
Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 273 | << "Unable to finalize the hash."; |
Darin Petkov | 934bb41 | 2010-11-18 11:21:35 -0800 | [diff] [blame] | 274 | if (!buffer_.empty()) { |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 275 | LOG(INFO) << "Discarding " << buffer_.size() << " unused downloaded bytes"; |
| 276 | if (err >= 0) |
Darin Petkov | 934bb41 | 2010-11-18 11:21:35 -0800 | [diff] [blame] | 277 | err = 1; |
Darin Petkov | 934bb41 | 2010-11-18 11:21:35 -0800 | [diff] [blame] | 278 | } |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 279 | return -err; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 282 | int DeltaPerformer::CloseCurrentPartition() { |
| 283 | int err = 0; |
| 284 | if (source_fd_ && !source_fd_->Close()) { |
| 285 | err = errno; |
| 286 | PLOG(ERROR) << "Error closing source partition"; |
| 287 | if (!err) |
| 288 | err = 1; |
| 289 | } |
| 290 | source_fd_.reset(); |
Alex Deymo | 51c264e | 2016-11-04 15:49:53 -0700 | [diff] [blame] | 291 | if (source_ecc_fd_ && !source_ecc_fd_->Close()) { |
| 292 | err = errno; |
| 293 | PLOG(ERROR) << "Error closing ECC source partition"; |
| 294 | if (!err) |
| 295 | err = 1; |
| 296 | } |
| 297 | source_ecc_fd_.reset(); |
| 298 | source_ecc_open_failure_ = false; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 299 | source_path_.clear(); |
| 300 | |
| 301 | if (target_fd_ && !target_fd_->Close()) { |
| 302 | err = errno; |
| 303 | PLOG(ERROR) << "Error closing target partition"; |
| 304 | if (!err) |
| 305 | err = 1; |
| 306 | } |
| 307 | target_fd_.reset(); |
| 308 | target_path_.clear(); |
| 309 | return -err; |
| 310 | } |
| 311 | |
| 312 | bool DeltaPerformer::OpenCurrentPartition() { |
| 313 | if (current_partition_ >= partitions_.size()) |
| 314 | return false; |
| 315 | |
| 316 | const PartitionUpdate& partition = partitions_[current_partition_]; |
Sen Jiang | d2ff2a0 | 2017-04-06 14:53:31 -0700 | [diff] [blame] | 317 | size_t num_previous_partitions = |
| 318 | install_plan_->partitions.size() - partitions_.size(); |
| 319 | const InstallPlan::Partition& install_part = |
| 320 | install_plan_->partitions[num_previous_partitions + current_partition_]; |
Tianjie | 55abd3c | 2020-06-19 00:22:59 -0700 | [diff] [blame] | 321 | // Open source fds if we have a delta payload, or for partitions in the |
| 322 | // partial update. |
Tianjie | d3865d1 | 2020-06-03 15:25:17 -0700 | [diff] [blame] | 323 | bool source_may_exist = manifest_.partial_update() || |
Tianjie | 55abd3c | 2020-06-19 00:22:59 -0700 | [diff] [blame] | 324 | payload_->type == InstallPayloadType::kDelta; |
Tianjie | d3865d1 | 2020-06-03 15:25:17 -0700 | [diff] [blame] | 325 | // We shouldn't open the source partition in certain cases, e.g. some dynamic |
| 326 | // partitions in delta payload, partitions included in the full payload for |
| 327 | // partial updates. Use the source size as the indicator. |
| 328 | if (source_may_exist && install_part.source_size > 0) { |
Sen Jiang | d2ff2a0 | 2017-04-06 14:53:31 -0700 | [diff] [blame] | 329 | source_path_ = install_part.source_path; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 330 | int err; |
Amin Hassani | a3fc20a | 2017-09-19 17:19:34 -0700 | [diff] [blame] | 331 | source_fd_ = OpenFile(source_path_.c_str(), O_RDONLY, false, &err); |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 332 | if (!source_fd_) { |
| 333 | LOG(ERROR) << "Unable to open source partition " |
| 334 | << partition.partition_name() << " on slot " |
| 335 | << BootControlInterface::SlotName(install_plan_->source_slot) |
| 336 | << ", file " << source_path_; |
| 337 | return false; |
| 338 | } |
| 339 | } |
| 340 | |
Sen Jiang | d2ff2a0 | 2017-04-06 14:53:31 -0700 | [diff] [blame] | 341 | target_path_ = install_part.target_path; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 342 | int err; |
Grant Grundler | a763c2d | 2017-05-10 11:12:52 -0700 | [diff] [blame] | 343 | |
Amin Hassani | 7ecda26 | 2017-07-11 17:10:50 -0700 | [diff] [blame] | 344 | int flags = O_RDWR; |
Amin Hassani | ed37d68 | 2018-04-06 13:22:00 -0700 | [diff] [blame] | 345 | if (!interactive_) |
Amin Hassani | 7ecda26 | 2017-07-11 17:10:50 -0700 | [diff] [blame] | 346 | flags |= O_DSYNC; |
| 347 | |
| 348 | LOG(INFO) << "Opening " << target_path_ << " partition with" |
Amin Hassani | ed37d68 | 2018-04-06 13:22:00 -0700 | [diff] [blame] | 349 | << (interactive_ ? "out" : "") << " O_DSYNC"; |
Amin Hassani | 7ecda26 | 2017-07-11 17:10:50 -0700 | [diff] [blame] | 350 | |
Amin Hassani | a3fc20a | 2017-09-19 17:19:34 -0700 | [diff] [blame] | 351 | target_fd_ = OpenFile(target_path_.c_str(), flags, true, &err); |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 352 | if (!target_fd_) { |
| 353 | LOG(ERROR) << "Unable to open target partition " |
| 354 | << partition.partition_name() << " on slot " |
| 355 | << BootControlInterface::SlotName(install_plan_->target_slot) |
| 356 | << ", file " << target_path_; |
| 357 | return false; |
| 358 | } |
Alex Deymo | b86787c | 2016-05-12 18:46:25 -0700 | [diff] [blame] | 359 | |
| 360 | LOG(INFO) << "Applying " << partition.operations().size() |
| 361 | << " operations to partition \"" << partition.partition_name() |
| 362 | << "\""; |
| 363 | |
| 364 | // Discard the end of the partition, but ignore failures. |
Sen Jiang | d2ff2a0 | 2017-04-06 14:53:31 -0700 | [diff] [blame] | 365 | DiscardPartitionTail(target_fd_, install_part.target_size); |
Alex Deymo | b86787c | 2016-05-12 18:46:25 -0700 | [diff] [blame] | 366 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 367 | return true; |
| 368 | } |
| 369 | |
Alex Deymo | 51c264e | 2016-11-04 15:49:53 -0700 | [diff] [blame] | 370 | bool DeltaPerformer::OpenCurrentECCPartition() { |
| 371 | if (source_ecc_fd_) |
| 372 | return true; |
| 373 | |
| 374 | if (source_ecc_open_failure_) |
| 375 | return false; |
| 376 | |
| 377 | if (current_partition_ >= partitions_.size()) |
| 378 | return false; |
| 379 | |
Amin Hassani | 0f59a9a | 2019-09-27 10:24:31 -0700 | [diff] [blame] | 380 | // No support for ECC for full payloads. |
| 381 | if (payload_->type == InstallPayloadType::kFull) |
Alex Deymo | 51c264e | 2016-11-04 15:49:53 -0700 | [diff] [blame] | 382 | return false; |
| 383 | |
| 384 | #if USE_FEC |
| 385 | const PartitionUpdate& partition = partitions_[current_partition_]; |
| 386 | size_t num_previous_partitions = |
| 387 | install_plan_->partitions.size() - partitions_.size(); |
| 388 | const InstallPlan::Partition& install_part = |
| 389 | install_plan_->partitions[num_previous_partitions + current_partition_]; |
| 390 | string path = install_part.source_path; |
| 391 | FileDescriptorPtr fd(new FecFileDescriptor()); |
| 392 | if (!fd->Open(path.c_str(), O_RDONLY, 0)) { |
| 393 | PLOG(ERROR) << "Unable to open ECC source partition " |
| 394 | << partition.partition_name() << " on slot " |
| 395 | << BootControlInterface::SlotName(install_plan_->source_slot) |
| 396 | << ", file " << path; |
| 397 | source_ecc_open_failure_ = true; |
| 398 | return false; |
| 399 | } |
| 400 | source_ecc_fd_ = fd; |
| 401 | #else |
| 402 | // No support for ECC compiled. |
| 403 | source_ecc_open_failure_ = true; |
| 404 | #endif // USE_FEC |
| 405 | |
| 406 | return !source_ecc_open_failure_; |
| 407 | } |
| 408 | |
Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame] | 409 | namespace { |
| 410 | |
| 411 | void LogPartitionInfoHash(const PartitionInfo& info, const string& tag) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 412 | string sha256 = brillo::data_encoding::Base64Encode(info.hash()); |
Alex Vakulenko | 981a9fb | 2015-02-09 12:51:24 -0800 | [diff] [blame] | 413 | LOG(INFO) << "PartitionInfo " << tag << " sha256: " << sha256 |
| 414 | << " size: " << info.size(); |
Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 417 | void LogPartitionInfo(const vector<PartitionUpdate>& partitions) { |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 418 | for (const PartitionUpdate& partition : partitions) { |
Sen Jiang | 7b9a587 | 2018-01-17 13:25:06 -0800 | [diff] [blame] | 419 | if (partition.has_old_partition_info()) { |
| 420 | LogPartitionInfoHash(partition.old_partition_info(), |
| 421 | "old " + partition.partition_name()); |
| 422 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 423 | LogPartitionInfoHash(partition.new_partition_info(), |
| 424 | "new " + partition.partition_name()); |
| 425 | } |
Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 428 | } // namespace |
Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame] | 429 | |
Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 430 | uint32_t DeltaPerformer::GetMinorVersion() const { |
| 431 | if (manifest_.has_minor_version()) { |
| 432 | return manifest_.minor_version(); |
Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 433 | } |
Sen Jiang | f123663 | 2018-05-11 16:03:23 -0700 | [diff] [blame] | 434 | return payload_->type == InstallPayloadType::kDelta |
| 435 | ? kMaxSupportedMinorPayloadVersion |
| 436 | : kFullPayloadMinorVersion; |
Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 437 | } |
| 438 | |
Sen Jiang | b8060e4 | 2015-09-24 17:30:50 -0700 | [diff] [blame] | 439 | bool DeltaPerformer::IsHeaderParsed() const { |
| 440 | return metadata_size_ != 0; |
| 441 | } |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 442 | |
Sen Jiang | 9c89e84 | 2018-02-02 13:51:21 -0800 | [diff] [blame] | 443 | MetadataParseResult DeltaPerformer::ParsePayloadMetadata( |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 444 | const brillo::Blob& payload, ErrorCode* error) { |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 445 | *error = ErrorCode::kSuccess; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 446 | |
Sen Jiang | b8060e4 | 2015-09-24 17:30:50 -0700 | [diff] [blame] | 447 | if (!IsHeaderParsed()) { |
Sen Jiang | f123663 | 2018-05-11 16:03:23 -0700 | [diff] [blame] | 448 | MetadataParseResult result = |
| 449 | payload_metadata_.ParsePayloadHeader(payload, error); |
Sen Jiang | 9c89e84 | 2018-02-02 13:51:21 -0800 | [diff] [blame] | 450 | if (result != MetadataParseResult::kSuccess) |
| 451 | return result; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 452 | |
Sen Jiang | 9c89e84 | 2018-02-02 13:51:21 -0800 | [diff] [blame] | 453 | metadata_size_ = payload_metadata_.GetMetadataSize(); |
| 454 | metadata_signature_size_ = payload_metadata_.GetMetadataSignatureSize(); |
| 455 | major_payload_version_ = payload_metadata_.GetMajorVersion(); |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 456 | |
| 457 | // If the metadata size is present in install plan, check for it immediately |
| 458 | // even before waiting for that many number of bytes to be downloaded in the |
| 459 | // payload. This will prevent any attack which relies on us downloading data |
| 460 | // beyond the expected metadata size. |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 461 | if (install_plan_->hash_checks_mandatory) { |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 462 | if (payload_->metadata_size != metadata_size_) { |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 463 | LOG(ERROR) << "Mandatory metadata size in Omaha response (" |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 464 | << payload_->metadata_size |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 465 | << ") is missing/incorrect, actual = " << metadata_size_; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 466 | *error = ErrorCode::kDownloadInvalidMetadataSize; |
Sen Jiang | 9c89e84 | 2018-02-02 13:51:21 -0800 | [diff] [blame] | 467 | return MetadataParseResult::kError; |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 468 | } |
| 469 | } |
Andrew | 3a7dc26 | 2019-12-19 11:38:08 -0800 | [diff] [blame] | 470 | |
| 471 | // Check that the |metadata signature size_| and |metadata_size_| are not |
| 472 | // very big numbers. This is necessary since |update_engine| needs to write |
| 473 | // these values into the buffer before being able to use them, and if an |
| 474 | // attacker sets these values to a very big number, the buffer will overflow |
| 475 | // and |update_engine| will crash. A simple way of solving this is to check |
| 476 | // that the size of both values is smaller than the payload itself. |
| 477 | if (metadata_size_ + metadata_signature_size_ > payload_->size) { |
| 478 | LOG(ERROR) << "The size of the metadata_size(" << metadata_size_ << ")" |
| 479 | << " or metadata signature(" << metadata_signature_size_ << ")" |
| 480 | << " is greater than the size of the payload" |
| 481 | << "(" << payload_->size << ")"; |
| 482 | *error = ErrorCode::kDownloadInvalidMetadataSize; |
| 483 | return MetadataParseResult::kError; |
| 484 | } |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | // 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] | 488 | // metadata and its signature (if exist) to be read in before we can parse it. |
| 489 | if (payload.size() < metadata_size_ + metadata_signature_size_) |
Sen Jiang | 9c89e84 | 2018-02-02 13:51:21 -0800 | [diff] [blame] | 490 | return MetadataParseResult::kInsufficientData; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 491 | |
| 492 | // 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] | 493 | // 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] | 494 | // that we just log once (instead of logging n times) if it takes n |
| 495 | // DeltaPerformer::Write calls to download the full manifest. |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 496 | if (payload_->metadata_size == metadata_size_) { |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 497 | LOG(INFO) << "Manifest size in payload matches expected value from Omaha"; |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 498 | } else { |
| 499 | // For mandatory-cases, we'd have already returned a kMetadataParseError |
| 500 | // above. We'll be here only for non-mandatory cases. Just send a UMA stat. |
| 501 | LOG(WARNING) << "Ignoring missing/incorrect metadata size (" |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 502 | << payload_->metadata_size |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 503 | << ") in Omaha response as validation is not mandatory. " |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 504 | << "Trusting metadata size in payload = " << metadata_size_; |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 505 | } |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 506 | |
Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 507 | auto [payload_verifier, perform_verification] = CreatePayloadVerifier(); |
| 508 | if (!payload_verifier) { |
| 509 | LOG(ERROR) << "Failed to create payload verifier."; |
Sen Jiang | 08c6da1 | 2019-01-07 18:28:56 -0800 | [diff] [blame] | 510 | *error = ErrorCode::kDownloadMetadataSignatureVerificationError; |
Tianjie Xu | 6c70b93 | 2019-10-22 16:46:00 -0700 | [diff] [blame] | 511 | if (perform_verification) { |
| 512 | return MetadataParseResult::kError; |
| 513 | } |
| 514 | } else { |
| 515 | // We have the full metadata in |payload|. Verify its integrity |
| 516 | // and authenticity based on the information we have in Omaha response. |
| 517 | *error = payload_metadata_.ValidateMetadataSignature( |
| 518 | payload, payload_->metadata_signature, *payload_verifier); |
Sen Jiang | 08c6da1 | 2019-01-07 18:28:56 -0800 | [diff] [blame] | 519 | } |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 520 | if (*error != ErrorCode::kSuccess) { |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 521 | if (install_plan_->hash_checks_mandatory) { |
David Zeuthen | bc27aac | 2013-11-26 11:17:48 -0800 | [diff] [blame] | 522 | // The autoupdate_CatchBadSignatures test checks for this string |
| 523 | // in log-files. Keep in sync. |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 524 | LOG(ERROR) << "Mandatory metadata signature validation failed"; |
Sen Jiang | 9c89e84 | 2018-02-02 13:51:21 -0800 | [diff] [blame] | 525 | return MetadataParseResult::kError; |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 526 | } |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 527 | |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 528 | // For non-mandatory cases, just send a UMA stat. |
| 529 | LOG(WARNING) << "Ignoring metadata signature validation failures"; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 530 | *error = ErrorCode::kSuccess; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 533 | // 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] | 534 | if (!payload_metadata_.GetManifest(payload, &manifest_)) { |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 535 | LOG(ERROR) << "Unable to parse manifest in update file."; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 536 | *error = ErrorCode::kDownloadManifestParseError; |
Sen Jiang | 9c89e84 | 2018-02-02 13:51:21 -0800 | [diff] [blame] | 537 | return MetadataParseResult::kError; |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 538 | } |
Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 539 | |
| 540 | manifest_parsed_ = true; |
Sen Jiang | 9c89e84 | 2018-02-02 13:51:21 -0800 | [diff] [blame] | 541 | return MetadataParseResult::kSuccess; |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 542 | } |
| 543 | |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 544 | #define OP_DURATION_HISTOGRAM(_op_name, _start_time) \ |
| 545 | LOCAL_HISTOGRAM_CUSTOM_TIMES( \ |
| 546 | "UpdateEngine.DownloadAction.InstallOperation::" _op_name ".Duration", \ |
Andrew | 9d5a61d | 2020-03-26 13:40:37 -0700 | [diff] [blame] | 547 | (base::TimeTicks::Now() - _start_time), \ |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 548 | base::TimeDelta::FromMilliseconds(10), \ |
| 549 | base::TimeDelta::FromMinutes(5), \ |
| 550 | 20); |
Lann Martin | 39f5714 | 2017-07-14 09:18:42 -0600 | [diff] [blame] | 551 | |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 552 | // Wrapper around write. Returns true if all requested bytes |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 553 | // were written, or false on any error, regardless of progress |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 554 | // and stores an action exit code in |error|. |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 555 | bool DeltaPerformer::Write(const void* bytes, size_t count, ErrorCode* error) { |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 556 | *error = ErrorCode::kSuccess; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 557 | const char* c_bytes = reinterpret_cast<const char*>(bytes); |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 558 | |
| 559 | // Update the total byte downloaded count and the progress logs. |
| 560 | total_bytes_received_ += count; |
| 561 | UpdateOverallProgress(false, "Completed "); |
| 562 | |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 563 | while (!manifest_valid_) { |
Sen Jiang | b8060e4 | 2015-09-24 17:30:50 -0700 | [diff] [blame] | 564 | // Read data up to the needed limit; this is either maximium payload header |
| 565 | // size, or the full metadata size (once it becomes known). |
| 566 | const bool do_read_header = !IsHeaderParsed(); |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 567 | CopyDataToBuffer( |
| 568 | &c_bytes, |
| 569 | &count, |
| 570 | (do_read_header ? kMaxPayloadHeaderSize |
| 571 | : metadata_size_ + metadata_signature_size_)); |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 572 | |
Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 573 | MetadataParseResult result = ParsePayloadMetadata(buffer_, error); |
Sen Jiang | 9c89e84 | 2018-02-02 13:51:21 -0800 | [diff] [blame] | 574 | if (result == MetadataParseResult::kError) |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 575 | return false; |
Sen Jiang | 9c89e84 | 2018-02-02 13:51:21 -0800 | [diff] [blame] | 576 | if (result == MetadataParseResult::kInsufficientData) { |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 577 | // If we just processed the header, make an attempt on the manifest. |
Sen Jiang | b8060e4 | 2015-09-24 17:30:50 -0700 | [diff] [blame] | 578 | if (do_read_header && IsHeaderParsed()) |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 579 | continue; |
| 580 | |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 581 | return true; |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 582 | } |
Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 583 | |
| 584 | // Checks the integrity of the payload manifest. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 585 | if ((*error = ValidateManifest()) != ErrorCode::kSuccess) |
Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 586 | return false; |
Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 587 | manifest_valid_ = true; |
Kelvin Zhang | cc011d3 | 2020-07-10 18:20:08 -0400 | [diff] [blame] | 588 | if (!install_plan_->is_resume) { |
| 589 | prefs_->SetString(kPrefsManifestBytes, {buffer_.begin(), buffer_.end()}); |
| 590 | } |
Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 591 | |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 592 | // Clear the download buffer. |
Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 593 | DiscardBuffer(false, metadata_size_); |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 594 | |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 595 | block_size_ = manifest_.block_size(); |
| 596 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 597 | // This populates |partitions_| and the |install_plan.partitions| with the |
| 598 | // list of partitions from the manifest. |
| 599 | if (!ParseManifestPartitions(error)) |
| 600 | return false; |
| 601 | |
Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 602 | // |install_plan.partitions| was filled in, nothing need to be done here if |
| 603 | // the payload was already applied, returns false to terminate http fetcher, |
| 604 | // but keep |error| as ErrorCode::kSuccess. |
| 605 | if (payload_->already_applied) |
| 606 | return false; |
| 607 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 608 | num_total_operations_ = 0; |
| 609 | for (const auto& partition : partitions_) { |
| 610 | num_total_operations_ += partition.operations_size(); |
| 611 | acc_num_operations_.push_back(num_total_operations_); |
| 612 | } |
| 613 | |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 614 | LOG_IF(WARNING, |
| 615 | !prefs_->SetInt64(kPrefsManifestMetadataSize, metadata_size_)) |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 616 | << "Unable to save the manifest metadata size."; |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 617 | LOG_IF(WARNING, |
| 618 | !prefs_->SetInt64(kPrefsManifestSignatureSize, |
| 619 | metadata_signature_size_)) |
Alex Deymo | f25eb49 | 2016-02-26 00:20:08 -0800 | [diff] [blame] | 620 | << "Unable to save the manifest signature size."; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 621 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 622 | if (!PrimeUpdateState()) { |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 623 | *error = ErrorCode::kDownloadStateInitializationError; |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 624 | LOG(ERROR) << "Unable to prime the update state."; |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 625 | return false; |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 626 | } |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 627 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 628 | if (next_operation_num_ < acc_num_operations_[current_partition_]) { |
| 629 | if (!OpenCurrentPartition()) { |
| 630 | *error = ErrorCode::kInstallDeviceOpenError; |
| 631 | return false; |
| 632 | } |
Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 633 | } |
| 634 | |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 635 | if (next_operation_num_ > 0) |
| 636 | UpdateOverallProgress(true, "Resuming after "); |
| 637 | LOG(INFO) << "Starting to apply update payload operations"; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 638 | } |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 639 | |
| 640 | while (next_operation_num_ < num_total_operations_) { |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 641 | // Check if we should cancel the current attempt for any reason. |
| 642 | // In this case, *error will have already been populated with the reason |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 643 | // why we're canceling. |
Alex Deymo | 542c19b | 2015-12-03 07:43:31 -0300 | [diff] [blame] | 644 | if (download_delegate_ && download_delegate_->ShouldCancel(error)) |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 645 | return false; |
| 646 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 647 | // We know there are more operations to perform because we didn't reach the |
| 648 | // |num_total_operations_| limit yet. |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 649 | if (next_operation_num_ >= acc_num_operations_[current_partition_]) { |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 650 | CloseCurrentPartition(); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 651 | // Skip until there are operations for current_partition_. |
| 652 | while (next_operation_num_ >= acc_num_operations_[current_partition_]) { |
| 653 | current_partition_++; |
| 654 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 655 | if (!OpenCurrentPartition()) { |
| 656 | *error = ErrorCode::kInstallDeviceOpenError; |
| 657 | return false; |
| 658 | } |
| 659 | } |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 660 | const size_t partition_operation_num = |
| 661 | next_operation_num_ - |
| 662 | (current_partition_ ? acc_num_operations_[current_partition_ - 1] : 0); |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 663 | |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 664 | const InstallOperation& op = |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 665 | partitions_[current_partition_].operations(partition_operation_num); |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 666 | |
| 667 | CopyDataToBuffer(&c_bytes, &count, op.data_length()); |
| 668 | |
| 669 | // Check whether we received all of the next operation's data payload. |
| 670 | if (!CanPerformInstallOperation(op)) |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 671 | return true; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 672 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 673 | // Validate the operation only if the metadata signature is present. |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 674 | // Otherwise, keep the old behavior. This serves as a knob to disable |
| 675 | // the validation logic in case we find some regression after rollout. |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 676 | // NOTE: If hash checks are mandatory and if metadata_signature is empty, |
| 677 | // we would have already failed in ParsePayloadMetadata method and thus not |
| 678 | // even be here. So no need to handle that case again here. |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 679 | if (!payload_->metadata_signature.empty()) { |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 680 | // Note: Validate must be called only if CanPerformInstallOperation is |
| 681 | // called. Otherwise, we might be failing operations before even if there |
| 682 | // isn't sufficient data to compute the proper hash. |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 683 | *error = ValidateOperationHash(op); |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 684 | if (*error != ErrorCode::kSuccess) { |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 685 | if (install_plan_->hash_checks_mandatory) { |
| 686 | LOG(ERROR) << "Mandatory operation hash check failed"; |
| 687 | return false; |
| 688 | } |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 689 | |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 690 | // For non-mandatory cases, just send a UMA stat. |
| 691 | LOG(WARNING) << "Ignoring operation validation errors"; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 692 | *error = ErrorCode::kSuccess; |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 693 | } |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 694 | } |
| 695 | |
Darin Petkov | 45580e4 | 2010-10-08 14:02:40 -0700 | [diff] [blame] | 696 | // Makes sure we unblock exit when this operation completes. |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 697 | ScopedTerminatorExitUnblocker exit_unblocker = |
| 698 | ScopedTerminatorExitUnblocker(); // Avoids a compiler unused var bug. |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 699 | |
Lann Martin | 39f5714 | 2017-07-14 09:18:42 -0600 | [diff] [blame] | 700 | base::TimeTicks op_start_time = base::TimeTicks::Now(); |
| 701 | |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 702 | bool op_result; |
Alex Deymo | 2d621a3 | 2015-10-01 11:09:01 -0700 | [diff] [blame] | 703 | switch (op.type()) { |
| 704 | case InstallOperation::REPLACE: |
| 705 | case InstallOperation::REPLACE_BZ: |
| 706 | case InstallOperation::REPLACE_XZ: |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 707 | op_result = PerformReplaceOperation(op); |
Lann Martin | 39f5714 | 2017-07-14 09:18:42 -0600 | [diff] [blame] | 708 | OP_DURATION_HISTOGRAM("REPLACE", op_start_time); |
Alex Deymo | 2d621a3 | 2015-10-01 11:09:01 -0700 | [diff] [blame] | 709 | break; |
Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 710 | case InstallOperation::ZERO: |
| 711 | case InstallOperation::DISCARD: |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 712 | op_result = PerformZeroOrDiscardOperation(op); |
Lann Martin | 39f5714 | 2017-07-14 09:18:42 -0600 | [diff] [blame] | 713 | OP_DURATION_HISTOGRAM("ZERO_OR_DISCARD", op_start_time); |
Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 714 | break; |
Alex Deymo | 2d621a3 | 2015-10-01 11:09:01 -0700 | [diff] [blame] | 715 | case InstallOperation::SOURCE_COPY: |
Sen Jiang | be2c47b | 2016-06-15 14:09:27 -0700 | [diff] [blame] | 716 | op_result = PerformSourceCopyOperation(op, error); |
Lann Martin | 39f5714 | 2017-07-14 09:18:42 -0600 | [diff] [blame] | 717 | OP_DURATION_HISTOGRAM("SOURCE_COPY", op_start_time); |
Alex Deymo | 2d621a3 | 2015-10-01 11:09:01 -0700 | [diff] [blame] | 718 | break; |
| 719 | case InstallOperation::SOURCE_BSDIFF: |
Amin Hassani | efa62d9 | 2017-11-09 13:46:56 -0800 | [diff] [blame] | 720 | case InstallOperation::BROTLI_BSDIFF: |
Sen Jiang | be2c47b | 2016-06-15 14:09:27 -0700 | [diff] [blame] | 721 | op_result = PerformSourceBsdiffOperation(op, error); |
Lann Martin | 39f5714 | 2017-07-14 09:18:42 -0600 | [diff] [blame] | 722 | OP_DURATION_HISTOGRAM("SOURCE_BSDIFF", op_start_time); |
Alex Deymo | 2d621a3 | 2015-10-01 11:09:01 -0700 | [diff] [blame] | 723 | break; |
Amin Hassani | 49fdb09 | 2017-08-04 13:10:59 -0700 | [diff] [blame] | 724 | case InstallOperation::PUFFDIFF: |
Amin Hassani | 02855c2 | 2017-09-06 22:34:50 -0700 | [diff] [blame] | 725 | op_result = PerformPuffDiffOperation(op, error); |
| 726 | OP_DURATION_HISTOGRAM("PUFFDIFF", op_start_time); |
Sen Jiang | bc3e6b0 | 2016-01-19 18:39:26 +0800 | [diff] [blame] | 727 | break; |
Alex Deymo | 2d621a3 | 2015-10-01 11:09:01 -0700 | [diff] [blame] | 728 | default: |
Alex Deymo | eecb0a5 | 2017-05-19 15:15:08 -0700 | [diff] [blame] | 729 | op_result = false; |
Alex Deymo | 2d621a3 | 2015-10-01 11:09:01 -0700 | [diff] [blame] | 730 | } |
| 731 | if (!HandleOpResult(op_result, InstallOperationTypeName(op.type()), error)) |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 732 | return false; |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 733 | |
Amin Hassani | 5192fe5 | 2017-08-28 10:28:46 -0700 | [diff] [blame] | 734 | if (!target_fd_->Flush()) { |
| 735 | return false; |
| 736 | } |
| 737 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 738 | next_operation_num_++; |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 739 | UpdateOverallProgress(false, "Completed "); |
Sen Jiang | 3a25dc2 | 2019-01-10 14:14:41 -0800 | [diff] [blame] | 740 | CheckpointUpdateProgress(false); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 741 | } |
Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 742 | |
Saint Chou | 0a92a62 | 2020-07-29 14:25:35 +0000 | [diff] [blame^] | 743 | // 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] | 744 | // If we already extracted the signature we should skip this step. |
Amin Hassani | 822d485 | 2020-03-10 01:50:42 +0000 | [diff] [blame] | 745 | if (manifest_.has_signatures_offset() && manifest_.has_signatures_size() && |
Alex Deymo | f25eb49 | 2016-02-26 00:20:08 -0800 | [diff] [blame] | 746 | signatures_message_data_.empty()) { |
Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 747 | if (manifest_.signatures_offset() != buffer_offset_) { |
| 748 | LOG(ERROR) << "Payload signatures offset points to blob offset " |
| 749 | << manifest_.signatures_offset() |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 750 | << " but signatures are expected at offset " << buffer_offset_; |
Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 751 | *error = ErrorCode::kDownloadPayloadVerificationError; |
| 752 | return false; |
| 753 | } |
| 754 | CopyDataToBuffer(&c_bytes, &count, manifest_.signatures_size()); |
| 755 | // Needs more data to cover entire signature. |
| 756 | if (buffer_.size() < manifest_.signatures_size()) |
| 757 | return true; |
| 758 | if (!ExtractSignatureMessage()) { |
| 759 | LOG(ERROR) << "Extract payload signature failed."; |
| 760 | *error = ErrorCode::kDownloadPayloadVerificationError; |
| 761 | return false; |
| 762 | } |
| 763 | DiscardBuffer(true, 0); |
Alex Deymo | f25eb49 | 2016-02-26 00:20:08 -0800 | [diff] [blame] | 764 | // Since we extracted the SignatureMessage we need to advance the |
| 765 | // checkpoint, otherwise we would reload the signature and try to extract |
| 766 | // it again. |
Sen Jiang | 3a25dc2 | 2019-01-10 14:14:41 -0800 | [diff] [blame] | 767 | // This is the last checkpoint for an update, force this checkpoint to be |
| 768 | // saved. |
| 769 | CheckpointUpdateProgress(true); |
Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 770 | } |
| 771 | |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 772 | return true; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 773 | } |
| 774 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 775 | bool DeltaPerformer::IsManifestValid() { |
| 776 | return manifest_valid_; |
| 777 | } |
| 778 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 779 | bool DeltaPerformer::ParseManifestPartitions(ErrorCode* error) { |
Amin Hassani | 822d485 | 2020-03-10 01:50:42 +0000 | [diff] [blame] | 780 | partitions_.clear(); |
| 781 | for (const PartitionUpdate& partition : manifest_.partitions()) { |
| 782 | partitions_.push_back(partition); |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 783 | } |
| 784 | |
Tianjie | d3865d1 | 2020-06-03 15:25:17 -0700 | [diff] [blame] | 785 | // For VAB and partial updates, the partition preparation will copy the |
| 786 | // dynamic partitions metadata to the target metadata slot, and rename the |
| 787 | // slot suffix of the partitions in the metadata. |
| 788 | if (install_plan_->target_slot != BootControlInterface::kInvalidSlot) { |
| 789 | uint64_t required_size = 0; |
| 790 | if (!PreparePartitionsForUpdate(&required_size)) { |
| 791 | if (required_size > 0) { |
| 792 | *error = ErrorCode::kNotEnoughSpace; |
| 793 | } else { |
| 794 | *error = ErrorCode::kInstallDeviceOpenError; |
| 795 | } |
| 796 | return false; |
| 797 | } |
| 798 | } |
| 799 | |
Tianjie | 55abd3c | 2020-06-19 00:22:59 -0700 | [diff] [blame] | 800 | // Partitions in manifest are no longer needed after preparing partitions. |
Amin Hassani | 822d485 | 2020-03-10 01:50:42 +0000 | [diff] [blame] | 801 | manifest_.clear_partitions(); |
Tianjie | d3865d1 | 2020-06-03 15:25:17 -0700 | [diff] [blame] | 802 | // TODO(xunchang) TBD: allow partial update only on devices with dynamic |
| 803 | // partition. |
| 804 | if (manifest_.partial_update()) { |
| 805 | std::set<std::string> touched_partitions; |
| 806 | for (const auto& partition_update : partitions_) { |
| 807 | touched_partitions.insert(partition_update.partition_name()); |
| 808 | } |
| 809 | |
Tianjie | 99d570d | 2020-06-04 14:57:19 -0700 | [diff] [blame] | 810 | auto generator = partition_update_generator::Create(boot_control_, |
| 811 | manifest_.block_size()); |
Tianjie | 24f9609 | 2020-06-30 12:26:25 -0700 | [diff] [blame] | 812 | std::vector<PartitionUpdate> untouched_static_partitions; |
Tianjie | d3865d1 | 2020-06-03 15:25:17 -0700 | [diff] [blame] | 813 | TEST_AND_RETURN_FALSE( |
| 814 | generator->GenerateOperationsForPartitionsNotInPayload( |
| 815 | install_plan_->source_slot, |
| 816 | install_plan_->target_slot, |
| 817 | touched_partitions, |
Tianjie | 24f9609 | 2020-06-30 12:26:25 -0700 | [diff] [blame] | 818 | &untouched_static_partitions)); |
| 819 | partitions_.insert(partitions_.end(), |
| 820 | untouched_static_partitions.begin(), |
| 821 | untouched_static_partitions.end()); |
| 822 | |
| 823 | // Save the untouched dynamic partitions in install plan. |
| 824 | std::vector<std::string> dynamic_partitions; |
| 825 | if (!boot_control_->GetDynamicPartitionControl() |
| 826 | ->ListDynamicPartitionsForSlot(install_plan_->source_slot, |
| 827 | &dynamic_partitions)) { |
| 828 | LOG(ERROR) << "Failed to load dynamic partitions from slot " |
| 829 | << install_plan_->source_slot; |
| 830 | return false; |
| 831 | } |
| 832 | install_plan_->untouched_dynamic_partitions.clear(); |
| 833 | for (const auto& name : dynamic_partitions) { |
| 834 | if (touched_partitions.find(name) == touched_partitions.end()) { |
| 835 | install_plan_->untouched_dynamic_partitions.push_back(name); |
| 836 | } |
| 837 | } |
Tianjie | d3865d1 | 2020-06-03 15:25:17 -0700 | [diff] [blame] | 838 | } |
| 839 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 840 | // Fill in the InstallPlan::partitions based on the partitions from the |
| 841 | // payload. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 842 | for (const auto& partition : partitions_) { |
| 843 | InstallPlan::Partition install_part; |
| 844 | install_part.name = partition.partition_name(); |
| 845 | install_part.run_postinstall = |
| 846 | partition.has_run_postinstall() && partition.run_postinstall(); |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 847 | if (install_part.run_postinstall) { |
| 848 | install_part.postinstall_path = |
| 849 | (partition.has_postinstall_path() ? partition.postinstall_path() |
| 850 | : kPostinstallDefaultScript); |
| 851 | install_part.filesystem_type = partition.filesystem_type(); |
Alex Deymo | 5b91c6b | 2016-08-04 20:33:36 -0700 | [diff] [blame] | 852 | install_part.postinstall_optional = partition.postinstall_optional(); |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 853 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 854 | |
| 855 | if (partition.has_old_partition_info()) { |
| 856 | const PartitionInfo& info = partition.old_partition_info(); |
| 857 | install_part.source_size = info.size(); |
| 858 | install_part.source_hash.assign(info.hash().begin(), info.hash().end()); |
| 859 | } |
| 860 | |
| 861 | if (!partition.has_new_partition_info()) { |
| 862 | LOG(ERROR) << "Unable to get new partition hash info on partition " |
| 863 | << install_part.name << "."; |
| 864 | *error = ErrorCode::kDownloadNewPartitionInfoError; |
| 865 | return false; |
| 866 | } |
| 867 | const PartitionInfo& info = partition.new_partition_info(); |
| 868 | install_part.target_size = info.size(); |
| 869 | install_part.target_hash.assign(info.hash().begin(), info.hash().end()); |
| 870 | |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 871 | install_part.block_size = block_size_; |
| 872 | if (partition.has_hash_tree_extent()) { |
| 873 | Extent extent = partition.hash_tree_data_extent(); |
| 874 | install_part.hash_tree_data_offset = extent.start_block() * block_size_; |
| 875 | install_part.hash_tree_data_size = extent.num_blocks() * block_size_; |
| 876 | extent = partition.hash_tree_extent(); |
| 877 | install_part.hash_tree_offset = extent.start_block() * block_size_; |
| 878 | install_part.hash_tree_size = extent.num_blocks() * block_size_; |
| 879 | uint64_t hash_tree_data_end = |
| 880 | install_part.hash_tree_data_offset + install_part.hash_tree_data_size; |
| 881 | if (install_part.hash_tree_offset < hash_tree_data_end) { |
| 882 | LOG(ERROR) << "Invalid hash tree extents, hash tree data ends at " |
| 883 | << hash_tree_data_end << ", but hash tree starts at " |
| 884 | << install_part.hash_tree_offset; |
| 885 | *error = ErrorCode::kDownloadNewPartitionInfoError; |
| 886 | return false; |
| 887 | } |
| 888 | install_part.hash_tree_algorithm = partition.hash_tree_algorithm(); |
| 889 | install_part.hash_tree_salt.assign(partition.hash_tree_salt().begin(), |
| 890 | partition.hash_tree_salt().end()); |
| 891 | } |
| 892 | if (partition.has_fec_extent()) { |
| 893 | Extent extent = partition.fec_data_extent(); |
| 894 | install_part.fec_data_offset = extent.start_block() * block_size_; |
| 895 | install_part.fec_data_size = extent.num_blocks() * block_size_; |
| 896 | extent = partition.fec_extent(); |
| 897 | install_part.fec_offset = extent.start_block() * block_size_; |
| 898 | install_part.fec_size = extent.num_blocks() * block_size_; |
| 899 | uint64_t fec_data_end = |
| 900 | install_part.fec_data_offset + install_part.fec_data_size; |
| 901 | if (install_part.fec_offset < fec_data_end) { |
| 902 | LOG(ERROR) << "Invalid fec extents, fec data ends at " << fec_data_end |
| 903 | << ", but fec starts at " << install_part.fec_offset; |
| 904 | *error = ErrorCode::kDownloadNewPartitionInfoError; |
| 905 | return false; |
| 906 | } |
| 907 | install_part.fec_roots = partition.fec_roots(); |
| 908 | } |
| 909 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 910 | install_plan_->partitions.push_back(install_part); |
| 911 | } |
| 912 | |
Tianjie | d3865d1 | 2020-06-03 15:25:17 -0700 | [diff] [blame] | 913 | // TODO(xunchang) only need to load the partitions for those in payload. |
| 914 | // Because we have already loaded the other once when generating SOURCE_COPY |
| 915 | // operations. |
Alex Deymo | 542c19b | 2015-12-03 07:43:31 -0300 | [diff] [blame] | 916 | if (!install_plan_->LoadPartitionsFromSlots(boot_control_)) { |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 917 | LOG(ERROR) << "Unable to determine all the partition devices."; |
| 918 | *error = ErrorCode::kInstallDeviceOpenError; |
| 919 | return false; |
| 920 | } |
| 921 | LogPartitionInfo(partitions_); |
| 922 | return true; |
| 923 | } |
| 924 | |
Yifan Hong | b9d6357 | 2020-01-09 17:50:46 -0800 | [diff] [blame] | 925 | bool DeltaPerformer::PreparePartitionsForUpdate(uint64_t* required_size) { |
| 926 | // Call static PreparePartitionsForUpdate with hash from |
| 927 | // kPrefsUpdateCheckResponseHash to ensure hash of payload that space is |
| 928 | // preallocated for is the same as the hash of payload being applied. |
| 929 | string update_check_response_hash; |
| 930 | ignore_result(prefs_->GetString(kPrefsUpdateCheckResponseHash, |
| 931 | &update_check_response_hash)); |
| 932 | return PreparePartitionsForUpdate(prefs_, |
| 933 | boot_control_, |
| 934 | install_plan_->target_slot, |
| 935 | manifest_, |
| 936 | update_check_response_hash, |
| 937 | required_size); |
| 938 | } |
| 939 | |
| 940 | bool DeltaPerformer::PreparePartitionsForUpdate( |
| 941 | PrefsInterface* prefs, |
| 942 | BootControlInterface* boot_control, |
| 943 | BootControlInterface::Slot target_slot, |
| 944 | const DeltaArchiveManifest& manifest, |
| 945 | const std::string& update_check_response_hash, |
| 946 | uint64_t* required_size) { |
| 947 | string last_hash; |
| 948 | ignore_result( |
| 949 | prefs->GetString(kPrefsDynamicPartitionMetadataUpdated, &last_hash)); |
| 950 | |
| 951 | bool is_resume = !update_check_response_hash.empty() && |
| 952 | last_hash == update_check_response_hash; |
| 953 | |
| 954 | if (is_resume) { |
| 955 | LOG(INFO) << "Using previously prepared partitions for update. hash = " |
| 956 | << last_hash; |
| 957 | } else { |
| 958 | LOG(INFO) << "Preparing partitions for new update. last hash = " |
| 959 | << last_hash << ", new hash = " << update_check_response_hash; |
| 960 | } |
| 961 | |
| 962 | if (!boot_control->GetDynamicPartitionControl()->PreparePartitionsForUpdate( |
| 963 | boot_control->GetCurrentSlot(), |
| 964 | target_slot, |
| 965 | manifest, |
| 966 | !is_resume /* should update */, |
| 967 | required_size)) { |
Yifan Hong | 9acd9cb | 2018-10-19 14:52:45 -0700 | [diff] [blame] | 968 | LOG(ERROR) << "Unable to initialize partition metadata for slot " |
Yifan Hong | b9d6357 | 2020-01-09 17:50:46 -0800 | [diff] [blame] | 969 | << BootControlInterface::SlotName(target_slot); |
Yifan Hong | 9acd9cb | 2018-10-19 14:52:45 -0700 | [diff] [blame] | 970 | return false; |
| 971 | } |
Yifan Hong | b9d6357 | 2020-01-09 17:50:46 -0800 | [diff] [blame] | 972 | |
| 973 | TEST_AND_RETURN_FALSE(prefs->SetString(kPrefsDynamicPartitionMetadataUpdated, |
| 974 | update_check_response_hash)); |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 975 | LOG(INFO) << "PreparePartitionsForUpdate done."; |
Yifan Hong | 9acd9cb | 2018-10-19 14:52:45 -0700 | [diff] [blame] | 976 | |
| 977 | return true; |
| 978 | } |
| 979 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 980 | bool DeltaPerformer::CanPerformInstallOperation( |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 981 | const chromeos_update_engine::InstallOperation& operation) { |
Alex Deymo | 0497d05 | 2016-03-23 09:16:59 -0700 | [diff] [blame] | 982 | // If we don't have a data blob we can apply it right away. |
| 983 | if (!operation.has_data_offset() && !operation.has_data_length()) |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 984 | return true; |
| 985 | |
| 986 | // See if we have the entire data blob in the buffer |
| 987 | if (operation.data_offset() < buffer_offset_) { |
| 988 | LOG(ERROR) << "we threw away data it seems?"; |
| 989 | return false; |
| 990 | } |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 991 | |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 992 | return (operation.data_offset() + operation.data_length() <= |
| 993 | buffer_offset_ + buffer_.size()); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 994 | } |
| 995 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 996 | bool DeltaPerformer::PerformReplaceOperation( |
| 997 | const InstallOperation& operation) { |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 998 | CHECK(operation.type() == InstallOperation::REPLACE || |
Alex Deymo | 2d621a3 | 2015-10-01 11:09:01 -0700 | [diff] [blame] | 999 | operation.type() == InstallOperation::REPLACE_BZ || |
| 1000 | operation.type() == InstallOperation::REPLACE_XZ); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 1001 | |
| 1002 | // Since we delete data off the beginning of the buffer as we use it, |
| 1003 | // 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] | 1004 | TEST_AND_RETURN_FALSE(buffer_offset_ == operation.data_offset()); |
| 1005 | TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length()); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 1006 | |
Alex Deymo | 0532287 | 2015-09-30 09:50:24 -0700 | [diff] [blame] | 1007 | // Setup the ExtentWriter stack based on the operation type. |
Sen Jiang | 5e1af98 | 2018-11-01 15:01:45 -0700 | [diff] [blame] | 1008 | std::unique_ptr<ExtentWriter> writer = std::make_unique<DirectExtentWriter>(); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 1009 | |
Alex Deymo | 2d621a3 | 2015-10-01 11:09:01 -0700 | [diff] [blame] | 1010 | if (operation.type() == InstallOperation::REPLACE_BZ) { |
Alex Deymo | 0532287 | 2015-09-30 09:50:24 -0700 | [diff] [blame] | 1011 | writer.reset(new BzipExtentWriter(std::move(writer))); |
Alex Deymo | 2d621a3 | 2015-10-01 11:09:01 -0700 | [diff] [blame] | 1012 | } else if (operation.type() == InstallOperation::REPLACE_XZ) { |
| 1013 | writer.reset(new XzExtentWriter(std::move(writer))); |
| 1014 | } |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 1015 | |
Amin Hassani | cd7edbe | 2017-09-18 17:05:02 -0700 | [diff] [blame] | 1016 | TEST_AND_RETURN_FALSE( |
| 1017 | writer->Init(target_fd_, operation.dst_extents(), block_size_)); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1018 | TEST_AND_RETURN_FALSE(writer->Write(buffer_.data(), operation.data_length())); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 1019 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 1020 | // Update buffer |
Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 1021 | DiscardBuffer(true, buffer_.size()); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 1022 | return true; |
| 1023 | } |
| 1024 | |
Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 1025 | bool DeltaPerformer::PerformZeroOrDiscardOperation( |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 1026 | const InstallOperation& operation) { |
Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 1027 | CHECK(operation.type() == InstallOperation::DISCARD || |
| 1028 | operation.type() == InstallOperation::ZERO); |
| 1029 | |
| 1030 | // These operations have no blob. |
| 1031 | TEST_AND_RETURN_FALSE(!operation.has_data_offset()); |
| 1032 | TEST_AND_RETURN_FALSE(!operation.has_data_length()); |
| 1033 | |
Alex Deymo | 05e0e38 | 2015-12-07 20:18:16 -0800 | [diff] [blame] | 1034 | #ifdef BLKZEROOUT |
| 1035 | bool attempt_ioctl = true; |
Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 1036 | int request = |
| 1037 | (operation.type() == InstallOperation::ZERO ? BLKZEROOUT : BLKDISCARD); |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 1038 | #else // !defined(BLKZEROOUT) |
Alex Deymo | 05e0e38 | 2015-12-07 20:18:16 -0800 | [diff] [blame] | 1039 | bool attempt_ioctl = false; |
| 1040 | int request = 0; |
| 1041 | #endif // !defined(BLKZEROOUT) |
Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 1042 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1043 | brillo::Blob zeros; |
Chih-Hung Hsieh | 5c6bb1d | 2016-07-27 13:33:15 -0700 | [diff] [blame] | 1044 | for (const Extent& extent : operation.dst_extents()) { |
Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 1045 | const uint64_t start = extent.start_block() * block_size_; |
| 1046 | const uint64_t length = extent.num_blocks() * block_size_; |
| 1047 | if (attempt_ioctl) { |
| 1048 | int result = 0; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 1049 | if (target_fd_->BlkIoctl(request, start, length, &result) && result == 0) |
Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 1050 | continue; |
| 1051 | attempt_ioctl = false; |
Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 1052 | } |
| 1053 | // In case of failure, we fall back to writing 0 to the selected region. |
Tianjie Xu | 06bbe49 | 2018-01-12 12:37:48 -0800 | [diff] [blame] | 1054 | zeros.resize(16 * block_size_); |
Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 1055 | for (uint64_t offset = 0; offset < length; offset += zeros.size()) { |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 1056 | uint64_t chunk_length = |
| 1057 | min(length - offset, static_cast<uint64_t>(zeros.size())); |
Amin Hassani | d87304c | 2017-08-29 11:40:03 -0700 | [diff] [blame] | 1058 | TEST_AND_RETURN_FALSE(utils::PWriteAll( |
| 1059 | target_fd_, zeros.data(), chunk_length, start + offset)); |
Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 1060 | } |
| 1061 | } |
| 1062 | return true; |
| 1063 | } |
| 1064 | |
Sen Jiang | 8371c1c | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 1065 | bool DeltaPerformer::ValidateSourceHash(const brillo::Blob& calculated_hash, |
| 1066 | const InstallOperation& operation, |
| 1067 | const FileDescriptorPtr source_fd, |
| 1068 | ErrorCode* error) { |
Sen Jiang | 2ec4aab | 2015-11-13 15:04:03 -0800 | [diff] [blame] | 1069 | brillo::Blob expected_source_hash(operation.src_sha256_hash().begin(), |
| 1070 | operation.src_sha256_hash().end()); |
| 1071 | if (calculated_hash != expected_source_hash) { |
Alex Deymo | 6714084 | 2016-06-15 13:28:59 -0700 | [diff] [blame] | 1072 | LOG(ERROR) << "The hash of the source data on disk for this operation " |
| 1073 | << "doesn't match the expected value. This could mean that the " |
| 1074 | << "delta update payload was targeted for another version, or " |
| 1075 | << "that the source partition was modified after it was " |
| 1076 | << "installed, for example, by mounting a filesystem."; |
| 1077 | LOG(ERROR) << "Expected: sha256|hex = " |
| 1078 | << base::HexEncode(expected_source_hash.data(), |
| 1079 | expected_source_hash.size()); |
| 1080 | LOG(ERROR) << "Calculated: sha256|hex = " |
| 1081 | << base::HexEncode(calculated_hash.data(), |
| 1082 | calculated_hash.size()); |
| 1083 | |
| 1084 | vector<string> source_extents; |
| 1085 | for (const Extent& ext : operation.src_extents()) { |
Tamas Berghammer | 9d66d76 | 2016-07-15 14:19:04 +0100 | [diff] [blame] | 1086 | source_extents.push_back( |
| 1087 | base::StringPrintf("%" PRIu64 ":%" PRIu64, |
| 1088 | static_cast<uint64_t>(ext.start_block()), |
| 1089 | static_cast<uint64_t>(ext.num_blocks()))); |
Alex Deymo | 6714084 | 2016-06-15 13:28:59 -0700 | [diff] [blame] | 1090 | } |
| 1091 | LOG(ERROR) << "Operation source (offset:size) in blocks: " |
| 1092 | << base::JoinString(source_extents, ","); |
Sen Jiang | be2c47b | 2016-06-15 14:09:27 -0700 | [diff] [blame] | 1093 | |
HÃ¥kan Kvist | 4e13cf4 | 2018-01-23 12:57:55 +0100 | [diff] [blame] | 1094 | // Log remount history if this device is an ext4 partition. |
| 1095 | LogMountHistory(source_fd); |
| 1096 | |
Sen Jiang | be2c47b | 2016-06-15 14:09:27 -0700 | [diff] [blame] | 1097 | *error = ErrorCode::kDownloadStateInitializationError; |
Sen Jiang | 2ec4aab | 2015-11-13 15:04:03 -0800 | [diff] [blame] | 1098 | return false; |
| 1099 | } |
| 1100 | return true; |
| 1101 | } |
| 1102 | |
Allie Wood | 9f6f0a5 | 2015-03-30 11:25:47 -0700 | [diff] [blame] | 1103 | bool DeltaPerformer::PerformSourceCopyOperation( |
Sen Jiang | be2c47b | 2016-06-15 14:09:27 -0700 | [diff] [blame] | 1104 | const InstallOperation& operation, ErrorCode* error) { |
Allie Wood | 9f6f0a5 | 2015-03-30 11:25:47 -0700 | [diff] [blame] | 1105 | if (operation.has_src_length()) |
| 1106 | TEST_AND_RETURN_FALSE(operation.src_length() % block_size_ == 0); |
| 1107 | if (operation.has_dst_length()) |
| 1108 | TEST_AND_RETURN_FALSE(operation.dst_length() % block_size_ == 0); |
| 1109 | |
Hridya Valsaraju | e69ca5f | 2019-02-25 22:33:56 -0800 | [diff] [blame] | 1110 | TEST_AND_RETURN_FALSE(source_fd_ != nullptr); |
| 1111 | |
Alessio Balsini | 8d38431 | 2019-11-26 11:46:33 +0000 | [diff] [blame] | 1112 | // The device may optimize the SOURCE_COPY operation. |
| 1113 | // Being this a device-specific optimization let DynamicPartitionController |
| 1114 | // decide it the operation should be skipped. |
Yifan Hong | 6eec995 | 2019-12-04 13:12:01 -0800 | [diff] [blame] | 1115 | const PartitionUpdate& partition = partitions_[current_partition_]; |
Alessio Balsini | 8d38431 | 2019-11-26 11:46:33 +0000 | [diff] [blame] | 1116 | const auto& partition_control = boot_control_->GetDynamicPartitionControl(); |
Yifan Hong | f526156 | 2020-03-10 10:28:10 -0700 | [diff] [blame] | 1117 | |
| 1118 | InstallOperation buf; |
| 1119 | bool should_optimize = partition_control->OptimizeOperation( |
| 1120 | partition.partition_name(), operation, &buf); |
| 1121 | const InstallOperation& optimized = should_optimize ? buf : operation; |
Alessio Balsini | 8d38431 | 2019-11-26 11:46:33 +0000 | [diff] [blame] | 1122 | |
Sen Jiang | 2ec4aab | 2015-11-13 15:04:03 -0800 | [diff] [blame] | 1123 | if (operation.has_src_sha256_hash()) { |
Alessio Balsini | 8d38431 | 2019-11-26 11:46:33 +0000 | [diff] [blame] | 1124 | bool read_ok; |
Alex Deymo | 51c264e | 2016-11-04 15:49:53 -0700 | [diff] [blame] | 1125 | brillo::Blob source_hash; |
| 1126 | brillo::Blob expected_source_hash(operation.src_sha256_hash().begin(), |
| 1127 | operation.src_sha256_hash().end()); |
Sen Jiang | 2ec4aab | 2015-11-13 15:04:03 -0800 | [diff] [blame] | 1128 | |
Alex Deymo | 51c264e | 2016-11-04 15:49:53 -0700 | [diff] [blame] | 1129 | // We fall back to use the error corrected device if the hash of the raw |
| 1130 | // device doesn't match or there was an error reading the source partition. |
| 1131 | // Note that this code will also fall back if writing the target partition |
| 1132 | // fails. |
Yifan Hong | f526156 | 2020-03-10 10:28:10 -0700 | [diff] [blame] | 1133 | if (should_optimize) { |
| 1134 | // Hash operation.src_extents(), then copy optimized.src_extents to |
| 1135 | // optimized.dst_extents. |
| 1136 | read_ok = |
| 1137 | fd_utils::ReadAndHashExtents( |
| 1138 | source_fd_, operation.src_extents(), block_size_, &source_hash) && |
| 1139 | fd_utils::CopyAndHashExtents(source_fd_, |
| 1140 | optimized.src_extents(), |
| 1141 | target_fd_, |
| 1142 | optimized.dst_extents(), |
| 1143 | block_size_, |
| 1144 | nullptr /* skip hashing */); |
Alessio Balsini | 8d38431 | 2019-11-26 11:46:33 +0000 | [diff] [blame] | 1145 | } else { |
| 1146 | read_ok = fd_utils::CopyAndHashExtents(source_fd_, |
| 1147 | operation.src_extents(), |
| 1148 | target_fd_, |
| 1149 | operation.dst_extents(), |
| 1150 | block_size_, |
| 1151 | &source_hash); |
| 1152 | } |
Alex Deymo | 51c264e | 2016-11-04 15:49:53 -0700 | [diff] [blame] | 1153 | if (read_ok && expected_source_hash == source_hash) |
| 1154 | return true; |
| 1155 | |
| 1156 | if (!OpenCurrentECCPartition()) { |
| 1157 | // The following function call will return false since the source hash |
| 1158 | // mismatches, but we still want to call it so it prints the appropriate |
| 1159 | // log message. |
| 1160 | return ValidateSourceHash(source_hash, operation, source_fd_, error); |
| 1161 | } |
| 1162 | |
| 1163 | LOG(WARNING) << "Source hash from RAW device mismatched: found " |
| 1164 | << base::HexEncode(source_hash.data(), source_hash.size()) |
| 1165 | << ", expected " |
| 1166 | << base::HexEncode(expected_source_hash.data(), |
| 1167 | expected_source_hash.size()); |
| 1168 | |
Yifan Hong | f526156 | 2020-03-10 10:28:10 -0700 | [diff] [blame] | 1169 | if (should_optimize) { |
Alessio Balsini | 8d38431 | 2019-11-26 11:46:33 +0000 | [diff] [blame] | 1170 | TEST_AND_RETURN_FALSE(fd_utils::ReadAndHashExtents( |
| 1171 | source_ecc_fd_, operation.src_extents(), block_size_, &source_hash)); |
Yifan Hong | f526156 | 2020-03-10 10:28:10 -0700 | [diff] [blame] | 1172 | TEST_AND_RETURN_FALSE( |
| 1173 | fd_utils::CopyAndHashExtents(source_ecc_fd_, |
| 1174 | optimized.src_extents(), |
| 1175 | target_fd_, |
| 1176 | optimized.dst_extents(), |
| 1177 | block_size_, |
| 1178 | nullptr /* skip hashing */)); |
Alessio Balsini | 8d38431 | 2019-11-26 11:46:33 +0000 | [diff] [blame] | 1179 | } else { |
| 1180 | TEST_AND_RETURN_FALSE( |
| 1181 | fd_utils::CopyAndHashExtents(source_ecc_fd_, |
| 1182 | operation.src_extents(), |
| 1183 | target_fd_, |
| 1184 | operation.dst_extents(), |
| 1185 | block_size_, |
| 1186 | &source_hash)); |
| 1187 | } |
Alex Deymo | 51c264e | 2016-11-04 15:49:53 -0700 | [diff] [blame] | 1188 | TEST_AND_RETURN_FALSE( |
| 1189 | ValidateSourceHash(source_hash, operation, source_ecc_fd_, error)); |
| 1190 | // At this point reading from the the error corrected device worked, but |
| 1191 | // reading from the raw device failed, so this is considered a recovered |
| 1192 | // failure. |
| 1193 | source_ecc_recovered_failures_++; |
| 1194 | } else { |
| 1195 | // When the operation doesn't include a source hash, we attempt the error |
| 1196 | // corrected device first since we can't verify the block in the raw device |
| 1197 | // at this point, but we fall back to the raw device since the error |
| 1198 | // corrected device can be shorter or not available. |
Alessio Balsini | 8d38431 | 2019-11-26 11:46:33 +0000 | [diff] [blame] | 1199 | |
Alex Deymo | 51c264e | 2016-11-04 15:49:53 -0700 | [diff] [blame] | 1200 | if (OpenCurrentECCPartition() && |
| 1201 | fd_utils::CopyAndHashExtents(source_ecc_fd_, |
Yifan Hong | f526156 | 2020-03-10 10:28:10 -0700 | [diff] [blame] | 1202 | optimized.src_extents(), |
Alex Deymo | 51c264e | 2016-11-04 15:49:53 -0700 | [diff] [blame] | 1203 | target_fd_, |
Yifan Hong | f526156 | 2020-03-10 10:28:10 -0700 | [diff] [blame] | 1204 | optimized.dst_extents(), |
Alex Deymo | 51c264e | 2016-11-04 15:49:53 -0700 | [diff] [blame] | 1205 | block_size_, |
| 1206 | nullptr)) { |
| 1207 | return true; |
| 1208 | } |
| 1209 | TEST_AND_RETURN_FALSE(fd_utils::CopyAndHashExtents(source_fd_, |
Yifan Hong | f526156 | 2020-03-10 10:28:10 -0700 | [diff] [blame] | 1210 | optimized.src_extents(), |
Alex Deymo | 51c264e | 2016-11-04 15:49:53 -0700 | [diff] [blame] | 1211 | target_fd_, |
Yifan Hong | f526156 | 2020-03-10 10:28:10 -0700 | [diff] [blame] | 1212 | optimized.dst_extents(), |
Alex Deymo | 51c264e | 2016-11-04 15:49:53 -0700 | [diff] [blame] | 1213 | block_size_, |
| 1214 | nullptr)); |
| 1215 | } |
Allie Wood | 9f6f0a5 | 2015-03-30 11:25:47 -0700 | [diff] [blame] | 1216 | return true; |
| 1217 | } |
| 1218 | |
Sen Jiang | 77ab7bd | 2018-05-22 17:24:23 -0700 | [diff] [blame] | 1219 | FileDescriptorPtr DeltaPerformer::ChooseSourceFD( |
| 1220 | const InstallOperation& operation, ErrorCode* error) { |
Hridya Valsaraju | e69ca5f | 2019-02-25 22:33:56 -0800 | [diff] [blame] | 1221 | if (source_fd_ == nullptr) { |
| 1222 | LOG(ERROR) << "ChooseSourceFD fail: source_fd_ == nullptr"; |
| 1223 | return nullptr; |
| 1224 | } |
| 1225 | |
Sen Jiang | 77ab7bd | 2018-05-22 17:24:23 -0700 | [diff] [blame] | 1226 | if (!operation.has_src_sha256_hash()) { |
| 1227 | // When the operation doesn't include a source hash, we attempt the error |
| 1228 | // corrected device first since we can't verify the block in the raw device |
| 1229 | // at this point, but we first need to make sure all extents are readable |
| 1230 | // since the error corrected device can be shorter or not available. |
| 1231 | if (OpenCurrentECCPartition() && |
| 1232 | fd_utils::ReadAndHashExtents( |
| 1233 | source_ecc_fd_, operation.src_extents(), block_size_, nullptr)) { |
| 1234 | return source_ecc_fd_; |
| 1235 | } |
| 1236 | return source_fd_; |
| 1237 | } |
| 1238 | |
| 1239 | brillo::Blob source_hash; |
| 1240 | brillo::Blob expected_source_hash(operation.src_sha256_hash().begin(), |
| 1241 | operation.src_sha256_hash().end()); |
| 1242 | if (fd_utils::ReadAndHashExtents( |
| 1243 | source_fd_, operation.src_extents(), block_size_, &source_hash) && |
| 1244 | source_hash == expected_source_hash) { |
| 1245 | return source_fd_; |
| 1246 | } |
| 1247 | // We fall back to use the error corrected device if the hash of the raw |
| 1248 | // device doesn't match or there was an error reading the source partition. |
| 1249 | if (!OpenCurrentECCPartition()) { |
| 1250 | // The following function call will return false since the source hash |
| 1251 | // mismatches, but we still want to call it so it prints the appropriate |
| 1252 | // log message. |
| 1253 | ValidateSourceHash(source_hash, operation, source_fd_, error); |
| 1254 | return nullptr; |
| 1255 | } |
| 1256 | LOG(WARNING) << "Source hash from RAW device mismatched: found " |
| 1257 | << base::HexEncode(source_hash.data(), source_hash.size()) |
| 1258 | << ", expected " |
| 1259 | << base::HexEncode(expected_source_hash.data(), |
| 1260 | expected_source_hash.size()); |
| 1261 | |
| 1262 | if (fd_utils::ReadAndHashExtents( |
| 1263 | source_ecc_fd_, operation.src_extents(), block_size_, &source_hash) && |
| 1264 | ValidateSourceHash(source_hash, operation, source_ecc_fd_, error)) { |
| 1265 | // At this point reading from the the error corrected device worked, but |
| 1266 | // reading from the raw device failed, so this is considered a recovered |
| 1267 | // failure. |
| 1268 | source_ecc_recovered_failures_++; |
| 1269 | return source_ecc_fd_; |
| 1270 | } |
| 1271 | return nullptr; |
| 1272 | } |
| 1273 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 1274 | bool DeltaPerformer::ExtentsToBsdiffPositionsString( |
| 1275 | const RepeatedPtrField<Extent>& extents, |
| 1276 | uint64_t block_size, |
| 1277 | uint64_t full_length, |
| 1278 | string* positions_string) { |
| 1279 | string ret; |
| 1280 | uint64_t length = 0; |
Chih-Hung Hsieh | 5c6bb1d | 2016-07-27 13:33:15 -0700 | [diff] [blame] | 1281 | for (const Extent& extent : extents) { |
Allie Wood | 5687345 | 2015-03-27 17:48:40 -0700 | [diff] [blame] | 1282 | int64_t start = extent.start_block() * block_size; |
Tamas Berghammer | 9d66d76 | 2016-07-15 14:19:04 +0100 | [diff] [blame] | 1283 | uint64_t this_length = |
| 1284 | min(full_length - length, |
| 1285 | static_cast<uint64_t>(extent.num_blocks()) * block_size); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 1286 | ret += base::StringPrintf("%" PRIi64 ":%" PRIu64 ",", start, this_length); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 1287 | length += this_length; |
| 1288 | } |
| 1289 | TEST_AND_RETURN_FALSE(length == full_length); |
| 1290 | if (!ret.empty()) |
| 1291 | ret.resize(ret.size() - 1); // Strip trailing comma off |
| 1292 | *positions_string = ret; |
| 1293 | return true; |
| 1294 | } |
| 1295 | |
Amin Hassani | d9cb290 | 2017-09-20 12:11:39 -0700 | [diff] [blame] | 1296 | namespace { |
| 1297 | |
| 1298 | class BsdiffExtentFile : public bsdiff::FileInterface { |
| 1299 | public: |
| 1300 | BsdiffExtentFile(std::unique_ptr<ExtentReader> reader, size_t size) |
| 1301 | : BsdiffExtentFile(std::move(reader), nullptr, size) {} |
| 1302 | BsdiffExtentFile(std::unique_ptr<ExtentWriter> writer, size_t size) |
| 1303 | : BsdiffExtentFile(nullptr, std::move(writer), size) {} |
| 1304 | |
| 1305 | ~BsdiffExtentFile() override = default; |
| 1306 | |
| 1307 | bool Read(void* buf, size_t count, size_t* bytes_read) override { |
| 1308 | TEST_AND_RETURN_FALSE(reader_->Read(buf, count)); |
| 1309 | *bytes_read = count; |
| 1310 | offset_ += count; |
| 1311 | return true; |
| 1312 | } |
| 1313 | |
| 1314 | bool Write(const void* buf, size_t count, size_t* bytes_written) override { |
| 1315 | TEST_AND_RETURN_FALSE(writer_->Write(buf, count)); |
| 1316 | *bytes_written = count; |
| 1317 | offset_ += count; |
| 1318 | return true; |
| 1319 | } |
| 1320 | |
| 1321 | bool Seek(off_t pos) override { |
| 1322 | if (reader_ != nullptr) { |
| 1323 | TEST_AND_RETURN_FALSE(reader_->Seek(pos)); |
| 1324 | offset_ = pos; |
| 1325 | } else { |
| 1326 | // For writes technically there should be no change of position, or it |
| 1327 | // should be equivalent of current offset. |
| 1328 | TEST_AND_RETURN_FALSE(offset_ == static_cast<uint64_t>(pos)); |
| 1329 | } |
| 1330 | return true; |
| 1331 | } |
| 1332 | |
Sen Jiang | 5e1af98 | 2018-11-01 15:01:45 -0700 | [diff] [blame] | 1333 | bool Close() override { return true; } |
Amin Hassani | d9cb290 | 2017-09-20 12:11:39 -0700 | [diff] [blame] | 1334 | |
| 1335 | bool GetSize(uint64_t* size) override { |
| 1336 | *size = size_; |
| 1337 | return true; |
| 1338 | } |
| 1339 | |
| 1340 | private: |
| 1341 | BsdiffExtentFile(std::unique_ptr<ExtentReader> reader, |
| 1342 | std::unique_ptr<ExtentWriter> writer, |
| 1343 | size_t size) |
| 1344 | : reader_(std::move(reader)), |
| 1345 | writer_(std::move(writer)), |
| 1346 | size_(size), |
| 1347 | offset_(0) {} |
| 1348 | |
| 1349 | std::unique_ptr<ExtentReader> reader_; |
| 1350 | std::unique_ptr<ExtentWriter> writer_; |
| 1351 | uint64_t size_; |
| 1352 | uint64_t offset_; |
| 1353 | |
| 1354 | DISALLOW_COPY_AND_ASSIGN(BsdiffExtentFile); |
| 1355 | }; |
| 1356 | |
| 1357 | } // namespace |
| 1358 | |
Allie Wood | 9f6f0a5 | 2015-03-30 11:25:47 -0700 | [diff] [blame] | 1359 | bool DeltaPerformer::PerformSourceBsdiffOperation( |
Sen Jiang | be2c47b | 2016-06-15 14:09:27 -0700 | [diff] [blame] | 1360 | const InstallOperation& operation, ErrorCode* error) { |
Allie Wood | 9f6f0a5 | 2015-03-30 11:25:47 -0700 | [diff] [blame] | 1361 | // Since we delete data off the beginning of the buffer as we use it, |
| 1362 | // the data we need should be exactly at the beginning of the buffer. |
| 1363 | TEST_AND_RETURN_FALSE(buffer_offset_ == operation.data_offset()); |
| 1364 | TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length()); |
| 1365 | if (operation.has_src_length()) |
| 1366 | TEST_AND_RETURN_FALSE(operation.src_length() % block_size_ == 0); |
| 1367 | if (operation.has_dst_length()) |
| 1368 | TEST_AND_RETURN_FALSE(operation.dst_length() % block_size_ == 0); |
| 1369 | |
Sen Jiang | 77ab7bd | 2018-05-22 17:24:23 -0700 | [diff] [blame] | 1370 | FileDescriptorPtr source_fd = ChooseSourceFD(operation, error); |
| 1371 | TEST_AND_RETURN_FALSE(source_fd != nullptr); |
Sen Jiang | 2ec4aab | 2015-11-13 15:04:03 -0800 | [diff] [blame] | 1372 | |
Amin Hassani | d9cb290 | 2017-09-20 12:11:39 -0700 | [diff] [blame] | 1373 | auto reader = std::make_unique<DirectExtentReader>(); |
| 1374 | TEST_AND_RETURN_FALSE( |
Sen Jiang | 77ab7bd | 2018-05-22 17:24:23 -0700 | [diff] [blame] | 1375 | reader->Init(source_fd, operation.src_extents(), block_size_)); |
Amin Hassani | d9cb290 | 2017-09-20 12:11:39 -0700 | [diff] [blame] | 1376 | auto src_file = std::make_unique<BsdiffExtentFile>( |
| 1377 | std::move(reader), |
| 1378 | utils::BlocksInExtents(operation.src_extents()) * block_size_); |
Allie Wood | 9f6f0a5 | 2015-03-30 11:25:47 -0700 | [diff] [blame] | 1379 | |
Amin Hassani | d9cb290 | 2017-09-20 12:11:39 -0700 | [diff] [blame] | 1380 | auto writer = std::make_unique<DirectExtentWriter>(); |
| 1381 | TEST_AND_RETURN_FALSE( |
| 1382 | writer->Init(target_fd_, operation.dst_extents(), block_size_)); |
| 1383 | auto dst_file = std::make_unique<BsdiffExtentFile>( |
| 1384 | std::move(writer), |
| 1385 | utils::BlocksInExtents(operation.dst_extents()) * block_size_); |
| 1386 | |
| 1387 | TEST_AND_RETURN_FALSE(bsdiff::bspatch(std::move(src_file), |
| 1388 | std::move(dst_file), |
Sen Jiang | 7a65909 | 2016-05-10 15:31:46 -0700 | [diff] [blame] | 1389 | buffer_.data(), |
Amin Hassani | d9cb290 | 2017-09-20 12:11:39 -0700 | [diff] [blame] | 1390 | buffer_.size()) == 0); |
Sen Jiang | bc3e6b0 | 2016-01-19 18:39:26 +0800 | [diff] [blame] | 1391 | DiscardBuffer(true, buffer_.size()); |
| 1392 | return true; |
| 1393 | } |
| 1394 | |
Amin Hassani | 02855c2 | 2017-09-06 22:34:50 -0700 | [diff] [blame] | 1395 | namespace { |
| 1396 | |
| 1397 | // A class to be passed to |puffpatch| for reading from |source_fd_| and writing |
| 1398 | // into |target_fd_|. |
| 1399 | class PuffinExtentStream : public puffin::StreamInterface { |
| 1400 | public: |
| 1401 | // Constructor for creating a stream for reading from an |ExtentReader|. |
Amin Hassani | 678a68f | 2018-02-28 11:54:47 -0800 | [diff] [blame] | 1402 | PuffinExtentStream(std::unique_ptr<ExtentReader> reader, uint64_t size) |
Amin Hassani | 02855c2 | 2017-09-06 22:34:50 -0700 | [diff] [blame] | 1403 | : PuffinExtentStream(std::move(reader), nullptr, size) {} |
| 1404 | |
| 1405 | // Constructor for creating a stream for writing to an |ExtentWriter|. |
Amin Hassani | 678a68f | 2018-02-28 11:54:47 -0800 | [diff] [blame] | 1406 | PuffinExtentStream(std::unique_ptr<ExtentWriter> writer, uint64_t size) |
Amin Hassani | 02855c2 | 2017-09-06 22:34:50 -0700 | [diff] [blame] | 1407 | : PuffinExtentStream(nullptr, std::move(writer), size) {} |
| 1408 | |
| 1409 | ~PuffinExtentStream() override = default; |
| 1410 | |
Amin Hassani | 678a68f | 2018-02-28 11:54:47 -0800 | [diff] [blame] | 1411 | bool GetSize(uint64_t* size) const override { |
Amin Hassani | 02855c2 | 2017-09-06 22:34:50 -0700 | [diff] [blame] | 1412 | *size = size_; |
| 1413 | return true; |
| 1414 | } |
| 1415 | |
Amin Hassani | 678a68f | 2018-02-28 11:54:47 -0800 | [diff] [blame] | 1416 | bool GetOffset(uint64_t* offset) const override { |
Amin Hassani | 02855c2 | 2017-09-06 22:34:50 -0700 | [diff] [blame] | 1417 | *offset = offset_; |
| 1418 | return true; |
| 1419 | } |
| 1420 | |
Amin Hassani | 678a68f | 2018-02-28 11:54:47 -0800 | [diff] [blame] | 1421 | bool Seek(uint64_t offset) override { |
Amin Hassani | 02855c2 | 2017-09-06 22:34:50 -0700 | [diff] [blame] | 1422 | if (is_read_) { |
| 1423 | TEST_AND_RETURN_FALSE(reader_->Seek(offset)); |
| 1424 | offset_ = offset; |
| 1425 | } else { |
| 1426 | // For writes technically there should be no change of position, or it |
| 1427 | // should equivalent of current offset. |
| 1428 | TEST_AND_RETURN_FALSE(offset_ == offset); |
| 1429 | } |
| 1430 | return true; |
| 1431 | } |
| 1432 | |
| 1433 | bool Read(void* buffer, size_t count) override { |
| 1434 | TEST_AND_RETURN_FALSE(is_read_); |
| 1435 | TEST_AND_RETURN_FALSE(reader_->Read(buffer, count)); |
| 1436 | offset_ += count; |
| 1437 | return true; |
| 1438 | } |
| 1439 | |
| 1440 | bool Write(const void* buffer, size_t count) override { |
| 1441 | TEST_AND_RETURN_FALSE(!is_read_); |
| 1442 | TEST_AND_RETURN_FALSE(writer_->Write(buffer, count)); |
| 1443 | offset_ += count; |
| 1444 | return true; |
| 1445 | } |
| 1446 | |
Sen Jiang | 5e1af98 | 2018-11-01 15:01:45 -0700 | [diff] [blame] | 1447 | bool Close() override { return true; } |
Amin Hassani | 02855c2 | 2017-09-06 22:34:50 -0700 | [diff] [blame] | 1448 | |
| 1449 | private: |
| 1450 | PuffinExtentStream(std::unique_ptr<ExtentReader> reader, |
| 1451 | std::unique_ptr<ExtentWriter> writer, |
Amin Hassani | 678a68f | 2018-02-28 11:54:47 -0800 | [diff] [blame] | 1452 | uint64_t size) |
Amin Hassani | 02855c2 | 2017-09-06 22:34:50 -0700 | [diff] [blame] | 1453 | : reader_(std::move(reader)), |
| 1454 | writer_(std::move(writer)), |
| 1455 | size_(size), |
| 1456 | offset_(0), |
| 1457 | is_read_(reader_ ? true : false) {} |
| 1458 | |
| 1459 | std::unique_ptr<ExtentReader> reader_; |
| 1460 | std::unique_ptr<ExtentWriter> writer_; |
| 1461 | uint64_t size_; |
| 1462 | uint64_t offset_; |
| 1463 | bool is_read_; |
| 1464 | |
| 1465 | DISALLOW_COPY_AND_ASSIGN(PuffinExtentStream); |
| 1466 | }; |
| 1467 | |
| 1468 | } // namespace |
| 1469 | |
| 1470 | bool DeltaPerformer::PerformPuffDiffOperation(const InstallOperation& operation, |
| 1471 | ErrorCode* error) { |
| 1472 | // Since we delete data off the beginning of the buffer as we use it, |
| 1473 | // the data we need should be exactly at the beginning of the buffer. |
| 1474 | TEST_AND_RETURN_FALSE(buffer_offset_ == operation.data_offset()); |
| 1475 | TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length()); |
| 1476 | |
Sen Jiang | 77ab7bd | 2018-05-22 17:24:23 -0700 | [diff] [blame] | 1477 | FileDescriptorPtr source_fd = ChooseSourceFD(operation, error); |
| 1478 | TEST_AND_RETURN_FALSE(source_fd != nullptr); |
Amin Hassani | 02855c2 | 2017-09-06 22:34:50 -0700 | [diff] [blame] | 1479 | |
| 1480 | auto reader = std::make_unique<DirectExtentReader>(); |
| 1481 | TEST_AND_RETURN_FALSE( |
Sen Jiang | 77ab7bd | 2018-05-22 17:24:23 -0700 | [diff] [blame] | 1482 | reader->Init(source_fd, operation.src_extents(), block_size_)); |
Amin Hassani | 02855c2 | 2017-09-06 22:34:50 -0700 | [diff] [blame] | 1483 | puffin::UniqueStreamPtr src_stream(new PuffinExtentStream( |
| 1484 | std::move(reader), |
| 1485 | utils::BlocksInExtents(operation.src_extents()) * block_size_)); |
| 1486 | |
| 1487 | auto writer = std::make_unique<DirectExtentWriter>(); |
| 1488 | TEST_AND_RETURN_FALSE( |
| 1489 | writer->Init(target_fd_, operation.dst_extents(), block_size_)); |
| 1490 | puffin::UniqueStreamPtr dst_stream(new PuffinExtentStream( |
| 1491 | std::move(writer), |
| 1492 | utils::BlocksInExtents(operation.dst_extents()) * block_size_)); |
| 1493 | |
| 1494 | const size_t kMaxCacheSize = 5 * 1024 * 1024; // Total 5MB cache. |
| 1495 | TEST_AND_RETURN_FALSE(puffin::PuffPatch(std::move(src_stream), |
| 1496 | std::move(dst_stream), |
| 1497 | buffer_.data(), |
| 1498 | buffer_.size(), |
| 1499 | kMaxCacheSize)); |
Allie Wood | 9f6f0a5 | 2015-03-30 11:25:47 -0700 | [diff] [blame] | 1500 | DiscardBuffer(true, buffer_.size()); |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 1501 | return true; |
| 1502 | } |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 1503 | |
Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 1504 | bool DeltaPerformer::ExtractSignatureMessage() { |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 1505 | TEST_AND_RETURN_FALSE(signatures_message_data_.empty()); |
| 1506 | TEST_AND_RETURN_FALSE(buffer_offset_ == manifest_.signatures_offset()); |
| 1507 | TEST_AND_RETURN_FALSE(buffer_.size() >= manifest_.signatures_size()); |
Darin Petkov | 4f0a07b | 2011-05-25 16:47:20 -0700 | [diff] [blame] | 1508 | signatures_message_data_.assign( |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 1509 | buffer_.begin(), buffer_.begin() + manifest_.signatures_size()); |
Darin Petkov | 4f0a07b | 2011-05-25 16:47:20 -0700 | [diff] [blame] | 1510 | |
| 1511 | // Save the signature blob because if the update is interrupted after the |
| 1512 | // download phase we don't go through this path anymore. Some alternatives to |
| 1513 | // consider: |
| 1514 | // |
| 1515 | // 1. On resume, re-download the signature blob from the server and re-verify |
| 1516 | // it. |
| 1517 | // |
| 1518 | // 2. Verify the signature as soon as it's received and don't checkpoint the |
| 1519 | // blob and the signed sha-256 context. |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 1520 | LOG_IF(WARNING, |
| 1521 | !prefs_->SetString(kPrefsUpdateStateSignatureBlob, |
Sen Jiang | 9b2f178 | 2019-01-24 14:27:50 -0800 | [diff] [blame] | 1522 | signatures_message_data_)) |
Darin Petkov | 4f0a07b | 2011-05-25 16:47:20 -0700 | [diff] [blame] | 1523 | << "Unable to store the signature blob."; |
Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 1524 | |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 1525 | LOG(INFO) << "Extracted signature data of size " |
| 1526 | << manifest_.signatures_size() << " at " |
| 1527 | << manifest_.signatures_offset(); |
| 1528 | return true; |
| 1529 | } |
| 1530 | |
Sen Jiang | 08c6da1 | 2019-01-07 18:28:56 -0800 | [diff] [blame] | 1531 | bool DeltaPerformer::GetPublicKey(string* out_public_key) { |
| 1532 | out_public_key->clear(); |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 1533 | |
Sen Jiang | 08c6da1 | 2019-01-07 18:28:56 -0800 | [diff] [blame] | 1534 | if (utils::FileExists(public_key_path_.c_str())) { |
| 1535 | LOG(INFO) << "Verifying using public key: " << public_key_path_; |
| 1536 | return utils::ReadFile(public_key_path_, out_public_key); |
| 1537 | } |
| 1538 | |
| 1539 | // If this is an official build then we are not allowed to use public key from |
| 1540 | // Omaha response. |
| 1541 | if (!hardware_->IsOfficialBuild() && !install_plan_->public_key_rsa.empty()) { |
| 1542 | LOG(INFO) << "Verifying using public key from Omaha response."; |
| 1543 | return brillo::data_encoding::Base64Decode(install_plan_->public_key_rsa, |
| 1544 | out_public_key); |
| 1545 | } |
Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 1546 | LOG(INFO) << "No public keys found for verification."; |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 1547 | return true; |
| 1548 | } |
| 1549 | |
Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 1550 | std::pair<std::unique_ptr<PayloadVerifier>, bool> |
| 1551 | DeltaPerformer::CreatePayloadVerifier() { |
| 1552 | if (utils::FileExists(update_certificates_path_.c_str())) { |
| 1553 | LOG(INFO) << "Verifying using certificates: " << update_certificates_path_; |
| 1554 | return { |
| 1555 | PayloadVerifier::CreateInstanceFromZipPath(update_certificates_path_), |
| 1556 | true}; |
| 1557 | } |
| 1558 | |
| 1559 | string public_key; |
| 1560 | if (!GetPublicKey(&public_key)) { |
| 1561 | LOG(ERROR) << "Failed to read public key"; |
| 1562 | return {nullptr, true}; |
| 1563 | } |
| 1564 | |
| 1565 | // Skips the verification if the public key is empty. |
| 1566 | if (public_key.empty()) { |
| 1567 | return {nullptr, false}; |
| 1568 | } |
| 1569 | return {PayloadVerifier::CreateInstance(public_key), true}; |
| 1570 | } |
| 1571 | |
Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 1572 | ErrorCode DeltaPerformer::ValidateManifest() { |
Saint Chou | 0a92a62 | 2020-07-29 14:25:35 +0000 | [diff] [blame^] | 1573 | // Perform assorted checks to validation check the manifest, make sure it |
Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 1574 | // matches data from other sources, and that it is a supported version. |
Amin Hassani | 822d485 | 2020-03-10 01:50:42 +0000 | [diff] [blame] | 1575 | bool has_old_fields = std::any_of(manifest_.partitions().begin(), |
| 1576 | manifest_.partitions().end(), |
| 1577 | [](const PartitionUpdate& partition) { |
| 1578 | return partition.has_old_partition_info(); |
| 1579 | }); |
Sen Jiang | c8f6b7a | 2015-10-21 11:09:59 -0700 | [diff] [blame] | 1580 | |
Alex Deymo | 64d9878 | 2016-02-05 18:03:48 -0800 | [diff] [blame] | 1581 | // 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] | 1582 | // update. Also, always treat the partial update as delta so that we can |
| 1583 | // perform the minor version check correctly. |
Alex Deymo | 64d9878 | 2016-02-05 18:03:48 -0800 | [diff] [blame] | 1584 | InstallPayloadType actual_payload_type = |
Tianjie | f5baff4 | 2020-07-17 21:43:22 -0700 | [diff] [blame] | 1585 | (has_old_fields || manifest_.partial_update()) |
| 1586 | ? InstallPayloadType::kDelta |
| 1587 | : InstallPayloadType::kFull; |
Alex Deymo | 64d9878 | 2016-02-05 18:03:48 -0800 | [diff] [blame] | 1588 | |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 1589 | if (payload_->type == InstallPayloadType::kUnknown) { |
Alex Deymo | 64d9878 | 2016-02-05 18:03:48 -0800 | [diff] [blame] | 1590 | LOG(INFO) << "Detected a '" |
| 1591 | << InstallPayloadTypeToString(actual_payload_type) |
| 1592 | << "' payload."; |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 1593 | payload_->type = actual_payload_type; |
| 1594 | } else if (payload_->type != actual_payload_type) { |
Alex Deymo | 64d9878 | 2016-02-05 18:03:48 -0800 | [diff] [blame] | 1595 | LOG(ERROR) << "InstallPlan expected a '" |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 1596 | << InstallPayloadTypeToString(payload_->type) |
Alex Deymo | 64d9878 | 2016-02-05 18:03:48 -0800 | [diff] [blame] | 1597 | << "' payload but the downloaded manifest contains a '" |
| 1598 | << InstallPayloadTypeToString(actual_payload_type) |
| 1599 | << "' payload."; |
| 1600 | return ErrorCode::kPayloadMismatchedType; |
| 1601 | } |
Alex Deymo | 64d9878 | 2016-02-05 18:03:48 -0800 | [diff] [blame] | 1602 | // Check that the minor version is compatible. |
Tianjie | d3865d1 | 2020-06-03 15:25:17 -0700 | [diff] [blame] | 1603 | // TODO(xunchang) increment minor version & add check for partial update |
Alex Deymo | 64d9878 | 2016-02-05 18:03:48 -0800 | [diff] [blame] | 1604 | if (actual_payload_type == InstallPayloadType::kFull) { |
Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 1605 | if (manifest_.minor_version() != kFullPayloadMinorVersion) { |
| 1606 | LOG(ERROR) << "Manifest contains minor version " |
| 1607 | << manifest_.minor_version() |
| 1608 | << ", but all full payloads should have version " |
| 1609 | << kFullPayloadMinorVersion << "."; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1610 | return ErrorCode::kUnsupportedMinorPayloadVersion; |
Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 1611 | } |
| 1612 | } else { |
Sen Jiang | f123663 | 2018-05-11 16:03:23 -0700 | [diff] [blame] | 1613 | if (manifest_.minor_version() < kMinSupportedMinorPayloadVersion || |
| 1614 | manifest_.minor_version() > kMaxSupportedMinorPayloadVersion) { |
Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 1615 | LOG(ERROR) << "Manifest contains minor version " |
| 1616 | << manifest_.minor_version() |
Sen Jiang | f123663 | 2018-05-11 16:03:23 -0700 | [diff] [blame] | 1617 | << " not in the range of supported minor versions [" |
| 1618 | << kMinSupportedMinorPayloadVersion << ", " |
| 1619 | << kMaxSupportedMinorPayloadVersion << "]."; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1620 | return ErrorCode::kUnsupportedMinorPayloadVersion; |
Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 1621 | } |
Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 1622 | } |
| 1623 | |
Amin Hassani | 822d485 | 2020-03-10 01:50:42 +0000 | [diff] [blame] | 1624 | if (manifest_.has_old_rootfs_info() || manifest_.has_new_rootfs_info() || |
| 1625 | manifest_.has_old_kernel_info() || manifest_.has_new_kernel_info() || |
| 1626 | manifest_.install_operations_size() != 0 || |
| 1627 | manifest_.kernel_install_operations_size() != 0) { |
| 1628 | LOG(ERROR) << "Manifest contains deprecated fields."; |
| 1629 | return ErrorCode::kPayloadMismatchedType; |
Sen Jiang | 3e728fe | 2015-11-05 11:37:23 -0800 | [diff] [blame] | 1630 | } |
| 1631 | |
Sen Jiang | 8e768e9 | 2017-06-28 17:13:19 -0700 | [diff] [blame] | 1632 | if (manifest_.max_timestamp() < hardware_->GetBuildTimestamp()) { |
| 1633 | LOG(ERROR) << "The current OS build timestamp (" |
| 1634 | << hardware_->GetBuildTimestamp() |
| 1635 | << ") is newer than the maximum timestamp in the manifest (" |
| 1636 | << manifest_.max_timestamp() << ")"; |
Tianjie Xu | 4ad3af6 | 2019-10-30 11:59:45 -0700 | [diff] [blame] | 1637 | if (!hardware_->AllowDowngrade()) { |
| 1638 | return ErrorCode::kPayloadTimestampError; |
| 1639 | } |
| 1640 | LOG(INFO) << "The current OS build allows downgrade, continuing to apply" |
| 1641 | " the payload with an older timestamp."; |
Sen Jiang | 8e768e9 | 2017-06-28 17:13:19 -0700 | [diff] [blame] | 1642 | } |
| 1643 | |
Amin Hassani | 55c7541 | 2019-10-07 11:20:39 -0700 | [diff] [blame] | 1644 | // TODO(crbug.com/37661) we should be adding more and more manifest checks, |
| 1645 | // such as partition boundaries, etc. |
Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 1646 | |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1647 | return ErrorCode::kSuccess; |
Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 1648 | } |
| 1649 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1650 | ErrorCode DeltaPerformer::ValidateOperationHash( |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 1651 | const InstallOperation& operation) { |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1652 | if (!operation.data_sha256_hash().size()) { |
| 1653 | if (!operation.data_length()) { |
| 1654 | // Operations that do not have any data blob won't have any operation hash |
| 1655 | // either. So, these operations are always considered validated since the |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 1656 | // metadata that contains all the non-data-blob portions of the operation |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 1657 | // has already been validated. This is true for both HTTP and HTTPS cases. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1658 | return ErrorCode::kSuccess; |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1659 | } |
| 1660 | |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 1661 | // No hash is present for an operation that has data blobs. This shouldn't |
| 1662 | // happen normally for any client that has this code, because the |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1663 | // corresponding update should have been produced with the operation |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 1664 | // hashes. So if it happens it means either we've turned operation hash |
| 1665 | // generation off in DeltaDiffGenerator or it's a regression of some sort. |
Saint Chou | 0a92a62 | 2020-07-29 14:25:35 +0000 | [diff] [blame^] | 1666 | // One caveat though: The last operation is a unused signature operation |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1667 | // that doesn't have a hash at the time the manifest is created. So we |
| 1668 | // should not complaint about that operation. This operation can be |
| 1669 | // recognized by the fact that it's offset is mentioned in the manifest. |
| 1670 | if (manifest_.signatures_offset() && |
| 1671 | manifest_.signatures_offset() == operation.data_offset()) { |
| 1672 | LOG(INFO) << "Skipping hash verification for signature operation " |
| 1673 | << next_operation_num_ + 1; |
| 1674 | } else { |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 1675 | if (install_plan_->hash_checks_mandatory) { |
| 1676 | LOG(ERROR) << "Missing mandatory operation hash for operation " |
| 1677 | << next_operation_num_ + 1; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1678 | return ErrorCode::kDownloadOperationHashMissingError; |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 1679 | } |
| 1680 | |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 1681 | LOG(WARNING) << "Cannot validate operation " << next_operation_num_ + 1 |
| 1682 | << " as there's no operation hash in manifest"; |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1683 | } |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1684 | return ErrorCode::kSuccess; |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1685 | } |
| 1686 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1687 | brillo::Blob expected_op_hash; |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1688 | expected_op_hash.assign(operation.data_sha256_hash().data(), |
| 1689 | (operation.data_sha256_hash().data() + |
| 1690 | operation.data_sha256_hash().size())); |
| 1691 | |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 1692 | brillo::Blob calculated_op_hash; |
| 1693 | if (!HashCalculator::RawHashOfBytes( |
| 1694 | buffer_.data(), operation.data_length(), &calculated_op_hash)) { |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1695 | LOG(ERROR) << "Unable to compute actual hash of operation " |
| 1696 | << next_operation_num_; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1697 | return ErrorCode::kDownloadOperationHashVerificationError; |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1698 | } |
| 1699 | |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1700 | if (calculated_op_hash != expected_op_hash) { |
| 1701 | LOG(ERROR) << "Hash verification failed for operation " |
| 1702 | << next_operation_num_ << ". Expected hash = "; |
| 1703 | utils::HexDumpVector(expected_op_hash); |
| 1704 | LOG(ERROR) << "Calculated hash over " << operation.data_length() |
| 1705 | << " bytes at offset: " << operation.data_offset() << " = "; |
| 1706 | utils::HexDumpVector(calculated_op_hash); |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1707 | return ErrorCode::kDownloadOperationHashMismatch; |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1708 | } |
| 1709 | |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1710 | return ErrorCode::kSuccess; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 1711 | } |
| 1712 | |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 1713 | #define TEST_AND_RETURN_VAL(_retval, _condition) \ |
| 1714 | do { \ |
| 1715 | if (!(_condition)) { \ |
| 1716 | LOG(ERROR) << "VerifyPayload failure: " << #_condition; \ |
| 1717 | return _retval; \ |
| 1718 | } \ |
Andrew de los Reyes | 771e1bd | 2011-08-30 14:47:23 -0700 | [diff] [blame] | 1719 | } while (0); |
Andrew de los Reyes | fb830ba | 2011-04-04 11:42:43 -0700 | [diff] [blame] | 1720 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1721 | ErrorCode DeltaPerformer::VerifyPayload( |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 1722 | const brillo::Blob& update_check_response_hash, |
Andrew de los Reyes | 771e1bd | 2011-08-30 14:47:23 -0700 | [diff] [blame] | 1723 | const uint64_t update_check_response_size) { |
Jay Srinivasan | 0d8fb40 | 2012-05-07 19:19:38 -0700 | [diff] [blame] | 1724 | // Verifies the download size. |
Sen Jiang | 3a25dc2 | 2019-01-10 14:14:41 -0800 | [diff] [blame] | 1725 | if (update_check_response_size != |
| 1726 | metadata_size_ + metadata_signature_size_ + buffer_offset_) { |
| 1727 | LOG(ERROR) << "update_check_response_size (" << update_check_response_size |
| 1728 | << ") doesn't match metadata_size (" << metadata_size_ |
| 1729 | << ") + metadata_signature_size (" << metadata_signature_size_ |
| 1730 | << ") + buffer_offset (" << buffer_offset_ << ")."; |
| 1731 | return ErrorCode::kPayloadSizeMismatchError; |
| 1732 | } |
Jay Srinivasan | 0d8fb40 | 2012-05-07 19:19:38 -0700 | [diff] [blame] | 1733 | |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 1734 | // Verifies the payload hash. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1735 | TEST_AND_RETURN_VAL(ErrorCode::kDownloadPayloadVerificationError, |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 1736 | !payload_hash_calculator_.raw_hash().empty()); |
| 1737 | TEST_AND_RETURN_VAL( |
| 1738 | ErrorCode::kPayloadHashMismatchError, |
| 1739 | payload_hash_calculator_.raw_hash() == update_check_response_hash); |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 1740 | |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1741 | TEST_AND_RETURN_VAL(ErrorCode::kSignedDeltaPayloadExpectedError, |
Andrew de los Reyes | 771e1bd | 2011-08-30 14:47:23 -0700 | [diff] [blame] | 1742 | !signatures_message_data_.empty()); |
Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 1743 | brillo::Blob hash_data = signed_hash_calculator_.raw_hash(); |
Alex Deymo | b552a68 | 2015-09-30 09:36:49 -0700 | [diff] [blame] | 1744 | TEST_AND_RETURN_VAL(ErrorCode::kDownloadPayloadPubKeyVerificationError, |
xunchang | cda3c03 | 2019-03-26 15:41:14 -0700 | [diff] [blame] | 1745 | hash_data.size() == kSHA256Size); |
Alex Deymo | b552a68 | 2015-09-30 09:36:49 -0700 | [diff] [blame] | 1746 | |
Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 1747 | auto [payload_verifier, perform_verification] = CreatePayloadVerifier(); |
| 1748 | if (!perform_verification) { |
| 1749 | LOG(WARNING) << "Not verifying signed delta payload -- missing public key."; |
| 1750 | return ErrorCode::kSuccess; |
| 1751 | } |
Tianjie Xu | 6cf830b | 2019-09-30 11:31:49 -0700 | [diff] [blame] | 1752 | if (!payload_verifier) { |
Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 1753 | LOG(ERROR) << "Failed to create the payload verifier."; |
Tianjie Xu | 6cf830b | 2019-09-30 11:31:49 -0700 | [diff] [blame] | 1754 | return ErrorCode::kDownloadPayloadPubKeyVerificationError; |
| 1755 | } |
| 1756 | if (!payload_verifier->VerifySignature(signatures_message_data_, hash_data)) { |
David Zeuthen | bc27aac | 2013-11-26 11:17:48 -0800 | [diff] [blame] | 1757 | // The autoupdate_CatchBadSignatures test checks for this string |
| 1758 | // in log-files. Keep in sync. |
Alex Deymo | b552a68 | 2015-09-30 09:36:49 -0700 | [diff] [blame] | 1759 | LOG(ERROR) << "Public key verification failed, thus update failed."; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1760 | return ErrorCode::kDownloadPayloadPubKeyVerificationError; |
Andrew de los Reyes | fb830ba | 2011-04-04 11:42:43 -0700 | [diff] [blame] | 1761 | } |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 1762 | |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 1763 | LOG(INFO) << "Payload hash matches value in payload."; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1764 | return ErrorCode::kSuccess; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 1765 | } |
| 1766 | |
Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 1767 | void DeltaPerformer::DiscardBuffer(bool do_advance_offset, |
| 1768 | size_t signed_hash_buffer_size) { |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 1769 | // Update the buffer offset. |
Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 1770 | if (do_advance_offset) |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 1771 | buffer_offset_ += buffer_.size(); |
| 1772 | |
| 1773 | // Hash the content. |
Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 1774 | payload_hash_calculator_.Update(buffer_.data(), buffer_.size()); |
| 1775 | signed_hash_calculator_.Update(buffer_.data(), signed_hash_buffer_size); |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 1776 | |
| 1777 | // 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] | 1778 | brillo::Blob().swap(buffer_); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 1779 | } |
| 1780 | |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1781 | bool DeltaPerformer::CanResumeUpdate(PrefsInterface* prefs, |
Chih-Hung Hsieh | 5c6bb1d | 2016-07-27 13:33:15 -0700 | [diff] [blame] | 1782 | const string& update_check_response_hash) { |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1783 | int64_t next_operation = kUpdateStateOperationInvalid; |
Alex Deymo | 3310b22 | 2015-03-30 15:59:07 -0700 | [diff] [blame] | 1784 | if (!(prefs->GetInt64(kPrefsUpdateStateNextOperation, &next_operation) && |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 1785 | next_operation != kUpdateStateOperationInvalid && next_operation > 0)) |
Alex Deymo | 3310b22 | 2015-03-30 15:59:07 -0700 | [diff] [blame] | 1786 | return false; |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1787 | |
| 1788 | string interrupted_hash; |
Alex Deymo | 3310b22 | 2015-03-30 15:59:07 -0700 | [diff] [blame] | 1789 | if (!(prefs->GetString(kPrefsUpdateCheckResponseHash, &interrupted_hash) && |
| 1790 | !interrupted_hash.empty() && |
| 1791 | interrupted_hash == update_check_response_hash)) |
| 1792 | return false; |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1793 | |
Darin Petkov | 6142614 | 2010-10-08 11:04:55 -0700 | [diff] [blame] | 1794 | int64_t resumed_update_failures; |
Alex Deymo | f25eb49 | 2016-02-26 00:20:08 -0800 | [diff] [blame] | 1795 | // Note that storing this value is optional, but if it is there it should not |
| 1796 | // be more than the limit. |
| 1797 | if (prefs->GetInt64(kPrefsResumedUpdateFailures, &resumed_update_failures) && |
| 1798 | resumed_update_failures > kMaxResumedUpdateFailures) |
Alex Deymo | 3310b22 | 2015-03-30 15:59:07 -0700 | [diff] [blame] | 1799 | return false; |
Darin Petkov | 6142614 | 2010-10-08 11:04:55 -0700 | [diff] [blame] | 1800 | |
Saint Chou | 0a92a62 | 2020-07-29 14:25:35 +0000 | [diff] [blame^] | 1801 | // Validation check the rest. |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1802 | int64_t next_data_offset = -1; |
Alex Deymo | 3310b22 | 2015-03-30 15:59:07 -0700 | [diff] [blame] | 1803 | if (!(prefs->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset) && |
| 1804 | next_data_offset >= 0)) |
| 1805 | return false; |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1806 | |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 1807 | string sha256_context; |
Alex Deymo | 3310b22 | 2015-03-30 15:59:07 -0700 | [diff] [blame] | 1808 | if (!(prefs->GetString(kPrefsUpdateStateSHA256Context, &sha256_context) && |
| 1809 | !sha256_context.empty())) |
| 1810 | return false; |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1811 | |
| 1812 | int64_t manifest_metadata_size = 0; |
Alex Deymo | 3310b22 | 2015-03-30 15:59:07 -0700 | [diff] [blame] | 1813 | if (!(prefs->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size) && |
| 1814 | manifest_metadata_size > 0)) |
| 1815 | return false; |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1816 | |
Alex Deymo | f25eb49 | 2016-02-26 00:20:08 -0800 | [diff] [blame] | 1817 | int64_t manifest_signature_size = 0; |
| 1818 | if (!(prefs->GetInt64(kPrefsManifestSignatureSize, |
| 1819 | &manifest_signature_size) && |
| 1820 | manifest_signature_size >= 0)) |
| 1821 | return false; |
| 1822 | |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1823 | return true; |
| 1824 | } |
| 1825 | |
Yifan Hong | 55c2bfe | 2020-01-13 17:01:19 -0800 | [diff] [blame] | 1826 | bool DeltaPerformer::ResetUpdateProgress( |
| 1827 | PrefsInterface* prefs, |
| 1828 | bool quick, |
| 1829 | bool skip_dynamic_partititon_metadata_updated) { |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1830 | TEST_AND_RETURN_FALSE(prefs->SetInt64(kPrefsUpdateStateNextOperation, |
| 1831 | kUpdateStateOperationInvalid)); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1832 | if (!quick) { |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1833 | prefs->SetInt64(kPrefsUpdateStateNextDataOffset, -1); |
David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 1834 | prefs->SetInt64(kPrefsUpdateStateNextDataLength, 0); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1835 | prefs->SetString(kPrefsUpdateStateSHA256Context, ""); |
| 1836 | prefs->SetString(kPrefsUpdateStateSignedSHA256Context, ""); |
Darin Petkov | 4f0a07b | 2011-05-25 16:47:20 -0700 | [diff] [blame] | 1837 | prefs->SetString(kPrefsUpdateStateSignatureBlob, ""); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1838 | prefs->SetInt64(kPrefsManifestMetadataSize, -1); |
Alex Deymo | f25eb49 | 2016-02-26 00:20:08 -0800 | [diff] [blame] | 1839 | prefs->SetInt64(kPrefsManifestSignatureSize, -1); |
Darin Petkov | 6142614 | 2010-10-08 11:04:55 -0700 | [diff] [blame] | 1840 | prefs->SetInt64(kPrefsResumedUpdateFailures, 0); |
Sen Jiang | fe52282 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 1841 | prefs->Delete(kPrefsPostInstallSucceeded); |
Sen Jiang | 3eeaf7d | 2018-10-11 13:55:32 -0700 | [diff] [blame] | 1842 | prefs->Delete(kPrefsVerityWritten); |
Yifan Hong | 55c2bfe | 2020-01-13 17:01:19 -0800 | [diff] [blame] | 1843 | |
| 1844 | if (!skip_dynamic_partititon_metadata_updated) { |
| 1845 | LOG(INFO) << "Resetting recorded hash for prepared partitions."; |
| 1846 | prefs->Delete(kPrefsDynamicPartitionMetadataUpdated); |
| 1847 | } |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1848 | } |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 1849 | return true; |
| 1850 | } |
| 1851 | |
Sen Jiang | 3a25dc2 | 2019-01-10 14:14:41 -0800 | [diff] [blame] | 1852 | bool DeltaPerformer::CheckpointUpdateProgress(bool force) { |
Sen Jiang | 53c2ec5 | 2019-01-10 15:27:59 -0800 | [diff] [blame] | 1853 | base::TimeTicks curr_time = base::TimeTicks::Now(); |
Sen Jiang | 3a25dc2 | 2019-01-10 14:14:41 -0800 | [diff] [blame] | 1854 | if (force || curr_time > update_checkpoint_time_) { |
Colin Howes | 0e452c9 | 2018-11-02 13:18:44 -0700 | [diff] [blame] | 1855 | update_checkpoint_time_ = curr_time + update_checkpoint_wait_; |
| 1856 | } else { |
| 1857 | return false; |
| 1858 | } |
| 1859 | |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 1860 | Terminator::set_exit_blocked(true); |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1861 | if (last_updated_buffer_offset_ != buffer_offset_) { |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 1862 | // 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] | 1863 | ResetUpdateProgress(prefs_, true); |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 1864 | TEST_AND_RETURN_FALSE(prefs_->SetString( |
| 1865 | kPrefsUpdateStateSHA256Context, payload_hash_calculator_.GetContext())); |
Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 1866 | TEST_AND_RETURN_FALSE( |
| 1867 | prefs_->SetString(kPrefsUpdateStateSignedSHA256Context, |
| 1868 | signed_hash_calculator_.GetContext())); |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 1869 | TEST_AND_RETURN_FALSE( |
| 1870 | prefs_->SetInt64(kPrefsUpdateStateNextDataOffset, buffer_offset_)); |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1871 | last_updated_buffer_offset_ = buffer_offset_; |
David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 1872 | |
| 1873 | if (next_operation_num_ < num_total_operations_) { |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 1874 | size_t partition_index = current_partition_; |
| 1875 | while (next_operation_num_ >= acc_num_operations_[partition_index]) |
| 1876 | partition_index++; |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 1877 | const size_t partition_operation_num = |
| 1878 | next_operation_num_ - |
| 1879 | (partition_index ? acc_num_operations_[partition_index - 1] : 0); |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 1880 | const InstallOperation& op = |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 1881 | partitions_[partition_index].operations(partition_operation_num); |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 1882 | TEST_AND_RETURN_FALSE( |
| 1883 | prefs_->SetInt64(kPrefsUpdateStateNextDataLength, op.data_length())); |
David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 1884 | } else { |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 1885 | TEST_AND_RETURN_FALSE( |
| 1886 | prefs_->SetInt64(kPrefsUpdateStateNextDataLength, 0)); |
David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 1887 | } |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1888 | } |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 1889 | TEST_AND_RETURN_FALSE( |
| 1890 | prefs_->SetInt64(kPrefsUpdateStateNextOperation, next_operation_num_)); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 1891 | return true; |
| 1892 | } |
| 1893 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1894 | bool DeltaPerformer::PrimeUpdateState() { |
| 1895 | CHECK(manifest_valid_); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1896 | |
| 1897 | int64_t next_operation = kUpdateStateOperationInvalid; |
| 1898 | if (!prefs_->GetInt64(kPrefsUpdateStateNextOperation, &next_operation) || |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 1899 | next_operation == kUpdateStateOperationInvalid || next_operation <= 0) { |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1900 | // Initiating a new update, no more state needs to be initialized. |
| 1901 | return true; |
| 1902 | } |
| 1903 | next_operation_num_ = next_operation; |
| 1904 | |
| 1905 | // Resuming an update -- load the rest of the update state. |
| 1906 | int64_t next_data_offset = -1; |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 1907 | TEST_AND_RETURN_FALSE( |
| 1908 | prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset) && |
| 1909 | next_data_offset >= 0); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1910 | buffer_offset_ = next_data_offset; |
| 1911 | |
Darin Petkov | 4f0a07b | 2011-05-25 16:47:20 -0700 | [diff] [blame] | 1912 | // The signed hash context and the signature blob may be empty if the |
| 1913 | // interrupted update didn't reach the signature. |
Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 1914 | string signed_hash_context; |
| 1915 | if (prefs_->GetString(kPrefsUpdateStateSignedSHA256Context, |
| 1916 | &signed_hash_context)) { |
| 1917 | TEST_AND_RETURN_FALSE( |
| 1918 | signed_hash_calculator_.SetContext(signed_hash_context)); |
| 1919 | } |
| 1920 | |
Sen Jiang | 9b2f178 | 2019-01-24 14:27:50 -0800 | [diff] [blame] | 1921 | prefs_->GetString(kPrefsUpdateStateSignatureBlob, &signatures_message_data_); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1922 | |
| 1923 | string hash_context; |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 1924 | TEST_AND_RETURN_FALSE( |
| 1925 | prefs_->GetString(kPrefsUpdateStateSHA256Context, &hash_context) && |
| 1926 | payload_hash_calculator_.SetContext(hash_context)); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1927 | |
| 1928 | int64_t manifest_metadata_size = 0; |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 1929 | TEST_AND_RETURN_FALSE( |
| 1930 | prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size) && |
| 1931 | manifest_metadata_size > 0); |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 1932 | metadata_size_ = manifest_metadata_size; |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1933 | |
Alex Deymo | f25eb49 | 2016-02-26 00:20:08 -0800 | [diff] [blame] | 1934 | int64_t manifest_signature_size = 0; |
| 1935 | TEST_AND_RETURN_FALSE( |
| 1936 | prefs_->GetInt64(kPrefsManifestSignatureSize, &manifest_signature_size) && |
| 1937 | manifest_signature_size >= 0); |
| 1938 | metadata_signature_size_ = manifest_signature_size; |
| 1939 | |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 1940 | // Advance the download progress to reflect what doesn't need to be |
| 1941 | // re-downloaded. |
| 1942 | total_bytes_received_ += buffer_offset_; |
| 1943 | |
Darin Petkov | 6142614 | 2010-10-08 11:04:55 -0700 | [diff] [blame] | 1944 | // Speculatively count the resume as a failure. |
| 1945 | int64_t resumed_update_failures; |
| 1946 | if (prefs_->GetInt64(kPrefsResumedUpdateFailures, &resumed_update_failures)) { |
| 1947 | resumed_update_failures++; |
| 1948 | } else { |
| 1949 | resumed_update_failures = 1; |
| 1950 | } |
| 1951 | prefs_->SetInt64(kPrefsResumedUpdateFailures, resumed_update_failures); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1952 | return true; |
| 1953 | } |
| 1954 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 1955 | } // namespace chromeos_update_engine |