Anatomy of a kernel attack
Overflow
A vulnerability in an operating system kernel is a security nightmare. This article analyzes some well known kernel security problems, explains how they are exploited, and gives real-life examples of attacks that used these time-honored techniques.
Some security issues remind me of Groundhog Day – they just keep coming back. One example of a problem that won't go away is buffer overflow, which was first described in 1972 [1], got a fair bit of attention in 1996 in the oft-quoted Phrack e-zine article "Smashing the Stack for Fun and Profit" [2], and is still one of the most prevalent programming mistakes that can lead to a code injection. And it isn't due to the lack of media coverage that integer overflows are still around – long after they caused the Ariane 5 rocket explosion [3] and the massive Stagefright security issue in Android. The fact is that many of the same few problems continue to turn up, and most could have been prevented by code analysis [4] and defensive programming.
This article takes a close look at some of the techniques attackers use to crack the Linux kernel.
Stein and Shot
You can perform a simple test at home for a graphic example of a buffer overflow: Grab a shot glass and a comfortably sized Munich beer stein, fill the stein to the top, then pour all of its contents into the shot glass. Take note of the overflow. To prevent this situation, the bartender must compare the source's size to the size of the destination buffer.
A buffer overflow simply sends too much data into a too small of a buffer. In the case of a data buffer, the overflow does not just pour out onto the floor but actually overwrites the adjacent data structures. If the buffer is on the stack, the overflow overwrites other data on the stack – which might be other variables, constants, and data used to control an instruction pointer, such as the return address. The return address is used by the final ret
command in any subroutine, so that the CPU can find the next instruction to execute. A change to this address can alter the control flow of the process.
In many buffer overflow attacks, the instruction pointer is set to a memory region the attacker controls and is thus able to fill with the code he wants to execute; however, an attacker can also set the instruction pointer to a library function, such as a call to libc, and provide this function with a properly crafted stack in order to make the library execute the instructions for the attack. The case of sending the attack to libc is called a "return-to-libc" attack, and it is used as a workaround on CPUs supporting non-executable flags for memory regions.
The non-executable flag was introduced because the memory region attackers control is usually the stack, which means that the data sent to overflow the buffer could also provide the code to be executed. If the stack is marked as a "data" region (i.e., non-executable), the code on the stack would fail. However, calling a system-provided function, i.e. in libc, avoids this problem – this workaround is known as "return-to-libc".
Setting the Stage
Listing 1 has a simple example of a vulnerable C program. To test it, compile it with
gcc -o buf -mpreferred-stack-boundary=2 buf.c
Listing 1
Buffer Overflow Example
01 #include "stdio.h" 02 void read_and_print (void) 03 { 04 char text[160]; 05 gets(text); 06 printf("Your input:\n%s\n", text); 07 } 08 09 int main () 10 { 11 read_and_print(); 12 return 0; 13 }
which, even on a 64-bit system, configures data on the stack to be aligned at two bytes.
A quick test run with the correct size of the input string won't show anything suspicious; however, entering more than 160 characters will mostly likely result in a segmentation fault. If typing 160+ characters isn't your favorite way to pass the time, Perl might offer a helping hand:
perl -e 'print "A"x162' | ./buf
A side effect of this rather uniform input is that it is easy to spot when looking at the stack with a debugger such as gdb
. The setup is shown in Listing 2.
Listing 2
Setting Up for gbd
01 (gdb) list 02 4 { 03 5 char text[160]; 04 6 05 7 gets(text); 06 8 printf("Your input:\n%s\n", text); 07 9 } 08 10 09 11 int main () 10 12 { 11 13 read_and_print(); 12 (gdb) break 8 13 Breakpoint 1 at 0x804839b: file buf.c, line 8. 14 (gdb) display/200b $esp
Once the program runs, with
run < <(perl -e 'print "A"x162')
in gdb
, it will break in line 8 of the C code (Listing 2, line 12) and display the stack contents. The 162 "A"
s (0x41
) are obvious (Figure 1). Next to the "A"
s, the return address is stored – and easily discovered once main()
is disassembled (Figure 2). The return address is stored according to the architecture endianess, hence the bytes seem to be swapped to 0xbb 0x83 0x04 0x08
.
Go Back
A simple first test would just set the return address to main+3
and rerun the function again. Note that a string in C is null terminated, and I have a stack alignment of two bytes (Figure 2); thus, to attack, I now need to send 164 "A"
s plus the new return address:
run < <(perl -e 'print "A"x164 ."\xb6\x83\x04\x08"')
You will notice that the breakpoint is triggered twice, which should not happen under regular circumstances. The process then crashes because the stack is corrupted.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.