Skip to content
Memory corruption Advisory Advanced CVE-2024-23605

llama.cpp GGUF library header.n_kv heap-based buffer overflow vulnerability

The GGUF model file parser multiplies an attacker-controlled key-value count by the size of a structure without checking for overflow, so a crafted .gguf file can cause a smaller allocation than needed. Writing entries into that undersized heap buffer overflows it, which the report says can lead to code execution.

Francesco Benvenuto Follow

Originally disclosed on Cisco Talos

High 8.8 View original

A heap-based buffer overflow vulnerability exists in the GGUF library header.nkv functionality of llama.cpp Commit 18c2e17. A specially crafted .gguf file can lead to code execution.

Francesco Benvenuto, on Cisco Talos

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 report has not been written yet. The Original tab links to the source.

How it worked

Root cause

The key-value count read from the file is multiplied by the structure size without an overflow check, so the allocation can be smaller than the loop that fills it.

Impact

A malicious model file could cause a heap buffer overflow that the report says can lead to code execution, though exploitation requires the victim to load the file.

What the attacker needed

Victim loads a crafted .gguf file

The fix

The vulnerability was fixed by the vendor, presumably by validating the key-value count before allocating.

How it was found

  1. Open a crafted .gguf file with the vulnerable build.
  2. The parser reads the header, including the key-value count.
  3. It allocates an array sized by multiplying that count by the structure size, which overflows.
  4. It then loops over the count, writing each key and type into the undersized array.
  5. The write past the end of the heap buffer is detected by AddressSanitizer.

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 report has not been written yet. The Original tab links to the source.

A program that loads AI model files reads a number from the file saying how many settings entries to expect, then reserves memory for that many. If the number is huge, the arithmetic used to work out the memory size wraps around and reserves far too little, so writing the entries spills past the end of the reserved block. That kind of spill is called a heap buffer overflow and can let an attacker run their own code.

What is Memory corruption?

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

More by Francesco Benvenuto

View all

152 more disclosed reports, best first.

More from Cisco Talos

View all

The newest disclosed reports for this program.

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.