Skip to content
Myr

Limit path depth to prevent stack overflow

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Deeply nested path arrays can cause unbounded recursion in `jv_setpath`, `jv_getpath`, and `jv_delpaths`, leading to stack overflow. Add a depth limit of 10000 to match the existing `tojson` depth limit. This fixes CVE-2026-33947.

authored by itchyny · committed by itchyny · parent 2f09060 · Browse files · 2 files+46 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+46 added−0 removed

  • tests:+25 added−0 removed
  • other:+21 added−0 removed

2 files+46 added−0 removed

modified:src/jv_aux.c+21 added
363 unmodified lines
364 jv_free(keys);365 return t;366}367368#ifndef MAX_PATH_DEPTH369#define MAX_PATH_DEPTH (10000)370#endif371372jv jv_setpath(jv root, jv path, jv value) {373 if (jv_get_kind(path) != JV_KIND_ARRAY) {374 jv_free(value);375 jv_free(root);376 jv_free(path);377 return jv_invalid_with_msg(jv_string("Path must be specified as an array"));378 }379 if (jv_array_length(jv_copy(path)) > MAX_PATH_DEPTH) {380 jv_free(value);381 jv_free(root);382 jv_free(path);383 return jv_invalid_with_msg(jv_string("Path too deep"));384 }385 if (!jv_is_valid(root)){386 jv_free(value);387 jv_free(path);388 return root;
44 unmodified lines
433 jv_free(root);434 jv_free(path);435 return jv_invalid_with_msg(jv_string("Path must be specified as an array"));436 }437 if (jv_array_length(jv_copy(path)) > MAX_PATH_DEPTH) {438 jv_free(root);439 jv_free(path);440 return jv_invalid_with_msg(jv_string("Path too deep"));441 }442 if (!jv_is_valid(root)) {443 jv_free(path);444 return root;445 }
70 unmodified lines
516 jv_kind_name(jv_get_kind(elem))));517 jv_free(elem);518 return err;519 }520 if (jv_array_length(jv_copy(elem)) > MAX_PATH_DEPTH) {521 jv_free(object);522 jv_free(paths);523 jv_free(elem);524 return jv_invalid_with_msg(jv_string("Path too deep"));525 }526 jv_free(elem);527 }528 if (jv_array_length(jv_copy(paths)) == 0) {529 // nothing is being deleted
246 unmodified lines
modified:tests/jq.test+25 added
2566 unmodified lines
2567# (N+1)2568reduce range(10001) as $_ ([];[.]) | tojson | contains("<skipped: too deep>")2569null2570true25712572# regression test for CVE-2026-339472573setpath([range(10000) | 0]; 0) | flatten2574null2575[0]25762577try setpath([range(10001) | 0]; 0) catch .2578null2579"Path too deep"25802581getpath([range(10000) | 0])2582null2583null25842585try getpath([range(10001) | 0]) catch .2586null2587"Path too deep"25882589delpaths([[range(10000) | 0]])2590null2591null25922593try delpaths([[range(10001) | 0]]) catch .2594null2595"Path too deep"