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

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