Kernel page table isolation (KPTI)
Kernel page table isolation is documented as a countermeasure to shared userspace and kernel space attacks like Meltdown.
There is a set of unique pagetables for userspace and a a unique set for kernel space. When switching into kernel mode execution or from kernel mode execution, the page tables currently being used are swapped between kernel space and userspace.
Consequently because this is a protection against the Meltdown form of attack, our buffer overflow example doesn't need to add a lot to it's existing exploit to overcome this mitigation.
Annoyingly, all it does it send a segfault to our process when returning from kernel space into userspace in our kernel_rop exploit.
The ./launch_SMEP_KPTI.sh script launch the example kernel with KPTI enabled and running our existing exploit results in a segfault:

There are two main techniques in over comming this mitigation:
Signal Handler
Since our process is being sent a segfault, we can register a signal handler to handle that segfault and call our drop_shell function.
Our exploit's main function will look something like:
This simple addition should work in most CTF cases and we can run our original exploit again and see it work:

KPTI trampoline
The idea behind this technique is use the kernel's existing method of transitioning between userspace and kernelspace page tables in our exploit to transition gracefully to our drop_shell function.
The function swapgs_restore_regs_and_return_to_usermode is used to move between these two pages and with an appropriate leak we can reuse this function in our rop chain.
The source for this function can be found here:
You can use the whole function, however you would need a lot of dummy registers for the whole POP_REGS macro, which will try and pop every register onto the stack.
Instead since we control the program counter, we usually want to jump into the middle of this function around the first mov instruction to follow the swapgs and iretq instructions.
So instead of registering a signal handler, we simply add a gadget at the end of our rop chain pointing to the kpti trampoline with some dummy values for the extra pop instructions:
Last updated
Was this helpful?