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