Repository navigation
Conversation
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>
There was a problem hiding this comment.
🟡 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_ALIGNand 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
| align = flash_area_align(fap); | ||
| if (align == 0 || align > BOOT_SERIAL_TAIL_BUFFER_SIZE) { |
There was a problem hiding this comment.
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>

bs_upload()pads the unaligned end of the last image chunk to the flashwrite alignment in a stack buffer of
BOOT_MAX_ALIGNbytes: it copies up toflash_area_align(fap) - 1remaining bytes into it and then writesflash_area_align(fap)bytes from it. Nothing relates the write alignment ofthe upload target to
BOOT_MAX_ALIGN, which defaults to 8 and is derived froma 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:
BOOT_MAX_ALIGNandMCUBOOT_SERIAL_UNALIGNED_BUFFER_SIZE, so devices with large write blocksthat already rely on the unaligned buffer keep working;
upload with
MGMT_ERR_EINVALif it is zero or does not fit the buffer(before the slot is erased);
write length.
Note the behaviour change: an upload target whose write alignment is larger
than both
BOOT_MAX_ALIGNand the unaligned buffer is now refused at thefirst chunk. Previously it worked only when the image size happened to be a
multiple of the alignment.
Testing
nrf52840dk/nrf52840serial recovery builds: unaligned buffer0 and default (64), direct image upload with swap-move, direct image
upload with swap-offset
boot_serial.c)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