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

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