fix(commander): reject an out-of-range mode to replace on registration (#28619)

An external component registering a flight mode can ask to replace an internal
navigation state. The requested value was stored without a range check and later
used as a shift amount when building the selectable mode mask:

	can_set_nav_state_mask &= ~(1u << cur_mode.replaces_nav_state);

replace_internal_mode is a uint8 and NAVIGATION_STATE_MAX is 31, so anything
from 32 upwards shifts a uint32 by more than its width, which is undefined. In
practice the shift is taken modulo the width, so it clears the bit of an
unrelated mode and that mode stops being selectable.

The registration path already rejects a mode that another component has claimed,
with a message and reply.success = false. Reject an out-of-range one the same
way, so the request fails and the component is told, rather than corrupting the
mask. The status print at the top of the file already bounds the same field.

Reported by @lihnucs.

Assisted-by: Claude:claude-opus-5[1m]

Signed-off-by: Julian Oes <julian@oes.ch>
This commit is contained in:
Julian Oes authored and GitHub committed 2026-09-08 16:24:45 -06:00
1 parent 5a0034b732
commit adb45a88b6
1 file changed
+7
+7
View File
@@ -302,6 +302,13 @@ void ModeManagement::checkNewRegistrations(UpdateRequest &update_request)
reply.success = false;
} else if (request.enable_replace_internal_mode) {
// The mode to replace is used as a shift amount when building the selectable
// mode mask, so it has to be a real navigation state
if (request.replace_internal_mode >= vehicle_status_s::NAVIGATION_STATE_MAX) {
PX4_ERR("Invalid mode to replace (%i)", request.replace_internal_mode);
reply.success = false;
}
// Check if another one already replaces the same mode
for (int i = Modes::FIRST_EXTERNAL_NAV_STATE; i <= Modes::LAST_EXTERNAL_NAV_STATE; ++i) {
if (_modes.valid(i)) {