OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/extensions/sandboxed_extension_unpacker.h" | 5 #include "chrome/browser/extensions/sandboxed_extension_unpacker.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
10 #include "base/base64.h" | 10 #include "base/base64.h" |
11 #include "base/crypto/signature_verifier.h" | 11 #include "base/crypto/signature_verifier.h" |
12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
13 #include "base/file_util_proxy.h" | 13 #include "base/file_util_proxy.h" |
14 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
| 15 #include "base/metrics/histogram.h" |
| 16 #include "base/path_service.h" |
15 #include "base/scoped_handle.h" | 17 #include "base/scoped_handle.h" |
16 #include "base/task.h" | 18 #include "base/task.h" |
17 #include "base/utf_string_conversions.h" // TODO(viettrungluu): delete me. | 19 #include "base/utf_string_conversions.h" // TODO(viettrungluu): delete me. |
18 #include "chrome/browser/browser_thread.h" | 20 #include "chrome/browser/browser_thread.h" |
19 #include "chrome/browser/extensions/extension_service.h" | 21 #include "chrome/browser/extensions/extension_service.h" |
20 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" | 22 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" |
| 23 #include "chrome/common/chrome_paths.h" |
21 #include "chrome/common/chrome_switches.h" | 24 #include "chrome/common/chrome_switches.h" |
22 #include "chrome/common/extensions/extension.h" | 25 #include "chrome/common/extensions/extension.h" |
23 #include "chrome/common/extensions/extension_constants.h" | 26 #include "chrome/common/extensions/extension_constants.h" |
24 #include "chrome/common/extensions/extension_file_util.h" | 27 #include "chrome/common/extensions/extension_file_util.h" |
25 #include "chrome/common/extensions/extension_l10n_util.h" | 28 #include "chrome/common/extensions/extension_l10n_util.h" |
26 #include "chrome/common/extensions/extension_unpacker.h" | 29 #include "chrome/common/extensions/extension_unpacker.h" |
27 #include "chrome/common/json_value_serializer.h" | 30 #include "chrome/common/json_value_serializer.h" |
28 #include "gfx/codec/png_codec.h" | 31 #include "gfx/codec/png_codec.h" |
29 #include "grit/generated_resources.h" | 32 #include "grit/generated_resources.h" |
30 #include "third_party/skia/include/core/SkBitmap.h" | 33 #include "third_party/skia/include/core/SkBitmap.h" |
31 | 34 |
32 const char SandboxedExtensionUnpacker::kExtensionHeaderMagic[] = "Cr24"; | 35 const char SandboxedExtensionUnpacker::kExtensionHeaderMagic[] = "Cr24"; |
33 | 36 |
34 SandboxedExtensionUnpacker::SandboxedExtensionUnpacker( | 37 SandboxedExtensionUnpacker::SandboxedExtensionUnpacker( |
35 const FilePath& crx_path, | 38 const FilePath& crx_path, |
36 const FilePath& temp_path, | |
37 ResourceDispatcherHost* rdh, | 39 ResourceDispatcherHost* rdh, |
38 SandboxedExtensionUnpackerClient* client) | 40 SandboxedExtensionUnpackerClient* client) |
39 : crx_path_(crx_path), temp_path_(temp_path), | 41 : crx_path_(crx_path), |
40 thread_identifier_(BrowserThread::ID_COUNT), | 42 thread_identifier_(BrowserThread::ID_COUNT), |
41 rdh_(rdh), client_(client), got_response_(false) { | 43 rdh_(rdh), client_(client), got_response_(false) { |
42 } | 44 } |
43 | 45 |
| 46 bool SandboxedExtensionUnpacker::CreateTempDirectory() { |
| 47 CHECK(BrowserThread::GetCurrentThreadIdentifier(&thread_identifier_)); |
| 48 |
| 49 FilePath user_data_temp_dir = extension_file_util::GetUserDataTempDir(); |
| 50 if (user_data_temp_dir.empty()) { |
| 51 // TODO(skerner): This should have its own string. |
| 52 // Using an existing string so that the change can be merged. |
| 53 ReportFailure(l10n_util::GetStringFUTF8( |
| 54 IDS_EXTENSION_PACKAGE_INSTALL_ERROR, |
| 55 ASCIIToUTF16("COULD_NOT_CREATE_TEMP_DIRECTORY"))); |
| 56 return false; |
| 57 } |
| 58 |
| 59 if (!temp_dir_.CreateUniqueTempDirUnderPath(user_data_temp_dir)) { |
| 60 ReportFailure(l10n_util::GetStringFUTF8( |
| 61 IDS_EXTENSION_PACKAGE_INSTALL_ERROR, |
| 62 ASCIIToUTF16("COULD_NOT_CREATE_TEMP_DIRECTORY"))); |
| 63 return false; |
| 64 } |
| 65 |
| 66 return true; |
| 67 } |
| 68 |
44 void SandboxedExtensionUnpacker::Start() { | 69 void SandboxedExtensionUnpacker::Start() { |
45 // We assume that we are started on the thread that the client wants us to do | 70 // We assume that we are started on the thread that the client wants us to do |
46 // file IO on. | 71 // file IO on. |
47 CHECK(BrowserThread::GetCurrentThreadIdentifier(&thread_identifier_)); | 72 CHECK(BrowserThread::GetCurrentThreadIdentifier(&thread_identifier_)); |
48 | 73 |
49 // Create a temporary directory to work in. | 74 if (!CreateTempDirectory()) |
50 if (!temp_dir_.CreateUniqueTempDirUnderPath(temp_path_)) { | 75 return; // ReportFailure() already called. |
51 // Could not create temporary directory. | |
52 ReportFailure(l10n_util::GetStringFUTF8( | |
53 IDS_EXTENSION_PACKAGE_INSTALL_ERROR, | |
54 ASCIIToUTF16("COULD_NOT_CREATE_TEMP_DIRECTORY"))); | |
55 return; | |
56 } | |
57 | 76 |
58 // Initialize the path that will eventually contain the unpacked extension. | 77 // Initialize the path that will eventually contain the unpacked extension. |
59 extension_root_ = temp_dir_.path().AppendASCII( | 78 extension_root_ = temp_dir_.path().AppendASCII( |
60 extension_filenames::kTempExtensionName); | 79 extension_filenames::kTempExtensionName); |
61 | 80 |
62 // Extract the public key and validate the package. | 81 // Extract the public key and validate the package. |
63 if (!ValidateSignature()) | 82 if (!ValidateSignature()) |
64 return; // ValidateSignature() already reported the error. | 83 return; // ValidateSignature() already reported the error. |
65 | 84 |
66 // Copy the crx file into our working directory. | 85 // Copy the crx file into our working directory. |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 ASCIIToUTF16("CRX_SIGNATURE_VERIFICATION_FAILED"))); | 323 ASCIIToUTF16("CRX_SIGNATURE_VERIFICATION_FAILED"))); |
305 return false; | 324 return false; |
306 } | 325 } |
307 | 326 |
308 base::Base64Encode(std::string(reinterpret_cast<char*>(&key.front()), | 327 base::Base64Encode(std::string(reinterpret_cast<char*>(&key.front()), |
309 key.size()), &public_key_); | 328 key.size()), &public_key_); |
310 return true; | 329 return true; |
311 } | 330 } |
312 | 331 |
313 void SandboxedExtensionUnpacker::ReportFailure(const std::string& error) { | 332 void SandboxedExtensionUnpacker::ReportFailure(const std::string& error) { |
| 333 UMA_HISTOGRAM_COUNTS("Extensions.SandboxUnpackFailure", 1); |
314 client_->OnUnpackFailure(error); | 334 client_->OnUnpackFailure(error); |
315 } | 335 } |
316 | 336 |
317 void SandboxedExtensionUnpacker::ReportSuccess() { | 337 void SandboxedExtensionUnpacker::ReportSuccess() { |
| 338 UMA_HISTOGRAM_COUNTS("Extensions.SandboxUnpackSuccess", 1); |
| 339 |
318 // Client takes ownership of temporary directory and extension. | 340 // Client takes ownership of temporary directory and extension. |
319 client_->OnUnpackSuccess(temp_dir_.Take(), extension_root_, extension_); | 341 client_->OnUnpackSuccess(temp_dir_.Take(), extension_root_, extension_); |
320 extension_ = NULL; | 342 extension_ = NULL; |
321 } | 343 } |
322 | 344 |
323 DictionaryValue* SandboxedExtensionUnpacker::RewriteManifestFile( | 345 DictionaryValue* SandboxedExtensionUnpacker::RewriteManifestFile( |
324 const DictionaryValue& manifest) { | 346 const DictionaryValue& manifest) { |
325 // Add the public key extracted earlier to the parsed manifest and overwrite | 347 // Add the public key extracted earlier to the parsed manifest and overwrite |
326 // the original manifest. We do this to ensure the manifest doesn't contain an | 348 // the original manifest. We do this to ensure the manifest doesn't contain an |
327 // exploitable bug that could be used to compromise the browser. | 349 // exploitable bug that could be used to compromise the browser. |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
490 // Error saving catalog. | 512 // Error saving catalog. |
491 ReportFailure(l10n_util::GetStringFUTF8( | 513 ReportFailure(l10n_util::GetStringFUTF8( |
492 IDS_EXTENSION_PACKAGE_INSTALL_ERROR, | 514 IDS_EXTENSION_PACKAGE_INSTALL_ERROR, |
493 ASCIIToUTF16("ERROR_SAVING_CATALOG"))); | 515 ASCIIToUTF16("ERROR_SAVING_CATALOG"))); |
494 return false; | 516 return false; |
495 } | 517 } |
496 } | 518 } |
497 | 519 |
498 return true; | 520 return true; |
499 } | 521 } |
OLD | NEW |