Skip to content

boot: boot_serial: Bound tail write alignment in upload - #2985

Open
d3zd3z wants to merge 3 commits into
mcu-tools:mainfrom
d3zd3z:fix-serial-upload-align-overflow
Open

d3zd3z wants to merge 3 commits into
mcu-tools:mainfrom
d3zd3z:fix-serial-upload-align-overflow

Conversation

@d3zd3z

@d3zd3z d3zd3z commented Nov 14, 2026

Copy link
Copy Markdown
Member

bs_upload() pads the unaligned end of the last image chunk to the flash
write alignment in a stack buffer of BOOT_MAX_ALIGN bytes: it copies up to
flash_area_align(fap) - 1 remaining bytes into it and then writes
flash_area_align(fap) bytes from it. Nothing relates the write alignment of
the upload target to BOOT_MAX_ALIGN, which defaults to 8 and is derived from
a single flash device, while the target can be any area the image number maps
to. With a larger alignment the copy runs past the end of the buffer and the
write reads past it. A first chunk (offset 0) shorter than the alignment is
enough to reach this, since its length is not checked against the image
length.

This change:

  • sizes the tail buffer as the larger of BOOT_MAX_ALIGN and
    MCUBOOT_SERIAL_UNALIGNED_BUFFER_SIZE, so devices with large write blocks
    that already rely on the unaligned buffer keep working;
  • reads the alignment once after opening the flash area and rejects the
    upload with MGMT_ERR_EINVAL if it is zero or does not fit the buffer
    (before the slot is erased);
  • uses that one value for the remainder, the aligned write path and the final
    write length.

Note the behaviour change: an upload target whose write alignment is larger
than both BOOT_MAX_ALIGN and the unaligned buffer is now refused at the
first chunk. Previously it worked only when the image size happened to be a
multiple of the alignment.

Testing

  • Zephyr nrf52840dk/nrf52840 serial recovery builds: unaligned buffer
    0 and default (64), direct image upload with swap-move, direct image
    upload with swap-offset
  • Simulator default build unchanged (the simulator does not build
    boot_serial.c)
  • Hardware test of serial recovery upload, ideally including a device
    with a write block larger than 8 bytes (for example LPC55xxx or MCXN)
    and an image whose size is not a multiple of the write block

bs_upload() pads the unaligned end of the last image chunk to the flash
write alignment in a BOOT_MAX_ALIGN sized stack buffer: it copies up to
flash_area_align(fap) - 1 remaining bytes into it and then writes
flash_area_align(fap) bytes from it. Nothing relates the write
alignment of the upload target to BOOT_MAX_ALIGN, which defaults to 8
and is derived from a single flash device, while the target can be any
area the image number maps to. With a larger alignment the copy runs
past the end of the buffer and the write reads past it. A first chunk
(offset 0) shorter than the alignment is enough to reach this, since
its length is not checked against the image length.

Size the tail buffer as the larger of BOOT_MAX_ALIGN and
MCUBOOT_SERIAL_UNALIGNED_BUFFER_SIZE, so devices with large write
blocks that already rely on the unaligned buffer keep working. Read the
alignment once after opening the flash area, reject the upload if it is
zero or does not fit the buffer, and use that value for the remainder,
the aligned write path and the final write length.

Assisted-by: Claude:opus-5.5
Signed-off-by: David Brown <david.brown@linaro.org>
Copilot AI balanced review requested due to automatic review settings November 14, 2026 21:26

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Regression coverage is missing for partial writes and alignment boundaries, including rejection before slot erasure.

1 open issue
What changed in this PR

This PR hardens MCUboot serial recovery uploads by bounding flash write alignment to the available tail buffer, preventing out-of-bounds access during partial writes.

Changes:

  • Sizes the tail buffer to the larger of BOOT_MAX_ALIGN and the configured unaligned buffer.
  • Rejects zero or oversized alignments before erasing the upload slot.
  • Reuses the validated alignment throughout the upload write paths.
File Description
boot/​boot_serial/​src/​boot_serial.c Bounds upload alignment, enlarges the tail buffer, and consistently uses the validated value.

Review effort: Balanced

Comment on lines +1254 to +1255
align = flash_area_align(fap);
if (align == 0 || align > BOOT_SERIAL_TAIL_BUFFER_SIZE) {

@d3zd3z d3zd3z Nov 14, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added a regression test in the second commit. The Mynewt boot_serial unit tests ran with a flash write alignment of 1, so the tail path was never executed; the test package now sets MCU_FLASH_MIN_WRITE_SIZE to 16 and a new case uploads images whose length is not a multiple of the alignment (single chunks of 1 and A-1 bytes, and multi-chunk images ending in a 1-byte tail, an A-1 byte tail, and whole units plus a tail). It checks the response rc and offset, the image contents including the tail, and that the padding and the following write unit stay erased.

The rejection of a zero or oversized alignment is not covered. On Mynewt the write alignment and the tail buffer size both derive from MCU_FLASH_MIN_WRITE_SIZE, so that state cannot be set up in this harness without adding a mock of flash_area_align() to the production code, which I would rather not do for this change. The Mynewt workflow is currently disabled, so the new test has been run locally with newt test @mcuboot/boot/boot_serial. The third commit adds boot/boot_serial/** to that workflow's path filter, so these tests run on boot_serial changes once it is re-enabled.

The Mynewt boot_serial unit tests only upload chunks that are whole
multiples of the flash write alignment, and the native flash write
alignment defaults to 1, so the path in bs_upload() that pads the last,
unaligned, part of an image to the write alignment is never run.

Raise MCU_FLASH_MIN_WRITE_SIZE to 16 for the test package, which also
raises BOOT_MAX_ALIGN to 16, and add a test that uploads images whose
length is not a multiple of the alignment: single chunks of 1 and of
alignment - 1 bytes, and multi-chunk images whose last chunk is a 1
byte tail, an alignment - 1 byte tail, or whole write units followed
by a tail. For each image it checks that every response has rc 0 and
asks for the expected next offset, that the image including its tail
reads back from the primary slot, and that the padding up to the next
write unit boundary, and the unit after it, are left erased. The test
UART now captures the response so that it can be decoded.

On Mynewt the write alignment and the size of the tail buffer both
derive from MCU_FLASH_MIN_WRITE_SIZE, so an upload target whose
alignment is zero or exceeds the tail buffer cannot be set up here, and
its rejection is not covered by this test.

Assisted-by: Claude:opus-5.5
Signed-off-by: David Brown <david.brown@linaro.org>
The Mynewt workflow runs the boot_serial unit tests with
"newt test @mcuboot/boot/boot_serial", but its path filter does not
include boot/boot_serial, so a pull request that only changes the
serial recovery code or its tests skips them.

Add boot/boot_serial to the filter.

Assisted-by: Claude:mythos-5.2
Signed-off-by: David Brown <david.brown@linaro.org>

This branch has not been deployed

No deployments
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