Submitted By:            Douglas R. Reno <renodr at linuxfromscratch dot org>
Date:                    2021-12-26
Initial Package Version: 2.9
Origin:                  Upstream (See BLFS Ticket #15851)
Upstream Status:         Applied
Description:             Fixes six security vulnerabilities that could allow for
                         decryption of SSID connection information, crashes, and
                         content spoofing. One of these affects all devices that
                         use the UPnP standard.

diff -Naurp wpa_supplicant-2.9.orig/src/ap/drv_callbacks.c wpa_supplicant-2.9/src/ap/drv_callbacks.c
--- wpa_supplicant-2.9.orig/src/ap/drv_callbacks.c	2019-08-07 08:25:25.000000000 -0500
+++ wpa_supplicant-2.9/src/ap/drv_callbacks.c	2021-12-26 11:52:21.099419242 -0600
@@ -131,6 +131,21 @@ int hostapd_notif_assoc(struct hostapd_d
 			   "hostapd_notif_assoc: Skip event with no address");
 		return -1;
 	}
+
+   if (is_multicast_ether_addr(addr) ||
+       is_zero_ether_addr(addr) ||
+       os_memcmp(addr, hapd->own_addr, ETH_ALEN) == 0) {
+      /* 
+       * Do not process any frames with unexpected/invalid SA so that
+       * we do not add any state for unexpected STA addresses or end
+       * up sending out frames to unexpected destinations.
+       */
+      wpa_printf(MSG_DEBUG, "%s: Invalid SA=" MACSTR 
+            " in received indication - ignore this indication silently",
+            __func__, MAC2STR(addr));
+      return 0;
+   }
+
 	random_add_randomness(addr, ETH_ALEN);
 
 	hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
diff -Naurp wpa_supplicant-2.9.orig/src/ap/ieee802_11.c wpa_supplicant-2.9/src/ap/ieee802_11.c
--- wpa_supplicant-2.9.orig/src/ap/ieee802_11.c	2019-08-07 08:25:25.000000000 -0500
+++ wpa_supplicant-2.9/src/ap/ieee802_11.c	2021-12-26 11:54:01.716967200 -0600
@@ -4626,6 +4626,20 @@ int ieee802_11_mgmt(struct hostapd_data
 	fc = le_to_host16(mgmt->frame_control);
 	stype = WLAN_FC_GET_STYPE(fc);
 
+   if (is_multicast_ether_addr(mgmt->sa) ||
+       is_zero_ether_addr(mgmt->sa) ||
+       os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
+      /*
+       * Do not process any frames with unexpected/invalid SA so that
+       * we do not add any state for unexpected STA addresses or end
+       * up sending out frames to unexpected destinations.
+       */
+      wpa_printf(MSG_DEBUG, "MGMT: Invalid SA=" MACSTR
+                 " in received frame - ignore this frame silently",
+                 MAC2STR(mgmt->sa));
+      return 0;
+   }
+
 	if (stype == WLAN_FC_STYPE_BEACON) {
 		handle_beacon(hapd, mgmt, len, fi);
 		return 1;
diff -Naurp wpa_supplicant-2.9.orig/src/p2p/p2p.c wpa_supplicant-2.9/src/p2p/p2p.c
--- wpa_supplicant-2.9.orig/src/p2p/p2p.c	2019-08-07 08:25:25.000000000 -0500
+++ wpa_supplicant-2.9/src/p2p/p2p.c	2021-12-26 12:59:33.526046188 -0600
@@ -453,6 +453,8 @@ static void p2p_copy_client_info(struct
 	dev->info.config_methods = cli->config_methods;
 	os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8);
 	dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types;
+   if (dev->info.wps_sec_dev_type_list_len > WPS_SEC_DEV_TYPE_MAX_LEN)
+       dev->info.wps_sec_dev_type_list_len = WPS_SEC_DEV_TYPE_MAX_LEN;
 	os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types,
 		  dev->info.wps_sec_dev_type_list_len);
 }
diff -Naurp wpa_supplicant-2.9.orig/src/p2p/p2p_pd.c wpa_supplicant-2.9/src/p2p/p2p_pd.c
--- wpa_supplicant-2.9.orig/src/p2p/p2p_pd.c	2019-08-07 08:25:25.000000000 -0500
+++ wpa_supplicant-2.9/src/p2p/p2p_pd.c	2021-12-26 13:00:34.846750392 -0600
@@ -595,14 +595,12 @@ void p2p_process_prov_disc_req(struct p2
 			goto out;
 		}
 
+      dev = p2p_get_device(p2p, sa);
 		if (!dev) {
-			dev = p2p_get_device(p2p, sa);
-			if (!dev) {
-				p2p_dbg(p2p,
-					"Provision Discovery device not found "
-					MACSTR, MAC2STR(sa));
-				goto out;
-			}
+			p2p_dbg(p2p,
+                 "Provision Discovery device not found " 
+                 MACSTR, MAC2STR(sa));
+         goto out;
 		}
 	} else if (msg.wfd_subelems) {
 		wpabuf_free(dev->info.wfd_subelems);
diff -Naurp wpa_supplicant-2.9.orig/src/tls/pkcs1.c wpa_supplicant-2.9/src/tls/pkcs1.c
--- wpa_supplicant-2.9.orig/src/tls/pkcs1.c	2019-08-07 08:25:25.000000000 -0500
+++ wpa_supplicant-2.9/src/tls/pkcs1.c	2021-12-26 13:03:50.428806694 -0600
@@ -241,6 +241,9 @@ int pkcs1_v15_sig_ver(struct crypto_publ
 		return -1;
 	}
 
+   wpa_hexdump(MSG_MSGDUMP, "PCKS #1: DigestInfo",
+               hdr.payload, hdr.length);
+
 	pos = hdr.payload;
 	end = pos + hdr.length;
 
@@ -261,6 +264,8 @@ int pkcs1_v15_sig_ver(struct crypto_publ
 		os_free(decrypted);
 		return -1;
 	}
+   wpa_hexdump(MSG_MSGDUMP, "PKCS #1: DigestAlgorithmIdentifier",
+               hdr.payload, hdr.length);
 	da_end = hdr.payload + hdr.length;
 
 	if (asn1_get_oid(hdr.payload, hdr.length, &oid, &next)) {
@@ -269,6 +274,23 @@ int pkcs1_v15_sig_ver(struct crypto_publ
 		os_free(decrypted);
 		return -1;
 	}
+   wpa_hexdump(MSG_MSGDUMP, "PKCS #1: Digest algorithm parameters",
+               next, da_end - next);
+
+   /*
+    * RFC 5754: The correct encoding for the SHA2 algorithms would be to
+    * omit the parameters, but there are implementations that encode these
+    * as a NULL element. Allow these two cases and reject everything else.
+    */
+   if (da_end > next &&
+       (asn1_get_next(next, da_end - next, &hdr) < 0 ||
+        !asn1_is_null(&hdr) ||
+        hdr.payload + hdr.length != da_end)) {
+         wpa_printf(MSG_DEBUG,
+                    "PKCS #1: Unexpected digest algorithm parameters");
+         os_free(decrypted);
+         return -1;
+   }
 
 	if (!asn1_oid_equal(&oid, hash_alg)) {
 		char txt[100], txt2[100];
diff -Naurp wpa_supplicant-2.9.orig/src/tls/x509v3.c wpa_supplicant-2.9/src/tls/x509v3.c
--- wpa_supplicant-2.9.orig/src/tls/x509v3.c	2019-08-07 08:25:25.000000000 -0500
+++ wpa_supplicant-2.9/src/tls/x509v3.c	2021-12-26 13:08:19.526507762 -0600
@@ -1834,6 +1834,7 @@ int x509_check_signature(struct x509_cer
 		os_free(data);
 		return -1;
 	}
+   wpa_hexdump(MSG_MSGDUMP, "X509: DigestInfo", hdr.payload, hdr.length);
 
 	pos = hdr.payload;
 	end = pos + hdr.length;
@@ -1855,6 +1856,8 @@ int x509_check_signature(struct x509_cer
 		os_free(data);
 		return -1;
 	}
+   wpa_hexdump(MSG_MSGDUMP, "X509: DigestAlgorithmIdentifier",
+               hdr.payload, hdr.length);
 	da_end = hdr.payload + hdr.length;
 
 	if (asn1_get_oid(hdr.payload, hdr.length, &oid, &next)) {
@@ -1863,6 +1866,24 @@ int x509_check_signature(struct x509_cer
 		return -1;
 	}
 
+   wpa_hexdump(MSG_MSGDUMP, "X509: Digest algorithm parameters",
+               next, da_end - next);
+
+   /*
+    * RFC 5754: The correct encoding for the SHA2 algorithms would be to
+    * omit the parameters, but there are implementations that encode these
+    * as a NULL element. Allow these two cases and reject everything else.
+    */
+   if (da_end > next &&
+       (asn1_get_next(next, da_end - next, &hdr) < 0 ||
+        !asn1_is_null(&hdr) ||
+        hdr.payload + hdr.length != da_end)) {
+      wpa_printf(MSG_DEBUG,
+                 "X509: Unexpected digest algorithm parameters");
+      os_free(data);
+      return -1;
+   }
+
 	if (x509_sha1_oid(&oid)) {
 		if (signature->oid.oid[6] != 5 /* sha-1WithRSAEncryption */) {
 			wpa_printf(MSG_DEBUG, "X509: digestAlgorithm SHA1 "
diff -Naurp wpa_supplicant-2.9.orig/src/wps/wps_er.c wpa_supplicant-2.9/src/wps/wps_er.c
--- wpa_supplicant-2.9.orig/src/wps/wps_er.c	2019-08-07 08:25:25.000000000 -0500
+++ wpa_supplicant-2.9/src/wps/wps_er.c	2021-12-26 11:54:59.682706182 -0600
@@ -1298,7 +1298,7 @@ wps_er_init(struct wps_context *wps, con
 			   "with %s", filter);
 	}
 	if (get_netif_info(er->ifname, &er->ip_addr, &er->ip_addr_text,
-			   er->mac_addr)) {
+			   NULL, er->mac_addr)) {
 		wpa_printf(MSG_INFO, "WPS UPnP: Could not get IP/MAC address "
 			   "for %s. Does it have IP address?", er->ifname);
 		wps_er_deinit(er, NULL, NULL);
diff -Naurp wpa_supplicant-2.9.orig/src/wps/wps_upnp.c wpa_supplicant-2.9/src/wps/wps_upnp.c
--- wpa_supplicant-2.9.orig/src/wps/wps_upnp.c	2019-08-07 08:25:25.000000000 -0500
+++ wpa_supplicant-2.9/src/wps/wps_upnp.c	2021-12-26 12:09:34.937981976 -0600
@@ -302,6 +302,12 @@ static void subscr_addr_free_all(struct
 	}
 }
 
+static int local_network_addr(struct upnp_wps_device_sm *sm,
+                              struct sockaddr_in *addr)
+{
+   return (addr->sin_addr.s_addr & sm->netmask.s_addr) ==
+          (sm->ip_addr & sm->netmask.s_addr);
+}
 
 /* subscr_addr_add_url -- add address(es) for one url to subscription */
 static void subscr_addr_add_url(struct subscription *s, const char *url,
@@ -320,9 +326,16 @@ static void subscr_addr_add_url(struct s
 	int rerr;
 	size_t host_len, path_len;
 
-	/* url MUST begin with http: */
-	if (url_len < 7 || os_strncasecmp(url, "http://", 7))
+	/* 
+    * URL MUST begin with a HTTP scheme. In addition, limit the length of
+    * the URL to 700 characters which is around the limit that was 
+    * implicitly enforced for more than 10 years due to a bug in
+    * generating the event messages.
+    */
+   if (url_len < 7 || os_strncasecmp(url, "http://", 7) || url_len > 700) {
+      wpa_printf(MSG_DEBUG, "WPS UPnP: Reject an unacceptable URL");
 		goto fail;
+   }
 	url += 7;
 	url_len -= 7;
 
@@ -381,6 +394,7 @@ static void subscr_addr_add_url(struct s
 
 	for (rp = result; rp; rp = rp->ai_next) {
 		struct subscr_addr *a;
+      struct sockaddr_in *addr = (struct sockaddr_in *) rp->ai_addr;`
 
 		/* Limit no. of address to avoid denial of service attack */
 		if (dl_list_len(&s->addr_list) >= MAX_ADDR_PER_SUBSCRIPTION) {
@@ -389,6 +403,13 @@ static void subscr_addr_add_url(struct s
 			break;
 		}
 
+      if (!local_network_addr(s->sm, addr)) {
+         wpa_printf(MSG_INFO,
+                    "WPS UPnP: Ignore a delivery URL that points to another network %s",
+                    inet_ntoa(addr->sin_addr));
+         continue;
+      }
+
 		a = os_zalloc(sizeof(*a) + alloc_len);
 		if (a == NULL)
 			break;
@@ -889,11 +910,12 @@ static int eth_get(const char *device, u
  * @net_if: Selected network interface name
  * @ip_addr: Buffer for returning IP address in network byte order
  * @ip_addr_text: Buffer for returning a pointer to allocated IP address text
+ * @netmask: Buffer for returning netmask or %NULL if not needed
  * @mac: Buffer for returning MAC address
  * Returns: 0 on success, -1 on failure
  */
 int get_netif_info(const char *net_if, unsigned *ip_addr, char **ip_addr_text,
-		   u8 mac[ETH_ALEN])
+		   struct in_addr *netmask, u8 mac[ETH_ALEN])
 {
 	struct ifreq req;
 	int sock = -1;
@@ -919,6 +941,19 @@ int get_netif_info(const char *net_if, u
 	in_addr.s_addr = *ip_addr;
 	os_snprintf(*ip_addr_text, 16, "%s", inet_ntoa(in_addr));
 
+   if (netmask) {
+      os_memset(&req, 0, sizeof(req));
+      os_strlcpy(req.ifr_name, net_if, sizeof(req.ifr_name));
+      if (ioctl(sock, SIOCGIFNETMASK, &req) < 0) {
+         wpa_printf(MSG_ERROR,
+               "WPS UPnP: SIOCGIFNETMASK failed: %d (%s)",
+               errno, strerror(errno));
+         goto fail;
+      }
+      addr = (struct sockaddr_in *) &req.ifr_netmask;
+      netmask->s_addr = addr->sin_addr.s_addr;
+   }
+
 #ifdef __linux__
 	os_strlcpy(req.ifr_name, net_if, sizeof(req.ifr_name));
 	if (ioctl(sock, SIOCGIFHWADDR, &req) < 0) {
@@ -1025,11 +1060,15 @@ static int upnp_wps_device_start(struct
 
 	/* Determine which IP and mac address we're using */
 	if (get_netif_info(net_if, &sm->ip_addr, &sm->ip_addr_text,
-			   sm->mac_addr)) {
+			   &sm->netmask, sm->mac_addr)) {
 		wpa_printf(MSG_INFO, "WPS UPnP: Could not get IP/MAC address "
 			   "for %s. Does it have IP address?", net_if);
 		goto fail;
 	}
+   wpa_printf(MSG_DEBUG, "WPS UPnP: Local IP address %s netmask %s hwaddr "
+              MACSTR,
+              sm->ip_addr_text, inet_ntoa(sm->netmask),
+              MAC2STR(sm->mac_addr));
 
 	/* Listen for incoming TCP connections so that others
 	 * can fetch our "xml files" from us.
diff -Naurp wpa_supplicant-2.9.orig/src/wps/wps_upnp_event.c wpa_supplicant-2.9/src/wps/wps_upnp_event.c
--- wpa_supplicant-2.9.orig/src/wps/wps_upnp_event.c	2019-08-07 08:25:25.000000000 -0500
+++ wpa_supplicant-2.9/src/wps/wps_upnp_event.c	2021-12-26 12:58:28.374360399 -0600
@@ -147,7 +147,8 @@ static struct wpabuf * event_build_messa
 	struct wpabuf *buf;
 	char *b;
 
-	buf = wpabuf_alloc(1000 + wpabuf_len(e->data));
+	buf = wpabuf_alloc(1000 + os_strlen(e->addr->path) + 
+                      wpabuf_len(e->data));
 	if (buf == NULL)
 		return NULL;
 	wpabuf_printf(buf, "NOTIFY %s HTTP/1.1\r\n", e->addr->path);
@@ -293,7 +294,7 @@ static int event_send_start(struct subsc
 
 	buf = event_build_message(e);
 	if (buf == NULL) {
-		event_retry(e, 0);
+		event_addr_failure(e);
 		return -1;
 	}
 
@@ -301,7 +302,7 @@ static int event_send_start(struct subsc
 					 event_http_cb, e);
 	if (e->http_event == NULL) {
 		wpabuf_free(buf);
-		event_retry(e, 0);
+		event_addr_failure(e);
 		return -1;
 	}
 
diff -Naurp wpa_supplicant-2.9.orig/src/wps/wps_upnp_i.h wpa_supplicant-2.9/src/wps/wps_upnp_i.h
--- wpa_supplicant-2.9.orig/src/wps/wps_upnp_i.h	2019-08-07 08:25:25.000000000 -0500
+++ wpa_supplicant-2.9/src/wps/wps_upnp_i.h	2021-12-26 12:02:02.808791086 -0600
@@ -128,6 +128,7 @@ struct upnp_wps_device_sm {
 	u8 mac_addr[ETH_ALEN]; /* mac addr of network i.f. we use */
 	char *ip_addr_text; /* IP address of network i.f. we use */
 	unsigned ip_addr; /* IP address of network i.f. we use (host order) */
+   struct in_addr netmask;
 	int multicast_sd; /* send multicast messages over this socket */
 	int ssdp_sd; /* receive discovery UPD packets on socket */
 	int ssdp_sd_registered; /* nonzero if we must unregister */
@@ -158,7 +159,7 @@ struct subscription * subscription_find(
 					const u8 uuid[UUID_LEN]);
 void subscr_addr_delete(struct subscr_addr *a);
 int get_netif_info(const char *net_if, unsigned *ip_addr, char **ip_addr_text,
-		   u8 mac[ETH_ALEN]);
+		   struct in_addr *netmask, u8 mac[ETH_ALEN]);
 
 /* wps_upnp_ssdp.c */
 void msearchreply_state_machine_stop(struct advertisement_state_machine *a);
diff -Naurp wpa_supplicant-2.9.orig/wpa_supplicant/ctrl_iface.h wpa_supplicant-2.9/wpa_supplicant/ctrl_iface.h
--- wpa_supplicant-2.9.orig/wpa_supplicant/ctrl_iface.h	2019-08-07 08:25:25.000000000 -0500
+++ wpa_supplicant-2.9/wpa_supplicant/ctrl_iface.h	2021-12-26 13:10:12.777961000 -0600
@@ -66,14 +66,17 @@ wpa_supplicant_ctrl_iface_init(struct wp
 
 /**
  * wpa_supplicant_ctrl_iface_deinit - Deinitialize control interface
+ * @wpa_s: Pointer to wpa_supplicant data
  * @priv: Pointer to private data from wpa_supplicant_ctrl_iface_init()
  *
  * Deinitialize the control interface that was initialized with
- * wpa_supplicant_ctrl_iface_init().
+ * wpa_supplicant_ctrl_iface_init() and any data related to the wpa_s instance.
+ * @priv may be %NULL if the control interface has not yet been initialized.
  *
  * Required to be implemented in each control interface backend.
  */
-void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv);
+void wpa_supplicant_ctrl_iface_deinit(struct wpa_supplicant *wpa_s,
+                                      struct ctrl_iface_priv *priv);
 
 /**
  * wpa_supplicant_ctrl_iface_wait - Wait for ctrl_iface monitor
@@ -124,7 +127,8 @@ wpa_supplicant_ctrl_iface_init(struct wp
 }
 
 static inline void
-wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
+wpa_supplicant_ctrl_iface_deinit(struct wpa_supplicant *wpa_s,
+                                 struct ctrl_iface_priv *priv)
 {
 }
 
diff -Naurp wpa_supplicant-2.9.orig/wpa_supplicant/ctrl_iface_named_pipe.c wpa_supplicant-2.9/wpa_supplicant/ctrl_iface_named_pipe.c
--- wpa_supplicant-2.9.orig/wpa_supplicant/ctrl_iface_named_pipe.c	2019-08-07 08:25:25.000000000 -0500
+++ wpa_supplicant-2.9/wpa_supplicant/ctrl_iface_named_pipe.c	2021-12-26 13:10:50.692777926 -0600
@@ -462,8 +462,11 @@ wpa_supplicant_ctrl_iface_init(struct wp
 }
 
 
-void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
+void wpa_supplicant_ctrl_iface_deinit(struct wpa_supplicant *wpa_s,
+                                      struct ctrl_iface_priv *priv)
 {
+   if (!priv)
+      return;
 	while (priv->ctrl_dst)
 		ctrl_close_pipe(priv->ctrl_dst);
 	if (priv->sec_attr_set)
diff -Naurp wpa_supplicant-2.9.orig/wpa_supplicant/ctrl_iface_udp.c wpa_supplicant-2.9/wpa_supplicant/ctrl_iface_udp.c
--- wpa_supplicant-2.9.orig/wpa_supplicant/ctrl_iface_udp.c	2019-08-07 08:25:25.000000000 -0500
+++ wpa_supplicant-2.9/wpa_supplicant/ctrl_iface_udp.c	2021-12-26 13:11:37.997463075 -0600
@@ -473,8 +473,12 @@ fail:
 }
 
 
-void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
+void wpa_supplicant_ctrl_iface_deinit(struct wpa_supplicant *wpa_s,
+                                      struct ctrl_iface_priv *priv)
 {
+   if (!priv)
+      return;
+
 	if (priv->sock > -1) {
 		eloop_unregister_read_sock(priv->sock);
 		if (priv->ctrl_dst) {
diff -Naurp wpa_supplicant-2.9.orig/wpa_supplicant/ctrl_iface_unix.c wpa_supplicant-2.9/wpa_supplicant/ctrl_iface_unix.c
--- wpa_supplicant-2.9.orig/wpa_supplicant/ctrl_iface_unix.c	2019-08-07 08:25:25.000000000 -0500
+++ wpa_supplicant-2.9/wpa_supplicant/ctrl_iface_unix.c	2021-12-26 13:16:05.167751155 -0600
@@ -789,13 +789,53 @@ static int wpas_ctrl_iface_reinit(struct
 	return priv->sock;
 }
 
+static void
+wpas_global_ctrl_iface_flush_queued_msg(struct wpa_global *global,
+                                        struct wpa_supplicant *wpa_s)
+{
+   struct ctrl_iface_global_priv *gpriv;
+   struct ctrl_iface_msg *msg, *prev_msg;
+   unsigned int count = 0;
+
+   if (!global || !global->ctrl_iface)
+      return;
+
+   gpriv = global->ctrl_iface;
+   dl_list_for_each_safe(msg, prev_msg, &gpriv->msg_queue,
+                         struct ctrl_iface_msg, list) {
+      if (msg->wpa_s == wpa_s) {
+         count++;
+         dl_list_del(&msg->list);
+         os_free(msg);
+      }
+   }
+
+   if (count) {
+      wpa_printf(MSG_DEBUG,
+                 "CTRL: Dropped %u pending message(s) for interface that is being removed",
+                 count);
+   }
+}
 
-void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
+void wpa_supplicant_ctrl_iface_deinit(struct wpa_supplicant *wpa_s,
+                                      struct ctrl_iface_priv *priv)
 {
 	struct wpa_ctrl_dst *dst, *prev;
 	struct ctrl_iface_msg *msg, *prev_msg;
 	struct ctrl_iface_global_priv *gpriv;
 
+   if (!priv) {
+      /*
+       * Control interface has not yet been initialized, so there is
+       * nothing to deinitialize here. However, there might be a
+       * pending message for this interface, so get rid of any such
+       * entry before completing interface removal.
+       */
+      wpas_global_ctrl_iface_flush_queued_msg(wpa_s->global, wpa_s);
+      eloop_cancel_timeout(wpas_ctrl_msg_queue_timeout, wpa_s, NULL);
+      return;
+   }
+
 	if (priv->sock > -1) {
 		char *fname;
 		char *buf, *dir = NULL;
@@ -867,6 +907,7 @@ free_dst:
 			}
 		}
 	}
+   wpas_global_ctrl_iface_flush_queued_msg(wpa_s->global, wpa_s);
 	eloop_cancel_timeout(wpas_ctrl_msg_queue_timeout, priv->wpa_s, NULL);
 	os_free(priv);
 }
diff -Naurp wpa_supplicant-2.9.orig/wpa_supplicant/eapol_test.c wpa_supplicant-2.9/wpa_supplicant/eapol_test.c
--- wpa_supplicant-2.9.orig/wpa_supplicant/eapol_test.c	2019-08-07 08:25:25.000000000 -0500
+++ wpa_supplicant-2.9/wpa_supplicant/eapol_test.c	2021-12-26 13:16:40.013539371 -0600
@@ -674,10 +674,8 @@ static void test_eapol_clean(struct eapo
 	os_free(e->radius_conf);
 	e->radius_conf = NULL;
 	scard_deinit(wpa_s->scard);
-	if (wpa_s->ctrl_iface) {
-		wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
-		wpa_s->ctrl_iface = NULL;
-	}
+	wpa_supplicant_ctrl_iface_deinit(wpa_s, wpa_s->ctrl_iface);
+   wpa_s->ctrl_iface = NULL;
 
 	ext_password_deinit(wpa_s->ext_pw);
 	wpa_s->ext_pw = NULL;
diff -Naurp wpa_supplicant-2.9.orig/wpa_supplicant/preauth_test.c wpa_supplicant-2.9/wpa_supplicant/preauth_test.c
--- wpa_supplicant-2.9.orig/wpa_supplicant/preauth_test.c	2019-08-07 08:25:25.000000000 -0500
+++ wpa_supplicant-2.9/wpa_supplicant/preauth_test.c	2021-12-26 13:17:18.757306372 -0600
@@ -184,10 +184,8 @@ static void test_eapol_clean(struct wpa_
 	pmksa_candidate_free(wpa_s->wpa);
 	wpa_sm_deinit(wpa_s->wpa);
 	scard_deinit(wpa_s->scard);
-	if (wpa_s->ctrl_iface) {
-		wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
-		wpa_s->ctrl_iface = NULL;
-	}
+	wpa_supplicant_ctrl_iface_deinit(wpa_s, wpa_s->ctrl_iface);
+   wpa_s->ctrl_iface = NULL;
 	wpa_config_free(wpa_s->conf);
 }
 
diff -Naurp wpa_supplicant-2.9.orig/wpa_supplicant/wpa_supplicant.c wpa_supplicant-2.9/wpa_supplicant/wpa_supplicant.c
--- wpa_supplicant-2.9.orig/wpa_supplicant/wpa_supplicant.c	2019-08-07 08:25:25.000000000 -0500
+++ wpa_supplicant-2.9/wpa_supplicant/wpa_supplicant.c	2021-12-26 13:19:06.562670187 -0600
@@ -1073,8 +1073,8 @@ int wpa_supplicant_reload_configuration(
 		    os_strcmp(conf->ctrl_interface,
 			      wpa_s->conf->ctrl_interface) != 0);
 
-	if (reconf_ctrl && wpa_s->ctrl_iface) {
-		wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
+	if (reconf_ctrl) {
+		wpa_supplicant_ctrl_iface_deinit(wpa_s, wpa_s->ctrl_iface);
 		wpa_s->ctrl_iface = NULL;
 	}
 
@@ -6031,10 +6031,8 @@ static void wpa_supplicant_deinit_iface(
 	if (terminate)
 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING);
 
-	if (wpa_s->ctrl_iface) {
-		wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
-		wpa_s->ctrl_iface = NULL;
-	}
+   wpa_supplicant_ctrl_iface_deinit(wpa_s, wpa_s->ctrl_iface);
+   wpa_s->ctrl_iface = NULL;
 
 #ifdef CONFIG_MESH
 	if (wpa_s->ifmsh) {
