Skip to content

json-schema-to-grammar: harden _visit_pattern against malformed regex patterns - #25308

Closed
professor-moody wants to merge 1 commit into
ggml-org:masterfrom
professor-moody:fix-json-schema-visit-pattern
Closed

professor-moody wants to merge 1 commit into
ggml-org:masterfrom
professor-moody:fix-json-schema-visit-pattern

Conversation

@professor-moody

@professor-moody professor-moody commented Jul 4, 2026 •

Copy link
Copy Markdown

Two crashes in _visit_pattern (common/json-schema-to-grammar.cpp) reachable from an attacker-influenced JSON-schema pattern e.g. a response_format grammar submitted to the server:

  1. Null dereference / SIGSEGV. seq.back() is called with no seq.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 onto seq dereferences an empty vector.
  2. Stack overflow. transform() recurses once per nested ( with no depth limit, so a deeply-nested pattern overflows the stack.

Fix

  • Guard seq.empty() before both seq.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 thrown std::invalid_argument) instead of dereferencing.
  • Cap transform() nesting depth (MAX_PATTERN_NESTING).

Validation (built against this commit)

  • Before: both inputs crash — the empty-seq case SIGSEGVs, the deep-nesting case overflows the stack.
  • After: both are rejected cleanly with JSON schema conversion failed: Quantifier with nothing to repeat / Pattern nesting too deep; test-json-schema-to-grammar passes unchanged.

Fixes #25284
Fixes #25283

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.
@professor-moody
professor-moody requested a review from a team as a code owner July 4, 2026 19:09
@ggml-gh-bot

ggml-gh-bot Bot commented Jul 4, 2026

Copy link
Copy Markdown

Hi @professor-moody, thanks for your contribution!

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

  • 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.

@professor-moody

Copy link
Copy Markdown
Author

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.

@ckastner

ckastner commented Aug 2, 2026

Copy link
Copy Markdown
Collaborator

@professor-moody could you please attach an example JSON to reproduce the issue that is being fixed?

@professor-moody

Copy link
Copy Markdown
Author

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.json

Depth 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 std::invalid_argument with "Quantifier with nothing to repeat" and "Pattern nesting too deep" respectively, so callers get a normal conversion error instead of losing the process.

I checked these by calling json_schema_to_grammar() from a small driver, since there's no standalone CLI for the converter. test-json-schema-to-grammar still passes with the patch applied.

@ckastner

ckastner commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

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 llama-server, or did I miss something?

SECURITY.md explicitly considers DoS attacks against llama-server as invalid. It explicitly states to not use llama-server outside of isolated and secure environment, or in untrusted networks.

I'm mentioning this all because the CVEs cause quite a bit of busywork downstream.

@professor-moody

Copy link
Copy Markdown
Author

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 llama-server, or did I miss something?

SECURITY.md explicitly considers DoS attacks against llama-server as invalid. It explicitly states to not use llama-server outside of isolated and secure environment, or in untrusted networks.

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.

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

Labels

None yet

Projects

None yet

2 participants