| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "sandbox/win/src/process_mitigations.h" | 5 #include "sandbox/win/src/process_mitigations.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/win/windows_version.h" | 9 #include "base/win/windows_version.h" |
| 10 #include "sandbox/win/src/nt_internals.h" | 10 #include "sandbox/win/src/nt_internals.h" |
| 11 #include "sandbox/win/src/restricted_token_utils.h" | 11 #include "sandbox/win/src/restricted_token_utils.h" |
| 12 #include "sandbox/win/src/sandbox_rand.h" |
| 12 #include "sandbox/win/src/win_utils.h" | 13 #include "sandbox/win/src/win_utils.h" |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 // Functions for enabling policies. | 17 // Functions for enabling policies. |
| 17 typedef BOOL (WINAPI *SetProcessDEPPolicyFunction)(DWORD dwFlags); | 18 typedef BOOL (WINAPI *SetProcessDEPPolicyFunction)(DWORD dwFlags); |
| 18 | 19 |
| 19 typedef BOOL (WINAPI *SetProcessMitigationPolicyFunction)( | 20 typedef BOOL (WINAPI *SetProcessMitigationPolicyFunction)( |
| 20 PROCESS_MITIGATION_POLICY mitigation_policy, | 21 PROCESS_MITIGATION_POLICY mitigation_policy, |
| 21 PVOID buffer, | 22 PVOID buffer, |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 return flags & (MITIGATION_BOTTOM_UP_ASLR | | 279 return flags & (MITIGATION_BOTTOM_UP_ASLR | |
| 279 MITIGATION_DLL_SEARCH_ORDER); | 280 MITIGATION_DLL_SEARCH_ORDER); |
| 280 } | 281 } |
| 281 | 282 |
| 282 bool ApplyProcessMitigationsToSuspendedProcess(HANDLE process, | 283 bool ApplyProcessMitigationsToSuspendedProcess(HANDLE process, |
| 283 MitigationFlags flags) { | 284 MitigationFlags flags) { |
| 284 // This is a hack to fake a weak bottom-up ASLR on 32-bit Windows. | 285 // This is a hack to fake a weak bottom-up ASLR on 32-bit Windows. |
| 285 #if !defined(_WIN64) | 286 #if !defined(_WIN64) |
| 286 if (flags & MITIGATION_BOTTOM_UP_ASLR) { | 287 if (flags & MITIGATION_BOTTOM_UP_ASLR) { |
| 287 unsigned int limit; | 288 unsigned int limit; |
| 288 rand_s(&limit); | 289 GetRandom(&limit); |
| 289 char* ptr = 0; | 290 char* ptr = 0; |
| 290 const size_t kMask64k = 0xFFFF; | 291 const size_t kMask64k = 0xFFFF; |
| 291 // Random range (512k-16.5mb) in 64k steps. | 292 // Random range (512k-16.5mb) in 64k steps. |
| 292 const char* end = ptr + ((((limit % 16384) + 512) * 1024) & ~kMask64k); | 293 const char* end = ptr + ((((limit % 16384) + 512) * 1024) & ~kMask64k); |
| 293 while (ptr < end) { | 294 while (ptr < end) { |
| 294 MEMORY_BASIC_INFORMATION memory_info; | 295 MEMORY_BASIC_INFORMATION memory_info; |
| 295 if (!::VirtualQueryEx(process, ptr, &memory_info, sizeof(memory_info))) | 296 if (!::VirtualQueryEx(process, ptr, &memory_info, sizeof(memory_info))) |
| 296 break; | 297 break; |
| 297 size_t size = std::min((memory_info.RegionSize + kMask64k) & ~kMask64k, | 298 size_t size = std::min((memory_info.RegionSize + kMask64k) & ~kMask64k, |
| 298 static_cast<SIZE_T>(end - ptr)); | 299 static_cast<SIZE_T>(end - ptr)); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 321 } | 322 } |
| 322 | 323 |
| 323 bool CanSetProcessMitigationsPreStartup(MitigationFlags flags) { | 324 bool CanSetProcessMitigationsPreStartup(MitigationFlags flags) { |
| 324 // These mitigations cannot be enabled prior to startup. | 325 // These mitigations cannot be enabled prior to startup. |
| 325 return !(flags & (MITIGATION_STRICT_HANDLE_CHECKS | | 326 return !(flags & (MITIGATION_STRICT_HANDLE_CHECKS | |
| 326 MITIGATION_DLL_SEARCH_ORDER)); | 327 MITIGATION_DLL_SEARCH_ORDER)); |
| 327 } | 328 } |
| 328 | 329 |
| 329 } // namespace sandbox | 330 } // namespace sandbox |
| 330 | 331 |
| OLD | NEW |