OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "base/stringprintf.h" |
| 6 #include "base/win/scoped_handle.h" |
| 7 |
| 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/sandbox_utils.h" |
| 14 #include "sandbox/win/src/target_services.h" |
| 15 #include "sandbox/win/src/win_utils.h" |
| 16 #include "sandbox/win/tests/common/controller.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 namespace { |
| 20 |
| 21 typedef BOOL (WINAPI *GetProcessDEPPolicyFunction)( |
| 22 HANDLE process, |
| 23 LPDWORD flags, |
| 24 PBOOL permanent); |
| 25 |
| 26 typedef BOOL (WINAPI *GetProcessMitigationPolicyFunction)( |
| 27 HANDLE process, |
| 28 PROCESS_MITIGATION_POLICY mitigation_policy, |
| 29 PVOID buffer, |
| 30 SIZE_T length); |
| 31 |
| 32 GetProcessMitigationPolicyFunction get_process_mitigation_policy; |
| 33 |
| 34 bool CheckWin8DepPolicy() { |
| 35 PROCESS_MITIGATION_DEP_POLICY policy; |
| 36 if (!get_process_mitigation_policy(::GetCurrentProcess(), ProcessDEPPolicy, |
| 37 &policy, sizeof(policy))) { |
| 38 return false; |
| 39 } |
| 40 return policy.Enable && policy.Permanent; |
| 41 } |
| 42 |
| 43 bool CheckWin8AslrPolicy() { |
| 44 PROCESS_MITIGATION_ASLR_POLICY policy; |
| 45 if (!get_process_mitigation_policy(::GetCurrentProcess(), ProcessASLRPolicy, |
| 46 &policy, sizeof(policy))) { |
| 47 return false; |
| 48 } |
| 49 return policy.EnableForceRelocateImages && policy.DisallowStrippedImages; |
| 50 } |
| 51 |
| 52 bool CheckWin8StrictHandlePolicy() { |
| 53 PROCESS_MITIGATION_STRICT_HANDLE_CHECK_POLICY policy; |
| 54 if (!get_process_mitigation_policy(::GetCurrentProcess(), |
| 55 ProcessStrictHandleCheckPolicy, |
| 56 &policy, sizeof(policy))) { |
| 57 return false; |
| 58 } |
| 59 return policy.RaiseExceptionOnInvalidHandleReference && |
| 60 policy.HandleExceptionsPermanentlyEnabled; |
| 61 } |
| 62 |
| 63 bool CheckWin8Win32CallPolicy() { |
| 64 PROCESS_MITIGATION_SYSTEM_CALL_DISABLE_POLICY policy; |
| 65 if (!get_process_mitigation_policy(::GetCurrentProcess(), |
| 66 ProcessSystemCallDisablePolicy, |
| 67 &policy, sizeof(policy))) { |
| 68 return false; |
| 69 } |
| 70 return policy.DisallowWin32kSystemCalls; |
| 71 } |
| 72 |
| 73 bool CheckWin8DllExtensionPolicy() { |
| 74 PROCESS_MITIGATION_EXTENSION_POINT_DISABLE_POLICY policy; |
| 75 if (!get_process_mitigation_policy(::GetCurrentProcess(), |
| 76 ProcessExtensionPointDisablePolicy, |
| 77 &policy, sizeof(policy))) { |
| 78 return false; |
| 79 } |
| 80 return policy.DisableExtensionPoints; |
| 81 } |
| 82 |
| 83 } // namespace |
| 84 |
| 85 namespace sandbox { |
| 86 |
| 87 SBOX_TESTS_COMMAND int CheckWin8(int argc, wchar_t **argv) { |
| 88 get_process_mitigation_policy = |
| 89 reinterpret_cast<GetProcessMitigationPolicyFunction>( |
| 90 ::GetProcAddress(::GetModuleHandleW(L"kernel32.dll"), |
| 91 "GetProcessMitigationPolicy")); |
| 92 |
| 93 if (!get_process_mitigation_policy) |
| 94 return SBOX_TEST_NOT_FOUND; |
| 95 |
| 96 if (!CheckWin8DepPolicy()) |
| 97 return SBOX_TEST_FIRST_ERROR; |
| 98 |
| 99 if (!CheckWin8AslrPolicy()) |
| 100 return SBOX_TEST_SECOND_ERROR; |
| 101 |
| 102 if (!CheckWin8StrictHandlePolicy()) |
| 103 return SBOX_TEST_THIRD_ERROR; |
| 104 |
| 105 if (!CheckWin8Win32CallPolicy()) |
| 106 return SBOX_TEST_FOURTH_ERROR; |
| 107 |
| 108 if (!CheckWin8DllExtensionPolicy()) |
| 109 return SBOX_TEST_FIFTH_ERROR; |
| 110 |
| 111 return SBOX_TEST_SUCCEEDED; |
| 112 } |
| 113 |
| 114 TEST(ProcessMitigationsTest, CheckWin8) { |
| 115 if (base::win::GetVersion() < base::win::VERSION_WIN8) |
| 116 return; |
| 117 |
| 118 TestRunner runner; |
| 119 sandbox::TargetPolicy* policy = runner.GetPolicy(); |
| 120 |
| 121 EXPECT_EQ(policy->SetProcessMitigations(MITIGATION_DEP | |
| 122 MITIGATION_DEP_NO_ATL_THUNK | |
| 123 MITIGATION_RELOCATE_IMAGE | |
| 124 MITIGATION_RELOCATE_IMAGE_REQUIRED | |
| 125 MITIGATION_EXTENSION_DLL_DISABLE), |
| 126 SBOX_ALL_OK); |
| 127 |
| 128 EXPECT_EQ(policy->SetDelayedProcessMitigations( |
| 129 MITIGATION_STRICT_HANDLE_CHECKS | |
| 130 MITIGATION_WIN32K_DISABLE), |
| 131 SBOX_ALL_OK); |
| 132 |
| 133 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"CheckWin8")); |
| 134 } |
| 135 |
| 136 |
| 137 SBOX_TESTS_COMMAND int CheckDep(int argc, wchar_t **argv) { |
| 138 #ifndef _WIN64 // DEP is always enabled on 64-bit. |
| 139 GetProcessDEPPolicyFunction get_process_dep_policy = |
| 140 reinterpret_cast<GetProcessDEPPolicyFunction>( |
| 141 ::GetProcAddress(::GetModuleHandleW(L"kernel32.dll"), |
| 142 "GetProcessDEPPolicy")); |
| 143 if (get_process_dep_policy) { |
| 144 BOOL is_permanent = FALSE; |
| 145 DWORD dep_flags = 0; |
| 146 |
| 147 if (!get_process_dep_policy(::GetCurrentProcess(), &dep_flags, |
| 148 &is_permanent)) { |
| 149 return SBOX_TEST_FIRST_ERROR; |
| 150 } |
| 151 |
| 152 if (!(dep_flags & PROCESS_DEP_ENABLE) || !is_permanent) |
| 153 return SBOX_TEST_SECOND_ERROR; |
| 154 |
| 155 } else { |
| 156 NtQueryInformationProcessFunction query_information_process = NULL; |
| 157 ResolveNTFunctionPtr("NtQueryInformationProcess", |
| 158 &query_information_process); |
| 159 if (!query_information_process) |
| 160 return SBOX_TEST_NOT_FOUND; |
| 161 |
| 162 ULONG size = 0; |
| 163 ULONG dep_flags = 0; |
| 164 if (!SUCCEEDED(query_information_process(::GetCurrentProcess(), |
| 165 ProcessExecuteFlags, &dep_flags, |
| 166 sizeof(dep_flags), &size))) { |
| 167 return SBOX_TEST_THIRD_ERROR; |
| 168 } |
| 169 |
| 170 const int MEM_EXECUTE_OPTION_ENABLE = 1; |
| 171 const int MEM_EXECUTE_OPTION_DISABLE = 2; |
| 172 const int MEM_EXECUTE_OPTION_ATL7_THUNK_EMULATION = 4; |
| 173 const int MEM_EXECUTE_OPTION_PERMANENT = 8; |
| 174 dep_flags &= 0xff; |
| 175 |
| 176 if (dep_flags != (MEM_EXECUTE_OPTION_DISABLE | |
| 177 MEM_EXECUTE_OPTION_PERMANENT)) { |
| 178 return SBOX_TEST_FOURTH_ERROR; |
| 179 } |
| 180 } |
| 181 #endif |
| 182 |
| 183 return SBOX_TEST_SUCCEEDED; |
| 184 } |
| 185 |
| 186 TEST(ProcessMitigationsTest, CheckDep) { |
| 187 if (!IsXPSP2OrLater() || base::win::GetVersion() > base::win::VERSION_WIN7) |
| 188 return; |
| 189 |
| 190 TestRunner runner; |
| 191 sandbox::TargetPolicy* policy = runner.GetPolicy(); |
| 192 |
| 193 EXPECT_EQ(policy->SetProcessMitigations(MITIGATION_DEP | |
| 194 MITIGATION_DEP_NO_ATL_THUNK | |
| 195 MITIGATION_SEHOP), |
| 196 SBOX_ALL_OK); |
| 197 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"CheckDep")); |
| 198 } |
| 199 |
| 200 } // namespace sandbox |
| 201 |
OLD | NEW |