1e63f5d12SPaul BeesleySecure Development Guidelines 2e63f5d12SPaul Beesley============================= 32e302371SAmbroise Vincent 42e302371SAmbroise VincentThis page contains guidance on what to check for additional security measures, 52e302371SAmbroise Vincentincluding build options that can be modified to improve security or catch issues 62e302371SAmbroise Vincentearly in development. 72e302371SAmbroise Vincent 8e63f5d12SPaul BeesleySecurity considerations 9e63f5d12SPaul Beesley----------------------- 10e63f5d12SPaul Beesley 11e63f5d12SPaul BeesleyPart of the security of a platform is handling errors correctly, as described in 12e63f5d12SPaul Beesleythe previous section. There are several other security considerations covered in 13e63f5d12SPaul Beesleythis section. 14e63f5d12SPaul Beesley 15e63f5d12SPaul BeesleyDo not leak secrets to the normal world 16e63f5d12SPaul Beesley^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 17e63f5d12SPaul Beesley 18e63f5d12SPaul BeesleyThe secure world **must not** leak secrets to the normal world, for example in 19e63f5d12SPaul Beesleyresponse to an SMC. 20e63f5d12SPaul Beesley 21e63f5d12SPaul BeesleyHandling Denial of Service attacks 22e63f5d12SPaul Beesley^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 23e63f5d12SPaul Beesley 24e63f5d12SPaul BeesleyThe secure world **should never** crash or become unusable due to receiving too 25e63f5d12SPaul Beesleymany normal world requests (a *Denial of Service* or *DoS* attack). It should 26e63f5d12SPaul Beesleyhave a mechanism for throttling or ignoring normal world requests. 27e63f5d12SPaul Beesley 2862c9be71SPetre-Ionut TudorPreventing Secure-world timing information leakage via PMU counters 2962c9be71SPetre-Ionut Tudor^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 3062c9be71SPetre-Ionut Tudor 3162c9be71SPetre-Ionut TudorThe Secure world needs to implement some defenses to prevent the Non-secure 3262c9be71SPetre-Ionut Tudorworld from making it leak timing information. In general, higher privilege 3362c9be71SPetre-Ionut Tudorlevels must defend from those below when the PMU is treated as an attack 3462c9be71SPetre-Ionut Tudorvector. 3562c9be71SPetre-Ionut Tudor 3662c9be71SPetre-Ionut TudorRefer to the :ref:`Performance Monitoring Unit` guide for detailed information 3762c9be71SPetre-Ionut Tudoron the PMU registers. 3862c9be71SPetre-Ionut Tudor 3962c9be71SPetre-Ionut TudorTiming leakage attacks from the Non-secure world 4062c9be71SPetre-Ionut Tudor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 4162c9be71SPetre-Ionut Tudor 4262c9be71SPetre-Ionut TudorSince the Non-secure world has access to the ``PMCR`` register, it can 4362c9be71SPetre-Ionut Tudorconfigure the PMU to increment counters at any exception level and in both 4462c9be71SPetre-Ionut TudorSecure and Non-secure state. Thus, it attempts to leak timing information from 4562c9be71SPetre-Ionut Tudorthe Secure world. 4662c9be71SPetre-Ionut Tudor 4762c9be71SPetre-Ionut TudorShown below is an example of such a configuration: 4862c9be71SPetre-Ionut Tudor 4962c9be71SPetre-Ionut Tudor- ``PMEVTYPER0_EL0`` and ``PMCCFILTR_EL0``: 5062c9be71SPetre-Ionut Tudor 5162c9be71SPetre-Ionut Tudor - Set ``P`` to ``0``. 5262c9be71SPetre-Ionut Tudor - Set ``NSK`` to ``1``. 5362c9be71SPetre-Ionut Tudor - Set ``M`` to ``0``. 5462c9be71SPetre-Ionut Tudor - Set ``NSH`` to ``0``. 5562c9be71SPetre-Ionut Tudor - Set ``SH`` to ``1``. 5662c9be71SPetre-Ionut Tudor 5762c9be71SPetre-Ionut Tudor- ``PMCNTENSET_EL0``: 5862c9be71SPetre-Ionut Tudor 5962c9be71SPetre-Ionut Tudor - Set ``P[0]`` to ``1``. 6062c9be71SPetre-Ionut Tudor - Set ``C`` to ``1``. 6162c9be71SPetre-Ionut Tudor 6262c9be71SPetre-Ionut Tudor- ``PMCR_EL0``: 6362c9be71SPetre-Ionut Tudor 6462c9be71SPetre-Ionut Tudor - Set ``DP`` to ``0``. 6562c9be71SPetre-Ionut Tudor - Set ``E`` to ``1``. 6662c9be71SPetre-Ionut Tudor 6762c9be71SPetre-Ionut TudorThis configuration instructs ``PMEVCNTR0_EL0`` and ``PMCCNTR_EL0`` to increment 6862c9be71SPetre-Ionut Tudorat Secure EL1, Secure EL2 (if implemented) and EL3. 6962c9be71SPetre-Ionut Tudor 7062c9be71SPetre-Ionut TudorSince the Non-secure world has fine-grained control over where (at which 7162c9be71SPetre-Ionut Tudorexception levels) it instructs counters to increment, obtaining event counts 7262c9be71SPetre-Ionut Tudorwould allow it to carry out side-channel timing attacks against the Secure 7362c9be71SPetre-Ionut Tudorworld. Examples include Spectre, Meltdown, as well as extracting secrets from 7462c9be71SPetre-Ionut Tudorcryptographic algorithms with data-dependent variations in their execution 7562c9be71SPetre-Ionut Tudortime. 7662c9be71SPetre-Ionut Tudor 7762c9be71SPetre-Ionut TudorSecure world mitigation strategies 7862c9be71SPetre-Ionut Tudor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 7962c9be71SPetre-Ionut Tudor 8062c9be71SPetre-Ionut TudorThe ``MDCR_EL3`` register allows EL3 to configure the PMU (among other things). 8162c9be71SPetre-Ionut TudorThe `Arm ARM`_ details all of the bit fields in this register, but for the PMU 8262c9be71SPetre-Ionut Tudorthere are two bits which determine the permissions of the counters: 8362c9be71SPetre-Ionut Tudor 8462c9be71SPetre-Ionut Tudor- ``SPME`` for the programmable counters. 8562c9be71SPetre-Ionut Tudor- ``SCCD`` for the cycle counter. 8662c9be71SPetre-Ionut Tudor 8762c9be71SPetre-Ionut TudorDepending on the implemented features, the Secure world can prohibit counting 8862c9be71SPetre-Ionut Tudorin AArch64 state via the following: 8962c9be71SPetre-Ionut Tudor 9062c9be71SPetre-Ionut Tudor- ARMv8.2-Debug not implemented: 9162c9be71SPetre-Ionut Tudor 9262c9be71SPetre-Ionut Tudor - Prohibit general event counters and the cycle counter: 9362c9be71SPetre-Ionut Tudor ``MDCR_EL3.SPME == 0 && PMCR_EL0.DP == 1 && !ExternalSecureNoninvasiveDebugEnabled()``. 9462c9be71SPetre-Ionut Tudor 9562c9be71SPetre-Ionut Tudor - ``MDCR_EL3.SPME`` resets to ``0``, so by default general events should 9662c9be71SPetre-Ionut Tudor not be counted in the Secure world. 9762c9be71SPetre-Ionut Tudor - The ``PMCR_EL0.DP`` bit therefore needs to be set to ``1`` when EL3 is 9862c9be71SPetre-Ionut Tudor entered and ``PMCR_EL0`` needs to be saved and restored in EL3. 9962c9be71SPetre-Ionut Tudor - ``ExternalSecureNoninvasiveDebugEnabled()`` is an authentication 10062c9be71SPetre-Ionut Tudor interface which is implementation-defined unless ARMv8.4-Debug is 10162c9be71SPetre-Ionut Tudor implemented. The `Arm ARM`_ has detailed information on this topic. 10262c9be71SPetre-Ionut Tudor 10362c9be71SPetre-Ionut Tudor - The only other way is to disable the ``PMCR_EL0.E`` bit upon entering 10462c9be71SPetre-Ionut Tudor EL3, which disables counting altogether. 10562c9be71SPetre-Ionut Tudor 10662c9be71SPetre-Ionut Tudor- ARMv8.2-Debug implemented: 10762c9be71SPetre-Ionut Tudor 10862c9be71SPetre-Ionut Tudor - Prohibit general event counters: ``MDCR_EL3.SPME == 0``. 10962c9be71SPetre-Ionut Tudor - Prohibit cycle counter: ``MDCR_EL3.SPME == 0 && PMCR_EL0.DP == 1``. 11062c9be71SPetre-Ionut Tudor ``PMCR_EL0`` therefore needs to be saved and restored in EL3. 11162c9be71SPetre-Ionut Tudor 11262c9be71SPetre-Ionut Tudor- ARMv8.5-PMU implemented: 11362c9be71SPetre-Ionut Tudor 11462c9be71SPetre-Ionut Tudor - Prohibit general event counters: as in ARMv8.2-Debug. 11562c9be71SPetre-Ionut Tudor - Prohibit cycle counter: ``MDCR_EL3.SCCD == 1`` 11662c9be71SPetre-Ionut Tudor 11762c9be71SPetre-Ionut TudorIn Aarch32 execution state the ``MDCR_EL3`` alias is the ``SDCR`` register, 11862c9be71SPetre-Ionut Tudorwhich has some of the bit fields of ``MDCR_EL3``, most importantly the ``SPME`` 11962c9be71SPetre-Ionut Tudorand ``SCCD`` bits. 12062c9be71SPetre-Ionut Tudor 1212e302371SAmbroise VincentBuild options 1222e302371SAmbroise Vincent------------- 1232e302371SAmbroise Vincent 1242e302371SAmbroise VincentSeveral build options can be used to check for security issues. Refer to the 12543f35ef5SPaul Beesley:ref:`Build Options` for detailed information on these. 1262e302371SAmbroise Vincent 1272e302371SAmbroise Vincent- The ``BRANCH_PROTECTION`` build flag can be used to enable Pointer 1282e302371SAmbroise Vincent Authentication and Branch Target Identification. 1292e302371SAmbroise Vincent 1302e302371SAmbroise Vincent- The ``ENABLE_STACK_PROTECTOR`` build flag can be used to identify buffer 1312e302371SAmbroise Vincent overflows. 1322e302371SAmbroise Vincent 1332e302371SAmbroise Vincent- The ``W`` build flag can be used to enable a number of compiler warning 134291be198SBoyan Karatotev options to detect potentially incorrect code. TF-A is tested with ``W=0`` but 135291be198SBoyan Karatotev it is recommended to develop against ``W=2`` (which will eventually become the 136291be198SBoyan Karatotev default). 1372e302371SAmbroise Vincent 138*85bebe18SSandrine BailleuxAdditional guidelines are provided below for some security-related build 139*85bebe18SSandrine Bailleuxoptions: 140*85bebe18SSandrine Bailleux 141*85bebe18SSandrine Bailleux- The ``ENABLE_CONSOLE_GETC`` build flag should be set to 0 to disable the 142*85bebe18SSandrine Bailleux `getc()` feature, which allows the firmware to read characters from the 143*85bebe18SSandrine Bailleux console. Keeping this feature enabled is considered dangerous from a security 144*85bebe18SSandrine Bailleux point of view because it potentially allows an attacker to inject arbitrary 145*85bebe18SSandrine Bailleux data into the firmware. It should only be enabled on a need basis if there is 146*85bebe18SSandrine Bailleux a use case for it, for example in a testing or factory environment. 147*85bebe18SSandrine Bailleux 14862c9be71SPetre-Ionut Tudor.. rubric:: References 14962c9be71SPetre-Ionut Tudor 15062c9be71SPetre-Ionut Tudor- `Arm ARM`_ 15162c9be71SPetre-Ionut Tudor 15234760951SPaul Beesley-------------- 1532e302371SAmbroise Vincent 154e63f5d12SPaul Beesley*Copyright (c) 2019-2020, Arm Limited. All rights reserved.* 15562c9be71SPetre-Ionut Tudor 15662c9be71SPetre-Ionut Tudor.. _Arm ARM: https://developer.arm.com/docs/ddi0487/latest 157