Blame SOURCES/0035-Cleanup-the-SCEP-helper-curl-and-talloc-contexts-whe.patch

f0b236
From 0897d5131489c7eac21d558625c30d23b0a1774d Mon Sep 17 00:00:00 2001
f0b236
From: Your Name <you@example.com>
f0b236
Date: Tue, 14 Apr 2020 13:17:14 +0000
f0b236
Subject: [PATCH 35/39] Cleanup the SCEP helper curl and talloc contexts when
f0b236
 finished
f0b236
f0b236
The talloc context was freed in only a few cases and the curl
f0b236
context was never freed.
f0b236
---
f0b236
 src/scep.c     | 127 ++++++++++++++++++++++++++++++++-----------------
f0b236
 src/submit-h.c |  15 +++++-
f0b236
 src/submit-h.h |   1 +
f0b236
 3 files changed, 97 insertions(+), 46 deletions(-)
f0b236
f0b236
diff --git a/src/scep.c b/src/scep.c
f0b236
index 0b8bef9..4d00692 100644
f0b236
--- a/src/scep.c
f0b236
+++ b/src/scep.c
f0b236
@@ -199,7 +199,7 @@ int
f0b236
 main(int argc, const char **argv)
f0b236
 {
f0b236
 	const char *url = NULL, *results = NULL, *results2 = NULL;
f0b236
-	struct cm_submit_h_context *hctx;
f0b236
+	struct cm_submit_h_context *hctx = NULL;
f0b236
 	int c, verbose = 0, results_length = 0, results_length2 = 0, i;
f0b236
 	int prefer_non_renewal = 0, can_renewal = 0;
f0b236
 	int response_code = 0, response_code2 = 0;
f0b236
@@ -225,7 +225,8 @@ main(int argc, const char **argv)
f0b236
 	size_t payload_length;
f0b236
 	long error;
f0b236
 	PKCS7 *p7;
f0b236
-	poptContext pctx;
f0b236
+	int rval = CM_SUBMIT_STATUS_UNCONFIGURED;
f0b236
+	poptContext pctx = NULL;
f0b236
 	struct poptOption popts[] = {
f0b236
 		{"url", 'u', POPT_ARG_STRING, &url, 0, "service location", "URL"},
f0b236
 		{"ca-identifier", 'i', POPT_ARG_STRING, &id, 0, "name to use when querying for capabilities", "IDENTIFIER"},
f0b236
@@ -388,8 +389,8 @@ main(int argc, const char **argv)
f0b236
 			}
f0b236
 			if ((message == NULL) || (strlen(message) == 0)) {
f0b236
 				printf(_("Error reading request.  Expected PKCS7 data containing a GetInitialCert pkiMessage, got nothing.\n"));
f0b236
-				free(cainfo);
f0b236
-				return CM_SUBMIT_STATUS_NEED_SCEP_MESSAGES;
f0b236
+				rval = CM_SUBMIT_STATUS_NEED_SCEP_MESSAGES;
f0b236
+				goto done;
f0b236
 			}
f0b236
 			/* First step: read capabilities for our use. */
f0b236
 			params = talloc_asprintf(ctx, "operation=" OP_GET_CA_CAPS);
f0b236
@@ -408,8 +409,8 @@ main(int argc, const char **argv)
f0b236
 			}
f0b236
 			if ((message == NULL) || (strlen(message) == 0)) {
f0b236
 				printf(_("Error reading request.  Expected PKCS7 data containing a PKCSReq pkiMessage, got nothing.\n"));
f0b236
-				free(cainfo);
f0b236
-				return CM_SUBMIT_STATUS_NEED_SCEP_MESSAGES;
f0b236
+				rval = CM_SUBMIT_STATUS_NEED_SCEP_MESSAGES;
f0b236
+				goto done;
f0b236
 			}
f0b236
 			/* First step: read capabilities for our use. */
f0b236
 			params = talloc_asprintf(ctx, "operation=" OP_GET_CA_CAPS);
f0b236
@@ -420,8 +421,8 @@ main(int argc, const char **argv)
f0b236
 	/* Supply help output, if it's needed. */
f0b236
 	if (missing_args) {
f0b236
 		poptPrintUsage(pctx, stdout, 0);
f0b236
-		free(cainfo);
f0b236
-		return CM_SUBMIT_STATUS_UNCONFIGURED;
f0b236
+		rval = CM_SUBMIT_STATUS_UNCONFIGURED;
f0b236
+		goto done;
f0b236
 	}
f0b236
 
f0b236
 	/* Check the rekey PKCSReq message, if we have one. */
f0b236
@@ -505,7 +506,6 @@ main(int argc, const char **argv)
f0b236
 				verbose > 1 ?
f0b236
 				cm_submit_h_curl_verbose_on :
f0b236
 				cm_submit_h_curl_verbose_off);
f0b236
-	free(cainfo);
f0b236
 	cm_submit_h_run(hctx);
f0b236
 	content_type = cm_submit_h_result_type(hctx);
f0b236
 	if (content_type == NULL) {
f0b236
@@ -551,7 +551,8 @@ main(int argc, const char **argv)
f0b236
 		}
f0b236
 		if ((tmp2 == NULL) || (strlen(tmp2) == 0)) {
f0b236
 			printf(_("Error reading request.  Expected PKCS7 data containing a GetInitialCert pkiMessage, got nothing.\n"));
f0b236
-			return CM_SUBMIT_STATUS_NEED_SCEP_MESSAGES;
f0b236
+			rval = CM_SUBMIT_STATUS_NEED_SCEP_MESSAGES;
f0b236
+			goto done;
f0b236
 		} else
f0b236
 		if (verbose > 0) {
f0b236
 			if (tmp2 == rekey_message) {
f0b236
@@ -576,7 +577,8 @@ main(int argc, const char **argv)
f0b236
 		}
f0b236
 		if ((tmp2 == NULL) || (strlen(tmp2) == 0)) {
f0b236
 			printf(_("Error reading request.  Expected PKCS7 data containing a PKCSReq pkiMessage, got nothing.\n"));
f0b236
-			return CM_SUBMIT_STATUS_NEED_SCEP_MESSAGES;
f0b236
+			rval = CM_SUBMIT_STATUS_NEED_SCEP_MESSAGES;
f0b236
+			goto done;
f0b236
 		} else
f0b236
 		if (verbose > 0) {
f0b236
 			if (tmp2 == rekey_message) {
f0b236
@@ -638,7 +640,8 @@ main(int argc, const char **argv)
f0b236
 			       cm_submit_h_result_code(hctx),
f0b236
 			       url);
f0b236
 		}
f0b236
-		return CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+		rval = CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+		goto done;
f0b236
 	}
f0b236
 	switch (op) {
f0b236
 	case op_unset:
f0b236
@@ -651,16 +654,19 @@ main(int argc, const char **argv)
f0b236
 			       response_code, url);
f0b236
 			if (response_code == 500) {
f0b236
 				/* The server might recover, right? */
f0b236
-				return CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+				rval = CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+				goto done;
f0b236
 			} else {
f0b236
 				/* Maybe not? */
f0b236
-				return CM_SUBMIT_STATUS_REJECTED;
f0b236
+				rval = CM_SUBMIT_STATUS_REJECTED;
f0b236
+				goto done;
f0b236
 			}
f0b236
 		}
f0b236
 		if (results == NULL) {
f0b236
 			printf(_("Internal error: no response to \"%s?%s\".\n"),
f0b236
 			       url, params);
f0b236
-			return CM_SUBMIT_STATUS_REJECTED;
f0b236
+			rval = CM_SUBMIT_STATUS_REJECTED;
f0b236
+			goto done;
f0b236
 		}
f0b236
 		break;
f0b236
 	case op_get_cert_initial:
f0b236
@@ -685,10 +691,12 @@ main(int argc, const char **argv)
f0b236
 				fprintf(stderr, "Result is surprisingly large, "
f0b236
 					"suppressing it.\n");
f0b236
 			}
f0b236
-			return CM_SUBMIT_STATUS_REJECTED;
f0b236
+			rval = CM_SUBMIT_STATUS_REJECTED;
f0b236
+			goto done;
f0b236
 		}
f0b236
 		printf("%s\n", results);
f0b236
-		return CM_SUBMIT_STATUS_ISSUED;
f0b236
+		rval = CM_SUBMIT_STATUS_ISSUED;
f0b236
+		goto done;
f0b236
 		break;
f0b236
 	case op_get_ca_certs:
f0b236
 		if ((strcasecmp(content_type,
f0b236
@@ -697,7 +705,8 @@ main(int argc, const char **argv)
f0b236
 				"application/x-x509-ca-ra-cert") != 0)) {
f0b236
 			printf(_("Server reply was of unexpected MIME type "
f0b236
 				 "\"%s\".\n"), content_type);
f0b236
-			return CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+			rval = CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+			goto done;
f0b236
 		}
f0b236
 		if (racert == NULL) {
f0b236
 			racertp = &racer;;
f0b236
@@ -710,7 +719,8 @@ main(int argc, const char **argv)
f0b236
 						 n_buffers + 1);
f0b236
 			if ((buffers == NULL) || (lengths == NULL)) {
f0b236
 				fprintf(stderr, "Out of memory.\n");
f0b236
-				return CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+				rval = CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+				goto done;
f0b236
 			}
f0b236
 			buffers[n_buffers] = (unsigned char *) racert;
f0b236
 			lengths[n_buffers] = strlen(racert);
f0b236
@@ -727,7 +737,8 @@ main(int argc, const char **argv)
f0b236
 						 n_buffers + 1);
f0b236
 			if ((buffers == NULL) || (lengths == NULL)) {
f0b236
 				fprintf(stderr, "Out of memory.\n");
f0b236
-				return CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+				rval = CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+				goto done;
f0b236
 			}
f0b236
 			buffers[n_buffers] = (unsigned char *) cacert;
f0b236
 			lengths[n_buffers] = strlen(cacert);
f0b236
@@ -741,7 +752,8 @@ main(int argc, const char **argv)
f0b236
 						 n_buffers + 1);
f0b236
 			if ((buffers == NULL) || (lengths == NULL)) {
f0b236
 				fprintf(stderr, "Out of memory.\n");
f0b236
-				return CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+				rval = CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+				goto done;
f0b236
 			}
f0b236
 			buffers[n_buffers] = (unsigned char *) results;
f0b236
 			lengths[n_buffers] = results_length;
f0b236
@@ -755,7 +767,8 @@ main(int argc, const char **argv)
f0b236
 						 n_buffers + 1);
f0b236
 			if ((buffers == NULL) || (lengths == NULL)) {
f0b236
 				fprintf(stderr, "Out of memory.\n");
f0b236
-				return CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+				rval = CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+				goto done;
f0b236
 			}
f0b236
 			buffers[n_buffers] = (unsigned char *) results2;
f0b236
 			lengths[n_buffers] = results_length2;
f0b236
@@ -850,7 +863,8 @@ main(int argc, const char **argv)
f0b236
 						 n_buffers + 1);
f0b236
 			if ((buffers == NULL) || (lengths == NULL)) {
f0b236
 				fprintf(stderr, "Out of memory.\n");
f0b236
-				return CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+				rval = CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+				goto done;
f0b236
 			}
f0b236
 			buffers[n_buffers] = (unsigned char *) results2;
f0b236
 			lengths[n_buffers] = results_length2;
f0b236
@@ -882,11 +896,11 @@ main(int argc, const char **argv)
f0b236
 					}
f0b236
 				}
f0b236
 			}
f0b236
-			talloc_free(ctx);
f0b236
-			return CM_SUBMIT_STATUS_ISSUED;
f0b236
+			rval = CM_SUBMIT_STATUS_ISSUED;
f0b236
+			goto done;
f0b236
 		} else {
f0b236
-			talloc_free(ctx);
f0b236
-			return CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+			rval = CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+			goto done;
f0b236
 		}
f0b236
 		break;
f0b236
 	case op_get_cert_initial:
f0b236
@@ -957,42 +971,50 @@ main(int argc, const char **argv)
f0b236
 				fprintf(stderr, "%s", s);
f0b236
 				cm_log(1, "%s", s);
f0b236
 				free(s);
f0b236
-				return CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+				rval = CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+				goto done;
f0b236
 			}
f0b236
 			if ((msgtype == NULL) ||
f0b236
 			    (strcmp(msgtype, SCEP_MSGTYPE_CERTREP) != 0)) {
f0b236
 				printf(_("Error: reply was not a CertRep (%s).\n"),
f0b236
 				       msgtype ? msgtype : "none");
f0b236
-				return CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+				rval = CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+				goto done;
f0b236
 			}
f0b236
 			if (tx == NULL) {
f0b236
 				printf(_("Error: reply is missing transactionId.\n"));
f0b236
-				return CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+				rval = CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+				goto done;
f0b236
 			}
f0b236
 			if (sent_tx != NULL) {
f0b236
 				if (strcmp(sent_tx, tx) != 0) {
f0b236
 					printf(_("Error: reply contains a "
f0b236
 						 "different transactionId.\n"));
f0b236
-					return CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+					rval = CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+					goto done;
f0b236
 				}
f0b236
 			}
f0b236
 			if (pkistatus == NULL) {
f0b236
 				printf(_("Error: reply is missing pkiStatus.\n"));
f0b236
-				return CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+				rval = CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+				goto done;
f0b236
 			}
f0b236
 			if (recipient_nonce == NULL) {
f0b236
 				printf(_("Error: reply is missing recipientNonce.\n"));
f0b236
-				return CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+				rval = CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+				goto done;
f0b236
 			}
f0b236
 			if ((recipient_nonce_length != sent_nonce_length) ||
f0b236
 			    (memcmp(recipient_nonce, sent_nonce,
f0b236
 				    sent_nonce_length) != 0)) {
f0b236
 				printf(_("Error: reply nonce doesn't match request.\n"));
f0b236
-				return CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+				rval = CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+				goto done;
f0b236
 			}
f0b236
 			if (sender_nonce == NULL) {
f0b236
 				printf(_("Error: reply is missing senderNonce.\n"));
f0b236
-				return CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+				rval = CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+				goto done;
f0b236
 			}
f0b236
 			if (strcmp(pkistatus, SCEP_PKISTATUS_PENDING) == 0) {
f0b236
 				if (verbose > 0) {
f0b236
@@ -1002,7 +1024,8 @@ main(int argc, const char **argv)
f0b236
 				s = cm_store_base64_from_bin(ctx, sender_nonce,
f0b236
 							     sender_nonce_length);
f0b236
 				printf("%s\n", s);
f0b236
-				return CM_SUBMIT_STATUS_WAIT;
f0b236
+				rval = CM_SUBMIT_STATUS_WAIT;
f0b236
+				goto done;
f0b236
 			} else
f0b236
 			if (strcmp(pkistatus, SCEP_PKISTATUS_FAILURE) == 0) {
f0b236
 				if (verbose > 0) {
f0b236
@@ -1050,7 +1073,8 @@ main(int argc, const char **argv)
f0b236
 					printf(_("Server returned failure code \"%s\".\n"),
f0b236
 					       failinfo);
f0b236
 				}
f0b236
-				return CM_SUBMIT_STATUS_REJECTED;
f0b236
+				rval = CM_SUBMIT_STATUS_REJECTED;
f0b236
+				goto done;
f0b236
 			} else
f0b236
 			if (strcmp(pkistatus, SCEP_PKISTATUS_SUCCESS) == 0) {
f0b236
 				if (verbose > 0) {
f0b236
@@ -1067,7 +1091,8 @@ main(int argc, const char **argv)
f0b236
 					s = cm_submit_u_pem_from_base64("PKCS7", 0, s);
f0b236
 					fprintf(stderr, "Full reply:\n%s", s);
f0b236
 					free(s);
f0b236
-					return CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+					rval = CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+					goto done;
f0b236
 				}
f0b236
 				if (!PKCS7_type_is_enveloped(p7)) {
f0b236
 					printf(_("Error: signed-data payload is not enveloped-data.\n"));
f0b236
@@ -1079,7 +1104,8 @@ main(int argc, const char **argv)
f0b236
 					s = cm_submit_u_pem_from_base64("PKCS7", 0, s);
f0b236
 					fprintf(stderr, "Full reply:\n%s", s);
f0b236
 					free(s);
f0b236
-					return CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+					rval = CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+					goto done;
f0b236
 				}
f0b236
 				if ((p7->d.enveloped == NULL) ||
f0b236
 				    (p7->d.enveloped->enc_data == NULL) ||
f0b236
@@ -1094,29 +1120,42 @@ main(int argc, const char **argv)
f0b236
 					s = cm_submit_u_pem_from_base64("PKCS7", 0, s);
f0b236
 					fprintf(stderr, "Full reply:\n%s", s);
f0b236
 					free(s);
f0b236
-					return CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+					rval = CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+					goto done;
f0b236
 				}
f0b236
 				s = cm_store_base64_from_bin(ctx, payload,
f0b236
 							     payload_length);
f0b236
 				s = cm_submit_u_pem_from_base64("PKCS7", 0, s);
f0b236
 				printf("%s", s);
f0b236
 				free(s);
f0b236
-				return CM_SUBMIT_STATUS_ISSUED;
f0b236
+				rval = CM_SUBMIT_STATUS_ISSUED;
f0b236
+				goto done;
f0b236
 			} else {
f0b236
 				if (verbose > 0) {
f0b236
 					fprintf(stderr, "SCEP status is \"%s\".\n", pkistatus);
f0b236
 				}
f0b236
 				printf(_("Error: pkiStatus \"%s\" not recognized.\n"),
f0b236
 				       pkistatus);
f0b236
-				return CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+				rval = CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+				goto done;
f0b236
 			}
f0b236
 		} else {
f0b236
 			printf(_("Server reply was of unexpected MIME type "
f0b236
 				 "\"%s\".\n"), content_type);
f0b236
 			printf("Full reply:\n%.*s", results_length2, results2);
f0b236
-			return CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+			rval = CM_SUBMIT_STATUS_UNREACHABLE;
f0b236
+			goto done;
f0b236
 		}
f0b236
 		break;
f0b236
 	}
f0b236
-	return CM_SUBMIT_STATUS_UNCONFIGURED;
f0b236
+
f0b236
+done:
f0b236
+	if (pctx) {
f0b236
+		poptFreeContext(pctx);
f0b236
+	}
f0b236
+	free(cainfo);
f0b236
+	free(id);
f0b236
+	cm_submit_h_cleanup(hctx);
f0b236
+	talloc_free(ctx);
f0b236
+	return rval;
f0b236
 }
f0b236
diff --git a/src/submit-h.c b/src/submit-h.c
f0b236
index 33f9b39..9b507db 100644
f0b236
--- a/src/submit-h.c
f0b236
+++ b/src/submit-h.c
f0b236
@@ -298,6 +298,15 @@ cm_submit_h_result_type(struct cm_submit_h_context *ctx)
f0b236
 	return ret;
f0b236
 }
f0b236
 
f0b236
+void
f0b236
+cm_submit_h_cleanup(struct cm_submit_h_context *ctx)
f0b236
+{
f0b236
+
f0b236
+	if (ctx != NULL && ctx->curl != NULL) {
f0b236
+		curl_easy_cleanup(ctx->curl);
f0b236
+	}
f0b236
+}
f0b236
+
f0b236
 #ifdef CM_SUBMIT_H_MAIN
f0b236
 int
f0b236
 main(int argc, const char **argv)
f0b236
@@ -307,7 +316,7 @@ main(int argc, const char **argv)
f0b236
 	enum cm_submit_h_opt_negotiate negotiate;
f0b236
 	enum cm_submit_h_opt_delegate negotiate_delegate;
f0b236
 	enum cm_submit_h_opt_clientauth clientauth;
f0b236
-	int c, fd, l, verbose = 0, length = 0;
f0b236
+	int c, fd, l, verbose = 0, length = 0, rval = 0;
f0b236
 	char *ctype, *accept, *capath, *cainfo, *sslcert, *sslkey, *sslpass;
f0b236
 	char *pinfile;
f0b236
 	const char *method, *url;
f0b236
@@ -423,6 +432,8 @@ main(int argc, const char **argv)
f0b236
 			cm_submit_h_result_code(ctx),
f0b236
 			cm_submit_h_result_code_text(ctx));
f0b236
 	}
f0b236
-	return cm_submit_h_result_code(ctx);
f0b236
+	rval = cm_submit_h_result_code(ctx);
f0b236
+	cm_submit_h_cleanup(ctx);
f0b236
+	return rval;
f0b236
 }
f0b236
 #endif
f0b236
diff --git a/src/submit-h.h b/src/submit-h.h
f0b236
index 1283c53..931cc89 100644
f0b236
--- a/src/submit-h.h
f0b236
+++ b/src/submit-h.h
f0b236
@@ -61,5 +61,6 @@ int cm_submit_h_result_code(struct cm_submit_h_context *ctx);
f0b236
 const char *cm_submit_h_result_code_text(struct cm_submit_h_context *ctx);
f0b236
 const char *cm_submit_h_results(struct cm_submit_h_context *ctx, int *length);
f0b236
 const char *cm_submit_h_result_type(struct cm_submit_h_context *ctx);
f0b236
+void cm_submit_h_cleanup(struct cm_submit_h_context *ctx);
f0b236
 
f0b236
 #endif
f0b236
-- 
f0b236
2.21.1
f0b236