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

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: Address comments, add scoped ptr 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
« no previous file with comments | « chrome_elf/ntdll_cache.h ('k') | chrome_elf/thunk_getter.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h"
8 #include "base/win/pe_image.h" 12 #include "base/win/pe_image.h"
9 #include "chrome_elf/ntdll_cache.h" 13 #include "chrome_elf/thunk_getter.h"
14 #include "sandbox/win/src/interception_internal.h"
15 #include "sandbox/win/src/internal_types.h"
16 #include "sandbox/win/src/service_resolver.h"
10 17
11 FunctionLookupTable g_ntdll_lookup; 18 FunctionLookupTable g_ntdll_lookup;
12 19
20 // Allocate storage for thunks in a page of this module to save on doing
21 // an extra allocation at run time.
22 #pragma section(".crthunk",read,execute)
23 __declspec(allocate(".crthunk")) sandbox::ThunkData g_nt_thunk_storage;
24
25
26
13 namespace { 27 namespace {
14 28
15 bool EnumExportsCallback(const base::win::PEImage& image, 29 bool EnumExportsCallback(const base::win::PEImage& image,
16 DWORD ordinal, 30 DWORD ordinal,
17 DWORD hint, 31 DWORD hint,
18 LPCSTR name, 32 LPCSTR name,
19 PVOID function_addr, 33 PVOID function_addr,
20 LPCSTR forward, 34 LPCSTR forward,
21 PVOID cookie) { 35 PVOID cookie) {
22 // Our lookup only cares about named functions that are in ntdll, so skip 36 // Our lookup only cares about named functions that are in ntdll, so skip
23 // unnamed or forwarded exports. 37 // unnamed or forwarded exports.
24 if (name && function_addr) 38 if (name && function_addr)
25 g_ntdll_lookup[std::string(name)] = function_addr; 39 g_ntdll_lookup[std::string(name)] = function_addr;
26 40
27 return true; 41 return true;
28 } 42 }
29 43
30 } // namespace 44 } // namespace
31 45
32 void InitCache() { 46 void InitCache() {
33 HMODULE ntdll_handle = ::GetModuleHandle(L"ntdll.dll"); 47 HMODULE ntdll_handle = ::GetModuleHandle(L"ntdll.dll");
34 48
35 base::win::PEImage ntdll_image(ntdll_handle); 49 base::win::PEImage ntdll_image(ntdll_handle);
36 50
37 ntdll_image.EnumExports(EnumExportsCallback, NULL); 51 ntdll_image.EnumExports(EnumExportsCallback, NULL);
52
53 // If ntdll has already been patched, don't copy it.
54 const bool kRelaxed = false;
55
56 // Create a thunk via the appropriate ServiceResolver instance.
57 scoped_ptr<sandbox::ServiceResolverThunk> thunk(GetThunk(kRelaxed));
58
59 if (thunk.get()) {
60 BYTE* thunk_storage = reinterpret_cast<BYTE*>(&g_nt_thunk_storage);
61
62 // Mark the thunk storage as readable and writeable, since we
63 // are ready to write to it.
64 DWORD old_protect = 0;
65 if (!::VirtualProtect(&g_nt_thunk_storage,
66 sizeof(g_nt_thunk_storage),
67 PAGE_EXECUTE_READWRITE,
68 &old_protect)) {
69 return;
70 }
71
72 size_t storage_used = 0;
73 NTSTATUS ret = thunk->CopyThunk(::GetModuleHandle(sandbox::kNtdllName),
74 "NtCreateFile",
75 thunk_storage,
76 sizeof(sandbox::ThunkData),
77 &storage_used);
78
79 if (!NT_SUCCESS(ret)) {
80 memset(&g_nt_thunk_storage, 0, sizeof(g_nt_thunk_storage));
81 }
82
83 // Ensure that the pointer to the old function can't be changed.
84 ::VirtualProtect(&g_nt_thunk_storage,
85 sizeof(g_nt_thunk_storage),
86 PAGE_EXECUTE_READ,
87 &old_protect);
88 }
38 } 89 }
OLDNEW
« no previous file with comments | « chrome_elf/ntdll_cache.h ('k') | chrome_elf/thunk_getter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698