Skip to content
Memory corruption Write-up Advanced

A deep dive into CVE-2023-2163: 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. 9 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 it is executed 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, every program has to be verified before it can be loaded, so that all eBPF security assumptions are met. The verifier that performs this task is complex code, and validating that a program is safe to execute is difficult. Many vulnerabilities have been found in the eBPF verifier, and when one is exploited the result is usually a local privilege escalation, or a container escape in containerised environments. The verifier's code has been audited extensively, but the task becomes harder as new features are added and the verifier grows.

This is the story of CVE-2023-2163, a vulnerability in the eBPF verifier found with a fuzzer, the root-cause analysis, and the fix.

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, and it is available at https://github.com/google/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, published in the Buzzer repository under pkg/strategies/pointer_arithmetic.go. It 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, the team concludes that a write out of bounds likely happened.

The bug Buzzer found lies in the eBPF path pruning logic. The team was able to write an exploit that works both as a container escape and as a local privilege escalation.

Finding it

How path pruning works

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

When the verifier encounters an if condition and does not know for certain which values the registers are going to have, it attempts to simulate the execution of all possible states. In the example below, the modulus operation on r6 in block (1) makes the verifier uncertain about which values the register can take. When it reaches the conditional in block (2), it does not definitively know which branch is taken and must simulate the execution of all possible branches. The possible paths are:

  • [redacted]
  • [redacted]
  • [redacted]
  • [redacted]
From the original on bughunters.google.com

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

A high number of conditional jumps would produce an exponential number of paths, 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, it will not bother to explore it further.

Precise tracking and the flaw

To help the verifier determine more efficiently when to prune a path, the concept of precise tracking was introduced. If a register is involved in a pointer arithmetic operation, passed as a constant to a helper function, and so on, it is marked as precise and the verifier is forced to explore all states that register is involved in.

The example above 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 the epilogue, and therefore marks r6 as precise.
  3. When the verifier attempts to explore other states, it determines 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. It therefore treated the rest of the states as equivalent and pruned them.

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

Exploitation

To summarise the situation before exploitation:

  1. The verifier is in charge of making sure all programs are safe.
  2. To do this, it 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 are 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 (Linux LPE eBPF CVE-2021-3490) and @_manfp (CVE-2020-8835, Linux kernel privilege escalation via improper eBPF program verification). The code is published in the Google security research repository under pocs/linux/cve-2023-2163.

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 thought it was 0. The first instruction in the eBPF exploit program is therefore the following shift, which produces the desired value 1:

Text
r6 >>= 10

Step 2: corrupt a stack pointer with a helper call

With r6 believed to be 0 but in reality holding 1, the exploit can corrupt a pointer in the eBPF stack to achieve arbitrary read/write. This is done through the helper function bpf_skb_load_bytes_relative. The stack layout used is:

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 following eBPF instructions are then 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 writes 9 bytes of memory instead of 8, because r6 is 1. In other words, it corrupts 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. Given the stack layout above, r2 would hold 0xCAFE and r3 would hold 0xBACA.

After corrupting the stack pointer with the right value, however, the exploit can arrive at a situation at runtime where r2 holds 0xBACA and r3 contains a pointer to the eBPF map. This is achieved with 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, 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, the exploit overwrites all of it with the desired pointer. Then it simply executes eBPF instructions to 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 the playground.

Step 4: from map leak to root shell

From this point the exploit does not differ much from Chompie's exploit (a lot of code was borrowed from it). 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 step 1. After this step the exploit has the address of the init_pid_ns struct, which should contain the process credentials.
  3. Walk the radix tree until it finds an entry whose comm field matches the one of the executable exploit name. The PID is not used because it is not a reliable heuristic when 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 running inside a container, the exploit will now observe the host's file system.
  5. Execute system("/bin/bash") and enjoy the 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 a local privilege escalation 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 failed to mark r9 as contributing to the precision of r6. Because of that, when it reached block (2) it considered all subsequent paths equivalent to the already explored path and pruned them, even though r9 influenced the value of the precise register r6. The program could then reach the epilogue with an r6 value the verifier believed was 0 but which was a different number at runtime, and perform pointer arithmetic with it.

Impact

A program exploiting this bug can achieve arbitrary read/write in the kernel, defeat KASLR through a map pointer leak, patch its own process credentials to uid 0, and patch the fs_struct pointer so that, inside a container, it observes the host's file system. The result is a local privilege escalation, or a container escape in containerised environments. The published exploit only works as a container escape for certain versions of Linux, and works as a local privilege escalation for Ubuntu and other distributions because the offsets they use for the overwritten data structures are different.

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 in the kernel mailing list thread. It is unclear whether this has a performance impact on the eBPF verifier. What is clear is that the team continued to run the same pointer arithmetic fuzzing strategy and has not been able to discover further issues.

Variants to test

  • Run the pointer arithmetic strategy against other kernel subsystems that maintain their own verifiers or state-tracking logic. A positive result is a magic value written by the generated program that never appears in user-land, which suggests an out-of-bounds write.
  • Extend the strategy to other helper functions that take a length or offset derived from a register the verifier tracks. A positive result is a helper writing more bytes than the verifier's model allows, corrupting an adjacent stack slot.
  • Target other precision-related paths: registers that influence a precise register indirectly through arithmetic, conditional jumps or helper arguments. A positive result is a state pruned as equivalent that reaches the exit with a different runtime value for the precise register.
  • Adapt the exploit to distributions with different structure offsets. A positive result is a root shell or a view of the host file system from inside a container.

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. 19 min read.

What happened, in plain words

A team of security researchers at Google found a serious bug in the Linux kernel, the core program that sits underneath almost every server, phone and container in the world. The bug lived in a part of the kernel called the eBPF verifier. If you could load a specially crafted program into the kernel, the verifier would wave it through as safe when it was not, and that program could then take full control of the machine: it could read and write any memory in the kernel, give itself the highest level of permission (root), and, when running inside a container, break out and see the host machine's files.

The bug is tracked as CVE-2023-2163. "CVE" is just a catalogue number that the security world gives to a publicly known vulnerability so everyone can refer to the same thing. The team found it with a tool they built called Buzzer, which automatically generates huge numbers of test programs and watches for one that behaves badly. They then worked out exactly why the verifier made the wrong decision, wrote a working exploit to prove the impact, and a fix was made.

This article walks through the whole story from zero. You do not need to know what a kernel is, what a register is, or what a fuzzer is. Each idea is introduced when it first comes up.

Background: what is the Linux kernel and what is eBPF?

The Linux kernel is the deepest layer of software on a Linux machine. It talks to the hardware (disk, network card, memory) and decides which program is allowed to do what. Ordinary programs like your browser cannot touch the hardware directly; they ask the kernel, and the kernel either allows it or refuses.

eBPF is a feature that lets you add small programs to the kernel while it is running, without rebooting and without writing a full kernel module (a module is a chunk of code that plugs directly into the kernel, and a buggy one can crash the whole machine). You write your program in a special simple instruction language called bytecode, hand it to the kernel, and the kernel runs it later when a chosen event happens, for example when a particular system call is made. A system call (syscall) is how a normal program asks the kernel to do something, like open a file or send data over the network.

This is powerful and useful, but it also means you are running code at the highest privilege level in the machine. If an attacker can sneak a malicious eBPF program past the checks, they get kernel-level power. So before any eBPF program is allowed to run, it must be verified.

The verifier: the bouncer at the door

The verifier is the piece of kernel code that inspects every eBPF program before loading it. Think of it as a bouncer at a club door who reads your ID and checks your pockets before letting you in. It has to be sure the program is safe to run before letting it in. Only then is the program allowed in.

This is genuinely hard. The verifier is complex code, and the more features eBPF gains, the more complex it becomes. Over the years many vulnerabilities have been found in it. When one is exploited, the usual result is a local privilege escalation (LPE): an ordinary user on the machine becomes root. In a container (a lightweight isolated box on a shared machine, like a separate flat in the same building), the result is a container escape: code inside the box gets out and sees the host.

This is the story of CVE-2023-2163: a bug in the verifier, found with a fuzzer, analysed to its root cause, and fixed.

Setting up: building a fuzzer called Buzzer

A fuzzer is a program that automatically throws huge numbers of generated inputs at a target and watches for anything that goes wrong. It is like shaking thousands of keys in a lock to see if one turns, instead of reasoning about the lock by hand.

After reading Simon Scannell's article on eBPF fuzzing, the team at Google decided to build a proper eBPF fuzzer, one able to generate a high volume of syntactically valid programs, meaning programs that are correctly formed and will actually be accepted for checking rather than rejected immediately for a typo. That fuzzer is Buzzer, published at https://github.com/google/buzzer.

Buzzer can be configured to use strategies, which are recipes for how to try to trigger a logical bug. For this research the team wrote a pointer arithmetic strategy, published in the Buzzer repository under pkg/strategies/pointer_arithmetic.go. It works like this:

  1. Generates a header initialising the registers to random values. A register is a tiny slot inside the processor that holds one number at a time; eBPF programs do all their work by moving numbers between registers.
  2. Generates a series of random arithmetic and jmp instructions. jmp means jump: it tells the program to continue at a different place, which is how branches and if conditions are built.
  3. Chooses a random register and performs an addition operation with a pointer to an eBPF map element. A pointer is a number that is really a memory address, like a house number telling you where something lives. An eBPF map is a shared storage area that the eBPF program and the normal user program can both read and write, like a shared noticeboard.
  4. Writes a magic value to that element. A magic value is a distinctive number chosen so it is easy to spot later. If the value written is not observed in user-land (the ordinary, unprivileged side of the machine where normal programs run), the team concludes that a write out of bounds likely happened: the program wrote somewhere it was not supposed to, and the value landed in the wrong place.

The bug Buzzer found lies in the eBPF path pruning logic. The team was able to write an exploit that works both as a container escape and as a local privilege escalation.

Finding it: how path pruning works

One of the main jobs of the verifier is to make sure the programs loaded into the kernel are safe to run. Checking safety is not trivial, especially when the program has branches: places where it can go one way or another depending on a value.

When the verifier encounters an if condition and does not know for certain which values the registers are going to have, it attempts to simulate the execution of all possible states. A state here means the set of possible values that the registers and the stack slots can hold at that point. The stack is a small scratch area of memory each program gets for temporary values; a stack slot is one position in it.

In the example below, the modulus operation on register r6 in block (1) makes the verifier uncertain about which values the register can take. Modulus (written %) is the remainder after division: 7 modulus 4 is 3. When the verifier reaches the conditional in block (2), it does not definitively know which branch is taken and must simulate the execution of all possible branches. The possible paths are:

  • [redacted]
  • [redacted]
  • [redacted]
  • [redacted]
From the original on bughunters.google.com

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

Each path is a route through the numbered blocks. Because the verifier cannot tell which route the program will really take, it walks all four.

A high number of conditional jumps would produce an exponential number of paths: two branches become four, four become eight, and so on. That would badly slow down the act of loading programs into the kernel. For this reason the eBPF developers came up with the path pruning strategy. Pruning means cutting branches off the tree of possibilities. The rule is: if for a given state the verifier can guarantee that it has already demonstrated that an equivalent state can reach an exit instruction safely, it will not bother to explore it further. It is like a maze solver who notices they have already proved a certain junction always leads to the exit, so they do not walk it again.

Precise tracking and the flaw

To help the verifier decide more efficiently when to prune a path, the concept of precise tracking was introduced. If a register is involved in a pointer arithmetic operation, passed as a constant to a helper function, and so on, it is marked as precise, and the verifier is forced to explore all states that register is involved in. A helper function is a ready-made utility the kernel offers to eBPF programs, so the program does not have to implement common tasks itself.

The example above 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 the epilogue (the closing part of the program), and therefore marks r6 as precise.
  3. When the verifier attempts to explore other states, it determines 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. It therefore treated the rest of the states as equivalent and pruned them.

This is exactly the test case Buzzer found. The path the program ended up taking at runtime was [redacted], and in block 6 the program could execute pointer arithmetic operations with an r6 that the verifier assumed was 0 but was in fact a different number. In plain terms: the bouncer checked one visitor's pockets, decided everyone dressed similarly was fine, and waved through someone carrying something dangerous.

Exploitation: turning the bug into control

To summarise the situation before exploitation:

  1. The verifier is in charge of making sure all programs are safe.
  2. To do this, it 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 are 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 (Linux LPE eBPF CVE-2021-3490) and @_manfp (CVE-2020-8835, Linux kernel privilege escalation via improper eBPF program verification). The code is published in the Google security research repository under pocs/linux/cve-2023-2163.

The high level plan is:

  1. Achieve arbitrary read/write: the ability to read and write any memory address in the kernel, which is the master key to everything else.
  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. The uid is the numeric user ID; root is 0. The fs_struct is the kernel structure that records which file system a process sees.

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 thought it was 0. 0x400 is hexadecimal notation for the number 1024. The first instruction in the eBPF exploit program is therefore the following shift, which produces the desired value 1:

Text
r6 >>= 10

>>= means shift the bits of the number to the right by the given amount. Shifting 0x400 right by 10 places gives 1. The team needed exactly 1 because a length of 1 is what makes the next step write one byte too many.

Step 2: corrupt a stack pointer with a helper call

With r6 believed to be 0 but in reality holding 1, the exploit can corrupt a pointer in the eBPF stack to achieve arbitrary read/write. This is done through the helper function bpf_skb_load_bytes_relative, which copies bytes from a network packet into a buffer. The stack layout used is:

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

Offsets are measured from a reference point in the stack; -8 is just below it, -40 further down. The two scalar values 0xCAFE and 0xBACA are just recognisable marker numbers. The following eBPF instructions are then 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 the address of the incoming network packet into register 1, the first argument of the helper.
  • r2 = 0 sets the offset inside the packet to 0, so copying starts at the beginning.
  • r3 = r10 - 40 sets the destination to the buffer at stack offset -40. r10 is the register that points at the current stack frame.
  • r4 = r6 copies the corrupted register into the length argument.
  • r4 += 8 adds 8, so the length becomes 9 instead of the intended 8.
  • r5 = 1 selects the variant of the helper to use.
  • BPF_FUNC_skb_load_bytes_relative(r1, r2, r3, r4, r5) performs the copy with those five arguments.

The call to skb_load_bytes_relative writes 9 bytes of memory instead of 8, because r6 is 1. In other words, it corrupts the first byte of the -32 offset value in the stack. That slot held a pointer to stack offset -8; now its first byte is different, so it points somewhere else.

Step 3: arbitrary read/write and defeating KASLR

KASLR stands for Kernel Address Space Layout Randomisation. Each time the machine boots, the kernel is placed at a random address in memory, so an attacker who does not know that address cannot aim at kernel structures. Defeating KASLR means learning the real address. 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. The * means "read the value stored at this address". Given the stack layout above, r2 would hold 0xCAFE and r3 would hold 0xBACA.

After corrupting the stack pointer with the right value, however, the exploit can arrive at a situation at runtime where r2 holds 0xBACA and r3 contains a pointer to the eBPF map. This is achieved with 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, R3 is a leak to the map pointer.
  5. If R2 != 0xBACA, set byte = (byte + 1) % 256 and go to 2).

Why this works: the exploit does not know which byte value will make the corrupted pointer land exactly where it wants, so it simply tries all 256 possible byte values in turn. Each attempt sends a packet filled with that byte, the eBPF program copies it into the corrupted slot, and the user-land side checks whether the marker 0xBACA appeared where expected. When it does, the other register has handed over a real kernel address. 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, the exploit overwrites all of it with the desired pointer. Then it simply executes eBPF instructions to 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 the playground.

Step 4: from map leak to root shell

From this point the exploit does not differ much from Chompie's exploit (a lot of code was borrowed from it). The rough algorithm is:

  1. Iteratively search for the string init_pid_ns in kstrtab. kstrtab is the kernel's table of symbol name strings, and init_pid_ns is the name of a kernel structure that tracks processes.
  2. Find the symbol in ksymtab that references the string found in step 1. ksymtab is the table that maps those names to addresses. After this step the exploit has the address of the init_pid_ns struct, which should contain the process credentials.
  3. Walk the radix tree until it finds an entry whose comm field matches the one of the executable exploit name. A radix tree is a tree-shaped data structure the kernel uses to look things up quickly; the comm field is the short name a process gives itself. The PID (process ID number) is not used because it is not a reliable heuristic when 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 running inside a container, the exploit will now observe the host's file system. PID 1 is the first process the system starts, and its view of the file system is the host's.
  5. Execute system("/bin/bash") and enjoy the root shell. system runs a command, and /bin/bash is the standard Linux shell; a root shell is a command prompt with full administrator power.

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 a local privilege escalation for Ubuntu and other distributions because the offsets they use for the data structures that were overwritten are different. An offset is the distance in bytes from the start of a structure to a particular field; if the structure is laid out slightly differently, the same number points at the wrong field. Adapting the code so that it works under any Linux distribution is left as an exercise to the reader.

Root cause

The verifier failed to mark r9 as contributing to the precision of r6. Because of that, when it reached block (2) it considered all subsequent paths equivalent to the already explored path and pruned them, even though r9 influenced the value of the precise register r6. The program could then reach the epilogue with an r6 value the verifier believed was 0 but which was a different number at runtime, and perform pointer arithmetic with it.

Impact

A program exploiting this bug can achieve arbitrary read/write in the kernel, defeat KASLR through a map pointer leak, patch its own process credentials to uid 0, and patch the fs_struct pointer so that, inside a container, it observes the host's file system. The result is a local privilege escalation, or a container escape in containerised environments. The published exploit only works as a container escape for certain versions of Linux, and works as a local privilege escalation for Ubuntu and other distributions because the offsets they use for the overwritten data structures are different.

Fix

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

Variants to test

  • Run the pointer arithmetic strategy against other kernel subsystems that maintain their own verifiers or state-tracking logic. A positive result is a magic value written by the generated program that never appears in user-land, which suggests an out-of-bounds write.
  • Extend the strategy to other helper functions that take a length or offset derived from a register the verifier tracks. A positive result is a helper writing more bytes than the verifier's model allows, corrupting an adjacent stack slot.
  • Target other precision-related paths: registers that influence a precise register indirectly through arithmetic, conditional jumps or helper arguments. A positive result is a state pruned as equivalent that reaches the exit with a different runtime value for the precise register.
  • Adapt the exploit to distributions with different structure offsets. A positive result is a root shell or a view of the host file system from inside a container.

Glossary

  • Arbitrary read/write: the ability to read and write any memory address in the kernel, not just the ones a program is supposed to touch.
  • Bytecode: a simple instruction language that a program is written in before being run by another program, here the eBPF program format.
  • Branch: a point in a program where execution can go one way or another depending on a value.
  • Buzzer: the eBPF fuzzer built by Google, available at https://github.com/google/buzzer.
  • comm field: the short name a process gives itself, stored in the kernel.
  • Container: a lightweight isolated box on a shared machine, keeping one workload separate from others.
  • Container escape: getting out of a container and reaching the host machine.
  • CVE: a catalogue number given to a publicly known vulnerability so everyone can refer to the same thing.
  • eBPF: a Linux feature that lets small programs be loaded into the kernel at runtime and run when chosen events happen.
  • eBPF map: a shared storage area that an eBPF program and a normal user program can both read and write.
  • Epilogue: the closing part of a program, where it exits.
  • Exponential: growing by doubling each time, so small additions cause huge increases.
  • fs_struct: the kernel structure that records which file system a process sees.
  • Fuzzer: a program that automatically throws huge numbers of generated inputs at a target and watches for anything that goes wrong.
  • Helper function: a ready-made utility the kernel offers to eBPF programs.
  • Hexadecimal: a way of writing numbers in base 16, where 0x400 means 1024.
  • Heuristic: a rule of thumb that usually works but is not guaranteed.
  • Instruction: one single step a program performs.
  • jmp: a jump instruction that tells the program to continue at a different place.
  • KASLR: Kernel Address Space Layout Randomisation; the kernel is placed at a random address each boot so attackers cannot aim at it.
  • Kernel: the deepest layer of software on a Linux machine, which talks to hardware and enforces permissions.
  • Kernel module: a chunk of code that plugs directly into the kernel; a buggy one can crash the machine.
  • ksymtab: the kernel table that maps symbol names to addresses.
  • kstrtab: the kernel table of symbol name strings.
  • Local privilege escalation (LPE): an ordinary user on a machine gaining the highest level of permission, root.
  • Magic value: a distinctive number chosen so it is easy to spot later.
  • Modulus: the remainder after division, written %.
  • Offset: the distance in bytes from the start of a structure to a particular field.
  • Path pruning: cutting branches off the tree of possibilities the verifier would otherwise explore, when an equivalent state has already been proved safe.
  • Pointer: a number that is really a memory address, telling you where something lives.
  • Pointer arithmetic: adding or subtracting numbers from a pointer to move to another address.
  • Precise tracking: marking a register as precise so the verifier must explore every state that register is involved in.
  • Radix tree: a tree-shaped data structure the kernel uses to look things up quickly.
  • Register: a tiny slot inside the processor that holds one number at a time.
  • Root: the highest level of permission on a Linux machine, user ID 0.
  • Root shell: a command prompt with full administrator power.
  • Scalar: a plain number, as opposed to a pointer.
  • Stack: a small scratch area of memory each program gets for temporary values.
  • Stack slot: one position in the stack.
  • State: the set of possible values that registers and stack slots can hold at a point in a program.
  • Strategy: in Buzzer, a recipe for how to try to trigger a logical bug.
  • Symbol: a named item in the kernel, such as a structure or function.
  • Syscall (system call): how a normal program asks the kernel to do something, like open a file or send data.
  • Syntactically valid: correctly formed, so it is accepted for checking rather than rejected for a typo.
  • uid: the numeric user ID; root is 0.
  • User-land: the ordinary, unprivileged side of the machine where normal programs run.
  • Verifier: the kernel code that inspects every eBPF program before loading it and refuses unsafe ones.
  • Vulnerability: a flaw in software that an attacker can use to make it behave in a way its authors did not intend.

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.