Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 2 | // 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 Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 6 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 7 | #include <endian.h> |
| 8 | #include <errno.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 <algorithm> |
| 11 | #include <cstring> |
| 12 | #include <string> |
| 13 | #include <vector> |
| 14 | |
Chris Masone | d903c3b | 2011-05-12 15:35:46 -0700 | [diff] [blame] | 15 | #include <base/memory/scoped_ptr.h> |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 16 | #include <base/string_util.h> |
Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 17 | #include <base/stringprintf.h> |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 18 | #include <google/protobuf/repeated_field.h> |
| 19 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 20 | #include "update_engine/bzip_extent_writer.h" |
| 21 | #include "update_engine/delta_diff_generator.h" |
Andrew de los Reyes | 353777c | 2010-10-08 10:34:30 -0700 | [diff] [blame] | 22 | #include "update_engine/extent_ranges.h" |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 23 | #include "update_engine/extent_writer.h" |
| 24 | #include "update_engine/graph_types.h" |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 25 | #include "update_engine/payload_signer.h" |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 26 | #include "update_engine/prefs_interface.h" |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 27 | #include "update_engine/subprocess.h" |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 28 | #include "update_engine/terminator.h" |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 29 | |
| 30 | using std::min; |
| 31 | using std::string; |
| 32 | using std::vector; |
| 33 | using google::protobuf::RepeatedPtrField; |
| 34 | |
| 35 | namespace chromeos_update_engine { |
| 36 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 37 | const uint64_t DeltaPerformer::kDeltaVersionSize = 8; |
| 38 | const uint64_t DeltaPerformer::kDeltaManifestSizeSize = 8; |
Darin Petkov | abc7bc0 | 2011-02-23 14:39:43 -0800 | [diff] [blame] | 39 | const char DeltaPerformer::kUpdatePayloadPublicKeyPath[] = |
| 40 | "/usr/share/update_engine/update-payload-key.pub.pem"; |
| 41 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 42 | namespace { |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 43 | const int kUpdateStateOperationInvalid = -1; |
Darin Petkov | 6142614 | 2010-10-08 11:04:55 -0700 | [diff] [blame] | 44 | const int kMaxResumedUpdateFailures = 10; |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 45 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 46 | // Converts extents to a human-readable string, for use by DumpUpdateProto(). |
| 47 | string ExtentsToString(const RepeatedPtrField<Extent>& extents) { |
| 48 | string ret; |
| 49 | for (int i = 0; i < extents.size(); i++) { |
| 50 | const Extent& extent = extents.Get(i); |
| 51 | if (extent.start_block() == kSparseHole) { |
| 52 | ret += StringPrintf("{kSparseHole, %" PRIu64 "}, ", extent.num_blocks()); |
| 53 | } else { |
| 54 | ret += StringPrintf("{%" PRIu64 ", %" PRIu64 "}, ", |
| 55 | extent.start_block(), extent.num_blocks()); |
| 56 | } |
| 57 | } |
| 58 | if (!ret.empty()) { |
| 59 | DCHECK_GT(ret.size(), static_cast<size_t>(1)); |
| 60 | ret.resize(ret.size() - 2); |
| 61 | } |
| 62 | return ret; |
| 63 | } |
| 64 | |
| 65 | // LOGs a DeltaArchiveManifest object. Useful for debugging. |
| 66 | void DumpUpdateProto(const DeltaArchiveManifest& manifest) { |
| 67 | LOG(INFO) << "Update Proto:"; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 68 | LOG(INFO) << " block_size: " << manifest.block_size(); |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 69 | for (int i = 0; i < (manifest.install_operations_size() + |
| 70 | manifest.kernel_install_operations_size()); i++) { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 71 | const DeltaArchiveManifest_InstallOperation& op = |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 72 | i < manifest.install_operations_size() ? |
| 73 | manifest.install_operations(i) : |
| 74 | manifest.kernel_install_operations( |
| 75 | i - manifest.install_operations_size()); |
| 76 | if (i == 0) |
| 77 | LOG(INFO) << " Rootfs ops:"; |
| 78 | else if (i == manifest.install_operations_size()) |
| 79 | LOG(INFO) << " Kernel ops:"; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 80 | LOG(INFO) << " operation(" << i << ")"; |
| 81 | LOG(INFO) << " type: " |
| 82 | << DeltaArchiveManifest_InstallOperation_Type_Name(op.type()); |
| 83 | if (op.has_data_offset()) |
| 84 | LOG(INFO) << " data_offset: " << op.data_offset(); |
| 85 | if (op.has_data_length()) |
| 86 | LOG(INFO) << " data_length: " << op.data_length(); |
| 87 | LOG(INFO) << " src_extents: " << ExtentsToString(op.src_extents()); |
| 88 | if (op.has_src_length()) |
| 89 | LOG(INFO) << " src_length: " << op.src_length(); |
| 90 | LOG(INFO) << " dst_extents: " << ExtentsToString(op.dst_extents()); |
| 91 | if (op.has_dst_length()) |
| 92 | LOG(INFO) << " dst_length: " << op.dst_length(); |
| 93 | } |
| 94 | } |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 95 | |
| 96 | // Opens path for read/write, put the fd into *fd. On success returns true |
| 97 | // and sets *err to 0. On failure, returns false and sets *err to errno. |
| 98 | bool OpenFile(const char* path, int* fd, int* err) { |
| 99 | if (*fd != -1) { |
| 100 | LOG(ERROR) << "Can't open(" << path << "), *fd != -1 (it's " << *fd << ")"; |
| 101 | *err = EINVAL; |
| 102 | return false; |
| 103 | } |
| 104 | *fd = open(path, O_RDWR, 000); |
| 105 | if (*fd < 0) { |
| 106 | *err = errno; |
| 107 | PLOG(ERROR) << "Unable to open file " << path; |
| 108 | return false; |
| 109 | } |
| 110 | *err = 0; |
| 111 | return true; |
| 112 | } |
| 113 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 114 | } // namespace {} |
| 115 | |
Andrew de los Reyes | 353777c | 2010-10-08 10:34:30 -0700 | [diff] [blame] | 116 | // Returns true if |op| is idempotent -- i.e., if we can interrupt it and repeat |
| 117 | // it safely. Returns false otherwise. |
| 118 | bool DeltaPerformer::IsIdempotentOperation( |
| 119 | const DeltaArchiveManifest_InstallOperation& op) { |
| 120 | if (op.src_extents_size() == 0) { |
| 121 | return true; |
| 122 | } |
Darin Petkov | 9fa7ec5 | 2010-10-18 11:45:23 -0700 | [diff] [blame] | 123 | // When in doubt, it's safe to declare an op non-idempotent. Note that we |
| 124 | // could detect other types of idempotent operations here such as a MOVE that |
| 125 | // moves blocks onto themselves. However, we rely on the server to not send |
| 126 | // such operations at all. |
Andrew de los Reyes | 353777c | 2010-10-08 10:34:30 -0700 | [diff] [blame] | 127 | ExtentRanges src_ranges; |
| 128 | src_ranges.AddRepeatedExtents(op.src_extents()); |
| 129 | const uint64_t block_count = src_ranges.blocks(); |
| 130 | src_ranges.SubtractRepeatedExtents(op.dst_extents()); |
| 131 | return block_count == src_ranges.blocks(); |
| 132 | } |
| 133 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 134 | int DeltaPerformer::Open(const char* path, int flags, mode_t mode) { |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 135 | int err; |
| 136 | if (OpenFile(path, &fd_, &err)) |
| 137 | path_ = path; |
| 138 | return -err; |
| 139 | } |
| 140 | |
| 141 | bool DeltaPerformer::OpenKernel(const char* kernel_path) { |
| 142 | int err; |
| 143 | bool success = OpenFile(kernel_path, &kernel_fd_, &err); |
| 144 | if (success) |
| 145 | kernel_path_ = kernel_path; |
| 146 | return success; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | int DeltaPerformer::Close() { |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 150 | int err = 0; |
| 151 | if (close(kernel_fd_) == -1) { |
| 152 | err = errno; |
| 153 | PLOG(ERROR) << "Unable to close kernel fd:"; |
| 154 | } |
| 155 | if (close(fd_) == -1) { |
| 156 | err = errno; |
| 157 | PLOG(ERROR) << "Unable to close rootfs fd:"; |
| 158 | } |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 159 | LOG_IF(ERROR, !hash_calculator_.Finalize()) << "Unable to finalize the hash."; |
Darin Petkov | 934bb41 | 2010-11-18 11:21:35 -0800 | [diff] [blame] | 160 | fd_ = -2; // Set to invalid so that calls to Open() will fail. |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 161 | path_ = ""; |
Darin Petkov | 934bb41 | 2010-11-18 11:21:35 -0800 | [diff] [blame] | 162 | if (!buffer_.empty()) { |
| 163 | LOG(ERROR) << "Called Close() while buffer not empty!"; |
| 164 | if (err >= 0) { |
| 165 | err = 1; |
| 166 | } |
| 167 | } |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 168 | return -err; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame] | 171 | namespace { |
| 172 | |
| 173 | void LogPartitionInfoHash(const PartitionInfo& info, const string& tag) { |
| 174 | string sha256; |
| 175 | if (OmahaHashCalculator::Base64Encode(info.hash().data(), |
| 176 | info.hash().size(), |
| 177 | &sha256)) { |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 178 | LOG(INFO) << "PartitionInfo " << tag << " sha256: " << sha256 |
| 179 | << " size: " << info.size(); |
Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame] | 180 | } else { |
| 181 | LOG(ERROR) << "Base64Encode failed for tag: " << tag; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | void LogPartitionInfo(const DeltaArchiveManifest& manifest) { |
| 186 | if (manifest.has_old_kernel_info()) |
| 187 | LogPartitionInfoHash(manifest.old_kernel_info(), "old_kernel_info"); |
| 188 | if (manifest.has_old_rootfs_info()) |
| 189 | LogPartitionInfoHash(manifest.old_rootfs_info(), "old_rootfs_info"); |
| 190 | if (manifest.has_new_kernel_info()) |
| 191 | LogPartitionInfoHash(manifest.new_kernel_info(), "new_kernel_info"); |
| 192 | if (manifest.has_new_rootfs_info()) |
| 193 | LogPartitionInfoHash(manifest.new_rootfs_info(), "new_rootfs_info"); |
| 194 | } |
| 195 | |
| 196 | } // namespace {} |
| 197 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 198 | uint64_t DeltaPerformer::GetManifestSizeOffset() { |
| 199 | // Manifest size is stored right after the magic string and the version. |
| 200 | return strlen(kDeltaMagic) + kDeltaVersionSize; |
| 201 | } |
| 202 | |
| 203 | uint64_t DeltaPerformer::GetManifestOffset() { |
| 204 | // Actual manifest begins right after the manifest size field. |
| 205 | return GetManifestSizeOffset() + kDeltaManifestSizeSize; |
| 206 | } |
| 207 | |
| 208 | |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 209 | DeltaPerformer::MetadataParseResult DeltaPerformer::ParsePayloadMetadata( |
| 210 | const std::vector<char>& payload, |
| 211 | DeltaArchiveManifest* manifest, |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 212 | uint64_t* metadata_size, |
| 213 | ActionExitCode* error) { |
| 214 | *error = kActionCodeSuccess; |
| 215 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 216 | // manifest_offset is the byte offset where the manifest protobuf begins. |
| 217 | const uint64_t manifest_offset = GetManifestOffset(); |
| 218 | if (payload.size() < manifest_offset) { |
| 219 | // Don't have enough bytes to even know the manifest size. |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 220 | return kMetadataParseInsufficientData; |
| 221 | } |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 222 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 223 | // Validate the magic string. |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 224 | if (memcmp(payload.data(), kDeltaMagic, strlen(kDeltaMagic)) != 0) { |
| 225 | LOG(ERROR) << "Bad payload format -- invalid delta magic."; |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 226 | *error = kActionCodeDownloadInvalidMetadataMagicString; |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 227 | return kMetadataParseError; |
| 228 | } |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 229 | |
| 230 | // TODO(jaysri): Compare the version number and skip unknown manifest |
| 231 | // versions. We don't check the version at all today. |
| 232 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 233 | // Next, parse the manifest size. |
| 234 | uint64_t manifest_size; |
| 235 | COMPILE_ASSERT(sizeof(manifest_size) == kDeltaManifestSizeSize, |
| 236 | manifest_size_size_mismatch); |
| 237 | memcpy(&manifest_size, |
| 238 | &payload[GetManifestSizeOffset()], |
| 239 | kDeltaManifestSizeSize); |
| 240 | manifest_size = be64toh(manifest_size); // switch big endian to host |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 241 | |
| 242 | // Now, check if the metasize we computed matches what was passed in |
| 243 | // through Omaha Response. |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 244 | *metadata_size = manifest_offset + manifest_size; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 245 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 246 | // If the metadata size is present in install plan, check for it immediately |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 247 | // even before waiting for that many number of bytes to be downloaded |
| 248 | // in the payload. This will prevent any attack which relies on us downloading |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 249 | // data beyond the expected metadata size. |
| 250 | if (install_plan_->metadata_size > 0 && |
| 251 | install_plan_->metadata_size != *metadata_size) { |
| 252 | LOG(ERROR) << "Invalid metadata size. Expected = " |
| 253 | << install_plan_->metadata_size |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 254 | << "Actual = " << *metadata_size; |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame^] | 255 | // Send a UMA Stat here to help with the decision to enforce |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 256 | // this check in a future release, as mentioned below. |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame^] | 257 | SendUMAStat(kActionCodeDownloadInvalidMetadataSize); |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 258 | |
| 259 | // TODO(jaysri): VALIDATION: Initially we don't want to make this a fatal |
| 260 | // error. But in the next release, we should uncomment the lines below. |
| 261 | // *error = kActionCodeDownloadInvalidManifest; |
| 262 | // return kMetadataParseError; |
| 263 | } |
| 264 | |
| 265 | // Now that we have validated the metadata size, we should wait for the full |
| 266 | // metadata to be read in before we can parse it. |
| 267 | if (payload.size() < *metadata_size) { |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 268 | return kMetadataParseInsufficientData; |
| 269 | } |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 270 | |
| 271 | // Log whether we validated the size or simply trusting what's in the payload |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 272 | // here. This is logged here (after we received the full metadata data) so |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 273 | // that we just log once (instead of logging n times) if it takes n |
| 274 | // DeltaPerformer::Write calls to download the full manifest. |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 275 | if (install_plan_->metadata_size == 0) |
| 276 | LOG(WARNING) << "No metadata size specified in Omaha. " |
| 277 | << "Trusting metadata size in payload = " << *metadata_size; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 278 | else |
| 279 | LOG(INFO) << "Manifest size in payload matches expected value from Omaha"; |
| 280 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 281 | // We have the full metadata in |payload|. Verify its integrity |
| 282 | // and authenticity based on the information we have in Omaha response. |
| 283 | *error = ValidateMetadataSignature(&payload[0], *metadata_size); |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 284 | if (*error != kActionCodeSuccess) { |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame^] | 285 | // Send a UMA Stat here to help with the decision to enforce |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 286 | // this check in a future release, as mentioned below. |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame^] | 287 | SendUMAStat(*error); |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 288 | |
| 289 | // TODO(jaysri): VALIDATION: Initially we don't want to make this a fatal |
| 290 | // error. But in the next release, we should remove the line below and |
| 291 | // return an error. |
| 292 | *error = kActionCodeSuccess; |
| 293 | } |
| 294 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 295 | // The metadata in |payload| is deemed valid. So, it's now safe to |
| 296 | // parse the protobuf. |
| 297 | if (!manifest->ParseFromArray(&payload[manifest_offset], manifest_size)) { |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 298 | LOG(ERROR) << "Unable to parse manifest in update file."; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 299 | *error = kActionCodeDownloadManifestParseError; |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 300 | return kMetadataParseError; |
| 301 | } |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 302 | return kMetadataParseSuccess; |
| 303 | } |
| 304 | |
| 305 | |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 306 | // Wrapper around write. Returns true if all requested bytes |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 307 | // were written, or false on any error, reguardless of progress |
| 308 | // and stores an action exit code in |error|. |
| 309 | bool DeltaPerformer::Write(const void* bytes, size_t count, |
| 310 | ActionExitCode *error) { |
| 311 | *error = kActionCodeSuccess; |
| 312 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 313 | const char* c_bytes = reinterpret_cast<const char*>(bytes); |
| 314 | buffer_.insert(buffer_.end(), c_bytes, c_bytes + count); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 315 | if (!manifest_valid_) { |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 316 | MetadataParseResult result = ParsePayloadMetadata(buffer_, |
| 317 | &manifest_, |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 318 | &manifest_metadata_size_, |
| 319 | error); |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 320 | if (result == kMetadataParseError) { |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 321 | return false; |
Darin Petkov | 934bb41 | 2010-11-18 11:21:35 -0800 | [diff] [blame] | 322 | } |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 323 | if (result == kMetadataParseInsufficientData) { |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 324 | return true; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 325 | } |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 326 | // Remove protobuf and header info from buffer_, so buffer_ contains |
| 327 | // just data blobs |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 328 | DiscardBufferHeadBytes(manifest_metadata_size_); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 329 | LOG_IF(WARNING, !prefs_->SetInt64(kPrefsManifestMetadataSize, |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 330 | manifest_metadata_size_)) |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 331 | << "Unable to save the manifest metadata size."; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 332 | manifest_valid_ = true; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 333 | |
Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame] | 334 | LogPartitionInfo(manifest_); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 335 | if (!PrimeUpdateState()) { |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 336 | *error = kActionCodeDownloadStateInitializationError; |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 337 | LOG(ERROR) << "Unable to prime the update state."; |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 338 | return false; |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 339 | } |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 340 | } |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 341 | ssize_t total_operations = manifest_.install_operations_size() + |
| 342 | manifest_.kernel_install_operations_size(); |
| 343 | while (next_operation_num_ < total_operations) { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 344 | const DeltaArchiveManifest_InstallOperation &op = |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 345 | next_operation_num_ < manifest_.install_operations_size() ? |
| 346 | manifest_.install_operations(next_operation_num_) : |
| 347 | manifest_.kernel_install_operations( |
| 348 | next_operation_num_ - manifest_.install_operations_size()); |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 349 | if (!CanPerformInstallOperation(op)) { |
| 350 | // This means we don't have enough bytes received yet to carry out the |
| 351 | // next operation. |
| 352 | return true; |
| 353 | } |
| 354 | |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 355 | bool should_log = (next_operation_num_ % 1000 == 0 || |
| 356 | next_operation_num_ == total_operations - 1); |
| 357 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 358 | // Validate the operation only if the metadata signature is present. |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 359 | // Otherwise, keep the old behavior. This serves as a knob to disable |
| 360 | // the validation logic in case we find some regression after rollout. |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 361 | if (!install_plan_->metadata_signature.empty()) { |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 362 | // Note: Validate must be called only if CanPerformInstallOperation is |
| 363 | // called. Otherwise, we might be failing operations before even if there |
| 364 | // isn't sufficient data to compute the proper hash. |
| 365 | *error = ValidateOperationHash(op, should_log); |
| 366 | if (*error != kActionCodeSuccess) { |
| 367 | // Cannot proceed further as operation hash is invalid. |
| 368 | // Higher level code will take care of retrying appropriately. |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame^] | 369 | |
| 370 | // Send a UMA stat to indicate that an operation hash failed to be |
| 371 | // validated as expected. |
| 372 | SendUMAStat(*error); |
| 373 | |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 374 | // TODO(jaysri): VALIDATION: For now, we don't treat this as fatal. |
| 375 | // But once we're confident that the new code works fine in the field, |
| 376 | // we should uncomment the line below. |
| 377 | // return false; |
| 378 | LOG(INFO) << "Ignoring operation validation errors for now"; |
| 379 | } |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Darin Petkov | 45580e4 | 2010-10-08 14:02:40 -0700 | [diff] [blame] | 382 | // Makes sure we unblock exit when this operation completes. |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 383 | ScopedTerminatorExitUnblocker exit_unblocker = |
| 384 | ScopedTerminatorExitUnblocker(); // Avoids a compiler unused var bug. |
Andrew de los Reyes | bef0c7d | 2010-08-20 10:20:10 -0700 | [diff] [blame] | 385 | // Log every thousandth operation, and also the first and last ones |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 386 | if (should_log) { |
Andrew de los Reyes | bef0c7d | 2010-08-20 10:20:10 -0700 | [diff] [blame] | 387 | LOG(INFO) << "Performing operation " << (next_operation_num_ + 1) << "/" |
| 388 | << total_operations; |
| 389 | } |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 390 | bool is_kernel_partition = |
| 391 | (next_operation_num_ >= manifest_.install_operations_size()); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 392 | if (op.type() == DeltaArchiveManifest_InstallOperation_Type_REPLACE || |
| 393 | op.type() == DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) { |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 394 | if (!PerformReplaceOperation(op, is_kernel_partition)) { |
| 395 | LOG(ERROR) << "Failed to perform replace operation " |
| 396 | << next_operation_num_; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 397 | *error = kActionCodeDownloadOperationExecutionError; |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 398 | return false; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 399 | } |
| 400 | } else if (op.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE) { |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 401 | if (!PerformMoveOperation(op, is_kernel_partition)) { |
| 402 | LOG(ERROR) << "Failed to perform move operation " |
| 403 | << next_operation_num_; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 404 | *error = kActionCodeDownloadOperationExecutionError; |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 405 | return false; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 406 | } |
| 407 | } else if (op.type() == DeltaArchiveManifest_InstallOperation_Type_BSDIFF) { |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 408 | if (!PerformBsdiffOperation(op, is_kernel_partition)) { |
| 409 | LOG(ERROR) << "Failed to perform bsdiff operation " |
| 410 | << next_operation_num_; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 411 | *error = kActionCodeDownloadOperationExecutionError; |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 412 | return false; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 413 | } |
| 414 | } |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 415 | next_operation_num_++; |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 416 | CheckpointUpdateProgress(); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 417 | } |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 418 | return true; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | bool DeltaPerformer::CanPerformInstallOperation( |
| 422 | const chromeos_update_engine::DeltaArchiveManifest_InstallOperation& |
| 423 | operation) { |
| 424 | // Move operations don't require any data blob, so they can always |
| 425 | // be performed |
| 426 | if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE) |
| 427 | return true; |
| 428 | |
| 429 | // See if we have the entire data blob in the buffer |
| 430 | if (operation.data_offset() < buffer_offset_) { |
| 431 | LOG(ERROR) << "we threw away data it seems?"; |
| 432 | return false; |
| 433 | } |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 434 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 435 | return (operation.data_offset() + operation.data_length()) <= |
| 436 | (buffer_offset_ + buffer_.size()); |
| 437 | } |
| 438 | |
| 439 | bool DeltaPerformer::PerformReplaceOperation( |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 440 | const DeltaArchiveManifest_InstallOperation& operation, |
| 441 | bool is_kernel_partition) { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 442 | CHECK(operation.type() == \ |
| 443 | DeltaArchiveManifest_InstallOperation_Type_REPLACE || \ |
| 444 | operation.type() == \ |
| 445 | DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ); |
| 446 | |
| 447 | // Since we delete data off the beginning of the buffer as we use it, |
| 448 | // the data we need should be exactly at the beginning of the buffer. |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 449 | TEST_AND_RETURN_FALSE(buffer_offset_ == operation.data_offset()); |
| 450 | TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length()); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 451 | |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 452 | // Extract the signature message if it's in this operation. |
| 453 | ExtractSignatureMessage(operation); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 454 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 455 | DirectExtentWriter direct_writer; |
| 456 | ZeroPadExtentWriter zero_pad_writer(&direct_writer); |
| 457 | scoped_ptr<BzipExtentWriter> bzip_writer; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 458 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 459 | // Since bzip decompression is optional, we have a variable writer that will |
| 460 | // point to one of the ExtentWriter objects above. |
| 461 | ExtentWriter* writer = NULL; |
| 462 | if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_REPLACE) { |
| 463 | writer = &zero_pad_writer; |
| 464 | } else if (operation.type() == |
| 465 | DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) { |
| 466 | bzip_writer.reset(new BzipExtentWriter(&zero_pad_writer)); |
| 467 | writer = bzip_writer.get(); |
| 468 | } else { |
| 469 | NOTREACHED(); |
| 470 | } |
| 471 | |
| 472 | // Create a vector of extents to pass to the ExtentWriter. |
| 473 | vector<Extent> extents; |
| 474 | for (int i = 0; i < operation.dst_extents_size(); i++) { |
| 475 | extents.push_back(operation.dst_extents(i)); |
| 476 | } |
| 477 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 478 | int fd = is_kernel_partition ? kernel_fd_ : fd_; |
| 479 | |
| 480 | TEST_AND_RETURN_FALSE(writer->Init(fd, extents, block_size_)); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 481 | TEST_AND_RETURN_FALSE(writer->Write(&buffer_[0], operation.data_length())); |
| 482 | TEST_AND_RETURN_FALSE(writer->End()); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 483 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 484 | // Update buffer |
| 485 | buffer_offset_ += operation.data_length(); |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 486 | DiscardBufferHeadBytes(operation.data_length()); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 487 | return true; |
| 488 | } |
| 489 | |
| 490 | bool DeltaPerformer::PerformMoveOperation( |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 491 | const DeltaArchiveManifest_InstallOperation& operation, |
| 492 | bool is_kernel_partition) { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 493 | // Calculate buffer size. Note, this function doesn't do a sliding |
| 494 | // window to copy in case the source and destination blocks overlap. |
| 495 | // If we wanted to do a sliding window, we could program the server |
| 496 | // to generate deltas that effectively did a sliding window. |
| 497 | |
| 498 | uint64_t blocks_to_read = 0; |
| 499 | for (int i = 0; i < operation.src_extents_size(); i++) |
| 500 | blocks_to_read += operation.src_extents(i).num_blocks(); |
| 501 | |
| 502 | uint64_t blocks_to_write = 0; |
| 503 | for (int i = 0; i < operation.dst_extents_size(); i++) |
| 504 | blocks_to_write += operation.dst_extents(i).num_blocks(); |
| 505 | |
| 506 | DCHECK_EQ(blocks_to_write, blocks_to_read); |
| 507 | vector<char> buf(blocks_to_write * block_size_); |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 508 | |
| 509 | int fd = is_kernel_partition ? kernel_fd_ : fd_; |
| 510 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 511 | // Read in bytes. |
| 512 | ssize_t bytes_read = 0; |
| 513 | for (int i = 0; i < operation.src_extents_size(); i++) { |
| 514 | ssize_t bytes_read_this_iteration = 0; |
| 515 | const Extent& extent = operation.src_extents(i); |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 516 | TEST_AND_RETURN_FALSE(utils::PReadAll(fd, |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 517 | &buf[bytes_read], |
| 518 | extent.num_blocks() * block_size_, |
| 519 | extent.start_block() * block_size_, |
| 520 | &bytes_read_this_iteration)); |
| 521 | TEST_AND_RETURN_FALSE( |
| 522 | bytes_read_this_iteration == |
| 523 | static_cast<ssize_t>(extent.num_blocks() * block_size_)); |
| 524 | bytes_read += bytes_read_this_iteration; |
| 525 | } |
| 526 | |
Darin Petkov | 45580e4 | 2010-10-08 14:02:40 -0700 | [diff] [blame] | 527 | // If this is a non-idempotent operation, request a delayed exit and clear the |
| 528 | // update state in case the operation gets interrupted. Do this as late as |
| 529 | // possible. |
| 530 | if (!IsIdempotentOperation(operation)) { |
| 531 | Terminator::set_exit_blocked(true); |
| 532 | ResetUpdateProgress(prefs_, true); |
| 533 | } |
| 534 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 535 | // Write bytes out. |
| 536 | ssize_t bytes_written = 0; |
| 537 | for (int i = 0; i < operation.dst_extents_size(); i++) { |
| 538 | const Extent& extent = operation.dst_extents(i); |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 539 | TEST_AND_RETURN_FALSE(utils::PWriteAll(fd, |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 540 | &buf[bytes_written], |
| 541 | extent.num_blocks() * block_size_, |
| 542 | extent.start_block() * block_size_)); |
| 543 | bytes_written += extent.num_blocks() * block_size_; |
| 544 | } |
| 545 | DCHECK_EQ(bytes_written, bytes_read); |
| 546 | DCHECK_EQ(bytes_written, static_cast<ssize_t>(buf.size())); |
| 547 | return true; |
| 548 | } |
| 549 | |
| 550 | bool DeltaPerformer::ExtentsToBsdiffPositionsString( |
| 551 | const RepeatedPtrField<Extent>& extents, |
| 552 | uint64_t block_size, |
| 553 | uint64_t full_length, |
| 554 | string* positions_string) { |
| 555 | string ret; |
| 556 | uint64_t length = 0; |
| 557 | for (int i = 0; i < extents.size(); i++) { |
| 558 | Extent extent = extents.Get(i); |
| 559 | int64_t start = extent.start_block(); |
| 560 | uint64_t this_length = min(full_length - length, |
| 561 | extent.num_blocks() * block_size); |
| 562 | if (start == static_cast<int64_t>(kSparseHole)) |
| 563 | start = -1; |
| 564 | else |
| 565 | start *= block_size; |
| 566 | ret += StringPrintf("%" PRIi64 ":%" PRIu64 ",", start, this_length); |
| 567 | length += this_length; |
| 568 | } |
| 569 | TEST_AND_RETURN_FALSE(length == full_length); |
| 570 | if (!ret.empty()) |
| 571 | ret.resize(ret.size() - 1); // Strip trailing comma off |
| 572 | *positions_string = ret; |
| 573 | return true; |
| 574 | } |
| 575 | |
| 576 | bool DeltaPerformer::PerformBsdiffOperation( |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 577 | const DeltaArchiveManifest_InstallOperation& operation, |
| 578 | bool is_kernel_partition) { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 579 | // Since we delete data off the beginning of the buffer as we use it, |
| 580 | // the data we need should be exactly at the beginning of the buffer. |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 581 | TEST_AND_RETURN_FALSE(buffer_offset_ == operation.data_offset()); |
| 582 | TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length()); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 583 | |
| 584 | string input_positions; |
| 585 | TEST_AND_RETURN_FALSE(ExtentsToBsdiffPositionsString(operation.src_extents(), |
| 586 | block_size_, |
| 587 | operation.src_length(), |
| 588 | &input_positions)); |
| 589 | string output_positions; |
| 590 | TEST_AND_RETURN_FALSE(ExtentsToBsdiffPositionsString(operation.dst_extents(), |
| 591 | block_size_, |
| 592 | operation.dst_length(), |
| 593 | &output_positions)); |
| 594 | |
| 595 | string temp_filename; |
| 596 | TEST_AND_RETURN_FALSE(utils::MakeTempFile("/tmp/au_patch.XXXXXX", |
| 597 | &temp_filename, |
| 598 | NULL)); |
| 599 | ScopedPathUnlinker path_unlinker(temp_filename); |
| 600 | { |
| 601 | int fd = open(temp_filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 602 | ScopedFdCloser fd_closer(&fd); |
| 603 | TEST_AND_RETURN_FALSE( |
| 604 | utils::WriteAll(fd, &buffer_[0], operation.data_length())); |
| 605 | } |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 606 | |
| 607 | int fd = is_kernel_partition ? kernel_fd_ : fd_; |
Andrew de los Reyes | 5a23283 | 2010-10-12 16:20:54 -0700 | [diff] [blame] | 608 | const string& path = StringPrintf("/dev/fd/%d", fd); |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 609 | |
Darin Petkov | 45580e4 | 2010-10-08 14:02:40 -0700 | [diff] [blame] | 610 | // If this is a non-idempotent operation, request a delayed exit and clear the |
| 611 | // update state in case the operation gets interrupted. Do this as late as |
| 612 | // possible. |
| 613 | if (!IsIdempotentOperation(operation)) { |
| 614 | Terminator::set_exit_blocked(true); |
| 615 | ResetUpdateProgress(prefs_, true); |
| 616 | } |
| 617 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 618 | vector<string> cmd; |
| 619 | cmd.push_back(kBspatchPath); |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 620 | cmd.push_back(path); |
| 621 | cmd.push_back(path); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 622 | cmd.push_back(temp_filename); |
| 623 | cmd.push_back(input_positions); |
| 624 | cmd.push_back(output_positions); |
| 625 | int return_code = 0; |
Andrew de los Reyes | 5a23283 | 2010-10-12 16:20:54 -0700 | [diff] [blame] | 626 | TEST_AND_RETURN_FALSE( |
| 627 | Subprocess::SynchronousExecFlags(cmd, |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 628 | G_SPAWN_LEAVE_DESCRIPTORS_OPEN, |
Andrew de los Reyes | 5a23283 | 2010-10-12 16:20:54 -0700 | [diff] [blame] | 629 | &return_code, |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 630 | NULL)); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 631 | TEST_AND_RETURN_FALSE(return_code == 0); |
| 632 | |
| 633 | if (operation.dst_length() % block_size_) { |
| 634 | // Zero out rest of final block. |
| 635 | // TODO(adlr): build this into bspatch; it's more efficient that way. |
| 636 | const Extent& last_extent = |
| 637 | operation.dst_extents(operation.dst_extents_size() - 1); |
| 638 | const uint64_t end_byte = |
| 639 | (last_extent.start_block() + last_extent.num_blocks()) * block_size_; |
| 640 | const uint64_t begin_byte = |
| 641 | end_byte - (block_size_ - operation.dst_length() % block_size_); |
| 642 | vector<char> zeros(end_byte - begin_byte); |
| 643 | TEST_AND_RETURN_FALSE( |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 644 | utils::PWriteAll(fd, &zeros[0], end_byte - begin_byte, begin_byte)); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | // Update buffer. |
| 648 | buffer_offset_ += operation.data_length(); |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 649 | DiscardBufferHeadBytes(operation.data_length()); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 650 | return true; |
| 651 | } |
| 652 | |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 653 | bool DeltaPerformer::ExtractSignatureMessage( |
| 654 | const DeltaArchiveManifest_InstallOperation& operation) { |
| 655 | if (operation.type() != DeltaArchiveManifest_InstallOperation_Type_REPLACE || |
| 656 | !manifest_.has_signatures_offset() || |
| 657 | manifest_.signatures_offset() != operation.data_offset()) { |
| 658 | return false; |
| 659 | } |
| 660 | TEST_AND_RETURN_FALSE(manifest_.has_signatures_size() && |
| 661 | manifest_.signatures_size() == operation.data_length()); |
| 662 | TEST_AND_RETURN_FALSE(signatures_message_data_.empty()); |
| 663 | TEST_AND_RETURN_FALSE(buffer_offset_ == manifest_.signatures_offset()); |
| 664 | TEST_AND_RETURN_FALSE(buffer_.size() >= manifest_.signatures_size()); |
Darin Petkov | 4f0a07b | 2011-05-25 16:47:20 -0700 | [diff] [blame] | 665 | signatures_message_data_.assign( |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 666 | buffer_.begin(), |
| 667 | buffer_.begin() + manifest_.signatures_size()); |
Darin Petkov | 4f0a07b | 2011-05-25 16:47:20 -0700 | [diff] [blame] | 668 | |
| 669 | // Save the signature blob because if the update is interrupted after the |
| 670 | // download phase we don't go through this path anymore. Some alternatives to |
| 671 | // consider: |
| 672 | // |
| 673 | // 1. On resume, re-download the signature blob from the server and re-verify |
| 674 | // it. |
| 675 | // |
| 676 | // 2. Verify the signature as soon as it's received and don't checkpoint the |
| 677 | // blob and the signed sha-256 context. |
| 678 | LOG_IF(WARNING, !prefs_->SetString(kPrefsUpdateStateSignatureBlob, |
| 679 | string(&signatures_message_data_[0], |
| 680 | signatures_message_data_.size()))) |
| 681 | << "Unable to store the signature blob."; |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 682 | // The hash of all data consumed so far should be verified against the signed |
| 683 | // hash. |
| 684 | signed_hash_context_ = hash_calculator_.GetContext(); |
| 685 | LOG_IF(WARNING, !prefs_->SetString(kPrefsUpdateStateSignedSHA256Context, |
| 686 | signed_hash_context_)) |
| 687 | << "Unable to store the signed hash context."; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 688 | LOG(INFO) << "Extracted signature data of size " |
| 689 | << manifest_.signatures_size() << " at " |
| 690 | << manifest_.signatures_offset(); |
| 691 | return true; |
| 692 | } |
| 693 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 694 | ActionExitCode DeltaPerformer::ValidateMetadataSignature( |
| 695 | const char* metadata, uint64_t metadata_size) { |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 696 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 697 | if (install_plan_->metadata_signature.empty()) { |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 698 | // If this is not present, we cannot validate the manifest. This should |
| 699 | // never happen in normal circumstances, but this can be used as a |
| 700 | // release-knob to turn off the new code path that verify per-operation |
| 701 | // hashes. So, for now, we should not treat this as a failure. Once we are |
| 702 | // confident this path is bug-free, we should treat this as a failure so |
| 703 | // that we remain robust even if the connection to Omaha is subjected to |
| 704 | // any SSL attack. |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 705 | LOG(WARNING) << "Cannot validate metadata as the signature is empty"; |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame^] | 706 | |
| 707 | // Send a UMA stat here so we're aware of any man-in-the-middle attempts to |
| 708 | // bypass these checks. |
| 709 | SendUMAStat(kActionCodeDownloadMetadataSignatureMissingError); |
| 710 | |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 711 | return kActionCodeSuccess; |
| 712 | } |
| 713 | |
| 714 | // Convert base64-encoded signature to raw bytes. |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 715 | vector<char> metadata_signature; |
| 716 | if (!OmahaHashCalculator::Base64Decode(install_plan_->metadata_signature, |
| 717 | &metadata_signature)) { |
| 718 | LOG(ERROR) << "Unable to decode base64 metadata signature: " |
| 719 | << install_plan_->metadata_signature; |
| 720 | return kActionCodeDownloadMetadataSignatureError; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 721 | } |
| 722 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 723 | vector<char> expected_metadata_hash; |
| 724 | if (!PayloadSigner::GetRawHashFromSignature(metadata_signature, |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 725 | public_key_path_, |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 726 | &expected_metadata_hash)) { |
| 727 | LOG(ERROR) << "Unable to compute expected hash from metadata signature"; |
| 728 | return kActionCodeDownloadMetadataSignatureError; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 729 | } |
| 730 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 731 | OmahaHashCalculator metadata_hasher; |
| 732 | metadata_hasher.Update(metadata, metadata_size); |
| 733 | if (!metadata_hasher.Finalize()) { |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 734 | LOG(ERROR) << "Unable to compute actual hash of manifest"; |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 735 | return kActionCodeDownloadMetadataSignatureVerificationError; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 736 | } |
| 737 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 738 | vector<char> calculated_metadata_hash = metadata_hasher.raw_hash(); |
| 739 | PayloadSigner::PadRSA2048SHA256Hash(&calculated_metadata_hash); |
| 740 | if (calculated_metadata_hash.empty()) { |
| 741 | LOG(ERROR) << "Computed actual hash of metadata is empty."; |
| 742 | return kActionCodeDownloadMetadataSignatureVerificationError; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 743 | } |
| 744 | |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 745 | if (calculated_metadata_hash != expected_metadata_hash) { |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 746 | LOG(ERROR) << "Manifest hash verification failed. Expected hash = "; |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 747 | utils::HexDumpVector(expected_metadata_hash); |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 748 | LOG(ERROR) << "Calculated hash = "; |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 749 | utils::HexDumpVector(calculated_metadata_hash); |
| 750 | return kActionCodeDownloadMetadataSignatureMismatch; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | LOG(INFO) << "Manifest signature matches expected value in Omaha response"; |
| 754 | return kActionCodeSuccess; |
| 755 | } |
| 756 | |
| 757 | ActionExitCode DeltaPerformer::ValidateOperationHash( |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 758 | const DeltaArchiveManifest_InstallOperation& operation, |
| 759 | bool should_log) { |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 760 | |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 761 | if (!operation.data_sha256_hash().size()) { |
| 762 | if (!operation.data_length()) { |
| 763 | // Operations that do not have any data blob won't have any operation hash |
| 764 | // either. So, these operations are always considered validated since the |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 765 | // metadata that contains all the non-data-blob portions of the operation |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 766 | // has already been validated. |
| 767 | return kActionCodeSuccess; |
| 768 | } |
| 769 | |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame^] | 770 | // Send a UMA stat here so we're aware of any man-in-the-middle attempts to |
| 771 | // bypass these checks. |
| 772 | SendUMAStat(kActionCodeDownloadOperationHashMissingError); |
| 773 | |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 774 | // TODO(jaysri): VALIDATION: no hash is present for the operation. This |
| 775 | // shouldn't happen normally for any client that has this code, because the |
| 776 | // corresponding update should have been produced with the operation |
| 777 | // hashes. But if it happens it's likely that we've turned this feature off |
| 778 | // in Omaha rule for some reason. Once we make these hashes mandatory, we |
| 779 | // should return an error here. |
| 780 | // One caveat though: The last operation is a dummy signature operation |
| 781 | // that doesn't have a hash at the time the manifest is created. So we |
| 782 | // should not complaint about that operation. This operation can be |
| 783 | // recognized by the fact that it's offset is mentioned in the manifest. |
| 784 | if (manifest_.signatures_offset() && |
| 785 | manifest_.signatures_offset() == operation.data_offset()) { |
| 786 | LOG(INFO) << "Skipping hash verification for signature operation " |
| 787 | << next_operation_num_ + 1; |
| 788 | } else { |
| 789 | // TODO(jaysri): Uncomment this logging after fixing dev server |
| 790 | // LOG(WARNING) << "Cannot validate operation " << next_operation_num_ + 1 |
| 791 | // << " as no expected hash present"; |
| 792 | } |
| 793 | return kActionCodeSuccess; |
| 794 | } |
| 795 | |
| 796 | vector<char> expected_op_hash; |
| 797 | expected_op_hash.assign(operation.data_sha256_hash().data(), |
| 798 | (operation.data_sha256_hash().data() + |
| 799 | operation.data_sha256_hash().size())); |
| 800 | |
| 801 | OmahaHashCalculator operation_hasher; |
| 802 | operation_hasher.Update(&buffer_[0], operation.data_length()); |
| 803 | if (!operation_hasher.Finalize()) { |
| 804 | LOG(ERROR) << "Unable to compute actual hash of operation " |
| 805 | << next_operation_num_; |
| 806 | return kActionCodeDownloadOperationHashVerificationError; |
| 807 | } |
| 808 | |
| 809 | vector<char> calculated_op_hash = operation_hasher.raw_hash(); |
| 810 | if (calculated_op_hash != expected_op_hash) { |
| 811 | LOG(ERROR) << "Hash verification failed for operation " |
| 812 | << next_operation_num_ << ". Expected hash = "; |
| 813 | utils::HexDumpVector(expected_op_hash); |
| 814 | LOG(ERROR) << "Calculated hash over " << operation.data_length() |
| 815 | << " bytes at offset: " << operation.data_offset() << " = "; |
| 816 | utils::HexDumpVector(calculated_op_hash); |
| 817 | return kActionCodeDownloadOperationHashMismatch; |
| 818 | } |
| 819 | |
| 820 | if (should_log) |
| 821 | LOG(INFO) << "Validated operation " << next_operation_num_ + 1; |
| 822 | |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 823 | return kActionCodeSuccess; |
| 824 | } |
| 825 | |
Andrew de los Reyes | 771e1bd | 2011-08-30 14:47:23 -0700 | [diff] [blame] | 826 | #define TEST_AND_RETURN_VAL(_retval, _condition) \ |
| 827 | do { \ |
| 828 | if (!(_condition)) { \ |
| 829 | LOG(ERROR) << "VerifyPayload failure: " << #_condition; \ |
| 830 | return _retval; \ |
| 831 | } \ |
| 832 | } while (0); |
Andrew de los Reyes | fb830ba | 2011-04-04 11:42:43 -0700 | [diff] [blame] | 833 | |
Andrew de los Reyes | 771e1bd | 2011-08-30 14:47:23 -0700 | [diff] [blame] | 834 | ActionExitCode DeltaPerformer::VerifyPayload( |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 835 | const std::string& update_check_response_hash, |
Andrew de los Reyes | 771e1bd | 2011-08-30 14:47:23 -0700 | [diff] [blame] | 836 | const uint64_t update_check_response_size) { |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 837 | LOG(INFO) << "Verifying delta payload using public key: " << public_key_path_; |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 838 | |
Jay Srinivasan | 0d8fb40 | 2012-05-07 19:19:38 -0700 | [diff] [blame] | 839 | // Verifies the download size. |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 840 | TEST_AND_RETURN_VAL(kActionCodePayloadSizeMismatchError, |
Jay Srinivasan | 0d8fb40 | 2012-05-07 19:19:38 -0700 | [diff] [blame] | 841 | update_check_response_size == |
| 842 | manifest_metadata_size_ + buffer_offset_); |
| 843 | |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 844 | // Verifies the payload hash. |
| 845 | const string& payload_hash_data = hash_calculator_.hash(); |
Andrew de los Reyes | 771e1bd | 2011-08-30 14:47:23 -0700 | [diff] [blame] | 846 | TEST_AND_RETURN_VAL(kActionCodeDownloadPayloadVerificationError, |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 847 | !payload_hash_data.empty()); |
| 848 | TEST_AND_RETURN_VAL(kActionCodePayloadHashMismatchError, |
| 849 | payload_hash_data == update_check_response_hash); |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 850 | |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 851 | // Verifies the signed payload hash. |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 852 | if (!utils::FileExists(public_key_path_.c_str())) { |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 853 | LOG(WARNING) << "Not verifying signed delta payload -- missing public key."; |
Andrew de los Reyes | 771e1bd | 2011-08-30 14:47:23 -0700 | [diff] [blame] | 854 | return kActionCodeSuccess; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 855 | } |
Andrew de los Reyes | 771e1bd | 2011-08-30 14:47:23 -0700 | [diff] [blame] | 856 | TEST_AND_RETURN_VAL(kActionCodeSignedDeltaPayloadExpectedError, |
| 857 | !signatures_message_data_.empty()); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 858 | vector<char> signed_hash_data; |
Andrew de los Reyes | 771e1bd | 2011-08-30 14:47:23 -0700 | [diff] [blame] | 859 | TEST_AND_RETURN_VAL(kActionCodeDownloadPayloadPubKeyVerificationError, |
| 860 | PayloadSigner::VerifySignature( |
| 861 | signatures_message_data_, |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 862 | public_key_path_, |
Andrew de los Reyes | 771e1bd | 2011-08-30 14:47:23 -0700 | [diff] [blame] | 863 | &signed_hash_data)); |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 864 | OmahaHashCalculator signed_hasher; |
Andrew de los Reyes | 771e1bd | 2011-08-30 14:47:23 -0700 | [diff] [blame] | 865 | TEST_AND_RETURN_VAL(kActionCodeDownloadPayloadPubKeyVerificationError, |
| 866 | signed_hasher.SetContext(signed_hash_context_)); |
| 867 | TEST_AND_RETURN_VAL(kActionCodeDownloadPayloadPubKeyVerificationError, |
| 868 | signed_hasher.Finalize()); |
Andrew de los Reyes | bdfaaf0 | 2011-03-30 10:35:12 -0700 | [diff] [blame] | 869 | vector<char> hash_data = signed_hasher.raw_hash(); |
| 870 | PayloadSigner::PadRSA2048SHA256Hash(&hash_data); |
Andrew de los Reyes | 771e1bd | 2011-08-30 14:47:23 -0700 | [diff] [blame] | 871 | TEST_AND_RETURN_VAL(kActionCodeDownloadPayloadPubKeyVerificationError, |
| 872 | !hash_data.empty()); |
Andrew de los Reyes | fb830ba | 2011-04-04 11:42:43 -0700 | [diff] [blame] | 873 | if (hash_data != signed_hash_data) { |
Andrew de los Reyes | 771e1bd | 2011-08-30 14:47:23 -0700 | [diff] [blame] | 874 | LOG(ERROR) << "Public key verification failed, thus update failed. " |
Andrew de los Reyes | fb830ba | 2011-04-04 11:42:43 -0700 | [diff] [blame] | 875 | "Attached Signature:"; |
| 876 | utils::HexDumpVector(signed_hash_data); |
| 877 | LOG(ERROR) << "Computed Signature:"; |
| 878 | utils::HexDumpVector(hash_data); |
Andrew de los Reyes | 771e1bd | 2011-08-30 14:47:23 -0700 | [diff] [blame] | 879 | return kActionCodeDownloadPayloadPubKeyVerificationError; |
Andrew de los Reyes | fb830ba | 2011-04-04 11:42:43 -0700 | [diff] [blame] | 880 | } |
Andrew de los Reyes | 771e1bd | 2011-08-30 14:47:23 -0700 | [diff] [blame] | 881 | return kActionCodeSuccess; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 882 | } |
| 883 | |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 884 | bool DeltaPerformer::GetNewPartitionInfo(uint64_t* kernel_size, |
| 885 | vector<char>* kernel_hash, |
| 886 | uint64_t* rootfs_size, |
| 887 | vector<char>* rootfs_hash) { |
Darin Petkov | 2dd0109 | 2010-10-08 15:43:05 -0700 | [diff] [blame] | 888 | TEST_AND_RETURN_FALSE(manifest_valid_ && |
| 889 | manifest_.has_new_kernel_info() && |
| 890 | manifest_.has_new_rootfs_info()); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 891 | *kernel_size = manifest_.new_kernel_info().size(); |
| 892 | *rootfs_size = manifest_.new_rootfs_info().size(); |
| 893 | vector<char> new_kernel_hash(manifest_.new_kernel_info().hash().begin(), |
| 894 | manifest_.new_kernel_info().hash().end()); |
| 895 | vector<char> new_rootfs_hash(manifest_.new_rootfs_info().hash().begin(), |
| 896 | manifest_.new_rootfs_info().hash().end()); |
| 897 | kernel_hash->swap(new_kernel_hash); |
| 898 | rootfs_hash->swap(new_rootfs_hash); |
Darin Petkov | 2dd0109 | 2010-10-08 15:43:05 -0700 | [diff] [blame] | 899 | return true; |
| 900 | } |
| 901 | |
Andrew de los Reyes | 100bb7d | 2011-08-09 17:35:07 -0700 | [diff] [blame] | 902 | namespace { |
| 903 | void LogVerifyError(bool is_kern, |
| 904 | const string& local_hash, |
| 905 | const string& expected_hash) { |
| 906 | const char* type = is_kern ? "kernel" : "rootfs"; |
| 907 | LOG(ERROR) << "This is a server-side error due to " |
| 908 | << "mismatched delta update image!"; |
| 909 | LOG(ERROR) << "The delta I've been given contains a " << type << " delta " |
| 910 | << "update that must be applied over a " << type << " with " |
| 911 | << "a specific checksum, but the " << type << " we're starting " |
| 912 | << "with doesn't have that checksum! This means that " |
| 913 | << "the delta I've been given doesn't match my existing " |
| 914 | << "system. The " << type << " partition I have has hash: " |
| 915 | << local_hash << " but the update expected me to have " |
| 916 | << expected_hash << " ."; |
| 917 | if (is_kern) { |
| 918 | LOG(INFO) << "To get the checksum of a kernel partition on a " |
| 919 | << "booted machine, run this command (change /dev/sda2 " |
| 920 | << "as needed): dd if=/dev/sda2 bs=1M 2>/dev/null | " |
| 921 | << "openssl dgst -sha256 -binary | openssl base64"; |
| 922 | } else { |
| 923 | LOG(INFO) << "To get the checksum of a rootfs partition on a " |
| 924 | << "booted machine, run this command (change /dev/sda3 " |
| 925 | << "as needed): dd if=/dev/sda3 bs=1M count=$(( " |
| 926 | << "$(dumpe2fs /dev/sda3 2>/dev/null | grep 'Block count' " |
| 927 | << "| sed 's/[^0-9]*//') / 256 )) | " |
| 928 | << "openssl dgst -sha256 -binary | openssl base64"; |
| 929 | } |
| 930 | LOG(INFO) << "To get the checksum of partitions in a bin file, " |
| 931 | << "run: .../src/scripts/sha256_partitions.sh .../file.bin"; |
| 932 | } |
| 933 | |
| 934 | string StringForHashBytes(const void* bytes, size_t size) { |
| 935 | string ret; |
| 936 | if (!OmahaHashCalculator::Base64Encode(bytes, size, &ret)) { |
| 937 | ret = "<unknown>"; |
| 938 | } |
| 939 | return ret; |
| 940 | } |
| 941 | } // namespace |
| 942 | |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 943 | bool DeltaPerformer::VerifySourcePartitions() { |
| 944 | LOG(INFO) << "Verifying source partitions."; |
| 945 | CHECK(manifest_valid_); |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 946 | CHECK(install_plan_); |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 947 | if (manifest_.has_old_kernel_info()) { |
| 948 | const PartitionInfo& info = manifest_.old_kernel_info(); |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 949 | bool valid = |
| 950 | !install_plan_->kernel_hash.empty() && |
| 951 | install_plan_->kernel_hash.size() == info.hash().size() && |
| 952 | memcmp(install_plan_->kernel_hash.data(), |
Andrew de los Reyes | 100bb7d | 2011-08-09 17:35:07 -0700 | [diff] [blame] | 953 | info.hash().data(), |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 954 | install_plan_->kernel_hash.size()) == 0; |
Andrew de los Reyes | 100bb7d | 2011-08-09 17:35:07 -0700 | [diff] [blame] | 955 | if (!valid) { |
| 956 | LogVerifyError(true, |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 957 | StringForHashBytes(install_plan_->kernel_hash.data(), |
| 958 | install_plan_->kernel_hash.size()), |
Andrew de los Reyes | 100bb7d | 2011-08-09 17:35:07 -0700 | [diff] [blame] | 959 | StringForHashBytes(info.hash().data(), |
| 960 | info.hash().size())); |
| 961 | } |
| 962 | TEST_AND_RETURN_FALSE(valid); |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 963 | } |
| 964 | if (manifest_.has_old_rootfs_info()) { |
| 965 | const PartitionInfo& info = manifest_.old_rootfs_info(); |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 966 | bool valid = |
| 967 | !install_plan_->rootfs_hash.empty() && |
| 968 | install_plan_->rootfs_hash.size() == info.hash().size() && |
| 969 | memcmp(install_plan_->rootfs_hash.data(), |
Andrew de los Reyes | 100bb7d | 2011-08-09 17:35:07 -0700 | [diff] [blame] | 970 | info.hash().data(), |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 971 | install_plan_->rootfs_hash.size()) == 0; |
Andrew de los Reyes | 100bb7d | 2011-08-09 17:35:07 -0700 | [diff] [blame] | 972 | if (!valid) { |
| 973 | LogVerifyError(false, |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 974 | StringForHashBytes(install_plan_->kernel_hash.data(), |
| 975 | install_plan_->kernel_hash.size()), |
Andrew de los Reyes | 100bb7d | 2011-08-09 17:35:07 -0700 | [diff] [blame] | 976 | StringForHashBytes(info.hash().data(), |
| 977 | info.hash().size())); |
| 978 | } |
| 979 | TEST_AND_RETURN_FALSE(valid); |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 980 | } |
| 981 | return true; |
| 982 | } |
| 983 | |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 984 | void DeltaPerformer::DiscardBufferHeadBytes(size_t count) { |
| 985 | hash_calculator_.Update(&buffer_[0], count); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 986 | buffer_.erase(buffer_.begin(), buffer_.begin() + count); |
| 987 | } |
| 988 | |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 989 | bool DeltaPerformer::CanResumeUpdate(PrefsInterface* prefs, |
| 990 | string update_check_response_hash) { |
| 991 | int64_t next_operation = kUpdateStateOperationInvalid; |
| 992 | TEST_AND_RETURN_FALSE(prefs->GetInt64(kPrefsUpdateStateNextOperation, |
| 993 | &next_operation) && |
| 994 | next_operation != kUpdateStateOperationInvalid && |
| 995 | next_operation > 0); |
| 996 | |
| 997 | string interrupted_hash; |
| 998 | TEST_AND_RETURN_FALSE(prefs->GetString(kPrefsUpdateCheckResponseHash, |
| 999 | &interrupted_hash) && |
| 1000 | !interrupted_hash.empty() && |
| 1001 | interrupted_hash == update_check_response_hash); |
| 1002 | |
Darin Petkov | 6142614 | 2010-10-08 11:04:55 -0700 | [diff] [blame] | 1003 | int64_t resumed_update_failures; |
| 1004 | TEST_AND_RETURN_FALSE(!prefs->GetInt64(kPrefsResumedUpdateFailures, |
| 1005 | &resumed_update_failures) || |
| 1006 | resumed_update_failures <= kMaxResumedUpdateFailures); |
| 1007 | |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1008 | // Sanity check the rest. |
| 1009 | int64_t next_data_offset = -1; |
| 1010 | TEST_AND_RETURN_FALSE(prefs->GetInt64(kPrefsUpdateStateNextDataOffset, |
| 1011 | &next_data_offset) && |
| 1012 | next_data_offset >= 0); |
| 1013 | |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 1014 | string sha256_context; |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1015 | TEST_AND_RETURN_FALSE( |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 1016 | prefs->GetString(kPrefsUpdateStateSHA256Context, &sha256_context) && |
| 1017 | !sha256_context.empty()); |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1018 | |
| 1019 | int64_t manifest_metadata_size = 0; |
| 1020 | TEST_AND_RETURN_FALSE(prefs->GetInt64(kPrefsManifestMetadataSize, |
| 1021 | &manifest_metadata_size) && |
| 1022 | manifest_metadata_size > 0); |
| 1023 | |
| 1024 | return true; |
| 1025 | } |
| 1026 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1027 | bool DeltaPerformer::ResetUpdateProgress(PrefsInterface* prefs, bool quick) { |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1028 | TEST_AND_RETURN_FALSE(prefs->SetInt64(kPrefsUpdateStateNextOperation, |
| 1029 | kUpdateStateOperationInvalid)); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1030 | if (!quick) { |
| 1031 | prefs->SetString(kPrefsUpdateCheckResponseHash, ""); |
| 1032 | prefs->SetInt64(kPrefsUpdateStateNextDataOffset, -1); |
| 1033 | prefs->SetString(kPrefsUpdateStateSHA256Context, ""); |
| 1034 | prefs->SetString(kPrefsUpdateStateSignedSHA256Context, ""); |
Darin Petkov | 4f0a07b | 2011-05-25 16:47:20 -0700 | [diff] [blame] | 1035 | prefs->SetString(kPrefsUpdateStateSignatureBlob, ""); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1036 | prefs->SetInt64(kPrefsManifestMetadataSize, -1); |
Darin Petkov | 6142614 | 2010-10-08 11:04:55 -0700 | [diff] [blame] | 1037 | prefs->SetInt64(kPrefsResumedUpdateFailures, 0); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1038 | } |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 1039 | return true; |
| 1040 | } |
| 1041 | |
| 1042 | bool DeltaPerformer::CheckpointUpdateProgress() { |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 1043 | Terminator::set_exit_blocked(true); |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1044 | if (last_updated_buffer_offset_ != buffer_offset_) { |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 1045 | // Resets the progress in case we die in the middle of the state update. |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1046 | ResetUpdateProgress(prefs_, true); |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1047 | TEST_AND_RETURN_FALSE( |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 1048 | prefs_->SetString(kPrefsUpdateStateSHA256Context, |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 1049 | hash_calculator_.GetContext())); |
| 1050 | TEST_AND_RETURN_FALSE(prefs_->SetInt64(kPrefsUpdateStateNextDataOffset, |
| 1051 | buffer_offset_)); |
| 1052 | last_updated_buffer_offset_ = buffer_offset_; |
| 1053 | } |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 1054 | TEST_AND_RETURN_FALSE(prefs_->SetInt64(kPrefsUpdateStateNextOperation, |
| 1055 | next_operation_num_)); |
| 1056 | return true; |
| 1057 | } |
| 1058 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1059 | bool DeltaPerformer::PrimeUpdateState() { |
| 1060 | CHECK(manifest_valid_); |
| 1061 | block_size_ = manifest_.block_size(); |
| 1062 | |
| 1063 | int64_t next_operation = kUpdateStateOperationInvalid; |
| 1064 | if (!prefs_->GetInt64(kPrefsUpdateStateNextOperation, &next_operation) || |
| 1065 | next_operation == kUpdateStateOperationInvalid || |
| 1066 | next_operation <= 0) { |
| 1067 | // Initiating a new update, no more state needs to be initialized. |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 1068 | TEST_AND_RETURN_FALSE(VerifySourcePartitions()); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1069 | return true; |
| 1070 | } |
| 1071 | next_operation_num_ = next_operation; |
| 1072 | |
| 1073 | // Resuming an update -- load the rest of the update state. |
| 1074 | int64_t next_data_offset = -1; |
| 1075 | TEST_AND_RETURN_FALSE(prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, |
| 1076 | &next_data_offset) && |
| 1077 | next_data_offset >= 0); |
| 1078 | buffer_offset_ = next_data_offset; |
| 1079 | |
Darin Petkov | 4f0a07b | 2011-05-25 16:47:20 -0700 | [diff] [blame] | 1080 | // The signed hash context and the signature blob may be empty if the |
| 1081 | // interrupted update didn't reach the signature. |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1082 | prefs_->GetString(kPrefsUpdateStateSignedSHA256Context, |
| 1083 | &signed_hash_context_); |
Darin Petkov | 4f0a07b | 2011-05-25 16:47:20 -0700 | [diff] [blame] | 1084 | string signature_blob; |
| 1085 | if (prefs_->GetString(kPrefsUpdateStateSignatureBlob, &signature_blob)) { |
| 1086 | signatures_message_data_.assign(signature_blob.begin(), |
| 1087 | signature_blob.end()); |
| 1088 | } |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1089 | |
| 1090 | string hash_context; |
| 1091 | TEST_AND_RETURN_FALSE(prefs_->GetString(kPrefsUpdateStateSHA256Context, |
| 1092 | &hash_context) && |
| 1093 | hash_calculator_.SetContext(hash_context)); |
| 1094 | |
| 1095 | int64_t manifest_metadata_size = 0; |
| 1096 | TEST_AND_RETURN_FALSE(prefs_->GetInt64(kPrefsManifestMetadataSize, |
| 1097 | &manifest_metadata_size) && |
| 1098 | manifest_metadata_size > 0); |
| 1099 | manifest_metadata_size_ = manifest_metadata_size; |
| 1100 | |
Darin Petkov | 6142614 | 2010-10-08 11:04:55 -0700 | [diff] [blame] | 1101 | // Speculatively count the resume as a failure. |
| 1102 | int64_t resumed_update_failures; |
| 1103 | if (prefs_->GetInt64(kPrefsResumedUpdateFailures, &resumed_update_failures)) { |
| 1104 | resumed_update_failures++; |
| 1105 | } else { |
| 1106 | resumed_update_failures = 1; |
| 1107 | } |
| 1108 | prefs_->SetInt64(kPrefsResumedUpdateFailures, resumed_update_failures); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 1109 | return true; |
| 1110 | } |
| 1111 | |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame^] | 1112 | void DeltaPerformer::SendUMAStat(ActionExitCode code) { |
| 1113 | if (system_state_) { |
| 1114 | utils::SendErrorCodeToUMA(system_state_->metrics_lib(), code); |
| 1115 | } |
| 1116 | } |
| 1117 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 1118 | } // namespace chromeos_update_engine |