Skip to content

AP_HAL_ChibiOS: stop F1 bdshot decode reading past its buffer - #35036

Open
khancyr wants to merge 1 commit into
ArduPilot:masterfrom
khancyr:bdshot_f1_buffer_overread
Open

khancyr wants to merge 1 commit into
ArduPilot:masterfrom
khancyr:bdshot_f1_buffer_overread

Conversation

@khancyr

@khancyr khancyr commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Summary

Fix two problems in bdshot_decode_telemetry_packet_f1(), the bidirectional DShot decode used on F1 IOMCUs:

  • When the buffer ran out before 21 bits, the last pass still read buffer[i] with i at count or count+1. With a full buffer that reads past the end of dma_buffer_copy.
  • The bits check only ran for an index inside the buffer, so after the fill a second pass shifted by len - 1 with len 0. When a transition had already taken the bits past 21, the fill wrapped len so that bits came back to exactly 21, and the corrupt frame was decoded.

Read the buffer only for an index inside it, and check the bits on every pass, so an overshooting frame now gives INVALID_ERPM.

Found while answering a review question on #34647.

Classification & Testing (check all that apply and add your own)

  • Checked by a human programmer

  • Non-functional change

  • No-binary change

  • Infrastructure change (e.g. unit tests, helper scripts)

  • Automated test(s) verify changes (e.g. unit test, autotest)

  • Tested manually, description below (e.g. SITL)

  • Tested on hardware

  • Logs attached

  • Logs available on request

  • The old and new loops were compared on the host with 2 million generated frames, with ARM shift semantics for the old one. The results are the same except for the overshooting frames, which the old loop decoded and the new one rejects.

  • A full buffer of equal samples made the old loop read buffer[23] of a 22-element buffer; the new loop stays in range.

  • Builds for iomcu-f103-dshot.

  • Not tested on hardware.

Description

This PR was prepared with AI assistance (Claude Code) and checked by me.

When the buffer ran out before 21 bits, the last pass filled the
remaining bits but still read buffer[i] with i at count or count+1.
With a full buffer that is past the end of dma_buffer_copy. Only read
the buffer for an index inside it.

The bits check only ran for an index inside the buffer, so after the
fill a second pass shifted by len - 1 with len 0. And when a transition
had already taken bits past 21, the fill wrapped len so that bits came
back to exactly 21 and the corrupt frame was decoded. Check bits first
on every pass, so such a frame now gives INVALID_ERPM.

Checked on the host against the old loop with 2 million generated
frames, with ARM shift semantics for the old one: the results are the
same except for those overshooting frames, and a full buffer of equal
samples no longer reads buffer[23] of 22.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AP-Review

Copy link
Copy Markdown

Automated review note — AI-generated (Claude+Codex), validated against the live diff. Please sanity-check before acting.
Verdict: ACCEPT

Reviewed at head 4677ae3534.
Full report: https://firmware.ardupilot.org/Tools/APReview/DevCallReviews/PRReviews/ardupilot/ardupilot/35036/1.html#pr35036

This targeted fix looks correct. Moving the bits check first and reading the buffer only for in-range indices stops the F1 reversed decoder reading past dma_buffer_copy, and valid frames decode as before.

Follow-up (not required for this PR)

  • RCOutput_bdshot.cpp:764 (unchanged code): the generic bdshot_decode_telemetry_packet(), used on FMU boards and for non-reversed F1 captures, has the same pattern. On its fill pass it runs oldValue = buffer[i] with i == count, which reads dma_buffer_copy[22] when count is 22. The same reordering would fix it.

A suggestion to reword the commit message was withdrawn: the old loop could accept an overshooting frame (a reproduced example decodes to 0x0000), so the message is accurate.

@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown

Global Size Summary (Total Flash delta in bytes)

Built from 4677ae3 rebased onto 32f5752

Board Periph tracker blimp bootloader copter heli iofw plane rover sub
DrotekP3Pro _ _ _ * _ _ _ _ _ _
Durandal _ * * _ * * _ * * *
Hitec-Airspeed * _ _ _ _ _ _ _ _ _
KakuteF7 _ * * _ * * _ * * *
MambaH743v4 _ * * _ * * _ * * *
MatekF405-CAN _ _ _ * _ _ _ _ _ _
MatekF405 _ * * _ * * _ * * *
MatekH743-bdshot _ * * _ * * _ * * *
Pixhawk1-1M _ * * _ * * _ * * *
disco _ * * _ * * _ * * *
f103-GPS * _ _ _ _ _ _ _ _ _

@peterbarker

Copy link
Copy Markdown
Contributor

@andyp1per this looks correct to tridge and myself, but tridge thinks it really should be tested on hardware befoire merging. Do you have a suitable setup for that?

The iofirmwares will need to be rebuilt with this change in it.

@khancyr khancyr removed the DevCallEU label Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants