Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 1 | // 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 Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 9 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 10 | #include <vector> |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 11 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 12 | #include <google/protobuf/repeated_field.h> |
Andrew de los Reyes | 353777c | 2010-10-08 10:34:30 -0700 | [diff] [blame] | 13 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 14 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 15 | #include "update_engine/file_writer.h" |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 16 | #include "update_engine/install_plan.h" |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 17 | #include "update_engine/omaha_hash_calculator.h" |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 18 | #include "update_engine/update_metadata.pb.h" |
| 19 | |
| 20 | namespace chromeos_update_engine { |
| 21 | |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 22 | class PrefsInterface; |
| 23 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 24 | // 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 | |
| 27 | class DeltaPerformer : public FileWriter { |
| 28 | public: |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 29 | enum MetadataParseResult { |
| 30 | kMetadataParseSuccess, |
| 31 | kMetadataParseError, |
| 32 | kMetadataParseInsufficientData, |
| 33 | }; |
| 34 | |
Darin Petkov | abc7bc0 | 2011-02-23 14:39:43 -0800 | [diff] [blame] | 35 | static const char kUpdatePayloadPublicKeyPath[]; |
| 36 | |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 37 | DeltaPerformer(PrefsInterface* prefs, InstallPlan* install_plan) |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 38 | : prefs_(prefs), |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 39 | install_plan_(install_plan), |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 40 | fd_(-1), |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 41 | kernel_fd_(-1), |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 42 | manifest_valid_(false), |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 43 | manifest_metadata_size_(0), |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 44 | next_operation_num_(0), |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 45 | buffer_offset_(0), |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 46 | last_updated_buffer_offset_(kuint64max), |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 47 | block_size_(0), |
| 48 | public_key_path_(kUpdatePayloadPublicKeyPath) {} |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 49 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 50 | // 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 Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 54 | // 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 Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 58 | // 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 Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 68 | |
| 69 | // Wrapper around close. Returns 0 on success or -errno on error. |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 70 | // Closes both 'path' given to Open() and the kernel path. |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 71 | int Close(); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 72 | |
| 73 | // Verifies the downloaded payload against the signed hash included in the |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 74 | // 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 Reyes | 771e1bd | 2011-08-30 14:47:23 -0700 | [diff] [blame] | 81 | const uint64_t update_check_response_size); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 82 | |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 83 | // 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 Petkov | 2dd0109 | 2010-10-08 15:43:05 -0700 | [diff] [blame] | 93 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 94 | // 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 Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 109 | // 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 Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 115 | // 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 Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 119 | |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 120 | // 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 Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 126 | MetadataParseResult ParsePayloadMetadata( |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 127 | const std::vector<char>& payload, |
| 128 | DeltaArchiveManifest* manifest, |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 129 | uint64_t* metadata_size, |
| 130 | ActionExitCode* error); |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 131 | |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 132 | void set_public_key_path(const std::string& public_key_path) { |
| 133 | public_key_path_ = public_key_path; |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 136 | private: |
Andrew de los Reyes | 353777c | 2010-10-08 10:34:30 -0700 | [diff] [blame] | 137 | friend class DeltaPerformerTest; |
| 138 | FRIEND_TEST(DeltaPerformerTest, IsIdempotentOperationTest); |
| 139 | |
| 140 | static bool IsIdempotentOperation( |
| 141 | const DeltaArchiveManifest_InstallOperation& op); |
| 142 | |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 143 | // 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 Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 149 | // 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 Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 153 | |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 154 | // 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( |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame^] | 158 | const DeltaArchiveManifest_InstallOperation& operation, bool should_log); |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 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 Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 171 | // Returns true on success. |
| 172 | bool PerformInstallOperation( |
| 173 | const DeltaArchiveManifest_InstallOperation& operation); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 174 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 175 | // These perform a specific type of operation and return true on success. |
| 176 | bool PerformReplaceOperation( |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 177 | const DeltaArchiveManifest_InstallOperation& operation, |
| 178 | bool is_kernel_partition); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 179 | bool PerformMoveOperation( |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 180 | const DeltaArchiveManifest_InstallOperation& operation, |
| 181 | bool is_kernel_partition); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 182 | bool PerformBsdiffOperation( |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 183 | const DeltaArchiveManifest_InstallOperation& operation, |
| 184 | bool is_kernel_partition); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 185 | |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 186 | // 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 Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 191 | // Updates the hash calculator with |count| bytes at the head of |buffer_| and |
| 192 | // then discards them. |
| 193 | void DiscardBufferHeadBytes(size_t count); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 194 | |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 195 | // Checkpoints the update progress into persistent storage to allow this |
| 196 | // update attempt to be resumed after reboot. |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 197 | bool CheckpointUpdateProgress(); |
| 198 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 199 | // 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 Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 204 | // Update Engine preference store. |
| 205 | PrefsInterface* prefs_; |
| 206 | |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 207 | // Install Plan based on Omaha Response. |
| 208 | InstallPlan* install_plan_; |
| 209 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 210 | // File descriptor of open device. |
| 211 | int fd_; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 212 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 213 | // File descriptor of the kernel device |
| 214 | int kernel_fd_; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 215 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 216 | std::string path_; // Path that fd_ refers to. |
| 217 | std::string kernel_path_; // Path that kernel_fd_ refers to. |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 218 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 219 | DeltaArchiveManifest manifest_; |
| 220 | bool manifest_valid_; |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 221 | uint64_t manifest_metadata_size_; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 222 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 223 | // Index of the next operation to perform in the manifest. |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 224 | int next_operation_num_; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 225 | |
| 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 Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 233 | |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 234 | // Last |buffer_offset_| value updated as part of the progress update. |
| 235 | uint64_t last_updated_buffer_offset_; |
| 236 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 237 | // The block size (parsed from the manifest). |
| 238 | uint32_t block_size_; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 239 | |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 240 | // Calculates the payload hash. |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 241 | OmahaHashCalculator hash_calculator_; |
| 242 | |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 243 | // Saves the signed hash context. |
| 244 | std::string signed_hash_context_; |
| 245 | |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 246 | // Signatures message blob extracted directly from the payload. |
| 247 | std::vector<char> signatures_message_data_; |
| 248 | |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 249 | // 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 Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 252 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 253 | DISALLOW_COPY_AND_ASSIGN(DeltaPerformer); |
| 254 | }; |
| 255 | |
| 256 | } // namespace chromeos_update_engine |
| 257 | |
| 258 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_PERFORMER_H__ |