1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 /**
6 * LSPs are evil little bits of code that are allowed to inject into our
7 * networking stack by Windows. Once they have wormed into our process
8 * they gnaw at our innards until we crash. Here we force the buggers
9 * into the light by recording them in our crash information.
10 * We do the enumeration on a thread because I'm concerned about startup perf
11 * on machines with several LSPs.
12 */
13
14 #include "nsICrashReporter.h"
15 #include "nsISupportsImpl.h"
16 #include "nsServiceManagerUtils.h"
17 #include "nsThreadUtils.h"
18 #include "nsQueryObject.h"
19 #include "nsWindowsHelpers.h"
20 #include <windows.h>
21 #include <rpc.h>
22 #include <ws2spi.h>
23
24 namespace mozilla {
25 namespace crashreporter {
26
27 class LSPAnnotationGatherer : public Runnable {
~LSPAnnotationGatherer()28 ~LSPAnnotationGatherer() {}
29
30 public:
LSPAnnotationGatherer()31 LSPAnnotationGatherer() : Runnable("crashreporter::LSPAnnotationGatherer") {}
32 NS_DECL_NSIRUNNABLE
33
34 void Annotate();
35 nsCString mString;
36 nsCOMPtr<nsIThread> mThread;
37 };
38
Annotate()39 void LSPAnnotationGatherer::Annotate() {
40 nsCOMPtr<nsICrashReporter> cr =
41 do_GetService("@mozilla.org/toolkit/crash-reporter;1");
42 bool enabled;
43 if (cr && NS_SUCCEEDED(cr->GetEnabled(&enabled)) && enabled) {
44 cr->AnnotateCrashReport(NS_LITERAL_CSTRING("Winsock_LSP"), mString);
45 }
46 mThread->AsyncShutdown();
47 }
48
49 NS_IMETHODIMP
Run()50 LSPAnnotationGatherer::Run() {
51 NS_SetCurrentThreadName("LSP Annotator");
52
53 mThread = NS_GetCurrentThread();
54
55 DWORD size = 0;
56 int err;
57 // Get the size of the buffer we need
58 if (SOCKET_ERROR != WSCEnumProtocols(nullptr, nullptr, &size, &err) ||
59 err != WSAENOBUFS) {
60 // Er, what?
61 NS_NOTREACHED("WSCEnumProtocols suceeded when it should have failed ...");
62 return NS_ERROR_FAILURE;
63 }
64
65 auto byteArray = MakeUnique<char[]>(size);
66 WSAPROTOCOL_INFOW* providers =
67 reinterpret_cast<WSAPROTOCOL_INFOW*>(byteArray.get());
68
69 int n = WSCEnumProtocols(nullptr, providers, &size, &err);
70 if (n == SOCKET_ERROR) {
71 // Lame. We provided the right size buffer; we'll just give up now.
72 NS_WARNING("Could not get LSP list");
73 return NS_ERROR_FAILURE;
74 }
75
76 nsCString str;
77 for (int i = 0; i < n; i++) {
78 AppendUTF16toUTF8(nsDependentString(providers[i].szProtocol), str);
79 str.AppendLiteral(" : ");
80 str.AppendInt(providers[i].iVersion);
81 str.AppendLiteral(" : ");
82 str.AppendInt(providers[i].iAddressFamily);
83 str.AppendLiteral(" : ");
84 str.AppendInt(providers[i].iSocketType);
85 str.AppendLiteral(" : ");
86 str.AppendInt(providers[i].iProtocol);
87 str.AppendLiteral(" : ");
88 str.AppendPrintf("0x%x", providers[i].dwServiceFlags1);
89 str.AppendLiteral(" : ");
90 str.AppendPrintf("0x%x", providers[i].dwProviderFlags);
91 str.AppendLiteral(" : ");
92
93 wchar_t path[MAX_PATH];
94 int pathLen = MAX_PATH;
95 if (!WSCGetProviderPath(&providers[i].ProviderId, path, &pathLen, &err)) {
96 AppendUTF16toUTF8(nsDependentString(path), str);
97 }
98
99 str.AppendLiteral(" : ");
100 // If WSCGetProviderInfo is available, we should call it to obtain the
101 // category flags for this provider. When present, these flags inform
102 // Windows as to which order to chain the providers.
103 nsModuleHandle ws2_32(LoadLibraryW(L"ws2_32.dll"));
104 if (ws2_32) {
105 decltype(WSCGetProviderInfo)* pWSCGetProviderInfo =
106 reinterpret_cast<decltype(WSCGetProviderInfo)*>(
107 GetProcAddress(ws2_32, "WSCGetProviderInfo"));
108 if (pWSCGetProviderInfo) {
109 DWORD categoryInfo;
110 size_t categoryInfoSize = sizeof(categoryInfo);
111 if (!pWSCGetProviderInfo(
112 &providers[i].ProviderId, ProviderInfoLspCategories,
113 (PBYTE)&categoryInfo, &categoryInfoSize, 0, &err)) {
114 str.AppendPrintf("0x%x", categoryInfo);
115 }
116 }
117 }
118
119 str.AppendLiteral(" : ");
120 if (providers[i].ProtocolChain.ChainLen <= BASE_PROTOCOL) {
121 // If we're dealing with a catalog entry that identifies an individual
122 // base or layer provider, log its provider GUID.
123 RPC_CSTR provIdStr = nullptr;
124 if (UuidToStringA(&providers[i].ProviderId, &provIdStr) == RPC_S_OK) {
125 str.Append(reinterpret_cast<char*>(provIdStr));
126 RpcStringFreeA(&provIdStr);
127 }
128 }
129
130 if (i + 1 != n) {
131 str.AppendLiteral(" \n ");
132 }
133 }
134
135 mString = str;
136 NS_DispatchToMainThread(
137 NewRunnableMethod("crashreporter::LSPAnnotationGatherer::Annotate", this,
138 &LSPAnnotationGatherer::Annotate));
139 return NS_OK;
140 }
141
LSPAnnotate()142 void LSPAnnotate() {
143 nsCOMPtr<nsIThread> thread;
144 nsCOMPtr<nsIRunnable> runnable = do_QueryObject(new LSPAnnotationGatherer());
145 NS_NewNamedThread("LSP Annotate", getter_AddRefs(thread), runnable);
146 }
147
148 } // namespace crashreporter
149 } // namespace mozilla
150