Skip to content

Commit ce45f4c

Browse files
pdgendtcfriedt
authored andcommitted
net: lib: wifi: mgmt: Fix memory leaks
When connecting to WiFi from stored credentials, the key_passwd is never freed. Additionally if the connect fails, the allocated data was never freed. Convert heap allocated memory to stack allocated buffers. Signed-off-by: Pieter De Gendt <[email protected]>
1 parent 7eba25f commit ce45f4c

File tree

1 file changed

+35
-83
lines changed

1 file changed

+35
-83
lines changed

subsys/net/l2/wifi/wifi_mgmt.c

Lines changed: 35 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ LOG_MODULE_REGISTER(net_wifi_mgmt, CONFIG_NET_L2_WIFI_MGMT_LOG_LEVEL);
1414
#include <errno.h>
1515
#include <string.h>
1616
#include <stdio.h>
17+
#include <zephyr/toolchain.h>
1718
#include <zephyr/net/net_core.h>
1819
#include <zephyr/net/net_if.h>
1920
#include <zephyr/net/wifi_mgmt.h>
@@ -1527,53 +1528,25 @@ BUILD_ASSERT(sizeof(CONFIG_WIFI_CREDENTIALS_STATIC_SSID) != 1,
15271528
"CONFIG_WIFI_CREDENTIALS_STATIC_SSID required");
15281529
#endif /* defined(CONFIG_WIFI_CREDENTIALS_STATIC) */
15291530

1531+
/**
1532+
* Disable -Wcast-qual in this function, the buffers passed in the params argument are mutable.
1533+
*/
1534+
TOOLCHAIN_DISABLE_WARNING(TOOLCHAIN_WARNING_CAST_QUAL)
15301535
static int __stored_creds_to_params(struct wifi_credentials_personal *creds,
15311536
struct wifi_connect_req_params *params)
15321537
{
1533-
char *ssid = NULL;
1534-
char *psk = NULL;
1535-
#ifdef CONFIG_WIFI_NM_WPA_SUPPLICANT_CRYPTO_ENTERPRISE
1536-
char *key_passwd = NULL;
1537-
#endif /* CONFIG_WIFI_NM_WPA_SUPPLICANT_CRYPTO_ENTERPRISE */
1538-
int ret;
1539-
15401538
/* SSID */
1541-
ssid = (char *)k_malloc(creds->header.ssid_len + 1);
1542-
if (!ssid) {
1543-
LOG_ERR("Failed to allocate memory for SSID\n");
1544-
ret = -ENOMEM;
1545-
goto err_out;
1546-
}
1547-
1548-
memset(ssid, 0, creds->header.ssid_len + 1);
1549-
ret = snprintf(ssid, creds->header.ssid_len + 1, "%s", creds->header.ssid);
1550-
if (ret > creds->header.ssid_len) {
1539+
if (creds->header.ssid_len > WIFI_SSID_MAX_LEN) {
15511540
LOG_ERR("SSID string truncated\n");
1552-
ret = -EINVAL;
1553-
goto err_out;
1541+
return -EINVAL;
15541542
}
15551543

1556-
params->ssid = ssid;
1544+
memcpy((uint8_t *)params->ssid, creds->header.ssid, creds->header.ssid_len);
15571545
params->ssid_length = creds->header.ssid_len;
15581546

15591547
/* PSK (optional) */
1560-
if (creds->password_len > 0) {
1561-
psk = (char *)k_malloc(creds->password_len + 1);
1562-
if (!psk) {
1563-
LOG_ERR("Failed to allocate memory for PSK\n");
1564-
ret = -ENOMEM;
1565-
goto err_out;
1566-
}
1567-
1568-
memset(psk, 0, creds->password_len + 1);
1569-
ret = snprintf(psk, creds->password_len + 1, "%s", creds->password);
1570-
if (ret > creds->password_len) {
1571-
LOG_ERR("PSK string truncated\n");
1572-
ret = -EINVAL;
1573-
goto err_out;
1574-
}
1575-
1576-
params->psk = psk;
1548+
if (creds->password_len > 0 && creds->password_len <= WIFI_PSK_MAX_LEN) {
1549+
memcpy((uint8_t *)params->psk, creds->password, creds->password_len);
15771550
params->psk_length = creds->password_len;
15781551
}
15791552

@@ -1583,21 +1556,12 @@ static int __stored_creds_to_params(struct wifi_credentials_personal *creds,
15831556
#ifdef CONFIG_WIFI_NM_WPA_SUPPLICANT_CRYPTO_ENTERPRISE
15841557
if (params->security == WIFI_SECURITY_TYPE_EAP_TLS) {
15851558
if (creds->header.key_passwd_length > 0) {
1586-
key_passwd = (char *)k_malloc(creds->header.key_passwd_length + 1);
1587-
if (!key_passwd) {
1588-
LOG_ERR("Failed to allocate memory for key_passwd\n");
1589-
ret = -ENOMEM;
1590-
goto err_out;
1591-
}
1592-
memset(key_passwd, 0, creds->header.key_passwd_length + 1);
1593-
ret = snprintf(key_passwd, creds->header.key_passwd_length + 1, "%s",
1594-
creds->header.key_passwd);
1595-
if (ret > creds->header.key_passwd_length) {
1559+
if (creds->header.key_passwd_length > WIFI_ENT_PSWD_MAX_LEN) {
15961560
LOG_ERR("key_passwd string truncated\n");
1597-
ret = -EINVAL;
1598-
goto err_out;
1561+
return -EINVAL;
15991562
}
1600-
params->key_passwd = key_passwd;
1563+
memcpy((uint8_t *)params->key_passwd, creds->header.key_passwd,
1564+
creds->header.key_passwd_length);
16011565
params->key_passwd_length = creds->header.key_passwd_length;
16021566
}
16031567
}
@@ -1632,26 +1596,8 @@ static int __stored_creds_to_params(struct wifi_credentials_personal *creds,
16321596
}
16331597

16341598
return 0;
1635-
err_out:
1636-
if (ssid) {
1637-
k_free(ssid);
1638-
ssid = NULL;
1639-
}
1640-
1641-
if (psk) {
1642-
k_free(psk);
1643-
psk = NULL;
1644-
}
1645-
1646-
#ifdef CONFIG_WIFI_NM_WPA_SUPPLICANT_CRYPTO_ENTERPRISE
1647-
if (key_passwd) {
1648-
k_free(key_passwd);
1649-
key_passwd = NULL;
1650-
}
1651-
#endif /* CONFIG_WIFI_NM_WPA_SUPPLICANT_CRYPTO_ENTERPRISE */
1652-
1653-
return ret;
16541599
}
1600+
TOOLCHAIN_ENABLE_WARNING(TOOLCHAIN_WARNING_CAST_QUAL)
16551601

16561602
static inline const char *wpa_supp_security_txt(enum wifi_security_type security)
16571603
{
@@ -1674,31 +1620,37 @@ static int add_network_from_credentials_struct_personal(struct wifi_credentials_
16741620
struct net_if *iface)
16751621
{
16761622
int ret = 0;
1677-
struct wifi_connect_req_params cnx_params = {0};
1623+
uint8_t ssid[WIFI_SSID_MAX_LEN + 1] = {0};
1624+
uint8_t psk[WIFI_PSK_MAX_LEN + 1] = {0};
1625+
#ifdef CONFIG_WIFI_NM_WPA_SUPPLICANT_CRYPTO_ENTERPRISE
1626+
uint8_t key_passwd[WIFI_ENT_PSWD_MAX_LEN + 1] = {0};
1627+
#endif /* CONFIG_WIFI_NM_WPA_SUPPLICANT_CRYPTO_ENTERPRISE */
1628+
1629+
struct wifi_connect_req_params cnx_params = {
1630+
.ssid = ssid,
1631+
.psk = psk,
1632+
#ifdef CONFIG_WIFI_NM_WPA_SUPPLICANT_CRYPTO_ENTERPRISE
1633+
.key_passwd = key_passwd,
1634+
#endif /* CONFIG_WIFI_NM_WPA_SUPPLICANT_CRYPTO_ENTERPRISE */
1635+
};
16781636

1679-
if (__stored_creds_to_params(creds, &cnx_params)) {
1637+
if (__stored_creds_to_params(creds, &cnx_params) != 0) {
16801638
ret = -ENOEXEC;
16811639
goto out;
16821640
}
16831641

1684-
if (net_mgmt(NET_REQUEST_WIFI_CONNECT, iface, &cnx_params,
1685-
sizeof(struct wifi_connect_req_params))) {
1686-
LOG_ERR("Connection request failed\n");
1642+
ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, iface, &cnx_params,
1643+
sizeof(struct wifi_connect_req_params));
1644+
if (ret < 0) {
1645+
LOG_ERR("Connection request failed (%d)", ret);
16871646

1688-
return -ENOEXEC;
1647+
ret = -ENOEXEC;
1648+
goto out;
16891649
}
16901650

16911651
LOG_INF("Connection requested");
16921652

16931653
out:
1694-
if (cnx_params.psk) {
1695-
k_free((void *)cnx_params.psk);
1696-
}
1697-
1698-
if (cnx_params.ssid) {
1699-
k_free((void *)cnx_params.ssid);
1700-
}
1701-
17021654
return ret;
17031655
}
17041656

0 commit comments

Comments
 (0)