blob: 0bdf7caa4094f13819efb96e0d47df20bcd4c87e [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
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070012#include <google/protobuf/repeated_field.h>
Andrew de los Reyes353777c2010-10-08 10:34:30 -070013#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkovd7061ab2010-10-06 14:37:09 -070014
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070015#include "update_engine/file_writer.h"
Jay Srinivasan51dcf262012-09-13 17:24:32 -070016#include "update_engine/install_plan.h"
Darin Petkovd7061ab2010-10-06 14:37:09 -070017#include "update_engine/omaha_hash_calculator.h"
Jay Srinivasanf0572052012-10-23 18:12:56 -070018#include "update_engine/system_state.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070019#include "update_engine/update_metadata.pb.h"
20
21namespace chromeos_update_engine {
22
Darin Petkov73058b42010-10-06 16:32:19 -070023class PrefsInterface;
24
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070025// This class performs the actions in a delta update synchronously. The delta
26// update itself should be passed in in chunks as it is received.
27
28class DeltaPerformer : public FileWriter {
29 public:
Darin Petkov9574f7e2011-01-13 10:48:12 -080030 enum MetadataParseResult {
31 kMetadataParseSuccess,
32 kMetadataParseError,
33 kMetadataParseInsufficientData,
34 };
35
Jay Srinivasanf4318702012-09-24 11:56:24 -070036 static const uint64_t kDeltaVersionSize;
37 static const uint64_t kDeltaManifestSizeSize;
Darin Petkovabc7bc02011-02-23 14:39:43 -080038 static const char kUpdatePayloadPublicKeyPath[];
39
Jay Srinivasanf0572052012-10-23 18:12:56 -070040 DeltaPerformer(PrefsInterface* prefs,
41 SystemState* system_state,
42 InstallPlan* install_plan)
Darin Petkov73058b42010-10-06 16:32:19 -070043 : prefs_(prefs),
Jay Srinivasanf0572052012-10-23 18:12:56 -070044 system_state_(system_state),
Jay Srinivasan51dcf262012-09-13 17:24:32 -070045 install_plan_(install_plan),
Darin Petkov73058b42010-10-06 16:32:19 -070046 fd_(-1),
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070047 kernel_fd_(-1),
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070048 manifest_valid_(false),
Darin Petkov437adc42010-10-07 13:12:24 -070049 manifest_metadata_size_(0),
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070050 next_operation_num_(0),
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070051 buffer_offset_(0),
Darin Petkov0406e402010-10-06 21:33:11 -070052 last_updated_buffer_offset_(kuint64max),
Jay Srinivasan51dcf262012-09-13 17:24:32 -070053 block_size_(0),
54 public_key_path_(kUpdatePayloadPublicKeyPath) {}
Darin Petkovd7061ab2010-10-06 14:37:09 -070055
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070056 // Opens the kernel. Should be called before or after Open(), but before
57 // Write(). The kernel file will be close()d when Close() is called.
58 bool OpenKernel(const char* kernel_path);
59
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070060 // flags and mode ignored. Once Close()d, a DeltaPerformer can't be
61 // Open()ed again.
62 int Open(const char* path, int flags, mode_t mode);
63
Jay Srinivasan51dcf262012-09-13 17:24:32 -070064 // FileWriter's Write implementation where caller doesn't care about
65 // error codes.
66 bool Write(const void* bytes, size_t count) {
67 ActionExitCode error;
68 return Write(bytes, count, &error);
69 }
70
71 // FileWriter's Write implementation that returns a more specific |error| code
72 // in case of failures in Write operation.
73 bool Write(const void* bytes, size_t count, ActionExitCode *error);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070074
75 // Wrapper around close. Returns 0 on success or -errno on error.
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070076 // Closes both 'path' given to Open() and the kernel path.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070077 int Close();
Darin Petkovd7061ab2010-10-06 14:37:09 -070078
79 // Verifies the downloaded payload against the signed hash included in the
Jay Srinivasan51dcf262012-09-13 17:24:32 -070080 // payload, against the update check hash (which is in base64 format) and
81 // size using the public key and returns kActionCodeSuccess on success, an
82 // error code on failure. This method should be called after closing the
83 // stream. Note this method skips the signed hash check if the public key is
84 // unavailable; it returns kActionCodeSignedDeltaPayloadExpectedError if the
85 // public key is available but the delta payload doesn't include a signature.
86 ActionExitCode VerifyPayload(const std::string& update_check_response_hash,
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -070087 const uint64_t update_check_response_size);
Darin Petkovd7061ab2010-10-06 14:37:09 -070088
Darin Petkov3aefa862010-12-07 14:45:00 -080089 // Reads from the update manifest the expected sizes and hashes of the target
90 // kernel and rootfs partitions. These values can be used for applied update
91 // hash verification. This method must be called after the update manifest has
92 // been parsed (e.g., after closing the stream). Returns true on success, and
93 // false on failure (e.g., when the values are not present in the update
94 // manifest).
95 bool GetNewPartitionInfo(uint64_t* kernel_size,
96 std::vector<char>* kernel_hash,
97 uint64_t* rootfs_size,
98 std::vector<char>* rootfs_hash);
Darin Petkov2dd01092010-10-08 15:43:05 -070099
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700100 // Converts an ordered collection of Extent objects which contain data of
101 // length full_length to a comma-separated string. For each Extent, the
102 // string will have the start offset and then the length in bytes.
103 // The length value of the last extent in the string may be short, since
104 // the full length of all extents in the string is capped to full_length.
105 // Also, an extent starting at kSparseHole, appears as -1 in the string.
106 // For example, if the Extents are {1, 1}, {4, 2}, {kSparseHole, 1},
107 // {0, 1}, block_size is 4096, and full_length is 5 * block_size - 13,
108 // the resulting string will be: "4096:4096,16384:8192,-1:4096,0:4083"
109 static bool ExtentsToBsdiffPositionsString(
110 const google::protobuf::RepeatedPtrField<Extent>& extents,
111 uint64_t block_size,
112 uint64_t full_length,
113 std::string* positions_string);
114
Darin Petkov0406e402010-10-06 21:33:11 -0700115 // Returns true if a previous update attempt can be continued based on the
116 // persistent preferences and the new update check response hash.
117 static bool CanResumeUpdate(PrefsInterface* prefs,
118 std::string update_check_response_hash);
119
120 // Resets the persistent update progress state to indicate that an update
Darin Petkov9b230572010-10-08 10:20:09 -0700121 // can't be resumed. Performs a quick update-in-progress reset if |quick| is
122 // true, otherwise resets all progress-related update state. Returns true on
123 // success, false otherwise.
124 static bool ResetUpdateProgress(PrefsInterface* prefs, bool quick);
Darin Petkov0406e402010-10-06 21:33:11 -0700125
Darin Petkov9574f7e2011-01-13 10:48:12 -0800126 // Attempts to parse the update metadata starting from the beginning of
127 // |payload| into |manifest|. On success, sets |metadata_size| to the total
128 // metadata bytes (including the delta magic and metadata size fields), and
129 // returns kMetadataParseSuccess. Returns kMetadataParseInsufficientData if
130 // more data is needed to parse the complete metadata. Returns
131 // kMetadataParseError if the metadata can't be parsed given the payload.
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700132 MetadataParseResult ParsePayloadMetadata(
Darin Petkov9574f7e2011-01-13 10:48:12 -0800133 const std::vector<char>& payload,
134 DeltaArchiveManifest* manifest,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700135 uint64_t* metadata_size,
136 ActionExitCode* error);
Darin Petkov9574f7e2011-01-13 10:48:12 -0800137
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700138 void set_public_key_path(const std::string& public_key_path) {
139 public_key_path_ = public_key_path;
Darin Petkov698d0412010-10-13 10:59:44 -0700140 }
141
Jay Srinivasanf4318702012-09-24 11:56:24 -0700142 // Returns the byte offset at which the manifest protobuf begins in a
143 // payload.
144 static uint64_t GetManifestOffset();
145
146 // Returns the byte offset where the size of the manifest is stored in
147 // a payload. This offset precedes the actual start of the manifest
148 // that's returned by the GetManifestOffset method.
149 static uint64_t GetManifestSizeOffset();
150
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700151 private:
Andrew de los Reyes353777c2010-10-08 10:34:30 -0700152 friend class DeltaPerformerTest;
153 FRIEND_TEST(DeltaPerformerTest, IsIdempotentOperationTest);
154
155 static bool IsIdempotentOperation(
156 const DeltaArchiveManifest_InstallOperation& op);
157
Darin Petkov698d0412010-10-13 10:59:44 -0700158 // Verifies that the expected source partition hashes (if present) match the
159 // hashes for the current partitions. Returns true if there're no expected
160 // hashes in the payload (e.g., if it's a new-style full update) or if the
161 // hashes match; returns false otherwise.
162 bool VerifySourcePartitions();
163
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700164 // Returns true if enough of the delta file has been passed via Write()
165 // to be able to perform a given install operation.
166 bool CanPerformInstallOperation(
167 const DeltaArchiveManifest_InstallOperation& operation);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700168
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700169 // Validates that the hash of the blobs corresponding to the given |operation|
170 // matches what's specified in the manifest in the payload.
171 // Returns kActionCodeSuccess on match or a suitable error code otherwise.
172 ActionExitCode ValidateOperationHash(
Jay Srinivasan00f76b62012-09-17 18:48:36 -0700173 const DeltaArchiveManifest_InstallOperation& operation, bool should_log);
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700174
175 // Interprets the given |protobuf| as a DeltaArchiveManifest protocol buffer
176 // of the given protobuf_length and verifies that the signed hash of the
Jay Srinivasanf4318702012-09-24 11:56:24 -0700177 // metadata matches what's specified in the install plan from Omaha.
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700178 // Returns kActionCodeSuccess on match or a suitable error code otherwise.
179 // This method must be called before any part of the |protobuf| is parsed
180 // so that a man-in-the-middle attack on the SSL connection to the payload
181 // server doesn't exploit any vulnerability in the code that parses the
182 // protocol buffer.
Jay Srinivasanf4318702012-09-24 11:56:24 -0700183 ActionExitCode ValidateMetadataSignature(const char* protobuf,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700184 uint64_t protobuf_length);
185
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700186 // Returns true on success.
187 bool PerformInstallOperation(
188 const DeltaArchiveManifest_InstallOperation& operation);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700189
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700190 // These perform a specific type of operation and return true on success.
191 bool PerformReplaceOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700192 const DeltaArchiveManifest_InstallOperation& operation,
193 bool is_kernel_partition);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700194 bool PerformMoveOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700195 const DeltaArchiveManifest_InstallOperation& operation,
196 bool is_kernel_partition);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700197 bool PerformBsdiffOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700198 const DeltaArchiveManifest_InstallOperation& operation,
199 bool is_kernel_partition);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700200
Darin Petkovd7061ab2010-10-06 14:37:09 -0700201 // Returns true if the payload signature message has been extracted from
202 // |operation|, false otherwise.
203 bool ExtractSignatureMessage(
204 const DeltaArchiveManifest_InstallOperation& operation);
205
Darin Petkov437adc42010-10-07 13:12:24 -0700206 // Updates the hash calculator with |count| bytes at the head of |buffer_| and
207 // then discards them.
208 void DiscardBufferHeadBytes(size_t count);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700209
Darin Petkov0406e402010-10-06 21:33:11 -0700210 // Checkpoints the update progress into persistent storage to allow this
211 // update attempt to be resumed after reboot.
Darin Petkov73058b42010-10-06 16:32:19 -0700212 bool CheckpointUpdateProgress();
213
Darin Petkov9b230572010-10-08 10:20:09 -0700214 // Primes the required update state. Returns true if the update state was
215 // successfully initialized to a saved resume state or if the update is a new
216 // update. Returns false otherwise.
217 bool PrimeUpdateState();
218
Jay Srinivasanf0572052012-10-23 18:12:56 -0700219 // Sends UMA statistics for the given error code.
220 void SendUMAStat(ActionExitCode code);
221
Darin Petkov73058b42010-10-06 16:32:19 -0700222 // Update Engine preference store.
223 PrefsInterface* prefs_;
224
Jay Srinivasanf0572052012-10-23 18:12:56 -0700225 // Global context of the system.
226 SystemState* system_state_;
227
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700228 // Install Plan based on Omaha Response.
229 InstallPlan* install_plan_;
230
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700231 // File descriptor of open device.
232 int fd_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700233
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700234 // File descriptor of the kernel device
235 int kernel_fd_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700236
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700237 std::string path_; // Path that fd_ refers to.
238 std::string kernel_path_; // Path that kernel_fd_ refers to.
Darin Petkovd7061ab2010-10-06 14:37:09 -0700239
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700240 DeltaArchiveManifest manifest_;
241 bool manifest_valid_;
Darin Petkov437adc42010-10-07 13:12:24 -0700242 uint64_t manifest_metadata_size_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700243
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700244 // Index of the next operation to perform in the manifest.
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700245 int next_operation_num_;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700246
247 // buffer_ is a window of the data that's been downloaded. At first,
248 // it contains the beginning of the download, but after the protobuf
249 // has been downloaded and parsed, it contains a sliding window of
250 // data blobs.
251 std::vector<char> buffer_;
252 // Offset of buffer_ in the binary blobs section of the update.
253 uint64_t buffer_offset_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700254
Darin Petkov0406e402010-10-06 21:33:11 -0700255 // Last |buffer_offset_| value updated as part of the progress update.
256 uint64_t last_updated_buffer_offset_;
257
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700258 // The block size (parsed from the manifest).
259 uint32_t block_size_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700260
Darin Petkov437adc42010-10-07 13:12:24 -0700261 // Calculates the payload hash.
Darin Petkovd7061ab2010-10-06 14:37:09 -0700262 OmahaHashCalculator hash_calculator_;
263
Darin Petkov437adc42010-10-07 13:12:24 -0700264 // Saves the signed hash context.
265 std::string signed_hash_context_;
266
Darin Petkovd7061ab2010-10-06 14:37:09 -0700267 // Signatures message blob extracted directly from the payload.
268 std::vector<char> signatures_message_data_;
269
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700270 // The public key to be used. Provided as a member so that tests can
271 // override with test keys.
272 std::string public_key_path_;
Darin Petkov698d0412010-10-13 10:59:44 -0700273
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700274 DISALLOW_COPY_AND_ASSIGN(DeltaPerformer);
275};
276
277} // namespace chromeos_update_engine
278
279#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_PERFORMER_H__