llama-vocab: unbounded id_to_token.resize(n_tokens) from untrusted llama.vocab_size in no_vocab path (783-byte file allocates ~43 GB)

Open Beginner friendly
#32,662 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@xztakin is already working on this.

Since Nov 24, 2026.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
66/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp
Domain
backend

Research direction

Start in src/llama-vocab.cpp around the no-vocab path that reads llama.vocab_size and calls id_to_token.resize(n_tokens). Reproduce with the supplied 783-byte GGUF or inspect how the metadata value is validated before allocation. Done when an excessive dummy-token count is rejected cleanly and the supplied control file still passes the vocabulary stage.

Written by the indexing model from the issue text.

Description

bug-unconfirmed
Name and Version

Loader at commit 2d8d003937b4ad42c1ada09dcdb0236eb3290a8f (b12560); the resize site is byte-identical at current master (checked Nov 20). Reproduced with a minimal loader that calls llama_model_load_from_file directly, the same entry point llama-cli and llama-server use.

Operating systems

Linux

GGML backends

CPU

Hardware

32-core x86-64 host, 62 GB RAM, no GPU. Backend-independent: the allocation happens in llama_vocab load before any backend dispatch.

Models

Not a real model. Fuzzer-generated 783-byte GGUF with zero tensors, tokenizer.ggml.model = none, llama.vocab_size = 1073774080 (u32). Both files are inlined in the description below as base64 with one-command regeneration and sha256s.

Problem description & steps to reproduce

In the no_vocab / none tokenizer path the vocab size is read as a raw u32 from GGUF metadata and passed straight to id_to_token.resize(n_tokens) (src/llama-vocab.cpp:2302-2306 at current master) with no cross-check against the tokens array length, the file size, or any sanity bound:

// read vocab size from metadata
uint32_t n_tokens = 0;
if (ml.get_key(LLM_KV_VOCAB_SIZE, n_tokens, false)) {
    LLAMA_LOG_WARN("%s: adding %u dummy tokens\n", __func__, n_tokens);
    id_to_token.resize(n_tokens);
}

id_to_token is a std::vector<token_data> with sizeof(token_data) == 40, so a file that sets llama.vocab_size = 1073774080 makes the loader attempt malloc(42950963200), about 43 GB, before any tensor is read.

Impact: any process that loads an untrusted GGUF through llama_model_load_from_file (llama-server, llama-cli, anything built on the library) can be killed or pinned by a 783-byte file. Uncapped, RSS climbs to 41.5 GB over about 3 minutes and the kernel then OOM-kills the process (SIGKILL observed); under an 8 GB address-space limit the allocation throws std::bad_alloc and the load fails cleanly. A restart-looping llama-server fed a hostile "model" file is a straightforward denial of service. I read SECURITY.md as treating DoS case-by-case, so I am filing this as an input-validation bug rather than a security report; it looks like the same hardening family as #26690 (validate GGUF array element types) and #26261/#26690-style untrusted-file validation.

Steps to reproduce:

  1. Build any tool that calls llama_model_load_from_file (llama-cli -m works).
  2. Load the attached poc.gguf (783 bytes, sha256 432a9dcda52ae4e443e15b6ef619ff00bdd377036b8d4a982c7508c2157963d2).
  3. Uncapped: watch RSS climb ~3 min, then kernel OOM-kill. Under ulimit -v 8388608: immediate clean std::bad_alloc (log below).

The file is fuzzer output: the two fields that matter are tokenizer.ggml.model = "none" and llama.vocab_size = 1073774080; the other mutated keys are unknown metadata and are ignored by the loader. The attached ctrl-vocab4.gguf differs in exactly 3 bytes (vocab_size = 4) and passes the vocab stage to fail later at normal tensor checks, which isolates the allocation to this one field.

Suggested fix, cheapest first: reject n_tokens above a sanity maximum in the no_vocab path (the value only sizes dummy tokens; there is no legitimate hundreds-of-millions dummy vocab) with a clean load error; or bound it by something derivable from the file; or cross-check against the tokens array element count when one is present, as #26690 does for element types.

Verification: everything above reproduces from the two attached files and the commands shown, at the commit and master states named.

The two files, inline (binary GGUF, 783 bytes each; paste into a file and decode):

printf '%s' 'R0dVRgMAAAAAAAAAAAAAAA8AAAAAAAAAFAAAAAAAAABnZW5lcmFsLmFyY2hpdGVjdHVyZQgAAAAFAAAAAAAAAGxsYW1hDAAAAAAAAABnZW5lcmFsLm5hbWUIAAAADgAAAAAAAABkZWVwc2Vlay1jb2RlchEAAAAAAAAAbGxhbWEuYmxvY2tfY291bnQEAAAAIAAAABQAAAAAAAAAbGxhbWEuY29udGV4dF9sZW5ndGgEAAAAAEAAABYAAAAAAAA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AAAAAAAbGxhbWEuY29udGV4dF9sZW5ndGgEAAAAAEAAABYAAAAAAAAAbGxhbWEuZW1iZWRkaW5nX2xlbmd0aAQAAAAAEAEAGQAAAAAAAABsbGFtYS5mamVlX2ZvcndhcmRfbGVuZ3RoBAAAAAArAAAaAAAAAAAAAGxsYW1hLmF0dGVudGlvbi5oZWFkX2NvdW50BAAAACAAAAAdAAAAAAAAAGxsYW1hLmF0dGVuYW1hLnJvcGUuZnJlcV9iYXNlBgAAAABQw0cmAAAAAAAAAGxsYW1hLmF0dGVudGlvbi5sYXllcl9ub3JtX3Jtc19lcHNpbG9uBgAAAL03fDURAAAAAAAAAGdlbmVyYWwuZmlsZV90eXBlBAAAAAsAAAAQAAAAAAAAAGxsYW1hLnZvY2FiX3NpemUEAAAABAAAABoAAAAAAAAAbGxhbWEqcm9wZS5kaW1lbnNpb25fY291bnQEAAAAgAAAABcAAAAAAAAAbGxhbWEucm9wZS5zY2FsaW5nLnR5cGUIAAAABgAAAAAAAABsaW5lYXIZAAAAAAAAAGxsYW1hLnJvcGUuc2NhbGluZy5mYWN0b3IGAAAAAACAQBQAAAAAAAAAdG9rZW5pemVyLmdnbGwubW9kZWwIAAAABAAAAAAAAABub25lZ3B0NBIAAAAAAAAAdG+WZW5pemVyLmdnbWwucHJlCAAAAA4AAAAAAAAAZGVlcHNlZWstY29kZXIVAAAAAAAAAHRva2VuaXpTdDlleGNlcHRpb25lci5nZ21sLnRva2VucwkAAAAAAAAAAMOvAgAAAAAAAADDsAIAAAAAAAAAbGluZQMAAAAAAAAA

ctrl-vocab4.gguf is poc.gguf with the 4 bytes at offset 465 changed from 00 7e 00 40 to 04 00 00 00; every other byte is identical.

First Bad Commit

#11231 added the dummy-token path for no_vocab (Jan 2025); the resize has been unbounded since introduction, not a regression.

Relevant log output

Uncapped run (62 GB host), production entry llama_model_load_from_file:
load: adding 1073774080 dummy tokens
(RSS climbs for ~3 minutes, then)
kernel: Out of memory: Killed process (SIGKILL)

Same file under ulimit -v 8388608:
load: adding 1073774080 dummy tokens
llama_model_load: error loading model: error loading model vocabulary: std::bad_alloc
llama_model_load_from_file_impl: failed to load model

Control (ctrl-vocab4.gguf, vocab_size = 4, 3 bytes differ):
load_tensors: layer 32 assigned to device CPU, is_swa = 0
create_tensor: loading tensor token_embd.weight
llama_model_load: error loading model: check_tensor_dims: tensor 'token_embd.weight' not found

Dominant language
C++
Stars
136k
Forks
26.1k
Avg merge
1d 19h
Merged PRs (30d)
521

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ggml-org/llama.cpp

All issues in ggml-org/llama.cpp

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.