| 1 | //===-- AArch64PointerAuth.h -- Harden code using PAuth ---------*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLVM_LIB_TARGET_AARCH64_AARCH64POINTERAUTH_H |
| 10 | #define LLVM_LIB_TARGET_AARCH64_AARCH64POINTERAUTH_H |
| 11 | |
| 12 | #include "Utils/AArch64BaseInfo.h" |
| 13 | |
| 14 | namespace llvm { |
| 15 | namespace AArch64PAuth { |
| 16 | |
| 17 | /// PAuth key to be used with function pointers in .init_array and .fini_array. |
| 18 | constexpr AArch64PACKey::ID InitFiniKey = AArch64PACKey::IA; |
| 19 | |
| 20 | /// Constant discriminator to be used with function pointers in .init_array and |
| 21 | /// .fini_array. The value is ptrauth_string_discriminator("init_fini") |
| 22 | constexpr unsigned InitFiniPointerConstantDiscriminator = 0xD9D4; |
| 23 | |
| 24 | /// Variants of check performed on an authenticated pointer. |
| 25 | /// |
| 26 | /// In cases such as authenticating the LR value when performing a tail call |
| 27 | /// or when re-signing a signed pointer with a different signing schema, |
| 28 | /// a failed authentication may not generate an exception on its own and may |
| 29 | /// create an authentication or signing oracle if not checked explicitly. |
| 30 | /// |
| 31 | /// A number of check methods modify control flow in a similar way by |
| 32 | /// rewriting the code |
| 33 | /// |
| 34 | /// ``` |
| 35 | /// <authenticate LR> |
| 36 | /// <more instructions> |
| 37 | /// ``` |
| 38 | /// |
| 39 | /// as follows: |
| 40 | /// |
| 41 | /// ``` |
| 42 | /// <authenticate LR> |
| 43 | /// <method-specific checker> |
| 44 | /// on_fail: |
| 45 | /// brk <code> |
| 46 | /// on_success: |
| 47 | /// <more instructions> |
| 48 | /// |
| 49 | /// ``` |
| 50 | enum class AuthCheckMethod { |
| 51 | /// Do not check the value at all |
| 52 | None, |
| 53 | |
| 54 | /// Perform a load to a temporary register |
| 55 | DummyLoad, |
| 56 | |
| 57 | /// Check by comparing bits 62 and 61 of the authenticated address. |
| 58 | /// |
| 59 | /// This method modifies control flow and inserts the following checker: |
| 60 | /// |
| 61 | /// ``` |
| 62 | /// eor Xtmp, Xn, Xn, lsl #1 |
| 63 | /// tbz Xtmp, #62, on_success |
| 64 | /// ``` |
| 65 | HighBitsNoTBI, |
| 66 | |
| 67 | /// Check by comparing the authenticated value with an XPAC-ed one without |
| 68 | /// using PAuth instructions not encoded as HINT. Can only be applied to LR. |
| 69 | /// |
| 70 | /// This method modifies control flow and inserts the following checker: |
| 71 | /// |
| 72 | /// ``` |
| 73 | /// mov Xtmp, LR |
| 74 | /// xpaclri ; encoded as "hint #7" |
| 75 | /// ; Note: at this point, the LR register contains the address as if |
| 76 | /// ; the authentication succeeded and the temporary register contains the |
| 77 | /// ; *real* result of authentication. |
| 78 | /// cmp Xtmp, LR |
| 79 | /// b.eq on_success |
| 80 | /// ``` |
| 81 | XPACHint, |
| 82 | |
| 83 | /// Similar to XPACHint but using Armv8.3-only XPAC instruction, thus |
| 84 | /// not restricted to LR: |
| 85 | /// ``` |
| 86 | /// mov Xtmp, Xn |
| 87 | /// xpac(i|d) Xn |
| 88 | /// cmp Xtmp, Xn |
| 89 | /// b.eq on_success |
| 90 | /// ``` |
| 91 | XPAC, |
| 92 | }; |
| 93 | |
| 94 | #define AUTH_CHECK_METHOD_CL_VALUES_COMMON \ |
| 95 | clEnumValN(AArch64PAuth::AuthCheckMethod::None, "none", \ |
| 96 | "Do not check authenticated address"), \ |
| 97 | clEnumValN(AArch64PAuth::AuthCheckMethod::DummyLoad, "load", \ |
| 98 | "Perform dummy load from authenticated address"), \ |
| 99 | clEnumValN( \ |
| 100 | AArch64PAuth::AuthCheckMethod::HighBitsNoTBI, "high-bits-notbi", \ |
| 101 | "Compare bits 62 and 61 of address (TBI should be disabled)"), \ |
| 102 | clEnumValN(AArch64PAuth::AuthCheckMethod::XPAC, "xpac", \ |
| 103 | "Compare with the result of XPAC (requires Armv8.3-a)") |
| 104 | |
| 105 | #define AUTH_CHECK_METHOD_CL_VALUES_LR \ |
| 106 | AUTH_CHECK_METHOD_CL_VALUES_COMMON, \ |
| 107 | clEnumValN(AArch64PAuth::AuthCheckMethod::XPACHint, "xpac-hint", \ |
| 108 | "Compare with the result of XPACLRI") |
| 109 | |
| 110 | /// Returns the number of bytes added by checkAuthenticatedRegister. |
| 111 | unsigned getCheckerSizeInBytes(AuthCheckMethod Method); |
| 112 | |
| 113 | } // end namespace AArch64PAuth |
| 114 | } // end namespace llvm |
| 115 | |
| 116 | #endif |
| 117 | |