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 |
Mark Seaborn
2015/02/09 04:48:34
Nit: remove empty line (only 1 between top-level d
Yusuke Sato
2015/02/11 05:54:20
Done.
| |
41 IPC::PlatformFileForTransit file_desc, | 42 typedef void (*WriteFileInfoReply)( |
42 uint64 file_token_lo, | 43 IPC::Message* reply_msg, |
43 uint64 file_token_hi); | 44 const std::vector<nacl::NaClFileInfo>& files_info); |
44 | 45 |
45 void DoRegisterOpenedNaClExecutableFile( | 46 void DoRegisterOpenedNaClExecutableFile( |
46 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, | 47 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
47 base::File file, | 48 scoped_ptr<base::File[]> resource_files, |
48 base::FilePath file_path, | 49 const std::vector<base::FilePath>& resource_file_paths, |
49 IPC::Message* reply_msg, | 50 IPC::Message* reply_msg, |
50 WriteFileInfoReply write_reply_message) { | 51 WriteFileInfoReply write_reply_message) { |
51 // IO thread owns the NaClBrowser singleton. | 52 // IO thread owns the NaClBrowser singleton. |
52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
53 | 54 |
54 nacl::NaClBrowser* nacl_browser = nacl::NaClBrowser::GetInstance(); | 55 nacl::NaClBrowser* nacl_browser = nacl::NaClBrowser::GetInstance(); |
55 uint64 file_token_lo = 0; | 56 std::vector<nacl::NaClFileInfo> reply_files_info; |
56 uint64 file_token_hi = 0; | 57 for (size_t i = 0; i < resource_file_paths.size(); ++i) { |
57 nacl_browser->PutFilePath(file_path, &file_token_lo, &file_token_hi); | 58 uint64 resource_file_token_lo = 0; |
59 uint64 resource_file_token_hi = 0; | |
60 nacl_browser->PutFilePath(resource_file_paths[i], | |
61 &resource_file_token_lo, | |
62 &resource_file_token_hi); | |
63 IPC::PlatformFileForTransit resource_file = IPC::TakeFileHandleForProcess( | |
64 resource_files[i].Pass(), nacl_host_message_filter->PeerHandle()); | |
65 nacl::NaClFileInfo resource_file_info( | |
66 resource_file, resource_file_token_lo, resource_file_token_hi); | |
67 reply_files_info.push_back(resource_file_info); | |
68 } | |
58 | 69 |
59 IPC::PlatformFileForTransit file_desc = IPC::TakeFileHandleForProcess( | 70 write_reply_message(reply_msg, reply_files_info); |
60 file.Pass(), | 71 nacl_host_message_filter->Send(reply_msg); |
61 nacl_host_message_filter->PeerHandle()); | 72 } |
62 | 73 |
63 write_reply_message(reply_msg, file_desc, file_token_lo, file_token_hi); | 74 void WriteReplyParamsWrapper( |
64 nacl_host_message_filter->Send(reply_msg); | 75 IPC::Message* reply_msg, |
76 const std::vector<nacl::NaClFileInfo>& files_info) { | |
77 DCHECK(!files_info.empty()); | |
78 NaClHostMsg_GetReadonlyPnaclFD::WriteReplyParams(reply_msg, files_info[0]); | |
65 } | 79 } |
66 | 80 |
67 void DoOpenPnaclFile( | 81 void DoOpenPnaclFile( |
68 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, | 82 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
69 const std::string& filename, | 83 const std::string& filename, |
70 bool is_executable, | 84 bool is_executable, |
71 IPC::Message* reply_msg) { | 85 IPC::Message* reply_msg) { |
72 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 86 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
73 base::FilePath full_filepath; | 87 base::FilePath full_filepath; |
74 | 88 |
(...skipping 16 matching lines...) Expand all Loading... | |
91 if (!file_to_open.IsValid()) { | 105 if (!file_to_open.IsValid()) { |
92 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 106 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
93 return; | 107 return; |
94 } | 108 } |
95 | 109 |
96 // This function is running on the blocking pool, but the path needs to be | 110 // This function is running on the blocking pool, but the path needs to be |
97 // registered in a structure owned by the IO thread. | 111 // registered in a structure owned by the IO thread. |
98 // Not all PNaCl files are executable. Only register those that are | 112 // Not all PNaCl files are executable. Only register those that are |
99 // executable in the NaCl file_path cache. | 113 // executable in the NaCl file_path cache. |
100 if (is_executable) { | 114 if (is_executable) { |
115 scoped_ptr<base::File[]> resource_files(new base::File[1]); | |
116 resource_files[0] = file_to_open.Pass(); | |
117 std::vector<base::FilePath> resource_file_paths(1); | |
Mark Seaborn
2015/02/09 04:48:34
Are you sure that base::Bind() makes a copy of thi
Yusuke Sato
2015/02/11 05:54:20
Yes, base/callback.h has some comments: https://co
| |
118 resource_file_paths[0] = full_filepath; | |
119 | |
101 BrowserThread::PostTask( | 120 BrowserThread::PostTask( |
102 BrowserThread::IO, FROM_HERE, | 121 BrowserThread::IO, FROM_HERE, |
103 base::Bind(&DoRegisterOpenedNaClExecutableFile, | 122 base::Bind(&DoRegisterOpenedNaClExecutableFile, |
104 nacl_host_message_filter, | 123 nacl_host_message_filter, |
105 Passed(file_to_open.Pass()), full_filepath, reply_msg, | 124 Passed(resource_files.Pass()), |
106 static_cast<WriteFileInfoReply>( | 125 resource_file_paths, |
107 NaClHostMsg_GetReadonlyPnaclFD::WriteReplyParams))); | 126 reply_msg, |
127 static_cast<WriteFileInfoReply>(WriteReplyParamsWrapper))); | |
108 } else { | 128 } else { |
109 IPC::PlatformFileForTransit target_desc = | 129 IPC::PlatformFileForTransit target_desc = |
110 IPC::TakeFileHandleForProcess(file_to_open.Pass(), | 130 IPC::TakeFileHandleForProcess(file_to_open.Pass(), |
111 nacl_host_message_filter->PeerHandle()); | 131 nacl_host_message_filter->PeerHandle()); |
112 uint64_t dummy_file_token = 0; | 132 uint64_t dummy_file_token = 0; |
113 NaClHostMsg_GetReadonlyPnaclFD::WriteReplyParams( | 133 NaClHostMsg_GetReadonlyPnaclFD::WriteReplyParams( |
114 reply_msg, target_desc, dummy_file_token, dummy_file_token); | 134 reply_msg, |
135 nacl::NaClFileInfo(target_desc, dummy_file_token, dummy_file_token)); | |
115 nacl_host_message_filter->Send(reply_msg); | 136 nacl_host_message_filter->Send(reply_msg); |
116 } | 137 } |
117 } | 138 } |
118 | 139 |
119 // Convert the file URL into a file descriptor. | 140 // Convert the resource URLs into file descriptors. |
120 // This function is security sensitive. Be sure to check with a security | 141 // This function is security sensitive. Be sure to check with a security |
121 // person before you modify it. | 142 // person before you modify it. |
122 void DoOpenNaClExecutableOnThreadPool( | 143 void DoOpenNaClResourcesOnThreadPool( |
123 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, | 144 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
124 const GURL& file_url, | 145 const std::vector<GURL>& resource_urls, |
125 bool enable_validation_caching, | 146 bool enable_validation_caching, |
126 IPC::Message* reply_msg) { | 147 IPC::Message* reply_msg) { |
127 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 148 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
128 | 149 |
129 base::FilePath file_path; | 150 // Convert |resource_urls| into Files and FilePaths. |
130 if (!nacl::NaClBrowser::GetDelegate()->MapUrlToLocalFilePath( | 151 scoped_ptr<base::File[]> resource_files(new base::File[resource_urls.size()]); |
131 file_url, | 152 std::vector<base::FilePath> resource_file_paths(resource_urls.size()); |
132 true /* use_blocking_api */, | 153 |
133 nacl_host_message_filter->profile_directory(), | 154 for (size_t i = 0; i < resource_urls.size(); ++i) { |
134 &file_path)) { | 155 if (!nacl::NaClBrowser::GetDelegate()->MapUrlToLocalFilePath( |
135 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 156 resource_urls[i], |
136 return; | 157 true /* use_blocking_api */, |
158 nacl_host_message_filter->profile_directory(), | |
159 &resource_file_paths[i])) { | |
160 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | |
161 return; | |
162 } | |
163 resource_files[i] = nacl::OpenNaClReadExecImpl(resource_file_paths[i], | |
164 true /* is_executable */); | |
165 if (!resource_files[i].IsValid()) { | |
166 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | |
167 return; | |
168 } | |
137 } | 169 } |
138 | 170 |
139 base::File file = nacl::OpenNaClReadExecImpl(file_path, | 171 // Opening NaCl executables works with or without validation caching. |
140 true /* is_executable */); | 172 // Validation caching requires that the file descriptor is registered now |
141 if (file.IsValid()) { | 173 // for later use, which will save time. |
142 // Opening a NaCl executable works with or without validation caching. | 174 // When validation caching isn't used (e.g. Non-SFI mode), there is no |
143 // Validation caching requires that the file descriptor is registered now | 175 // reason to do that unnecessary registration. |
144 // for later use, which will save time. | 176 if (enable_validation_caching) { |
145 // When validation caching isn't used (e.g. Non-SFI mode), there is no | 177 // This function is running on the blocking pool, but the path needs to be |
146 // reason to do that unnecessary registration. | 178 // registered in a structure owned by the IO thread. |
147 if (enable_validation_caching) { | 179 BrowserThread::PostTask( |
148 // This function is running on the blocking pool, but the path needs to be | 180 BrowserThread::IO, |
149 // registered in a structure owned by the IO thread. | 181 FROM_HERE, |
150 BrowserThread::PostTask( | 182 base::Bind( |
151 BrowserThread::IO, FROM_HERE, | 183 &DoRegisterOpenedNaClExecutableFile, |
152 base::Bind( | 184 nacl_host_message_filter, |
153 &DoRegisterOpenedNaClExecutableFile, | 185 Passed(resource_files.Pass()), |
154 nacl_host_message_filter, | 186 resource_file_paths, |
155 Passed(file.Pass()), file_path, reply_msg, | 187 reply_msg, static_cast<WriteFileInfoReply>( |
156 static_cast<WriteFileInfoReply>( | 188 NaClHostMsg_OpenNaClResources::WriteReplyParams))); |
157 NaClHostMsg_OpenNaClExecutable::WriteReplyParams))); | 189 } else { |
158 } else { | 190 uint64_t dummy_file_token = 0; |
191 std::vector<nacl::NaClFileInfo> reply_files_info; | |
192 for (size_t i = 0; i < resource_urls.size(); ++i) { | |
159 IPC::PlatformFileForTransit file_desc = | 193 IPC::PlatformFileForTransit file_desc = |
160 IPC::TakeFileHandleForProcess(file.Pass(), | 194 IPC::TakeFileHandleForProcess(resource_files[i].Pass(), |
161 nacl_host_message_filter->PeerHandle()); | 195 nacl_host_message_filter->PeerHandle()); |
162 uint64_t dummy_file_token = 0; | 196 reply_files_info.push_back(nacl::NaClFileInfo( |
163 NaClHostMsg_OpenNaClExecutable::WriteReplyParams( | 197 file_desc, dummy_file_token, dummy_file_token)); |
164 reply_msg, file_desc, dummy_file_token, dummy_file_token); | |
165 nacl_host_message_filter->Send(reply_msg); | |
166 } | 198 } |
167 } else { | 199 NaClHostMsg_OpenNaClResources::WriteReplyParams(reply_msg, |
168 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 200 reply_files_info); |
169 return; | 201 nacl_host_message_filter->Send(reply_msg); |
170 } | 202 } |
171 } | 203 } |
172 | 204 |
173 } // namespace | 205 } // namespace |
174 | 206 |
175 namespace nacl_file_host { | 207 namespace nacl_file_host { |
176 | 208 |
177 void GetReadonlyPnaclFd( | 209 void GetReadonlyPnaclFd( |
178 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, | 210 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
179 const std::string& filename, | 211 const std::string& filename, |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
216 pnacl_dir.empty()) | 248 pnacl_dir.empty()) |
217 return false; | 249 return false; |
218 | 250 |
219 // Prepend the prefix to restrict files to a whitelisted set. | 251 // Prepend the prefix to restrict files to a whitelisted set. |
220 base::FilePath full_path = pnacl_dir.AppendASCII( | 252 base::FilePath full_path = pnacl_dir.AppendASCII( |
221 std::string(kExpectedFilePrefix) + filename); | 253 std::string(kExpectedFilePrefix) + filename); |
222 *file_to_open = full_path; | 254 *file_to_open = full_path; |
223 return true; | 255 return true; |
224 } | 256 } |
225 | 257 |
226 void OpenNaClExecutable( | 258 void OpenNaClResources( |
227 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, | 259 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
228 int render_view_id, | 260 int render_view_id, |
229 const GURL& file_url, | 261 const std::vector<GURL>& resource_urls, |
230 bool enable_validation_caching, | 262 bool enable_validation_caching, |
231 IPC::Message* reply_msg) { | 263 IPC::Message* reply_msg) { |
232 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 264 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
233 BrowserThread::PostTask( | 265 BrowserThread::PostTask( |
234 BrowserThread::UI, FROM_HERE, | 266 BrowserThread::UI, FROM_HERE, |
235 base::Bind( | 267 base::Bind( |
236 &OpenNaClExecutable, | 268 &OpenNaClResources, |
237 nacl_host_message_filter, | 269 nacl_host_message_filter, |
238 render_view_id, | 270 render_view_id, |
239 file_url, | 271 resource_urls, |
240 enable_validation_caching, | 272 enable_validation_caching, |
241 reply_msg)); | 273 reply_msg)); |
242 return; | 274 return; |
243 } | 275 } |
244 | 276 |
245 // Make sure render_view_id is valid and that the URL is a part of the | 277 // Make sure render_view_id is valid and that the URL is a part of the |
246 // render view's site. Without these checks, apps could probe the extension | 278 // render view's site. Without these checks, apps could probe the extension |
247 // directory or run NaCl code from other extensions. | 279 // directory or run NaCl code from other extensions. |
248 content::RenderViewHost* rvh = content::RenderViewHost::FromID( | 280 content::RenderViewHost* rvh = content::RenderViewHost::FromID( |
249 nacl_host_message_filter->render_process_id(), render_view_id); | 281 nacl_host_message_filter->render_process_id(), render_view_id); |
250 if (!rvh) { | 282 if (!rvh) { |
251 nacl_host_message_filter->BadMessageReceived(); // Kill the renderer. | 283 nacl_host_message_filter->BadMessageReceived(); // Kill the renderer. |
252 return; | 284 return; |
253 } | 285 } |
254 content::SiteInstance* site_instance = rvh->GetSiteInstance(); | 286 content::SiteInstance* site_instance = rvh->GetSiteInstance(); |
255 if (!content::SiteInstance::IsSameWebSite(site_instance->GetBrowserContext(), | 287 for (size_t i = 0; i < resource_urls.size(); ++i) { |
256 site_instance->GetSiteURL(), | 288 if (!content::SiteInstance::IsSameWebSite( |
257 file_url)) { | 289 site_instance->GetBrowserContext(), |
258 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 290 site_instance->GetSiteURL(), |
259 return; | 291 resource_urls[i])) { |
292 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | |
293 return; | |
294 } | |
260 } | 295 } |
261 | 296 |
262 // The URL is part of the current app. Now query the extension system for the | 297 // The URL is part of the current app. Now query the extension system for the |
263 // file path and convert that to a file descriptor. This should be done on a | 298 // file path and convert that to a file descriptor. This should be done on a |
264 // blocking pool thread. | 299 // blocking pool thread. |
265 if (!BrowserThread::PostBlockingPoolTask( | 300 if (!BrowserThread::PostBlockingPoolTask( |
266 FROM_HERE, | 301 FROM_HERE, |
267 base::Bind( | 302 base::Bind( |
268 &DoOpenNaClExecutableOnThreadPool, | 303 &DoOpenNaClResourcesOnThreadPool, |
269 nacl_host_message_filter, | 304 nacl_host_message_filter, |
270 file_url, | 305 resource_urls, |
271 enable_validation_caching, | 306 enable_validation_caching, |
272 reply_msg))) { | 307 reply_msg))) { |
273 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 308 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
274 } | 309 } |
275 } | 310 } |
276 | 311 |
277 } // namespace nacl_file_host | 312 } // namespace nacl_file_host |
OLD | NEW |