text
stringlengths
0
1.99k
---[ Index
0 - Introduction
1 - The Challenge
1.1 - Environment
2 - ToaruOS
2.1 - Mitigations
3 - Kernel Bugs
4 - Searching for a bug
4.1 - How to open a file
4.2 - Becoming root normally
4.3 - SUID on the kernel side
4.4 - ptrace
4.5 - Poking the first hole
4.6 - Flat mapping excursion
5 - The bug
6 - Write-what-where, but where?
6.1 - No KASLR
6.2 - SUIDn't
7 - In Closing
8 - References
A - Exploit Code
---[ 0 - Introduction
In this article I would like to talk about the process of finding and
exploiting a kernel zero day.
I will use a CTF challenge about finding zero days in a hobby OS kernel
as scaffolding and walk through the layers of protection that the
kernel provides and one of the zero days used to break them.
I think it is a great way to dive into some of the lower level code and
bug classes that can only occur on a kernel level without having to
first understand the internals of a major modern OS kernel and its many
mitigations.
---[ 1 - The Challenge
During the 38C3 conference HXP hosted a CTF that included a kernel
exploitation challenge called "Ser Szwajcarski" (polish for swiss
cheese). Apart from the name the challenge was unusual in two other
respects:
Firstly, it wasn't for any major OS, but instead for a relatively niche
hobby kernel.
Secondly, it targeted the current version (at the time [1]) of the OS.
So really, the challenge was to find a zero day for the OS.
---[ 1.1 - Environment
Before we get into the details, what was the setup of the challenge?
You were provided a low-priv remote shell running on ToaruOS[2] and had
to access the flag in a file that only the root user could access.
They also provided a Dockerfile so that you could set up an identical
local test environment.
---[ 2 - ToaruOS
So, what kind of OS is ToaruOS?
It is a unix-like hobby OS written by Kevin Lange. It is one of the
more advanced hobby OS projects and still actively being developed.
But this isn't a history lesson, so I'll get straight to the parts that
are relevant to us.
---[ 2.1 - Mitigations
Modern operating systems employ a large number of mitigations to make
them more resilient, for safety and for security.
I'll give a brief overview of the major common ones on x86_64 Linux and
then go over how they apply to ToaruOS in 3.1.5.
Basically all of them have analogues for different architectures and
operating systems, but that's way too much to cover.
I am also leaving out several other mitigations that aren't relevant to
the vulnerability or are Linux-specific.
---[ 2.1.1 - CPU rings
On x86 the CPU can run with several distinct privilege levels called
rings. These restrict which actions the CPU is allowed to perform.
For example you can not change the CR3 register, which points to the
page directory, while in ring 3. For this article all you need to know
is that ring 0 is 'kernel mode' and ring 3 is 'user mode'.
This is why system calls exist. A system call is just a CPU in ring 3
causing an interrupt that is handled by the kernel in ring 0.
That code in the kernel then interprets the request and checks if it is