/
DxePagingAuditTestApp.c
293 lines (243 loc) · 9.8 KB
/
DxePagingAuditTestApp.c
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
/** @file -- DxePagingAuditTestApp.c
This Shell App tests the page table or writes page table and
memory map information to SFS
Copyright (c) Microsoft Corporation.
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "../../PagingAuditCommon.h"
#include <Library/UnitTestLib.h>
#include <Library/CpuPageTableLib.h>
#include <Library/DxeMemoryProtectionHobLib.h>
#include <Protocol/MemoryProtectionSpecialRegionProtocol.h>
#include <Protocol/ShellParameters.h>
#include <Protocol/Shell.h>
#define UNIT_TEST_APP_NAME "Paging Audit Test"
#define UNIT_TEST_APP_VERSION "1"
#define MAX_CHARS_TO_READ 3
// TRUE if A interval subsumes B interval
#define CHECK_SUBSUMPTION(AStart, AEnd, BStart, BEnd) \
((AStart <= BStart) && (AEnd >= BEnd))
typedef struct _PAGING_AUDIT_TEST_CONTEXT {
IA32_MAP_ENTRY *Entries;
UINTN Count;
} PAGING_AUDIT_TEST_CONTEXT;
CHAR8 *mMemoryInfoDatabaseBuffer = NULL;
UINTN mMemoryInfoDatabaseSize = 0;
UINTN mMemoryInfoDatabaseAllocSize = 0;
/**
Check the page table for Read/Write/Execute regions.
@param[in] Context Unit test context
@retval UNIT_TEST_PASSED The unit test passed
@retval other The unit test failed
**/
UNIT_TEST_STATUS
EFIAPI
NoReadWriteExecute (
IN UNIT_TEST_CONTEXT Context
)
{
IA32_MAP_ENTRY *Map = ((PAGING_AUDIT_TEST_CONTEXT *)Context)->Entries;
UINTN MapCount = ((PAGING_AUDIT_TEST_CONTEXT *)Context)->Count;
UINTN Index = 0;
BOOLEAN FoundRWXAddress = FALSE;
BOOLEAN IgnoreRWXAddress = FALSE;
MEMORY_PROTECTION_DEBUG_PROTOCOL *MemoryProtectionProtocol = NULL;
MEMORY_PROTECTION_SPECIAL_REGION_PROTOCOL *SpecialRegionProtocol = NULL;
MEMORY_PROTECTION_SPECIAL_REGION *SpecialRegions = NULL;
UINTN SpecialRegionCount = 0;
UINTN SpecialRegionIndex = 0;
IMAGE_RANGE_DESCRIPTOR *NonProtectedImageList = NULL;
LIST_ENTRY *NonProtectedImageLink = NULL;
IMAGE_RANGE_DESCRIPTOR *NonProtectedImage = NULL;
UT_ASSERT_NOT_EFI_ERROR (
gBS->LocateProtocol (
&gMemoryProtectionDebugProtocolGuid,
NULL,
(VOID **)&MemoryProtectionProtocol
)
);
UT_ASSERT_NOT_EFI_ERROR (
MemoryProtectionProtocol->GetImageList (
&NonProtectedImageList,
NonProtected
)
);
UT_ASSERT_NOT_EFI_ERROR (
gBS->LocateProtocol (
&gMemoryProtectionSpecialRegionProtocolGuid,
NULL,
(VOID **)&SpecialRegionProtocol
)
);
UT_ASSERT_NOT_EFI_ERROR (
SpecialRegionProtocol->GetSpecialRegions (
&SpecialRegions,
&SpecialRegionCount
)
);
for ( ; Index < MapCount; Index++) {
if ((Map[Index].Attribute.Bits.ReadWrite != 0) && (Map[Index].Attribute.Bits.Nx == 0)) {
IgnoreRWXAddress = FALSE;
if (NonProtectedImageList != NULL) {
for (NonProtectedImageLink = NonProtectedImageList->Link.ForwardLink;
NonProtectedImageLink != &NonProtectedImageList->Link;
NonProtectedImageLink = NonProtectedImageLink->ForwardLink)
{
NonProtectedImage = CR (
NonProtectedImageLink,
IMAGE_RANGE_DESCRIPTOR,
Link,
IMAGE_RANGE_DESCRIPTOR_SIGNATURE
);
if CHECK_SUBSUMPTION (
NonProtectedImage->Base,
NonProtectedImage->Base + NonProtectedImage->Length,
Map[Index].LinearAddress,
Map[Index].LinearAddress + Map[Index].Length
) {
IgnoreRWXAddress = TRUE;
break;
}
}
}
if ((SpecialRegionCount > 0) && !IgnoreRWXAddress) {
for (SpecialRegionIndex = 0; SpecialRegionIndex < SpecialRegionCount; SpecialRegionIndex++) {
if (CHECK_SUBSUMPTION (
SpecialRegions[SpecialRegionIndex].Start,
SpecialRegions[SpecialRegionIndex].Start + SpecialRegions[SpecialRegionIndex].Length,
Map[Index].LinearAddress,
Map[Index].LinearAddress + Map[Index].Length
) &&
(SpecialRegions[SpecialRegionIndex].EfiAttributes == 0))
{
IgnoreRWXAddress = TRUE;
break;
}
}
}
if (!IgnoreRWXAddress) {
UT_LOG_ERROR ("Memory Range 0x%llx-0x%llx is Read/Write/Execute\n", Map[Index].LinearAddress, Map[Index].LinearAddress + Map[Index].Length);
FoundRWXAddress = TRUE;
} else {
UT_LOG_WARNING ("Memory Range 0x%llx-0x%llx is Read/Write/Execute. This range is excepted from the test.\n", Map[Index].LinearAddress, Map[Index].LinearAddress + Map[Index].Length);
}
}
}
UT_ASSERT_FALSE (FoundRWXAddress);
return UNIT_TEST_PASSED;
}
/**
DxePagingAuditTestAppEntryPoint
@param[in] ImageHandle The firmware allocated handle for the EFI image.
@param[in] SystemTable A pointer to the EFI System Table.
@retval EFI_SUCCESS The entry point executed successfully.
@retval other Some error occurred when executing this entry point.
**/
EFI_STATUS
EFIAPI
DxePagingAuditTestAppEntryPoint (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
EFI_STATUS Status;
UNIT_TEST_FRAMEWORK_HANDLE Fw = NULL;
UNIT_TEST_SUITE_HANDLE Misc = NULL;
PAGING_AUDIT_TEST_CONTEXT *Context;
IA32_CR4 Cr4;
PAGING_MODE PagingMode;
IA32_MAP_ENTRY *Map = NULL;
UINTN MapCount = 0;
UINTN PagesAllocated = 0;
BOOLEAN RunTests = TRUE;
EFI_SHELL_PARAMETERS_PROTOCOL *ShellParams;
DEBUG ((DEBUG_ERROR, "%a()\n", __FUNCTION__));
DEBUG ((DEBUG_ERROR, "%a v%a\n", UNIT_TEST_APP_NAME, UNIT_TEST_APP_VERSION));
Status = gBS->HandleProtocol (
gImageHandle,
&gEfiShellParametersProtocolGuid,
(VOID **)&ShellParams
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_INFO, "%a Could not retrieve command line args!\n", __FUNCTION__));
return EFI_PROTOCOL_ERROR;
}
if (ShellParams->Argc > 1) {
RunTests = FALSE;
if (StrnCmp (ShellParams->Argv[1], L"-r", 4) == 0) {
RunTests = TRUE;
} else if (StrnCmp (ShellParams->Argv[1], L"-d", 4) == 0) {
DumpPagingInfo (NULL, NULL);
} else {
if (StrnCmp (ShellParams->Argv[1], L"-h", 4) != 0) {
DEBUG ((DEBUG_INFO, "Invalid argument!\n"));
}
DEBUG ((DEBUG_INFO, "-h : Print available flags\n"));
DEBUG ((DEBUG_INFO, "-d : Dump the page table files to the EFI partition\n"));
DEBUG ((DEBUG_INFO, "-r : Run the application tests\n"));
DEBUG ((DEBUG_INFO, "NOTE: Combined flags (i.e. -rd) is not supported\n"));
}
}
if (RunTests) {
Context = (PAGING_AUDIT_TEST_CONTEXT *)AllocateZeroPool (sizeof (PAGING_AUDIT_TEST_CONTEXT));
if (Context == NULL) {
DEBUG ((DEBUG_ERROR, "Failed to allocate test context\n"));
goto EXIT;
}
//
// Start setting up the test framework for running the tests.
//
Status = InitUnitTestFramework (&Fw, UNIT_TEST_APP_NAME, gEfiCallerBaseName, UNIT_TEST_APP_VERSION);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status));
goto EXIT;
}
// Poll CR4 to deterimine the page table depth
Cr4.UintN = AsmReadCr4 ();
if (Cr4.Bits.LA57 != 0) {
PagingMode = Paging5Level;
} else {
PagingMode = Paging4Level;
}
// CR3 is the page table pointer
Status = PageTableParse (AsmReadCr3 (), PagingMode, NULL, &MapCount);
while (Status == RETURN_BUFFER_TOO_SMALL) {
if ((Map != NULL) && (PagesAllocated > 0)) {
FreePages (Map, PagesAllocated);
}
PagesAllocated = EFI_SIZE_TO_PAGES (MapCount * sizeof (IA32_MAP_ENTRY));
Map = AllocatePages (PagesAllocated);
if (Map == NULL) {
DEBUG ((DEBUG_ERROR, "Failed to allocate page table map\n"));
goto EXIT;
}
Status = PageTableParse (AsmReadCr3 (), PagingMode, Map, &MapCount);
}
Context->Entries = Map;
Context->Count = MapCount;
//
// Create test suite
//
CreateUnitTestSuite (&Misc, Fw, "Miscellaneous tests", "Security.Misc", NULL, NULL);
if (Misc == NULL) {
DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for TestSuite\n"));
goto EXIT;
}
AddTestCase (Misc, "No pages can be read,write,execute", "Security.Misc.NoReadWriteExecute", NoReadWriteExecute, NULL, NULL, Context);
//
// Execute the tests.
//
Status = RunAllTestSuites (Fw);
EXIT:
if (Fw) {
FreeUnitTestFramework (Fw);
}
if ((Map != NULL) && (PagesAllocated > 0)) {
FreePages (Map, PagesAllocated);
}
if (Context != NULL) {
FreePool (Context);
}
}
return EFI_SUCCESS;
} // DxePagingAuditTestAppEntryPoint()