-
Notifications
You must be signed in to change notification settings - Fork 82
/
specucheck.c
468 lines (428 loc) · 17 KB
/
specucheck.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
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
/*++
Copyright (c) Alex Ionescu. All rights reserved.
Module Name:
specucheck.c
Abstract:
This module implements a checker app for CVE-2017-5754 and CVE-2017-5715
Author:
Alex Ionescu (@aionescu) 03-Jan-2018 - Initial version
Environment:
User mode only.
--*/
//
// OS Headers
//
#include <ntstatus.h>
#define WIN32_NO_STATUS
#include <windows.h>
#include <winternl.h>
#include <wchar.h>
//
// Internal structures and information classes
//
#pragma warning(push)
#pragma warning(disable:4214)
#define SystemSpeculationControlInformation (SYSTEM_INFORMATION_CLASS)201
typedef struct _SYSTEM_SPECULATION_CONTROL_INFORMATION
{
struct
{
ULONG BpbEnabled : 1;
ULONG BpbDisabledSystemPolicy : 1;
ULONG BpbDisabledNoHardwareSupport : 1;
ULONG SpecCtrlEnumerated : 1;
ULONG SpecCmdEnumerated : 1;
ULONG IbrsPresent : 1;
ULONG StibpPresent : 1;
ULONG SmepPresent : 1;
ULONG SpeculativeStoreBypassDisableAvailable : 1;
ULONG SpeculativeStoreBypassDisableSupported : 1;
ULONG SpeculativeStoreBypassDisabledSystemWide : 1;
ULONG SpeculativeStoreBypassDisabledKernel : 1;
ULONG SpeculativeStoreBypassDisableRequired : 1;
ULONG BpbDisabledKernelToUser : 1;
ULONG SpecCtrlRetpolineEnabled : 1;
ULONG SpecCtrlImportOptimizationEnabled : 1;
ULONG Reserved : 24;
} SpeculationControlFlags;
} SYSTEM_SPECULATION_CONTROL_INFORMATION, *PSYSTEM_SPECULATION_CONTROL_INFORMATION;
#define SystemKernelVaShadowInformation (SYSTEM_INFORMATION_CLASS)196
typedef struct _SYSTEM_KERNEL_VA_SHADOW_INFORMATION
{
struct
{
ULONG KvaShadowEnabled : 1;
ULONG KvaShadowUserGlobal : 1;
ULONG KvaShadowPcid : 1;
ULONG KvaShadowInvpcid : 1;
ULONG KvaShadowRequired : 1;
ULONG KvaShadowRequiredAvailable : 1;
ULONG InvalidPteBit : 6;
ULONG L1DataCacheFlushSupported : 1;
ULONG L1TerminalFaultMitigationPresent : 1;
ULONG Reserved : 18;
} KvaShadowFlags;
} SYSTEM_KERNEL_VA_SHADOW_INFORMATION, *PSYSTEM_KERNEL_VA_SHADOW_INFORMATION;
#pragma warning(pop)
//
// ANSI Check
//
BOOL g_SupportsAnsi;
//
// Welcome Banner
//
const WCHAR WelcomeString[] =
L"SpecuCheck v1.1.0 -- Copyright(c) 2018 Alex Ionescu\n"
L"https://ionescu007.github.io/SpecuCheck/ -- @aionescu\n"
L"--------------------------------------------------------\n\n";
//
// Error String
//
const WCHAR UnpatchedString[] =
L"Your system either does not have the appropriate patch, "
L"or it may not support the information class required.\n";
//
// KVA Status String
//
const WCHAR g_KvaStatusString[] =
L"%sMitigations for %sCVE-2017-5754 [rogue data cache load]%s\n"
L"--------------------------------------------------------\n"
L"[-] Kernel VA Shadowing Enabled: %s%s\n"
L" ├───> Unnecessary due lack of CPU vulnerability: %s%s\n"
L" ├───> With User Pages Marked Global: %s%s\n"
L" ├───> With PCID Support: %s%s\n"
L" └───> With PCID Flushing Optimization (INVPCID): %s%s\n\n";
//
// L1Tf Status String
//
const WCHAR g_L1tfStatusString[] =
L"%sMitigations for %sCVE-2018-3620 [L1 terminal fault]%s\n"
L"--------------------------------------------------------\n"
L"[-] L1TF Mitigation Enabled: %s%s\n"
L" ├───> Unnecessary due lack of CPU vulnerability: %s%s\n"
L" ├───> CPU Microcode Supports Data Cache Flush: %s%s\n"
L" └───> With KVA Shadow and Invalid PTE Bit: %s%s\n\n";
//
// Speculation Control Status String
//
const WCHAR g_SpecControlStatusString[] =
L"%sMitigations for %sCVE-2017-5715 [branch target injection]%s\n"
L"--------------------------------------------------------\n"
L"[-] Branch Prediction Mitigations Enabled: %s%s\n"
L" ├───> Disabled due to System Policy (Registry): %s%s\n"
L" ├───> Disabled due to Lack of Microcode Update: %s%s\n"
L" └───> Disabled for kernel to user transitions: %s%s\n"
L"[-] Branch Prediction Mitigations Optimized: %s%s\n"
L" └───> With Import Address Table Optimization: %s%s\n"
L"[-] CPU Microcode Supports SPEC_CTRL MSR (048h): %s%s\n"
L" ├───> Windows will use IBRS (01h): %s%s\n"
L" └───> Windows will use STIPB (02h): %s%s\n"
L"[-] CPU Microcode Supports PRED_CMD MSR (049h): %s%s\n"
L" └───> Windows will use IBPB (01h): %s%s\n\n";
//
// Speculation Control (2) Status String
//
const WCHAR g_SpecControlStatusString2[] =
L"%sMitigations for %sCVE-2018-3639 [speculative store bypass]%s\n"
L"--------------------------------------------------------\n"
L"[-] SSBD Mitigations Enabled: %s%s\n"
L" ├───> Disabled due to lack of OS Support: %s%s\n"
L" ├───> Disabled due to lack of Microcode Update: %s%s\n"
L" ├───> Enabled for system-wide transitions: %s%s\n"
L" └───> Enabled for kernel-mode transitions only: %s%s\n";
//
// Error codes used for clarity
//
typedef enum _SPC_ERROR_CODES
{
SpcSuccess = 0,
SpcFailedToOpenStandardOut = -2,
SpcFailedToQueryKvaShadowing = -3,
SpcFailedToQuerySpeculationControl = -4,
SpcUnknownInfoClassFailure = -5,
} SPC_ERROR_CODES;
PCHAR
FORCEINLINE
GetResetString (
VOID
)
{
return g_SupportsAnsi ? "\x1b[0m" : "";
}
PCHAR
FORCEINLINE
GetRedNoString (
VOID
)
{
return g_SupportsAnsi ? "\x1b[1;31m no" : " no (undesirable)";
}
PCHAR
FORCEINLINE
GetGreenYesString (
VOID
)
{
return g_SupportsAnsi ? "\x1b[1;32myes" : "yes (desirable)";
}
PCHAR
FORCEINLINE
GetRedYesString (
VOID
)
{
return g_SupportsAnsi ? "\x1b[1;31myes" : "yes (undesirable)";
}
PCHAR
FORCEINLINE
GetGreenNoString (
VOID
)
{
return g_SupportsAnsi ? "\x1b[1;32m no" : " no (desirable)";
}
PCHAR
FORCEINLINE
GetCyanString (
VOID
)
{
return g_SupportsAnsi ? "\x1b[1;36m" : "";
}
INT
SpcMain (
VOID
)
{
HANDLE hStdOut;
NTSTATUS status;
SYSTEM_KERNEL_VA_SHADOW_INFORMATION kvaInfo;
SYSTEM_SPECULATION_CONTROL_INFORMATION specInfo;
SPC_ERROR_CODES errorCode;
WCHAR stateBuffer[2048];
INT charsWritten;
//
// Open the output handle -- also not much we can do if this fails
//
hStdOut = CreateFile(L"CONOUT$",
GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hStdOut == INVALID_HANDLE_VALUE)
{
hStdOut = INVALID_HANDLE_VALUE;
errorCode = SpcFailedToOpenStandardOut;
goto Exit;
}
//
// Enable ANSI on Windows 10 if supported
//
g_SupportsAnsi = SetConsoleMode(hStdOut,
ENABLE_PROCESSED_OUTPUT |
ENABLE_VIRTUAL_TERMINAL_PROCESSING);
SetConsoleTitle(L"SpecuCheck v1.1.1");
//
// We now have display capabilities -- say hello!
//
WriteConsole(hStdOut, WelcomeString, ARRAYSIZE(WelcomeString) - 1, NULL, NULL);
//
// Get the KVA Shadow Information
//
status = NtQuerySystemInformation(SystemKernelVaShadowInformation,
&kvaInfo,
sizeof(kvaInfo),
NULL);
if (status == STATUS_INVALID_INFO_CLASS)
{
//
// Print out an error if this failed
//
WriteConsole(hStdOut,
UnpatchedString,
ARRAYSIZE(UnpatchedString) - 1,
NULL,
NULL);
errorCode = SpcFailedToQueryKvaShadowing;
goto Exit;
}
if (status == STATUS_NOT_IMPLEMENTED)
{
//
// x86 Systems without the mitigation active
//
RtlZeroMemory(&kvaInfo, sizeof(kvaInfo));
}
else if (!NT_SUCCESS(status))
{
errorCode = SpcUnknownInfoClassFailure;
goto Exit;
}
//
// Print status of KVA Features
//
charsWritten = swprintf(stateBuffer,
ARRAYSIZE(stateBuffer),
g_KvaStatusString,
GetResetString(),
GetCyanString(),
GetResetString(),
((kvaInfo.KvaShadowFlags.KvaShadowEnabled) ||
((kvaInfo.KvaShadowFlags.KvaShadowRequiredAvailable) &&
!(kvaInfo.KvaShadowFlags.KvaShadowRequired))) ?
GetGreenYesString() : GetRedNoString(),
GetResetString(),
((kvaInfo.KvaShadowFlags.KvaShadowRequiredAvailable) &&
!(kvaInfo.KvaShadowFlags.KvaShadowRequired)) ?
GetGreenYesString() : GetRedNoString(),
GetResetString(),
kvaInfo.KvaShadowFlags.KvaShadowUserGlobal ?
GetRedYesString() : GetGreenNoString(),
GetResetString(),
kvaInfo.KvaShadowFlags.KvaShadowPcid ?
GetGreenYesString() : GetRedNoString(),
GetResetString(),
kvaInfo.KvaShadowFlags.KvaShadowInvpcid ?
GetGreenYesString() : GetRedNoString(),
GetResetString());
WriteConsole(hStdOut, stateBuffer, charsWritten, NULL, NULL);
//
// Print status of L1TF Features
//
charsWritten = swprintf(stateBuffer,
ARRAYSIZE(stateBuffer),
g_L1tfStatusString,
GetResetString(),
GetCyanString(),
GetResetString(),
((kvaInfo.KvaShadowFlags.L1TerminalFaultMitigationPresent) ||
((kvaInfo.KvaShadowFlags.KvaShadowEnabled) &&
(kvaInfo.KvaShadowFlags.InvalidPteBit))) ?
GetGreenYesString() : GetRedNoString(),
GetResetString(),
((kvaInfo.KvaShadowFlags.KvaShadowRequiredAvailable) &&
!(kvaInfo.KvaShadowFlags.KvaShadowRequired)) ?
GetGreenYesString() : GetRedNoString(),
GetResetString(),
kvaInfo.KvaShadowFlags.L1DataCacheFlushSupported ?
GetGreenYesString() : GetRedNoString(),
GetResetString(),
((kvaInfo.KvaShadowFlags.KvaShadowEnabled) &&
(kvaInfo.KvaShadowFlags.InvalidPteBit)) ?
GetGreenYesString() : GetRedNoString(),
GetResetString());
WriteConsole(hStdOut, stateBuffer, charsWritten, NULL, NULL);
//
// Get the Speculation Control Information
//
status = NtQuerySystemInformation(SystemSpeculationControlInformation,
&specInfo,
sizeof(specInfo),
NULL);
if (status == STATUS_INVALID_INFO_CLASS)
{
//
// Print out an error if this failed
//
WriteConsole(hStdOut,
UnpatchedString,
ARRAYSIZE(UnpatchedString) - 1,
NULL,
NULL);
errorCode = SpcFailedToQuerySpeculationControl;
goto Exit;
}
else if (!NT_SUCCESS(status))
{
errorCode = SpcUnknownInfoClassFailure;
goto Exit;
}
//
// Print status of Speculation Control Features
//
charsWritten = swprintf(stateBuffer,
ARRAYSIZE(stateBuffer),
g_SpecControlStatusString,
GetResetString(),
GetCyanString(),
GetResetString(),
specInfo.SpeculationControlFlags.BpbEnabled ?
GetGreenYesString() : GetRedNoString(),
GetResetString(),
specInfo.SpeculationControlFlags.BpbDisabledSystemPolicy ?
GetRedYesString() : GetGreenNoString(),
GetResetString(),
specInfo.SpeculationControlFlags.BpbDisabledNoHardwareSupport ?
GetRedYesString() : GetGreenNoString(),
GetResetString(),
specInfo.SpeculationControlFlags.BpbDisabledKernelToUser ?
GetRedYesString() : GetGreenNoString(),
GetResetString(),
specInfo.SpeculationControlFlags.SpecCtrlRetpolineEnabled ?
GetGreenYesString() : GetRedNoString(),
GetResetString(),
specInfo.SpeculationControlFlags.SpecCtrlImportOptimizationEnabled ?
GetGreenYesString() : GetRedNoString(),
GetResetString(),
specInfo.SpeculationControlFlags.SpecCtrlEnumerated ?
GetGreenYesString() : GetRedNoString(),
GetResetString(),
specInfo.SpeculationControlFlags.IbrsPresent ?
GetGreenYesString() : GetRedNoString(),
GetResetString(),
specInfo.SpeculationControlFlags.StibpPresent ?
GetGreenYesString() : GetRedNoString(),
GetResetString(),
specInfo.SpeculationControlFlags.SpecCmdEnumerated ?
GetGreenYesString() : GetRedNoString(),
GetResetString(),
specInfo.SpeculationControlFlags.SpecCmdEnumerated ?
GetGreenYesString() : GetRedNoString(),
GetResetString());
WriteConsole(hStdOut, stateBuffer, charsWritten, NULL, NULL);
//
// Print status of Speculation Control SSBD Features
//
charsWritten = swprintf(stateBuffer,
ARRAYSIZE(stateBuffer),
g_SpecControlStatusString2,
GetResetString(),
GetCyanString(),
GetResetString(),
specInfo.SpeculationControlFlags.SpeculativeStoreBypassDisableAvailable &&
(specInfo.SpeculationControlFlags.SpeculativeStoreBypassDisableSupported ||
!specInfo.SpeculationControlFlags.SpeculativeStoreBypassDisableRequired) ?
GetGreenYesString() : GetRedNoString(),
GetResetString(),
!specInfo.SpeculationControlFlags.SpeculativeStoreBypassDisableAvailable ?
GetRedYesString() : GetGreenNoString(),
GetResetString(),
!specInfo.SpeculationControlFlags.SpeculativeStoreBypassDisableSupported ?
GetRedYesString() : GetGreenNoString(),
GetResetString(),
specInfo.SpeculationControlFlags.SpeculativeStoreBypassDisabledSystemWide ?
GetGreenYesString() : GetRedNoString(),
GetResetString(),
specInfo.SpeculationControlFlags.SpeculativeStoreBypassDisabledKernel ?
GetGreenYesString() : GetRedNoString(),
GetResetString());
WriteConsole(hStdOut, stateBuffer, charsWritten, NULL, NULL);
//
// This is our happy path
//
errorCode = SpcSuccess;
Exit:
//
// Close output handle if needed
//
if (hStdOut != INVALID_HANDLE_VALUE)
{
CloseHandle(hStdOut);
}
//
// Return the error code back to the caller, for debugging
//
return errorCode;
}