Home | History | Annotate | Download | only in src
      1 // Copyright (c) 2013 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 "sandbox/win/src/named_pipe_dispatcher.h"
      6 
      7 #include "base/basictypes.h"
      8 #include "base/strings/string_split.h"
      9 
     10 #include "sandbox/win/src/crosscall_client.h"
     11 #include "sandbox/win/src/interception.h"
     12 #include "sandbox/win/src/interceptors.h"
     13 #include "sandbox/win/src/ipc_tags.h"
     14 #include "sandbox/win/src/named_pipe_interception.h"
     15 #include "sandbox/win/src/named_pipe_policy.h"
     16 #include "sandbox/win/src/policy_broker.h"
     17 #include "sandbox/win/src/policy_params.h"
     18 #include "sandbox/win/src/sandbox.h"
     19 
     20 
     21 namespace sandbox {
     22 
     23 NamedPipeDispatcher::NamedPipeDispatcher(PolicyBase* policy_base)
     24     : policy_base_(policy_base) {
     25   static const IPCCall create_params = {
     26     {IPC_CREATENAMEDPIPEW_TAG, WCHAR_TYPE, ULONG_TYPE, ULONG_TYPE, ULONG_TYPE,
     27      ULONG_TYPE, ULONG_TYPE, ULONG_TYPE},
     28     reinterpret_cast<CallbackGeneric>(&NamedPipeDispatcher::CreateNamedPipe)
     29   };
     30 
     31   ipc_calls_.push_back(create_params);
     32 }
     33 
     34 bool NamedPipeDispatcher::SetupService(InterceptionManager* manager,
     35                                        int service) {
     36   if (IPC_CREATENAMEDPIPEW_TAG == service)
     37     return INTERCEPT_EAT(manager, kKerneldllName, CreateNamedPipeW,
     38                          CREATE_NAMED_PIPE_ID, 36);
     39 
     40   return false;
     41 }
     42 
     43 bool NamedPipeDispatcher::CreateNamedPipe(
     44     IPCInfo* ipc, base::string16* name, DWORD open_mode, DWORD pipe_mode,
     45     DWORD max_instances, DWORD out_buffer_size, DWORD in_buffer_size,
     46     DWORD default_timeout) {
     47   ipc->return_info.win32_result = ERROR_ACCESS_DENIED;
     48   ipc->return_info.handle = INVALID_HANDLE_VALUE;
     49 
     50   std::vector<base::string16> paths;
     51   std::vector<base::string16> innerpaths;
     52   base::SplitString(*name, '/', &paths);
     53 
     54   for (std::vector<base::string16>::const_iterator iter = paths.begin();
     55        iter != paths.end(); ++iter) {
     56     base::SplitString(*iter, '\\', &innerpaths);
     57     for (std::vector<base::string16>::const_iterator iter2 = innerpaths.begin();
     58          iter2 != innerpaths.end(); ++iter2) {
     59       if (*iter2 == L"..")
     60         return true;
     61     }
     62   }
     63 
     64   const wchar_t* pipe_name = name->c_str();
     65   CountedParameterSet<NameBased> params;
     66   params[NameBased::NAME] = ParamPickerMake(pipe_name);
     67 
     68   EvalResult eval = policy_base_->EvalPolicy(IPC_CREATENAMEDPIPEW_TAG,
     69                                              params.GetBase());
     70 
     71   // "For file I/O, the "\\?\" prefix to a path string tells the Windows APIs to
     72   // disable all string parsing and to send the string that follows it straight
     73   // to the file system."
     74   // http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx
     75   // This ensures even if there is a path traversal in the pipe name, and it is
     76   // able to get past the checks above, it will still not be allowed to escape
     77   // our whitelisted namespace.
     78   if (name->compare(0, 4, L"\\\\.\\") == 0)
     79     name->replace(0, 4, L"\\\\\?\\");
     80 
     81   HANDLE pipe;
     82   DWORD ret = NamedPipePolicy::CreateNamedPipeAction(eval, *ipc->client_info,
     83                                                      *name, open_mode,
     84                                                      pipe_mode, max_instances,
     85                                                      out_buffer_size,
     86                                                      in_buffer_size,
     87                                                      default_timeout, &pipe);
     88 
     89   ipc->return_info.win32_result = ret;
     90   ipc->return_info.handle = pipe;
     91   return true;
     92 }
     93 
     94 }  // namespace sandbox
     95