OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome_elf/ntdll_cache.h" | |
6 | |
5 #include <stdint.h> | 7 #include <stdint.h> |
6 #include <windows.h> | 8 #include <windows.h> |
7 | 9 |
8 #include "chrome_elf/ntdll_cache.h" | 10 #include "base/basictypes.h" |
11 #include "chrome_elf/thunk_getter.h" | |
12 #include "sandbox/win/src/interception_internal.h" | |
13 #include "sandbox/win/src/internal_types.h" | |
14 #include "sandbox/win/src/service_resolver.h" | |
15 | |
16 // Allocate storage for thunks in a page of this module to save on doing | |
17 // an extra allocation at run time. | |
18 #pragma section(".crthunk", read, execute) | |
19 __declspec(allocate(".crthunk")) sandbox::ThunkData g_nt_thunk_storage; | |
9 | 20 |
10 FunctionLookupTable g_ntdll_lookup; | 21 FunctionLookupTable g_ntdll_lookup; |
11 | 22 |
12 void InitCache() { | 23 void InitCache() { |
13 HMODULE ntdll_handle = ::GetModuleHandle(L"ntdll.dll"); | 24 HMODULE ntdll_handle = ::GetModuleHandle(sandbox::kNtdllName); |
14 | 25 |
15 // To find the Export Address Table address, we start from the DOS header. | 26 // To find the Export Address Table address, we start from the DOS header. |
16 // The module handle is actually the address of the header. | 27 // The module handle is actually the address of the header. |
17 IMAGE_DOS_HEADER* dos_header = | 28 IMAGE_DOS_HEADER* dos_header = |
18 reinterpret_cast<IMAGE_DOS_HEADER*>(ntdll_handle); | 29 reinterpret_cast<IMAGE_DOS_HEADER*>(ntdll_handle); |
19 // The e_lfanew is an offset from the DOS header to the NT header. It should | 30 // The e_lfanew is an offset from the DOS header to the NT header. It should |
20 // never be 0. | 31 // never be 0. |
21 IMAGE_NT_HEADERS* nt_headers = reinterpret_cast<IMAGE_NT_HEADERS*>( | 32 IMAGE_NT_HEADERS* nt_headers = reinterpret_cast<IMAGE_NT_HEADERS*>( |
22 ntdll_handle + dos_header->e_lfanew / sizeof(uint32_t)); | 33 ntdll_handle + dos_header->e_lfanew / sizeof(uint32_t)); |
23 // For modules that have an import address table, its offset from the | 34 // For modules that have an import address table, its offset from the |
(...skipping 17 matching lines...) Expand all Loading... | |
41 base_addr + exports->AddressOfFunctions); | 52 base_addr + exports->AddressOfFunctions); |
42 int num_entries = exports->NumberOfNames; | 53 int num_entries = exports->NumberOfNames; |
43 | 54 |
44 for (int i = 0; i < num_entries; i++) { | 55 for (int i = 0; i < num_entries; i++) { |
45 char* name = reinterpret_cast<char*>(base_addr + names[i]); | 56 char* name = reinterpret_cast<char*>(base_addr + names[i]); |
46 WORD ord = ordinals[i]; | 57 WORD ord = ordinals[i]; |
47 DWORD func = funcs[ord]; | 58 DWORD func = funcs[ord]; |
48 FARPROC func_addr = reinterpret_cast<FARPROC>(func + base_addr); | 59 FARPROC func_addr = reinterpret_cast<FARPROC>(func + base_addr); |
49 g_ntdll_lookup[std::string(name)] = func_addr; | 60 g_ntdll_lookup[std::string(name)] = func_addr; |
50 } | 61 } |
62 | |
63 // If ntdll has already been patched, don't copy it. | |
64 const bool kRelaxed = false; | |
65 | |
66 // Create a thunk via the appropriate ServiceResolver instance. | |
67 sandbox::ServiceResolverThunk* thunk = GetThunk(kRelaxed); | |
robertshield
2014/03/17 22:21:27
try using scoped_ptr if we can do so with dependen
Cait (Slow)
2014/03/18 00:23:10
Done.
| |
68 | |
69 if (thunk) { | |
70 BYTE* thunk_storage = reinterpret_cast<BYTE*>(&g_nt_thunk_storage); | |
71 | |
72 // Mark the thunk storage as readable and writeable, since we | |
robertshield
2014/03/17 22:21:27
*we are
Cait (Slow)
2014/03/18 00:23:10
Done.
| |
73 // ready to write to it. | |
74 DWORD old_protect = 0; | |
75 if (!::VirtualProtect(&g_nt_thunk_storage, | |
76 sizeof(g_nt_thunk_storage), | |
77 PAGE_EXECUTE_READWRITE, | |
78 &old_protect)) { | |
79 return; | |
80 } | |
81 | |
82 size_t storage_used = 0; | |
83 NTSTATUS ret = thunk->CopyThunk(::GetModuleHandle(sandbox::kNtdllName), | |
84 "NtCreateFile", | |
85 thunk_storage, | |
86 sizeof(sandbox::ThunkData), | |
87 &storage_used); | |
88 delete thunk; | |
89 | |
90 if (!NT_SUCCESS(ret)) { | |
91 memset(&g_nt_thunk_storage, 0, sizeof(g_nt_thunk_storage)); | |
92 } | |
93 | |
94 // Ensure that the pointer to the old function can't be changed. | |
95 ::VirtualProtect(&g_nt_thunk_storage, | |
96 sizeof(g_nt_thunk_storage), | |
97 PAGE_EXECUTE_READ, | |
98 &old_protect); | |
99 } | |
51 } | 100 } |
OLD | NEW |