From 49b9ff1379d3c1081bbda687e5a548a423965a64 Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Mon, 26 Oct 2026 09:10:40 -0500 Subject: [PATCH 1/4] Terminate and round-trip 32-character SSIDs and 64-character passwords in the U64 WiFi bridge ConnectCommand_t.ssid was char[32], so a 32-character SSID had no terminator and was later read with %s and strcmp. Make it char[33] and terminate it after the two copies into it. The NVS slot lookup read the stored SSID into 32 bytes, so a 32-character SSID (33 with the NUL) failed with ESP_ERR_NVS_INVALID_LENGTH, looked like a free slot, and was stored again on every connect; read into 33 bytes. The reconnect read into last_connect.ssid now uses sizeof. The password has the same flaw: cmd_wifi_connect copied 64 bytes into pw[68] of an uninitialised stack ConnectCommand_t, so a 64-character passphrase or hex PSK had no terminator. Terminate it (ev.pw[64] = 0), and read the stored password with the size of last_connect.pw instead of 64 (wifi_find_password callers and the recent-AP read), since a 64-character password is 65 bytes in NVS. Bump the module version to 1.7 (IDENT_MINOR, with a changelog line in rpc_dispatch.h), so that the updater installs the fixed module on a device that already runs the current one. Co-Authored-By: Claude Sonnet 5.5 Co-Authored-By: Claude Opus 5.5 --- software/wifi/raw_u64/main/rpc_dispatch.c | 2 ++ software/wifi/raw_u64/main/rpc_dispatch.h | 6 ++++-- software/wifi/raw_u64/main/wifi_modem.c | 13 +++++++------ software/wifi/raw_u64/main/wifi_modem.h | 2 +- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/software/wifi/raw_u64/main/rpc_dispatch.c b/software/wifi/raw_u64/main/rpc_dispatch.c index 8663e1cdd..68a618e2e 100644 --- a/software/wifi/raw_u64/main/rpc_dispatch.c +++ b/software/wifi/raw_u64/main/rpc_dispatch.c @@ -106,7 +106,9 @@ void cmd_wifi_connect(command_buf_t *buf) ev.command = CMD_WIFI_CONNECT; ev.auth_mode = param->auth_mode; memcpy(&ev.ssid, param->ssid, 32); + ev.ssid[32] = 0; memcpy(&ev.pw, param->password, 64); + ev.pw[64] = 0; if (xQueueSend(connect_commands, &ev, 1000) == pdFALSE) { resp->esp_err = ESP_ERR_NO_MEM; diff --git a/software/wifi/raw_u64/main/rpc_dispatch.h b/software/wifi/raw_u64/main/rpc_dispatch.h index 7e6a4b9a5..596cd040f 100644 --- a/software/wifi/raw_u64/main/rpc_dispatch.h +++ b/software/wifi/raw_u64/main/rpc_dispatch.h @@ -26,13 +26,15 @@ void start_dispatch(QueueHandle_t queue); void send_button_event(uint8_t button); void send_keepalive(); -#define IDENT_STRING "ESP32 WiFi Bridge V1.6" +#define IDENT_STRING "ESP32 WiFi Bridge V1.7" #define IDENT_MAJOR 1 +// 1.7: terminates 32-character SSIDs and 64-character passwords, and reads +// them back from NVS whole. // 1.6: the connector retries after a dropped link instead of idling in // Disconnected forever. The Ultimate's updater only reflashes the module // when this differs from what the module reports, so a module change that // does not bump it is silently never installed. -#define IDENT_MINOR 6 +#define IDENT_MINOR 7 #define MULTITHREADED 0 #define DISPATCHER_STACK 3072 diff --git a/software/wifi/raw_u64/main/wifi_modem.c b/software/wifi/raw_u64/main/wifi_modem.c index 59c95302b..9276dd70b 100644 --- a/software/wifi/raw_u64/main/wifi_modem.c +++ b/software/wifi/raw_u64/main/wifi_modem.c @@ -369,8 +369,8 @@ esp_err_t wifi_store_ap(ConnectCommand_t *cmd) key[0] = 's'; key[1] = 'a' + i; key[2] = '0'; // SSID sa, sb, sc, sd, ... - size_t len = 32; - char ssid_buf[32]; + size_t len = 33; + char ssid_buf[33]; // see if the ssid has been stored already err = nvs_get_str(handle, key, ssid_buf, &len); if (err != ESP_OK) { // nope, it doesn't exist @@ -545,10 +545,10 @@ esp_err_t wifi_connect_to_scanned(int index) // now look up password err = ESP_OK; if (ap_info[index].authmode) { - err = wifi_find_password(ap_info[index].ssid, last_connect.pw, 64, &last_connect.list_index); + err = wifi_find_password(ap_info[index].ssid, last_connect.pw, sizeof(last_connect.pw), &last_connect.list_index); } else { // no authentication required. But make sure the user has configured this AP. - err = wifi_find_password(ap_info[index].ssid, last_connect.pw, 64, &last_connect.list_index); + err = wifi_find_password(ap_info[index].ssid, last_connect.pw, sizeof(last_connect.pw), &last_connect.list_index); if ((err == ESP_OK)&&(last_connect.pw[0] != 0)) { // AP is known and configured, however, with a password. Do not allow to connect. err = ESP_ERR_NOT_FOUND; @@ -556,6 +556,7 @@ esp_err_t wifi_connect_to_scanned(int index) } if (err == ESP_OK) { // known AP found strncpy(last_connect.ssid, (const char *)(ap_info[index].ssid), 32); + last_connect.ssid[32] = 0; last_connect.auth_mode = ap_info[index].authmode; return attempt_connect(last_connect.ssid, last_connect.pw, last_connect.auth_mode); } @@ -582,11 +583,11 @@ esp_err_t wifi_connect_to_stored(int index) key[1] = 'a' + index; key[2] = '0'; // SSID sa, sb, sc, sd, ... - size_t len = 32; + size_t len = sizeof(last_connect.ssid); err = nvs_get_str(handle, key, last_connect.ssid, &len); if (err == ESP_OK) { key[0] = 'p'; // password pa, pb, pc, pd, ... - len = 64; + len = sizeof(last_connect.pw); err = nvs_get_str(handle, key, last_connect.pw, &len); if (err == ESP_OK) { key[0] = 'm'; // authmode ma, mb, mc, md, ... diff --git a/software/wifi/raw_u64/main/wifi_modem.h b/software/wifi/raw_u64/main/wifi_modem.h index d9971db32..20a571352 100644 --- a/software/wifi/raw_u64/main/wifi_modem.h +++ b/software/wifi/raw_u64/main/wifi_modem.h @@ -7,7 +7,7 @@ extern QueueHandle_t connect_commands; extern SemaphoreHandle_t connect_semaphore; typedef struct { - char ssid[32]; + char ssid[33]; // 32 SSID bytes plus the terminator char pw[68]; uint8_t auth_mode; uint8_t command; From 8fbd9dcf98a8addf666e19177d0af9297d3e4a15 Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Mon, 26 Oct 2026 19:12:50 -0500 Subject: [PATCH 2/4] Terminate and round-trip 32-character SSIDs and 64-character passwords in the U64 control module ConnectCommand_t.ssid was char[32], so a 32-character SSID had no terminator and was later read with %s and strcmp. Make it char[33] and terminate it after the two copies into it. The NVS slot lookup read the stored SSID into 32 bytes, so a 32-character SSID (33 with the NUL) failed with ESP_ERR_NVS_INVALID_LENGTH, looked like a free slot, and was stored again on every connect; read into 33 bytes. The reconnect read into last_connect.ssid now uses sizeof. The password has the same flaw: cmd_wifi_connect copied 64 bytes into pw[68] of an uninitialised stack ConnectCommand_t, so a 64-character passphrase or hex PSK had no terminator. Terminate it (ev.pw[64] = 0), and read the stored password with the size of last_connect.pw instead of 64 (wifi_find_password callers and the recent-AP read), since a 64-character password is 65 bytes in NVS. The recovery hotspot copies in wifi_connect_to_hotspot already use sizeof(last_connect.ssid) and sizeof(last_connect.pw) after a memset, so they follow the new size and need no change. Bump the module version to 1.20 (IDENT_MINOR, with a changelog line in rpc_dispatch.h), so that the updater installs the fixed module on a device that already runs the current one. Co-Authored-By: Claude Sonnet 5.5 Co-Authored-By: Claude Opus 5.5 --- software/u64ctrl/main/rpc_dispatch.c | 2 ++ software/u64ctrl/main/rpc_dispatch.h | 6 ++++-- software/u64ctrl/main/wifi_modem.c | 13 +++++++------ software/u64ctrl/main/wifi_modem.h | 2 +- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/software/u64ctrl/main/rpc_dispatch.c b/software/u64ctrl/main/rpc_dispatch.c index 5c010bff7..db3771ce2 100644 --- a/software/u64ctrl/main/rpc_dispatch.c +++ b/software/u64ctrl/main/rpc_dispatch.c @@ -112,7 +112,9 @@ void cmd_wifi_connect(command_buf_t *buf) ev.command = CMD_WIFI_CONNECT; ev.auth_mode = param->auth_mode; memcpy(&ev.ssid, param->ssid, 32); + ev.ssid[32] = 0; memcpy(&ev.pw, param->password, 64); + ev.pw[64] = 0; if (xQueueSend(connect_commands, &ev, 1000) == pdFALSE) { resp->esp_err = ESP_ERR_NO_MEM; diff --git a/software/u64ctrl/main/rpc_dispatch.h b/software/u64ctrl/main/rpc_dispatch.h index f385ff7c4..c4a995d1a 100644 --- a/software/u64ctrl/main/rpc_dispatch.h +++ b/software/u64ctrl/main/rpc_dispatch.h @@ -26,8 +26,10 @@ void start_dispatch(QueueHandle_t queue); void send_button_event(uint8_t button); void send_keepalive(); -#define IDENT_STRING "ESP32 WiFi Bridge V1.19" +#define IDENT_STRING "ESP32 WiFi Bridge V1.20" #define IDENT_MAJOR 1 +// 1.20: terminates 32-character SSIDs and 64-character passwords, and reads +// them back from NVS whole. // 1.19: a recovery also tries the my_hotspot access point after the stored ones. // 1.18: recovers the firmware over Wi-Fi when the switch is held down for three // seconds while off, and stores the recovery server the menu sets. @@ -38,7 +40,7 @@ void send_keepalive(); // when this is higher than what the module reports, so a module change that // does not bump it is silently never installed, and an older release never // replaces a newer module. -#define IDENT_MINOR 19 +#define IDENT_MINOR 20 #define MULTITHREADED 0 #define DISPATCHER_STACK 3072 diff --git a/software/u64ctrl/main/wifi_modem.c b/software/u64ctrl/main/wifi_modem.c index 1be27d7cb..6b1020a98 100644 --- a/software/u64ctrl/main/wifi_modem.c +++ b/software/u64ctrl/main/wifi_modem.c @@ -474,8 +474,8 @@ esp_err_t wifi_store_ap(ConnectCommand_t *cmd) key[0] = 's'; key[1] = 'a' + i; key[2] = '0'; // SSID sa, sb, sc, sd, ... - size_t len = 32; - char ssid_buf[32]; + size_t len = 33; + char ssid_buf[33]; // see if the ssid has been stored already err = nvs_get_str(handle, key, ssid_buf, &len); if (err != ESP_OK) { // nope, it doesn't exist @@ -650,10 +650,10 @@ esp_err_t wifi_connect_to_scanned(int index) // now look up password err = ESP_OK; if (ap_info[index].authmode) { - err = wifi_find_password(ap_info[index].ssid, last_connect.pw, 64, &last_connect.list_index); + err = wifi_find_password(ap_info[index].ssid, last_connect.pw, sizeof(last_connect.pw), &last_connect.list_index); } else { // no authentication required. But make sure the user has configured this AP. - err = wifi_find_password(ap_info[index].ssid, last_connect.pw, 64, &last_connect.list_index); + err = wifi_find_password(ap_info[index].ssid, last_connect.pw, sizeof(last_connect.pw), &last_connect.list_index); if ((err == ESP_OK) && (last_connect.pw[0] != 0)) { // AP is known and configured, however, with a password. Do not allow to connect. err = ESP_ERR_NOT_FOUND; @@ -661,6 +661,7 @@ esp_err_t wifi_connect_to_scanned(int index) } if (err == ESP_OK) { // known AP found strncpy(last_connect.ssid, (const char *)(ap_info[index].ssid), 32); + last_connect.ssid[32] = 0; last_connect.auth_mode = ap_info[index].authmode; return attempt_connect(last_connect.ssid, last_connect.pw, last_connect.auth_mode); } @@ -687,11 +688,11 @@ esp_err_t wifi_connect_to_stored(int index) key[1] = 'a' + index; key[2] = '0'; // SSID sa, sb, sc, sd, ... - size_t len = 32; + size_t len = sizeof(last_connect.ssid); err = nvs_get_str(handle, key, last_connect.ssid, &len); if (err == ESP_OK) { key[0] = 'p'; // password pa, pb, pc, pd, ... - len = 64; + len = sizeof(last_connect.pw); err = nvs_get_str(handle, key, last_connect.pw, &len); if (err == ESP_OK) { key[0] = 'm'; // authmode ma, mb, mc, md, ... diff --git a/software/u64ctrl/main/wifi_modem.h b/software/u64ctrl/main/wifi_modem.h index 8328dab24..643334acd 100644 --- a/software/u64ctrl/main/wifi_modem.h +++ b/software/u64ctrl/main/wifi_modem.h @@ -7,7 +7,7 @@ extern QueueHandle_t connect_commands; extern SemaphoreHandle_t connect_semaphore; typedef struct { - char ssid[32]; + char ssid[33]; // 32 SSID bytes plus the terminator char pw[68]; uint8_t auth_mode; uint8_t command; From c711ab6fe8f192d3351b246dc742b1188293d269 Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Mon, 26 Oct 2026 19:12:50 -0500 Subject: [PATCH 3/4] Terminate and round-trip 32-character SSIDs and 64-character passwords in the C3 WiFi bridge ConnectCommand_t.ssid was char[32], so a 32-character SSID had no terminator and was later read with %s and strcmp. Make it char[33] and terminate it after the two copies into it. The NVS slot lookup read the stored SSID into 32 bytes, so a 32-character SSID (33 with the NUL) failed with ESP_ERR_NVS_INVALID_LENGTH, looked like a free slot, and was stored again on every connect; read into 33 bytes. The reconnect read into last_connect.ssid now uses sizeof. The password has the same flaw: cmd_wifi_connect copied 64 bytes into pw[68] of an uninitialised stack ConnectCommand_t, so a 64-character passphrase or hex PSK had no terminator. Terminate it (ev.pw[64] = 0), and read the stored password with the size of last_connect.pw instead of 64 (wifi_find_password callers and the recent-AP read), since a 64-character password is 65 bytes in NVS. Bump the module version to 1.7 (IDENT_MINOR, with a changelog line in rpc_dispatch.h), so that the updater installs the fixed module on a device that already runs the current one. Co-Authored-By: Claude Sonnet 5.5 Co-Authored-By: Claude Opus 5.5 --- software/wifi/raw_c3/main/rpc_dispatch.c | 2 ++ software/wifi/raw_c3/main/rpc_dispatch.h | 6 ++++-- software/wifi/raw_c3/main/wifi_modem.c | 13 +++++++------ software/wifi/raw_c3/main/wifi_modem.h | 2 +- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/software/wifi/raw_c3/main/rpc_dispatch.c b/software/wifi/raw_c3/main/rpc_dispatch.c index 54fa6263f..ab24b9811 100644 --- a/software/wifi/raw_c3/main/rpc_dispatch.c +++ b/software/wifi/raw_c3/main/rpc_dispatch.c @@ -106,7 +106,9 @@ void cmd_wifi_connect(command_buf_t *buf) ev.command = CMD_WIFI_CONNECT; ev.auth_mode = param->auth_mode; memcpy(&ev.ssid, param->ssid, 32); + ev.ssid[32] = 0; memcpy(&ev.pw, param->password, 64); + ev.pw[64] = 0; if (xQueueSend(connect_commands, &ev, 1000) == pdFALSE) { resp->esp_err = ESP_ERR_NO_MEM; diff --git a/software/wifi/raw_c3/main/rpc_dispatch.h b/software/wifi/raw_c3/main/rpc_dispatch.h index e17bcb2f1..aad4296ad 100644 --- a/software/wifi/raw_c3/main/rpc_dispatch.h +++ b/software/wifi/raw_c3/main/rpc_dispatch.h @@ -24,13 +24,15 @@ void start_dispatch(QueueHandle_t queue); void send_button_event(uint8_t button); void send_keepalive(); -#define IDENT_STRING "ESP32 WiFi Bridge V1.6" +#define IDENT_STRING "ESP32 WiFi Bridge V1.7" #define IDENT_MAJOR 1 +// 1.7: terminates 32-character SSIDs and 64-character passwords, and reads +// them back from NVS whole. // 1.6: the connector retries after a dropped link instead of idling in // Disconnected forever. The Ultimate's updater only reflashes the module // when this differs from what the module reports, so a module change that // does not bump it is silently never installed. -#define IDENT_MINOR 6 +#define IDENT_MINOR 7 #define MULTITHREADED 0 #define DISPATCHER_STACK 3072 diff --git a/software/wifi/raw_c3/main/wifi_modem.c b/software/wifi/raw_c3/main/wifi_modem.c index 59c95302b..9276dd70b 100644 --- a/software/wifi/raw_c3/main/wifi_modem.c +++ b/software/wifi/raw_c3/main/wifi_modem.c @@ -369,8 +369,8 @@ esp_err_t wifi_store_ap(ConnectCommand_t *cmd) key[0] = 's'; key[1] = 'a' + i; key[2] = '0'; // SSID sa, sb, sc, sd, ... - size_t len = 32; - char ssid_buf[32]; + size_t len = 33; + char ssid_buf[33]; // see if the ssid has been stored already err = nvs_get_str(handle, key, ssid_buf, &len); if (err != ESP_OK) { // nope, it doesn't exist @@ -545,10 +545,10 @@ esp_err_t wifi_connect_to_scanned(int index) // now look up password err = ESP_OK; if (ap_info[index].authmode) { - err = wifi_find_password(ap_info[index].ssid, last_connect.pw, 64, &last_connect.list_index); + err = wifi_find_password(ap_info[index].ssid, last_connect.pw, sizeof(last_connect.pw), &last_connect.list_index); } else { // no authentication required. But make sure the user has configured this AP. - err = wifi_find_password(ap_info[index].ssid, last_connect.pw, 64, &last_connect.list_index); + err = wifi_find_password(ap_info[index].ssid, last_connect.pw, sizeof(last_connect.pw), &last_connect.list_index); if ((err == ESP_OK)&&(last_connect.pw[0] != 0)) { // AP is known and configured, however, with a password. Do not allow to connect. err = ESP_ERR_NOT_FOUND; @@ -556,6 +556,7 @@ esp_err_t wifi_connect_to_scanned(int index) } if (err == ESP_OK) { // known AP found strncpy(last_connect.ssid, (const char *)(ap_info[index].ssid), 32); + last_connect.ssid[32] = 0; last_connect.auth_mode = ap_info[index].authmode; return attempt_connect(last_connect.ssid, last_connect.pw, last_connect.auth_mode); } @@ -582,11 +583,11 @@ esp_err_t wifi_connect_to_stored(int index) key[1] = 'a' + index; key[2] = '0'; // SSID sa, sb, sc, sd, ... - size_t len = 32; + size_t len = sizeof(last_connect.ssid); err = nvs_get_str(handle, key, last_connect.ssid, &len); if (err == ESP_OK) { key[0] = 'p'; // password pa, pb, pc, pd, ... - len = 64; + len = sizeof(last_connect.pw); err = nvs_get_str(handle, key, last_connect.pw, &len); if (err == ESP_OK) { key[0] = 'm'; // authmode ma, mb, mc, md, ... diff --git a/software/wifi/raw_c3/main/wifi_modem.h b/software/wifi/raw_c3/main/wifi_modem.h index d9971db32..20a571352 100644 --- a/software/wifi/raw_c3/main/wifi_modem.h +++ b/software/wifi/raw_c3/main/wifi_modem.h @@ -7,7 +7,7 @@ extern QueueHandle_t connect_commands; extern SemaphoreHandle_t connect_semaphore; typedef struct { - char ssid[32]; + char ssid[33]; // 32 SSID bytes plus the terminator char pw[68]; uint8_t auth_mode; uint8_t command; From 0a02a1e3f158ac1b8842d1335b0246c9a9a0d300 Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Mon, 26 Oct 2026 19:35:11 -0500 Subject: [PATCH 4/4] Leave room for the terminator in the Wi-Fi AP list password box BrowsableWifiAP::connect_ap declared char password[64] and passed 64 to string_box, which takes that many characters plus the terminator, so a 64-character key wrote its NUL one byte past the buffer. Make the buffer 65 bytes. This is the only user interface path that can enter a 64-character key. Co-Authored-By: Claude Sonnet 5.5 --- software/io/wifi/wifi.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/software/io/wifi/wifi.cc b/software/io/wifi/wifi.cc index 2df2ba61a..564a4e768 100644 --- a/software/io/wifi/wifi.cc +++ b/software/io/wifi/wifi.cc @@ -433,7 +433,7 @@ void BrowsableWifiAP :: fetch_context_items(IndexedList&items) SubsysResultCode_e BrowsableWifiAP :: connect_ap(SubsysCommand *cmd) { - char password[64] = { 0 }; + char password[65] = { 0 }; // string_box takes 64 characters plus the terminator BrowsableWifiAP *bap = (BrowsableWifiAP *)cmd->functionID; if (bap->auth) { if(cmd->user_interface) {