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