| 1 |
Pointer Authentication |
| 2 |
====================== |
| 3 |
|
| 4 |
.. contents:: |
| 5 |
:local: |
| 6 |
|
| 7 |
Introduction |
| 8 |
------------ |
| 9 |
|
| 10 |
Pointer authentication is a technology which offers strong probabilistic |
| 11 |
protection against exploiting a broad class of memory bugs to take control of |
| 12 |
program execution. When adopted consistently in a language ABI, it provides |
| 13 |
a form of relatively fine-grained control flow integrity (CFI) check that |
| 14 |
resists both return-oriented programming (ROP) and jump-oriented programming |
| 15 |
(JOP) attacks. |
| 16 |
|
| 17 |
While pointer authentication can be implemented purely in software, direct |
| 18 |
hardware support (e.g. as provided by Armv8.3 PAuth) can dramatically improve |
| 19 |
performance and code size. Similarly, while pointer authentication |
| 20 |
can be implemented on any architecture, taking advantage of the (typically) |
| 21 |
excess addressing range of a target with 64-bit pointers minimizes the impact |
| 22 |
on memory performance and can allow interoperation with existing code (by |
| 23 |
disabling pointer authentication dynamically). This document will generally |
| 24 |
attempt to present the pointer authentication feature independent of any |
| 25 |
hardware implementation or ABI. Considerations that are |
| 26 |
implementation-specific are clearly identified throughout. |
| 27 |
|
| 28 |
Note that there are several different terms in use: |
| 29 |
|
| 30 |
- **Pointer authentication** is a target-independent language technology. |
| 31 |
|
| 32 |
- **PAuth** (sometimes referred to as **PAC**, for Pointer Authentication |
| 33 |
Codes) is an AArch64 architecture extension that provides hardware support |
| 34 |
for pointer authentication. Additional extensions either modify some of the |
| 35 |
PAuth instruction behavior (notably FPAC), or provide new instruction |
| 36 |
variants (PAuth_LR). |
| 37 |
|
| 38 |
- **Armv8.3** is an AArch64 architecture revision that makes PAuth mandatory. |
| 39 |
|
| 40 |
- **arm64e** is a specific ABI (not yet fully stable) for implementing pointer |
| 41 |
authentication using PAuth on certain Apple operating systems. |
| 42 |
|
| 43 |
This document serves four purposes: |
| 44 |
|
| 45 |
- It describes the basic ideas of pointer authentication. |
| 46 |
|
| 47 |
- It documents several language extensions that are useful on targets using |
| 48 |
pointer authentication. |
| 49 |
|
| 50 |
- It presents a theory of operation for the security mitigation, describing the |
| 51 |
basic requirements for correctness, various weaknesses in the mechanism, and |
| 52 |
ways in which programmers can strengthen its protections (including |
| 53 |
recommendations for language implementors). |
| 54 |
|
| 55 |
- It documents the stable ABI of the C, C++, and Objective-C languages on arm64e |
| 56 |
platforms. |
| 57 |
|
| 58 |
|
| 59 |
Basic concepts |
| 60 |
-------------- |
| 61 |
|
| 62 |
The simple address of an object or function is a **raw pointer**. A raw |
| 63 |
pointer can be **signed** to produce a **signed pointer**. A signed pointer |
| 64 |
can be then **authenticated** in order to verify that it was **validly signed** |
| 65 |
and extract the original raw pointer. These terms reflect the most likely |
| 66 |
implementation technique: computing and storing a cryptographic signature along |
| 67 |
with the pointer. |
| 68 |
|
| 69 |
An **abstract signing key** is a name which refers to a secret key which is |
| 70 |
used to sign and authenticate pointers. The concrete key value for a |
| 71 |
particular name is consistent throughout a process. |
| 72 |
|
| 73 |
A **discriminator** is an arbitrary value used to **diversify** signed pointers |
| 74 |
so that one validly-signed pointer cannot simply be copied over another. |
| 75 |
A discriminator is simply opaque data of some implementation-defined size that |
| 76 |
is included in the signature as a salt (see `Discriminators`_ for details.) |
| 77 |
|
| 78 |
Nearly all aspects of pointer authentication use just these two primary |
| 79 |
operations: |
| 80 |
|
| 81 |
- ``sign(raw_pointer, key, discriminator)`` produces a signed pointer given |
| 82 |
a raw pointer, an abstract signing key, and a discriminator. |
| 83 |
|
| 84 |
- ``auth(signed_pointer, key, discriminator)`` produces a raw pointer given |
| 85 |
a signed pointer, an abstract signing key, and a discriminator. |
| 86 |
|
| 87 |
``auth(sign(raw_pointer, key, discriminator), key, discriminator)`` must |
| 88 |
succeed and produce ``raw_pointer``. ``auth`` applied to a value that was |
| 89 |
ultimately produced in any other way is expected to fail, which halts the |
| 90 |
program either: |
| 91 |
|
| 92 |
- immediately, on implementations that enforce ``auth`` success (e.g., when |
| 93 |
using compiler-generated ``auth`` failure checks, or Armv8.3 with the FPAC |
| 94 |
extension), or |
| 95 |
|
| 96 |
- when the resulting pointer value is used, on implementations that don't. |
| 97 |
|
| 98 |
However, regardless of the implementation's handling of ``auth`` failures, it |
| 99 |
is permitted for ``auth`` to fail to detect that a signed pointer was not |
| 100 |
produced in this way, in which case it may return anything; this is what makes |
| 101 |
pointer authentication a probabilistic mitigation rather than a perfect one. |
| 102 |
|
| 103 |
There are two secondary operations which are required only to implement certain |
| 104 |
intrinsics in ``<ptrauth.h>``: |
| 105 |
|
| 106 |
- ``strip(signed_pointer, key)`` produces a raw pointer given a signed pointer |
| 107 |
and a key without verifying its validity, unlike ``auth``. This is useful |
| 108 |
for certain kinds of tooling, such as crash backtraces; it should generally |
| 109 |
not be used in the basic language ABI except in very careful ways. |
| 110 |
|
| 111 |
- ``sign_generic(value)`` produces a cryptographic signature for arbitrary |
| 112 |
data, not necessarily a pointer. This is useful for efficiently verifying |
| 113 |
that non-pointer data has not been tampered with. |
| 114 |
|
| 115 |
Whenever any of these operations is called for, the key value must be known |
| 116 |
statically. This is because the layout of a signed pointer may vary according |
| 117 |
to the signing key. (For example, in Armv8.3, the layout of a signed pointer |
| 118 |
depends on whether Top Byte Ignore (TBI) is enabled, which can be set |
| 119 |
independently for I and D keys.) |
| 120 |
|
| 121 |
.. admonition:: Note for API designers and language implementors |
| 122 |
|
| 123 |
These are the *primitive* operations of pointer authentication, provided for |
| 124 |
clarity of description. They are not suitable either as high-level |
| 125 |
interfaces or as primitives in a compiler IR because they expose raw |
| 126 |
pointers. Raw pointers require special attention in the language |
| 127 |
implementation to avoid the accidental creation of exploitable code |
| 128 |
sequences; see the section on `Attackable code sequences`_. |
| 129 |
|
| 130 |
The following details are all implementation-defined: |
| 131 |
|
| 132 |
- the nature of a signed pointer |
| 133 |
- the size of a discriminator |
| 134 |
- the number and nature of the signing keys |
| 135 |
- the implementation of the ``sign``, ``auth``, ``strip``, and ``sign_generic`` |
| 136 |
operations |
| 137 |
|
| 138 |
While the use of the terms "sign" and "signed pointer" suggest the use of |
| 139 |
a cryptographic signature, other implementations may be possible. See |
| 140 |
`Alternative implementations`_ for an exploration of implementation options. |
| 141 |
|
| 142 |
.. admonition:: Implementation example: Armv8.3 |
| 143 |
|
| 144 |
Readers may find it helpful to know how these terms map to Armv8.3 PAuth: |
| 145 |
|
| 146 |
- A signed pointer is a pointer with a signature stored in the |
| 147 |
otherwise-unused high bits. The kernel configures the address width based |
| 148 |
on the system's addressing needs, and enables TBI for I or D keys as |
| 149 |
needed. The bits above the address bits and below the TBI bits (if |
| 150 |
enabled) are unused. The signature width then depends on this addressing |
| 151 |
configuration. |
| 152 |
|
| 153 |
- A discriminator is a 64-bit integer. Constant discriminators are 16-bit |
| 154 |
integers. Blending a constant discriminator into an address consists of |
| 155 |
replacing the top 16 bits of the pointer containing the address with the |
| 156 |
constant. Pointers used for blending purposes should only have address |
| 157 |
bits, since higher bits will be at least partially overwritten with the |
| 158 |
constant discriminator. |
| 159 |
|
| 160 |
- There are five 128-bit signing-key registers, each of which can only be |
| 161 |
directly read or set by privileged code. Of these, four are used for |
| 162 |
signing pointers, and the fifth is used only for ``sign_generic``. The key |
| 163 |
data is simply a pepper added to the hash, not an encryption key, and so |
| 164 |
can be initialized using random data. |
| 165 |
|
| 166 |
- ``sign`` computes a cryptographic hash of the pointer, discriminator, and |
| 167 |
signing key, and stores it in the high bits as the signature. ``auth`` |
| 168 |
removes the signature, computes the same hash, and compares the result with |
| 169 |
the stored signature. ``strip`` removes the signature without |
| 170 |
authenticating it. The ``aut`` instructions in the baseline Armv8.3 PAuth |
| 171 |
feature do not guarantee to trap on authentication failure; instead, they |
| 172 |
simply corrupt the pointer so that later uses will likely trap. Unless the |
| 173 |
"later use" follows immediately and cannot be recovered from (e.g. with a |
| 174 |
signal handler), this does not provide adequate protection against |
| 175 |
`authentication oracles`_, so implementations must emit additional |
| 176 |
instructions to force an immediate trap. This is unnecessary if the |
| 177 |
processor provides the optional ``FPAC`` extension, which guarantees an |
| 178 |
immediate trap. |
| 179 |
|
| 180 |
- ``sign_generic`` corresponds to the ``pacga`` instruction, which takes two |
| 181 |
64-bit values and produces a 64-bit cryptographic hash. Implementations of |
| 182 |
this instruction are not required to produce meaningful data in all bits of |
| 183 |
the result. |
| 184 |
|
| 185 |
Discriminators |
| 186 |
~~~~~~~~~~~~~~ |
| 187 |
|
| 188 |
A discriminator is arbitrary extra data which alters the signature calculated |
| 189 |
for a pointer. When two pointers are signed differently --- either with |
| 190 |
different keys or with different discriminators --- an attacker cannot simply |
| 191 |
replace one pointer with the other. |
| 192 |
|
| 193 |
To use standard cryptographic terminology, a discriminator acts as a |
| 194 |
`salt <https://en.wikipedia.org/wiki/Salt_(cryptography)>`_ in the signing of a |
| 195 |
pointer, and the key data acts as a |
| 196 |
`pepper <https://en.wikipedia.org/wiki/Pepper_(cryptography)>`_. That is, |
| 197 |
both the discriminator and key data are ultimately just added as inputs to the |
| 198 |
signing algorithm along with the pointer, but they serve significantly |
| 199 |
different roles. The key data is a common secret added to every signature, |
| 200 |
whereas the discriminator is a value that can be derived from |
| 201 |
the context in which a specific pointer is signed. However, unlike a password |
| 202 |
salt, it's important that discriminators be *independently* derived from the |
| 203 |
circumstances of the signing; they should never simply be stored alongside |
| 204 |
a pointer. Discriminators are then re-derived in authentication operations. |
| 205 |
|
| 206 |
The intrinsic interface in ``<ptrauth.h>`` allows an arbitrary discriminator |
| 207 |
value to be provided, but can only be used when running normal code. The |
| 208 |
discriminators used by language ABIs must be restricted to make it feasible for |
| 209 |
the loader to sign pointers stored in global memory without needing excessive |
| 210 |
amounts of metadata. Under these restrictions, a discriminator may consist of |
| 211 |
either or both of the following: |
| 212 |
|
| 213 |
- The address at which the pointer is stored in memory. A pointer signed with |
| 214 |
a discriminator which incorporates its storage address is said to have |
| 215 |
**address diversity**. In general, using address diversity means that |
| 216 |
a pointer cannot be reliably copied by an attacker to or from a different |
| 217 |
memory location. However, an attacker may still be able to attack a larger |
| 218 |
call sequence if they can alter the address through which the pointer is |
| 219 |
accessed. Furthermore, some situations cannot use address diversity because |
| 220 |
of language or other restrictions. |
| 221 |
|
| 222 |
- A constant integer, called a **constant discriminator**. A pointer signed |
| 223 |
with a non-zero constant discriminator is said to have **constant |
| 224 |
diversity**. If the discriminator is specific to a single declaration, it is |
| 225 |
said to have **declaration diversity**; if the discriminator is specific to |
| 226 |
a type of value, it is said to have **type diversity**. For example, C++ |
| 227 |
v-tables on arm64e sign their component functions using a hash of their |
| 228 |
method names and signatures, which provides declaration diversity; similarly, |
| 229 |
C++ member function pointers sign their invocation functions using a hash of |
| 230 |
the member pointer type, which provides type diversity. |
| 231 |
|
| 232 |
The implementation may need to restrict constant discriminators to be |
| 233 |
significantly smaller than the full size of a discriminator. For example, on |
| 234 |
arm64e, constant discriminators are only 16-bit values. This is believed to |
| 235 |
not significantly weaken the mitigation, since collisions remain uncommon. |
| 236 |
|
| 237 |
The algorithm for blending a constant discriminator with a storage address is |
| 238 |
implementation-defined. |
| 239 |
|
| 240 |
.. _Signing schemas: |
| 241 |
|
| 242 |
Signing schemas |
| 243 |
~~~~~~~~~~~~~~~ |
| 244 |
|
| 245 |
Correct use of pointer authentication requires the signing code and the |
| 246 |
authenticating code to agree about the **signing schema** for the pointer: |
| 247 |
|
| 248 |
- the abstract signing key with which the pointer should be signed and |
| 249 |
- an algorithm for computing the discriminator. |
| 250 |
|
| 251 |
As described in the section above on `Discriminators`_, in most situations, the |
| 252 |
discriminator is produced by taking a constant discriminator and optionally |
| 253 |
blending it with the storage address of the pointer. In these situations, the |
| 254 |
signing schema breaks down even more simply: |
| 255 |
|
| 256 |
- the abstract signing key, |
| 257 |
- a constant discriminator, and |
| 258 |
- whether to use address diversity. |
| 259 |
|
| 260 |
It is important that the signing schema be independently derived at all signing |
| 261 |
and authentication sites. Preferably, the schema should be hard-coded |
| 262 |
everywhere it is needed, but at the very least, it must not be derived by |
| 263 |
inspecting information stored along with the pointer. See the section on |
| 264 |
`Attacks on pointer authentication`_ for more information. |
| 265 |
|
| 266 |
|
| 267 |
Language features |
| 268 |
----------------- |
| 269 |
|
| 270 |
There are three levels of the pointer authentication language feature: |
| 271 |
|
| 272 |
- The language implementation automatically signs and authenticates function |
| 273 |
pointers (and certain data pointers) across a variety of standard situations, |
| 274 |
including return addresses, function pointers, and C++ virtual functions. The |
| 275 |
intent is for all pointers to code in program memory to be signed in some way |
| 276 |
and for all branches to code in program text to authenticate those |
| 277 |
signatures. In addition to the code pointers themselves, we also use pointer |
| 278 |
authentication to protect data values that directly or indirectly influence |
| 279 |
control flow or program integrity, or can provide attackers with some other |
| 280 |
powerful program compromise. |
| 281 |
|
| 282 |
- The language also provides extensions to override the default rules used by |
| 283 |
the language implementation. For example, the ``__ptrauth`` type qualifier |
| 284 |
can be used to change how pointers or pointer sized integers are signed when |
| 285 |
they are stored in a particular variable or field; this provides much stronger |
| 286 |
protection than is guaranteed by the default rules for C function and data |
| 287 |
pointers. |
| 288 |
|
| 289 |
- Finally, the language provides the ``<ptrauth.h>`` intrinsic interface for |
| 290 |
manually signing and authenticating pointers in code. These can be used in |
| 291 |
circumstances where very specific behavior is required. |
| 292 |
|
| 293 |
Language implementation |
| 294 |
~~~~~~~~~~~~~~~~~~~~~~~ |
| 295 |
|
| 296 |
For the most part, pointer authentication is an unobserved detail of the |
| 297 |
implementation of the programming language. Any element of the language |
| 298 |
implementation that would perform an indirect branch to a pointer is implicitly |
| 299 |
altered so that the pointer is signed when first constructed and authenticated |
| 300 |
when the branch is performed. This includes: |
| 301 |
|
| 302 |
- indirect-call features in the programming language, such as C function |
| 303 |
pointers, C++ virtual functions, C++ member function pointers, the "blocks" |
| 304 |
C extension, and so on; |
| 305 |
|
| 306 |
- returning from a function, no matter how it is called; and |
| 307 |
|
| 308 |
- indirect calls introduced by the implementation, such as branches through the |
| 309 |
global offset table (GOT) used to implement direct calls to functions defined |
| 310 |
outside of the current shared object. |
| 311 |
|
| 312 |
For more information about this, see the `Language ABI`_ section. |
| 313 |
|
| 314 |
However, some aspects of the implementation are observable by the programmer or |
| 315 |
otherwise require special notice. |
| 316 |
|
| 317 |
C data pointers |
| 318 |
^^^^^^^^^^^^^^^ |
| 319 |
|
| 320 |
The current implementation in Clang does not sign pointers to ordinary data by |
| 321 |
default. For a partial explanation of the reasoning behind this, see the |
| 322 |
`Theory of Operation`_ section. |
| 323 |
|
| 324 |
A specific data pointer which is more security-sensitive than most can be |
| 325 |
signed using the `__ptrauth qualifier`_ or using the ``<ptrauth.h>`` |
| 326 |
intrinsics. |
| 327 |
|
| 328 |
C function pointers |
| 329 |
^^^^^^^^^^^^^^^^^^^ |
| 330 |
|
| 331 |
The C standard imposes restrictions on the representation and semantics of |
| 332 |
function pointer types which make it difficult to achieve satisfactory |
| 333 |
signature diversity in the default language rules. See `Attacks on pointer |
| 334 |
authentication`_ for more information about signature diversity. Programmers |
| 335 |
should strongly consider using the ``__ptrauth`` qualifier to improve the |
| 336 |
protections for important function pointers, such as the components of of |
| 337 |
a hand-rolled "v-table"; see the section on the `__ptrauth qualifier`_ for |
| 338 |
details. |
| 339 |
|
| 340 |
The value of a pointer to a C function includes a signature, even when the |
| 341 |
value is cast to a non-function-pointer type like ``void*`` or ``intptr_t``. On |
| 342 |
implementations that use high bits to store the signature, this means that |
| 343 |
relational comparisons and hashes will vary according to the exact signature |
| 344 |
value, which is likely to change between executions of a program. In some |
| 345 |
implementations, it may also vary based on the exact function pointer type. |
| 346 |
|
| 347 |
Null pointers |
| 348 |
^^^^^^^^^^^^^ |
| 349 |
|
| 350 |
In principle, an implementation could derive the signed null pointer value |
| 351 |
simply by applying the standard signing algorithm to the raw null pointer |
| 352 |
value. However, for likely signing algorithms, this would mean that the signed |
| 353 |
null pointer value would no longer be statically known, which would have many |
| 354 |
negative consequences. For one, it would become substantially more expensive |
| 355 |
to emit null pointer values or to perform null-pointer checks. For another, |
| 356 |
the pervasive (even if technically unportable) assumption that null pointers |
| 357 |
are bitwise zero would be invalidated, making it substantially more difficult |
| 358 |
to adopt pointer authentication, as well as weakening common optimizations for |
| 359 |
zero-initialized memory such as the use of ``.bzz`` sections. Therefore it is |
| 360 |
beneficial to treat null pointers specially by giving them their usual |
| 361 |
representation. On AArch64, this requires additional code when working with |
| 362 |
possibly-null pointers, such as when copying a pointer field that has been |
| 363 |
signed with address diversity. |
| 364 |
|
| 365 |
While this representation of nulls is the safest option for the general case, |
| 366 |
there are some situations in which a null pointer may have important semantic |
| 367 |
or security impact. For that purpose Clang has the concept of a pointer |
| 368 |
authentication schema that signs and authenticates null values. |
| 369 |
|
| 370 |
Return addresses |
| 371 |
^^^^^^^^^^^^^^^^ |
| 372 |
|
| 373 |
The current implementation in Clang implicitly signs the return addresses in |
| 374 |
function calls. While the value of the return address is technically an |
| 375 |
implementation detail of a function, there are some important libraries and |
| 376 |
development tools which rely on manually walking the chain of stack frames. |
| 377 |
These tools must be updated to correctly account for pointer authentication, |
| 378 |
either by stripping signatures (if security is not important for the tool, e.g. |
| 379 |
if it is capturing a stack trace during a crash) or properly authenticating |
| 380 |
them. More information about how these values are signed is available in the |
| 381 |
`Language ABI`_ section. |
| 382 |
|
| 383 |
C++ virtual functions |
| 384 |
^^^^^^^^^^^^^^^^^^^^^ |
| 385 |
|
| 386 |
The current implementation in Clang signs virtual function pointers with |
| 387 |
a discriminator derived from the full signature of the overridden method, |
| 388 |
including the method name and parameter types. It is possible to write C++ |
| 389 |
code that relies on v-table layout remaining constant despite changes to |
| 390 |
a method signature; for example, a parameter might be a ``typedef`` that |
| 391 |
resolves to a different type based on a build setting. Such code violates |
| 392 |
C++'s One Definition Rule (ODR), but that violation is not normally detected; |
| 393 |
however, pointer authentication will detect it. |
| 394 |
|
| 395 |
Language extensions |
| 396 |
~~~~~~~~~~~~~~~~~~~ |
| 397 |
|
| 398 |
Feature testing |
| 399 |
^^^^^^^^^^^^^^^ |
| 400 |
|
| 401 |
Whether the current target uses pointer authentication can be tested for with |
| 402 |
a number of different tests. |
| 403 |
|
| 404 |
- ``__PTRAUTH__`` macro is defined if ``<ptrauth.h>`` provides its normal |
| 405 |
interface. This implies support for the pointer authentication intrinsics |
| 406 |
and the ``__ptrauth`` qualifier. |
| 407 |
|
| 408 |
- ``__has_feature(ptrauth_returns)`` is true if the target uses pointer |
| 409 |
authentication to protect return addresses. |
| 410 |
|
| 411 |
- ``__has_feature(ptrauth_calls)`` is true if the target uses pointer |
| 412 |
authentication to protect indirect branches. On arm64e this implies |
| 413 |
``__has_feature(ptrauth_returns)``, ``__has_feature(ptrauth_intrinsics)``, |
| 414 |
and the ``__PTRAUTH__`` macro. |
| 415 |
|
| 416 |
- For backwards compatibility purposes ``__has_feature(ptrauth_intrinsics)`` |
| 417 |
and ``__has_feature(ptrauth_qualifier)`` are available on arm64e targets. |
| 418 |
These features are synonymous with each other, and are equivalent to testing |
| 419 |
for the ``__PTRAUTH__`` macro definition. Use of these features should be |
| 420 |
restricted to cases where backwards compatibility is required, and should be |
| 421 |
paired with ``defined(__PTRAUTH__)``. |
| 422 |
|
| 423 |
|
| 424 |
Clang provides several other tests only for historical purposes; for current |
| 425 |
purposes they are all equivalent to ``ptrauth_calls``. |
| 426 |
|
| 427 |
``__ptrauth`` qualifier |
| 428 |
^^^^^^^^^^^^^^^^^^^^^^^ |
| 429 |
|
| 430 |
``__ptrauth(key, address, discriminator)`` is an extended type |
| 431 |
qualifier which causes so-qualified objects to hold pointers or pointer sized |
| 432 |
integers signed using the specified schema rather than the default schema for |
| 433 |
such types. |
| 434 |
|
| 435 |
In the current implementation in Clang, the qualified type must be a C pointer |
| 436 |
type, either to a function or to an object, or a pointer sized integer. It |
| 437 |
currently cannot be an Objective-C pointer type, a C++ reference type, or a |
| 438 |
block pointer type; these restrictions may be lifted in the future. |
| 439 |
|
| 440 |
The current implementation in Clang is known to not provide adequate safety |
| 441 |
guarantees against the creation of `signing oracles`_ when assigning data |
| 442 |
pointers to ``__ptrauth``-qualified gl-values. See the section on `safe |
| 443 |
derivation`_ for more information. |
| 444 |
|
| 445 |
The qualifier's operands are as follows: |
| 446 |
|
| 447 |
- ``key`` - an expression evaluating to a key value from ``<ptrauth.h>``; must |
| 448 |
be a constant expression |
| 449 |
|
| 450 |
- ``address`` - whether to use address diversity (1) or not (0); must be |
| 451 |
a constant expression with one of these two values |
| 452 |
|
| 453 |
- ``discriminator`` - a constant discriminator; must be a constant expression |
| 454 |
|
| 455 |
See `Discriminators`_ for more information about discriminators. |
| 456 |
|
| 457 |
Currently the operands must be constant-evaluable even within templates. In the |
| 458 |
future this restriction may be lifted to allow value-dependent expressions as |
| 459 |
long as they instantiate to a constant expression. |
| 460 |
|
| 461 |
Consistent with the ordinary C/C++ rule for parameters, top-level ``__ptrauth`` |
| 462 |
qualifiers on a parameter (after parameter type adjustment) are ignored when |
| 463 |
deriving the type of the function. The parameter will be passed using the |
| 464 |
default ABI for the unqualified pointer type. |
| 465 |
|
| 466 |
If ``x`` is an object of type ``__ptrauth(key, address, discriminator) T``, |
| 467 |
then the signing schema of the value stored in ``x`` is a key of ``key`` and |
| 468 |
a discriminator determined as follows: |
| 469 |
|
| 470 |
- if ``address`` is 0, then the discriminator is ``discriminator``; |
| 471 |
|
| 472 |
- if ``address`` is 1 and ``discriminator`` is 0, then the discriminator is |
| 473 |
``&x``; otherwise |
| 474 |
|
| 475 |
- if ``address`` is 1 and ``discriminator`` is non-zero, then the discriminator |
| 476 |
is ``ptrauth_blend_discriminator(&x, discriminator)``; see |
| 477 |
`ptrauth_blend_discriminator`_. |
| 478 |
|
| 479 |
Non-triviality from address diversity |
| 480 |
+++++++++++++++++++++++++++++++++++++ |
| 481 |
|
| 482 |
Address diversity must impose additional restrictions in order to allow the |
| 483 |
implementation to correctly copy values. In C++, a type qualified with address |
| 484 |
diversity is treated like a class type with non-trivial copy/move constructors |
| 485 |
and assignment operators, with the usual effect on containing classes and |
| 486 |
unions. C does not have a standard concept of non-triviality, and so we must |
| 487 |
describe the basic rules here, with the intention of imitating the emergent |
| 488 |
rules of C++: |
| 489 |
|
| 490 |
- A type may be **non-trivial to copy**. |
| 491 |
|
| 492 |
- A type may also be **illegal to copy**. Types that are illegal to copy are |
| 493 |
always non-trivial to copy. |
| 494 |
|
| 495 |
- A type may also be **address-sensitive**. This includes types that use self |
| 496 |
referencing pointers, data protected by address diversified pointer |
| 497 |
authentication, or other similar concepts. |
| 498 |
|
| 499 |
- A type qualified with a ``ptrauth`` qualifier or implicit authentication |
| 500 |
schema that requires address diversity is non-trivial to copy and |
| 501 |
address-sensitive. |
| 502 |
|
| 503 |
- An array type is illegal to copy, non-trivial to copy, or address-sensitive |
| 504 |
if its element type is illegal to copy, non-trivial to copy, or |
| 505 |
address-sensitive, respectively. |
| 506 |
|
| 507 |
- A struct type is illegal to copy, non-trivial to copy, or address-sensitive |
| 508 |
if it has a field whose type is illegal to copy, non-trivial to copy, or |
| 509 |
address-sensitive, respectively. |
| 510 |
|
| 511 |
- A union type is both illegal and non-trivial to copy if it has a field whose |
| 512 |
type is non-trivial or illegal to copy. |
| 513 |
|
| 514 |
- A union type is address-sensitive if it has a field whose type is |
| 515 |
address-sensitive. |
| 516 |
|
| 517 |
- A program is ill-formed if it uses a type that is illegal to copy as |
| 518 |
a function parameter, argument, or return type. |
| 519 |
|
| 520 |
- A program is ill-formed if an expression requires a type to be copied that is |
| 521 |
illegal to copy. |
| 522 |
|
| 523 |
- Otherwise, copying a type that is non-trivial to copy correctly copies its |
| 524 |
subobjects. |
| 525 |
|
| 526 |
- Types that are address-sensitive must always be passed and returned |
| 527 |
indirectly. Thus, changing the address-sensitivity of a type may be |
| 528 |
ABI-breaking even if its size and alignment do not change. |
| 529 |
|
| 530 |
``<ptrauth.h>`` |
| 531 |
~~~~~~~~~~~~~~~ |
| 532 |
|
| 533 |
This header defines the following types and operations: |
| 534 |
|
| 535 |
``ptrauth_key`` |
| 536 |
^^^^^^^^^^^^^^^ |
| 537 |
|
| 538 |
This ``enum`` is the type of abstract signing keys. In addition to defining |
| 539 |
the set of implementation-specific signing keys (for example, Armv8.3 defines |
| 540 |
``ptrauth_key_asia``), it also defines some portable aliases for those keys. |
| 541 |
For example, ``ptrauth_key_function_pointer`` is the key generally used for |
| 542 |
C function pointers, which will generally be suitable for other |
| 543 |
function-signing schemas. |
| 544 |
|
| 545 |
In all the operation descriptions below, key values must be constant values |
| 546 |
corresponding to one of the implementation-specific abstract signing keys from |
| 547 |
this ``enum``. |
| 548 |
|
| 549 |
``ptrauth_extra_data_t`` |
| 550 |
^^^^^^^^^^^^^^^^^^^^^^^^ |
| 551 |
|
| 552 |
This is a ``typedef`` of a standard integer type of the correct size to hold |
| 553 |
a discriminator value. |
| 554 |
|
| 555 |
In the signing and authentication operation descriptions below, discriminator |
| 556 |
values must have either pointer type or integer type. If the discriminator is |
| 557 |
an integer, it will be coerced to ``ptrauth_extra_data_t``. |
| 558 |
|
| 559 |
``ptrauth_blend_discriminator`` |
| 560 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 561 |
|
| 562 |
.. code-block:: c |
| 563 |
|
| 564 |
ptrauth_blend_discriminator(pointer, integer) |
| 565 |
|
| 566 |
Produce a discriminator value which blends information from the given pointer |
| 567 |
and the given integer. |
| 568 |
|
| 569 |
Implementations may ignore some bits from each value, which is to say, the |
| 570 |
blending algorithm may be chosen for speed and convenience over theoretical |
| 571 |
strength as a hash-combining algorithm. For example, arm64e simply overwrites |
| 572 |
the high 16 bits of the pointer with the low 16 bits of the integer, which can |
| 573 |
be done in a single instruction with an immediate integer. |
| 574 |
|
| 575 |
``pointer`` must have pointer type, and ``integer`` must have integer type. The |
| 576 |
result has type ``ptrauth_extra_data_t``. |
| 577 |
|
| 578 |
``ptrauth_string_discriminator`` |
| 579 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 580 |
|
| 581 |
.. code-block:: c |
| 582 |
|
| 583 |
ptrauth_string_discriminator(string) |
| 584 |
|
| 585 |
Compute a constant discriminator from the given string. |
| 586 |
|
| 587 |
``string`` must be a string literal of ``char`` character type. The result has |
| 588 |
type ``ptrauth_extra_data_t``. |
| 589 |
|
| 590 |
The result value is never zero and always within range for both the |
| 591 |
``__ptrauth`` qualifier and ``ptrauth_blend_discriminator``. |
| 592 |
|
| 593 |
This can be used in constant expressions. |
| 594 |
|
| 595 |
``ptrauth_type_discriminator`` |
| 596 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 597 |
|
| 598 |
.. code-block:: c |
| 599 |
|
| 600 |
ptrauth_type_discriminator(type) |
| 601 |
|
| 602 |
Compute the constant discriminator derived from the given type, as is computed |
| 603 |
for automatically type diversified schemas. |
| 604 |
|
| 605 |
``type`` must be a type. The result has the type ``ptrauth_extra_data_t``. |
| 606 |
|
| 607 |
This can be used in constant expressions. |
| 608 |
|
| 609 |
``ptrauth_function_pointer_type_discriminator`` |
| 610 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 611 |
|
| 612 |
.. code-block:: c |
| 613 |
|
| 614 |
ptrauth_function_pointer_type_discriminator(function_type) |
| 615 |
|
| 616 |
Compute the constant discriminator derived from the provided function type, for |
| 617 |
use in contexts where the default function authentication schema. If function |
| 618 |
pointer type diversity is enabled, this is equivalent to |
| 619 |
`ptrauth_type_discriminator(function_type)`, if it is not enabled this is `0`. |
| 620 |
|
| 621 |
``function_type`` must be a function type. The result has the type ``ptrauth_extra_data_t``. |
| 622 |
|
| 623 |
This can be used in constant expressions. |
| 624 |
|
| 625 |
``ptrauth_strip`` |
| 626 |
^^^^^^^^^^^^^^^^^ |
| 627 |
|
| 628 |
.. code-block:: c |
| 629 |
|
| 630 |
ptrauth_strip(signedPointer, key) |
| 631 |
|
| 632 |
Given that ``signedPointer`` matches the layout for signed pointers signed with |
| 633 |
the given key, extract the raw pointer from it. This operation does not trap |
| 634 |
and cannot fail, even if the pointer is not validly signed. |
| 635 |
|
| 636 |
``ptrauth_sign_constant`` |
| 637 |
^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 638 |
|
| 639 |
.. code-block:: c |
| 640 |
|
| 641 |
ptrauth_sign_constant(pointer, key, discriminator) |
| 642 |
|
| 643 |
Return a signed pointer for a constant address in a manner which guarantees |
| 644 |
a non-attackable sequence. |
| 645 |
|
| 646 |
``pointer`` must be a constant expression of pointer type which evaluates to |
| 647 |
a non-null pointer. |
| 648 |
``key`` must be a constant expression of type ``ptrauth_key``. |
| 649 |
``discriminator`` must be a constant expression of pointer or integer type; |
| 650 |
if an integer, it will be coerced to ``ptrauth_extra_data_t``. |
| 651 |
The result will have the same type as ``pointer``. |
| 652 |
|
| 653 |
This can be used in constant expressions. |
| 654 |
|
| 655 |
``ptrauth_sign_unauthenticated`` |
| 656 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 657 |
|
| 658 |
.. code-block:: c |
| 659 |
|
| 660 |
ptrauth_sign_unauthenticated(pointer, key, discriminator) |
| 661 |
|
| 662 |
Produce a signed pointer for the given raw pointer without applying any |
| 663 |
authentication or extra treatment. This operation is not required to have the |
| 664 |
same behavior on a null pointer that the language implementation would. |
| 665 |
|
| 666 |
This is a treacherous operation that can easily result in `signing oracles`_. |
| 667 |
Programs should use it seldom and carefully. |
| 668 |
|
| 669 |
``ptrauth_auth_and_resign`` |
| 670 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 671 |
|
| 672 |
.. code-block:: c |
| 673 |
|
| 674 |
ptrauth_auth_and_resign(pointer, oldKey, oldDiscriminator, newKey, newDiscriminator) |
| 675 |
|
| 676 |
Authenticate that ``pointer`` is signed with ``oldKey`` and |
| 677 |
``oldDiscriminator`` and then resign the raw-pointer result of that |
| 678 |
authentication with ``newKey`` and ``newDiscriminator``. |
| 679 |
|
| 680 |
``pointer`` must have pointer type. The result will have the same type as |
| 681 |
``pointer``. This operation is not required to have the same behavior on |
| 682 |
a null pointer that the language implementation would. |
| 683 |
|
| 684 |
The code sequence produced for this operation must not be directly attackable. |
| 685 |
However, if the discriminator values are not constant integers, their |
| 686 |
computations may still be attackable. In the future, Clang should be enhanced |
| 687 |
to guarantee non-attackability if these expressions are |
| 688 |
:ref:`safely-derived<Safe derivation>`. |
| 689 |
|
| 690 |
``ptrauth_auth_function`` |
| 691 |
^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 692 |
|
| 693 |
.. code-block:: c |
| 694 |
|
| 695 |
ptrauth_auth_function(pointer, key, discriminator) |
| 696 |
|
| 697 |
Authenticate that ``pointer`` is signed with ``key`` and ``discriminator`` and |
| 698 |
re-sign it to the standard schema for a function pointer of its type. |
| 699 |
|
| 700 |
``pointer`` must have function pointer type. The result will have the same |
| 701 |
type as ``pointer``. This operation is not required to have the same behavior |
| 702 |
on a null pointer that the language implementation would. |
| 703 |
|
| 704 |
This operation makes the same attackability guarantees as |
| 705 |
``ptrauth_auth_and_resign``. |
| 706 |
|
| 707 |
If this operation appears syntactically as the function operand of a call, |
| 708 |
Clang guarantees that the call will directly authenticate the function value |
| 709 |
using the given schema rather than re-signing to the standard schema. |
| 710 |
|
| 711 |
``ptrauth_auth_data`` |
| 712 |
^^^^^^^^^^^^^^^^^^^^^ |
| 713 |
|
| 714 |
.. code-block:: c |
| 715 |
|
| 716 |
ptrauth_auth_data(pointer, key, discriminator) |
| 717 |
|
| 718 |
Authenticate that ``pointer`` is signed with ``key`` and ``discriminator`` and |
| 719 |
remove the signature. |
| 720 |
|
| 721 |
``pointer`` must have object pointer type. The result will have the same type |
| 722 |
as ``pointer``. This operation is not required to have the same behavior on |
| 723 |
a null pointer that the language implementation would. |
| 724 |
|
| 725 |
In the future when Clang makes safe derivation guarantees, the result of |
| 726 |
this operation should be considered safely-derived. |
| 727 |
|
| 728 |
``ptrauth_sign_generic_data`` |
| 729 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 730 |
|
| 731 |
.. code-block:: c |
| 732 |
|
| 733 |
ptrauth_sign_generic_data(value1, value2) |
| 734 |
|
| 735 |
Computes a signature for the given pair of values, incorporating a secret |
| 736 |
signing key. |
| 737 |
|
| 738 |
This operation can be used to verify that arbitrary data has not been tampered |
| 739 |
with by computing a signature for the data, storing that signature, and then |
| 740 |
repeating this process and verifying that it yields the same result. This can |
| 741 |
be reasonably done in any number of ways; for example, a library could compute |
| 742 |
an ordinary checksum of the data and just sign the result in order to get the |
| 743 |
tamper-resistance advantages of the secret signing key (since otherwise an |
| 744 |
attacker could reliably overwrite both the data and the checksum). |
| 745 |
|
| 746 |
``value1`` and ``value2`` must be either pointers or integers. If the integers |
| 747 |
are larger than ``uintptr_t`` then data not representable in ``uintptr_t`` may |
| 748 |
be discarded. |
| 749 |
|
| 750 |
The result will have type ``ptrauth_generic_signature_t``, which is an integer |
| 751 |
type. Implementations are not required to make all bits of the result equally |
| 752 |
significant; in particular, some implementations are known to not leave |
| 753 |
meaningful data in the low bits. |
| 754 |
|
| 755 |
Standard ``__ptrauth`` qualifiers |
| 756 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 757 |
|
| 758 |
``<ptrauth.h>`` additionally provides several macros which expand to |
| 759 |
``__ptrauth`` qualifiers for common ABI situations. |
| 760 |
|
| 761 |
For convenience, these macros expand to nothing when pointer authentication is |
| 762 |
disabled. |
| 763 |
|
| 764 |
These macros can be found in the header; some details of these macros may be |
| 765 |
unstable or implementation-specific. |
| 766 |
|
| 767 |
|
| 768 |
Theory of operation |
| 769 |
------------------- |
| 770 |
|
| 771 |
The threat model of pointer authentication is as follows: |
| 772 |
|
| 773 |
- The attacker has the ability to read and write to a certain range of |
| 774 |
addresses, possibly the entire address space. However, they are constrained |
| 775 |
by the normal rules of the process: for example, they cannot write to memory |
| 776 |
that is mapped read-only, and if they access unmapped memory it will trigger |
| 777 |
a trap. |
| 778 |
|
| 779 |
- The attacker has no ability to add arbitrary executable code to the program. |
| 780 |
For example, the program does not include malicious code to begin with, and |
| 781 |
the attacker cannot alter existing instructions, load a malicious shared |
| 782 |
library, or remap writable pages as executable. If the attacker wants to get |
| 783 |
the process to perform a specific sequence of actions, they must somehow |
| 784 |
subvert the normal control flow of the process. |
| 785 |
|
| 786 |
In both of the above paragraphs, it is merely assumed that the attacker's |
| 787 |
*current* capabilities are restricted; that is, their current exploit does not |
| 788 |
directly give them the power to do these things. The attacker's immediate goal |
| 789 |
may well be to leverage their exploit to gain these capabilities, e.g. to load |
| 790 |
a malicious dynamic library into the process, even though the process does not |
| 791 |
directly contain code to do so. |
| 792 |
|
| 793 |
Note that any bug that fits the above threat model can be immediately exploited |
| 794 |
as a denial-of-service attack by simply performing an illegal access and |
| 795 |
crashing the program. Pointer authentication cannot protect against this. |
| 796 |
While denial-of-service attacks are unfortunate, they are also unquestionably |
| 797 |
the best possible result of a bug this severe. Therefore, pointer authentication |
| 798 |
enthusiastically embraces the idea of halting the program on a pointer |
| 799 |
authentication failure rather than continuing in a possibly-compromised state. |
| 800 |
|
| 801 |
Pointer authentication is a form of control-flow integrity (CFI) enforcement. |
| 802 |
The basic security hypothesis behind CFI enforcement is that many bugs can only |
| 803 |
be usefully exploited (other than as a denial-of-service) by leveraging them to |
| 804 |
subvert the control flow of the program. If this is true, then by inhibiting or |
| 805 |
limiting that subversion, it may be possible to largely mitigate the security |
| 806 |
consequences of those bugs by rendering them impractical (or, ideally, |
| 807 |
impossible) to exploit. |
| 808 |
|
| 809 |
Every indirect branch in a program has a purpose. Using human intelligence, a |
| 810 |
programmer can describe where a particular branch *should* go according to this |
| 811 |
purpose: a ``return`` in ``printf`` should return to the call site, a particular |
| 812 |
call in ``qsort`` should call the comparator that was passed in as an argument, |
| 813 |
and so on. But for CFI to enforce that every branch in a program goes where it |
| 814 |
*should* in this sense would require CFI to perfectly enforce every semantic |
| 815 |
rule of the program's abstract machine; that is, it would require making the |
| 816 |
programming environment perfectly sound. That is out of scope. Instead, the |
| 817 |
goal of CFI is merely to catch attempts to make a branch go somewhere that its |
| 818 |
obviously *shouldn't* for its purpose: for example, to stop a call from |
| 819 |
branching into the middle of a function rather than its beginning. As the |
| 820 |
information available to CFI gets better about the purpose of the branch, CFI |
| 821 |
can enforce tighter and tighter restrictions on where the branch is permitted to |
| 822 |
go. Still, ultimately CFI cannot make the program sound. This may help explain |
| 823 |
why pointer authentication makes some of the choices it does: for example, to |
| 824 |
sign and authenticate mostly code pointers rather than every pointer in the |
| 825 |
program. Preventing attackers from redirecting branches is both particularly |
| 826 |
important and particularly approachable as a goal. Detecting corruption more |
| 827 |
broadly is infeasible with these techniques, and the attempt would have far |
| 828 |
higher cost. |
| 829 |
|
| 830 |
Attacks on pointer authentication |
| 831 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 832 |
|
| 833 |
Pointer authentication works as follows. Every indirect branch in a program has |
| 834 |
a purpose. For every purpose, the implementation chooses a |
| 835 |
:ref:`signing schema<Signing schemas>`. At some place where a pointer is known |
| 836 |
to be correct for its purpose, it is signed according to the purpose's schema. |
| 837 |
At every place where the pointer is needed for its purpose, it is authenticated |
| 838 |
according to the purpose's schema. If that authentication fails, the program is |
| 839 |
halted. |
| 840 |
|
| 841 |
There are a variety of ways to attack this. |
| 842 |
|
| 843 |
Attacks of interest to programmers |
| 844 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 845 |
|
| 846 |
These attacks arise from weaknesses in the default protections offered by |
| 847 |
pointer authentication. They can be addressed by using attributes or intrinsics |
| 848 |
to opt in to stronger protection. |
| 849 |
|
| 850 |
Substitution attacks |
| 851 |
++++++++++++++++++++ |
| 852 |
|
| 853 |
An attacker can simply overwrite a pointer intended for one purpose with a |
| 854 |
pointer intended for another purpose if both purposes use the same signing |
| 855 |
schema and that schema does not use address diversity. |
| 856 |
|
| 857 |
The most common source of this weakness is when code relies on using the default |
| 858 |
language rules for C function pointers. The current implementation uses the |
| 859 |
exact same signing schema for all C function pointers, even for functions of |
| 860 |
substantially different type. While efforts are ongoing to improve constant |
| 861 |
diversity for C function pointers of different type, there are necessary limits |
| 862 |
to this. The C standard requires function pointers to be copyable with |
| 863 |
``memcpy``, which means that function pointers can never use address diversity. |
| 864 |
Furthermore, even if a function pointer can only be replaced with another |
| 865 |
function of the exact same type, that can still be useful to an attacker, as in |
| 866 |
the following example of a hand-rolled "v-table": |
| 867 |
|
| 868 |
.. code-block:: c |
| 869 |
|
| 870 |
struct ObjectOperations { |
| 871 |
void (*retain)(Object *); |
| 872 |
void (*release)(Object *); |
| 873 |
void (*deallocate)(Object *); |
| 874 |
void (*logStatus)(Object *); |
| 875 |
}; |
| 876 |
|
| 877 |
The weakness in this design is that by lacking any context specific |
| 878 |
discriminator, this means an attacker can substitute any of these fields with |
| 879 |
any other function pointer signed with the default schema. Similarly the lack of |
| 880 |
address diversity allows an attacker to replace the functions in one type's |
| 881 |
"v-table" with those of another. This can be mitigated by overriding the default |
| 882 |
authentication schema with a more specific signing schema for each purpose. For |
| 883 |
instance, in this example, the ``__ptrauth`` qualifier can be used with a |
| 884 |
different constant discriminator for each field. Since there's no particular |
| 885 |
reason it's important for this v-table to be copyable with ``memcpy``, the |
| 886 |
functions can also be signed with address diversity: |
| 887 |
|
| 888 |
.. code-block:: c |
| 889 |
|
| 890 |
#if defined(__PTRAUTH__) |
| 891 |
#define objectOperation(discriminator) \ |
| 892 |
__ptrauth(ptrauth_key_function_pointer, 1, discriminator) |
| 893 |
#else |
| 894 |
#define objectOperation(discriminator) |
| 895 |
#endif |
| 896 |
|
| 897 |
struct ObjectOperations { |
| 898 |
void (*objectOperation(0xf017) retain)(Object *); |
| 899 |
void (*objectOperation(0x2639) release)(Object *); |
| 900 |
void (*objectOperation(0x8bb0) deallocate)(Object *); |
| 901 |
void (*objectOperation(0xc5d4) logStatus)(Object *); |
| 902 |
}; |
| 903 |
|
| 904 |
This weakness can also sometimes be mitigated by simply keeping the signed |
| 905 |
pointer in constant memory, but this is less effective than using better signing |
| 906 |
diversity. |
| 907 |
|
| 908 |
.. _Access path attacks: |
| 909 |
|
| 910 |
Access path attacks |
| 911 |
+++++++++++++++++++ |
| 912 |
|
| 913 |
If a signed pointer is often accessed indirectly (that is, by first loading the |
| 914 |
address of the object where the signed pointer is stored), an attacker can |
| 915 |
affect uses of it by overwriting the intermediate pointer in the access path. |
| 916 |
|
| 917 |
The most common scenario exhibiting this weakness is an object with a pointer to |
| 918 |
a "v-table" (a structure holding many function pointers). An attacker does not |
| 919 |
need to replace a signed function pointer in the v-table if they can instead |
| 920 |
simply replace the v-table pointer in the object with their own pointer --- |
| 921 |
perhaps to memory where they've constructed their own v-table, or to existing |
| 922 |
memory that coincidentally happens to contain a signed pointer at the right |
| 923 |
offset that's been signed with the right signing schema. |
| 924 |
|
| 925 |
This attack arises because data pointers are not signed by default. It works |
| 926 |
even if the signed pointer uses address diversity: address diversity merely |
| 927 |
means that each pointer is signed with its own storage address, |
| 928 |
which (by design) is invariant to changes in the accessing pointer. |
| 929 |
|
| 930 |
Using sufficiently diverse signing schemas within the v-table can provide |
| 931 |
reasonably strong mitigation against this weakness. Always use address and type |
| 932 |
diversity in v-tables to prevent attackers from assembling their own v-table. |
| 933 |
Avoid re-using constant discriminators to prevent attackers from replacing a |
| 934 |
v-table pointer with a pointer to totally unrelated memory that just happens to |
| 935 |
contain an similarly-signed pointer, or reused memory containing a different |
| 936 |
type. |
| 937 |
|
| 938 |
Further mitigation can be attained by signing pointers to v-tables. Any |
| 939 |
signature at all should prevent attackers from forging v-table pointers; they |
| 940 |
will need to somehow harvest an existing signed pointer from elsewhere in |
| 941 |
memory. Using a meaningful constant discriminator will force this to be |
| 942 |
harvested from an object with similar structure (e.g. a different implementation |
| 943 |
of the same interface). Using address diversity will prevent such harvesting |
| 944 |
entirely. However, care must be taken when sourcing the v-table pointer |
| 945 |
originally; do not blindly sign a pointer that is not |
| 946 |
:ref:`safely derived<Safe derivation>`. |
| 947 |
|
| 948 |
.. _Signing oracles: |
| 949 |
|
| 950 |
Signing oracles |
| 951 |
+++++++++++++++ |
| 952 |
|
| 953 |
A signing oracle is a bit of code which can be exploited by an attacker to sign |
| 954 |
an arbitrary pointer in a way that can later be recovered. Such oracles can be |
| 955 |
used by attackers to forge signatures matching the oracle's signing schema, |
| 956 |
which is likely to cause a total compromise of pointer authentication's |
| 957 |
effectiveness. |
| 958 |
|
| 959 |
This attack only affects ordinary programmers if they are using certain |
| 960 |
treacherous patterns of code. Currently this includes: |
| 961 |
|
| 962 |
- all uses of the ``__ptrauth_sign_unauthenticated`` intrinsic and |
| 963 |
- assigning values to ``__ptrauth``-qualified l-values. |
| 964 |
|
| 965 |
Care must be taken in these situations to ensure that the pointer being signed |
| 966 |
has been :ref:`safely derived<Safe derivation>` or is otherwise not possible to |
| 967 |
attack. (In some cases, this may be challenging without compiler support.) |
| 968 |
|
| 969 |
A diagnostic will be added in the future for implicitly dangerous patterns of |
| 970 |
code, such as assigning a non-safely-derived values to a |
| 971 |
``__ptrauth``-qualified l-value. |
| 972 |
|
| 973 |
.. _Authentication oracles: |
| 974 |
|
| 975 |
Authentication oracles |
| 976 |
++++++++++++++++++++++ |
| 977 |
|
| 978 |
An authentication oracle is a bit of code which can be exploited by an attacker |
| 979 |
to leak whether a signed pointer is validly signed without halting the program |
| 980 |
if it isn't. Such oracles can be used to forge signatures matching the oracle's |
| 981 |
signing schema if the attacker can repeatedly invoke the oracle for different |
| 982 |
candidate signed pointers. This is likely to cause a total compromise of pointer |
| 983 |
authentication's effectiveness. |
| 984 |
|
| 985 |
There should be no way for an ordinary programmer to create an authentication |
| 986 |
oracle using the current set of operations. However, implementation flaws in the |
| 987 |
past have occasionally given rise to authentication oracles due to a failure to |
| 988 |
immediately trap on authentication failure. |
| 989 |
|
| 990 |
The likelihood of creating an authentication oracle is why there is currently no |
| 991 |
intrinsic which queries whether a signed pointer is validly signed. |
| 992 |
|
| 993 |
|
| 994 |
Attacks of interest to implementors |
| 995 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 996 |
|
| 997 |
These attacks are not inherent to the model; they arise from mistakes in either |
| 998 |
implementing or using the `sign` and `auth` operations. Avoiding these mistakes |
| 999 |
requires careful work throughout the system. |
| 1000 |
|
| 1001 |
Failure to trap on authentication failure |
| 1002 |
+++++++++++++++++++++++++++++++++++++++++ |
| 1003 |
|
| 1004 |
Any failure to halt the program on an authentication failure is likely to be |
| 1005 |
exploitable by attackers to create an |
| 1006 |
:ref:`authentication oracle<Authentication oracles>`. |
| 1007 |
|
| 1008 |
There are several different ways to introduce this problem: |
| 1009 |
|
| 1010 |
- The implementation might try to halt the program in some way that can be |
| 1011 |
intercepted. |
| 1012 |
|
| 1013 |
For example, the Armv8.3 ``aut`` instructions do not directly trap on |
| 1014 |
authentication failure on processors that lack the ``FPAC`` extension. |
| 1015 |
Instead, they corrupt their results to be invalid pointers, with the idea that |
| 1016 |
subsequent uses of those pointers will trigger traps as bad memory accesses. |
| 1017 |
However, most kernels do not immediately halt programs that trap due to bad |
| 1018 |
memory accesses; instead, they notify the process to give it an opportunity to |
| 1019 |
recover. If this happens with an ``auth`` failure, the attacker may be able to |
| 1020 |
exploit the recovery path in a way that creates an oracle. Kernels must |
| 1021 |
provide a way for a process to trap unrecoverably, and this should cover all |
| 1022 |
``FPAC`` traps. Compilers must ensure that ``auth`` failures trigger an |
| 1023 |
unrecoverable trap, ideally by taking advantage of ``FPAC``, but if necessary |
| 1024 |
by emitting extra instructions. |
| 1025 |
|
| 1026 |
- A compiler might use an intermediate representation (IR) for ``sign`` and |
| 1027 |
``auth`` operations that cannot make adequate correctness guarantees. |
| 1028 |
|
| 1029 |
For example, suppose that an IR uses ARMv8.3-like semantics for ``auth``: the |
| 1030 |
operation merely corrupts its result on failure instead of promising to trap. |
| 1031 |
A frontend might emit patterns of IR that always follow an ``auth`` with a |
| 1032 |
memory access, thinking that this ensures correctness. But if the IR can be |
| 1033 |
transformed to insert code between the ``auth`` and the access, or if the |
| 1034 |
``auth`` can be speculated, then this potentially creates an oracle. It is |
| 1035 |
better for ``auth`` to semantically guarantee to trap, potentially requiring |
| 1036 |
an explicit check in the generated code. An ARMv8.3-like target can avoid this |
| 1037 |
explicit check in the common case by recognizing the pattern of an ``auth`` |
| 1038 |
followed immediately by an access. |
| 1039 |
|
| 1040 |
Attackable code sequences |
| 1041 |
+++++++++++++++++++++++++ |
| 1042 |
|
| 1043 |
If code that is part of a pointer authentication operation is interleaved with |
| 1044 |
code that may itself be vulnerable to attacks, an attacker may be able to use |
| 1045 |
this to create a :ref:`signing<Signing oracles>` or |
| 1046 |
:ref:`authentication<Authentication oracles>` oracle. |
| 1047 |
|
| 1048 |
For example, suppose that the compiler is generating a call to a function and |
| 1049 |
passing two arguments: a signed constant pointer and a value derived from a |
| 1050 |
call. In ARMv8.3, this code might look like so: |
| 1051 |
|
| 1052 |
.. code-block:: asm |
| 1053 |
|
| 1054 |
adr x19, _callback. ; compute &_callback |
| 1055 |
paciza x19 ; sign it with a constant discriminator of 0 |
| 1056 |
blr _argGenerator ; call _argGenerator() (returns in x0) |
| 1057 |
mov x1, x0 ; move call result to second arg register |
| 1058 |
mov x0, x19 ; move signed &_callback to first arg register |
| 1059 |
blr _function ; call _function |
| 1060 |
|
| 1061 |
This code is correct, as would be a sequencing that does *both* the ``adr`` and |
| 1062 |
the ``paciza`` after the call to ``_argGenerator``. But a sequence that |
| 1063 |
computes the address of ``_callback`` but leaves it as a raw pointer in a |
| 1064 |
register during the call to ``_argGenerator`` would be vulnerable: |
| 1065 |
|
| 1066 |
.. code-block:: asm |
| 1067 |
|
| 1068 |
adr x19, _callback. ; compute &_callback |
| 1069 |
blr _argGenerator ; call _argGenerator() (returns in x0) |
| 1070 |
mov x1, x0 ; move call result to second arg register |
| 1071 |
paciza x19 ; sign &_callback |
| 1072 |
mov x0, x19 ; move signed &_callback to first arg register |
| 1073 |
blr _function ; call _function |
| 1074 |
|
| 1075 |
If ``_argGenerator`` spills ``x19`` (a callee-save register), and if the |
| 1076 |
attacker can perform a write during this call, then the attacker can overwrite |
| 1077 |
the spill slot with an arbitrary pointer that will eventually be unconditionally |
| 1078 |
signed after the function returns. This would be a signing oracle. |
| 1079 |
|
| 1080 |
The implementation can avoid this by obeying two basic rules: |
| 1081 |
|
| 1082 |
- The compiler's intermediate representations (IR) should not provide operations |
| 1083 |
that expose intermediate raw pointers. This may require providing extra |
| 1084 |
operations that perform useful combinations of operations. |
| 1085 |
|
| 1086 |
For example, there should be an "atomic" auth-and-resign operation that should |
| 1087 |
be used instead of emitting an ``auth`` operation whose result is fed into a |
| 1088 |
``sign``. |
| 1089 |
|
| 1090 |
Similarly, if a pointer should be authenticated as part of doing a memory |
| 1091 |
access or a call, then the access or call should be decorated with enough |
| 1092 |
information to perform the authentication; there should not be a separate |
| 1093 |
``auth`` whose result is used as the pointer operand for the access or call. |
| 1094 |
(In LLVM IR, we do this for calls, but not yet for loads or stores.) |
| 1095 |
|
| 1096 |
"Operations" includes things like materializing a signed value to a known |
| 1097 |
function or global variable. The compiler must be able to recognize and emit |
| 1098 |
this as a unified operation, rather than potentially splitting it up as in |
| 1099 |
the example above. |
| 1100 |
|
| 1101 |
- The compiler backend should not be too aggressive about scheduling |
| 1102 |
instructions that are part of a pointer authentication operation. This may |
| 1103 |
require custom code-generation of these operations in some cases. |
| 1104 |
|
| 1105 |
Register clobbering |
| 1106 |
+++++++++++++++++++ |
| 1107 |
|
| 1108 |
As a refinement of the section on `Attackable code sequences`_, if the attacker |
| 1109 |
has the ability to modify arbitrary *register* state at arbitrary points in the |
| 1110 |
program, then special care must be taken. |
| 1111 |
|
| 1112 |
For example, ARMv8.3 might materialize a signed function pointer like so: |
| 1113 |
|
| 1114 |
.. code-block:: asm |
| 1115 |
|
| 1116 |
adr x0, _callback. ; compute &_callback |
| 1117 |
paciza x0 ; sign it with a constant discriminator of 0 |
| 1118 |
|
| 1119 |
If an attacker has the ability to overwrite ``x0`` between these two |
| 1120 |
instructions, this code sequence is vulnerable to becoming a signing oracle. |
| 1121 |
|
| 1122 |
For the most part, this sort of attack is not possible: it is a basic element of |
| 1123 |
the design of modern computation that register state is private and inviolable. |
| 1124 |
However, in systems that support asynchronous interrupts, this property requires |
| 1125 |
the cooperation of the interrupt-handling code. If that code saves register |
| 1126 |
state to memory, and that memory can be overwritten by an attacker, then |
| 1127 |
essentially the attack can overwrite arbitrary register state at an arbitrary |
| 1128 |
point. This could be a concern if the threat model includes attacks on the |
| 1129 |
kernel or if the program uses user-space preemptive multitasking. |
| 1130 |
|
| 1131 |
(Readers might object that an attacker cannot rely on asynchronous interrupts |
| 1132 |
triggering at an exact instruction boundary. In fact, researchers have had some |
| 1133 |
success in doing exactly that. Even ignoring that, though, we should aim to |
| 1134 |
protect against lucky attackers just as much as good ones.) |
| 1135 |
|
| 1136 |
To protect against this, saved register state must be at least partially signed |
| 1137 |
(using something like `ptrauth_sign_generic_data`_). This is required for |
| 1138 |
correctness anyway because saved thread states include security-critical |
| 1139 |
registers such as SP, FP, PC, and LR (where applicable). Ideally, this |
| 1140 |
signature would cover all the registers, but since saving and restoring |
| 1141 |
registers can be very performance-sensitive, that may not be acceptable. It is |
| 1142 |
sufficient to set aside a small number of scratch registers that will be |
| 1143 |
guaranteed to be preserved correctly; the compiler can then be careful to only |
| 1144 |
store critical values like intermediate raw pointers in those registers. |
| 1145 |
|
| 1146 |
``setjmp`` and ``longjmp`` should sign and authenticate the core registers (SP, |
| 1147 |
FP, PC, and LR), but they do not need to worry about intermediate values because |
| 1148 |
``setjmp`` can only be called synchronously, and the compiler should never |
| 1149 |
schedule pointer-authentication operations interleaved with arbitrary calls. |
| 1150 |
|
| 1151 |
.. _Relative addresses: |
| 1152 |
|
| 1153 |
Attacks on relative addressing |
| 1154 |
++++++++++++++++++++++++++++++ |
| 1155 |
|
| 1156 |
Relative addressing is a technique used to compress and reduce the load-time |
| 1157 |
cost of infrequently-used global data. The pointer authentication system is |
| 1158 |
unlikely to support signing or authenticating a relative address, and in most |
| 1159 |
cases it would defeat the point to do so: it would take additional storage |
| 1160 |
space, and applying the signature would take extra work at load time. |
| 1161 |
|
| 1162 |
Relative addressing is not precluded by the use of pointer authentication, but |
| 1163 |
it does take extra considerations to make it secure: |
| 1164 |
|
| 1165 |
- Relative addresses must only be stored in read-only memory. A writable |
| 1166 |
relative address can be overwritten to point nearly anywhere, making it |
| 1167 |
inherently insecure; this danger can only be compensated for with techniques |
| 1168 |
for protecting arbitrary data like `ptrauth_sign_generic_data`_. |
| 1169 |
|
| 1170 |
- Relative addresses must only be accessed through signed pointers with adequate |
| 1171 |
diversity. If an attacker can perform an `access path attack` to replace the |
| 1172 |
pointer through which the relative address is accessed, they can easily cause |
| 1173 |
the relative address to point wherever they want. |
| 1174 |
|
| 1175 |
Signature forging |
| 1176 |
+++++++++++++++++ |
| 1177 |
|
| 1178 |
If an attacker can exactly reproduce the behavior of the signing algorithm, and |
| 1179 |
they know all the correct inputs to it, then they can perfectly forge a |
| 1180 |
signature on an arbitrary pointer. |
| 1181 |
|
| 1182 |
There are three components to avoiding this mistake: |
| 1183 |
|
| 1184 |
- The abstract signing algorithm should be good: it should not have glaring |
| 1185 |
flaws which would allow attackers to predict its result with better than |
| 1186 |
random accuracy without knowing all the inputs (like the key values). |
| 1187 |
|
| 1188 |
- The key values should be kept secret. If at all possible, they should never |
| 1189 |
be stored in accessible memory, or perhaps only stored encrypted. |
| 1190 |
|
| 1191 |
- Contexts that are meant to be independently protected should use different |
| 1192 |
key values. For example, the kernel should not use the same keys as user |
| 1193 |
processes. Different user processes should also use different keys from each |
| 1194 |
other as much as possible, although this may pose its own technical |
| 1195 |
challenges. |
| 1196 |
|
| 1197 |
Remapping |
| 1198 |
+++++++++ |
| 1199 |
|
| 1200 |
If an attacker can change the memory protections on certain pages of the |
| 1201 |
program's memory, that can substantially weaken the protections afforded by |
| 1202 |
pointer authentication. |
| 1203 |
|
| 1204 |
- If an attacker can inject their own executable code, they can also certainly |
| 1205 |
inject code that can be used as a :ref:`signing oracle<Signing Oracles>`. |
| 1206 |
The same is true if they can write to the instruction stream. |
| 1207 |
|
| 1208 |
- If an attacker can remap read-only program data sections to be writable, then |
| 1209 |
any use of :ref:`relative addresses` in global data becomes insecure. |
| 1210 |
|
| 1211 |
- On platforms that use them, if an attacker can remap the memory containing |
| 1212 |
the `global offset tables`_ as writable, then any unsigned pointers in those |
| 1213 |
tables are insecure. |
| 1214 |
|
| 1215 |
Remapping memory in this way often requires the attacker to have already |
| 1216 |
substantively subverted the control flow of the process. Nonetheless, if the |
| 1217 |
operating system has a mechanism for mapping pages in a way that cannot be |
| 1218 |
remapped, this should be used wherever possible. |
| 1219 |
|
| 1220 |
.. _Safe Derivation: |
| 1221 |
|
| 1222 |
Safe derivation |
| 1223 |
~~~~~~~~~~~~~~~ |
| 1224 |
|
| 1225 |
Whether a data pointer is stored, even briefly, as a raw pointer can affect the |
| 1226 |
security-correctness of a program. (Function pointers are never implicitly |
| 1227 |
stored as raw pointers; raw pointers to functions can only be produced with the |
| 1228 |
``<ptrauth.h>`` intrinsics.) Repeated re-signing can also impact performance. |
| 1229 |
Clang makes a modest set of guarantees in this area: |
| 1230 |
|
| 1231 |
- An expression of pointer type is said to be **safely derived** if: |
| 1232 |
|
| 1233 |
- it takes the address of a global variable or function, or |
| 1234 |
|
| 1235 |
- it is a load from a gl-value of ``__ptrauth``-qualified type, or |
| 1236 |
|
| 1237 |
- it is a load from read-only memory that has been initialized from a safely |
| 1238 |
derived source, such as the `data const` section of a binary or library. |
| 1239 |
|
| 1240 |
- If a value that is safely derived is assigned to a ``__ptrauth``-qualified |
| 1241 |
object, including by initialization, then the value will be directly signed as |
| 1242 |
appropriate for the target qualifier and will not be stored as a raw pointer. |
| 1243 |
|
| 1244 |
- If the function expression of a call is a gl-value of ``__ptrauth``-qualified |
| 1245 |
type, then the call will be authenticated directly according to the source |
| 1246 |
qualifier and will not be resigned to the default rule for a function pointer |
| 1247 |
of its type. |
| 1248 |
|
| 1249 |
These guarantees are known to be inadequate for data pointer security. In |
| 1250 |
particular, Clang should be enhanced to make the following guarantees: |
| 1251 |
|
| 1252 |
- A pointer should additionally be considered safely derived if it is: |
| 1253 |
|
| 1254 |
- the address of a gl-value that is safely derived, |
| 1255 |
|
| 1256 |
- the result of pointer arithmetic on a pointer that is safely derived (with |
| 1257 |
some restrictions on the integer operand), |
| 1258 |
|
| 1259 |
- the result of a comma operator where the second operand is safely derived, |
| 1260 |
|
| 1261 |
- the result of a conditional operator where the selected operand is safely |
| 1262 |
derived, or |
| 1263 |
|
| 1264 |
- the result of loading from a safely derived gl-value. |
| 1265 |
|
| 1266 |
- A gl-value should be considered safely derived if it is: |
| 1267 |
|
| 1268 |
- a dereference of a safely derived pointer, |
| 1269 |
|
| 1270 |
- a member access into a safely derived gl-value, or |
| 1271 |
|
| 1272 |
- a reference to a variable. |
| 1273 |
|
| 1274 |
- An access to a safely derived gl-value should be guaranteed to not allow |
| 1275 |
replacement of any of the safely-derived component values at any point in the |
| 1276 |
access. "Access" should include loading a function pointer. |
| 1277 |
|
| 1278 |
- Assignments should include pointer-arithmetic operators like ``+=``. |
| 1279 |
|
| 1280 |
Making these guarantees will require further work, including significant new |
| 1281 |
support in LLVM IR. |
| 1282 |
|
| 1283 |
Furthermore, Clang should implement a warning when assigning a data pointer that |
| 1284 |
is not safely derived to a ``__ptrauth``-qualified gl-value. |
| 1285 |
|
| 1286 |
|
| 1287 |
Language ABI |
| 1288 |
------------ |
| 1289 |
|
| 1290 |
This section describes the pointer-authentication ABI currently implemented in |
| 1291 |
Clang for the Apple arm64e target. As other targets adopt pointer |
| 1292 |
authentication, this section should be generalized to express their ABIs as |
| 1293 |
well. |
| 1294 |
|
| 1295 |
Key assignments |
| 1296 |
~~~~~~~~~~~~~~~ |
| 1297 |
|
| 1298 |
ARMv8.3 provides four abstract signing keys: ``IA``, ``IB``, ``DA``, and ``DB``. |
| 1299 |
The architecture designates ``IA`` and ``IB`` for signing code pointers and |
| 1300 |
``DA`` and ``DB`` for signing data pointers; this is reinforced by two |
| 1301 |
properties: |
| 1302 |
|
| 1303 |
- The ISA provides instructions that perform combined auth+call and auth+load |
| 1304 |
operations; these instructions can only use the ``I`` keys and ``D`` keys, |
| 1305 |
respectively. |
| 1306 |
|
| 1307 |
- AArch64's TBI feature can be separately enabled for code pointers (controlling |
| 1308 |
whether indirect-branch instructions ignore those bits) and data pointers |
| 1309 |
(controlling whether memory-access instructions) ignore those bits. If TBI is |
| 1310 |
enabled for a kind of pointer, the sign and auth operations preserve the TBI |
| 1311 |
bits when signing with an associated keys (at the cost of shrinking the number |
| 1312 |
of available signing bits by 8). |
| 1313 |
|
| 1314 |
arm64e then further subdivides the keys as follows: |
| 1315 |
|
| 1316 |
- The ``A`` keys are used for primarily "global" purposes like signing v-tables |
| 1317 |
and function pointers. These keys are sometimes called *process-independent* |
| 1318 |
or *cross-process* because on existing OSes they are not changed when changing |
| 1319 |
processes, although this is not a platform guarantee. |
| 1320 |
|
| 1321 |
- The ``B`` keys are used for primarily "local" purposes like signing return |
| 1322 |
addresses. These keys are sometimes called *process-specific* because they |
| 1323 |
are typically different between processes. However, they are in fact shared |
| 1324 |
across processes in one situation: systems which provide ``fork`` cannot |
| 1325 |
change these keys in the child process; they can only be changed during |
| 1326 |
``exec``. |
| 1327 |
|
| 1328 |
Implementation-defined algorithms and quantities |
| 1329 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1330 |
|
| 1331 |
The cryptographic hash algorithm used to compute signatures in ARMv8.3 is a |
| 1332 |
private detail of the hardware implementation. |
| 1333 |
|
| 1334 |
arm64e restricts constant discriminators (used in ``__ptrauth`` and |
| 1335 |
``ptrauth_blend_discriminator``) to the range from 0 to 65535, inclusive. A 0 |
| 1336 |
discriminator generally signifies that no blending is required; see the |
| 1337 |
documentation for ``ptrauth_blend_discriminator``. This range is somewhat |
| 1338 |
narrow but has two advantages: |
| 1339 |
|
| 1340 |
- The AArch64 ISA allows an arbitrary 16-bit immediate to be written over the |
| 1341 |
top 16 bits of a register in a single instruction: |
| 1342 |
|
| 1343 |
.. code-block:: asm |
| 1344 |
|
| 1345 |
movk xN, #0x4849, LSL 48 |
| 1346 |
|
| 1347 |
This is ideal for the discriminator blending operation because it adds minimal |
| 1348 |
code-size overhead and avoids overwriting any interesting bits from the |
| 1349 |
pointer. Blending in a wider constant discriminator would either clobber |
| 1350 |
interesting bits (e.g. if it was loaded with ``movk xN, #0x4c4f, LSL 32``) or |
| 1351 |
require significantly more code (e.g. if the discriminator was loaded with a |
| 1352 |
``mov+bfi`` sequence). |
| 1353 |
|
| 1354 |
- It is possible to pack a 16-bit discriminator into loader metadata with |
| 1355 |
minimal compromises, whereas a wider discriminator would require extra |
| 1356 |
metadata storage and therefore significantly impact load times. |
| 1357 |
|
| 1358 |
The string hash used by ``ptrauth_string_discriminator`` is a 64-bit SipHash-2-4 |
| 1359 |
using the constant seed ``b5d4c9eb79104a796fec8b1b428781d4`` (big-endian), with |
| 1360 |
the result reduced by modulo to the range of non-zero discriminators (i.e. |
| 1361 |
``(rawHash % 65535) + 1``). |
| 1362 |
|
| 1363 |
Return addresses |
| 1364 |
~~~~~~~~~~~~~~~~ |
| 1365 |
|
| 1366 |
The kernel must ensure that attackers cannot replace LR due to an asynchronous |
| 1367 |
exception; see `Register clobbering`_. If this is done by generally protecting |
| 1368 |
LR, then functions which don't spill LR to the stack can avoid signing it |
| 1369 |
entirely. Otherwise, the return address must be signed; on arm64e it is signed |
| 1370 |
with the ``IB`` key using the stack pointer on entry as the discriminator. |
| 1371 |
|
| 1372 |
Protecting return addresses is of such particular importance that the ``IB`` key |
| 1373 |
is almost entirely reserved for this purpose. |
| 1374 |
|
| 1375 |
Global offset tables |
| 1376 |
~~~~~~~~~~~~~~~~~~~~ |
| 1377 |
|
| 1378 |
The global offset table (GOT) is not part of the language ABI, but it is a |
| 1379 |
common implementation technique for dynamic linking which deserves special |
| 1380 |
discussion here. |
| 1381 |
|
| 1382 |
Whenever possible, signed pointers should be materialized directly in code |
| 1383 |
rather than via the GOT, e.g. using an ``adrp+add+pac`` sequence on ARMv8.3. |
| 1384 |
This decreases the amount of work necessary at load time to initialize the GOT, |
| 1385 |
but more importantly, it defines away the potential for several attacks: |
| 1386 |
|
| 1387 |
- Attackers cannot change instructions, so there is no way to cause this code |
| 1388 |
sequence to materialize a different pointer, whereas an access via the GOT |
| 1389 |
always has *at minimum* a probabilistic chance to be the target of successful |
| 1390 |
`substitution attacks`_. |
| 1391 |
|
| 1392 |
- The GOT is a dense pool of fixed pointers at a fixed offset relative to code; |
| 1393 |
attackers can search this pool for useful pointers that can be used in |
| 1394 |
`substitution attacks`_, whereas pointers that are only materialized directly |
| 1395 |
are not so easily available. |
| 1396 |
|
| 1397 |
- Similarly, attackers can use `access path attacks`_ to replace a pointer to a |
| 1398 |
signed pointer with a pointer to the GOT if the signing schema used within the |
| 1399 |
GOT happens to be the same as the original pointer. This kind of collision |
| 1400 |
becomes much less likely to be useful the fewer pointers are in the GOT in the |
| 1401 |
first place. |
| 1402 |
|
| 1403 |
If this can be done for a symbol, then the compiler need only ensure that it |
| 1404 |
materializes the signed pointer using registers that are safe against |
| 1405 |
`register clobbering`_. |
| 1406 |
|
| 1407 |
However, many symbols can only be accessed via the GOT, e.g. because they |
| 1408 |
resolve to definitions outside of the current image. In this case, care must |
| 1409 |
be taken to ensure that using the GOT does not introduce weaknesses. |
| 1410 |
|
| 1411 |
- If the entire GOT can be mapped read-only after loading, then no signing is |
| 1412 |
required within the GOT. In fact, not signing pointers in the GOT is |
| 1413 |
preferable in this case because it makes the GOT useless for the harvesting |
| 1414 |
and access-path attacks above. Storing raw pointers in this way is usually |
| 1415 |
extremely unsafe, but for the special case of an immutable GOT entry it's fine |
| 1416 |
because the GOT is always accessed via an address that is directly |
| 1417 |
materialized in code and thus provably unattackable. (But see `Remapping`_.) |
| 1418 |
|
| 1419 |
- Otherwise, GOT entries which are used for producing a signed pointer constant |
| 1420 |
must be signed. The signing schema used in the GOT need not match the target |
| 1421 |
signing schema for the signed constant. To counteract the threats of |
| 1422 |
substitution attacks, it's best if GOT entries can be signed with address |
| 1423 |
diversity. Using a good constant discriminator as well (perhaps derived from |
| 1424 |
the symbol name) can make it less useful to use a pointer to the GOT as the |
| 1425 |
replacement in an :ref:`access path attack<Access path attacks>`. |
| 1426 |
|
| 1427 |
In either case, the compiler must ensure that materializing the address of a GOT |
| 1428 |
entry as part of producing a signed pointer constant is not vulnerable to |
| 1429 |
`register clobbering`_. If the linker also generates code for this, e.g. for |
| 1430 |
call stubs, this generated code must take the same precautions. |
| 1431 |
|
| 1432 |
Dynamic symbol lookup |
| 1433 |
~~~~~~~~~~~~~~~~~~~~~ |
| 1434 |
|
| 1435 |
On platforms that support dynamically loading or resolving symbols it is |
| 1436 |
necessary for them to define the pointer authentication semantics of the APIs |
| 1437 |
provided to perform such lookups. While the platform may choose to reply |
| 1438 |
unsigned pointers from such function and rely on the caller performing the |
| 1439 |
initial signing, doing so creates the opportunity for caller side errors that |
| 1440 |
create :ref:`signing oracles<Signing Oracles>`. |
| 1441 |
|
| 1442 |
On arm64e the `dlsym` function is used to resolve a symbol at runtime. If the |
| 1443 |
resolved symbol is a function or other code pointer the returned pointer is |
| 1444 |
signed using the default function signing schema described in |
| 1445 |
:ref:`C function pointers<C function abi>`. If the resolved symbol is not a code pointer it is |
| 1446 |
returned as an unsigned pointer. |
| 1447 |
|
| 1448 |
.. _C function abi: |
| 1449 |
|
| 1450 |
C function pointers |
| 1451 |
~~~~~~~~~~~~~~~~~~~ |
| 1452 |
|
| 1453 |
On arm64e, C function pointers are currently signed with the ``IA`` key without |
| 1454 |
address diversity and with a constant discriminator of 0. |
| 1455 |
|
| 1456 |
The C and C++ standards do not permit C function pointers to be signed with |
| 1457 |
address diversity by default: in C++ terms, function pointer types are required |
| 1458 |
to be trivially copyable, which means they must be copyable with ``memcpy``. |
| 1459 |
|
| 1460 |
The use of a uniform constant discriminator greatly simplifies the adoption of |
| 1461 |
arm64e, but it is a significant weakness in the mitigation because it allows any |
| 1462 |
C function pointer to be replaced with another. Clang supports |
| 1463 |
`-fptrauth-function-pointer-type-discrimination`, which enables a variant ABI |
| 1464 |
that uses type discrimination for function pointers. When generating the type |
| 1465 |
based discriminator for a function type all primitive integer types are |
| 1466 |
considered equivalent due to the prevalence of mismatching integer parameter |
| 1467 |
types in real world code. Type discrimination of function pointers is |
| 1468 |
ABI-incompatible with the standard arm64e ABI, but it can be used in constrained |
| 1469 |
contexts such as embedded systems or in code that does not require function |
| 1470 |
pointer interoperation with the standard ABI (e.g. because it does not pass |
| 1471 |
function pointers back and forth, or only does so through |
| 1472 |
``__ptrauth``-qualified l-values). |
| 1473 |
|
| 1474 |
C++ virtual tables |
| 1475 |
~~~~~~~~~~~~~~~~~~ |
| 1476 |
|
| 1477 |
By default the pointer to a C++ virtual table is currently signed with the |
| 1478 |
``DA`` key, address diversity, and a constant discriminator equal to the string |
| 1479 |
hash (see `ptrauth_string_discriminator`_) of the mangled v-table identifier |
| 1480 |
of the primary base class for the v-table. To support existing code or ABI |
| 1481 |
constraints it is possible to use the `ptrauth_vtable_pointer` attribute to |
| 1482 |
override the schema used for the v-table pointer of the base type of |
| 1483 |
polymorphic class hierarchy. This attribute permits the configuration of the |
| 1484 |
key, address diversity mode, and any extra constant discriminator to be used. |
| 1485 |
|
| 1486 |
Virtual functions in a C++ virtual table are signed with the ``IA`` key, address |
| 1487 |
diversity, and a constant discriminator equal to the string hash (see |
| 1488 |
`ptrauth_string_discriminator`_) of the mangled name of the function which |
| 1489 |
originally gave rise to the v-table slot. |
| 1490 |
|
| 1491 |
C++ dynamic_cast |
| 1492 |
~~~~~~~~~~~~~~~~ |
| 1493 |
|
| 1494 |
C++'s ``dynamic_cast`` presents a difficulty relative to other polymorphic |
| 1495 |
languages that have a |
| 1496 |
`top type <https://en.wikipedia.org/wiki/Any_type>` as the use of declaration |
| 1497 |
diversity for v-table pointers results in distinct signing schemas for each |
| 1498 |
isolated type hierarchy. As a result it is not possible for the Itanium ABI |
| 1499 |
defined ``__dynamic_cast`` entry point to directly authenticate the v-table |
| 1500 |
pointer of the provided object. |
| 1501 |
|
| 1502 |
The current implementation uses a forced authentication of the subject object's |
| 1503 |
v-table prior to invoking ``__dynamic_cast`` to partially verify that the |
| 1504 |
object's vtable is valid. The ``__dynamic_cast`` implementation currently relies |
| 1505 |
on this caller side check to limit the substitutability of the v-table pointer |
| 1506 |
with an incorrect or invalid v-table. The subsequent implementation of the |
| 1507 |
dynamic cast algorithm is built on pointer auth protected ``type_info`` objects. |
| 1508 |
|
| 1509 |
In future a richer solution may be developed to support vending the correct |
| 1510 |
authentication schema directly to the ``dynamic_cast`` implementation. |
| 1511 |
|
| 1512 |
C++ std::type_info v-table pointers |
| 1513 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1514 |
|
| 1515 |
The v-table pointer of the ``std::type_info`` type is signed with the ``DA`` key |
| 1516 |
and no additional diversity. |
| 1517 |
|
| 1518 |
C++ member function pointers |
| 1519 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1520 |
|
| 1521 |
A member function pointer is signed with the ``IA`` key, no address diversity, |
| 1522 |
and a constant discriminator equal to the string hash |
| 1523 |
(see `ptrauth_string_discriminator`_) of the member pointer type. Address |
| 1524 |
diversity is not permitted by C++ for member function pointers because they must |
| 1525 |
be trivially-copyable types. |
| 1526 |
|
| 1527 |
The Itanium C++ ABI specifies that member function pointers to virtual functions |
| 1528 |
simply store an offset to the correct v-table slot. This ABI cannot be used |
| 1529 |
securely with pointer authentication because there is no safe place to store the |
| 1530 |
constant discriminator for the target v-table slot: if it's stored with the |
| 1531 |
offset, an attacker can simply overwrite it with the right discriminator for the |
| 1532 |
offset. Even if the programmer never uses pointers to virtual functions, the |
| 1533 |
existence of this code path makes all member function pointer dereferences |
| 1534 |
insecure. |
| 1535 |
|
| 1536 |
arm64e changes this ABI so that virtual function pointers are stored using |
| 1537 |
dispatch thunks with vague linkage. Because arm64e supports interoperation with |
| 1538 |
``arm64`` code when pointer authentication is disabled, an arm64e member |
| 1539 |
function pointer dereference still recognizes the virtual-function |
| 1540 |
representation but uses an bogus discriminator on that path that should always |
| 1541 |
trap if pointer authentication is enabled dynamically. |
| 1542 |
|
| 1543 |
The use of dispatch thunks means that ``==`` on member function pointers is no |
| 1544 |
longer reliable for virtual functions, but this is acceptable because the |
| 1545 |
standard makes no guarantees about it in the first place. |
| 1546 |
|
| 1547 |
The use of dispatch thunks also is required to support declaration specific |
| 1548 |
authentication schemas for v-table pointers. |
| 1549 |
|
| 1550 |
C++ mangling |
| 1551 |
~~~~~~~~~~~~ |
| 1552 |
|
| 1553 |
When the ``__ptrauth`` qualifier appears in a C++ mangled name, |
| 1554 |
it is mangled as a vendor qualifier with the signature |
| 1555 |
``U9__ptrauthILj<key>ELb<addressDiscriminated>ELj<extraDiscriminator>EE``. |
| 1556 |
|
| 1557 |
e.g. ``int * __ptrauth(1, 0, 1234)`` will be mangled as |
| 1558 |
``U9__ptrauthILj1ELb0ELj1234EE``. |
| 1559 |
|
| 1560 |
If the vtable pointer authentication scheme of a polymorphic class is overridden |
| 1561 |
we mangle the override information with the vendor qualifier |
| 1562 |
``__vtptrauth(int key, bool addressDiscriminated, unsigned extraDiscriminator)``, |
| 1563 |
where the extra discriminator is the explicit value the specified discrimination |
| 1564 |
mode evalutes to. |
| 1565 |
|
| 1566 |
Blocks |
| 1567 |
~~~~~~ |
| 1568 |
|
| 1569 |
Block pointers are data pointers which must interoperate with the ObjC `id` type |
| 1570 |
and therefore cannot be signed themselves. As blocks conform to the ObjC `id` |
| 1571 |
type, they contain an ``isa`` pointer signed as described |
| 1572 |
:ref:`below<Objc isa and super>`. |
| 1573 |
|
| 1574 |
The invocation pointer in a block is signed with the ``IA`` key using address |
| 1575 |
diversity and a constant discriminator of 0. Using a uniform discriminator is |
| 1576 |
seen as a weakness to be potentially improved, but this is tricky due to the |
| 1577 |
subtype polymorphism directly permitted for blocks. |
| 1578 |
|
| 1579 |
Block descriptors and ``__block`` variables can contain pointers to functions |
| 1580 |
that can be used to copy or destroy the object. These functions are signed with |
| 1581 |
the ``IA`` key, address diversity, and a constant discriminator of 0. The |
| 1582 |
structure of block descriptors is under consideration for improvement. |
| 1583 |
|
| 1584 |
Objective-C runtime |
| 1585 |
~~~~~~~~~~~~~~~~~~~ |
| 1586 |
|
| 1587 |
In addition to the compile time ABI design, the Objective-C runtime provides |
| 1588 |
additional protection to methods and other metadata that have been loaded into |
| 1589 |
the Objective-C method cache; this protection is private to the runtime. |
| 1590 |
|
| 1591 |
Objective-C methods |
| 1592 |
~~~~~~~~~~~~~~~~~~~ |
| 1593 |
|
| 1594 |
Objective-C method lists sign methods with the ``IA`` key using address |
| 1595 |
diversity and a constant discriminator of 0. Using a uniform constant |
| 1596 |
discriminator is believed to be acceptable because these tables are only |
| 1597 |
accessed internally to the Objective-C runtime. |
| 1598 |
|
| 1599 |
Objective-C class method list pointer |
| 1600 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1601 |
|
| 1602 |
The method list pointer in Objective-C classes are signed with the ``DA`` key |
| 1603 |
using address diversity, and a constant discriminator of 0xC310. |
| 1604 |
|
| 1605 |
Objective-C class read-only data pointer |
| 1606 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1607 |
|
| 1608 |
The read-only data pointer in Objective-C classes are signed with the ``DA`` key |
| 1609 |
using address diversity, and a constant discriminator of 0x61F8. |
| 1610 |
|
| 1611 |
.. _Objc isa and super: |
| 1612 |
|
| 1613 |
Objective-C ``isa`` and ``super`` pointers |
| 1614 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1615 |
|
| 1616 |
An Objective-C object's ``isa`` and ``super`` pointers are both signed with |
| 1617 |
the ``DA`` key using address diversity and constant discriminators of 0x6AE1 |
| 1618 |
and 0x25DA respectively. |
| 1619 |
|
| 1620 |
Objective-C ``SEL`` pointers |
| 1621 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1622 |
|
| 1623 |
By default, the type of an Objective-C instance variable of type ``SEL``, when |
| 1624 |
the qualifiers do not include an explicit ``__ptrauth`` qualifier, is adjusted |
| 1625 |
to be qualified with ``__ptrauth(ptrauth_key_asdb, 1, 0x57C2)``. |
| 1626 |
|
| 1627 |
This provides a measure of implicit at-rest protection to Objective-C classes |
| 1628 |
that store selectors, as in the common target-action design pattern. This |
| 1629 |
prevents attackers from overriding the selector to invoke an arbitrary different |
| 1630 |
method, which is a major attack vector in Objective-C. Since ``SEL`` values are |
| 1631 |
not normally passed around as signed pointers, there is a |
| 1632 |
:ref:`signing oracle<Signing Oracles>` associated with the initialization of the |
| 1633 |
ivar, but the use of address and constant diversity limit the risks. |
| 1634 |
|
| 1635 |
The implicit qualifier means that the type of the ivar does not match its |
| 1636 |
declaration, which can cause type errors if the address of the ivar is taken: |
| 1637 |
|
| 1638 |
.. code-block:: ObjC |
| 1639 |
|
| 1640 |
@interface A : NSObject { |
| 1641 |
SEL _s; |
| 1642 |
} |
| 1643 |
@end |
| 1644 |
|
| 1645 |
void f(SEL *); |
| 1646 |
|
| 1647 |
@implementation A |
| 1648 |
-(void)g |
| 1649 |
{ |
| 1650 |
f(&_s); |
| 1651 |
} |
| 1652 |
@end |
| 1653 |
|
| 1654 |
To fix such a mismatch the schema macro from `<ptrauth.h>`: |
| 1655 |
|
| 1656 |
.. code-block:: ObjC |
| 1657 |
|
| 1658 |
#include <ptrauth.h> |
| 1659 |
|
| 1660 |
void f(SEL __ptrauth_objc_sel*); |
| 1661 |
|
| 1662 |
or less safely, and introducing the possibility of an |
| 1663 |
:ref:`signing or authentication oracle<Signing oracles>`, an unauthenticated |
| 1664 |
temporary may be used as intermediate storage. |
| 1665 |
|
| 1666 |
Alternative implementations |
| 1667 |
--------------------------- |
| 1668 |
|
| 1669 |
Signature storage |
| 1670 |
~~~~~~~~~~~~~~~~~ |
| 1671 |
|
| 1672 |
It is not critical for the security of pointer authentication that the |
| 1673 |
signature be stored "together" with the pointer, as it is in Armv8.3. An |
| 1674 |
implementation could just as well store the signature in a separate word, so |
| 1675 |
that the ``sizeof`` a signed pointer would be larger than the ``sizeof`` a raw |
| 1676 |
pointer. |
| 1677 |
|
| 1678 |
Storing the signature in the high bits, as Armv8.3 does, has several trade-offs: |
| 1679 |
|
| 1680 |
- Disadvantage: there are substantially fewer bits available for the signature, |
| 1681 |
weakening the mitigation by making it much easier for an attacker to simply |
| 1682 |
guess the correct signature. |
| 1683 |
|
| 1684 |
- Disadvantage: future growth of the address space will necessarily further |
| 1685 |
weaken the mitigation. |
| 1686 |
|
| 1687 |
- Advantage: memory layouts don't change, so it's possible for |
| 1688 |
pointer-authentication-enabled code (for example, in a system library) to |
| 1689 |
efficiently interoperate with existing code, as long as pointer |
| 1690 |
authentication can be disabled dynamically. |
| 1691 |
|
| 1692 |
- Advantage: the size of a signed pointer doesn't grow, which might |
| 1693 |
significantly increase memory requirements, code size, and register pressure. |
| 1694 |
|
| 1695 |
- Advantage: the size of a signed pointer is the same as a raw pointer, so |
| 1696 |
generic APIs which work in types like `void *` (such as `dlsym`) can still |
| 1697 |
return signed pointers. This means that clients of these APIs will not |
| 1698 |
require insecure code in order to correctly receive a function pointer. |
| 1699 |
|
| 1700 |
Hashing vs. encrypting pointers |
| 1701 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1702 |
|
| 1703 |
Armv8.3 implements ``sign`` by computing a cryptographic hash and storing that |
| 1704 |
in the spare bits of the pointer. This means that there are relatively few |
| 1705 |
possible values for the valid signed pointer, since the bits corresponding to |
| 1706 |
the raw pointer are known. Together with an ``auth`` oracle, this can make it |
| 1707 |
computationally feasible to discover the correct signature with brute force. |
| 1708 |
(The implementation should of course endeavor not to introduce ``auth`` |
| 1709 |
oracles, but this can be difficult, and attackers can be devious.) |
| 1710 |
|
| 1711 |
If the implementation can instead *encrypt* the pointer during ``sign`` and |
| 1712 |
*decrypt* it during ``auth``, this brute-force attack becomes far less |
| 1713 |
feasible, even with an ``auth`` oracle. However, there are several problems |
| 1714 |
with this idea: |
| 1715 |
|
| 1716 |
- It's unclear whether this kind of encryption is even possible without |
| 1717 |
increasing the storage size of a signed pointer. If the storage size can be |
| 1718 |
increased, brute-force attacks can be equally well mitigated by simply storing |
| 1719 |
a larger signature. |
| 1720 |
|
| 1721 |
- It would likely be impossible to implement a ``strip`` operation, which might |
| 1722 |
make debuggers and other out-of-process tools far more difficult to write, as |
| 1723 |
well as generally making primitive debugging more challenging. |
| 1724 |
|
| 1725 |
- Implementations can benefit from being able to extract the raw pointer |
| 1726 |
immediately from a signed pointer. An Armv8.3 processor executing an |
| 1727 |
``auth``-and-load instruction can perform the load and ``auth`` in parallel; |
| 1728 |
a processor which instead encrypted the pointer would be forced to perform |
| 1729 |
these operations serially. |