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

Ota/fix sudo issue #1487

Merged
merged 24 commits into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
570da97
Fix sudo file path issue
pvyawaha Dec 12, 2020
93f803f
fix path for update file
pvyawaha Dec 12, 2020
8dea7a1
fix compiler warning
pvyawaha Dec 12, 2020
05922a2
Update platform/posix/ota_pal/source/ota_pal_posix.c
pvyawaha Dec 12, 2020
c6a48f2
fix ut
divekarshubham Dec 12, 2020
130011b
fix ut
divekarshubham Dec 12, 2020
2445cb9
Dump platform image state path on error
pvyawaha Dec 12, 2020
8e4aebf
Update submodule ptr
divekarshubham Dec 13, 2020
024456d
Remove snprintf mock since it's not being used
yanjos-dev Dec 13, 2020
6355e90
Remove remaining usage of snprintf in OTA PAL tests
yanjos-dev Dec 13, 2020
55d100b
Add getcwd mock to OTA PAL unit tests
yanjos-dev Dec 13, 2020
8853b8e
Remove unused variable from OTA PAl tests
yanjos-dev Dec 13, 2020
2af54c1
Merge branch 'main' of github.com:aws/aws-iot-device-sdk-embedded-C i…
divekarshubham Dec 13, 2020
9627a0a
Extracting file path generation in a separate function
divekarshubham Dec 13, 2020
7b0541a
Removing unused variables
divekarshubham Dec 13, 2020
5bf5d25
Add OTA PAL test for long file paths
yanjos-dev Dec 13, 2020
7a5c6a3
Addressing PR comments
divekarshubham Dec 14, 2020
4504c03
Merge UT changes
divekarshubham Dec 14, 2020
4784e65
spell fix
divekarshubham Dec 14, 2020
a3d852d
Refactoring to use status and spell fix
divekarshubham Dec 14, 2020
28c31fa
Fix OTA PAL test asserts
yanjos-dev Dec 14, 2020
28d0453
Remove unused variable
yanjos-dev Dec 14, 2020
8e4a008
Using strcat instead on strncat and ota submodule update
divekarshubham Dec 14, 2020
bcf7882
Merge branch 'ota/fix_sudo_issue' of github.com:aws/aws-iot-device-sd…
divekarshubham Dec 14, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions platform/posix/ota_pal/source/include/ota_pal_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,22 @@

#include "ota.h"



/**
* @brief Maximum file path length on Linux
*/
#define OTA_FILE_PATH_LENGTH_MAX 512

/**
* @brief The OTA platform interface status for generating
* absolute file path from the incoming relative file path.
*/
typedef enum OtaPalPathGenStatus
{
OtaPalFileGenSuccess, /*!< @brief Absolute path generation success. */
OtaPalCWDFailed, /*!< @brief getcwd failed to output path. */
OtaPalBufferInsufficient /*!< @brief Buffer insufficient for storing the file path. */
} OtaPalPathGenStatus_t;

/**
* @brief Abort an OTA transfer.
*
Expand Down
42 changes: 32 additions & 10 deletions platform/posix/ota_pal/source/ota_pal_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ static OtaPalStatus_t otaPal_CheckFileSignature( OtaFileContext_t * const C );
* @param realFilePath Buffer to store the file path + file name.
* @param pFilePath File name to append to the end of current path.
*/
static void getFilePathFromCWD( char * realFilePath,
const char * pFilePath );
static OtaPalPathGenStatus_t getFilePathFromCWD( char * realFilePath,
const char * pFilePath );

/*-----------------------------------------------------------*/

Expand Down Expand Up @@ -332,31 +332,36 @@ static OtaPalStatus_t otaPal_CheckFileSignature( OtaFileContext_t * const C )
return OTA_PAL_COMBINE_ERR( mainErr, 0 );
}

static void getFilePathFromCWD( char * pCompleteFilePath,
const char * pFileName )
static OtaPalPathGenStatus_t getFilePathFromCWD( char * pCompleteFilePath,
const char * pFileName )
{
char * pCurrentDir = NULL;
OtaPalPathGenStatus_t status = OtaPalFileGenSuccess;

/* Get current directory. */
pCurrentDir = getcwd( pCompleteFilePath, OTA_FILE_PATH_LENGTH_MAX - 1 );
cobusve marked this conversation as resolved.
Show resolved Hide resolved

if( pCurrentDir == NULL )
{
LogError( ( "Failed to get current working directory: %s", strerror( errno ) ) );
status = OtaPalCWDFailed;
}
else
{
/* Add the filename . */
if( strlen( pCompleteFilePath ) + strlen( pFileName ) + 2U > OTA_FILE_PATH_LENGTH_MAX )
{
LogError( ( "Insufficient space to generate file path" ) );
cobusve marked this conversation as resolved.
Show resolved Hide resolved
status = OtaPalBufferInsufficient;
}
else
{
pCompleteFilePath[ strlen( pCompleteFilePath ) ] = '/';
strncat( pCompleteFilePath, pFileName, strlen( pFileName ) + 1U );
strcat( pCompleteFilePath, "/" );
strncat( pCompleteFilePath, pFileName, strlen( pFileName ) );
cobusve marked this conversation as resolved.
Show resolved Hide resolved
}
}

return status;
}

/*-----------------------------------------------------------*/
Expand Down Expand Up @@ -409,21 +414,26 @@ OtaPalStatus_t otaPal_CreateFileForRx( OtaFileContext_t * const C )
{
OtaPalStatus_t result = OTA_PAL_COMBINE_ERR( OtaPalUninitialized, 0 );
char realFilePath[ OTA_FILE_PATH_LENGTH_MAX ];

OtaPalPathGenStatus_t status = OtaPalFileGenSuccess;

if( C != NULL )
{
if( C->pFilePath != NULL )
{
if( C->pFilePath[ 0 ] != ( uint8_t ) '/' )
{
getFilePathFromCWD( realFilePath, ( const char * ) C->pFilePath );
status = getFilePathFromCWD( realFilePath, ( const char * ) C->pFilePath );
}
else
{
( void ) strncpy( realFilePath, ( const char * ) C->pFilePath, strlen( ( const char * ) C->pFilePath ) + 1U );
}

if( status != OtaPalFileGenSuccess )
{
LogError( ( "Could not generate the absolute path for the file" ) );
}

/* POSIX port using standard library */
/* coverity[misra_c_2012_rule_21_6_violation] */
C->pFile = fopen( ( const char * ) realFilePath, "w+b" );
cobusve marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -578,6 +588,7 @@ OtaPalStatus_t otaPal_SetPlatformImageState( OtaFileContext_t * const C,
OtaImageState_t eState )
{
OtaPalMainStatus_t mainErr = OtaPalBadImageState;
OtaPalPathGenStatus_t status = OtaPalFileGenSuccess;
int32_t subErr = 0;
FILE * pPlatformImageState = NULL;
char imageStateFile[ OTA_FILE_PATH_LENGTH_MAX ] = { 0 };
Expand All @@ -587,7 +598,12 @@ OtaPalStatus_t otaPal_SetPlatformImageState( OtaFileContext_t * const C,
if( ( eState != OtaImageStateUnknown ) && ( eState <= OtaLastImageState ) )
{
/* Get file path for the image state file. */
getFilePathFromCWD( imageStateFile, OTA_PLATFORM_IMAGE_STATE_FILE );
status = getFilePathFromCWD( imageStateFile, OTA_PLATFORM_IMAGE_STATE_FILE );

if( status != OtaPalFileGenSuccess )
{
LogError( ( "Could not generate the absolute path for the file" ) );
}

/* POSIX port using standard library */
/* coverity[misra_c_2012_rule_21_6_violation] */
cobusve marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -664,12 +680,18 @@ OtaPalImageState_t otaPal_GetPlatformImageState( OtaFileContext_t * const C )
FILE * pPlatformImageState = NULL;
OtaImageState_t eSavedAgentState = OtaImageStateUnknown;
OtaPalImageState_t ePalState = OtaPalImageStateUnknown;
OtaPalPathGenStatus_t status = OtaPalFileGenSuccess;
char imageStateFile[ OTA_FILE_PATH_LENGTH_MAX ] = { 0 };

( void ) C;

/* Get file path for the image state file. */
getFilePathFromCWD( imageStateFile, OTA_PLATFORM_IMAGE_STATE_FILE );
status = getFilePathFromCWD( imageStateFile, OTA_PLATFORM_IMAGE_STATE_FILE );

if( status != OtaPalFileGenSuccess )
{
LogError( ( "Could not generate the absolute path for the file" ) );
}

/* POSIX port using standard library */
/* coverity[misra_c_2012_rule_21_6_violation] */
cobusve marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
30 changes: 29 additions & 1 deletion platform/posix/ota_pal/utest/ota_pal_posix_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,34 @@ void test_OTAPAL_CreateFileForRx_PathTypes( void )
TEST_ASSERT_EQUAL( OtaPalSuccess, result );
}

/**
* @brief Test that otaPal_CreateFileForRx will correctly handle a file path
* that is too long.
*/
void test_OTAPAL_CreateFileForRx_InvalidPathLength( void )
{
OtaPalMainStatus_t result;
FILE placeholder_file;
OtaFileContext_t otaFileContext;
const size_t invalidLength = OTA_FILE_PATH_LENGTH_MAX + 1U;
char invalidLengthPath[ invalidLength ];
size_t i;

/* Test calling getcwd and having it return a path that is too long. */
for( i = 0U; i < ( invalidLength - 1U ); ++i )
{
invalidLengthPath[ i ] = 'x';
}

invalidLengthPath[ invalidLength - 1U ] = '\0';
otaFileContext.pFilePath = ( uint8_t * ) "placeholder_path";
getcwd_ExpectAnyArgsAndReturn( "placeholder_return" );
getcwd_ReturnArrayThruPtr_buf( invalidLengthPath, invalidLength );
fopen_ExpectAnyArgsAndReturn( &placeholder_file );
result = OTA_PAL_MAIN_ERR( otaPal_CreateFileForRx( &otaFileContext ) );
TEST_ASSERT_EQUAL( OtaPalSuccess, result );
}

/**
* @brief Test that otaPal_CreateFileForRx will return correct result code.
*/
Expand Down Expand Up @@ -1153,5 +1181,5 @@ void test_OTAPAL_GetPlatformImageState_getcwd_fail( void )
OTA_PAL_FailSingleMock_unistd( getcwd_fn );
OTA_PAL_FailSingleMock_stdio( none_fn, NULL );
ePalImageState = otaPal_GetPlatformImageState( &otaFileContext );
TEST_ASSERT_EQUAL( OtaPalImageStateValid, ePalImageState );
TEST_ASSERT_EQUAL( OtaPalImageStateInvalid, ePalImageState );
}