Home | History | Annotate | Download | only in src
      1 // Copyright (c) 2006-2008 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/sync_interception.h"
      6 
      7 #include "sandbox/win/src/crosscall_client.h"
      8 #include "sandbox/win/src/ipc_tags.h"
      9 #include "sandbox/win/src/policy_params.h"
     10 #include "sandbox/win/src/policy_target.h"
     11 #include "sandbox/win/src/sandbox_factory.h"
     12 #include "sandbox/win/src/sandbox_nt_util.h"
     13 #include "sandbox/win/src/sharedmem_ipc_client.h"
     14 #include "sandbox/win/src/target_services.h"
     15 
     16 namespace sandbox {
     17 
     18 HANDLE WINAPI TargetCreateEventW(CreateEventWFunction orig_CreateEvent,
     19                                  LPSECURITY_ATTRIBUTES security_attributes,
     20                                  BOOL manual_reset, BOOL initial_state,
     21                                  LPCWSTR name) {
     22   // Check if the process can create it first.
     23   HANDLE handle = orig_CreateEvent(security_attributes, manual_reset,
     24                                    initial_state, name);
     25   DWORD original_error = ::GetLastError();
     26   if (NULL != handle)
     27     return handle;
     28 
     29   // We don't trust that the IPC can work this early.
     30   if (!SandboxFactory::GetTargetServices()->GetState()->InitCalled())
     31     return NULL;
     32 
     33   do {
     34     if (security_attributes)
     35       break;
     36 
     37     void* memory = GetGlobalIPCMemory();
     38     if (NULL == memory)
     39       break;
     40 
     41     CountedParameterSet<NameBased> params;
     42     params[NameBased::NAME] = ParamPickerMake(name);
     43 
     44     if (!QueryBroker(IPC_CREATEEVENT_TAG, params.GetBase()))
     45       break;
     46 
     47     SharedMemIPCClient ipc(memory);
     48     CrossCallReturn answer = {0};
     49     ResultCode code = CrossCall(ipc, IPC_CREATEEVENT_TAG, name, manual_reset,
     50                                 initial_state, &answer);
     51 
     52     if (SBOX_ALL_OK != code)
     53       break;
     54 
     55     ::SetLastError(answer.win32_result);
     56     return answer.handle;
     57   } while (false);
     58 
     59   ::SetLastError(original_error);
     60   return NULL;
     61 }
     62 
     63 // Interception of OpenEventW on the child process.
     64 // It should never be called directly
     65 HANDLE WINAPI TargetOpenEventW(OpenEventWFunction orig_OpenEvent,
     66                                ACCESS_MASK desired_access, BOOL inherit_handle,
     67                                LPCWSTR name) {
     68   // Check if the process can open it first.
     69   HANDLE handle = orig_OpenEvent(desired_access, inherit_handle, name);
     70   DWORD original_error = ::GetLastError();
     71   if (NULL != handle)
     72     return handle;
     73 
     74   // We don't trust that the IPC can work this early.
     75   if (!SandboxFactory::GetTargetServices()->GetState()->InitCalled())
     76     return NULL;
     77 
     78   do {
     79     void* memory = GetGlobalIPCMemory();
     80     if (NULL == memory)
     81       break;
     82 
     83     uint32 inherit_handle_ipc = inherit_handle;
     84     CountedParameterSet<OpenEventParams> params;
     85     params[OpenEventParams::NAME] = ParamPickerMake(name);
     86     params[OpenEventParams::ACCESS] = ParamPickerMake(desired_access);
     87 
     88     if (!QueryBroker(IPC_OPENEVENT_TAG, params.GetBase()))
     89       break;
     90 
     91     SharedMemIPCClient ipc(memory);
     92     CrossCallReturn answer = {0};
     93     ResultCode code = CrossCall(ipc, IPC_OPENEVENT_TAG, name, desired_access,
     94                                 inherit_handle_ipc, &answer);
     95 
     96     if (SBOX_ALL_OK != code)
     97       break;
     98 
     99     ::SetLastError(answer.win32_result);
    100     return answer.handle;
    101   } while (false);
    102 
    103   ::SetLastError(original_error);
    104   return NULL;
    105 }
    106 
    107 }  // namespace sandbox
    108