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 "components/nacl/browser/nacl_file_host.h" | 5 #include "components/nacl/browser/nacl_file_host.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file.h" | 8 #include "base/files/file.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "base/threading/sequenced_worker_pool.h" | 12 #include "base/threading/sequenced_worker_pool.h" |
13 #include "components/nacl/browser/nacl_browser.h" | 13 #include "components/nacl/browser/nacl_browser.h" |
14 #include "components/nacl/browser/nacl_browser_delegate.h" | 14 #include "components/nacl/browser/nacl_browser_delegate.h" |
15 #include "components/nacl/browser/nacl_host_message_filter.h" | 15 #include "components/nacl/browser/nacl_host_message_filter.h" |
16 #include "components/nacl/common/nacl_host_messages.h" | 16 #include "components/nacl/common/nacl_host_messages.h" |
17 #include "components/nacl/common/nacl_types.h" | |
17 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
18 #include "content/public/browser/render_view_host.h" | 19 #include "content/public/browser/render_view_host.h" |
19 #include "content/public/browser/site_instance.h" | 20 #include "content/public/browser/site_instance.h" |
20 #include "ipc/ipc_platform_file.h" | 21 #include "ipc/ipc_platform_file.h" |
21 | 22 |
22 using content::BrowserThread; | 23 using content::BrowserThread; |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
26 // Force a prefix to prevent user from opening "magic" files. | 27 // Force a prefix to prevent user from opening "magic" files. |
27 const char* kExpectedFilePrefix = "pnacl_public_"; | 28 const char* kExpectedFilePrefix = "pnacl_public_"; |
28 | 29 |
29 // Restrict PNaCl file lengths to reduce likelyhood of hitting bugs | 30 // Restrict PNaCl file lengths to reduce likelyhood of hitting bugs |
30 // in file name limit error-handling-code-paths, etc. | 31 // in file name limit error-handling-code-paths, etc. |
31 const size_t kMaxFileLength = 40; | 32 const size_t kMaxFileLength = 40; |
32 | 33 |
33 void NotifyRendererOfError( | 34 void NotifyRendererOfError( |
34 nacl::NaClHostMessageFilter* nacl_host_message_filter, | 35 nacl::NaClHostMessageFilter* nacl_host_message_filter, |
35 IPC::Message* reply_msg) { | 36 IPC::Message* reply_msg) { |
36 reply_msg->set_reply_error(); | 37 reply_msg->set_reply_error(); |
37 nacl_host_message_filter->Send(reply_msg); | 38 nacl_host_message_filter->Send(reply_msg); |
38 } | 39 } |
39 | 40 |
40 typedef void (*WriteFileInfoReply)(IPC::Message* reply_msg, | 41 void DoRegisterOpenedNaClExecutableFilePnacl( |
Mark Seaborn
2015/02/02 23:21:50
Isn't this the same as DoRegisterOpenedNaClExecuta
Yusuke Sato
2015/02/04 02:00:28
Done.
| |
41 IPC::PlatformFileForTransit file_desc, | 42 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
42 uint64 file_token_lo, | 43 base::File file, |
43 uint64 file_token_hi); | 44 base::FilePath file_path, |
45 IPC::Message* reply_msg) { | |
46 // IO thread owns the NaClBrowser singleton. | |
47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
48 | |
49 nacl::NaClBrowser* nacl_browser = nacl::NaClBrowser::GetInstance(); | |
50 uint64 file_token_lo = 0; | |
51 uint64 file_token_hi = 0; | |
52 nacl_browser->PutFilePath(file_path, &file_token_lo, &file_token_hi); | |
53 | |
54 IPC::PlatformFileForTransit file_desc = IPC::TakeFileHandleForProcess( | |
55 file.Pass(), nacl_host_message_filter->PeerHandle()); | |
56 | |
57 NaClHostMsg_GetReadonlyPnaclFD::WriteReplyParams( | |
58 reply_msg, file_desc, file_token_lo, file_token_hi); | |
59 nacl_host_message_filter->Send(reply_msg); | |
60 } | |
44 | 61 |
45 void DoRegisterOpenedNaClExecutableFile( | 62 void DoRegisterOpenedNaClExecutableFile( |
46 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, | 63 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
47 base::File file, | 64 base::File file, |
48 base::FilePath file_path, | 65 base::FilePath file_path, |
49 IPC::Message* reply_msg, | 66 scoped_ptr<base::File[]> resource_files, |
50 WriteFileInfoReply write_reply_message) { | 67 const std::vector<base::FilePath>& resource_file_paths, |
68 IPC::Message* reply_msg) { | |
51 // IO thread owns the NaClBrowser singleton. | 69 // IO thread owns the NaClBrowser singleton. |
52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
53 | 71 |
54 nacl::NaClBrowser* nacl_browser = nacl::NaClBrowser::GetInstance(); | 72 nacl::NaClBrowser* nacl_browser = nacl::NaClBrowser::GetInstance(); |
55 uint64 file_token_lo = 0; | 73 uint64 file_token_lo = 0; |
56 uint64 file_token_hi = 0; | 74 uint64 file_token_hi = 0; |
57 nacl_browser->PutFilePath(file_path, &file_token_lo, &file_token_hi); | 75 nacl_browser->PutFilePath(file_path, &file_token_lo, &file_token_hi); |
58 | 76 |
59 IPC::PlatformFileForTransit file_desc = IPC::TakeFileHandleForProcess( | 77 IPC::PlatformFileForTransit file_desc = IPC::TakeFileHandleForProcess( |
60 file.Pass(), | 78 file.Pass(), |
61 nacl_host_message_filter->PeerHandle()); | 79 nacl_host_message_filter->PeerHandle()); |
80 nacl::NaClOpenExecutableResult::FileInfo nexe_file_info( | |
81 file_desc, file_token_lo, file_token_hi); | |
62 | 82 |
63 write_reply_message(reply_msg, file_desc, file_token_lo, file_token_hi); | 83 std::vector<nacl::NaClOpenExecutableResult::FileInfo> resource_files_info; |
84 for (size_t i = 0; i < resource_file_paths.size(); ++i) { | |
85 uint64 resource_file_token_lo = 0; | |
86 uint64 resource_file_token_hi = 0; | |
87 nacl_browser->PutFilePath(resource_file_paths[i], | |
88 &resource_file_token_lo, | |
89 &resource_file_token_hi); | |
90 IPC::PlatformFileForTransit resource_file = IPC::TakeFileHandleForProcess( | |
91 resource_files[i].Pass(), nacl_host_message_filter->PeerHandle()); | |
92 nacl::NaClOpenExecutableResult::FileInfo resource_file_info( | |
93 resource_file, resource_file_token_lo, resource_file_token_hi); | |
94 resource_files_info.push_back(resource_file_info); | |
95 } | |
96 | |
97 NaClHostMsg_OpenNaClResources::WriteReplyParams( | |
98 reply_msg, | |
99 nacl::NaClOpenExecutableResult(nexe_file_info, resource_files_info)); | |
64 nacl_host_message_filter->Send(reply_msg); | 100 nacl_host_message_filter->Send(reply_msg); |
65 } | 101 } |
66 | 102 |
67 void DoOpenPnaclFile( | 103 void DoOpenPnaclFile( |
68 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, | 104 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
69 const std::string& filename, | 105 const std::string& filename, |
70 bool is_executable, | 106 bool is_executable, |
71 IPC::Message* reply_msg) { | 107 IPC::Message* reply_msg) { |
72 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 108 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
73 base::FilePath full_filepath; | 109 base::FilePath full_filepath; |
(...skipping 19 matching lines...) Expand all Loading... | |
93 return; | 129 return; |
94 } | 130 } |
95 | 131 |
96 // This function is running on the blocking pool, but the path needs to be | 132 // This function is running on the blocking pool, but the path needs to be |
97 // registered in a structure owned by the IO thread. | 133 // registered in a structure owned by the IO thread. |
98 // Not all PNaCl files are executable. Only register those that are | 134 // Not all PNaCl files are executable. Only register those that are |
99 // executable in the NaCl file_path cache. | 135 // executable in the NaCl file_path cache. |
100 if (is_executable) { | 136 if (is_executable) { |
101 BrowserThread::PostTask( | 137 BrowserThread::PostTask( |
102 BrowserThread::IO, FROM_HERE, | 138 BrowserThread::IO, FROM_HERE, |
103 base::Bind(&DoRegisterOpenedNaClExecutableFile, | 139 base::Bind(&DoRegisterOpenedNaClExecutableFilePnacl, |
104 nacl_host_message_filter, | 140 nacl_host_message_filter, |
105 Passed(file_to_open.Pass()), full_filepath, reply_msg, | 141 Passed(file_to_open.Pass()), full_filepath, reply_msg)); |
106 static_cast<WriteFileInfoReply>( | |
107 NaClHostMsg_GetReadonlyPnaclFD::WriteReplyParams))); | |
108 } else { | 142 } else { |
109 IPC::PlatformFileForTransit target_desc = | 143 IPC::PlatformFileForTransit target_desc = |
110 IPC::TakeFileHandleForProcess(file_to_open.Pass(), | 144 IPC::TakeFileHandleForProcess(file_to_open.Pass(), |
111 nacl_host_message_filter->PeerHandle()); | 145 nacl_host_message_filter->PeerHandle()); |
112 uint64_t dummy_file_token = 0; | 146 uint64_t dummy_file_token = 0; |
113 NaClHostMsg_GetReadonlyPnaclFD::WriteReplyParams( | 147 NaClHostMsg_GetReadonlyPnaclFD::WriteReplyParams( |
114 reply_msg, target_desc, dummy_file_token, dummy_file_token); | 148 reply_msg, target_desc, dummy_file_token, dummy_file_token); |
115 nacl_host_message_filter->Send(reply_msg); | 149 nacl_host_message_filter->Send(reply_msg); |
116 } | 150 } |
117 } | 151 } |
118 | 152 |
119 // Convert the file URL into a file descriptor. | 153 // Convert the file URL into a file descriptor. |
120 // This function is security sensitive. Be sure to check with a security | 154 // This function is security sensitive. Be sure to check with a security |
121 // person before you modify it. | 155 // person before you modify it. |
122 void DoOpenNaClExecutableOnThreadPool( | 156 void DoOpenNaClResourcesOnThreadPool( |
123 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, | 157 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
124 const GURL& file_url, | 158 const GURL& executable_file_url, |
159 const std::vector<GURL>& resource_urls, | |
125 IPC::Message* reply_msg) { | 160 IPC::Message* reply_msg) { |
126 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 161 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
127 | 162 |
128 base::FilePath file_path; | 163 base::FilePath file_path; |
129 if (!nacl::NaClBrowser::GetDelegate()->MapUrlToLocalFilePath( | 164 if (!nacl::NaClBrowser::GetDelegate()->MapUrlToLocalFilePath( |
hidehiko
2015/01/28 09:05:19
Looks very similar to L186-L199.
Can be extracted
Yusuke Sato
2015/02/04 02:00:28
Done (unified the two code paths)
| |
130 file_url, | 165 executable_file_url, |
131 true /* use_blocking_api */, | 166 true /* use_blocking_api */, |
132 nacl_host_message_filter->profile_directory(), | 167 nacl_host_message_filter->profile_directory(), |
133 &file_path)) { | 168 &file_path)) { |
134 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 169 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
135 return; | 170 return; |
136 } | 171 } |
137 | 172 |
138 base::File file = nacl::OpenNaClReadExecImpl(file_path, | 173 base::File file = nacl::OpenNaClReadExecImpl(file_path, |
139 true /* is_executable */); | 174 true /* is_executable */); |
140 if (file.IsValid()) { | 175 if (!file.IsValid()) { |
141 // This function is running on the blocking pool, but the path needs to be | |
142 // registered in a structure owned by the IO thread. | |
143 BrowserThread::PostTask( | |
144 BrowserThread::IO, FROM_HERE, | |
145 base::Bind( | |
146 &DoRegisterOpenedNaClExecutableFile, | |
147 nacl_host_message_filter, | |
148 Passed(file.Pass()), file_path, reply_msg, | |
149 static_cast<WriteFileInfoReply>( | |
150 NaClHostMsg_OpenNaClExecutable::WriteReplyParams))); | |
151 } else { | |
152 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 176 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
153 return; | 177 return; |
154 } | 178 } |
179 | |
180 scoped_ptr<base::File[]> resource_files; | |
181 if (!resource_urls.empty()) | |
182 resource_files.reset(new base::File[resource_urls.size()]); | |
183 std::vector<base::FilePath> resource_file_paths(resource_urls.size()); | |
184 | |
185 for (size_t i = 0; i < resource_urls.size(); ++i) { | |
186 if (!nacl::NaClBrowser::GetDelegate()->MapUrlToLocalFilePath( | |
187 resource_urls[i], | |
188 true /* use_blocking_api */, | |
189 nacl_host_message_filter->profile_directory(), | |
190 &resource_file_paths[i])) { | |
191 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | |
192 return; | |
193 } | |
194 resource_files[i] = nacl::OpenNaClReadExecImpl(resource_file_paths[i], | |
195 true /* is_executable */); | |
196 if (!resource_files[i].IsValid()) { | |
197 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | |
198 return; | |
199 } | |
200 } | |
201 | |
202 // This function is running on the blocking pool, but the path needs to be | |
203 // registered in a structure owned by the IO thread. | |
204 BrowserThread::PostTask( | |
205 BrowserThread::IO, | |
206 FROM_HERE, | |
207 base::Bind(&DoRegisterOpenedNaClExecutableFile, | |
208 nacl_host_message_filter, | |
209 Passed(file.Pass()), | |
210 file_path, | |
211 Passed(resource_files.Pass()), | |
212 resource_file_paths, | |
213 reply_msg)); | |
155 } | 214 } |
156 | 215 |
157 } // namespace | 216 } // namespace |
158 | 217 |
159 namespace nacl_file_host { | 218 namespace nacl_file_host { |
160 | 219 |
161 void GetReadonlyPnaclFd( | 220 void GetReadonlyPnaclFd( |
162 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, | 221 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
163 const std::string& filename, | 222 const std::string& filename, |
164 bool is_executable, | 223 bool is_executable, |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
200 pnacl_dir.empty()) | 259 pnacl_dir.empty()) |
201 return false; | 260 return false; |
202 | 261 |
203 // Prepend the prefix to restrict files to a whitelisted set. | 262 // Prepend the prefix to restrict files to a whitelisted set. |
204 base::FilePath full_path = pnacl_dir.AppendASCII( | 263 base::FilePath full_path = pnacl_dir.AppendASCII( |
205 std::string(kExpectedFilePrefix) + filename); | 264 std::string(kExpectedFilePrefix) + filename); |
206 *file_to_open = full_path; | 265 *file_to_open = full_path; |
207 return true; | 266 return true; |
208 } | 267 } |
209 | 268 |
210 void OpenNaClExecutable( | 269 void OpenNaClResources( |
211 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, | 270 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
212 int render_view_id, | 271 int render_view_id, |
213 const GURL& file_url, | 272 const GURL& executable_file_url, |
273 const std::vector<GURL>& resource_urls, | |
214 IPC::Message* reply_msg) { | 274 IPC::Message* reply_msg) { |
215 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 275 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
216 BrowserThread::PostTask( | 276 BrowserThread::PostTask( |
217 BrowserThread::UI, FROM_HERE, | 277 BrowserThread::UI, FROM_HERE, |
218 base::Bind( | 278 base::Bind( |
219 &OpenNaClExecutable, | 279 &OpenNaClResources, |
220 nacl_host_message_filter, | 280 nacl_host_message_filter, |
221 render_view_id, file_url, reply_msg)); | 281 render_view_id, executable_file_url, resource_urls, reply_msg)); |
222 return; | 282 return; |
223 } | 283 } |
224 | 284 |
225 // Make sure render_view_id is valid and that the URL is a part of the | 285 // Make sure render_view_id is valid and that the URL is a part of the |
226 // render view's site. Without these checks, apps could probe the extension | 286 // render view's site. Without these checks, apps could probe the extension |
227 // directory or run NaCl code from other extensions. | 287 // directory or run NaCl code from other extensions. |
228 content::RenderViewHost* rvh = content::RenderViewHost::FromID( | 288 content::RenderViewHost* rvh = content::RenderViewHost::FromID( |
229 nacl_host_message_filter->render_process_id(), render_view_id); | 289 nacl_host_message_filter->render_process_id(), render_view_id); |
230 if (!rvh) { | 290 if (!rvh) { |
231 nacl_host_message_filter->BadMessageReceived(); // Kill the renderer. | 291 nacl_host_message_filter->BadMessageReceived(); // Kill the renderer. |
232 return; | 292 return; |
233 } | 293 } |
234 content::SiteInstance* site_instance = rvh->GetSiteInstance(); | 294 content::SiteInstance* site_instance = rvh->GetSiteInstance(); |
235 if (!content::SiteInstance::IsSameWebSite(site_instance->GetBrowserContext(), | 295 if (!content::SiteInstance::IsSameWebSite(site_instance->GetBrowserContext(), |
236 site_instance->GetSiteURL(), | 296 site_instance->GetSiteURL(), |
237 file_url)) { | 297 executable_file_url)) { |
238 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 298 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
239 return; | 299 return; |
240 } | 300 } |
241 | 301 |
302 for (size_t i = 0; i < resource_urls.size(); ++i) { | |
303 if (!content::SiteInstance::IsSameWebSite( | |
304 site_instance->GetBrowserContext(), | |
305 site_instance->GetSiteURL(), | |
306 resource_urls[i])) { | |
307 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | |
308 return; | |
309 } | |
310 } | |
311 | |
242 // The URL is part of the current app. Now query the extension system for the | 312 // The URL is part of the current app. Now query the extension system for the |
243 // file path and convert that to a file descriptor. This should be done on a | 313 // file path and convert that to a file descriptor. This should be done on a |
244 // blocking pool thread. | 314 // blocking pool thread. |
245 if (!BrowserThread::PostBlockingPoolTask( | 315 if (!BrowserThread::PostBlockingPoolTask( |
246 FROM_HERE, | 316 FROM_HERE, |
247 base::Bind( | 317 base::Bind( |
248 &DoOpenNaClExecutableOnThreadPool, | 318 &DoOpenNaClResourcesOnThreadPool, |
249 nacl_host_message_filter, | 319 nacl_host_message_filter, |
250 file_url, reply_msg))) { | 320 executable_file_url, resource_urls, reply_msg))) { |
251 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 321 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
252 } | 322 } |
253 } | 323 } |
254 | 324 |
255 } // namespace nacl_file_host | 325 } // namespace nacl_file_host |
OLD | NEW |