Avoid possible memleak in X509_policy_check() When tree_calculate_user_set() fails, a jump to error failed to deallocate a possibly allocated |auth_nodes|. (Imported from upstream's 58314197b54cc1417cfa62d1987462f72a2559e0.) Also sync up a couple of comments from that revision. Upstream's reformat script mangled them and we never did the manual fixup. Change-Id: I1ed896d13ec94d122d71df72af5a3be4eb0eb9d1 Reviewed-on: https://boringssl-review.googlesource.com/17644 Commit-Queue: Steven Valdez <svaldez@google.com> Reviewed-by: Steven Valdez <svaldez@google.com> CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
diff --git a/crypto/x509v3/pcy_tree.c b/crypto/x509v3/pcy_tree.c index 71f9a6d..256fe88 100644 --- a/crypto/x509v3/pcy_tree.c +++ b/crypto/x509v3/pcy_tree.c
@@ -136,11 +136,14 @@ #endif -/* - * Initialize policy tree. Return values: 0 Some internal error occured. -1 - * Inconsistent or invalid extensions in certificates. 1 Tree initialized - * OK. 2 Policy tree is empty. 5 Tree OK and requireExplicitPolicy true. 6 - * Tree empty and requireExplicitPolicy true. +/*- + * Initialize policy tree. Return values: + * 0 Some internal error occurred. + * -1 Inconsistent or invalid extensions in certificates. + * 1 Tree initialized OK. + * 2 Policy tree is empty. + * 5 Tree OK and requireExplicitPolicy true. + * 6 Tree empty and requireExplicitPolicy true. */ static int tree_init(X509_POLICY_TREE **ptree, STACK_OF(X509) *certs, @@ -720,10 +723,13 @@ } -/* - * Application policy checking function. Return codes: 0 Internal Error. 1 - * Successful. -1 One or more certificates contain invalid or inconsistent - * extensions -2 User constrained policy set empty and requireExplicit true. +/*- + * Application policy checking function. + * Return codes: + * 0 Internal Error. + * 1 Successful. + * -1 One or more certificates contain invalid or inconsistent extensions + * -2 User constrained policy set empty and requireExplicit true. */ int X509_policy_check(X509_POLICY_TREE **ptree, int *pexplicit_policy, @@ -731,6 +737,7 @@ STACK_OF(ASN1_OBJECT) *policy_oids, unsigned int flags) { int ret; + int calc_ret; X509_POLICY_TREE *tree = NULL; STACK_OF(X509_POLICY_NODE) *nodes, *auth_nodes = NULL; *ptree = NULL; @@ -799,16 +806,19 @@ /* Tree is not empty: continue */ - ret = tree_calculate_authority_set(tree, &auth_nodes); + calc_ret = tree_calculate_authority_set(tree, &auth_nodes); + + if (!calc_ret) + goto error; + + ret = tree_calculate_user_set(tree, policy_oids, auth_nodes); + + if (calc_ret == 2) + sk_X509_POLICY_NODE_free(auth_nodes); if (!ret) goto error; - if (!tree_calculate_user_set(tree, policy_oids, auth_nodes)) - goto error; - - if (ret == 2) - sk_X509_POLICY_NODE_free(auth_nodes); if (tree) *ptree = tree;