CVE-2008-3531: FreeBSD kernel stack overflow exploit development
About four months ago I developed a reliable exploit for vulnerability CVE-2008-3531, which is also addressed in the advisory FreeBSD-SA-08:08.nmount. In this post I will use this vulnerability to provide an overview of the development process for FreeBSD kernel stack exploits.
CVE-2008-3531 is a kernel stack overflow vulnerability that affects FreeBSD versions 7.0-RELEASE and 7.0-STABLE, but not 7.1-RELEASE nor 7.1-STABLE as the CVE entry seems to suggest.
The bug is in function vfs_filteropt()
at src/sys/kern/vfs_mount.c
:
The first step of the exploit development process involves identifying the vulnerability’s conditions and assessing its impact.
In line 1833 sprintf()
is used to write an error message to a locally
declared static buffer, namely errmsg
declared in line 1804 with a size of
255 bytes. The variable p
used in sprintf()
is a pointer to the mount
option’s name. Conceptually a mount option is a tuple of the form (name,
value). The vulnerable sprintf()
call can be reached from userland when
p
’s (i.e. the mount option’s name) corresponding value is
invalid, but not NULL (due to the checks performed in the first TAILQ_FOREACH
loop). For example, the tuple (“AAAA”, BBBB”)
satisfies this condition; the mount option’s value is the string
“BBBB” which is invalid and not NULL therefore p
would point to
the string “AAAA”. Both the mount option’s name (p
) and the
mount option’s value are user-controlled. This allows the overflow of the
errmsg
buffer by supplying a mount option name of arbitrary length and as we
will see below, less importantly in this case, arbitrary content. Since
errmsg
is on a kernel stack, we can use the overflow to corrupt the current
stack frame’s saved return address with the ultimate goal of diverting
the kernel’s execution flow to code of our own choosing.
Now that we have explored the conditions and concluded that we can indeed
achieve arbitrary code execution we have to explore the ways we can trigger the
vulnerability. There are many possible execution paths to reach
vfs_filteropt()
from userland. After browsing FreeBSD’s file system
stacking source code for a couple of minutes I decided to use the following:
nmount() -> vfs_donmount() -> msdosfs_mount() -> vfs_filteropt()
By default on FreeBSD the
nmount(2)
system call can only be called by root. In order for it to be enabled for
unprivileged users the
sysctl(8)
variable vfs.usermount
must be set to a non-zero value.
At this point we know that the vulnerability can potentially lead to arbitrary
code execution and how to trigger it. The next step is to find a place to store
our arbitrary code and divert the kernel’s execution flow to that memory
address. Due to the structure of the format string used in the sprintf()
call, we do not have direct control of the value that overwrites the saved
return address in vfs_filteropt()
’s kernel stack frame.
However, indirect control is more than enough to achieve arbitrary code
execution. When p
points to a string of 248 ‘A’s followed by NULL
(i.e. 248 * ‘A’ + ‘\0’) the saved return address is
overwritten with the value 0x6e776f
, that is the “nwo” of
“unknown” in the sprintf()
’s format string. Using the
exploitation methodology of kernel NULL pointer dereference vulnerabilities, we
can use
mmap(2)
to map memory at the page boundary 0x6e7000
. Then we can
place our arbitrary kernel shellcode 0x76f
bytes after that. Therefore, when
the corrupted saved return address with the value 0x6e776f
is restored into the
EIP register the kernel will execute our instructions that have been mapped to
this address.
The next step in the exploit development process is to write these instructions. Specifically, our kernel shellcode should:
- locate the credentials of the user that triggers the vulnerability and escalate his privileges,
- ensure kernel continuation. In other words, the system must be kept in a running condition and stable after exploitation.
User credentials specifying the process owner’s privileges in FreeBSD are
stored in a structure of type ucred
defined at src/sys/ucred.h
:
A pointer to the ucred
structure exists in a structure of type proc
defined
at src/sys/proc.h
:
The address of the proc
structure can be dynamically located at runtime from
unprivileged processes in a number of ways:
- The
sysctl(3)
kern.proc.pid
kernel interface and thekinfo_proc
structure. - The
allproc
symbol that the FreeBSD kernel exports by default. - The
curthread
pointer from thepcpu
structure (segment FS in kernel context points to it).
You can find more information about the first alternative in the talk I gave on FreeBSD kernel stack overflows at the University of Piraeus Software Libre Society, Event #16: Computer Security (unfortunately the slides from the talk are only available in Greek currently). The second alternative will be the subject of a future post. In the developed exploit I will use the third alternative.
The other task that our shellcode should perform is to maintain the stability of the system by ensuring the kernel’s continuation. One way to approach this would be to port Silvio Cesare’s “iret” return to userland approach (presented at his “Open source kernel auditing and exploitation” Black Hat talk) to FreeBSD. Although a full investigation of Silvio’s “iret” technique on FreeBSD would be very interesting, it is beyond the scope of this post.
In order to successfully return to userland from the kernel shellcode I will
use another approach. Remember that the execution path I decided to take is
nmount() -> vfs_donmount() -> msdosfs_mount() -> vfs_filteropt()
. After the
shellcode has performed privilege escalation it could return to where
vfs_filteropt()
was supposed to return, that is in msdosfs_mount()
. However
that is not possible since msdosfs_mount()
’s saved registers have been
corrupted when vfs_filteropt()
’s stack frame was smashed by the
overflow. The values of these saved registers cannot be restored, consequently
there is no safe way to return to msdosfs_mount()
after privilege escalation.
The solution I have implemented in the exploit bypasses msdosfs_mount()
completely and returns to the pre-previous from vfs_filteropt()
function,
namely vfs_donmount()
. The saved registers’ values of vfs_donmount()
are uncorrupted in msdosfs_mount()
’s stack frame. To make this more
clear, consider the following pseudocode that is based on the relevant
deadlisting part:
Taking into consideration the above analysis, the complete kernel shellcode for the developed exploit is the following (you can download it from here):
Now we have a way to safely return from kernel to userland and ensure the continuation of the exploited system. The complete exploit is (you can download it from here):
Finally, a sample run of the exploit:
And this concludes my post. I hope you enjoyed reading this as much as I enjoyed writing it.