1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "sandbox/src/dep.h"
9 #include "base/logging.h"
15 // These values are in the Windows 2008 SDK but not in the previous ones. Define
16 // the values here until we're sure everyone updated their SDK.
17 #ifndef PROCESS_DEP_ENABLE
18 #define PROCESS_DEP_ENABLE 0x00000001
20 #ifndef PROCESS_DEP_DISABLE_ATL_THUNK_EMULATION
21 #define PROCESS_DEP_DISABLE_ATL_THUNK_EMULATION 0x00000002
24 // SetProcessDEPPolicy is declared in the Windows 2008 SDK.
25 typedef BOOL (WINAPI
*FnSetProcessDEPPolicy
)(DWORD dwFlags
);
27 enum PROCESS_INFORMATION_CLASS
{
28 ProcessExecuteFlags
= 0x22,
31 // Flags named as per their usage.
32 const int MEM_EXECUTE_OPTION_ENABLE
= 1;
33 const int MEM_EXECUTE_OPTION_DISABLE
= 2;
34 const int MEM_EXECUTE_OPTION_ATL7_THUNK_EMULATION
= 4;
35 const int MEM_EXECUTE_OPTION_PERMANENT
= 8;
37 // Not exactly the right signature but that will suffice.
38 typedef HRESULT (WINAPI
*FnNtSetInformationProcess
)(
40 PROCESS_INFORMATION_CLASS ProcessInformationClass
,
41 PVOID ProcessInformation
,
42 ULONG ProcessInformationLength
);
46 bool SetCurrentProcessDEP(DepEnforcement enforcement
) {
48 // DEP is always on in x64.
49 return enforcement
!= DEP_DISABLED
;
51 // Only available on Windows XP SP2 and Windows Server 2003 SP1.
52 // For reference: http://www.uninformed.org/?v=2&a=4
53 FnNtSetInformationProcess NtSetInformationProc
=
54 reinterpret_cast<FnNtSetInformationProcess
>(
55 GetProcAddress(GetModuleHandle(L
"ntdll.dll"),
56 "NtSetInformationProcess"));
58 if (!NtSetInformationProc
)
61 // Flags being used as per SetProcessDEPPolicy on Vista SP1.
63 switch (enforcement
) {
66 dep_flags
= MEM_EXECUTE_OPTION_DISABLE
;
70 dep_flags
= MEM_EXECUTE_OPTION_PERMANENT
| MEM_EXECUTE_OPTION_ENABLE
;
72 case DEP_ENABLED_ATL7_COMPAT
:
74 dep_flags
= MEM_EXECUTE_OPTION_PERMANENT
| MEM_EXECUTE_OPTION_ENABLE
|
75 MEM_EXECUTE_OPTION_ATL7_THUNK_EMULATION
;
82 HRESULT status
= NtSetInformationProc(GetCurrentProcess(),
86 return SUCCEEDED(status
);
89 } // namespace sandbox