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