xref: /rk3399_ARM-atf/docs/security_advisories/security-advisory-tfv-10.rst (revision 81445dd10705799610307be57125ac4bae1a5730)
1d7156d41SSandrine BailleuxAdvisory TFV-10 (CVE-2022-47630)
2d7156d41SSandrine Bailleux================================
3d7156d41SSandrine Bailleux
4d7156d41SSandrine Bailleux+----------------+-------------------------------------------------------------+
5d7156d41SSandrine Bailleux| Title          | Incorrect validation of X.509 certificate extensions can    |
6d7156d41SSandrine Bailleux|                | result in an out-of-bounds read.                            |
7d7156d41SSandrine Bailleux+================+=============================================================+
8d7156d41SSandrine Bailleux| CVE ID         | `CVE-2022-47630`_                                           |
9d7156d41SSandrine Bailleux+----------------+-------------------------------------------------------------+
10d7156d41SSandrine Bailleux| Date           | Reported on 12 Dec 2022                                     |
11d7156d41SSandrine Bailleux+----------------+-------------------------------------------------------------+
12d7156d41SSandrine Bailleux| Versions       | v1.2 to v2.8                                                |
13d7156d41SSandrine Bailleux| Affected       |                                                             |
14d7156d41SSandrine Bailleux+----------------+-------------------------------------------------------------+
15d7156d41SSandrine Bailleux| Configurations | BL1 and BL2 with Trusted Boot enabled with custom,          |
16d7156d41SSandrine Bailleux| Affected       | downstream usages of ``get_ext()`` and/or ``auth_nvctr()``  |
17d7156d41SSandrine Bailleux|                | interfaces. Not exploitable in upstream TF-A code.          |
18d7156d41SSandrine Bailleux+----------------+-------------------------------------------------------------+
19d7156d41SSandrine Bailleux| Impact         | Out-of-bounds read.                                         |
20d7156d41SSandrine Bailleux+----------------+-------------------------------------------------------------+
21d7156d41SSandrine Bailleux| Fix Version    | - `fd37982a19a4a291`_ "fix(auth): forbid junk after         |
22d7156d41SSandrine Bailleux|                |   extensions"                                               |
23d7156d41SSandrine Bailleux|                |                                                             |
24d7156d41SSandrine Bailleux|                | - `72460f50e2437a85`_ "fix(auth): require at least one      |
25d7156d41SSandrine Bailleux|                |   extension to be present"                                  |
26d7156d41SSandrine Bailleux|                |                                                             |
27d7156d41SSandrine Bailleux|                | - `f5c51855d36e399e`_ "fix(auth): properly validate X.509   |
28d7156d41SSandrine Bailleux|                |   extensions"                                               |
29d7156d41SSandrine Bailleux|                |                                                             |
30d7156d41SSandrine Bailleux|                | - `abb8f936fd0ad085`_ "fix(auth): avoid out-of-bounds read  |
31d7156d41SSandrine Bailleux|                |   in auth_nvctr()"                                          |
32d7156d41SSandrine Bailleux|                |                                                             |
33d7156d41SSandrine Bailleux|                | Note that `72460f50e2437a85`_ is not fixing any             |
34d7156d41SSandrine Bailleux|                | vulnerability per se but it is required for                 |
35d7156d41SSandrine Bailleux|                | `f5c51855d36e399e`_ to apply cleanly.                       |
36d7156d41SSandrine Bailleux+----------------+-------------------------------------------------------------+
37d7156d41SSandrine Bailleux| Credit         | Demi Marie Obenour, Invisible Things Lab                    |
38d7156d41SSandrine Bailleux+----------------+-------------------------------------------------------------+
39d7156d41SSandrine Bailleux
40d7156d41SSandrine BailleuxThis security advisory describes a vulnerability in the X.509 parser used to
41d7156d41SSandrine Bailleuxparse boot certificates in TF-A trusted boot: it is possible for a crafted
42d7156d41SSandrine Bailleuxcertificate to cause an out-of-bounds memory read.
43d7156d41SSandrine Bailleux
44d7156d41SSandrine BailleuxNote that upstream platforms are **not** affected by this. Only downstream
45d7156d41SSandrine Bailleuxplatforms may be, if (and only if) the interfaces described below are used in a
46d7156d41SSandrine Bailleuxdifferent context than seen in upstream code. Details of such context is
47d7156d41SSandrine Bailleuxdescribed in the rest of this document.
48d7156d41SSandrine Bailleux
49d7156d41SSandrine BailleuxTo fully understand this security advisory, it is recommended to refer to the
50d7156d41SSandrine Bailleuxfollowing standards documents:
51d7156d41SSandrine Bailleux
52d7156d41SSandrine Bailleux - `RFC 5280`_, *Internet X.509 Public Key Infrastructure Certificate and
53d7156d41SSandrine Bailleux   Certificate Revocation List (CRL) Profile*.
54d7156d41SSandrine Bailleux
55d7156d41SSandrine Bailleux - `ITU-T X.690`_, *ASN.1 encoding rules: Specification of Basic Encoding Rules
56d7156d41SSandrine Bailleux   (BER), Canonical Encoding Rules (CER) and Distinguished Encoding Rules
57d7156d41SSandrine Bailleux   (DER).*
58d7156d41SSandrine Bailleux
59d7156d41SSandrine BailleuxBug 1: Insufficient certificate validation
60d7156d41SSandrine Bailleux------------------------------------------
61d7156d41SSandrine Bailleux
62d7156d41SSandrine BailleuxThe vulnerability lies in the following source file:
63d7156d41SSandrine Bailleux``drivers/auth/mbedtls/mbedtls_x509_parser.c``. By design, ``get_ext()`` does
64d7156d41SSandrine Bailleuxnot check the return value of the various ``mbedtls_*()`` functions, as
65d7156d41SSandrine Bailleux``cert_parse()`` is assumed to have guaranteed that they will always succeed.
66d7156d41SSandrine BailleuxHowever, it passes the end of an extension as the end pointer to these
67d7156d41SSandrine Bailleuxfunctions, whereas ``cert_parse()`` passes the end of the ``TBSCertificate``.
68d7156d41SSandrine BailleuxFurthermore, ``cert_parse()`` does not check that the contents of the extension
69d7156d41SSandrine Bailleuxhave the same length as the extension itself. It also does not check that the
70d7156d41SSandrine Bailleuxextension block extends to the end of the ``TBSCertificate``.
71d7156d41SSandrine Bailleux
72d7156d41SSandrine BailleuxThis is a problem, as ``mbedtls_asn1_get_tag()`` leaves ``*p`` and ``*len``
73d7156d41SSandrine Bailleuxundefined on failure.  In practice, this results in ``get_ext()`` continuing to
74d7156d41SSandrine Bailleuxparse at different offsets than were used (and validated) by ``cert_parse()``,
75d7156d41SSandrine Bailleuxwhich means that the in-bounds guarantee provided by ``cert_parse()`` no longer
76d7156d41SSandrine Bailleuxholds.  The result is that it is possible for ``get_ext()`` to read memory past
77d7156d41SSandrine Bailleuxthe end of the certificate.  This could potentially access memory with dangerous
78d7156d41SSandrine Bailleuxread side effects, or leak microarchitectural state that could theoretically be
79d7156d41SSandrine Bailleuxretrieved through some side-channel attacks as part of a more complex attack.
80d7156d41SSandrine Bailleux
81d7156d41SSandrine BailleuxBug 2: Missing bounds check in ``auth_nvctr()``
82d7156d41SSandrine Bailleux-----------------------------------------------
83d7156d41SSandrine Bailleux``auth_nvctr()`` does not check that the buffer provided is
84d7156d41SSandrine Bailleuxlong enough to hold an ``ASN.1 INTEGER``.  Since ``auth_nvctr()`` will only ever
85d7156d41SSandrine Bailleuxread 6 bytes, it is possible to read up to 6 bytes past the end of the buffer.
86d7156d41SSandrine Bailleux
87d7156d41SSandrine BailleuxExploitability Analysis
88d7156d41SSandrine Bailleux-----------------------
89d7156d41SSandrine Bailleux
90d7156d41SSandrine BailleuxUpstream TF-A Code
91d7156d41SSandrine Bailleux~~~~~~~~~~~~~~~~~~
92d7156d41SSandrine Bailleux
93d7156d41SSandrine BailleuxIn upstream TF-A code, the only caller of ``auth_nvctr()`` takes its input from
94d7156d41SSandrine Bailleux``get_ext()``, which means that the second bug is exploitable, so is the first.
95d7156d41SSandrine BailleuxTherefore, only the first bug need be considered.
96d7156d41SSandrine Bailleux
97d7156d41SSandrine BailleuxAll standard chains of trust provided in TF-A source tree (that is, under
98d7156d41SSandrine Bailleux``drivers/auth/``) require that the certificate's signature has already been
99d7156d41SSandrine Bailleuxvalidated prior to calling ``get_ext()``, or any function that calls ``get_ext()``.
100d7156d41SSandrine BailleuxPlatforms taking their chain of trust from a dynamic configuration file (such as
101*1e8b5354SBoyan Karatotev``fdts/tbbr_cot_descriptors.dts``) are also safe, as signature verification will
102d7156d41SSandrine Bailleuxalways be done prior to any calls to ``get_ext()`` or ``auth_nvctr()`` in this
103d7156d41SSandrine Bailleuxcase, no matter the order of the properties in the file.  Therefore, it is not
104d7156d41SSandrine Bailleuxpossible to exploit this vulnerability pre-authentication in upstream TF-A.
105d7156d41SSandrine Bailleux
106d7156d41SSandrine BailleuxFurthermore, the data read through ``get_ext()`` only
107d7156d41SSandrine Bailleuxever gets used by the authentication framework (``drivers/auth/auth_mod.c``),
108d7156d41SSandrine Bailleuxwhich greatly reduces the range of inputs it will ever receive and thus the
109d7156d41SSandrine Bailleuximpact this has. Specifically, the authentication framework uses ``get_ext()``
110d7156d41SSandrine Bailleuxin three cases:
111d7156d41SSandrine Bailleux
112d7156d41SSandrine Bailleux 1. Retrieving a hash from an X.509 certificate to check the integrity of a
113d7156d41SSandrine Bailleux    child certificate (see ``auth_hash()``).
114d7156d41SSandrine Bailleux
115d7156d41SSandrine Bailleux 2. Retrieving the signature details from an X.509 certificate to check its
116d7156d41SSandrine Bailleux    authenticity and integrity (see ``auth_signature()``).
117d7156d41SSandrine Bailleux
118d7156d41SSandrine Bailleux 3. Retrieving the security counter value from an X.509 certificate to protect
119d7156d41SSandrine Bailleux    it from unauthorized rollback to a previous version (see ``auth_nvctr()``).
120d7156d41SSandrine Bailleux
121d7156d41SSandrine BailleuxNone of these uses authentication framework write to the out-of-bounds memory,
122d7156d41SSandrine Bailleuxso no memory corruption is possible.
123d7156d41SSandrine Bailleux
124d7156d41SSandrine BailleuxIn summary, there are 2 separate issues - one in ``get_ext()`` and another one
125d7156d41SSandrine Bailleuxin ``auth_nvctr()`` - but neither of these can be exploited in the context of
126d7156d41SSandrine BailleuxTF-A upstream code.
127d7156d41SSandrine Bailleux
128d7156d41SSandrine BailleuxOnly in the following 2 cases do we expect this vulnerability to be triggerable
129d7156d41SSandrine Bailleuxprior to authentication:
130d7156d41SSandrine Bailleux
131d7156d41SSandrine Bailleux - The platform uses a custom chain of trust which uses the non-volatile counter
132d7156d41SSandrine Bailleux   authentication method (``AUTH_METHOD_NV_CTR``) before the cryptographic
133d7156d41SSandrine Bailleux   authentication method (``AUTH_METHOD_SIG``).
134d7156d41SSandrine Bailleux
135d7156d41SSandrine Bailleux - The chain of trust uses a custom authentication method that calls
136d7156d41SSandrine Bailleux   ``get_ext()`` before cryptographic authentication.
137d7156d41SSandrine Bailleux
138d7156d41SSandrine BailleuxCustom Image Parsers
139d7156d41SSandrine Bailleux~~~~~~~~~~~~~~~~~~~~
140d7156d41SSandrine Bailleux
141d7156d41SSandrine BailleuxIf the platform uses a custom image parser instead of the certificate parser,
142d7156d41SSandrine Bailleuxthe bug in the certificate parser is obviously not relevant.  The bug in
143d7156d41SSandrine Bailleux``auth_nvctr()`` *may* be relevant, but only if the returned data is:
144d7156d41SSandrine Bailleux
145d7156d41SSandrine Bailleux- Taken from an untrusted source (meaning that it is read prior to
146d7156d41SSandrine Bailleux  authentication).
147d7156d41SSandrine Bailleux
148d7156d41SSandrine Bailleux- Not already checked to be a primitively-encoded ASN.1 tag.
149d7156d41SSandrine Bailleux
150d7156d41SSandrine BailleuxIn particular, if the custom image parser implementation wraps a 32-bit integer
151d7156d41SSandrine Bailleuxin an ASN.1 ``INTEGER``, it is not affected.
152d7156d41SSandrine Bailleux
153d7156d41SSandrine Bailleux.. _CVE-2022-47630: http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2022-47630
154d7156d41SSandrine Bailleux.. _fd37982a19a4a291: https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git/commit/?id=fd37982a19a4a291
155d7156d41SSandrine Bailleux.. _72460f50e2437a85: https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git/commit/?id=72460f50e2437a85
156d7156d41SSandrine Bailleux.. _f5c51855d36e399e: https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git/commit/?id=f5c51855d36e399e
157d7156d41SSandrine Bailleux.. _abb8f936fd0ad085: https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git/commit/?id=abb8f936fd0ad085
158d7156d41SSandrine Bailleux.. _RFC 5280: https://www.ietf.org/rfc/rfc5280.txt
159d7156d41SSandrine Bailleux.. _ITU-T X.690: https://www.itu.int/ITU-T/studygroups/com10/languages/X.690_1297.pdf
160