Skip to content

fix: guard against NULL data_ptr in CF_CFDP_CopyStringFromLV#501

Open
stark256-spec wants to merge 1 commit into
nasa:devfrom
stark256-spec:fix/null-deref-copy-string-from-lv
Open

fix: guard against NULL data_ptr in CF_CFDP_CopyStringFromLV#501
stark256-spec wants to merge 1 commit into
nasa:devfrom
stark256-spec:fix/null-deref-copy-string-from-lv

Conversation

@stark256-spec
Copy link
Copy Markdown

Summary

CF_CFDP_DoDecodeChunk() returns NULL when the attacker-supplied LV length field in a CFDP Metadata PDU exceeds the remaining bytes in the receive buffer (it also marks the codec state as DONE). Without a corresponding NULL check in CF_CFDP_CopyStringFromLV(), the memcpy(buf, src_lv->data_ptr, ...) call receives a NULL source pointer — undefined behaviour that causes a NULL-pointer dereference (crash / denial-of-service) on most platforms when a crafted Metadata PDU is received.

Fixes #491.

Root cause

// CF_CFDP_DoDecodeChunk sets data_ptr = NULL when bounds check fails
pllv->data_ptr = CF_CFDP_DoDecodeChunk(state, pllv->length);

// ...later in CF_CFDP_CopyStringFromLV, no NULL guard:
if (src_lv->length < buf_maxsz)
{
    memcpy(buf, src_lv->data_ptr, src_lv->length);  // UB if data_ptr == NULL

Fix

Add src_lv->data_ptr != NULL to the existing guard — one line, no signature changes:

if (src_lv->data_ptr != NULL && src_lv->length < buf_maxsz)

CF_CFDP_CopyStringFromLV() now returns CF_ERROR consistently whether the PDU ran out of data (NULL ptr from decode) or the string would overflow the destination buffer.

Note: CF_CFDP_RecvMd() already checks CF_CODEC_IS_OK() before calling this function, which catches the malformed PDU at the Metadata level. This fix adds a defence-in-depth guard at the function level.

CF_CFDP_DoDecodeChunk() sets data_ptr to NULL when the attacker-supplied
LV length field exceeds the remaining PDU bytes. Without an explicit NULL
check, CF_CFDP_CopyStringFromLV() passes NULL directly to memcpy(), which
is undefined behaviour and causes a NULL-pointer dereference on most
platforms (crash / denial of service via crafted CFDP Metadata PDU).

Add a data_ptr != NULL guard before the memcpy so the function returns
CF_ERROR consistently whether the decode ran out of PDU data (NULL ptr)
or the string was longer than the destination buffer.

Fixes: nasa#491
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.

[SECURITY] OOB Heap Read in CF_CFDP_CopyStringFromLV via Unvalidated PDU LV Length Field — Remotely Triggerable via Crafted Metadata PDU

2 participants