Skip to content

WiFi: check the size of requests and events before using them - #1080

Open
JC-000 wants to merge 8 commits into
GideonZ:masterfrom
JC-000:fix/wifi-rpc-size-checks-on1041
Open

JC-000 wants to merge 8 commits into
GideonZ:masterfrom
JC-000:fix/wifi-rpc-size-checks-on1041

Conversation

@JC-000

@JC-000 JC-000 commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1075

Problem

Nothing checked how many bytes a frame had before its command or event read them. A short or damaged frame was run from the stale rest of its buffer: CMD_SET_BAUD set a baud rate, flow control and line polarity from nothing (and then slept a second), CMD_WIFI_CONNECT used a Wi-Fi SSID and password, CMD_SEND_PACKET put as many bytes on the air as a stale length said. On the Ultimate's side a module that answers such a command "not implemented" echoes the command byte, which can be an event code: an 8-byte EVENT_CONNECTED made the task take an SSID from the buffer's earlier life ("Could not connect to " on a C64 Ultimate). #1041 added new payload readers (the SSID of a connect attempt, the optional BSSID, the radio setting events) that read their bytes unchecked in the same way.

What changed and why

Modules: a check before the dispatch. Nothing below a header; the whole request structure of CMD_SET_BAUD, CMD_GET_TIME (and on u64ctrl CMD_SET_SERIAL, CMD_SET_POWER_MODE, CMD_SET_WAKE_ON_WIFI, CMD_SET_RECOVERY_URL); 98 bytes for CMD_WIFI_CONNECT; for CMD_SEND_PACKET the 8-byte head and the length it claims. A short request with a header is answered ESP_ERR_INVALID_ARG, one without is dropped; both are counted and logged. Ultimate side: a check before the event switch inside #1041's RunModeThread, only for what the task reads, so an older module with shorter events still works. It covers what #1041 added: EVENT_CONNECT_PENDING (the SSID of the attempt), EVENT_RADIO_SETTING / EVENT_CONNECT_ANSWER / EVENT_RESTARTED (a header is all they read), EVENT_DISCONNECTED (its reason is read through a function that asks for the size itself). The connect request's check is the one cmd_wifi_connect makes (the password whole; BSSID and channel after it are optional). Compile-time assertions tie the senders' sizes to the sizes asked for, and the host task harness sends events at the size the module sends them.

Needs #1041, which is merged.

Products. the ESP32 modules (raw_u64, raw_c3, u64ctrl) and the Ultimate-side WiFi task, so every product with the WiFi module: the Ultimate 64, Ultimate 64 Elite II, C64 Ultimate and the Ultimate-II cartridges (U2+, U2+L). Host tests, and on hardware on both devices (see below).

How I tested it

Host: macOS, with the device-free tests run in an Ubuntu 24.04 container (g++ 13.3). The hardware runs used the test harness from the same machine.

Host tests (device-free, run before and after the change):

  • request and event size checks, including the payload readers added by WiFi: Disable turns the radio off, plus the connect and AP-list fixes from #1031 #1041: make -C software/wifi/size_tests test; make -C software/wifi/size_tests red; make host_tests
    • with the change: 12 tests pass per module build (raw_u64, raw_c3, u64ctrl); make host_tests builds the size-check tests and then stops at recoverymanifest (mbedTLS/ESP-IDF, absent in the container), which is unrelated
    • without it: red against the code without a check; the models of a missing or wrong check all fail as required

Hardware.

Device Firmware Ultimate FPGA C64 core
Ultimate 64 Elite 7b15448a7 126 1.50
C64 Ultimate 7b15448a7 126 1.50

On hardware, this build was flashed on both the U64 Elite and the C64 Ultimate and driven by a bench-only test build that sends deliberately short messages. Undersized module requests — a time request, a send-packet request claiming more bytes than it carries, and a baud-rate request — are refused by the module with an error reply, and the module answers the next normal request straight away. Undersized events toward the Ultimate — including the connect events this variant adds, read inside #1041's RunModeThread — are logged and dropped, with nothing acting on them (no connection or address is reported from a short event). Across real Wi-Fi connect/disconnect cycles, the network suites and the soaks, no legitimate message was rejected.

Standard profile: the flash round ran the network suites and the soaks (above), not the full standard profile. The host-driven ./run-tests standard profile does not exercise the module-side firmware in any case.

Untested

  • Ultimate-II cartridge (U2+ / U2+L): not tested. I do not have one. Not built either for the cartridges, because the build environment I use cannot build those targets; CI builds them.

Written with Claude Opus 5.5 in Claude Code.

JC-000 and others added 6 commits November 9, 2026 12:05
…fore using them

Nothing checked how many bytes a frame had before its command or event read them. A short or
damaged frame was run from the stale rest of its buffer: CMD_SET_BAUD set a baud rate, flow
control and line polarity from nothing (and then slept a second), CMD_WIFI_CONNECT a Wi-Fi
SSID and password, CMD_SEND_PACKET put as many bytes on the air as a stale length said.
On the Ultimate's side a module that answers such a command "not implemented" echoes the
command byte, which can be an event code: an EVENT_CONNECTED of 8 bytes made the task take an
SSID from the buffer's earlier life (seen on a C64 Ultimate: "Could not connect to <garbage>").

Modules (raw_u64, raw_c3, u64ctrl): rpc_request_too_short() in rpc_dispatch.c, before the
dispatch: nothing below a header; the whole request structure of CMD_SET_BAUD, CMD_GET_TIME
and, on u64ctrl, CMD_SET_SERIAL, CMD_SET_POWER_MODE, CMD_SET_WAKE_ON_WIFI,
CMD_SET_RECOVERY_URL (the Ultimate sends the whole structure of each); 98 bytes for
CMD_WIFI_CONNECT as cmd_wifi_connect has it; for CMD_SEND_PACKET the 8-byte head and the
length it claims. A short request with a header is answered ESP_ERR_INVALID_ARG, one without is
dropped; both are counted and logged. Every other command reads its header only.

Ultimate (wifi.cc, wifi_event_size.h): wifi_event_too_short() before the event switch: nothing
below a header; EVENT_CONNECTED its 32-byte SSID, EVENT_GOTIP the address, EVENT_RECV_PACKET its
length and that many bytes. Only what the task reads, so an older module with shorter events
still works. The two internal events (sendEvent, sendConnectEvent) now set their size.

Host tests (software/wifi/tests/rpc, `make test`, `make red`): every command below a header, the
request structures one byte short and whole, the packet length, each event; RED against the code
without a check (1031 failing checks per module), 14 models of a missing or wrong check, all killed.
The module identity is bumped as a placeholder; the maintainers assign the number at merge.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
GideonZ#1041 (wifi-fixes) adds its own software/wifi/tests; the trees must not share a directory.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The handler prints the address and the flag after the gateway; the check stopped at the address. The modules send the event whole (sizeof(event_pkt_got_ip)), so nothing real is rejected.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ptional BSSID

Re-places the Ultimate-side check inside GideonZ#1041's RunModeThread and extends it to what GideonZ#1041
adds: EVENT_CONNECT_PENDING (the SSID of the attempt, read by feedback.begin(); the internal
sender now sets the size), EVENT_RADIO_SETTING / EVENT_CONNECT_ANSWER / EVENT_RESTARTED (a header
is all they read), EVENT_DISCONNECTED (its reason is read through wifi_disconnect_reason(), which
asks for the size itself, so a bare header is let through). The connect request's check is now the
one cmd_wifi_connect makes (the password whole; the BSSID and channel after it are optional).
The module identity is bumped as a placeholder; the maintainers assign the number at merge.
Runs in make host_tests.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ed for, as compile-time assertions

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ize the module sends them

software/io/wifi/tests compiles RunModeThread, which now calls wifi_event_too_short() and counts in wifi_events_short: the harness includes the header and declares the counter, and its scripted EVENT_CONNECTED / EVENT_CONNECT_PENDING / EVENT_GOTIP are the whole structures, as the module and sendConnectPending send them (a header-only one was never real). The drop message prints the command in decimal: the firmware printf has no %b argument.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@chrisgleissner

Copy link
Copy Markdown
Collaborator

@JC-000 Please rebase this PR on top of the latest master now that PR #1041 has been merged. This also applies to all other PRs which contain changes that were already part of that PR. Many thanks.

Clean merge, no conflicts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@JC-000

JC-000 commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

Master is merged into this branch now that #1041 is in (e9ec844), so the diff here is only the size checks. The branch is updated by a merge, not a rebase, following your note on #1041 that base updates go in by merge. #1042 was the only other PR carrying #1041's commits; it has the same update (9d24bbb), with the old sed models deleted as on master. The host tests of both pass.

Written with Claude Sonnet and Claude Opus in Claude Code.

@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 Nov 10, 2026
@chrisgleissner

Copy link
Copy Markdown
Collaborator

@JC-000 thanks for this. #1041 is merged and the branch now merges cleanly with green CI. Can the Draft flag come off, or is something else still outstanding? If it is ready, I'll review it.

…deonZ#1080)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@JC-000 JC-000 changed the title # WiFi: check the size of requests and events before using them (on top of #1041) WiFi: check the size of requests and events before using them (on top of #1041) Nov 10, 2026
@JC-000 JC-000 changed the title WiFi: check the size of requests and events before using them (on top of #1041) WiFi: check the size of requests and events before using them Nov 10, 2026
@JC-000
JC-000 marked this pull request as ready for review November 10, 2026 14:59
@JC-000

JC-000 commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

Yes, it's ready. #1041 was the only thing it waited for. It's now up to date with master and CI is green, and I've updated the description so it no longer refers to #1041 as pending. The hardware runs in the description used a bench build from before this merge; the merge brings in master only. One note: its module version numbers will need renumbering if #1042 or the UART PRs (#1077-#1079) merge first.

Written with Claude Opus and Claude Sonnet in Claude Code.

@chrisgleissner

Copy link
Copy Markdown
Collaborator

Thanks. I read the check in wifi_event_size.h and the module side in raw_u64; the minimum sizes match what the handlers read, and the old-module case (shorter events) is covered. Nothing to change in the code.

Two things before it can merge. The module versions need renumbering depending on the order of #1042 and #1077 to #1079, so please agree an order. The cartridge targets are CI-built only; fine to state, but then this should go in after the Elite and C64 Ultimate runs are repeated on the final merge result.

@JC-000

JC-000 commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

Proposed merge order: #1077, #1078, #1079, #1080, then #1042. #1077 only adds tests and has no module version. #1078 contains #1077's tests and #1079 contains #1078's change, so they go in that order. #1080 follows, and I'll repeat the Elite and C64 Ultimate runs on the merge result before it goes in. #1042 goes last, as you asked on that PR.

As each PR lands, I'll merge master into the next one and bump its module minor version past the one before. No rebases.

Written with Claude Opus and Claude Sonnet in Claude Code.

@chrisgleissner

Copy link
Copy Markdown
Collaborator

Sounds great. BTW, I will be on holiday the next 10 days so cannot merge. But I hope @GideonZ can take over... Your changes BTW have been absolutely stellar @JC-000 , as is your absolutely mind blowing Trx64 Multicore work. A true game changer in how I use my C64U, and I mean that. Fantastic work.

@JC-000

JC-000 commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, much appreciated. TRX64 is Jondalar's work though, not mine. The credit for that one goes to him.

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.

WiFi RPC: requests and events are read without checking their length first

2 participants