llama.cpp icon indicating copy to clipboard operation
llama.cpp copied to clipboard

Misc. bug: Missing range check on grammar_triggers[].type allows remote SIGABRT in llama-server

Open Yunzez opened this issue 1 month ago • 1 comments

Name and Version

version: 0.7.0-dev (build 50, commit https://github.com/ggml-org/llama.cpp/commit/0ce8cd11416c8840498e6701bcbe39ad4b858c2b) built with GNU 11.5.0 for Linux x86_64

Operating systems

Linux

Which llama.cpp modules do you know to be affected?

llama-server

Command line


Problem description & steps to reproduce

A single unauthenticated POST /completion (or /v1/completions) with a grammar_triggers entry whose type is outside the valid enum range crashes the whole server with SIGABRT in default config.

cause

The trigger type is cast directly from a client integer to the enum with no range check:

tools/server/server-common.h:75-81   server_grammar_trigger(const json & in):
    value.type  = (common_grammar_trigger_type) in.at("type").get<int>();   // arbitrary int -> enum
    value.value = in.at("value").get<std::string>();

Non-WORD triggers are stored unchanged (no validation):

common/sampling.cpp:1084-1085   else { sampling.grammar_triggers.push_back(std::move(trigger)); }

and the consuming switch has an asserting default:

common/sampling.cpp:226-258   for (trigger : params.grammar_triggers) switch (trigger.type) {
    case ..._WORD / _PATTERN / _PATTERN_FULL / _TOKEN: ...
    default: GGML_ASSERT(false && "unknown trigger type");   // :257
}

The request is accepted (schema eval just stores the trigger); the abort fires later on the inference worker thread when the slot launches:

server-context.cpp:1924-1930   try { slot.smpl.reset(common_sampler_init(model_tgt, task.params.sampling)); }
                               catch (const std::exception & e) { ... }   // catches std::exception

Crucially that try/catch does not save it: GGML_ASSERT → GGML_ABORT → ggml_abort() → abort() (ggml/include/ggml.h:287-288) raises SIGABRT, which bypasses C++ exception handling. The process dies, taking down every slot and client. The trigger loop runs in the non-llguidance branch even when no grammar is set, so no grammar/flag is required.

Any type not in {0,1,2,3} triggers it (e.g. 4, -1, 99). (type must be an integer and value a string, else at().get<>() throws → clean 400, not a crash.) Reachable on /completion and /v1/completions; not via /v1/chat/completions (it erases grammar_triggers, server-common.cpp:1479-1482).

Repro: Contains 2 conditions:

control {"prompt":"hi","n_predict":1}                               -> HTTP 200, alive
bomb    {"prompt":"hi","n_predict":1,
         "grammar_triggers":[{"type":99,"value":"x"}]}              -> "Aborted (core dumped)",
                                                                       server exit 134 (SIGABRT),
                                                                       /health -> connection refused

repro_llamacpp_grammar_triggers_abort.py

First Bad Commit

No response

Relevant log output

Logs

Yunzez avatar Oct 19 '26 19:10 Yunzez

Reproduced on macOS arm64 (AppleClang 21), master 0a1ef30, using the tiny stories260K.gguf, so no real model download is needed. Control POST /completion {"prompt":"hi","n_predict":1} returns HTTP 200 in 28 ms; adding "grammar_triggers":[{"type":99,"value":"x"}] kills the process with exit 134 (SIGABRT), and /health then refuses connections. The log ends with common/sampling.cpp:258: GGML_ASSERT(false && "unknown trigger type") failed. The unchecked cast now sits at tools/server/server-common.h:77, and the try/catch around common_sampler_init at tools/server/server-context.cpp:2014 only catches std::exception, so abort() bypasses it on any platform.

0xamlab avatar Oct 21 '26 13:10 0xamlab