blob: 8ea30c37e57bd86c259bf7f99f32475caab1582c [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"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070018#include "update_engine/update_metadata.pb.h"
19
20namespace chromeos_update_engine {
21
Darin Petkov73058b42010-10-06 16:32:19 -070022class PrefsInterface;
23
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070024// This class performs the actions in a delta update synchronously. The delta
25// update itself should be passed in in chunks as it is received.
26
27class DeltaPerformer : public FileWriter {
28 public:
Darin Petkov9574f7e2011-01-13 10:48:12 -080029 enum MetadataParseResult {
30 kMetadataParseSuccess,
31 kMetadataParseError,
32 kMetadataParseInsufficientData,
33 };
34
Darin Petkovabc7bc02011-02-23 14:39:43 -080035 static const char kUpdatePayloadPublicKeyPath[];
36
Jay Srinivasan51dcf262012-09-13 17:24:32 -070037 DeltaPerformer(PrefsInterface* prefs, InstallPlan* install_plan)
Darin Petkov73058b42010-10-06 16:32:19 -070038 : prefs_(prefs),
Jay Srinivasan51dcf262012-09-13 17:24:32 -070039 install_plan_(install_plan),
Darin Petkov73058b42010-10-06 16:32:19 -070040 fd_(-1),
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070041 kernel_fd_(-1),
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070042 manifest_valid_(false),
Darin Petkov437adc42010-10-07 13:12:24 -070043 manifest_metadata_size_(0),
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070044 next_operation_num_(0),
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070045 buffer_offset_(0),
Darin Petkov0406e402010-10-06 21:33:11 -070046 last_updated_buffer_offset_(kuint64max),
Jay Srinivasan51dcf262012-09-13 17:24:32 -070047 block_size_(0),
48 public_key_path_(kUpdatePayloadPublicKeyPath) {}
Darin Petkovd7061ab2010-10-06 14:37:09 -070049
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070050 // Opens the kernel. Should be called before or after Open(), but before
51 // Write(). The kernel file will be close()d when Close() is called.
52 bool OpenKernel(const char* kernel_path);
53
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070054 // flags and mode ignored. Once Close()d, a DeltaPerformer can't be
55 // Open()ed again.
56 int Open(const char* path, int flags, mode_t mode);
57
Jay Srinivasan51dcf262012-09-13 17:24:32 -070058 // FileWriter's Write implementation where caller doesn't care about
59 // error codes.
60 bool Write(const void* bytes, size_t count) {
61 ActionExitCode error;
62 return Write(bytes, count, &error);
63 }
64
65 // FileWriter's Write implementation that returns a more specific |error| code
66 // in case of failures in Write operation.
67 bool Write(const void* bytes, size_t count, ActionExitCode *error);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070068
69 // Wrapper around close. Returns 0 on success or -errno on error.
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070070 // Closes both 'path' given to Open() and the kernel path.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070071 int Close();
Darin Petkovd7061ab2010-10-06 14:37:09 -070072
73 // Verifies the downloaded payload against the signed hash included in the
Jay Srinivasan51dcf262012-09-13 17:24:32 -070074 // payload, against the update check hash (which is in base64 format) and
75 // size using the public key and returns kActionCodeSuccess on success, an
76 // error code on failure. This method should be called after closing the
77 // stream. Note this method skips the signed hash check if the public key is
78 // unavailable; it returns kActionCodeSignedDeltaPayloadExpectedError if the
79 // public key is available but the delta payload doesn't include a signature.
80 ActionExitCode VerifyPayload(const std::string& update_check_response_hash,
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -070081 const uint64_t update_check_response_size);
Darin Petkovd7061ab2010-10-06 14:37:09 -070082
Darin Petkov3aefa862010-12-07 14:45:00 -080083 // Reads from the update manifest the expected sizes and hashes of the target
84 // kernel and rootfs partitions. These values can be used for applied update
85 // hash verification. This method must be called after the update manifest has
86 // been parsed (e.g., after closing the stream). Returns true on success, and
87 // false on failure (e.g., when the values are not present in the update
88 // manifest).
89 bool GetNewPartitionInfo(uint64_t* kernel_size,
90 std::vector<char>* kernel_hash,
91 uint64_t* rootfs_size,
92 std::vector<char>* rootfs_hash);
Darin Petkov2dd01092010-10-08 15:43:05 -070093
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070094 // Converts an ordered collection of Extent objects which contain data of
95 // length full_length to a comma-separated string. For each Extent, the
96 // string will have the start offset and then the length in bytes.
97 // The length value of the last extent in the string may be short, since
98 // the full length of all extents in the string is capped to full_length.
99 // Also, an extent starting at kSparseHole, appears as -1 in the string.
100 // For example, if the Extents are {1, 1}, {4, 2}, {kSparseHole, 1},
101 // {0, 1}, block_size is 4096, and full_length is 5 * block_size - 13,
102 // the resulting string will be: "4096:4096,16384:8192,-1:4096,0:4083"
103 static bool ExtentsToBsdiffPositionsString(
104 const google::protobuf::RepeatedPtrField<Extent>& extents,
105 uint64_t block_size,
106 uint64_t full_length,
107 std::string* positions_string);
108
Darin Petkov0406e402010-10-06 21:33:11 -0700109 // Returns true if a previous update attempt can be continued based on the
110 // persistent preferences and the new update check response hash.
111 static bool CanResumeUpdate(PrefsInterface* prefs,
112 std::string update_check_response_hash);
113
114 // Resets the persistent update progress state to indicate that an update
Darin Petkov9b230572010-10-08 10:20:09 -0700115 // can't be resumed. Performs a quick update-in-progress reset if |quick| is
116 // true, otherwise resets all progress-related update state. Returns true on
117 // success, false otherwise.
118 static bool ResetUpdateProgress(PrefsInterface* prefs, bool quick);
Darin Petkov0406e402010-10-06 21:33:11 -0700119
Darin Petkov9574f7e2011-01-13 10:48:12 -0800120 // Attempts to parse the update metadata starting from the beginning of
121 // |payload| into |manifest|. On success, sets |metadata_size| to the total
122 // metadata bytes (including the delta magic and metadata size fields), and
123 // returns kMetadataParseSuccess. Returns kMetadataParseInsufficientData if
124 // more data is needed to parse the complete metadata. Returns
125 // kMetadataParseError if the metadata can't be parsed given the payload.
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700126 MetadataParseResult ParsePayloadMetadata(
Darin Petkov9574f7e2011-01-13 10:48:12 -0800127 const std::vector<char>& payload,
128 DeltaArchiveManifest* manifest,
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700129 uint64_t* metadata_size,
130 ActionExitCode* error);
Darin Petkov9574f7e2011-01-13 10:48:12 -0800131
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700132 void set_public_key_path(const std::string& public_key_path) {
133 public_key_path_ = public_key_path;
Darin Petkov698d0412010-10-13 10:59:44 -0700134 }
135
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700136 private:
Andrew de los Reyes353777c2010-10-08 10:34:30 -0700137 friend class DeltaPerformerTest;
138 FRIEND_TEST(DeltaPerformerTest, IsIdempotentOperationTest);
139
140 static bool IsIdempotentOperation(
141 const DeltaArchiveManifest_InstallOperation& op);
142
Darin Petkov698d0412010-10-13 10:59:44 -0700143 // Verifies that the expected source partition hashes (if present) match the
144 // hashes for the current partitions. Returns true if there're no expected
145 // hashes in the payload (e.g., if it's a new-style full update) or if the
146 // hashes match; returns false otherwise.
147 bool VerifySourcePartitions();
148
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700149 // Returns true if enough of the delta file has been passed via Write()
150 // to be able to perform a given install operation.
151 bool CanPerformInstallOperation(
152 const DeltaArchiveManifest_InstallOperation& operation);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700153
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700154 // Validates that the hash of the blobs corresponding to the given |operation|
155 // matches what's specified in the manifest in the payload.
156 // Returns kActionCodeSuccess on match or a suitable error code otherwise.
157 ActionExitCode ValidateOperationHash(
158 const DeltaArchiveManifest_InstallOperation& operation);
159
160 // Interprets the given |protobuf| as a DeltaArchiveManifest protocol buffer
161 // of the given protobuf_length and verifies that the signed hash of the
162 // manifest matches what's specified in the install plan from Omaha.
163 // Returns kActionCodeSuccess on match or a suitable error code otherwise.
164 // This method must be called before any part of the |protobuf| is parsed
165 // so that a man-in-the-middle attack on the SSL connection to the payload
166 // server doesn't exploit any vulnerability in the code that parses the
167 // protocol buffer.
168 ActionExitCode ValidateManifestSignature(const char* protobuf,
169 uint64_t protobuf_length);
170
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700171 // Returns true on success.
172 bool PerformInstallOperation(
173 const DeltaArchiveManifest_InstallOperation& operation);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700174
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700175 // These perform a specific type of operation and return true on success.
176 bool PerformReplaceOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700177 const DeltaArchiveManifest_InstallOperation& operation,
178 bool is_kernel_partition);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700179 bool PerformMoveOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700180 const DeltaArchiveManifest_InstallOperation& operation,
181 bool is_kernel_partition);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700182 bool PerformBsdiffOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700183 const DeltaArchiveManifest_InstallOperation& operation,
184 bool is_kernel_partition);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700185
Darin Petkovd7061ab2010-10-06 14:37:09 -0700186 // Returns true if the payload signature message has been extracted from
187 // |operation|, false otherwise.
188 bool ExtractSignatureMessage(
189 const DeltaArchiveManifest_InstallOperation& operation);
190
Darin Petkov437adc42010-10-07 13:12:24 -0700191 // Updates the hash calculator with |count| bytes at the head of |buffer_| and
192 // then discards them.
193 void DiscardBufferHeadBytes(size_t count);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700194
Darin Petkov0406e402010-10-06 21:33:11 -0700195 // Checkpoints the update progress into persistent storage to allow this
196 // update attempt to be resumed after reboot.
Darin Petkov73058b42010-10-06 16:32:19 -0700197 bool CheckpointUpdateProgress();
198
Darin Petkov9b230572010-10-08 10:20:09 -0700199 // Primes the required update state. Returns true if the update state was
200 // successfully initialized to a saved resume state or if the update is a new
201 // update. Returns false otherwise.
202 bool PrimeUpdateState();
203
Darin Petkov73058b42010-10-06 16:32:19 -0700204 // Update Engine preference store.
205 PrefsInterface* prefs_;
206
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700207 // Install Plan based on Omaha Response.
208 InstallPlan* install_plan_;
209
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700210 // File descriptor of open device.
211 int fd_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700212
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700213 // File descriptor of the kernel device
214 int kernel_fd_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700215
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700216 std::string path_; // Path that fd_ refers to.
217 std::string kernel_path_; // Path that kernel_fd_ refers to.
Darin Petkovd7061ab2010-10-06 14:37:09 -0700218
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700219 DeltaArchiveManifest manifest_;
220 bool manifest_valid_;
Darin Petkov437adc42010-10-07 13:12:24 -0700221 uint64_t manifest_metadata_size_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700222
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700223 // Index of the next operation to perform in the manifest.
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700224 int next_operation_num_;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700225
226 // buffer_ is a window of the data that's been downloaded. At first,
227 // it contains the beginning of the download, but after the protobuf
228 // has been downloaded and parsed, it contains a sliding window of
229 // data blobs.
230 std::vector<char> buffer_;
231 // Offset of buffer_ in the binary blobs section of the update.
232 uint64_t buffer_offset_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700233
Darin Petkov0406e402010-10-06 21:33:11 -0700234 // Last |buffer_offset_| value updated as part of the progress update.
235 uint64_t last_updated_buffer_offset_;
236
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700237 // The block size (parsed from the manifest).
238 uint32_t block_size_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700239
Darin Petkov437adc42010-10-07 13:12:24 -0700240 // Calculates the payload hash.
Darin Petkovd7061ab2010-10-06 14:37:09 -0700241 OmahaHashCalculator hash_calculator_;
242
Darin Petkov437adc42010-10-07 13:12:24 -0700243 // Saves the signed hash context.
244 std::string signed_hash_context_;
245
Darin Petkovd7061ab2010-10-06 14:37:09 -0700246 // Signatures message blob extracted directly from the payload.
247 std::vector<char> signatures_message_data_;
248
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700249 // The public key to be used. Provided as a member so that tests can
250 // override with test keys.
251 std::string public_key_path_;
Darin Petkov698d0412010-10-13 10:59:44 -0700252
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700253 DISALLOW_COPY_AND_ASSIGN(DeltaPerformer);
254};
255
256} // namespace chromeos_update_engine
257
258#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_PERFORMER_H__