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/memory/scoped_handle.h"
15 #include "base/path_service.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "chrome/browser/component_updater/component_patcher.h"
18 #include "chrome/browser/component_updater/component_updater_service.h"
19 #include "chrome/common/chrome_utility_messages.h"
20 #include "chrome/common/extensions/extension_constants.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/utility_process_host.h"
23 #include "courgette/courgette.h"
24 #include "courgette/third_party/bsdiff.h"
25 #include "crypto/secure_hash.h"
26 #include "crypto/sha2.h"
27 #include "crypto/signature_verifier.h"
28 #include "extensions/common/crx_file.h"
29 #include "ipc/ipc_message_macros.h"
31 using crypto::SecureHash
;
33 namespace component_updater
{
37 const char kInput
[] = "input";
38 const char kOp
[] = "op";
39 const char kOutput
[] = "output";
40 const char kPatch
[] = "patch";
41 const char kSha256
[] = "sha256";
43 // The integer offset disambiguates between overlapping error ranges.
44 const int kCourgetteErrorOffset
= 300;
45 const int kBsdiffErrorOffset
= 600;
47 class CourgetteTraits
: public DeltaUpdateOpPatchStrategy
{
49 virtual int GetErrorOffset() const OVERRIDE
;
50 virtual int GetSuccessCode() const OVERRIDE
;
51 virtual scoped_ptr
<IPC::Message
> GetPatchMessage(
52 base::FilePath input_abs_path
,
53 base::FilePath patch_abs_path
,
54 base::FilePath output_abs_path
) OVERRIDE
;
55 virtual int Patch(base::FilePath input_abs_path
,
56 base::FilePath patch_abs_path
,
57 base::FilePath output_abs_path
) OVERRIDE
;
60 int CourgetteTraits::GetErrorOffset() const {
61 return kCourgetteErrorOffset
;
64 int CourgetteTraits::GetSuccessCode() const {
65 return courgette::C_OK
;
68 scoped_ptr
<IPC::Message
> CourgetteTraits::GetPatchMessage(
69 base::FilePath input_abs_path
,
70 base::FilePath patch_abs_path
,
71 base::FilePath output_abs_path
) {
72 return scoped_ptr
<IPC::Message
>(
73 new ChromeUtilityMsg_PatchFileCourgette(input_abs_path
,
78 int CourgetteTraits::Patch(base::FilePath input_abs_path
,
79 base::FilePath patch_abs_path
,
80 base::FilePath output_abs_path
) {
81 return courgette::ApplyEnsemblePatch(input_abs_path
.value().c_str(),
82 patch_abs_path
.value().c_str(),
83 output_abs_path
.value().c_str());
86 class BsdiffTraits
: public DeltaUpdateOpPatchStrategy
{
88 virtual int GetErrorOffset() const OVERRIDE
;
89 virtual int GetSuccessCode() const OVERRIDE
;
90 virtual scoped_ptr
<IPC::Message
> GetPatchMessage(
91 base::FilePath input_abs_path
,
92 base::FilePath patch_abs_path
,
93 base::FilePath output_abs_path
) OVERRIDE
;
94 virtual int Patch(base::FilePath input_abs_path
,
95 base::FilePath patch_abs_path
,
96 base::FilePath output_abs_path
) OVERRIDE
;
99 int BsdiffTraits::GetErrorOffset() const {
100 return kBsdiffErrorOffset
;
103 int BsdiffTraits::GetSuccessCode() const {
104 return courgette::OK
;
107 scoped_ptr
<IPC::Message
> BsdiffTraits::GetPatchMessage(
108 base::FilePath input_abs_path
,
109 base::FilePath patch_abs_path
,
110 base::FilePath output_abs_path
) {
111 return scoped_ptr
<IPC::Message
>(
112 new ChromeUtilityMsg_PatchFileBsdiff(input_abs_path
,
117 int BsdiffTraits::Patch(base::FilePath input_abs_path
,
118 base::FilePath patch_abs_path
,
119 base::FilePath output_abs_path
) {
120 return courgette::ApplyBinaryPatch(input_abs_path
,
127 DeltaUpdateOpPatchStrategy::~DeltaUpdateOpPatchStrategy() {
130 DeltaUpdateOp
* CreateDeltaUpdateOp(const std::string
& operation
) {
131 if (operation
== "copy") {
132 return new DeltaUpdateOpCopy();
133 } else if (operation
== "create") {
134 return new DeltaUpdateOpCreate();
135 } else if (operation
== "bsdiff") {
136 scoped_ptr
<DeltaUpdateOpPatchStrategy
> strategy(new BsdiffTraits());
137 return new DeltaUpdateOpPatch(strategy
.Pass());
138 } else if (operation
== "courgette") {
139 scoped_ptr
<DeltaUpdateOpPatchStrategy
> strategy(new CourgetteTraits());
140 return new DeltaUpdateOpPatch(strategy
.Pass());
145 DeltaUpdateOp
* CreateDeltaUpdateOp(const base::DictionaryValue
& command
) {
146 std::string operation
;
147 if (!command
.GetString(kOp
, &operation
))
149 return CreateDeltaUpdateOp(operation
);
152 DeltaUpdateOp::DeltaUpdateOp() : in_process_(false) {
155 DeltaUpdateOp::~DeltaUpdateOp() {
158 void DeltaUpdateOp::Run(const base::DictionaryValue
* command_args
,
159 const base::FilePath
& input_dir
,
160 const base::FilePath
& unpack_dir
,
161 ComponentInstaller
* installer
,
163 const ComponentUnpacker::Callback
& callback
,
164 scoped_refptr
<base::SequencedTaskRunner
> task_runner
) {
165 callback_
= callback
;
166 in_process_
= in_process
;
167 task_runner_
= task_runner
;
168 std::string output_rel_path
;
169 if (!command_args
->GetString(kOutput
, &output_rel_path
) ||
170 !command_args
->GetString(kSha256
, &output_sha256_
)) {
171 DoneRunning(ComponentUnpacker::kDeltaBadCommands
, 0);
176 unpack_dir
.Append(base::FilePath::FromUTF8Unsafe(output_rel_path
));
177 ComponentUnpacker::Error parse_result
=
178 DoParseArguments(command_args
, input_dir
, installer
);
179 if (parse_result
!= ComponentUnpacker::kNone
) {
180 DoneRunning(parse_result
, 0);
184 const base::FilePath parent
= output_abs_path_
.DirName();
185 if (!base::DirectoryExists(parent
)) {
186 if (!base::CreateDirectory(parent
)) {
187 DoneRunning(ComponentUnpacker::kIoError
, 0);
192 DoRun(base::Bind(&DeltaUpdateOp::DoneRunning
,
193 scoped_refptr
<DeltaUpdateOp
>(this)));
196 void DeltaUpdateOp::DoneRunning(ComponentUnpacker::Error error
,
197 int extended_error
) {
198 if (error
== ComponentUnpacker::kNone
)
200 task_runner_
->PostTask(FROM_HERE
,
201 base::Bind(callback_
, error
, extended_error
));
205 // Uses the hash as a checksum to confirm that the file now residing in the
206 // output directory probably has the contents it should.
207 ComponentUnpacker::Error
DeltaUpdateOp::CheckHash() {
208 std::vector
<uint8
> expected_hash
;
209 if (!base::HexStringToBytes(output_sha256_
, &expected_hash
) ||
210 expected_hash
.size() != crypto::kSHA256Length
)
211 return ComponentUnpacker::kDeltaVerificationFailure
;
213 base::MemoryMappedFile output_file_mmapped
;
214 if (!output_file_mmapped
.Initialize(output_abs_path_
))
215 return ComponentUnpacker::kDeltaVerificationFailure
;
217 uint8 actual_hash
[crypto::kSHA256Length
] = {0};
218 const scoped_ptr
<SecureHash
> hasher(SecureHash::Create(SecureHash::SHA256
));
219 hasher
->Update(output_file_mmapped
.data(), output_file_mmapped
.length());
220 hasher
->Finish(actual_hash
, sizeof(actual_hash
));
221 if (memcmp(actual_hash
, &expected_hash
[0], sizeof(actual_hash
)))
222 return ComponentUnpacker::kDeltaVerificationFailure
;
224 return ComponentUnpacker::kNone
;
227 bool DeltaUpdateOp::InProcess() {
231 scoped_refptr
<base::SequencedTaskRunner
> DeltaUpdateOp::GetTaskRunner() {
235 DeltaUpdateOpCopy::DeltaUpdateOpCopy() {
238 DeltaUpdateOpCopy::~DeltaUpdateOpCopy() {
241 ComponentUnpacker::Error
DeltaUpdateOpCopy::DoParseArguments(
242 const base::DictionaryValue
* command_args
,
243 const base::FilePath
& input_dir
,
244 ComponentInstaller
* installer
) {
245 std::string input_rel_path
;
246 if (!command_args
->GetString(kInput
, &input_rel_path
))
247 return ComponentUnpacker::kDeltaBadCommands
;
249 if (!installer
->GetInstalledFile(input_rel_path
, &input_abs_path_
))
250 return ComponentUnpacker::kDeltaMissingExistingFile
;
252 return ComponentUnpacker::kNone
;
255 void DeltaUpdateOpCopy::DoRun(const ComponentUnpacker::Callback
& callback
) {
256 if (!base::CopyFile(input_abs_path_
, output_abs_path_
))
257 callback
.Run(ComponentUnpacker::kDeltaOperationFailure
, 0);
259 callback
.Run(ComponentUnpacker::kNone
, 0);
262 DeltaUpdateOpCreate::DeltaUpdateOpCreate() {
265 DeltaUpdateOpCreate::~DeltaUpdateOpCreate() {
268 ComponentUnpacker::Error
DeltaUpdateOpCreate::DoParseArguments(
269 const base::DictionaryValue
* command_args
,
270 const base::FilePath
& input_dir
,
271 ComponentInstaller
* installer
) {
272 std::string patch_rel_path
;
273 if (!command_args
->GetString(kPatch
, &patch_rel_path
))
274 return ComponentUnpacker::kDeltaBadCommands
;
277 input_dir
.Append(base::FilePath::FromUTF8Unsafe(patch_rel_path
));
279 return ComponentUnpacker::kNone
;
282 void DeltaUpdateOpCreate::DoRun(const ComponentUnpacker::Callback
& callback
) {
283 if (!base::Move(patch_abs_path_
, output_abs_path_
))
284 callback
.Run(ComponentUnpacker::kDeltaOperationFailure
, 0);
286 callback
.Run(ComponentUnpacker::kNone
, 0);
289 DeltaUpdateOpPatchHost::DeltaUpdateOpPatchHost(
290 scoped_refptr
<DeltaUpdateOpPatch
> patcher
,
291 scoped_refptr
<base::SequencedTaskRunner
> task_runner
)
292 : patcher_(patcher
), task_runner_(task_runner
) {
295 DeltaUpdateOpPatchHost::~DeltaUpdateOpPatchHost() {
298 void DeltaUpdateOpPatchHost::StartProcess(scoped_ptr
<IPC::Message
> message
) {
299 // The DeltaUpdateOpPatchHost is not responsible for deleting the
300 // UtilityProcessHost object.
301 content::UtilityProcessHost
* host
= content::UtilityProcessHost::Create(
302 this, base::MessageLoopProxy::current().get());
303 host
->DisableSandbox();
304 host
->Send(message
.release());
307 bool DeltaUpdateOpPatchHost::OnMessageReceived(const IPC::Message
& message
) {
309 IPC_BEGIN_MESSAGE_MAP(DeltaUpdateOpPatchHost
, message
)
310 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_PatchFile_Succeeded
,
312 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_PatchFile_Failed
,
314 IPC_MESSAGE_UNHANDLED(handled
= false)
315 IPC_END_MESSAGE_MAP()
319 void DeltaUpdateOpPatchHost::OnPatchSucceeded() {
320 task_runner_
->PostTask(FROM_HERE
,
321 base::Bind(&DeltaUpdateOpPatch::DonePatching
,
323 ComponentUnpacker::kNone
,
329 void DeltaUpdateOpPatchHost::OnPatchFailed(int error_code
) {
330 task_runner_
->PostTask(FROM_HERE
,
331 base::Bind(&DeltaUpdateOpPatch::DonePatching
,
333 ComponentUnpacker::kDeltaOperationFailure
,
339 void DeltaUpdateOpPatchHost::OnProcessCrashed(int exit_code
) {
340 task_runner_
->PostTask(
342 base::Bind(&DeltaUpdateOpPatch::DonePatching
,
344 ComponentUnpacker::kDeltaPatchProcessFailure
,
350 DeltaUpdateOpPatch::DeltaUpdateOpPatch(
351 scoped_ptr
<DeltaUpdateOpPatchStrategy
> strategy
) {
352 strategy_
= strategy
.Pass();
355 DeltaUpdateOpPatch::~DeltaUpdateOpPatch() {
358 ComponentUnpacker::Error
DeltaUpdateOpPatch::DoParseArguments(
359 const base::DictionaryValue
* command_args
,
360 const base::FilePath
& input_dir
,
361 ComponentInstaller
* installer
) {
362 std::string patch_rel_path
;
363 std::string input_rel_path
;
364 if (!command_args
->GetString(kPatch
, &patch_rel_path
) ||
365 !command_args
->GetString(kInput
, &input_rel_path
))
366 return ComponentUnpacker::kDeltaBadCommands
;
368 if (!installer
->GetInstalledFile(input_rel_path
, &input_abs_path_
))
369 return ComponentUnpacker::kDeltaMissingExistingFile
;
372 input_dir
.Append(base::FilePath::FromUTF8Unsafe(patch_rel_path
));
374 return ComponentUnpacker::kNone
;
377 void DeltaUpdateOpPatch::DoRun(const ComponentUnpacker::Callback
& callback
) {
378 callback_
= callback
;
380 host_
= new DeltaUpdateOpPatchHost(scoped_refptr
<DeltaUpdateOpPatch
>(this),
382 content::BrowserThread::PostTask(
383 content::BrowserThread::IO
,
385 base::Bind(&DeltaUpdateOpPatchHost::StartProcess
,
387 base::Passed(strategy_
->GetPatchMessage(input_abs_path_
,
389 output_abs_path_
))));
392 const int result
= strategy_
->Patch(input_abs_path_
,
395 if (result
== strategy_
->GetSuccessCode())
396 DonePatching(ComponentUnpacker::kNone
, 0);
398 DonePatching(ComponentUnpacker::kDeltaOperationFailure
, result
);
401 void DeltaUpdateOpPatch::DonePatching(ComponentUnpacker::Error error
,
404 if (error
!= ComponentUnpacker::kNone
) {
405 error_code
+= strategy_
->GetErrorOffset();
407 callback_
.Run(error
, error_code
);
408 // The callback is no longer needed - it is best to release it in case it
409 // contains a reference to this object.
413 } // namespace component_updater