Skip to content

Undefined behavior - hk unit test #353

@mariaKt

Description

@mariaKt

The hk unit test contains an undefined behavior where out of bounds array access occurs. See paragraph 6.5.6/8 of the C18 standard for more details.

The undefined behavior occurs when the pointer variable OuterCpyEntry is dereferenced:

if ( (OuterCpyEntry->OutputMid != HK_UNDEFINED_ENTRY) &&

The pointer variable OuterCpyEntry is initialized to point to elements of array StartOfCopyTable inside a loop as follows:

for (Loop1 = 0; Loop1 < HK_COPY_TABLE_ENTRIES; Loop1++)
{
OuterCpyEntry = & StartOfCopyTable [Loop1];

The value that the array pointer StartOfCopyTable has when the undefined behavior occurs is the value of the global pointer variable HK_AppData.CopyTablePtr, propagated to it through the call chain. The HK_AppData.CopyTablePtr global pointer variable is initialized in line 92 of the HK_AppMain_Test_Nominal test, one of the tests performed by the hk unit test.

void HK_AppMain_Test_Nominal(void)
{
hk_copy_table_entry_t CopyTable = {0};
hk_runtime_tbl_entry_t RuntimeTable = {0};
HK_AppData.CopyTablePtr = &CopyTable;

Note that HK_AppData.CopyTablePtr is initialized here as an one element array. Given that, and the fact that the array is accessed in a loop that assumes more than one elements, we have an out of bounds array access.

Note that there is code between the original initialization of the global pointer variable HK_AppData.CopyTablePtr and the out of bounds access that can load an array into memory and overwrite the prior value of HK_AppData.CopyTablePtr to point to it.

Status = CFE_TBL_Load (HK_AppData.CopyTableHandle,
CFE_TBL_SRC_FILE,
HK_COPY_TABLE_FILENAME);

Status = CFE_TBL_GetAddress ( (void *) (& HK_AppData.CopyTablePtr),
HK_AppData.CopyTableHandle);

However, this code is skipped because the hooks CFE_TBL_Load and CFE_TBL_GetAddress (that would load the array and change the value of HK_AppData.CopyTablePtr respectively) are configured at the initialization of the test to only return a specified return code.

/* Set to make HK_AppInit return CFE_SUCCESS */
Ut_CFE_TBL_SetReturnCode(UT_CFE_TBL_LOAD_INDEX, CFE_SUCCESS, 1);
/* Set to prevent error message in HK_AppInit */
Ut_CFE_TBL_SetReturnCode(UT_CFE_TBL_GETADDRESS_INDEX, CFE_TBL_INFO_UPDATED, 1);

A similar issue occurs for other tests performed by the hk unit test.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions