Skip to content

Commit 0850bc3

Browse files
committed
Bug 1451511: Add cross-process function hooking to DLL interceptor; r=handyman
--HG-- rename : ipc/mscom/DynamicallyLinkedFunctionPtr.h => mozglue/misc/DynamicallyLinkedFunctionPtr.h extra : amend_source : 1eea43cda6e05f722f0b1373535d9ceabac18661
1 parent 9e9c6ca commit 0850bc3

10 files changed

+786
-36
lines changed

ipc/mscom/AgileReference.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
#include "mozilla/mscom/AgileReference.h"
88

9-
#include "DynamicallyLinkedFunctionPtr.h"
109
#include "mozilla/DebugOnly.h"
10+
#include "mozilla/DynamicallyLinkedFunctionPtr.h"
1111
#include "mozilla/Assertions.h"
1212
#include "mozilla/Move.h"
1313

ipc/mscom/DynamicallyLinkedFunctionPtr.h renamed to mozglue/misc/DynamicallyLinkedFunctionPtr.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
* License, v. 2.0. If a copy of the MPL was not distributed with this
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
66

7-
#ifndef mozilla_mscom_DynamicallyLinkedFunctionPtr_h
8-
#define mozilla_mscom_DynamicallyLinkedFunctionPtr_h
7+
#ifndef mozilla_DynamicallyLinkedFunctionPtr_h
8+
#define mozilla_DynamicallyLinkedFunctionPtr_h
99

1010
#include "mozilla/Move.h"
1111
#include <windows.h>
1212

1313
namespace mozilla {
14-
namespace mscom {
1514

1615
template <typename T>
1716
class DynamicallyLinkedFunctionPtr;
@@ -68,8 +67,7 @@ class DynamicallyLinkedFunctionPtr<R (__stdcall*)(Args...)>
6867
FunctionPtrT mFunction;
6968
};
7069

71-
} // namespace mscom
7270
} // namespace mozilla
7371

74-
#endif // mozilla_mscom_DynamicallyLinkedFunctionPtr_h
72+
#endif // mozilla_DynamicallyLinkedFunctionPtr_h
7573

mozglue/misc/WindowsMapRemoteView.cpp

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2+
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3+
/* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
6+
7+
#include "mozilla/WindowsMapRemoteView.h"
8+
9+
#include "mozilla/Assertions.h"
10+
#include "mozilla/DynamicallyLinkedFunctionPtr.h"
11+
12+
#include <winternl.h>
13+
14+
#if (NTDDI_VERSION < NTDDI_WIN10_RS2)
15+
16+
// MapViewOfFile2 is just an inline function that calls MapViewOfFileNuma2 with
17+
// its preferred node set to NUMA_NO_PREFERRED_NODE
18+
PVOID WINAPI
19+
MapViewOfFileNuma2(HANDLE aFileMapping, HANDLE aProcess, ULONG64 aOffset,
20+
PVOID aBaseAddress, SIZE_T aViewSize, ULONG aAllocationType,
21+
ULONG aPageProtection, ULONG aPreferredNode);
22+
23+
BOOL WINAPI
24+
UnmapViewOfFile2(HANDLE aProcess, PVOID aBaseAddress, ULONG aUnmapFlags);
25+
26+
#endif // (NTDDI_VERSION < NTDDI_WIN10_RS2)
27+
28+
enum SECTION_INHERIT
29+
{
30+
ViewShare = 1,
31+
ViewUnmap = 2
32+
};
33+
34+
NTSTATUS NTAPI
35+
NtMapViewOfSection(HANDLE aSection, HANDLE aProcess, PVOID* aBaseAddress,
36+
ULONG_PTR aZeroBits, SIZE_T aCommitSize,
37+
PLARGE_INTEGER aSectionOffset, PSIZE_T aViewSize,
38+
SECTION_INHERIT aInheritDisposition, ULONG aAllocationType,
39+
ULONG aProtectionFlags);
40+
41+
NTSTATUS NTAPI
42+
NtUnmapViewOfSection(HANDLE aProcess, PVOID aBaseAddress);
43+
44+
static DWORD
45+
GetWin32ErrorCode(NTSTATUS aNtStatus)
46+
{
47+
static const mozilla::DynamicallyLinkedFunctionPtr<decltype(&RtlNtStatusToDosError)>
48+
pRtlNtStatusToDosError(L"ntdll.dll", "RtlNtStatusToDosError");
49+
50+
MOZ_ASSERT(!!pRtlNtStatusToDosError);
51+
if (!pRtlNtStatusToDosError) {
52+
return ERROR_GEN_FAILURE;
53+
}
54+
55+
return pRtlNtStatusToDosError(aNtStatus);
56+
}
57+
58+
namespace mozilla {
59+
60+
MFBT_API void*
61+
MapRemoteViewOfFile(HANDLE aFileMapping, HANDLE aProcess, ULONG64 aOffset,
62+
PVOID aBaseAddress, SIZE_T aViewSize, ULONG aAllocationType,
63+
ULONG aProtectionFlags)
64+
{
65+
static const DynamicallyLinkedFunctionPtr<decltype(&MapViewOfFileNuma2)>
66+
pMapViewOfFileNuma2(L"Api-ms-win-core-memory-l1-1-5.dll", "MapViewOfFileNuma2");
67+
68+
if (!!pMapViewOfFileNuma2) {
69+
return pMapViewOfFileNuma2(aFileMapping, aProcess, aOffset, aBaseAddress,
70+
aViewSize, aAllocationType, aProtectionFlags,
71+
NUMA_NO_PREFERRED_NODE);
72+
}
73+
74+
static const DynamicallyLinkedFunctionPtr<decltype(&NtMapViewOfSection)>
75+
pNtMapViewOfSection(L"ntdll.dll", "NtMapViewOfSection");
76+
77+
MOZ_ASSERT(!!pNtMapViewOfSection);
78+
if (!pNtMapViewOfSection) {
79+
return nullptr;
80+
}
81+
82+
// For the sake of consistency, we only permit the same flags that
83+
// MapViewOfFileNuma2 allows
84+
if (aAllocationType != 0 && aAllocationType != MEM_RESERVE &&
85+
aAllocationType != MEM_LARGE_PAGES) {
86+
::SetLastError(ERROR_INVALID_PARAMETER);
87+
return nullptr;
88+
}
89+
90+
NTSTATUS ntStatus;
91+
92+
LARGE_INTEGER offset;
93+
offset.QuadPart = aOffset;
94+
95+
ntStatus = pNtMapViewOfSection(aFileMapping, aProcess, &aBaseAddress, 0, 0,
96+
&offset, &aViewSize, ViewUnmap,
97+
aAllocationType, aProtectionFlags);
98+
if (NT_SUCCESS(ntStatus)) {
99+
::SetLastError(ERROR_SUCCESS);
100+
return aBaseAddress;
101+
}
102+
103+
::SetLastError(GetWin32ErrorCode(ntStatus));
104+
return nullptr;
105+
}
106+
107+
MFBT_API bool
108+
UnmapRemoteViewOfFile(HANDLE aProcess, PVOID aBaseAddress)
109+
{
110+
static const DynamicallyLinkedFunctionPtr<decltype(&UnmapViewOfFile2)>
111+
pUnmapViewOfFile2(L"kernel32.dll", "UnmapViewOfFile2");
112+
113+
if (!!pUnmapViewOfFile2) {
114+
return !!pUnmapViewOfFile2(aProcess, aBaseAddress, 0);
115+
}
116+
117+
static const DynamicallyLinkedFunctionPtr<decltype(&NtUnmapViewOfSection)>
118+
pNtUnmapViewOfSection(L"ntdll.dll", "NtUnmapViewOfSection");
119+
120+
MOZ_ASSERT(!!pNtUnmapViewOfSection);
121+
if (!pNtUnmapViewOfSection) {
122+
return false;
123+
}
124+
125+
NTSTATUS ntStatus = pNtUnmapViewOfSection(aProcess, aBaseAddress);
126+
::SetLastError(GetWin32ErrorCode(ntStatus));
127+
return NT_SUCCESS(ntStatus);
128+
}
129+
130+
} // namespace mozilla
131+

mozglue/misc/WindowsMapRemoteView.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2+
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3+
/* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
6+
7+
#ifndef mozilla_WindowsMapRemoteView_h
8+
#define mozilla_WindowsMapRemoteView_h
9+
10+
#include "mozilla/Types.h"
11+
12+
#include <windows.h>
13+
14+
namespace mozilla {
15+
16+
MFBT_API PVOID
17+
MapRemoteViewOfFile(HANDLE aFileMapping, HANDLE aProcess, ULONG64 aOffset,
18+
PVOID aBaseAddress, SIZE_T aViewSize, ULONG aAllocationType,
19+
ULONG aProtectionFlags);
20+
21+
MFBT_API bool
22+
UnmapRemoteViewOfFile(HANDLE aProcess, PVOID aBaseAddress);
23+
24+
} // namespace mozilla
25+
26+
#endif // mozilla_WindowsMapRemoteView_h

0 commit comments

Comments
 (0)