Skip to content
Memory corruption Write-up Advanced

How we found and fixed an eBPF Linux Kernel Vulnerability

bughunters.google.com

Published on bughunters.google.com

The Linux kernel, a dynamic and ever-changing core of the operating system, is constantly being enhanced with new features and capabilities. This continuous evolution inadvertenly expands the potential attack surface, making it increasingly challenging to detect the emergence of critical vulnerabilities.

The author, on bughunters.google.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 10 min read.

Context

eBPF lets you extend the functionality of the Linux kernel at runtime without writing kernel modules or risking a system crash. You load a program in a custom bytecode, the kernel validates it for safety, and then executes it when a certain event happens, for example when a specific syscall is made.

Because this model allows executing arbitrary code at a high privilege level, it significantly increases the kernel's attack surface. Every program therefore has to be verified before it can be loaded, so that all eBPF safety assumptions are met. The verifier that performs this task is complex code, and it has been the source of many vulnerabilities. When one of them is exploited, the result is usually a local privilege escalation, or a container escape in containerised environments. Auditing the verifier is hard, and it gets harder as new eBPF features are added and the verifier grows.

This is the story of CVE-2023-2163, a bug in the eBPF verifier's path pruning logic, found with a fuzzer called Buzzer, for which the team was able to write an exploit usable as both a container escape and an LPE.

Setup

After reading Simon Scannell's article on eBPF fuzzing, the team at Google decided to create a "proper" eBPF fuzzer, one able to generate a high volume of syntactically valid programs. That fuzzer is Buzzer.

Buzzer can be configured to use strategies, which are ways to attempt to trigger logical bugs. For this research the team created a pointer arithmetic strategy, which works like this:

  1. Generates a header initializing the registers to random values

  2. Generates a series of random arithmetic and jmp instructions

  3. Chooses a random register and performs an addition operation with a pointer to an eBPF map element

  4. Writes a magic value to that element

    • If the value written is not observed in user-land, we can conclude that a write out of bounds likely happened

The bug Buzzer discovered lies in the eBPF path pruning logic.

Finding it

One of the main purposes of the eBPF verifier is to make sure that the programs being loaded into the kernel are safe to run. Validating the safety of a program is not trivial, especially when there is branching involved.

When the verifier encounters an if condition and doesn't know for certain which values the registers are going to have, it attempts to simulate the execution of all possible states. Consider the following example:

From the original on bughunters.google.com

Fig. 1. Example of simulating the execution of all possible states

In block (1), the modulus operation on r6 makes the verifier uncertain about which values the register can take. As such, when it makes it to the conditional in block (2), it does not definitively know which branch is taken and must simulate the execution of all possible branches. In this case, the possible paths are:

  • [redacted]
  • [redacted]
  • [redacted]
  • [redacted]

A high number of conditional jumps would result in an exponential number of paths that the execution flow could take at runtime, which would negatively affect the performance of loading programs into the kernel. For this reason, the eBPF developers came up with the path pruning strategy. If, for a given state (the possible values that registers and stack slots can take), the verifier can guarantee that it has already demonstrated that an equivalent state can reach an exit instruction safely, then it will not bother to explore it further.

To help the verifier more efficiently determine when to prune a path, the concept of "precise tracking" was introduced. In a nutshell: if a register is involved in a pointer arithmetic operation, passed as a constant to a helper function, etc. it will be marked as "precise" and the verifier will be forced to explore all states said register is involved in.

Going back to the preceding figure shows precisely how CVE-2023-2163 works:

  1. The verifier begins by exploring the path [redacted] since it takes the false branch first.
  2. At this point, the verifier assumes there is a pointer arithmetic operation and an exit in Epilogue, and therefore will mark r6 as precise.
  3. When the verifier attempts to explore other states, it will determine in block (2) that all subsequent paths are equivalent. This is because it fails to mark r9 as contributing to the precision of r6.

Before the fix, the verifier assumed that r9 did not affect the preciseness of r6, and it had already deduced that it could safely reach the exit in a previous path with r6. As such, it treated the rest of the states as equivalent and pruned them.

This is exactly the test case that Buzzer found. The path that the program ended up taking at runtime was [redacted], and in 6 pointer arithmetic operations could be executed with an r6 that the verifier assumed was 0 but was, in fact, a different number.

Exploitation

To sum up the situation:

  1. The verifier is in charge of making sure all programs are safe
  2. To do this, the verifier sometimes has to explore all possible paths a program could take
  3. Since this can be expensive from a performance standpoint, some paths are assumed "equivalent" to others already explored and thus not explored (pruned)
  4. CVE-2023-2163 is a bug in the verifier where a path is erroneously assumed to be safe due to improper "precise" tracking

The exploit builds on the work of @chompie and @_manfp, both critical in its development. The code is published in the Google security research repository.

The high level plan is:

  1. Achieve arbitrary read/write
  2. Follow the same steps Manfred Paul took to find the process credentials and patch both the uid and the fs_struct pointer to elevate privileges

Step 1: make the corrupted register equal 1

Through manual analysis the team noticed that at runtime, r6 had the value 0x400, while the verifier thinks it is 0. The first instruction in the eBPF exploit program is therefore the following shift, which produces the desired 1 value:

Text
r6 >>= 10

Step 2: corrupt a stack pointer with an oversized helper write

Now that r6 is believed to be 0 but in reality holds 1, a pointer in the eBPF stack can be corrupted to achieve arbitrary read/write. This is done through the helper function [bpf_skb_load_bytes_relative](https://man7.org/linux/man-pages/man7/bpf-helpers.7.html#:~:text=long%20bpf_skb_load_bytes_relative(const%20void%20*skb%2C%20u32%20offset%2C). Consider the following stack layout:

Stack offset Value
-8 0xCAFE (arbitrary scalar value 1)
-16 0xBACA (arbitrary scalar value 2)
-24 PTR to eBPF Map
-32 Ptr to Stack offset -8
-40 Buffer for skb_load_bytes_relative function

Then the following eBPF instructions are executed:

Text
r1 = ptr_to_packet

r2 = 0

r3 = r10 - 40

r4 = r6

r4 += 8

r5 = 1

BPF_FUNC_skb_load_bytes_relative(r1, r2, r3, r4, r5)

The call to skb_load_bytes_relative will write 9 bytes of memory instead of 8, due to r6 being 1. In other words, it will corrupt the first byte of the -32 offset value in the stack.

Step 3: arbitrary read/write and defeating KASLR

Under normal circumstances, after executing the following snippet:

Text
r1 = *(r10 - 32)
r2 = *(r1 - 0)
r3 = *(r1 - 8)

the verifier would expect that r1 holds a pointer to the stack region at offset -8. Therefore, given the stack layout above, r2 would hold the value 0xCAFE and r3 would hold the value 0xBACA.

However, after corrupting the stack pointer with the right value, a situation can be reached at runtime where r2 holds value 0xBACA, and r2 contains a pointer to the eBPF map. This is achieved by the following algorithm:

  1. Set byte = 0
  2. Send a network packet with data[9] = {byte, byte, byte, byte, byte, byte, byte, byte, byte}
  3. In eBPF: Store the values of R2 and R3
  4. In user--land: read the values of R2 and R3, if R2 == 0xBACA, we know that R3 is a leak to the map pointer
  5. If R2 != 0xBACA, set byte = (byte + 1) % 256 and go to 2)

Once the value of the eBPF map pointer is leaked, KASLR is defeated.

Arbitrary read/write follows the same strategy, except that instead of overwriting a single byte of the contiguous stack region, all of it is overwritten with the desired pointer. Then eBPF instructions can simply read/write from that corrupted pointer: in the eyes of the verifier, the program is operating in the BPF stack, whereas in reality the kernel is its playground.

Step 4: from map leak to root shell

From here, the exploit doesn't differ much from Chompie's exploit (a lot of code was borrowed from hers). The rough algorithm is:

  1. Iteratively search for the string "init_pid_ns" in kstrtab

  2. Find the symbol in ksymtab that references the string found in 1, after this step we have the address of the init_pid_ns struct which should contain the process credentials

  3. Walk the radix tree until we find an entry whose comm field matches the one of our executable exploit name

    • Why not simply use the PID? Because that is not a reliable heuristic if we are running inside a container
  4. Patch the uid to 0 and the fs_struct pointer; patching the latter with the same pointer referenced by PID 1 guarantees that if we are running inside a container, the exploit will now observe the host's file system

  5. Execute system ("/bin/bash") and enjoy our root shell!

Here is a screenshot of the exploit running as a container escape:

From the original on bughunters.google.com

Fig. 2. Exploit running as a container escape

The published code only works as a container escape for certain versions of Linux. It will only work as an LPE for Ubuntu and other distributions because the offsets they use for the data structures that were overwritten are different. Adapting the code so that it works under any Linux distribution is left as an exercise to the reader.

Root cause

The verifier prunes a path when it believes an equivalent state has already been shown to reach an exit safely. Precision tracking is what makes that judgement sound: a register involved in pointer arithmetic or passed as a constant to a helper is marked precise, forcing the verifier to explore every state it appears in. The bug was that a register contributing to the precision of a precise register, r9 in the example, was not itself marked as contributing to that precision. The verifier therefore treated later paths as equivalent to an already explored one and pruned them, even though at runtime the pruned path was the one taken, with an r6 the verifier believed to be 0.

Impact

When a verifier vulnerability like this is exploited, the result is usually a local privilege escalation, or a container escape in containerised environments. The exploit written for CVE-2023-2163 achieves arbitrary read/write in the kernel, defeats KASLR through the map pointer leak, patches the process credentials and the fs_struct pointer, and yields a root shell, though the published code only works as an LPE for Ubuntu and other distributions with matching offsets. As a container escape it works only for certain versions of Linux; as an LPE it works for Ubuntu and other distributions whose data structure offsets match those the exploit overwrites.

Fix

The fix consisted of marking imprecise registers in an operation that influenced a precise register as precise. More details on the root-cause analysis and the patch can be found here. It is unclear whether this has a performance impact on the eBPF verifier. What is clear is that the same pointer arithmetic fuzzing strategy has continued to run and has not been able to discover further issues.

Variants to test

  • Run the pointer arithmetic strategy against other verifier features that interact with precision tracking, such as helper calls that take register values as constants. A positive result is a magic value written to a map element that never appears in user-land, indicating an out-of-bounds write.
  • Target the pruning logic with programs that mix conditional jumps and modulus operations on registers, as in the example above, and check whether states the verifier calls equivalent actually diverge at runtime.
  • Exercise the bpf_skb_load_bytes_relative path with a length derived from a register the verifier believes is zero. A positive result is corruption of an adjacent stack slot, observable as a changed pointer value in a subsequent load.
  • Repeat the byte-by-byte brute force used to leak the map pointer against other kernel pointers reachable from a corrupted stack slot. A positive result is a stable leak that defeats KASLR.
  • Adapt the credential patching to other distributions by recomputing the structure offsets, and verify whether the container escape works on kernel versions outside those the published code targets.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 21 min read.

What happened, in plain words

A team at Google found a security bug in the part of the Linux kernel that checks programs before they are allowed to run. The bug is tracked as CVE-2023-2163. "CVE" is just a public catalogue number for a known vulnerability, so people can look it up and talk about it unambiguously. The bug was found by a tool called Buzzer, which generates huge numbers of test programs automatically, and the team then wrote a working exploit: a piece of code that abuses the bug to take control of a machine. The exploit can be used in two ways. As a local privilege escalation (LPE), it lets an ordinary user on a machine become the administrator, called root, at least on Ubuntu and other distributions whose data structure offsets match. As a container escape, it lets a program that is supposed to be locked inside a container break out and see the rest of the machine.

This article walks through the whole story in the same order as the technical write-up: what eBPF is, how the fuzzer works, how the bug happens, how the exploit is built step by step, what the fix was, and what the team suggests trying next. Every technical term is explained where it first appears, and every code snippet is followed by a plain-words explanation of each line.

Background: what eBPF is and why it is risky

eBPF is a feature of the Linux kernel, the core of the operating system. It lets you add new behaviour to the kernel while the system is running, without writing a kernel module. A kernel module is a piece of code loaded directly into the kernel; writing one is complicated and a mistake in it can crash the whole machine. eBPF avoids that by using a different route.

You write a small program in a custom bytecode. Bytecode is a low-level instruction format, a list of simple operations for a machine to perform. The kernel first validates the program for safety, and only then runs it when a certain event happens, for example when a specific syscall is made. A syscall is the way a normal program asks the kernel to do something on its behalf, such as reading a file or sending data over the network.

This model is powerful, and that is exactly the problem. It allows executing arbitrary code at a high privilege level, meaning code that runs with the kernel's own authority. That significantly increases the kernel's attack surface: the set of places where an attacker could try to get in. Because of this risk, every eBPF program has to be verified before it can be loaded, so that all eBPF safety assumptions are met. The component that does this checking is called the verifier.

The verifier is complex code, and it has been the source of many vulnerabilities. When one of them is exploited, the result is usually a local privilege escalation, or a container escape in containerised environments. A container is a lightweight isolated box on a machine, used to run one application without letting it touch the rest of the system; a container escape is when code inside the box gets out. Auditing the verifier is hard, and it gets harder as new eBPF features are added and the verifier grows.

This is the story of CVE-2023-2163, a bug in the eBPF verifier's path pruning logic, found with a fuzzer called Buzzer, for which the team was able to write an exploit usable as both a container escape and an LPE.

Setting up the fuzzer

A fuzzer is a program that throws large amounts of automatically generated input at a target and watches for something going wrong. After reading Simon Scannell's article on eBPF fuzzing, the team at Google decided to create a "proper" eBPF fuzzer, one able to generate a high volume of syntactically valid programs. "Syntactically valid" means the programs are well formed: they follow the rules of the bytecode language, so the verifier actually gets to analyse them instead of rejecting them immediately for a typo. That fuzzer is Buzzer.

Buzzer can be configured to use strategies, which are ways to attempt to trigger logical bugs. A logical bug is a flaw in the reasoning of the code rather than a simple crash: the code does something that is allowed by its own rules but wrong in reality. For this research the team created a pointer arithmetic strategy, which works like this:

  1. Generates a header initializing the registers to random values

  2. Generates a series of random arithmetic and jmp instructions

  3. Chooses a random register and performs an addition operation with a pointer to an eBPF map element

  4. Writes a magic value to that element

    • If the value written is not observed in user-land, we can conclude that a write out of bounds likely happened

A register is a small storage slot inside the processor, used to hold values the program is working with right now. A pointer is a value that says where something lives in memory, like a street address rather than the house itself. Pointer arithmetic means doing sums on such an address, for example adding a number to move to a nearby location. An eBPF map is a storage area that an eBPF program and a normal program can both access, so data can be passed between them. User-land means the ordinary programs you run, as opposed to the kernel. A magic value is a distinctive number chosen so it is easy to recognise later. A write out of bounds means writing to a memory location the program was not supposed to touch, which is how memory corruption bugs usually start.

The idea behind the strategy is simple: write a recognisable number into a map element through a pointer, then look from user-land to see whether it landed where expected. If the value never shows up, the write probably went somewhere else, out of bounds.

The bug Buzzer discovered lies in the eBPF path pruning logic.

How the verifier explores a program

One of the main purposes of the eBPF verifier is to make sure that the programs being loaded into the kernel are safe to run. Validating the safety of a program is not trivial, especially when there is branching involved. Branching means the program can go one way or another depending on a condition, like a fork in a road.

When the verifier encounters an if condition and doesn't know for certain which values the registers are going to have, it attempts to simulate the execution of all possible states. Consider the following example:

From the original on bughunters.google.com

Fig. 1. Example of simulating the execution of all possible states

In block (1), the modulus operation on r6 makes the verifier uncertain about which values the register can take. A modulus operation gives the remainder after a division, so the result depends on the exact starting value, and if the verifier does not know that value it cannot know the result either. As such, when it makes it to the conditional in block (2), it does not definitively know which branch is taken and must simulate the execution of all possible branches. In this case, the possible paths are:

  • [redacted]
  • [redacted]
  • [redacted]
  • [redacted]

Each path is a sequence of blocks the program could follow. A high number of conditional jumps would result in an exponential number of paths that the execution flow could take at runtime, which would negatively affect the performance of loading programs into the kernel. Exponential here means the count doubles with every extra branch, so it quickly becomes unmanageable.

For this reason, the eBPF developers came up with the path pruning strategy. Pruning means cutting branches off the tree of possibilities. If, for a given state (the possible values that registers and stack slots can take), the verifier can guarantee that it has already demonstrated that an equivalent state can reach an exit instruction safely, then it will not bother to explore it further. A stack is a region of memory used for short-lived working values; a stack slot is one position in it. An exit instruction is where the program finishes.

To help the verifier more efficiently determine when to prune a path, the concept of "precise tracking" was introduced. In a nutshell: if a register is involved in a pointer arithmetic operation, passed as a constant to a helper function, etc. it will be marked as "precise" and the verifier will be forced to explore all states said register is involved in. A helper function is a ready-made routine the kernel offers to eBPF programs, so the program does not have to implement common tasks itself.

The bug itself

Going back to the preceding figure shows precisely how CVE-2023-2163 works:

  1. The verifier begins by exploring the path [redacted] since it takes the false branch first.
  2. At this point, the verifier assumes there is a pointer arithmetic operation and an exit in Epilogue, and therefore will mark r6 as precise.
  3. When the verifier attempts to explore other states, it will determine in block (2) that all subsequent paths are equivalent. This is because it fails to mark r9 as contributing to the precision of r6.

Before the fix, the verifier assumed that r9 did not affect the preciseness of r6, and it had already deduced that it could safely reach the exit in a previous path with r6. As such, it treated the rest of the states as equivalent and pruned them.

In plain words: the verifier checked one route through the program, decided it was safe, and then assumed other routes were close enough to skip. But one of those skipped routes was not actually the same, because a second register, r9, quietly influenced the value of r6 and the verifier did not account for that influence.

This is exactly the test case that Buzzer found. The path that the program ended up taking at runtime was [redacted], and in 6 pointer arithmetic operations could be executed with an r6 that the verifier assumed was 0 but was, in fact, a different number. That gap between what the verifier believes and what actually happens is the vulnerability.

Exploitation: the plan

To sum up the situation:

  1. The verifier is in charge of making sure all programs are safe
  2. To do this, the verifier sometimes has to explore all possible paths a program could take
  3. Since this can be expensive from a performance standpoint, some paths are assumed "equivalent" to others already explored and thus not explored (pruned)
  4. CVE-2023-2163 is a bug in the verifier where a path is erroneously assumed to be safe due to improper "precise" tracking

The exploit builds on the work of @chompie and @_manfp, both critical in its development. The code is published in the Google security research repository.

The high level plan is:

  1. Achieve arbitrary read/write
  2. Follow the same steps Manfred Paul took to find the process credentials and patch both the uid and the fs_struct pointer to elevate privileges

Arbitrary read/write means being able to read and change any memory location in the kernel, not just the ones the program is supposed to touch. Process credentials are the kernel's record of who a process is and what it is allowed to do; the uid is the user identifier, where 0 means root. The fs_struct pointer is the address of the structure that describes which file system the process sees. Patching means overwriting those values with our own.

Step 1: make the corrupted register equal 1

Through manual analysis the team noticed that at runtime, r6 had the value 0x400, while the verifier thinks it is 0. The first instruction in the eBPF exploit program is therefore the following shift, which produces the desired 1 value:

Text
r6 >>= 10

This single line takes the value in register r6 and shifts it right by ten binary places. Shifting right by ten is the same as dividing by 1024, and 0x400 is 1024 in decimal, so the result is 1. The verifier still believes r6 is 0, so it will allow operations that are only safe when the value is 0, while the real value is 1. Starting from 1 rather than 0 is what makes the next step possible.

Step 2: corrupt a stack pointer with an oversized helper write

Now that r6 is believed to be 0 but in reality holds 1, a pointer in the eBPF stack can be corrupted to achieve arbitrary read/write. This is done through the helper function [bpf_skb_load_bytes_relative](https://man7.org/linux/man-pages/man7/bpf-helpers.7.html#:~:text=long%20bpf_skb_load_bytes_relative(const%20void%20*skb%2C%20u32%20offset%2C). Consider the following stack layout:

Stack offset Value
-8 0xCAFE (arbitrary scalar value 1)
-16 0xBACA (arbitrary scalar value 2)
-24 PTR to eBPF Map
-32 Ptr to Stack offset -8
-40 Buffer for skb_load_bytes_relative function

The table describes what sits where in the program's stack, counting backwards from a reference point. A scalar value is just a plain number, not an address. The buffer at -40 is the space where the helper will copy incoming data. The pointer at -32 points back to the value at -8, and the pointer at -24 points to the map. The exploit's goal is to change that pointer at -32 without the verifier noticing.

Then the following eBPF instructions are executed:

Text
r1 = ptr_to_packet

r2 = 0

r3 = r10 - 40

r4 = r6

r4 += 8

r5 = 1

BPF_FUNC_skb_load_bytes_relative(r1, r2, r3, r4, r5)

Line by line:

  • r1 = ptr_to_packet puts a pointer to the incoming network packet into r1, which is the source of the data to copy.
  • r2 = 0 sets the offset inside the packet to zero, so copying starts at the beginning.
  • r3 = r10 - 40 computes the destination address: r10 is the frame pointer, the reference point for the current stack, and subtracting 40 gives the buffer at offset -40.
  • r4 = r6 copies r6 into r4. The verifier thinks r6 is 0.
  • r4 += 8 adds 8, so the verifier believes the length is 8.
  • r5 = 1 sets the last argument to 1.
  • BPF_FUNC_skb_load_bytes_relative(r1, r2, r3, r4, r5) calls the helper with those five arguments.

The call to skb_load_bytes_relative will write 9 bytes of memory instead of 8, due to r6 being 1. In other words, it will corrupt the first byte of the -32 offset value in the stack. The verifier approved a write of 8 bytes, which would have stopped exactly at the boundary; the extra byte spills into the neighbouring slot and changes the pointer stored there.

Step 3: arbitrary read/write and defeating KASLR

KASLR stands for Kernel Address Space Layout Randomisation. It is a defence that loads the kernel at a random address each time the machine boots, so an attacker who does not know that address cannot reliably aim at kernel data. Defeating KASLR means finding out the address anyway.

Under normal circumstances, after executing the following snippet:

Text
r1 = *(r10 - 32)
r2 = *(r1 - 0)
r3 = *(r1 - 8)

the verifier would expect that r1 holds a pointer to the stack region at offset -8. Therefore, given the stack layout above, r2 would hold the value 0xCAFE and r3 would hold the value 0xBACA.

Line by line:

  • r1 = *(r10 - 32) loads the pointer stored at stack offset -32 into r1. The asterisk means "the value at this address".
  • r2 = *(r1 - 0) reads the value the pointer points at, which in the intended layout is the value at -8, namely 0xCAFE.
  • r3 = *(r1 - 8) reads eight bytes before that, which in the intended layout is the value at -16, namely 0xBACA.

However, after corrupting the stack pointer with the right value, a situation can be reached at runtime where r2 holds value 0xBACA, and r2 contains a pointer to the eBPF map. This is achieved by the following algorithm:

  1. Set byte = 0
  2. Send a network packet with data[9] = {byte, byte, byte, byte, byte, byte, byte, byte, byte}
  3. In eBPF: Store the values of R2 and R3
  4. In user--land: read the values of R2 and R3, if R2 == 0xBACA, we know that R3 is a leak to the map pointer
  5. If R2 != 0xBACA, set byte = (byte + 1) % 256 and go to 2)

This is a brute-force search over a single byte. The exploit sends a packet whose nine bytes all carry the same trial value, the eBPF program copies it in and records what r2 and r3 ended up being, and user-land checks the result. If r2 is not the expected marker, the trial value is incremented, wrapping around after 255, and the whole thing is tried again. When r2 finally equals 0xBACA, the value in r3 is the address of the map, which is a kernel address. A leak here means an unintended disclosure of a value the attacker should not be able to see.

Once the value of the eBPF map pointer is leaked, KASLR is defeated.

Arbitrary read/write follows the same strategy, except that instead of overwriting a single byte of the contiguous stack region, all of it is overwritten with the desired pointer. Then eBPF instructions can simply read/write from that corrupted pointer: in the eyes of the verifier, the program is operating in the BPF stack, whereas in reality the kernel is its playground. Contiguous means the bytes sit next to each other in memory, so overwriting the whole slot replaces the pointer completely rather than nudging one byte of it.

Step 4: from map leak to root shell

From here, the exploit doesn't differ much from Chompie's exploit (a lot of code was borrowed from hers). The rough algorithm is:

  1. Iteratively search for the string "init_pid_ns" in kstrtab

  2. Find the symbol in ksymtab that references the string found in 1, after this step we have the address of the init_pid_ns struct which should contain the process credentials

  3. Walk the radix tree until we find an entry whose comm field matches the one of our executable exploit name

    • Why not simply use the PID? Because that is not a reliable heuristic if we are running inside a container
  4. Patch the uid to 0 and the fs_struct pointer; patching the latter with the same pointer referenced by PID 1 guarantees that if we are running inside a container, the exploit will now observe the host's file system

  5. Execute system ("/bin/bash") and enjoy our root shell!

Some of these terms need unpacking. kstrtab is a table of symbol names stored as text inside the kernel. ksymtab is a companion table that maps those names to their addresses. A symbol is simply a named thing in the kernel, such as a function or a data structure. A struct is a bundle of related fields kept together in memory. A radix tree is a tree-shaped data structure used to look things up quickly by number; here it is used to find processes by their identifier. The comm field is the short name a process gives itself. A PID is the process identifier, the number the system uses to refer to a running program. PID 1 is the first process started on a machine, the ancestor of everything else. A root shell is a command prompt running with full administrator rights.

Step by step, the exploit first finds a known text label in the kernel, then uses the address table to turn that label into a real address, then walks the process lookup structure to find its own entry by name rather than by number, then rewrites its own identity to root and points its view of the file system at the host's, and finally starts a shell.

Here is a screenshot of the exploit running as a container escape:

From the original on bughunters.google.com

Fig. 2. Exploit running as a container escape

The published code only works as a container escape for certain versions of Linux. It will only work as an LPE for Ubuntu and other distributions because the offsets they use for the data structures that were overwritten are different. An offset is the distance from the start of a structure to a particular field; if the layout changes between versions, the same number points at the wrong place. Adapting the code so that it works under any Linux distribution is left as an exercise to the reader.

Root cause

The verifier prunes a path when it believes an equivalent state has already been shown to reach an exit safely. Precision tracking is what makes that judgement sound: a register involved in pointer arithmetic or passed as a constant to a helper is marked precise, forcing the verifier to explore every state it appears in. The bug was that a register contributing to the precision of a precise register, r9 in the example, was not itself marked as contributing to that precision. The verifier therefore treated later paths as equivalent to an already explored one and pruned them, even though at runtime the pruned path was the one taken, with an r6 the verifier believed to be 0.

Impact

When a verifier vulnerability like this is exploited, the result is usually a local privilege escalation, or a container escape in containerised environments. The exploit written for CVE-2023-2163 achieves arbitrary read/write in the kernel, defeats KASLR through the map pointer leak, patches the process credentials and the fs_struct pointer, and yields a root shell, though the published code only works as an LPE for Ubuntu and other distributions with matching offsets. As a container escape it works only for certain versions of Linux; as an LPE it works for Ubuntu and other distributions whose data structure offsets match those the exploit overwrites.

Fix

The fix consisted of marking imprecise registers in an operation that influenced a precise register as precise. More details on the root-cause analysis and the patch can be found here. It is unclear whether this has a performance impact on the eBPF verifier. What is clear is that the same pointer arithmetic fuzzing strategy has continued to run and has not been able to discover further issues.

Variants to test

  • Run the pointer arithmetic strategy against other verifier features that interact with precision tracking, such as helper calls that take register values as constants. A positive result is a magic value written to a map element that never appears in user-land, indicating an out-of-bounds write.
  • Target the pruning logic with programs that mix conditional jumps and modulus operations on registers, as in the example above, and check whether states the verifier calls equivalent actually diverge at runtime.
  • Exercise the bpf_skb_load_bytes_relative path with a length derived from a register the verifier believes is zero. A positive result is corruption of an adjacent stack slot, observable as a changed pointer value in a subsequent load.
  • Repeat the byte-by-byte brute force used to leak the map pointer against other kernel pointers reachable from a corrupted stack slot. A positive result is a stable leak that defeats KASLR.
  • Adapt the credential patching to other distributions by recomputing the structure offsets, and verify whether the container escape works on kernel versions outside those the published code targets.

Glossary

  • eBPF: a Linux feature that lets small programs be loaded into the kernel at runtime, after the kernel checks them for safety.
  • Kernel: the core part of an operating system that manages memory, processes and hardware.
  • Kernel module: code loaded directly into the kernel, powerful but risky to write.
  • Bytecode: a low-level instruction format that a machine or virtual machine executes.
  • Syscall: the mechanism a normal program uses to ask the kernel to perform an action.
  • Attack surface: all the places where an attacker could try to get in.
  • Verifier: the kernel component that checks an eBPF program is safe before loading it.
  • Local privilege escalation (LPE): turning an ordinary user account into an administrator account on the same machine.
  • Container: a lightweight isolated environment for running an application on a shared machine.
  • Container escape: breaking out of a container to reach the rest of the machine.
  • CVE: a public catalogue number for a known vulnerability.
  • Fuzzer: a tool that generates large amounts of input automatically to find bugs.
  • Syntactically valid: well formed according to the rules of a language, so it is not rejected outright.
  • Strategy: in Buzzer, a configured way of generating programs to try to trigger a particular kind of bug.
  • Logical bug: a flaw in the reasoning of code rather than a simple crash.
  • Register: a small storage slot inside the processor holding a value in use.
  • Pointer: a value that gives the memory address of something.
  • Pointer arithmetic: doing sums on an address to move to a nearby location.
  • Map: a storage area shared between an eBPF program and user-land programs.
  • User-land: ordinary programs, as opposed to the kernel.
  • Magic value: a distinctive number chosen so it is easy to recognise later.
  • Out of bounds: outside the memory region a program is allowed to touch.
  • Branching: a point where a program can go one way or another depending on a condition.
  • Modulus operation: the remainder after a division.
  • Path: one sequence of blocks a program could follow.
  • Exponential: growing by a fixed factor with each step, quickly becoming unmanageable.
  • Path pruning: skipping branches the verifier believes it has already proven safe.
  • State: the possible values registers and stack slots can hold at a point in the program.
  • Stack: a memory region used for short-lived working values.
  • Stack slot: one position in the stack.
  • Exit instruction: the instruction where a program finishes.
  • Precise tracking: marking registers whose exact values matter, forcing the verifier to explore every state they appear in.
  • Helper function: a ready-made routine the kernel offers to eBPF programs.
  • Arbitrary read/write: the ability to read and change any memory location, not just permitted ones.
  • Process credentials: the kernel's record of a process's identity and permissions.
  • uid: the user identifier; 0 means root.
  • fs_struct: the kernel structure describing which file system a process sees.
  • Patching: overwriting a value in memory with one of your own.
  • KASLR: Kernel Address Space Layout Randomisation, a defence that loads the kernel at a random address each boot.
  • Leak: an unintended disclosure of a value the attacker should not see.
  • Contiguous: stored next to each other in memory without gaps.
  • kstrtab: a kernel table holding symbol names as text.
  • ksymtab: a kernel table mapping symbol names to their addresses.
  • Symbol: a named thing in the kernel, such as a function or data structure.
  • Struct: a bundle of related fields kept together in memory.
  • Radix tree: a tree-shaped data structure for fast lookup by number.
  • comm field: the short name a process gives itself.
  • PID: the number the system uses to refer to a running process.
  • PID 1: the first process started on a machine, ancestor of all others.
  • Root shell: a command prompt running with full administrator rights.
  • Offset: the distance from the start of a structure to a particular field.
  • Brute force: trying every possible value until one works.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.