| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/sandbox_utils.h" | 5 #include "sandbox/win/src/sandbox_utils.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/win/windows_version.h" | 10 #include "base/win/windows_version.h" |
| 11 #include "sandbox/win/src/internal_types.h" | 11 #include "sandbox/win/src/internal_types.h" |
| 12 #include "sandbox/win/src/nt_internals.h" | |
| 13 | 12 |
| 14 namespace sandbox { | 13 namespace sandbox { |
| 15 | 14 |
| 16 bool GetModuleHandleHelper(DWORD flags, const wchar_t* module_name, | |
| 17 HMODULE* module) { | |
| 18 DCHECK(module); | |
| 19 | |
| 20 HMODULE kernel32_base = ::GetModuleHandle(kKerneldllName); | |
| 21 if (!kernel32_base) { | |
| 22 NOTREACHED(); | |
| 23 return false; | |
| 24 } | |
| 25 | |
| 26 GetModuleHandleExFunction get_module_handle_ex = reinterpret_cast< | |
| 27 GetModuleHandleExFunction>(::GetProcAddress(kernel32_base, | |
| 28 "GetModuleHandleExW")); | |
| 29 if (get_module_handle_ex) | |
| 30 return (get_module_handle_ex(flags, module_name, module) != FALSE); | |
| 31 | |
| 32 if (!flags) { | |
| 33 *module = ::LoadLibrary(module_name); | |
| 34 } else if (flags & GET_MODULE_HANDLE_EX_FLAG_PIN) { | |
| 35 NOTREACHED(); | |
| 36 return false; | |
| 37 } else if (!(flags & GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS)) { | |
| 38 DCHECK((flags & GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT) == | |
| 39 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT); | |
| 40 | |
| 41 *module = ::GetModuleHandle(module_name); | |
| 42 } else { | |
| 43 DCHECK((flags & (GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT | | |
| 44 GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS)) == | |
| 45 (GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT | | |
| 46 GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS)); | |
| 47 | |
| 48 MEMORY_BASIC_INFORMATION info = {0}; | |
| 49 size_t returned = VirtualQuery(module_name, &info, sizeof(info)); | |
| 50 if (sizeof(info) != returned) | |
| 51 return false; | |
| 52 *module = reinterpret_cast<HMODULE>(info.AllocationBase); | |
| 53 } | |
| 54 return true; | |
| 55 } | |
| 56 | |
| 57 bool IsXPSP2OrLater() { | 15 bool IsXPSP2OrLater() { |
| 58 base::win::Version version = base::win::GetVersion(); | 16 base::win::Version version = base::win::GetVersion(); |
| 59 return (version > base::win::VERSION_XP) || | 17 return (version > base::win::VERSION_XP) || |
| 60 ((version == base::win::VERSION_XP) && | 18 ((version == base::win::VERSION_XP) && |
| 61 (base::win::OSInfo::GetInstance()->service_pack().major >= 2)); | 19 (base::win::OSInfo::GetInstance()->service_pack().major >= 2)); |
| 62 } | 20 } |
| 63 | 21 |
| 64 void InitObjectAttribs(const std::wstring& name, ULONG attributes, HANDLE root, | 22 void InitObjectAttribs(const std::wstring& name, |
| 65 OBJECT_ATTRIBUTES* obj_attr, UNICODE_STRING* uni_name) { | 23 ULONG attributes, |
| 24 HANDLE root, |
| 25 OBJECT_ATTRIBUTES* obj_attr, |
| 26 UNICODE_STRING* uni_name) { |
| 66 static RtlInitUnicodeStringFunction RtlInitUnicodeString; | 27 static RtlInitUnicodeStringFunction RtlInitUnicodeString; |
| 67 if (!RtlInitUnicodeString) { | 28 if (!RtlInitUnicodeString) { |
| 68 HMODULE ntdll = ::GetModuleHandle(kNtdllName); | 29 HMODULE ntdll = ::GetModuleHandle(kNtdllName); |
| 69 RtlInitUnicodeString = reinterpret_cast<RtlInitUnicodeStringFunction>( | 30 RtlInitUnicodeString = reinterpret_cast<RtlInitUnicodeStringFunction>( |
| 70 GetProcAddress(ntdll, "RtlInitUnicodeString")); | 31 GetProcAddress(ntdll, "RtlInitUnicodeString")); |
| 71 DCHECK(RtlInitUnicodeString); | 32 DCHECK(RtlInitUnicodeString); |
| 72 } | 33 } |
| 73 RtlInitUnicodeString(uni_name, name.c_str()); | 34 RtlInitUnicodeString(uni_name, name.c_str()); |
| 74 InitializeObjectAttributes(obj_attr, uni_name, attributes, root, NULL); | 35 InitializeObjectAttributes(obj_attr, uni_name, attributes, root, NULL); |
| 75 } | 36 } |
| 76 | 37 |
| 77 }; // namespace sandbox | 38 }; // namespace sandbox |
| OLD | NEW |