In this episode: a closer look at x86 memory segmentation and paging
A Foreword
Since publishing the original post I found a few things that weren’t entirely correct, or deserved a better explanation. I compiled and posted them as this post, and cross-referenced them in the original post marked with the word “Update” in bold. No parts of the exploit have been changed since the original release.
Double Fault while trying to execute from the stack
My original attempt to run the bootstrap shellcode from the stack failed to work. Instead, it was generating a Double Fault exception. As we remember, there’s no NX bit feature in the CPU, so what’s blocking our attempts? The answer is actually simple - memory segmentation.
Memory segmentation history
Memory segmentation was Intel’s first approach to managing the address space. The idea is that the memory is split into the segments, and each program has its own set of segments for executable code (CS), data (DS), stack (SS), and up to 3 optional segments for any additional needs (originally only ES, later FS and GS were added). The program addresses its parts via offsets inside the said segments, and the base addresses of the segments can be defined and set by the operating system, making the programs relocatable. On top of that, each segment specifies its size (aka limit). Memory protection has been introduced in the Intel 80286, and since then any attempt to access the segment outside its limit generates a segmentation fault exception.
Intel 80286 has also pioneered the virtual memory - the approach where 4 KB memory pages can be dynamically bound between the logical address space that the programs operate in, and the prysical address space used to access the actual RAM. With such a powerful tool being available, memory segmentation has been decommisioned quickly, leading to the common use of the flat memory model, when all the segments mark all the 32 bits of address space as being available. The last nail to the memory segmentation coffin happened then the x86-64 has been released, making the flat memory model the only available option in the 64 bit mode.
Memory segmentation 101
The segment registers (ES, CS, SS, DS, FS, GS) are 16 bit-wide registers containing the following data:
The values listed there are privilege level (aka protection rings), the bit indicating which descriptor table to address (local or global), and the index of the segment descriptor in the said table. The addresses of both local and global descriptor tables are stored in the ILDT and IGDT registers respectively.
The segment descriptors have the following structure:
The values we’re interested in here are base address and segment limit.
Pinpointing the problem
To figure out what’s going on, let’s inspect the segment descriptor on a running Xbox. By attaching the debugger to a running game, we can first of all check the register values:
The GDTR register holds the address of the global descriptor table. The values in the CS / SS / DS / ES registers are the offsets into the table pointing at the table entries. As we can see, CS points to the entry at the offset 0x8, while DS, SS, and ES point at the same entry at the offset 0x10.
Let’s now dump the memory containing the GDT:
The entry marked with yellow represents CS, and the entry marked with teal represents DS, SS, and ES. To decode their contents, let’s grab the GDT decoder from the pwndbg project, slightly adapt it to our needs, and run it against the values above:
As we can see, DS, SS, and ES span across the whole address space, fitting the flat memory model. The CS in turn starts at 0 and ends at 0xbfffffff, excluding the chuck of address space at the top.
Now comes the interesting part. The DS / SS / ES descriptor states that these segments contain data that can be read (implicitly) and written (explicitly). However, the CS one states that this segment contains code that can be read (explicitly) and executed (implicitly). As we learned in the part 1, the game’s stack is located above 0xd0000000, landing outside the executable memory region. Mystery solved!
As an experiment, I tried changing the limit of the CS to 0xffffffff with the debugger (the modified byte is marked with green):
After doing that, the take 2 shellcode worked perfectly! Yet another case where literally 1 byte was stopping us from running the exploit!
In retrospect, even though the CPU doesn’t have the NX-bit, this method could have been used to limit the executable memory only to the code sections of the kernel and the running executable (game / dashboard / etc). While not being a complete fix of the Xbox security issues, this would have at least harden the system from the buffer overflow attacks.
Address space and memory paging
I originally wrote a misleding paragraph about the Xbox address space in the part 1. Naturally, Xbox kernel uses virtual memory, which can be clearly seen from the following signs:
- The stack is mapped waaaaay above the first 64 MB of address space;
- The bit 31 (PG or “Paging”) and the bit 0 (PE or “Protection Enable”) of the register CR0 are set;
- The Xbox kernel is the modified NT kernel, which requires the memory paging to operate!
32 Bit Memory Paging 101
Xbox kernel uses the 32 bit paging mode, which consists of the following components:
- Page Directory - consists of 1024 Page Directory Entries (or PDEs) indexed by the bits 31-22 of the linear address. Referenced by the address stored in the CR3. A PDE can either define a 4 MB page, or point to a Page Table;
- Page Table - consists of 1024 Page Table Entries (PTEs) indexed by the bits 21-12 of the linear address. A PTE defines a 4 KB page;
- Either a 4 KB or a 4 MB page in the physical memory, addressed by either the bits 21-0 or 11-0 respectively.
A deeper dive into the mechanism of memory paging is out of scope of this post. Instead, let’s take a look at a practical example.
Xbox Kernel Memory Mapping
To get a better idea of how the memory mapping gets set up by the Xbox kernel, I wrote a simple pagetable parser. It takes the following inputs:
- A raw memory dump from a running system, I used Xemu to dump the memory with the game running;
- The value of the CR3 register, containing the physical address of the Page Directory. Xbox seems to always have this register filled with
0xf000, so this address is hardcoded.
A full example output of the parser can be found here. The lines without indentation represent PDEs, and the following indented lines are the PTEs from the Page Table references by the said PDE.
The key takeaways from analyzing it are:
-
The physical address of the PDE (
0xf000) is mapped to the logical address0xc0000000(line 9329). The page table it points at contains the following PTEs:- The first 7 entries (lines 9330-9336) are the PTEs mapping the game data;
- The following 16 entries (lines 9337-9352) are the PTEs mapping the kernel memory;
- The following entry (line 9353) points to the PDE itself;
- The following entry (line 9354) points to the PTE used for mapping the stack;
- The rest of the entries (lines 9355-9417) are the PTEs that perform identity mapping on the top 256 MB of the logical address space minus the very last 4 MB, where the shadow RAM, the memory-mapped hardware registers, and the aliased flash ROM are located.
The PDE page is mapped to the logical address space so the kernel can change the memory mappings after the memory paging has been turned on.
-
The game is loaded into the lowest part of the logical address space (lines 1-5559) and takes around 27 MB of it. In physical address space this part starts from the 764 KB mark;
-
The kernel is loaded into the second half of the address space, starting from
0x80000000(lines 5560-5607); -
The pages on the lines 5619-9328 seem to be unused memory claimed by the kernel, so they can be used later for heap allocation and for growing the stack;
-
The physical memory chunk in the range 32-48 MB is nowhere to be found, likely meaning that it’s internallly switched to be used as VRAM;
-
The very first page of the logical address space isn’t mapped, so any access to the address
0x0would lead to a page fault, allowing the kernel to pass it to the running programm as a null-pointer exception. After all,0x0is also a valid linear address!
Bonus: page tables patching in 007 / MetalArms exploits
Now since we have a better idea about memory paging, we can figure out how the 007 and MetalArms exploit shellcodes disable the memory write protection.
Here’s the part of disassembled MetalArms exploit that disables the protection:
The code does the following:
- It starts reading the memory from the logical address
0xc0200000which translates to the physical address0x03ff0000(line 9337 from the parser output above). This physical address contains the PDE mapping the kernel memory starting from the linear address0x80000000(line 5560); - The code reads 4 bytes representing a PTE at a time, clears the lowest byte, replaces the lowest byte contents with
0x63, and repeats this process 4096 times.
To understand what’s going on in the step 2, let’s take a look at the PTE structure:
Clearing the lowest byte removes all the flags, and setting the lowest byte to the value 0x63 marks the PTE as Present, Writable, Accessed, and Dirty.
The process is repeated for 4096 PTEs, marking the 16 MB of the kernel address space as writable. For some reason, it also marks all these pages as present, although only a small fraction of those are (lines 5561-5607). A bit of a blunt approach, although it works, and by the end of the day, that is what matters.
Wrapping Up
The Original Xbox turned out to be a fantastic system to get familiar with the bare-metal parts of x86! If you read until here - thanks for following my journey, and good luck!