text
stringlengths
0
1.99k
Ideally we would like to just overwrite our own processes' uid to be 0
to become root.
Unfortunately for us, the check in 'ptrace_peek()' is correct. So,
while we can write in the kernel, we can't read anything anywhere in it.
---[ 6.1 - No KASLR
ToaruOS doesn't have KASLR, so we know exactly where in memory the
kernel is ahead of time. But what does that gain us?
We could try to overwrite a global pointer, for example the current
process and point it into our user space memory to a fake data
structure.
This would probably work since ToaruOS doesn't have SMAP.
We could overwrite the address of an interrupt handler or a syscall or
some other function pointer and redirect it so that we can run our own
code in ring 0.
This, too, would probably work since ToaruOS doesn't have SMEP.
But both of these strategies require some extra effort in faking a data
structure or writing C code that works properly in ring 0.
---[ 6.2 - SUIDn't
The exploit strategy I ended up using was a lot simpler to implement.
We can alter kernel memory anywhere, even the .text section and we know
where everything is since there's no KASLR.
Remember the SUID check in 'elf_exec' that I talked about in 5.3?
Because we know the exact version of the kernel, we can simply look at
the kernel image or read /proc/kallsyms in our local instance to find
out at which address in virtual memory that function is.
local@livecd ~$ sudo cat /proc/kallsyms | grep elf_exec
000000000010f300 elf_exec
Disassembling the function with 'objdump' we can find the exact jump
instruction that implements the if statement for SUID binaries.
It is compiled to a 'jne' (jump not equal) conditional
jump instruction that skips past the uid assignment if the binary isn't
SUID.
10f365: 0f 85 48 01 00 00 jne 0x10f4b3
To turn 'jne' into 'je' we just need to flip the 0x85 into a 0x84 byte.
10f365: 0f 84 48 01 00 00 je 0x10f4b3
This negates the check so that now only non-SUID binary will assign
their owners uid.
Afterwards, simply running '/bin/esh' turns you into root and you can
read the flag.
---[ 7 - In Closing
I hope this article can help some curious people get started in the
kernel security space. If nothing else maybe it can give somebody an
appreciation for it.
I also hope it doesn't seem like I am disparaging ToaruOS in any way.
I really like the project. Security is not its main focus and still it
is likely more stable and secure than many other hobby OSes.
Kernel security is very very hard and even harder on defense.
Kernel security has many pitfalls. Both because it is the core of what
protects everything on any OS and also because there are many low level
details that we usually have the luxury of ignoring in user space.
I want to thank the HXP team for the fun CTF they hosted and my friends
who I forced to proof-read for me. In particular Lukas Ratz, who
motivated me to participate in the first place.
---[ 8 - References
[1] https://github.com/klange/toaruos/tree/a54a0cbbee1ac18bceb2371e4987
6eab9abb3a11
[2] https://github.com/klange/toaruos
[3] https://wiki.osdev.org/Paging
---[ A - Exploit Code
#include <stdio.h>
#include <unistd.h>
#include <sys/ptrace.h>