dyld-851.27.tar.gz
[apple/dyld.git] / dyld3 / PointerAuth.h
1 /*
2 * Copyright (c) 2017 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25
26 #ifndef __DYLD_POINTER_AUTH_H__
27 #define __DYLD_POINTER_AUTH_H__
28
29 #include <ptrauth.h>
30
31 #if __has_feature(ptrauth_calls)
32 #define __ptrauth_dyld_address_auth __ptrauth(ptrauth_key_process_dependent_data, 1, 0)
33 #define __ptrauth_dyld_function_ptr __ptrauth(ptrauth_key_process_dependent_code, 1, 0)
34 #else
35 #define __ptrauth_dyld_address_auth
36 #define __ptrauth_dyld_function_ptr
37 #endif
38
39 namespace dyld3 {
40
41 // On arm64e, signs the given pointer with the address of where it is stored.
42 // Other archs just have a regular pointer
43 #pragma clang diagnostic push
44 #pragma clang diagnostic ignored "-Wptrauth-null-pointers"
45 template<typename T>
46 struct AuthenticatedValue {
47 static_assert(sizeof(T) <= sizeof(uintptr_t));
48
49 AuthenticatedValue() {
50 this->value = ptrauth_sign_unauthenticated(nullptr, ptrauth_key_process_dependent_data, this);
51 }
52 ~AuthenticatedValue() = default;
53 AuthenticatedValue(const AuthenticatedValue& other) {
54 this->value = ptrauth_auth_and_resign(other.value,
55 ptrauth_key_process_dependent_data, &other,
56 ptrauth_key_process_dependent_data, this);
57 }
58 AuthenticatedValue(AuthenticatedValue&& other) {
59 this->value = ptrauth_auth_and_resign(other.value,
60 ptrauth_key_process_dependent_data, &other,
61 ptrauth_key_process_dependent_data, this);
62 other.value = ptrauth_sign_unauthenticated(nullptr, ptrauth_key_process_dependent_data, &other);
63 }
64
65 AuthenticatedValue& operator=(const AuthenticatedValue&) = delete;
66 AuthenticatedValue& operator=(AuthenticatedValue&&) = delete;
67
68 // Add a few convenience methods for interoperating with values of the given type
69 AuthenticatedValue& operator=(const T& other) {
70 this->value = ptrauth_sign_unauthenticated(other, ptrauth_key_process_dependent_data, this);
71 return *this;
72 }
73 bool operator==(const T& other) const {
74 return ptrauth_auth_data(this->value, ptrauth_key_process_dependent_data, this) == other;
75 }
76 bool operator!=(const T& other) const {
77 return ptrauth_auth_data(this->value, ptrauth_key_process_dependent_data, this) != other;
78 }
79
80 private:
81 const void* value;
82 };
83
84
85 #pragma clang diagnostic pop
86
87 } // namespace dyld3
88
89 #endif // __DYLD_POINTER_AUTH_H__
90