1*4fe91230SJoel Hutton+----------------+-------------------------------------------------------------+ 2*4fe91230SJoel Hutton| Title | Malformed Firmware Update SMC can result in copy or | 3*4fe91230SJoel Hutton| | authentication of unexpected data in secure memory in | 4*4fe91230SJoel Hutton| | AArch32 state | 5*4fe91230SJoel Hutton+================+=============================================================+ 6*4fe91230SJoel Hutton| CVE ID | CVE-2017-9607 | 7*4fe91230SJoel Hutton+----------------+-------------------------------------------------------------+ 8*4fe91230SJoel Hutton| Date | 20 Jun 2017 | 9*4fe91230SJoel Hutton+----------------+-------------------------------------------------------------+ 10*4fe91230SJoel Hutton| Versions | None (only between 22 May 2017 and 14 June 2017) | 11*4fe91230SJoel Hutton| Affected | | 12*4fe91230SJoel Hutton+----------------+-------------------------------------------------------------+ 13*4fe91230SJoel Hutton| Configurations | Platforms that use AArch32 BL1 plus untrusted normal world | 14*4fe91230SJoel Hutton| Affected | firmware update code executing before BL31 | 15*4fe91230SJoel Hutton+----------------+-------------------------------------------------------------+ 16*4fe91230SJoel Hutton| Impact | Copy or authentication of unexpected data in the secure | 17*4fe91230SJoel Hutton| | memory | 18*4fe91230SJoel Hutton+----------------+-------------------------------------------------------------+ 19*4fe91230SJoel Hutton| Fix Version | `Pull Request #979`_ (merged on 14 June 2017) | 20*4fe91230SJoel Hutton+----------------+-------------------------------------------------------------+ 21*4fe91230SJoel Hutton| Credit | ARM | 22*4fe91230SJoel Hutton+----------------+-------------------------------------------------------------+ 23*4fe91230SJoel Hutton 24*4fe91230SJoel HuttonThe ``include/lib/utils_def.h`` header file provides the 25*4fe91230SJoel Hutton``check_uptr_overflow()`` macro, which aims at detecting arithmetic overflows 26*4fe91230SJoel Huttonthat may occur when computing the sum of a base pointer and an offset. This 27*4fe91230SJoel Huttonmacro evaluates to 1 if the sum of the given base pointer and offset would 28*4fe91230SJoel Huttonresult in a value large enough to wrap around, which may lead to unpredictable 29*4fe91230SJoel Huttonbehaviour. 30*4fe91230SJoel Hutton 31*4fe91230SJoel HuttonThe macro code is at line 52, referring to the version of the code as of `commit 32*4fe91230SJoel Huttonc396b73`_: 33*4fe91230SJoel Hutton 34*4fe91230SJoel Hutton.. code:: c 35*4fe91230SJoel Hutton 36*4fe91230SJoel Hutton /* 37*4fe91230SJoel Hutton * Evaluates to 1 if (ptr + inc) overflows, 0 otherwise. 38*4fe91230SJoel Hutton * Both arguments must be unsigned pointer values (i.e. uintptr_t). 39*4fe91230SJoel Hutton */ 40*4fe91230SJoel Hutton #define check_uptr_overflow(ptr, inc) \ 41*4fe91230SJoel Hutton (((ptr) > UINTPTR_MAX - (inc)) ? 1 : 0) 42*4fe91230SJoel Hutton 43*4fe91230SJoel HuttonThis macro does not work correctly for AArch32 images. It fails to detect 44*4fe91230SJoel Huttonoverflows when the sum of its two parameters fall into the ``[2^32, 2^64 - 1]`` 45*4fe91230SJoel Huttonrange. Therefore, any AArch32 code relying on this macro to detect such integer 46*4fe91230SJoel Huttonoverflows is actually not protected. 47*4fe91230SJoel Hutton 48*4fe91230SJoel HuttonThe buggy code has been present in ARM Trusted Firmware (TF) since `Pull Request 49*4fe91230SJoel Hutton#678`_ was merged (on 18 August 2016). However, the upstream code was not 50*4fe91230SJoel Huttonvulnerable until `Pull Request #939`_ was merged (on 22 May 2017), which 51*4fe91230SJoel Huttonintroduced AArch32 support for the Trusted Board Boot (TBB) feature. Before 52*4fe91230SJoel Huttonthen, the ``check_uptr_overflow()`` macro was not used in AArch32 code. 53*4fe91230SJoel Hutton 54*4fe91230SJoel HuttonThe vulnerability resides in the BL1 FWU SMC handling code and it may be 55*4fe91230SJoel Huttonexploited when *all* the following conditions apply: 56*4fe91230SJoel Hutton 57*4fe91230SJoel Hutton- Platform code uses TF BL1 with the ``TRUSTED_BOARD_BOOT`` build option. 58*4fe91230SJoel Hutton 59*4fe91230SJoel Hutton- Platform code uses the Firmware Update (FWU) code provided in 60*4fe91230SJoel Hutton ``bl1/bl1_fwu.c``, which is part of the TBB support. 61*4fe91230SJoel Hutton 62*4fe91230SJoel Hutton- TF BL1 is compiled with the ``ARCH=aarch32`` build option. 63*4fe91230SJoel Hutton 64*4fe91230SJoel HuttonIn this context, the AArch32 BL1 image might fail to detect potential integer 65*4fe91230SJoel Huttonoverflows in the input validation checks while handling the 66*4fe91230SJoel Hutton``FWU_SMC_IMAGE_COPY`` and ``FWU_SMC_IMAGE_AUTH`` SMCs. 67*4fe91230SJoel Hutton 68*4fe91230SJoel HuttonThe ``FWU_SMC_IMAGE_COPY`` SMC handler is designed to copy an image into secure 69*4fe91230SJoel Huttonmemory for subsequent authentication. This is implemented by the 70*4fe91230SJoel Hutton``bl1_fwu_image_copy()`` function, which has the following function prototype: 71*4fe91230SJoel Hutton 72*4fe91230SJoel Hutton.. code:: c 73*4fe91230SJoel Hutton 74*4fe91230SJoel Hutton static int bl1_fwu_image_copy(unsigned int image_id, 75*4fe91230SJoel Hutton uintptr_t image_src, 76*4fe91230SJoel Hutton unsigned int block_size, 77*4fe91230SJoel Hutton unsigned int image_size, 78*4fe91230SJoel Hutton unsigned int flags) 79*4fe91230SJoel Hutton 80*4fe91230SJoel Hutton``image_src`` is an SMC argument and therefore potentially controllable by an 81*4fe91230SJoel Huttonattacker. A very large 32-bit value, for example ``2^32 -1``, may result in the 82*4fe91230SJoel Huttonsum of ``image_src`` and ``block_size`` overflowing a 32-bit type, which 83*4fe91230SJoel Hutton``check_uptr_overflow()`` will fail to detect. Depending on its implementation, 84*4fe91230SJoel Huttonthe platform-specific function ``bl1_plat_mem_check()`` might get defeated by 85*4fe91230SJoel Huttonthese unsanitized values and allow the following memory copy operation, that 86*4fe91230SJoel Huttonwould wrap around. This may allow an attacker to copy unexpected data into 87*4fe91230SJoel Huttonsecure memory if the memory is mapped in BL1's address space, or cause a fatal 88*4fe91230SJoel Huttonexception if it's not. 89*4fe91230SJoel Hutton 90*4fe91230SJoel HuttonThe ``FWU_SMC_IMAGE_AUTH`` SMC handler is designed to authenticate an image 91*4fe91230SJoel Huttonresident in secure memory. This is implemented by the ``bl1_fwu_image_auth()`` 92*4fe91230SJoel Huttonfunction, which has the following function prototype: 93*4fe91230SJoel Hutton 94*4fe91230SJoel Hutton.. code:: c 95*4fe91230SJoel Hutton 96*4fe91230SJoel Hutton static int bl1_fwu_image_auth(unsigned int image_id, 97*4fe91230SJoel Hutton uintptr_t image_src, 98*4fe91230SJoel Hutton unsigned int image_size, 99*4fe91230SJoel Hutton unsigned int flags) 100*4fe91230SJoel Hutton 101*4fe91230SJoel HuttonSimilarly, if an attacker has control over the ``image_src`` or ``image_size`` 102*4fe91230SJoel Huttonarguments through the SMC interface and injects high values whose sum overflows, 103*4fe91230SJoel Huttonthey might defeat the ``bl1_plat_mem_check()`` function and make the 104*4fe91230SJoel Huttonauthentication module read data outside of what's normally allowed by the 105*4fe91230SJoel Huttonplatform code or crash the platform. 106*4fe91230SJoel Hutton 107*4fe91230SJoel HuttonNote that in both cases, a separate vulnerability is required to leverage this 108*4fe91230SJoel Huttonvulnerability; for example a way to get the system to change its behaviour based 109*4fe91230SJoel Huttonon the unexpected secure memory accesses. Moreover, the normal world FWU code 110*4fe91230SJoel Huttonwould need to be compromised in order to send a malformed FWU SMC that triggers 111*4fe91230SJoel Huttonan integer overflow. 112*4fe91230SJoel Hutton 113*4fe91230SJoel HuttonThe vulnerability is known to affect all ARM standard platforms when enabling 114*4fe91230SJoel Huttonthe ``TRUSTED_BOARD_BOOT`` and ``ARCH=aarch32`` build options. Other platforms 115*4fe91230SJoel Huttonmay also be affected if they fulfil the above conditions. 116*4fe91230SJoel Hutton 117*4fe91230SJoel Hutton.. _commit c396b73: https://github.com/ARM-software/arm-trusted-firmware/commit/c396b73 118*4fe91230SJoel Hutton.. _Pull Request #678: https://github.com/ARM-software/arm-trusted-firmware/pull/678 119*4fe91230SJoel Hutton.. _Pull Request #939: https://github.com/ARM-software/arm-trusted-firmware/pull/939 120*4fe91230SJoel Hutton.. _Pull Request #979: https://github.com/ARM-software/arm-trusted-firmware/pull/979 121