Blame SOURCES/rh1489919-mka-Add-error-handling-for-secy_init_macsec-calls.patch

41389a
From 7612e65b9bdfe03e5a018e3c897f4a3292c42ee4 Mon Sep 17 00:00:00 2001
41389a
Message-Id: <7612e65b9bdfe03e5a018e3c897f4a3292c42ee4.1506941240.git.davide.caratti@gmail.com>
41389a
From: Sabrina Dubroca <sd@queasysnail.net>
41389a
Date: Tue, 22 Aug 2017 10:34:19 +0200
41389a
Subject: [PATCH] mka: Add error handling for secy_init_macsec() calls
41389a
41389a
secy_init_macsec() can fail (if ->macsec_init fails), and
41389a
ieee802_1x_kay_init() should handle this and not let MKA run any
41389a
further, because nothing is going to work anyway.
41389a
41389a
On failure, ieee802_1x_kay_init() must deinit its kay, which will free
41389a
kay->ctx, so ieee802_1x_kay_init callers (only ieee802_1x_alloc_kay_sm)
41389a
must not do it. Before this patch there is a double-free of the ctx
41389a
argument when ieee802_1x_kay_deinit() was called.
41389a
41389a
Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
41389a
---
41389a
 src/pae/ieee802_1x_kay.c  | 25 ++++++++++++++-----------
41389a
 wpa_supplicant/wpas_kay.c |  5 ++---
41389a
 2 files changed, 16 insertions(+), 14 deletions(-)
41389a
41389a
diff --git a/src/pae/ieee802_1x_kay.c b/src/pae/ieee802_1x_kay.c
41389a
index ff55f88b8..c4bfcbc63 100644
41389a
--- a/src/pae/ieee802_1x_kay.c
41389a
+++ b/src/pae/ieee802_1x_kay.c
41389a
@@ -3100,6 +3100,7 @@ ieee802_1x_kay_init(struct ieee802_1x_kay_ctx *ctx, enum macsec_policy policy,
41389a
 	kay = os_zalloc(sizeof(*kay));
41389a
 	if (!kay) {
41389a
 		wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
41389a
+		os_free(ctx);
41389a
 		return NULL;
41389a
 	}
41389a
 
41389a
@@ -3134,10 +3135,8 @@ ieee802_1x_kay_init(struct ieee802_1x_kay_ctx *ctx, enum macsec_policy policy,
41389a
 	dl_list_init(&kay->participant_list);
41389a
 
41389a
 	if (policy != DO_NOT_SECURE &&
41389a
-	    secy_get_capability(kay, &kay->macsec_capable) < 0) {
41389a
-		os_free(kay);
41389a
-		return NULL;
41389a
-	}
41389a
+	    secy_get_capability(kay, &kay->macsec_capable) < 0)
41389a
+		goto error;
41389a
 
41389a
 	if (policy == DO_NOT_SECURE ||
41389a
 	    kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
41389a
@@ -3164,16 +3163,17 @@ ieee802_1x_kay_init(struct ieee802_1x_kay_ctx *ctx, enum macsec_policy policy,
41389a
 	wpa_printf(MSG_DEBUG, "KaY: state machine created");
41389a
 
41389a
 	/* Initialize the SecY must be prio to CP, as CP will control SecY */
41389a
-	secy_init_macsec(kay);
41389a
+	if (secy_init_macsec(kay) < 0) {
41389a
+		wpa_printf(MSG_DEBUG, "KaY: Could not initialize MACsec");
41389a
+		goto error;
41389a
+	}
41389a
 
41389a
 	wpa_printf(MSG_DEBUG, "KaY: secy init macsec done");
41389a
 
41389a
 	/* init CP */
41389a
 	kay->cp = ieee802_1x_cp_sm_init(kay);
41389a
-	if (kay->cp == NULL) {
41389a
-		ieee802_1x_kay_deinit(kay);
41389a
-		return NULL;
41389a
-	}
41389a
+	if (kay->cp == NULL)
41389a
+		goto error;
41389a
 
41389a
 	if (policy == DO_NOT_SECURE) {
41389a
 		ieee802_1x_cp_connect_authenticated(kay->cp);
41389a
@@ -3184,12 +3184,15 @@ ieee802_1x_kay_init(struct ieee802_1x_kay_ctx *ctx, enum macsec_policy policy,
41389a
 		if (kay->l2_mka == NULL) {
41389a
 			wpa_printf(MSG_WARNING,
41389a
 				   "KaY: Failed to initialize L2 packet processing for MKA packet");
41389a
-			ieee802_1x_kay_deinit(kay);
41389a
-			return NULL;
41389a
+			goto error;
41389a
 		}
41389a
 	}
41389a
 
41389a
 	return kay;
41389a
+
41389a
+error:
41389a
+	ieee802_1x_kay_deinit(kay);
41389a
+	return NULL;
41389a
 }
41389a
 
41389a
 
41389a
diff --git a/wpa_supplicant/wpas_kay.c b/wpa_supplicant/wpas_kay.c
41389a
index d087e00ad..587e5c3dd 100644
41389a
--- a/wpa_supplicant/wpas_kay.c
41389a
+++ b/wpa_supplicant/wpas_kay.c
41389a
@@ -235,10 +235,9 @@ int ieee802_1x_alloc_kay_sm(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
41389a
 	res = ieee802_1x_kay_init(kay_ctx, policy, ssid->macsec_port,
41389a
 				  ssid->mka_priority, wpa_s->ifname,
41389a
 				  wpa_s->own_addr);
41389a
-	if (res == NULL) {
41389a
-		os_free(kay_ctx);
41389a
+	/* ieee802_1x_kay_init() frees kay_ctx on failure */
41389a
+	if (res == NULL)
41389a
 		return -1;
41389a
-	}
41389a
 
41389a
 	wpa_s->kay = res;
41389a
 
41389a
-- 
41389a
2.13.5
41389a