Double-Fetch
The most simple of vulnerabilities
Last updated
Was this helpful?
The most simple of vulnerabilities
Last updated
Was this helpful?
A double-fetch vulnerability is when data is accessed from userspace multiple times. Because userspace programs will commonly pass parameters in to the kernel as pointers, the data can be modified at any time. If it is modified at the exact right time, an attacker could compromise the execution of the kernel.
Let's start with a convoluted example, where all we want to do is change the id
that the module stores. We are not allowed to set it to 0
, as that is the ID of root
, but all other values are allowed.
The code below will be the contents of the read()
function of a kernel. I've removed , but here are the relevant parts:
The program will:
Check if the ID we are attempting to switch to is 0
If it is, it doesn't allow us, as we attempted to log in as root
Sleep for 1 second (this is just to illustrate the example better, we will remove it later)
Compare the password to p4ssw0rd
If it is, it will set the id
variable to the id
in the creds
structure
Let's say we want to communicate with the module, and we set up a simple C program to do so:
We compile this statically (as there are no shared libraries on our VM):
As expected, the id
variable gets set to 900
- we can check this in dmesg
:
That all works fine.
The flaw here is that creds->id
is dereferenced twice. What does this mean? The kernel module is passed a reference to a Credentials
struct:
This is a pointer, and that is perhaps the most important thing to remember. When we interact with the module, we give it a specific memory address. This memory address holds the Credentials
struct that we define and pass to the module. The kernel does not have a copy - it relies on the user's copy, and goes to userspace memory to use it.
Because this struct is controlled by the user, they have the power to change it whenever they like.
The kernel module uses the id
field of the struct on two separate occasions. Firstly, to check that the ID we wish to swap to is valid (not 0
):
And once more, to set the id
variable:
Again, this might seem fine - but it's not. What is stopping it from changing inbetween these two uses? The answer is simple: nothing. That is what differentiates userspace exploitation from kernel space.
Inbetween the two dereferences creds->id
, there is a timeframe. Here, we have artificially extended it (by sleeping for one second). We have a race codition - the aim is to switch id
in that timeframe. If we do this successfully, we will pass the initial check (as the ID will start off as 900
), but by the time it is copied to id
, it will have become 0
and we have bypassed the security check.
Here's the plan, visually, if it helps:
In the waiting period, we swap out the id
.
With that in mind, the "exploit" is fairly self-explanatory - we start another thread, wait 0.3 seconds, and change id
!
We have to compile it statically, as the VM has no shared libraries.
Now we have to somehow get it into the file system. In order to do that, we need to first extract the .cpio
archive (you may want to do this in another folder):
Now copy exploit
there and make sure it's marked executable. You can then compress the filesystem again:
Use the newly-created initramfs.cpio
to lauch the VM with run.sh
. Executing exploit
, it is successful!