109a78
From 6b522f8780813726799e6b8cf0f1f8e0ce2c8ebf Mon Sep 17 00:00:00 2001
109a78
From: Mathy Vanhoef <Mathy.Vanhoef@nyu.edu>
109a78
Date: Fri, 4 Oct 2019 17:53:52 +0400
109a78
Subject: [PATCH] EAP-pwd: fix DoS due to multithreaded BN_CTX access
109a78
109a78
The EAP-pwd module created one global OpenSSL BN_CTX instance, and
109a78
used this instance in all incoming requests. This means that different
109a78
threads used the same BN_CTX instance, which can result in a crash.
109a78
An adversary can trigger these crashes by concurrently initiating
109a78
multiple EAP-pwd handshakes from different clients.
109a78
109a78
Fix this bug by creating a separate BN_CTX instance for each request.
109a78
---
109a78
 .../rlm_eap/types/rlm_eap_pwd/eap_pwd.h       |  1 +
109a78
 .../rlm_eap/types/rlm_eap_pwd/rlm_eap_pwd.c   | 24 +++++++++----------
109a78
 .../rlm_eap/types/rlm_eap_pwd/rlm_eap_pwd.h   |  2 --
109a78
 3 files changed, 13 insertions(+), 14 deletions(-)
109a78
109a78
diff --git a/src/modules/rlm_eap/types/rlm_eap_pwd/eap_pwd.h b/src/modules/rlm_eap/types/rlm_eap_pwd/eap_pwd.h
109a78
index 013a6e7992..ca12778f61 100644
109a78
--- a/src/modules/rlm_eap/types/rlm_eap_pwd/eap_pwd.h
109a78
+++ b/src/modules/rlm_eap/types/rlm_eap_pwd/eap_pwd.h
109a78
@@ -90,6 +90,7 @@ typedef struct _pwd_session_t {
109a78
     uint8_t *out;     /* message to fragment */
109a78
     size_t out_pos;
109a78
     size_t out_len;
109a78
+    BN_CTX *bnctx;
109a78
     EC_GROUP *group;
109a78
     EC_POINT *pwe;
109a78
     BIGNUM *order;
109a78
diff --git a/src/modules/rlm_eap/types/rlm_eap_pwd/rlm_eap_pwd.c b/src/modules/rlm_eap/types/rlm_eap_pwd/rlm_eap_pwd.c
109a78
index 76cc57023e..eefca985d7 100644
109a78
--- a/src/modules/rlm_eap/types/rlm_eap_pwd/rlm_eap_pwd.c
109a78
+++ b/src/modules/rlm_eap/types/rlm_eap_pwd/rlm_eap_pwd.c
109a78
@@ -55,8 +55,6 @@ static int mod_detach (void *arg)
109a78
 
109a78
 	inst = (eap_pwd_t *) arg;
109a78
 
109a78
-	if (inst->bnctx) BN_CTX_free(inst->bnctx);
109a78
-
109a78
 	return 0;
109a78
 }
109a78
 
109a78
@@ -76,11 +74,6 @@ static int mod_instantiate (CONF_SECTION *cs, void **instance)
109a78
 		return -1;
109a78
 	}
109a78
 
109a78
-	if ((inst->bnctx = BN_CTX_new()) == NULL) {
109a78
-		cf_log_err_cs(cs, "Failed to get BN context");
109a78
-		return -1;
109a78
-	}
109a78
-
109a78
 	return 0;
109a78
 }
109a78
 
109a78
@@ -96,6 +89,7 @@ static int _free_pwd_session (pwd_session_t *session)
109a78
 	EC_POINT_clear_free(session->pwe);
109a78
 	BN_clear_free(session->order);
109a78
 	BN_clear_free(session->prime);
109a78
+	BN_CTX_free(session->bnctx);
109a78
 
109a78
 	return 0;
109a78
 }
109a78
@@ -217,6 +211,12 @@ static int mod_session_init (void *instance, eap_handler_t *handler)
109a78
 	session->order = NULL;
109a78
 	session->prime = NULL;
109a78
 
109a78
+	session->bnctx = BN_CTX_new();
109a78
+	if (session->bnctx == NULL) {
109a78
+		ERROR("rlm_eap_pwd: Failed to get BN context");
109a78
+		return 0;
109a78
+	}
109a78
+
109a78
 	/*
109a78
 	 *	The admin can dynamically change the MTU.
109a78
 	 */
109a78
@@ -496,7 +496,7 @@ static int mod_process(void *arg, eap_handler_t *handler)
109a78
 		/*
109a78
 		 * compute our scalar and element
109a78
 		 */
109a78
-		if (compute_scalar_element(session, inst->bnctx)) {
109a78
+		if (compute_scalar_element(session, session->bnctx)) {
109a78
 			DEBUG2("failed to compute server's scalar and element");
109a78
 			return 0;
109a78
 		}
109a78
@@ -508,7 +508,7 @@ static int mod_process(void *arg, eap_handler_t *handler)
109a78
 		 * element is a point, get both coordinates: x and y
109a78
 		 */
109a78
 		if (!EC_POINT_get_affine_coordinates_GFp(session->group, session->my_element, x, y,
109a78
-							 inst->bnctx)) {
109a78
+							 session->bnctx)) {
109a78
 			DEBUG2("server point assignment failed");
109a78
 			BN_clear_free(x);
109a78
 			BN_clear_free(y);
109a78
@@ -552,7 +552,7 @@ static int mod_process(void *arg, eap_handler_t *handler)
109a78
 		/*
109a78
 		 * process the peer's commit and generate the shared key, k
109a78
 		 */
109a78
-		if (process_peer_commit(session, in, in_len, inst->bnctx)) {
109a78
+		if (process_peer_commit(session, in, in_len, session->bnctx)) {
109a78
 			RDEBUG2("failed to process peer's commit");
109a78
 			return 0;
109a78
 		}
109a78
@@ -560,7 +560,7 @@ static int mod_process(void *arg, eap_handler_t *handler)
109a78
 		/*
109a78
 		 * compute our confirm blob
109a78
 		 */
109a78
-		if (compute_server_confirm(session, session->my_confirm, inst->bnctx)) {
109a78
+		if (compute_server_confirm(session, session->my_confirm, session->bnctx)) {
109a78
 			ERROR("rlm_eap_pwd: failed to compute confirm!");
109a78
 			return 0;
109a78
 		}
109a78
@@ -591,7 +591,7 @@ static int mod_process(void *arg, eap_handler_t *handler)
109a78
 			RDEBUG2("pwd exchange is incorrect: not commit!");
109a78
 			return 0;
109a78
 		}
109a78
-		if (compute_peer_confirm(session, peer_confirm, inst->bnctx)) {
109a78
+		if (compute_peer_confirm(session, peer_confirm, session->bnctx)) {
109a78
 			RDEBUG2("pwd exchange cannot compute peer's confirm");
109a78
 			return 0;
109a78
 		}
109a78
diff --git a/src/modules/rlm_eap/types/rlm_eap_pwd/rlm_eap_pwd.h b/src/modules/rlm_eap/types/rlm_eap_pwd/rlm_eap_pwd.h
109a78
index 189530d066..2264566bb6 100644
109a78
--- a/src/modules/rlm_eap/types/rlm_eap_pwd/rlm_eap_pwd.h
109a78
+++ b/src/modules/rlm_eap/types/rlm_eap_pwd/rlm_eap_pwd.h
109a78
@@ -40,8 +40,6 @@
109a78
 #include <freeradius-devel/modules.h>
109a78
 
109a78
 typedef struct _eap_pwd_t {
109a78
-    BN_CTX *bnctx;
109a78
-
109a78
     uint32_t	group;
109a78
     uint32_t	fragment_size;
109a78
     char const	*server_id;