grammar : reject inverted repetition range {m,n} (m>n)#25602
grammar : reject inverted repetition range {m,n} (m>n)#25602harrison001 wants to merge 1 commit into
Conversation
|
Hi @harrison001, 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. |
Overview
handle_repetitionsinllama_grammar_parser::parse_sequencecomputes the optional-repetition count as an unsigned subtraction:For an inverted range like
{2,1}(min=2, max=1),max_times - min_timesunderflows to ~1.8e19, so the loop appends rules until the process is OOM-killed. The existingMAX_REPETITION_THRESHOLDguard does not catch this, because it is computed fromtotal_rules = max_times(which is1here), not from the underflowedn_opt.Because
grammaris accepted from untrusted request bodies byllama-server(/completion, and thejson_schema→grammar path of/v1/chat/completions), this is a remote, unauthenticated DoS.This PR rejects
max_times < min_timesbefore expansion;parse()already catchesstd::runtime_errorand returns a clean parse failure.Additional information
Minimal PoC (18 bytes):
→ unbounded memory growth (libFuzzer OOM over
llama_grammar_parser::parse). Found onmastervia libFuzzer + ASan.Requirements
llama_grammar_parser::parse; the one-line fix and the PoC were drafted with the help of an AI coding assistant and reviewed/verified by me. I am responsible for all submitted changes.