Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add checks for pOtaBuffer to be non_null #402

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Bujain
Copy link
Contributor

@Bujain Bujain commented Dec 9, 2021

OTA_Init is a public function and a user can pass NULL value to pOtaBuffer which can result in NULL pointer de-referencing in initializeAppBuffers function.

Description

This PR adds checks such that if NULL value is passed to pOtaBuffer then the otaAgent would not run and display error in the logs indicating the NULL pointer in pOtaBuffer.

Checklist:

  • I have tested my changes. No regression in existing tests.
  • My code is formatted using Uncrustify.
  • I have read and applied the rules stated in CONTRIBUTING.md.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

}
else
{
LogError( ( "Error: Thing name is too long.\r\n" ) );
uint32_t strLength = ( uint32_t ) ( strlen( ( const char * ) pThingName ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The signature of this function should be updated from:

OtaErr_t OTA_Init( OtaAppBuffer_t * pOtaBuffer,
                   const OtaInterfaces_t * pOtaInterfaces,
                   const uint8_t * pThingName,
                   OtaAppCallback_t OtaAppCallback )

to

OtaErr_t OTA_Init( OtaAppBuffer_t * pOtaBuffer,
                   const OtaInterfaces_t * pOtaInterfaces,
                   const char * pThingName,
                   OtaAppCallback_t OtaAppCallback )

if pThingName is indeed a null-terminated string.

LogError( ( "Error: Thing name is too long.\r\n" ) );
uint32_t strLength = ( uint32_t ) ( strlen( ( const char * ) pThingName ) );

if( strLength <= otaconfigMAX_THINGNAME_LEN )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be simplified to

if( strnlen( pThingName, otaconfigMAX_THINGNAME_LEN + 1 ) <= otaconfigMAX_THINGNAME_LEN )
{
    (void) strncpy(  otaAgent.pThingName, pThingName, otaconfigMAX_THINGNAME_LEN );
}
else
{
    LogError("...");
    returnStatus = xxx;
}

}
else
{
LogError( ( "Error: Thing name is too long.\r\n" ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't returnStatus be set to a particular value here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
LogError( ( "Error: Thing name is too long.\r\n" ) );
LogError( ( "Error: Thing name is too long." ) );

*/
( void ) memcpy( otaAgent.pThingName, pThingName, strLength + 1UL );
returnStatus = OtaErrNone;
LogError( ( "Error: Thing name is NULL.\r\n" ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
LogError( ( "Error: Thing name is NULL.\r\n" ) );
LogError( ( "Error: Thing name is NULL." ) );
returnStatus = OtaErrInvalidArg;

LogError messages don't include an \r\n in the rest of this file.

{
LogError( ( "Error: Thing name is NULL.\r\n" ) );
LogError( ( "Error: pOtaBuffer is NULL.\r\n" ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
LogError( ( "Error: pOtaBuffer is NULL.\r\n" ) );
LogError( ( "Error: pOtaBuffer is NULL." ) );

{
LogError( ( "Error: Thing name is NULL.\r\n" ) );
LogError( ( "Error: pOtaBuffer is NULL.\r\n" ) );
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
returnStatus = OtaErrInvalidArg;
}

{
LogError( ( "Error: Thing name is NULL.\r\n" ) );
LogError( ( "Error: pOtaBuffer is NULL.\r\n" ) );
}
else
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
else
if( returnStatus == OtaErrNone )

{
OtaAppBuffer_t * otaAppBuffer = NULL;

OTA_Init( otaAppBuffer,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return value of OTA_Init should be checked ( Should be OtaErrInvalidArg right? ).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the current implementation of the function OTA_Init only returns two values. If there are no errors during the initialization of the agent then it returns OtaErrNone and OtaErrUninitialized otherwise.

*/
( void ) memcpy( otaAgent.pThingName, pThingName, strLength + 1UL );
returnStatus = OtaErrNone;
LogError( ( "Error: Thing name is NULL.\r\n" ) );
}
else
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be turned into an if / else if / else set of statements to improve readability.

@@ -3094,9 +3094,6 @@ OtaErr_t OTA_Init( OtaAppBuffer_t * pOtaBuffer,
*/
otaAgent.pOtaInterface = pOtaInterfaces;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is pOtaInterfaces allowed to be null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, pOtaInterfaces can be NULL. There is a conditional statement in OTA_EventProcessingTask function which checks if the pOtaInterfaces are NULL, if they are then the OTA agent would throws an error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not return an error if the parameter is invalid?

@@ -3094,9 +3094,6 @@ OtaErr_t OTA_Init( OtaAppBuffer_t * pOtaBuffer,
*/
otaAgent.pOtaInterface = pOtaInterfaces;

/* Initialize application buffers. */
initializeAppBuffers( pOtaBuffer );

/* Initialize local buffers. */
initializeLocalBuffers();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is OtaAppCallback allowed to be NULL?

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.

None yet

3 participants