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
3 // found in the LICENSE file.
5 #include "chrome/browser/nacl_host/nacl_file_host.h"
8 #include "base/file_util.h"
9 #include "base/files/file_path.h"
10 #include "base/path_service.h"
11 #include "base/platform_file.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/threading/sequenced_worker_pool.h"
14 #include "chrome/browser/nacl_host/nacl_host_message_filter.h"
15 #include "components/nacl/browser/nacl_browser.h"
16 #include "components/nacl/common/nacl_browser_delegate.h"
17 #include "components/nacl/common/nacl_host_messages.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/render_view_host.h"
20 #include "content/public/browser/site_instance.h"
21 #include "ipc/ipc_platform_file.h"
23 using content::BrowserThread;
27 // Force a prefix to prevent user from opening "magic" files.
28 const char* kExpectedFilePrefix = "pnacl_public_";
30 // Restrict PNaCl file lengths to reduce likelyhood of hitting bugs
31 // in file name limit error-handling-code-paths, etc.
32 const size_t kMaxFileLength = 40;
34 void NotifyRendererOfError(
35 NaClHostMessageFilter* nacl_host_message_filter,
36 IPC::Message* reply_msg) {
37 reply_msg->set_reply_error();
38 nacl_host_message_filter->Send(reply_msg);
41 bool PnaclDoOpenFile(const base::FilePath& file_to_open,
42 base::PlatformFile* out_file) {
43 base::PlatformFileError error_code;
44 *out_file = base::CreatePlatformFile(file_to_open,
45 base::PLATFORM_FILE_OPEN |
46 base::PLATFORM_FILE_READ,
49 if (error_code != base::PLATFORM_FILE_OK) {
56 scoped_refptr<NaClHostMessageFilter> nacl_host_message_filter,
57 const std::string& filename,
58 IPC::Message* reply_msg) {
59 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
60 base::FilePath full_filepath;
62 // PNaCl must be installed.
63 base::FilePath pnacl_dir;
64 if (!nacl::NaClBrowser::GetDelegate()->GetPnaclDirectory(&pnacl_dir) ||
65 !base::PathExists(pnacl_dir)) {
66 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
70 // Do some validation.
71 if (!nacl_file_host::PnaclCanOpenFile(filename, &full_filepath)) {
72 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
76 base::PlatformFile file_to_open;
77 if (!PnaclDoOpenFile(full_filepath, &file_to_open)) {
78 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
83 // Do any DuplicateHandle magic that is necessary first.
84 IPC::PlatformFileForTransit target_desc =
85 IPC::GetFileHandleForProcess(file_to_open,
86 nacl_host_message_filter->PeerHandle(),
87 true /* Close source */);
88 if (target_desc == IPC::InvalidPlatformFileForTransit()) {
89 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
92 NaClHostMsg_GetReadonlyPnaclFD::WriteReplyParams(
93 reply_msg, target_desc);
94 nacl_host_message_filter->Send(reply_msg);
97 void DoRegisterOpenedNaClExecutableFile(
98 scoped_refptr<NaClHostMessageFilter> nacl_host_message_filter,
99 base::PlatformFile file,
100 base::FilePath file_path,
101 IPC::Message* reply_msg) {
102 // IO thread owns the NaClBrowser singleton.
103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
105 nacl::NaClBrowser* nacl_browser = nacl::NaClBrowser::GetInstance();
106 uint64 file_token_lo = 0;
107 uint64 file_token_hi = 0;
108 nacl_browser->PutFilePath(file_path, &file_token_lo, &file_token_hi);
110 IPC::PlatformFileForTransit file_desc = IPC::GetFileHandleForProcess(
112 nacl_host_message_filter->PeerHandle(),
113 true /* close_source */);
115 NaClHostMsg_OpenNaClExecutable::WriteReplyParams(
116 reply_msg, file_desc, file_token_lo, file_token_hi);
117 nacl_host_message_filter->Send(reply_msg);
120 // Convert the file URL into a file descriptor.
121 // This function is security sensitive. Be sure to check with a security
122 // person before you modify it.
123 void DoOpenNaClExecutableOnThreadPool(
124 scoped_refptr<NaClHostMessageFilter> nacl_host_message_filter,
125 const GURL& file_url,
126 IPC::Message* reply_msg) {
127 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
129 base::FilePath file_path;
130 if (!nacl::NaClBrowser::GetDelegate()->MapUrlToLocalFilePath(
131 file_url, true /* use_blocking_api */, &file_path)) {
132 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
136 base::PlatformFile file;
137 nacl::OpenNaClExecutableImpl(file_path, &file);
138 if (file != base::kInvalidPlatformFileValue) {
139 // This function is running on the blocking pool, but the path needs to be
140 // registered in a structure owned by the IO thread.
141 BrowserThread::PostTask(
142 BrowserThread::IO, FROM_HERE,
144 &DoRegisterOpenedNaClExecutableFile,
145 nacl_host_message_filter,
146 file, file_path, reply_msg));
148 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
155 namespace nacl_file_host {
157 void GetReadonlyPnaclFd(
158 scoped_refptr<NaClHostMessageFilter> nacl_host_message_filter,
159 const std::string& filename,
160 IPC::Message* reply_msg) {
161 if (!BrowserThread::PostBlockingPoolTask(
163 base::Bind(&DoOpenPnaclFile,
164 nacl_host_message_filter,
167 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
171 // This function is security sensitive. Be sure to check with a security
172 // person before you modify it.
173 bool PnaclCanOpenFile(const std::string& filename,
174 base::FilePath* file_to_open) {
175 if (filename.length() > kMaxFileLength)
178 if (filename.empty())
181 // Restrict character set of the file name to something really simple
182 // (a-z, 0-9, and underscores).
183 for (size_t i = 0; i < filename.length(); ++i) {
184 char charAt = filename[i];
185 if (charAt < 'a' || charAt > 'z')
186 if (charAt < '0' || charAt > '9')
191 // PNaCl must be installed.
192 base::FilePath pnacl_dir;
193 if (!nacl::NaClBrowser::GetDelegate()->GetPnaclDirectory(&pnacl_dir) ||
197 // Prepend the prefix to restrict files to a whitelisted set.
198 base::FilePath full_path = pnacl_dir.AppendASCII(
199 std::string(kExpectedFilePrefix) + filename);
200 *file_to_open = full_path;
204 void OpenNaClExecutable(
205 scoped_refptr<NaClHostMessageFilter> nacl_host_message_filter,
207 const GURL& file_url,
208 IPC::Message* reply_msg) {
209 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
210 BrowserThread::PostTask(
211 BrowserThread::UI, FROM_HERE,
214 nacl_host_message_filter,
215 render_view_id, file_url, reply_msg));
219 // Make sure render_view_id is valid and that the URL is a part of the
220 // render view's site. Without these checks, apps could probe the extension
221 // directory or run NaCl code from other extensions.
222 content::RenderViewHost* rvh = content::RenderViewHost::FromID(
223 nacl_host_message_filter->render_process_id(), render_view_id);
225 nacl_host_message_filter->BadMessageReceived(); // Kill the renderer.
228 content::SiteInstance* site_instance = rvh->GetSiteInstance();
229 if (!content::SiteInstance::IsSameWebSite(site_instance->GetBrowserContext(),
230 site_instance->GetSiteURL(),
232 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
236 // The URL is part of the current app. Now query the extension system for the
237 // file path and convert that to a file descriptor. This should be done on a
238 // blocking pool thread.
239 if (!BrowserThread::PostBlockingPoolTask(
242 &DoOpenNaClExecutableOnThreadPool,
243 nacl_host_message_filter,
244 file_url, reply_msg))) {
245 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
249 } // namespace nacl_file_host