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_path.h" | 10 #include "base/files/file_path.h" |
10 #include "base/path_service.h" | 11 #include "base/path_service.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 "components/nacl/browser/nacl_browser.h" | 14 #include "components/nacl/browser/nacl_browser.h" |
15 #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" | 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 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 bool PnaclDoOpenFile(const base::FilePath& file_to_open, | 41 base::File PnaclDoOpenFile(const base::FilePath& file_to_open) { |
42 base::PlatformFile* out_file) { | 42 return base::File(file_to_open, |
43 base::PlatformFileError error_code; | 43 base::File::FLAG_OPEN | base::File::FLAG_READ); |
44 *out_file = base::CreatePlatformFile(file_to_open, | |
45 base::PLATFORM_FILE_OPEN | | |
46 base::PLATFORM_FILE_READ, | |
47 NULL, | |
48 &error_code); | |
49 if (error_code != base::PLATFORM_FILE_OK) { | |
50 return false; | |
51 } | |
52 return true; | |
53 } | 44 } |
54 | 45 |
55 void DoOpenPnaclFile( | 46 void DoOpenPnaclFile( |
56 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, | 47 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
57 const std::string& filename, | 48 const std::string& filename, |
58 IPC::Message* reply_msg) { | 49 IPC::Message* reply_msg) { |
59 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 50 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
60 base::FilePath full_filepath; | 51 base::FilePath full_filepath; |
61 | 52 |
62 // PNaCl must be installed. | 53 // PNaCl must be installed. |
63 base::FilePath pnacl_dir; | 54 base::FilePath pnacl_dir; |
64 if (!nacl::NaClBrowser::GetDelegate()->GetPnaclDirectory(&pnacl_dir) || | 55 if (!nacl::NaClBrowser::GetDelegate()->GetPnaclDirectory(&pnacl_dir) || |
65 !base::PathExists(pnacl_dir)) { | 56 !base::PathExists(pnacl_dir)) { |
66 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 57 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
67 return; | 58 return; |
68 } | 59 } |
69 | 60 |
70 // Do some validation. | 61 // Do some validation. |
71 if (!nacl_file_host::PnaclCanOpenFile(filename, &full_filepath)) { | 62 if (!nacl_file_host::PnaclCanOpenFile(filename, &full_filepath)) { |
72 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 63 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
73 return; | 64 return; |
74 } | 65 } |
75 | 66 |
76 base::PlatformFile file_to_open; | 67 base::File file_to_open = PnaclDoOpenFile(full_filepath); |
77 if (!PnaclDoOpenFile(full_filepath, &file_to_open)) { | 68 if (!file_to_open.IsValid()) { |
78 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 69 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
79 return; | 70 return; |
80 } | 71 } |
81 | 72 |
82 // Send the reply! | 73 // Send the reply! |
83 // Do any DuplicateHandle magic that is necessary first. | 74 // Do any DuplicateHandle magic that is necessary first. |
84 IPC::PlatformFileForTransit target_desc = | 75 IPC::PlatformFileForTransit target_desc = |
85 IPC::GetFileHandleForProcess(file_to_open, | 76 IPC::TakeFileHandleForProcess(file_to_open.Pass(), |
86 nacl_host_message_filter->PeerHandle(), | 77 nacl_host_message_filter->PeerHandle()); |
87 true /* Close source */); | |
88 if (target_desc == IPC::InvalidPlatformFileForTransit()) { | 78 if (target_desc == IPC::InvalidPlatformFileForTransit()) { |
89 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 79 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
90 return; | 80 return; |
91 } | 81 } |
92 NaClHostMsg_GetReadonlyPnaclFD::WriteReplyParams( | 82 NaClHostMsg_GetReadonlyPnaclFD::WriteReplyParams( |
93 reply_msg, target_desc); | 83 reply_msg, target_desc); |
94 nacl_host_message_filter->Send(reply_msg); | 84 nacl_host_message_filter->Send(reply_msg); |
95 } | 85 } |
96 | 86 |
97 void DoRegisterOpenedNaClExecutableFile( | 87 void DoRegisterOpenedNaClExecutableFile( |
98 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, | 88 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
99 base::PlatformFile file, | 89 base::File file, |
100 base::FilePath file_path, | 90 base::FilePath file_path, |
101 IPC::Message* reply_msg) { | 91 IPC::Message* reply_msg) { |
102 // IO thread owns the NaClBrowser singleton. | 92 // IO thread owns the NaClBrowser singleton. |
103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
104 | 94 |
105 nacl::NaClBrowser* nacl_browser = nacl::NaClBrowser::GetInstance(); | 95 nacl::NaClBrowser* nacl_browser = nacl::NaClBrowser::GetInstance(); |
106 uint64 file_token_lo = 0; | 96 uint64 file_token_lo = 0; |
107 uint64 file_token_hi = 0; | 97 uint64 file_token_hi = 0; |
108 nacl_browser->PutFilePath(file_path, &file_token_lo, &file_token_hi); | 98 nacl_browser->PutFilePath(file_path, &file_token_lo, &file_token_hi); |
109 | 99 |
110 IPC::PlatformFileForTransit file_desc = IPC::GetFileHandleForProcess( | 100 IPC::PlatformFileForTransit file_desc = IPC::TakeFileHandleForProcess( |
111 file, | 101 file.Pass(), |
112 nacl_host_message_filter->PeerHandle(), | 102 nacl_host_message_filter->PeerHandle()); |
113 true /* close_source */); | |
114 | 103 |
115 NaClHostMsg_OpenNaClExecutable::WriteReplyParams( | 104 NaClHostMsg_OpenNaClExecutable::WriteReplyParams( |
116 reply_msg, file_desc, file_token_lo, file_token_hi); | 105 reply_msg, file_desc, file_token_lo, file_token_hi); |
117 nacl_host_message_filter->Send(reply_msg); | 106 nacl_host_message_filter->Send(reply_msg); |
118 } | 107 } |
119 | 108 |
120 // Convert the file URL into a file descriptor. | 109 // Convert the file URL into a file descriptor. |
121 // This function is security sensitive. Be sure to check with a security | 110 // This function is security sensitive. Be sure to check with a security |
122 // person before you modify it. | 111 // person before you modify it. |
123 void DoOpenNaClExecutableOnThreadPool( | 112 void DoOpenNaClExecutableOnThreadPool( |
124 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, | 113 scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter, |
125 const GURL& file_url, | 114 const GURL& file_url, |
126 IPC::Message* reply_msg) { | 115 IPC::Message* reply_msg) { |
127 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 116 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
128 | 117 |
129 base::FilePath file_path; | 118 base::FilePath file_path; |
130 if (!nacl::NaClBrowser::GetDelegate()->MapUrlToLocalFilePath( | 119 if (!nacl::NaClBrowser::GetDelegate()->MapUrlToLocalFilePath( |
131 file_url, true /* use_blocking_api */, &file_path)) { | 120 file_url, true /* use_blocking_api */, &file_path)) { |
132 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 121 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
133 return; | 122 return; |
134 } | 123 } |
135 | 124 |
136 base::PlatformFile file = nacl::OpenNaClExecutableImpl(file_path); | 125 base::File file = nacl::OpenNaClExecutableImpl(file_path); |
137 if (file != base::kInvalidPlatformFileValue) { | 126 if (file.IsValid()) { |
138 // This function is running on the blocking pool, but the path needs to be | 127 // This function is running on the blocking pool, but the path needs to be |
139 // registered in a structure owned by the IO thread. | 128 // registered in a structure owned by the IO thread. |
140 BrowserThread::PostTask( | 129 BrowserThread::PostTask( |
141 BrowserThread::IO, FROM_HERE, | 130 BrowserThread::IO, FROM_HERE, |
142 base::Bind( | 131 base::Bind( |
143 &DoRegisterOpenedNaClExecutableFile, | 132 &DoRegisterOpenedNaClExecutableFile, |
144 nacl_host_message_filter, | 133 nacl_host_message_filter, |
145 file, file_path, reply_msg)); | 134 Passed(file.Pass()), file_path, reply_msg)); |
146 } else { | 135 } else { |
147 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 136 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
148 return; | 137 return; |
149 } | 138 } |
150 } | 139 } |
151 | 140 |
152 } // namespace | 141 } // namespace |
153 | 142 |
154 namespace nacl_file_host { | 143 namespace nacl_file_host { |
155 | 144 |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 FROM_HERE, | 228 FROM_HERE, |
240 base::Bind( | 229 base::Bind( |
241 &DoOpenNaClExecutableOnThreadPool, | 230 &DoOpenNaClExecutableOnThreadPool, |
242 nacl_host_message_filter, | 231 nacl_host_message_filter, |
243 file_url, reply_msg))) { | 232 file_url, reply_msg))) { |
244 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); | 233 NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg); |
245 } | 234 } |
246 } | 235 } |
247 | 236 |
248 } // namespace nacl_file_host | 237 } // namespace nacl_file_host |
OLD | NEW |