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