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