From 2fda03ccdd1e4c96134cabacad40baa3d787b7ef Mon Sep 17 00:00:00 2001 From: fl0l0u Date: Sun, 15 Nov 2026 17:07:53 +0000 Subject: [PATCH] vendor: the pattern parser counts its nesting, and refuses past 64 A tool schema's `pattern` reaches the grammar converter as client data, and the group scanner recursed once per nested '(': a few tens of KB of brackets inside one JSON string was an uncatchable stack overflow of the request worker. The body's depth and value bounds never saw it -- the brackets are string content, not JSON nesting -- and 0003, the patch that made this same function survive a hostile pattern, hardened the empty and the looping cases but never the recursing one. It is the one remote crash in the audit, and one request is the whole attack. transform() now counts its nesting and records the refusal once, where every unwinding frame met the same '(' and repeated it; check_errors turns it into the clean throw the server already answers with a 400. Proven on the compile host against the real converter: the 20000-group pattern is refused by name, a 40-group pattern -- far past anything a real schema nests -- still compiles. The record keeps the addition under 0003's name, whose footing it stands on. --- tests/gbnf_test.cpp | 16 ++++++++++++++ vendor/SHA256SUMS | 4 ++-- vendor/UPSTREAM | 6 +++++ .../llama/common/json-schema-to-grammar.cpp | 22 +++++++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/tests/gbnf_test.cpp b/tests/gbnf_test.cpp index f6c74ee4..8efa2af5 100644 --- a/tests/gbnf_test.cpp +++ b/tests/gbnf_test.cpp @@ -1515,6 +1515,22 @@ TEST(gbnf_a_grammar_whose_masks_need_unbounded_context_is_refused) { std::fprintf(stderr, " refused: %s\n", err.c_str()); } +/* `pattern` is client data, and the group scanner recurses once per nested '('. Unbounded, a + * few tens of KB of '(' -- one JSON string, so the body's depth and value bounds never see it -- + * stack-overflows the request worker past any catch. Nesting past the cap is refused the way + * every other unsupported syntax is, and a pattern no real schema exceeds stays legal. */ +TEST(gbnf_a_pattern_nesting_past_the_cap_is_refused_not_fatal) { + const std::string deep = "^" + std::string(20000, '(') + "a" + std::string(20000, ')') + "$"; + const std::string schema = std::string(R"({"type":"string","pattern":")") + deep + R"("})"; + std::string gbnf, err; + CHECK(grammar_from_json_schema(schema, "root", &gbnf, &err) != 0); + CHECK(err.find("deep") != std::string::npos); + const std::string sane = "^" + std::string(40, '(') + "a" + std::string(40, ')') + "$"; + const std::string schema_ok = std::string(R"({"type":"string","pattern":")") + sane + R"("})"; + std::string gbnf2, err2; + CHECK_OK(grammar_from_json_schema(schema_ok, "root", &gbnf2, &err2)); +} + /* A rule that can never match is refused by name. `r ::= r` passes the left-recursion check (the * reference is in tail position) but only leads back into itself, and upstream reads the resulting * empty stack set as admitting every token. A grammar that merely never ends still compiles. */ diff --git a/vendor/SHA256SUMS b/vendor/SHA256SUMS index f71da7ff..51a42341 100644 --- a/vendor/SHA256SUMS +++ b/vendor/SHA256SUMS @@ -27,7 +27,7 @@ dee35610e7de346501f49a0f62bb9c99f9dc10534a22c0ee20baabf11fc61ca3 vendor/llama/c fe5bb39af419fffc5291796848edc9f6d990c416ea8ff5fe602b9e4b8dd4140b vendor/llama/common/jinja/value.h 00a6eb892deb9c0578fec97e72b8dbb2518216bb325a972987691b67a14ecc02 vendor/llama/common/json-partial.cpp 1ff33e25293b376c9262c5e5842fcb50ebcf8f4e5c5222505a8a757be53fd4a5 vendor/llama/common/json-partial.h -a8743199b372fc75feb74b6441fbe860227751b0c379860f06a57e82953769bf vendor/llama/common/json-schema-to-grammar.cpp +c330c964ffc9f895c9d3c7050eb4a2372c9116c8aee85b4c6542952eb4134c35 vendor/llama/common/json-schema-to-grammar.cpp e2e2d20d39e57ecf3c440df04097be4c6b3d59ba09f0655f675384d1c99f6801 vendor/llama/common/json-schema-to-grammar.h d4b2739e4b06668dc14a1d3c6ed15c4fcb93d5c6e976f06bc3363e797fff9c1c vendor/llama/common/peg-parser.cpp 4e77267b71d77a5a5617d89a1cfb1e7b43df0af52e6ba38f305abfdf9f443464 vendor/llama/common/peg-parser.h @@ -50,7 +50,7 @@ d5104362705e687271807f499481db6033d85988e13fac12f36e370c6fce0551 vendor/shim/ll 5d556a22069133f7931850a13094d7390ba091051f0dc98cb1cfca824142a705 vendor/shim/llama_shim.cpp 628efd8a88524082c4736d3bc52790aa459b40728f213246c54550011f2754a2 vendor/shim/log.h 1c343ff2dbf801873acc2ee67c0e874137fb36df31421f36fc5cb6812463aae4 vendor/shim/ryml_impl.cpp -c06f36e9ccb21458ed37af649e00b143f7bc773e0c7623d4f19f49c7a15ecfe5 vendor/UPSTREAM +4e3fcc1f56d00c21967fa16e1d81aa9a012f1669c514ed16226204ff62e25d02 vendor/UPSTREAM 45e384d3d52c73cba3a64d6e6c25d47cd738cd8a55c30629e3201046eda62947 vendor/yyjson/LICENSE d2d58ef0a3b2267862dc363832c4f3185fd375aea933d5d3419d1a940d6ece2e vendor/yyjson/yyjson.c ef803cda5c06b8962face6dfa39c3284b3bcf3e73f4d7317664690cc779679ca vendor/yyjson/yyjson.h diff --git a/vendor/UPSTREAM b/vendor/UPSTREAM index e547d109..9abb9aa1 100644 --- a/vendor/UPSTREAM +++ b/vendor/UPSTREAM @@ -139,6 +139,12 @@ vendor/llama/common/regex-partial.{h,cpp} <- llama.cpp common/* adds a size test, two empty-container guards, and a did-we-consume-a-byte test at the bottom of the loop, each recording an error the existing check_errors() already turns into a throw. + Added 2026-11 on 0003's footing: the same client `pattern` can also KILL the worker by + recursion -- transform() calls itself once per nested '(', no cap anywhere, and no JSON + depth bound helps because the brackets are string content, not JSON nesting. A pattern of + tens of KB of '(' is an uncatchable stack overflow. A nesting counter caps the recursion + at 64 groups and records one more error the existing check_errors() turns into a refusal. + On 0004: three things upstream ACCEPTS and then gets wrong, each now refused by name. A remote $ref is fetched through a callback the public entry point stubs out, so it silently degrades into "accepts any object" -- we refuse and fetch nothing, because a sampler that diff --git a/vendor/llama/common/json-schema-to-grammar.cpp b/vendor/llama/common/json-schema-to-grammar.cpp index 7278aadb..b6d9f264 100644 --- a/vendor/llama/common/json-schema-to-grammar.cpp +++ b/vendor/llama/common/json-schema-to-grammar.cpp @@ -590,7 +590,29 @@ private: auto s = ls.first; return is_literal ? "\"" + s + "\"" : s; }; + int pw_nesting = 0; + bool pw_too_deep = false; + constexpr int kMaxPatternNesting = 64; std::function transform = [&]() -> literal_or_rule { + // RADIANCE: `client_pattern` is client data, and every nested `(` recurses this + // lambda once. A few tens of KB of '(' is an uncatchable stack overflow of the + // request worker -- the JSON depth and value bounds cannot help, because the + // brackets are string content, not JSON nesting. A pattern nesting past a depth + // no real one reaches is refused like any other unsupported syntax. Once only: + // every unwinding frame meets the same '(' again, and the error text travels. + if (++pw_nesting > kMaxPatternNesting) { + --pw_nesting; + if (!pw_too_deep) { + _errors.push_back("Pattern nests groups more than " + + std::to_string(kMaxPatternNesting) + " deep"); + pw_too_deep = true; + } + return std::make_pair(std::string(), false); + } + struct Leave { + int& d; + ~Leave() { --d; } + } leave{ pw_nesting }; size_t start = i; std::vector seq;