1*91f16700SchasingluluSecure Development Guidelines 2*91f16700Schasinglulu============================= 3*91f16700Schasinglulu 4*91f16700SchasingluluThis page contains guidance on what to check for additional security measures, 5*91f16700Schasingluluincluding build options that can be modified to improve security or catch issues 6*91f16700Schasingluluearly in development. 7*91f16700Schasinglulu 8*91f16700SchasingluluSecurity considerations 9*91f16700Schasinglulu----------------------- 10*91f16700Schasinglulu 11*91f16700SchasingluluPart of the security of a platform is handling errors correctly, as described in 12*91f16700Schasingluluthe previous section. There are several other security considerations covered in 13*91f16700Schasingluluthis section. 14*91f16700Schasinglulu 15*91f16700SchasingluluDo not leak secrets to the normal world 16*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 17*91f16700Schasinglulu 18*91f16700SchasingluluThe secure world **must not** leak secrets to the normal world, for example in 19*91f16700Schasingluluresponse to an SMC. 20*91f16700Schasinglulu 21*91f16700SchasingluluHandling Denial of Service attacks 22*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 23*91f16700Schasinglulu 24*91f16700SchasingluluThe secure world **should never** crash or become unusable due to receiving too 25*91f16700Schasinglulumany normal world requests (a *Denial of Service* or *DoS* attack). It should 26*91f16700Schasingluluhave a mechanism for throttling or ignoring normal world requests. 27*91f16700Schasinglulu 28*91f16700SchasingluluPreventing Secure-world timing information leakage via PMU counters 29*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 30*91f16700Schasinglulu 31*91f16700SchasingluluThe Secure world needs to implement some defenses to prevent the Non-secure 32*91f16700Schasingluluworld from making it leak timing information. In general, higher privilege 33*91f16700Schasinglululevels must defend from those below when the PMU is treated as an attack 34*91f16700Schasingluluvector. 35*91f16700Schasinglulu 36*91f16700SchasingluluRefer to the :ref:`Performance Monitoring Unit` guide for detailed information 37*91f16700Schasingluluon the PMU registers. 38*91f16700Schasinglulu 39*91f16700SchasingluluTiming leakage attacks from the Non-secure world 40*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 41*91f16700Schasinglulu 42*91f16700SchasingluluSince the Non-secure world has access to the ``PMCR`` register, it can 43*91f16700Schasingluluconfigure the PMU to increment counters at any exception level and in both 44*91f16700SchasingluluSecure and Non-secure state. Thus, it attempts to leak timing information from 45*91f16700Schasingluluthe Secure world. 46*91f16700Schasinglulu 47*91f16700SchasingluluShown below is an example of such a configuration: 48*91f16700Schasinglulu 49*91f16700Schasinglulu- ``PMEVTYPER0_EL0`` and ``PMCCFILTR_EL0``: 50*91f16700Schasinglulu 51*91f16700Schasinglulu - Set ``P`` to ``0``. 52*91f16700Schasinglulu - Set ``NSK`` to ``1``. 53*91f16700Schasinglulu - Set ``M`` to ``0``. 54*91f16700Schasinglulu - Set ``NSH`` to ``0``. 55*91f16700Schasinglulu - Set ``SH`` to ``1``. 56*91f16700Schasinglulu 57*91f16700Schasinglulu- ``PMCNTENSET_EL0``: 58*91f16700Schasinglulu 59*91f16700Schasinglulu - Set ``P[0]`` to ``1``. 60*91f16700Schasinglulu - Set ``C`` to ``1``. 61*91f16700Schasinglulu 62*91f16700Schasinglulu- ``PMCR_EL0``: 63*91f16700Schasinglulu 64*91f16700Schasinglulu - Set ``DP`` to ``0``. 65*91f16700Schasinglulu - Set ``E`` to ``1``. 66*91f16700Schasinglulu 67*91f16700SchasingluluThis configuration instructs ``PMEVCNTR0_EL0`` and ``PMCCNTR_EL0`` to increment 68*91f16700Schasingluluat Secure EL1, Secure EL2 (if implemented) and EL3. 69*91f16700Schasinglulu 70*91f16700SchasingluluSince the Non-secure world has fine-grained control over where (at which 71*91f16700Schasingluluexception levels) it instructs counters to increment, obtaining event counts 72*91f16700Schasingluluwould allow it to carry out side-channel timing attacks against the Secure 73*91f16700Schasingluluworld. Examples include Spectre, Meltdown, as well as extracting secrets from 74*91f16700Schasinglulucryptographic algorithms with data-dependent variations in their execution 75*91f16700Schasinglulutime. 76*91f16700Schasinglulu 77*91f16700SchasingluluSecure world mitigation strategies 78*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 79*91f16700Schasinglulu 80*91f16700SchasingluluThe ``MDCR_EL3`` register allows EL3 to configure the PMU (among other things). 81*91f16700SchasingluluThe `Arm ARM`_ details all of the bit fields in this register, but for the PMU 82*91f16700Schasingluluthere are two bits which determine the permissions of the counters: 83*91f16700Schasinglulu 84*91f16700Schasinglulu- ``SPME`` for the programmable counters. 85*91f16700Schasinglulu- ``SCCD`` for the cycle counter. 86*91f16700Schasinglulu 87*91f16700SchasingluluDepending on the implemented features, the Secure world can prohibit counting 88*91f16700Schasingluluin AArch64 state via the following: 89*91f16700Schasinglulu 90*91f16700Schasinglulu- ARMv8.2-Debug not implemented: 91*91f16700Schasinglulu 92*91f16700Schasinglulu - Prohibit general event counters and the cycle counter: 93*91f16700Schasinglulu ``MDCR_EL3.SPME == 0 && PMCR_EL0.DP == 1 && !ExternalSecureNoninvasiveDebugEnabled()``. 94*91f16700Schasinglulu 95*91f16700Schasinglulu - ``MDCR_EL3.SPME`` resets to ``0``, so by default general events should 96*91f16700Schasinglulu not be counted in the Secure world. 97*91f16700Schasinglulu - The ``PMCR_EL0.DP`` bit therefore needs to be set to ``1`` when EL3 is 98*91f16700Schasinglulu entered and ``PMCR_EL0`` needs to be saved and restored in EL3. 99*91f16700Schasinglulu - ``ExternalSecureNoninvasiveDebugEnabled()`` is an authentication 100*91f16700Schasinglulu interface which is implementation-defined unless ARMv8.4-Debug is 101*91f16700Schasinglulu implemented. The `Arm ARM`_ has detailed information on this topic. 102*91f16700Schasinglulu 103*91f16700Schasinglulu - The only other way is to disable the ``PMCR_EL0.E`` bit upon entering 104*91f16700Schasinglulu EL3, which disables counting altogether. 105*91f16700Schasinglulu 106*91f16700Schasinglulu- ARMv8.2-Debug implemented: 107*91f16700Schasinglulu 108*91f16700Schasinglulu - Prohibit general event counters: ``MDCR_EL3.SPME == 0``. 109*91f16700Schasinglulu - Prohibit cycle counter: ``MDCR_EL3.SPME == 0 && PMCR_EL0.DP == 1``. 110*91f16700Schasinglulu ``PMCR_EL0`` therefore needs to be saved and restored in EL3. 111*91f16700Schasinglulu 112*91f16700Schasinglulu- ARMv8.5-PMU implemented: 113*91f16700Schasinglulu 114*91f16700Schasinglulu - Prohibit general event counters: as in ARMv8.2-Debug. 115*91f16700Schasinglulu - Prohibit cycle counter: ``MDCR_EL3.SCCD == 1`` 116*91f16700Schasinglulu 117*91f16700SchasingluluIn Aarch32 execution state the ``MDCR_EL3`` alias is the ``SDCR`` register, 118*91f16700Schasingluluwhich has some of the bit fields of ``MDCR_EL3``, most importantly the ``SPME`` 119*91f16700Schasingluluand ``SCCD`` bits. 120*91f16700Schasinglulu 121*91f16700SchasingluluBuild options 122*91f16700Schasinglulu------------- 123*91f16700Schasinglulu 124*91f16700SchasingluluSeveral build options can be used to check for security issues. Refer to the 125*91f16700Schasinglulu:ref:`Build Options` for detailed information on these. 126*91f16700Schasinglulu 127*91f16700Schasinglulu- The ``BRANCH_PROTECTION`` build flag can be used to enable Pointer 128*91f16700Schasinglulu Authentication and Branch Target Identification. 129*91f16700Schasinglulu 130*91f16700Schasinglulu- The ``ENABLE_STACK_PROTECTOR`` build flag can be used to identify buffer 131*91f16700Schasinglulu overflows. 132*91f16700Schasinglulu 133*91f16700Schasinglulu- The ``W`` build flag can be used to enable a number of compiler warning 134*91f16700Schasinglulu options to detect potentially incorrect code. TF-A is tested with ``W=0`` but 135*91f16700Schasinglulu it is recommended to develop against ``W=2`` (which will eventually become the 136*91f16700Schasinglulu default). 137*91f16700Schasinglulu 138*91f16700SchasingluluAdditional guidelines are provided below for some security-related build 139*91f16700Schasingluluoptions: 140*91f16700Schasinglulu 141*91f16700Schasinglulu- The ``ENABLE_CONSOLE_GETC`` build flag should be set to 0 to disable the 142*91f16700Schasinglulu `getc()` feature, which allows the firmware to read characters from the 143*91f16700Schasinglulu console. Keeping this feature enabled is considered dangerous from a security 144*91f16700Schasinglulu point of view because it potentially allows an attacker to inject arbitrary 145*91f16700Schasinglulu data into the firmware. It should only be enabled on a need basis if there is 146*91f16700Schasinglulu a use case for it, for example in a testing or factory environment. 147*91f16700Schasinglulu 148*91f16700Schasinglulu.. rubric:: References 149*91f16700Schasinglulu 150*91f16700Schasinglulu- `Arm ARM`_ 151*91f16700Schasinglulu 152*91f16700Schasinglulu-------------- 153*91f16700Schasinglulu 154*91f16700Schasinglulu*Copyright (c) 2019-2020, Arm Limited. All rights reserved.* 155*91f16700Schasinglulu 156*91f16700Schasinglulu.. _Arm ARM: https://developer.arm.com/docs/ddi0487/latest 157