| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/safe_browsing/sandboxed_zip_analyzer.h" | 5 #include "chrome/browser/safe_browsing/sandboxed_zip_analyzer.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | |
| 11 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 12 #include "base/location.h" | |
| 13 #include "base/logging.h" | |
| 14 #include "base/task_scheduler/post_task.h" | 11 #include "base/task_scheduler/post_task.h" |
| 15 #include "base/threading/sequenced_worker_pool.h" | |
| 16 #include "chrome/common/chrome_utility_messages.h" | |
| 17 #include "chrome/common/safe_browsing/zip_analyzer_results.h" | 12 #include "chrome/common/safe_browsing/zip_analyzer_results.h" |
| 18 #include "chrome/grit/generated_resources.h" | 13 #include "chrome/grit/generated_resources.h" |
| 19 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/browser/child_process_data.h" | |
| 21 #include "content/public/browser/render_process_host.h" | |
| 22 #include "content/public/common/content_switches.h" | |
| 23 #include "ipc/ipc_message_macros.h" | |
| 24 #include "ipc/ipc_platform_file.h" | |
| 25 #include "ui/base/l10n/l10n_util.h" | 15 #include "ui/base/l10n/l10n_util.h" |
| 26 | 16 |
| 27 using content::BrowserThread; | |
| 28 | |
| 29 namespace safe_browsing { | 17 namespace safe_browsing { |
| 30 | 18 |
| 31 SandboxedZipAnalyzer::SandboxedZipAnalyzer( | 19 SandboxedZipAnalyzer::SandboxedZipAnalyzer(const base::FilePath& zip_file, |
| 32 const base::FilePath& zip_file, | 20 const ResultCallback& callback) |
| 33 const ResultCallback& result_callback) | 21 : file_path_(zip_file), callback_(callback) { |
| 34 : zip_file_name_(zip_file), | 22 DCHECK(callback); |
| 35 callback_(result_callback), | |
| 36 callback_called_(false) { | |
| 37 } | 23 } |
| 38 | 24 |
| 39 void SandboxedZipAnalyzer::Start() { | 25 void SandboxedZipAnalyzer::Start() { |
| 40 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 26 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 41 // Starting the analyzer will block on opening the zip file, so run this | 27 |
| 42 // on a worker thread. The task does not need to block shutdown. | |
| 43 base::PostTaskWithTraits( | 28 base::PostTaskWithTraits( |
| 44 FROM_HERE, base::TaskTraits() | 29 FROM_HERE, |
| 45 .MayBlock() | 30 base::TaskTraits() |
| 46 .WithPriority(base::TaskPriority::BACKGROUND) | 31 .MayBlock() |
| 47 .WithShutdownBehavior( | 32 .WithPriority(base::TaskPriority::BACKGROUND) |
| 48 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN), | 33 .WithShutdownBehavior( |
| 49 base::Bind(&SandboxedZipAnalyzer::AnalyzeInSandbox, this)); | 34 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN), |
| 35 base::Bind(&SandboxedZipAnalyzer::PrepareFileToAnalyze, this)); |
| 50 } | 36 } |
| 51 | 37 |
| 52 SandboxedZipAnalyzer::~SandboxedZipAnalyzer() { | 38 SandboxedZipAnalyzer::~SandboxedZipAnalyzer() = default; |
| 53 // If we're using UtilityProcessHost, we may not be destroyed on | |
| 54 // the UI or IO thread. | |
| 55 CloseTemporaryFile(); | |
| 56 } | |
| 57 | 39 |
| 58 void SandboxedZipAnalyzer::CloseTemporaryFile() { | 40 void SandboxedZipAnalyzer::PrepareFileToAnalyze() { |
| 59 if (!temp_file_.IsValid()) | 41 base::File file(file_path_, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 60 return; | |
| 61 // Close the temporary file in the blocking pool since doing so will delete | |
| 62 // the file. | |
| 63 base::PostTaskWithTraits( | |
| 64 FROM_HERE, base::TaskTraits() | |
| 65 .MayBlock() | |
| 66 .WithPriority(base::TaskPriority::BACKGROUND) | |
| 67 .WithShutdownBehavior( | |
| 68 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN), | |
| 69 base::Bind(&base::File::Close, | |
| 70 base::Owned(new base::File(std::move(temp_file_))))); | |
| 71 } | |
| 72 | 42 |
| 73 void SandboxedZipAnalyzer::AnalyzeInSandbox() { | 43 if (!file.IsValid()) { |
| 74 // This zip file will be closed on the IO thread once it has been handed | 44 DLOG(ERROR) << "Could not open file: " << file_path_.value(); |
| 75 // off to the child process. | 45 ReportFileFailure(); |
| 76 zip_file_.Initialize(zip_file_name_, | |
| 77 base::File::FLAG_OPEN | base::File::FLAG_READ); | |
| 78 if (!zip_file_.IsValid()) { | |
| 79 DVLOG(1) << "Could not open zip file: " << zip_file_name_.value(); | |
| 80 if (!BrowserThread::PostTask( | |
| 81 BrowserThread::IO, FROM_HERE, | |
| 82 base::Bind(&SandboxedZipAnalyzer::OnAnalyzeZipFileFinished, this, | |
| 83 zip_analyzer::Results()))) { | |
| 84 NOTREACHED(); | |
| 85 } | |
| 86 return; | 46 return; |
| 87 } | 47 } |
| 88 | 48 |
| 89 // This temp file will be closed in the blocking pool when results from the | |
| 90 // analyzer return. | |
| 91 base::FilePath temp_path; | 49 base::FilePath temp_path; |
| 50 base::File temp_file; |
| 92 if (base::CreateTemporaryFile(&temp_path)) { | 51 if (base::CreateTemporaryFile(&temp_path)) { |
| 93 temp_file_.Initialize(temp_path, (base::File::FLAG_CREATE_ALWAYS | | 52 temp_file.Initialize( |
| 94 base::File::FLAG_READ | | 53 temp_path, (base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_READ | |
| 95 base::File::FLAG_WRITE | | 54 base::File::FLAG_WRITE | base::File::FLAG_TEMPORARY | |
| 96 base::File::FLAG_TEMPORARY | | 55 base::File::FLAG_DELETE_ON_CLOSE)); |
| 97 base::File::FLAG_DELETE_ON_CLOSE)); | |
| 98 } | 56 } |
| 99 DVLOG_IF(1, !temp_file_.IsValid()) | |
| 100 << "Could not open temporary output file: " << temp_path.value(); | |
| 101 | 57 |
| 102 BrowserThread::PostTask( | 58 if (!temp_file.IsValid()) { |
| 103 BrowserThread::IO, FROM_HERE, | 59 DLOG(ERROR) << "Could not open temp file: " << temp_path.value(); |
| 104 base::Bind(&SandboxedZipAnalyzer::StartProcessOnIOThread, this)); | 60 ReportFileFailure(); |
| 61 return; |
| 62 } |
| 63 |
| 64 content::BrowserThread::PostTask( |
| 65 content::BrowserThread::UI, FROM_HERE, |
| 66 base::Bind(&SandboxedZipAnalyzer::AnalyzeFile, this, base::Passed(&file), |
| 67 base::Passed(&temp_file))); |
| 105 } | 68 } |
| 106 | 69 |
| 107 void SandboxedZipAnalyzer::OnProcessCrashed(int exit_code) { | 70 void SandboxedZipAnalyzer::ReportFileFailure() { |
| 108 OnAnalyzeZipFileFinished(zip_analyzer::Results()); | 71 DCHECK(!utility_process_mojo_client_); |
| 72 |
| 73 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 74 base::Bind(callback_, Results())); |
| 109 } | 75 } |
| 110 | 76 |
| 111 void SandboxedZipAnalyzer::OnProcessLaunchFailed(int error_code) { | 77 void SandboxedZipAnalyzer::AnalyzeFile(base::File file, base::File temp_file) { |
| 112 OnAnalyzeZipFileFinished(zip_analyzer::Results()); | 78 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 79 DCHECK(!utility_process_mojo_client_); |
| 80 |
| 81 utility_process_mojo_client_ = base::MakeUnique< |
| 82 content::UtilityProcessMojoClient<chrome::mojom::SafeArchiveAnalyzer>>( |
| 83 l10n_util::GetStringUTF16( |
| 84 IDS_UTILITY_PROCESS_SAFE_BROWSING_ZIP_FILE_ANALYZER_NAME)); |
| 85 utility_process_mojo_client_->set_error_callback( |
| 86 base::Bind(&SandboxedZipAnalyzer::AnalyzeFileDone, this, Results())); |
| 87 |
| 88 utility_process_mojo_client_->Start(); |
| 89 |
| 90 utility_process_mojo_client_->service()->AnalyzeZipFile( |
| 91 std::move(file), std::move(temp_file), |
| 92 base::Bind(&SandboxedZipAnalyzer::AnalyzeFileDone, this)); |
| 113 } | 93 } |
| 114 | 94 |
| 115 bool SandboxedZipAnalyzer::OnMessageReceived(const IPC::Message& message) { | 95 void SandboxedZipAnalyzer::AnalyzeFileDone(const Results& results) { |
| 116 bool handled = true; | 96 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 117 IPC_BEGIN_MESSAGE_MAP(SandboxedZipAnalyzer, message) | |
| 118 IPC_MESSAGE_HANDLER( | |
| 119 ChromeUtilityHostMsg_AnalyzeZipFileForDownloadProtection_Finished, | |
| 120 OnAnalyzeZipFileFinished) | |
| 121 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 122 IPC_END_MESSAGE_MAP() | |
| 123 return handled; | |
| 124 } | |
| 125 | 97 |
| 126 void SandboxedZipAnalyzer::StartProcessOnIOThread() { | 98 utility_process_mojo_client_.reset(); |
| 127 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 99 callback_.Run(results); |
| 128 utility_process_host_ = | |
| 129 content::UtilityProcessHost::Create( | |
| 130 this, BrowserThread::GetTaskRunnerForThread(BrowserThread::IO).get()) | |
| 131 ->AsWeakPtr(); | |
| 132 utility_process_host_->SetName(l10n_util::GetStringUTF16( | |
| 133 IDS_UTILITY_PROCESS_SAFE_BROWSING_ZIP_FILE_ANALYZER_NAME)); | |
| 134 utility_process_host_->Send( | |
| 135 new ChromeUtilityMsg_AnalyzeZipFileForDownloadProtection( | |
| 136 IPC::TakePlatformFileForTransit(std::move(zip_file_)), | |
| 137 IPC::GetPlatformFileForTransit(temp_file_.GetPlatformFile(), | |
| 138 false /* !close_source_handle */))); | |
| 139 } | |
| 140 | |
| 141 void SandboxedZipAnalyzer::OnAnalyzeZipFileFinished( | |
| 142 const zip_analyzer::Results& results) { | |
| 143 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 144 if (callback_called_) | |
| 145 return; | |
| 146 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 147 base::Bind(callback_, results)); | |
| 148 callback_called_ = true; | |
| 149 CloseTemporaryFile(); | |
| 150 } | 100 } |
| 151 | 101 |
| 152 } // namespace safe_browsing | 102 } // namespace safe_browsing |
| OLD | NEW |