1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef mozilla_UntrustedModulesData_h
#define mozilla_UntrustedModulesData_h
#if defined(XP_WIN)
# include "ipc/IPCMessageUtils.h"
# include "mozilla/CombinedStacks.h"
# include "mozilla/DebugOnly.h"
# include "mozilla/LinkedList.h"
# include "mozilla/Maybe.h"
# include "mozilla/RefPtr.h"
# include "mozilla/TypedEnumBits.h"
# include "mozilla/Unused.h"
# include "mozilla/Variant.h"
# include "mozilla/Vector.h"
# include "mozilla/WinHeaderOnlyUtils.h"
# include "nsCOMPtr.h"
# include "nsHashKeys.h"
# include "nsIFile.h"
# include "nsISupportsImpl.h"
# include "nsRefPtrHashtable.h"
# include "nsString.h"
# include "nsXULAppAPI.h"
namespace mozilla {
namespace glue {
struct EnhancedModuleLoadInfo;
} // namespace glue
enum class ModuleTrustFlags : uint32_t {
None = 0,
MozillaSignature = 1,
MicrosoftWindowsSignature = 2,
MicrosoftVersion = 4,
FirefoxDirectory = 8,
FirefoxDirectoryAndVersion = 0x10,
SystemDirectory = 0x20,
KeyboardLayout = 0x40,
JitPI = 0x80,
WinSxSDirectory = 0x100,
};
MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(ModuleTrustFlags);
class VendorInfo final {
public:
enum class Source : uint32_t {
None,
Signature,
VersionInfo,
};
VendorInfo() : mSource(Source::None) {}
VendorInfo(const Source aSource, const nsAString& aVendor,
bool aHasNestedMicrosoftSignature)
: mSource(aSource),
mVendor(aVendor),
mHasNestedMicrosoftSignature(aHasNestedMicrosoftSignature) {
MOZ_ASSERT(aSource != Source::None && !aVendor.IsEmpty());
}
Source mSource;
nsString mVendor;
bool mHasNestedMicrosoftSignature;
};
class ModulesMap;
class ModuleRecord final {
public:
explicit ModuleRecord(const nsAString& aResolvedNtPath);
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ModuleRecord)
nsString mResolvedNtName;
nsCOMPtr<nsIFile> mResolvedDosName;
nsString mSanitizedDllName;
Maybe<ModuleVersion> mVersion;
Maybe<VendorInfo> mVendorInfo;
ModuleTrustFlags mTrustFlags;
explicit operator bool() const { return !mSanitizedDllName.IsEmpty(); }
bool IsXUL() const;
bool IsTrusted() const;
ModuleRecord(const ModuleRecord&) = delete;
ModuleRecord(ModuleRecord&&) = delete;
ModuleRecord& operator=(const ModuleRecord&) = delete;
ModuleRecord& operator=(ModuleRecord&&) = delete;
private:
ModuleRecord();
~ModuleRecord() = default;
void GetVersionAndVendorInfo(const nsAString& aPath);
int32_t GetScoreThreshold() const;
friend struct ::IPC::ParamTraits<ModulesMap>;
};
/**
* This type holds module path data using one of two internal representations.
* It may be created from either a nsTHashtable or a Vector, and may be
* serialized from either representation into a common format over the wire.
* Deserialization always uses the Vector representation.
*/
struct ModulePaths final {
using SetType = nsTHashtable<nsStringCaseInsensitiveHashKey>;
using VecType = Vector<nsString>;
Variant<SetType, VecType> mModuleNtPaths;
template <typename T>
explicit ModulePaths(T&& aPaths)
: mModuleNtPaths(AsVariant(std::forward<T>(aPaths))) {}
ModulePaths() : mModuleNtPaths(VecType()) {}
ModulePaths(const ModulePaths& aOther) = delete;
ModulePaths(ModulePaths&& aOther) = default;
ModulePaths& operator=(const ModulePaths&) = delete;
ModulePaths& operator=(ModulePaths&&) = default;
};
class ProcessedModuleLoadEvent final {
public:
ProcessedModuleLoadEvent();
ProcessedModuleLoadEvent(glue::EnhancedModuleLoadInfo&& aModLoadInfo,
RefPtr<ModuleRecord>&& aModuleRecord);
explicit operator bool() const { return mModule && *mModule; }
bool IsXULLoad() const;
bool IsTrusted() const;
uint64_t mProcessUptimeMS;
Maybe<double> mLoadDurationMS;
DWORD mThreadId;
nsCString mThreadName;
nsString mRequestedDllName;
// We intentionally store mBaseAddress as part of the event and not the
// module, as relocation may cause it to change between loads. If so, we want
// to know about it.
uintptr_t mBaseAddress;
RefPtr<ModuleRecord> mModule;
bool mIsDependent;
uint32_t mLoadStatus; // corresponding to enum ModuleLoadInfo::Status
ProcessedModuleLoadEvent(const ProcessedModuleLoadEvent&) = delete;
ProcessedModuleLoadEvent& operator=(const ProcessedModuleLoadEvent&) = delete;
ProcessedModuleLoadEvent(ProcessedModuleLoadEvent&&) = default;
ProcessedModuleLoadEvent& operator=(ProcessedModuleLoadEvent&&) = default;
void SanitizeRequestedDllName();
private:
static Maybe<LONGLONG> ComputeQPCTimeStampForProcessCreation();
static uint64_t QPCTimeStampToProcessUptimeMilliseconds(
const LARGE_INTEGER& aTimeStamp);
};
// Declaring ModulesMap this way makes it much easier to forward declare than
// if we had used |using| or |typedef|.
class ModulesMap final
: public nsRefPtrHashtable<nsStringCaseInsensitiveHashKey, ModuleRecord> {
public:
ModulesMap()
: nsRefPtrHashtable<nsStringCaseInsensitiveHashKey, ModuleRecord>() {}
};
struct ProcessedModuleLoadEventContainer final
: public LinkedListElement<ProcessedModuleLoadEventContainer> {
ProcessedModuleLoadEvent mEvent;
ProcessedModuleLoadEventContainer() = default;
explicit ProcessedModuleLoadEventContainer(ProcessedModuleLoadEvent&& aEvent)
: mEvent(std::move(aEvent)) {}
ProcessedModuleLoadEventContainer(ProcessedModuleLoadEventContainer&&) =
default;
ProcessedModuleLoadEventContainer& operator=(
ProcessedModuleLoadEventContainer&&) = default;
ProcessedModuleLoadEventContainer(const ProcessedModuleLoadEventContainer&) =
delete;
ProcessedModuleLoadEventContainer& operator=(
const ProcessedModuleLoadEventContainer&) = delete;
};
using UntrustedModuleLoadingEvents =
AutoCleanLinkedList<ProcessedModuleLoadEventContainer>;
class UntrustedModulesData final {
// Merge aNewData.mEvents into this->mModules and also
// make module entries in aNewData point to items in this->mModules.
void MergeModules(UntrustedModulesData& aNewData);
public:
// Ensure mEvents will never retain more than kMaxEvents events.
// This constant matches the maximum in Telemetry::CombinedStacks.
// Truncate() relies on these being the same.
static constexpr size_t kMaxEvents = 50;
UntrustedModulesData()
: mProcessType(XRE_GetProcessType()),
mPid(::GetCurrentProcessId()),
mNumEvents(0),
mSanitizationFailures(0),
mTrustTestFailures(0) {
MOZ_ASSERT(kMaxEvents == mStacks.GetMaxStacksCount());
}
UntrustedModulesData(UntrustedModulesData&&) = default;
UntrustedModulesData& operator=(UntrustedModulesData&&) = default;
UntrustedModulesData(const UntrustedModulesData&) = delete;
UntrustedModulesData& operator=(const UntrustedModulesData&) = delete;
explicit operator bool() const {
return !mEvents.isEmpty() || mSanitizationFailures || mTrustTestFailures ||
mXULLoadDurationMS.isSome();
}
void AddNewLoads(const ModulesMap& aModulesMap,
UntrustedModuleLoadingEvents&& aEvents,
Vector<Telemetry::ProcessedStack>&& aStacks);
void Merge(UntrustedModulesData&& aNewData);
void MergeWithoutStacks(UntrustedModulesData&& aNewData);
void Swap(UntrustedModulesData& aOther);
// Drop callstack data and old loading events.
void Truncate(bool aDropCallstackData);
GeckoProcessType mProcessType;
DWORD mPid;
TimeDuration mElapsed;
ModulesMap mModules;
uint32_t mNumEvents;
UntrustedModuleLoadingEvents mEvents;
Telemetry::CombinedStacks mStacks;
Maybe<double> mXULLoadDurationMS;
uint32_t mSanitizationFailures;
uint32_t mTrustTestFailures;
};
class ModulesMapResult final {
public:
ModulesMapResult() : mTrustTestFailures(0) {}
ModulesMapResult(const ModulesMapResult& aOther) = delete;
ModulesMapResult(ModulesMapResult&& aOther) = default;
ModulesMapResult& operator=(const ModulesMapResult& aOther) = delete;
ModulesMapResult& operator=(ModulesMapResult&& aOther) = default;
ModulesMap mModules;
uint32_t mTrustTestFailures;
};
} // namespace mozilla
namespace IPC {
template <>
struct ParamTraits<mozilla::ModuleVersion> {
typedef mozilla::ModuleVersion paramType;
static void Write(MessageWriter* aWriter, const paramType& aParam) {
aWriter->WriteUInt64(aParam.AsInteger());
}
static bool Read(MessageReader* aReader, paramType* aResult) {
uint64_t ver;
if (!aReader->ReadUInt64(&ver)) {
return false;
}
*aResult = ver;
return true;
}
};
template <>
struct ParamTraits<mozilla::VendorInfo> {
typedef mozilla::VendorInfo paramType;
static void Write(MessageWriter* aWriter, const paramType& aParam) {
aWriter->WriteUInt32(static_cast<uint32_t>(aParam.mSource));
WriteParam(aWriter, aParam.mVendor);
WriteParam(aWriter, aParam.mHasNestedMicrosoftSignature);
}
static bool Read(MessageReader* aReader, paramType* aResult) {
uint32_t source;
if (!aReader->ReadUInt32(&source)) {
return false;
}
aResult->mSource = static_cast<mozilla::VendorInfo::Source>(source);
if (!ReadParam(aReader, &aResult->mVendor)) {
return false;
}
if (!ReadParam(aReader, &aResult->mHasNestedMicrosoftSignature)) {
return false;
}
return true;
}
};
template <>
struct ParamTraits<mozilla::ModuleRecord> {
typedef mozilla::ModuleRecord paramType;
static void Write(MessageWriter* aWriter, const paramType& aParam) {
WriteParam(aWriter, aParam.mResolvedNtName);
nsAutoString resolvedDosName;
if (aParam.mResolvedDosName) {
mozilla::DebugOnly<nsresult> rv =
aParam.mResolvedDosName->GetPath(resolvedDosName);
MOZ_ASSERT(NS_SUCCEEDED(rv));
}
WriteParam(aWriter, resolvedDosName);
WriteParam(aWriter, aParam.mSanitizedDllName);
WriteParam(aWriter, aParam.mVersion);
WriteParam(aWriter, aParam.mVendorInfo);
aWriter->WriteUInt32(static_cast<uint32_t>(aParam.mTrustFlags));
}
static bool Read(MessageReader* aReader, paramType* aResult) {
if (!ReadParam(aReader, &aResult->mResolvedNtName)) {
return false;
}
nsAutoString resolvedDosName;
if (!ReadParam(aReader, &resolvedDosName)) {
return false;
}
if (resolvedDosName.IsEmpty()) {
aResult->mResolvedDosName = nullptr;
} else if (NS_FAILED(NS_NewLocalFile(
resolvedDosName, false,
getter_AddRefs(aResult->mResolvedDosName)))) {
return false;
}
if (!ReadParam(aReader, &aResult->mSanitizedDllName)) {
return false;
}
if (!ReadParam(aReader, &aResult->mVersion)) {
return false;
}
if (!ReadParam(aReader, &aResult->mVendorInfo)) {
return false;
}
uint32_t trustFlags;
if (!aReader->ReadUInt32(&trustFlags)) {
return false;
}
aResult->mTrustFlags = static_cast<mozilla::ModuleTrustFlags>(trustFlags);
return true;
}
};
template <>
struct ParamTraits<mozilla::ModulesMap> {
typedef mozilla::ModulesMap paramType;
static void Write(MessageWriter* aWriter, const paramType& aParam) {
aWriter->WriteUInt32(aParam.Count());
for (const auto& entry : aParam) {
MOZ_RELEASE_ASSERT(entry.GetData());
WriteParam(aWriter, entry.GetKey());
WriteParam(aWriter, *(entry.GetData()));
}
}
static bool Read(MessageReader* aReader, paramType* aResult) {
uint32_t count;
if (!ReadParam(aReader, &count)) {
return false;
}
for (uint32_t current = 0; current < count; ++current) {
nsAutoString key;
if (!ReadParam(aReader, &key) || key.IsEmpty()) {
return false;
}
RefPtr<mozilla::ModuleRecord> rec(new mozilla::ModuleRecord());
if (!ReadParam(aReader, rec.get())) {
return false;
}
aResult->InsertOrUpdate(key, std::move(rec));
}
return true;
}
};
template <>
struct ParamTraits<mozilla::ModulePaths> {
typedef mozilla::ModulePaths paramType;
static void Write(MessageWriter* aWriter, const paramType& aParam) {
aParam.mModuleNtPaths.match(
[aWriter](const paramType::SetType& aSet) { WriteSet(aWriter, aSet); },
[aWriter](const paramType::VecType& aVec) {
WriteVector(aWriter, aVec);
});
}
static bool Read(MessageReader* aReader, paramType* aResult) {
uint32_t len;
if (!aReader->ReadUInt32(&len)) {
return false;
}
// As noted in the comments for ModulePaths, we only deserialize using the
// Vector representation.
auto& vec = aResult->mModuleNtPaths.as<paramType::VecType>();
if (!vec.reserve(len)) {
return false;
}
for (uint32_t idx = 0; idx < len; ++idx) {
nsString str;
if (!ReadParam(aReader, &str)) {
return false;
}
if (!vec.emplaceBack(std::move(str))) {
return false;
}
}
return true;
}
private:
// NB: This function must write out the set in the same format as WriteVector
static void WriteSet(MessageWriter* aWriter, const paramType::SetType& aSet) {
aWriter->WriteUInt32(aSet.Count());
for (const auto& key : aSet.Keys()) {
WriteParam(aWriter, key);
}
}
// NB: This function must write out the vector in the same format as WriteSet
static void WriteVector(MessageWriter* aWriter,
const paramType::VecType& aVec) {
aWriter->WriteUInt32(aVec.length());
for (auto const& item : aVec) {
WriteParam(aWriter, item);
}
}
};
template <>
struct ParamTraits<mozilla::UntrustedModulesData> {
typedef mozilla::UntrustedModulesData paramType;
static void Write(MessageWriter* aWriter, const paramType& aParam) {
aWriter->WriteUInt32(aParam.mProcessType);
aWriter->WriteULong(aParam.mPid);
WriteParam(aWriter, aParam.mElapsed);
WriteParam(aWriter, aParam.mModules);
aWriter->WriteUInt32(aParam.mNumEvents);
for (auto event : aParam.mEvents) {
WriteEvent(aWriter, event->mEvent);
}
WriteParam(aWriter, aParam.mStacks);
WriteParam(aWriter, aParam.mXULLoadDurationMS);
aWriter->WriteUInt32(aParam.mSanitizationFailures);
aWriter->WriteUInt32(aParam.mTrustTestFailures);
}
static bool Read(MessageReader* aReader, paramType* aResult) {
uint32_t processType;
if (!aReader->ReadUInt32(&processType)) {
return false;
}
aResult->mProcessType = static_cast<GeckoProcessType>(processType);
if (!aReader->ReadULong(&aResult->mPid)) {
return false;
}
if (!ReadParam(aReader, &aResult->mElapsed)) {
return false;
}
if (!ReadParam(aReader, &aResult->mModules)) {
return false;
}
// We read mEvents manually so that we can use ReadEvent defined below.
if (!ReadParam(aReader, &aResult->mNumEvents)) {
return false;
}
for (uint32_t curEventIdx = 0; curEventIdx < aResult->mNumEvents;
++curEventIdx) {
auto newEvent =
mozilla::MakeUnique<mozilla::ProcessedModuleLoadEventContainer>();
if (!ReadEvent(aReader, &newEvent->mEvent, aResult->mModules)) {
return false;
}
aResult->mEvents.insertBack(newEvent.release());
}
if (!ReadParam(aReader, &aResult->mStacks)) {
return false;
}
if (!ReadParam(aReader, &aResult->mXULLoadDurationMS)) {
return false;
}
if (!aReader->ReadUInt32(&aResult->mSanitizationFailures)) {
return false;
}
if (!aReader->ReadUInt32(&aResult->mTrustTestFailures)) {
return false;
}
return true;
}
private:
// Because ProcessedModuleLoadEvent depends on a hash table from
// UntrustedModulesData, we do its serialization as part of this
// specialization.
static void WriteEvent(MessageWriter* aWriter,
const mozilla::ProcessedModuleLoadEvent& aParam) {
aWriter->WriteUInt64(aParam.mProcessUptimeMS);
WriteParam(aWriter, aParam.mLoadDurationMS);
aWriter->WriteULong(aParam.mThreadId);
WriteParam(aWriter, aParam.mThreadName);
WriteParam(aWriter, aParam.mRequestedDllName);
WriteParam(aWriter, aParam.mBaseAddress);
WriteParam(aWriter, aParam.mIsDependent);
WriteParam(aWriter, aParam.mLoadStatus);
// We don't write the ModuleRecord directly; we write its key into the
// UntrustedModulesData::mModules hash table.
MOZ_ASSERT(aParam.mModule && !aParam.mModule->mResolvedNtName.IsEmpty());
WriteParam(aWriter, aParam.mModule->mResolvedNtName);
}
// Because ProcessedModuleLoadEvent depends on a hash table from
// UntrustedModulesData, we do its deserialization as part of this
// specialization.
static bool ReadEvent(MessageReader* aReader,
mozilla::ProcessedModuleLoadEvent* aResult,
const mozilla::ModulesMap& aModulesMap) {
if (!aReader->ReadUInt64(&aResult->mProcessUptimeMS)) {
return false;
}
if (!ReadParam(aReader, &aResult->mLoadDurationMS)) {
return false;
}
if (!aReader->ReadULong(&aResult->mThreadId)) {
return false;
}
if (!ReadParam(aReader, &aResult->mThreadName)) {
return false;
}
if (!ReadParam(aReader, &aResult->mRequestedDllName)) {
return false;
}
// When ProcessedModuleLoadEvent was constructed in a child process, we left
// mRequestedDllName unsanitized, so now is a good time to sanitize it.
aResult->SanitizeRequestedDllName();
if (!ReadParam(aReader, &aResult->mBaseAddress)) {
return false;
}
if (!ReadParam(aReader, &aResult->mIsDependent)) {
return false;
}
if (!ReadParam(aReader, &aResult->mLoadStatus)) {
return false;
}
nsAutoString resolvedNtName;
if (!ReadParam(aReader, &resolvedNtName)) {
return false;
}
// NB: While bad data integrity might for some reason result in a null
// mModule, we do not fail the deserialization; this is a data error,
// rather than an IPC error. The error is detected and dealt with in
// telemetry.
aResult->mModule = aModulesMap.Get(resolvedNtName);
return true;
}
};
template <>
struct ParamTraits<mozilla::ModulesMapResult> {
typedef mozilla::ModulesMapResult paramType;
static void Write(MessageWriter* aWriter, const paramType& aParam) {
WriteParam(aWriter, aParam.mModules);
aWriter->WriteUInt32(aParam.mTrustTestFailures);
}
static bool Read(MessageReader* aReader, paramType* aResult) {
if (!ReadParam(aReader, &aResult->mModules)) {
return false;
}
if (!aReader->ReadUInt32(&aResult->mTrustTestFailures)) {
return false;
}
return true;
}
};
} // namespace IPC
#else // defined(XP_WIN)
namespace mozilla {
// For compiling IPDL on non-Windows platforms
using UntrustedModulesData = uint32_t;
using ModulePaths = uint32_t;
using ModulesMapResult = uint32_t;
} // namespace mozilla
#endif // defined(XP_WIN)
#endif // mozilla_UntrustedModulesData_h
|