Add helper function to clear and free wpa_psk list

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>
This commit is contained in:
Stefan Tomanek 2015-01-19 23:57:41 +01:00 committed by Jouni Malinen
parent cfb5576d93
commit 891dfb3336
5 changed files with 21 additions and 17 deletions

View file

@ -2325,12 +2325,11 @@ static int hostapd_config_fill(struct hostapd_config *conf,
os_free(bss->ssid.wpa_passphrase); os_free(bss->ssid.wpa_passphrase);
bss->ssid.wpa_passphrase = os_strdup(pos); bss->ssid.wpa_passphrase = os_strdup(pos);
if (bss->ssid.wpa_passphrase) { if (bss->ssid.wpa_passphrase) {
os_free(bss->ssid.wpa_psk); hostapd_config_clear_wpa_psk(&bss->ssid.wpa_psk);
bss->ssid.wpa_psk = NULL;
bss->ssid.wpa_passphrase_set = 1; bss->ssid.wpa_passphrase_set = 1;
} }
} else if (os_strcmp(buf, "wpa_psk") == 0) { } 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)); bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
if (bss->ssid.wpa_psk == NULL) if (bss->ssid.wpa_psk == NULL)
return 1; return 1;
@ -2338,8 +2337,7 @@ static int hostapd_config_fill(struct hostapd_config *conf,
pos[PMK_LEN * 2] != '\0') { pos[PMK_LEN * 2] != '\0') {
wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.", wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
line, pos); line, pos);
os_free(bss->ssid.wpa_psk); hostapd_config_clear_wpa_psk(&bss->ssid.wpa_psk);
bss->ssid.wpa_psk = NULL;
return 1; return 1;
} }
bss->ssid.wpa_psk->group = 1; bss->ssid.wpa_psk->group = 1;

View 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) void hostapd_config_free_bss(struct hostapd_bss_config *conf)
{ {
struct hostapd_wpa_psk *psk, *prev;
struct hostapd_eap_user *user, *prev_user; struct hostapd_eap_user *user, *prev_user;
if (conf == NULL) if (conf == NULL)
return; return;
psk = conf->ssid.wpa_psk; hostapd_config_clear_wpa_psk(&conf->ssid.wpa_psk);
while (psk) {
prev = psk;
psk = psk->next;
bin_clear_free(prev, sizeof(*prev));
}
str_clear_free(conf->ssid.wpa_passphrase); str_clear_free(conf->ssid.wpa_passphrase);
os_free(conf->ssid.wpa_psk_file); os_free(conf->ssid.wpa_psk_file);

View file

@ -657,6 +657,7 @@ int hostapd_mac_comp_empty(const void *a);
struct hostapd_config * hostapd_config_defaults(void); struct hostapd_config * hostapd_config_defaults(void);
void hostapd_config_defaults_bss(struct hostapd_bss_config *bss); void hostapd_config_defaults_bss(struct hostapd_bss_config *bss);
void hostapd_config_free_eap_user(struct hostapd_eap_user *user); 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_bss(struct hostapd_bss_config *conf);
void hostapd_config_free(struct hostapd_config *conf); void hostapd_config_free(struct hostapd_config *conf);
int hostapd_maclist_found(struct mac_acl_entry *list, int num_entries, int hostapd_maclist_found(struct mac_acl_entry *list, int num_entries,

View 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 * Force PSK to be derived again since SSID or passphrase may
* have changed. * have changed.
*/ */
os_free(ssid->wpa_psk); hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
ssid->wpa_psk = NULL;
} }
if (hostapd_setup_wpa_psk(hapd->conf)) { if (hostapd_setup_wpa_psk(hapd->conf)) {
wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK " wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "

View file

@ -362,10 +362,9 @@ static int hapd_wps_reconfig_in_memory(struct hostapd_data *hapd,
if (bss->ssid.wpa_passphrase) if (bss->ssid.wpa_passphrase)
os_memcpy(bss->ssid.wpa_passphrase, cred->key, os_memcpy(bss->ssid.wpa_passphrase, cred->key,
cred->key_len); cred->key_len);
os_free(bss->ssid.wpa_psk); hostapd_config_clear_wpa_psk(&bss->ssid.wpa_psk);
bss->ssid.wpa_psk = NULL;
} else if (cred->key_len == 64) { } 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 = bss->ssid.wpa_psk =
os_zalloc(sizeof(struct hostapd_wpa_psk)); os_zalloc(sizeof(struct hostapd_wpa_psk));
if (bss->ssid.wpa_psk && if (bss->ssid.wpa_psk &&