1 // Copyright (c) 2011 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.
5 #ifndef SANDBOX_WIN_SRC_SANDBOX_POLICY_BASE_H_
6 #define SANDBOX_WIN_SRC_SANDBOX_POLICY_BASE_H_
13 #include "base/basictypes.h"
14 #include "base/compiler_specific.h"
15 #include "base/strings/string16.h"
16 #include "sandbox/win/src/crosscall_server.h"
17 #include "sandbox/win/src/handle_closer.h"
18 #include "sandbox/win/src/ipc_tags.h"
19 #include "sandbox/win/src/policy_engine_opcodes.h"
20 #include "sandbox/win/src/policy_engine_params.h"
21 #include "sandbox/win/src/sandbox_policy.h"
22 #include "sandbox/win/src/win_utils.h"
26 class AppContainerAttributes
;
31 // We act as a policy dispatcher, implementing the handler for the "ping" IPC,
32 // so we have to provide the appropriate handler on the OnMessageReady method.
33 // There is a static_cast for the handler, and the compiler only performs the
34 // cast if the first base class is Dispatcher.
35 class PolicyBase
: public Dispatcher
, public TargetPolicy
{
40 virtual void AddRef() override
;
41 virtual void Release() override
;
42 virtual ResultCode
SetTokenLevel(TokenLevel initial
,
43 TokenLevel lockdown
) override
;
44 virtual TokenLevel
GetInitialTokenLevel() const override
;
45 virtual TokenLevel
GetLockdownTokenLevel() const override
;
46 virtual ResultCode
SetJobLevel(JobLevel job_level
,
47 uint32 ui_exceptions
) override
;
48 virtual ResultCode
SetJobMemoryLimit(size_t memory_limit
) override
;
49 virtual ResultCode
SetAlternateDesktop(bool alternate_winstation
) override
;
50 virtual base::string16
GetAlternateDesktop() const override
;
51 virtual ResultCode
CreateAlternateDesktop(bool alternate_winstation
) override
;
52 virtual void DestroyAlternateDesktop() override
;
53 virtual ResultCode
SetIntegrityLevel(IntegrityLevel integrity_level
) override
;
54 virtual IntegrityLevel
GetIntegrityLevel() const override
;
55 virtual ResultCode
SetDelayedIntegrityLevel(
56 IntegrityLevel integrity_level
) override
;
57 virtual ResultCode
SetAppContainer(const wchar_t* sid
) override
;
58 virtual ResultCode
SetCapability(const wchar_t* sid
) override
;
59 virtual ResultCode
SetLowBox(const wchar_t* sid
) override
;
60 virtual ResultCode
SetProcessMitigations(MitigationFlags flags
) override
;
61 virtual MitigationFlags
GetProcessMitigations() override
;
62 virtual ResultCode
SetDelayedProcessMitigations(
63 MitigationFlags flags
) override
;
64 virtual MitigationFlags
GetDelayedProcessMitigations() const override
;
65 virtual void SetStrictInterceptions() override
;
66 virtual ResultCode
SetStdoutHandle(HANDLE handle
) override
;
67 virtual ResultCode
SetStderrHandle(HANDLE handle
) override
;
68 virtual ResultCode
AddRule(SubSystem subsystem
, Semantics semantics
,
69 const wchar_t* pattern
) override
;
70 virtual ResultCode
AddDllToUnload(const wchar_t* dll_name
) override
;
71 virtual ResultCode
AddKernelObjectToClose(
72 const base::char16
* handle_type
,
73 const base::char16
* handle_name
) override
;
76 virtual Dispatcher
* OnMessageReady(IPCParams
* ipc
,
77 CallbackGeneric
* callback
) override
;
78 virtual bool SetupService(InterceptionManager
* manager
, int service
) override
;
80 // Creates a Job object with the level specified in a previous call to
82 ResultCode
MakeJobObject(HANDLE
* job
);
84 // Creates the two tokens with the levels specified in a previous call to
86 ResultCode
MakeTokens(HANDLE
* initial
, HANDLE
* lockdown
);
88 const AppContainerAttributes
* GetAppContainer() const;
90 const PSID
GetLowBoxSid() const;
92 // Adds a target process to the internal list of targets. Internally a
93 // call to TargetProcess::Init() is issued.
94 bool AddTarget(TargetProcess
* target
);
96 // Called when there are no more active processes in a Job.
97 // Removes a Job object associated with this policy and the target associated
99 bool OnJobEmpty(HANDLE job
);
101 EvalResult
EvalPolicy(int service
, CountedParameterSetBase
* params
);
103 HANDLE
GetStdoutHandle();
104 HANDLE
GetStderrHandle();
109 // Test IPC providers.
110 bool Ping(IPCInfo
* ipc
, void* cookie
);
112 // Returns a dispatcher from ipc_targets_.
113 Dispatcher
* GetDispatcher(int ipc_tag
);
115 // Sets up interceptions for a new target.
116 bool SetupAllInterceptions(TargetProcess
* target
);
118 // Sets up the handle closer for a new target.
119 bool SetupHandleCloser(TargetProcess
* target
);
121 ResultCode
AddRuleInternal(SubSystem subsystem
,
123 const wchar_t* pattern
);
125 // This lock synchronizes operations on the targets_ collection.
126 CRITICAL_SECTION lock_
;
127 // Maintains the list of target process associated with this policy.
128 // The policy takes ownership of them.
129 typedef std::list
<TargetProcess
*> TargetSet
;
131 // Standard object-lifetime reference counter.
132 volatile LONG ref_count
;
133 // The user-defined global policy settings.
134 TokenLevel lockdown_level_
;
135 TokenLevel initial_level_
;
137 uint32 ui_exceptions_
;
138 size_t memory_limit_
;
139 bool use_alternate_desktop_
;
140 bool use_alternate_winstation_
;
141 // Helps the file system policy initialization.
142 bool file_system_init_
;
143 bool relaxed_interceptions_
;
144 HANDLE stdout_handle_
;
145 HANDLE stderr_handle_
;
146 IntegrityLevel integrity_level_
;
147 IntegrityLevel delayed_integrity_level_
;
148 MitigationFlags mitigations_
;
149 MitigationFlags delayed_mitigations_
;
150 // The array of objects that will answer IPC calls.
151 Dispatcher
* ipc_targets_
[IPC_LAST_TAG
];
152 // Object in charge of generating the low level policy.
153 LowLevelPolicy
* policy_maker_
;
154 // Memory structure that stores the low level policy.
155 PolicyGlobal
* policy_
;
156 // The list of dlls to unload in the target process.
157 std::vector
<base::string16
> blacklisted_dlls_
;
158 // This is a map of handle-types to names that we need to close in the
159 // target process. A null set means we need to close all handles of the
161 HandleCloser handle_closer_
;
162 std::vector
<base::string16
> capabilities_
;
163 scoped_ptr
<AppContainerAttributes
> appcontainer_list_
;
166 static HDESK alternate_desktop_handle_
;
167 static HWINSTA alternate_winstation_handle_
;
168 static IntegrityLevel alternate_desktop_integrity_level_label_
;
170 DISALLOW_COPY_AND_ASSIGN(PolicyBase
);
173 } // namespace sandbox
175 #endif // SANDBOX_WIN_SRC_SANDBOX_POLICY_BASE_H_