Thu, 15 Jan 2015 15:59:08 +0100
Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /* Win32 x86/x64 code for stack walking, symbol resolution, and function hooking */ |
michael@0 | 7 | |
michael@0 | 8 | #ifndef __nsDebugHelpWin32_h__ |
michael@0 | 9 | #define __nsDebugHelpWin32_h__ |
michael@0 | 10 | |
michael@0 | 11 | #if defined(_WIN32) && (defined(_M_IX86) || defined(_M_X64)) |
michael@0 | 12 | #ifndef WIN32_LEAN_AND_MEAN |
michael@0 | 13 | #define WIN32_LEAN_AND_MEAN |
michael@0 | 14 | #endif |
michael@0 | 15 | #include <windows.h> |
michael@0 | 16 | #include <imagehlp.h> |
michael@0 | 17 | #include <crtdbg.h> |
michael@0 | 18 | #else |
michael@0 | 19 | #error "nsDebugHelpWin32.h should only be included in Win32 x86/x64 builds" |
michael@0 | 20 | #endif |
michael@0 | 21 | |
michael@0 | 22 | // XXX temporary hack... |
michael@0 | 23 | //#include "hacky_defines.h" |
michael@0 | 24 | |
michael@0 | 25 | |
michael@0 | 26 | /***************************************************************************/ |
michael@0 | 27 | // useful macros... |
michael@0 | 28 | |
michael@0 | 29 | #ifdef DHW_IMPLEMENT_GLOBALS |
michael@0 | 30 | #define DHW_DECLARE_FUN_GLOBAL(name_) decltype(name_)* dhw##name_ |
michael@0 | 31 | #else |
michael@0 | 32 | #define DHW_DECLARE_FUN_GLOBAL(name_) extern decltype(name_)* dhw##name_ |
michael@0 | 33 | #endif |
michael@0 | 34 | |
michael@0 | 35 | |
michael@0 | 36 | /**********************************************************/ |
michael@0 | 37 | // This is used to get 'original' function addresses from DHWImportHooker. |
michael@0 | 38 | |
michael@0 | 39 | #define DHW_ORIGINAL(name_, hooker_) \ |
michael@0 | 40 | ((decltype(name_)*) hooker_ . GetOriginalFunction()) |
michael@0 | 41 | |
michael@0 | 42 | /***************************************************************************/ |
michael@0 | 43 | // Global declarations of entry points into ImgHelp functions |
michael@0 | 44 | |
michael@0 | 45 | #ifndef _WIN64 |
michael@0 | 46 | DHW_DECLARE_FUN_GLOBAL(EnumerateLoadedModules); |
michael@0 | 47 | #else |
michael@0 | 48 | DHW_DECLARE_FUN_GLOBAL(EnumerateLoadedModules64); |
michael@0 | 49 | #endif |
michael@0 | 50 | |
michael@0 | 51 | DHW_DECLARE_FUN_GLOBAL(ImageDirectoryEntryToData); |
michael@0 | 52 | |
michael@0 | 53 | /***************************************************************************/ |
michael@0 | 54 | |
michael@0 | 55 | extern bool |
michael@0 | 56 | dhwEnsureImageHlpInitialized(); |
michael@0 | 57 | |
michael@0 | 58 | /***************************************************************************/ |
michael@0 | 59 | |
michael@0 | 60 | class DHWImportHooker |
michael@0 | 61 | { |
michael@0 | 62 | public: |
michael@0 | 63 | |
michael@0 | 64 | DHWImportHooker(const char* aModuleName, |
michael@0 | 65 | const char* aFunctionName, |
michael@0 | 66 | PROC aHook, |
michael@0 | 67 | bool aExcludeOurModule = false); |
michael@0 | 68 | |
michael@0 | 69 | ~DHWImportHooker(); |
michael@0 | 70 | |
michael@0 | 71 | PROC GetOriginalFunction() {return mOriginal;} |
michael@0 | 72 | |
michael@0 | 73 | bool PatchAllModules(); |
michael@0 | 74 | bool PatchOneModule(HMODULE aModule, const char* name); |
michael@0 | 75 | static bool ModuleLoaded(HMODULE aModule, DWORD flags); |
michael@0 | 76 | |
michael@0 | 77 | |
michael@0 | 78 | // I think that these should be made not static members, but allocated |
michael@0 | 79 | // things created in an explicit static 'init' method and cleaned up in |
michael@0 | 80 | // an explicit static 'finish' method. This would allow the application |
michael@0 | 81 | // to have proper lifetime control over all the hooks. |
michael@0 | 82 | |
michael@0 | 83 | static DHWImportHooker &getLoadLibraryWHooker(); |
michael@0 | 84 | static DHWImportHooker &getLoadLibraryExWHooker(); |
michael@0 | 85 | static DHWImportHooker &getLoadLibraryAHooker(); |
michael@0 | 86 | static DHWImportHooker &getLoadLibraryExAHooker(); |
michael@0 | 87 | static DHWImportHooker &getGetProcAddressHooker(); |
michael@0 | 88 | |
michael@0 | 89 | static HMODULE WINAPI LoadLibraryA(PCSTR path); |
michael@0 | 90 | |
michael@0 | 91 | private: |
michael@0 | 92 | DHWImportHooker* mNext; |
michael@0 | 93 | const char* mModuleName; |
michael@0 | 94 | const char* mFunctionName; |
michael@0 | 95 | PROC mOriginal; |
michael@0 | 96 | PROC mHook; |
michael@0 | 97 | HMODULE mIgnoreModule; |
michael@0 | 98 | bool mHooking; |
michael@0 | 99 | |
michael@0 | 100 | private: |
michael@0 | 101 | static PRLock* gLock; |
michael@0 | 102 | static DHWImportHooker* gHooks; |
michael@0 | 103 | static decltype(GetProcAddress)* gRealGetProcAddress; |
michael@0 | 104 | |
michael@0 | 105 | static HMODULE WINAPI LoadLibraryW(PCWSTR path); |
michael@0 | 106 | static HMODULE WINAPI LoadLibraryExW(PCWSTR path, HANDLE file, DWORD flags); |
michael@0 | 107 | static HMODULE WINAPI LoadLibraryExA(PCSTR path, HANDLE file, DWORD flags); |
michael@0 | 108 | |
michael@0 | 109 | static FARPROC WINAPI GetProcAddress(HMODULE aModule, PCSTR aFunctionName); |
michael@0 | 110 | }; |
michael@0 | 111 | |
michael@0 | 112 | /***************************************************************************/ |
michael@0 | 113 | // This supports the _CrtSetAllocHook based hooking. |
michael@0 | 114 | // This system sucks because you don't get to see the allocated pointer. I |
michael@0 | 115 | // don't think it appropriate for nsTraceMalloc, but is useful as a means to make |
michael@0 | 116 | // malloc fail for testing purposes. |
michael@0 | 117 | #if 0 //comment out this stuff. not necessary |
michael@0 | 118 | |
michael@0 | 119 | class DHWAllocationSizeDebugHook |
michael@0 | 120 | { |
michael@0 | 121 | public: |
michael@0 | 122 | virtual bool AllocHook(size_t size) = 0; |
michael@0 | 123 | virtual bool ReallocHook(size_t size, size_t sizeOld) = 0; |
michael@0 | 124 | virtual bool FreeHook(size_t size) = 0; |
michael@0 | 125 | }; |
michael@0 | 126 | |
michael@0 | 127 | extern bool dhwSetAllocationSizeDebugHook(DHWAllocationSizeDebugHook* hook); |
michael@0 | 128 | extern bool dhwClearAllocationSizeDebugHook(); |
michael@0 | 129 | |
michael@0 | 130 | /***************************************************************************/ |
michael@0 | 131 | #endif //0 |
michael@0 | 132 | |
michael@0 | 133 | #endif /* __nsDebugHelpWin32_h__ */ |