Skip to content

Commit

Permalink
ota-agent-task: Fix bugs in ota_agent_task.c
Browse files Browse the repository at this point in the history
This commit fixes usage of memcpy with potentially user-defined inputs,
without checking that the buffer could fit these inputs.

Signed-off-by: Reuben Cartwright <Reuben.Cartwright@arm.com>
  • Loading branch information
RC-Repositories authored and urutva committed Sep 26, 2024
1 parent be40ea6 commit 7733f67
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ STATIC OtaEventData_t * prvOTAEventBufferGet( void )
{
eventBuffer[ ulIndex ].bufferUsed = true;
pFreeBuffer = &eventBuffer[ ulIndex ];
pFreeBuffer->dataLength = sizeof( pFreeBuffer->data );
break;
}
}
Expand Down Expand Up @@ -616,13 +617,20 @@ STATIC void prvMqttJobCallback( void * pvIncomingPublishCallbackContext,

if( pData != NULL )
{
memcpy( pData->data, pxPublishInfo->pPayload, pxPublishInfo->payloadLength );
pData->dataLength = pxPublishInfo->payloadLength;
eventMsg.eventId = OtaAgentEventReceivedJobDocument;
eventMsg.pEventData = pData;
if( ( size_t ) pData->dataLength >= pxPublishInfo->payloadLength )
{
memcpy( pData->data, pxPublishInfo->pPayload, pxPublishInfo->payloadLength );
pData->dataLength = pxPublishInfo->payloadLength;
eventMsg.eventId = OtaAgentEventReceivedJobDocument;
eventMsg.pEventData = pData;

/* Send job document received event. */
OTA_SignalEvent( &eventMsg );
/* Send job document received event. */
OTA_SignalEvent( &eventMsg );
}
else
{
LogError( ( "Error: OTA data buffers are too small for the Job message provided.\n" ) );
}
}
else
{
Expand Down Expand Up @@ -666,13 +674,20 @@ STATIC void prvMqttDataCallback( void * pvIncomingPublishCallbackContext,

if( pxData != NULL )
{
memcpy( pxData->data, pxPublishInfo->pPayload, pxPublishInfo->payloadLength );
pxData->dataLength = pxPublishInfo->payloadLength;
eventMsg.eventId = OtaAgentEventReceivedFileBlock;
eventMsg.pEventData = pxData;
if( ( size_t ) pxData->dataLength >= pxPublishInfo->payloadLength )
{
memcpy( pxData->data, pxPublishInfo->pPayload, pxPublishInfo->payloadLength );
pxData->dataLength = pxPublishInfo->payloadLength;
eventMsg.eventId = OtaAgentEventReceivedFileBlock;
eventMsg.pEventData = pxData;

/* Send job document received event. */
OTA_SignalEvent( &eventMsg );
/* Send file block received event. */
OTA_SignalEvent( &eventMsg );
}
else
{
LogError( ( "Error: OTA data buffers are too small for the data message received.\n" ) );
}
}
else
{
Expand Down Expand Up @@ -762,6 +777,9 @@ STATIC void prvMQTTUnsubscribeCompleteCallback( MQTTAgentCommandContext_t * pxCo
}
}

/*
* Precondition: pTopicFilter is not null.
*/
STATIC OtaMqttStatus_t prvMQTTSubscribe( const char * pTopicFilter,
uint16_t topicFilterLength,
uint8_t ucQoS )
Expand Down
2 changes: 2 additions & 0 deletions release_changes/202409121339.change
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
aws-iot-unit-test: Add tests for ota_agent_task.c.
ota-agent-task: Debug unsafe memcpy usages in ota_agent_task.c.

0 comments on commit 7733f67

Please sign in to comment.