writeup / cJSON #1093

Blowing the stack of a JSON parser

Nhaajt2026-11-06C, ASan, fuzzing, x86-64 issue #1093

I reported a stack overflow in cJSON, a small C JSON parser. The input is nothing but opening brackets. This page walks through why that is enough, and lets you push the stack yourself.

01How fuzzing found it

The issue says it came from an independent fuzzing exercise, built the way sanitizer fuzzing and CI runs usually are: cJSON.c compiled with -O0 -fsanitize=address -g. The crash reproduces through cJSON_ParseWithLength and was minimized down to a single repeated byte.

The usual libFuzzer or AFL style loop looks like this. This sketch is a generic example of the workflow, not the harness from the issue.

// generic libFuzzer-style harness (illustrative)
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
    cJSON *j = cJSON_ParseWithLength((const char*)data, size);
    cJSON_Delete(j);
    return 0;
}
// build: clang -O0 -g -fsanitize=address,fuzzer harness.c cJSON.c
// run:   ./fuzzer corpus/   then minimize the crashing input

Minimizing is what made it readable: the crash file shrank to 755 [ bytes. A binary search found the boundary: 754 levels return NULL normally, 755 crash.

02The input shape

Deep nesting. No values, no closing brackets, no other content. The parser never gets to see anything valid, it just keeps descending.

$ ./cjson_asan_o0 < deepnest_755.json
==PID==ERROR: AddressSanitizer: stack-overflow on address 0x...
    <empty stack>
SUMMARY: AddressSanitizer: stack-overflow

There is no backtrace. A pure stack overflow leaves ASan with no stack to run its own crash reporting on, which is why the trace is empty.

03Why recursion depth blows the stack

cJSON parses recursively: a value dispatches to parse_array or parse_object, which call back into parse_value for each element. One more [ means one more round of calls, and every call pushes a stack frame: a return address, the saved rbp, and the function's local variables.

CJSON_NESTING_LIMIT (1000) counts recursive calls. It does not count bytes. Under -O0 every local gets its own stack slot, and ASan adds redzone padding around each stack allocation, so each frame is much bigger than in an optimized build. 755 levels were enough to use up 8 MiB, well before the counter reached 1000. At -O2 without sanitizers the same inputs (780, 1000, 1500, 5000 and 50000 levels) are all rejected cleanly.

Simplified model. Frame sizes are chosen to match the 754/755 boundary, not measured.

What the model does: each nesting level pushes parse_value then parse_array, 11,112 bytes per level in total (8 MiB divided by 755 is about 11.1 KB). Drag the depth to 754 and press Jump: it parses to the end of the input and fails cleanly. Set 755 and the second frame of level 755 goes past the stack limit and the process gets SIGSEGV, even though the counter only says 755. Tick the fix box to see the guard take over. Note that the default limit of 1000 does not help here.

The same mechanism at the instruction level, on a much smaller function. This is a hand-written subset emulator (push, pop, mov, sub, add, call, ret, cmp, jcc, lea). Click a dot to set a breakpoint, step in, step over, or run to the crash. Change the depth and watch where it dies.

Simplified model. Not code from cJSON, and the stack limit is tiny on purpose.

Keys while the debugger has focus: s step, o step over, r run, x reset.

04The fix and mitigation

This is not exploitable in a normal optimized release build, and it was filed as a robustness and documentation note, not a security report. The options listed in the issue, in order of effort:

Model note: the fix toggle in the visualizer is a lower limit (512 or 700). It bails out at level limit+1 with a clean NULL before the stack runs out. Picking 1000 shows why the shipped value does not catch this case.

A limit only protects you if its worst-case frame cost fits in the stack you actually have: limit × bytes per level < stack size.

05What I learned

Original report: DaveGamble/cJSON#1093.