blob: 7943d7ab6846e62c0f90158a5e87b27d75784d95 [file] [log] [blame]
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_PERFORMER_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_PERFORMER_H__
7
8#include <inttypes.h>
Darin Petkovd7061ab2010-10-06 14:37:09 -07009
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070010#include <vector>
Darin Petkovd7061ab2010-10-06 14:37:09 -070011
Gilad Arnold8a86fa52013-01-15 12:35:05 -080012#include <base/time.h>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070013#include <google/protobuf/repeated_field.h>
Andrew de los Reyes353777c2010-10-08 10:34:30 -070014#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkovd7061ab2010-10-06 14:37:09 -070015
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070016#include "update_engine/file_writer.h"
Jay Srinivasan51dcf262012-09-13 17:24:32 -070017#include "update_engine/install_plan.h"
Darin Petkovd7061ab2010-10-06 14:37:09 -070018#include "update_engine/omaha_hash_calculator.h"
Jay Srinivasanf0572052012-10-23 18:12:56 -070019#include "update_engine/system_state.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070020#include "update_engine/update_metadata.pb.h"
21
22namespace chromeos_update_engine {
23
Darin Petkov73058b42010-10-06 16:32:19 -070024class PrefsInterface;
25
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070026// This class performs the actions in a delta update synchronously. The delta
27// update itself should be passed in in chunks as it is received.
28
29class DeltaPerformer : public FileWriter {
30 public:
Darin Petkov9574f7e2011-01-13 10:48:12 -080031 enum MetadataParseResult {
32 kMetadataParseSuccess,
33 kMetadataParseError,
34 kMetadataParseInsufficientData,
35 };
36
Jay Srinivasanf4318702012-09-24 11:56:24 -070037 static const uint64_t kDeltaVersionSize;
38 static const uint64_t kDeltaManifestSizeSize;
Darin Petkovabc7bc02011-02-23 14:39:43 -080039 static const char kUpdatePayloadPublicKeyPath[];
40
Gilad Arnold8a86fa52013-01-15 12:35:05 -080041 // Defines the granularity of progress logging in terms of how many "completed
42 // chunks" we want to report at the most.
43 static const unsigned kProgressLogMaxChunks;
44 // Defines a timeout since the last progress was logged after which we want to
45 // force another log message (even if the current chunk was not completed).
46 static const unsigned kProgressLogTimeoutSeconds;
47 // These define the relative weights (0-100) we give to the different work
48 // components associated with an update when computing an overall progress.
49 // Currently they include the download progress and the number of completed
50 // operations. They must add up to one hundred (100).
51 static const unsigned kProgressDownloadWeight;
52 static const unsigned kProgressOperationsWeight;
53
Jay Srinivasanf0572052012-10-23 18:12:56 -070054 DeltaPerformer(PrefsInterface* prefs,
55 SystemState* system_state,
56 InstallPlan* install_plan)
Darin Petkov73058b42010-10-06 16:32:19 -070057 : prefs_(prefs),
Jay Srinivasanf0572052012-10-23 18:12:56 -070058 system_state_(system_state),
Jay Srinivasan51dcf262012-09-13 17:24:32 -070059 install_plan_(install_plan),
Darin Petkov73058b42010-10-06 16:32:19 -070060 fd_(-1),
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070061 kernel_fd_(-1),
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070062 manifest_valid_(false),
Darin Petkov437adc42010-10-07 13:12:24 -070063 manifest_metadata_size_(0),
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070064 next_operation_num_(0),
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070065 buffer_offset_(0),
Darin Petkov0406e402010-10-06 21:33:11 -070066 last_updated_buffer_offset_(kuint64max),
Jay Srinivasan51dcf262012-09-13 17:24:32 -070067 block_size_(0),
Gilad Arnold8a86fa52013-01-15 12:35:05 -080068 public_key_path_(kUpdatePayloadPublicKeyPath),
69 total_bytes_received_(0),
70 num_rootfs_operations_(0),
71 num_total_operations_(0),
72 overall_progress_(0),
73 last_progress_chunk_(0),
74 forced_progress_log_wait_(
75 base::TimeDelta::FromSeconds(kProgressLogTimeoutSeconds)) {}
Darin Petkovd7061ab2010-10-06 14:37:09 -070076
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070077 // Opens the kernel. Should be called before or after Open(), but before
78 // Write(). The kernel file will be close()d when Close() is called.
79 bool OpenKernel(const char* kernel_path);
80
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070081 // flags and mode ignored. Once Close()d, a DeltaPerformer can't be
82 // Open()ed again.
83 int Open(const char* path, int flags, mode_t mode);
84
Jay Srinivasan51dcf262012-09-13 17:24:32 -070085 // FileWriter's Write implementation where caller doesn't care about
86 // error codes.
87 bool Write(const void* bytes, size_t count) {
David Zeuthena99981f2013-04-29 13:42:47 -070088 ErrorCode error;
Jay Srinivasan51dcf262012-09-13 17:24:32 -070089 return Write(bytes, count, &error);
90 }
91
92 // FileWriter's Write implementation that returns a more specific |error| code
93 // in case of failures in Write operation.
David Zeuthena99981f2013-04-29 13:42:47 -070094 bool Write(const void* bytes, size_t count, ErrorCode *error);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070095
96 // Wrapper around close. Returns 0 on success or -errno on error.
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070097 // Closes both 'path' given to Open() and the kernel path.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070098 int Close();
Darin Petkovd7061ab2010-10-06 14:37:09 -070099
David Zeuthen8f191b22013-08-06 12:27:50 -0700100 // Returns |true| only if the manifest has been processed and it's valid.
101 bool IsManifestValid();
102
Darin Petkovd7061ab2010-10-06 14:37:09 -0700103 // Verifies the downloaded payload against the signed hash included in the
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700104 // payload, against the update check hash (which is in base64 format) and
David Zeuthena99981f2013-04-29 13:42:47 -0700105 // size using the public key and returns kErrorCodeSuccess on success, an
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700106 // error code on failure. This method should be called after closing the
107 // stream. Note this method skips the signed hash check if the public key is
David Zeuthena99981f2013-04-29 13:42:47 -0700108 // unavailable; it returns kErrorCodeSignedDeltaPayloadExpectedError if the
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700109 // public key is available but the delta payload doesn't include a signature.
David Zeuthena99981f2013-04-29 13:42:47 -0700110 ErrorCode VerifyPayload(const std::string& update_check_response_hash,
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -0700111 const uint64_t update_check_response_size);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700112
Darin Petkov3aefa862010-12-07 14:45:00 -0800113 // Reads from the update manifest the expected sizes and hashes of the target
114 // kernel and rootfs partitions. These values can be used for applied update
115 // hash verification. This method must be called after the update manifest has
116 // been parsed (e.g., after closing the stream). Returns true on success, and
117 // false on failure (e.g., when the values are not present in the update
118 // manifest).
119 bool GetNewPartitionInfo(uint64_t* kernel_size,
120 std::vector<char>* kernel_hash,
121 uint64_t* rootfs_size,
122 std::vector<char>* rootfs_hash);
Darin Petkov2dd01092010-10-08 15:43:05 -0700123
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700124 // Converts an ordered collection of Extent objects which contain data of
125 // length full_length to a comma-separated string. For each Extent, the
126 // string will have the start offset and then the length in bytes.
127 // The length value of the last extent in the string may be short, since
128 // the full length of all extents in the string is capped to full_length.
129 // Also, an extent starting at kSparseHole, appears as -1 in the string.
130 // For example, if the Extents are {1, 1}, {4, 2}, {kSparseHole, 1},
131 // {0, 1}, block_size is 4096, and full_length is 5 * block_size - 13,
132 // the resulting string will be: "4096:4096,16384:8192,-1:4096,0:4083"
133 static bool ExtentsToBsdiffPositionsString(
134 const google::protobuf::RepeatedPtrField<Extent>& extents,
135 uint64_t block_size,
136 uint64_t full_length,
137 std::string* positions_string);
138
Darin Petkov0406e402010-10-06 21:33:11 -0700139 // Returns true if a previous update attempt can be continued based on the
140 // persistent preferences and the new update check response hash.
141 static bool CanResumeUpdate(PrefsInterface* prefs,
142 std::string update_check_response_hash);
143
144 // Resets the persistent update progress state to indicate that an update
Darin Petkov9b230572010-10-08 10:20:09 -0700145 // can't be resumed. Performs a quick update-in-progress reset if |quick| is
146 // true, otherwise resets all progress-related update state. Returns true on
147 // success, false otherwise.
148 static bool ResetUpdateProgress(PrefsInterface* prefs, bool quick);
Darin Petkov0406e402010-10-06 21:33:11 -0700149
Darin Petkov9574f7e2011-01-13 10:48:12 -0800150 // Attempts to parse the update metadata starting from the beginning of
151 // |payload| into |manifest|. On success, sets |metadata_size| to the total
152 // metadata bytes (including the delta magic and metadata size fields), and
153 // returns kMetadataParseSuccess. Returns kMetadataParseInsufficientData if
154 // more data is needed to parse the complete metadata. Returns
155 // kMetadataParseError if the metadata can't be parsed given the payload.
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700156 MetadataParseResult ParsePayloadMetadata(
Darin Petkov9574f7e2011-01-13 10:48:12 -0800157 const std::vector<char>& payload,
158 DeltaArchiveManifest* manifest,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700159 uint64_t* metadata_size,
David Zeuthena99981f2013-04-29 13:42:47 -0700160 ErrorCode* error);
Darin Petkov9574f7e2011-01-13 10:48:12 -0800161
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700162 void set_public_key_path(const std::string& public_key_path) {
163 public_key_path_ = public_key_path;
Darin Petkov698d0412010-10-13 10:59:44 -0700164 }
165
Jay Srinivasanf4318702012-09-24 11:56:24 -0700166 // Returns the byte offset at which the manifest protobuf begins in a
167 // payload.
168 static uint64_t GetManifestOffset();
169
170 // Returns the byte offset where the size of the manifest is stored in
171 // a payload. This offset precedes the actual start of the manifest
172 // that's returned by the GetManifestOffset method.
173 static uint64_t GetManifestSizeOffset();
174
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700175 private:
Andrew de los Reyes353777c2010-10-08 10:34:30 -0700176 friend class DeltaPerformerTest;
177 FRIEND_TEST(DeltaPerformerTest, IsIdempotentOperationTest);
178
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800179 // Logs the progress of downloading/applying an update.
180 void LogProgress(const char* message_prefix);
181
182 // Update overall progress metrics, log as necessary.
183 void UpdateOverallProgress(bool force_log, const char* message_prefix);
184
Andrew de los Reyes353777c2010-10-08 10:34:30 -0700185 static bool IsIdempotentOperation(
186 const DeltaArchiveManifest_InstallOperation& op);
187
Darin Petkov698d0412010-10-13 10:59:44 -0700188 // Verifies that the expected source partition hashes (if present) match the
189 // hashes for the current partitions. Returns true if there're no expected
190 // hashes in the payload (e.g., if it's a new-style full update) or if the
191 // hashes match; returns false otherwise.
192 bool VerifySourcePartitions();
193
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700194 // Returns true if enough of the delta file has been passed via Write()
195 // to be able to perform a given install operation.
196 bool CanPerformInstallOperation(
197 const DeltaArchiveManifest_InstallOperation& operation);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700198
Gilad Arnold21504f02013-05-24 08:51:22 -0700199 // Checks the integrity of the payload manifest. Returns true upon success,
200 // false otherwise.
201 ErrorCode ValidateManifest();
202
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700203 // Validates that the hash of the blobs corresponding to the given |operation|
204 // matches what's specified in the manifest in the payload.
David Zeuthena99981f2013-04-29 13:42:47 -0700205 // Returns kErrorCodeSuccess on match or a suitable error code otherwise.
206 ErrorCode ValidateOperationHash(
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800207 const DeltaArchiveManifest_InstallOperation& operation);
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700208
209 // Interprets the given |protobuf| as a DeltaArchiveManifest protocol buffer
210 // of the given protobuf_length and verifies that the signed hash of the
Jay Srinivasanf4318702012-09-24 11:56:24 -0700211 // metadata matches what's specified in the install plan from Omaha.
David Zeuthena99981f2013-04-29 13:42:47 -0700212 // Returns kErrorCodeSuccess on match or a suitable error code otherwise.
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700213 // This method must be called before any part of the |protobuf| is parsed
214 // so that a man-in-the-middle attack on the SSL connection to the payload
215 // server doesn't exploit any vulnerability in the code that parses the
216 // protocol buffer.
David Zeuthena99981f2013-04-29 13:42:47 -0700217 ErrorCode ValidateMetadataSignature(const char* protobuf,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700218 uint64_t protobuf_length);
219
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700220 // Returns true on success.
221 bool PerformInstallOperation(
222 const DeltaArchiveManifest_InstallOperation& operation);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700223
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700224 // These perform a specific type of operation and return true on success.
225 bool PerformReplaceOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700226 const DeltaArchiveManifest_InstallOperation& operation,
227 bool is_kernel_partition);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700228 bool PerformMoveOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700229 const DeltaArchiveManifest_InstallOperation& operation,
230 bool is_kernel_partition);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700231 bool PerformBsdiffOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700232 const DeltaArchiveManifest_InstallOperation& operation,
233 bool is_kernel_partition);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700234
Darin Petkovd7061ab2010-10-06 14:37:09 -0700235 // Returns true if the payload signature message has been extracted from
236 // |operation|, false otherwise.
237 bool ExtractSignatureMessage(
238 const DeltaArchiveManifest_InstallOperation& operation);
239
Darin Petkov437adc42010-10-07 13:12:24 -0700240 // Updates the hash calculator with |count| bytes at the head of |buffer_| and
241 // then discards them.
242 void DiscardBufferHeadBytes(size_t count);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700243
Darin Petkov0406e402010-10-06 21:33:11 -0700244 // Checkpoints the update progress into persistent storage to allow this
245 // update attempt to be resumed after reboot.
Darin Petkov73058b42010-10-06 16:32:19 -0700246 bool CheckpointUpdateProgress();
247
Darin Petkov9b230572010-10-08 10:20:09 -0700248 // Primes the required update state. Returns true if the update state was
249 // successfully initialized to a saved resume state or if the update is a new
250 // update. Returns false otherwise.
251 bool PrimeUpdateState();
252
Jay Srinivasanf0572052012-10-23 18:12:56 -0700253 // Sends UMA statistics for the given error code.
David Zeuthena99981f2013-04-29 13:42:47 -0700254 void SendUmaStat(ErrorCode code);
Jay Srinivasanf0572052012-10-23 18:12:56 -0700255
Darin Petkov73058b42010-10-06 16:32:19 -0700256 // Update Engine preference store.
257 PrefsInterface* prefs_;
258
Jay Srinivasanf0572052012-10-23 18:12:56 -0700259 // Global context of the system.
260 SystemState* system_state_;
261
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700262 // Install Plan based on Omaha Response.
263 InstallPlan* install_plan_;
264
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700265 // File descriptor of open device.
266 int fd_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700267
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700268 // File descriptor of the kernel device
269 int kernel_fd_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700270
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700271 std::string path_; // Path that fd_ refers to.
272 std::string kernel_path_; // Path that kernel_fd_ refers to.
Darin Petkovd7061ab2010-10-06 14:37:09 -0700273
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700274 DeltaArchiveManifest manifest_;
275 bool manifest_valid_;
Darin Petkov437adc42010-10-07 13:12:24 -0700276 uint64_t manifest_metadata_size_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700277
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700278 // Index of the next operation to perform in the manifest.
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800279 size_t next_operation_num_;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700280
281 // buffer_ is a window of the data that's been downloaded. At first,
282 // it contains the beginning of the download, but after the protobuf
283 // has been downloaded and parsed, it contains a sliding window of
284 // data blobs.
285 std::vector<char> buffer_;
286 // Offset of buffer_ in the binary blobs section of the update.
287 uint64_t buffer_offset_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700288
Darin Petkov0406e402010-10-06 21:33:11 -0700289 // Last |buffer_offset_| value updated as part of the progress update.
290 uint64_t last_updated_buffer_offset_;
291
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700292 // The block size (parsed from the manifest).
293 uint32_t block_size_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700294
Darin Petkov437adc42010-10-07 13:12:24 -0700295 // Calculates the payload hash.
Darin Petkovd7061ab2010-10-06 14:37:09 -0700296 OmahaHashCalculator hash_calculator_;
297
Darin Petkov437adc42010-10-07 13:12:24 -0700298 // Saves the signed hash context.
299 std::string signed_hash_context_;
300
Darin Petkovd7061ab2010-10-06 14:37:09 -0700301 // Signatures message blob extracted directly from the payload.
302 std::vector<char> signatures_message_data_;
303
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700304 // The public key to be used. Provided as a member so that tests can
305 // override with test keys.
306 std::string public_key_path_;
Darin Petkov698d0412010-10-13 10:59:44 -0700307
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800308 // The number of bytes received so far, used for progress tracking.
309 size_t total_bytes_received_;
310
311 // The number rootfs and total operations in a payload, once we know them.
312 size_t num_rootfs_operations_;
313 size_t num_total_operations_;
314
315 // An overall progress counter, which should reflect both download progress
316 // and the ratio of applied operations. Range is 0-100.
317 unsigned overall_progress_;
318
319 // The last progress chunk recorded.
320 unsigned last_progress_chunk_;
321
322 // The timeout after which we should force emitting a progress log (constant),
323 // and the actual point in time for the next forced log to be emitted.
324 const base::TimeDelta forced_progress_log_wait_;
325 base::Time forced_progress_log_time_;
326
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700327 DISALLOW_COPY_AND_ASSIGN(DeltaPerformer);
328};
329
330} // namespace chromeos_update_engine
331
332#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_PERFORMER_H__