Repository navigation
json-schema-to-grammar: harden _visit_pattern against malformed regex patterns - #25308
professor-moody wants to merge 1 commit into
Conversation
Two crashes reachable from an attacker-influenced JSON-schema pattern
(e.g. a response_format grammar on the server):
- Null deref: seq.back() is called with no seq.empty() guard at the *,+,?
and {n,m} handlers. A quantifier with nothing to repeat (e.g. after an
unhandled lookbehind that pushes nothing) dereferences an empty vector.
- Stack overflow: transform() recurses per nested '(' with no depth limit;
a deeply-nested pattern overflows the stack.
Guard seq.empty() before both seq.back() sites (report a parse error via
_errors instead), and cap transform() nesting depth.
|
Hi @professor-moody, thanks for your contribution! Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:
Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below. |
|
Thanks, and fair flag. I use an AI-assisted fuzzing and triage tool in my security research, and I used it to help format concise PRs. The underlying work is mine and human-verified. I built llama.cpp at this commit, reproduced both crashes (the empty-seq SIGSEGV at the quantifier handler, and the stack overflow from unbounded transform() recursion), wrote the guards, and confirmed the inputs are now rejected cleanly and test-json-schema-to-grammar still passes. The change is a few lines of straightforward input validation. Happy to rewrite the description or commit message in different words if that helps meet the guideline, and to answer any review questions. |
|
@professor-moody could you please attach an example JSON to reproduce the issue that is being fixed? |
|
Sure. Two schemas, both crash on master (tested at 3581ba0) and are rejected cleanly with this patch. 1. Quantifier with nothing to repeat {"type": "string", "pattern": "^*$"}2. Deep group nesting. Too big to paste, but it's a one-liner: python3 -c 'n=20000; print(__import__("json").dumps({"type":"string","pattern":"^"+"("*n+")"*n+"$"}))' > deep.jsonDepth matters: 5000 is fine, 20000 overflows the stack on a default 8 MB stack. Before the patch both segfault (exit 139). After it, both raise I checked these by calling |
|
The fix itself looks correct, but I don't think the impact assessment of the CVE-2026-17500 and CVE-2026-17501 are, as they contradict this project's security guidelines. The CVEs are listed as exploitable over the network, but the only way I see this happening is through SECURITY.md explicitly considers DoS attacks against I'm mentioning this all because the CVEs cause quite a bit of busywork downstream. |
Thanks, and I hear you on the work, that said from a security perspective, I see it differently. First, I didn't set those scores. Both went to VulDB, which assigned the vectors and rated them Medium, below my own initial assessment. AV:N describes where the input arrives from, not an endorsement of exposing the server. Regarding SECURITY.md guidance, I read it as a deployment recommendation rather than a technical control. llama-server binds a port and serves an HTTP API, so the input is network-reachable regardless of what the documentation advises. Guidance not to expose a service doesn't change what the code does when it is exposed, and distributors ship it as a package with users who make that call independently. That's the gap a third-party identifier fills. It isn't a judgment that upstream should treat this as a vulnerability, and it doesn't oblige you to. Since you said the fix looks correct, is anything blocking the PR? Happy to rebase or split it. |
Two crashes in
_visit_pattern(common/json-schema-to-grammar.cpp) reachable from an attacker-influenced JSON-schemapatterne.g. aresponse_formatgrammar submitted to the server:seq.back()is called with noseq.empty()guard at the*/+/?handler and the{n,m}handler. A quantifier with nothing to repeat — for example after an unhandled lookbehind(?<=x), which pushes nothing ontoseqdereferences an empty vector.transform()recurses once per nested(with no depth limit, so a deeply-nested pattern overflows the stack.Fix
seq.empty()before bothseq.back()sites; on a malformed pattern, push a parse error to_errors(which the existing code at the end of the function turns into a thrownstd::invalid_argument) instead of dereferencing.transform()nesting depth (MAX_PATTERN_NESTING).Validation (built against this commit)
seqcase SIGSEGVs, the deep-nesting case overflows the stack.JSON schema conversion failed: Quantifier with nothing to repeat/Pattern nesting too deep;test-json-schema-to-grammarpasses unchanged.Fixes #25284
Fixes #25283