| 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/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 // in file name limit error-handling-code-paths, etc. | 31 // in file name limit error-handling-code-paths, etc. |
| 32 const size_t kMaxFileLength = 40; | 32 const size_t kMaxFileLength = 40; |
| 33 | 33 |
| 34 void NotifyRendererOfError( | 34 void NotifyRendererOfError( |
| 35 nacl::NaClHostMessageFilter* nacl_host_message_filter, | 35 nacl::NaClHostMessageFilter* nacl_host_message_filter, |
| 36 IPC::Message* reply_msg) { | 36 IPC::Message* reply_msg) { |
| 37 reply_msg->set_reply_error(); | 37 reply_msg->set_reply_error(); |
| 38 nacl_host_message_filter->Send(reply_msg); | 38 nacl_host_message_filter->Send(reply_msg); |
| 39 } | 39 } |
| 40 | 40 |
| 41 base::File PnaclDoOpenFile(const base::FilePath& file_to_open) { | 41 // Make a wrapper function for the NaClHostMsg_GetReadonlyPnaclFD macro, |
| 42 return base::File(file_to_open, | 42 // so that there is a function pointer. |
| 43 base::File::FLAG_OPEN | base::File::FLAG_READ); | 43 void WriteGetReadonlyPnaclFDReply(IPC::Message* reply_msg, |
| 44 IPC::PlatformFileForTransit file_desc, |
| 45 uint64 file_token_lo, |
| 46 uint64 file_token_hi) { |
| 47 NaClHostMsg_GetReadonlyPnaclFD::WriteReplyParams(reply_msg, |
| 48 file_desc, |
| 49 file_token_lo, |
| 50 file_token_hi); |
| 51 } |
| 52 |
| 53 // Make a wrapper function for the NaClHostMsg_OpenNaClExecutable macro, |
| 54 // so that there is a function pointer. |
| 55 void WriteOpenNaClExecutableReply(IPC::Message* reply_msg, |
| 56 IPC::PlatformFileForTransit file_desc, |
| 57 uint64 file_token_lo, |
| 58 uint64 file_token_hi) { |
| 59 NaClHostMsg_OpenNaClExecutable::WriteReplyParams(reply_msg, |
| 60 file_desc, |
| 61 file_token_lo, |
| 62 file_token_hi); |
| 63 } |
| 64 |
| 65 void DoRegisterOpenedNaClExecutableFile( |
| 66 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
| 67 base::File file, |
| 68 base::FilePath file_path, |
| 69 IPC::Message* reply_msg, |
| 70 void (*WriteReplyParams)(IPC::Message* msg, |
| 71 IPC::PlatformFileForTransit desc, |
| 72 uint64 lo_token, |
| 73 uint64 hi_token)) { |
| 74 // IO thread owns the NaClBrowser singleton. |
| 75 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 76 |
| 77 nacl::NaClBrowser* nacl_browser = nacl::NaClBrowser::GetInstance(); |
| 78 uint64 file_token_lo = 0; |
| 79 uint64 file_token_hi = 0; |
| 80 nacl_browser->PutFilePath(file_path, &file_token_lo, &file_token_hi); |
| 81 |
| 82 IPC::PlatformFileForTransit file_desc = IPC::TakeFileHandleForProcess( |
| 83 file.Pass(), |
| 84 nacl_host_message_filter->PeerHandle()); |
| 85 |
| 86 (*WriteReplyParams)(reply_msg, file_desc, file_token_lo, file_token_hi); |
| 87 nacl_host_message_filter->Send(reply_msg); |
| 44 } | 88 } |
| 45 | 89 |
| 46 void DoOpenPnaclFile( | 90 void DoOpenPnaclFile( |
| 47 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, | 91 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
| 48 const std::string& filename, | 92 const std::string& filename, |
| 93 bool is_executable, |
| 49 IPC::Message* reply_msg) { | 94 IPC::Message* reply_msg) { |
| 50 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 95 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 51 base::FilePath full_filepath; | 96 base::FilePath full_filepath; |
| 52 | 97 |
| 53 // PNaCl must be installed. | 98 // PNaCl must be installed. |
| 54 base::FilePath pnacl_dir; | 99 base::FilePath pnacl_dir; |
| 55 if (!nacl::NaClBrowser::GetDelegate()->GetPnaclDirectory(&pnacl_dir) || | 100 if (!nacl::NaClBrowser::GetDelegate()->GetPnaclDirectory(&pnacl_dir) || |
| 56 !base::PathExists(pnacl_dir)) { | 101 !base::PathExists(pnacl_dir)) { |
| 57 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 102 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
| 58 return; | 103 return; |
| 59 } | 104 } |
| 60 | 105 |
| 61 // Do some validation. | 106 // Do some validation. |
| 62 if (!nacl_file_host::PnaclCanOpenFile(filename, &full_filepath)) { | 107 if (!nacl_file_host::PnaclCanOpenFile(filename, &full_filepath)) { |
| 63 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 108 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
| 64 return; | 109 return; |
| 65 } | 110 } |
| 66 | 111 |
| 67 base::File file_to_open = PnaclDoOpenFile(full_filepath); | 112 base::File file_to_open = nacl::OpenNaClReadExecImpl(full_filepath, |
| 113 is_executable); |
| 68 if (!file_to_open.IsValid()) { | 114 if (!file_to_open.IsValid()) { |
| 69 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 115 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
| 70 return; | 116 return; |
| 71 } | 117 } |
| 72 | 118 |
| 73 // Send the reply! | 119 // This function is running on the blocking pool, but the path needs to be |
| 74 // Do any DuplicateHandle magic that is necessary first. | 120 // registered in a structure owned by the IO thread. |
| 75 IPC::PlatformFileForTransit target_desc = | 121 // Not all PNaCl files are executable. Only register those that are |
| 76 IPC::TakeFileHandleForProcess(file_to_open.Pass(), | 122 // executable in the NaCl file_path cache. |
| 77 nacl_host_message_filter->PeerHandle()); | 123 if (is_executable) { |
| 78 if (target_desc == IPC::InvalidPlatformFileForTransit()) { | 124 BrowserThread::PostTask( |
| 79 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 125 BrowserThread::IO, FROM_HERE, |
| 80 return; | 126 base::Bind( |
| 127 &DoRegisterOpenedNaClExecutableFile, |
| 128 nacl_host_message_filter, |
| 129 Passed(file_to_open.Pass()), full_filepath, reply_msg, |
| 130 &WriteGetReadonlyPnaclFDReply)); |
| 131 } else { |
| 132 IPC::PlatformFileForTransit file_desc = IPC::TakeFileHandleForProcess( |
| 133 file_to_open.Pass(), |
| 134 nacl_host_message_filter->PeerHandle()); |
| 135 |
| 136 WriteGetReadonlyPnaclFDReply(reply_msg, file_desc, 0, 0); |
| 137 nacl_host_message_filter->Send(reply_msg); |
| 81 } | 138 } |
| 82 NaClHostMsg_GetReadonlyPnaclFD::WriteReplyParams( | |
| 83 reply_msg, target_desc); | |
| 84 nacl_host_message_filter->Send(reply_msg); | |
| 85 } | |
| 86 | |
| 87 void DoRegisterOpenedNaClExecutableFile( | |
| 88 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, | |
| 89 base::File file, | |
| 90 base::FilePath file_path, | |
| 91 IPC::Message* reply_msg) { | |
| 92 // IO thread owns the NaClBrowser singleton. | |
| 93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 94 | |
| 95 nacl::NaClBrowser* nacl_browser = nacl::NaClBrowser::GetInstance(); | |
| 96 uint64 file_token_lo = 0; | |
| 97 uint64 file_token_hi = 0; | |
| 98 nacl_browser->PutFilePath(file_path, &file_token_lo, &file_token_hi); | |
| 99 | |
| 100 IPC::PlatformFileForTransit file_desc = IPC::TakeFileHandleForProcess( | |
| 101 file.Pass(), | |
| 102 nacl_host_message_filter->PeerHandle()); | |
| 103 | |
| 104 NaClHostMsg_OpenNaClExecutable::WriteReplyParams( | |
| 105 reply_msg, file_desc, file_token_lo, file_token_hi); | |
| 106 nacl_host_message_filter->Send(reply_msg); | |
| 107 } | 139 } |
| 108 | 140 |
| 109 // Convert the file URL into a file descriptor. | 141 // Convert the file URL into a file descriptor. |
| 110 // 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 |
| 111 // person before you modify it. | 143 // person before you modify it. |
| 112 void DoOpenNaClExecutableOnThreadPool( | 144 void DoOpenNaClExecutableOnThreadPool( |
| 113 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, | 145 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
| 114 const GURL& file_url, | 146 const GURL& file_url, |
| 115 IPC::Message* reply_msg) { | 147 IPC::Message* reply_msg) { |
| 116 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 148 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 117 | 149 |
| 118 base::FilePath file_path; | 150 base::FilePath file_path; |
| 119 if (!nacl::NaClBrowser::GetDelegate()->MapUrlToLocalFilePath( | 151 if (!nacl::NaClBrowser::GetDelegate()->MapUrlToLocalFilePath( |
| 120 file_url, true /* use_blocking_api */, &file_path)) { | 152 file_url, true /* use_blocking_api */, &file_path)) { |
| 121 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 153 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
| 122 return; | 154 return; |
| 123 } | 155 } |
| 124 | 156 |
| 125 base::File file = nacl::OpenNaClExecutableImpl(file_path); | 157 base::File file = nacl::OpenNaClReadExecImpl(file_path, true); |
| 126 if (file.IsValid()) { | 158 if (file.IsValid()) { |
| 127 // This function is running on the blocking pool, but the path needs to be | 159 // This function is running on the blocking pool, but the path needs to be |
| 128 // registered in a structure owned by the IO thread. | 160 // registered in a structure owned by the IO thread. |
| 129 BrowserThread::PostTask( | 161 BrowserThread::PostTask( |
| 130 BrowserThread::IO, FROM_HERE, | 162 BrowserThread::IO, FROM_HERE, |
| 131 base::Bind( | 163 base::Bind( |
| 132 &DoRegisterOpenedNaClExecutableFile, | 164 &DoRegisterOpenedNaClExecutableFile, |
| 133 nacl_host_message_filter, | 165 nacl_host_message_filter, |
| 134 Passed(file.Pass()), file_path, reply_msg)); | 166 Passed(file.Pass()), file_path, reply_msg, |
| 167 &WriteOpenNaClExecutableReply)); |
| 135 } else { | 168 } else { |
| 136 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 169 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
| 137 return; | 170 return; |
| 138 } | 171 } |
| 139 } | 172 } |
| 140 | 173 |
| 141 } // namespace | 174 } // namespace |
| 142 | 175 |
| 143 namespace nacl_file_host { | 176 namespace nacl_file_host { |
| 144 | 177 |
| 145 void GetReadonlyPnaclFd( | 178 void GetReadonlyPnaclFd( |
| 146 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, | 179 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
| 147 const std::string& filename, | 180 const std::string& filename, |
| 181 bool is_executable, |
| 148 IPC::Message* reply_msg) { | 182 IPC::Message* reply_msg) { |
| 149 if (!BrowserThread::PostBlockingPoolTask( | 183 if (!BrowserThread::PostBlockingPoolTask( |
| 150 FROM_HERE, | 184 FROM_HERE, |
| 151 base::Bind(&DoOpenPnaclFile, | 185 base::Bind(&DoOpenPnaclFile, |
| 152 nacl_host_message_filter, | 186 nacl_host_message_filter, |
| 153 filename, | 187 filename, |
| 188 is_executable, |
| 154 reply_msg))) { | 189 reply_msg))) { |
| 155 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 190 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
| 156 } | 191 } |
| 157 } | 192 } |
| 158 | 193 |
| 159 // This function is security sensitive. Be sure to check with a security | 194 // This function is security sensitive. Be sure to check with a security |
| 160 // person before you modify it. | 195 // person before you modify it. |
| 161 bool PnaclCanOpenFile(const std::string& filename, | 196 bool PnaclCanOpenFile(const std::string& filename, |
| 162 base::FilePath* file_to_open) { | 197 base::FilePath* file_to_open) { |
| 163 if (filename.length() > kMaxFileLength) | 198 if (filename.length() > kMaxFileLength) |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 FROM_HERE, | 263 FROM_HERE, |
| 229 base::Bind( | 264 base::Bind( |
| 230 &DoOpenNaClExecutableOnThreadPool, | 265 &DoOpenNaClExecutableOnThreadPool, |
| 231 nacl_host_message_filter, | 266 nacl_host_message_filter, |
| 232 file_url, reply_msg))) { | 267 file_url, reply_msg))) { |
| 233 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 268 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
| 234 } | 269 } |
| 235 } | 270 } |
| 236 | 271 |
| 237 } // namespace nacl_file_host | 272 } // namespace nacl_file_host |
| OLD | NEW |