security/sandbox/win/src/process_mitigations_test.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial