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