Repository navigation
Terminate 32-character SSIDs and 64-character passwords in the ESP32 WiFi modules - #1027
Conversation
|
Hi @JC-000, Thanks for having spotted and fixed this bug. As it addresses a concrete bug, I think this PR of yours should be merged first. You wrote 'This PR does not change the module versions'. Could you please rebase this PR against master and then update the module versions? Also, a lot of your work is low level and covers both the U64, C64U, and U2+L. It would be ideal if you could test code that you contribute against all these main target devices. Have you considered purchasing a U2+L? I don't mind testing the odd PR for a firmware dev who lacks one of the three devices mentioned above. But it may make sense for you getting these devices yourself as well, so you can demonstrate that your changes work on real hardware. Finally, you wrote 'and the red case on any device' was not tested. Could you please perform these tests, or describe why you could not? Thanks |
…s 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 <noreply@anthropic.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s 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 <noreply@anthropic.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s 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 <noreply@anthropic.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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 <noreply@anthropic.com>
1f5ef38 to
0a02a1e
Compare
|
Rebased onto master (60f16dc) and force-pushed; the head is now Module versions bumped to the next number after master, each in the commit that changes that module, with a changelog line in its
Re-checked after the rebase:
I don't have a U2+L at the moment. The raw_c3 module builds, but I can't run it on hardware, so I'd gladly take you up on testing the U2+L side. I'm looking into getting one. Written with Claude Opus 5.5 in Claude Code. |
|
Thanks @JC-000 , that is excellent! I plan to test this tomorrow on the Ultimate II+L. If you reach out to Gideon and ask if he can make you a special deal for the UII+L given you are such an avid contributor, maybe he can give you a rebate. It's worth asking :-) |
chrisgleissner
left a comment
There was a problem hiding this comment.
Reviewed 0a02a1e3f158ac1b8842d1335b0246c9a9a0d300; no blocking findings.
Verified on my Ultimate II+L, attached to a C64 Ultimate:
- Live firmware reports this exact commit, FPGA 126, and ESP32 WiFi Bridge V1.7. The uploaded updater's SHA-256 matches the local build.
- A 32-byte SSID connects with both a 63-character passphrase and a 64-digit raw WPA2 PSK. The PSK was derived from the AP's passphrase using PBKDF2-HMAC-SHA1, so the AP need not accept a raw key in its configuration UI. DHCP and REST over WiFi pass.
- The password dialog rejects a 65th character. The resulting 64-digit key authenticates successfully.
- After a power cycle through the C64U, the U2+L reconnects using the saved 32-byte SSID and 64-digit key, without entering credentials.
- Connecting and storing another AP works; automatic fallback reaches a known network when that AP is unavailable. Both devices pass final ping, REST, FTP, Telnet, identity and DMA checks. Normal WiFi is restored.
Host checks use the actual extracted functions, NVS/queue stubs, ASan/UBSan and patterned stack initialization: 61/61 pass in each of the three modules; identical checks expose 19 failures per module on the base commit. These cover scanned/stored credential reads and NVS slot reuse/preservation. There is no hardware red/base comparison, and this run does not independently prove scanned-path boundary-key retrieval or NVS slot preservation on hardware.
The fix belongs in the owning modules' buffers/NVS reads and the shared password dialog; the RPC's 32/64-byte fields remain unchanged. CI Build is green. Thanks for the fix!
Fixes a bug that is present in all three ESP32 modules on
master:software/wifi/raw_u64(U64),software/u64ctrl(C64U) andsoftware/wifi/raw_c3(U2+L). Four commits: one per ESP32 module (4 files, +14 / -9 each) and one for the Nios-side password box (1 file, +1 / -1). The code is the same in each; the raw_u64 and raw_c3wifi_modem.care identical onmaster.The bug
ConnectCommand_t.ssidischar[32], so a 32-character SSID has no terminator. It is then read with%sandstrcmp. The NVS lookup inwifi_store_apreads a stored SSID into 32 bytes, but a 32-character SSID needs 33 with the NUL. The read fails withESP_ERR_NVS_INVALID_LENGTH, the slot looks free, and the SSID is stored again on every connect.cmd_wifi_connectcopies 64 bytes intopw[68]of an uninitialised stackConnectCommand_t. A 64-character passphrase or hex PSK therefore has no terminator.BrowsableWifiAP::connect_apinsoftware/io/wifi/wifi.ccdeclareschar password[64]and passes 64 tostring_box, which takes that many characters plus the terminator. A 64-character key therefore writes its NUL one byte past the buffer. This is the only user interface path that can enter a 64-character key.wifi_find_passwordis called with a fixed length of 64 (twice, inwifi_connect_to_scanned) andwifi_connect_to_storedreads the password with 64. A 64-character password is 65 bytes in NVS.The fix
In each module:
ssidbecomeschar[33]and is terminated after both copies into it (cmd_wifi_connect,wifi_connect_to_scanned).ev.pw[64]is terminated.sizeofof the destination instead of 32 and 64.last_connectinwifi_connect_to_hotspot. Those copies already usesizeofof the destination after amemset, so they follow the new size and are unchanged.The password box (
wifi.cc) gets a 65-byte buffer, as its own commit.Why now
The ESP-IDF 6 move (#996) builds
raw_u64with-O2. At-O2, GCC flags thestrncpy(last_connect.ssid, ..., 32)into the 32-byte array (stringop-truncation), and the module builds with-Werror, so that build stops. #996 therefore depends on the raw_u64 commit of this PR. That reason applies to raw_u64 only; u64ctrl and raw_c3 are fixed because they have the same bug. It is a bug on its own, with no ESP-IDF 6 involved.Module version
Each module commit bumps that module's version to the next number after
master(raw_u64andraw_c31.7,u64ctrl1.20), with a changelog line in itsrpc_dispatch.h, so the updater installs the fixed module on a device that already runs the current one.Testing
master(rebased onto 60f16dc; ESP-IDF 5.3.1,-Og), with no new warnings: raw_u64 (U64 image), u64ctrl (C64U image) and raw_c3 (make esp32_raw_c3).masterbuild): the U64E with theraw_u64commit, and the C64 Ultimate with theu64ctrlcommit. Both devices connected to their access point and ran their suites. The 64-character-key case was not reproduced on hardware. Common access points (a UniFi one here) reject a 64-hex-digit WPA2 key, so that case rests on reading the code. A SoftAP on an ESP32 could prove it, if you want that. The SSID half (the slot being stored again on every connect) cannot be seen from the Nios side, so there is no hardware red/green for it either.wifi.cc, one array size): builds with the U64 image (app_space PASS).Not tested: raw_c3 on a device, and the red case on any device.
Written with Claude Sonnet 5.5 in Claude Code; reviewed adversarially by separate Claude Opus 5.5 sessions before posting. Rebase and version bumps with Claude Opus 5.5.