Skip to content

Bounds-check host register writes in handle_dma - #12

Merged
twells46 merged 3 commits into
masterfrom
oscar/dma-write-bounds
Oct 12, 2026
Merged

twells46 merged 3 commits into
masterfrom
oscar/dma-write-bounds

Conversation

@objorkman

@objorkman objorkman commented Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

Part of #11 (item 1).

What and why

handle_dma() trusted the count byte and every address in a host packet.

  • Count byte: libwallaby zero-pads its packets, so a count larger than the real number of writes made the firmware apply (0, 0) pairs. That writes 0 to register 0, the 'J' start byte, and from then on libwallaby logs DMA de-synchronized on every transfer until the STM32 resets. A single bit flip that raises the count is enough.
  • Larger counts: these read past the 89 bytes the DMA fills and then into aTxBuffer itself, treating sensor bytes as (address, value) pairs.
  • Addresses: 153 and above wrote past the end of aTxBuffer, into the analog filter state and other globals.

Changes

  • Firmware/src/wallaby_dma.c:
    • Clamp the count byte to 42, the most pairs that fit between byte 4 and the trailing 'S'. Clamping rather than dropping the packet keeps the real writes when only the count byte is corrupted: libwallaby zero-pads its packets, and the address check skips those (0, 0) pairs.
    • Skip writes to the start and version registers (below REG_RW_DIG_IN_H) and to addresses at or above REG_ALL_COUNT. The check runs before any side effects (done-bit clearing, adc_dirty, dig_dirty).
    • Remove the TODO: register/value checks before assignment comment.
  • AGENTS.md: the handle_dma() description now mentions the new checks.

The register map is unchanged. libwallaby sends 1–4 writes per packet to addresses 3–152 (platform.cpp already rejects ≥ 153), so normal traffic is unaffected.

Verification

  • Docker build passes. wallaby_dma.c still compiles with no warnings; the build's 8 warnings are the same newlib syscall-stub linker warnings as on master.
  • The disassembly shows both checks: the count is clamped to 42 (cmp r1, #42; movcs r1, #42), and an unsigned range check covers addresses 3–152.
  • I simulated every single-bit flip of the count byte in libwallaby-shaped 1-, 2- and 4-write packets, with aRxBuffer directly followed by aTxBuffer as in the memory map:
    • Every flip that raises the count: master clears 'J', except the 128-bit flip, which wraps back to the real count. With this PR, the real write is applied and nothing else is written.
    • A flip to 0 loses the write on both.
  • The file keeps its CRLF line endings (no lines are LF-only).
  • Not yet tested on hardware. To test on a Wombat:
    1. On current firmware, send the frame below, then run any libwallaby program, such as one that prints analog(0). Expected: DMA de-synchronized on every call until wallaby_reset_coproc.
      import spidev
      s = spidev.SpiDev(); s.open(0, 0); s.max_speed_hz = 16000000
      buf = [0] * 89
      buf[0], buf[1], buf[3], buf[88] = ord('J'), 4, 2, ord('S')  # claims 2 writes, carries none
      s.xfer2(buf)
    2. Flash this branch's wombat.bin (CI artifact) and repeat. Expected: no errors.
    3. Quick regression: motors (motor, mav, off), servos, and digital and analog I/O.

handle_dma() trusted the packet's count byte and every write address.
libwallaby zero-pads its packets, so any count larger than the real
number of writes applied (0, 0) pairs and cleared the 'J' start byte,
after which libwallaby logs "DMA de-synchronized" on every transfer until
the STM32 resets. Larger counts read past the 89 bytes the DMA fills and
then into aTxBuffer itself, and addresses of 153 or more wrote past the
end of aTxBuffer.

Packets whose count exceeds the 42 pairs that fit before the trailing
'S' are now ignored, and writes to the start and version registers or
past REG_ALL_COUNT are skipped. libwallaby sends 1-4 writes to addresses
3-152, so normal traffic is unaffected.

Part of #11.
@objorkman objorkman self-assigned this Oct 1, 2026
@objorkman objorkman mentioned this pull request Oct 1, 2026
3 of 4 tasks
objorkman and others added 2 commits September 30, 2026 21:05
Dropping every write from a packet whose count byte exceeds 42 lost the
real write whenever a count-byte bit flip pushed it above 42. That
includes a flip of the top bit, which master happened to handle
correctly because the count wrapped back to its real value. libwallaby
zero-pads its packets and the address check already skips (0, 0) pairs,
so clamping the count to 42 applies the real writes and nothing else.

Also corrects the address-check comment: libwallaby checks only the
start byte on every transfer, and nothing on the host reads the version
registers. They're protected because the register map makes them
read-only.

Part of #11.
Clamping an oversized count to 42 applied 42 pairs even when the body
was garbage too. On master a count of 128-169 wrapped and applied fewer.
The real count is now recovered from where libwallaby's zero padding
starts, and a packet with no padding is dropped. Single-bit flips of the
count byte still apply exactly the real writes.

Packets with a corrupt count or a skipped write used to be silent. They
are now counted, mod 16, in the unused high nibble of REG_RW_MOT_DONE.
The nibble is republished on every packet, so the main loop's
read-modify-write of the done bits can't hold back a stale value. libkipr
masks the done bits, so existing hosts are unaffected.

Part of #11.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@objorkman
objorkman marked this pull request as ready for review October 4, 2026 02:31
@twells46

Copy link
Copy Markdown
Contributor

I tested this on a Wombat and pushed one commit on top, 6896e00.

Hardware results for this PR as submitted (80ad31a)

Raw spidev frames shaped like libwallaby's, compared against master 4814ab8:

Test master this PR
Your repro (count 2, no pairs) 199/200 frames desynced 0/200
Servo register round-trip (w8/w16/w32) pass pass
Every single-bit flip of the count byte on a w16 desync on 6 of 8 flips real write applied, no desync
Writes to registers 0..2 start byte overwritten, desync ignored
20,000-frame soak 0 desync, 4796 frames/s 0 desync, 4798 frames/s

A host-compiled differential test of handle_dma() (master vs. this PR, aRxBuffer/aTxBuffer laid out as in the memory map) found no difference for any legitimate frame: all 65,536 single writes to 3..152, every w16/w32 start address, and 200,000 random frames of 1..42 valid pairs.

Two things the clamp made worse, fixed in 6896e00

  1. A corrupt count over a non-zero body applied more writes than master. With a count of 128..169, master's stop = 2 * num_regs wraps in uint8_t, so it applies n−128 pairs. Clamping to 42 applies all 42. With random bodies, the clamp changed more registers than master in 96% of such frames (22.8 vs. 11.7 bytes on average). On hardware, a count of 128 over an unpadded body moved servo 0's register to 2000 where master left it at 1500.

    Now a count above 42 is treated as corrupt and the real count is recovered from where the zero padding starts. If there is no padding before 'S', the body is as suspect as the count and nothing is applied. Single-bit flips of the count byte on libwallaby frames still apply exactly the real writes.

  2. Corruption became silent. Master turned an inflated count into a permanent DMA de-synchronized, which is bad but visible. With the clamp there was no signal at all, and debug_printf is an empty stub.

    Packets with a corrupt count or a skipped write are now counted, mod 16, in bits 7:4 of REG_RW_MOT_DONE. Those bits were unused: libkipr and wallaby_motor.c both mask the low four. The one behaviour change is that a host write to register 60 can no longer set bits 7:4; nothing in libwallaby writes that register.

    The host side is a small libwallaby change that logs a rate-limited warning when the counter moves. I'll open that separately. Stock libkipr 1.2.4 ignores the nibble and works unchanged against this firmware.

Hardware results with 6896e00

  • All of the tests above still pass.
  • Count 128 over an unpadded body: servo 0 stays at 1500.
  • Reject counter: moves by exactly 1 for each of three kinds of rejected frame, 0 drift over 100 clean frames.
  • Regression check through libkipr, stock 1.2.4 and patched, motors and servos on ports 0 and 3:
    • motor, off, freeze, mav, mrp/mtp with the done bit, servo enable and positions, digital output registers on ports 0..9, analog, digital and button reads. All pass, and the motors and servos were watched moving.
    • The done bit stays set across three injected rejected frames while the counter moves by 3.
  • Not tested: the electrical level on a driven digital output, and buttons or analog sensors being changed by hand.

scripts/check-register-map.sh passes and the files keep their CRLF line endings. #13 merges onto this with no conflict and builds; I haven't flashed that combination.

@twells46
twells46 merged commit 2776fb2 into master Oct 12, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants