Skip to content

Commit 7c0c890

Browse files
authored
Add stack size validation in SecureContext_AllocateContext (#1402)
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 7c0c890

File tree

15 files changed

+135
-30
lines changed

15 files changed

+135
-30
lines changed

portable/ARMv8M/secure/context/secure_context.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,15 @@ secureportNON_SECURE_CALLABLE void SecureContext_Init( void )
213213
/* Were we able to get a free context? */
214214
if( ulSecureContextIndex < secureconfigMAX_SECURE_CONTEXTS )
215215
{
216-
/* Allocate the stack space. */
217-
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
216+
/* Allocate the stack space if possible. */
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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,15 @@ secureportNON_SECURE_CALLABLE void SecureContext_Init( void )
213213
/* Were we able to get a free context? */
214214
if( ulSecureContextIndex < secureconfigMAX_SECURE_CONTEXTS )
215215
{
216-
/* Allocate the stack space. */
217-
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
216+
/* Allocate the stack space if possible. */
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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,15 @@ secureportNON_SECURE_CALLABLE void SecureContext_Init( void )
213213
/* Were we able to get a free context? */
214214
if( ulSecureContextIndex < secureconfigMAX_SECURE_CONTEXTS )
215215
{
216-
/* Allocate the stack space. */
217-
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
216+
/* Allocate the stack space if possible. */
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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,15 @@ secureportNON_SECURE_CALLABLE void SecureContext_Init( void )
213213
/* Were we able to get a free context? */
214214
if( ulSecureContextIndex < secureconfigMAX_SECURE_CONTEXTS )
215215
{
216-
/* Allocate the stack space. */
217-
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
216+
/* Allocate the stack space if possible. */
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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,15 @@ secureportNON_SECURE_CALLABLE void SecureContext_Init( void )
213213
/* Were we able to get a free context? */
214214
if( ulSecureContextIndex < secureconfigMAX_SECURE_CONTEXTS )
215215
{
216-
/* Allocate the stack space. */
217-
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
216+
/* Allocate the stack space if possible. */
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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,15 @@ secureportNON_SECURE_CALLABLE void SecureContext_Init( void )
213213
/* Were we able to get a free context? */
214214
if( ulSecureContextIndex < secureconfigMAX_SECURE_CONTEXTS )
215215
{
216-
/* Allocate the stack space. */
217-
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
216+
/* Allocate the stack space if possible. */
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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,15 @@ secureportNON_SECURE_CALLABLE void SecureContext_Init( void )
213213
/* Were we able to get a free context? */
214214
if( ulSecureContextIndex < secureconfigMAX_SECURE_CONTEXTS )
215215
{
216-
/* Allocate the stack space. */
217-
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
216+
/* Allocate the stack space if possible. */
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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,15 @@ secureportNON_SECURE_CALLABLE void SecureContext_Init( void )
213213
/* Were we able to get a free context? */
214214
if( ulSecureContextIndex < secureconfigMAX_SECURE_CONTEXTS )
215215
{
216-
/* Allocate the stack space. */
217-
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
216+
/* Allocate the stack space if possible. */
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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,15 @@ secureportNON_SECURE_CALLABLE void SecureContext_Init( void )
213213
/* Were we able to get a free context? */
214214
if( ulSecureContextIndex < secureconfigMAX_SECURE_CONTEXTS )
215215
{
216-
/* Allocate the stack space. */
217-
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
216+
/* Allocate the stack space if possible. */
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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,15 @@ secureportNON_SECURE_CALLABLE void SecureContext_Init( void )
213213
/* Were we able to get a free context? */
214214
if( ulSecureContextIndex < secureconfigMAX_SECURE_CONTEXTS )
215215
{
216-
/* Allocate the stack space. */
217-
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
216+
/* Allocate the stack space if possible. */
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)