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 | |
| 17 | #include "update_engine/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 <endian.h> |
| 20 | #include <errno.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> |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 24 | #include <memory> |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 25 | #include <string> |
| 26 | #include <vector> |
| 27 | |
Ben Chan | 06c76a4 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 28 | #include <base/files/file_util.h> |
Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 29 | #include <base/format_macros.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 30 | #include <base/strings/string_util.h> |
| 31 | #include <base/strings/stringprintf.h> |
Alex Vakulenko | 981a9fb | 2015-02-09 12:51:24 -0800 | [diff] [blame] | 32 | #include <chromeos/data_encoding.h> |
Alex Deymo | 0532287 | 2015-09-30 09:50:24 -0700 | [diff] [blame] | 33 | #include <chromeos/make_unique_ptr.h> |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 34 | #include <google/protobuf/repeated_field.h> |
| 35 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 36 | #include "update_engine/bzip_extent_writer.h" |
Jay Srinivasan | d29695d | 2013-04-08 15:08:05 -0700 | [diff] [blame] | 37 | #include "update_engine/constants.h" |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 38 | #include "update_engine/extent_writer.h" |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 39 | #include "update_engine/hardware_interface.h" |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 40 | #if USE_MTD |
| 41 | #include "update_engine/mtd_file_descriptor.h" |
| 42 | #endif |
Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 43 | #include "update_engine/payload_constants.h" |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 44 | #include "update_engine/payload_state_interface.h" |
Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 45 | #include "update_engine/payload_verifier.h" |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 46 | #include "update_engine/prefs_interface.h" |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 47 | #include "update_engine/subprocess.h" |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 48 | #include "update_engine/terminator.h" |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 49 | #include "update_engine/update_attempter.h" |
Alex Deymo | 2d621a3 | 2015-10-01 11:09:01 -0700 | [diff] [blame] | 50 | #include "update_engine/xz_extent_writer.h" |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 51 | |
Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 52 | using google::protobuf::RepeatedPtrField; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 53 | using std::min; |
| 54 | using std::string; |
| 55 | using std::vector; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 56 | |
| 57 | namespace chromeos_update_engine { |
| 58 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 59 | const uint64_t DeltaPerformer::kDeltaVersionSize = 8; |
| 60 | const uint64_t DeltaPerformer::kDeltaManifestSizeSize = 8; |
Don Garrett | 4d03944 | 2013-10-28 18:40:06 -0700 | [diff] [blame] | 61 | const uint64_t DeltaPerformer::kSupportedMajorPayloadVersion = 1; |
Allie Wood | 6eeec90 | 2015-04-15 10:40:52 -0700 | [diff] [blame] | 62 | const uint64_t DeltaPerformer::kSupportedMinorPayloadVersion = 2; |
Don Garrett | 4d03944 | 2013-10-28 18:40:06 -0700 | [diff] [blame] | 63 | |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 64 | const unsigned DeltaPerformer::kProgressLogMaxChunks = 10; |
| 65 | const unsigned DeltaPerformer::kProgressLogTimeoutSeconds = 30; |
| 66 | const unsigned DeltaPerformer::kProgressDownloadWeight = 50; |
| 67 | const unsigned DeltaPerformer::kProgressOperationsWeight = 50; |
Darin Petkov | abc7bc0 | 2011-02-23 14:39:43 -0800 | [diff] [blame] | 68 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 69 | namespace { |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 70 | const int kUpdateStateOperationInvalid = -1; |
Darin Petkov | 6142614 | 2010-10-08 11:04:55 -0700 | [diff] [blame] | 71 | const int kMaxResumedUpdateFailures = 10; |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 72 | #if USE_MTD |
| 73 | const int kUbiVolumeAttachTimeout = 5 * 60; |
| 74 | #endif |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 75 | |
| 76 | FileDescriptorPtr CreateFileDescriptor(const char* path) { |
| 77 | FileDescriptorPtr ret; |
| 78 | #if USE_MTD |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 79 | if (strstr(path, "/dev/ubi") == path) { |
| 80 | if (!UbiFileDescriptor::IsUbi(path)) { |
| 81 | // The volume might not have been attached at boot time. |
| 82 | int volume_no; |
| 83 | if (utils::SplitPartitionName(path, nullptr, &volume_no)) { |
| 84 | utils::TryAttachingUbiVolume(volume_no, kUbiVolumeAttachTimeout); |
| 85 | } |
| 86 | } |
| 87 | if (UbiFileDescriptor::IsUbi(path)) { |
| 88 | LOG(INFO) << path << " is a UBI device."; |
| 89 | ret.reset(new UbiFileDescriptor); |
| 90 | } |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 91 | } else if (MtdFileDescriptor::IsMtd(path)) { |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 92 | LOG(INFO) << path << " is an MTD device."; |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 93 | ret.reset(new MtdFileDescriptor); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 94 | } else { |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 95 | LOG(INFO) << path << " is not an MTD nor a UBI device."; |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 96 | #endif |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 97 | ret.reset(new EintrSafeFileDescriptor); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 98 | #if USE_MTD |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 99 | } |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 100 | #endif |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 101 | return ret; |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 104 | // Opens path for read/write. On success returns an open FileDescriptor |
| 105 | // and sets *err to 0. On failure, sets *err to errno and returns nullptr. |
| 106 | FileDescriptorPtr OpenFile(const char* path, int* err) { |
| 107 | FileDescriptorPtr fd = CreateFileDescriptor(path); |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 108 | int mode = O_RDWR; |
| 109 | #if USE_MTD |
| 110 | // On NAND devices, we can either read, or write, but not both. So here we |
| 111 | // use O_WRONLY. |
| 112 | if (UbiFileDescriptor::IsUbi(path) || MtdFileDescriptor::IsMtd(path)) { |
| 113 | mode = O_WRONLY; |
| 114 | } |
| 115 | #endif |
| 116 | if (!fd->Open(path, mode, 000)) { |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 117 | *err = errno; |
| 118 | PLOG(ERROR) << "Unable to open file " << path; |
| 119 | return nullptr; |
| 120 | } |
| 121 | *err = 0; |
| 122 | return fd; |
| 123 | } |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 124 | } // namespace |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 125 | |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 126 | |
| 127 | // Computes the ratio of |part| and |total|, scaled to |norm|, using integer |
| 128 | // arithmetic. |
| 129 | static uint64_t IntRatio(uint64_t part, uint64_t total, uint64_t norm) { |
| 130 | return part * norm / total; |
| 131 | } |
| 132 | |
| 133 | void DeltaPerformer::LogProgress(const char* message_prefix) { |
| 134 | // Format operations total count and percentage. |
| 135 | string total_operations_str("?"); |
| 136 | string completed_percentage_str(""); |
| 137 | if (num_total_operations_) { |
Alex Deymo | c00c98a | 2015-03-17 17:38:00 -0700 | [diff] [blame] | 138 | total_operations_str = std::to_string(num_total_operations_); |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 139 | // Upcasting to 64-bit to avoid overflow, back to size_t for formatting. |
| 140 | completed_percentage_str = |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 141 | base::StringPrintf(" (%" PRIu64 "%%)", |
Alex Deymo | c00c98a | 2015-03-17 17:38:00 -0700 | [diff] [blame] | 142 | IntRatio(next_operation_num_, num_total_operations_, |
| 143 | 100)); |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | // Format download total count and percentage. |
| 147 | size_t payload_size = install_plan_->payload_size; |
| 148 | string payload_size_str("?"); |
| 149 | string downloaded_percentage_str(""); |
| 150 | if (payload_size) { |
Alex Deymo | c00c98a | 2015-03-17 17:38:00 -0700 | [diff] [blame] | 151 | payload_size_str = std::to_string(payload_size); |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 152 | // Upcasting to 64-bit to avoid overflow, back to size_t for formatting. |
| 153 | downloaded_percentage_str = |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 154 | base::StringPrintf(" (%" PRIu64 "%%)", |
Alex Deymo | c00c98a | 2015-03-17 17:38:00 -0700 | [diff] [blame] | 155 | IntRatio(total_bytes_received_, payload_size, 100)); |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | LOG(INFO) << (message_prefix ? message_prefix : "") << next_operation_num_ |
| 159 | << "/" << total_operations_str << " operations" |
| 160 | << completed_percentage_str << ", " << total_bytes_received_ |
| 161 | << "/" << payload_size_str << " bytes downloaded" |
| 162 | << downloaded_percentage_str << ", overall progress " |
| 163 | << overall_progress_ << "%"; |
| 164 | } |
| 165 | |
| 166 | void DeltaPerformer::UpdateOverallProgress(bool force_log, |
| 167 | const char* message_prefix) { |
| 168 | // Compute our download and overall progress. |
| 169 | unsigned new_overall_progress = 0; |
| 170 | COMPILE_ASSERT(kProgressDownloadWeight + kProgressOperationsWeight == 100, |
| 171 | progress_weight_dont_add_up); |
| 172 | // Only consider download progress if its total size is known; otherwise |
| 173 | // adjust the operations weight to compensate for the absence of download |
| 174 | // progress. Also, make sure to cap the download portion at |
| 175 | // kProgressDownloadWeight, in case we end up downloading more than we |
| 176 | // initially expected (this indicates a problem, but could generally happen). |
| 177 | // TODO(garnold) the correction of operations weight when we do not have the |
| 178 | // total payload size, as well as the conditional guard below, should both be |
| 179 | // eliminated once we ensure that the payload_size in the install plan is |
| 180 | // always given and is non-zero. This currently isn't the case during unit |
| 181 | // tests (see chromium-os:37969). |
| 182 | size_t payload_size = install_plan_->payload_size; |
| 183 | unsigned actual_operations_weight = kProgressOperationsWeight; |
| 184 | if (payload_size) |
| 185 | new_overall_progress += min( |
| 186 | static_cast<unsigned>(IntRatio(total_bytes_received_, payload_size, |
| 187 | kProgressDownloadWeight)), |
| 188 | kProgressDownloadWeight); |
| 189 | else |
| 190 | actual_operations_weight += kProgressDownloadWeight; |
| 191 | |
| 192 | // Only add completed operations if their total number is known; we definitely |
| 193 | // expect an update to have at least one operation, so the expectation is that |
| 194 | // this will eventually reach |actual_operations_weight|. |
| 195 | if (num_total_operations_) |
| 196 | new_overall_progress += IntRatio(next_operation_num_, num_total_operations_, |
| 197 | actual_operations_weight); |
| 198 | |
| 199 | // Progress ratio cannot recede, unless our assumptions about the total |
| 200 | // payload size, total number of operations, or the monotonicity of progress |
| 201 | // is breached. |
| 202 | if (new_overall_progress < overall_progress_) { |
| 203 | LOG(WARNING) << "progress counter receded from " << overall_progress_ |
| 204 | << "% down to " << new_overall_progress << "%; this is a bug"; |
| 205 | force_log = true; |
| 206 | } |
| 207 | overall_progress_ = new_overall_progress; |
| 208 | |
| 209 | // Update chunk index, log as needed: if forced by called, or we completed a |
| 210 | // progress chunk, or a timeout has expired. |
| 211 | base::Time curr_time = base::Time::Now(); |
| 212 | unsigned curr_progress_chunk = |
| 213 | overall_progress_ * kProgressLogMaxChunks / 100; |
| 214 | if (force_log || curr_progress_chunk > last_progress_chunk_ || |
| 215 | curr_time > forced_progress_log_time_) { |
| 216 | forced_progress_log_time_ = curr_time + forced_progress_log_wait_; |
| 217 | LogProgress(message_prefix); |
| 218 | } |
| 219 | last_progress_chunk_ = curr_progress_chunk; |
| 220 | } |
| 221 | |
| 222 | |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 223 | size_t DeltaPerformer::CopyDataToBuffer(const char** bytes_p, size_t* count_p, |
| 224 | size_t max) { |
| 225 | const size_t count = *count_p; |
| 226 | if (!count) |
| 227 | return 0; // Special case shortcut. |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 228 | size_t read_len = min(count, max - buffer_.size()); |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 229 | const char* bytes_start = *bytes_p; |
| 230 | const char* bytes_end = bytes_start + read_len; |
| 231 | buffer_.insert(buffer_.end(), bytes_start, bytes_end); |
| 232 | *bytes_p = bytes_end; |
| 233 | *count_p = count - read_len; |
| 234 | return read_len; |
| 235 | } |
| 236 | |
| 237 | |
| 238 | bool DeltaPerformer::HandleOpResult(bool op_result, const char* op_type_name, |
| 239 | ErrorCode* error) { |
| 240 | if (op_result) |
| 241 | return true; |
| 242 | |
| 243 | LOG(ERROR) << "Failed to perform " << op_type_name << " operation " |
| 244 | << next_operation_num_; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 245 | *error = ErrorCode::kDownloadOperationExecutionError; |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 246 | return false; |
| 247 | } |
| 248 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 249 | int DeltaPerformer::Open(const char* path, int flags, mode_t mode) { |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 250 | int err; |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 251 | fd_ = OpenFile(path, &err); |
| 252 | if (fd_) |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 253 | path_ = path; |
| 254 | return -err; |
| 255 | } |
| 256 | |
| 257 | bool DeltaPerformer::OpenKernel(const char* kernel_path) { |
| 258 | int err; |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 259 | kernel_fd_ = OpenFile(kernel_path, &err); |
| 260 | if (kernel_fd_) |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 261 | kernel_path_ = kernel_path; |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 262 | return static_cast<bool>(kernel_fd_); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Alex Deymo | 1beda78 | 2015-06-07 23:01:25 +0200 | [diff] [blame] | 265 | bool DeltaPerformer::OpenSourceRootfs(const string& source_path) { |
Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 266 | int err; |
| 267 | source_fd_ = OpenFile(source_path.c_str(), &err); |
| 268 | return static_cast<bool>(source_fd_); |
| 269 | } |
| 270 | |
Alex Deymo | 1beda78 | 2015-06-07 23:01:25 +0200 | [diff] [blame] | 271 | bool DeltaPerformer::OpenSourceKernel(const string& source_kernel_path) { |
Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 272 | int err; |
| 273 | source_kernel_fd_ = OpenFile(source_kernel_path.c_str(), &err); |
| 274 | return static_cast<bool>(source_kernel_fd_); |
| 275 | } |
| 276 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 277 | int DeltaPerformer::Close() { |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 278 | int err = 0; |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 279 | if (kernel_fd_ && !kernel_fd_->Close()) { |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 280 | err = errno; |
| 281 | PLOG(ERROR) << "Unable to close kernel fd:"; |
| 282 | } |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 283 | if (!fd_->Close()) { |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 284 | err = errno; |
| 285 | PLOG(ERROR) << "Unable to close rootfs fd:"; |
| 286 | } |
Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 287 | if (source_fd_ && !source_fd_->Close()) { |
| 288 | err = errno; |
| 289 | PLOG(ERROR) << "Unable to close source rootfs fd:"; |
| 290 | } |
| 291 | if (source_kernel_fd_ && !source_kernel_fd_->Close()) { |
| 292 | err = errno; |
| 293 | PLOG(ERROR) << "Unable to close source kernel fd:"; |
| 294 | } |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 295 | LOG_IF(ERROR, !hash_calculator_.Finalize()) << "Unable to finalize the hash."; |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 296 | fd_.reset(); // Set to invalid so that calls to Open() will fail. |
Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 297 | kernel_fd_.reset(); |
| 298 | source_fd_.reset(); |
| 299 | source_kernel_fd_.reset(); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 300 | path_ = ""; |
Darin Petkov | 934bb41 | 2010-11-18 11:21:35 -0800 | [diff] [blame] | 301 | if (!buffer_.empty()) { |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 302 | LOG(INFO) << "Discarding " << buffer_.size() << " unused downloaded bytes"; |
| 303 | if (err >= 0) |
Darin Petkov | 934bb41 | 2010-11-18 11:21:35 -0800 | [diff] [blame] | 304 | err = 1; |
Darin Petkov | 934bb41 | 2010-11-18 11:21:35 -0800 | [diff] [blame] | 305 | } |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 306 | return -err; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame] | 309 | namespace { |
| 310 | |
| 311 | void LogPartitionInfoHash(const PartitionInfo& info, const string& tag) { |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 312 | string sha256 = chromeos::data_encoding::Base64Encode(info.hash()); |
Alex Vakulenko | 981a9fb | 2015-02-09 12:51:24 -0800 | [diff] [blame] | 313 | LOG(INFO) << "PartitionInfo " << tag << " sha256: " << sha256 |
| 314 | << " size: " << info.size(); |
Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | void LogPartitionInfo(const DeltaArchiveManifest& manifest) { |
| 318 | if (manifest.has_old_kernel_info()) |
| 319 | LogPartitionInfoHash(manifest.old_kernel_info(), "old_kernel_info"); |
| 320 | if (manifest.has_old_rootfs_info()) |
| 321 | LogPartitionInfoHash(manifest.old_rootfs_info(), "old_rootfs_info"); |
| 322 | if (manifest.has_new_kernel_info()) |
| 323 | LogPartitionInfoHash(manifest.new_kernel_info(), "new_kernel_info"); |
| 324 | if (manifest.has_new_rootfs_info()) |
| 325 | LogPartitionInfoHash(manifest.new_rootfs_info(), "new_rootfs_info"); |
| 326 | } |
| 327 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 328 | } // namespace |
Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame] | 329 | |
Don Garrett | 4d03944 | 2013-10-28 18:40:06 -0700 | [diff] [blame] | 330 | uint64_t DeltaPerformer::GetVersionOffset() { |
Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 331 | // Manifest size is stored right after the magic string and the version. |
| 332 | return strlen(kDeltaMagic); |
Don Garrett | 4d03944 | 2013-10-28 18:40:06 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 335 | uint64_t DeltaPerformer::GetManifestSizeOffset() { |
Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 336 | // Manifest size is stored right after the magic string and the version. |
| 337 | return strlen(kDeltaMagic) + kDeltaVersionSize; |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | uint64_t DeltaPerformer::GetManifestOffset() { |
Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 341 | // Actual manifest begins right after the manifest size field. |
| 342 | return GetManifestSizeOffset() + kDeltaManifestSizeSize; |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 345 | uint64_t DeltaPerformer::GetMetadataSize() const { |
Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 346 | return metadata_size_; |
| 347 | } |
| 348 | |
Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 349 | uint32_t DeltaPerformer::GetMinorVersion() const { |
| 350 | if (manifest_.has_minor_version()) { |
| 351 | return manifest_.minor_version(); |
| 352 | } else { |
| 353 | return (install_plan_->is_full_update ? |
| 354 | kFullPayloadMinorVersion : |
| 355 | kSupportedMinorPayloadVersion); |
| 356 | } |
| 357 | } |
| 358 | |
Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 359 | bool DeltaPerformer::GetManifest(DeltaArchiveManifest* out_manifest_p) const { |
| 360 | if (!manifest_parsed_) |
| 361 | return false; |
| 362 | *out_manifest_p = manifest_; |
| 363 | return true; |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 364 | } |
| 365 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 366 | |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 367 | DeltaPerformer::MetadataParseResult DeltaPerformer::ParsePayloadMetadata( |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 368 | const chromeos::Blob& payload, ErrorCode* error) { |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 369 | *error = ErrorCode::kSuccess; |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 370 | const uint64_t manifest_offset = GetManifestOffset(); |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 371 | uint64_t manifest_size = (metadata_size_ ? |
| 372 | metadata_size_ - manifest_offset : 0); |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 373 | |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 374 | if (!manifest_size) { |
| 375 | // Ensure we have data to cover the payload header. |
| 376 | if (payload.size() < manifest_offset) |
| 377 | return kMetadataParseInsufficientData; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 378 | |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 379 | // Validate the magic string. |
| 380 | if (memcmp(payload.data(), kDeltaMagic, strlen(kDeltaMagic)) != 0) { |
| 381 | LOG(ERROR) << "Bad payload format -- invalid delta magic."; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 382 | *error = ErrorCode::kDownloadInvalidMetadataMagicString; |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 383 | return kMetadataParseError; |
| 384 | } |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 385 | |
| 386 | // Extract the payload version from the metadata. |
| 387 | uint64_t major_payload_version; |
| 388 | COMPILE_ASSERT(sizeof(major_payload_version) == kDeltaVersionSize, |
| 389 | major_payload_version_size_mismatch); |
| 390 | memcpy(&major_payload_version, |
| 391 | &payload[GetVersionOffset()], |
| 392 | kDeltaVersionSize); |
| 393 | // switch big endian to host |
| 394 | major_payload_version = be64toh(major_payload_version); |
| 395 | |
| 396 | if (major_payload_version != kSupportedMajorPayloadVersion) { |
| 397 | LOG(ERROR) << "Bad payload format -- unsupported payload version: " |
| 398 | << major_payload_version; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 399 | *error = ErrorCode::kUnsupportedMajorPayloadVersion; |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 400 | return kMetadataParseError; |
| 401 | } |
| 402 | |
| 403 | // Next, parse the manifest size. |
| 404 | COMPILE_ASSERT(sizeof(manifest_size) == kDeltaManifestSizeSize, |
| 405 | manifest_size_size_mismatch); |
| 406 | memcpy(&manifest_size, |
| 407 | &payload[GetManifestSizeOffset()], |
| 408 | kDeltaManifestSizeSize); |
| 409 | manifest_size = be64toh(manifest_size); // switch big endian to host |
| 410 | |
| 411 | // If the metadata size is present in install plan, check for it immediately |
| 412 | // even before waiting for that many number of bytes to be downloaded in the |
| 413 | // payload. This will prevent any attack which relies on us downloading data |
| 414 | // beyond the expected metadata size. |
| 415 | metadata_size_ = manifest_offset + manifest_size; |
| 416 | if (install_plan_->hash_checks_mandatory) { |
| 417 | if (install_plan_->metadata_size != metadata_size_) { |
| 418 | LOG(ERROR) << "Mandatory metadata size in Omaha response (" |
| 419 | << install_plan_->metadata_size |
| 420 | << ") is missing/incorrect, actual = " << metadata_size_; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 421 | *error = ErrorCode::kDownloadInvalidMetadataSize; |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 422 | return kMetadataParseError; |
| 423 | } |
| 424 | } |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | // Now that we have validated the metadata size, we should wait for the full |
| 428 | // metadata to be read in before we can parse it. |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 429 | if (payload.size() < metadata_size_) |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 430 | return kMetadataParseInsufficientData; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 431 | |
| 432 | // 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] | 433 | // 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] | 434 | // that we just log once (instead of logging n times) if it takes n |
| 435 | // DeltaPerformer::Write calls to download the full manifest. |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 436 | if (install_plan_->metadata_size == metadata_size_) { |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 437 | LOG(INFO) << "Manifest size in payload matches expected value from Omaha"; |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 438 | } else { |
| 439 | // For mandatory-cases, we'd have already returned a kMetadataParseError |
| 440 | // above. We'll be here only for non-mandatory cases. Just send a UMA stat. |
| 441 | LOG(WARNING) << "Ignoring missing/incorrect metadata size (" |
| 442 | << install_plan_->metadata_size |
| 443 | << ") in Omaha response as validation is not mandatory. " |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 444 | << "Trusting metadata size in payload = " << metadata_size_; |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 445 | } |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 446 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 447 | // We have the full metadata in |payload|. Verify its integrity |
| 448 | // and authenticity based on the information we have in Omaha response. |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 449 | *error = ValidateMetadataSignature(payload.data(), metadata_size_); |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 450 | if (*error != ErrorCode::kSuccess) { |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 451 | if (install_plan_->hash_checks_mandatory) { |
David Zeuthen | bc27aac | 2013-11-26 11:17:48 -0800 | [diff] [blame] | 452 | // The autoupdate_CatchBadSignatures test checks for this string |
| 453 | // in log-files. Keep in sync. |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 454 | LOG(ERROR) << "Mandatory metadata signature validation failed"; |
| 455 | return kMetadataParseError; |
| 456 | } |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 457 | |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 458 | // For non-mandatory cases, just send a UMA stat. |
| 459 | LOG(WARNING) << "Ignoring metadata signature validation failures"; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 460 | *error = ErrorCode::kSuccess; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 463 | // The payload metadata is deemed valid, it's safe to parse the protobuf. |
| 464 | if (!manifest_.ParseFromArray(&payload[manifest_offset], manifest_size)) { |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 465 | LOG(ERROR) << "Unable to parse manifest in update file."; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 466 | *error = ErrorCode::kDownloadManifestParseError; |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 467 | return kMetadataParseError; |
| 468 | } |
Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 469 | |
| 470 | manifest_parsed_ = true; |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 471 | return kMetadataParseSuccess; |
| 472 | } |
| 473 | |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 474 | // Wrapper around write. Returns true if all requested bytes |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 475 | // were written, or false on any error, regardless of progress |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 476 | // and stores an action exit code in |error|. |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 477 | bool DeltaPerformer::Write(const void* bytes, size_t count, ErrorCode *error) { |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 478 | *error = ErrorCode::kSuccess; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 479 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 480 | const char* c_bytes = reinterpret_cast<const char*>(bytes); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 481 | system_state_->payload_state()->DownloadProgress(count); |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 482 | |
| 483 | // Update the total byte downloaded count and the progress logs. |
| 484 | total_bytes_received_ += count; |
| 485 | UpdateOverallProgress(false, "Completed "); |
| 486 | |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 487 | while (!manifest_valid_) { |
| 488 | // Read data up to the needed limit; this is either the payload header size, |
| 489 | // or the full metadata size (once it becomes known). |
| 490 | const bool do_read_header = !metadata_size_; |
| 491 | CopyDataToBuffer(&c_bytes, &count, |
| 492 | (do_read_header ? GetManifestOffset() : |
| 493 | metadata_size_)); |
| 494 | |
Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 495 | MetadataParseResult result = ParsePayloadMetadata(buffer_, error); |
Gilad Arnold | 5cac591 | 2013-05-24 17:21:17 -0700 | [diff] [blame] | 496 | if (result == kMetadataParseError) |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 497 | return false; |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 498 | if (result == kMetadataParseInsufficientData) { |
| 499 | // If we just processed the header, make an attempt on the manifest. |
| 500 | if (do_read_header && metadata_size_) |
| 501 | continue; |
| 502 | |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 503 | return true; |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 504 | } |
Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 505 | |
| 506 | // Checks the integrity of the payload manifest. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 507 | if ((*error = ValidateManifest()) != ErrorCode::kSuccess) |
Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 508 | return false; |
Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 509 | manifest_valid_ = true; |
Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 510 | |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 511 | // Clear the download buffer. |
Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 512 | DiscardBuffer(false); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 513 | LOG_IF(WARNING, !prefs_->SetInt64(kPrefsManifestMetadataSize, |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 514 | metadata_size_)) |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 515 | << "Unable to save the manifest metadata size."; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 516 | |
Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame] | 517 | LogPartitionInfo(manifest_); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 518 | if (!PrimeUpdateState()) { |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 519 | *error = ErrorCode::kDownloadStateInitializationError; |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 520 | LOG(ERROR) << "Unable to prime the update state."; |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 521 | return false; |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 522 | } |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 523 | |
Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 524 | // Open source fds if we have a delta payload with minor version 2. |
| 525 | if (!install_plan_->is_full_update && |
| 526 | GetMinorVersion() == kSourceMinorPayloadVersion) { |
| 527 | if (!OpenSourceRootfs(install_plan_->source_path)) { |
| 528 | LOG(ERROR) << "Unable to open source rootfs partition file " |
| 529 | << install_plan_->source_path; |
| 530 | Close(); |
| 531 | return false; |
| 532 | } |
| 533 | if (!OpenSourceKernel(install_plan_->kernel_source_path)) { |
| 534 | LOG(ERROR) << "Unable to open source kernel partition file " |
| 535 | << install_plan_->kernel_source_path; |
| 536 | Close(); |
| 537 | return false; |
| 538 | } |
| 539 | } |
| 540 | |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 541 | num_rootfs_operations_ = manifest_.install_operations_size(); |
| 542 | num_total_operations_ = |
| 543 | num_rootfs_operations_ + manifest_.kernel_install_operations_size(); |
| 544 | if (next_operation_num_ > 0) |
| 545 | UpdateOverallProgress(true, "Resuming after "); |
| 546 | LOG(INFO) << "Starting to apply update payload operations"; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 547 | } |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 548 | |
| 549 | while (next_operation_num_ < num_total_operations_) { |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 550 | // Check if we should cancel the current attempt for any reason. |
| 551 | // In this case, *error will have already been populated with the reason |
| 552 | // why we're cancelling. |
| 553 | if (system_state_->update_attempter()->ShouldCancel(error)) |
| 554 | return false; |
| 555 | |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 556 | const bool is_kernel_partition = |
| 557 | (next_operation_num_ >= num_rootfs_operations_); |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 558 | const InstallOperation& op = |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 559 | is_kernel_partition ? |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 560 | manifest_.kernel_install_operations(next_operation_num_ - |
| 561 | num_rootfs_operations_) : |
| 562 | manifest_.install_operations(next_operation_num_); |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 563 | |
| 564 | CopyDataToBuffer(&c_bytes, &count, op.data_length()); |
| 565 | |
| 566 | // Check whether we received all of the next operation's data payload. |
| 567 | if (!CanPerformInstallOperation(op)) |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 568 | return true; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 569 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 570 | // Validate the operation only if the metadata signature is present. |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 571 | // Otherwise, keep the old behavior. This serves as a knob to disable |
| 572 | // the validation logic in case we find some regression after rollout. |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 573 | // NOTE: If hash checks are mandatory and if metadata_signature is empty, |
| 574 | // we would have already failed in ParsePayloadMetadata method and thus not |
| 575 | // even be here. So no need to handle that case again here. |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 576 | if (!install_plan_->metadata_signature.empty()) { |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 577 | // Note: Validate must be called only if CanPerformInstallOperation is |
| 578 | // called. Otherwise, we might be failing operations before even if there |
| 579 | // isn't sufficient data to compute the proper hash. |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 580 | *error = ValidateOperationHash(op); |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 581 | if (*error != ErrorCode::kSuccess) { |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 582 | if (install_plan_->hash_checks_mandatory) { |
| 583 | LOG(ERROR) << "Mandatory operation hash check failed"; |
| 584 | return false; |
| 585 | } |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 586 | |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 587 | // For non-mandatory cases, just send a UMA stat. |
| 588 | LOG(WARNING) << "Ignoring operation validation errors"; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 589 | *error = ErrorCode::kSuccess; |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 590 | } |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Darin Petkov | 45580e4 | 2010-10-08 14:02:40 -0700 | [diff] [blame] | 593 | // Makes sure we unblock exit when this operation completes. |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 594 | ScopedTerminatorExitUnblocker exit_unblocker = |
| 595 | ScopedTerminatorExitUnblocker(); // Avoids a compiler unused var bug. |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 596 | |
| 597 | bool op_result; |
Alex Deymo | 2d621a3 | 2015-10-01 11:09:01 -0700 | [diff] [blame] | 598 | switch (op.type()) { |
| 599 | case InstallOperation::REPLACE: |
| 600 | case InstallOperation::REPLACE_BZ: |
| 601 | case InstallOperation::REPLACE_XZ: |
| 602 | op_result = PerformReplaceOperation(op, is_kernel_partition); |
| 603 | break; |
| 604 | case InstallOperation::MOVE: |
| 605 | op_result = PerformMoveOperation(op, is_kernel_partition); |
| 606 | break; |
| 607 | case InstallOperation::BSDIFF: |
| 608 | op_result = PerformBsdiffOperation(op, is_kernel_partition); |
| 609 | break; |
| 610 | case InstallOperation::SOURCE_COPY: |
| 611 | op_result = PerformSourceCopyOperation(op, is_kernel_partition); |
| 612 | break; |
| 613 | case InstallOperation::SOURCE_BSDIFF: |
| 614 | op_result = PerformSourceBsdiffOperation(op, is_kernel_partition); |
| 615 | break; |
| 616 | default: |
| 617 | op_result = false; |
| 618 | } |
| 619 | if (!HandleOpResult(op_result, InstallOperationTypeName(op.type()), error)) |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 620 | return false; |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 621 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 622 | next_operation_num_++; |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 623 | UpdateOverallProgress(false, "Completed "); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 624 | CheckpointUpdateProgress(); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 625 | } |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 626 | return true; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 627 | } |
| 628 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 629 | bool DeltaPerformer::IsManifestValid() { |
| 630 | return manifest_valid_; |
| 631 | } |
| 632 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 633 | bool DeltaPerformer::CanPerformInstallOperation( |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 634 | const chromeos_update_engine::InstallOperation& operation) { |
Allie Wood | 9f6f0a5 | 2015-03-30 11:25:47 -0700 | [diff] [blame] | 635 | // Move and source_copy operations don't require any data blob, so they can |
| 636 | // always be performed. |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 637 | if (operation.type() == InstallOperation::MOVE || |
| 638 | operation.type() == InstallOperation::SOURCE_COPY) |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 639 | return true; |
| 640 | |
| 641 | // See if we have the entire data blob in the buffer |
| 642 | if (operation.data_offset() < buffer_offset_) { |
| 643 | LOG(ERROR) << "we threw away data it seems?"; |
| 644 | return false; |
| 645 | } |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 646 | |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 647 | return (operation.data_offset() + operation.data_length() <= |
| 648 | buffer_offset_ + buffer_.size()); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 651 | bool DeltaPerformer::PerformReplaceOperation(const InstallOperation& operation, |
| 652 | bool is_kernel_partition) { |
| 653 | CHECK(operation.type() == InstallOperation::REPLACE || |
Alex Deymo | 2d621a3 | 2015-10-01 11:09:01 -0700 | [diff] [blame] | 654 | operation.type() == InstallOperation::REPLACE_BZ || |
| 655 | operation.type() == InstallOperation::REPLACE_XZ); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 656 | |
| 657 | // Since we delete data off the beginning of the buffer as we use it, |
| 658 | // 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] | 659 | TEST_AND_RETURN_FALSE(buffer_offset_ == operation.data_offset()); |
| 660 | TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length()); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 661 | |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 662 | // Extract the signature message if it's in this operation. |
| 663 | ExtractSignatureMessage(operation); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 664 | |
Alex Deymo | 0532287 | 2015-09-30 09:50:24 -0700 | [diff] [blame] | 665 | // Setup the ExtentWriter stack based on the operation type. |
| 666 | std::unique_ptr<ExtentWriter> writer = |
| 667 | chromeos::make_unique_ptr(new ZeroPadExtentWriter( |
| 668 | chromeos::make_unique_ptr(new DirectExtentWriter()))); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 669 | |
Alex Deymo | 2d621a3 | 2015-10-01 11:09:01 -0700 | [diff] [blame] | 670 | if (operation.type() == InstallOperation::REPLACE_BZ) { |
Alex Deymo | 0532287 | 2015-09-30 09:50:24 -0700 | [diff] [blame] | 671 | writer.reset(new BzipExtentWriter(std::move(writer))); |
Alex Deymo | 2d621a3 | 2015-10-01 11:09:01 -0700 | [diff] [blame] | 672 | } else if (operation.type() == InstallOperation::REPLACE_XZ) { |
| 673 | writer.reset(new XzExtentWriter(std::move(writer))); |
| 674 | } |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 675 | |
| 676 | // Create a vector of extents to pass to the ExtentWriter. |
| 677 | vector<Extent> extents; |
| 678 | for (int i = 0; i < operation.dst_extents_size(); i++) { |
| 679 | extents.push_back(operation.dst_extents(i)); |
| 680 | } |
| 681 | |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 682 | FileDescriptorPtr fd = is_kernel_partition ? kernel_fd_ : fd_; |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 683 | |
| 684 | TEST_AND_RETURN_FALSE(writer->Init(fd, extents, block_size_)); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 685 | TEST_AND_RETURN_FALSE(writer->Write(buffer_.data(), operation.data_length())); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 686 | TEST_AND_RETURN_FALSE(writer->End()); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 687 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 688 | // Update buffer |
Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 689 | DiscardBuffer(true); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 690 | return true; |
| 691 | } |
| 692 | |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 693 | bool DeltaPerformer::PerformMoveOperation(const InstallOperation& operation, |
| 694 | bool is_kernel_partition) { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 695 | // Calculate buffer size. Note, this function doesn't do a sliding |
| 696 | // window to copy in case the source and destination blocks overlap. |
| 697 | // If we wanted to do a sliding window, we could program the server |
| 698 | // to generate deltas that effectively did a sliding window. |
| 699 | |
| 700 | uint64_t blocks_to_read = 0; |
| 701 | for (int i = 0; i < operation.src_extents_size(); i++) |
| 702 | blocks_to_read += operation.src_extents(i).num_blocks(); |
| 703 | |
| 704 | uint64_t blocks_to_write = 0; |
| 705 | for (int i = 0; i < operation.dst_extents_size(); i++) |
| 706 | blocks_to_write += operation.dst_extents(i).num_blocks(); |
| 707 | |
| 708 | DCHECK_EQ(blocks_to_write, blocks_to_read); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 709 | chromeos::Blob buf(blocks_to_write * block_size_); |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 710 | |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 711 | FileDescriptorPtr fd = is_kernel_partition ? kernel_fd_ : fd_; |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 712 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 713 | // Read in bytes. |
| 714 | ssize_t bytes_read = 0; |
| 715 | for (int i = 0; i < operation.src_extents_size(); i++) { |
| 716 | ssize_t bytes_read_this_iteration = 0; |
| 717 | const Extent& extent = operation.src_extents(i); |
Darin Petkov | 8a075a7 | 2013-04-25 14:46:09 +0200 | [diff] [blame] | 718 | const size_t bytes = extent.num_blocks() * block_size_; |
Allie Wood | 5687345 | 2015-03-27 17:48:40 -0700 | [diff] [blame] | 719 | TEST_AND_RETURN_FALSE(extent.start_block() != kSparseHole); |
| 720 | TEST_AND_RETURN_FALSE(utils::PReadAll(fd, |
| 721 | &buf[bytes_read], |
| 722 | bytes, |
| 723 | extent.start_block() * block_size_, |
| 724 | &bytes_read_this_iteration)); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 725 | TEST_AND_RETURN_FALSE( |
Darin Petkov | 8a075a7 | 2013-04-25 14:46:09 +0200 | [diff] [blame] | 726 | bytes_read_this_iteration == static_cast<ssize_t>(bytes)); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 727 | bytes_read += bytes_read_this_iteration; |
| 728 | } |
| 729 | |
| 730 | // Write bytes out. |
| 731 | ssize_t bytes_written = 0; |
| 732 | for (int i = 0; i < operation.dst_extents_size(); i++) { |
| 733 | const Extent& extent = operation.dst_extents(i); |
Darin Petkov | 8a075a7 | 2013-04-25 14:46:09 +0200 | [diff] [blame] | 734 | const size_t bytes = extent.num_blocks() * block_size_; |
Allie Wood | 5687345 | 2015-03-27 17:48:40 -0700 | [diff] [blame] | 735 | TEST_AND_RETURN_FALSE(extent.start_block() != kSparseHole); |
| 736 | TEST_AND_RETURN_FALSE(utils::PWriteAll(fd, |
| 737 | &buf[bytes_written], |
| 738 | bytes, |
| 739 | extent.start_block() * block_size_)); |
Darin Petkov | 8a075a7 | 2013-04-25 14:46:09 +0200 | [diff] [blame] | 740 | bytes_written += bytes; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 741 | } |
| 742 | DCHECK_EQ(bytes_written, bytes_read); |
| 743 | DCHECK_EQ(bytes_written, static_cast<ssize_t>(buf.size())); |
| 744 | return true; |
| 745 | } |
| 746 | |
Allie Wood | 9f6f0a5 | 2015-03-30 11:25:47 -0700 | [diff] [blame] | 747 | namespace { |
| 748 | |
| 749 | // Takes |extents| and fills an empty vector |blocks| with a block index for |
| 750 | // each block in |extents|. For example, [(3, 2), (8, 1)] would give [3, 4, 8]. |
| 751 | void ExtentsToBlocks(const RepeatedPtrField<Extent>& extents, |
| 752 | vector<uint64_t>* blocks) { |
| 753 | for (Extent ext : extents) { |
| 754 | for (uint64_t j = 0; j < ext.num_blocks(); j++) |
| 755 | blocks->push_back(ext.start_block() + j); |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | // Takes |extents| and returns the number of blocks in those extents. |
| 760 | uint64_t GetBlockCount(const RepeatedPtrField<Extent>& extents) { |
| 761 | uint64_t sum = 0; |
| 762 | for (Extent ext : extents) { |
| 763 | sum += ext.num_blocks(); |
| 764 | } |
| 765 | return sum; |
| 766 | } |
| 767 | |
| 768 | } // namespace |
| 769 | |
| 770 | bool DeltaPerformer::PerformSourceCopyOperation( |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 771 | const InstallOperation& operation, |
Allie Wood | 9f6f0a5 | 2015-03-30 11:25:47 -0700 | [diff] [blame] | 772 | bool is_kernel_partition) { |
| 773 | if (operation.has_src_length()) |
| 774 | TEST_AND_RETURN_FALSE(operation.src_length() % block_size_ == 0); |
| 775 | if (operation.has_dst_length()) |
| 776 | TEST_AND_RETURN_FALSE(operation.dst_length() % block_size_ == 0); |
| 777 | |
| 778 | uint64_t blocks_to_read = GetBlockCount(operation.src_extents()); |
| 779 | uint64_t blocks_to_write = GetBlockCount(operation.dst_extents()); |
| 780 | TEST_AND_RETURN_FALSE(blocks_to_write == blocks_to_read); |
| 781 | |
| 782 | // Create vectors of all the individual src/dst blocks. |
| 783 | vector<uint64_t> src_blocks; |
| 784 | vector<uint64_t> dst_blocks; |
| 785 | ExtentsToBlocks(operation.src_extents(), &src_blocks); |
| 786 | ExtentsToBlocks(operation.dst_extents(), &dst_blocks); |
| 787 | DCHECK_EQ(src_blocks.size(), blocks_to_read); |
| 788 | DCHECK_EQ(src_blocks.size(), dst_blocks.size()); |
| 789 | |
| 790 | FileDescriptorPtr src_fd = |
| 791 | is_kernel_partition ? source_kernel_fd_ : source_fd_; |
| 792 | FileDescriptorPtr dst_fd = is_kernel_partition? kernel_fd_ : fd_; |
| 793 | |
| 794 | chromeos::Blob buf(block_size_); |
| 795 | ssize_t bytes_read = 0; |
| 796 | // Read/write one block at a time. |
| 797 | for (uint64_t i = 0; i < blocks_to_read; i++) { |
| 798 | ssize_t bytes_read_this_iteration = 0; |
| 799 | uint64_t src_block = src_blocks[i]; |
| 800 | uint64_t dst_block = dst_blocks[i]; |
| 801 | |
| 802 | // Read in bytes. |
| 803 | TEST_AND_RETURN_FALSE( |
| 804 | utils::PReadAll(src_fd, |
| 805 | buf.data(), |
| 806 | block_size_, |
| 807 | src_block * block_size_, |
| 808 | &bytes_read_this_iteration)); |
| 809 | |
| 810 | // Write bytes out. |
| 811 | TEST_AND_RETURN_FALSE( |
| 812 | utils::PWriteAll(dst_fd, |
| 813 | buf.data(), |
| 814 | block_size_, |
| 815 | dst_block * block_size_)); |
| 816 | |
| 817 | bytes_read += bytes_read_this_iteration; |
| 818 | TEST_AND_RETURN_FALSE(bytes_read_this_iteration == |
| 819 | static_cast<ssize_t>(block_size_)); |
| 820 | } |
| 821 | DCHECK_EQ(bytes_read, static_cast<ssize_t>(blocks_to_read * block_size_)); |
| 822 | return true; |
| 823 | } |
| 824 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 825 | bool DeltaPerformer::ExtentsToBsdiffPositionsString( |
| 826 | const RepeatedPtrField<Extent>& extents, |
| 827 | uint64_t block_size, |
| 828 | uint64_t full_length, |
| 829 | string* positions_string) { |
| 830 | string ret; |
| 831 | uint64_t length = 0; |
| 832 | for (int i = 0; i < extents.size(); i++) { |
| 833 | Extent extent = extents.Get(i); |
Allie Wood | 5687345 | 2015-03-27 17:48:40 -0700 | [diff] [blame] | 834 | int64_t start = extent.start_block() * block_size; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 835 | uint64_t this_length = min(full_length - length, |
| 836 | extent.num_blocks() * block_size); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 837 | ret += base::StringPrintf("%" PRIi64 ":%" PRIu64 ",", start, this_length); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 838 | length += this_length; |
| 839 | } |
| 840 | TEST_AND_RETURN_FALSE(length == full_length); |
| 841 | if (!ret.empty()) |
| 842 | ret.resize(ret.size() - 1); // Strip trailing comma off |
| 843 | *positions_string = ret; |
| 844 | return true; |
| 845 | } |
| 846 | |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 847 | bool DeltaPerformer::PerformBsdiffOperation(const InstallOperation& operation, |
| 848 | bool is_kernel_partition) { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 849 | // Since we delete data off the beginning of the buffer as we use it, |
| 850 | // the data we need should be exactly at the beginning of the buffer. |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 851 | TEST_AND_RETURN_FALSE(buffer_offset_ == operation.data_offset()); |
| 852 | TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length()); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 853 | |
| 854 | string input_positions; |
| 855 | TEST_AND_RETURN_FALSE(ExtentsToBsdiffPositionsString(operation.src_extents(), |
| 856 | block_size_, |
| 857 | operation.src_length(), |
| 858 | &input_positions)); |
| 859 | string output_positions; |
| 860 | TEST_AND_RETURN_FALSE(ExtentsToBsdiffPositionsString(operation.dst_extents(), |
| 861 | block_size_, |
| 862 | operation.dst_length(), |
| 863 | &output_positions)); |
| 864 | |
| 865 | string temp_filename; |
Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 866 | TEST_AND_RETURN_FALSE(utils::MakeTempFile("au_patch.XXXXXX", |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 867 | &temp_filename, |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 868 | nullptr)); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 869 | ScopedPathUnlinker path_unlinker(temp_filename); |
| 870 | { |
| 871 | int fd = open(temp_filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 872 | ScopedFdCloser fd_closer(&fd); |
| 873 | TEST_AND_RETURN_FALSE( |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 874 | utils::WriteAll(fd, buffer_.data(), operation.data_length())); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 875 | } |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 876 | |
Darin Petkov | 7f2ec75 | 2013-04-03 14:45:19 +0200 | [diff] [blame] | 877 | // Update the buffer to release the patch data memory as soon as the patch |
| 878 | // file is written out. |
Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 879 | DiscardBuffer(true); |
Darin Petkov | 7f2ec75 | 2013-04-03 14:45:19 +0200 | [diff] [blame] | 880 | |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 881 | const string& path = is_kernel_partition ? kernel_path_ : path_; |
Allie Wood | 9f6f0a5 | 2015-03-30 11:25:47 -0700 | [diff] [blame] | 882 | vector<string> cmd{kBspatchPath, path, path, temp_filename, |
| 883 | input_positions, output_positions}; |
| 884 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 885 | int return_code = 0; |
Andrew de los Reyes | 5a23283 | 2010-10-12 16:20:54 -0700 | [diff] [blame] | 886 | TEST_AND_RETURN_FALSE( |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 887 | Subprocess::SynchronousExecFlags(cmd, Subprocess::kSearchPath, |
| 888 | &return_code, nullptr)); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 889 | TEST_AND_RETURN_FALSE(return_code == 0); |
| 890 | |
| 891 | if (operation.dst_length() % block_size_) { |
| 892 | // Zero out rest of final block. |
| 893 | // TODO(adlr): build this into bspatch; it's more efficient that way. |
| 894 | const Extent& last_extent = |
| 895 | operation.dst_extents(operation.dst_extents_size() - 1); |
| 896 | const uint64_t end_byte = |
| 897 | (last_extent.start_block() + last_extent.num_blocks()) * block_size_; |
| 898 | const uint64_t begin_byte = |
| 899 | end_byte - (block_size_ - operation.dst_length() % block_size_); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 900 | chromeos::Blob zeros(end_byte - begin_byte); |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 901 | FileDescriptorPtr fd = is_kernel_partition ? kernel_fd_ : fd_; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 902 | TEST_AND_RETURN_FALSE( |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 903 | utils::PWriteAll(fd, zeros.data(), end_byte - begin_byte, begin_byte)); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 904 | } |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 905 | return true; |
| 906 | } |
| 907 | |
Allie Wood | 9f6f0a5 | 2015-03-30 11:25:47 -0700 | [diff] [blame] | 908 | bool DeltaPerformer::PerformSourceBsdiffOperation( |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 909 | const InstallOperation& operation, |
Allie Wood | 9f6f0a5 | 2015-03-30 11:25:47 -0700 | [diff] [blame] | 910 | bool is_kernel_partition) { |
| 911 | // Since we delete data off the beginning of the buffer as we use it, |
| 912 | // the data we need should be exactly at the beginning of the buffer. |
| 913 | TEST_AND_RETURN_FALSE(buffer_offset_ == operation.data_offset()); |
| 914 | TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length()); |
| 915 | if (operation.has_src_length()) |
| 916 | TEST_AND_RETURN_FALSE(operation.src_length() % block_size_ == 0); |
| 917 | if (operation.has_dst_length()) |
| 918 | TEST_AND_RETURN_FALSE(operation.dst_length() % block_size_ == 0); |
| 919 | |
| 920 | string input_positions; |
| 921 | TEST_AND_RETURN_FALSE(ExtentsToBsdiffPositionsString(operation.src_extents(), |
| 922 | block_size_, |
| 923 | operation.src_length(), |
| 924 | &input_positions)); |
| 925 | string output_positions; |
| 926 | TEST_AND_RETURN_FALSE(ExtentsToBsdiffPositionsString(operation.dst_extents(), |
| 927 | block_size_, |
| 928 | operation.dst_length(), |
| 929 | &output_positions)); |
| 930 | |
| 931 | string temp_filename; |
Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 932 | TEST_AND_RETURN_FALSE(utils::MakeTempFile("au_patch.XXXXXX", |
Allie Wood | 9f6f0a5 | 2015-03-30 11:25:47 -0700 | [diff] [blame] | 933 | &temp_filename, |
| 934 | nullptr)); |
| 935 | ScopedPathUnlinker path_unlinker(temp_filename); |
| 936 | { |
| 937 | int fd = open(temp_filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 938 | ScopedFdCloser fd_closer(&fd); |
| 939 | TEST_AND_RETURN_FALSE( |
| 940 | utils::WriteAll(fd, buffer_.data(), operation.data_length())); |
| 941 | } |
| 942 | |
| 943 | // Update the buffer to release the patch data memory as soon as the patch |
| 944 | // file is written out. |
| 945 | DiscardBuffer(true); |
| 946 | |
| 947 | const string& src_path = is_kernel_partition ? |
| 948 | install_plan_->kernel_source_path : |
| 949 | install_plan_->source_path; |
| 950 | const string& dst_path = is_kernel_partition ? kernel_path_ : path_; |
| 951 | vector<string> cmd{kBspatchPath, src_path, dst_path, temp_filename, |
| 952 | input_positions, output_positions}; |
| 953 | |
| 954 | int return_code = 0; |
| 955 | TEST_AND_RETURN_FALSE( |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 956 | Subprocess::SynchronousExecFlags(cmd, Subprocess::kSearchPath, |
| 957 | &return_code, nullptr)); |
Allie Wood | 9f6f0a5 | 2015-03-30 11:25:47 -0700 | [diff] [blame] | 958 | TEST_AND_RETURN_FALSE(return_code == 0); |
| 959 | return true; |
| 960 | } |
| 961 | |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 962 | bool DeltaPerformer::ExtractSignatureMessage( |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 963 | const InstallOperation& operation) { |
| 964 | if (operation.type() != InstallOperation::REPLACE || |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 965 | !manifest_.has_signatures_offset() || |
| 966 | manifest_.signatures_offset() != operation.data_offset()) { |
| 967 | return false; |
| 968 | } |
| 969 | TEST_AND_RETURN_FALSE(manifest_.has_signatures_size() && |
| 970 | manifest_.signatures_size() == operation.data_length()); |
| 971 | TEST_AND_RETURN_FALSE(signatures_message_data_.empty()); |
| 972 | TEST_AND_RETURN_FALSE(buffer_offset_ == manifest_.signatures_offset()); |
| 973 | TEST_AND_RETURN_FALSE(buffer_.size() >= manifest_.signatures_size()); |
Darin Petkov | 4f0a07b | 2011-05-25 16:47:20 -0700 | [diff] [blame] | 974 | signatures_message_data_.assign( |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 975 | buffer_.begin(), |
| 976 | buffer_.begin() + manifest_.signatures_size()); |
Darin Petkov | 4f0a07b | 2011-05-25 16:47:20 -0700 | [diff] [blame] | 977 | |
| 978 | // Save the signature blob because if the update is interrupted after the |
| 979 | // download phase we don't go through this path anymore. Some alternatives to |
| 980 | // consider: |
| 981 | // |
| 982 | // 1. On resume, re-download the signature blob from the server and re-verify |
| 983 | // it. |
| 984 | // |
| 985 | // 2. Verify the signature as soon as it's received and don't checkpoint the |
| 986 | // blob and the signed sha-256 context. |
| 987 | LOG_IF(WARNING, !prefs_->SetString(kPrefsUpdateStateSignatureBlob, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 988 | string(signatures_message_data_.begin(), |
| 989 | signatures_message_data_.end()))) |
Darin Petkov | 4f0a07b | 2011-05-25 16:47:20 -0700 | [diff] [blame] | 990 | << "Unable to store the signature blob."; |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 991 | // The hash of all data consumed so far should be verified against the signed |
| 992 | // hash. |
| 993 | signed_hash_context_ = hash_calculator_.GetContext(); |
| 994 | LOG_IF(WARNING, !prefs_->SetString(kPrefsUpdateStateSignedSHA256Context, |
| 995 | signed_hash_context_)) |
| 996 | << "Unable to store the signed hash context."; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 997 | LOG(INFO) << "Extracted signature data of size " |
| 998 | << manifest_.signatures_size() << " at " |
| 999 | << manifest_.signatures_offset(); |
| 1000 | return true; |
| 1001 | } |
| 1002 | |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 1003 | bool DeltaPerformer::GetPublicKeyFromResponse(base::FilePath *out_tmp_key) { |
| 1004 | if (system_state_->hardware()->IsOfficialBuild() || |
| 1005 | utils::FileExists(public_key_path_.c_str()) || |
| 1006 | install_plan_->public_key_rsa.empty()) |
| 1007 | return false; |
| 1008 | |
| 1009 | if (!utils::DecodeAndStoreBase64String(install_plan_->public_key_rsa, |
| 1010 | out_tmp_key)) |
| 1011 | return false; |
| 1012 | |
| 1013 | return true; |
| 1014 | } |
| 1015 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1016 | ErrorCode DeltaPerformer::ValidateMetadataSignature( |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1017 | const void* metadata, uint64_t metadata_size) { |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 1018 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 1019 | if (install_plan_->metadata_signature.empty()) { |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 1020 | if (install_plan_->hash_checks_mandatory) { |
| 1021 | LOG(ERROR) << "Missing mandatory metadata signature in Omaha response"; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1022 | return ErrorCode::kDownloadMetadataSignatureMissingError; |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 1023 | } |
| 1024 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 1025 | LOG(WARNING) << "Cannot validate metadata as the signature is empty"; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1026 | return ErrorCode::kSuccess; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | // Convert base64-encoded signature to raw bytes. |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1030 | chromeos::Blob metadata_signature; |
Alex Vakulenko | 981a9fb | 2015-02-09 12:51:24 -0800 | [diff] [blame] | 1031 | if (!chromeos::data_encoding::Base64Decode(install_plan_->metadata_signature, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1032 | &metadata_signature)) { |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 1033 | LOG(ERROR) << "Unable to decode base64 metadata signature: " |
| 1034 | << install_plan_->metadata_signature; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1035 | return ErrorCode::kDownloadMetadataSignatureError; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 1036 | } |
| 1037 | |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 1038 | // See if we should use the public RSA key in the Omaha response. |
| 1039 | base::FilePath path_to_public_key(public_key_path_); |
| 1040 | base::FilePath tmp_key; |
| 1041 | if (GetPublicKeyFromResponse(&tmp_key)) |
| 1042 | path_to_public_key = tmp_key; |
| 1043 | ScopedPathUnlinker tmp_key_remover(tmp_key.value()); |
| 1044 | if (tmp_key.empty()) |
| 1045 | tmp_key_remover.set_should_remove(false); |
| 1046 | |
| 1047 | LOG(INFO) << "Verifying metadata hash signature using public key: " |
| 1048 | << path_to_public_key.value(); |
| 1049 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1050 | chromeos::Blob expected_metadata_hash; |
Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 1051 | if (!PayloadVerifier::GetRawHashFromSignature(metadata_signature, |
| 1052 | path_to_public_key.value(), |
| 1053 | &expected_metadata_hash)) { |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 1054 | LOG(ERROR) << "Unable to compute expected hash from metadata signature"; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1055 | return ErrorCode::kDownloadMetadataSignatureError; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 1056 | } |
| 1057 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 1058 | OmahaHashCalculator metadata_hasher; |
| 1059 | metadata_hasher.Update(metadata, metadata_size); |
| 1060 | if (!metadata_hasher.Finalize()) { |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 1061 | LOG(ERROR) << "Unable to compute actual hash of manifest"; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1062 | return ErrorCode::kDownloadMetadataSignatureVerificationError; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1065 | chromeos::Blob calculated_metadata_hash = metadata_hasher.raw_hash(); |
Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 1066 | PayloadVerifier::PadRSA2048SHA256Hash(&calculated_metadata_hash); |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 1067 | if (calculated_metadata_hash.empty()) { |
| 1068 | LOG(ERROR) << "Computed actual hash of metadata is empty."; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1069 | return ErrorCode::kDownloadMetadataSignatureVerificationError; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 1070 | } |
| 1071 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 1072 | if (calculated_metadata_hash != expected_metadata_hash) { |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 1073 | LOG(ERROR) << "Manifest hash verification failed. Expected hash = "; |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 1074 | utils::HexDumpVector(expected_metadata_hash); |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 1075 | LOG(ERROR) << "Calculated hash = "; |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 1076 | utils::HexDumpVector(calculated_metadata_hash); |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1077 | return ErrorCode::kDownloadMetadataSignatureMismatch; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 1078 | } |
| 1079 | |
David Zeuthen | bc27aac | 2013-11-26 11:17:48 -0800 | [diff] [blame] | 1080 | // The autoupdate_CatchBadSignatures test checks for this string in |
| 1081 | // log-files. Keep in sync. |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 1082 | LOG(INFO) << "Metadata hash signature matches value in Omaha response."; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1083 | return ErrorCode::kSuccess; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 1086 | ErrorCode DeltaPerformer::ValidateManifest() { |
Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 1087 | // Perform assorted checks to sanity check the manifest, make sure it |
| 1088 | // matches data from other sources, and that it is a supported version. |
Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 1089 | // |
| 1090 | // TODO(garnold) in general, the presence of an old partition hash should be |
| 1091 | // the sole indicator for a delta update, as we would generally like update |
| 1092 | // payloads to be self contained and not assume an Omaha response to tell us |
| 1093 | // that. However, since this requires some massive reengineering of the update |
| 1094 | // flow (making filesystem copying happen conditionally only *after* |
| 1095 | // downloading and parsing of the update manifest) we'll put it off for now. |
| 1096 | // See chromium-os:7597 for further discussion. |
Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 1097 | if (install_plan_->is_full_update) { |
| 1098 | if (manifest_.has_old_kernel_info() || manifest_.has_old_rootfs_info()) { |
| 1099 | LOG(ERROR) << "Purported full payload contains old partition " |
| 1100 | "hash(es), aborting update"; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1101 | return ErrorCode::kPayloadMismatchedType; |
Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | if (manifest_.minor_version() != kFullPayloadMinorVersion) { |
| 1105 | LOG(ERROR) << "Manifest contains minor version " |
| 1106 | << manifest_.minor_version() |
| 1107 | << ", but all full payloads should have version " |
| 1108 | << kFullPayloadMinorVersion << "."; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1109 | return ErrorCode::kUnsupportedMinorPayloadVersion; |
Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 1110 | } |
| 1111 | } else { |
Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 1112 | if (manifest_.minor_version() != supported_minor_version_) { |
Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 1113 | LOG(ERROR) << "Manifest contains minor version " |
| 1114 | << manifest_.minor_version() |
| 1115 | << " not the supported " |
Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 1116 | << supported_minor_version_; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1117 | return ErrorCode::kUnsupportedMinorPayloadVersion; |
Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 1118 | } |
Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | // TODO(garnold) we should be adding more and more manifest checks, such as |
| 1122 | // partition boundaries etc (see chromium-os:37661). |
| 1123 | |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1124 | return ErrorCode::kSuccess; |
Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1127 | ErrorCode DeltaPerformer::ValidateOperationHash( |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 1128 | const InstallOperation& operation) { |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1129 | if (!operation.data_sha256_hash().size()) { |
| 1130 | if (!operation.data_length()) { |
| 1131 | // Operations that do not have any data blob won't have any operation hash |
| 1132 | // either. So, these operations are always considered validated since the |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 1133 | // metadata that contains all the non-data-blob portions of the operation |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 1134 | // 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] | 1135 | return ErrorCode::kSuccess; |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 1138 | // No hash is present for an operation that has data blobs. This shouldn't |
| 1139 | // happen normally for any client that has this code, because the |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1140 | // corresponding update should have been produced with the operation |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 1141 | // hashes. So if it happens it means either we've turned operation hash |
| 1142 | // generation off in DeltaDiffGenerator or it's a regression of some sort. |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1143 | // One caveat though: The last operation is a dummy signature operation |
| 1144 | // that doesn't have a hash at the time the manifest is created. So we |
| 1145 | // should not complaint about that operation. This operation can be |
| 1146 | // recognized by the fact that it's offset is mentioned in the manifest. |
| 1147 | if (manifest_.signatures_offset() && |
| 1148 | manifest_.signatures_offset() == operation.data_offset()) { |
| 1149 | LOG(INFO) << "Skipping hash verification for signature operation " |
| 1150 | << next_operation_num_ + 1; |
| 1151 | } else { |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 1152 | if (install_plan_->hash_checks_mandatory) { |
| 1153 | LOG(ERROR) << "Missing mandatory operation hash for operation " |
| 1154 | << next_operation_num_ + 1; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1155 | return ErrorCode::kDownloadOperationHashMissingError; |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 1156 | } |
| 1157 | |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 1158 | LOG(WARNING) << "Cannot validate operation " << next_operation_num_ + 1 |
| 1159 | << " as there's no operation hash in manifest"; |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1160 | } |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1161 | return ErrorCode::kSuccess; |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1162 | } |
| 1163 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1164 | chromeos::Blob expected_op_hash; |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1165 | expected_op_hash.assign(operation.data_sha256_hash().data(), |
| 1166 | (operation.data_sha256_hash().data() + |
| 1167 | operation.data_sha256_hash().size())); |
| 1168 | |
| 1169 | OmahaHashCalculator operation_hasher; |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1170 | operation_hasher.Update(buffer_.data(), operation.data_length()); |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1171 | if (!operation_hasher.Finalize()) { |
| 1172 | LOG(ERROR) << "Unable to compute actual hash of operation " |
| 1173 | << next_operation_num_; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1174 | return ErrorCode::kDownloadOperationHashVerificationError; |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1175 | } |
| 1176 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1177 | chromeos::Blob calculated_op_hash = operation_hasher.raw_hash(); |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1178 | if (calculated_op_hash != expected_op_hash) { |
| 1179 | LOG(ERROR) << "Hash verification failed for operation " |
| 1180 | << next_operation_num_ << ". Expected hash = "; |
| 1181 | utils::HexDumpVector(expected_op_hash); |
| 1182 | LOG(ERROR) << "Calculated hash over " << operation.data_length() |
| 1183 | << " bytes at offset: " << operation.data_offset() << " = "; |
| 1184 | utils::HexDumpVector(calculated_op_hash); |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1185 | return ErrorCode::kDownloadOperationHashMismatch; |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 1186 | } |
| 1187 | |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1188 | return ErrorCode::kSuccess; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 1189 | } |
| 1190 | |
Andrew de los Reyes | 771e1bd | 2011-08-30 14:47:23 -0700 | [diff] [blame] | 1191 | #define TEST_AND_RETURN_VAL(_retval, _condition) \ |
| 1192 | do { \ |
| 1193 | if (!(_condition)) { \ |
| 1194 | LOG(ERROR) << "VerifyPayload failure: " << #_condition; \ |
| 1195 | return _retval; \ |
| 1196 | } \ |
| 1197 | } while (0); |
Andrew de los Reyes | fb830ba | 2011-04-04 11:42:43 -0700 | [diff] [blame] | 1198 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 1199 | ErrorCode DeltaPerformer::VerifyPayload( |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 1200 | const string& update_check_response_hash, |
Andrew de los Reyes | 771e1bd | 2011-08-30 14:47:23 -0700 | [diff] [blame] | 1201 | const uint64_t update_check_response_size) { |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 1202 | |
| 1203 | // See if we should use the public RSA key in the Omaha response. |
| 1204 | base::FilePath path_to_public_key(public_key_path_); |
| 1205 | base::FilePath tmp_key; |
| 1206 | if (GetPublicKeyFromResponse(&tmp_key)) |
| 1207 | path_to_public_key = tmp_key; |
| 1208 | ScopedPathUnlinker tmp_key_remover(tmp_key.value()); |
| 1209 | if (tmp_key.empty()) |
| 1210 | tmp_key_remover.set_should_remove(false); |
| 1211 | |
| 1212 | LOG(INFO) << "Verifying payload using public key: " |
| 1213 | << path_to_public_key.value(); |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 1214 | |
Jay Srinivasan | 0d8fb40 | 2012-05-07 19:19:38 -0700 | [diff] [blame] | 1215 | // Verifies the download size. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1216 | TEST_AND_RETURN_VAL(ErrorCode::kPayloadSizeMismatchError, |
Jay Srinivasan | 0d8fb40 | 2012-05-07 19:19:38 -0700 | [diff] [blame] | 1217 | update_check_response_size == |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 1218 | metadata_size_ + buffer_offset_); |
Jay Srinivasan | 0d8fb40 | 2012-05-07 19:19:38 -0700 | [diff] [blame] | 1219 | |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 1220 | // Verifies the payload hash. |
| 1221 | const string& payload_hash_data = hash_calculator_.hash(); |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1222 | TEST_AND_RETURN_VAL(ErrorCode::kDownloadPayloadVerificationError, |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 1223 | !payload_hash_data.empty()); |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1224 | TEST_AND_RETURN_VAL(ErrorCode::kPayloadHashMismatchError, |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 1225 | payload_hash_data == update_check_response_hash); |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 1226 | |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 1227 | // Verifies the signed payload hash. |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 1228 | if (!utils::FileExists(path_to_public_key.value().c_str())) { |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 1229 | LOG(WARNING) << "Not verifying signed delta payload -- missing public key."; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1230 | return ErrorCode::kSuccess; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 1231 | } |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1232 | TEST_AND_RETURN_VAL(ErrorCode::kSignedDeltaPayloadExpectedError, |
Andrew de los Reyes | 771e1bd | 2011-08-30 14:47:23 -0700 | [diff] [blame] | 1233 | !signatures_message_data_.empty()); |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 1234 | OmahaHashCalculator signed_hasher; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1235 | TEST_AND_RETURN_VAL(ErrorCode::kDownloadPayloadPubKeyVerificationError, |
Andrew de los Reyes | 771e1bd | 2011-08-30 14:47:23 -0700 | [diff] [blame] | 1236 | signed_hasher.SetContext(signed_hash_context_)); |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1237 | TEST_AND_RETURN_VAL(ErrorCode::kDownloadPayloadPubKeyVerificationError, |
Andrew de los Reyes | 771e1bd | 2011-08-30 14:47:23 -0700 | [diff] [blame] | 1238 | signed_hasher.Finalize()); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1239 | chromeos::Blob hash_data = signed_hasher.raw_hash(); |
Alex Deymo | b552a68 | 2015-09-30 09:36:49 -0700 | [diff] [blame] | 1240 | TEST_AND_RETURN_VAL(ErrorCode::kDownloadPayloadPubKeyVerificationError, |
| 1241 | PayloadVerifier::PadRSA2048SHA256Hash(&hash_data)); |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1242 | TEST_AND_RETURN_VAL(ErrorCode::kDownloadPayloadPubKeyVerificationError, |
Andrew de los Reyes | 771e1bd | 2011-08-30 14:47:23 -0700 | [diff] [blame] | 1243 | !hash_data.empty()); |
Alex Deymo | b552a68 | 2015-09-30 09:36:49 -0700 | [diff] [blame] | 1244 | |
| 1245 | if (!PayloadVerifier::VerifySignature( |
| 1246 | signatures_message_data_, path_to_public_key.value(), hash_data)) { |
David Zeuthen | bc27aac | 2013-11-26 11:17:48 -0800 | [diff] [blame] | 1247 | // The autoupdate_CatchBadSignatures test checks for this string |
| 1248 | // in log-files. Keep in sync. |
Alex Deymo | b552a68 | 2015-09-30 09:36:49 -0700 | [diff] [blame] | 1249 | LOG(ERROR) << "Public key verification failed, thus update failed."; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1250 | return ErrorCode::kDownloadPayloadPubKeyVerificationError; |
Andrew de los Reyes | fb830ba | 2011-04-04 11:42:43 -0700 | [diff] [blame] | 1251 | } |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 1252 | |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 1253 | LOG(INFO) << "Payload hash matches value in payload."; |
| 1254 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 1255 | // At this point, we are guaranteed to have downloaded a full payload, i.e |
| 1256 | // the one whose size matches the size mentioned in Omaha response. If any |
| 1257 | // errors happen after this, it's likely a problem with the payload itself or |
| 1258 | // the state of the system and not a problem with the URL or network. So, |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 1259 | // indicate that to the payload state so that AU can backoff appropriately. |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 1260 | system_state_->payload_state()->DownloadComplete(); |
| 1261 | |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1262 | return ErrorCode::kSuccess; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 1263 | } |
| 1264 | |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 1265 | bool DeltaPerformer::GetNewPartitionInfo(uint64_t* kernel_size, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1266 | chromeos::Blob* kernel_hash, |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 1267 | uint64_t* rootfs_size, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1268 | chromeos::Blob* rootfs_hash) { |
Darin Petkov | 2dd0109 | 2010-10-08 15:43:05 -0700 | [diff] [blame] | 1269 | TEST_AND_RETURN_FALSE(manifest_valid_ && |
| 1270 | manifest_.has_new_kernel_info() && |
| 1271 | manifest_.has_new_rootfs_info()); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 1272 | *kernel_size = manifest_.new_kernel_info().size(); |
| 1273 | *rootfs_size = manifest_.new_rootfs_info().size(); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1274 | chromeos::Blob new_kernel_hash(manifest_.new_kernel_info().hash().begin(), |
| 1275 | manifest_.new_kernel_info().hash().end()); |
| 1276 | chromeos::Blob new_rootfs_hash(manifest_.new_rootfs_info().hash().begin(), |
| 1277 | manifest_.new_rootfs_info().hash().end()); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 1278 | kernel_hash->swap(new_kernel_hash); |
| 1279 | rootfs_hash->swap(new_rootfs_hash); |
Darin Petkov | 2dd0109 | 2010-10-08 15:43:05 -0700 | [diff] [blame] | 1280 | return true; |
| 1281 | } |
| 1282 | |
Andrew de los Reyes | 100bb7d | 2011-08-09 17:35:07 -0700 | [diff] [blame] | 1283 | namespace { |
| 1284 | void LogVerifyError(bool is_kern, |
| 1285 | const string& local_hash, |
| 1286 | const string& expected_hash) { |
| 1287 | const char* type = is_kern ? "kernel" : "rootfs"; |
| 1288 | LOG(ERROR) << "This is a server-side error due to " |
| 1289 | << "mismatched delta update image!"; |
| 1290 | LOG(ERROR) << "The delta I've been given contains a " << type << " delta " |
| 1291 | << "update that must be applied over a " << type << " with " |
| 1292 | << "a specific checksum, but the " << type << " we're starting " |
| 1293 | << "with doesn't have that checksum! This means that " |
| 1294 | << "the delta I've been given doesn't match my existing " |
| 1295 | << "system. The " << type << " partition I have has hash: " |
| 1296 | << local_hash << " but the update expected me to have " |
| 1297 | << expected_hash << " ."; |
| 1298 | if (is_kern) { |
| 1299 | LOG(INFO) << "To get the checksum of a kernel partition on a " |
| 1300 | << "booted machine, run this command (change /dev/sda2 " |
| 1301 | << "as needed): dd if=/dev/sda2 bs=1M 2>/dev/null | " |
| 1302 | << "openssl dgst -sha256 -binary | openssl base64"; |
| 1303 | } else { |
| 1304 | LOG(INFO) << "To get the checksum of a rootfs partition on a " |
| 1305 | << "booted machine, run this command (change /dev/sda3 " |
| 1306 | << "as needed): dd if=/dev/sda3 bs=1M count=$(( " |
| 1307 | << "$(dumpe2fs /dev/sda3 2>/dev/null | grep 'Block count' " |
| 1308 | << "| sed 's/[^0-9]*//') / 256 )) | " |
| 1309 | << "openssl dgst -sha256 -binary | openssl base64"; |
| 1310 | } |
| 1311 | LOG(INFO) << "To get the checksum of partitions in a bin file, " |
| 1312 | << "run: .../src/scripts/sha256_partitions.sh .../file.bin"; |
| 1313 | } |
| 1314 | |
| 1315 | string StringForHashBytes(const void* bytes, size_t size) { |
Alex Vakulenko | 981a9fb | 2015-02-09 12:51:24 -0800 | [diff] [blame] | 1316 | return chromeos::data_encoding::Base64Encode(bytes, size); |
Andrew de los Reyes | 100bb7d | 2011-08-09 17:35:07 -0700 | [diff] [blame] | 1317 | } |
| 1318 | } // namespace |
| 1319 | |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 1320 | bool DeltaPerformer::VerifySourcePartitions() { |
| 1321 | LOG(INFO) << "Verifying source partitions."; |
| 1322 | CHECK(manifest_valid_); |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 1323 | CHECK(install_plan_); |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 1324 | if (manifest_.has_old_kernel_info()) { |
| 1325 | const PartitionInfo& info = manifest_.old_kernel_info(); |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 1326 | bool valid = |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 1327 | !install_plan_->source_kernel_hash.empty() && |
| 1328 | install_plan_->source_kernel_hash.size() == info.hash().size() && |
| 1329 | memcmp(install_plan_->source_kernel_hash.data(), |
Andrew de los Reyes | 100bb7d | 2011-08-09 17:35:07 -0700 | [diff] [blame] | 1330 | info.hash().data(), |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 1331 | install_plan_->source_kernel_hash.size()) == 0; |
Andrew de los Reyes | 100bb7d | 2011-08-09 17:35:07 -0700 | [diff] [blame] | 1332 | if (!valid) { |
| 1333 | LogVerifyError(true, |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 1334 | StringForHashBytes( |
| 1335 | install_plan_->source_kernel_hash.data(), |
| 1336 | install_plan_->source_kernel_hash.size()), |
Andrew de los Reyes | 100bb7d | 2011-08-09 17:35:07 -0700 | [diff] [blame] | 1337 | StringForHashBytes(info.hash().data(), |
| 1338 | info.hash().size())); |
| 1339 | } |
| 1340 | TEST_AND_RETURN_FALSE(valid); |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 1341 | } |
| 1342 | if (manifest_.has_old_rootfs_info()) { |
| 1343 | const PartitionInfo& info = manifest_.old_rootfs_info(); |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 1344 | bool valid = |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 1345 | !install_plan_->source_rootfs_hash.empty() && |
| 1346 | install_plan_->source_rootfs_hash.size() == info.hash().size() && |
| 1347 | memcmp(install_plan_->source_rootfs_hash.data(), |
Andrew de los Reyes | 100bb7d | 2011-08-09 17:35:07 -0700 | [diff] [blame] | 1348 | info.hash().data(), |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 1349 | install_plan_->source_rootfs_hash.size()) == 0; |
Andrew de los Reyes | 100bb7d | 2011-08-09 17:35:07 -0700 | [diff] [blame] | 1350 | if (!valid) { |
| 1351 | LogVerifyError(false, |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 1352 | StringForHashBytes( |
| 1353 | install_plan_->source_rootfs_hash.data(), |
| 1354 | install_plan_->source_rootfs_hash.size()), |
Andrew de los Reyes | 100bb7d | 2011-08-09 17:35:07 -0700 | [diff] [blame] | 1355 | StringForHashBytes(info.hash().data(), |
| 1356 | info.hash().size())); |
| 1357 | } |
| 1358 | TEST_AND_RETURN_FALSE(valid); |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 1359 | } |
| 1360 | return true; |
| 1361 | } |
| 1362 | |
Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 1363 | void DeltaPerformer::DiscardBuffer(bool do_advance_offset) { |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 1364 | // Update the buffer offset. |
Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 1365 | if (do_advance_offset) |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 1366 | buffer_offset_ += buffer_.size(); |
| 1367 | |
| 1368 | // Hash the content. |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1369 | hash_calculator_.Update(buffer_.data(), buffer_.size()); |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 1370 | |
| 1371 | // Swap content with an empty vector to ensure that all memory is released. |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1372 | chromeos::Blob().swap(buffer_); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 1373 | } |
| 1374 | |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1375 | bool DeltaPerformer::CanResumeUpdate(PrefsInterface* prefs, |
| 1376 | string update_check_response_hash) { |
| 1377 | int64_t next_operation = kUpdateStateOperationInvalid; |
Alex Deymo | 3310b22 | 2015-03-30 15:59:07 -0700 | [diff] [blame] | 1378 | if (!(prefs->GetInt64(kPrefsUpdateStateNextOperation, &next_operation) && |
| 1379 | next_operation != kUpdateStateOperationInvalid && |
| 1380 | next_operation > 0)) |
| 1381 | return false; |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1382 | |
| 1383 | string interrupted_hash; |
Alex Deymo | 3310b22 | 2015-03-30 15:59:07 -0700 | [diff] [blame] | 1384 | if (!(prefs->GetString(kPrefsUpdateCheckResponseHash, &interrupted_hash) && |
| 1385 | !interrupted_hash.empty() && |
| 1386 | interrupted_hash == update_check_response_hash)) |
| 1387 | return false; |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1388 | |
Darin Petkov | 6142614 | 2010-10-08 11:04:55 -0700 | [diff] [blame] | 1389 | int64_t resumed_update_failures; |
Alex Deymo | 3310b22 | 2015-03-30 15:59:07 -0700 | [diff] [blame] | 1390 | if (!(prefs->GetInt64(kPrefsResumedUpdateFailures, &resumed_update_failures) |
| 1391 | && resumed_update_failures > kMaxResumedUpdateFailures)) |
| 1392 | return false; |
Darin Petkov | 6142614 | 2010-10-08 11:04:55 -0700 | [diff] [blame] | 1393 | |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1394 | // Sanity check the rest. |
| 1395 | int64_t next_data_offset = -1; |
Alex Deymo | 3310b22 | 2015-03-30 15:59:07 -0700 | [diff] [blame] | 1396 | if (!(prefs->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset) && |
| 1397 | next_data_offset >= 0)) |
| 1398 | return false; |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1399 | |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 1400 | string sha256_context; |
Alex Deymo | 3310b22 | 2015-03-30 15:59:07 -0700 | [diff] [blame] | 1401 | if (!(prefs->GetString(kPrefsUpdateStateSHA256Context, &sha256_context) && |
| 1402 | !sha256_context.empty())) |
| 1403 | return false; |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1404 | |
| 1405 | int64_t manifest_metadata_size = 0; |
Alex Deymo | 3310b22 | 2015-03-30 15:59:07 -0700 | [diff] [blame] | 1406 | if (!(prefs->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size) && |
| 1407 | manifest_metadata_size > 0)) |
| 1408 | return false; |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1409 | |
| 1410 | return true; |
| 1411 | } |
| 1412 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1413 | bool DeltaPerformer::ResetUpdateProgress(PrefsInterface* prefs, bool quick) { |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1414 | TEST_AND_RETURN_FALSE(prefs->SetInt64(kPrefsUpdateStateNextOperation, |
| 1415 | kUpdateStateOperationInvalid)); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1416 | if (!quick) { |
| 1417 | prefs->SetString(kPrefsUpdateCheckResponseHash, ""); |
| 1418 | prefs->SetInt64(kPrefsUpdateStateNextDataOffset, -1); |
David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 1419 | prefs->SetInt64(kPrefsUpdateStateNextDataLength, 0); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1420 | prefs->SetString(kPrefsUpdateStateSHA256Context, ""); |
| 1421 | prefs->SetString(kPrefsUpdateStateSignedSHA256Context, ""); |
Darin Petkov | 4f0a07b | 2011-05-25 16:47:20 -0700 | [diff] [blame] | 1422 | prefs->SetString(kPrefsUpdateStateSignatureBlob, ""); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1423 | prefs->SetInt64(kPrefsManifestMetadataSize, -1); |
Darin Petkov | 6142614 | 2010-10-08 11:04:55 -0700 | [diff] [blame] | 1424 | prefs->SetInt64(kPrefsResumedUpdateFailures, 0); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1425 | } |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 1426 | return true; |
| 1427 | } |
| 1428 | |
| 1429 | bool DeltaPerformer::CheckpointUpdateProgress() { |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 1430 | Terminator::set_exit_blocked(true); |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1431 | if (last_updated_buffer_offset_ != buffer_offset_) { |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 1432 | // 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] | 1433 | ResetUpdateProgress(prefs_, true); |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1434 | TEST_AND_RETURN_FALSE( |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 1435 | prefs_->SetString(kPrefsUpdateStateSHA256Context, |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1436 | hash_calculator_.GetContext())); |
| 1437 | TEST_AND_RETURN_FALSE(prefs_->SetInt64(kPrefsUpdateStateNextDataOffset, |
| 1438 | buffer_offset_)); |
| 1439 | last_updated_buffer_offset_ = buffer_offset_; |
David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 1440 | |
| 1441 | if (next_operation_num_ < num_total_operations_) { |
| 1442 | const bool is_kernel_partition = |
| 1443 | next_operation_num_ >= num_rootfs_operations_; |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 1444 | const InstallOperation& op = |
| 1445 | is_kernel_partition |
| 1446 | ? manifest_.kernel_install_operations(next_operation_num_ - |
| 1447 | num_rootfs_operations_) |
| 1448 | : manifest_.install_operations(next_operation_num_); |
David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 1449 | TEST_AND_RETURN_FALSE(prefs_->SetInt64(kPrefsUpdateStateNextDataLength, |
| 1450 | op.data_length())); |
| 1451 | } else { |
| 1452 | TEST_AND_RETURN_FALSE(prefs_->SetInt64(kPrefsUpdateStateNextDataLength, |
| 1453 | 0)); |
| 1454 | } |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1455 | } |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 1456 | TEST_AND_RETURN_FALSE(prefs_->SetInt64(kPrefsUpdateStateNextOperation, |
| 1457 | next_operation_num_)); |
| 1458 | return true; |
| 1459 | } |
| 1460 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1461 | bool DeltaPerformer::PrimeUpdateState() { |
| 1462 | CHECK(manifest_valid_); |
| 1463 | block_size_ = manifest_.block_size(); |
| 1464 | |
| 1465 | int64_t next_operation = kUpdateStateOperationInvalid; |
| 1466 | if (!prefs_->GetInt64(kPrefsUpdateStateNextOperation, &next_operation) || |
| 1467 | next_operation == kUpdateStateOperationInvalid || |
| 1468 | next_operation <= 0) { |
| 1469 | // Initiating a new update, no more state needs to be initialized. |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 1470 | TEST_AND_RETURN_FALSE(VerifySourcePartitions()); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1471 | return true; |
| 1472 | } |
| 1473 | next_operation_num_ = next_operation; |
| 1474 | |
| 1475 | // Resuming an update -- load the rest of the update state. |
| 1476 | int64_t next_data_offset = -1; |
| 1477 | TEST_AND_RETURN_FALSE(prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, |
| 1478 | &next_data_offset) && |
| 1479 | next_data_offset >= 0); |
| 1480 | buffer_offset_ = next_data_offset; |
| 1481 | |
Darin Petkov | 4f0a07b | 2011-05-25 16:47:20 -0700 | [diff] [blame] | 1482 | // The signed hash context and the signature blob may be empty if the |
| 1483 | // interrupted update didn't reach the signature. |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1484 | prefs_->GetString(kPrefsUpdateStateSignedSHA256Context, |
| 1485 | &signed_hash_context_); |
Darin Petkov | 4f0a07b | 2011-05-25 16:47:20 -0700 | [diff] [blame] | 1486 | string signature_blob; |
| 1487 | if (prefs_->GetString(kPrefsUpdateStateSignatureBlob, &signature_blob)) { |
| 1488 | signatures_message_data_.assign(signature_blob.begin(), |
| 1489 | signature_blob.end()); |
| 1490 | } |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1491 | |
| 1492 | string hash_context; |
| 1493 | TEST_AND_RETURN_FALSE(prefs_->GetString(kPrefsUpdateStateSHA256Context, |
| 1494 | &hash_context) && |
| 1495 | hash_calculator_.SetContext(hash_context)); |
| 1496 | |
| 1497 | int64_t manifest_metadata_size = 0; |
| 1498 | TEST_AND_RETURN_FALSE(prefs_->GetInt64(kPrefsManifestMetadataSize, |
| 1499 | &manifest_metadata_size) && |
| 1500 | manifest_metadata_size > 0); |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 1501 | metadata_size_ = manifest_metadata_size; |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1502 | |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 1503 | // Advance the download progress to reflect what doesn't need to be |
| 1504 | // re-downloaded. |
| 1505 | total_bytes_received_ += buffer_offset_; |
| 1506 | |
Darin Petkov | 6142614 | 2010-10-08 11:04:55 -0700 | [diff] [blame] | 1507 | // Speculatively count the resume as a failure. |
| 1508 | int64_t resumed_update_failures; |
| 1509 | if (prefs_->GetInt64(kPrefsResumedUpdateFailures, &resumed_update_failures)) { |
| 1510 | resumed_update_failures++; |
| 1511 | } else { |
| 1512 | resumed_update_failures = 1; |
| 1513 | } |
| 1514 | prefs_->SetInt64(kPrefsResumedUpdateFailures, resumed_update_failures); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1515 | return true; |
| 1516 | } |
| 1517 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 1518 | } // namespace chromeos_update_engine |