security-hardening.rst 5.7 KB

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