ipq807x: remove critical trips from AC/HK DTSI
[openwrt/staging/stintel.git] / package / libs / openssl / patches / 200-x509-excessive-resource-use-verifying-policy-constra.patch
1 From 959c59c7a0164117e7f8366466a32bb1f8d77ff1 Mon Sep 17 00:00:00 2001
2 From: Pauli <pauli@openssl.org>
3 Date: Wed, 8 Mar 2023 15:28:20 +1100
4 Subject: [PATCH] x509: excessive resource use verifying policy constraints
5
6 A security vulnerability has been identified in all supported versions
7 of OpenSSL related to the verification of X.509 certificate chains
8 that include policy constraints. Attackers may be able to exploit this
9 vulnerability by creating a malicious certificate chain that triggers
10 exponential use of computational resources, leading to a denial-of-service
11 (DoS) attack on affected systems.
12
13 Fixes CVE-2023-0464
14
15 Reviewed-by: Tomas Mraz <tomas@openssl.org>
16 Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
17 (Merged from https://github.com/openssl/openssl/pull/20568)
18
19 --- a/crypto/x509/pcy_local.h
20 +++ b/crypto/x509/pcy_local.h
21 @@ -111,6 +111,11 @@ struct X509_POLICY_LEVEL_st {
22 };
23
24 struct X509_POLICY_TREE_st {
25 + /* The number of nodes in the tree */
26 + size_t node_count;
27 + /* The maximum number of nodes in the tree */
28 + size_t node_maximum;
29 +
30 /* This is the tree 'level' data */
31 X509_POLICY_LEVEL *levels;
32 int nlevel;
33 @@ -157,7 +162,8 @@ X509_POLICY_NODE *ossl_policy_tree_find_
34 X509_POLICY_NODE *ossl_policy_level_add_node(X509_POLICY_LEVEL *level,
35 X509_POLICY_DATA *data,
36 X509_POLICY_NODE *parent,
37 - X509_POLICY_TREE *tree);
38 + X509_POLICY_TREE *tree,
39 + int extra_data);
40 void ossl_policy_node_free(X509_POLICY_NODE *node);
41 int ossl_policy_node_match(const X509_POLICY_LEVEL *lvl,
42 const X509_POLICY_NODE *node, const ASN1_OBJECT *oid);
43 --- a/crypto/x509/pcy_node.c
44 +++ b/crypto/x509/pcy_node.c
45 @@ -59,10 +59,15 @@ X509_POLICY_NODE *ossl_policy_level_find
46 X509_POLICY_NODE *ossl_policy_level_add_node(X509_POLICY_LEVEL *level,
47 X509_POLICY_DATA *data,
48 X509_POLICY_NODE *parent,
49 - X509_POLICY_TREE *tree)
50 + X509_POLICY_TREE *tree,
51 + int extra_data)
52 {
53 X509_POLICY_NODE *node;
54
55 + /* Verify that the tree isn't too large. This mitigates CVE-2023-0464 */
56 + if (tree->node_maximum > 0 && tree->node_count >= tree->node_maximum)
57 + return NULL;
58 +
59 node = OPENSSL_zalloc(sizeof(*node));
60 if (node == NULL) {
61 ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
62 @@ -70,7 +75,7 @@ X509_POLICY_NODE *ossl_policy_level_add_
63 }
64 node->data = data;
65 node->parent = parent;
66 - if (level) {
67 + if (level != NULL) {
68 if (OBJ_obj2nid(data->valid_policy) == NID_any_policy) {
69 if (level->anyPolicy)
70 goto node_error;
71 @@ -90,7 +95,7 @@ X509_POLICY_NODE *ossl_policy_level_add_
72 }
73 }
74
75 - if (tree) {
76 + if (extra_data) {
77 if (tree->extra_data == NULL)
78 tree->extra_data = sk_X509_POLICY_DATA_new_null();
79 if (tree->extra_data == NULL){
80 @@ -103,6 +108,7 @@ X509_POLICY_NODE *ossl_policy_level_add_
81 }
82 }
83
84 + tree->node_count++;
85 if (parent)
86 parent->nchild++;
87
88 --- a/crypto/x509/pcy_tree.c
89 +++ b/crypto/x509/pcy_tree.c
90 @@ -14,6 +14,17 @@
91
92 #include "pcy_local.h"
93
94 +/*
95 + * If the maximum number of nodes in the policy tree isn't defined, set it to
96 + * a generous default of 1000 nodes.
97 + *
98 + * Defining this to be zero means unlimited policy tree growth which opens the
99 + * door on CVE-2023-0464.
100 + */
101 +#ifndef OPENSSL_POLICY_TREE_NODES_MAX
102 +# define OPENSSL_POLICY_TREE_NODES_MAX 1000
103 +#endif
104 +
105 static void expected_print(BIO *channel,
106 X509_POLICY_LEVEL *lev, X509_POLICY_NODE *node,
107 int indent)
108 @@ -163,6 +174,9 @@ static int tree_init(X509_POLICY_TREE **
109 return X509_PCY_TREE_INTERNAL;
110 }
111
112 + /* Limit the growth of the tree to mitigate CVE-2023-0464 */
113 + tree->node_maximum = OPENSSL_POLICY_TREE_NODES_MAX;
114 +
115 /*
116 * http://tools.ietf.org/html/rfc5280#section-6.1.2, figure 3.
117 *
118 @@ -180,7 +194,7 @@ static int tree_init(X509_POLICY_TREE **
119 if ((data = ossl_policy_data_new(NULL,
120 OBJ_nid2obj(NID_any_policy), 0)) == NULL)
121 goto bad_tree;
122 - if (ossl_policy_level_add_node(level, data, NULL, tree) == NULL) {
123 + if (ossl_policy_level_add_node(level, data, NULL, tree, 1) == NULL) {
124 ossl_policy_data_free(data);
125 goto bad_tree;
126 }
127 @@ -239,7 +253,8 @@ static int tree_init(X509_POLICY_TREE **
128 * Return value: 1 on success, 0 otherwise
129 */
130 static int tree_link_matching_nodes(X509_POLICY_LEVEL *curr,
131 - X509_POLICY_DATA *data)
132 + X509_POLICY_DATA *data,
133 + X509_POLICY_TREE *tree)
134 {
135 X509_POLICY_LEVEL *last = curr - 1;
136 int i, matched = 0;
137 @@ -249,13 +264,13 @@ static int tree_link_matching_nodes(X509
138 X509_POLICY_NODE *node = sk_X509_POLICY_NODE_value(last->nodes, i);
139
140 if (ossl_policy_node_match(last, node, data->valid_policy)) {
141 - if (ossl_policy_level_add_node(curr, data, node, NULL) == NULL)
142 + if (ossl_policy_level_add_node(curr, data, node, tree, 0) == NULL)
143 return 0;
144 matched = 1;
145 }
146 }
147 if (!matched && last->anyPolicy) {
148 - if (ossl_policy_level_add_node(curr, data, last->anyPolicy, NULL) == NULL)
149 + if (ossl_policy_level_add_node(curr, data, last->anyPolicy, tree, 0) == NULL)
150 return 0;
151 }
152 return 1;
153 @@ -268,7 +283,8 @@ static int tree_link_matching_nodes(X509
154 * Return value: 1 on success, 0 otherwise.
155 */
156 static int tree_link_nodes(X509_POLICY_LEVEL *curr,
157 - const X509_POLICY_CACHE *cache)
158 + const X509_POLICY_CACHE *cache,
159 + X509_POLICY_TREE *tree)
160 {
161 int i;
162
163 @@ -276,7 +292,7 @@ static int tree_link_nodes(X509_POLICY_L
164 X509_POLICY_DATA *data = sk_X509_POLICY_DATA_value(cache->data, i);
165
166 /* Look for matching nodes in previous level */
167 - if (!tree_link_matching_nodes(curr, data))
168 + if (!tree_link_matching_nodes(curr, data, tree))
169 return 0;
170 }
171 return 1;
172 @@ -307,7 +323,7 @@ static int tree_add_unmatched(X509_POLIC
173 /* Curr may not have anyPolicy */
174 data->qualifier_set = cache->anyPolicy->qualifier_set;
175 data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;
176 - if (ossl_policy_level_add_node(curr, data, node, tree) == NULL) {
177 + if (ossl_policy_level_add_node(curr, data, node, tree, 1) == NULL) {
178 ossl_policy_data_free(data);
179 return 0;
180 }
181 @@ -370,7 +386,7 @@ static int tree_link_any(X509_POLICY_LEV
182 /* Finally add link to anyPolicy */
183 if (last->anyPolicy &&
184 ossl_policy_level_add_node(curr, cache->anyPolicy,
185 - last->anyPolicy, NULL) == NULL)
186 + last->anyPolicy, tree, 0) == NULL)
187 return 0;
188 return 1;
189 }
190 @@ -553,7 +569,7 @@ static int tree_calculate_user_set(X509_
191 extra->flags = POLICY_DATA_FLAG_SHARED_QUALIFIERS
192 | POLICY_DATA_FLAG_EXTRA_NODE;
193 node = ossl_policy_level_add_node(NULL, extra, anyPolicy->parent,
194 - tree);
195 + tree, 1);
196 }
197 if (!tree->user_policies) {
198 tree->user_policies = sk_X509_POLICY_NODE_new_null();
199 @@ -580,7 +596,7 @@ static int tree_evaluate(X509_POLICY_TRE
200
201 for (i = 1; i < tree->nlevel; i++, curr++) {
202 cache = ossl_policy_cache_set(curr->cert);
203 - if (!tree_link_nodes(curr, cache))
204 + if (!tree_link_nodes(curr, cache, tree))
205 return X509_PCY_TREE_INTERNAL;
206
207 if (!(curr->flags & X509_V_FLAG_INHIBIT_ANY)