1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/nacl_host/nacl_file_host.h"
#include "base/bind.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/path_service.h"
#include "base/platform_file.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/sequenced_worker_pool.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/component_updater/pnacl/pnacl_component_installer.h"
#include "chrome/browser/extensions/extension_info_map.h"
#include "chrome/browser/nacl_host/nacl_browser.h"
#include "chrome/browser/nacl_host/nacl_host_message_filter.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/extensions/manifest_handlers/shared_module_info.h"
#include "chrome/common/nacl_host_messages.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/site_instance.h"
#include "ipc/ipc_platform_file.h"
using content::BrowserThread;
using extensions::SharedModuleInfo;
namespace {
// Force a prefix to prevent user from opening "magic" files.
const char* kExpectedFilePrefix = "pnacl_public_";
// Restrict PNaCl file lengths to reduce likelyhood of hitting bugs
// in file name limit error-handling-code-paths, etc.
const size_t kMaxFileLength = 40;
void NotifyRendererOfError(
NaClHostMessageFilter* nacl_host_message_filter,
IPC::Message* reply_msg) {
reply_msg->set_reply_error();
nacl_host_message_filter->Send(reply_msg);
}
bool PnaclDoOpenFile(const base::FilePath& file_to_open,
base::PlatformFile* out_file) {
base::PlatformFileError error_code;
*out_file = base::CreatePlatformFile(file_to_open,
base::PLATFORM_FILE_OPEN |
base::PLATFORM_FILE_READ,
NULL,
&error_code);
if (error_code != base::PLATFORM_FILE_OK) {
return false;
}
return true;
}
void DoOpenPnaclFile(
scoped_refptr<NaClHostMessageFilter> nacl_host_message_filter,
const std::string& filename,
IPC::Message* reply_msg);
void PnaclCheckDone(
scoped_refptr<NaClHostMessageFilter> nacl_host_message_filter,
const std::string& filename,
IPC::Message* reply_msg,
bool success) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (!success) {
NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
} else {
if (!BrowserThread::PostBlockingPoolTask(
FROM_HERE,
base::Bind(&DoOpenPnaclFile,
nacl_host_message_filter,
filename,
reply_msg))) {
NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
}
}
}
void TryInstallPnacl(
scoped_refptr<NaClHostMessageFilter> nacl_host_message_filter,
const std::string& filename,
IPC::Message* reply_msg) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
ComponentUpdateService* cus = g_browser_process->component_updater();
PnaclComponentInstaller* pci =
g_browser_process->pnacl_component_installer();
RequestFirstInstall(cus,
pci,
base::Bind(&PnaclCheckDone,
nacl_host_message_filter,
filename,
reply_msg));
}
void DoOpenPnaclFile(
scoped_refptr<NaClHostMessageFilter> nacl_host_message_filter,
const std::string& filename,
IPC::Message* reply_msg) {
DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
base::FilePath full_filepath;
// PNaCl must be installed.
base::FilePath pnacl_dir;
if (!PathService::Get(chrome::DIR_PNACL_COMPONENT, &pnacl_dir) ||
!file_util::PathExists(pnacl_dir)) {
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&TryInstallPnacl,
nacl_host_message_filter,
filename,
reply_msg));
return;
}
// Do some validation.
if (!nacl_file_host::PnaclCanOpenFile(filename, &full_filepath)) {
NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
return;
}
base::PlatformFile file_to_open;
if (!PnaclDoOpenFile(full_filepath, &file_to_open)) {
NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
return;
}
// Send the reply!
// Do any DuplicateHandle magic that is necessary first.
IPC::PlatformFileForTransit target_desc =
IPC::GetFileHandleForProcess(file_to_open,
nacl_host_message_filter->peer_handle(),
true /* Close source */);
if (target_desc == IPC::InvalidPlatformFileForTransit()) {
NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
return;
}
NaClHostMsg_GetReadonlyPnaclFD::WriteReplyParams(
reply_msg, target_desc);
nacl_host_message_filter->Send(reply_msg);
}
void DoCreateTemporaryFile(
scoped_refptr<NaClHostMessageFilter> nacl_host_message_filter,
IPC::Message* reply_msg) {
DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
base::FilePath file_path;
if (!file_util::CreateTemporaryFile(&file_path)) {
NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
return;
}
base::PlatformFileError error;
base::PlatformFile file_handle = base::CreatePlatformFile(
file_path,
base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_READ |
base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_TEMPORARY |
base::PLATFORM_FILE_DELETE_ON_CLOSE,
NULL, &error);
if (error != base::PLATFORM_FILE_OK) {
NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
return;
}
// Send the reply!
// Do any DuplicateHandle magic that is necessary first.
IPC::PlatformFileForTransit target_desc =
IPC::GetFileHandleForProcess(file_handle,
nacl_host_message_filter->peer_handle(),
true);
if (target_desc == IPC::InvalidPlatformFileForTransit()) {
NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
return;
}
NaClHostMsg_NaClCreateTemporaryFile::WriteReplyParams(
reply_msg, target_desc);
nacl_host_message_filter->Send(reply_msg);
}
void DoRegisterOpenedNaClExecutableFile(
scoped_refptr<NaClHostMessageFilter> nacl_host_message_filter,
base::PlatformFile file,
base::FilePath file_path,
IPC::Message* reply_msg) {
// IO thread owns the NaClBrowser singleton.
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
uint64 file_token_lo = 0;
uint64 file_token_hi = 0;
nacl_browser->PutFilePath(file_path, &file_token_lo, &file_token_hi);
IPC::PlatformFileForTransit file_desc = IPC::GetFileHandleForProcess(
file,
nacl_host_message_filter->peer_handle(),
true /* close_source */);
NaClHostMsg_OpenNaClExecutable::WriteReplyParams(
reply_msg, file_desc, file_token_lo, file_token_hi);
nacl_host_message_filter->Send(reply_msg);
}
// Convert the file URL into a file path in the extension directory.
// This function is security sensitive. Be sure to check with a security
// person before you modify it.
bool GetExtensionFilePath(
scoped_refptr<ExtensionInfoMap> extension_info_map,
const GURL& file_url,
base::FilePath* file_path) {
// Check that the URL is recognized by the extension system.
const extensions::Extension* extension =
extension_info_map->extensions().GetExtensionOrAppByURL(
ExtensionURLInfo(file_url));
if (!extension)
return false;
std::string path = file_url.path();
extensions::ExtensionResource resource;
if (SharedModuleInfo::IsImportedPath(path)) {
// Check if this is a valid path that is imported for this extension.
std::string new_extension_id;
std::string new_relative_path;
SharedModuleInfo::ParseImportedPath(path, &new_extension_id,
&new_relative_path);
const extensions::Extension* new_extension =
extension_info_map->extensions().GetByID(new_extension_id);
if (!new_extension)
return false;
if (!SharedModuleInfo::ImportsExtensionById(extension, new_extension_id) ||
!SharedModuleInfo::IsExportAllowed(new_extension, new_relative_path)) {
return false;
}
resource = new_extension->GetResource(new_relative_path);
} else {
// Check that the URL references a resource in the extension.
resource = extension->GetResource(path);
}
if (resource.empty())
return false;
const base::FilePath resource_file_path = resource.GetFilePath();
if (resource_file_path.empty())
return false;
*file_path = resource_file_path;
return true;
}
// Convert the file URL into a file descriptor.
// This function is security sensitive. Be sure to check with a security
// person before you modify it.
void DoOpenNaClExecutableOnThreadPool(
scoped_refptr<NaClHostMessageFilter> nacl_host_message_filter,
scoped_refptr<ExtensionInfoMap> extension_info_map,
const GURL& file_url,
IPC::Message* reply_msg) {
DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
base::FilePath file_path;
if (!GetExtensionFilePath(extension_info_map, file_url, &file_path)) {
NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
return;
}
base::PlatformFile file;
nacl::OpenNaClExecutableImpl(file_path, &file);
if (file != base::kInvalidPlatformFileValue) {
// This function is running on the blocking pool, but the path needs to be
// registered in a structure owned by the IO thread.
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(
&DoRegisterOpenedNaClExecutableFile,
nacl_host_message_filter,
file, file_path, reply_msg));
} else {
NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
return;
}
}
} // namespace
namespace nacl_file_host {
void GetReadonlyPnaclFd(
scoped_refptr<NaClHostMessageFilter> nacl_host_message_filter,
const std::string& filename,
IPC::Message* reply_msg) {
if (!BrowserThread::PostBlockingPoolTask(
FROM_HERE,
base::Bind(&DoOpenPnaclFile,
nacl_host_message_filter,
filename,
reply_msg))) {
NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
}
}
// This function is security sensitive. Be sure to check with a security
// person before you modify it.
bool PnaclCanOpenFile(const std::string& filename,
base::FilePath* file_to_open) {
if (filename.length() > kMaxFileLength)
return false;
if (filename.empty())
return false;
// Restrict character set of the file name to something really simple
// (a-z, 0-9, and underscores).
for (size_t i = 0; i < filename.length(); ++i) {
char charAt = filename[i];
if (charAt < 'a' || charAt > 'z')
if (charAt < '0' || charAt > '9')
if (charAt != '_')
return false;
}
// PNaCl must be installed.
base::FilePath pnacl_dir;
if (!PathService::Get(chrome::DIR_PNACL_COMPONENT, &pnacl_dir) ||
pnacl_dir.empty())
return false;
// Prepend the prefix to restrict files to a whitelisted set.
base::FilePath full_path = pnacl_dir.AppendASCII(
std::string(kExpectedFilePrefix) + filename);
*file_to_open = full_path;
return true;
}
void CreateTemporaryFile(
scoped_refptr<NaClHostMessageFilter> nacl_host_message_filter,
IPC::Message* reply_msg) {
if (!BrowserThread::PostBlockingPoolTask(
FROM_HERE,
base::Bind(&DoCreateTemporaryFile,
nacl_host_message_filter,
reply_msg))) {
NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
}
}
void OpenNaClExecutable(
scoped_refptr<NaClHostMessageFilter> nacl_host_message_filter,
scoped_refptr<ExtensionInfoMap> extension_info_map,
int render_view_id,
const GURL& file_url,
IPC::Message* reply_msg) {
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(
&OpenNaClExecutable,
nacl_host_message_filter,
extension_info_map,
render_view_id, file_url, reply_msg));
return;
}
// Make sure render_view_id is valid and that the URL is a part of the
// render view's site. Without these checks, apps could probe the extension
// directory or run NaCl code from other extensions.
content::RenderViewHost* rvh = content::RenderViewHost::FromID(
nacl_host_message_filter->render_process_id(), render_view_id);
if (!rvh) {
nacl_host_message_filter->BadMessageReceived(); // Kill the renderer.
return;
}
content::SiteInstance* site_instance = rvh->GetSiteInstance();
if (!content::SiteInstance::IsSameWebSite(site_instance->GetBrowserContext(),
site_instance->GetSiteURL(),
file_url)) {
NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
return;
}
// The URL is part of the current app. Now query the extension system for the
// file path and convert that to a file descriptor. This should be done on a
// blocking pool thread.
if (!BrowserThread::PostBlockingPoolTask(
FROM_HERE,
base::Bind(
&DoOpenNaClExecutableOnThreadPool,
nacl_host_message_filter,
extension_info_map,
file_url, reply_msg))) {
NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
}
}
} // namespace nacl_file_host
|