|
| 1 | +From d1a12569d91641135976a8536776a4a329c02cc2 Mon Sep 17 00:00:00 2001 |
| 2 | +From: itchyny <itchyny@cybozu.co.jp> |
| 3 | +Date: Fri, 24 Apr 2026 22:02:24 +0900 |
| 4 | +Subject: [PATCH] Limit the containment check depth |
| 5 | + |
| 6 | +This fixes CVE-2026-40612. |
| 7 | + |
| 8 | +Modified to apply to Azure Linux. |
| 9 | +Added extra closed brackets `()` after try in jq.test file to avoid syntax errors caused due to version differences. |
| 10 | + |
| 11 | +Upstream Patch reference: https://github.com/jqlang/jq/commit/d1a12569d91641135976a8536776a4a329c02cc2.patch |
| 12 | +--- |
| 13 | + src/builtin.c | 5 ++++- |
| 14 | + src/jv.c | 40 +++++++++++++++++++++++++++------------- |
| 15 | + tests/jq.test | 9 +++++++++ |
| 16 | + 3 files changed, 40 insertions(+), 14 deletions(-) |
| 17 | + |
| 18 | +diff --git a/src/builtin.c b/src/builtin.c |
| 19 | +index 3cb8eb7..08b94ac 100644 |
| 20 | +--- a/src/builtin.c |
| 21 | ++++ b/src/builtin.c |
| 22 | +@@ -471,7 +471,10 @@ jv binop_greatereq(jv a, jv b) { |
| 23 | + |
| 24 | + static jv f_contains(jq_state *jq, jv a, jv b) { |
| 25 | + if (jv_get_kind(a) == jv_get_kind(b)) { |
| 26 | +- return jv_bool(jv_contains(a, b)); |
| 27 | ++ int r = jv_contains(a, b); |
| 28 | ++ if (r < 0) |
| 29 | ++ return jv_invalid_with_msg(jv_string("Containment check too deep")); |
| 30 | ++ return jv_bool(r); |
| 31 | + } else { |
| 32 | + return type_error2(a, b, "cannot have their containment checked"); |
| 33 | + } |
| 34 | +diff --git a/src/jv.c b/src/jv.c |
| 35 | +index 08ded35..5a2c3a2 100644 |
| 36 | +--- a/src/jv.c |
| 37 | ++++ b/src/jv.c |
| 38 | +@@ -914,19 +914,19 @@ static void jvp_clamp_slice_params(int len, int *pstart, int *pend) |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | +-static int jvp_array_contains(jv a, jv b) { |
| 43 | ++static int jvp_contains(jv a, jv b, int depth); |
| 44 | ++ |
| 45 | ++static int jvp_array_contains(jv a, jv b, int depth) { |
| 46 | + int r = 1; |
| 47 | + jv_array_foreach(b, bi, belem) { |
| 48 | + int ri = 0; |
| 49 | + jv_array_foreach(a, ai, aelem) { |
| 50 | +- if (jv_contains(aelem, jv_copy(belem))) { |
| 51 | +- ri = 1; |
| 52 | +- break; |
| 53 | +- } |
| 54 | ++ ri = jvp_contains(aelem, jv_copy(belem), depth); |
| 55 | ++ if (ri) break; |
| 56 | + } |
| 57 | + jv_free(belem); |
| 58 | +- if (!ri) { |
| 59 | +- r = 0; |
| 60 | ++ if (ri <= 0) { |
| 61 | ++ r = ri; |
| 62 | + break; |
| 63 | + } |
| 64 | + } |
| 65 | +@@ -1794,7 +1794,7 @@ static int jvp_object_equal(jv o1, jv o2) { |
| 66 | + return len1 == len2; |
| 67 | + } |
| 68 | + |
| 69 | +-static int jvp_object_contains(jv a, jv b) { |
| 70 | ++static int jvp_object_contains(jv a, jv b, int depth) { |
| 71 | + assert(JVP_HAS_KIND(a, JV_KIND_OBJECT)); |
| 72 | + assert(JVP_HAS_KIND(b, JV_KIND_OBJECT)); |
| 73 | + int r = 1; |
| 74 | +@@ -1802,9 +1802,9 @@ static int jvp_object_contains(jv a, jv b) { |
| 75 | + jv_object_foreach(b, key, b_val) { |
| 76 | + jv a_val = jv_object_get(jv_copy(a), key); |
| 77 | + |
| 78 | +- r = jv_contains(a_val, b_val); |
| 79 | ++ r = jvp_contains(a_val, b_val, depth); |
| 80 | + |
| 81 | +- if (!r) break; |
| 82 | ++ if (r <= 0) break; |
| 83 | + } |
| 84 | + return r; |
| 85 | + } |
| 86 | +@@ -2035,14 +2035,23 @@ int jv_identical(jv a, jv b) { |
| 87 | + return r; |
| 88 | + } |
| 89 | + |
| 90 | +-int jv_contains(jv a, jv b) { |
| 91 | ++#ifndef MAX_CONTAINS_DEPTH |
| 92 | ++#define MAX_CONTAINS_DEPTH (10000) |
| 93 | ++#endif |
| 94 | ++ |
| 95 | ++static int jvp_contains(jv a, jv b, int depth) { |
| 96 | ++ if (depth > MAX_CONTAINS_DEPTH) { |
| 97 | ++ jv_free(a); |
| 98 | ++ jv_free(b); |
| 99 | ++ return -1; |
| 100 | ++ } |
| 101 | + int r = 1; |
| 102 | + if (jv_get_kind(a) != jv_get_kind(b)) { |
| 103 | + r = 0; |
| 104 | + } else if (JVP_HAS_KIND(a, JV_KIND_OBJECT)) { |
| 105 | +- r = jvp_object_contains(a, b); |
| 106 | ++ r = jvp_object_contains(a, b, depth + 1); |
| 107 | + } else if (JVP_HAS_KIND(a, JV_KIND_ARRAY)) { |
| 108 | +- r = jvp_array_contains(a, b); |
| 109 | ++ r = jvp_array_contains(a, b, depth + 1); |
| 110 | + } else if (JVP_HAS_KIND(a, JV_KIND_STRING)) { |
| 111 | + int b_len = jv_string_length_bytes(jv_copy(b)); |
| 112 | + if (b_len != 0) { |
| 113 | +@@ -2058,3 +2067,8 @@ int jv_contains(jv a, jv b) { |
| 114 | + jv_free(b); |
| 115 | + return r; |
| 116 | + } |
| 117 | ++ |
| 118 | ++// Returns 1 (contained), 0 (not contained), or -1 (too deep) |
| 119 | ++int jv_contains(jv a, jv b) { |
| 120 | ++ return jvp_contains(a, b, 0); |
| 121 | ++} |
| 122 | +diff --git a/tests/jq.test b/tests/jq.test |
| 123 | +index 758a161..0516ff1 100644 |
| 124 | +--- a/tests/jq.test |
| 125 | ++++ b/tests/jq.test |
| 126 | +@@ -2155,3 +2155,12 @@ try ltrimstr("x") catch "x", try rtrimstr("x") catch "x" | "ok" |
| 127 | + {"hey":[]} |
| 128 | + "ok" |
| 129 | + "ok" |
| 130 | ++ |
| 131 | ++# regression test for CVE-2026-40612 |
| 132 | ++reduce range(10000) as $_ ([]; [.]) | contains([[]]) |
| 133 | ++null |
| 134 | ++true |
| 135 | ++ |
| 136 | ++try ((reduce range(10001) as $_ ([]; [.])) as $x | $x | contains($x)) catch . |
| 137 | ++null |
| 138 | ++"Containment check too deep" |
| 139 | +-- |
| 140 | +2.43.0 |
| 141 | + |
0 commit comments