From 4e2c757d8489e5b2e6966679f5f905faf00cbffe Mon Sep 17 00:00:00 2001 From: Phil Phauler Date: Sat, 5 Sep 2026 02:16:02 +0200 Subject: [PATCH 1/3] Fix #2745, validate CFE_MSG_Init size before clearing the buffer and use OR for byte concatenation CFE_MSG_Init used the caller's Size as the memset length before CFE_MSG_SetSize validated it, so an oversize argument cleared memory past the message buffer. Apply the same bounds CFE_MSG_SetSize enforces before the memset. CFE_MSG_GetSystem and CFE_MSG_GetSize concatenate header bytes with +; use | so the expression states a bit concatenation and survives a wider storage type. Findings 2 and 3 of #2745 are not part of this change: the priext initializer was fixed by 13ed881 (#2317) and the checksum walk needs a caller-provided bound, which is an API decision. --- modules/msg/fsw/src/cfe_msg_ccsdsext.c | 2 +- modules/msg/fsw/src/cfe_msg_ccsdspri.c | 5 ++--- modules/msg/fsw/src/cfe_msg_init.c | 7 +++++++ modules/msg/fsw/src/cfe_msg_priv.h | 3 +++ modules/msg/ut-coverage/test_cfe_msg_init.c | 16 ++++++++++++++++ 5 files changed, 29 insertions(+), 4 deletions(-) diff --git a/modules/msg/fsw/src/cfe_msg_ccsdsext.c b/modules/msg/fsw/src/cfe_msg_ccsdsext.c index 17c307893..87be7a745 100644 --- a/modules/msg/fsw/src/cfe_msg_ccsdsext.c +++ b/modules/msg/fsw/src/cfe_msg_ccsdsext.c @@ -251,7 +251,7 @@ CFE_Status_t CFE_MSG_GetSystem(const CFE_MSG_Message_t *MsgPtr, CFE_MSG_System_t return CFE_MSG_BAD_ARGUMENT; } - *System = (MsgPtr->CCSDS.Ext.SystemId[0] << 8) + MsgPtr->CCSDS.Ext.SystemId[1]; + *System = ((uint16)MsgPtr->CCSDS.Ext.SystemId[0] << 8) | MsgPtr->CCSDS.Ext.SystemId[1]; return CFE_SUCCESS; } diff --git a/modules/msg/fsw/src/cfe_msg_ccsdspri.c b/modules/msg/fsw/src/cfe_msg_ccsdspri.c index cadb4cff5..18a3ed615 100644 --- a/modules/msg/fsw/src/cfe_msg_ccsdspri.c +++ b/modules/msg/fsw/src/cfe_msg_ccsdspri.c @@ -26,8 +26,7 @@ #include -/* CCSDS Primary Standard definitions */ -#define CFE_MSG_SIZE_OFFSET 7 /**< \brief CCSDS size offset */ +/* CCSDS Primary Standard definitions (CFE_MSG_SIZE_OFFSET is in cfe_msg_priv.h) */ #define CFE_MSG_CCSDSVER_MASK 0xE000 /**< \brief CCSDS version mask */ #define CFE_MSG_CCSDSVER_SHIFT 13 /**< \brief CCSDS version shift */ #define CFE_MSG_TYPE_MASK 0x1000 /**< \brief CCSDS type mask, command when set */ @@ -378,7 +377,7 @@ CFE_Status_t CFE_MSG_GetSize(const CFE_MSG_Message_t *MsgPtr, CFE_MSG_Size_t *Si return CFE_MSG_BAD_ARGUMENT; } - *Size = (MsgPtr->CCSDS.Pri.Length[0] << 8) + MsgPtr->CCSDS.Pri.Length[1] + CFE_MSG_SIZE_OFFSET; + *Size = (((uint16)MsgPtr->CCSDS.Pri.Length[0] << 8) | MsgPtr->CCSDS.Pri.Length[1]) + CFE_MSG_SIZE_OFFSET; return CFE_SUCCESS; } diff --git a/modules/msg/fsw/src/cfe_msg_init.c b/modules/msg/fsw/src/cfe_msg_init.c index cc83dd1d2..e9c26fa86 100644 --- a/modules/msg/fsw/src/cfe_msg_init.c +++ b/modules/msg/fsw/src/cfe_msg_init.c @@ -41,6 +41,13 @@ CFE_Status_t CFE_MSG_Init(CFE_MSG_Message_t *MsgPtr, CFE_SB_MsgId_t MsgId, CFE_M return CFE_MSG_BAD_ARGUMENT; } + /* Reject an invalid size before it is used as the memset length. CFE_MSG_SetSize + * enforces the same bounds, but only after the buffer has already been cleared */ + if (Size < CFE_MSG_SIZE_OFFSET || Size > (0xFFFF + CFE_MSG_SIZE_OFFSET)) + { + return CFE_MSG_BAD_ARGUMENT; + } + /* Clear and set defaults */ memset(MsgPtr, 0, Size); CFE_MSG_InitDefaultHdr(MsgPtr); diff --git a/modules/msg/fsw/src/cfe_msg_priv.h b/modules/msg/fsw/src/cfe_msg_priv.h index eed5da357..01aab381f 100644 --- a/modules/msg/fsw/src/cfe_msg_priv.h +++ b/modules/msg/fsw/src/cfe_msg_priv.h @@ -32,6 +32,9 @@ #include "common_types.h" #include "cfe_msg_hdr.h" +/* CCSDS size offset, shared by the size accessors and CFE_MSG_Init */ +#define CFE_MSG_SIZE_OFFSET 7 /**< \brief CCSDS size offset */ + /*---------------------------------------------------------------------------------------*/ /** * \brief get generic header field (uint8 array[2]) diff --git a/modules/msg/ut-coverage/test_cfe_msg_init.c b/modules/msg/ut-coverage/test_cfe_msg_init.c index f43dc55e7..a0ef1965d 100644 --- a/modules/msg/ut-coverage/test_cfe_msg_init.c +++ b/modules/msg/ut-coverage/test_cfe_msg_init.c @@ -31,6 +31,7 @@ #include "test_cfe_msg_init.h" #include "cfe_error.h" #include "cfe_msg_defaults.h" +#include "cfe_msg_priv.h" #include #define TEST_DEFAULT_APID_MASK 0x780 /* Bits that can be set by default apid if msgid V2 */ @@ -40,6 +41,12 @@ */ void Test_MSG_Init(void) { + /* Sized to the largest length CFE_MSG_SetSize accepts; static to keep it off the stack */ + static union + { + CFE_MSG_Message_t Msg; + uint8 Bytes[0xFFFF + CFE_MSG_SIZE_OFFSET]; + } maxmsg; CFE_MSG_CommandHeader_t cmd; CFE_MSG_Size_t size; CFE_SB_MsgId_Atom_t msgidval_exp; @@ -57,6 +64,15 @@ void Test_MSG_Init(void) UT_SetDefaultReturnValue(UT_KEY(CFE_SB_IsValidMsgId), true); UtAssert_INT32_EQ(CFE_MSG_Init(NULL, msgid_act, sizeof(cmd)), CFE_MSG_BAD_ARGUMENT); UtAssert_INT32_EQ(CFE_MSG_Init(CFE_MSG_PTR(cmd), msgid_act, 0), CFE_MSG_BAD_ARGUMENT); + UtAssert_INT32_EQ(CFE_MSG_Init(CFE_MSG_PTR(cmd), msgid_act, CFE_MSG_SIZE_OFFSET - 1), CFE_MSG_BAD_ARGUMENT); + + /* An oversize length must be rejected before it is used to clear the buffer: cmd is only a + * command header, so clearing 0xFFFF + CFE_MSG_SIZE_OFFSET + 1 bytes would run past it */ + UtAssert_INT32_EQ(CFE_MSG_Init(CFE_MSG_PTR(cmd), msgid_act, 0xFFFF + CFE_MSG_SIZE_OFFSET + 1), + CFE_MSG_BAD_ARGUMENT); + + /* The largest representable size is still accepted */ + CFE_UtAssert_SUCCESS(CFE_MSG_Init(&maxmsg.Msg, msgid_act, sizeof(maxmsg))); UT_SetDefaultReturnValue(UT_KEY(CFE_SB_IsValidMsgId), false); UtAssert_INT32_EQ(CFE_MSG_Init(CFE_MSG_PTR(cmd), msgid_act, sizeof(cmd)), CFE_MSG_BAD_ARGUMENT); UtAssert_INT32_EQ(CFE_MSG_Init(CFE_MSG_PTR(cmd), CFE_SB_INVALID_MSG_ID, sizeof(cmd)), CFE_SUCCESS); From 18b701dd1031195b1dc2ecca98382e430587687d Mon Sep 17 00:00:00 2001 From: Phil Phauler Date: Thu, 17 Sep 2026 17:59:59 +0200 Subject: [PATCH 2/3] Fix #2745, keep CFE_MSG_Init oversize handling as SB expects CFE_MSG_Init calls CFE_MSG_SetSize (cfe_msg_ccsdspri.c:391) and returns its status, so the CCSDS-representable upper bound is already enforced by SetSize (Size > 0xFFFF + CFE_MSG_SIZE_OFFSET rejected there too) - it is not silently ignored or truncated. Init's own copy of that same upper bound, added in 4e2c757d, duplicates SetSize's check but runs before the buffer is cleared, which is wrong for CFE_SB_SendMsg's documented flow: modules/cfe_testcase/src/sb_sendrecv_test.c:443-453 builds a message sized CFE_MISSION_SB_MAX_SB_MSG_SIZE + 16 (32784 bytes with the repo's default 32768-byte CFE_MISSION_SB_MAX_SB_MSG_SIZE, cfe_sb_interface_cfg.h) into a buffer allocated at exactly CFE_MISSION_SB_MAX_SB_MSG_SIZE, and expects CFE_MSG_Init to succeed (32784 is still under the CCSDS max of 0xFFFF + 7 = 65542) while CFE_SB_TransmitBuffer rejects it as CFE_SB_MSG_TOO_BIG against the real allocated buffer. Keep only the lower bound in Init (Size < CFE_MSG_SIZE_OFFSET, matching SetSize's own minimum and rejecting a memset shorter than the header); drop the upper bound and let CFE_SB_TransmitBuffer/CFE_MISSION_SB_MAX_SB_MSG_SIZE do that job against the real buffer, as SetSize already does independently for the CCSDS wire-format limit. modules/msg/ut-coverage/test_cfe_msg_init.c: removed the 0xFFFF+CFE_MSG_SIZE_OFFSET+1 rejection case that called CFE_MSG_Init on `cmd` (a bare CFE_MSG_CommandHeader_t) - that size is now accepted by Init, and a memset of that size into `cmd` would run well past it. Kept the underflow case and the largest-representable-size acceptance case (CFE_MSG_Init on the pre-existing static `maxmsg` union already sized to sizeof(uint8[0xFFFF + CFE_MSG_SIZE_OFFSET])), which still exercises CFE_MSG_SetSize's own upper-bound branch end-to-end. Verification (WSL, nasa/cfs-ut bundle, cfe swapped to this branch, SIMULATION=native ENABLE_UNIT_TESTS=true, -j2): - coverage-msg-ALL: 618/618 PASS (was 619/619 before removing the one assertion), cfe_msg_init.c gcov: Lines 100.00% of 13, Branches 100.00% of 8. - Functional test (core-cpu1 + cfe_testcase loaded via ES_StartApp, matching functional-tests.yml): sb_sendrecv_test.c:446-447 pass before AND after this change (CFE_MSG_Init == CFE_SUCCESS, CFE_SB_TransmitBuffer == CFE_SB_MSG_TOO_BIG), overall SUMMARY TOTAL::2242 PASS::1867 FAIL::0 MIR::13 identical in both runs - this harness did not reproduce the CI regression described for run 33416261229 (that run is also dated 2026-08-31, before this branch's own 4e2c757d commit date of 2026-09-05). Flagged for Phil; the code change stands on the SetSize-duplication/design argument above independent of that specific reproduction. Findings 2 and 3 of #2745 remain out of scope, per 4e2c757d: the priext initializer was fixed by 13ed881 (#2317), and the checksum walk needs a caller-provided bound, which is an API decision. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01HDRvC9ePDP3i6xpxFAECFg --- modules/msg/fsw/src/cfe_msg_init.c | 9 ++++++--- modules/msg/ut-coverage/test_cfe_msg_init.c | 8 ++------ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/modules/msg/fsw/src/cfe_msg_init.c b/modules/msg/fsw/src/cfe_msg_init.c index e9c26fa86..1615cb0c3 100644 --- a/modules/msg/fsw/src/cfe_msg_init.c +++ b/modules/msg/fsw/src/cfe_msg_init.c @@ -41,9 +41,12 @@ CFE_Status_t CFE_MSG_Init(CFE_MSG_Message_t *MsgPtr, CFE_SB_MsgId_t MsgId, CFE_M return CFE_MSG_BAD_ARGUMENT; } - /* Reject an invalid size before it is used as the memset length. CFE_MSG_SetSize - * enforces the same bounds, but only after the buffer has already been cleared */ - if (Size < CFE_MSG_SIZE_OFFSET || Size > (0xFFFF + CFE_MSG_SIZE_OFFSET)) + /* Reject an invalid size before it is used as the memset length: a size shorter than + * the header cannot be cleared safely. The CCSDS-representable upper bound is not + * enforced here on purpose - CFE_SB_TransmitBuffer rejects an oversize message against + * the real buffer/CFE_MISSION_SB_MAX_SB_MSG_SIZE, and CFE_MSG_SetSize below still + * enforces the CCSDS 16-bit length-field maximum for the size that is actually stored */ + if (Size < CFE_MSG_SIZE_OFFSET) { return CFE_MSG_BAD_ARGUMENT; } diff --git a/modules/msg/ut-coverage/test_cfe_msg_init.c b/modules/msg/ut-coverage/test_cfe_msg_init.c index a0ef1965d..057c22669 100644 --- a/modules/msg/ut-coverage/test_cfe_msg_init.c +++ b/modules/msg/ut-coverage/test_cfe_msg_init.c @@ -66,12 +66,8 @@ void Test_MSG_Init(void) UtAssert_INT32_EQ(CFE_MSG_Init(CFE_MSG_PTR(cmd), msgid_act, 0), CFE_MSG_BAD_ARGUMENT); UtAssert_INT32_EQ(CFE_MSG_Init(CFE_MSG_PTR(cmd), msgid_act, CFE_MSG_SIZE_OFFSET - 1), CFE_MSG_BAD_ARGUMENT); - /* An oversize length must be rejected before it is used to clear the buffer: cmd is only a - * command header, so clearing 0xFFFF + CFE_MSG_SIZE_OFFSET + 1 bytes would run past it */ - UtAssert_INT32_EQ(CFE_MSG_Init(CFE_MSG_PTR(cmd), msgid_act, 0xFFFF + CFE_MSG_SIZE_OFFSET + 1), - CFE_MSG_BAD_ARGUMENT); - - /* The largest representable size is still accepted */ + /* The CCSDS-representable upper bound is enforced by CFE_MSG_SetSize (called below), not by + * Init itself - the largest representable size is still accepted */ CFE_UtAssert_SUCCESS(CFE_MSG_Init(&maxmsg.Msg, msgid_act, sizeof(maxmsg))); UT_SetDefaultReturnValue(UT_KEY(CFE_SB_IsValidMsgId), false); UtAssert_INT32_EQ(CFE_MSG_Init(CFE_MSG_PTR(cmd), msgid_act, sizeof(cmd)), CFE_MSG_BAD_ARGUMENT); From 0c3790cc7ffe4470fa20444921bf70e5d2d05195 Mon Sep 17 00:00:00 2001 From: Phil Phauler Date: Thu, 17 Sep 2026 18:09:16 +0200 Subject: [PATCH 3/3] Fix #2745, restore the CFE_MSG_Init CCSDS upper bound so an oversize length is rejected before the buffer is cleared Reverts 18b701dd: without the upper bound, CFE_MSG_Init clears Size bytes before CFE_MSG_SetSize rejects a length above the CCSDS maximum, which is the oversize path this fix exists to close. Code and tests return to 4e2c757d. --- modules/msg/fsw/src/cfe_msg_init.c | 9 +++------ modules/msg/ut-coverage/test_cfe_msg_init.c | 8 ++++++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/modules/msg/fsw/src/cfe_msg_init.c b/modules/msg/fsw/src/cfe_msg_init.c index 1615cb0c3..e9c26fa86 100644 --- a/modules/msg/fsw/src/cfe_msg_init.c +++ b/modules/msg/fsw/src/cfe_msg_init.c @@ -41,12 +41,9 @@ CFE_Status_t CFE_MSG_Init(CFE_MSG_Message_t *MsgPtr, CFE_SB_MsgId_t MsgId, CFE_M return CFE_MSG_BAD_ARGUMENT; } - /* Reject an invalid size before it is used as the memset length: a size shorter than - * the header cannot be cleared safely. The CCSDS-representable upper bound is not - * enforced here on purpose - CFE_SB_TransmitBuffer rejects an oversize message against - * the real buffer/CFE_MISSION_SB_MAX_SB_MSG_SIZE, and CFE_MSG_SetSize below still - * enforces the CCSDS 16-bit length-field maximum for the size that is actually stored */ - if (Size < CFE_MSG_SIZE_OFFSET) + /* Reject an invalid size before it is used as the memset length. CFE_MSG_SetSize + * enforces the same bounds, but only after the buffer has already been cleared */ + if (Size < CFE_MSG_SIZE_OFFSET || Size > (0xFFFF + CFE_MSG_SIZE_OFFSET)) { return CFE_MSG_BAD_ARGUMENT; } diff --git a/modules/msg/ut-coverage/test_cfe_msg_init.c b/modules/msg/ut-coverage/test_cfe_msg_init.c index 057c22669..a0ef1965d 100644 --- a/modules/msg/ut-coverage/test_cfe_msg_init.c +++ b/modules/msg/ut-coverage/test_cfe_msg_init.c @@ -66,8 +66,12 @@ void Test_MSG_Init(void) UtAssert_INT32_EQ(CFE_MSG_Init(CFE_MSG_PTR(cmd), msgid_act, 0), CFE_MSG_BAD_ARGUMENT); UtAssert_INT32_EQ(CFE_MSG_Init(CFE_MSG_PTR(cmd), msgid_act, CFE_MSG_SIZE_OFFSET - 1), CFE_MSG_BAD_ARGUMENT); - /* The CCSDS-representable upper bound is enforced by CFE_MSG_SetSize (called below), not by - * Init itself - the largest representable size is still accepted */ + /* An oversize length must be rejected before it is used to clear the buffer: cmd is only a + * command header, so clearing 0xFFFF + CFE_MSG_SIZE_OFFSET + 1 bytes would run past it */ + UtAssert_INT32_EQ(CFE_MSG_Init(CFE_MSG_PTR(cmd), msgid_act, 0xFFFF + CFE_MSG_SIZE_OFFSET + 1), + CFE_MSG_BAD_ARGUMENT); + + /* The largest representable size is still accepted */ CFE_UtAssert_SUCCESS(CFE_MSG_Init(&maxmsg.Msg, msgid_act, sizeof(maxmsg))); UT_SetDefaultReturnValue(UT_KEY(CFE_SB_IsValidMsgId), false); UtAssert_INT32_EQ(CFE_MSG_Init(CFE_MSG_PTR(cmd), msgid_act, sizeof(cmd)), CFE_MSG_BAD_ARGUMENT);