|
19 | 19 |
|
20 | 20 | typedef BOOL(WINAPI *LPFN_SETDEFAULTDLLDIRECTORIES)(DWORD); |
21 | 21 | typedef BOOL(WINAPI *LPFN_SETDLLDIRECTORYW)(LPCWSTR); |
| 22 | +typedef BOOL(WINAPI *LPFN_SETPROCESSMITIGATIONPOLICY)(PROCESS_MITIGATION_POLICY, PVOID, SIZE_T); |
22 | 23 |
|
23 | 24 | static BOOL vfInitialized = FALSE; |
24 | 25 | static LPFN_SETDEFAULTDLLDIRECTORIES vpfnSetDefaultDllDirectories = NULL; |
25 | 26 | static LPFN_SETDLLDIRECTORYW vpfnSetDllDirectory = NULL; |
| 27 | +static LPFN_SETPROCESSMITIGATIONPOLICY vpfnSetProcessMitigationPolicy = NULL; |
| 28 | + |
| 29 | +static const DWORD APP_MITIGATION_POLICY_DISABLED = 0; |
| 30 | +static const DWORD APP_MITIGATION_POLICY_ENABLED = 1; |
26 | 31 |
|
27 | 32 | /******************************************************************** |
28 | 33 | EscapeCommandLineArgument - encodes wzArgument such that |
@@ -50,6 +55,7 @@ static void Initialize() |
50 | 55 |
|
51 | 56 | vpfnSetDefaultDllDirectories = (LPFN_SETDEFAULTDLLDIRECTORIES)::GetProcAddress(hKernel32, "SetDefaultDllDirectories"); |
52 | 57 | vpfnSetDllDirectory = (LPFN_SETDLLDIRECTORYW)::GetProcAddress(hKernel32, "SetDllDirectoryW"); |
| 58 | + vpfnSetProcessMitigationPolicy = (LPFN_SETPROCESSMITIGATIONPOLICY)::GetProcAddress(hKernel32, "SetProcessMitigationPolicy"); |
53 | 59 |
|
54 | 60 | vfInitialized = TRUE; |
55 | 61 |
|
@@ -190,6 +196,100 @@ DAPI_(void) AppInitializeUnsafe() |
190 | 196 | ::HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0); |
191 | 197 | } |
192 | 198 |
|
| 199 | +DAPI_(HRESULT) AppSetDefaultProcessMitigationPolicy( |
| 200 | + __in_z LPCWSTR wzPolicyPath |
| 201 | + ) |
| 202 | +{ |
| 203 | + HRESULT hr = S_OK; |
| 204 | + HRESULT hrPolicy = S_OK; |
| 205 | + DWORD dwPolicy = APP_MITIGATION_POLICY_DISABLED; |
| 206 | + BOOL fApplied = FALSE; |
| 207 | + PROCESS_MITIGATION_REDIRECTION_TRUST_POLICY redirectionTrustPolicy = { }; |
| 208 | + PROCESS_MITIGATION_DYNAMIC_CODE_POLICY dynamicCodePolicy = { }; |
| 209 | + PROCESS_MITIGATION_FONT_DISABLE_POLICY fontDisablePolicy = { }; |
| 210 | + |
| 211 | + Initialize(); |
| 212 | + |
| 213 | + if (!vpfnSetProcessMitigationPolicy) |
| 214 | + { |
| 215 | + ExitFunction1(hr = S_FALSE); |
| 216 | + } |
| 217 | + |
| 218 | + hrPolicy = PolcReadNumber(wzPolicyPath, L"RedirectionGuard", APP_MITIGATION_POLICY_ENABLED, &dwPolicy); |
| 219 | + if (FAILED(hrPolicy)) |
| 220 | + { |
| 221 | + TraceError(hrPolicy, "Failed to read mitigation policy setting: RedirectionGuard."); |
| 222 | + dwPolicy = APP_MITIGATION_POLICY_ENABLED; |
| 223 | + } |
| 224 | + |
| 225 | + if (APP_MITIGATION_POLICY_ENABLED == dwPolicy) |
| 226 | + { |
| 227 | + redirectionTrustPolicy.EnforceRedirectionTrust = 1; |
| 228 | + |
| 229 | + if (!vpfnSetProcessMitigationPolicy(ProcessRedirectionTrustPolicy, &redirectionTrustPolicy, sizeof(redirectionTrustPolicy))) |
| 230 | + { |
| 231 | + hr = HRESULT_FROM_WIN32(::GetLastError()); |
| 232 | + TraceError(hr, "Failed to set RedirectionGuard mitigation policy."); |
| 233 | + } |
| 234 | + else |
| 235 | + { |
| 236 | + fApplied = TRUE; |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + hrPolicy = PolcReadNumber(wzPolicyPath, L"DynamicCode", APP_MITIGATION_POLICY_DISABLED, &dwPolicy); |
| 241 | + if (FAILED(hrPolicy)) |
| 242 | + { |
| 243 | + TraceError(hrPolicy, "Failed to read mitigation policy setting: DynamicCode."); |
| 244 | + dwPolicy = APP_MITIGATION_POLICY_DISABLED; |
| 245 | + } |
| 246 | + |
| 247 | + if (APP_MITIGATION_POLICY_ENABLED == dwPolicy) |
| 248 | + { |
| 249 | + dynamicCodePolicy.ProhibitDynamicCode = 1; |
| 250 | + |
| 251 | + if (!vpfnSetProcessMitigationPolicy(ProcessDynamicCodePolicy, &dynamicCodePolicy, sizeof(dynamicCodePolicy))) |
| 252 | + { |
| 253 | + hr = HRESULT_FROM_WIN32(::GetLastError()); |
| 254 | + TraceError(hr, "Failed to set DynamicCode mitigation policy."); |
| 255 | + } |
| 256 | + else |
| 257 | + { |
| 258 | + fApplied = TRUE; |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | + hrPolicy = PolcReadNumber(wzPolicyPath, L"FontDisable", APP_MITIGATION_POLICY_DISABLED, &dwPolicy); |
| 263 | + if (FAILED(hrPolicy)) |
| 264 | + { |
| 265 | + TraceError(hrPolicy, "Failed to read mitigation policy setting: FontDisable."); |
| 266 | + dwPolicy = APP_MITIGATION_POLICY_DISABLED; |
| 267 | + } |
| 268 | + |
| 269 | + if (APP_MITIGATION_POLICY_ENABLED == dwPolicy) |
| 270 | + { |
| 271 | + fontDisablePolicy.DisableNonSystemFonts = 1; |
| 272 | + |
| 273 | + if (!vpfnSetProcessMitigationPolicy(ProcessFontDisablePolicy, &fontDisablePolicy, sizeof(fontDisablePolicy))) |
| 274 | + { |
| 275 | + hr = HRESULT_FROM_WIN32(::GetLastError()); |
| 276 | + TraceError(hr, "Failed to set FontDisable mitigation policy."); |
| 277 | + } |
| 278 | + else |
| 279 | + { |
| 280 | + fApplied = TRUE; |
| 281 | + } |
| 282 | + } |
| 283 | + |
| 284 | +LExit: |
| 285 | + if (SUCCEEDED(hr) && !fApplied) |
| 286 | + { |
| 287 | + hr = S_FALSE; |
| 288 | + } |
| 289 | + |
| 290 | + return hr; |
| 291 | +} |
| 292 | + |
193 | 293 | DAPI_(HRESULT) AppAppendCommandLineArgument( |
194 | 294 | __deref_inout_z LPWSTR* psczCommandLine, |
195 | 295 | __in_z LPCWSTR wzArgument |
|
0 commit comments