1// Copyright (c) 2006-2010 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_dispatcher.h"
6
7#include "sandbox/win/src/crosscall_client.h"
8#include "sandbox/win/src/interception.h"
9#include "sandbox/win/src/interceptors.h"
10#include "sandbox/win/src/ipc_tags.h"
11#include "sandbox/win/src/policy_broker.h"
12#include "sandbox/win/src/policy_params.h"
13#include "sandbox/win/src/sandbox.h"
14#include "sandbox/win/src/sync_interception.h"
15#include "sandbox/win/src/sync_policy.h"
16
17namespace sandbox {
18
19SyncDispatcher::SyncDispatcher(PolicyBase* policy_base)
20    : policy_base_(policy_base) {
21  static const IPCCall create_params = {
22    {IPC_CREATEEVENT_TAG, WCHAR_TYPE, ULONG_TYPE, ULONG_TYPE},
23    reinterpret_cast<CallbackGeneric>(&SyncDispatcher::CreateEvent)
24  };
25
26  static const IPCCall open_params = {
27    {IPC_OPENEVENT_TAG, WCHAR_TYPE, ULONG_TYPE, ULONG_TYPE},
28    reinterpret_cast<CallbackGeneric>(&SyncDispatcher::OpenEvent)
29  };
30
31  ipc_calls_.push_back(create_params);
32  ipc_calls_.push_back(open_params);
33}
34
35bool SyncDispatcher::SetupService(InterceptionManager* manager,
36                                  int service) {
37  if (IPC_CREATEEVENT_TAG == service)
38      return INTERCEPT_EAT(manager, L"kernel32.dll", CreateEventW,
39                           CREATE_EVENT_ID, 20);
40
41  if (IPC_OPENEVENT_TAG == service)
42    return INTERCEPT_EAT(manager, L"kernel32.dll", OpenEventW,
43                         OPEN_EVENT_ID, 16);
44
45  return false;
46}
47
48bool SyncDispatcher::CreateEvent(IPCInfo* ipc, std::wstring* name,
49                                 DWORD manual_reset, DWORD initial_state) {
50  const wchar_t* event_name = name->c_str();
51  CountedParameterSet<NameBased> params;
52  params[NameBased::NAME] = ParamPickerMake(event_name);
53
54  EvalResult result = policy_base_->EvalPolicy(IPC_CREATEEVENT_TAG,
55                                               params.GetBase());
56  HANDLE handle = NULL;
57  DWORD ret = SyncPolicy::CreateEventAction(result, *ipc->client_info, *name,
58                                            manual_reset, initial_state,
59                                            &handle);
60  // Return operation status on the IPC.
61  ipc->return_info.win32_result = ret;
62  ipc->return_info.handle = handle;
63  return true;
64}
65
66bool SyncDispatcher::OpenEvent(IPCInfo* ipc, std::wstring* name,
67                               DWORD desired_access, DWORD inherit_handle) {
68  const wchar_t* event_name = name->c_str();
69
70  CountedParameterSet<OpenEventParams> params;
71  params[OpenEventParams::NAME] = ParamPickerMake(event_name);
72  params[OpenEventParams::ACCESS] = ParamPickerMake(desired_access);
73
74  EvalResult result = policy_base_->EvalPolicy(IPC_OPENEVENT_TAG,
75                                               params.GetBase());
76  HANDLE handle = NULL;
77  DWORD ret = SyncPolicy::OpenEventAction(result, *ipc->client_info, *name,
78                                          desired_access, inherit_handle,
79                                          &handle);
80  // Return operation status on the IPC.
81  ipc->return_info.win32_result = ret;
82  ipc->return_info.handle = handle;
83  return true;
84}
85
86}  // namespace sandbox
87