| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/component_updater/component_patcher_operation.h" | 5 #include "chrome/browser/component_updater/component_patcher_operation.h" |
| 6 | 6 |
| 7 #include <string> | |
| 8 #include <vector> | 7 #include <vector> |
| 9 | 8 |
| 10 #include "base/bind.h" | 9 #include "base/bind.h" |
| 11 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 12 #include "base/files/memory_mapped_file.h" | 11 #include "base/files/memory_mapped_file.h" |
| 13 #include "base/json/json_file_value_serializer.h" | 12 #include "base/location.h" |
| 14 #include "base/path_service.h" | |
| 15 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 16 #include "chrome/browser/component_updater/component_patcher.h" | 14 #include "chrome/browser/component_updater/component_patcher.h" |
| 17 #include "chrome/browser/component_updater/component_updater_service.h" | 15 #include "chrome/browser/component_updater/component_updater_service.h" |
| 18 #include "chrome/common/chrome_utility_messages.h" | |
| 19 #include "content/public/browser/browser_thread.h" | |
| 20 #include "content/public/browser/utility_process_host.h" | |
| 21 #include "courgette/courgette.h" | 16 #include "courgette/courgette.h" |
| 22 #include "courgette/third_party/bsdiff.h" | 17 #include "courgette/third_party/bsdiff.h" |
| 23 #include "crypto/secure_hash.h" | 18 #include "crypto/secure_hash.h" |
| 24 #include "crypto/sha2.h" | 19 #include "crypto/sha2.h" |
| 25 #include "crypto/signature_verifier.h" | 20 #include "crypto/signature_verifier.h" |
| 26 #include "extensions/common/crx_file.h" | 21 #include "extensions/common/crx_file.h" |
| 27 #include "ipc/ipc_message_macros.h" | |
| 28 | 22 |
| 29 using crypto::SecureHash; | 23 using crypto::SecureHash; |
| 30 | 24 |
| 31 namespace component_updater { | 25 namespace component_updater { |
| 32 | 26 |
| 33 namespace { | 27 namespace { |
| 34 | 28 |
| 35 const char kInput[] = "input"; | |
| 36 const char kOp[] = "op"; | |
| 37 const char kOutput[] = "output"; | 29 const char kOutput[] = "output"; |
| 38 const char kPatch[] = "patch"; | |
| 39 const char kSha256[] = "sha256"; | 30 const char kSha256[] = "sha256"; |
| 40 | 31 |
| 41 // The integer offset disambiguates between overlapping error ranges. | 32 class InProcessDeltaUpdateOpFactory : public DeltaUpdateOpFactory { |
| 42 const int kCourgetteErrorOffset = 300; | |
| 43 const int kBsdiffErrorOffset = 600; | |
| 44 | |
| 45 class CourgetteTraits : public DeltaUpdateOpPatchStrategy { | |
| 46 public: | 33 public: |
| 47 virtual int GetErrorOffset() const OVERRIDE; | 34 virtual DeltaUpdateOp* CreateDeltaUpdateOp( |
| 48 virtual int GetSuccessCode() const OVERRIDE; | 35 const std::string& operation) const OVERRIDE { |
| 49 virtual scoped_ptr<IPC::Message> GetPatchMessage( | 36 if (operation == "copy") { |
| 50 base::FilePath input_abs_path, | 37 return new DeltaUpdateOpCopy(); |
| 51 base::FilePath patch_abs_path, | 38 } else if (operation == "create") { |
| 52 base::FilePath output_abs_path) OVERRIDE; | 39 return new DeltaUpdateOpCreate(); |
| 53 virtual int Patch(base::FilePath input_abs_path, | 40 } else if (operation == kBsdiff || operation == kCourgette) { |
| 54 base::FilePath patch_abs_path, | 41 return new DeltaUpdateOpPatchInProcess(operation); |
| 55 base::FilePath output_abs_path) OVERRIDE; | 42 } |
| 43 return NULL; |
| 44 } |
| 56 }; | 45 }; |
| 57 | 46 |
| 58 int CourgetteTraits::GetErrorOffset() const { | |
| 59 return kCourgetteErrorOffset; | |
| 60 } | |
| 61 | |
| 62 int CourgetteTraits::GetSuccessCode() const { | |
| 63 return courgette::C_OK; | |
| 64 } | |
| 65 | |
| 66 scoped_ptr<IPC::Message> CourgetteTraits::GetPatchMessage( | |
| 67 base::FilePath input_abs_path, | |
| 68 base::FilePath patch_abs_path, | |
| 69 base::FilePath output_abs_path) { | |
| 70 return scoped_ptr<IPC::Message>( | |
| 71 new ChromeUtilityMsg_PatchFileCourgette(input_abs_path, | |
| 72 patch_abs_path, | |
| 73 output_abs_path)); | |
| 74 } | |
| 75 | |
| 76 int CourgetteTraits::Patch(base::FilePath input_abs_path, | |
| 77 base::FilePath patch_abs_path, | |
| 78 base::FilePath output_abs_path) { | |
| 79 return courgette::ApplyEnsemblePatch(input_abs_path.value().c_str(), | |
| 80 patch_abs_path.value().c_str(), | |
| 81 output_abs_path.value().c_str()); | |
| 82 } | |
| 83 | |
| 84 class BsdiffTraits : public DeltaUpdateOpPatchStrategy { | |
| 85 public: | |
| 86 virtual int GetErrorOffset() const OVERRIDE; | |
| 87 virtual int GetSuccessCode() const OVERRIDE; | |
| 88 virtual scoped_ptr<IPC::Message> GetPatchMessage( | |
| 89 base::FilePath input_abs_path, | |
| 90 base::FilePath patch_abs_path, | |
| 91 base::FilePath output_abs_path) OVERRIDE; | |
| 92 virtual int Patch(base::FilePath input_abs_path, | |
| 93 base::FilePath patch_abs_path, | |
| 94 base::FilePath output_abs_path) OVERRIDE; | |
| 95 }; | |
| 96 | |
| 97 int BsdiffTraits::GetErrorOffset() const { | |
| 98 return kBsdiffErrorOffset; | |
| 99 } | |
| 100 | |
| 101 int BsdiffTraits::GetSuccessCode() const { | |
| 102 return courgette::OK; | |
| 103 } | |
| 104 | |
| 105 scoped_ptr<IPC::Message> BsdiffTraits::GetPatchMessage( | |
| 106 base::FilePath input_abs_path, | |
| 107 base::FilePath patch_abs_path, | |
| 108 base::FilePath output_abs_path) { | |
| 109 return scoped_ptr<IPC::Message>( | |
| 110 new ChromeUtilityMsg_PatchFileBsdiff(input_abs_path, | |
| 111 patch_abs_path, | |
| 112 output_abs_path)); | |
| 113 } | |
| 114 | |
| 115 int BsdiffTraits::Patch(base::FilePath input_abs_path, | |
| 116 base::FilePath patch_abs_path, | |
| 117 base::FilePath output_abs_path) { | |
| 118 return courgette::ApplyBinaryPatch(input_abs_path, | |
| 119 patch_abs_path, | |
| 120 output_abs_path); | |
| 121 } | |
| 122 | |
| 123 } // namespace | 47 } // namespace |
| 124 | 48 |
| 125 DeltaUpdateOpPatchStrategy::~DeltaUpdateOpPatchStrategy() { | 49 DeltaUpdateOpFactory::~DeltaUpdateOpFactory() { |
| 126 } | 50 } |
| 127 | 51 |
| 128 DeltaUpdateOp* CreateDeltaUpdateOp(const std::string& operation) { | 52 DeltaUpdateOpFactory* CreateInProcessDeltaUpdateOpFactory() { |
| 129 if (operation == "copy") { | 53 return new InProcessDeltaUpdateOpFactory(); |
| 130 return new DeltaUpdateOpCopy(); | |
| 131 } else if (operation == "create") { | |
| 132 return new DeltaUpdateOpCreate(); | |
| 133 } else if (operation == "bsdiff") { | |
| 134 scoped_ptr<DeltaUpdateOpPatchStrategy> strategy(new BsdiffTraits()); | |
| 135 return new DeltaUpdateOpPatch(strategy.Pass()); | |
| 136 } else if (operation == "courgette") { | |
| 137 scoped_ptr<DeltaUpdateOpPatchStrategy> strategy(new CourgetteTraits()); | |
| 138 return new DeltaUpdateOpPatch(strategy.Pass()); | |
| 139 } | |
| 140 return NULL; | |
| 141 } | 54 } |
| 142 | 55 |
| 143 DeltaUpdateOp* CreateDeltaUpdateOp(const base::DictionaryValue& command) { | 56 DeltaUpdateOp::DeltaUpdateOp() { |
| 144 std::string operation; | |
| 145 if (!command.GetString(kOp, &operation)) | |
| 146 return NULL; | |
| 147 return CreateDeltaUpdateOp(operation); | |
| 148 } | |
| 149 | |
| 150 DeltaUpdateOp::DeltaUpdateOp() : in_process_(false) { | |
| 151 } | 57 } |
| 152 | 58 |
| 153 DeltaUpdateOp::~DeltaUpdateOp() { | 59 DeltaUpdateOp::~DeltaUpdateOp() { |
| 154 } | 60 } |
| 155 | 61 |
| 156 void DeltaUpdateOp::Run(const base::DictionaryValue* command_args, | 62 void DeltaUpdateOp::Run(const base::DictionaryValue* command_args, |
| 157 const base::FilePath& input_dir, | 63 const base::FilePath& input_dir, |
| 158 const base::FilePath& unpack_dir, | 64 const base::FilePath& unpack_dir, |
| 159 ComponentInstaller* installer, | 65 ComponentInstaller* installer, |
| 160 bool in_process, | |
| 161 const ComponentUnpacker::Callback& callback, | 66 const ComponentUnpacker::Callback& callback, |
| 162 scoped_refptr<base::SequencedTaskRunner> task_runner) { | 67 scoped_refptr<base::SequencedTaskRunner> task_runner) { |
| 163 callback_ = callback; | 68 callback_ = callback; |
| 164 in_process_ = in_process; | |
| 165 task_runner_ = task_runner; | 69 task_runner_ = task_runner; |
| 166 std::string output_rel_path; | 70 std::string output_rel_path; |
| 167 if (!command_args->GetString(kOutput, &output_rel_path) || | 71 if (!command_args->GetString(kOutput, &output_rel_path) || |
| 168 !command_args->GetString(kSha256, &output_sha256_)) { | 72 !command_args->GetString(kSha256, &output_sha256_)) { |
| 169 DoneRunning(ComponentUnpacker::kDeltaBadCommands, 0); | 73 DoneRunning(ComponentUnpacker::kDeltaBadCommands, 0); |
| 170 return; | 74 return; |
| 171 } | 75 } |
| 172 | 76 |
| 173 output_abs_path_ = | 77 output_abs_path_ = |
| 174 unpack_dir.Append(base::FilePath::FromUTF8Unsafe(output_rel_path)); | 78 unpack_dir.Append(base::FilePath::FromUTF8Unsafe(output_rel_path)); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 uint8 actual_hash[crypto::kSHA256Length] = {0}; | 119 uint8 actual_hash[crypto::kSHA256Length] = {0}; |
| 216 const scoped_ptr<SecureHash> hasher(SecureHash::Create(SecureHash::SHA256)); | 120 const scoped_ptr<SecureHash> hasher(SecureHash::Create(SecureHash::SHA256)); |
| 217 hasher->Update(output_file_mmapped.data(), output_file_mmapped.length()); | 121 hasher->Update(output_file_mmapped.data(), output_file_mmapped.length()); |
| 218 hasher->Finish(actual_hash, sizeof(actual_hash)); | 122 hasher->Finish(actual_hash, sizeof(actual_hash)); |
| 219 if (memcmp(actual_hash, &expected_hash[0], sizeof(actual_hash))) | 123 if (memcmp(actual_hash, &expected_hash[0], sizeof(actual_hash))) |
| 220 return ComponentUnpacker::kDeltaVerificationFailure; | 124 return ComponentUnpacker::kDeltaVerificationFailure; |
| 221 | 125 |
| 222 return ComponentUnpacker::kNone; | 126 return ComponentUnpacker::kNone; |
| 223 } | 127 } |
| 224 | 128 |
| 225 bool DeltaUpdateOp::InProcess() { | |
| 226 return in_process_; | |
| 227 } | |
| 228 | |
| 229 scoped_refptr<base::SequencedTaskRunner> DeltaUpdateOp::GetTaskRunner() { | 129 scoped_refptr<base::SequencedTaskRunner> DeltaUpdateOp::GetTaskRunner() { |
| 230 return task_runner_; | 130 return task_runner_; |
| 231 } | 131 } |
| 232 | 132 |
| 233 DeltaUpdateOpCopy::DeltaUpdateOpCopy() { | 133 DeltaUpdateOpCopy::DeltaUpdateOpCopy() { |
| 234 } | 134 } |
| 235 | 135 |
| 236 DeltaUpdateOpCopy::~DeltaUpdateOpCopy() { | 136 DeltaUpdateOpCopy::~DeltaUpdateOpCopy() { |
| 237 } | 137 } |
| 238 | 138 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 return ComponentUnpacker::kNone; | 177 return ComponentUnpacker::kNone; |
| 278 } | 178 } |
| 279 | 179 |
| 280 void DeltaUpdateOpCreate::DoRun(const ComponentUnpacker::Callback& callback) { | 180 void DeltaUpdateOpCreate::DoRun(const ComponentUnpacker::Callback& callback) { |
| 281 if (!base::Move(patch_abs_path_, output_abs_path_)) | 181 if (!base::Move(patch_abs_path_, output_abs_path_)) |
| 282 callback.Run(ComponentUnpacker::kDeltaOperationFailure, 0); | 182 callback.Run(ComponentUnpacker::kDeltaOperationFailure, 0); |
| 283 else | 183 else |
| 284 callback.Run(ComponentUnpacker::kNone, 0); | 184 callback.Run(ComponentUnpacker::kNone, 0); |
| 285 } | 185 } |
| 286 | 186 |
| 287 DeltaUpdateOpPatchHost::DeltaUpdateOpPatchHost( | 187 DeltaUpdateOpPatchInProcess::DeltaUpdateOpPatchInProcess( |
| 288 scoped_refptr<DeltaUpdateOpPatch> patcher, | 188 const std::string& operation) |
| 289 scoped_refptr<base::SequencedTaskRunner> task_runner) | 189 : operation_(operation) { |
| 290 : patcher_(patcher), task_runner_(task_runner) { | 190 DCHECK(operation == kBsdiff || operation == kCourgette); |
| 291 } | 191 } |
| 292 | 192 |
| 293 DeltaUpdateOpPatchHost::~DeltaUpdateOpPatchHost() { | 193 DeltaUpdateOpPatchInProcess::~DeltaUpdateOpPatchInProcess() { |
| 294 } | 194 } |
| 295 | 195 |
| 296 void DeltaUpdateOpPatchHost::StartProcess(scoped_ptr<IPC::Message> message) { | 196 ComponentUnpacker::Error DeltaUpdateOpPatchInProcess::DoParseArguments( |
| 297 // The DeltaUpdateOpPatchHost is not responsible for deleting the | |
| 298 // UtilityProcessHost object. | |
| 299 content::UtilityProcessHost* host = content::UtilityProcessHost::Create( | |
| 300 this, base::MessageLoopProxy::current().get()); | |
| 301 host->DisableSandbox(); | |
| 302 host->Send(message.release()); | |
| 303 } | |
| 304 | |
| 305 bool DeltaUpdateOpPatchHost::OnMessageReceived(const IPC::Message& message) { | |
| 306 bool handled = true; | |
| 307 IPC_BEGIN_MESSAGE_MAP(DeltaUpdateOpPatchHost, message) | |
| 308 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_PatchFile_Succeeded, | |
| 309 OnPatchSucceeded) | |
| 310 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_PatchFile_Failed, | |
| 311 OnPatchFailed) | |
| 312 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 313 IPC_END_MESSAGE_MAP() | |
| 314 return handled; | |
| 315 } | |
| 316 | |
| 317 void DeltaUpdateOpPatchHost::OnPatchSucceeded() { | |
| 318 task_runner_->PostTask(FROM_HERE, | |
| 319 base::Bind(&DeltaUpdateOpPatch::DonePatching, | |
| 320 patcher_, | |
| 321 ComponentUnpacker::kNone, | |
| 322 0)); | |
| 323 task_runner_ = NULL; | |
| 324 patcher_ = NULL; | |
| 325 } | |
| 326 | |
| 327 void DeltaUpdateOpPatchHost::OnPatchFailed(int error_code) { | |
| 328 task_runner_->PostTask(FROM_HERE, | |
| 329 base::Bind(&DeltaUpdateOpPatch::DonePatching, | |
| 330 patcher_, | |
| 331 ComponentUnpacker::kDeltaOperationFailure, | |
| 332 error_code)); | |
| 333 task_runner_ = NULL; | |
| 334 patcher_ = NULL; | |
| 335 } | |
| 336 | |
| 337 void DeltaUpdateOpPatchHost::OnProcessCrashed(int exit_code) { | |
| 338 task_runner_->PostTask( | |
| 339 FROM_HERE, | |
| 340 base::Bind(&DeltaUpdateOpPatch::DonePatching, | |
| 341 patcher_, | |
| 342 ComponentUnpacker::kDeltaPatchProcessFailure, | |
| 343 exit_code)); | |
| 344 task_runner_ = NULL; | |
| 345 patcher_ = NULL; | |
| 346 } | |
| 347 | |
| 348 DeltaUpdateOpPatch::DeltaUpdateOpPatch( | |
| 349 scoped_ptr<DeltaUpdateOpPatchStrategy> strategy) { | |
| 350 strategy_ = strategy.Pass(); | |
| 351 } | |
| 352 | |
| 353 DeltaUpdateOpPatch::~DeltaUpdateOpPatch() { | |
| 354 } | |
| 355 | |
| 356 ComponentUnpacker::Error DeltaUpdateOpPatch::DoParseArguments( | |
| 357 const base::DictionaryValue* command_args, | 197 const base::DictionaryValue* command_args, |
| 358 const base::FilePath& input_dir, | 198 const base::FilePath& input_dir, |
| 359 ComponentInstaller* installer) { | 199 ComponentInstaller* installer) { |
| 360 std::string patch_rel_path; | 200 std::string patch_rel_path; |
| 361 std::string input_rel_path; | 201 std::string input_rel_path; |
| 362 if (!command_args->GetString(kPatch, &patch_rel_path) || | 202 if (!command_args->GetString(kPatch, &patch_rel_path) || |
| 363 !command_args->GetString(kInput, &input_rel_path)) | 203 !command_args->GetString(kInput, &input_rel_path)) |
| 364 return ComponentUnpacker::kDeltaBadCommands; | 204 return ComponentUnpacker::kDeltaBadCommands; |
| 365 | 205 |
| 366 if (!installer->GetInstalledFile(input_rel_path, &input_abs_path_)) | 206 if (!installer->GetInstalledFile(input_rel_path, &input_abs_path_)) |
| 367 return ComponentUnpacker::kDeltaMissingExistingFile; | 207 return ComponentUnpacker::kDeltaMissingExistingFile; |
| 368 | 208 |
| 369 patch_abs_path_ = | 209 patch_abs_path_ = |
| 370 input_dir.Append(base::FilePath::FromUTF8Unsafe(patch_rel_path)); | 210 input_dir.Append(base::FilePath::FromUTF8Unsafe(patch_rel_path)); |
| 371 | 211 |
| 372 return ComponentUnpacker::kNone; | 212 return ComponentUnpacker::kNone; |
| 373 } | 213 } |
| 374 | 214 |
| 375 void DeltaUpdateOpPatch::DoRun(const ComponentUnpacker::Callback& callback) { | 215 void DeltaUpdateOpPatchInProcess::DoRun( |
| 376 callback_ = callback; | 216 const ComponentUnpacker::Callback& callback) { |
| 377 if (!InProcess()) { | 217 if (operation_ == kBsdiff) { |
| 378 host_ = new DeltaUpdateOpPatchHost(scoped_refptr<DeltaUpdateOpPatch>(this), | 218 int result = courgette::ApplyBinaryPatch( |
| 379 GetTaskRunner()); | 219 input_abs_path_, patch_abs_path_, output_abs_path_); |
| 380 content::BrowserThread::PostTask( | 220 if (result == courgette::OK) { |
| 381 content::BrowserThread::IO, | 221 callback.Run(ComponentUnpacker::kNone, 0); |
| 382 FROM_HERE, | 222 } else { |
| 383 base::Bind(&DeltaUpdateOpPatchHost::StartProcess, | 223 callback.Run(ComponentUnpacker::kDeltaOperationFailure, |
| 384 host_, | 224 result + kBsdiffErrorOffset); |
| 385 base::Passed(strategy_->GetPatchMessage(input_abs_path_, | 225 } |
| 386 patch_abs_path_, | 226 } else if (operation_ == kCourgette) { |
| 387 output_abs_path_)))); | 227 int result = |
| 388 return; | 228 courgette::ApplyEnsemblePatch(input_abs_path_.value().c_str(), |
| 229 patch_abs_path_.value().c_str(), |
| 230 output_abs_path_.value().c_str()); |
| 231 if (result == courgette::C_OK) { |
| 232 callback.Run(ComponentUnpacker::kNone, 0); |
| 233 } else { |
| 234 callback.Run(ComponentUnpacker::kDeltaOperationFailure, |
| 235 result + kCourgetteErrorOffset); |
| 236 } |
| 237 } else { |
| 238 NOTREACHED(); |
| 389 } | 239 } |
| 390 const int result = strategy_->Patch(input_abs_path_, | |
| 391 patch_abs_path_, | |
| 392 output_abs_path_); | |
| 393 if (result == strategy_->GetSuccessCode()) | |
| 394 DonePatching(ComponentUnpacker::kNone, 0); | |
| 395 else | |
| 396 DonePatching(ComponentUnpacker::kDeltaOperationFailure, result); | |
| 397 } | |
| 398 | |
| 399 void DeltaUpdateOpPatch::DonePatching(ComponentUnpacker::Error error, | |
| 400 int error_code) { | |
| 401 host_ = NULL; | |
| 402 if (error != ComponentUnpacker::kNone) { | |
| 403 error_code += strategy_->GetErrorOffset(); | |
| 404 } | |
| 405 callback_.Run(error, error_code); | |
| 406 // The callback is no longer needed - it is best to release it in case it | |
| 407 // contains a reference to this object. | |
| 408 callback_.Reset(); | |
| 409 } | 240 } |
| 410 | 241 |
| 411 } // namespace component_updater | 242 } // namespace component_updater |
| OLD | NEW |