Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
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: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
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 | #include <windows.h> |
michael@0 | 7 | #include <tlhelp32.h> |
michael@0 | 8 | #include <dbghelp.h> |
michael@0 | 9 | #include <sstream> |
michael@0 | 10 | |
michael@0 | 11 | #include "shared-libraries.h" |
michael@0 | 12 | #include "nsWindowsHelpers.h" |
michael@0 | 13 | |
michael@0 | 14 | #define CV_SIGNATURE 0x53445352 // 'SDSR' |
michael@0 | 15 | |
michael@0 | 16 | struct CodeViewRecord70 |
michael@0 | 17 | { |
michael@0 | 18 | uint32_t signature; |
michael@0 | 19 | GUID pdbSignature; |
michael@0 | 20 | uint32_t pdbAge; |
michael@0 | 21 | char pdbFileName[1]; |
michael@0 | 22 | }; |
michael@0 | 23 | |
michael@0 | 24 | static bool GetPdbInfo(uintptr_t aStart, nsID& aSignature, uint32_t& aAge, char** aPdbName) |
michael@0 | 25 | { |
michael@0 | 26 | if (!aStart) { |
michael@0 | 27 | return false; |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | PIMAGE_DOS_HEADER dosHeader = reinterpret_cast<PIMAGE_DOS_HEADER>(aStart); |
michael@0 | 31 | if (dosHeader->e_magic != IMAGE_DOS_SIGNATURE) { |
michael@0 | 32 | return false; |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | PIMAGE_NT_HEADERS ntHeaders = reinterpret_cast<PIMAGE_NT_HEADERS>( |
michael@0 | 36 | aStart + dosHeader->e_lfanew); |
michael@0 | 37 | if (ntHeaders->Signature != IMAGE_NT_SIGNATURE) { |
michael@0 | 38 | return false; |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | uint32_t relativeVirtualAddress = |
michael@0 | 42 | ntHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress; |
michael@0 | 43 | if (!relativeVirtualAddress) { |
michael@0 | 44 | return false; |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | PIMAGE_DEBUG_DIRECTORY debugDirectory = |
michael@0 | 48 | reinterpret_cast<PIMAGE_DEBUG_DIRECTORY>(aStart + relativeVirtualAddress); |
michael@0 | 49 | if (!debugDirectory || debugDirectory->Type != IMAGE_DEBUG_TYPE_CODEVIEW) { |
michael@0 | 50 | return false; |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | CodeViewRecord70 *debugInfo = reinterpret_cast<CodeViewRecord70 *>( |
michael@0 | 54 | aStart + debugDirectory->AddressOfRawData); |
michael@0 | 55 | if (!debugInfo || debugInfo->signature != CV_SIGNATURE) { |
michael@0 | 56 | return false; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | aAge = debugInfo->pdbAge; |
michael@0 | 60 | GUID& pdbSignature = debugInfo->pdbSignature; |
michael@0 | 61 | aSignature.m0 = pdbSignature.Data1; |
michael@0 | 62 | aSignature.m1 = pdbSignature.Data2; |
michael@0 | 63 | aSignature.m2 = pdbSignature.Data3; |
michael@0 | 64 | memcpy(aSignature.m3, pdbSignature.Data4, sizeof(pdbSignature.Data4)); |
michael@0 | 65 | |
michael@0 | 66 | // The PDB file name could be different from module filename, so report both |
michael@0 | 67 | // e.g. The PDB for C:\Windows\SysWOW64\ntdll.dll is wntdll.pdb |
michael@0 | 68 | char * leafName = strrchr(debugInfo->pdbFileName, '\\'); |
michael@0 | 69 | if (leafName) { |
michael@0 | 70 | // Only report the file portion of the path |
michael@0 | 71 | *aPdbName = leafName + 1; |
michael@0 | 72 | } else { |
michael@0 | 73 | *aPdbName = debugInfo->pdbFileName; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | return true; |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | static bool IsDashOrBraces(char c) |
michael@0 | 80 | { |
michael@0 | 81 | return c == '-' || c == '{' || c == '}'; |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | SharedLibraryInfo SharedLibraryInfo::GetInfoForSelf() |
michael@0 | 85 | { |
michael@0 | 86 | SharedLibraryInfo sharedLibraryInfo; |
michael@0 | 87 | |
michael@0 | 88 | nsAutoHandle snap(CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetCurrentProcessId())); |
michael@0 | 89 | |
michael@0 | 90 | MODULEENTRY32 module = {0}; |
michael@0 | 91 | module.dwSize = sizeof(MODULEENTRY32); |
michael@0 | 92 | if (Module32First(snap, &module)) { |
michael@0 | 93 | do { |
michael@0 | 94 | nsID pdbSig; |
michael@0 | 95 | uint32_t pdbAge; |
michael@0 | 96 | char *pdbName = NULL; |
michael@0 | 97 | |
michael@0 | 98 | // Load the module again to make sure that its handle will remain remain |
michael@0 | 99 | // valid as we attempt to read the PDB information from it. We load the |
michael@0 | 100 | // DLL as a datafile so that if the module actually gets unloaded between |
michael@0 | 101 | // the call to Module32Next and the following LoadLibraryEx, we don't end |
michael@0 | 102 | // up running the now newly loaded module's DllMain function. If the |
michael@0 | 103 | // module is already loaded, LoadLibraryEx just increments its refcount. |
michael@0 | 104 | // |
michael@0 | 105 | // Note that because of the race condition above, merely loading the DLL |
michael@0 | 106 | // again is not safe enough, therefore we also need to make sure that we |
michael@0 | 107 | // can read the memory mapped at the base address before we can safely |
michael@0 | 108 | // proceed to actually access those pages. |
michael@0 | 109 | HMODULE handleLock = LoadLibraryEx(module.szExePath, NULL, LOAD_LIBRARY_AS_DATAFILE); |
michael@0 | 110 | MEMORY_BASIC_INFORMATION vmemInfo = {0}; |
michael@0 | 111 | if (handleLock && |
michael@0 | 112 | sizeof(vmemInfo) == VirtualQuery(module.modBaseAddr, &vmemInfo, sizeof(vmemInfo)) && |
michael@0 | 113 | vmemInfo.State == MEM_COMMIT && |
michael@0 | 114 | GetPdbInfo((uintptr_t)module.modBaseAddr, pdbSig, pdbAge, &pdbName)) { |
michael@0 | 115 | std::ostringstream stream; |
michael@0 | 116 | stream << pdbSig.ToString() << std::hex << pdbAge; |
michael@0 | 117 | std::string breakpadId = stream.str(); |
michael@0 | 118 | std::string::iterator end = |
michael@0 | 119 | std::remove_if(breakpadId.begin(), breakpadId.end(), IsDashOrBraces); |
michael@0 | 120 | breakpadId.erase(end, breakpadId.end()); |
michael@0 | 121 | std::transform(breakpadId.begin(), breakpadId.end(), |
michael@0 | 122 | breakpadId.begin(), toupper); |
michael@0 | 123 | |
michael@0 | 124 | SharedLibrary shlib((uintptr_t)module.modBaseAddr, |
michael@0 | 125 | (uintptr_t)module.modBaseAddr+module.modBaseSize, |
michael@0 | 126 | 0, // DLLs are always mapped at offset 0 on Windows |
michael@0 | 127 | breakpadId, |
michael@0 | 128 | pdbName); |
michael@0 | 129 | sharedLibraryInfo.AddSharedLibrary(shlib); |
michael@0 | 130 | } |
michael@0 | 131 | FreeLibrary(handleLock); // ok to free null handles |
michael@0 | 132 | } while (Module32Next(snap, &module)); |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | return sharedLibraryInfo; |
michael@0 | 136 | } |
michael@0 | 137 |