Skip to content

boot: boot_serial: Fix off-by-one in image number check - #2967

Merged
d3zd3z merged 2 commits into
mcu-tools:mainfrom
d3zd3z:fix-cf006
Nov 13, 2026
Merged

d3zd3z merged 2 commits into
mcu-tools:mainfrom
d3zd3z:fix-cf006

Conversation

@d3zd3z

@d3zd3z d3zd3z commented Nov 12, 2026

Copy link
Copy Markdown
Member

Follow-up to #2836.

The image number check added to bs_upload() rejects an image value only
when it is greater than BOOT_IMAGE_NUMBER. Image numbers are zero-based, so a
request naming image BOOT_IMAGE_NUMBER still passes, and on Zephyr the slot
lookup in __flash_area_ids_for_slot() then reads one entry past the end of
its partition id table and uses whatever follows as a flash area id.

  • boot: boot_serial: Fix off-by-one in image number check changes the
    comparison to >=, keeping the existing MCUmgr error.
  • boot: zephyr: Bounds-check slot lookup in sysflash makes
    __flash_area_ids_for_slot() return -EINVAL for an out-of-range image
    index or slot (the same value flash_area_id_from_multi_image_slot() already
    returns for an unknown slot), so flash_area_open() fails instead of the
    lookup reading past the table. In-range lookups are unchanged.

bs_upload() rejects an "image" number only when it is greater than
BOOT_IMAGE_NUMBER, but image numbers are zero-based, so a request
naming image BOOT_IMAGE_NUMBER passes the check. That value reaches
flash_area_id_from_multi_image_slot(), and on Zephyr the slot lookup
reads one entry past the end of its partition id table and uses
whatever follows as a flash area id. Reject any image number that is
not below BOOT_IMAGE_NUMBER, keeping the existing MCUmgr error.

Assisted-by: Claude:opus-5.5
Signed-off-by: David Brown <david.brown@linaro.org>
__flash_area_ids_for_slot() indexes its partition id table with
img * 2 + slot without checking either argument, so an out-of-range
image index or slot reads past the end of the table and returns an
arbitrary value as a flash area id. Return -EINVAL for an image index
outside [0, MCUBOOT_IMAGE_NUMBER) or a slot other than 0 or 1, the
same value flash_area_id_from_multi_image_slot() already returns for
an unknown slot, so that the following flash_area_open() fails and
the id never compares equal to a real flash area id. In-range lookups
are unchanged.

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 12, 2026 23:47

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.

🟢 Approval recommended

No active review issues are identified for the serial-upload validation and flash-slot bounds-check changes.

No open issues 🎉

What changed in this PR

This follow-up to #2836 tightens serial-upload validation and adds bounds checks to Zephyr’s flash-slot lookup to prevent out-of-range table access.

Changes:

  • Reject image indexes equal to BOOT_IMAGE_NUMBER in non-direct serial uploads, preserving the existing MCUmgr error.
  • Return -EINVAL for invalid image indexes or slots in Zephyr’s lookup helper, leaving valid lookups unchanged.
File Description
boot/​zephyr/​include/​sysflash/​sysflash.h Adds image and slot bounds checks before indexing the partition table.
boot/​boot_serial/​src/​boot_serial.c Fixes the off-by-one error in upload image validation.

Review effort: Balanced

FOR_EACH_NONEMPTY_TERM(PARTITION_ID, (,), ALL_AVAILABLE_SLOTS)
};

if (img < 0 || img >= MCUBOOT_IMAGE_NUMBER || slot < 0 || slot > 1) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

there is a define for the secondary image that should be used here

@d3zd3z
d3zd3z merged commit f30be48 into mcu-tools:main Nov 13, 2026
34 checks passed
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.

4 participants