OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/nacl_host/pnacl_file_host.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" |
| 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/path_service.h" |
| 12 #include "base/platform_file.h" |
| 13 #include "base/utf_string_conversions.h" |
| 14 #include "chrome/browser/renderer_host/chrome_render_message_filter.h" |
| 15 #include "chrome/common/chrome_paths.h" |
| 16 #include "chrome/common/render_messages.h" |
| 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "ipc/ipc_platform_file.h" |
| 19 |
| 20 using content::BrowserThread; |
| 21 |
| 22 namespace { |
| 23 |
| 24 void NotifyRendererOfError( |
| 25 ChromeRenderMessageFilter* chrome_render_message_filter, |
| 26 IPC::Message* reply_msg) { |
| 27 reply_msg->set_reply_error(); |
| 28 chrome_render_message_filter->Send(reply_msg); |
| 29 } |
| 30 |
| 31 bool PnaclDoOpenFile(const FilePath& file_to_open, |
| 32 base::PlatformFile* out_file) { |
| 33 base::PlatformFileError error_code; |
| 34 *out_file = base::CreatePlatformFile(file_to_open, |
| 35 base::PLATFORM_FILE_OPEN | |
| 36 base::PLATFORM_FILE_READ, |
| 37 NULL, |
| 38 &error_code); |
| 39 if (error_code != base::PLATFORM_FILE_OK) { |
| 40 return false; |
| 41 } |
| 42 return true; |
| 43 } |
| 44 |
| 45 void DoOpenPnaclFile( |
| 46 ChromeRenderMessageFilter* chrome_render_message_filter, |
| 47 const std::string& filename, |
| 48 IPC::Message* reply_msg) { |
| 49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 50 FilePath full_filepath; |
| 51 |
| 52 // Do some validation. |
| 53 if (!pnacl_file_host::PnaclCanOpenFile(filename, &full_filepath)) { |
| 54 NotifyRendererOfError(chrome_render_message_filter, reply_msg); |
| 55 return; |
| 56 } |
| 57 |
| 58 base::PlatformFile file_to_open; |
| 59 if (!PnaclDoOpenFile(full_filepath, &file_to_open)) { |
| 60 NotifyRendererOfError(chrome_render_message_filter, reply_msg); |
| 61 return; |
| 62 } |
| 63 |
| 64 // Send the reply! |
| 65 // Do any DuplicateHandle magic that is necessary first. |
| 66 IPC::PlatformFileForTransit target_desc = |
| 67 IPC::GetFileHandleForProcess(file_to_open, |
| 68 chrome_render_message_filter->peer_handle(), |
| 69 true /* Close source */); |
| 70 if (target_desc == IPC::InvalidPlatformFileForTransit()) { |
| 71 NotifyRendererOfError(chrome_render_message_filter, reply_msg); |
| 72 return; |
| 73 } |
| 74 ChromeViewHostMsg_GetReadonlyPnaclFD::WriteReplyParams( |
| 75 reply_msg, target_desc); |
| 76 chrome_render_message_filter->Send(reply_msg); |
| 77 } |
| 78 |
| 79 } // namespace |
| 80 |
| 81 namespace pnacl_file_host { |
| 82 |
| 83 void GetReadonlyPnaclFd( |
| 84 ChromeRenderMessageFilter* chrome_render_message_filter, |
| 85 const std::string& filename, |
| 86 IPC::Message* reply_msg) { |
| 87 if (!BrowserThread::PostTask( |
| 88 BrowserThread::FILE, FROM_HERE, |
| 89 base::Bind(&DoOpenPnaclFile, |
| 90 make_scoped_refptr(chrome_render_message_filter), |
| 91 filename, |
| 92 reply_msg))) { |
| 93 NotifyRendererOfError(chrome_render_message_filter, reply_msg); |
| 94 } |
| 95 } |
| 96 |
| 97 bool PnaclCanOpenFile(const std::string& filename, |
| 98 FilePath* file_to_open) { |
| 99 // The file must use only ASCII characters. |
| 100 if (!IsStringASCII(filename)) { |
| 101 return false; |
| 102 } |
| 103 |
| 104 // Disallow special shell characters, just in case... |
| 105 if (filename.find('%') != std::string::npos || |
| 106 filename.find('$') != std::string::npos) { |
| 107 return false; |
| 108 } |
| 109 |
| 110 #if defined(OS_WIN) |
| 111 FilePath file_to_find(ASCIIToUTF16(filename)); |
| 112 #elif defined(OS_POSIX) |
| 113 FilePath file_to_find(filename); |
| 114 #endif |
| 115 |
| 116 if (file_to_find.empty() || file_util::IsDot(file_to_find)) { |
| 117 return false; |
| 118 } |
| 119 |
| 120 // Disallow peeking outside of the pnacl component directory. |
| 121 if (file_to_find.ReferencesParent() || file_to_find.IsAbsolute()) { |
| 122 return false; |
| 123 } |
| 124 |
| 125 FilePath pnacl_dir; |
| 126 if (!PathService::Get(chrome::DIR_PNACL_COMPONENT, &pnacl_dir)) { |
| 127 return false; |
| 128 } |
| 129 if (pnacl_dir.empty()) { |
| 130 return false; |
| 131 } |
| 132 |
| 133 FilePath full_path = pnacl_dir.Append(file_to_find); |
| 134 *file_to_open = full_path; |
| 135 return true; |
| 136 } |
| 137 |
| 138 } // namespace pnacl_file_host |
OLD | NEW |