GDT and LDT in Windows Kernel Vulnerability Exploitation
Total Page:16
File Type:pdf, Size:1020Kb
GDT and LDT in Windows kernel vulnerability exploitation Matthew \j00ru" Jurczyk and Gynvael Coldwind, Hispasec 16 January 2010 Abstract Device Drivers[3], and How to exploit Windows kernel memory pool[6]. This paper describes some possible ways of exploiting The actual problem with the majority of the tech- kernel-mode write-what-where vulnerabilities in a sta- niques presented in the above publications is the fact ble manner, on Microsoft Windows NT-family systems. that they are mostly based on undocumented, internal The techniques presented in this document are mostly Windows behavior, that is not guaranteed to remain in based on altering processor-specific structures (such as the same form across system updates, service packs or the Global Descriptor Table and Local Descriptor Ta- respective system versions. Therefore, before perform- ble). ing the real exploitation, one is often obliged to check This publication does not cover any revolutionary the system version or employ additional heuristic algo- 0-day bugs, nor does it characterize any new vulnera- rithms (e.g. signature kernel memory scanning). bility class. However, it aims to show some interesting As it turns out, it is possible (and very convenient) methods that could be employed by exploit developers, to take advantage of some hardware-specific structures, in order to gain more stability in comparison to exist- that are undoubtedly implemented across every single ing techniques. Moreover, all hardware-specific infor- Windows version, in the very same way. To be more mation included in this paper is only confirmed to be precise, the structures covered in this paper are the valid on 32-bit x86 platforms. Global and Local Descriptor Tables, one of the most essential parts of protected mode itself. By overwrit- ing these structures in a certain way, one is able to The need of a stable exploit path perform a privilege escalation attack, compatible with every Windows NT system, provided it is running on Nowadays, as kernel-mode vulnerabilities are gaining an x86 processor. more and more attention world-wide (under 5% of Mi- crosoft security bulletins in 2007, over 10% in 2008), so are ring-0 exploitation techniques. One of the most Windows GDT and LDT common vulnerability class observed over the last cou- ple of years is the write-what-where condition, most of- The Global and Local Descriptor Tables are sets of spe- ten a result of improper pointer validation performed cific x86 structures used to define memory segments for by kernel modules (device drivers) or unsafe pool un- both data and code, as well as other system descriptors linking, after triggering a successful buffer overflow. like Task State Segments or Call-Gates. Also, the LDT As the name itself implies, such a vulnerability itself is defined as a single GDT entry. For more details makes it possible for an attacker to elevate the privi- regarding the GDT and LDT, please refer to the Intel leges by performing at least a single WRITE operation Manual, Volume 3A[2]. in some memory regions that are normally protected Despite the fact that Microsoft Windows imple- from being written to by unprivileged applications (us- ments a flat memory model, and that protected mode ing paging and other memory protection mechanisms). enforces the system to set up at least one Task State Before conducting a successful attack, however, one Segment per processor, the GDT has more entries than must firstly know what are the desired \what" and necessary. The table normally consists of a pair of \where" operands. kernel-mode code and data segment entries with De- Many publications have already been released, scriptor Privilege Level set to 0, a pair of user-mode pointing out a number of locations that might be used code and data segment entries (DPL=3), one TSS en- to gain specific benefits (such as escalating the execu- try, as well as three additional data segment entries tion privileges). A list of these papers includes Exploit- and an optional LDT entry. Neither Windows GDT, ing Common Flaws in Drivers[4], Exploiting Windows nor LDT have any Call-Gate entries, by default. Ta- 1 Index Type Base Limit DPL Notes 0 null null segment 1 code 0 FFFF (*4KB) 0 kernel-mode code segment 2 data 0 FFFF (*4KB) 0 kernel-mode data segment 3 code 0 FFFF (*4KB) 3 user-mode code segment 4 data 0 FFFF (*4KB) 3 user-mode data segment 5 tss 80042000 20AB 0 Task State Segment (per-processor) 6 data FFDFF000 1 (*4KB) 0 Windows Processor Control Region (per-processor) 7 data 7FFDF000 FFF 3 Windows Thread Environment Block (TEB) (per-thread) 8 data 400 FFFF 3 used by Windows NTVDM 9 ldt 86811000 7 0 optional custom LDT (per-process) Table 1: A typical Global Descriptor Table on Microsoft Windows ble 1 presents the original GDT table of the Microsoft Creating a Call-Gate entry in Windows XP system. LDT The GDT itself, as well as the TSS and PCR de- scriptor entries it contains, are defined per-processor. The TEB segment is defined per-thread (it is replaced As we already have some basic knowledge regarding during a thread-context switch), and the optional cus- the GDT and LDT structures, it's time to apply real tom LDT is defined per-process. kernel modifications. Our main purpose is to cause the By default, a new process does not have any Local payload to be executed within ring-0 privileges. There- Descriptor Table defined. Instead, it can be allocated fore, one of the most intuitive ways of transitioning the on demand, i.e. when the process requests a new LDT code flow ring3!ring0 using descriptor tables is prob- entry to be created (as described by Z0mbie[7]). A ably to set up a Call-Gate, pointing at our own code. pointer to the per-process LDT table is kept inside the Since this mechanism is designed so that it is possible associated KPROCESS structure, in the LdtDescrip- to make such transitions, the only thing we have to do tor field. On a context switch to a different process, is to make an existing descriptor entry become a Call- this field it loaded into the GDT table, and a LGDT Gate, or build one from scratch. Let's start from the command is issued. beginning, though. The GDT and LDT management functions are scat- As stated in the previous section, Windows pro- tered between the Win32API and the Native API. cesses don't have any default LDT entry specified. The These APIs provide the following functions: kernel is responsible for allocating necessary memory • GetThreadSelectorEntry { query GDT/LDT the first time the process tries to set up its own local entries (Win32API), descriptor. Building the LDT in the context of a local • NtQueryInformationProcess (ProcessLdtIn- process can be achieved by using either the NtSetInfor- formation) { query LDT entries (Native API), mationProcess or NtSetLdtEntries functions (both un- documented). In 2002, Z0mbie implemented and pub- • NtSetLdtEntries { write LDT entries (Native lished some exemple functions for playing with the API), GDT/LDT tables on Windows 2000; a majority of • NtSetInformationProcess (ProcessLdtInfor- these methods haven't changed since then, though. mation) { write LDT entries (Native API), Before trying to modify the LDT contents, one • other should firstly note a few important facts. To be- However, there are several restrictions regarding cus- gin with, there are multiple consequences of the first tom LDT entries, including the base and base+limit NtSetInformationProcess(ProcessLdtInformation) being limited to user-space, the segment being acces- function call. Primarily, the amount of virtual mem- sible in user mode (Descriptor Privilege Level field set ory required to hold a new entry is allocated. Next, a to 3), and the segment type being limited to data and special descriptor of the TSS/LDT type is formed and code segments only. set up in the global table. 2 Figure 1: Data Segment (source) compared to the Call-Gate Descriptor (destination) Because of the fact that the number of Global De- used to retrieve information about a specified selector scriptor Tables is the same as the number of proces- { GetThreadSelectorEntry[1]. sors the system is running on and, on the other hand, Since Windows does not want any user to be able each process may have its own LDT, the system kernel to create every kind of a descriptor inside LDT, the must somehow manage variable GDT entries. In order NtSetInformationProcess/SetLdtEntries functions are to do so, every single process has its own LdtDescrip- limited to defining only Code/Data (both 16- and 32- tor structure inside the KPROCESS object. When a bit) segments with DPL=3. Therefore, there are three task switch occurs, the thread manager replaces the options available for us: current LDT descriptor with the one associated with • Transforming an existing Data descriptor into a the thread to be executed. Moreover, after the GDT is Call-Gate, correctly filled with new data, the IDTR register value • is set to a valid GDT index. Transforming an existing Code descriptor into a After having the LDT created in the context of our Call-Gate, process (the contents are not important yet), we need • Creating a Call-Gate descriptor from the very be- to retrieve the actual table address; two stages are re- ginning (using the free area inside LDT). quired to achieve this: It is confirmed that all of these options work in practice; however, only the first one is going to be thor- 1. Get the IDTR register value (segment selector), oughly described here. 2. Get the GDT descriptor, based on the selector First of all, let's take a look at the two structures we from previous step.