Skip to content

Fix #2745, validate CFE_MSG_Init size and use OR for byte concatenation - #2821

Open
philphauler wants to merge 3 commits into
nasa:devfrom
philphauler:fix-2745-msg-4-findings
Open

philphauler wants to merge 3 commits into
nasa:devfrom
philphauler:fix-2745-msg-4-findings

Conversation

@philphauler

@philphauler philphauler commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

Checklist (Please check before submitting)

Describe the contribution

Fixes #2745, findings 1 and 4 only (scope narrowed per review):

  • CFE_MSG_Init: the caller-supplied Size is now validated before the memset, with the same bound CFE_MSG_SetSize uses (CFE_MSG_SIZE_OFFSET <= Size <= 0xFFFF + CFE_MSG_SIZE_OFFSET); CFE_MSG_SIZE_OFFSET moved to cfe_msg_priv.h so both share it.
  • GetSystem/GetSize: | instead of + for byte concatenation.

Finding 2 (priext initializer) was already fixed upstream in 13ed881 (#2317). Finding 3 (checksum walk) needs a decision on the BufLen API and is left out of this PR.

Testing performed

test_cfe_msg_init.c adds an oversize case (a 65 KB buffer, Size one over the bound) and an underflow case (Size below CFE_MSG_SIZE_OFFSET), both expecting CFE_MSG_BAD_ARGUMENT, plus the exact-bound case. Run locally in the cFS dev bundle with the tests kept and cfe_msg_init.c reverted to dev: coverage-msg-ALL dies with a SegFault in Test_MSG_Init (the oversize memset runs past the message). With the fix, coverage-msg-ALL passes.

Expected behavior changes

Malformed message inputs that previously caused OOB reads/writes now return an error. Bit-field concatenation in GetSystem/GetSize is now correct for fields with overlapping bits.

System(s) tested on

Hardware: x86_64 (WSL2), OS: Ubuntu 22.04, Versions: cFE dev 2612eb0 with OSAL and PSP from the cFS dev bundle, native build with ENABLE_UNIT_TESTS

Contributor Info

Filip Koscak - phil.phaulre@gmail.com

@dzbaker

dzbaker commented Aug 31, 2026

Copy link
Copy Markdown
Collaborator

@philphauler Thank you for your contribution. Please resolve the workflow failures.

@philphauler
philphauler force-pushed the fix-2745-msg-4-findings branch 2 times, most recently from 6ffe6c4 to 5e83079 Compare August 31, 2026 16:49

@sylvesterkaczmarek sylvesterkaczmarek 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.

The PR is scoped as fixing all four findings from #2745, but the current head still leaves two of them untouched: the malformed CFE_MSG_CMD_HDR_INIT initializer in default_cfe_msg_hdr_priext.h, and the checksum walk in cfe_msg_sechdr_checksum.c (that file only has BOM/whitespace changes here). Could those two fixes be included, or should the PR scope/title be narrowed to the findings actually addressed?

@philphauler

Copy link
Copy Markdown
Contributor Author

Imma look more into it 🫡⚡️

…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 nasa#2745 are not part of this change: the priext initializer
was fixed by 13ed881 (nasa#2317) and the checksum walk needs a caller-provided
bound, which is an API decision.
@philphauler
philphauler force-pushed the fix-2745-msg-4-findings branch from 4164a5f to 4e2c757 Compare September 5, 2026 20:55
@philphauler philphauler changed the title Fix #2745: cfe/modules/msg 4 findings HIGH/MEDIUM/LOW Fix #2745, validate CFE_MSG_Init size and use OR for byte concatenation Sep 5, 2026
@philphauler

Copy link
Copy Markdown
Contributor Author

Hi @sylvesterkaczmarek 👋 good call on the scope. Narrowed to findings 1 and 4 (4e2c757, squashed to one commit): finding 2 was already fixed upstream in 13ed881 (#2317) and finding 3 needs a decision on the BufLen API that does not belong in this PR. The size bound now mirrors CFE_MSG_SetSize (CFE_MSG_SIZE_OFFSET moved to cfe_msg_priv.h) and the UT adds the 65 KB-over-a-header and underflow cases. coverage-msg-ALL passes. Have a great day!

@sylvesterkaczmarek sylvesterkaczmarek 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.

The scope is now clear and matches the implementation: findings 1 and 4 are addressed here, with the already-upstream and API-design items left out. The shared size offset and boundary tests make the CFE_MSG_Init fix consistent with CFE_MSG_SetSize, and the byte concatenation uses the intended unsigned shift/OR. My scope concern is resolved.

@philphauler

Copy link
Copy Markdown
Contributor Author

Eyyyy let's gooooo ⚡️🚀

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 4e2c757, 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 4e2c757 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 nasa#2745 remain out of scope, per 4e2c757: the
priext initializer was fixed by 13ed881 (nasa#2317), and the checksum walk
needs a caller-provided bound, which is an API decision.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HDRvC9ePDP3i6xpxFAECFg
@philphauler

philphauler commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Hey @dzbaker 👋 following up on the workflow failures: they were on the earlier head (5e83079), where CFE_MSG_Init rejected anything above CFE_MISSION_SB_MAX_SB_MSG_SIZE. The functional test's CFE_FT_BigMsg is that max plus the SB header, so Init returned BAD_ARGUMENT before CFE_SB_TransmitBuffer could say MSG_TOO_BIG.

The current head (4e2c757, the one @sylvesterkaczmarek approved) bounds Init by the CCSDS length field instead (CFE_MSG_SIZE_OFFSET up to 0xFFFF + CFE_MSG_SIZE_OFFSET), leaving the SB buffer limit to TransmitBuffer as the test expects. Ran the functional suite on this head locally the way the workflow does (cFS dev bundle, core-cpu1 + cfe_testcase): SUMMARY TOTAL::2242 PASS::1867 FAIL::0, and sb_sendrecv_test.c:446-453 pass. Coverage for the msg module is 618/618 with cfe_msg_init.c at 100% lines and branches, and the five changed files are clean under the cFS dev .clang-format.

The nine workflows for 4e2c757 have been sitting in "approve and run" since Sept 5, so the checks shown are still the old head's. Could you kick them off when you get a sec? 🙏⚡

Edit: two more commits landed after this note (18b701d, then its revert 0c3790c), the tree at the head is identical to 4e2c757. The intermediate commit came from a background job of mine that dropped the CCSDS upper bound in Init; I reverted it because that reintroduces the oversize memset before SetSize can reject the length. Sorry for the noise, nothing changed against what was approved.

…ize length is rejected before the buffer is cleared

Reverts 18b701d: 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 4e2c757.

@sylvesterkaczmarek sylvesterkaczmarek 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.

Rechecked current 0c3790cc. Its tree is identical to the previously approved 4e2c757d (fe1ebb26579b in both cases); the intervening change was fully reverted. The scoped #2745 fixes and boundary coverage are therefore unchanged. No blocker from me.

@philphauler

Copy link
Copy Markdown
Contributor Author

Heyy @sylvesterkaczmarek im actually working here on improving my agentic setup, Lestarch just recently setup a really cool bot himself for checking all the issues in code, in order to offload and mitigate bugs and mistakes, so I got inspired and am re-reviewing my work ✨🙌

@philphauler

Copy link
Copy Markdown
Contributor Author

Quick update: my Individual CLA for the cFS framework went to GSFC-SoftwareRelease on 2026-09-16, after this PR was opened, so I've ticked that box in the checklist. Happy to adjust anything if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants