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