Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(453)

Side by Side Diff: chrome/browser/nacl_host/nacl_file_host.cc

Issue 15906013: Separate NaCl messages from the rest of chrome messages and create a new message filter. This is pa… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "chrome/browser/nacl_host/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/extensions/extension_info_map.h" 14 #include "chrome/browser/extensions/extension_info_map.h"
15 #include "chrome/browser/nacl_host/nacl_browser.h" 15 #include "chrome/browser/nacl_host/nacl_browser.h"
16 #include "chrome/browser/renderer_host/chrome_render_message_filter.h" 16 #include "chrome/browser/nacl_host/nacl_host_message_filter.h"
17 #include "chrome/common/chrome_paths.h" 17 #include "chrome/common/chrome_paths.h"
18 #include "chrome/common/extensions/manifest_handlers/shared_module_info.h" 18 #include "chrome/common/extensions/manifest_handlers/shared_module_info.h"
19 #include "chrome/common/render_messages.h" 19 #include "chrome/common/nacl_host_messages.h"
20 #include "content/public/browser/browser_thread.h" 20 #include "content/public/browser/browser_thread.h"
21 #include "content/public/browser/render_view_host.h" 21 #include "content/public/browser/render_view_host.h"
22 #include "content/public/browser/site_instance.h" 22 #include "content/public/browser/site_instance.h"
23 #include "ipc/ipc_platform_file.h" 23 #include "ipc/ipc_platform_file.h"
24 24
25 using content::BrowserThread; 25 using content::BrowserThread;
26 using extensions::SharedModuleInfo; 26 using extensions::SharedModuleInfo;
27 27
28 namespace { 28 namespace {
29 29
30 // Force a prefix to prevent user from opening "magic" files. 30 // Force a prefix to prevent user from opening "magic" files.
31 const char* kExpectedFilePrefix = "pnacl_public_"; 31 const char* kExpectedFilePrefix = "pnacl_public_";
32 32
33 // Restrict PNaCl file lengths to reduce likelyhood of hitting bugs 33 // Restrict PNaCl file lengths to reduce likelyhood of hitting bugs
34 // in file name limit error-handling-code-paths, etc. 34 // in file name limit error-handling-code-paths, etc.
35 const size_t kMaxFileLength = 40; 35 const size_t kMaxFileLength = 40;
36 36
37 void NotifyRendererOfError( 37 void NotifyRendererOfError(
38 ChromeRenderMessageFilter* chrome_render_message_filter, 38 NaClHostMessageFilter* nacl_host_message_filter,
39 IPC::Message* reply_msg) { 39 IPC::Message* reply_msg) {
40 reply_msg->set_reply_error(); 40 reply_msg->set_reply_error();
41 chrome_render_message_filter->Send(reply_msg); 41 nacl_host_message_filter->Send(reply_msg);
42 } 42 }
43 43
44 bool PnaclDoOpenFile(const base::FilePath& file_to_open, 44 bool PnaclDoOpenFile(const base::FilePath& file_to_open,
45 base::PlatformFile* out_file) { 45 base::PlatformFile* out_file) {
46 base::PlatformFileError error_code; 46 base::PlatformFileError error_code;
47 *out_file = base::CreatePlatformFile(file_to_open, 47 *out_file = base::CreatePlatformFile(file_to_open,
48 base::PLATFORM_FILE_OPEN | 48 base::PLATFORM_FILE_OPEN |
49 base::PLATFORM_FILE_READ, 49 base::PLATFORM_FILE_READ,
50 NULL, 50 NULL,
51 &error_code); 51 &error_code);
52 if (error_code != base::PLATFORM_FILE_OK) { 52 if (error_code != base::PLATFORM_FILE_OK) {
53 return false; 53 return false;
54 } 54 }
55 return true; 55 return true;
56 } 56 }
57 57
58 void DoOpenPnaclFile( 58 void DoOpenPnaclFile(
59 scoped_refptr<ChromeRenderMessageFilter> chrome_render_message_filter, 59 scoped_refptr<NaClHostMessageFilter> nacl_host_message_filter,
60 const std::string& filename, 60 const std::string& filename,
61 IPC::Message* reply_msg) { 61 IPC::Message* reply_msg) {
62 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); 62 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
63 base::FilePath full_filepath; 63 base::FilePath full_filepath;
64 64
65 // Do some validation. 65 // Do some validation.
66 if (!nacl_file_host::PnaclCanOpenFile(filename, &full_filepath)) { 66 if (!nacl_file_host::PnaclCanOpenFile(filename, &full_filepath)) {
67 NotifyRendererOfError(chrome_render_message_filter.get(), reply_msg); 67 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
68 return; 68 return;
69 } 69 }
70 70
71 base::PlatformFile file_to_open; 71 base::PlatformFile file_to_open;
72 if (!PnaclDoOpenFile(full_filepath, &file_to_open)) { 72 if (!PnaclDoOpenFile(full_filepath, &file_to_open)) {
73 NotifyRendererOfError(chrome_render_message_filter.get(), reply_msg); 73 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
74 return; 74 return;
75 } 75 }
76 76
77 // Send the reply! 77 // Send the reply!
78 // Do any DuplicateHandle magic that is necessary first. 78 // Do any DuplicateHandle magic that is necessary first.
79 IPC::PlatformFileForTransit target_desc = 79 IPC::PlatformFileForTransit target_desc =
80 IPC::GetFileHandleForProcess(file_to_open, 80 IPC::GetFileHandleForProcess(file_to_open,
81 chrome_render_message_filter->peer_handle(), 81 nacl_host_message_filter->peer_handle(),
82 true /* Close source */); 82 true /* Close source */);
83 if (target_desc == IPC::InvalidPlatformFileForTransit()) { 83 if (target_desc == IPC::InvalidPlatformFileForTransit()) {
84 NotifyRendererOfError(chrome_render_message_filter.get(), reply_msg); 84 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
85 return; 85 return;
86 } 86 }
87 ChromeViewHostMsg_GetReadonlyPnaclFD::WriteReplyParams( 87 NaClHostMsg_GetReadonlyPnaclFD::WriteReplyParams(
88 reply_msg, target_desc); 88 reply_msg, target_desc);
89 chrome_render_message_filter->Send(reply_msg); 89 nacl_host_message_filter->Send(reply_msg);
90 } 90 }
91 91
92 void DoCreateTemporaryFile( 92 void DoCreateTemporaryFile(
93 scoped_refptr<ChromeRenderMessageFilter> chrome_render_message_filter, 93 scoped_refptr<NaClHostMessageFilter> nacl_host_message_filter,
94 IPC::Message* reply_msg) { 94 IPC::Message* reply_msg) {
95 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); 95 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
96 96
97 base::FilePath file_path; 97 base::FilePath file_path;
98 if (!file_util::CreateTemporaryFile(&file_path)) { 98 if (!file_util::CreateTemporaryFile(&file_path)) {
99 NotifyRendererOfError(chrome_render_message_filter.get(), reply_msg); 99 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
100 return; 100 return;
101 } 101 }
102 102
103 base::PlatformFileError error; 103 base::PlatformFileError error;
104 base::PlatformFile file_handle = base::CreatePlatformFile( 104 base::PlatformFile file_handle = base::CreatePlatformFile(
105 file_path, 105 file_path,
106 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_READ | 106 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_READ |
107 base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_TEMPORARY | 107 base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_TEMPORARY |
108 base::PLATFORM_FILE_DELETE_ON_CLOSE, 108 base::PLATFORM_FILE_DELETE_ON_CLOSE,
109 NULL, &error); 109 NULL, &error);
110 110
111 if (error != base::PLATFORM_FILE_OK) { 111 if (error != base::PLATFORM_FILE_OK) {
112 NotifyRendererOfError(chrome_render_message_filter.get(), reply_msg); 112 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
113 return; 113 return;
114 } 114 }
115 115
116 // Send the reply! 116 // Send the reply!
117 // Do any DuplicateHandle magic that is necessary first. 117 // Do any DuplicateHandle magic that is necessary first.
118 IPC::PlatformFileForTransit target_desc = 118 IPC::PlatformFileForTransit target_desc =
119 IPC::GetFileHandleForProcess(file_handle, 119 IPC::GetFileHandleForProcess(file_handle,
120 chrome_render_message_filter->peer_handle(), 120 nacl_host_message_filter->peer_handle(),
121 true); 121 true);
122 if (target_desc == IPC::InvalidPlatformFileForTransit()) { 122 if (target_desc == IPC::InvalidPlatformFileForTransit()) {
123 NotifyRendererOfError(chrome_render_message_filter.get(), reply_msg); 123 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
124 return; 124 return;
125 } 125 }
126 126
127 ChromeViewHostMsg_NaClCreateTemporaryFile::WriteReplyParams( 127 NaClHostMsg_NaClCreateTemporaryFile::WriteReplyParams(
128 reply_msg, target_desc); 128 reply_msg, target_desc);
129 chrome_render_message_filter->Send(reply_msg); 129 nacl_host_message_filter->Send(reply_msg);
130 } 130 }
131 131
132 void DoRegisterOpenedNaClExecutableFile( 132 void DoRegisterOpenedNaClExecutableFile(
133 scoped_refptr<ChromeRenderMessageFilter> chrome_render_message_filter, 133 scoped_refptr<NaClHostMessageFilter> nacl_host_message_filter,
134 base::PlatformFile file, 134 base::PlatformFile file,
135 base::FilePath file_path, 135 base::FilePath file_path,
136 IPC::Message* reply_msg) { 136 IPC::Message* reply_msg) {
137 // IO thread owns the NaClBrowser singleton. 137 // IO thread owns the NaClBrowser singleton.
138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
139 139
140 NaClBrowser* nacl_browser = NaClBrowser::GetInstance(); 140 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
141 uint64_t file_token_lo = 0; 141 uint64 file_token_lo = 0;
142 uint64_t file_token_hi = 0; 142 uint64 file_token_hi = 0;
143 nacl_browser->PutFilePath(file_path, &file_token_lo, &file_token_hi); 143 nacl_browser->PutFilePath(file_path, &file_token_lo, &file_token_hi);
144 144
145 IPC::PlatformFileForTransit file_desc = IPC::GetFileHandleForProcess( 145 IPC::PlatformFileForTransit file_desc = IPC::GetFileHandleForProcess(
146 file, 146 file,
147 chrome_render_message_filter->peer_handle(), 147 nacl_host_message_filter->peer_handle(),
148 true /* close_source */); 148 true /* close_source */);
149 149
150 ChromeViewHostMsg_OpenNaClExecutable::WriteReplyParams( 150 NaClHostMsg_OpenNaClExecutable::WriteReplyParams(
151 reply_msg, file_desc, file_token_lo, file_token_hi); 151 reply_msg, file_desc, file_token_lo, file_token_hi);
152 chrome_render_message_filter->Send(reply_msg); 152 nacl_host_message_filter->Send(reply_msg);
153 } 153 }
154 154
155 // Convert the file URL into a file path in the extension directory. 155 // Convert the file URL into a file path in the extension directory.
156 // This function is security sensitive. Be sure to check with a security 156 // This function is security sensitive. Be sure to check with a security
157 // person before you modify it. 157 // person before you modify it.
158 bool GetExtensionFilePath( 158 bool GetExtensionFilePath(
159 scoped_refptr<ExtensionInfoMap> extension_info_map, 159 scoped_refptr<ExtensionInfoMap> extension_info_map,
160 const GURL& file_url, 160 const GURL& file_url,
161 base::FilePath* file_path) { 161 base::FilePath* file_path) {
162 // Check that the URL is recognized by the extension system. 162 // Check that the URL is recognized by the extension system.
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 return false; 199 return false;
200 200
201 *file_path = resource_file_path; 201 *file_path = resource_file_path;
202 return true; 202 return true;
203 } 203 }
204 204
205 // Convert the file URL into a file descriptor. 205 // Convert the file URL into a file descriptor.
206 // This function is security sensitive. Be sure to check with a security 206 // This function is security sensitive. Be sure to check with a security
207 // person before you modify it. 207 // person before you modify it.
208 void DoOpenNaClExecutableOnThreadPool( 208 void DoOpenNaClExecutableOnThreadPool(
209 scoped_refptr<ChromeRenderMessageFilter> chrome_render_message_filter, 209 scoped_refptr<NaClHostMessageFilter> nacl_host_message_filter,
210 scoped_refptr<ExtensionInfoMap> extension_info_map, 210 scoped_refptr<ExtensionInfoMap> extension_info_map,
211 const GURL& file_url, 211 const GURL& file_url,
212 IPC::Message* reply_msg) { 212 IPC::Message* reply_msg) {
213 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); 213 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
214 214
215 base::FilePath file_path; 215 base::FilePath file_path;
216 if (!GetExtensionFilePath(extension_info_map, file_url, &file_path)) { 216 if (!GetExtensionFilePath(extension_info_map, file_url, &file_path)) {
217 NotifyRendererOfError(chrome_render_message_filter.get(), reply_msg); 217 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
218 return; 218 return;
219 } 219 }
220 220
221 base::PlatformFile file; 221 base::PlatformFile file;
222 nacl::OpenNaClExecutableImpl(file_path, &file); 222 nacl::OpenNaClExecutableImpl(file_path, &file);
223 if (file != base::kInvalidPlatformFileValue) { 223 if (file != base::kInvalidPlatformFileValue) {
224 // This function is running on the blocking pool, but the path needs to be 224 // This function is running on the blocking pool, but the path needs to be
225 // registered in a structure owned by the IO thread. 225 // registered in a structure owned by the IO thread.
226 BrowserThread::PostTask( 226 BrowserThread::PostTask(
227 BrowserThread::IO, FROM_HERE, 227 BrowserThread::IO, FROM_HERE,
228 base::Bind( 228 base::Bind(
229 &DoRegisterOpenedNaClExecutableFile, 229 &DoRegisterOpenedNaClExecutableFile,
230 chrome_render_message_filter, 230 nacl_host_message_filter,
231 file, file_path, reply_msg)); 231 file, file_path, reply_msg));
232 } else { 232 } else {
233 NotifyRendererOfError(chrome_render_message_filter.get(), reply_msg); 233 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
234 return; 234 return;
235 } 235 }
236 } 236 }
237 237
238 } // namespace 238 } // namespace
239 239
240 namespace nacl_file_host { 240 namespace nacl_file_host {
241 241
242 void GetReadonlyPnaclFd( 242 void GetReadonlyPnaclFd(
243 scoped_refptr<ChromeRenderMessageFilter> chrome_render_message_filter, 243 scoped_refptr<NaClHostMessageFilter> nacl_host_message_filter,
244 const std::string& filename, 244 const std::string& filename,
245 IPC::Message* reply_msg) { 245 IPC::Message* reply_msg) {
246 if (!BrowserThread::PostBlockingPoolTask( 246 if (!BrowserThread::PostBlockingPoolTask(
247 FROM_HERE, 247 FROM_HERE,
248 base::Bind(&DoOpenPnaclFile, 248 base::Bind(&DoOpenPnaclFile,
249 chrome_render_message_filter, 249 nacl_host_message_filter,
250 filename, 250 filename,
251 reply_msg))) { 251 reply_msg))) {
252 NotifyRendererOfError(chrome_render_message_filter.get(), reply_msg); 252 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
253 } 253 }
254 } 254 }
255 255
256 // This function is security sensitive. Be sure to check with a security 256 // This function is security sensitive. Be sure to check with a security
257 // person before you modify it. 257 // person before you modify it.
258 bool PnaclCanOpenFile(const std::string& filename, 258 bool PnaclCanOpenFile(const std::string& filename,
259 base::FilePath* file_to_open) { 259 base::FilePath* file_to_open) {
260 if (filename.length() > kMaxFileLength) 260 if (filename.length() > kMaxFileLength)
261 return false; 261 return false;
262 262
(...skipping 17 matching lines...) Expand all
280 return false; 280 return false;
281 281
282 // Prepend the prefix to restrict files to a whitelisted set. 282 // Prepend the prefix to restrict files to a whitelisted set.
283 base::FilePath full_path = pnacl_dir.AppendASCII( 283 base::FilePath full_path = pnacl_dir.AppendASCII(
284 std::string(kExpectedFilePrefix) + filename); 284 std::string(kExpectedFilePrefix) + filename);
285 *file_to_open = full_path; 285 *file_to_open = full_path;
286 return true; 286 return true;
287 } 287 }
288 288
289 void CreateTemporaryFile( 289 void CreateTemporaryFile(
290 scoped_refptr<ChromeRenderMessageFilter> chrome_render_message_filter, 290 scoped_refptr<NaClHostMessageFilter> nacl_host_message_filter,
291 IPC::Message* reply_msg) { 291 IPC::Message* reply_msg) {
292 if (!BrowserThread::PostBlockingPoolTask( 292 if (!BrowserThread::PostBlockingPoolTask(
293 FROM_HERE, 293 FROM_HERE,
294 base::Bind(&DoCreateTemporaryFile, 294 base::Bind(&DoCreateTemporaryFile,
295 chrome_render_message_filter, 295 nacl_host_message_filter,
296 reply_msg))) { 296 reply_msg))) {
297 NotifyRendererOfError(chrome_render_message_filter.get(), reply_msg); 297 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
298 } 298 }
299 } 299 }
300 300
301 void OpenNaClExecutable( 301 void OpenNaClExecutable(
302 scoped_refptr<ChromeRenderMessageFilter> chrome_render_message_filter, 302 scoped_refptr<NaClHostMessageFilter> nacl_host_message_filter,
303 scoped_refptr<ExtensionInfoMap> extension_info_map, 303 scoped_refptr<ExtensionInfoMap> extension_info_map,
304 int render_view_id, 304 int render_view_id,
305 const GURL& file_url, 305 const GURL& file_url,
306 IPC::Message* reply_msg) { 306 IPC::Message* reply_msg) {
307 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { 307 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
308 BrowserThread::PostTask( 308 BrowserThread::PostTask(
309 BrowserThread::UI, FROM_HERE, 309 BrowserThread::UI, FROM_HERE,
310 base::Bind( 310 base::Bind(
311 &OpenNaClExecutable, 311 &OpenNaClExecutable,
312 chrome_render_message_filter, 312 nacl_host_message_filter,
313 extension_info_map, 313 extension_info_map,
314 render_view_id, file_url, reply_msg)); 314 render_view_id, file_url, reply_msg));
315 return; 315 return;
316 } 316 }
317 317
318 // Make sure render_view_id is valid and that the URL is a part of the 318 // Make sure render_view_id is valid and that the URL is a part of the
319 // render view's site. Without these checks, apps could probe the extension 319 // render view's site. Without these checks, apps could probe the extension
320 // directory or run NaCl code from other extensions. 320 // directory or run NaCl code from other extensions.
321 content::RenderViewHost* rvh = content::RenderViewHost::FromID( 321 content::RenderViewHost* rvh = content::RenderViewHost::FromID(
322 chrome_render_message_filter->render_process_id(), render_view_id); 322 nacl_host_message_filter->render_process_id(), render_view_id);
323 if (!rvh) { 323 if (!rvh) {
324 chrome_render_message_filter->BadMessageReceived(); // Kill the renderer. 324 nacl_host_message_filter->BadMessageReceived(); // Kill the renderer.
325 return; 325 return;
326 } 326 }
327 content::SiteInstance* site_instance = rvh->GetSiteInstance(); 327 content::SiteInstance* site_instance = rvh->GetSiteInstance();
328 if (!content::SiteInstance::IsSameWebSite(site_instance->GetBrowserContext(), 328 if (!content::SiteInstance::IsSameWebSite(site_instance->GetBrowserContext(),
329 site_instance->GetSiteURL(), 329 site_instance->GetSiteURL(),
330 file_url)) { 330 file_url)) {
331 NotifyRendererOfError(chrome_render_message_filter.get(), reply_msg); 331 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
332 return; 332 return;
333 } 333 }
334 334
335 // The URL is part of the current app. Now query the extension system for the 335 // The URL is part of the current app. Now query the extension system for the
336 // file path and convert that to a file descriptor. This should be done on a 336 // file path and convert that to a file descriptor. This should be done on a
337 // blocking pool thread. 337 // blocking pool thread.
338 if (!BrowserThread::PostBlockingPoolTask( 338 if (!BrowserThread::PostBlockingPoolTask(
339 FROM_HERE, 339 FROM_HERE,
340 base::Bind( 340 base::Bind(
341 &DoOpenNaClExecutableOnThreadPool, 341 &DoOpenNaClExecutableOnThreadPool,
342 chrome_render_message_filter, 342 nacl_host_message_filter,
343 extension_info_map, 343 extension_info_map,
344 file_url, reply_msg))) { 344 file_url, reply_msg))) {
345 NotifyRendererOfError(chrome_render_message_filter.get(), reply_msg); 345 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
346 } 346 }
347 } 347 }
348 348
349 } // namespace nacl_file_host 349 } // namespace nacl_file_host
OLDNEW
« no previous file with comments | « chrome/browser/nacl_host/nacl_file_host.h ('k') | chrome/browser/nacl_host/nacl_host_message_filter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698