Skip to content

Commit faa92f7

Browse files
committed
Implement secure stack sealing as per ARM's recommendation
Signed-off-by: Gaurav Aggarwal <[email protected]>
1 parent f87404b commit faa92f7

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
@@ -50,6 +50,16 @@
5050
*/
5151
#define securecontextCONTROL_VALUE_UNPRIVILEGED 0x03
5252

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

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

228+
/* Seal the created secure process stack. */
229+
*( uint32_t * )( pucStackMemory + ulSecureStackSize ) = securecontextSTACK_SEAL_VALUE;
230+
*( uint32_t * )( pucStackMemory + ulSecureStackSize + 4 ) = securecontextSTACK_SEAL_VALUE;
231+
218232
/* The stack cannot go beyond this location. This value is
219233
* programmed in the PSPLIM register on context switch.*/
220234
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
@@ -50,6 +50,16 @@
5050
*/
5151
#define securecontextCONTROL_VALUE_UNPRIVILEGED 0x03
5252

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

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

228+
/* Seal the created secure process stack. */
229+
*( uint32_t * )( pucStackMemory + ulSecureStackSize ) = securecontextSTACK_SEAL_VALUE;
230+
*( uint32_t * )( pucStackMemory + ulSecureStackSize + 4 ) = securecontextSTACK_SEAL_VALUE;
231+
218232
/* The stack cannot go beyond this location. This value is
219233
* programmed in the PSPLIM register on context switch.*/
220234
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
@@ -50,6 +50,16 @@
5050
*/
5151
#define securecontextCONTROL_VALUE_UNPRIVILEGED 0x03
5252

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

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

228+
/* Seal the created secure process stack. */
229+
*( uint32_t * )( pucStackMemory + ulSecureStackSize ) = securecontextSTACK_SEAL_VALUE;
230+
*( uint32_t * )( pucStackMemory + ulSecureStackSize + 4 ) = securecontextSTACK_SEAL_VALUE;
231+
218232
/* The stack cannot go beyond this location. This value is
219233
* programmed in the PSPLIM register on context switch.*/
220234
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
@@ -50,6 +50,16 @@
5050
*/
5151
#define securecontextCONTROL_VALUE_UNPRIVILEGED 0x03
5252

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

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

228+
/* Seal the created secure process stack. */
229+
*( uint32_t * )( pucStackMemory + ulSecureStackSize ) = securecontextSTACK_SEAL_VALUE;
230+
*( uint32_t * )( pucStackMemory + ulSecureStackSize + 4 ) = securecontextSTACK_SEAL_VALUE;
231+
218232
/* The stack cannot go beyond this location. This value is
219233
* programmed in the PSPLIM register on context switch.*/
220234
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
@@ -50,6 +50,16 @@
5050
*/
5151
#define securecontextCONTROL_VALUE_UNPRIVILEGED 0x03
5252

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

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

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

0 commit comments

Comments
 (0)