Blame SOURCES/macsec-0011-mka-Implement-reference-counting-on-data_key.patch

6c9f0c
From 99b82bf53792d48b5d0c3f9edcccc6e53c9510fe Mon Sep 17 00:00:00 2001
6c9f0c
Message-Id: <99b82bf53792d48b5d0c3f9edcccc6e53c9510fe.1488376601.git.dcaratti@redhat.com>
6c9f0c
From: Sabrina Dubroca <sd@queasysnail.net>
6c9f0c
Date: Fri, 21 Oct 2016 14:45:29 +0200
6c9f0c
Subject: [PATCH] mka: Implement reference counting on data_key
6c9f0c
6c9f0c
struct data_key already had a 'user' field for reference counting, but
6c9f0c
it was basically unused.
6c9f0c
6c9f0c
Add an ieee802_1x_kay_use_data_key() function to take a reference on a
6c9f0c
key, and use ieee802_1x_kay_deinit_data_key() to release the reference.
6c9f0c
6c9f0c
Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
6c9f0c
---
6c9f0c
 src/pae/ieee802_1x_kay.c | 28 ++++++++++++++++++++++++----
6c9f0c
 src/pae/ieee802_1x_kay.h |  2 +-
6c9f0c
 2 files changed, 25 insertions(+), 5 deletions(-)
6c9f0c
6c9f0c
diff --git a/src/pae/ieee802_1x_kay.c b/src/pae/ieee802_1x_kay.c
6c9f0c
index e312d04..63bbd13 100644
6c9f0c
--- a/src/pae/ieee802_1x_kay.c
6c9f0c
+++ b/src/pae/ieee802_1x_kay.c
6c9f0c
@@ -411,6 +411,8 @@ ieee802_1x_kay_get_peer_sci(struct ieee802_1x_mka_participant *participant,
6c9f0c
 }
6c9f0c
 
6c9f0c
 
6c9f0c
+static void ieee802_1x_kay_use_data_key(struct data_key *pkey);
6c9f0c
+
6c9f0c
 /**
6c9f0c
  * ieee802_1x_kay_init_receive_sa -
6c9f0c
  */
6c9f0c
@@ -429,6 +431,7 @@ ieee802_1x_kay_init_receive_sa(struct receive_sc *psc, u8 an, u32 lowest_pn,
6c9f0c
 		return NULL;
6c9f0c
 	}
6c9f0c
 
6c9f0c
+	ieee802_1x_kay_use_data_key(key);
6c9f0c
 	psa->pkey = key;
6c9f0c
 	psa->lowest_pn = lowest_pn;
6c9f0c
 	psa->next_pn = lowest_pn;
6c9f0c
@@ -447,11 +450,14 @@ ieee802_1x_kay_init_receive_sa(struct receive_sc *psc, u8 an, u32 lowest_pn,
6c9f0c
 }
6c9f0c
 
6c9f0c
 
6c9f0c
+static void ieee802_1x_kay_deinit_data_key(struct data_key *pkey);
6c9f0c
+
6c9f0c
 /**
6c9f0c
  * ieee802_1x_kay_deinit_receive_sa -
6c9f0c
  */
6c9f0c
 static void ieee802_1x_kay_deinit_receive_sa(struct receive_sa *psa)
6c9f0c
 {
6c9f0c
+	ieee802_1x_kay_deinit_data_key(psa->pkey);
6c9f0c
 	psa->pkey = NULL;
6c9f0c
 	wpa_printf(MSG_DEBUG,
6c9f0c
 		   "KaY: Delete receive SA(an: %hhu) of SC",
6c9f0c
@@ -1612,6 +1618,7 @@ ieee802_1x_mka_decode_dist_sak_body(
6c9f0c
 	sa_key->an = body->dan;
6c9f0c
 	ieee802_1x_kay_init_data_key(sa_key);
6c9f0c
 
6c9f0c
+	ieee802_1x_kay_use_data_key(sa_key);
6c9f0c
 	dl_list_add(&participant->sak_list, &sa_key->list);
6c9f0c
 
6c9f0c
 	ieee802_1x_cp_set_ciphersuite(kay->cp, cs->id);
6c9f0c
@@ -1873,7 +1880,17 @@ static struct mka_param_body_handler mka_body_handler[] = {
6c9f0c
 
6c9f0c
 
6c9f0c
 /**
6c9f0c
- * ieee802_1x_kay_deinit_data_key -
6c9f0c
+ * ieee802_1x_kay_use_data_key - Take reference on a key
6c9f0c
+ */
6c9f0c
+static void ieee802_1x_kay_use_data_key(struct data_key *pkey)
6c9f0c
+{
6c9f0c
+	pkey->user++;
6c9f0c
+}
6c9f0c
+
6c9f0c
+
6c9f0c
+/**
6c9f0c
+ * ieee802_1x_kay_deinit_data_key - Release reference on a key and
6c9f0c
+ * free if there are no remaining users
6c9f0c
  */
6c9f0c
 static void ieee802_1x_kay_deinit_data_key(struct data_key *pkey)
6c9f0c
 {
6c9f0c
@@ -1884,7 +1901,6 @@ static void ieee802_1x_kay_deinit_data_key(struct data_key *pkey)
6c9f0c
 	if (pkey->user > 1)
6c9f0c
 		return;
6c9f0c
 
6c9f0c
-	dl_list_del(&pkey->list);
6c9f0c
 	os_free(pkey->key);
6c9f0c
 	os_free(pkey);
6c9f0c
 }
6c9f0c
@@ -1994,7 +2010,9 @@ ieee802_1x_kay_generate_new_sak(struct ieee802_1x_mka_participant *participant)
6c9f0c
 
6c9f0c
 	participant->new_key = sa_key;
6c9f0c
 
6c9f0c
+	ieee802_1x_kay_use_data_key(sa_key);
6c9f0c
 	dl_list_add(&participant->sak_list, &sa_key->list);
6c9f0c
+
6c9f0c
 	ieee802_1x_cp_set_ciphersuite(kay->cp, cs->id);
6c9f0c
 	ieee802_1x_cp_sm_step(kay->cp);
6c9f0c
 	ieee802_1x_cp_set_offset(kay->cp, kay->macsec_confidentiality);
6c9f0c
@@ -2436,6 +2454,7 @@ ieee802_1x_kay_init_transmit_sa(struct transmit_sc *psc, u8 an, u32 next_PN,
6c9f0c
 		psa->confidentiality = FALSE;
6c9f0c
 
6c9f0c
 	psa->an = an;
6c9f0c
+	ieee802_1x_kay_use_data_key(key);
6c9f0c
 	psa->pkey = key;
6c9f0c
 	psa->next_pn = next_PN;
6c9f0c
 	psa->sc = psc;
6c9f0c
@@ -2457,6 +2476,7 @@ ieee802_1x_kay_init_transmit_sa(struct transmit_sc *psc, u8 an, u32 next_PN,
6c9f0c
  */
6c9f0c
 static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa)
6c9f0c
 {
6c9f0c
+	ieee802_1x_kay_deinit_data_key(psa->pkey);
6c9f0c
 	psa->pkey = NULL;
6c9f0c
 	wpa_printf(MSG_DEBUG,
6c9f0c
 		   "KaY: Delete transmit SA(an: %hhu) of SC",
6c9f0c
@@ -2708,6 +2728,7 @@ int ieee802_1x_kay_delete_sas(struct ieee802_1x_kay *kay,
6c9f0c
 	dl_list_for_each_safe(sa_key, pre_key, &principal->sak_list,
6c9f0c
 			      struct data_key, list) {
6c9f0c
 		if (is_ki_equal(&sa_key->key_identifier, ki)) {
6c9f0c
+			dl_list_del(&sa_key->list);
6c9f0c
 			ieee802_1x_kay_deinit_data_key(sa_key);
6c9f0c
 			break;
6c9f0c
 		}
6c9f0c
@@ -3375,8 +3396,7 @@ ieee802_1x_kay_delete_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn)
6c9f0c
 		sak = dl_list_entry(participant->sak_list.next,
6c9f0c
 				    struct data_key, list);
6c9f0c
 		dl_list_del(&sak->list);
6c9f0c
-		os_free(sak->key);
6c9f0c
-		os_free(sak);
6c9f0c
+		ieee802_1x_kay_deinit_data_key(sak);
6c9f0c
 	}
6c9f0c
 	while (!dl_list_empty(&participant->rxsc_list)) {
6c9f0c
 		rxsc = dl_list_entry(participant->rxsc_list.next,
6c9f0c
diff --git a/src/pae/ieee802_1x_kay.h b/src/pae/ieee802_1x_kay.h
6c9f0c
index 5233cb2..576a8a0 100644
6c9f0c
--- a/src/pae/ieee802_1x_kay.h
6c9f0c
+++ b/src/pae/ieee802_1x_kay.h
6c9f0c
@@ -70,7 +70,7 @@ struct data_key {
6c9f0c
 	Boolean rx_latest;
6c9f0c
 	Boolean tx_latest;
6c9f0c
 
6c9f0c
-	int user;  /* FIXME: to indicate if it can be delete safely */
6c9f0c
+	int user;
6c9f0c
 
6c9f0c
 	struct dl_list list;
6c9f0c
 };
6c9f0c
-- 
6c9f0c
2.7.4
6c9f0c