From adb45a88b6a97ad89f6fca7ffbafb62903dcb854 Mon Sep 17 00:00:00 2001 From: Julian Oes Date: Wed, 9 Sep 2026 10:24:45 +1200 Subject: [PATCH] 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 --- src/modules/commander/ModeManagement.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/modules/commander/ModeManagement.cpp b/src/modules/commander/ModeManagement.cpp index 33ae7514d33..59a8180f92a 100644 --- a/src/modules/commander/ModeManagement.cpp +++ b/src/modules/commander/ModeManagement.cpp @@ -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)) {