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