OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/nacl_host/nacl_file_host.h" | |
6 | |
7 #include "base/bind.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/browser/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" | |
22 | |
23 using content::BrowserThread; | |
24 | |
25 namespace { | |
26 | |
27 // Force a prefix to prevent user from opening "magic" files. | |
28 const char* kExpectedFilePrefix = "pnacl_public_"; | |
29 | |
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; | |
33 | |
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); | |
39 } | |
40 | |
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, | |
47 NULL, | |
48 &error_code); | |
49 if (error_code != base::PLATFORM_FILE_OK) { | |
50 return false; | |
51 } | |
52 return true; | |
53 } | |
54 | |
55 void DoOpenPnaclFile( | |
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; | |
61 | |
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); | |
67 return; | |
68 } | |
69 | |
70 // Do some validation. | |
71 if (!nacl_file_host::PnaclCanOpenFile(filename, &full_filepath)) { | |
72 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | |
73 return; | |
74 } | |
75 | |
76 base::PlatformFile file_to_open; | |
77 if (!PnaclDoOpenFile(full_filepath, &file_to_open)) { | |
78 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | |
79 return; | |
80 } | |
81 | |
82 // Send the reply! | |
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); | |
90 return; | |
91 } | |
92 NaClHostMsg_GetReadonlyPnaclFD::WriteReplyParams( | |
93 reply_msg, target_desc); | |
94 nacl_host_message_filter->Send(reply_msg); | |
95 } | |
96 | |
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)); | |
104 | |
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); | |
109 | |
110 IPC::PlatformFileForTransit file_desc = IPC::GetFileHandleForProcess( | |
111 file, | |
112 nacl_host_message_filter->PeerHandle(), | |
113 true /* close_source */); | |
114 | |
115 NaClHostMsg_OpenNaClExecutable::WriteReplyParams( | |
116 reply_msg, file_desc, file_token_lo, file_token_hi); | |
117 nacl_host_message_filter->Send(reply_msg); | |
118 } | |
119 | |
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()); | |
128 | |
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); | |
133 return; | |
134 } | |
135 | |
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, | |
143 base::Bind( | |
144 &DoRegisterOpenedNaClExecutableFile, | |
145 nacl_host_message_filter, | |
146 file, file_path, reply_msg)); | |
147 } else { | |
148 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | |
149 return; | |
150 } | |
151 } | |
152 | |
153 } // namespace | |
154 | |
155 namespace nacl_file_host { | |
156 | |
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( | |
162 FROM_HERE, | |
163 base::Bind(&DoOpenPnaclFile, | |
164 nacl_host_message_filter, | |
165 filename, | |
166 reply_msg))) { | |
167 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | |
168 } | |
169 } | |
170 | |
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) | |
176 return false; | |
177 | |
178 if (filename.empty()) | |
179 return false; | |
180 | |
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') | |
187 if (charAt != '_') | |
188 return false; | |
189 } | |
190 | |
191 // PNaCl must be installed. | |
192 base::FilePath pnacl_dir; | |
193 if (!nacl::NaClBrowser::GetDelegate()->GetPnaclDirectory(&pnacl_dir) || | |
194 pnacl_dir.empty()) | |
195 return false; | |
196 | |
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; | |
201 return true; | |
202 } | |
203 | |
204 void OpenNaClExecutable( | |
205 scoped_refptr<NaClHostMessageFilter> nacl_host_message_filter, | |
206 int render_view_id, | |
207 const GURL& file_url, | |
208 IPC::Message* reply_msg) { | |
209 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
210 BrowserThread::PostTask( | |
211 BrowserThread::UI, FROM_HERE, | |
212 base::Bind( | |
213 &OpenNaClExecutable, | |
214 nacl_host_message_filter, | |
215 render_view_id, file_url, reply_msg)); | |
216 return; | |
217 } | |
218 | |
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); | |
224 if (!rvh) { | |
225 nacl_host_message_filter->BadMessageReceived(); // Kill the renderer. | |
226 return; | |
227 } | |
228 content::SiteInstance* site_instance = rvh->GetSiteInstance(); | |
229 if (!content::SiteInstance::IsSameWebSite(site_instance->GetBrowserContext(), | |
230 site_instance->GetSiteURL(), | |
231 file_url)) { | |
232 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | |
233 return; | |
234 } | |
235 | |
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( | |
240 FROM_HERE, | |
241 base::Bind( | |
242 &DoOpenNaClExecutableOnThreadPool, | |
243 nacl_host_message_filter, | |
244 file_url, reply_msg))) { | |
245 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | |
246 } | |
247 } | |
248 | |
249 } // namespace nacl_file_host | |
OLD | NEW |