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 #include "base/strings/stringprintf.h"
6 #include "base/win/scoped_handle.h"
8 #include "base/win/windows_version.h"
9 #include "sandbox/win/src/nt_internals.h"
10 #include "sandbox/win/src/process_mitigations.h"
11 #include "sandbox/win/src/sandbox.h"
12 #include "sandbox/win/src/sandbox_factory.h"
13 #include "sandbox/win/src/target_services.h"
14 #include "sandbox/win/src/win_utils.h"
15 #include "sandbox/win/tests/common/controller.h"
16 #include "testing/gtest/include/gtest/gtest.h"
20 typedef BOOL (WINAPI
*GetProcessDEPPolicyFunction
)(
25 typedef BOOL (WINAPI
*GetProcessMitigationPolicyFunction
)(
27 PROCESS_MITIGATION_POLICY mitigation_policy
,
31 GetProcessMitigationPolicyFunction get_process_mitigation_policy
;
34 bool CheckWin8DepPolicy() {
35 PROCESS_MITIGATION_DEP_POLICY policy
= {};
36 if (!get_process_mitigation_policy(::GetCurrentProcess(), ProcessDEPPolicy
,
37 &policy
, sizeof(policy
))) {
40 return policy
.Enable
&& policy
.Permanent
;
42 #endif // !defined(_WIN64)
45 bool CheckWin8AslrPolicy() {
46 PROCESS_MITIGATION_ASLR_POLICY policy
= {};
47 if (!get_process_mitigation_policy(::GetCurrentProcess(), ProcessASLRPolicy
,
48 &policy
, sizeof(policy
))) {
51 return policy
.EnableForceRelocateImages
&& policy
.DisallowStrippedImages
;
53 #endif // defined(NDEBUG)
55 bool CheckWin8StrictHandlePolicy() {
56 PROCESS_MITIGATION_STRICT_HANDLE_CHECK_POLICY policy
= {};
57 if (!get_process_mitigation_policy(::GetCurrentProcess(),
58 ProcessStrictHandleCheckPolicy
,
59 &policy
, sizeof(policy
))) {
62 return policy
.RaiseExceptionOnInvalidHandleReference
&&
63 policy
.HandleExceptionsPermanentlyEnabled
;
66 bool CheckWin8Win32CallPolicy() {
67 PROCESS_MITIGATION_SYSTEM_CALL_DISABLE_POLICY policy
= {};
68 if (!get_process_mitigation_policy(::GetCurrentProcess(),
69 ProcessSystemCallDisablePolicy
,
70 &policy
, sizeof(policy
))) {
73 return policy
.DisallowWin32kSystemCalls
;
76 bool CheckWin8DllExtensionPolicy() {
77 PROCESS_MITIGATION_EXTENSION_POINT_DISABLE_POLICY policy
= {};
78 if (!get_process_mitigation_policy(::GetCurrentProcess(),
79 ProcessExtensionPointDisablePolicy
,
80 &policy
, sizeof(policy
))) {
83 return policy
.DisableExtensionPoints
;
90 SBOX_TESTS_COMMAND
int CheckWin8(int argc
, wchar_t **argv
) {
91 get_process_mitigation_policy
=
92 reinterpret_cast<GetProcessMitigationPolicyFunction
>(
93 ::GetProcAddress(::GetModuleHandleW(L
"kernel32.dll"),
94 "GetProcessMitigationPolicy"));
95 if (!get_process_mitigation_policy
)
96 return SBOX_TEST_NOT_FOUND
;
98 #if !defined(_WIN64) // DEP is always enabled on 64-bit.
99 if (!CheckWin8DepPolicy())
100 return SBOX_TEST_FIRST_ERROR
;
103 #if defined(NDEBUG) // ASLR cannot be forced in debug builds.
104 if (!CheckWin8AslrPolicy())
105 return SBOX_TEST_SECOND_ERROR
;
108 if (!CheckWin8StrictHandlePolicy())
109 return SBOX_TEST_THIRD_ERROR
;
111 if (!CheckWin8DllExtensionPolicy())
112 return SBOX_TEST_FIFTH_ERROR
;
114 return SBOX_TEST_SUCCEEDED
;
117 TEST(ProcessMitigationsTest
, CheckWin8
) {
118 if (base::win::GetVersion() < base::win::VERSION_WIN8
)
122 sandbox::TargetPolicy
* policy
= runner
.GetPolicy();
124 sandbox::MitigationFlags mitigations
= MITIGATION_DEP
|
125 MITIGATION_DEP_NO_ATL_THUNK
|
126 MITIGATION_EXTENSION_DLL_DISABLE
;
127 #if defined(NDEBUG) // ASLR cannot be forced in debug builds.
128 mitigations
|= MITIGATION_RELOCATE_IMAGE
|
129 MITIGATION_RELOCATE_IMAGE_REQUIRED
;
132 EXPECT_EQ(policy
->SetProcessMitigations(mitigations
), SBOX_ALL_OK
);
134 mitigations
|= MITIGATION_STRICT_HANDLE_CHECKS
;
136 EXPECT_EQ(policy
->SetDelayedProcessMitigations(mitigations
), SBOX_ALL_OK
);
138 EXPECT_EQ(SBOX_TEST_SUCCEEDED
, runner
.RunTest(L
"CheckWin8"));
142 SBOX_TESTS_COMMAND
int CheckDep(int argc
, wchar_t **argv
) {
143 GetProcessDEPPolicyFunction get_process_dep_policy
=
144 reinterpret_cast<GetProcessDEPPolicyFunction
>(
145 ::GetProcAddress(::GetModuleHandleW(L
"kernel32.dll"),
146 "GetProcessDEPPolicy"));
147 if (get_process_dep_policy
) {
148 BOOL is_permanent
= FALSE
;
151 if (!get_process_dep_policy(::GetCurrentProcess(), &dep_flags
,
153 return SBOX_TEST_FIRST_ERROR
;
156 if (!(dep_flags
& PROCESS_DEP_ENABLE
) || !is_permanent
)
157 return SBOX_TEST_SECOND_ERROR
;
160 NtQueryInformationProcessFunction query_information_process
= NULL
;
161 ResolveNTFunctionPtr("NtQueryInformationProcess",
162 &query_information_process
);
163 if (!query_information_process
)
164 return SBOX_TEST_NOT_FOUND
;
168 if (!SUCCEEDED(query_information_process(::GetCurrentProcess(),
169 ProcessExecuteFlags
, &dep_flags
,
170 sizeof(dep_flags
), &size
))) {
171 return SBOX_TEST_THIRD_ERROR
;
174 static const int MEM_EXECUTE_OPTION_DISABLE
= 2;
175 static const int MEM_EXECUTE_OPTION_PERMANENT
= 8;
178 if (dep_flags
!= (MEM_EXECUTE_OPTION_DISABLE
|
179 MEM_EXECUTE_OPTION_PERMANENT
)) {
180 return SBOX_TEST_FOURTH_ERROR
;
184 return SBOX_TEST_SUCCEEDED
;
187 #if !defined(_WIN64) // DEP is always enabled on 64-bit.
188 TEST(ProcessMitigationsTest
, CheckDep
) {
189 if (base::win::GetVersion() > base::win::VERSION_WIN7
)
193 sandbox::TargetPolicy
* policy
= runner
.GetPolicy();
195 EXPECT_EQ(policy
->SetProcessMitigations(
197 MITIGATION_DEP_NO_ATL_THUNK
|
200 EXPECT_EQ(SBOX_TEST_SUCCEEDED
, runner
.RunTest(L
"CheckDep"));
204 SBOX_TESTS_COMMAND
int CheckWin8Lockdown(int argc
, wchar_t **argv
) {
205 get_process_mitigation_policy
=
206 reinterpret_cast<GetProcessMitigationPolicyFunction
>(
207 ::GetProcAddress(::GetModuleHandleW(L
"kernel32.dll"),
208 "GetProcessMitigationPolicy"));
209 if (!get_process_mitigation_policy
)
210 return SBOX_TEST_NOT_FOUND
;
212 if (!CheckWin8Win32CallPolicy())
213 return SBOX_TEST_FIRST_ERROR
;
214 return SBOX_TEST_SUCCEEDED
;
217 // This test validates that setting the MITIGATION_WIN32K_DISABLE mitigation on
218 // the target process causes the launch to fail in process initialization.
219 // The test process itself links against user32/gdi32.
220 TEST(ProcessMitigationsTest
, CheckWin8Win32KLockDownFailure
) {
221 if (base::win::GetVersion() < base::win::VERSION_WIN8
)
225 sandbox::TargetPolicy
* policy
= runner
.GetPolicy();
227 EXPECT_EQ(policy
->SetProcessMitigations(MITIGATION_WIN32K_DISABLE
),
229 EXPECT_NE(SBOX_TEST_SUCCEEDED
, runner
.RunTest(L
"CheckWin8Lockdown"));
232 // This test validates that setting the MITIGATION_WIN32K_DISABLE mitigation
233 // along with the policy to fake user32 and gdi32 initialization successfully
234 // launches the target process.
235 // The test process itself links against user32/gdi32.
236 TEST(ProcessMitigationsTest
, CheckWin8Win32KLockDownSuccess
) {
237 if (base::win::GetVersion() < base::win::VERSION_WIN8
)
241 sandbox::TargetPolicy
* policy
= runner
.GetPolicy();
243 EXPECT_EQ(policy
->SetProcessMitigations(MITIGATION_WIN32K_DISABLE
),
245 EXPECT_EQ(policy
->AddRule(sandbox::TargetPolicy::SUBSYS_WIN32K_LOCKDOWN
,
246 sandbox::TargetPolicy::FAKE_USER_GDI_INIT
, NULL
),
247 sandbox::SBOX_ALL_OK
);
248 EXPECT_EQ(SBOX_TEST_SUCCEEDED
, runner
.RunTest(L
"CheckWin8Lockdown"));
251 } // namespace sandbox