xref: /rk3399_ARM-atf/docs/process/security-hardening.rst (revision 62c9be71d6b6356e021e3640000e4e30f4cbb3e5)
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
28*62c9be71SPetre-Ionut TudorPreventing Secure-world timing information leakage via PMU counters
29*62c9be71SPetre-Ionut Tudor^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30*62c9be71SPetre-Ionut Tudor
31*62c9be71SPetre-Ionut TudorThe Secure world needs to implement some defenses to prevent the Non-secure
32*62c9be71SPetre-Ionut Tudorworld from making it leak timing information. In general, higher privilege
33*62c9be71SPetre-Ionut Tudorlevels must defend from those below when the PMU is treated as an attack
34*62c9be71SPetre-Ionut Tudorvector.
35*62c9be71SPetre-Ionut Tudor
36*62c9be71SPetre-Ionut TudorRefer to the :ref:`Performance Monitoring Unit` guide for detailed information
37*62c9be71SPetre-Ionut Tudoron the PMU registers.
38*62c9be71SPetre-Ionut Tudor
39*62c9be71SPetre-Ionut TudorTiming leakage attacks from the Non-secure world
40*62c9be71SPetre-Ionut Tudor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
41*62c9be71SPetre-Ionut Tudor
42*62c9be71SPetre-Ionut TudorSince the Non-secure world has access to the ``PMCR`` register, it can
43*62c9be71SPetre-Ionut Tudorconfigure the PMU to increment counters at any exception level and in both
44*62c9be71SPetre-Ionut TudorSecure and Non-secure state. Thus, it attempts to leak timing information from
45*62c9be71SPetre-Ionut Tudorthe Secure world.
46*62c9be71SPetre-Ionut Tudor
47*62c9be71SPetre-Ionut TudorShown below is an example of such a configuration:
48*62c9be71SPetre-Ionut Tudor
49*62c9be71SPetre-Ionut Tudor-  ``PMEVTYPER0_EL0`` and ``PMCCFILTR_EL0``:
50*62c9be71SPetre-Ionut Tudor
51*62c9be71SPetre-Ionut Tudor   -  Set ``P`` to ``0``.
52*62c9be71SPetre-Ionut Tudor   -  Set ``NSK`` to ``1``.
53*62c9be71SPetre-Ionut Tudor   -  Set ``M`` to ``0``.
54*62c9be71SPetre-Ionut Tudor   -  Set ``NSH`` to ``0``.
55*62c9be71SPetre-Ionut Tudor   -  Set ``SH`` to ``1``.
56*62c9be71SPetre-Ionut Tudor
57*62c9be71SPetre-Ionut Tudor-  ``PMCNTENSET_EL0``:
58*62c9be71SPetre-Ionut Tudor
59*62c9be71SPetre-Ionut Tudor   -  Set ``P[0]`` to ``1``.
60*62c9be71SPetre-Ionut Tudor   -  Set ``C`` to ``1``.
61*62c9be71SPetre-Ionut Tudor
62*62c9be71SPetre-Ionut Tudor-  ``PMCR_EL0``:
63*62c9be71SPetre-Ionut Tudor
64*62c9be71SPetre-Ionut Tudor   -  Set ``DP`` to ``0``.
65*62c9be71SPetre-Ionut Tudor   -  Set ``E`` to ``1``.
66*62c9be71SPetre-Ionut Tudor
67*62c9be71SPetre-Ionut TudorThis configuration instructs ``PMEVCNTR0_EL0`` and ``PMCCNTR_EL0`` to increment
68*62c9be71SPetre-Ionut Tudorat Secure EL1, Secure EL2 (if implemented) and EL3.
69*62c9be71SPetre-Ionut Tudor
70*62c9be71SPetre-Ionut TudorSince the Non-secure world has fine-grained control over where (at which
71*62c9be71SPetre-Ionut Tudorexception levels) it instructs counters to increment, obtaining event counts
72*62c9be71SPetre-Ionut Tudorwould allow it to carry out side-channel timing attacks against the Secure
73*62c9be71SPetre-Ionut Tudorworld. Examples include Spectre, Meltdown, as well as extracting secrets from
74*62c9be71SPetre-Ionut Tudorcryptographic algorithms with data-dependent variations in their execution
75*62c9be71SPetre-Ionut Tudortime.
76*62c9be71SPetre-Ionut Tudor
77*62c9be71SPetre-Ionut TudorSecure world mitigation strategies
78*62c9be71SPetre-Ionut Tudor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
79*62c9be71SPetre-Ionut Tudor
80*62c9be71SPetre-Ionut TudorThe ``MDCR_EL3`` register allows EL3 to configure the PMU (among other things).
81*62c9be71SPetre-Ionut TudorThe `Arm ARM`_ details all of the bit fields in this register, but for the PMU
82*62c9be71SPetre-Ionut Tudorthere are two bits which determine the permissions of the counters:
83*62c9be71SPetre-Ionut Tudor
84*62c9be71SPetre-Ionut Tudor-  ``SPME`` for the programmable counters.
85*62c9be71SPetre-Ionut Tudor-  ``SCCD`` for the cycle counter.
86*62c9be71SPetre-Ionut Tudor
87*62c9be71SPetre-Ionut TudorDepending on the implemented features, the Secure world can prohibit counting
88*62c9be71SPetre-Ionut Tudorin AArch64 state via the following:
89*62c9be71SPetre-Ionut Tudor
90*62c9be71SPetre-Ionut Tudor-  ARMv8.2-Debug not implemented:
91*62c9be71SPetre-Ionut Tudor
92*62c9be71SPetre-Ionut Tudor   -  Prohibit general event counters and the cycle counter:
93*62c9be71SPetre-Ionut Tudor      ``MDCR_EL3.SPME == 0 && PMCR_EL0.DP == 1 && !ExternalSecureNoninvasiveDebugEnabled()``.
94*62c9be71SPetre-Ionut Tudor
95*62c9be71SPetre-Ionut Tudor      -  ``MDCR_EL3.SPME`` resets to ``0``, so by default general events should
96*62c9be71SPetre-Ionut Tudor         not be counted in the Secure world.
97*62c9be71SPetre-Ionut Tudor      -  The ``PMCR_EL0.DP`` bit therefore needs to be set to ``1`` when EL3 is
98*62c9be71SPetre-Ionut Tudor         entered and ``PMCR_EL0`` needs to be saved and restored in EL3.
99*62c9be71SPetre-Ionut Tudor      -  ``ExternalSecureNoninvasiveDebugEnabled()`` is an authentication
100*62c9be71SPetre-Ionut Tudor         interface which is implementation-defined unless ARMv8.4-Debug is
101*62c9be71SPetre-Ionut Tudor         implemented. The `Arm ARM`_ has detailed information on this topic.
102*62c9be71SPetre-Ionut Tudor
103*62c9be71SPetre-Ionut Tudor   -  The only other way is to disable the ``PMCR_EL0.E`` bit upon entering
104*62c9be71SPetre-Ionut Tudor      EL3, which disables counting altogether.
105*62c9be71SPetre-Ionut Tudor
106*62c9be71SPetre-Ionut Tudor-  ARMv8.2-Debug implemented:
107*62c9be71SPetre-Ionut Tudor
108*62c9be71SPetre-Ionut Tudor   -  Prohibit general event counters: ``MDCR_EL3.SPME == 0``.
109*62c9be71SPetre-Ionut Tudor   -  Prohibit cycle counter: ``MDCR_EL3.SPME == 0 && PMCR_EL0.DP == 1``.
110*62c9be71SPetre-Ionut Tudor      ``PMCR_EL0`` therefore needs to be saved and restored in EL3.
111*62c9be71SPetre-Ionut Tudor
112*62c9be71SPetre-Ionut Tudor-  ARMv8.5-PMU implemented:
113*62c9be71SPetre-Ionut Tudor
114*62c9be71SPetre-Ionut Tudor   -  Prohibit general event counters: as in ARMv8.2-Debug.
115*62c9be71SPetre-Ionut Tudor   -  Prohibit cycle counter: ``MDCR_EL3.SCCD == 1``
116*62c9be71SPetre-Ionut Tudor
117*62c9be71SPetre-Ionut TudorIn Aarch32 execution state the ``MDCR_EL3`` alias is the ``SDCR`` register,
118*62c9be71SPetre-Ionut Tudorwhich has some of the bit fields of ``MDCR_EL3``, most importantly the ``SPME``
119*62c9be71SPetre-Ionut Tudorand ``SCCD`` bits.
120*62c9be71SPetre-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
1342e302371SAmbroise Vincent  options to detect potentially incorrect code.
1352e302371SAmbroise Vincent
1362e302371SAmbroise Vincent  - W=0 (default value)
1372e302371SAmbroise Vincent
1382e302371SAmbroise Vincent    The ``Wunused`` with ``Wno-unused-parameter``, ``Wdisabled-optimization``
1392e302371SAmbroise Vincent    and ``Wvla`` flags are enabled.
1402e302371SAmbroise Vincent
1412e302371SAmbroise Vincent    The ``Wunused-but-set-variable``, ``Wmaybe-uninitialized`` and
1422e302371SAmbroise Vincent    ``Wpacked-bitfield-compat`` are GCC specific flags that are also enabled.
1432e302371SAmbroise Vincent
1442e302371SAmbroise Vincent  - W=1
1452e302371SAmbroise Vincent
14611a96e0eSJustin Chadwell    Adds ``Wextra``, ``Wmissing-format-attribute``, ``Wmissing-prototypes``,
14711a96e0eSJustin Chadwell    ``Wold-style-definition`` and ``Wunused-const-variable``.
1482e302371SAmbroise Vincent
1492e302371SAmbroise Vincent  - W=2
1502e302371SAmbroise Vincent
1512e302371SAmbroise Vincent    Adds ``Waggregate-return``, ``Wcast-align``, ``Wnested-externs``,
152b8baa934SJustin Chadwell    ``Wshadow``, ``Wlogical-op``.
1532e302371SAmbroise Vincent
1542e302371SAmbroise Vincent  - W=3
1552e302371SAmbroise Vincent
1562e302371SAmbroise Vincent    Adds ``Wbad-function-cast``, ``Wcast-qual``, ``Wconversion``, ``Wpacked``,
15711a96e0eSJustin Chadwell    ``Wpointer-arith``, ``Wredundant-decls`` and
1582e302371SAmbroise Vincent    ``Wswitch-default``.
1592e302371SAmbroise Vincent
1602e302371SAmbroise Vincent  Refer to the GCC or Clang documentation for more information on the individual
1612e302371SAmbroise Vincent  options: https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html and
1622e302371SAmbroise Vincent  https://clang.llvm.org/docs/DiagnosticsReference.html.
1632e302371SAmbroise Vincent
1642e302371SAmbroise Vincent  NB: The ``Werror`` flag is enabled by default in TF-A and can be disabled by
1652e302371SAmbroise Vincent  setting the ``E`` build flag to 0.
1662e302371SAmbroise Vincent
167*62c9be71SPetre-Ionut Tudor.. rubric:: References
168*62c9be71SPetre-Ionut Tudor
169*62c9be71SPetre-Ionut Tudor-  `Arm ARM`_
170*62c9be71SPetre-Ionut Tudor
17134760951SPaul Beesley--------------
1722e302371SAmbroise Vincent
173e63f5d12SPaul Beesley*Copyright (c) 2019-2020, Arm Limited. All rights reserved.*
174*62c9be71SPetre-Ionut Tudor
175*62c9be71SPetre-Ionut Tudor.. _Arm ARM: https://developer.arm.com/docs/ddi0487/latest
176