1 From 879f7080d7e141f415c79eaa3a8ac4a3dad0348b 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
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.
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/20569)
19 diff --git a/crypto/x509v3/pcy_local.h b/crypto/x509v3/pcy_local.h
20 index 5daf78de45..344aa06765 100644
21 --- a/crypto/x509v3/pcy_local.h
22 +++ b/crypto/x509v3/pcy_local.h
23 @@ -111,6 +111,11 @@ struct X509_POLICY_LEVEL_st {
26 struct X509_POLICY_TREE_st {
27 + /* The number of nodes in the tree */
29 + /* The maximum number of nodes in the tree */
30 + size_t node_maximum;
32 /* This is the tree 'level' data */
33 X509_POLICY_LEVEL *levels;
35 @@ -159,7 +164,8 @@ X509_POLICY_NODE *tree_find_sk(STACK_OF(X509_POLICY_NODE) *sk,
36 X509_POLICY_NODE *level_add_node(X509_POLICY_LEVEL *level,
37 X509_POLICY_DATA *data,
38 X509_POLICY_NODE *parent,
39 - X509_POLICY_TREE *tree);
40 + X509_POLICY_TREE *tree,
42 void policy_node_free(X509_POLICY_NODE *node);
43 int policy_node_match(const X509_POLICY_LEVEL *lvl,
44 const X509_POLICY_NODE *node, const ASN1_OBJECT *oid);
45 diff --git a/crypto/x509v3/pcy_node.c b/crypto/x509v3/pcy_node.c
46 index e2d7b15322..d574fb9d66 100644
47 --- a/crypto/x509v3/pcy_node.c
48 +++ b/crypto/x509v3/pcy_node.c
49 @@ -59,10 +59,15 @@ X509_POLICY_NODE *level_find_node(const X509_POLICY_LEVEL *level,
50 X509_POLICY_NODE *level_add_node(X509_POLICY_LEVEL *level,
51 X509_POLICY_DATA *data,
52 X509_POLICY_NODE *parent,
53 - X509_POLICY_TREE *tree)
54 + X509_POLICY_TREE *tree,
57 X509_POLICY_NODE *node;
59 + /* Verify that the tree isn't too large. This mitigates CVE-2023-0464 */
60 + if (tree->node_maximum > 0 && tree->node_count >= tree->node_maximum)
63 node = OPENSSL_zalloc(sizeof(*node));
65 X509V3err(X509V3_F_LEVEL_ADD_NODE, ERR_R_MALLOC_FAILURE);
66 @@ -70,7 +75,7 @@ X509_POLICY_NODE *level_add_node(X509_POLICY_LEVEL *level,
69 node->parent = parent;
71 + if (level != NULL) {
72 if (OBJ_obj2nid(data->valid_policy) == NID_any_policy) {
75 @@ -90,7 +95,7 @@ X509_POLICY_NODE *level_add_node(X509_POLICY_LEVEL *level,
81 if (tree->extra_data == NULL)
82 tree->extra_data = sk_X509_POLICY_DATA_new_null();
83 if (tree->extra_data == NULL){
84 @@ -103,6 +108,7 @@ X509_POLICY_NODE *level_add_node(X509_POLICY_LEVEL *level,
92 diff --git a/crypto/x509v3/pcy_tree.c b/crypto/x509v3/pcy_tree.c
93 index 6e8322cbc5..6c7fd35405 100644
94 --- a/crypto/x509v3/pcy_tree.c
95 +++ b/crypto/x509v3/pcy_tree.c
98 #include "pcy_local.h"
101 + * If the maximum number of nodes in the policy tree isn't defined, set it to
102 + * a generous default of 1000 nodes.
104 + * Defining this to be zero means unlimited policy tree growth which opens the
105 + * door on CVE-2023-0464.
108 +#ifndef OPENSSL_POLICY_TREE_NODES_MAX
109 +# define OPENSSL_POLICY_TREE_NODES_MAX 1000
113 * Enable this to print out the complete policy tree at various point during
115 @@ -168,6 +180,9 @@ static int tree_init(X509_POLICY_TREE **ptree, STACK_OF(X509) *certs,
116 return X509_PCY_TREE_INTERNAL;
119 + /* Limit the growth of the tree to mitigate CVE-2023-0464 */
120 + tree->node_maximum = OPENSSL_POLICY_TREE_NODES_MAX;
123 * http://tools.ietf.org/html/rfc5280#section-6.1.2, figure 3.
125 @@ -184,7 +199,7 @@ static int tree_init(X509_POLICY_TREE **ptree, STACK_OF(X509) *certs,
126 level = tree->levels;
127 if ((data = policy_data_new(NULL, OBJ_nid2obj(NID_any_policy), 0)) == NULL)
129 - if (level_add_node(level, data, NULL, tree) == NULL) {
130 + if (level_add_node(level, data, NULL, tree, 1) == NULL) {
131 policy_data_free(data);
134 @@ -243,7 +258,8 @@ static int tree_init(X509_POLICY_TREE **ptree, STACK_OF(X509) *certs,
135 * Return value: 1 on success, 0 otherwise
137 static int tree_link_matching_nodes(X509_POLICY_LEVEL *curr,
138 - X509_POLICY_DATA *data)
139 + X509_POLICY_DATA *data,
140 + X509_POLICY_TREE *tree)
142 X509_POLICY_LEVEL *last = curr - 1;
144 @@ -253,13 +269,13 @@ static int tree_link_matching_nodes(X509_POLICY_LEVEL *curr,
145 X509_POLICY_NODE *node = sk_X509_POLICY_NODE_value(last->nodes, i);
147 if (policy_node_match(last, node, data->valid_policy)) {
148 - if (level_add_node(curr, data, node, NULL) == NULL)
149 + if (level_add_node(curr, data, node, tree, 0) == NULL)
154 if (!matched && last->anyPolicy) {
155 - if (level_add_node(curr, data, last->anyPolicy, NULL) == NULL)
156 + if (level_add_node(curr, data, last->anyPolicy, tree, 0) == NULL)
160 @@ -272,7 +288,8 @@ static int tree_link_matching_nodes(X509_POLICY_LEVEL *curr,
161 * Return value: 1 on success, 0 otherwise.
163 static int tree_link_nodes(X509_POLICY_LEVEL *curr,
164 - const X509_POLICY_CACHE *cache)
165 + const X509_POLICY_CACHE *cache,
166 + X509_POLICY_TREE *tree)
170 @@ -280,7 +297,7 @@ static int tree_link_nodes(X509_POLICY_LEVEL *curr,
171 X509_POLICY_DATA *data = sk_X509_POLICY_DATA_value(cache->data, i);
173 /* Look for matching nodes in previous level */
174 - if (!tree_link_matching_nodes(curr, data))
175 + if (!tree_link_matching_nodes(curr, data, tree))
179 @@ -311,7 +328,7 @@ static int tree_add_unmatched(X509_POLICY_LEVEL *curr,
180 /* Curr may not have anyPolicy */
181 data->qualifier_set = cache->anyPolicy->qualifier_set;
182 data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;
183 - if (level_add_node(curr, data, node, tree) == NULL) {
184 + if (level_add_node(curr, data, node, tree, 1) == NULL) {
185 policy_data_free(data);
188 @@ -373,7 +390,7 @@ static int tree_link_any(X509_POLICY_LEVEL *curr,
190 /* Finally add link to anyPolicy */
191 if (last->anyPolicy &&
192 - level_add_node(curr, cache->anyPolicy, last->anyPolicy, NULL) == NULL)
193 + level_add_node(curr, cache->anyPolicy, last->anyPolicy, tree, 0) == NULL)
197 @@ -555,7 +572,7 @@ static int tree_calculate_user_set(X509_POLICY_TREE *tree,
198 extra->qualifier_set = anyPolicy->data->qualifier_set;
199 extra->flags = POLICY_DATA_FLAG_SHARED_QUALIFIERS
200 | POLICY_DATA_FLAG_EXTRA_NODE;
201 - node = level_add_node(NULL, extra, anyPolicy->parent, tree);
202 + node = level_add_node(NULL, extra, anyPolicy->parent, tree, 1);
204 if (!tree->user_policies) {
205 tree->user_policies = sk_X509_POLICY_NODE_new_null();
206 @@ -582,7 +599,7 @@ static int tree_evaluate(X509_POLICY_TREE *tree)
208 for (i = 1; i < tree->nlevel; i++, curr++) {
209 cache = policy_cache_set(curr->cert);
210 - if (!tree_link_nodes(curr, cache))
211 + if (!tree_link_nodes(curr, cache, tree))
212 return X509_PCY_TREE_INTERNAL;
214 if (!(curr->flags & X509_V_FLAG_INHIBIT_ANY)