blob: 2af555c7fafc681074c6599050967145b0fe3bd9 [file] [log] [blame]
Mike Frysinger8155d082012-04-06 15:23:18 -04001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "update_engine/delta_performer.h"
Darin Petkovd7061ab2010-10-06 14:37:09 -07006
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07007#include <endian.h>
8#include <errno.h>
Darin Petkovd7061ab2010-10-06 14:37:09 -07009
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070010#include <algorithm>
11#include <cstring>
12#include <string>
13#include <vector>
14
David Zeuthene7f89172013-10-31 10:21:04 -070015#include <base/file_util.h>
Chris Masoned903c3b2011-05-12 15:35:46 -070016#include <base/memory/scoped_ptr.h>
Darin Petkovd7061ab2010-10-06 14:37:09 -070017#include <base/string_util.h>
Mike Frysinger8155d082012-04-06 15:23:18 -040018#include <base/stringprintf.h>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070019#include <google/protobuf/repeated_field.h>
20
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070021#include "update_engine/bzip_extent_writer.h"
Jay Srinivasand29695d2013-04-08 15:08:05 -070022#include "update_engine/constants.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070023#include "update_engine/delta_diff_generator.h"
Andrew de los Reyes353777c2010-10-08 10:34:30 -070024#include "update_engine/extent_ranges.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070025#include "update_engine/extent_writer.h"
26#include "update_engine/graph_types.h"
David Zeuthene7f89172013-10-31 10:21:04 -070027#include "update_engine/hardware_interface.h"
Darin Petkovd7061ab2010-10-06 14:37:09 -070028#include "update_engine/payload_signer.h"
Jay Srinivasan55f50c22013-01-10 19:24:35 -080029#include "update_engine/payload_state_interface.h"
Darin Petkov73058b42010-10-06 16:32:19 -070030#include "update_engine/prefs_interface.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070031#include "update_engine/subprocess.h"
Darin Petkov9c0baf82010-10-07 13:44:48 -070032#include "update_engine/terminator.h"
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070033#include "update_engine/update_attempter.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070034
35using std::min;
36using std::string;
37using std::vector;
38using google::protobuf::RepeatedPtrField;
39
40namespace chromeos_update_engine {
41
Jay Srinivasanf4318702012-09-24 11:56:24 -070042const uint64_t DeltaPerformer::kDeltaVersionSize = 8;
43const uint64_t DeltaPerformer::kDeltaManifestSizeSize = 8;
Don Garrett4d039442013-10-28 18:40:06 -070044const uint64_t DeltaPerformer::kSupportedMajorPayloadVersion = 1;
Don Garrettb8dd1d92013-11-22 17:40:02 -080045const uint64_t DeltaPerformer::kSupportedMinorPayloadVersion = 1;
46const uint64_t DeltaPerformer::kFullPayloadMinorVersion = 0;
Don Garrett4d039442013-10-28 18:40:06 -070047
Darin Petkovabc7bc02011-02-23 14:39:43 -080048const char DeltaPerformer::kUpdatePayloadPublicKeyPath[] =
49 "/usr/share/update_engine/update-payload-key.pub.pem";
Gilad Arnold8a86fa52013-01-15 12:35:05 -080050const unsigned DeltaPerformer::kProgressLogMaxChunks = 10;
51const unsigned DeltaPerformer::kProgressLogTimeoutSeconds = 30;
52const unsigned DeltaPerformer::kProgressDownloadWeight = 50;
53const unsigned DeltaPerformer::kProgressOperationsWeight = 50;
Darin Petkovabc7bc02011-02-23 14:39:43 -080054
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070055namespace {
Darin Petkov73058b42010-10-06 16:32:19 -070056const int kUpdateStateOperationInvalid = -1;
Darin Petkov61426142010-10-08 11:04:55 -070057const int kMaxResumedUpdateFailures = 10;
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070058// Opens path for read/write, put the fd into *fd. On success returns true
59// and sets *err to 0. On failure, returns false and sets *err to errno.
60bool OpenFile(const char* path, int* fd, int* err) {
61 if (*fd != -1) {
62 LOG(ERROR) << "Can't open(" << path << "), *fd != -1 (it's " << *fd << ")";
63 *err = EINVAL;
64 return false;
65 }
66 *fd = open(path, O_RDWR, 000);
67 if (*fd < 0) {
68 *err = errno;
69 PLOG(ERROR) << "Unable to open file " << path;
70 return false;
71 }
72 *err = 0;
73 return true;
74}
75
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070076} // namespace {}
77
Gilad Arnold8a86fa52013-01-15 12:35:05 -080078
79// Computes the ratio of |part| and |total|, scaled to |norm|, using integer
80// arithmetic.
81static uint64_t IntRatio(uint64_t part, uint64_t total, uint64_t norm) {
82 return part * norm / total;
83}
84
85void DeltaPerformer::LogProgress(const char* message_prefix) {
86 // Format operations total count and percentage.
87 string total_operations_str("?");
88 string completed_percentage_str("");
89 if (num_total_operations_) {
90 total_operations_str = StringPrintf("%zu", num_total_operations_);
91 // Upcasting to 64-bit to avoid overflow, back to size_t for formatting.
92 completed_percentage_str =
93 StringPrintf(" (%llu%%)",
94 IntRatio(next_operation_num_, num_total_operations_,
95 100));
96 }
97
98 // Format download total count and percentage.
99 size_t payload_size = install_plan_->payload_size;
100 string payload_size_str("?");
101 string downloaded_percentage_str("");
102 if (payload_size) {
103 payload_size_str = StringPrintf("%zu", payload_size);
104 // Upcasting to 64-bit to avoid overflow, back to size_t for formatting.
105 downloaded_percentage_str =
106 StringPrintf(" (%llu%%)",
107 IntRatio(total_bytes_received_, payload_size, 100));
108 }
109
110 LOG(INFO) << (message_prefix ? message_prefix : "") << next_operation_num_
111 << "/" << total_operations_str << " operations"
112 << completed_percentage_str << ", " << total_bytes_received_
113 << "/" << payload_size_str << " bytes downloaded"
114 << downloaded_percentage_str << ", overall progress "
115 << overall_progress_ << "%";
116}
117
118void DeltaPerformer::UpdateOverallProgress(bool force_log,
119 const char* message_prefix) {
120 // Compute our download and overall progress.
121 unsigned new_overall_progress = 0;
122 COMPILE_ASSERT(kProgressDownloadWeight + kProgressOperationsWeight == 100,
123 progress_weight_dont_add_up);
124 // Only consider download progress if its total size is known; otherwise
125 // adjust the operations weight to compensate for the absence of download
126 // progress. Also, make sure to cap the download portion at
127 // kProgressDownloadWeight, in case we end up downloading more than we
128 // initially expected (this indicates a problem, but could generally happen).
129 // TODO(garnold) the correction of operations weight when we do not have the
130 // total payload size, as well as the conditional guard below, should both be
131 // eliminated once we ensure that the payload_size in the install plan is
132 // always given and is non-zero. This currently isn't the case during unit
133 // tests (see chromium-os:37969).
134 size_t payload_size = install_plan_->payload_size;
135 unsigned actual_operations_weight = kProgressOperationsWeight;
136 if (payload_size)
137 new_overall_progress += min(
138 static_cast<unsigned>(IntRatio(total_bytes_received_, payload_size,
139 kProgressDownloadWeight)),
140 kProgressDownloadWeight);
141 else
142 actual_operations_weight += kProgressDownloadWeight;
143
144 // Only add completed operations if their total number is known; we definitely
145 // expect an update to have at least one operation, so the expectation is that
146 // this will eventually reach |actual_operations_weight|.
147 if (num_total_operations_)
148 new_overall_progress += IntRatio(next_operation_num_, num_total_operations_,
149 actual_operations_weight);
150
151 // Progress ratio cannot recede, unless our assumptions about the total
152 // payload size, total number of operations, or the monotonicity of progress
153 // is breached.
154 if (new_overall_progress < overall_progress_) {
155 LOG(WARNING) << "progress counter receded from " << overall_progress_
156 << "% down to " << new_overall_progress << "%; this is a bug";
157 force_log = true;
158 }
159 overall_progress_ = new_overall_progress;
160
161 // Update chunk index, log as needed: if forced by called, or we completed a
162 // progress chunk, or a timeout has expired.
163 base::Time curr_time = base::Time::Now();
164 unsigned curr_progress_chunk =
165 overall_progress_ * kProgressLogMaxChunks / 100;
166 if (force_log || curr_progress_chunk > last_progress_chunk_ ||
167 curr_time > forced_progress_log_time_) {
168 forced_progress_log_time_ = curr_time + forced_progress_log_wait_;
169 LogProgress(message_prefix);
170 }
171 last_progress_chunk_ = curr_progress_chunk;
172}
173
174
Andrew de los Reyes353777c2010-10-08 10:34:30 -0700175// Returns true if |op| is idempotent -- i.e., if we can interrupt it and repeat
176// it safely. Returns false otherwise.
177bool DeltaPerformer::IsIdempotentOperation(
178 const DeltaArchiveManifest_InstallOperation& op) {
179 if (op.src_extents_size() == 0) {
180 return true;
181 }
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700182 // When in doubt, it's safe to declare an op non-idempotent. Note that we
183 // could detect other types of idempotent operations here such as a MOVE that
184 // moves blocks onto themselves. However, we rely on the server to not send
185 // such operations at all.
Andrew de los Reyes353777c2010-10-08 10:34:30 -0700186 ExtentRanges src_ranges;
187 src_ranges.AddRepeatedExtents(op.src_extents());
188 const uint64_t block_count = src_ranges.blocks();
189 src_ranges.SubtractRepeatedExtents(op.dst_extents());
190 return block_count == src_ranges.blocks();
191}
192
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700193int DeltaPerformer::Open(const char* path, int flags, mode_t mode) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700194 int err;
195 if (OpenFile(path, &fd_, &err))
196 path_ = path;
197 return -err;
198}
199
200bool DeltaPerformer::OpenKernel(const char* kernel_path) {
201 int err;
202 bool success = OpenFile(kernel_path, &kernel_fd_, &err);
203 if (success)
204 kernel_path_ = kernel_path;
205 return success;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700206}
207
208int DeltaPerformer::Close() {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700209 int err = 0;
210 if (close(kernel_fd_) == -1) {
211 err = errno;
212 PLOG(ERROR) << "Unable to close kernel fd:";
213 }
214 if (close(fd_) == -1) {
215 err = errno;
216 PLOG(ERROR) << "Unable to close rootfs fd:";
217 }
Darin Petkovd7061ab2010-10-06 14:37:09 -0700218 LOG_IF(ERROR, !hash_calculator_.Finalize()) << "Unable to finalize the hash.";
Darin Petkov934bb412010-11-18 11:21:35 -0800219 fd_ = -2; // Set to invalid so that calls to Open() will fail.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700220 path_ = "";
Darin Petkov934bb412010-11-18 11:21:35 -0800221 if (!buffer_.empty()) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700222 LOG(INFO) << "Discarding " << buffer_.size() << " unused downloaded bytes";
223 if (err >= 0)
Darin Petkov934bb412010-11-18 11:21:35 -0800224 err = 1;
Darin Petkov934bb412010-11-18 11:21:35 -0800225 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700226 return -err;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700227}
228
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700229namespace {
230
231void LogPartitionInfoHash(const PartitionInfo& info, const string& tag) {
232 string sha256;
233 if (OmahaHashCalculator::Base64Encode(info.hash().data(),
234 info.hash().size(),
235 &sha256)) {
Darin Petkov3aefa862010-12-07 14:45:00 -0800236 LOG(INFO) << "PartitionInfo " << tag << " sha256: " << sha256
237 << " size: " << info.size();
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700238 } else {
239 LOG(ERROR) << "Base64Encode failed for tag: " << tag;
240 }
241}
242
243void LogPartitionInfo(const DeltaArchiveManifest& manifest) {
244 if (manifest.has_old_kernel_info())
245 LogPartitionInfoHash(manifest.old_kernel_info(), "old_kernel_info");
246 if (manifest.has_old_rootfs_info())
247 LogPartitionInfoHash(manifest.old_rootfs_info(), "old_rootfs_info");
248 if (manifest.has_new_kernel_info())
249 LogPartitionInfoHash(manifest.new_kernel_info(), "new_kernel_info");
250 if (manifest.has_new_rootfs_info())
251 LogPartitionInfoHash(manifest.new_rootfs_info(), "new_rootfs_info");
252}
253
254} // namespace {}
255
Don Garrett4d039442013-10-28 18:40:06 -0700256uint64_t DeltaPerformer::GetVersionOffset() {
257 // Manifest size is stored right after the magic string and the version.
258 return strlen(kDeltaMagic);
259}
260
Jay Srinivasanf4318702012-09-24 11:56:24 -0700261uint64_t DeltaPerformer::GetManifestSizeOffset() {
262 // Manifest size is stored right after the magic string and the version.
263 return strlen(kDeltaMagic) + kDeltaVersionSize;
264}
265
266uint64_t DeltaPerformer::GetManifestOffset() {
267 // Actual manifest begins right after the manifest size field.
268 return GetManifestSizeOffset() + kDeltaManifestSizeSize;
269}
270
271
Darin Petkov9574f7e2011-01-13 10:48:12 -0800272DeltaPerformer::MetadataParseResult DeltaPerformer::ParsePayloadMetadata(
273 const std::vector<char>& payload,
274 DeltaArchiveManifest* manifest,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700275 uint64_t* metadata_size,
David Zeuthena99981f2013-04-29 13:42:47 -0700276 ErrorCode* error) {
277 *error = kErrorCodeSuccess;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700278
Jay Srinivasanf4318702012-09-24 11:56:24 -0700279 // manifest_offset is the byte offset where the manifest protobuf begins.
280 const uint64_t manifest_offset = GetManifestOffset();
281 if (payload.size() < manifest_offset) {
282 // Don't have enough bytes to even know the manifest size.
Darin Petkov9574f7e2011-01-13 10:48:12 -0800283 return kMetadataParseInsufficientData;
284 }
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700285
Jay Srinivasanf4318702012-09-24 11:56:24 -0700286 // Validate the magic string.
Darin Petkov9574f7e2011-01-13 10:48:12 -0800287 if (memcmp(payload.data(), kDeltaMagic, strlen(kDeltaMagic)) != 0) {
288 LOG(ERROR) << "Bad payload format -- invalid delta magic.";
David Zeuthena99981f2013-04-29 13:42:47 -0700289 *error = kErrorCodeDownloadInvalidMetadataMagicString;
Darin Petkov9574f7e2011-01-13 10:48:12 -0800290 return kMetadataParseError;
291 }
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700292
Don Garrett4d039442013-10-28 18:40:06 -0700293 // Extract the payload version from the metadata.
294 uint64_t major_payload_version;
295 COMPILE_ASSERT(sizeof(major_payload_version) == kDeltaVersionSize,
296 major_payload_version_size_mismatch);
297 memcpy(&major_payload_version,
298 &payload[GetVersionOffset()],
299 kDeltaVersionSize);
300 // switch big endian to host
301 major_payload_version = be64toh(major_payload_version);
302
303 if (major_payload_version != kSupportedMajorPayloadVersion) {
304 LOG(ERROR) << "Bad payload format -- unsupported payload version: "
305 << major_payload_version;
306 *error = kErrorCodeUnsupportedMajorPayloadVersion;
307 return kMetadataParseError;
308 }
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700309
Jay Srinivasanf4318702012-09-24 11:56:24 -0700310 // Next, parse the manifest size.
311 uint64_t manifest_size;
312 COMPILE_ASSERT(sizeof(manifest_size) == kDeltaManifestSizeSize,
313 manifest_size_size_mismatch);
314 memcpy(&manifest_size,
315 &payload[GetManifestSizeOffset()],
316 kDeltaManifestSizeSize);
317 manifest_size = be64toh(manifest_size); // switch big endian to host
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700318
319 // Now, check if the metasize we computed matches what was passed in
320 // through Omaha Response.
Jay Srinivasanf4318702012-09-24 11:56:24 -0700321 *metadata_size = manifest_offset + manifest_size;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700322
Jay Srinivasanf4318702012-09-24 11:56:24 -0700323 // If the metadata size is present in install plan, check for it immediately
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700324 // even before waiting for that many number of bytes to be downloaded
325 // in the payload. This will prevent any attack which relies on us downloading
Jay Srinivasanf4318702012-09-24 11:56:24 -0700326 // data beyond the expected metadata size.
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800327 if (install_plan_->hash_checks_mandatory) {
328 if (install_plan_->metadata_size != *metadata_size) {
329 LOG(ERROR) << "Mandatory metadata size in Omaha response ("
330 << install_plan_->metadata_size << ") is missing/incorrect."
331 << ", Actual = " << *metadata_size;
David Zeuthena99981f2013-04-29 13:42:47 -0700332 *error = kErrorCodeDownloadInvalidMetadataSize;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800333 return kMetadataParseError;
334 }
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700335 }
336
337 // Now that we have validated the metadata size, we should wait for the full
338 // metadata to be read in before we can parse it.
339 if (payload.size() < *metadata_size) {
Darin Petkov9574f7e2011-01-13 10:48:12 -0800340 return kMetadataParseInsufficientData;
341 }
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700342
343 // Log whether we validated the size or simply trusting what's in the payload
Jay Srinivasanf4318702012-09-24 11:56:24 -0700344 // here. This is logged here (after we received the full metadata data) so
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700345 // that we just log once (instead of logging n times) if it takes n
346 // DeltaPerformer::Write calls to download the full manifest.
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800347 if (install_plan_->metadata_size == *metadata_size) {
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700348 LOG(INFO) << "Manifest size in payload matches expected value from Omaha";
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800349 } else {
350 // For mandatory-cases, we'd have already returned a kMetadataParseError
351 // above. We'll be here only for non-mandatory cases. Just send a UMA stat.
352 LOG(WARNING) << "Ignoring missing/incorrect metadata size ("
353 << install_plan_->metadata_size
354 << ") in Omaha response as validation is not mandatory. "
355 << "Trusting metadata size in payload = " << *metadata_size;
David Zeuthena99981f2013-04-29 13:42:47 -0700356 SendUmaStat(kErrorCodeDownloadInvalidMetadataSize);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800357 }
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700358
Jay Srinivasanf4318702012-09-24 11:56:24 -0700359 // We have the full metadata in |payload|. Verify its integrity
360 // and authenticity based on the information we have in Omaha response.
361 *error = ValidateMetadataSignature(&payload[0], *metadata_size);
David Zeuthena99981f2013-04-29 13:42:47 -0700362 if (*error != kErrorCodeSuccess) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800363 if (install_plan_->hash_checks_mandatory) {
David Zeuthenbc27aac2013-11-26 11:17:48 -0800364 // The autoupdate_CatchBadSignatures test checks for this string
365 // in log-files. Keep in sync.
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800366 LOG(ERROR) << "Mandatory metadata signature validation failed";
367 return kMetadataParseError;
368 }
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700369
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800370 // For non-mandatory cases, just send a UMA stat.
371 LOG(WARNING) << "Ignoring metadata signature validation failures";
372 SendUmaStat(*error);
David Zeuthena99981f2013-04-29 13:42:47 -0700373 *error = kErrorCodeSuccess;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700374 }
375
Jay Srinivasanf4318702012-09-24 11:56:24 -0700376 // The metadata in |payload| is deemed valid. So, it's now safe to
377 // parse the protobuf.
378 if (!manifest->ParseFromArray(&payload[manifest_offset], manifest_size)) {
Darin Petkov9574f7e2011-01-13 10:48:12 -0800379 LOG(ERROR) << "Unable to parse manifest in update file.";
David Zeuthena99981f2013-04-29 13:42:47 -0700380 *error = kErrorCodeDownloadManifestParseError;
Darin Petkov9574f7e2011-01-13 10:48:12 -0800381 return kMetadataParseError;
382 }
Darin Petkov9574f7e2011-01-13 10:48:12 -0800383 return kMetadataParseSuccess;
384}
385
386
Don Garrette410e0f2011-11-10 15:39:01 -0800387// Wrapper around write. Returns true if all requested bytes
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800388// were written, or false on any error, regardless of progress
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700389// and stores an action exit code in |error|.
390bool DeltaPerformer::Write(const void* bytes, size_t count,
David Zeuthena99981f2013-04-29 13:42:47 -0700391 ErrorCode *error) {
392 *error = kErrorCodeSuccess;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700393
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700394 const char* c_bytes = reinterpret_cast<const char*>(bytes);
395 buffer_.insert(buffer_.end(), c_bytes, c_bytes + count);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800396 system_state_->payload_state()->DownloadProgress(count);
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800397
398 // Update the total byte downloaded count and the progress logs.
399 total_bytes_received_ += count;
400 UpdateOverallProgress(false, "Completed ");
401
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700402 if (!manifest_valid_) {
Darin Petkov9574f7e2011-01-13 10:48:12 -0800403 MetadataParseResult result = ParsePayloadMetadata(buffer_,
404 &manifest_,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700405 &manifest_metadata_size_,
406 error);
Gilad Arnold5cac5912013-05-24 17:21:17 -0700407 if (result == kMetadataParseError)
Don Garrette410e0f2011-11-10 15:39:01 -0800408 return false;
Gilad Arnold5cac5912013-05-24 17:21:17 -0700409 if (result == kMetadataParseInsufficientData)
Don Garrette410e0f2011-11-10 15:39:01 -0800410 return true;
Gilad Arnold21504f02013-05-24 08:51:22 -0700411
412 // Checks the integrity of the payload manifest.
413 if ((*error = ValidateManifest()) != kErrorCodeSuccess)
414 return false;
415
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700416 // Remove protobuf and header info from buffer_, so buffer_ contains
417 // just data blobs
Darin Petkov437adc42010-10-07 13:12:24 -0700418 DiscardBufferHeadBytes(manifest_metadata_size_);
Darin Petkov73058b42010-10-06 16:32:19 -0700419 LOG_IF(WARNING, !prefs_->SetInt64(kPrefsManifestMetadataSize,
Darin Petkov437adc42010-10-07 13:12:24 -0700420 manifest_metadata_size_))
Darin Petkov73058b42010-10-06 16:32:19 -0700421 << "Unable to save the manifest metadata size.";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700422 manifest_valid_ = true;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700423
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700424 LogPartitionInfo(manifest_);
Darin Petkov9b230572010-10-08 10:20:09 -0700425 if (!PrimeUpdateState()) {
David Zeuthena99981f2013-04-29 13:42:47 -0700426 *error = kErrorCodeDownloadStateInitializationError;
Darin Petkov9b230572010-10-08 10:20:09 -0700427 LOG(ERROR) << "Unable to prime the update state.";
Don Garrette410e0f2011-11-10 15:39:01 -0800428 return false;
Darin Petkov9b230572010-10-08 10:20:09 -0700429 }
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800430
431 num_rootfs_operations_ = manifest_.install_operations_size();
432 num_total_operations_ =
433 num_rootfs_operations_ + manifest_.kernel_install_operations_size();
434 if (next_operation_num_ > 0)
435 UpdateOverallProgress(true, "Resuming after ");
436 LOG(INFO) << "Starting to apply update payload operations";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700437 }
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800438
439 while (next_operation_num_ < num_total_operations_) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700440 // Check if we should cancel the current attempt for any reason.
441 // In this case, *error will have already been populated with the reason
442 // why we're cancelling.
443 if (system_state_->update_attempter()->ShouldCancel(error))
444 return false;
445
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800446 const bool is_kernel_partition =
447 (next_operation_num_ >= num_rootfs_operations_);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700448 const DeltaArchiveManifest_InstallOperation &op =
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800449 is_kernel_partition ?
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700450 manifest_.kernel_install_operations(
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800451 next_operation_num_ - num_rootfs_operations_) :
452 manifest_.install_operations(next_operation_num_);
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700453 if (!CanPerformInstallOperation(op)) {
454 // This means we don't have enough bytes received yet to carry out the
455 // next operation.
456 return true;
457 }
458
Jay Srinivasanf4318702012-09-24 11:56:24 -0700459 // Validate the operation only if the metadata signature is present.
Jay Srinivasan00f76b62012-09-17 18:48:36 -0700460 // Otherwise, keep the old behavior. This serves as a knob to disable
461 // the validation logic in case we find some regression after rollout.
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800462 // NOTE: If hash checks are mandatory and if metadata_signature is empty,
463 // we would have already failed in ParsePayloadMetadata method and thus not
464 // even be here. So no need to handle that case again here.
Jay Srinivasanf4318702012-09-24 11:56:24 -0700465 if (!install_plan_->metadata_signature.empty()) {
Jay Srinivasan00f76b62012-09-17 18:48:36 -0700466 // Note: Validate must be called only if CanPerformInstallOperation is
467 // called. Otherwise, we might be failing operations before even if there
468 // isn't sufficient data to compute the proper hash.
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800469 *error = ValidateOperationHash(op);
David Zeuthena99981f2013-04-29 13:42:47 -0700470 if (*error != kErrorCodeSuccess) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800471 if (install_plan_->hash_checks_mandatory) {
472 LOG(ERROR) << "Mandatory operation hash check failed";
473 return false;
474 }
Jay Srinivasanf0572052012-10-23 18:12:56 -0700475
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800476 // For non-mandatory cases, just send a UMA stat.
477 LOG(WARNING) << "Ignoring operation validation errors";
Jay Srinivasanedce2832012-10-24 18:57:47 -0700478 SendUmaStat(*error);
David Zeuthena99981f2013-04-29 13:42:47 -0700479 *error = kErrorCodeSuccess;
Jay Srinivasan00f76b62012-09-17 18:48:36 -0700480 }
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700481 }
482
Darin Petkov45580e42010-10-08 14:02:40 -0700483 // Makes sure we unblock exit when this operation completes.
Darin Petkov9c0baf82010-10-07 13:44:48 -0700484 ScopedTerminatorExitUnblocker exit_unblocker =
485 ScopedTerminatorExitUnblocker(); // Avoids a compiler unused var bug.
Andrew de los Reyesbef0c7d2010-08-20 10:20:10 -0700486 // Log every thousandth operation, and also the first and last ones
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700487 if (op.type() == DeltaArchiveManifest_InstallOperation_Type_REPLACE ||
488 op.type() == DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700489 if (!PerformReplaceOperation(op, is_kernel_partition)) {
490 LOG(ERROR) << "Failed to perform replace operation "
491 << next_operation_num_;
David Zeuthena99981f2013-04-29 13:42:47 -0700492 *error = kErrorCodeDownloadOperationExecutionError;
Don Garrette410e0f2011-11-10 15:39:01 -0800493 return false;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700494 }
495 } else if (op.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700496 if (!PerformMoveOperation(op, is_kernel_partition)) {
497 LOG(ERROR) << "Failed to perform move operation "
498 << next_operation_num_;
David Zeuthena99981f2013-04-29 13:42:47 -0700499 *error = kErrorCodeDownloadOperationExecutionError;
Don Garrette410e0f2011-11-10 15:39:01 -0800500 return false;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700501 }
502 } else if (op.type() == DeltaArchiveManifest_InstallOperation_Type_BSDIFF) {
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700503 if (!PerformBsdiffOperation(op, is_kernel_partition)) {
504 LOG(ERROR) << "Failed to perform bsdiff operation "
505 << next_operation_num_;
David Zeuthena99981f2013-04-29 13:42:47 -0700506 *error = kErrorCodeDownloadOperationExecutionError;
Don Garrette410e0f2011-11-10 15:39:01 -0800507 return false;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700508 }
509 }
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800510
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700511 next_operation_num_++;
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800512 UpdateOverallProgress(false, "Completed ");
Darin Petkov73058b42010-10-06 16:32:19 -0700513 CheckpointUpdateProgress();
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700514 }
Don Garrette410e0f2011-11-10 15:39:01 -0800515 return true;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700516}
517
David Zeuthen8f191b22013-08-06 12:27:50 -0700518bool DeltaPerformer::IsManifestValid() {
519 return manifest_valid_;
520}
521
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700522bool DeltaPerformer::CanPerformInstallOperation(
523 const chromeos_update_engine::DeltaArchiveManifest_InstallOperation&
524 operation) {
525 // Move operations don't require any data blob, so they can always
526 // be performed
527 if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE)
528 return true;
529
530 // See if we have the entire data blob in the buffer
531 if (operation.data_offset() < buffer_offset_) {
532 LOG(ERROR) << "we threw away data it seems?";
533 return false;
534 }
Darin Petkovd7061ab2010-10-06 14:37:09 -0700535
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700536 return (operation.data_offset() + operation.data_length()) <=
537 (buffer_offset_ + buffer_.size());
538}
539
540bool DeltaPerformer::PerformReplaceOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700541 const DeltaArchiveManifest_InstallOperation& operation,
542 bool is_kernel_partition) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700543 CHECK(operation.type() == \
544 DeltaArchiveManifest_InstallOperation_Type_REPLACE || \
545 operation.type() == \
546 DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ);
547
548 // Since we delete data off the beginning of the buffer as we use it,
549 // the data we need should be exactly at the beginning of the buffer.
Darin Petkov9b230572010-10-08 10:20:09 -0700550 TEST_AND_RETURN_FALSE(buffer_offset_ == operation.data_offset());
551 TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length());
Darin Petkovd7061ab2010-10-06 14:37:09 -0700552
Darin Petkov437adc42010-10-07 13:12:24 -0700553 // Extract the signature message if it's in this operation.
554 ExtractSignatureMessage(operation);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700555
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700556 DirectExtentWriter direct_writer;
557 ZeroPadExtentWriter zero_pad_writer(&direct_writer);
558 scoped_ptr<BzipExtentWriter> bzip_writer;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700559
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700560 // Since bzip decompression is optional, we have a variable writer that will
561 // point to one of the ExtentWriter objects above.
562 ExtentWriter* writer = NULL;
563 if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_REPLACE) {
564 writer = &zero_pad_writer;
565 } else if (operation.type() ==
566 DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) {
567 bzip_writer.reset(new BzipExtentWriter(&zero_pad_writer));
568 writer = bzip_writer.get();
569 } else {
570 NOTREACHED();
571 }
572
573 // Create a vector of extents to pass to the ExtentWriter.
574 vector<Extent> extents;
575 for (int i = 0; i < operation.dst_extents_size(); i++) {
576 extents.push_back(operation.dst_extents(i));
577 }
578
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700579 int fd = is_kernel_partition ? kernel_fd_ : fd_;
580
581 TEST_AND_RETURN_FALSE(writer->Init(fd, extents, block_size_));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700582 TEST_AND_RETURN_FALSE(writer->Write(&buffer_[0], operation.data_length()));
583 TEST_AND_RETURN_FALSE(writer->End());
Darin Petkovd7061ab2010-10-06 14:37:09 -0700584
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700585 // Update buffer
586 buffer_offset_ += operation.data_length();
Darin Petkov437adc42010-10-07 13:12:24 -0700587 DiscardBufferHeadBytes(operation.data_length());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700588 return true;
589}
590
591bool DeltaPerformer::PerformMoveOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700592 const DeltaArchiveManifest_InstallOperation& operation,
593 bool is_kernel_partition) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700594 // Calculate buffer size. Note, this function doesn't do a sliding
595 // window to copy in case the source and destination blocks overlap.
596 // If we wanted to do a sliding window, we could program the server
597 // to generate deltas that effectively did a sliding window.
598
599 uint64_t blocks_to_read = 0;
600 for (int i = 0; i < operation.src_extents_size(); i++)
601 blocks_to_read += operation.src_extents(i).num_blocks();
602
603 uint64_t blocks_to_write = 0;
604 for (int i = 0; i < operation.dst_extents_size(); i++)
605 blocks_to_write += operation.dst_extents(i).num_blocks();
606
607 DCHECK_EQ(blocks_to_write, blocks_to_read);
608 vector<char> buf(blocks_to_write * block_size_);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700609
610 int fd = is_kernel_partition ? kernel_fd_ : fd_;
611
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700612 // Read in bytes.
613 ssize_t bytes_read = 0;
614 for (int i = 0; i < operation.src_extents_size(); i++) {
615 ssize_t bytes_read_this_iteration = 0;
616 const Extent& extent = operation.src_extents(i);
Darin Petkov8a075a72013-04-25 14:46:09 +0200617 const size_t bytes = extent.num_blocks() * block_size_;
618 if (extent.start_block() == kSparseHole) {
619 bytes_read_this_iteration = bytes;
620 memset(&buf[bytes_read], 0, bytes);
621 } else {
622 TEST_AND_RETURN_FALSE(utils::PReadAll(fd,
623 &buf[bytes_read],
624 bytes,
625 extent.start_block() * block_size_,
626 &bytes_read_this_iteration));
627 }
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700628 TEST_AND_RETURN_FALSE(
Darin Petkov8a075a72013-04-25 14:46:09 +0200629 bytes_read_this_iteration == static_cast<ssize_t>(bytes));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700630 bytes_read += bytes_read_this_iteration;
631 }
632
Darin Petkov45580e42010-10-08 14:02:40 -0700633 // If this is a non-idempotent operation, request a delayed exit and clear the
634 // update state in case the operation gets interrupted. Do this as late as
635 // possible.
636 if (!IsIdempotentOperation(operation)) {
637 Terminator::set_exit_blocked(true);
638 ResetUpdateProgress(prefs_, true);
639 }
640
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700641 // Write bytes out.
642 ssize_t bytes_written = 0;
643 for (int i = 0; i < operation.dst_extents_size(); i++) {
644 const Extent& extent = operation.dst_extents(i);
Darin Petkov8a075a72013-04-25 14:46:09 +0200645 const size_t bytes = extent.num_blocks() * block_size_;
646 if (extent.start_block() == kSparseHole) {
Darin Petkov741a8222013-05-02 10:02:34 +0200647 DCHECK(buf.begin() + bytes_written ==
648 std::search_n(buf.begin() + bytes_written,
649 buf.begin() + bytes_written + bytes,
650 bytes, 0));
Darin Petkov8a075a72013-04-25 14:46:09 +0200651 } else {
652 TEST_AND_RETURN_FALSE(
653 utils::PWriteAll(fd,
654 &buf[bytes_written],
655 bytes,
656 extent.start_block() * block_size_));
657 }
658 bytes_written += bytes;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700659 }
660 DCHECK_EQ(bytes_written, bytes_read);
661 DCHECK_EQ(bytes_written, static_cast<ssize_t>(buf.size()));
662 return true;
663}
664
665bool DeltaPerformer::ExtentsToBsdiffPositionsString(
666 const RepeatedPtrField<Extent>& extents,
667 uint64_t block_size,
668 uint64_t full_length,
669 string* positions_string) {
670 string ret;
671 uint64_t length = 0;
672 for (int i = 0; i < extents.size(); i++) {
673 Extent extent = extents.Get(i);
674 int64_t start = extent.start_block();
675 uint64_t this_length = min(full_length - length,
676 extent.num_blocks() * block_size);
677 if (start == static_cast<int64_t>(kSparseHole))
678 start = -1;
679 else
680 start *= block_size;
681 ret += StringPrintf("%" PRIi64 ":%" PRIu64 ",", start, this_length);
682 length += this_length;
683 }
684 TEST_AND_RETURN_FALSE(length == full_length);
685 if (!ret.empty())
686 ret.resize(ret.size() - 1); // Strip trailing comma off
687 *positions_string = ret;
688 return true;
689}
690
691bool DeltaPerformer::PerformBsdiffOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700692 const DeltaArchiveManifest_InstallOperation& operation,
693 bool is_kernel_partition) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700694 // Since we delete data off the beginning of the buffer as we use it,
695 // the data we need should be exactly at the beginning of the buffer.
Darin Petkov9b230572010-10-08 10:20:09 -0700696 TEST_AND_RETURN_FALSE(buffer_offset_ == operation.data_offset());
697 TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700698
699 string input_positions;
700 TEST_AND_RETURN_FALSE(ExtentsToBsdiffPositionsString(operation.src_extents(),
701 block_size_,
702 operation.src_length(),
703 &input_positions));
704 string output_positions;
705 TEST_AND_RETURN_FALSE(ExtentsToBsdiffPositionsString(operation.dst_extents(),
706 block_size_,
707 operation.dst_length(),
708 &output_positions));
709
710 string temp_filename;
711 TEST_AND_RETURN_FALSE(utils::MakeTempFile("/tmp/au_patch.XXXXXX",
712 &temp_filename,
713 NULL));
714 ScopedPathUnlinker path_unlinker(temp_filename);
715 {
716 int fd = open(temp_filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644);
717 ScopedFdCloser fd_closer(&fd);
718 TEST_AND_RETURN_FALSE(
719 utils::WriteAll(fd, &buffer_[0], operation.data_length()));
720 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700721
Darin Petkov7f2ec752013-04-03 14:45:19 +0200722 // Update the buffer to release the patch data memory as soon as the patch
723 // file is written out.
724 buffer_offset_ += operation.data_length();
725 DiscardBufferHeadBytes(operation.data_length());
726
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700727 int fd = is_kernel_partition ? kernel_fd_ : fd_;
Darin Petkov3d1670d2013-07-12 14:37:06 +0200728 const string path = StringPrintf("/proc/self/fd/%d", fd);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700729
Darin Petkov45580e42010-10-08 14:02:40 -0700730 // If this is a non-idempotent operation, request a delayed exit and clear the
731 // update state in case the operation gets interrupted. Do this as late as
732 // possible.
733 if (!IsIdempotentOperation(operation)) {
734 Terminator::set_exit_blocked(true);
735 ResetUpdateProgress(prefs_, true);
736 }
737
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700738 vector<string> cmd;
739 cmd.push_back(kBspatchPath);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700740 cmd.push_back(path);
741 cmd.push_back(path);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700742 cmd.push_back(temp_filename);
743 cmd.push_back(input_positions);
744 cmd.push_back(output_positions);
745 int return_code = 0;
Andrew de los Reyes5a232832010-10-12 16:20:54 -0700746 TEST_AND_RETURN_FALSE(
747 Subprocess::SynchronousExecFlags(cmd,
Darin Petkov85d02b72011-05-17 13:25:51 -0700748 G_SPAWN_LEAVE_DESCRIPTORS_OPEN,
Andrew de los Reyes5a232832010-10-12 16:20:54 -0700749 &return_code,
Darin Petkov85d02b72011-05-17 13:25:51 -0700750 NULL));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700751 TEST_AND_RETURN_FALSE(return_code == 0);
752
753 if (operation.dst_length() % block_size_) {
754 // Zero out rest of final block.
755 // TODO(adlr): build this into bspatch; it's more efficient that way.
756 const Extent& last_extent =
757 operation.dst_extents(operation.dst_extents_size() - 1);
758 const uint64_t end_byte =
759 (last_extent.start_block() + last_extent.num_blocks()) * block_size_;
760 const uint64_t begin_byte =
761 end_byte - (block_size_ - operation.dst_length() % block_size_);
762 vector<char> zeros(end_byte - begin_byte);
763 TEST_AND_RETURN_FALSE(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700764 utils::PWriteAll(fd, &zeros[0], end_byte - begin_byte, begin_byte));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700765 }
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700766 return true;
767}
768
Darin Petkovd7061ab2010-10-06 14:37:09 -0700769bool DeltaPerformer::ExtractSignatureMessage(
770 const DeltaArchiveManifest_InstallOperation& operation) {
771 if (operation.type() != DeltaArchiveManifest_InstallOperation_Type_REPLACE ||
772 !manifest_.has_signatures_offset() ||
773 manifest_.signatures_offset() != operation.data_offset()) {
774 return false;
775 }
776 TEST_AND_RETURN_FALSE(manifest_.has_signatures_size() &&
777 manifest_.signatures_size() == operation.data_length());
778 TEST_AND_RETURN_FALSE(signatures_message_data_.empty());
779 TEST_AND_RETURN_FALSE(buffer_offset_ == manifest_.signatures_offset());
780 TEST_AND_RETURN_FALSE(buffer_.size() >= manifest_.signatures_size());
Darin Petkov4f0a07b2011-05-25 16:47:20 -0700781 signatures_message_data_.assign(
Darin Petkovd7061ab2010-10-06 14:37:09 -0700782 buffer_.begin(),
783 buffer_.begin() + manifest_.signatures_size());
Darin Petkov4f0a07b2011-05-25 16:47:20 -0700784
785 // Save the signature blob because if the update is interrupted after the
786 // download phase we don't go through this path anymore. Some alternatives to
787 // consider:
788 //
789 // 1. On resume, re-download the signature blob from the server and re-verify
790 // it.
791 //
792 // 2. Verify the signature as soon as it's received and don't checkpoint the
793 // blob and the signed sha-256 context.
794 LOG_IF(WARNING, !prefs_->SetString(kPrefsUpdateStateSignatureBlob,
795 string(&signatures_message_data_[0],
796 signatures_message_data_.size())))
797 << "Unable to store the signature blob.";
Darin Petkov437adc42010-10-07 13:12:24 -0700798 // The hash of all data consumed so far should be verified against the signed
799 // hash.
800 signed_hash_context_ = hash_calculator_.GetContext();
801 LOG_IF(WARNING, !prefs_->SetString(kPrefsUpdateStateSignedSHA256Context,
802 signed_hash_context_))
803 << "Unable to store the signed hash context.";
Darin Petkovd7061ab2010-10-06 14:37:09 -0700804 LOG(INFO) << "Extracted signature data of size "
805 << manifest_.signatures_size() << " at "
806 << manifest_.signatures_offset();
807 return true;
808}
809
David Zeuthene7f89172013-10-31 10:21:04 -0700810bool DeltaPerformer::GetPublicKeyFromResponse(base::FilePath *out_tmp_key) {
811 if (system_state_->hardware()->IsOfficialBuild() ||
812 utils::FileExists(public_key_path_.c_str()) ||
813 install_plan_->public_key_rsa.empty())
814 return false;
815
816 if (!utils::DecodeAndStoreBase64String(install_plan_->public_key_rsa,
817 out_tmp_key))
818 return false;
819
820 return true;
821}
822
David Zeuthena99981f2013-04-29 13:42:47 -0700823ErrorCode DeltaPerformer::ValidateMetadataSignature(
Jay Srinivasanf4318702012-09-24 11:56:24 -0700824 const char* metadata, uint64_t metadata_size) {
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700825
Jay Srinivasanf4318702012-09-24 11:56:24 -0700826 if (install_plan_->metadata_signature.empty()) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800827 if (install_plan_->hash_checks_mandatory) {
828 LOG(ERROR) << "Missing mandatory metadata signature in Omaha response";
David Zeuthena99981f2013-04-29 13:42:47 -0700829 return kErrorCodeDownloadMetadataSignatureMissingError;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800830 }
831
832 // For non-mandatory cases, just send a UMA stat.
Jay Srinivasanf4318702012-09-24 11:56:24 -0700833 LOG(WARNING) << "Cannot validate metadata as the signature is empty";
David Zeuthena99981f2013-04-29 13:42:47 -0700834 SendUmaStat(kErrorCodeDownloadMetadataSignatureMissingError);
835 return kErrorCodeSuccess;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700836 }
837
838 // Convert base64-encoded signature to raw bytes.
Jay Srinivasanf4318702012-09-24 11:56:24 -0700839 vector<char> metadata_signature;
840 if (!OmahaHashCalculator::Base64Decode(install_plan_->metadata_signature,
841 &metadata_signature)) {
842 LOG(ERROR) << "Unable to decode base64 metadata signature: "
843 << install_plan_->metadata_signature;
David Zeuthena99981f2013-04-29 13:42:47 -0700844 return kErrorCodeDownloadMetadataSignatureError;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700845 }
846
David Zeuthene7f89172013-10-31 10:21:04 -0700847 // See if we should use the public RSA key in the Omaha response.
848 base::FilePath path_to_public_key(public_key_path_);
849 base::FilePath tmp_key;
850 if (GetPublicKeyFromResponse(&tmp_key))
851 path_to_public_key = tmp_key;
852 ScopedPathUnlinker tmp_key_remover(tmp_key.value());
853 if (tmp_key.empty())
854 tmp_key_remover.set_should_remove(false);
855
856 LOG(INFO) << "Verifying metadata hash signature using public key: "
857 << path_to_public_key.value();
858
Jay Srinivasanf4318702012-09-24 11:56:24 -0700859 vector<char> expected_metadata_hash;
860 if (!PayloadSigner::GetRawHashFromSignature(metadata_signature,
David Zeuthene7f89172013-10-31 10:21:04 -0700861 path_to_public_key.value(),
Jay Srinivasanf4318702012-09-24 11:56:24 -0700862 &expected_metadata_hash)) {
863 LOG(ERROR) << "Unable to compute expected hash from metadata signature";
David Zeuthena99981f2013-04-29 13:42:47 -0700864 return kErrorCodeDownloadMetadataSignatureError;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700865 }
866
Jay Srinivasanf4318702012-09-24 11:56:24 -0700867 OmahaHashCalculator metadata_hasher;
868 metadata_hasher.Update(metadata, metadata_size);
869 if (!metadata_hasher.Finalize()) {
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700870 LOG(ERROR) << "Unable to compute actual hash of manifest";
David Zeuthena99981f2013-04-29 13:42:47 -0700871 return kErrorCodeDownloadMetadataSignatureVerificationError;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700872 }
873
Jay Srinivasanf4318702012-09-24 11:56:24 -0700874 vector<char> calculated_metadata_hash = metadata_hasher.raw_hash();
875 PayloadSigner::PadRSA2048SHA256Hash(&calculated_metadata_hash);
876 if (calculated_metadata_hash.empty()) {
877 LOG(ERROR) << "Computed actual hash of metadata is empty.";
David Zeuthena99981f2013-04-29 13:42:47 -0700878 return kErrorCodeDownloadMetadataSignatureVerificationError;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700879 }
880
Jay Srinivasanf4318702012-09-24 11:56:24 -0700881 if (calculated_metadata_hash != expected_metadata_hash) {
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700882 LOG(ERROR) << "Manifest hash verification failed. Expected hash = ";
Jay Srinivasanf4318702012-09-24 11:56:24 -0700883 utils::HexDumpVector(expected_metadata_hash);
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700884 LOG(ERROR) << "Calculated hash = ";
Jay Srinivasanf4318702012-09-24 11:56:24 -0700885 utils::HexDumpVector(calculated_metadata_hash);
David Zeuthena99981f2013-04-29 13:42:47 -0700886 return kErrorCodeDownloadMetadataSignatureMismatch;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700887 }
888
David Zeuthenbc27aac2013-11-26 11:17:48 -0800889 // The autoupdate_CatchBadSignatures test checks for this string in
890 // log-files. Keep in sync.
David Zeuthene7f89172013-10-31 10:21:04 -0700891 LOG(INFO) << "Metadata hash signature matches value in Omaha response.";
David Zeuthena99981f2013-04-29 13:42:47 -0700892 return kErrorCodeSuccess;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700893}
894
Gilad Arnold21504f02013-05-24 08:51:22 -0700895ErrorCode DeltaPerformer::ValidateManifest() {
Don Garrettb8dd1d92013-11-22 17:40:02 -0800896 // Perform assorted checks to sanity check the manifest, make sure it
897 // matches data from other sources, and that it is a supported version.
Gilad Arnold21504f02013-05-24 08:51:22 -0700898 //
899 // TODO(garnold) in general, the presence of an old partition hash should be
900 // the sole indicator for a delta update, as we would generally like update
901 // payloads to be self contained and not assume an Omaha response to tell us
902 // that. However, since this requires some massive reengineering of the update
903 // flow (making filesystem copying happen conditionally only *after*
904 // downloading and parsing of the update manifest) we'll put it off for now.
905 // See chromium-os:7597 for further discussion.
Don Garrettb8dd1d92013-11-22 17:40:02 -0800906 if (install_plan_->is_full_update) {
907 if (manifest_.has_old_kernel_info() || manifest_.has_old_rootfs_info()) {
908 LOG(ERROR) << "Purported full payload contains old partition "
909 "hash(es), aborting update";
910 return kErrorCodePayloadMismatchedType;
911 }
912
913 if (manifest_.minor_version() != kFullPayloadMinorVersion) {
914 LOG(ERROR) << "Manifest contains minor version "
915 << manifest_.minor_version()
916 << ", but all full payloads should have version "
917 << kFullPayloadMinorVersion << ".";
918 return kErrorCodeUnsupportedMinorPayloadVersion;
919 }
920 } else {
921 if (manifest_.minor_version() != kSupportedMinorPayloadVersion) {
922 LOG(ERROR) << "Manifest contains minor version "
923 << manifest_.minor_version()
924 << " not the supported "
925 << kSupportedMinorPayloadVersion;
926 return kErrorCodeUnsupportedMinorPayloadVersion;
927 }
Gilad Arnold21504f02013-05-24 08:51:22 -0700928 }
929
930 // TODO(garnold) we should be adding more and more manifest checks, such as
931 // partition boundaries etc (see chromium-os:37661).
932
933 return kErrorCodeSuccess;
934}
935
David Zeuthena99981f2013-04-29 13:42:47 -0700936ErrorCode DeltaPerformer::ValidateOperationHash(
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800937 const DeltaArchiveManifest_InstallOperation& operation) {
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700938
Jay Srinivasan00f76b62012-09-17 18:48:36 -0700939 if (!operation.data_sha256_hash().size()) {
940 if (!operation.data_length()) {
941 // Operations that do not have any data blob won't have any operation hash
942 // either. So, these operations are always considered validated since the
Jay Srinivasanf4318702012-09-24 11:56:24 -0700943 // metadata that contains all the non-data-blob portions of the operation
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800944 // has already been validated. This is true for both HTTP and HTTPS cases.
David Zeuthena99981f2013-04-29 13:42:47 -0700945 return kErrorCodeSuccess;
Jay Srinivasan00f76b62012-09-17 18:48:36 -0700946 }
947
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800948 // No hash is present for an operation that has data blobs. This shouldn't
949 // happen normally for any client that has this code, because the
Jay Srinivasan00f76b62012-09-17 18:48:36 -0700950 // corresponding update should have been produced with the operation
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800951 // hashes. So if it happens it means either we've turned operation hash
952 // generation off in DeltaDiffGenerator or it's a regression of some sort.
Jay Srinivasan00f76b62012-09-17 18:48:36 -0700953 // One caveat though: The last operation is a dummy signature operation
954 // that doesn't have a hash at the time the manifest is created. So we
955 // should not complaint about that operation. This operation can be
956 // recognized by the fact that it's offset is mentioned in the manifest.
957 if (manifest_.signatures_offset() &&
958 manifest_.signatures_offset() == operation.data_offset()) {
959 LOG(INFO) << "Skipping hash verification for signature operation "
960 << next_operation_num_ + 1;
961 } else {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800962 if (install_plan_->hash_checks_mandatory) {
963 LOG(ERROR) << "Missing mandatory operation hash for operation "
964 << next_operation_num_ + 1;
David Zeuthena99981f2013-04-29 13:42:47 -0700965 return kErrorCodeDownloadOperationHashMissingError;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800966 }
967
968 // For non-mandatory cases, just send a UMA stat.
969 LOG(WARNING) << "Cannot validate operation " << next_operation_num_ + 1
970 << " as there's no operation hash in manifest";
David Zeuthena99981f2013-04-29 13:42:47 -0700971 SendUmaStat(kErrorCodeDownloadOperationHashMissingError);
Jay Srinivasan00f76b62012-09-17 18:48:36 -0700972 }
David Zeuthena99981f2013-04-29 13:42:47 -0700973 return kErrorCodeSuccess;
Jay Srinivasan00f76b62012-09-17 18:48:36 -0700974 }
975
976 vector<char> expected_op_hash;
977 expected_op_hash.assign(operation.data_sha256_hash().data(),
978 (operation.data_sha256_hash().data() +
979 operation.data_sha256_hash().size()));
980
981 OmahaHashCalculator operation_hasher;
982 operation_hasher.Update(&buffer_[0], operation.data_length());
983 if (!operation_hasher.Finalize()) {
984 LOG(ERROR) << "Unable to compute actual hash of operation "
985 << next_operation_num_;
David Zeuthena99981f2013-04-29 13:42:47 -0700986 return kErrorCodeDownloadOperationHashVerificationError;
Jay Srinivasan00f76b62012-09-17 18:48:36 -0700987 }
988
989 vector<char> calculated_op_hash = operation_hasher.raw_hash();
990 if (calculated_op_hash != expected_op_hash) {
991 LOG(ERROR) << "Hash verification failed for operation "
992 << next_operation_num_ << ". Expected hash = ";
993 utils::HexDumpVector(expected_op_hash);
994 LOG(ERROR) << "Calculated hash over " << operation.data_length()
995 << " bytes at offset: " << operation.data_offset() << " = ";
996 utils::HexDumpVector(calculated_op_hash);
David Zeuthena99981f2013-04-29 13:42:47 -0700997 return kErrorCodeDownloadOperationHashMismatch;
Jay Srinivasan00f76b62012-09-17 18:48:36 -0700998 }
999
David Zeuthena99981f2013-04-29 13:42:47 -07001000 return kErrorCodeSuccess;
Jay Srinivasan51dcf262012-09-13 17:24:32 -07001001}
1002
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -07001003#define TEST_AND_RETURN_VAL(_retval, _condition) \
1004 do { \
1005 if (!(_condition)) { \
1006 LOG(ERROR) << "VerifyPayload failure: " << #_condition; \
1007 return _retval; \
1008 } \
1009 } while (0);
Andrew de los Reyesfb830ba2011-04-04 11:42:43 -07001010
David Zeuthena99981f2013-04-29 13:42:47 -07001011ErrorCode DeltaPerformer::VerifyPayload(
Darin Petkov437adc42010-10-07 13:12:24 -07001012 const std::string& update_check_response_hash,
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -07001013 const uint64_t update_check_response_size) {
David Zeuthene7f89172013-10-31 10:21:04 -07001014
1015 // See if we should use the public RSA key in the Omaha response.
1016 base::FilePath path_to_public_key(public_key_path_);
1017 base::FilePath tmp_key;
1018 if (GetPublicKeyFromResponse(&tmp_key))
1019 path_to_public_key = tmp_key;
1020 ScopedPathUnlinker tmp_key_remover(tmp_key.value());
1021 if (tmp_key.empty())
1022 tmp_key_remover.set_should_remove(false);
1023
1024 LOG(INFO) << "Verifying payload using public key: "
1025 << path_to_public_key.value();
Darin Petkov437adc42010-10-07 13:12:24 -07001026
Jay Srinivasan0d8fb402012-05-07 19:19:38 -07001027 // Verifies the download size.
David Zeuthena99981f2013-04-29 13:42:47 -07001028 TEST_AND_RETURN_VAL(kErrorCodePayloadSizeMismatchError,
Jay Srinivasan0d8fb402012-05-07 19:19:38 -07001029 update_check_response_size ==
1030 manifest_metadata_size_ + buffer_offset_);
1031
Jay Srinivasan51dcf262012-09-13 17:24:32 -07001032 // Verifies the payload hash.
1033 const string& payload_hash_data = hash_calculator_.hash();
David Zeuthena99981f2013-04-29 13:42:47 -07001034 TEST_AND_RETURN_VAL(kErrorCodeDownloadPayloadVerificationError,
Jay Srinivasan51dcf262012-09-13 17:24:32 -07001035 !payload_hash_data.empty());
David Zeuthena99981f2013-04-29 13:42:47 -07001036 TEST_AND_RETURN_VAL(kErrorCodePayloadHashMismatchError,
Jay Srinivasan51dcf262012-09-13 17:24:32 -07001037 payload_hash_data == update_check_response_hash);
Darin Petkov437adc42010-10-07 13:12:24 -07001038
Darin Petkov437adc42010-10-07 13:12:24 -07001039 // Verifies the signed payload hash.
David Zeuthene7f89172013-10-31 10:21:04 -07001040 if (!utils::FileExists(path_to_public_key.value().c_str())) {
Darin Petkov437adc42010-10-07 13:12:24 -07001041 LOG(WARNING) << "Not verifying signed delta payload -- missing public key.";
David Zeuthena99981f2013-04-29 13:42:47 -07001042 return kErrorCodeSuccess;
Darin Petkovd7061ab2010-10-06 14:37:09 -07001043 }
David Zeuthena99981f2013-04-29 13:42:47 -07001044 TEST_AND_RETURN_VAL(kErrorCodeSignedDeltaPayloadExpectedError,
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -07001045 !signatures_message_data_.empty());
Darin Petkovd7061ab2010-10-06 14:37:09 -07001046 vector<char> signed_hash_data;
David Zeuthena99981f2013-04-29 13:42:47 -07001047 TEST_AND_RETURN_VAL(kErrorCodeDownloadPayloadPubKeyVerificationError,
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -07001048 PayloadSigner::VerifySignature(
1049 signatures_message_data_,
David Zeuthene7f89172013-10-31 10:21:04 -07001050 path_to_public_key.value(),
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -07001051 &signed_hash_data));
Darin Petkov437adc42010-10-07 13:12:24 -07001052 OmahaHashCalculator signed_hasher;
David Zeuthena99981f2013-04-29 13:42:47 -07001053 TEST_AND_RETURN_VAL(kErrorCodeDownloadPayloadPubKeyVerificationError,
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -07001054 signed_hasher.SetContext(signed_hash_context_));
David Zeuthena99981f2013-04-29 13:42:47 -07001055 TEST_AND_RETURN_VAL(kErrorCodeDownloadPayloadPubKeyVerificationError,
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -07001056 signed_hasher.Finalize());
Andrew de los Reyesbdfaaf02011-03-30 10:35:12 -07001057 vector<char> hash_data = signed_hasher.raw_hash();
1058 PayloadSigner::PadRSA2048SHA256Hash(&hash_data);
David Zeuthena99981f2013-04-29 13:42:47 -07001059 TEST_AND_RETURN_VAL(kErrorCodeDownloadPayloadPubKeyVerificationError,
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -07001060 !hash_data.empty());
Andrew de los Reyesfb830ba2011-04-04 11:42:43 -07001061 if (hash_data != signed_hash_data) {
David Zeuthenbc27aac2013-11-26 11:17:48 -08001062 // The autoupdate_CatchBadSignatures test checks for this string
1063 // in log-files. Keep in sync.
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -07001064 LOG(ERROR) << "Public key verification failed, thus update failed. "
Andrew de los Reyesfb830ba2011-04-04 11:42:43 -07001065 "Attached Signature:";
1066 utils::HexDumpVector(signed_hash_data);
1067 LOG(ERROR) << "Computed Signature:";
1068 utils::HexDumpVector(hash_data);
David Zeuthena99981f2013-04-29 13:42:47 -07001069 return kErrorCodeDownloadPayloadPubKeyVerificationError;
Andrew de los Reyesfb830ba2011-04-04 11:42:43 -07001070 }
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -08001071
David Zeuthene7f89172013-10-31 10:21:04 -07001072 LOG(INFO) << "Payload hash matches value in payload.";
1073
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -08001074 // At this point, we are guaranteed to have downloaded a full payload, i.e
1075 // the one whose size matches the size mentioned in Omaha response. If any
1076 // errors happen after this, it's likely a problem with the payload itself or
1077 // the state of the system and not a problem with the URL or network. So,
Jay Srinivasan08262882012-12-28 19:29:43 -08001078 // indicate that to the payload state so that AU can backoff appropriately.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -08001079 system_state_->payload_state()->DownloadComplete();
1080
David Zeuthena99981f2013-04-29 13:42:47 -07001081 return kErrorCodeSuccess;
Darin Petkovd7061ab2010-10-06 14:37:09 -07001082}
1083
Darin Petkov3aefa862010-12-07 14:45:00 -08001084bool DeltaPerformer::GetNewPartitionInfo(uint64_t* kernel_size,
1085 vector<char>* kernel_hash,
1086 uint64_t* rootfs_size,
1087 vector<char>* rootfs_hash) {
Darin Petkov2dd01092010-10-08 15:43:05 -07001088 TEST_AND_RETURN_FALSE(manifest_valid_ &&
1089 manifest_.has_new_kernel_info() &&
1090 manifest_.has_new_rootfs_info());
Darin Petkov3aefa862010-12-07 14:45:00 -08001091 *kernel_size = manifest_.new_kernel_info().size();
1092 *rootfs_size = manifest_.new_rootfs_info().size();
1093 vector<char> new_kernel_hash(manifest_.new_kernel_info().hash().begin(),
1094 manifest_.new_kernel_info().hash().end());
1095 vector<char> new_rootfs_hash(manifest_.new_rootfs_info().hash().begin(),
1096 manifest_.new_rootfs_info().hash().end());
1097 kernel_hash->swap(new_kernel_hash);
1098 rootfs_hash->swap(new_rootfs_hash);
Darin Petkov2dd01092010-10-08 15:43:05 -07001099 return true;
1100}
1101
Andrew de los Reyes100bb7d2011-08-09 17:35:07 -07001102namespace {
1103void LogVerifyError(bool is_kern,
1104 const string& local_hash,
1105 const string& expected_hash) {
1106 const char* type = is_kern ? "kernel" : "rootfs";
1107 LOG(ERROR) << "This is a server-side error due to "
1108 << "mismatched delta update image!";
1109 LOG(ERROR) << "The delta I've been given contains a " << type << " delta "
1110 << "update that must be applied over a " << type << " with "
1111 << "a specific checksum, but the " << type << " we're starting "
1112 << "with doesn't have that checksum! This means that "
1113 << "the delta I've been given doesn't match my existing "
1114 << "system. The " << type << " partition I have has hash: "
1115 << local_hash << " but the update expected me to have "
1116 << expected_hash << " .";
1117 if (is_kern) {
1118 LOG(INFO) << "To get the checksum of a kernel partition on a "
1119 << "booted machine, run this command (change /dev/sda2 "
1120 << "as needed): dd if=/dev/sda2 bs=1M 2>/dev/null | "
1121 << "openssl dgst -sha256 -binary | openssl base64";
1122 } else {
1123 LOG(INFO) << "To get the checksum of a rootfs partition on a "
1124 << "booted machine, run this command (change /dev/sda3 "
1125 << "as needed): dd if=/dev/sda3 bs=1M count=$(( "
1126 << "$(dumpe2fs /dev/sda3 2>/dev/null | grep 'Block count' "
1127 << "| sed 's/[^0-9]*//') / 256 )) | "
1128 << "openssl dgst -sha256 -binary | openssl base64";
1129 }
1130 LOG(INFO) << "To get the checksum of partitions in a bin file, "
1131 << "run: .../src/scripts/sha256_partitions.sh .../file.bin";
1132}
1133
1134string StringForHashBytes(const void* bytes, size_t size) {
1135 string ret;
1136 if (!OmahaHashCalculator::Base64Encode(bytes, size, &ret)) {
1137 ret = "<unknown>";
1138 }
1139 return ret;
1140}
1141} // namespace
1142
Darin Petkov698d0412010-10-13 10:59:44 -07001143bool DeltaPerformer::VerifySourcePartitions() {
1144 LOG(INFO) << "Verifying source partitions.";
1145 CHECK(manifest_valid_);
Jay Srinivasan51dcf262012-09-13 17:24:32 -07001146 CHECK(install_plan_);
Darin Petkov698d0412010-10-13 10:59:44 -07001147 if (manifest_.has_old_kernel_info()) {
1148 const PartitionInfo& info = manifest_.old_kernel_info();
Jay Srinivasan51dcf262012-09-13 17:24:32 -07001149 bool valid =
1150 !install_plan_->kernel_hash.empty() &&
1151 install_plan_->kernel_hash.size() == info.hash().size() &&
1152 memcmp(install_plan_->kernel_hash.data(),
Andrew de los Reyes100bb7d2011-08-09 17:35:07 -07001153 info.hash().data(),
Jay Srinivasan51dcf262012-09-13 17:24:32 -07001154 install_plan_->kernel_hash.size()) == 0;
Andrew de los Reyes100bb7d2011-08-09 17:35:07 -07001155 if (!valid) {
1156 LogVerifyError(true,
Jay Srinivasan51dcf262012-09-13 17:24:32 -07001157 StringForHashBytes(install_plan_->kernel_hash.data(),
1158 install_plan_->kernel_hash.size()),
Andrew de los Reyes100bb7d2011-08-09 17:35:07 -07001159 StringForHashBytes(info.hash().data(),
1160 info.hash().size()));
1161 }
1162 TEST_AND_RETURN_FALSE(valid);
Darin Petkov698d0412010-10-13 10:59:44 -07001163 }
1164 if (manifest_.has_old_rootfs_info()) {
1165 const PartitionInfo& info = manifest_.old_rootfs_info();
Jay Srinivasan51dcf262012-09-13 17:24:32 -07001166 bool valid =
1167 !install_plan_->rootfs_hash.empty() &&
1168 install_plan_->rootfs_hash.size() == info.hash().size() &&
1169 memcmp(install_plan_->rootfs_hash.data(),
Andrew de los Reyes100bb7d2011-08-09 17:35:07 -07001170 info.hash().data(),
Jay Srinivasan51dcf262012-09-13 17:24:32 -07001171 install_plan_->rootfs_hash.size()) == 0;
Andrew de los Reyes100bb7d2011-08-09 17:35:07 -07001172 if (!valid) {
1173 LogVerifyError(false,
Chris Sosa670d6802013-03-29 14:17:45 -07001174 StringForHashBytes(install_plan_->rootfs_hash.data(),
1175 install_plan_->rootfs_hash.size()),
Andrew de los Reyes100bb7d2011-08-09 17:35:07 -07001176 StringForHashBytes(info.hash().data(),
1177 info.hash().size()));
1178 }
1179 TEST_AND_RETURN_FALSE(valid);
Darin Petkov698d0412010-10-13 10:59:44 -07001180 }
1181 return true;
1182}
1183
Darin Petkov437adc42010-10-07 13:12:24 -07001184void DeltaPerformer::DiscardBufferHeadBytes(size_t count) {
1185 hash_calculator_.Update(&buffer_[0], count);
Darin Petkov7f2ec752013-04-03 14:45:19 +02001186 // Copy the remainder data into a temporary vector first to ensure that any
1187 // unused memory in the updated |buffer_| will be released.
1188 vector<char> temp(buffer_.begin() + count, buffer_.end());
1189 buffer_.swap(temp);
Darin Petkovd7061ab2010-10-06 14:37:09 -07001190}
1191
Darin Petkov0406e402010-10-06 21:33:11 -07001192bool DeltaPerformer::CanResumeUpdate(PrefsInterface* prefs,
1193 string update_check_response_hash) {
1194 int64_t next_operation = kUpdateStateOperationInvalid;
1195 TEST_AND_RETURN_FALSE(prefs->GetInt64(kPrefsUpdateStateNextOperation,
1196 &next_operation) &&
1197 next_operation != kUpdateStateOperationInvalid &&
1198 next_operation > 0);
1199
1200 string interrupted_hash;
1201 TEST_AND_RETURN_FALSE(prefs->GetString(kPrefsUpdateCheckResponseHash,
1202 &interrupted_hash) &&
David Zeuthenc41c2282013-06-17 16:33:06 -07001203 !interrupted_hash.empty() &&
1204 interrupted_hash == update_check_response_hash);
Darin Petkov0406e402010-10-06 21:33:11 -07001205
Darin Petkov61426142010-10-08 11:04:55 -07001206 int64_t resumed_update_failures;
1207 TEST_AND_RETURN_FALSE(!prefs->GetInt64(kPrefsResumedUpdateFailures,
1208 &resumed_update_failures) ||
1209 resumed_update_failures <= kMaxResumedUpdateFailures);
1210
Darin Petkov0406e402010-10-06 21:33:11 -07001211 // Sanity check the rest.
1212 int64_t next_data_offset = -1;
1213 TEST_AND_RETURN_FALSE(prefs->GetInt64(kPrefsUpdateStateNextDataOffset,
1214 &next_data_offset) &&
1215 next_data_offset >= 0);
1216
Darin Petkov437adc42010-10-07 13:12:24 -07001217 string sha256_context;
Darin Petkov0406e402010-10-06 21:33:11 -07001218 TEST_AND_RETURN_FALSE(
Darin Petkov437adc42010-10-07 13:12:24 -07001219 prefs->GetString(kPrefsUpdateStateSHA256Context, &sha256_context) &&
1220 !sha256_context.empty());
Darin Petkov0406e402010-10-06 21:33:11 -07001221
1222 int64_t manifest_metadata_size = 0;
1223 TEST_AND_RETURN_FALSE(prefs->GetInt64(kPrefsManifestMetadataSize,
1224 &manifest_metadata_size) &&
1225 manifest_metadata_size > 0);
1226
1227 return true;
1228}
1229
Darin Petkov9b230572010-10-08 10:20:09 -07001230bool DeltaPerformer::ResetUpdateProgress(PrefsInterface* prefs, bool quick) {
Darin Petkov0406e402010-10-06 21:33:11 -07001231 TEST_AND_RETURN_FALSE(prefs->SetInt64(kPrefsUpdateStateNextOperation,
1232 kUpdateStateOperationInvalid));
Darin Petkov9b230572010-10-08 10:20:09 -07001233 if (!quick) {
1234 prefs->SetString(kPrefsUpdateCheckResponseHash, "");
1235 prefs->SetInt64(kPrefsUpdateStateNextDataOffset, -1);
David Zeuthen41996ad2013-09-24 15:43:24 -07001236 prefs->SetInt64(kPrefsUpdateStateNextDataLength, 0);
Darin Petkov9b230572010-10-08 10:20:09 -07001237 prefs->SetString(kPrefsUpdateStateSHA256Context, "");
1238 prefs->SetString(kPrefsUpdateStateSignedSHA256Context, "");
Darin Petkov4f0a07b2011-05-25 16:47:20 -07001239 prefs->SetString(kPrefsUpdateStateSignatureBlob, "");
Darin Petkov9b230572010-10-08 10:20:09 -07001240 prefs->SetInt64(kPrefsManifestMetadataSize, -1);
Darin Petkov61426142010-10-08 11:04:55 -07001241 prefs->SetInt64(kPrefsResumedUpdateFailures, 0);
Darin Petkov9b230572010-10-08 10:20:09 -07001242 }
Darin Petkov73058b42010-10-06 16:32:19 -07001243 return true;
1244}
1245
1246bool DeltaPerformer::CheckpointUpdateProgress() {
Darin Petkov9c0baf82010-10-07 13:44:48 -07001247 Terminator::set_exit_blocked(true);
Darin Petkov0406e402010-10-06 21:33:11 -07001248 if (last_updated_buffer_offset_ != buffer_offset_) {
Darin Petkov9c0baf82010-10-07 13:44:48 -07001249 // Resets the progress in case we die in the middle of the state update.
Darin Petkov9b230572010-10-08 10:20:09 -07001250 ResetUpdateProgress(prefs_, true);
Darin Petkov0406e402010-10-06 21:33:11 -07001251 TEST_AND_RETURN_FALSE(
Darin Petkov437adc42010-10-07 13:12:24 -07001252 prefs_->SetString(kPrefsUpdateStateSHA256Context,
Darin Petkov0406e402010-10-06 21:33:11 -07001253 hash_calculator_.GetContext()));
1254 TEST_AND_RETURN_FALSE(prefs_->SetInt64(kPrefsUpdateStateNextDataOffset,
1255 buffer_offset_));
1256 last_updated_buffer_offset_ = buffer_offset_;
David Zeuthen41996ad2013-09-24 15:43:24 -07001257
1258 if (next_operation_num_ < num_total_operations_) {
1259 const bool is_kernel_partition =
1260 next_operation_num_ >= num_rootfs_operations_;
1261 const DeltaArchiveManifest_InstallOperation &op =
1262 is_kernel_partition ?
1263 manifest_.kernel_install_operations(
1264 next_operation_num_ - num_rootfs_operations_) :
1265 manifest_.install_operations(next_operation_num_);
1266 TEST_AND_RETURN_FALSE(prefs_->SetInt64(kPrefsUpdateStateNextDataLength,
1267 op.data_length()));
1268 } else {
1269 TEST_AND_RETURN_FALSE(prefs_->SetInt64(kPrefsUpdateStateNextDataLength,
1270 0));
1271 }
Darin Petkov0406e402010-10-06 21:33:11 -07001272 }
Darin Petkov73058b42010-10-06 16:32:19 -07001273 TEST_AND_RETURN_FALSE(prefs_->SetInt64(kPrefsUpdateStateNextOperation,
1274 next_operation_num_));
1275 return true;
1276}
1277
Darin Petkov9b230572010-10-08 10:20:09 -07001278bool DeltaPerformer::PrimeUpdateState() {
1279 CHECK(manifest_valid_);
1280 block_size_ = manifest_.block_size();
1281
1282 int64_t next_operation = kUpdateStateOperationInvalid;
1283 if (!prefs_->GetInt64(kPrefsUpdateStateNextOperation, &next_operation) ||
1284 next_operation == kUpdateStateOperationInvalid ||
1285 next_operation <= 0) {
1286 // Initiating a new update, no more state needs to be initialized.
Darin Petkov698d0412010-10-13 10:59:44 -07001287 TEST_AND_RETURN_FALSE(VerifySourcePartitions());
Darin Petkov9b230572010-10-08 10:20:09 -07001288 return true;
1289 }
1290 next_operation_num_ = next_operation;
1291
1292 // Resuming an update -- load the rest of the update state.
1293 int64_t next_data_offset = -1;
1294 TEST_AND_RETURN_FALSE(prefs_->GetInt64(kPrefsUpdateStateNextDataOffset,
1295 &next_data_offset) &&
1296 next_data_offset >= 0);
1297 buffer_offset_ = next_data_offset;
1298
Darin Petkov4f0a07b2011-05-25 16:47:20 -07001299 // The signed hash context and the signature blob may be empty if the
1300 // interrupted update didn't reach the signature.
Darin Petkov9b230572010-10-08 10:20:09 -07001301 prefs_->GetString(kPrefsUpdateStateSignedSHA256Context,
1302 &signed_hash_context_);
Darin Petkov4f0a07b2011-05-25 16:47:20 -07001303 string signature_blob;
1304 if (prefs_->GetString(kPrefsUpdateStateSignatureBlob, &signature_blob)) {
1305 signatures_message_data_.assign(signature_blob.begin(),
1306 signature_blob.end());
1307 }
Darin Petkov9b230572010-10-08 10:20:09 -07001308
1309 string hash_context;
1310 TEST_AND_RETURN_FALSE(prefs_->GetString(kPrefsUpdateStateSHA256Context,
1311 &hash_context) &&
1312 hash_calculator_.SetContext(hash_context));
1313
1314 int64_t manifest_metadata_size = 0;
1315 TEST_AND_RETURN_FALSE(prefs_->GetInt64(kPrefsManifestMetadataSize,
1316 &manifest_metadata_size) &&
1317 manifest_metadata_size > 0);
1318 manifest_metadata_size_ = manifest_metadata_size;
1319
Gilad Arnold8a86fa52013-01-15 12:35:05 -08001320 // Advance the download progress to reflect what doesn't need to be
1321 // re-downloaded.
1322 total_bytes_received_ += buffer_offset_;
1323
Darin Petkov61426142010-10-08 11:04:55 -07001324 // Speculatively count the resume as a failure.
1325 int64_t resumed_update_failures;
1326 if (prefs_->GetInt64(kPrefsResumedUpdateFailures, &resumed_update_failures)) {
1327 resumed_update_failures++;
1328 } else {
1329 resumed_update_failures = 1;
1330 }
1331 prefs_->SetInt64(kPrefsResumedUpdateFailures, resumed_update_failures);
Darin Petkov9b230572010-10-08 10:20:09 -07001332 return true;
1333}
1334
David Zeuthena99981f2013-04-29 13:42:47 -07001335void DeltaPerformer::SendUmaStat(ErrorCode code) {
Jay Srinivasan55f50c22013-01-10 19:24:35 -08001336 utils::SendErrorCodeToUma(system_state_, code);
Jay Srinivasanf0572052012-10-23 18:12:56 -07001337}
1338
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001339} // namespace chromeos_update_engine