Skip to content

Commit 83e56c3

Browse files
authored
Disallow unprivileged critical sections with MPU wrappers v2 (#1427)
When using MPU wrappers version 2 (configUSE_MPU_WRAPPERS_V1 == 0), portRAISE_PRIVILEGE() is a no-op because the portSVC_RAISE_PRIVILEGE handler is compiled only for MPU wrappers version 1. As a result, an unprivileged task that calls taskENTER_CRITICAL() does not actually raise its privilege, so the subsequent BASEPRI write is ignored by the hardware and the critical section silently fails to mask interrupts. This produces latent, hard-to-debug faults. configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS is therefore not supported with MPU wrappers version 2. In the ARMv7-M MPU ports: - When the option is left undefined under v2, default it to 0 instead of 1 so the dangerous default configuration is safe. - When the option is explicitly set to 1 under v2, raise a compile-time #error so the unsupported configuration is rejected loudly rather than failing silently at run time. Behaviour for MPU wrappers version 1 is unchanged.
1 parent fc25364 commit 83e56c3

4 files changed

Lines changed: 48 additions & 8 deletions

File tree

portable/GCC/ARM_CM3_MPU/port.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,19 @@
5555
#define portNVIC_SYSTICK_CLK ( 0 )
5656
#endif
5757

58+
/* Unprivileged critical sections are not supported when using MPU wrappers
59+
* version 2. Default the option to 0 and reject an explicit value of 1. */
5860
#ifndef configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS
59-
#warning "configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS is not defined. We recommend defining it to 0 in FreeRTOSConfig.h for better security."
60-
#define configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS 1
61+
#if ( configUSE_MPU_WRAPPERS_V1 == 0 )
62+
#define configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS 0
63+
#else
64+
#warning "configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS is not defined. We recommend defining it to 0 in FreeRTOSConfig.h for better security."
65+
#define configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS 1
66+
#endif
67+
#else
68+
#if ( ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) )
69+
#error configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS is not supported with MPU wrappers version 2 ( configUSE_MPU_WRAPPERS_V1 == 0 ).
70+
#endif
6171
#endif
6272

6373
/* Prototype of all Interrupt Service Routines (ISRs). */

portable/GCC/ARM_CM4_MPU/port.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,19 @@
5959
#define portNVIC_SYSTICK_CLK ( 0 )
6060
#endif
6161

62+
/* Unprivileged critical sections are not supported when using MPU wrappers
63+
* version 2. Default the option to 0 and reject an explicit value of 1. */
6264
#ifndef configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS
63-
#warning "configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS is not defined. We recommend defining it to 0 in FreeRTOSConfig.h for better security."
64-
#define configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS 1
65+
#if ( configUSE_MPU_WRAPPERS_V1 == 0 )
66+
#define configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS 0
67+
#else
68+
#warning "configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS is not defined. We recommend defining it to 0 in FreeRTOSConfig.h for better security."
69+
#define configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS 1
70+
#endif
71+
#else
72+
#if ( ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) )
73+
#error configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS is not supported with MPU wrappers version 2 ( configUSE_MPU_WRAPPERS_V1 == 0 ).
74+
#endif
6575
#endif
6676

6777
/* Prototype of all Interrupt Service Routines (ISRs). */

portable/IAR/ARM_CM4F_MPU/port.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,19 @@
6666
#define portNVIC_SYSTICK_CLK_BIT ( 0 )
6767
#endif
6868

69+
/* Unprivileged critical sections are not supported when using MPU wrappers
70+
* version 2. Default the option to 0 and reject an explicit value of 1. */
6971
#ifndef configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS
70-
#warning "configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS is not defined. We recommend defining it to 0 in FreeRTOSConfig.h for better security."
71-
#define configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS 1
72+
#if ( configUSE_MPU_WRAPPERS_V1 == 0 )
73+
#define configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS 0
74+
#else
75+
#warning "configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS is not defined. We recommend defining it to 0 in FreeRTOSConfig.h for better security."
76+
#define configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS 1
77+
#endif
78+
#else
79+
#if ( ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) )
80+
#error configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS is not supported with MPU wrappers version 2 ( configUSE_MPU_WRAPPERS_V1 == 0 ).
81+
#endif
7282
#endif
7383

7484
/* Prototype of all Interrupt Service Routines (ISRs). */

portable/RVDS/ARM_CM4_MPU/port.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,19 @@
4848

4949
#undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
5050

51+
/* Unprivileged critical sections are not supported when using MPU wrappers
52+
* version 2. Default the option to 0 and reject an explicit value of 1. */
5153
#ifndef configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS
52-
#warning "configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS is not defined. We recommend defining it to 0 in FreeRTOSConfig.h for better security."
53-
#define configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS 1
54+
#if ( configUSE_MPU_WRAPPERS_V1 == 0 )
55+
#define configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS 0
56+
#else
57+
#warning "configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS is not defined. We recommend defining it to 0 in FreeRTOSConfig.h for better security."
58+
#define configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS 1
59+
#endif
60+
#else
61+
#if ( ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) )
62+
#error configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS is not supported with MPU wrappers version 2 ( configUSE_MPU_WRAPPERS_V1 == 0 ).
63+
#endif
5464
#endif
5565

5666
/* Prototype of all Interrupt Service Routines (ISRs). */

0 commit comments

Comments
 (0)