A collection of various techniques to infer the Linux kernel base virtual address as an unprivileged local user, for the purpose of bypassing Kernel Address Space Layout Randomization (KASLR).
Supports x86, x86_64, ARM32, ARM64, MIPS32, MIPS64.
KASLD is written in C and structured for easy re-use. Each file in the ./src
directory uses a different technique to retrieve or infer kernel addresses
and can be compiled individually.
In some instances a compiler which supports the _GNU_SOURCE
macro is required.
./kasld
is a lazy shell script wrapper which simply builds and executes each
of these files, offering a quick and easy method to check for address leaks
on a target system. This script requires make
.
Refer to output.md for example output from various distros.
Leaked addresses may need to be bit masked off appropriately for the target kernel, depending on kernel alignment. Once bitmasked, the address may need to be adjusted based on text offset, although on x86_64 and arm64 (since 2020-04-15) the text offset is zero.
Common default kernel config options are defined in src/kasld.h.
Kernel logs (dmesg
/ syslog
) offer a wealth of information, including
kernel pointers.
Historically, raw kernel pointers were frequently printed to the kernel debug log
without using %pK
.
Bugs which trigger a kernel oops can be used to leak kernel pointers by reading the kernel debug log. There are countless examples. A few simple examples are available in the extra directory.
Modern distros ship with kernel.dmesg_restrict
enabled by default to prevent
unprivileged users from accessing the kernel debug log. grsecurity hardened
kernels also support kernel.grsecurity.dmesg
to prevent unprivileged access.
A single kernel pointer leak can be used to infer the location of the kernel virtual address space and offset of the kernel base address.
Prior to the introduction of Function Granular KASLR (aka "finer grained KASLR") in early 5.x kernels in 2020, the entire kernel code text was mapped with only the base address randomized.
Offsets to useful kernel functions (commit_creds
, prepare_kernel_cred
,
native_write_cr4
, etc) from the base address could be pre-calculated on other
systems with the same kernel - an easy task for publicly available kernels
(ie, distro kernels).
Offsets may also be retrieved from various file system locations (/proc/kallsyms
, vmlinux
, System.map
, etc) depending on file system permissions. jonoberheide/ksymhunter automates this process.
FG KASLR "rearranges your kernel code at load time on a per-function level granularity" and can be enabled with the CONFIG_FG_KASLR flag. Following the introduction of FG KASLR, the location of kernel and module functions are independently randomized and no longer located at a constant offset from the kernel .text
base.
This makes calculating offset to useful functions more difficult and renders kernel pointer leaks significantly less useful.
KASLD serves as a non-exhaustive collection and reference for address leaks useful in KASLR bypass; however, it is far from complete. There are many additional noteworthy techniques not included for various reasons.
The extra/check-hardware-vulnerabilities script performs rudimentary checks for several known hardware vulnerabilities, but does not implement these techniques. Refer to:
Prefetch side-channel attacks:
- xairy/kernel-exploits/prefetch-side-channel
- Prefetch Side-Channel Attacks: Bypassing SMAP and Kernel ASLR
Branch Target Buffer (BTB) based side-channel attacks:
Translation Lookaside Buffer (TLB) side-channel attacks:
- TagBleed: Breaking KASLR on the Isolated Kernel Address Space using Tagged TLBs
- renorobert/tagbleedvmm
RAMBleed side-channel attack (CVE-2019-0174):
Remote kernel pointer leak via IP packet headers:
- From IP ID to Device ID and KASLR Bypass (CVE-2019-10639).
show_floppy kernel function pointer leak (CVE-2018-7273) (requires floppy
driver).
kernel_waitid
leak (CVE-2017-14954) (only affects kernels 4.13-rc1 to 4.13.4):
- wait_for_kaslr_to_be_effective.c.
- https://github.com/salls/kernel-exploits/blob/master/CVE-2017-5123/exploit_no_smap.c
Exploiting uninitialized stack variables:
- Leak kernel pointer by exploiting uninitialized uses in Linux kernel
- Exploiting Uses of Uninitialized Stack Variables in Linux Kernels to Leak Kernel Pointers
- jinb-park/leak-kptr
- compat_get_timex kernel stack pointer leak (CVE-2018-11508).
- sctp_af_inet kernel pointer leak (CVE-2017-7558) (requires
libsctp-dev
). - rtnl_fill_link_ifmap kernel stack pointer leak (CVE-2016-4486).
- snd_timer_user_params kernel stack pointer leak (CVE-2016-4569).
Exploiting an arbitrary read using msg_msg
struct:
- Four Bytes of Power: Exploiting CVE-2021-26708 in the Linux kernel | Alexander Popov
- CVE-2021-22555: Turning \x00\x00 into 10000$ | security-research
- Exploiting CVE-2021-43267 - Haxxin
- Will's Root: pbctf 2021 Nightclub Writeup: More Fun with Linux Kernel Heap Notes!
- Will's Root: corCTF 2021 Fire of Salvation Writeup: Utilizing msg_msg Objects for Arbitrary Read and Arbitrary Write in the Linux Kernel
- [corCTF 2021] Wall Of Perdition: Utilizing msg_msg Objects For Arbitrary Read And Arbitrary Write In The Linux Kernel
- [CVE-2021-42008] Exploiting A 16-Year-Old Vulnerability In The Linux 6pack Driver
Privileged arbitrary read (or write) in kernel space can bypass KASLR:
- https://ryiron.wordpress.com/2013/09/05/kptr_restrict-finding-kernel-symbols-for-shell-code/
- Arbitrary-read vulnerability in the timer subsystem (CVE-2017-18344):
Various areas of DebugFS
(/sys/kernel/debug/*
) may disclose kernel pointers; however,
DebugFS is not readable by unprivileged users by default (since 2012-08-27).
This change pre-dates Linux KASLR by 2 years. DebugFS may still be
readable in some non-default configurations.
KASLD is MIT licensed but borrows heavily from modified third-party code snippets and proof of concept code.
Various code snippets were taken from third-parties and may have different license restrictions. Refer to the reference URLs in the comment headers available in each file for credits and more information.
- grsecurity - KASLR: An Exercise in Cargo Cult Security (grsecurity, 2013)
- Randomize kernel base address on boot [LWN.net]
- Function Granular KASLR (LWN.net)
- An Info-Leak Resistant Kernel Randomization for Virtualized Systems | IEEE Journals & Magazine | IEEE Xplore
- Linux Kernel Driver DataBase
- Micro architecture attacks on KASLR (Anders FoghPosted, 2016)
- 0xAX/linux-insides
- Understanding the Linux Virtual Memory Manager (Mel Gorman, 2004)
- Linux Kernel Programming (2021, Kaiwan N Billimoria)