Skip to content

Commit 1616e67

Browse files
committed
Add stack size validation in SecureContext_AllocateContext
Validate that ulSecureStackSize + securecontextSTACK_SEAL_SIZE does not overflow before calling pvPortMalloc in the ARMv8-M secure context ports. Reported by Jordan Mecom (Block, Inc.)
1 parent bdcde95 commit 1616e67

15 files changed

Lines changed: 120 additions & 15 deletions

File tree

portable/ARMv8M/secure/context/secure_context.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,14 @@ secureportNON_SECURE_CALLABLE void SecureContext_Init( void )
214214
if( ulSecureContextIndex < secureconfigMAX_SECURE_CONTEXTS )
215215
{
216216
/* Allocate the stack space. */
217-
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
217+
if( ulSecureStackSize > ( UINT32_MAX - securecontextSTACK_SEAL_SIZE ) )
218+
{
219+
pucStackMemory = NULL;
220+
}
221+
else
222+
{
223+
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
224+
}
218225

219226
if( pucStackMemory != NULL )
220227
{

portable/GCC/ARM_CM23/secure/secure_context.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,14 @@ secureportNON_SECURE_CALLABLE void SecureContext_Init( void )
214214
if( ulSecureContextIndex < secureconfigMAX_SECURE_CONTEXTS )
215215
{
216216
/* Allocate the stack space. */
217-
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
217+
if( ulSecureStackSize > ( UINT32_MAX - securecontextSTACK_SEAL_SIZE ) )
218+
{
219+
pucStackMemory = NULL;
220+
}
221+
else
222+
{
223+
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
224+
}
218225

219226
if( pucStackMemory != NULL )
220227
{

portable/GCC/ARM_CM33/secure/secure_context.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,14 @@ secureportNON_SECURE_CALLABLE void SecureContext_Init( void )
214214
if( ulSecureContextIndex < secureconfigMAX_SECURE_CONTEXTS )
215215
{
216216
/* Allocate the stack space. */
217-
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
217+
if( ulSecureStackSize > ( UINT32_MAX - securecontextSTACK_SEAL_SIZE ) )
218+
{
219+
pucStackMemory = NULL;
220+
}
221+
else
222+
{
223+
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
224+
}
218225

219226
if( pucStackMemory != NULL )
220227
{

portable/GCC/ARM_CM35P/secure/secure_context.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,14 @@ secureportNON_SECURE_CALLABLE void SecureContext_Init( void )
214214
if( ulSecureContextIndex < secureconfigMAX_SECURE_CONTEXTS )
215215
{
216216
/* Allocate the stack space. */
217-
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
217+
if( ulSecureStackSize > ( UINT32_MAX - securecontextSTACK_SEAL_SIZE ) )
218+
{
219+
pucStackMemory = NULL;
220+
}
221+
else
222+
{
223+
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
224+
}
218225

219226
if( pucStackMemory != NULL )
220227
{

portable/GCC/ARM_CM52/secure/secure_context.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,14 @@ secureportNON_SECURE_CALLABLE void SecureContext_Init( void )
214214
if( ulSecureContextIndex < secureconfigMAX_SECURE_CONTEXTS )
215215
{
216216
/* Allocate the stack space. */
217-
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
217+
if( ulSecureStackSize > ( UINT32_MAX - securecontextSTACK_SEAL_SIZE ) )
218+
{
219+
pucStackMemory = NULL;
220+
}
221+
else
222+
{
223+
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
224+
}
218225

219226
if( pucStackMemory != NULL )
220227
{

portable/GCC/ARM_CM55/secure/secure_context.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,14 @@ secureportNON_SECURE_CALLABLE void SecureContext_Init( void )
214214
if( ulSecureContextIndex < secureconfigMAX_SECURE_CONTEXTS )
215215
{
216216
/* Allocate the stack space. */
217-
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
217+
if( ulSecureStackSize > ( UINT32_MAX - securecontextSTACK_SEAL_SIZE ) )
218+
{
219+
pucStackMemory = NULL;
220+
}
221+
else
222+
{
223+
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
224+
}
218225

219226
if( pucStackMemory != NULL )
220227
{

portable/GCC/ARM_CM85/secure/secure_context.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,14 @@ secureportNON_SECURE_CALLABLE void SecureContext_Init( void )
214214
if( ulSecureContextIndex < secureconfigMAX_SECURE_CONTEXTS )
215215
{
216216
/* Allocate the stack space. */
217-
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
217+
if( ulSecureStackSize > ( UINT32_MAX - securecontextSTACK_SEAL_SIZE ) )
218+
{
219+
pucStackMemory = NULL;
220+
}
221+
else
222+
{
223+
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
224+
}
218225

219226
if( pucStackMemory != NULL )
220227
{

portable/GCC/ARM_STAR_MC3/secure/secure_context.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,14 @@ secureportNON_SECURE_CALLABLE void SecureContext_Init( void )
214214
if( ulSecureContextIndex < secureconfigMAX_SECURE_CONTEXTS )
215215
{
216216
/* Allocate the stack space. */
217-
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
217+
if( ulSecureStackSize > ( UINT32_MAX - securecontextSTACK_SEAL_SIZE ) )
218+
{
219+
pucStackMemory = NULL;
220+
}
221+
else
222+
{
223+
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
224+
}
218225

219226
if( pucStackMemory != NULL )
220227
{

portable/IAR/ARM_CM23/secure/secure_context.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,14 @@ secureportNON_SECURE_CALLABLE void SecureContext_Init( void )
214214
if( ulSecureContextIndex < secureconfigMAX_SECURE_CONTEXTS )
215215
{
216216
/* Allocate the stack space. */
217-
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
217+
if( ulSecureStackSize > ( UINT32_MAX - securecontextSTACK_SEAL_SIZE ) )
218+
{
219+
pucStackMemory = NULL;
220+
}
221+
else
222+
{
223+
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
224+
}
218225

219226
if( pucStackMemory != NULL )
220227
{

portable/IAR/ARM_CM33/secure/secure_context.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,14 @@ secureportNON_SECURE_CALLABLE void SecureContext_Init( void )
214214
if( ulSecureContextIndex < secureconfigMAX_SECURE_CONTEXTS )
215215
{
216216
/* Allocate the stack space. */
217-
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
217+
if( ulSecureStackSize > ( UINT32_MAX - securecontextSTACK_SEAL_SIZE ) )
218+
{
219+
pucStackMemory = NULL;
220+
}
221+
else
222+
{
223+
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
224+
}
218225

219226
if( pucStackMemory != NULL )
220227
{

0 commit comments

Comments
 (0)