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.
Originally disclosed on Cisco Talos
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.
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
- Open a crafted .gguf file with the vulnerable build.
- The parser reads the header, including the key-value count.
- It allocates an array sized by multiplying that count by the structure size, which overflows.
- It then loops over the count, writing each key and type into the undersized array.
- 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.