| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
| 3 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include "mozilla/WindowsProcessMitigations.h" |
| 8 |
|
| 9 |
#include <processthreadsapi.h> |
| 10 |
|
| 11 |
#include "mozilla/Assertions.h" |
| 12 |
#include "mozilla/DynamicallyLinkedFunctionPtr.h" |
| 13 |
#include "mozilla/WindowsVersion.h" |
| 14 |
|
| 15 |
static_assert(sizeof(PROCESS_MITIGATION_DYNAMIC_CODE_POLICY) == 4); |
| 16 |
|
| 17 |
namespace mozilla { |
| 18 |
|
| 19 |
static decltype(&::GetProcessMitigationPolicy) |
| 20 |
FetchGetProcessMitigationPolicyFunc() { |
| 21 |
static const StaticDynamicallyLinkedFunctionPtr< |
| 22 |
decltype(&::GetProcessMitigationPolicy)> |
| 23 |
pGetProcessMitigationPolicy(L"kernel32.dll", |
| 24 |
"GetProcessMitigationPolicy"); |
| 25 |
return pGetProcessMitigationPolicy; |
| 26 |
} |
| 27 |
|
| 28 |
static bool sWin32kLockedDownInPolicy = false; |
| 29 |
|
| 30 |
MFBT_API bool IsWin32kLockedDown() { |
| 31 |
static bool sWin32kLockedDown = []() { |
| 32 |
auto pGetProcessMitigationPolicy = FetchGetProcessMitigationPolicyFunc(); |
| 33 |
|
| 34 |
PROCESS_MITIGATION_SYSTEM_CALL_DISABLE_POLICY polInfo; |
| 35 |
if (!pGetProcessMitigationPolicy || |
| 36 |
!pGetProcessMitigationPolicy(::GetCurrentProcess(), |
| 37 |
ProcessSystemCallDisablePolicy, &polInfo, |
| 38 |
sizeof(polInfo))) { |
| 39 |
// We failed to get pointer to GetProcessMitigationPolicy or the call |
| 40 |
// to it failed, so just return what the sandbox policy says. |
| 41 |
return sWin32kLockedDownInPolicy; |
| 42 |
} |
| 43 |
|
| 44 |
return !!polInfo.DisallowWin32kSystemCalls; |
| 45 |
}(); |
| 46 |
|
| 47 |
return sWin32kLockedDown; |
| 48 |
} |
| 49 |
|
| 50 |
MFBT_API void SetWin32kLockedDownInPolicy() { |
| 51 |
sWin32kLockedDownInPolicy = true; |
| 52 |
} |
| 53 |
|
| 54 |
MFBT_API bool IsDynamicCodeDisabled() { |
| 55 |
auto pGetProcessMitigationPolicy = FetchGetProcessMitigationPolicyFunc(); |
| 56 |
if (!pGetProcessMitigationPolicy) { |
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
PROCESS_MITIGATION_DYNAMIC_CODE_POLICY polInfo; |
| 61 |
if (!pGetProcessMitigationPolicy(::GetCurrentProcess(), |
| 62 |
ProcessDynamicCodePolicy, &polInfo, |
| 63 |
sizeof(polInfo))) { |
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
return polInfo.ProhibitDynamicCode; |
| 68 |
} |
| 69 |
|
| 70 |
MFBT_API bool IsEafPlusEnabled() { |
| 71 |
auto pGetProcessMitigationPolicy = FetchGetProcessMitigationPolicyFunc(); |
| 72 |
if (!pGetProcessMitigationPolicy) { |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
PROCESS_MITIGATION_PAYLOAD_RESTRICTION_POLICY polInfo; |
| 77 |
if (!pGetProcessMitigationPolicy(::GetCurrentProcess(), |
| 78 |
ProcessPayloadRestrictionPolicy, &polInfo, |
| 79 |
sizeof(polInfo))) { |
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
return polInfo.EnableExportAddressFilterPlus; |
| 84 |
} |
| 85 |
|
| 86 |
MFBT_API bool IsUserShadowStackEnabled() { |
| 87 |
auto pGetProcessMitigationPolicy = FetchGetProcessMitigationPolicyFunc(); |
| 88 |
if (!pGetProcessMitigationPolicy) { |
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
PROCESS_MITIGATION_USER_SHADOW_STACK_POLICY polInfo; |
| 93 |
if (!pGetProcessMitigationPolicy(::GetCurrentProcess(), |
| 94 |
ProcessUserShadowStackPolicy, &polInfo, |
| 95 |
sizeof(polInfo))) { |
| 96 |
return false; |
| 97 |
} |
| 98 |
|
| 99 |
return polInfo.EnableUserShadowStack; |
| 100 |
} |
| 101 |
|
| 102 |
MFBT_API bool IsPreferLoadFromSystem32Available() { |
| 103 |
return mozilla::IsWin10AnniversaryUpdateOrLater(); |
| 104 |
} |
| 105 |
|
| 106 |
MFBT_API bool IsPreferLoadFromSystem32Enabled() { |
| 107 |
auto pGetProcessMitigationPolicy = FetchGetProcessMitigationPolicyFunc(); |
| 108 |
if (!pGetProcessMitigationPolicy) { |
| 109 |
return false; |
| 110 |
} |
| 111 |
|
| 112 |
PROCESS_MITIGATION_IMAGE_LOAD_POLICY imgLoadPol{}; |
| 113 |
if (!pGetProcessMitigationPolicy(::GetCurrentProcess(), |
| 114 |
ProcessImageLoadPolicy, &imgLoadPol, |
| 115 |
sizeof(imgLoadPol))) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
return imgLoadPol.PreferSystem32Images; |
| 120 |
} |
| 121 |
|
| 122 |
MFBT_API bool EnablePreferLoadFromSystem32() { |
| 123 |
auto pGetProcessMitigationPolicy = FetchGetProcessMitigationPolicyFunc(); |
| 124 |
if (!pGetProcessMitigationPolicy) { |
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
static const mozilla::StaticDynamicallyLinkedFunctionPtr< |
| 129 |
decltype(&::SetProcessMitigationPolicy)> |
| 130 |
pSetProcessMitigationPolicy(L"kernel32.dll", |
| 131 |
"SetProcessMitigationPolicy"); |
| 132 |
if (!pSetProcessMitigationPolicy) { |
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
PROCESS_MITIGATION_IMAGE_LOAD_POLICY imgLoadPol{}; |
| 137 |
if (!pGetProcessMitigationPolicy(::GetCurrentProcess(), |
| 138 |
ProcessImageLoadPolicy, &imgLoadPol, |
| 139 |
sizeof(imgLoadPol))) { |
| 140 |
return false; |
| 141 |
} |
| 142 |
|
| 143 |
if (imgLoadPol.PreferSystem32Images) { |
| 144 |
return true; |
| 145 |
} |
| 146 |
|
| 147 |
imgLoadPol.PreferSystem32Images = 1; |
| 148 |
return pSetProcessMitigationPolicy(ProcessImageLoadPolicy, &imgLoadPol, |
| 149 |
sizeof(imgLoadPol)); |
| 150 |
} |
| 151 |
|
| 152 |
} // namespace mozilla |