Skip to content
RCE Write-up Advanced

Heap-overflowing Llama.cpp to RCE

A heap overflow existed in llama.cpp's RPC server because the cpy_tensor backend method copied data using a size derived from attacker-controlled tensor dimensions without comparing the source and destination sizes. An attacker could overflow a heap buffer and overwrite an adjacent buffer structure, potentially redirecting backend function pointers to achieve remote code execution.

retr0.blog

Published on retr0.blog

Llama.cpp is always something I would love to work on, a sort of 'ultimate' goal for my AI/ML research; not only that but finding a stack/heap-overflow RCE in this sophisticated and well-developed ML Project always sounds so cool. (Besides, I am so hungry for a binary-exp in AI Projects to prove my binary-exploitation…

The author, on retr0.blog

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.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the cpy_tensor implementation trusted the source tensor's computed byte size and copied that many bytes into the destination without verifying the destination buffer was large enough.

Impact

An attacker could corrupt heap memory and redirect backend function pointers, potentially achieving remote code execution on the RPC server, though the author notes the exploitation required careful heap grooming and workarounds for allocator errors.

What the attacker needed

Network access to an rpc-server instance

How it was found

  1. Connect to the RPC server and allocate buffers through the alloc_buffer command.
  2. Deserialise a source tensor whose dimensions make ggml_nbytes report a large size.
  3. Use set_tensor to fill the source tensor's memory with the payload.
  4. Trigger cpy_tensor so the oversized copy overflows into the adjacent buffer structure.
  5. Shape the overflow to overwrite the neighbouring buffer's iface function pointers.
  6. Invoke a backend operation so the corrupted iface pointer is called.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

A tensor is a multi-dimensional array of numbers used in machine learning. The RPC server lets one machine ask another to do tensor work, and it copies tensor data between memory buffers. Because the copy length came from numbers the client supplied, a client could make the copy longer than the destination buffer, spilling data into neighbouring memory and corrupting a structure that holds function pointers.

What is RCE?

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.