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
3 // found in the LICENSE file.
5 #include "chrome/browser/component_updater/component_patcher_operation.h"
10 #include "base/bind.h"
11 #include "base/file_util.h"
12 #include "base/files/memory_mapped_file.h"
13 #include "base/json/json_file_value_serializer.h"
14 #include "base/path_service.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "chrome/browser/component_updater/component_patcher.h"
17 #include "chrome/browser/component_updater/component_updater_service.h"
18 #include "chrome/common/chrome_utility_messages.h"
19 #include "chrome/common/extensions/extension_constants.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "content/public/browser/utility_process_host.h"
22 #include "courgette/courgette.h"
23 #include "courgette/third_party/bsdiff.h"
24 #include "crypto/secure_hash.h"
25 #include "crypto/sha2.h"
26 #include "crypto/signature_verifier.h"
27 #include "extensions/common/crx_file.h"
28 #include "ipc/ipc_message_macros.h"
30 using crypto::SecureHash;
32 namespace component_updater {
36 const char kInput[] = "input";
37 const char kOp[] = "op";
38 const char kOutput[] = "output";
39 const char kPatch[] = "patch";
40 const char kSha256[] = "sha256";
42 // The integer offset disambiguates between overlapping error ranges.
43 const int kCourgetteErrorOffset = 300;
44 const int kBsdiffErrorOffset = 600;
46 class CourgetteTraits : public DeltaUpdateOpPatchStrategy {
48 virtual int GetErrorOffset() const OVERRIDE;
49 virtual int GetSuccessCode() const OVERRIDE;
50 virtual scoped_ptr<IPC::Message> GetPatchMessage(
51 base::FilePath input_abs_path,
52 base::FilePath patch_abs_path,
53 base::FilePath output_abs_path) OVERRIDE;
54 virtual int Patch(base::FilePath input_abs_path,
55 base::FilePath patch_abs_path,
56 base::FilePath output_abs_path) OVERRIDE;
59 int CourgetteTraits::GetErrorOffset() const {
60 return kCourgetteErrorOffset;
63 int CourgetteTraits::GetSuccessCode() const {
64 return courgette::C_OK;
67 scoped_ptr<IPC::Message> CourgetteTraits::GetPatchMessage(
68 base::FilePath input_abs_path,
69 base::FilePath patch_abs_path,
70 base::FilePath output_abs_path) {
71 return scoped_ptr<IPC::Message>(
72 new ChromeUtilityMsg_PatchFileCourgette(input_abs_path,
77 int CourgetteTraits::Patch(base::FilePath input_abs_path,
78 base::FilePath patch_abs_path,
79 base::FilePath output_abs_path) {
80 return courgette::ApplyEnsemblePatch(input_abs_path.value().c_str(),
81 patch_abs_path.value().c_str(),
82 output_abs_path.value().c_str());
85 class BsdiffTraits : public DeltaUpdateOpPatchStrategy {
87 virtual int GetErrorOffset() const OVERRIDE;
88 virtual int GetSuccessCode() const OVERRIDE;
89 virtual scoped_ptr<IPC::Message> GetPatchMessage(
90 base::FilePath input_abs_path,
91 base::FilePath patch_abs_path,
92 base::FilePath output_abs_path) OVERRIDE;
93 virtual int Patch(base::FilePath input_abs_path,
94 base::FilePath patch_abs_path,
95 base::FilePath output_abs_path) OVERRIDE;
98 int BsdiffTraits::GetErrorOffset() const {
99 return kBsdiffErrorOffset;
102 int BsdiffTraits::GetSuccessCode() const {
103 return courgette::OK;
106 scoped_ptr<IPC::Message> BsdiffTraits::GetPatchMessage(
107 base::FilePath input_abs_path,
108 base::FilePath patch_abs_path,
109 base::FilePath output_abs_path) {
110 return scoped_ptr<IPC::Message>(
111 new ChromeUtilityMsg_PatchFileBsdiff(input_abs_path,
116 int BsdiffTraits::Patch(base::FilePath input_abs_path,
117 base::FilePath patch_abs_path,
118 base::FilePath output_abs_path) {
119 return courgette::ApplyBinaryPatch(input_abs_path,
126 DeltaUpdateOpPatchStrategy::~DeltaUpdateOpPatchStrategy() {
129 DeltaUpdateOp* CreateDeltaUpdateOp(const std::string& operation) {
130 if (operation == "copy") {
131 return new DeltaUpdateOpCopy();
132 } else if (operation == "create") {
133 return new DeltaUpdateOpCreate();
134 } else if (operation == "bsdiff") {
135 scoped_ptr<DeltaUpdateOpPatchStrategy> strategy(new BsdiffTraits());
136 return new DeltaUpdateOpPatch(strategy.Pass());
137 } else if (operation == "courgette") {
138 scoped_ptr<DeltaUpdateOpPatchStrategy> strategy(new CourgetteTraits());
139 return new DeltaUpdateOpPatch(strategy.Pass());
144 DeltaUpdateOp* CreateDeltaUpdateOp(const base::DictionaryValue& command) {
145 std::string operation;
146 if (!command.GetString(kOp, &operation))
148 return CreateDeltaUpdateOp(operation);
151 DeltaUpdateOp::DeltaUpdateOp() : in_process_(false) {
154 DeltaUpdateOp::~DeltaUpdateOp() {
157 void DeltaUpdateOp::Run(const base::DictionaryValue* command_args,
158 const base::FilePath& input_dir,
159 const base::FilePath& unpack_dir,
160 ComponentInstaller* installer,
162 const ComponentUnpacker::Callback& callback,
163 scoped_refptr<base::SequencedTaskRunner> task_runner) {
164 callback_ = callback;
165 in_process_ = in_process;
166 task_runner_ = task_runner;
167 std::string output_rel_path;
168 if (!command_args->GetString(kOutput, &output_rel_path) ||
169 !command_args->GetString(kSha256, &output_sha256_)) {
170 DoneRunning(ComponentUnpacker::kDeltaBadCommands, 0);
175 unpack_dir.Append(base::FilePath::FromUTF8Unsafe(output_rel_path));
176 ComponentUnpacker::Error parse_result =
177 DoParseArguments(command_args, input_dir, installer);
178 if (parse_result != ComponentUnpacker::kNone) {
179 DoneRunning(parse_result, 0);
183 const base::FilePath parent = output_abs_path_.DirName();
184 if (!base::DirectoryExists(parent)) {
185 if (!base::CreateDirectory(parent)) {
186 DoneRunning(ComponentUnpacker::kIoError, 0);
191 DoRun(base::Bind(&DeltaUpdateOp::DoneRunning,
192 scoped_refptr<DeltaUpdateOp>(this)));
195 void DeltaUpdateOp::DoneRunning(ComponentUnpacker::Error error,
196 int extended_error) {
197 if (error == ComponentUnpacker::kNone)
199 task_runner_->PostTask(FROM_HERE,
200 base::Bind(callback_, error, extended_error));
204 // Uses the hash as a checksum to confirm that the file now residing in the
205 // output directory probably has the contents it should.
206 ComponentUnpacker::Error DeltaUpdateOp::CheckHash() {
207 std::vector<uint8> expected_hash;
208 if (!base::HexStringToBytes(output_sha256_, &expected_hash) ||
209 expected_hash.size() != crypto::kSHA256Length)
210 return ComponentUnpacker::kDeltaVerificationFailure;
212 base::MemoryMappedFile output_file_mmapped;
213 if (!output_file_mmapped.Initialize(output_abs_path_))
214 return ComponentUnpacker::kDeltaVerificationFailure;
216 uint8 actual_hash[crypto::kSHA256Length] = {0};
217 const scoped_ptr<SecureHash> hasher(SecureHash::Create(SecureHash::SHA256));
218 hasher->Update(output_file_mmapped.data(), output_file_mmapped.length());
219 hasher->Finish(actual_hash, sizeof(actual_hash));
220 if (memcmp(actual_hash, &expected_hash[0], sizeof(actual_hash)))
221 return ComponentUnpacker::kDeltaVerificationFailure;
223 return ComponentUnpacker::kNone;
226 bool DeltaUpdateOp::InProcess() {
230 scoped_refptr<base::SequencedTaskRunner> DeltaUpdateOp::GetTaskRunner() {
234 DeltaUpdateOpCopy::DeltaUpdateOpCopy() {
237 DeltaUpdateOpCopy::~DeltaUpdateOpCopy() {
240 ComponentUnpacker::Error DeltaUpdateOpCopy::DoParseArguments(
241 const base::DictionaryValue* command_args,
242 const base::FilePath& input_dir,
243 ComponentInstaller* installer) {
244 std::string input_rel_path;
245 if (!command_args->GetString(kInput, &input_rel_path))
246 return ComponentUnpacker::kDeltaBadCommands;
248 if (!installer->GetInstalledFile(input_rel_path, &input_abs_path_))
249 return ComponentUnpacker::kDeltaMissingExistingFile;
251 return ComponentUnpacker::kNone;
254 void DeltaUpdateOpCopy::DoRun(const ComponentUnpacker::Callback& callback) {
255 if (!base::CopyFile(input_abs_path_, output_abs_path_))
256 callback.Run(ComponentUnpacker::kDeltaOperationFailure, 0);
258 callback.Run(ComponentUnpacker::kNone, 0);
261 DeltaUpdateOpCreate::DeltaUpdateOpCreate() {
264 DeltaUpdateOpCreate::~DeltaUpdateOpCreate() {
267 ComponentUnpacker::Error DeltaUpdateOpCreate::DoParseArguments(
268 const base::DictionaryValue* command_args,
269 const base::FilePath& input_dir,
270 ComponentInstaller* installer) {
271 std::string patch_rel_path;
272 if (!command_args->GetString(kPatch, &patch_rel_path))
273 return ComponentUnpacker::kDeltaBadCommands;
276 input_dir.Append(base::FilePath::FromUTF8Unsafe(patch_rel_path));
278 return ComponentUnpacker::kNone;
281 void DeltaUpdateOpCreate::DoRun(const ComponentUnpacker::Callback& callback) {
282 if (!base::Move(patch_abs_path_, output_abs_path_))
283 callback.Run(ComponentUnpacker::kDeltaOperationFailure, 0);
285 callback.Run(ComponentUnpacker::kNone, 0);
288 DeltaUpdateOpPatchHost::DeltaUpdateOpPatchHost(
289 scoped_refptr<DeltaUpdateOpPatch> patcher,
290 scoped_refptr<base::SequencedTaskRunner> task_runner)
291 : patcher_(patcher), task_runner_(task_runner) {
294 DeltaUpdateOpPatchHost::~DeltaUpdateOpPatchHost() {
297 void DeltaUpdateOpPatchHost::StartProcess(scoped_ptr<IPC::Message> message) {
298 // The DeltaUpdateOpPatchHost is not responsible for deleting the
299 // UtilityProcessHost object.
300 content::UtilityProcessHost* host = content::UtilityProcessHost::Create(
301 this, base::MessageLoopProxy::current().get());
302 host->DisableSandbox();
303 host->Send(message.release());
306 bool DeltaUpdateOpPatchHost::OnMessageReceived(const IPC::Message& message) {
308 IPC_BEGIN_MESSAGE_MAP(DeltaUpdateOpPatchHost, message)
309 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_PatchFile_Succeeded,
311 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_PatchFile_Failed,
313 IPC_MESSAGE_UNHANDLED(handled = false)
314 IPC_END_MESSAGE_MAP()
318 void DeltaUpdateOpPatchHost::OnPatchSucceeded() {
319 task_runner_->PostTask(FROM_HERE,
320 base::Bind(&DeltaUpdateOpPatch::DonePatching,
322 ComponentUnpacker::kNone,
328 void DeltaUpdateOpPatchHost::OnPatchFailed(int error_code) {
329 task_runner_->PostTask(FROM_HERE,
330 base::Bind(&DeltaUpdateOpPatch::DonePatching,
332 ComponentUnpacker::kDeltaOperationFailure,
338 void DeltaUpdateOpPatchHost::OnProcessCrashed(int exit_code) {
339 task_runner_->PostTask(
341 base::Bind(&DeltaUpdateOpPatch::DonePatching,
343 ComponentUnpacker::kDeltaPatchProcessFailure,
349 DeltaUpdateOpPatch::DeltaUpdateOpPatch(
350 scoped_ptr<DeltaUpdateOpPatchStrategy> strategy) {
351 strategy_ = strategy.Pass();
354 DeltaUpdateOpPatch::~DeltaUpdateOpPatch() {
357 ComponentUnpacker::Error DeltaUpdateOpPatch::DoParseArguments(
358 const base::DictionaryValue* command_args,
359 const base::FilePath& input_dir,
360 ComponentInstaller* installer) {
361 std::string patch_rel_path;
362 std::string input_rel_path;
363 if (!command_args->GetString(kPatch, &patch_rel_path) ||
364 !command_args->GetString(kInput, &input_rel_path))
365 return ComponentUnpacker::kDeltaBadCommands;
367 if (!installer->GetInstalledFile(input_rel_path, &input_abs_path_))
368 return ComponentUnpacker::kDeltaMissingExistingFile;
371 input_dir.Append(base::FilePath::FromUTF8Unsafe(patch_rel_path));
373 return ComponentUnpacker::kNone;
376 void DeltaUpdateOpPatch::DoRun(const ComponentUnpacker::Callback& callback) {
377 callback_ = callback;
379 host_ = new DeltaUpdateOpPatchHost(scoped_refptr<DeltaUpdateOpPatch>(this),
381 content::BrowserThread::PostTask(
382 content::BrowserThread::IO,
384 base::Bind(&DeltaUpdateOpPatchHost::StartProcess,
386 base::Passed(strategy_->GetPatchMessage(input_abs_path_,
388 output_abs_path_))));
391 const int result = strategy_->Patch(input_abs_path_,
394 if (result == strategy_->GetSuccessCode())
395 DonePatching(ComponentUnpacker::kNone, 0);
397 DonePatching(ComponentUnpacker::kDeltaOperationFailure, result);
400 void DeltaUpdateOpPatch::DonePatching(ComponentUnpacker::Error error,
403 if (error != ComponentUnpacker::kNone) {
404 error_code += strategy_->GetErrorOffset();
406 callback_.Run(error, error_code);
407 // The callback is no longer needed - it is best to release it in case it
408 // contains a reference to this object.
412 } // namespace component_updater