Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #include <map> |
michael@0 | 6 | |
michael@0 | 7 | #include "sandbox/win/src/policy_broker.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "base/logging.h" |
michael@0 | 10 | #include "base/win/pe_image.h" |
michael@0 | 11 | #include "base/win/windows_version.h" |
michael@0 | 12 | #include "sandbox/win/src/interception.h" |
michael@0 | 13 | #include "sandbox/win/src/interceptors.h" |
michael@0 | 14 | #include "sandbox/win/src/policy_target.h" |
michael@0 | 15 | #include "sandbox/win/src/process_thread_interception.h" |
michael@0 | 16 | #include "sandbox/win/src/sandbox.h" |
michael@0 | 17 | #include "sandbox/win/src/sandbox_nt_types.h" |
michael@0 | 18 | #include "sandbox/win/src/sandbox_types.h" |
michael@0 | 19 | #include "sandbox/win/src/target_process.h" |
michael@0 | 20 | |
michael@0 | 21 | // This code executes on the broker side, as a callback from the policy on the |
michael@0 | 22 | // target side (the child). |
michael@0 | 23 | |
michael@0 | 24 | namespace sandbox { |
michael@0 | 25 | |
michael@0 | 26 | // This is the list of all imported symbols from ntdll.dll. |
michael@0 | 27 | SANDBOX_INTERCEPT NtExports g_nt; |
michael@0 | 28 | |
michael@0 | 29 | #define INIT_GLOBAL_NT(member) \ |
michael@0 | 30 | g_nt.member = reinterpret_cast<Nt##member##Function>( \ |
michael@0 | 31 | ntdll_image.GetProcAddress("Nt" #member)); \ |
michael@0 | 32 | if (NULL == g_nt.member) \ |
michael@0 | 33 | return false |
michael@0 | 34 | |
michael@0 | 35 | #define INIT_GLOBAL_RTL(member) \ |
michael@0 | 36 | g_nt.member = reinterpret_cast<member##Function>( \ |
michael@0 | 37 | ntdll_image.GetProcAddress(#member)); \ |
michael@0 | 38 | if (NULL == g_nt.member) \ |
michael@0 | 39 | return false |
michael@0 | 40 | |
michael@0 | 41 | bool SetupNtdllImports(TargetProcess *child) { |
michael@0 | 42 | HMODULE ntdll = ::GetModuleHandle(kNtdllName); |
michael@0 | 43 | base::win::PEImage ntdll_image(ntdll); |
michael@0 | 44 | |
michael@0 | 45 | // Bypass purify's interception. |
michael@0 | 46 | wchar_t* loader_get = reinterpret_cast<wchar_t*>( |
michael@0 | 47 | ntdll_image.GetProcAddress("LdrGetDllHandle")); |
michael@0 | 48 | if (loader_get) { |
michael@0 | 49 | GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | |
michael@0 | 50 | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, |
michael@0 | 51 | loader_get, &ntdll); |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | INIT_GLOBAL_NT(AllocateVirtualMemory); |
michael@0 | 55 | INIT_GLOBAL_NT(Close); |
michael@0 | 56 | INIT_GLOBAL_NT(DuplicateObject); |
michael@0 | 57 | INIT_GLOBAL_NT(FreeVirtualMemory); |
michael@0 | 58 | INIT_GLOBAL_NT(MapViewOfSection); |
michael@0 | 59 | INIT_GLOBAL_NT(ProtectVirtualMemory); |
michael@0 | 60 | INIT_GLOBAL_NT(QueryInformationProcess); |
michael@0 | 61 | INIT_GLOBAL_NT(QueryObject); |
michael@0 | 62 | INIT_GLOBAL_NT(QuerySection); |
michael@0 | 63 | INIT_GLOBAL_NT(QueryVirtualMemory); |
michael@0 | 64 | INIT_GLOBAL_NT(UnmapViewOfSection); |
michael@0 | 65 | |
michael@0 | 66 | INIT_GLOBAL_RTL(RtlAllocateHeap); |
michael@0 | 67 | INIT_GLOBAL_RTL(RtlAnsiStringToUnicodeString); |
michael@0 | 68 | INIT_GLOBAL_RTL(RtlCompareUnicodeString); |
michael@0 | 69 | INIT_GLOBAL_RTL(RtlCreateHeap); |
michael@0 | 70 | INIT_GLOBAL_RTL(RtlCreateUserThread); |
michael@0 | 71 | INIT_GLOBAL_RTL(RtlDestroyHeap); |
michael@0 | 72 | INIT_GLOBAL_RTL(RtlFreeHeap); |
michael@0 | 73 | INIT_GLOBAL_RTL(_strnicmp); |
michael@0 | 74 | INIT_GLOBAL_RTL(strlen); |
michael@0 | 75 | INIT_GLOBAL_RTL(wcslen); |
michael@0 | 76 | |
michael@0 | 77 | #ifndef NDEBUG |
michael@0 | 78 | // Verify that the structure is fully initialized. |
michael@0 | 79 | for (size_t i = 0; i < sizeof(g_nt)/sizeof(void*); i++) |
michael@0 | 80 | DCHECK(reinterpret_cast<char**>(&g_nt)[i]); |
michael@0 | 81 | #endif |
michael@0 | 82 | return (SBOX_ALL_OK == child->TransferVariable("g_nt", &g_nt, sizeof(g_nt))); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | #undef INIT_GLOBAL_NT |
michael@0 | 86 | #undef INIT_GLOBAL_RTL |
michael@0 | 87 | |
michael@0 | 88 | bool SetupBasicInterceptions(InterceptionManager* manager) { |
michael@0 | 89 | // Interceptions provided by process_thread_policy, without actual policy. |
michael@0 | 90 | if (!INTERCEPT_NT(manager, NtOpenThread, OPEN_TREAD_ID, 20) || |
michael@0 | 91 | !INTERCEPT_NT(manager, NtOpenProcess, OPEN_PROCESS_ID, 20) || |
michael@0 | 92 | !INTERCEPT_NT(manager, NtOpenProcessToken, OPEN_PROCESS_TOKEN_ID, 16)) |
michael@0 | 93 | return false; |
michael@0 | 94 | |
michael@0 | 95 | // Interceptions with neither policy nor IPC. |
michael@0 | 96 | if (!INTERCEPT_NT(manager, NtSetInformationThread, SET_INFORMATION_THREAD_ID, |
michael@0 | 97 | 20) || |
michael@0 | 98 | !INTERCEPT_NT(manager, NtOpenThreadToken, OPEN_THREAD_TOKEN_ID, 20)) |
michael@0 | 99 | return false; |
michael@0 | 100 | |
michael@0 | 101 | if (base::win::GetVersion() >= base::win::VERSION_XP) { |
michael@0 | 102 | // Bug 27218: We don't have dispatch for some x64 syscalls. |
michael@0 | 103 | // This one is also provided by process_thread_policy. |
michael@0 | 104 | if (!INTERCEPT_NT(manager, NtOpenProcessTokenEx, OPEN_PROCESS_TOKEN_EX_ID, |
michael@0 | 105 | 20)) |
michael@0 | 106 | return false; |
michael@0 | 107 | |
michael@0 | 108 | return INTERCEPT_NT(manager, NtOpenThreadTokenEx, OPEN_THREAD_TOKEN_EX_ID, |
michael@0 | 109 | 24); |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | return true; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | } // namespace sandbox |