Chromium Code Reviews| 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 |
| 41 typedef void (*WriteFileInfoReplyPnacl)(IPC::Message* reply_msg, | |
| 42 IPC::PlatformFileForTransit file_desc, | |
| 43 uint64 file_token_lo, | |
| 44 uint64 file_token_hi); | |
| 45 | |
| 46 void DoRegisterOpenedNaClExecutableFilePnacl( | |
| 47 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, | |
| 48 base::File file, | |
| 49 base::FilePath file_path, | |
| 50 IPC::Message* reply_msg, | |
| 51 WriteFileInfoReplyPnacl write_reply_message) { | |
| 52 // IO thread owns the NaClBrowser singleton. | |
| 53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 54 | |
| 55 nacl::NaClBrowser* nacl_browser = nacl::NaClBrowser::GetInstance(); | |
| 56 uint64 file_token_lo = 0; | |
| 57 uint64 file_token_hi = 0; | |
| 58 nacl_browser->PutFilePath(file_path, &file_token_lo, &file_token_hi); | |
| 59 | |
| 60 IPC::PlatformFileForTransit file_desc = IPC::TakeFileHandleForProcess( | |
| 61 file.Pass(), nacl_host_message_filter->PeerHandle()); | |
| 62 | |
| 63 write_reply_message(reply_msg, file_desc, file_token_lo, file_token_hi); | |
|
teravest
2014/11/10 20:36:11
Why is write_reply_message passed as a parameter i
Yusuke Sato
2014/11/11 00:58:35
Good catch, fixed. Thanks.
| |
| 64 nacl_host_message_filter->Send(reply_msg); | |
| 65 } | |
| 66 | |
| 40 typedef void (*WriteFileInfoReply)(IPC::Message* reply_msg, | 67 typedef void (*WriteFileInfoReply)(IPC::Message* reply_msg, |
| 41 IPC::PlatformFileForTransit file_desc, | 68 nacl::NaClOpenExecutableResult open_result); |
| 42 uint64 file_token_lo, | |
| 43 uint64 file_token_hi); | |
| 44 | 69 |
| 45 void DoRegisterOpenedNaClExecutableFile( | 70 void DoRegisterOpenedNaClExecutableFile( |
| 46 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, | 71 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
| 47 base::File file, | 72 base::File file, |
| 48 base::FilePath file_path, | 73 base::FilePath file_path, |
| 74 scoped_ptr<base::File[]> resource_files, | |
| 75 const std::vector<base::FilePath>& resource_file_paths, | |
| 49 IPC::Message* reply_msg, | 76 IPC::Message* reply_msg, |
| 50 WriteFileInfoReply write_reply_message) { | 77 WriteFileInfoReply write_reply_message) { |
| 51 // IO thread owns the NaClBrowser singleton. | 78 // IO thread owns the NaClBrowser singleton. |
| 52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 53 | 80 |
| 54 nacl::NaClBrowser* nacl_browser = nacl::NaClBrowser::GetInstance(); | 81 nacl::NaClBrowser* nacl_browser = nacl::NaClBrowser::GetInstance(); |
| 55 uint64 file_token_lo = 0; | 82 uint64 file_token_lo = 0; |
| 56 uint64 file_token_hi = 0; | 83 uint64 file_token_hi = 0; |
| 57 nacl_browser->PutFilePath(file_path, &file_token_lo, &file_token_hi); | 84 nacl_browser->PutFilePath(file_path, &file_token_lo, &file_token_hi); |
| 58 | 85 |
| 59 IPC::PlatformFileForTransit file_desc = IPC::TakeFileHandleForProcess( | 86 IPC::PlatformFileForTransit file_desc = IPC::TakeFileHandleForProcess( |
| 60 file.Pass(), | 87 file.Pass(), |
| 61 nacl_host_message_filter->PeerHandle()); | 88 nacl_host_message_filter->PeerHandle()); |
| 89 nacl::NaClOpenExecutableResult::FileInfo nexe_file_info( | |
| 90 file_desc, file_token_lo, file_token_hi); | |
| 62 | 91 |
| 63 write_reply_message(reply_msg, file_desc, file_token_lo, file_token_hi); | 92 std::vector<nacl::NaClOpenExecutableResult::FileInfo> resource_files_info; |
| 93 for (size_t i = 0; i < resource_file_paths.size(); ++i) { | |
| 94 uint64 resource_file_token_lo = 0; | |
| 95 uint64 resource_file_token_hi = 0; | |
| 96 nacl_browser->PutFilePath(resource_file_paths[i], | |
| 97 &resource_file_token_lo, | |
| 98 &resource_file_token_hi); | |
| 99 IPC::PlatformFileForTransit resource_file = IPC::TakeFileHandleForProcess( | |
| 100 resource_files[i].Pass(), nacl_host_message_filter->PeerHandle()); | |
| 101 nacl::NaClOpenExecutableResult::FileInfo resource_file_info( | |
| 102 resource_file, resource_file_token_lo, resource_file_token_hi); | |
| 103 resource_files_info.push_back(resource_file_info); | |
| 104 } | |
| 105 | |
| 106 write_reply_message( | |
| 107 reply_msg, | |
| 108 nacl::NaClOpenExecutableResult(nexe_file_info, resource_files_info)); | |
| 64 nacl_host_message_filter->Send(reply_msg); | 109 nacl_host_message_filter->Send(reply_msg); |
| 65 } | 110 } |
| 66 | 111 |
| 67 void DoOpenPnaclFile( | 112 void DoOpenPnaclFile( |
| 68 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, | 113 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
| 69 const std::string& filename, | 114 const std::string& filename, |
| 70 bool is_executable, | 115 bool is_executable, |
| 71 IPC::Message* reply_msg) { | 116 IPC::Message* reply_msg) { |
| 72 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 117 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 73 base::FilePath full_filepath; | 118 base::FilePath full_filepath; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 93 return; | 138 return; |
| 94 } | 139 } |
| 95 | 140 |
| 96 // This function is running on the blocking pool, but the path needs to be | 141 // This function is running on the blocking pool, but the path needs to be |
| 97 // registered in a structure owned by the IO thread. | 142 // registered in a structure owned by the IO thread. |
| 98 // Not all PNaCl files are executable. Only register those that are | 143 // Not all PNaCl files are executable. Only register those that are |
| 99 // executable in the NaCl file_path cache. | 144 // executable in the NaCl file_path cache. |
| 100 if (is_executable) { | 145 if (is_executable) { |
| 101 BrowserThread::PostTask( | 146 BrowserThread::PostTask( |
| 102 BrowserThread::IO, FROM_HERE, | 147 BrowserThread::IO, FROM_HERE, |
| 103 base::Bind(&DoRegisterOpenedNaClExecutableFile, | 148 base::Bind(&DoRegisterOpenedNaClExecutableFilePnacl, |
| 104 nacl_host_message_filter, | 149 nacl_host_message_filter, |
| 105 Passed(file_to_open.Pass()), full_filepath, reply_msg, | 150 Passed(file_to_open.Pass()), full_filepath, reply_msg, |
| 106 static_cast<WriteFileInfoReply>( | 151 static_cast<WriteFileInfoReplyPnacl>( |
| 107 NaClHostMsg_GetReadonlyPnaclFD::WriteReplyParams))); | 152 NaClHostMsg_GetReadonlyPnaclFD::WriteReplyParams))); |
| 108 } else { | 153 } else { |
| 109 IPC::PlatformFileForTransit target_desc = | 154 IPC::PlatformFileForTransit target_desc = |
| 110 IPC::TakeFileHandleForProcess(file_to_open.Pass(), | 155 IPC::TakeFileHandleForProcess(file_to_open.Pass(), |
| 111 nacl_host_message_filter->PeerHandle()); | 156 nacl_host_message_filter->PeerHandle()); |
| 112 uint64_t dummy_file_token = 0; | 157 uint64_t dummy_file_token = 0; |
| 113 NaClHostMsg_GetReadonlyPnaclFD::WriteReplyParams( | 158 NaClHostMsg_GetReadonlyPnaclFD::WriteReplyParams( |
| 114 reply_msg, target_desc, dummy_file_token, dummy_file_token); | 159 reply_msg, target_desc, dummy_file_token, dummy_file_token); |
| 115 nacl_host_message_filter->Send(reply_msg); | 160 nacl_host_message_filter->Send(reply_msg); |
| 116 } | 161 } |
| 117 } | 162 } |
| 118 | 163 |
| 119 // Convert the file URL into a file descriptor. | 164 // Convert the file URL into a file descriptor. |
| 120 // This function is security sensitive. Be sure to check with a security | 165 // This function is security sensitive. Be sure to check with a security |
| 121 // person before you modify it. | 166 // person before you modify it. |
| 122 void DoOpenNaClExecutableOnThreadPool( | 167 void DoOpenNaClResourcesOnThreadPool( |
| 123 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, | 168 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
| 124 const GURL& file_url, | 169 const GURL& executable_file_url, |
| 170 const std::vector<GURL>& resource_urls, | |
| 125 IPC::Message* reply_msg) { | 171 IPC::Message* reply_msg) { |
| 126 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 172 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 127 | 173 |
| 128 base::FilePath file_path; | 174 base::FilePath file_path; |
| 129 if (!nacl::NaClBrowser::GetDelegate()->MapUrlToLocalFilePath( | 175 if (!nacl::NaClBrowser::GetDelegate()->MapUrlToLocalFilePath( |
| 130 file_url, | 176 executable_file_url, |
| 131 true /* use_blocking_api */, | 177 true /* use_blocking_api */, |
| 132 nacl_host_message_filter->profile_directory(), | 178 nacl_host_message_filter->profile_directory(), |
| 133 &file_path)) { | 179 &file_path)) { |
| 134 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 180 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
| 135 return; | 181 return; |
| 136 } | 182 } |
| 137 | 183 |
| 138 base::File file = nacl::OpenNaClReadExecImpl(file_path, | 184 base::File file = nacl::OpenNaClReadExecImpl(file_path, |
| 139 true /* is_executable */); | 185 true /* is_executable */); |
| 140 if (file.IsValid()) { | 186 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); | 187 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
| 153 return; | 188 return; |
| 154 } | 189 } |
| 190 | |
| 191 scoped_ptr<base::File[]> resource_files; | |
| 192 if (!resource_urls.empty()) | |
| 193 resource_files.reset(new base::File[resource_urls.size()]); | |
| 194 std::vector<base::FilePath> resource_file_paths(resource_urls.size()); | |
| 195 | |
| 196 for (size_t i = 0; i < resource_urls.size(); ++i) { | |
| 197 if (!nacl::NaClBrowser::GetDelegate()->MapUrlToLocalFilePath( | |
| 198 resource_urls[i], | |
| 199 true /* use_blocking_api */, | |
| 200 nacl_host_message_filter->profile_directory(), | |
| 201 &resource_file_paths[i])) { | |
| 202 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | |
| 203 return; | |
| 204 } | |
| 205 resource_files[i] = nacl::OpenNaClReadExecImpl(resource_file_paths[i], | |
| 206 true /* is_executable */); | |
| 207 if (!resource_files[i].IsValid()) { | |
| 208 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | |
| 209 return; | |
| 210 } | |
| 211 } | |
| 212 | |
| 213 // This function is running on the blocking pool, but the path needs to be | |
| 214 // registered in a structure owned by the IO thread. | |
| 215 BrowserThread::PostTask( | |
| 216 BrowserThread::IO, | |
| 217 FROM_HERE, | |
| 218 base::Bind(&DoRegisterOpenedNaClExecutableFile, | |
| 219 nacl_host_message_filter, | |
| 220 Passed(file.Pass()), | |
| 221 file_path, | |
| 222 Passed(resource_files.Pass()), | |
| 223 resource_file_paths, | |
| 224 reply_msg, | |
| 225 static_cast<WriteFileInfoReply>( | |
| 226 NaClHostMsg_OpenNaClResources::WriteReplyParams))); | |
| 155 } | 227 } |
| 156 | 228 |
| 157 } // namespace | 229 } // namespace |
| 158 | 230 |
| 159 namespace nacl_file_host { | 231 namespace nacl_file_host { |
| 160 | 232 |
| 161 void GetReadonlyPnaclFd( | 233 void GetReadonlyPnaclFd( |
| 162 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, | 234 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
| 163 const std::string& filename, | 235 const std::string& filename, |
| 164 bool is_executable, | 236 bool is_executable, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 200 pnacl_dir.empty()) | 272 pnacl_dir.empty()) |
| 201 return false; | 273 return false; |
| 202 | 274 |
| 203 // Prepend the prefix to restrict files to a whitelisted set. | 275 // Prepend the prefix to restrict files to a whitelisted set. |
| 204 base::FilePath full_path = pnacl_dir.AppendASCII( | 276 base::FilePath full_path = pnacl_dir.AppendASCII( |
| 205 std::string(kExpectedFilePrefix) + filename); | 277 std::string(kExpectedFilePrefix) + filename); |
| 206 *file_to_open = full_path; | 278 *file_to_open = full_path; |
| 207 return true; | 279 return true; |
| 208 } | 280 } |
| 209 | 281 |
| 210 void OpenNaClExecutable( | 282 void OpenNaClResources( |
| 211 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, | 283 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
| 212 int render_view_id, | 284 int render_view_id, |
| 213 const GURL& file_url, | 285 const GURL& executable_file_url, |
| 286 const std::vector<GURL>& resource_urls, | |
| 214 IPC::Message* reply_msg) { | 287 IPC::Message* reply_msg) { |
| 215 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 288 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 216 BrowserThread::PostTask( | 289 BrowserThread::PostTask( |
| 217 BrowserThread::UI, FROM_HERE, | 290 BrowserThread::UI, FROM_HERE, |
| 218 base::Bind( | 291 base::Bind( |
| 219 &OpenNaClExecutable, | 292 &OpenNaClResources, |
| 220 nacl_host_message_filter, | 293 nacl_host_message_filter, |
| 221 render_view_id, file_url, reply_msg)); | 294 render_view_id, executable_file_url, resource_urls, reply_msg)); |
| 222 return; | 295 return; |
| 223 } | 296 } |
| 224 | 297 |
| 225 // Make sure render_view_id is valid and that the URL is a part of the | 298 // 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 | 299 // render view's site. Without these checks, apps could probe the extension |
| 227 // directory or run NaCl code from other extensions. | 300 // directory or run NaCl code from other extensions. |
| 228 content::RenderViewHost* rvh = content::RenderViewHost::FromID( | 301 content::RenderViewHost* rvh = content::RenderViewHost::FromID( |
| 229 nacl_host_message_filter->render_process_id(), render_view_id); | 302 nacl_host_message_filter->render_process_id(), render_view_id); |
| 230 if (!rvh) { | 303 if (!rvh) { |
| 231 nacl_host_message_filter->BadMessageReceived(); // Kill the renderer. | 304 nacl_host_message_filter->BadMessageReceived(); // Kill the renderer. |
| 232 return; | 305 return; |
| 233 } | 306 } |
| 234 content::SiteInstance* site_instance = rvh->GetSiteInstance(); | 307 content::SiteInstance* site_instance = rvh->GetSiteInstance(); |
| 235 if (!content::SiteInstance::IsSameWebSite(site_instance->GetBrowserContext(), | 308 if (!content::SiteInstance::IsSameWebSite(site_instance->GetBrowserContext(), |
| 236 site_instance->GetSiteURL(), | 309 site_instance->GetSiteURL(), |
| 237 file_url)) { | 310 executable_file_url)) { |
| 238 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 311 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
| 239 return; | 312 return; |
| 240 } | 313 } |
| 241 | 314 |
| 315 for (size_t i = 0; i < resource_urls.size(); ++i) { | |
| 316 if (!content::SiteInstance::IsSameWebSite( | |
| 317 site_instance->GetBrowserContext(), | |
| 318 site_instance->GetSiteURL(), | |
| 319 resource_urls[i])) { | |
| 320 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | |
| 321 return; | |
| 322 } | |
| 323 } | |
| 324 | |
| 242 // The URL is part of the current app. Now query the extension system for the | 325 // 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 | 326 // file path and convert that to a file descriptor. This should be done on a |
| 244 // blocking pool thread. | 327 // blocking pool thread. |
| 245 if (!BrowserThread::PostBlockingPoolTask( | 328 if (!BrowserThread::PostBlockingPoolTask( |
| 246 FROM_HERE, | 329 FROM_HERE, |
| 247 base::Bind( | 330 base::Bind( |
| 248 &DoOpenNaClExecutableOnThreadPool, | 331 &DoOpenNaClResourcesOnThreadPool, |
| 249 nacl_host_message_filter, | 332 nacl_host_message_filter, |
| 250 file_url, reply_msg))) { | 333 executable_file_url, resource_urls, reply_msg))) { |
| 251 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 334 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
| 252 } | 335 } |
| 253 } | 336 } |
| 254 | 337 |
| 255 } // namespace nacl_file_host | 338 } // namespace nacl_file_host |
| OLD | NEW |