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 "chrome/browser/nacl_host/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_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
11 #include "base/platform_file.h" | 11 #include "base/platform_file.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "base/threading/sequenced_worker_pool.h" | 13 #include "base/threading/sequenced_worker_pool.h" |
14 #include "chrome/browser/nacl_host/nacl_host_message_filter.h" | |
15 #include "components/nacl/browser/nacl_browser.h" | 14 #include "components/nacl/browser/nacl_browser.h" |
16 #include "components/nacl/browser/nacl_browser_delegate.h" | 15 #include "components/nacl/browser/nacl_browser_delegate.h" |
| 16 #include "components/nacl/browser/nacl_host_message_filter.h" |
17 #include "components/nacl/common/nacl_host_messages.h" | 17 #include "components/nacl/common/nacl_host_messages.h" |
18 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
19 #include "content/public/browser/render_view_host.h" | 19 #include "content/public/browser/render_view_host.h" |
20 #include "content/public/browser/site_instance.h" | 20 #include "content/public/browser/site_instance.h" |
21 #include "ipc/ipc_platform_file.h" | 21 #include "ipc/ipc_platform_file.h" |
22 | 22 |
23 using content::BrowserThread; | 23 using content::BrowserThread; |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 // Force a prefix to prevent user from opening "magic" files. | 27 // Force a prefix to prevent user from opening "magic" files. |
28 const char* kExpectedFilePrefix = "pnacl_public_"; | 28 const char* kExpectedFilePrefix = "pnacl_public_"; |
29 | 29 |
30 // Restrict PNaCl file lengths to reduce likelyhood of hitting bugs | 30 // Restrict PNaCl file lengths to reduce likelyhood of hitting bugs |
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 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 bool PnaclDoOpenFile(const base::FilePath& file_to_open, | 41 bool PnaclDoOpenFile(const base::FilePath& file_to_open, |
42 base::PlatformFile* out_file) { | 42 base::PlatformFile* out_file) { |
43 base::PlatformFileError error_code; | 43 base::PlatformFileError error_code; |
44 *out_file = base::CreatePlatformFile(file_to_open, | 44 *out_file = base::CreatePlatformFile(file_to_open, |
45 base::PLATFORM_FILE_OPEN | | 45 base::PLATFORM_FILE_OPEN | |
46 base::PLATFORM_FILE_READ, | 46 base::PLATFORM_FILE_READ, |
47 NULL, | 47 NULL, |
48 &error_code); | 48 &error_code); |
49 if (error_code != base::PLATFORM_FILE_OK) { | 49 if (error_code != base::PLATFORM_FILE_OK) { |
50 return false; | 50 return false; |
51 } | 51 } |
52 return true; | 52 return true; |
53 } | 53 } |
54 | 54 |
55 void DoOpenPnaclFile( | 55 void DoOpenPnaclFile( |
56 scoped_refptr<NaClHostMessageFilter> nacl_host_message_filter, | 56 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
57 const std::string& filename, | 57 const std::string& filename, |
58 IPC::Message* reply_msg) { | 58 IPC::Message* reply_msg) { |
59 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 59 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
60 base::FilePath full_filepath; | 60 base::FilePath full_filepath; |
61 | 61 |
62 // PNaCl must be installed. | 62 // PNaCl must be installed. |
63 base::FilePath pnacl_dir; | 63 base::FilePath pnacl_dir; |
64 if (!nacl::NaClBrowser::GetDelegate()->GetPnaclDirectory(&pnacl_dir) || | 64 if (!nacl::NaClBrowser::GetDelegate()->GetPnaclDirectory(&pnacl_dir) || |
65 !base::PathExists(pnacl_dir)) { | 65 !base::PathExists(pnacl_dir)) { |
66 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 66 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
(...skipping 21 matching lines...) Expand all Loading... |
88 if (target_desc == IPC::InvalidPlatformFileForTransit()) { | 88 if (target_desc == IPC::InvalidPlatformFileForTransit()) { |
89 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 89 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
90 return; | 90 return; |
91 } | 91 } |
92 NaClHostMsg_GetReadonlyPnaclFD::WriteReplyParams( | 92 NaClHostMsg_GetReadonlyPnaclFD::WriteReplyParams( |
93 reply_msg, target_desc); | 93 reply_msg, target_desc); |
94 nacl_host_message_filter->Send(reply_msg); | 94 nacl_host_message_filter->Send(reply_msg); |
95 } | 95 } |
96 | 96 |
97 void DoRegisterOpenedNaClExecutableFile( | 97 void DoRegisterOpenedNaClExecutableFile( |
98 scoped_refptr<NaClHostMessageFilter> nacl_host_message_filter, | 98 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
99 base::PlatformFile file, | 99 base::PlatformFile file, |
100 base::FilePath file_path, | 100 base::FilePath file_path, |
101 IPC::Message* reply_msg) { | 101 IPC::Message* reply_msg) { |
102 // IO thread owns the NaClBrowser singleton. | 102 // IO thread owns the NaClBrowser singleton. |
103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
104 | 104 |
105 nacl::NaClBrowser* nacl_browser = nacl::NaClBrowser::GetInstance(); | 105 nacl::NaClBrowser* nacl_browser = nacl::NaClBrowser::GetInstance(); |
106 uint64 file_token_lo = 0; | 106 uint64 file_token_lo = 0; |
107 uint64 file_token_hi = 0; | 107 uint64 file_token_hi = 0; |
108 nacl_browser->PutFilePath(file_path, &file_token_lo, &file_token_hi); | 108 nacl_browser->PutFilePath(file_path, &file_token_lo, &file_token_hi); |
109 | 109 |
110 IPC::PlatformFileForTransit file_desc = IPC::GetFileHandleForProcess( | 110 IPC::PlatformFileForTransit file_desc = IPC::GetFileHandleForProcess( |
111 file, | 111 file, |
112 nacl_host_message_filter->PeerHandle(), | 112 nacl_host_message_filter->PeerHandle(), |
113 true /* close_source */); | 113 true /* close_source */); |
114 | 114 |
115 NaClHostMsg_OpenNaClExecutable::WriteReplyParams( | 115 NaClHostMsg_OpenNaClExecutable::WriteReplyParams( |
116 reply_msg, file_desc, file_token_lo, file_token_hi); | 116 reply_msg, file_desc, file_token_lo, file_token_hi); |
117 nacl_host_message_filter->Send(reply_msg); | 117 nacl_host_message_filter->Send(reply_msg); |
118 } | 118 } |
119 | 119 |
120 // Convert the file URL into a file descriptor. | 120 // Convert the file URL into a file descriptor. |
121 // This function is security sensitive. Be sure to check with a security | 121 // This function is security sensitive. Be sure to check with a security |
122 // person before you modify it. | 122 // person before you modify it. |
123 void DoOpenNaClExecutableOnThreadPool( | 123 void DoOpenNaClExecutableOnThreadPool( |
124 scoped_refptr<NaClHostMessageFilter> nacl_host_message_filter, | 124 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
125 const GURL& file_url, | 125 const GURL& file_url, |
126 IPC::Message* reply_msg) { | 126 IPC::Message* reply_msg) { |
127 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 127 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
128 | 128 |
129 base::FilePath file_path; | 129 base::FilePath file_path; |
130 if (!nacl::NaClBrowser::GetDelegate()->MapUrlToLocalFilePath( | 130 if (!nacl::NaClBrowser::GetDelegate()->MapUrlToLocalFilePath( |
131 file_url, true /* use_blocking_api */, &file_path)) { | 131 file_url, true /* use_blocking_api */, &file_path)) { |
132 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 132 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
133 return; | 133 return; |
134 } | 134 } |
(...skipping 13 matching lines...) Expand all Loading... |
148 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 148 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
149 return; | 149 return; |
150 } | 150 } |
151 } | 151 } |
152 | 152 |
153 } // namespace | 153 } // namespace |
154 | 154 |
155 namespace nacl_file_host { | 155 namespace nacl_file_host { |
156 | 156 |
157 void GetReadonlyPnaclFd( | 157 void GetReadonlyPnaclFd( |
158 scoped_refptr<NaClHostMessageFilter> nacl_host_message_filter, | 158 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
159 const std::string& filename, | 159 const std::string& filename, |
160 IPC::Message* reply_msg) { | 160 IPC::Message* reply_msg) { |
161 if (!BrowserThread::PostBlockingPoolTask( | 161 if (!BrowserThread::PostBlockingPoolTask( |
162 FROM_HERE, | 162 FROM_HERE, |
163 base::Bind(&DoOpenPnaclFile, | 163 base::Bind(&DoOpenPnaclFile, |
164 nacl_host_message_filter, | 164 nacl_host_message_filter, |
165 filename, | 165 filename, |
166 reply_msg))) { | 166 reply_msg))) { |
167 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 167 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
168 } | 168 } |
(...skipping 26 matching lines...) Expand all Loading... |
195 return false; | 195 return false; |
196 | 196 |
197 // Prepend the prefix to restrict files to a whitelisted set. | 197 // Prepend the prefix to restrict files to a whitelisted set. |
198 base::FilePath full_path = pnacl_dir.AppendASCII( | 198 base::FilePath full_path = pnacl_dir.AppendASCII( |
199 std::string(kExpectedFilePrefix) + filename); | 199 std::string(kExpectedFilePrefix) + filename); |
200 *file_to_open = full_path; | 200 *file_to_open = full_path; |
201 return true; | 201 return true; |
202 } | 202 } |
203 | 203 |
204 void OpenNaClExecutable( | 204 void OpenNaClExecutable( |
205 scoped_refptr<NaClHostMessageFilter> nacl_host_message_filter, | 205 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
206 int render_view_id, | 206 int render_view_id, |
207 const GURL& file_url, | 207 const GURL& file_url, |
208 IPC::Message* reply_msg) { | 208 IPC::Message* reply_msg) { |
209 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 209 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
210 BrowserThread::PostTask( | 210 BrowserThread::PostTask( |
211 BrowserThread::UI, FROM_HERE, | 211 BrowserThread::UI, FROM_HERE, |
212 base::Bind( | 212 base::Bind( |
213 &OpenNaClExecutable, | 213 &OpenNaClExecutable, |
214 nacl_host_message_filter, | 214 nacl_host_message_filter, |
215 render_view_id, file_url, reply_msg)); | 215 render_view_id, file_url, reply_msg)); |
(...skipping 24 matching lines...) Expand all Loading... |
240 FROM_HERE, | 240 FROM_HERE, |
241 base::Bind( | 241 base::Bind( |
242 &DoOpenNaClExecutableOnThreadPool, | 242 &DoOpenNaClExecutableOnThreadPool, |
243 nacl_host_message_filter, | 243 nacl_host_message_filter, |
244 file_url, reply_msg))) { | 244 file_url, reply_msg))) { |
245 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 245 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
246 } | 246 } |
247 } | 247 } |
248 | 248 |
249 } // namespace nacl_file_host | 249 } // namespace nacl_file_host |
OLD | NEW |