|
| 1 | +From fb59f1491058d58bdc3e8dd28f1773d1ac690a1f Mon Sep 17 00:00:00 2001 |
| 2 | +From: itchyny <itchyny@cybozu.co.jp> |
| 3 | +Date: Mon, 13 Apr 2026 11:23:40 +0900 |
| 4 | +Subject: [PATCH] Limit path depth to prevent stack overflow |
| 5 | + |
| 6 | +Deeply nested path arrays can cause unbounded recursion in |
| 7 | +`jv_setpath`, `jv_getpath`, and `jv_delpaths`, leading to |
| 8 | +stack overflow. Add a depth limit of 10000 to match the |
| 9 | +existing `tojson` depth limit. This fixes CVE-2026-33947. |
| 10 | +--- |
| 11 | + src/jv_aux.c | 21 +++++++++++++++++++++ |
| 12 | + 1 file changed, 21 insertions(+) |
| 13 | + |
| 14 | +diff --git a/src/jv_aux.c b/src/jv_aux.c |
| 15 | +index 018f380b10..fd5ff96684 100644 |
| 16 | +--- a/src/jv_aux.c |
| 17 | ++++ b/src/jv_aux.c |
| 18 | +@@ -365,6 +365,10 @@ static jv jv_dels(jv t, jv keys) { |
| 19 | + return t; |
| 20 | + } |
| 21 | + |
| 22 | ++#ifndef MAX_PATH_DEPTH |
| 23 | ++#define MAX_PATH_DEPTH (10000) |
| 24 | ++#endif |
| 25 | ++ |
| 26 | + jv jv_setpath(jv root, jv path, jv value) { |
| 27 | + if (jv_get_kind(path) != JV_KIND_ARRAY) { |
| 28 | + jv_free(value); |
| 29 | +@@ -372,6 +376,12 @@ jv jv_setpath(jv root, jv path, jv value) { |
| 30 | + jv_free(path); |
| 31 | + return jv_invalid_with_msg(jv_string("Path must be specified as an array")); |
| 32 | + } |
| 33 | ++ if (jv_array_length(jv_copy(path)) > MAX_PATH_DEPTH) { |
| 34 | ++ jv_free(value); |
| 35 | ++ jv_free(root); |
| 36 | ++ jv_free(path); |
| 37 | ++ return jv_invalid_with_msg(jv_string("Path too deep")); |
| 38 | ++ } |
| 39 | + if (!jv_is_valid(root)){ |
| 40 | + jv_free(value); |
| 41 | + jv_free(path); |
| 42 | +@@ -424,6 +434,11 @@ jv jv_getpath(jv root, jv path) { |
| 43 | + jv_free(path); |
| 44 | + return jv_invalid_with_msg(jv_string("Path must be specified as an array")); |
| 45 | + } |
| 46 | ++ if (jv_array_length(jv_copy(path)) > MAX_PATH_DEPTH) { |
| 47 | ++ jv_free(root); |
| 48 | ++ jv_free(path); |
| 49 | ++ return jv_invalid_with_msg(jv_string("Path too deep")); |
| 50 | ++ } |
| 51 | + if (!jv_is_valid(root)) { |
| 52 | + jv_free(path); |
| 53 | + return root; |
| 54 | +@@ -502,6 +517,12 @@ jv jv_delpaths(jv object, jv paths) { |
| 55 | + jv_free(elem); |
| 56 | + return err; |
| 57 | + } |
| 58 | ++ if (jv_array_length(jv_copy(elem)) > MAX_PATH_DEPTH) { |
| 59 | ++ jv_free(object); |
| 60 | ++ jv_free(paths); |
| 61 | ++ jv_free(elem); |
| 62 | ++ return jv_invalid_with_msg(jv_string("Path too deep")); |
| 63 | ++ } |
| 64 | + jv_free(elem); |
| 65 | + } |
| 66 | + if (jv_array_length(jv_copy(paths)) == 0) { |
0 commit comments