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