Answers checklist.
IDF version.
5.x and up
Espressif SoC revision.
ESP32-S3
Operating System used.
Windows
How did you build your project?
Command line with Make
If you are using Windows, please specify command line type.
None
Development Kit.
Generic ESP32-S3 Dev Kit
Power Supply used.
USB
What is the expected behavior?
I expect it to use strlcpy instead of memcpy
What is the actual behavior?
Good day,
There is code in the WiFi implementation of the ESP32-S3 (and, I suspect, in the WiFi code of other Espressif processors as well) that reads uninitialized memory.
The circumstances are such that this does not currently lead to any serious consequences, but it is still not a good practice and may become problematic in other environments.
int wifi_nvs_cfg_item_init(const char *key_name, uint8_t key_index, ..... /* more args, unimportant for the bug */) {
wifi_nvs_key_t *k;
if (key_index < WIFI_NVS_KEY_INDEX_MAX) {
if (min <= max) {
k = &g_wifi_nvs_cfg[key_index];
memcpy(k->name, key_name, 0x20);
k->index = key_index;
k->def_val = def_val;
k->data_type = data_type;
...
...
...
Please note the memcpy() call: the key_name (an ASCIIZ string) is copied into a structure field.
The destination field itself is fine and has sufficient space allocated.
However, the calls to wifi_nvs_cfg_item_init() raise concerns:
err = wifi_nvs_cfg_item_init("opmode", WIFI_NVS_KEY_OPMODE, 0, 1, 0, 4, 1, &s_wifi_nvs);
err = wifi_nvs_cfg_item_init("sta.ssid", ...);
err = wifi_nvs_cfg_item_init("sta.authmode", ...);
And so on - this pattern appears throughout net80211/ieee80211_nvs.c during WiFi startup sequence.
The issue is that memcpy(..., key_name, 0x20) always copies 32 bytes from key_name, while the provided string literals are significantly shorter. As a result, the code reads bytes past the terminating null character, effectively accessing unrelated adjacent memory contents.
It is fortunate that on the ESP32-S3 there is, by default, no strict memory protection or memory separation, so this does not immediately cause crashes or faults.
Nevertheless, this is still incorrect behavior and should ideally be replaced with a bounded string copy or a copy length based on strlen(key_name) + 1.
Steps to reproduce.
No steps to reproduce since no side effects.
Debug Logs.
Diagnostic report archive.
No response
More Information.
No response
Answers checklist.
IDF version.
5.x and up
Espressif SoC revision.
ESP32-S3
Operating System used.
Windows
How did you build your project?
Command line with Make
If you are using Windows, please specify command line type.
None
Development Kit.
Generic ESP32-S3 Dev Kit
Power Supply used.
USB
What is the expected behavior?
I expect it to use strlcpy instead of memcpy
What is the actual behavior?
Good day,
There is code in the WiFi implementation of the ESP32-S3 (and, I suspect, in the WiFi code of other Espressif processors as well) that reads uninitialized memory.
The circumstances are such that this does not currently lead to any serious consequences, but it is still not a good practice and may become problematic in other environments.
Please note the
memcpy()call: the key_name (an ASCIIZ string) is copied into a structure field.The destination field itself is fine and has sufficient space allocated.
However, the calls to
wifi_nvs_cfg_item_init()raise concerns:And so on - this pattern appears throughout
net80211/ieee80211_nvs.cduring WiFi startup sequence.The issue is that
memcpy(..., key_name, 0x20)always copies 32 bytes fromkey_name, while the provided string literals are significantly shorter. As a result, the code reads bytes past the terminating null character, effectively accessing unrelated adjacent memory contents.It is fortunate that on the ESP32-S3 there is, by default, no strict memory protection or memory separation, so this does not immediately cause crashes or faults.
Nevertheless, this is still incorrect behavior and should ideally be replaced with a bounded string copy or a copy length based on
strlen(key_name) + 1.Steps to reproduce.
No steps to reproduce since no side effects.
Debug Logs.
Diagnostic report archive.
No response
More Information.
No response