Skip to content

Terminate 32-character SSIDs and 64-character passwords in the ESP32 WiFi modules - #1027

Merged
chrisgleissner merged 4 commits into
GideonZ:masterfrom
JC-000:pr/fix-wifi-ssid-pw
Oct 29, 2026
Merged

chrisgleissner merged 4 commits into
GideonZ:masterfrom
JC-000:pr/fix-wifi-ssid-pw

Conversation

@JC-000

@JC-000 JC-000 commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Fixes a bug that is present in all three ESP32 modules on master: software/wifi/raw_u64 (U64), software/u64ctrl (C64U) and software/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_c3 wifi_modem.c are identical on master.

The bug

  • SSID. ConnectCommand_t.ssid is char[32], so a 32-character SSID has no terminator. It is then read with %s and strcmp. The NVS lookup in wifi_store_ap reads a stored SSID into 32 bytes, but a 32-character SSID needs 33 with the NUL. The read fails with ESP_ERR_NVS_INVALID_LENGTH, the slot looks free, and the SSID is stored again on every connect.
  • Password. cmd_wifi_connect copies 64 bytes into pw[68] of an uninitialised stack ConnectCommand_t. A 64-character passphrase or hex PSK therefore has no terminator.
  • Password box (Nios side). BrowsableWifiAP::connect_ap in software/io/wifi/wifi.cc declares char password[64] and passes 64 to string_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.
  • Stored-password reads. wifi_find_password is called with a fixed length of 64 (twice, in wifi_connect_to_scanned) and wifi_connect_to_stored reads the password with 64. A 64-character password is 65 bytes in NVS.

The fix

In each module:

  • ssid becomes char[33] and is terminated after both copies into it (cmd_wifi_connect, wifi_connect_to_scanned). ev.pw[64] is terminated.
  • The NVS slot lookup reads into 33 bytes.
  • The stored SSID and password reads use sizeof of the destination instead of 32 and 64.
  • u64ctrl also copies the recovery hotspot credentials into last_connect in wifi_connect_to_hotspot. Those copies already use sizeof of the destination after a memset, 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_u64 with -O2. At -O2, GCC flags the strncpy(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_u64 and raw_c3 1.7, u64ctrl 1.20), with a changelog line in its rpc_dispatch.h, so the updater installs the fixed module on a device that already runs the current one.

Testing

  • All three modules build on 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).
  • Hardware: the U64E and the C64 Ultimate ran this code in ESP-IDF 6 builds (not the master build): the U64E with the raw_u64 commit, and the C64 Ultimate with the u64ctrl commit. 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.
  • Nios side (wifi.cc, one array size): builds with the U64 image (app_space PASS).
  • raw_c3: build only. There is no U2+L here to run it on.

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.

@chrisgleissner

chrisgleissner commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

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

JC-000 and others added 4 commits October 27, 2026 16:51
…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>
@JC-000
JC-000 force-pushed the pr/fix-wifi-ssid-pw branch from 1f5ef38 to 0a02a1e Compare October 28, 2026 00:06
@JC-000

JC-000 commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto master (60f16dc) and force-pushed; the head is now 0a02a1e3f.

Module versions bumped to the next number after master, each in the commit that changes that module, with a changelog line in its rpc_dispatch.h:

  • raw_u64: 1.6 → 1.7
  • u64ctrl: 1.19 → 1.20
  • raw_c3: 1.6 → 1.7

Re-checked after the rebase:

  • Master's new commits touch none of the PR's files. The fix code is byte-identical to the previous head; the only new change is the version bump.
  • Builds: U64 image (raw_u64), U64-II image (u64ctrl) and raw_c3 (make esp32_raw_c3) all build.
  • The hardware runs described in the PR were on the previous head; the rebased build hasn't been re-run on hardware.

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.

@chrisgleissner

Copy link
Copy Markdown
Collaborator

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 chrisgleissner self-assigned this Oct 28, 2026
@chrisgleissner chrisgleissner added bug Existing behavior is incorrect, broken, or regressed; includes supported documentation defects. network Networking and remote services: REST, Socket DMA, FTP, Telnet, and online access. labels Oct 28, 2026

@chrisgleissner chrisgleissner left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

@chrisgleissner
chrisgleissner merged commit 660d6d6 into GideonZ:master Oct 29, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Existing behavior is incorrect, broken, or regressed; includes supported documentation defects. network Networking and remote services: REST, Socket DMA, FTP, Telnet, and online access.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants