]> git.ipfire.org Git - thirdparty/hostap.git/commitdiff
Add helper function to clear and free wpa_psk list
authorStefan Tomanek <stefan.tomanek@wertarbyte.de>
Mon, 19 Jan 2015 22:57:41 +0000 (23:57 +0100)
committerJouni Malinen <j@w1.fi>
Sat, 21 Feb 2015 15:11:15 +0000 (17:11 +0200)
This change adds the function hostapd_config_clear_wpa_psk() that
deletes an entire wpa_psk structure, making sure to follow the linked
list and to free the allocated memory of each PSK node. This helps to
prevent memory leaks when using PSKs from multiple sources and
reconfiguring the AP during runtime.

Signed-off-by: Stefan Tomanek <stefan.tomanek@wertarbyte.de>
hostapd/config_file.c
src/ap/ap_config.c
src/ap/ap_config.h
src/ap/hostapd.c
src/ap/wps_hostapd.c

index 3466f285172ecb62b073a18cc6b7d5270dcd4684..9edbe63263b7ba04c81f075588103638db4fd00d 100644 (file)
@@ -2325,12 +2325,11 @@ static int hostapd_config_fill(struct hostapd_config *conf,
                os_free(bss->ssid.wpa_passphrase);
                bss->ssid.wpa_passphrase = os_strdup(pos);
                if (bss->ssid.wpa_passphrase) {
-                       os_free(bss->ssid.wpa_psk);
-                       bss->ssid.wpa_psk = NULL;
+                       hostapd_config_clear_wpa_psk(&bss->ssid.wpa_psk);
                        bss->ssid.wpa_passphrase_set = 1;
                }
        } else if (os_strcmp(buf, "wpa_psk") == 0) {
-               os_free(bss->ssid.wpa_psk);
+               hostapd_config_clear_wpa_psk(&bss->ssid.wpa_psk);
                bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
                if (bss->ssid.wpa_psk == NULL)
                        return 1;
@@ -2338,8 +2337,7 @@ static int hostapd_config_fill(struct hostapd_config *conf,
                    pos[PMK_LEN * 2] != '\0') {
                        wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
                                   line, pos);
-                       os_free(bss->ssid.wpa_psk);
-                       bss->ssid.wpa_psk = NULL;
+                       hostapd_config_clear_wpa_psk(&bss->ssid.wpa_psk);
                        return 1;
                }
                bss->ssid.wpa_psk->group = 1;
index c1861d4f7041a4ce1571b4043dee408737f3a9d9..76011dc07fd4b6199822dba2f9268165ceabcc2b 100644 (file)
@@ -394,20 +394,27 @@ static void hostapd_config_free_wep(struct hostapd_wep_keys *keys)
 }
 
 
+void hostapd_config_clear_wpa_psk(struct hostapd_wpa_psk **l)
+{
+       struct hostapd_wpa_psk *psk, *tmp;
+
+       for (psk = *l; psk;) {
+               tmp = psk;
+               psk = psk->next;
+               bin_clear_free(tmp, sizeof(*tmp));
+       }
+       *l = NULL;
+}
+
+
 void hostapd_config_free_bss(struct hostapd_bss_config *conf)
 {
-       struct hostapd_wpa_psk *psk, *prev;
        struct hostapd_eap_user *user, *prev_user;
 
        if (conf == NULL)
                return;
 
-       psk = conf->ssid.wpa_psk;
-       while (psk) {
-               prev = psk;
-               psk = psk->next;
-               bin_clear_free(prev, sizeof(*prev));
-       }
+       hostapd_config_clear_wpa_psk(&conf->ssid.wpa_psk);
 
        str_clear_free(conf->ssid.wpa_passphrase);
        os_free(conf->ssid.wpa_psk_file);
index ec73771f6be35268c14ad6ba812a6f497e31f1c0..961d2dd389f8720960eb55aafb938311e5dd454a 100644 (file)
@@ -657,6 +657,7 @@ int hostapd_mac_comp_empty(const void *a);
 struct hostapd_config * hostapd_config_defaults(void);
 void hostapd_config_defaults_bss(struct hostapd_bss_config *bss);
 void hostapd_config_free_eap_user(struct hostapd_eap_user *user);
+void hostapd_config_clear_wpa_psk(struct hostapd_wpa_psk **p);
 void hostapd_config_free_bss(struct hostapd_bss_config *conf);
 void hostapd_config_free(struct hostapd_config *conf);
 int hostapd_maclist_found(struct mac_acl_entry *list, int num_entries,
index 61614dc3d1466768b1a702b86e47a807655a6799..e641b129fa35e3433f9f9610959aa8e16defd0f9 100644 (file)
@@ -82,8 +82,7 @@ static void hostapd_reload_bss(struct hostapd_data *hapd)
                 * Force PSK to be derived again since SSID or passphrase may
                 * have changed.
                 */
-               os_free(ssid->wpa_psk);
-               ssid->wpa_psk = NULL;
+               hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
        }
        if (hostapd_setup_wpa_psk(hapd->conf)) {
                wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
index 9ba7aba8d8ae3fc9407eb6c22053d1685588cd49..b0e8b0bfcac34f77ab89148342b16d3acfe7bdfe 100644 (file)
@@ -362,10 +362,9 @@ static int hapd_wps_reconfig_in_memory(struct hostapd_data *hapd,
                        if (bss->ssid.wpa_passphrase)
                                os_memcpy(bss->ssid.wpa_passphrase, cred->key,
                                          cred->key_len);
-                       os_free(bss->ssid.wpa_psk);
-                       bss->ssid.wpa_psk = NULL;
+                       hostapd_config_clear_wpa_psk(&bss->ssid.wpa_psk);
                } else if (cred->key_len == 64) {
-                       os_free(bss->ssid.wpa_psk);
+                       hostapd_config_clear_wpa_psk(&bss->ssid.wpa_psk);
                        bss->ssid.wpa_psk =
                                os_zalloc(sizeof(struct hostapd_wpa_psk));
                        if (bss->ssid.wpa_psk &&