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