Repository navigation
Keep USB and Ethernet transfers inside the buffers they were given (#822) - #843
Conversation
|
Hi @enver-haase , any chance for you to add a unit or ideally E2E test? As it is, without any test, this PR is not merge-ready. Thanks |
chrisgleissner
left a comment
There was a problem hiding this comment.
Request changes for 3.15b: the build proves these files compile, but no regression test exercises any changed path. Please add a host test (and wire it into CI) for: (1) AX88772 frame assembly at 0, 1, 1500, 1536 and 1537 bytes, with canaries before and after the caller buffer; assert the four-byte length/complement header and exact copied payload; (2) RMII frames of 42, 59, and 60 bytes; assert the first two are zero-padded to 60 without modifying the caller buffer; and (3) control_exchange with a non-8-byte setup; assert its semaphore is released before it returns -10. Narrow pure helpers are acceptable if that is what makes these memory-safety contracts host-testable. A hardware AX88772 transfer/capture remains useful additional evidence, but the deterministic regression tests are required before this patch can be release-ready.
|
Will look into it! |
|
Thanks @enver-haase . Once you got some tests added, thanks for also updating the PR here with red/green TDD results, demonstrating that the tests fail against the unpatched firmware and pass against the patched one. |
control_exchange() takes the mutex on entry and gives it back on every path out except one: a setup length other than 8 returns -10 while still holding it. Every later USB operation would then wait its full five seconds and fail, which on this device means the keyboard, mass storage and the Ethernet adapter all stop at once. Latent today, because no caller passes anything but 8. Found by reading, in the sweep in GideonZ#822, problem 5.
output_packet() wrote the Ethernet minimum into the length register but left
the buffer alone:
RMII_TX_LENGTH = (uint16_t)((pkt_len < 60)?60:pkt_len);
The engine transmits as many bytes as that register names, so for a 42 byte ARP
request it read 18 bytes past the frame and put them on the wire. Nothing is
corrupted -- the transmit path only reads -- but those bytes leave the device,
and with MEM_LIBC_MALLOC set the pbufs come from pvPortMalloc, so what goes out
is the neighbouring heap block. Every ARP request the device sends does this.
Short frames are now copied into a buffer this driver owns and zero filled to
60 there. Transmission is asynchronous, but the RMII_TX_BUSY test at the top of
the function means the previous frame has left before that buffer is written
again. Frames of 60 bytes or more are transmitted from the caller's buffer as
before, so the copy costs nothing on the traffic that matters.
Reported in GideonZ#822, problem 4.
output_packet() wrote the adapter's four byte length header in front of the
buffer it was handed:
uint8_t *size = buffer - 4;
That holds for a buffer from this driver's own receive pool, because the
receive path gives the stack usb_buffer + 4 and keeps those four bytes as
headroom. Neither buffer the stack supplies on transmit has any.
lwip_output_callback() passes a static uint8_t[1536] for a chained pbuf, so the
write lands in whatever precedes that array in .bss, and pbuf->payload for a
single one, where ETH_PAD_SIZE is 0 and PBUF_LINK_ENCAPSULATION_HLEN is left at
its default of 0, so it lands in the pbuf's own metadata or the neighbouring
pool entry. It is a plain CPU write, so no DMA behaviour is needed to trigger
it, and where it lands depends on the link order.
Header and frame are now assembled in a driver-owned buffer. bulk_out() holds
the USB mutex until the transfer has completed, so one buffer is enough. The
length is checked against it rather than trusted.
Incidentally that buffer comes from the allocator, so what reaches bulk_out()
is word aligned, which the two stack buffers were not required to be.
Reported in GideonZ#822, problem 1.
`ram_buffer` is `new uint8_t[(NUM_BUFFERS * 1536) + 256]` and was released with plain `delete`, which is undefined behaviour for an array. Noticed only because the previous change puts a correct `delete[]` on the line below it. Nothing to do with GideonZ#822; it is a one word correction in a destructor that this branch already touches.
Both fixes in this PR are about what leaves the device, and neither could be checked anywhere but on the machine, because the decision lived inline in a driver method that only builds for the target. Lift the two decisions into free functions over plain integers, the way software/drive/gcr_track_bounds.h does it, and pin them down on a build host. eth_tx_frame_to_send() chooses the buffer and length to transmit: a frame of 60 bytes or more goes out of the caller's buffer untouched, a shorter one is copied into the driver's pad buffer and zero filled. ax88772_tx_block() assembles the four byte length header and the frame in the driver's own buffer and returns how much to send, or -1 for a frame that does not fit. The tests state the property rather than the implementation: fill the memory around the frame with a sentinel byte, then require that no sentinel is inside what the function says to transmit. That is exactly the leak -- 18 bytes of the neighbouring heap block on every 42 byte ARP request -- and it is checked for every short length from 0 to 59. The AX88772 case guards the eight bytes in front of both buffers, since the header used to be written in front of the one the network stack owns. Ten cases, wired into the build workflow next to the UCI palette test. Against this commit all ten pass. Against a header restored to the two pre-fix behaviours -- pad the length but not the data, and write the header at frame - 4 -- nine of the ten fail; the tenth is the path that does not change, a frame of 60 bytes or more transmitted from the caller's buffer. Still not covered here, and still needing the machine: the sentinel word in front of the driver's transmit buffer on a live system, and an ARP capture off the LAN showing the padding bytes are zero.
ca4d815 to
6a15ed2
Compare
… green The suite pinned the two decisions but not at every length GideonZ#843 asked for, and it guarded only the memory in front of each buffer. A leak past the end is what problem 4 actually was, so that was the wrong side to watch. Both contracts are now checked with eight sentinel bytes in front of the frame and eight behind it, at the lengths from the review: 0, 1, 1500, 1536 and 1537 for the AX88772 block, where 1537 is one byte too many and has to be refused, and 42, 59 and 60 for the RMII path. Each case asserts the four byte header, length little endian then each byte complemented, the payload byte for byte, that the padding is zero and not the neighbour, and that the caller's buffer comes back exactly as it went in. The red half is new. eth_tx_frame_legacy.h models what the two drivers did to memory before the fix -- the AX88772 header at frame - 4, and a short frame whose length register was raised to 60 while the data was not -- and the same suite runs against it. It is a model of the defect, not of the device, and no firmware builds it. against this commit 12 passed against the pre-fix behaviour 11 failed The one that stays green either way is the control case: a frame of 60 bytes or more goes out of the caller's buffer untouched in both versions, and must. `make -C software/io/network/tests` now runs both halves, and the red one fails the build if it ever passes, so a later change that guts the checks is caught here instead of going quiet.
This reverts commit 3e37859. Problem 5 of GideonZ#822 is USB, not networking, and it only travelled in this PR because the sweep that found it also found the two transmit defects. It now has its own branch, where it is joined by the change that makes the class of defect impossible -- a single exit for the mutex -- and by the red/green measurement from a C64 Ultimate that the two together deserve. Reverted here rather than dropped, so this PR stays a fast-forward and the review on it keeps its anchors. What remains in this PR is the network half: the AX88772 length header, the short-frame padding, the delete[], and the host tests that pin all of it.
|
@chrisgleissner — all three items are answered, and problem 4 is now measured at the wire as well as on a build host. (1) and (2), the two transmit paths: host tests at exactly the lengths you named, canaries on both sides of every buffer — a leak past the end is what problem 4 actually was — with the four-byte header and complements checked, payload compared byte for byte, padding asserted zero, and the caller's buffer asserted unchanged. Wired into CI. 12 passed against the branch, 11 failed against The ARP capture you asked for. A C64 Ultimate on a direct cable to a capturing host, its Ethernet given a static address whose gateway does not exist, so it repeats ARP requests: 42 bytes of ARP, padded to Ethernet's 60. Same machine, same cable, same analysis script; only the firmware changed between the two runs. The counts differ because the two capture windows caught different numbers of retries. What matters is that not one frame in the first run was zero and not one in the second was anything else. The leaked bytes are readable. Bytes 42..59 of three frames from the unfixed device: The first is a fragment of a REST response — (3) That measurement corrects something in this PR: the wait is 25 seconds, not 5. Built: |
|
Excellent work. Many thanks @enver-haase @GideonZ I think this is merge ready based on the analysis of Enver. Do you agree? |
chrisgleissner
left a comment
There was a problem hiding this comment.
Re-reviewed the current head adversarially, including the new shared-buffer lifetime/serialization assumptions. I found no actionable issue.
The AX88772 block is bounds-checked before assembly, uses the existing uncached alias convention, and remains live through the synchronous bulk_out(). The RMII pad remains live until RMII_TX_BUSY clears, and lwIP serializes the link-output path, so the single pad buffer does not introduce a concurrent reuse window. The 0/1/1500/1536/1537 and 42/59/60 cases pass locally; the green suite also passes under ASan+UBSan with -Werror. CI is green and compiled both changed drivers across its target matrix. The on-wire red/green capture directly covers the RMII disclosure fix.
Approving.
|
@GideonZ — a gentle nudge on this one. Approved since 17 September, CI green, and it still merges cleanly against Problem 4 is the part I would not leave sitting: the RMII length register was raised to 60 while the data was not, so every ARP request puts 18 bytes of the neighbouring heap block on the wire. Measured on hardware, the padding was readable — fragments of REST answers and a DNS label. Happy to rebase it, split the |
|
Merged master into this branch (7e877b0) and shortened the comments in What this PR fixes
BuildWith the merge, HardwareI captured every frame shorter than 60 bytes sent by the device over 15 seconds of REST requests and zero-payload pings, and checked the padding bytes:
On both devices running this PR, pings with payloads of 0 to 40, 1400 and 1472 bytes, a 300 KB FTP upload and download with byte comparison, and REST, FTP and Telnet health checks all passed. Not tested on hardware: the U64 and the U2+, which use the same RMII driver, and the AX88772 path, because no USB Ethernet adapter was available. The AX88772 change is covered by the host tests and the builds only. |
|
No, not yet. There may be an AX88772 adapter in my office; I'll check there. If there isn't one, I'll order a UGREEN USB 2.0 10/100 adapter, which lists an AX88772A, and report the FTP round trip on master and on this branch once it has run. |
|
@chrisgleissner @GideonZ The AX88772 run on hardware that I promised above (FTP round trip, master vs. this change) has not happened: the delivery of the adapter I ordered was cancelled. Is it still wanted now that this is merged? If so, I'll order another adapter and post the run here. If the host tests are enough for the AX88772 path, I'll drop the follow-up from the description. |
My Ethernet driver work (#843) is finished, so a network change has no reason to wait for me. The row keeps the drive code I still work on.
Fixes two of the transmit defects in #822.
What this fixes
buffer - 4, in front of a buffer lwIP owns. Header and frame are now assembled in a driver-owned buffer, at the cost of a copy of up to 1518 bytes per frame (throughput not measured).~RmiiInterfacereleased anew[]array withdelete. Separate commit.Tests
Host,
make -C software/io/network/tests, in CI: 12 pass on the fix, 11 fail against a model of the pre-fix drivers.RMII on hardware (@chrisgleissner), frames under 60 bytes, padding bytes that are zero vs. other:
7117d88577e877b0c246f8005caAlso passed on both with this PR: pings of 0 to 1472 bytes, a 300 KB FTP round trip with byte comparison, REST/FTP/Telnet health checks.
Build:
u64,u64ii,u2,u2plus,u2pl.Not tested