Repository navigation
boot: boot_serial: Fix off-by-one in image number check - #2967
Merged
Merged
Conversation
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>
There was a problem hiding this comment.
🟢 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_NUMBERin non-direct serial uploads, preserving the existing MCUmgr error. - Return
-EINVALfor 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
nordicjm
approved these changes
Nov 13, 2026
| FOR_EACH_NONEMPTY_TERM(PARTITION_ID, (,), ALL_AVAILABLE_SLOTS) | ||
| }; | ||
|
|
||
| if (img < 0 || img >= MCUBOOT_IMAGE_NUMBER || slot < 0 || slot > 1) { |
Collaborator
There was a problem hiding this comment.
there is a define for the secondary image that should be used here
de-nordic
approved these changes
Nov 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #2836.
The image number check added to
bs_upload()rejects animagevalue onlywhen it is greater than
BOOT_IMAGE_NUMBER. Image numbers are zero-based, so arequest naming image
BOOT_IMAGE_NUMBERstill passes, and on Zephyr the slotlookup in
__flash_area_ids_for_slot()then reads one entry past the end ofits partition id table and uses whatever follows as a flash area id.
comparison to
>=, keeping the existing MCUmgr error.__flash_area_ids_for_slot()return-EINVALfor an out-of-range imageindex or slot (the same value
flash_area_id_from_multi_image_slot()alreadyreturns for an unknown slot), so
flash_area_open()fails instead of thelookup reading past the table. In-range lookups are unchanged.