Wed, 31 Dec 2014 06:09:35 +0100
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) 2012 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 "sandbox/win/src/process_mitigations.h" |
michael@0 | 6 | |
michael@0 | 7 | #include <algorithm> |
michael@0 | 8 | |
michael@0 | 9 | #include "base/win/windows_version.h" |
michael@0 | 10 | #include "sandbox/win/src/nt_internals.h" |
michael@0 | 11 | #include "sandbox/win/src/sandbox_utils.h" |
michael@0 | 12 | #include "sandbox/win/src/win_utils.h" |
michael@0 | 13 | |
michael@0 | 14 | namespace { |
michael@0 | 15 | |
michael@0 | 16 | // Functions for enabling policies. |
michael@0 | 17 | typedef BOOL (WINAPI *SetProcessDEPPolicyFunction)(DWORD dwFlags); |
michael@0 | 18 | |
michael@0 | 19 | typedef BOOL (WINAPI *SetProcessMitigationPolicyFunction)( |
michael@0 | 20 | PROCESS_MITIGATION_POLICY mitigation_policy, |
michael@0 | 21 | PVOID buffer, |
michael@0 | 22 | SIZE_T length); |
michael@0 | 23 | |
michael@0 | 24 | typedef BOOL (WINAPI *SetDefaultDllDirectoriesFunction)( |
michael@0 | 25 | DWORD DirectoryFlags); |
michael@0 | 26 | |
michael@0 | 27 | } // namespace |
michael@0 | 28 | |
michael@0 | 29 | namespace sandbox { |
michael@0 | 30 | |
michael@0 | 31 | bool ApplyProcessMitigationsToCurrentProcess(MitigationFlags flags) { |
michael@0 | 32 | if (!CanSetProcessMitigationsPostStartup(flags)) |
michael@0 | 33 | return false; |
michael@0 | 34 | |
michael@0 | 35 | // We can't apply anything before Win XP, so just return cleanly. |
michael@0 | 36 | if (!IsXPSP2OrLater()) |
michael@0 | 37 | return true; |
michael@0 | 38 | |
michael@0 | 39 | base::win::Version version = base::win::GetVersion(); |
michael@0 | 40 | HMODULE module = ::GetModuleHandleA("kernel32.dll"); |
michael@0 | 41 | |
michael@0 | 42 | if (version >= base::win::VERSION_VISTA && |
michael@0 | 43 | (flags & MITIGATION_DLL_SEARCH_ORDER)) { |
michael@0 | 44 | SetDefaultDllDirectoriesFunction set_default_dll_directories = |
michael@0 | 45 | reinterpret_cast<SetDefaultDllDirectoriesFunction>( |
michael@0 | 46 | ::GetProcAddress(module, "SetDefaultDllDirectories")); |
michael@0 | 47 | |
michael@0 | 48 | // Check for SetDefaultDllDirectories since it requires KB2533623. |
michael@0 | 49 | if (set_default_dll_directories) { |
michael@0 | 50 | if (!set_default_dll_directories(LOAD_LIBRARY_SEARCH_DEFAULT_DIRS) && |
michael@0 | 51 | ERROR_ACCESS_DENIED != ::GetLastError()) { |
michael@0 | 52 | return false; |
michael@0 | 53 | } |
michael@0 | 54 | } |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | // Set the heap to terminate on corruption |
michael@0 | 58 | if (version >= base::win::VERSION_VISTA && |
michael@0 | 59 | (flags & MITIGATION_HEAP_TERMINATE)) { |
michael@0 | 60 | if (!::HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, |
michael@0 | 61 | NULL, 0) && |
michael@0 | 62 | ERROR_ACCESS_DENIED != ::GetLastError()) { |
michael@0 | 63 | return false; |
michael@0 | 64 | } |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | #if !defined(_WIN64) // DEP is always enabled on 64-bit. |
michael@0 | 68 | if (flags & MITIGATION_DEP) { |
michael@0 | 69 | DWORD dep_flags = PROCESS_DEP_ENABLE; |
michael@0 | 70 | // DEP support is quirky on XP, so don't force a failure in that case. |
michael@0 | 71 | const bool return_on_fail = version >= base::win::VERSION_VISTA; |
michael@0 | 72 | |
michael@0 | 73 | if (flags & MITIGATION_DEP_NO_ATL_THUNK) |
michael@0 | 74 | dep_flags |= PROCESS_DEP_DISABLE_ATL_THUNK_EMULATION; |
michael@0 | 75 | |
michael@0 | 76 | SetProcessDEPPolicyFunction set_process_dep_policy = |
michael@0 | 77 | reinterpret_cast<SetProcessDEPPolicyFunction>( |
michael@0 | 78 | ::GetProcAddress(module, "SetProcessDEPPolicy")); |
michael@0 | 79 | if (set_process_dep_policy) { |
michael@0 | 80 | if (!set_process_dep_policy(dep_flags) && |
michael@0 | 81 | ERROR_ACCESS_DENIED != ::GetLastError() && return_on_fail) { |
michael@0 | 82 | return false; |
michael@0 | 83 | } |
michael@0 | 84 | } else { |
michael@0 | 85 | // We're on XP sp2, so use the less standard approach. |
michael@0 | 86 | // For reference: http://www.uninformed.org/?v=2&a=4 |
michael@0 | 87 | const int MEM_EXECUTE_OPTION_ENABLE = 1; |
michael@0 | 88 | const int MEM_EXECUTE_OPTION_DISABLE = 2; |
michael@0 | 89 | const int MEM_EXECUTE_OPTION_ATL7_THUNK_EMULATION = 4; |
michael@0 | 90 | const int MEM_EXECUTE_OPTION_PERMANENT = 8; |
michael@0 | 91 | |
michael@0 | 92 | NtSetInformationProcessFunction set_information_process = NULL; |
michael@0 | 93 | ResolveNTFunctionPtr("NtSetInformationProcess", |
michael@0 | 94 | &set_information_process); |
michael@0 | 95 | if (!set_information_process) |
michael@0 | 96 | return false; |
michael@0 | 97 | ULONG dep = MEM_EXECUTE_OPTION_DISABLE | MEM_EXECUTE_OPTION_PERMANENT; |
michael@0 | 98 | if (!(dep_flags & PROCESS_DEP_DISABLE_ATL_THUNK_EMULATION)) |
michael@0 | 99 | dep |= MEM_EXECUTE_OPTION_ATL7_THUNK_EMULATION; |
michael@0 | 100 | if (!SUCCEEDED(set_information_process(GetCurrentProcess(), |
michael@0 | 101 | ProcessExecuteFlags, |
michael@0 | 102 | &dep, sizeof(dep))) && |
michael@0 | 103 | ERROR_ACCESS_DENIED != ::GetLastError() && return_on_fail) { |
michael@0 | 104 | return false; |
michael@0 | 105 | } |
michael@0 | 106 | } |
michael@0 | 107 | } |
michael@0 | 108 | #endif |
michael@0 | 109 | |
michael@0 | 110 | // This is all we can do in Win7 and below. |
michael@0 | 111 | if (version < base::win::VERSION_WIN8) |
michael@0 | 112 | return true; |
michael@0 | 113 | |
michael@0 | 114 | SetProcessMitigationPolicyFunction set_process_mitigation_policy = |
michael@0 | 115 | reinterpret_cast<SetProcessMitigationPolicyFunction>( |
michael@0 | 116 | ::GetProcAddress(module, "SetProcessMitigationPolicy")); |
michael@0 | 117 | if (!set_process_mitigation_policy) |
michael@0 | 118 | return false; |
michael@0 | 119 | |
michael@0 | 120 | // Enable ASLR policies. |
michael@0 | 121 | if (flags & MITIGATION_RELOCATE_IMAGE) { |
michael@0 | 122 | PROCESS_MITIGATION_ASLR_POLICY policy = { 0 }; |
michael@0 | 123 | policy.EnableForceRelocateImages = true; |
michael@0 | 124 | policy.DisallowStrippedImages = (flags & |
michael@0 | 125 | MITIGATION_RELOCATE_IMAGE_REQUIRED) == |
michael@0 | 126 | MITIGATION_RELOCATE_IMAGE_REQUIRED; |
michael@0 | 127 | |
michael@0 | 128 | if (!set_process_mitigation_policy(ProcessASLRPolicy, &policy, |
michael@0 | 129 | sizeof(policy)) && |
michael@0 | 130 | ERROR_ACCESS_DENIED != ::GetLastError()) { |
michael@0 | 131 | return false; |
michael@0 | 132 | } |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | // Enable strict handle policies. |
michael@0 | 136 | if (flags & MITIGATION_STRICT_HANDLE_CHECKS) { |
michael@0 | 137 | PROCESS_MITIGATION_STRICT_HANDLE_CHECK_POLICY policy = { 0 }; |
michael@0 | 138 | policy.HandleExceptionsPermanentlyEnabled = |
michael@0 | 139 | policy.RaiseExceptionOnInvalidHandleReference = true; |
michael@0 | 140 | |
michael@0 | 141 | if (!set_process_mitigation_policy(ProcessStrictHandleCheckPolicy, &policy, |
michael@0 | 142 | sizeof(policy)) && |
michael@0 | 143 | ERROR_ACCESS_DENIED != ::GetLastError()) { |
michael@0 | 144 | return false; |
michael@0 | 145 | } |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | // Enable system call policies. |
michael@0 | 149 | if (flags & MITIGATION_WIN32K_DISABLE) { |
michael@0 | 150 | PROCESS_MITIGATION_SYSTEM_CALL_DISABLE_POLICY policy = { 0 }; |
michael@0 | 151 | policy.DisallowWin32kSystemCalls = true; |
michael@0 | 152 | |
michael@0 | 153 | if (!set_process_mitigation_policy(ProcessSystemCallDisablePolicy, &policy, |
michael@0 | 154 | sizeof(policy)) && |
michael@0 | 155 | ERROR_ACCESS_DENIED != ::GetLastError()) { |
michael@0 | 156 | return false; |
michael@0 | 157 | } |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | // Enable system call policies. |
michael@0 | 161 | if (flags & MITIGATION_EXTENSION_DLL_DISABLE) { |
michael@0 | 162 | PROCESS_MITIGATION_EXTENSION_POINT_DISABLE_POLICY policy = { 0 }; |
michael@0 | 163 | policy.DisableExtensionPoints = true; |
michael@0 | 164 | |
michael@0 | 165 | if (!set_process_mitigation_policy(ProcessExtensionPointDisablePolicy, |
michael@0 | 166 | &policy, sizeof(policy)) && |
michael@0 | 167 | ERROR_ACCESS_DENIED != ::GetLastError()) { |
michael@0 | 168 | return false; |
michael@0 | 169 | } |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | return true; |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | void ConvertProcessMitigationsToPolicy(MitigationFlags flags, |
michael@0 | 176 | DWORD64* policy_flags, size_t* size) { |
michael@0 | 177 | base::win::Version version = base::win::GetVersion(); |
michael@0 | 178 | |
michael@0 | 179 | *policy_flags = 0; |
michael@0 | 180 | #if defined(_WIN64) |
michael@0 | 181 | *size = sizeof(*policy_flags); |
michael@0 | 182 | #elif defined(_M_IX86) |
michael@0 | 183 | // A 64-bit flags attribute is illegal on 32-bit Win 7 and below. |
michael@0 | 184 | if (version < base::win::VERSION_WIN8) |
michael@0 | 185 | *size = sizeof(DWORD); |
michael@0 | 186 | else |
michael@0 | 187 | *size = sizeof(*policy_flags); |
michael@0 | 188 | #else |
michael@0 | 189 | #error This platform is not supported. |
michael@0 | 190 | #endif |
michael@0 | 191 | |
michael@0 | 192 | // Nothing for Win XP or Vista. |
michael@0 | 193 | if (version <= base::win::VERSION_VISTA) |
michael@0 | 194 | return; |
michael@0 | 195 | |
michael@0 | 196 | // DEP and SEHOP are not valid for 64-bit Windows |
michael@0 | 197 | #if !defined(_WIN64) |
michael@0 | 198 | if (flags & MITIGATION_DEP) { |
michael@0 | 199 | *policy_flags |= PROCESS_CREATION_MITIGATION_POLICY_DEP_ENABLE; |
michael@0 | 200 | if (!(flags & MITIGATION_DEP_NO_ATL_THUNK)) |
michael@0 | 201 | *policy_flags |= PROCESS_CREATION_MITIGATION_POLICY_DEP_ATL_THUNK_ENABLE; |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | if (flags & MITIGATION_SEHOP) |
michael@0 | 205 | *policy_flags |= PROCESS_CREATION_MITIGATION_POLICY_SEHOP_ENABLE; |
michael@0 | 206 | #endif |
michael@0 | 207 | |
michael@0 | 208 | // Win 7 |
michael@0 | 209 | if (version < base::win::VERSION_WIN8) |
michael@0 | 210 | return; |
michael@0 | 211 | |
michael@0 | 212 | if (flags & MITIGATION_RELOCATE_IMAGE) { |
michael@0 | 213 | *policy_flags |= |
michael@0 | 214 | PROCESS_CREATION_MITIGATION_POLICY_FORCE_RELOCATE_IMAGES_ALWAYS_ON; |
michael@0 | 215 | if (flags & MITIGATION_RELOCATE_IMAGE_REQUIRED) { |
michael@0 | 216 | *policy_flags |= |
michael@0 | 217 | PROCESS_CREATION_MITIGATION_POLICY_FORCE_RELOCATE_IMAGES_ALWAYS_ON_REQ_RELOCS; |
michael@0 | 218 | } |
michael@0 | 219 | } |
michael@0 | 220 | |
michael@0 | 221 | if (flags & MITIGATION_HEAP_TERMINATE) { |
michael@0 | 222 | *policy_flags |= |
michael@0 | 223 | PROCESS_CREATION_MITIGATION_POLICY_HEAP_TERMINATE_ALWAYS_ON; |
michael@0 | 224 | } |
michael@0 | 225 | |
michael@0 | 226 | if (flags & MITIGATION_BOTTOM_UP_ASLR) { |
michael@0 | 227 | *policy_flags |= |
michael@0 | 228 | PROCESS_CREATION_MITIGATION_POLICY_BOTTOM_UP_ASLR_ALWAYS_ON; |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | if (flags & MITIGATION_HIGH_ENTROPY_ASLR) { |
michael@0 | 232 | *policy_flags |= |
michael@0 | 233 | PROCESS_CREATION_MITIGATION_POLICY_HIGH_ENTROPY_ASLR_ALWAYS_ON; |
michael@0 | 234 | } |
michael@0 | 235 | |
michael@0 | 236 | if (flags & MITIGATION_STRICT_HANDLE_CHECKS) { |
michael@0 | 237 | *policy_flags |= |
michael@0 | 238 | PROCESS_CREATION_MITIGATION_POLICY_STRICT_HANDLE_CHECKS_ALWAYS_ON; |
michael@0 | 239 | } |
michael@0 | 240 | |
michael@0 | 241 | if (flags & MITIGATION_WIN32K_DISABLE) { |
michael@0 | 242 | *policy_flags |= |
michael@0 | 243 | PROCESS_CREATION_MITIGATION_POLICY_WIN32K_SYSTEM_CALL_DISABLE_ALWAYS_ON; |
michael@0 | 244 | } |
michael@0 | 245 | |
michael@0 | 246 | if (flags & MITIGATION_EXTENSION_DLL_DISABLE) { |
michael@0 | 247 | *policy_flags |= |
michael@0 | 248 | PROCESS_CREATION_MITIGATION_POLICY_EXTENSION_POINT_DISABLE_ALWAYS_ON; |
michael@0 | 249 | } |
michael@0 | 250 | } |
michael@0 | 251 | |
michael@0 | 252 | MitigationFlags FilterPostStartupProcessMitigations(MitigationFlags flags) { |
michael@0 | 253 | // Anything prior to XP SP2. |
michael@0 | 254 | if (!IsXPSP2OrLater()) |
michael@0 | 255 | return 0; |
michael@0 | 256 | |
michael@0 | 257 | base::win::Version version = base::win::GetVersion(); |
michael@0 | 258 | |
michael@0 | 259 | // Windows XP SP2+. |
michael@0 | 260 | if (version < base::win::VERSION_VISTA) { |
michael@0 | 261 | return flags & (MITIGATION_DEP | |
michael@0 | 262 | MITIGATION_DEP_NO_ATL_THUNK); |
michael@0 | 263 | |
michael@0 | 264 | // Windows Vista |
michael@0 | 265 | if (version < base::win::VERSION_WIN7) { |
michael@0 | 266 | return flags & (MITIGATION_DEP | |
michael@0 | 267 | MITIGATION_DEP_NO_ATL_THUNK | |
michael@0 | 268 | MITIGATION_BOTTOM_UP_ASLR | |
michael@0 | 269 | MITIGATION_DLL_SEARCH_ORDER | |
michael@0 | 270 | MITIGATION_HEAP_TERMINATE); |
michael@0 | 271 | } |
michael@0 | 272 | |
michael@0 | 273 | // Windows 7 and Vista. |
michael@0 | 274 | } else if (version < base::win::VERSION_WIN8) { |
michael@0 | 275 | return flags & (MITIGATION_BOTTOM_UP_ASLR | |
michael@0 | 276 | MITIGATION_DLL_SEARCH_ORDER | |
michael@0 | 277 | MITIGATION_HEAP_TERMINATE); |
michael@0 | 278 | } |
michael@0 | 279 | |
michael@0 | 280 | // Windows 8 and above. |
michael@0 | 281 | return flags & (MITIGATION_BOTTOM_UP_ASLR | |
michael@0 | 282 | MITIGATION_DLL_SEARCH_ORDER); |
michael@0 | 283 | } |
michael@0 | 284 | |
michael@0 | 285 | bool ApplyProcessMitigationsToSuspendedProcess(HANDLE process, |
michael@0 | 286 | MitigationFlags flags) { |
michael@0 | 287 | // This is a hack to fake a weak bottom-up ASLR on 32-bit Windows. |
michael@0 | 288 | #if !defined(_WIN64) |
michael@0 | 289 | if (flags & MITIGATION_BOTTOM_UP_ASLR) { |
michael@0 | 290 | unsigned int limit; |
michael@0 | 291 | rand_s(&limit); |
michael@0 | 292 | char* ptr = 0; |
michael@0 | 293 | const size_t kMask64k = 0xFFFF; |
michael@0 | 294 | // Random range (512k-16.5mb) in 64k steps. |
michael@0 | 295 | const char* end = ptr + ((((limit % 16384) + 512) * 1024) & ~kMask64k); |
michael@0 | 296 | while (ptr < end) { |
michael@0 | 297 | MEMORY_BASIC_INFORMATION memory_info; |
michael@0 | 298 | if (!::VirtualQueryEx(process, ptr, &memory_info, sizeof(memory_info))) |
michael@0 | 299 | break; |
michael@0 | 300 | size_t size = std::min((memory_info.RegionSize + kMask64k) & ~kMask64k, |
michael@0 | 301 | static_cast<SIZE_T>(end - ptr)); |
michael@0 | 302 | if (ptr && memory_info.State == MEM_FREE) |
michael@0 | 303 | ::VirtualAllocEx(process, ptr, size, MEM_RESERVE, PAGE_NOACCESS); |
michael@0 | 304 | ptr += size; |
michael@0 | 305 | } |
michael@0 | 306 | } |
michael@0 | 307 | #endif |
michael@0 | 308 | |
michael@0 | 309 | return true; |
michael@0 | 310 | } |
michael@0 | 311 | |
michael@0 | 312 | bool CanSetProcessMitigationsPostStartup(MitigationFlags flags) { |
michael@0 | 313 | // All of these mitigations can be enabled after startup. |
michael@0 | 314 | return !(flags & ~(MITIGATION_HEAP_TERMINATE | |
michael@0 | 315 | MITIGATION_DEP | |
michael@0 | 316 | MITIGATION_DEP_NO_ATL_THUNK | |
michael@0 | 317 | MITIGATION_RELOCATE_IMAGE | |
michael@0 | 318 | MITIGATION_RELOCATE_IMAGE_REQUIRED | |
michael@0 | 319 | MITIGATION_BOTTOM_UP_ASLR | |
michael@0 | 320 | MITIGATION_STRICT_HANDLE_CHECKS | |
michael@0 | 321 | MITIGATION_WIN32K_DISABLE | |
michael@0 | 322 | MITIGATION_EXTENSION_DLL_DISABLE | |
michael@0 | 323 | MITIGATION_DLL_SEARCH_ORDER)); |
michael@0 | 324 | } |
michael@0 | 325 | |
michael@0 | 326 | bool CanSetProcessMitigationsPreStartup(MitigationFlags flags) { |
michael@0 | 327 | // These mitigations cannot be enabled prior to startup. |
michael@0 | 328 | return !(flags & (MITIGATION_STRICT_HANDLE_CHECKS | |
michael@0 | 329 | MITIGATION_WIN32K_DISABLE | |
michael@0 | 330 | MITIGATION_DLL_SEARCH_ORDER)); |
michael@0 | 331 | } |
michael@0 | 332 | |
michael@0 | 333 | } // namespace sandbox |
michael@0 | 334 |