Skip to content

Commit c30a091

Browse files
committedSep 3, 2024
Sanitize SSID and SSID length
1 parent dd94fbc commit c30a091

File tree

8 files changed

+114
-91
lines changed

8 files changed

+114
-91
lines changed
 

‎examples/platform/silabs/SiWx917/SiWx917/sl_wifi_if.cpp

+26-18
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@
3636
#include "task.h"
3737
#include "wfx_host_events.h"
3838
#include "wfx_rsi.h"
39+
3940
#include <app/icd/server/ICDServerConfig.h>
4041
#include <inet/IPAddress.h>
42+
#include <lib/support/CHIPMem.h>
43+
#include <lib/support/CHIPMemString.h>
4144
#include <lib/support/logging/CHIPLogging.h>
4245

4346
extern "C" {
@@ -163,8 +166,10 @@ int32_t wfx_rsi_get_ap_info(wfx_wifi_scan_result_t * ap)
163166
{
164167
sl_status_t status = SL_STATUS_OK;
165168
int32_t rssi = 0;
169+
ap->ssid_length = wfx_rsi.sec.ssid_length;
166170
ap->security = wfx_rsi.sec.security;
167171
ap->chan = wfx_rsi.ap_chan;
172+
chip::Platform::CopyString(ap->ssid, ap->ssid_length, wfx_rsi.sec.ssid);
168173
memcpy(&ap->bssid[0], &wfx_rsi.ap_mac.octet[0], BSSID_LEN);
169174
sl_wifi_get_signal_strength(SL_WIFI_CLIENT_INTERFACE, &rssi);
170175
ap->rssi = rssi;
@@ -196,14 +201,14 @@ int32_t wfx_rsi_get_ap_ext(wfx_wifi_scan_ext_t * extra_info)
196201
}
197202

198203
/******************************************************************
199-
* @fn int32_t wfx_rsi_reset_count()
204+
* @fn int32_t wfx_rsi_reset_count(void)
200205
* @brief
201206
* Getting the driver reset count
202207
* @param[in] None
203208
* @return
204209
* status
205210
*********************************************************************/
206-
int32_t wfx_rsi_reset_count()
211+
int32_t wfx_rsi_reset_count(void)
207212
{
208213
sl_wifi_statistics_t test = { 0 };
209214
sl_status_t status = SL_STATUS_OK;
@@ -220,14 +225,14 @@ int32_t wfx_rsi_reset_count()
220225
}
221226

222227
/******************************************************************
223-
* @fn wfx_rsi_disconnect()
228+
* @fn wfx_rsi_disconnect(void)
224229
* @brief
225230
* Getting the driver disconnect status
226231
* @param[in] None
227232
* @return
228233
* status
229234
*********************************************************************/
230-
int32_t wfx_rsi_disconnect()
235+
int32_t wfx_rsi_disconnect(void)
231236
{
232237
return sl_wifi_disconnect(SL_WIFI_CLIENT_INTERFACE);
233238
}
@@ -533,7 +538,9 @@ sl_status_t show_scan_results(sl_wifi_scan_result_t * scan_result)
533538
for (int idx = 0; idx < (int) scan_result->scan_count; idx++)
534539
{
535540
memset(&cur_scan_result, 0, sizeof(cur_scan_result));
536-
strncpy(cur_scan_result.ssid, (char *) &scan_result->scan_info[idx].ssid, WFX_MAX_SSID_LENGTH);
541+
542+
cur_scan_result.ssid_length = strnlen((char *) &scan_result->scan_info[idx].ssid, WFX_MAX_SSID_LENGTH);
543+
chip::Platform::CopyString(cur_scan_result.ssid, cur_scan_result.ssid_length, (char *) &scan_result->scan_info[idx].ssid);
537544

538545
// if user has provided ssid, then check if the current scan result ssid matches the user provided ssid
539546
if (wfx_rsi.scan_ssid != NULL && strcmp(wfx_rsi.scan_ssid, cur_scan_result.ssid) != CMP_SUCCESS)
@@ -556,10 +563,10 @@ sl_status_t show_scan_results(sl_wifi_scan_result_t * scan_result)
556563
// cleanup and return
557564
wfx_rsi.dev_state &= ~WFX_RSI_ST_SCANSTARTED;
558565
wfx_rsi.scan_cb((wfx_wifi_scan_result_t *) 0);
559-
wfx_rsi.scan_cb = NULL;
566+
wfx_rsi.scan_cb = nullptr;
560567
if (wfx_rsi.scan_ssid)
561568
{
562-
vPortFree(wfx_rsi.scan_ssid);
569+
chip::Platform::MemoryFree(wfx_rsi.scan_ssid);
563570
wfx_rsi.scan_ssid = NULL;
564571
}
565572
return SL_STATUS_OK;
@@ -573,14 +580,14 @@ sl_status_t bg_scan_callback_handler(sl_wifi_event_t event, sl_wifi_scan_result_
573580
return SL_STATUS_OK;
574581
}
575582
/***************************************************************************************
576-
* @fn static void wfx_rsi_save_ap_info()
583+
* @fn static void wfx_rsi_save_ap_info(void)
577584
* @brief
578585
* Saving the details of the AP
579586
* @param[in] None
580587
* @return
581588
* None
582589
*******************************************************************************************/
583-
static void wfx_rsi_save_ap_info() // translation
590+
static void wfx_rsi_save_ap_info(void) // translation
584591
{
585592
sl_status_t status = SL_STATUS_OK;
586593
#ifndef EXP_BOARD
@@ -589,8 +596,8 @@ static void wfx_rsi_save_ap_info() // translation
589596
#endif
590597
sl_wifi_ssid_t ssid_arg;
591598
memset(&ssid_arg, 0, sizeof(ssid_arg));
592-
ssid_arg.length = strnlen(wfx_rsi.sec.ssid, WFX_MAX_SSID_LENGTH);
593-
strncpy((char *) &ssid_arg.value[0], wfx_rsi.sec.ssid, WFX_MAX_SSID_LENGTH);
599+
ssid_arg.length = wfx_rsi.sec.ssid_length;
600+
chip::Platform::CopyString((char *) &ssid_arg.value[0], ssid_arg.length, wfx_rsi.sec.ssid);
594601
sl_wifi_set_scan_callback(scan_callback_handler, NULL);
595602
scan_results_complete = false;
596603
#ifndef EXP_BOARD
@@ -616,7 +623,7 @@ static sl_status_t wfx_rsi_do_join(void)
616623
sl_status_t status = SL_STATUS_OK;
617624
sl_wifi_client_configuration_t ap;
618625
memset(&ap, 0, sizeof(ap));
619-
WfxEvent_t event;
626+
620627
switch (wfx_rsi.sec.security)
621628
{
622629
case WFX_SEC_WEP:
@@ -659,19 +666,17 @@ static sl_status_t wfx_rsi_do_join(void)
659666
status = sl_wifi_set_advanced_client_configuration(SL_WIFI_CLIENT_INTERFACE, &client_config);
660667
VerifyOrReturnError(status == SL_STATUS_OK, status);
661668
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER
662-
size_t psk_length = strlen(wfx_rsi.sec.passkey);
663-
VerifyOrReturnError(psk_length <= SL_WIFI_MAX_PSK_LENGTH, SL_STATUS_SI91X_INVALID_PSK_LENGTH);
664669
sl_net_credential_id_t id = SL_NET_DEFAULT_WIFI_CLIENT_CREDENTIAL_ID;
665-
status = sl_net_set_credential(id, SL_NET_WIFI_PSK, &wfx_rsi.sec.passkey[0], psk_length);
670+
status = sl_net_set_credential(id, SL_NET_WIFI_PSK, &wfx_rsi.sec.passkey[0], wfx_rsi.sec.passkey_length);
666671
VerifyOrReturnError(status == SL_STATUS_OK, status);
667672

668673
uint32_t timeout_ms = 0;
669-
ap.ssid.length = strnlen(wfx_rsi.sec.ssid, WFX_MAX_SSID_LENGTH);
674+
ap.ssid.length = wfx_rsi.sec.ssid_length;
670675
ap.encryption = SL_WIFI_NO_ENCRYPTION;
671676
ap.credential_id = id;
672-
memset(&ap.ssid.value, 0, (sizeof(ap.ssid.value) / sizeof(ap.ssid.value[0])));
673-
strncpy((char *) &ap.ssid.value[0], wfx_rsi.sec.ssid, WFX_MAX_SSID_LENGTH);
677+
memcpy((char *) &ap.ssid.value[0], wfx_rsi.sec.ssid, wfx_rsi.sec.ssid_length);
674678
ChipLogDetail(DeviceLayer, "wfx_rsi_do_join: SSID: %s, SECURITY: %d(%d)", ap.ssid.value, ap.security, wfx_rsi.sec.security);
679+
675680
status = sl_wifi_connect(SL_WIFI_CLIENT_INTERFACE, &ap, timeout_ms);
676681
// sl_wifi_connect returns SL_STATUS_IN_PROGRESS if join is in progress
677682
// after the initial scan is done, the scan does not check for SSID
@@ -684,8 +689,11 @@ static sl_status_t wfx_rsi_do_join(void)
684689
wfx_rsi.dev_state &= ~(WFX_RSI_ST_STA_CONNECTING | WFX_RSI_ST_STA_CONNECTED);
685690
ChipLogProgress(DeviceLayer, "wfx_rsi_do_join: retry attempt %d", wfx_rsi.join_retries);
686691
wfx_retry_connection(++wfx_rsi.join_retries);
692+
693+
WfxEvent_t event;
687694
event.eventType = WFX_EVT_STA_START_JOIN;
688695
WfxPostEvent(&event);
696+
689697
return status;
690698
}
691699

‎examples/platform/silabs/SiWx917/SiWx917/wfx_rsi_host.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ void wfx_clear_wifi_provision(void)
163163
sl_status_t wfx_connect_to_ap(void)
164164
{
165165
VerifyOrReturnError(wfx_rsi.dev_state & WFX_RSI_ST_STA_PROVISIONED, SL_STATUS_INVALID_CONFIGURATION);
166-
VerifyOrReturnError(strlen(wfx_rsi.sec.ssid) <= WFX_MAX_SSID_LENGTH, SL_STATUS_HAS_OVERFLOWED);
166+
VerifyOrReturnError(wfx_rsi.sec.ssid_length, SL_STATUS_INVALID_CREDENTIALS);
167+
VerifyOrReturnError(wfx_rsi.sec.ssid_length <= WFX_MAX_SSID_LENGTH, SL_STATUS_HAS_OVERFLOWED);
167168
ChipLogProgress(DeviceLayer, "connect to access point: %s", wfx_rsi.sec.ssid);
168169
WfxEvent_t event;
169170
event.eventType = WFX_EVT_STA_START_JOIN;
@@ -345,10 +346,10 @@ bool wfx_start_scan(char * ssid, void (*callback)(wfx_wifi_scan_result_t *))
345346
wfx_rsi.scan_cb = callback;
346347

347348
VerifyOrReturnError(ssid != nullptr, false);
348-
size_t ssid_len = strnlen(ssid, WFX_MAX_SSID_LENGTH);
349-
wfx_rsi.scan_ssid = reinterpret_cast<char *>(chip::Platform::MemoryAlloc(ssid_len + 1));
349+
wfx_rsi.scan_ssid_length = strnlen(ssid, WFX_MAX_SSID_LENGTH);
350+
wfx_rsi.scan_ssid = reinterpret_cast<char *>(chip::Platform::MemoryAlloc(wfx_rsi.scan_ssid_length));
350351
VerifyOrReturnError(wfx_rsi.scan_ssid != nullptr, false);
351-
strncpy(wfx_rsi.scan_ssid, ssid, WFX_MAX_SSID_LENGTH);
352+
chip::Platform::CopyString(wfx_rsi.scan_ssid, wfx_rsi.scan_ssid_length, ssid);
352353

353354
WfxEvent_t event;
354355
event.eventType = WFX_EVT_SCAN;

‎examples/platform/silabs/efr32/rs911x/rsi_if.c

+60-53
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ static void StartDHCPTimer(uint32_t timeout)
132132
*********************************************************************/
133133
int32_t wfx_rsi_get_ap_info(wfx_wifi_scan_result_t * ap)
134134
{
135-
int32_t status;
135+
int32_t status = RSI_SUCCESS;
136136
uint8_t rssi;
137137
ap->security = wfx_rsi.sec.security;
138138
ap->chan = wfx_rsi.ap_chan;
@@ -161,18 +161,16 @@ int32_t wfx_rsi_get_ap_ext(wfx_wifi_scan_ext_t * extra_info)
161161
if (status != RSI_SUCCESS)
162162
{
163163
SILABS_LOG("Failed, Error Code : 0x%lX", status);
164+
return status;
164165
}
165-
else
166-
{
167-
rsi_wlan_ext_stats_t * test = (rsi_wlan_ext_stats_t *) buff;
168-
extra_info->beacon_lost_count = test->beacon_lost_count - temp_reset.beacon_lost_count;
169-
extra_info->beacon_rx_count = test->beacon_rx_count - temp_reset.beacon_rx_count;
170-
extra_info->mcast_rx_count = test->mcast_rx_count - temp_reset.mcast_rx_count;
171-
extra_info->mcast_tx_count = test->mcast_tx_count - temp_reset.mcast_tx_count;
172-
extra_info->ucast_rx_count = test->ucast_rx_count - temp_reset.ucast_rx_count;
173-
extra_info->ucast_tx_count = test->ucast_tx_count - temp_reset.ucast_tx_count;
174-
extra_info->overrun_count = test->overrun_count - temp_reset.overrun_count;
175-
}
166+
rsi_wlan_ext_stats_t * test = (rsi_wlan_ext_stats_t *) buff;
167+
extra_info->beacon_lost_count = test->beacon_lost_count - temp_reset.beacon_lost_count;
168+
extra_info->beacon_rx_count = test->beacon_rx_count - temp_reset.beacon_rx_count;
169+
extra_info->mcast_rx_count = test->mcast_rx_count - temp_reset.mcast_rx_count;
170+
extra_info->mcast_tx_count = test->mcast_tx_count - temp_reset.mcast_tx_count;
171+
extra_info->ucast_rx_count = test->ucast_rx_count - temp_reset.ucast_rx_count;
172+
extra_info->ucast_tx_count = test->ucast_tx_count - temp_reset.ucast_tx_count;
173+
extra_info->overrun_count = test->overrun_count - temp_reset.overrun_count;
176174
return status;
177175
}
178176

@@ -192,18 +190,16 @@ int32_t wfx_rsi_reset_count()
192190
if (status != RSI_SUCCESS)
193191
{
194192
SILABS_LOG("Failed, Error Code : 0x%lX", status);
193+
return status;
195194
}
196-
else
197-
{
198-
rsi_wlan_ext_stats_t * test = (rsi_wlan_ext_stats_t *) buff;
199-
temp_reset.beacon_lost_count = test->beacon_lost_count;
200-
temp_reset.beacon_rx_count = test->beacon_rx_count;
201-
temp_reset.mcast_rx_count = test->mcast_rx_count;
202-
temp_reset.mcast_tx_count = test->mcast_tx_count;
203-
temp_reset.ucast_rx_count = test->ucast_rx_count;
204-
temp_reset.ucast_tx_count = test->ucast_tx_count;
205-
temp_reset.overrun_count = test->overrun_count;
206-
}
195+
rsi_wlan_ext_stats_t * test = (rsi_wlan_ext_stats_t *) buff;
196+
temp_reset.beacon_lost_count = test->beacon_lost_count;
197+
temp_reset.beacon_rx_count = test->beacon_rx_count;
198+
temp_reset.mcast_rx_count = test->mcast_rx_count;
199+
temp_reset.mcast_tx_count = test->mcast_tx_count;
200+
temp_reset.ucast_rx_count = test->ucast_rx_count;
201+
temp_reset.ucast_tx_count = test->ucast_tx_count;
202+
temp_reset.overrun_count = test->overrun_count;
207203
return status;
208204
}
209205

@@ -217,8 +213,7 @@ int32_t wfx_rsi_reset_count()
217213
*********************************************************************/
218214
int32_t wfx_rsi_disconnect()
219215
{
220-
int32_t status = rsi_wlan_disconnect();
221-
return status;
216+
return rsi_wlan_disconnect();
222217
}
223218

224219
#if SL_ICD_ENABLED
@@ -275,18 +270,17 @@ static void wfx_rsi_join_cb(uint16_t status, const uint8_t * buf, const uint16_t
275270
*/
276271
SILABS_LOG("wfx_rsi_join_cb: failed. retry: %d", wfx_rsi.join_retries);
277272
wfx_retry_connection(++wfx_rsi.join_retries);
273+
return;
278274
}
279-
else
280-
{
281-
/*
282-
* Join was complete - Do the DHCP
283-
*/
284-
SILABS_LOG("wfx_rsi_join_cb: success");
285-
memset(&temp_reset, 0, sizeof(wfx_wifi_scan_ext_t));
286-
WfxEvent.eventType = WFX_EVT_STA_CONN;
287-
WfxPostEvent(&WfxEvent);
288-
wfx_rsi.join_retries = 0;
289-
}
275+
276+
/*
277+
* Join was complete - Do the DHCP
278+
*/
279+
SILABS_LOG("wfx_rsi_join_cb: success");
280+
memset(&temp_reset, 0, sizeof(wfx_wifi_scan_ext_t));
281+
WfxEvent.eventType = WFX_EVT_STA_CONN;
282+
WfxPostEvent(&WfxEvent);
283+
wfx_rsi.join_retries = 0;
290284
}
291285

292286
/******************************************************************
@@ -453,7 +447,7 @@ static int32_t wfx_rsi_init(void)
453447
* @return
454448
* None
455449
*******************************************************************************************/
456-
static void wfx_rsi_save_ap_info() // translation
450+
static void wfx_rsi_save_ap_info(void) // translation
457451
{
458452
int32_t status;
459453
rsi_rsp_scan_t rsp;
@@ -707,29 +701,42 @@ void ProcessEvent(WfxEvent_t inEvent)
707701
SILABS_LOG("rsi_wlan_bgscan failed: %02x ", status);
708702
return;
709703
}
704+
705+
if (wfx_rsi.scan_cb == NULL)
706+
{
707+
return;
708+
}
709+
710710
rsi_scan_info_t * scan;
711711
wfx_wifi_scan_result_t ap;
712+
712713
for (int x = 0; x < scan_rsp.scan_count[0]; x++)
713714
{
714715
scan = &scan_rsp.scan_info[x];
715-
// is it a scan all or target scan
716-
if (!wfx_rsi.scan_ssid || (wfx_rsi.scan_ssid && strcmp(wfx_rsi.scan_ssid, (char *) scan->ssid) == CMP_SUCCESS))
716+
// clear structure and calculate size of SSID
717+
memset(&ap, 0, sizeof(ap));
718+
ap.ssid_length = strnlen((char *) scan->ssid, WFX_MAX_SSID_LENGTH);
719+
strncpy(ap.ssid, (char *) scan->ssid, ap.ssid_length);
720+
// assure null termination of scanned SSID
721+
ap.ssid[ap.ssid_length - 1] = 0;
722+
ap.security = scan->security_mode;
723+
ap.rssi = (-1) * scan->rssi_val;
724+
725+
configASSERT(sizeof(ap.bssid) == BSSID_LEN);
726+
configASSERT(sizeof(scan->bssid) == BSSID_LEN);
727+
memcpy(ap.bssid, scan->bssid, BSSID_LEN);
728+
729+
// no ssid filter set, return all results
730+
if (wfx_rsi.scan_ssid_length == 0)
717731
{
718-
// clear structure and calculate size of SSID
719-
memset(&ap, 0, sizeof(ap));
720-
strncpy(ap.ssid, (char *) scan->ssid, strnlen((const char *) scan->ssid, WFX_MAX_SSID_LENGTH));
721-
ap.security = scan->security_mode;
722-
ap.rssi = (-1) * scan->rssi_val;
723-
724-
configASSERT(sizeof(ap.bssid) == BSSID_LEN);
725-
configASSERT(sizeof(scan->bssid) == BSSID_LEN);
726-
memcpy(ap.bssid, scan->bssid, BSSID_LEN);
727732
(*wfx_rsi.scan_cb)(&ap);
728-
729-
if (wfx_rsi.scan_ssid)
730-
{
731-
break; // we found the targeted ssid.
732-
}
733+
continue;
734+
}
735+
// check if the scanned ssid is the one we are looking for
736+
else if (strcmp(wfx_rsi.scan_ssid, ap.ssid) == CMP_SUCCESS)
737+
{
738+
(*wfx_rsi.scan_cb)(&ap);
739+
break; // we found the targeted ssid.
733740
}
734741
}
735742

0 commit comments

Comments
 (0)