Chromium Code Reviews| 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 const bool kRelaxed = true; | |
| 64 | |
| 65 // Create a thunk via the appropriate ServiceResolver instance. | |
| 66 sandbox::ServiceResolverThunk* thunk = GetThunk(kRelaxed); | |
| 67 | |
| 68 if (!thunk) | |
|
robertshield
2014/03/04 01:47:07
This implies that the rest of the function is opti
Cait (Slow)
2014/03/04 15:43:56
Done.
| |
| 69 return; | |
| 70 | |
| 71 BYTE* thunk_storage = reinterpret_cast<BYTE*>(&g_nt_thunk_storage); | |
| 72 | |
| 73 // Mark the thunk storage as readable and writeable, since we | |
| 74 // ready to write to it. | |
| 75 DWORD old_protect = 0; | |
| 76 if (!VirtualProtect(&g_nt_thunk_storage, | |
|
robertshield
2014/03/04 01:47:07
In some places in this file we use the scope opera
Cait (Slow)
2014/03/04 15:43:56
Done.
| |
| 77 sizeof(g_nt_thunk_storage), | |
| 78 PAGE_EXECUTE_READWRITE, | |
| 79 &old_protect)) { | |
| 80 return; | |
| 81 } | |
| 82 | |
| 83 size_t storage_used = 0; | |
| 84 NTSTATUS ret = thunk->CopyThunk(::GetModuleHandle(sandbox::kNtdllName), | |
| 85 "NtCreateFile", | |
| 86 thunk_storage, | |
| 87 sizeof(sandbox::ThunkData), | |
| 88 &storage_used); | |
| 89 | |
| 90 // Ensure that the pointer to the old function can't be changed. | |
| 91 VirtualProtect(&g_nt_thunk_storage, | |
| 92 sizeof(g_nt_thunk_storage), | |
| 93 PAGE_EXECUTE_READ, | |
| 94 &old_protect); | |
| 95 | |
| 96 g_ntdll_lookup[std::string("NtCreateFile")] = | |
|
robertshield
2014/03/04 01:47:07
Is the explicit string constructor needed?
Cait (Slow)
2014/03/04 15:43:56
Done.
| |
| 97 reinterpret_cast<FARPROC>(&g_nt_thunk_storage); | |
| 51 } | 98 } |
| OLD | NEW |