Tamas Ban | 1f951d5 | 2019-11-15 16:38:34 +0000 | [diff] [blame] | 1 | ####################################### |
| 2 | Rollback protection in TF-M secure boot |
| 3 | ####################################### |
| 4 | |
| 5 | :Author: Tamas Ban |
| 6 | :Organization: Arm Limited |
| 7 | :Contact: Tamas Ban <tamas.ban@arm.com> |
| 8 | :Status: Accepted |
| 9 | |
| 10 | Anti-rollback protection |
| 11 | ======================== |
| 12 | The goal of anti-rollback protection is to prevent downgrading of the device to |
| 13 | an older version of its software, which has been deprecated due to security |
| 14 | concerns. |
| 15 | |
| 16 | Elements of software upgrade |
| 17 | ============================ |
| 18 | During a software upgrade the following entities are involved: |
| 19 | |
| 20 | - Boot loader |
| 21 | - Manifest data: Metadata of the software image: size, version, hash, |
| 22 | signature, etc. |
| 23 | - Software image: binary data, elf, etc. |
| 24 | |
| 25 | Validation of new image |
| 26 | ======================= |
| 27 | Boot loader is responsible to authenticate the new image according to the |
| 28 | required policies and decide whether the new image is fulfilling all the |
| 29 | requirements. Boot loader verifies the image integrity (hash calculation) and |
| 30 | the origination (signature validation), and might verify other requirements as |
| 31 | well. If the new image is successfully authenticated then the boot loader is in |
| 32 | charge to do the necessary steps (load to execute address, etc.) to enable the |
| 33 | new image to be executed. During the validation process the image and the |
| 34 | manifest data is checked. |
| 35 | |
| 36 | Manifest format in MCUBoot |
| 37 | ========================== |
| 38 | MCUBoot has a custom manifest format, which is composed of two parts: |
| 39 | |
| 40 | - Image header: Prepended to the beginning of the image. |
| 41 | It is integrity protected: |
| 42 | - TLV section: Appended to the end of the image. It is not integrity protected: |
| 43 | |
| 44 | - Hash of public key, hash of image, signature of image |
| 45 | |
| 46 | .. code-block:: c |
| 47 | |
| 48 | struct image_header { |
| 49 | uint32_t ih_magic; |
| 50 | uint32_t ih_load_addr; |
| 51 | uint16_t ih_hdr_size; |
| 52 | uint16_t _pad1; |
| 53 | uint32_t ih_img_size; |
| 54 | uint32_t ih_flags; |
| 55 | struct image_version ih_ver; |
| 56 | uint32_t _pad2; |
| 57 | }; |
| 58 | |
| 59 | Security counter |
| 60 | ================ |
| 61 | The aim of a security counter is to have an independent (from the image version) |
| 62 | counter in the image manifest to ensure anti-rollback protection. During |
| 63 | software release the value of this counter must be increased if a security flaw |
| 64 | was fixed in the current version. Later when this image is installed on the |
| 65 | device then it is not allowed to go back to earlier versions. It is beneficial |
| 66 | to handle this counter independently from image main version number: |
| 67 | |
| 68 | - It does not need to increase with each software release |
| 69 | - It makes it possible to do software downgrade to some extent: if the security |
| 70 | counter has the same value in the older image then it is accepted. |
| 71 | - If the boot loader verifies multiple images then these can be handled |
| 72 | independently. |
| 73 | |
| 74 | However, as an alternative solution the image version number also could serve |
| 75 | as the security counter of the image. Even the version number itself could be |
| 76 | checked during the anti-rollback verification or the value of the security |
| 77 | counter could be derived from the image main version. It is the responsibility |
| 78 | of the software issuer to define which policy to apply. |
| 79 | |
| 80 | Implementation of security counter |
| 81 | ================================== |
| 82 | The value of the security counter is a security critical data. Any change in |
| 83 | its value has a security implication. Therefore it must be in the integrity |
| 84 | protected part of the image manifest. Because the image header is almost fully |
Edison Ai | f7990c8 | 2020-07-16 18:31:48 +0800 | [diff] [blame] | 85 | utilized (few unused fields) and the change of image header structure could |
Tamas Ban | 1f951d5 | 2019-11-15 16:38:34 +0000 | [diff] [blame] | 86 | lead to compatibility issues between boot loader and runtime software, it is |
| 87 | proposed to extend the integrity protection to some part of the TLV section. |
| 88 | One of the unused fields in the image header can be used to store the size of |
| 89 | the integrity protected area of the TLV section. This is necessary to know how |
| 90 | to calculate properly the image hash and signature. With this extension of the |
| 91 | integrity protected area other attributes that require integrity protection |
| 92 | can easily be added to the image manifest. |
| 93 | |
| 94 | Trusted non-volatile (NV) counters |
| 95 | ================================== |
| 96 | The Trusted Base System Architecture (TBSA-M) defines non-volatile (NV) counters |
| 97 | or version counters as a counter with the following properties: |
| 98 | |
| 99 | - It must only be possible to increment a version counter through a Trusted |
| 100 | access. |
| 101 | - It must only be possible to increment a version counter. It must not be |
| 102 | possible to decrement it. |
| 103 | - When a version counter reaches its maximum value, it must not roll over, |
| 104 | and no further changes must be possible. |
| 105 | - A version counter must be non-volatile, and the stored value must survive |
| 106 | a power down period up to the lifetime of the device. |
| 107 | |
| 108 | Trusted non-volatile counters can be used to store the value of security |
| 109 | counters per updatable software image. Ideally all independently updatable |
| 110 | software images should have a separate security counter. In current TF-M |
| 111 | implementation the BL2 is not updatable and the secure and non-secure images |
| 112 | are updated together as a single binary. Therefore, one counter is enough to |
| 113 | implement rollback protection. In future the secure and non-secure binaries |
| 114 | will be handled independently; at that time the introduction of a second |
| 115 | counter will be necessary. |
| 116 | |
| 117 | Currently the NV counters can be manipulated through the interface described |
| 118 | in ``tfm_plat_nv_counters.h``. |
| 119 | |
| 120 | NV counters and anti-rollback protection |
| 121 | ======================================== |
| 122 | Trusted non-volatile counters might not be supported by a hardware platform. |
| 123 | In this case anti-rollback protection might still be feasible. |
| 124 | |
| 125 | The device threat model needs to consider the following aspects: |
| 126 | |
| 127 | - What is the trust level of the boot loader towards the active software |
| 128 | |
| 129 | - If the boot loader cannot protect the anti-rollback mechanism from the |
| 130 | secure image then the following threat is unmitigated: The content of the |
| 131 | memory area which holds the active image could be replaced with a valid but |
| 132 | an older version of the software with software only attack, i.e.: remote |
| 133 | code execution. |
| 134 | - If the boot loader does not trust the loaded image at all then security |
| 135 | counter must have a copy in NV counter area. |
| 136 | |
| 137 | - Another aspect to consider is where the active image is stored |
| 138 | |
| 139 | - Trusted memory: i.e. on-chip flash (eFlash). The threat that a malicious |
| 140 | actor can modify the content of trusted memory with HW attack is out of |
| 141 | scope for the current implementation. It is assumed that if an active image |
| 142 | and related manifest data is stored in trusted memory then the included |
| 143 | security counter cannot be compromised. |
| 144 | - Untrusted memory: i.e. external (off-chip) storage, where malicious actor |
| 145 | can physically access it so it is possible to modify its content, therefore |
| 146 | the value of included security counter is unreliable. |
| 147 | |
| 148 | If the active image is stored in untrusted storage and it is feasible to modify |
| 149 | its content (i.e. replace the whole image to an older version including |
| 150 | corresponding manifest) then the value of security counter must be copied to |
| 151 | the NV counter area. During software validation the boot loader must compare |
| 152 | the new image's security counters against the security counter stored in |
| 153 | non-volatile counters. |
| 154 | |
| 155 | If the active image is stored in trusted memory and boot loader trusts in the |
| 156 | active software then it is not mandatory to store the security counter to |
| 157 | non-volatile counter area. During software validation the boot loader is |
| 158 | allowed to compare the new image's security counters against active image's |
| 159 | security counter. |
| 160 | |
| 161 | The evaluation of trusted and untrusted memory must be done per target platform |
| 162 | during threat modelling. |
| 163 | |
| 164 | For the most robust implementation the following principles should be applied: |
| 165 | |
| 166 | - Always use NV counters for storing security counter value if supported by |
| 167 | the hardware. |
| 168 | - Each software stage must not be able to decrease their corresponding NV |
| 169 | counter. |
| 170 | |
| 171 | Boot flow with anti-rollback protection |
| 172 | ======================================= |
| 173 | During software upgrade as part of the image validation process the new and |
| 174 | active image security counters must be compared. The new image can be accepted |
| 175 | if its security counter has a greater or equal value than the active image |
| 176 | security counter. From where to extract the active image security counter it |
| 177 | can be platform dependent. It might read out directly from active image |
| 178 | manifest data (only if it is in trusted memory) or the corresponding |
| 179 | non-volatile counter is read. |
| 180 | |
| 181 | If non-volatile counters are used to save security counters then their value |
| 182 | must be updated: |
| 183 | |
| 184 | - If the boot loader does not support to revert previous images (just |
| 185 | overwrites the previously active image with the new one) in case of faulty |
| 186 | update then the non-volatile counter can be updated to be equal with the |
| 187 | new image security counter after successful authentication of the new image. |
| 188 | - If revert is supported then non-volatile counter can be updated just after |
| 189 | a test run of the new software when its health check is done. Just in case |
| 190 | of successful health check can the counter updated to avoid the prevention |
| 191 | of the revert. This might require a secondary restart of the device. |
| 192 | |
| 193 | Tool support |
| 194 | ============ |
| 195 | There is a Python script, ``imgtool.py`` which is used to prepare the new image |
| 196 | for upgrade (add header, sign the image, append TLV section). This script must |
| 197 | be modified to get an additional command line argument which serves as security |
| 198 | counter. The security counter must be included in the integrity protected part |
| 199 | of the TLV section. |
| 200 | |
| 201 | -------------- |
| 202 | |
Edison Ai | f7990c8 | 2020-07-16 18:31:48 +0800 | [diff] [blame] | 203 | *Copyright (c) 2019-2020, Arm Limited. All rights reserved.* |