Home | History | Annotate | Download | only in browser
      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 "components/nacl/browser/nacl_file_host.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/file_util.h"
      9 #include "base/files/file_path.h"
     10 #include "base/path_service.h"
     11 #include "base/platform_file.h"
     12 #include "base/strings/utf_string_conversions.h"
     13 #include "base/threading/sequenced_worker_pool.h"
     14 #include "components/nacl/browser/nacl_browser.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"
     18 #include "content/public/browser/browser_thread.h"
     19 #include "content/public/browser/render_view_host.h"
     20 #include "content/public/browser/site_instance.h"
     21 #include "ipc/ipc_platform_file.h"
     22 
     23 using content::BrowserThread;
     24 
     25 namespace {
     26 
     27 // Force a prefix to prevent user from opening "magic" files.
     28 const char* kExpectedFilePrefix = "pnacl_public_";
     29 
     30 // Restrict PNaCl file lengths to reduce likelyhood of hitting bugs
     31 // in file name limit error-handling-code-paths, etc.
     32 const size_t kMaxFileLength = 40;
     33 
     34 void NotifyRendererOfError(
     35     nacl::NaClHostMessageFilter* nacl_host_message_filter,
     36     IPC::Message* reply_msg) {
     37   reply_msg->set_reply_error();
     38   nacl_host_message_filter->Send(reply_msg);
     39 }
     40 
     41 bool PnaclDoOpenFile(const base::FilePath& file_to_open,
     42                      base::PlatformFile* out_file) {
     43   base::PlatformFileError error_code;
     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 }
     54 
     55 void DoOpenPnaclFile(
     56     scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter,
     57     const std::string& filename,
     58     IPC::Message* reply_msg) {
     59   DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
     60   base::FilePath full_filepath;
     61 
     62   // PNaCl must be installed.
     63   base::FilePath pnacl_dir;
     64   if (!nacl::NaClBrowser::GetDelegate()->GetPnaclDirectory(&pnacl_dir) ||
     65       !base::PathExists(pnacl_dir)) {
     66     NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
     67     return;
     68   }
     69 
     70   // Do some validation.
     71   if (!nacl_file_host::PnaclCanOpenFile(filename, &full_filepath)) {
     72     NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
     73     return;
     74   }
     75 
     76   base::PlatformFile file_to_open;
     77   if (!PnaclDoOpenFile(full_filepath, &file_to_open)) {
     78     NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
     79     return;
     80   }
     81 
     82   // Send the reply!
     83   // Do any DuplicateHandle magic that is necessary first.
     84   IPC::PlatformFileForTransit target_desc =
     85       IPC::GetFileHandleForProcess(file_to_open,
     86                                    nacl_host_message_filter->PeerHandle(),
     87                                    true /* Close source */);
     88   if (target_desc == IPC::InvalidPlatformFileForTransit()) {
     89     NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
     90     return;
     91   }
     92   NaClHostMsg_GetReadonlyPnaclFD::WriteReplyParams(
     93       reply_msg, target_desc);
     94   nacl_host_message_filter->Send(reply_msg);
     95 }
     96 
     97 void DoRegisterOpenedNaClExecutableFile(
     98     scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter,
     99     base::PlatformFile file,
    100     base::FilePath file_path,
    101     IPC::Message* reply_msg) {
    102   // IO thread owns the NaClBrowser singleton.
    103   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    104 
    105   nacl::NaClBrowser* nacl_browser = nacl::NaClBrowser::GetInstance();
    106   uint64 file_token_lo = 0;
    107   uint64 file_token_hi = 0;
    108   nacl_browser->PutFilePath(file_path, &file_token_lo, &file_token_hi);
    109 
    110   IPC::PlatformFileForTransit file_desc = IPC::GetFileHandleForProcess(
    111       file,
    112       nacl_host_message_filter->PeerHandle(),
    113       true /* close_source */);
    114 
    115   NaClHostMsg_OpenNaClExecutable::WriteReplyParams(
    116       reply_msg, file_desc, file_token_lo, file_token_hi);
    117   nacl_host_message_filter->Send(reply_msg);
    118 }
    119 
    120 // Convert the file URL into a file descriptor.
    121 // This function is security sensitive.  Be sure to check with a security
    122 // person before you modify it.
    123 void DoOpenNaClExecutableOnThreadPool(
    124     scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter,
    125     const GURL& file_url,
    126     IPC::Message* reply_msg) {
    127   DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
    128 
    129   base::FilePath file_path;
    130   if (!nacl::NaClBrowser::GetDelegate()->MapUrlToLocalFilePath(
    131           file_url, true /* use_blocking_api */, &file_path)) {
    132     NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
    133     return;
    134   }
    135 
    136   base::PlatformFile file = nacl::OpenNaClExecutableImpl(file_path);
    137   if (file != base::kInvalidPlatformFileValue) {
    138     // This function is running on the blocking pool, but the path needs to be
    139     // registered in a structure owned by the IO thread.
    140     BrowserThread::PostTask(
    141         BrowserThread::IO, FROM_HERE,
    142         base::Bind(
    143             &DoRegisterOpenedNaClExecutableFile,
    144             nacl_host_message_filter,
    145             file, file_path, reply_msg));
    146   } else {
    147     NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
    148     return;
    149   }
    150 }
    151 
    152 }  // namespace
    153 
    154 namespace nacl_file_host {
    155 
    156 void GetReadonlyPnaclFd(
    157     scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter,
    158     const std::string& filename,
    159     IPC::Message* reply_msg) {
    160   if (!BrowserThread::PostBlockingPoolTask(
    161           FROM_HERE,
    162           base::Bind(&DoOpenPnaclFile,
    163                      nacl_host_message_filter,
    164                      filename,
    165                      reply_msg))) {
    166     NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
    167   }
    168 }
    169 
    170 // This function is security sensitive.  Be sure to check with a security
    171 // person before you modify it.
    172 bool PnaclCanOpenFile(const std::string& filename,
    173                       base::FilePath* file_to_open) {
    174   if (filename.length() > kMaxFileLength)
    175     return false;
    176 
    177   if (filename.empty())
    178     return false;
    179 
    180   // Restrict character set of the file name to something really simple
    181   // (a-z, 0-9, and underscores).
    182   for (size_t i = 0; i < filename.length(); ++i) {
    183     char charAt = filename[i];
    184     if (charAt < 'a' || charAt > 'z')
    185       if (charAt < '0' || charAt > '9')
    186         if (charAt != '_')
    187           return false;
    188   }
    189 
    190   // PNaCl must be installed.
    191   base::FilePath pnacl_dir;
    192   if (!nacl::NaClBrowser::GetDelegate()->GetPnaclDirectory(&pnacl_dir) ||
    193       pnacl_dir.empty())
    194     return false;
    195 
    196   // Prepend the prefix to restrict files to a whitelisted set.
    197   base::FilePath full_path = pnacl_dir.AppendASCII(
    198       std::string(kExpectedFilePrefix) + filename);
    199   *file_to_open = full_path;
    200   return true;
    201 }
    202 
    203 void OpenNaClExecutable(
    204     scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter,
    205     int render_view_id,
    206     const GURL& file_url,
    207     IPC::Message* reply_msg) {
    208   if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
    209     BrowserThread::PostTask(
    210         BrowserThread::UI, FROM_HERE,
    211         base::Bind(
    212             &OpenNaClExecutable,
    213             nacl_host_message_filter,
    214             render_view_id, file_url, reply_msg));
    215     return;
    216   }
    217 
    218   // Make sure render_view_id is valid and that the URL is a part of the
    219   // render view's site. Without these checks, apps could probe the extension
    220   // directory or run NaCl code from other extensions.
    221   content::RenderViewHost* rvh = content::RenderViewHost::FromID(
    222       nacl_host_message_filter->render_process_id(), render_view_id);
    223   if (!rvh) {
    224     nacl_host_message_filter->BadMessageReceived();  // Kill the renderer.
    225     return;
    226   }
    227   content::SiteInstance* site_instance = rvh->GetSiteInstance();
    228   if (!content::SiteInstance::IsSameWebSite(site_instance->GetBrowserContext(),
    229                                             site_instance->GetSiteURL(),
    230                                             file_url)) {
    231     NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
    232     return;
    233   }
    234 
    235   // The URL is part of the current app. Now query the extension system for the
    236   // file path and convert that to a file descriptor. This should be done on a
    237   // blocking pool thread.
    238   if (!BrowserThread::PostBlockingPoolTask(
    239       FROM_HERE,
    240       base::Bind(
    241           &DoOpenNaClExecutableOnThreadPool,
    242           nacl_host_message_filter,
    243           file_url, reply_msg))) {
    244     NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
    245   }
    246 }
    247 
    248 }  // namespace nacl_file_host
    249