llama.cpp GGUF library GGUF_TYPE_ARRAY/GGUF_TYPE_STRING parsing heap-based buffer overflow vulnerability
A heap-based buffer overflow exists in llama.cpp's GGUF file parsing when handling arrays of strings, due to an integer overflow in the allocation size calculation. An attacker could provide a malicious .gguf file to trigger the overflow and potentially achieve code execution.
Originally disclosed on Cisco Talos
A heap-based buffer overflow vulnerability exists in the GGUF library GGUFTYPEARRAY/GGUFTYPESTRING parsing 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
Likely an integer overflow in the multiplication of the array element count by the size of the string structure, leading to an undersized heap allocation.
What the attacker needed
Victim opens a malicious .gguf file
The fix
The vulnerability was fixed by the vendor, though the exact fix is not described in the text.
How it was found
- Craft a .gguf file with a key-value pair of type GGUF_TYPE_ARRAY and array type GGUF_TYPE_STRING, setting the array element count to a large value that causes integer overflow.
- Open the crafted .gguf file with a vulnerable llama.cpp build, triggering the parsing function.
- The parser allocates a buffer smaller than needed due to the overflow, then writes string pointers beyond the buffer, causing a heap-based buffer overflow.
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.
When a program reads a file, it often calculates how much memory to reserve based on numbers in the file. If those numbers are huge, the calculation can wrap around to a small value, so the program reserves too little memory. Then, when it writes data into that memory, it overflows into other memory, which can crash the program or 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.