Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(242)

Side by Side Diff: chrome_elf/ntdll_cache.cc

Issue 183833004: Make chrome_elf use thunks instead of function pointers. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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)
69 return;
70
71 size_t storage_used;
robertshield 2014/02/28 21:02:22 = 0
Cait (Slow) 2014/03/03 20:55:11 Done.
72 NTSTATUS ret = thunk->CopyThunk(::GetModuleHandle(sandbox::kNtdllName),
73 "NtCreateFile",
74 &g_nt_thunk_storage,
75 sizeof(sandbox::ThunkData),
76 &storage_used);
77
78 g_ntdll_lookup[std::string("NtCreateFile")] =
79 reinterpret_cast<FARPROC>(&g_nt_thunk_storage);
80
81 //TODO(caitkp): Sanity checks?
82 //EXPECT_EQ(sizeof(sandbox::ThunkData), storage_used);
83
84 // TODO: Check to make sure thunk mem matches ntdll mem..
85 //EXPECT_TRUE(NT_SUCCESS(ret));
51 } 86 }
OLDNEW
« no previous file with comments | « chrome_elf/ntdll_cache.h ('k') | chrome_elf/thunk_getter.h » ('j') | chrome_elf/thunk_getter.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698