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