Bug 1536901 - add breakpad id to untrusted modules ping r=mhowell

Differential Revision: https://phabricator.services.mozilla.com/D172257
This commit is contained in:
Greg Stoll 2023-03-24 01:20:17 +00:00
parent 23e100dec1
commit 6030033461
6 changed files with 259 additions and 196 deletions

View File

@ -97,113 +97,126 @@ static bool IsModuleUnsafeToLoad(const std::string& aModuleName) {
return false;
}
void SharedLibraryInfo::AddSharedLibraryFromModuleInfo(
const wchar_t* aModulePath, mozilla::Maybe<HMODULE> aModule) {
mozilla::UniquePtr<char[]> utf8ModulePath(
mozilla::glue::WideToUTF8(aModulePath));
if (!utf8ModulePath) {
return;
}
std::string modulePathStr(utf8ModulePath.get());
size_t pos = modulePathStr.find_last_of("\\/");
std::string moduleNameStr = (pos != std::string::npos)
? modulePathStr.substr(pos + 1)
: modulePathStr;
// If the module is unsafe to call LoadLibraryEx for, we skip.
if (IsModuleUnsafeToLoad(moduleNameStr)) {
return;
}
// Load the module again to make sure that its handle will remain
// valid as we attempt to read the PDB information from it. We load the
// DLL as a datafile so that we don't end up running the newly loaded
// module's DllMain function. If the original handle |aModule| is
// valid, LoadLibraryEx just increments its refcount.
// LOAD_LIBRARY_AS_IMAGE_RESOURCE is needed to read information from the
// sections (not PE headers) which should be relocated by the loader,
// otherwise GetPdbInfo() will cause a crash.
nsModuleHandle handleLock(::LoadLibraryExW(
aModulePath, NULL,
LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE));
if (!handleLock) {
return;
}
mozilla::nt::PEHeaders headers(handleLock.get());
if (!headers) {
return;
}
mozilla::Maybe<mozilla::Range<const uint8_t>> bounds = headers.GetBounds();
if (!bounds) {
return;
}
// Put the original |aModule| into SharedLibrary, but we get debug info
// from |handleLock| as |aModule| might be inaccessible.
const uintptr_t modStart =
aModule.isSome() ? reinterpret_cast<uintptr_t>(*aModule)
: reinterpret_cast<uintptr_t>(handleLock.get());
const uintptr_t modEnd = modStart + bounds->length();
std::string breakpadId;
std::string pdbPathStr;
std::string pdbNameStr;
if (const auto* debugInfo = headers.GetPdbInfo()) {
MOZ_ASSERT(breakpadId.empty());
const GUID& pdbSig = debugInfo->pdbSignature;
AppendHex(pdbSig.Data1, breakpadId, WITH_PADDING);
AppendHex(pdbSig.Data2, breakpadId, WITH_PADDING);
AppendHex(pdbSig.Data3, breakpadId, WITH_PADDING);
AppendHex(reinterpret_cast<const unsigned char*>(&pdbSig.Data4),
reinterpret_cast<const unsigned char*>(&pdbSig.Data4) +
sizeof(pdbSig.Data4),
breakpadId);
AppendHex(debugInfo->pdbAge, breakpadId, WITHOUT_PADDING);
// The PDB file name could be different from module filename,
// so report both
// e.g. The PDB for C:\Windows\SysWOW64\ntdll.dll is wntdll.pdb
pdbPathStr = debugInfo->pdbFileName;
size_t pos = pdbPathStr.find_last_of("\\/");
pdbNameStr =
(pos != std::string::npos) ? pdbPathStr.substr(pos + 1) : pdbPathStr;
}
std::string codeId;
DWORD timestamp;
DWORD imageSize;
if (headers.GetTimeStamp(timestamp) && headers.GetImageSize(imageSize)) {
AppendHex(timestamp, codeId, WITH_PADDING);
AppendHex(imageSize, codeId, WITHOUT_PADDING, LOWERCASE);
}
std::string versionStr;
uint64_t version;
if (headers.GetVersionInfo(version)) {
versionStr += std::to_string((version >> 48) & 0xFFFF);
versionStr += '.';
versionStr += std::to_string((version >> 32) & 0xFFFF);
versionStr += '.';
versionStr += std::to_string((version >> 16) & 0xFFFF);
versionStr += '.';
versionStr += std::to_string(version & 0xFFFF);
}
SharedLibrary shlib(modStart, modEnd,
0, // DLLs are always mapped at offset 0 on Windows
breakpadId, codeId, moduleNameStr, modulePathStr,
pdbNameStr, pdbPathStr, versionStr, "");
AddSharedLibrary(shlib);
}
SharedLibraryInfo SharedLibraryInfo::GetInfoForSelf() {
SharedLibraryInfo sharedLibraryInfo;
auto addSharedLibraryFromModuleInfo = [&sharedLibraryInfo](
const wchar_t* aModulePath,
HMODULE aModule) {
mozilla::UniquePtr<char[]> utf8ModulePath(
mozilla::glue::WideToUTF8(aModulePath));
if (!utf8ModulePath) {
return;
}
std::string modulePathStr(utf8ModulePath.get());
size_t pos = modulePathStr.find_last_of("\\/");
std::string moduleNameStr = (pos != std::string::npos)
? modulePathStr.substr(pos + 1)
: modulePathStr;
// If the module is unsafe to call LoadLibraryEx for, we skip.
if (IsModuleUnsafeToLoad(moduleNameStr)) {
return;
}
// Load the module again to make sure that its handle will remain
// valid as we attempt to read the PDB information from it. We load the
// DLL as a datafile so that we don't end up running the newly loaded
// module's DllMain function. If the original handle |aModule| is
// valid, LoadLibraryEx just increments its refcount.
// LOAD_LIBRARY_AS_IMAGE_RESOURCE is needed to read information from the
// sections (not PE headers) which should be relocated by the loader,
// otherwise GetPdbInfo() will cause a crash.
nsModuleHandle handleLock(::LoadLibraryExW(
aModulePath, NULL,
LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE));
if (!handleLock) {
return;
}
mozilla::nt::PEHeaders headers(handleLock.get());
if (!headers) {
return;
}
mozilla::Maybe<mozilla::Range<const uint8_t>> bounds = headers.GetBounds();
if (!bounds) {
return;
}
// Put the original |aModule| into SharedLibrary, but we get debug info
// from |handleLock| as |aModule| might be inaccessible.
const uintptr_t modStart = reinterpret_cast<uintptr_t>(aModule);
const uintptr_t modEnd = modStart + bounds->length();
std::string breakpadId;
std::string pdbPathStr;
std::string pdbNameStr;
if (const auto* debugInfo = headers.GetPdbInfo()) {
MOZ_ASSERT(breakpadId.empty());
const GUID& pdbSig = debugInfo->pdbSignature;
AppendHex(pdbSig.Data1, breakpadId, WITH_PADDING);
AppendHex(pdbSig.Data2, breakpadId, WITH_PADDING);
AppendHex(pdbSig.Data3, breakpadId, WITH_PADDING);
AppendHex(reinterpret_cast<const unsigned char*>(&pdbSig.Data4),
reinterpret_cast<const unsigned char*>(&pdbSig.Data4) +
sizeof(pdbSig.Data4),
breakpadId);
AppendHex(debugInfo->pdbAge, breakpadId, WITHOUT_PADDING);
// The PDB file name could be different from module filename,
// so report both
// e.g. The PDB for C:\Windows\SysWOW64\ntdll.dll is wntdll.pdb
pdbPathStr = debugInfo->pdbFileName;
size_t pos = pdbPathStr.find_last_of("\\/");
pdbNameStr =
(pos != std::string::npos) ? pdbPathStr.substr(pos + 1) : pdbPathStr;
}
std::string codeId;
DWORD timestamp;
DWORD imageSize;
if (headers.GetTimeStamp(timestamp) && headers.GetImageSize(imageSize)) {
AppendHex(timestamp, codeId, WITH_PADDING);
AppendHex(imageSize, codeId, WITHOUT_PADDING, LOWERCASE);
}
std::string versionStr;
uint64_t version;
if (headers.GetVersionInfo(version)) {
versionStr += std::to_string((version >> 48) & 0xFFFF);
versionStr += '.';
versionStr += std::to_string((version >> 32) & 0xFFFF);
versionStr += '.';
versionStr += std::to_string((version >> 16) & 0xFFFF);
versionStr += '.';
versionStr += std::to_string(version & 0xFFFF);
}
SharedLibrary shlib(modStart, modEnd,
0, // DLLs are always mapped at offset 0 on Windows
breakpadId, codeId, moduleNameStr, modulePathStr,
pdbNameStr, pdbPathStr, versionStr, "");
sharedLibraryInfo.AddSharedLibrary(shlib);
};
auto addSharedLibraryFromModuleInfo =
[&sharedLibraryInfo](const wchar_t* aModulePath, HMODULE aModule) {
sharedLibraryInfo.AddSharedLibraryFromModuleInfo(
aModulePath, mozilla::Some(aModule));
};
mozilla::EnumerateProcessModules(addSharedLibraryFromModuleInfo);
return sharedLibraryInfo;
}
SharedLibraryInfo SharedLibraryInfo::GetInfoFromPath(const wchar_t* aPath) {
SharedLibraryInfo sharedLibraryInfo;
sharedLibraryInfo.AddSharedLibraryFromModuleInfo(aPath, mozilla::Nothing());
return sharedLibraryInfo;
}
void SharedLibraryInfo::Initialize() { /* do nothing */
}

View File

@ -125,6 +125,10 @@ static bool CompareAddresses(const SharedLibrary& first,
class SharedLibraryInfo {
public:
static SharedLibraryInfo GetInfoForSelf();
#ifdef XP_WIN
static SharedLibraryInfo GetInfoFromPath(const wchar_t* aPath);
#endif
static void Initialize();
SharedLibraryInfo() {}
@ -155,6 +159,11 @@ class SharedLibraryInfo {
void Clear() { mEntries.clear(); }
private:
#ifdef XP_WIN
void AddSharedLibraryFromModuleInfo(const wchar_t* aModulePath,
mozilla::Maybe<HMODULE> aModule);
#endif
std::vector<SharedLibrary> mEntries;
};

View File

@ -15,6 +15,10 @@
#include "nsUnicharUtils.h"
#include "nsXULAppAPI.h"
#if defined(MOZ_GECKO_PROFILER)
# include "shared-libraries.h"
#endif // MOZ_GECKO_PROFILER
namespace mozilla {
namespace Telemetry {
@ -166,6 +170,22 @@ static bool SerializeModule(JSContext* aCx,
}
}
#if defined(MOZ_GECKO_PROFILER)
if (aModule->mResolvedDosName) {
nsAutoString path;
if (aModule->mResolvedDosName->GetPath(path) == NS_OK) {
SharedLibraryInfo info = SharedLibraryInfo::GetInfoFromPath(path.Data());
if (info.GetSize() > 0) {
nsCString breakpadId = info.GetEntry(0).GetBreakpadId();
if (!AddLengthLimitedStringProp(aCx, obj, "debugID",
NS_ConvertASCIItoUTF16(breakpadId))) {
return false;
}
}
}
}
#endif // MOZ_GECKO_PROFILER
if (aModule->mVendorInfo.isSome()) {
const char* propName;

View File

@ -406,6 +406,9 @@ TEST_F(UntrustedModulesFixture, Serialize) {
u"\"modules\":\\[{"
u"\"resolvedDllName\":\"TestUntrustedModules_Dll1\\.dll\","
u"\"fileVersion\":\"1\\.2\\.3\\.4\","
// It would be nice to hard-code this, but this might change with
// compiler versions, etc.
u"\"debugID\":\"[0-9A-F]{33}\","
u"\"companyName\":\"Mozilla Corporation\",\"trustFlags\":0}\\],"
u"\"blockedModules\":\\[.*?\\]," // allow for the case where there are some blocked modules
u"\"processes\":{"

View File

@ -45,109 +45,123 @@ static bool IsModuleUnsafeToLoad(const nsAString& aModuleName) {
return false;
}
void AddSharedLibraryFromModuleInfo(SharedLibraryInfo& sharedLibraryInfo,
const wchar_t* aModulePath,
mozilla::Maybe<HMODULE> aModule) {
nsDependentSubstring moduleNameStr(
mozilla::nt::GetLeafName(nsDependentString(aModulePath)));
// If the module is unsafe to call LoadLibraryEx for, we skip.
if (IsModuleUnsafeToLoad(moduleNameStr)) {
return;
}
// If EAF+ is enabled, parsing ntdll's PE header causes a crash.
if (mozilla::IsEafPlusEnabled() &&
moduleNameStr.LowerCaseEqualsLiteral("ntdll.dll")) {
return;
}
// Load the module again to make sure that its handle will remain
// valid as we attempt to read the PDB information from it. We load the
// DLL as a datafile so that we don't end up running the newly loaded
// module's DllMain function. If the original handle |aModule| is valid,
// LoadLibraryEx just increments its refcount.
// LOAD_LIBRARY_AS_IMAGE_RESOURCE is needed to read information from the
// sections (not PE headers) which should be relocated by the loader,
// otherwise GetPdbInfo() will cause a crash.
nsModuleHandle handleLock(::LoadLibraryExW(
aModulePath, NULL,
LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE));
if (!handleLock) {
return;
}
mozilla::nt::PEHeaders headers(handleLock.get());
if (!headers) {
return;
}
mozilla::Maybe<mozilla::Range<const uint8_t>> bounds = headers.GetBounds();
if (!bounds) {
return;
}
// Put the original |aModule| into SharedLibrary, but we get debug info
// from |handleLock| as |aModule| might be inaccessible.
const uintptr_t modStart =
aModule.isSome() ? reinterpret_cast<uintptr_t>(*aModule)
: reinterpret_cast<uintptr_t>(handleLock.get());
const uintptr_t modEnd = modStart + bounds->length();
nsAutoCString breakpadId;
nsAutoString pdbPathStr;
if (const auto* debugInfo = headers.GetPdbInfo()) {
MOZ_ASSERT(breakpadId.IsEmpty());
const GUID& pdbSig = debugInfo->pdbSignature;
breakpadId.AppendPrintf(
"%08lX" // m0
"%04X%04X" // m1,m2
"%02X%02X%02X%02X%02X%02X%02X%02X" // m3
"%X", // pdbAge
pdbSig.Data1, pdbSig.Data2, pdbSig.Data3, pdbSig.Data4[0],
pdbSig.Data4[1], pdbSig.Data4[2], pdbSig.Data4[3], pdbSig.Data4[4],
pdbSig.Data4[5], pdbSig.Data4[6], pdbSig.Data4[7], debugInfo->pdbAge);
// The PDB file name could be different from module filename,
// so report both
// e.g. The PDB for C:\Windows\SysWOW64\ntdll.dll is wntdll.pdb
pdbPathStr = NS_ConvertUTF8toUTF16(debugInfo->pdbFileName);
}
nsAutoCString codeId;
DWORD timestamp;
DWORD imageSize;
if (headers.GetTimeStamp(timestamp) && headers.GetImageSize(imageSize)) {
codeId.AppendPrintf(
"%08lX" // Uppercase 8 digits of hex timestamp with leading zeroes.
"%lx", // Lowercase hex image size
timestamp, imageSize);
}
nsAutoCString versionStr;
uint64_t version;
if (headers.GetVersionInfo(version)) {
versionStr.AppendPrintf("%u.%u.%u.%u",
static_cast<uint32_t>((version >> 48) & 0xFFFFu),
static_cast<uint32_t>((version >> 32) & 0xFFFFu),
static_cast<uint32_t>((version >> 16) & 0xFFFFu),
static_cast<uint32_t>(version & 0xFFFFu));
}
const nsString& pdbNameStr =
PromiseFlatString(mozilla::nt::GetLeafName(pdbPathStr));
SharedLibrary shlib(modStart, modEnd,
0, // DLLs are always mapped at offset 0 on Windows
breakpadId, codeId, PromiseFlatString(moduleNameStr),
nsDependentString(aModulePath), pdbNameStr, pdbPathStr,
versionStr, "");
sharedLibraryInfo.AddSharedLibrary(shlib);
}
SharedLibraryInfo SharedLibraryInfo::GetInfoForSelf() {
SharedLibraryInfo sharedLibraryInfo;
auto addSharedLibraryFromModuleInfo = [&sharedLibraryInfo](
const wchar_t* aModulePath,
HMODULE aModule) {
nsDependentSubstring moduleNameStr(
mozilla::nt::GetLeafName(nsDependentString(aModulePath)));
// If the module is unsafe to call LoadLibraryEx for, we skip.
if (IsModuleUnsafeToLoad(moduleNameStr)) {
return;
}
// If EAF+ is enabled, parsing ntdll's PE header causes a crash.
if (mozilla::IsEafPlusEnabled() &&
moduleNameStr.LowerCaseEqualsLiteral("ntdll.dll")) {
return;
}
// Load the module again to make sure that its handle will remain
// valid as we attempt to read the PDB information from it. We load the
// DLL as a datafile so that we don't end up running the newly loaded
// module's DllMain function. If the original handle |aModule| is valid,
// LoadLibraryEx just increments its refcount.
// LOAD_LIBRARY_AS_IMAGE_RESOURCE is needed to read information from the
// sections (not PE headers) which should be relocated by the loader,
// otherwise GetPdbInfo() will cause a crash.
nsModuleHandle handleLock(::LoadLibraryExW(
aModulePath, NULL,
LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE));
if (!handleLock) {
return;
}
mozilla::nt::PEHeaders headers(handleLock.get());
if (!headers) {
return;
}
mozilla::Maybe<mozilla::Range<const uint8_t>> bounds = headers.GetBounds();
if (!bounds) {
return;
}
// Put the original |aModule| into SharedLibrary, but we get debug info
// from |handleLock| as |aModule| might be inaccessible.
const uintptr_t modStart = reinterpret_cast<uintptr_t>(aModule);
const uintptr_t modEnd = modStart + bounds->length();
nsAutoCString breakpadId;
nsAutoString pdbPathStr;
if (const auto* debugInfo = headers.GetPdbInfo()) {
MOZ_ASSERT(breakpadId.IsEmpty());
const GUID& pdbSig = debugInfo->pdbSignature;
breakpadId.AppendPrintf(
"%08lX" // m0
"%04X%04X" // m1,m2
"%02X%02X%02X%02X%02X%02X%02X%02X" // m3
"%X", // pdbAge
pdbSig.Data1, pdbSig.Data2, pdbSig.Data3, pdbSig.Data4[0],
pdbSig.Data4[1], pdbSig.Data4[2], pdbSig.Data4[3], pdbSig.Data4[4],
pdbSig.Data4[5], pdbSig.Data4[6], pdbSig.Data4[7], debugInfo->pdbAge);
// The PDB file name could be different from module filename,
// so report both
// e.g. The PDB for C:\Windows\SysWOW64\ntdll.dll is wntdll.pdb
pdbPathStr = NS_ConvertUTF8toUTF16(debugInfo->pdbFileName);
}
nsAutoCString codeId;
DWORD timestamp;
DWORD imageSize;
if (headers.GetTimeStamp(timestamp) && headers.GetImageSize(imageSize)) {
codeId.AppendPrintf(
"%08lX" // Uppercase 8 digits of hex timestamp with leading zeroes.
"%lx", // Lowercase hex image size
timestamp, imageSize);
}
nsAutoCString versionStr;
uint64_t version;
if (headers.GetVersionInfo(version)) {
versionStr.AppendPrintf("%u.%u.%u.%u",
static_cast<uint32_t>((version >> 48) & 0xFFFFu),
static_cast<uint32_t>((version >> 32) & 0xFFFFu),
static_cast<uint32_t>((version >> 16) & 0xFFFFu),
static_cast<uint32_t>(version & 0xFFFFu));
}
const nsString& pdbNameStr =
PromiseFlatString(mozilla::nt::GetLeafName(pdbPathStr));
SharedLibrary shlib(modStart, modEnd,
0, // DLLs are always mapped at offset 0 on Windows
breakpadId, codeId, PromiseFlatString(moduleNameStr),
nsDependentString(aModulePath), pdbNameStr, pdbPathStr,
versionStr, "");
sharedLibraryInfo.AddSharedLibrary(shlib);
};
auto addSharedLibraryFromModuleInfo =
[&sharedLibraryInfo](const wchar_t* aModulePath, HMODULE aModule) {
AddSharedLibraryFromModuleInfo(sharedLibraryInfo, aModulePath,
mozilla::Some(aModule));
};
mozilla::EnumerateProcessModules(addSharedLibraryFromModuleInfo);
return sharedLibraryInfo;
}
SharedLibraryInfo SharedLibraryInfo::GetInfoFromPath(const wchar_t* aPath) {
SharedLibraryInfo sharedLibraryInfo;
AddSharedLibraryFromModuleInfo(sharedLibraryInfo, aPath, mozilla::Nothing());
return sharedLibraryInfo;
}
void SharedLibraryInfo::Initialize() { /* do nothing */
}

View File

@ -118,6 +118,10 @@ static bool CompareAddresses(const SharedLibrary& first,
class SharedLibraryInfo {
public:
static SharedLibraryInfo GetInfoForSelf();
#ifdef XP_WIN
static SharedLibraryInfo GetInfoFromPath(const wchar_t* aPath);
#endif
static void Initialize();
void AddSharedLibrary(SharedLibrary entry) { mEntries.push_back(entry); }