Skip to content

fix(cypher): bound UNWIND literal-list buffer to prevent stack overflow#1173

Open
SEPURI-SAI-KRISHNA wants to merge 1 commit into
DeusData:mainfrom
SEPURI-SAI-KRISHNA:fix/cypher-unwind-stack-overflow
Open

fix(cypher): bound UNWIND literal-list buffer to prevent stack overflow#1173
SEPURI-SAI-KRISHNA wants to merge 1 commit into
DeusData:mainfrom
SEPURI-SAI-KRISHNA:fix/cypher-unwind-stack-overflow

Conversation

@SEPURI-SAI-KRISHNA

Copy link
Copy Markdown

What does this PR do?

Fixes a stack out-of-bounds write (CWE-787) in the Cypher UNWIND literal-list
parser (parse_unwind_clause, src/cypher/cypher.c).

When assembling a literal list into the fixed 2 KB stack buffer
char buf[CBM_SZ_2K], the code advances the write cursor by the return value of
snprintf:

blen += snprintf(buf + blen, sizeof(buf) - blen, "\"%s\"", peek(p)->text);

snprintf returns the number of bytes it would have written, so a single
oversized element pushes blen past sizeof(buf). The trailing writes are then
unguarded:

buf[blen++] = ']';
buf[blen] = '\0';

and on multi-element lists the next iteration's sizeof(buf) - blen underflows
to a huge size_t, letting snprintf write far past the buffer.

The query string is agent-controlled through the MCP query tool, and Cypher
string literals lex up to CBM_SZ_4K - 1 (4095) chars with no length cap before
parsing, so UNWIND ["<~3000 chars>"] AS x MATCH (n) RETURN n reaches the
overflow.

Reproduced first (UBSan, before the fix):

src/cypher/cypher.c:1855:12: runtime error: index 3002 out of bounds for type 'char [2048]'
src/cypher/cypher.c:1855:21: runtime error: store to address 0x... with insufficient space for an object of type 'char'
src/cypher/cypher.c:1856:12: runtime error: index 3003 out of bounds for type 'char [2048]'

The fix clamps blen after every snprintf and guards the raw
buf[blen++] stores — the same pattern already used by the sibling function
format_collect_list(), which builds an identical JSON array into a
CBM_SZ_2K buffer and was already hardened. After the fix all 161 cypher tests
pass with zero sanitizer errors.

Two regression tests are added (both fail under UBSan without the fix):

  • cypher_parse_unwind_oversized_literal_no_overflow — single oversized element
  • cypher_parse_unwind_many_elements_no_overflow — many elements accumulating past 2 KB

Checklist

  • Every commit is signed off (git commit -s) — required, CI rejects
    unsigned commits (DCO, see CONTRIBUTING.md)
  • Tests pass locally (make -f Makefile.cbm test)
  • Lint passes (make -f Makefile.cbm lint-ci)
  • New behavior is covered by a test (reproduce-first for bug fixes)

…ck overflow

Signed-off-by: SEPURI-SAI-KRISHNA <saik20533@gmail.com>
@SEPURI-SAI-KRISHNA
SEPURI-SAI-KRISHNA force-pushed the fix/cypher-unwind-stack-overflow branch from ffc88cc to d0457f2 Compare July 19, 2026 06:27
@DeusData DeusData added bug Something isn't working stability/performance Server crashes, OOM, hangs, high CPU/memory cypher Cypher query language parser/executor bugs security Security vulnerabilities, hardening labels Jul 19, 2026
@DeusData DeusData added this to the 0.9.1-rc milestone Jul 19, 2026
@DeusData DeusData added the priority/high Needs near-term maintainer attention; high-impact bug, regression, safety issue, or release blocker. label Jul 19, 2026
@DeusData

Copy link
Copy Markdown
Owner

Thanks for the reproduce-first memory-safety fix. Triage is complete: this is a high-priority 0.9.1-rc Cypher stability/security item. Exact-head review at d0457f2ba95a521fb53e433394aaa7a511cec3ee found the change scoped to the parser and sanitizer regressions, with no dependency, workflow, installer, network, or vendored-code changes. It remains in the maintainer review queue; this triage pass does not merge PRs.

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

Labels

bug Something isn't working cypher Cypher query language parser/executor bugs priority/high Needs near-term maintainer attention; high-impact bug, regression, safety issue, or release blocker. security Security vulnerabilities, hardening stability/performance Server crashes, OOM, hangs, high CPU/memory

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants