Linux Kernel Exploitation
Linux Kernel Exploitation
Privilege Escalation
Pietro Borrello
Who am I
TRX
Our Journey
1. Setting up the environment
see:
• https://github.com/hugsy/gef
• https://github.com/martinradev/gdb-pt-dump
First Steps in
Kernel Memory
Corruption
1 2 3
Kernel Attack Common Bugs Arbitrary Code
Surface Execution: is it
necessary?
Linux Kernel Attack Surface
OEM
Customizations Device
Loadable Drivers
Kernel
Modules
syscall
interface
Coprocessors
Common bugs
The kernel has to deal with pointers from userspace that are untrusted
What if ptr or ptr->data points to kernel space?
Direct userspace pointer usage
The kernel has to deal with pointers from userspace that are untrusted
What if ptr or ptr->data points to kernel space?
-> Add check to verify
Double Fetches
ring0
FREE
root
user
Gaining Root
Privileges
1 2 3
The ACE way The AAW way The 1337 way
ACE: Arbitrary Code Execution
1337: 1337
The ACE way - ret2usr
Now we are root! But how to safely return to userspace to spawn a shell?
The ACE way - ret2usr
We saw how controlling a code pointer may just allow us to jump back to
userspace, and execute arbitrary code at ring0
Supervisor Mode Execution Protection:
• prevent executing from userland pages when in kernel mode
• controlled by 20th bit of cr4
jmp
ring0 ring3
kernel userspace
Prevent hijacking - SMEP
jmp
ring0 ring3
kernel userspace
Prevent hijacking - SMEP
jmp
ring0 ring3
kernel userspace
Prevent hijacking - SMEP
prepare_kernel_cred(0);
commit_creds();
swapgs; ret;
iret;
Prevent hijacking - SMAP
We saw how controlling a pointer may allow us to ROP from userspace, and
execute arbitrary code at ring0
Supervisor Mode Access Prevention:
• prevent accessing data from userland pages when in kernel mode
• controlled by 21st bit of cr4 (pinned bit)
access
ring0 ring3
kernel userspace
Prevent hijacking - SMAP
access
ring0 ring3
kernel userspace
Prevent hijacking - SMAP
However…
Certain regions of the kernel cannot be randomized.
• initial _text region
• KPTI trampoline
• kernel symbol table ksymtab
FG-KASLR
Wait what? ksymtab
It is needed to export symbols so that they could be used by kernel modules
FG-KASLR
Wait what? ksymtab
It is needed to export symbols so that they could be used by kernel modules
FG-KASLR
Wait what? ksymtab
It is needed to export symbols so that they could be used by kernel modules
Bypass:
1. Leak _text image base address using an AAR
2. Compute the address of _ ksymtab_<func> from _ text base
3. Leak the value_offset entry from _ ksymtab_<func>
Structure Layout Randomization
Usually fields in a C structure are laid out by the compiler in order of their
declaration.
field1
field2
field3
field4
Structure Layout Randomization
Usually fields in a C structure are laid out by the compiler in order of their
declaration.
Randomly rearrange fields at compilation time, using a random seed.
field4
field1
field3
field2
Structure Layout Randomization
task_struct may have their layout randomized. How can we overwrite creds?
Structure Layout Randomization
task_struct may have their layout randomized. How can we overwrite creds?
-> need to reverse engineer the vmlinux binary to recover the field offsets
Kernel Hardening
Build the kernel with different security options to harden its attack surface
• Attack surface reduction
• Enable security features
Kernel Hardening
Build the kernel with different security options to harden its attack surface
• Attack surface reduction
• INIT_STACK_ALL: initialize all stack variables
• SECURITY_DMESG_RESTRICT: avoid leaks of kernel pointers in dmesg
• PANIC_ON_OOPS: panic on kernel oops
• MODULE_SIG_FORCE: force modules to be signed
• BPF_JIT=n: disable BPF jitter
Kernel Hardening
Build the kernel with different security options to harden its attack surface
• Enable security features
• STACKPROTECTOR_STRONG: improve stack canary coverage
• DEBUG_CREDENTIALS: keep track of pointers to cred struct
• HARDENED_USERCOPY: validate memory regions of user pointers
• SLAB_FREELIST_RANDOM/HARDENED: randomize/fortify allocators
• RANDOMIZE_KSTACK_OFFSET: randomize stack offset at each syscall
Kernel Hardening - USERMODEHELPER
SMAP prevents accessing data from userland pages when in kernel mode
Is Kernel ropping dead then?
access
ring0 ring3
kernel userspace
kROP - SMAP
SMAP prevents accessing data from userland pages when in kernel mode
Is Kernel ropping dead then?
• directly place the chain in kernel land if you have control over some data
• indirectly place the chain in kernel land
access
ring0 ring3
kernel userspace
kROP - SMAP
SMAP prevents accessing data from userland pages when in kernel mode
Is Kernel ropping dead then?
• directly place the chain in kernel land if you have control over some data
• indirectly place the chain in kernel land
T LY?
IR EC
IND
access
ring0 ring3
kernel userspace
kROP - physmap
The kernel has a view of the whole physical memory mapped in physmap
-> This means userspace pages are aliased in kernel memory!
kROP - physmap
The kernel has a view of the whole physical memory mapped in physmap
-> This means userspace pages are aliased in kernel memory!
access
ring0 ring3
kernel userspace
userspace
alias
kROP - physmap
The kernel has a view of the whole physical memory mapped in physmap
-> This means userspace pages are aliased in kernel memory!
• originally the mapping was RWX! access
(now fixed)
ring0 ring3
• SMAP bypass: kernel userspace
1. spray ropchain pages in userspace
2. locate the page in physmap using AAR
3. ROP to physmap userspace
alias
Leveraging useful structures
During kernel exploitation you have a lot of control on the objects that are
allocated as consequence of actions performed in userspace.
Often you have bugs that give you limited capabilities during exploitation
and want to:
• promote an out-of-bound read/write to AAR/W
• promote AAR/W to RIP control
• RIP control to ACE
Let’s look at some useful structures the kernel uses and that we can leverage
Useful structures - tty_struct
For each setxattr syscall the kernel allocates a buffer in heap with data
completely controlled by userspace. Couple with userfaultfd to avoid dealloc
● https://github.com/smallkirby/kernelpwn
● https://github.com/pr0cf5/kernel-exploit-practice
● https://lkmidas.github.io/posts/20210123-linux-kernel-pwn-part-1/
● https://lkmidas.github.io/posts/20210223-linux-kernel-pwn-modprobe/
● https://devilinside.me/blogs/small-steps-kernel-exploitation
● https://duasynt.com/blog/linux-kernel-heap-spray
Resources (2)
● https://ptr-yudai.hatenablog.com/entry/2020/03/16/165628
● https://googleprojectzero.blogspot.com/2020/02/mitigations-are-attack-surface-too.html
● https://blog.lexfo.fr/cve-2017-11176-linux-kernel-exploitation-part1.html
● https://meowmeowxw.gitlab.io/ctf/3k-2021-klibrary/
● https://google.github.io/security-research/pocs/linux/cve-2021-22555/writeup.html
● https://akulpillai.com/posts/learning_through_challenges1/
● https://github.com/R3x/How2Kernel
Resources (3)
● https://pr0cf5.github.io/ctf/2020/03/09/the-plight-of-tty-in-the-linux-kernel.html
● https://www.graplsecurity.com/post/kernel-pwning-with-ebpf-a-love-story