Skip to content

Commit f2ce7d3

Browse files
committed
Implement secure stack sealing as per ARM's recommendation
Signed-off-by: Gaurav Aggarwal <aggarg@amazon.com>
1 parent d1bea3e commit f2ce7d3

File tree

5 files changed

+95
-25
lines changed

5 files changed

+95
-25
lines changed

portable/ARMv8M/secure/context/secure_context.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@
5151
*/
5252
#define securecontextCONTROL_VALUE_UNPRIVILEGED 0x03
5353

54+
/**
55+
* @brief Size of stack seal values in bytes.
56+
*/
57+
#define securecontextSTACK_SEAL_SIZE 8
58+
59+
/**
60+
* @brief Stack seal value as recommended by ARM.
61+
*/
62+
#define securecontextSTACK_SEAL_VALUE 0xFEF5EDA5
63+
5464
/**
5565
* @brief Maximum number of secure contexts.
5666
*/
@@ -204,18 +214,22 @@ secureportNON_SECURE_CALLABLE void SecureContext_Init( void )
204214
if( ulSecureContextIndex < secureconfigMAX_SECURE_CONTEXTS )
205215
{
206216
/* Allocate the stack space. */
207-
pucStackMemory = pvPortMalloc( ulSecureStackSize );
217+
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
208218

209219
if( pucStackMemory != NULL )
210220
{
211221
/* Since stack grows down, the starting point will be the last
212222
* location. Note that this location is next to the last
213-
* allocated byte because the hardware decrements the stack
214-
* pointer before writing i.e. if stack pointer is 0x2, a push
215-
* operation will decrement the stack pointer to 0x1 and then
216-
* write at 0x1. */
223+
* allocated byte for stack (excluding the space for seal values)
224+
* because the hardware decrements the stack pointer before
225+
* writing i.e. if stack pointer is 0x2, a push operation will
226+
* decrement the stack pointer to 0x1 and then write at 0x1. */
217227
xSecureContexts[ ulSecureContextIndex ].pucStackStart = pucStackMemory + ulSecureStackSize;
218228

229+
/* Seal the created secure process stack. */
230+
*( uint32_t * )( pucStackMemory + ulSecureStackSize ) = securecontextSTACK_SEAL_VALUE;
231+
*( uint32_t * )( pucStackMemory + ulSecureStackSize + 4 ) = securecontextSTACK_SEAL_VALUE;
232+
219233
/* The stack cannot go beyond this location. This value is
220234
* programmed in the PSPLIM register on context switch.*/
221235
xSecureContexts[ ulSecureContextIndex ].pucStackLimit = pucStackMemory;

portable/GCC/ARM_CM23/secure/secure_context.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@
5151
*/
5252
#define securecontextCONTROL_VALUE_UNPRIVILEGED 0x03
5353

54+
/**
55+
* @brief Size of stack seal values in bytes.
56+
*/
57+
#define securecontextSTACK_SEAL_SIZE 8
58+
59+
/**
60+
* @brief Stack seal value as recommended by ARM.
61+
*/
62+
#define securecontextSTACK_SEAL_VALUE 0xFEF5EDA5
63+
5464
/**
5565
* @brief Maximum number of secure contexts.
5666
*/
@@ -204,18 +214,22 @@ secureportNON_SECURE_CALLABLE void SecureContext_Init( void )
204214
if( ulSecureContextIndex < secureconfigMAX_SECURE_CONTEXTS )
205215
{
206216
/* Allocate the stack space. */
207-
pucStackMemory = pvPortMalloc( ulSecureStackSize );
217+
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
208218

209219
if( pucStackMemory != NULL )
210220
{
211221
/* Since stack grows down, the starting point will be the last
212222
* location. Note that this location is next to the last
213-
* allocated byte because the hardware decrements the stack
214-
* pointer before writing i.e. if stack pointer is 0x2, a push
215-
* operation will decrement the stack pointer to 0x1 and then
216-
* write at 0x1. */
223+
* allocated byte for stack (excluding the space for seal values)
224+
* because the hardware decrements the stack pointer before
225+
* writing i.e. if stack pointer is 0x2, a push operation will
226+
* decrement the stack pointer to 0x1 and then write at 0x1. */
217227
xSecureContexts[ ulSecureContextIndex ].pucStackStart = pucStackMemory + ulSecureStackSize;
218228

229+
/* Seal the created secure process stack. */
230+
*( uint32_t * )( pucStackMemory + ulSecureStackSize ) = securecontextSTACK_SEAL_VALUE;
231+
*( uint32_t * )( pucStackMemory + ulSecureStackSize + 4 ) = securecontextSTACK_SEAL_VALUE;
232+
219233
/* The stack cannot go beyond this location. This value is
220234
* programmed in the PSPLIM register on context switch.*/
221235
xSecureContexts[ ulSecureContextIndex ].pucStackLimit = pucStackMemory;

portable/GCC/ARM_CM33/secure/secure_context.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@
5151
*/
5252
#define securecontextCONTROL_VALUE_UNPRIVILEGED 0x03
5353

54+
/**
55+
* @brief Size of stack seal values in bytes.
56+
*/
57+
#define securecontextSTACK_SEAL_SIZE 8
58+
59+
/**
60+
* @brief Stack seal value as recommended by ARM.
61+
*/
62+
#define securecontextSTACK_SEAL_VALUE 0xFEF5EDA5
63+
5464
/**
5565
* @brief Maximum number of secure contexts.
5666
*/
@@ -204,18 +214,22 @@ secureportNON_SECURE_CALLABLE void SecureContext_Init( void )
204214
if( ulSecureContextIndex < secureconfigMAX_SECURE_CONTEXTS )
205215
{
206216
/* Allocate the stack space. */
207-
pucStackMemory = pvPortMalloc( ulSecureStackSize );
217+
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
208218

209219
if( pucStackMemory != NULL )
210220
{
211221
/* Since stack grows down, the starting point will be the last
212222
* location. Note that this location is next to the last
213-
* allocated byte because the hardware decrements the stack
214-
* pointer before writing i.e. if stack pointer is 0x2, a push
215-
* operation will decrement the stack pointer to 0x1 and then
216-
* write at 0x1. */
223+
* allocated byte for stack (excluding the space for seal values)
224+
* because the hardware decrements the stack pointer before
225+
* writing i.e. if stack pointer is 0x2, a push operation will
226+
* decrement the stack pointer to 0x1 and then write at 0x1. */
217227
xSecureContexts[ ulSecureContextIndex ].pucStackStart = pucStackMemory + ulSecureStackSize;
218228

229+
/* Seal the created secure process stack. */
230+
*( uint32_t * )( pucStackMemory + ulSecureStackSize ) = securecontextSTACK_SEAL_VALUE;
231+
*( uint32_t * )( pucStackMemory + ulSecureStackSize + 4 ) = securecontextSTACK_SEAL_VALUE;
232+
219233
/* The stack cannot go beyond this location. This value is
220234
* programmed in the PSPLIM register on context switch.*/
221235
xSecureContexts[ ulSecureContextIndex ].pucStackLimit = pucStackMemory;

portable/IAR/ARM_CM23/secure/secure_context.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@
5151
*/
5252
#define securecontextCONTROL_VALUE_UNPRIVILEGED 0x03
5353

54+
/**
55+
* @brief Size of stack seal values in bytes.
56+
*/
57+
#define securecontextSTACK_SEAL_SIZE 8
58+
59+
/**
60+
* @brief Stack seal value as recommended by ARM.
61+
*/
62+
#define securecontextSTACK_SEAL_VALUE 0xFEF5EDA5
63+
5464
/**
5565
* @brief Maximum number of secure contexts.
5666
*/
@@ -204,18 +214,22 @@ secureportNON_SECURE_CALLABLE void SecureContext_Init( void )
204214
if( ulSecureContextIndex < secureconfigMAX_SECURE_CONTEXTS )
205215
{
206216
/* Allocate the stack space. */
207-
pucStackMemory = pvPortMalloc( ulSecureStackSize );
217+
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
208218

209219
if( pucStackMemory != NULL )
210220
{
211221
/* Since stack grows down, the starting point will be the last
212222
* location. Note that this location is next to the last
213-
* allocated byte because the hardware decrements the stack
214-
* pointer before writing i.e. if stack pointer is 0x2, a push
215-
* operation will decrement the stack pointer to 0x1 and then
216-
* write at 0x1. */
223+
* allocated byte for stack (excluding the space for seal values)
224+
* because the hardware decrements the stack pointer before
225+
* writing i.e. if stack pointer is 0x2, a push operation will
226+
* decrement the stack pointer to 0x1 and then write at 0x1. */
217227
xSecureContexts[ ulSecureContextIndex ].pucStackStart = pucStackMemory + ulSecureStackSize;
218228

229+
/* Seal the created secure process stack. */
230+
*( uint32_t * )( pucStackMemory + ulSecureStackSize ) = securecontextSTACK_SEAL_VALUE;
231+
*( uint32_t * )( pucStackMemory + ulSecureStackSize + 4 ) = securecontextSTACK_SEAL_VALUE;
232+
219233
/* The stack cannot go beyond this location. This value is
220234
* programmed in the PSPLIM register on context switch.*/
221235
xSecureContexts[ ulSecureContextIndex ].pucStackLimit = pucStackMemory;

portable/IAR/ARM_CM33/secure/secure_context.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@
5151
*/
5252
#define securecontextCONTROL_VALUE_UNPRIVILEGED 0x03
5353

54+
/**
55+
* @brief Size of stack seal values in bytes.
56+
*/
57+
#define securecontextSTACK_SEAL_SIZE 8
58+
59+
/**
60+
* @brief Stack seal value as recommended by ARM.
61+
*/
62+
#define securecontextSTACK_SEAL_VALUE 0xFEF5EDA5
63+
5464
/**
5565
* @brief Maximum number of secure contexts.
5666
*/
@@ -204,18 +214,22 @@ secureportNON_SECURE_CALLABLE void SecureContext_Init( void )
204214
if( ulSecureContextIndex < secureconfigMAX_SECURE_CONTEXTS )
205215
{
206216
/* Allocate the stack space. */
207-
pucStackMemory = pvPortMalloc( ulSecureStackSize );
217+
pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
208218

209219
if( pucStackMemory != NULL )
210220
{
211221
/* Since stack grows down, the starting point will be the last
212222
* location. Note that this location is next to the last
213-
* allocated byte because the hardware decrements the stack
214-
* pointer before writing i.e. if stack pointer is 0x2, a push
215-
* operation will decrement the stack pointer to 0x1 and then
216-
* write at 0x1. */
223+
* allocated byte for stack (excluding the space for seal values)
224+
* because the hardware decrements the stack pointer before
225+
* writing i.e. if stack pointer is 0x2, a push operation will
226+
* decrement the stack pointer to 0x1 and then write at 0x1. */
217227
xSecureContexts[ ulSecureContextIndex ].pucStackStart = pucStackMemory + ulSecureStackSize;
218228

229+
/* Seal the created secure process stack. */
230+
*( uint32_t * )( pucStackMemory + ulSecureStackSize ) = securecontextSTACK_SEAL_VALUE;
231+
*( uint32_t * )( pucStackMemory + ulSecureStackSize + 4 ) = securecontextSTACK_SEAL_VALUE;
232+
219233
/* The stack cannot go beyond this location. This value is
220234
* programmed in the PSPLIM register on context switch.*/
221235
xSecureContexts[ ulSecureContextIndex ].pucStackLimit = pucStackMemory;

0 commit comments

Comments
 (0)