clang 24.0.0git
ptrauth.h
Go to the documentation of this file.
1/*===---- ptrauth.h - Pointer authentication -------------------------------===
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
10#ifndef __PTRAUTH_H
11#define __PTRAUTH_H
12
13typedef enum {
18
19 /* A process-independent key which can be used to sign code pointers. */
21
22 /* A process-specific key which can be used to sign code pointers. */
24
25 /* A process-independent key which can be used to sign data pointers. */
27
28 /* A process-specific key which can be used to sign data pointers. */
30
31 /* The key used to sign return addresses on the stack.
32 The extra data is based on the storage address of the return address.
33 On AArch64, that is always the storage address of the return address + 8
34 (or, in other words, the value of the stack pointer on function entry) */
36
37 /* The key used to sign C function pointers.
38 The extra data is always 0. */
40
41 /* The key used to sign C++ v-table pointers.
42 The extra data is always 0. */
44
45 /* The key used to sign metadata pointers to Objective-C method-lists. */
47
48 /* The key used to sign Objective-C isa and super pointers. */
51
52 /* The key used to sign selector pointers */
54
55 /* The key used to sign Objective-C class_ro_t pointers. */
57
58 /* The key used to sign pointers in ELF .init_array/.fini_array. */
60
61 /* Other pointers signed under the ABI use private ABI rules. */
62
64
65/* An integer type of the appropriate size for a discriminator argument. */
66typedef __UINTPTR_TYPE__ ptrauth_extra_data_t;
67
68/* An integer type of the appropriate size for a generic signature. */
69typedef __UINTPTR_TYPE__ ptrauth_generic_signature_t;
70
71/* A signed pointer value embeds the original pointer together with
72 a signature that attests to the validity of that pointer. Because
73 this signature must use only "spare" bits of the pointer, a
74 signature's validity is probabilistic in practice: it is unlikely
75 but still plausible that an invalidly-derived signature will
76 somehow equal the correct signature and therefore successfully
77 authenticate. Nonetheless, this scheme provides a strong degree
78 of protection against certain kinds of attacks. */
79
80/* Authenticating a pointer that was not signed with the given key
81 and extra-data value will (likely) fail by trapping. */
82
83/* The null function pointer is always the all-zero bit pattern.
84 Signing an all-zero bit pattern will embed a (likely) non-zero
85 signature in the result, and so the result will not seem to be
86 a null function pointer. Authenticating this value will yield
87 a null function pointer back. However, authenticating an
88 all-zero bit pattern will probably fail, because the
89 authentication will expect a (likely) non-zero signature to
90 embedded in the value.
91
92 Because of this, if a pointer may validly be null, you should
93 check for null before attempting to authenticate it with one
94 of these intrinsics. This is not necessary when using the
95 __ptrauth qualifier; the compiler will perform this check
96 automatically. */
97
98#if __has_feature(ptrauth_intrinsics) || defined(__PTRAUTH__)
99
100/* Strip the signature from a value without authenticating it.
101
102 If the value is a function pointer, the result will not be a
103 legal function pointer because of the missing signature, and
104 attempting to call it will result in an authentication failure.
105
106 The value must be an expression of pointer type.
107 The key must be a constant expression of type ptrauth_key.
108 The result will have the same type as the original value. */
109#define ptrauth_strip(__value, __key) __builtin_ptrauth_strip(__value, __key)
110
111/* Blend a constant discriminator into the given pointer-like value
112 to form a new discriminator. Not all bits of the inputs are
113 guaranteed to contribute to the result.
114
115 On arm64e, the integer must fall within the range of a uint16_t;
116 other bits may be ignored.
117
118 For the purposes of ptrauth_sign_constant, the result of calling
119 this function is considered a constant expression if the arguments
120 are constant. Some restrictions may be imposed on the pointer.
121
122 The first argument must be an expression of pointer type.
123 The second argument must be an expression of integer type.
124 The result will have type uintptr_t. */
125#define ptrauth_blend_discriminator(__pointer, __integer) \
126 __builtin_ptrauth_blend_discriminator(__pointer, __integer)
127
128/* Return a signed pointer for a constant address in a manner which guarantees
129 a non-attackable sequence.
130
131 The value must be a constant expression of pointer type which evaluates to
132 a non-null pointer.
133 The key must be a constant expression of type ptrauth_key.
134 The extra data must be a constant expression of pointer or integer type;
135 if an integer, it will be coerced to ptrauth_extra_data_t.
136 The result will have the same type as the original value.
137
138 This can be used in constant expressions. */
139#define ptrauth_sign_constant(__value, __key, __data) \
140 __builtin_ptrauth_sign_constant(__value, __key, __data)
141
142/* Add a signature to the given pointer value using a specific key,
143 using the given extra data as a salt to the signing process.
144
145 This operation does not authenticate the original value and is
146 therefore potentially insecure if an attacker could possibly
147 control that value.
148
149 The value must be an expression of pointer type.
150 The key must be a constant expression of type ptrauth_key.
151 The extra data must be an expression of pointer or integer type;
152 if an integer, it will be coerced to ptrauth_extra_data_t.
153 The result will have the same type as the original value. */
154#define ptrauth_sign_unauthenticated(__value, __key, __data) \
155 __builtin_ptrauth_sign_unauthenticated(__value, __key, __data)
156
157/* Authenticate a pointer using one scheme and resign it using another.
158
159 If the result is subsequently authenticated using the new scheme, that
160 authentication is guaranteed to fail if and only if the initial
161 authentication failed.
162
163 The value must be an expression of pointer type.
164 The key must be a constant expression of type ptrauth_key.
165 The extra data must be an expression of pointer or integer type;
166 if an integer, it will be coerced to ptrauth_extra_data_t.
167 The result will have the same type as the original value.
168
169 This operation is guaranteed to not leave the intermediate value
170 available for attack before it is re-signed.
171
172 Do not pass a null pointer to this function. A null pointer
173 will not successfully authenticate.
174
175 This operation traps if the authentication fails. */
176#define ptrauth_auth_and_resign(__value, __old_key, __old_data, __new_key, \
177 __new_data) \
178 __builtin_ptrauth_auth_and_resign(__value, __old_key, __old_data, __new_key, \
179 __new_data)
180
181/* Authenticate a pointer using a PC-based signature scheme and resign
182 it using a different scheme.
183
184 If the result is subsequently authenticated using the new scheme, that
185 authentication is guaranteed to fail if and only if the initial
186 authentication failed.
187
188 The value must be an expression of pointer type.
189 The key must be a constant expression of type ptrauth_key.
190 The extra data must be an expression of pointer or integer type;
191 if an integer, it will be coerced to ptrauth_extra_data_t.
192 The oldpc must be an expression of pointer or integer type representing
193 the PC value where the original signature was created.
194 The result will have the same type as the original value.
195
196 This operation is guaranteed to not leave the intermediate value
197 available for attack before it is re-signed.
198
199 Do not pass a null pointer to this function. A null pointer
200 will not successfully authenticate. */
201#define ptrauth_auth_with_pc_and_resign(__value, __old_key, __old_data, \
202 __old_pc, __new_key, __new_data) \
203 __builtin_ptrauth_auth_with_pc_and_resign(__value, __old_key, __old_data, \
204 __old_pc, __new_key, __new_data)
205
206/* Authenticate a pointer using one scheme, load 32bit value at offset addend
207 from the pointer, and add this value to the pointer, sign using specified
208 scheme.
209
210 If the result is subsequently authenticated using the new scheme, that
211 authentication is guaranteed to fail if and only if the initial
212 authentication failed.
213
214 The value must be an expression of pointer type.
215 The key must be a constant expression of type ptrauth_key.
216 The extra data must be an expression of pointer or integer type;
217 if an integer, it will be coerced to ptrauth_extra_data_t.
218 The addend must be an immediate ptrdiff_t value.
219 The result will have the same type as the original value.
220
221 This operation is guaranteed to not leave the intermediate value
222 available for attack before it is re-signed.
223
224 Do not pass a null pointer to this function. A null pointer
225 will not successfully authenticate. */
226#define ptrauth_auth_load_relative_and_sign(__value, __old_key, __old_data, \
227 __new_key, __new_data, __offset) \
228 __builtin_ptrauth_auth_load_relative_and_sign( \
229 __value, __old_key, __old_data, __new_key, __new_data, __offset)
230
231/* Authenticate a pointer using one scheme and resign it as a C
232 function pointer.
233
234 If the result is subsequently authenticated using the new scheme, that
235 authentication is guaranteed to fail if and only if the initial
236 authentication failed.
237
238 The value must be an expression of function pointer type.
239 The key must be a constant expression of type ptrauth_key.
240 The extra data must be an expression of pointer or integer type;
241 if an integer, it will be coerced to ptrauth_extra_data_t.
242 The result will have the same type as the original value.
243
244 This operation is guaranteed to not leave the intermediate value
245 available for attack before it is re-signed. Additionally, if this
246 expression is used syntactically as the function expression in a
247 call, only a single authentication will be performed. */
248#define ptrauth_auth_function(__value, __old_key, __old_data) \
249 ptrauth_auth_and_resign(__value, __old_key, __old_data, \
250 ptrauth_key_function_pointer, 0)
251
252/* Authenticate a data pointer.
253
254 The value must be an expression of non-function pointer type.
255 The key must be a constant expression of type ptrauth_key.
256 The extra data must be an expression of pointer or integer type;
257 if an integer, it will be coerced to ptrauth_extra_data_t.
258 The result will have the same type as the original value.
259
260 This operation traps if the authentication fails. */
261#define ptrauth_auth_data(__value, __old_key, __old_data) \
262 __builtin_ptrauth_auth(__value, __old_key, __old_data)
263
264/* Compute a constant discriminator from the given string.
265
266 The argument must be a string literal of char character type. The result
267 has type ptrauth_extra_data_t.
268
269 The result value is never zero and always within range for both the
270 __ptrauth qualifier and ptrauth_blend_discriminator.
271
272 This can be used in constant expressions.
273*/
274#define ptrauth_string_discriminator(__string) \
275 __builtin_ptrauth_string_discriminator(__string)
276
277/* Compute a constant discriminator from the given type.
278
279 The result can be used as the second argument to
280 ptrauth_blend_discriminator or the third argument to the
281 __ptrauth qualifier. It has type size_t.
282
283 If the type is a C++ member function pointer type, the result is
284 the discriminator used to signed member function pointers of that
285 type. If the type is a function, function pointer, or function
286 reference type, the result is the discriminator used to sign
287 functions of that type. It is ill-formed to use this macro with any
288 other type.
289
290 A call to this function is an integer constant expression. */
291#define ptrauth_type_discriminator(__type) \
292 __builtin_ptrauth_type_discriminator(__type)
293
294/* Compute the constant discriminator used by Clang to sign pointers with the
295 given C function pointer type.
296
297 A call to this function is an integer constant expression. */
298#if __has_feature(ptrauth_function_pointer_type_discrimination)
299#define ptrauth_function_pointer_type_discriminator(__type) \
300 __builtin_ptrauth_type_discriminator(__type)
301#else
302#define ptrauth_function_pointer_type_discriminator(__type) \
303 ((ptrauth_extra_data_t)0)
304#endif
305
306/* Compute a signature for the given pair of pointer-sized values.
307 The order of the arguments is significant.
308
309 Like a pointer signature, the resulting signature depends on
310 private key data and therefore should not be reliably reproducible
311 by attackers. That means that this can be used to validate the
312 integrity of arbitrary data by storing a signature for that data
313 alongside it, then checking that the signature is still valid later.
314 Data which exceeds two pointers in size can be signed by either
315 computing a tree of generic signatures or just signing an ordinary
316 cryptographic hash of the data.
317
318 The result has type ptrauth_generic_signature_t. However, it may
319 not have as many bits of entropy as that type's width would suggest;
320 some implementations are known to compute a compressed signature as
321 if the arguments were a pointer and a discriminator.
322
323 The arguments must be either pointers or integers; if integers, they
324 will be coerce to uintptr_t. */
325#define ptrauth_sign_generic_data(__value, __data) \
326 __builtin_ptrauth_sign_generic_data(__value, __data)
327
328/* C++ vtable pointer signing class attribute */
329#define ptrauth_cxx_vtable_pointer(key, address_discrimination, \
330 extra_discrimination...) \
331 [[clang::ptrauth_vtable_pointer(key, address_discrimination, \
332 extra_discrimination)]]
333
334/* The value is ptrauth_string_discriminator("init_fini") */
335#define __ptrauth_init_fini_discriminator 0xd9d4
336
337/* Objective-C pointer auth ABI qualifiers */
338#define __ptrauth_objc_method_list_imp \
339 __ptrauth(ptrauth_key_function_pointer, 1, 0)
340
341#if __has_feature(ptrauth_objc_method_list_pointer)
342#define __ptrauth_objc_method_list_pointer \
343 __ptrauth(ptrauth_key_method_list_pointer, 1, 0xC310)
344#else
345#define __ptrauth_objc_method_list_pointer
346#endif
347
348#define __ptrauth_isa_discriminator 0x6AE1
349#define __ptrauth_super_discriminator 0xB5AB
350#define __ptrauth_objc_isa_pointer \
351 __ptrauth(ptrauth_key_objc_isa_pointer, 1, __ptrauth_isa_discriminator)
352#if __has_feature(ptrauth_restricted_intptr_qualifier)
353#define __ptrauth_objc_isa_uintptr \
354 __ptrauth_restricted_intptr(ptrauth_key_objc_isa_pointer, 1, \
355 __ptrauth_isa_discriminator)
356#else
357#define __ptrauth_objc_isa_uintptr \
358 __ptrauth(ptrauth_key_objc_isa_pointer, 1, __ptrauth_isa_discriminator)
359#endif
360
361#define __ptrauth_objc_super_pointer \
362 __ptrauth(ptrauth_key_objc_super_pointer, 1, __ptrauth_super_discriminator)
363
364#define __ptrauth_objc_sel_discriminator 0x57c2
365#if __has_feature(ptrauth_objc_interface_sel)
366#define __ptrauth_objc_sel \
367 __ptrauth(ptrauth_key_objc_sel_pointer, 1, __ptrauth_objc_sel_discriminator)
368#else
369#define __ptrauth_objc_sel
370#endif
371
372#define __ptrauth_objc_class_ro_discriminator 0x61f8
373#define __ptrauth_objc_class_ro \
374 __ptrauth(ptrauth_key_objc_class_ro_pointer, 1, \
375 __ptrauth_objc_class_ro_discriminator)
376
377#if __has_feature(ptrauth_init_fini_address_discrimination)
378#define __ptrauth_init_fini_pointer \
379 __ptrauth(ptrauth_key_init_fini_pointer, 1, __ptrauth_init_fini_discriminator)
380#else
381#define __ptrauth_init_fini_pointer \
382 __ptrauth(ptrauth_key_init_fini_pointer, 0, __ptrauth_init_fini_discriminator)
383#endif
384
385#else
386
387#define ptrauth_strip(__value, __key) \
388 __extension__({ \
389 (void)__key; \
390 __value; \
391 })
392
393#define ptrauth_blend_discriminator(__pointer, __integer) \
394 __extension__({ \
395 (void)__pointer; \
396 (void)__integer; \
397 ((ptrauth_extra_data_t)0); \
398 })
399
400#define ptrauth_sign_constant(__value, __key, __data) \
401 __extension__({ \
402 (void)__key; \
403 (void)__data; \
404 __value; \
405 })
406
407#define ptrauth_sign_unauthenticated(__value, __key, __data) \
408 __extension__({ \
409 (void)__key; \
410 (void)__data; \
411 __value; \
412 })
413
414#define ptrauth_auth_and_resign(__value, __old_key, __old_data, __new_key, \
415 __new_data) \
416 __extension__({ \
417 (void)__old_key; \
418 (void)__old_data; \
419 (void)__new_key; \
420 (void)__new_data; \
421 __value; \
422 })
423
424#define ptrauth_auth_with_pc_and_resign(__value, __old_key, __old_data, \
425 __old_pc, __new_key, __new_data) \
426 __extension__({ \
427 (void)__old_key; \
428 (void)__old_data; \
429 (void)__old_pc; \
430 (void)__new_key; \
431 (void)__new_data; \
432 __value; \
433 })
434
435#define ptrauth_auth_load_relative_and_sign(__value, __old_key, __old_data, \
436 __new_key, __new_data, __offset) \
437 __extension__({ \
438 (void)__old_key; \
439 (void)__old_data; \
440 (void)__new_key; \
441 (void)__new_data; \
442 const char *__value_tmp = (const char *)(__value); \
443 (void *)(__value_tmp + *(const int *)(__value_tmp + (__offset))); \
444 })
445
446#define ptrauth_auth_function(__value, __old_key, __old_data) \
447 __extension__({ \
448 (void)__old_key; \
449 (void)__old_data; \
450 __value; \
451 })
452
453#define ptrauth_auth_data(__value, __old_key, __old_data) \
454 __extension__({ \
455 (void)__old_key; \
456 (void)__old_data; \
457 __value; \
458 })
459
460#define ptrauth_string_discriminator(__string) \
461 __extension__({ \
462 (void)__string; \
463 ((ptrauth_extra_data_t)0); \
464 })
465
466#define ptrauth_type_discriminator(__type) ((ptrauth_extra_data_t)0)
467#define ptrauth_function_pointer_type_discriminator(__type) \
468 ((ptrauth_extra_data_t)0)
469
470#define ptrauth_sign_generic_data(__value, __data) \
471 __extension__({ \
472 (void)__value; \
473 (void)__data; \
474 ((ptrauth_generic_signature_t)0); \
475 })
476
477#define ptrauth_cxx_vtable_pointer(key, address_discrimination, \
478 extra_discrimination...)
479
480#define __ptrauth_objc_isa_pointer
481#define __ptrauth_objc_isa_uintptr
482#define __ptrauth_objc_super_pointer
483
484#endif /* __has_feature(ptrauth_intrinsics) || defined(__PTRAUTH__) */
485
486#endif /* __PTRAUTH_H */
__UINTPTR_TYPE__ ptrauth_extra_data_t
Definition ptrauth.h:66
__UINTPTR_TYPE__ ptrauth_generic_signature_t
Definition ptrauth.h:69
ptrauth_key
Definition ptrauth.h:13
@ ptrauth_key_method_list_pointer
Definition ptrauth.h:46
@ ptrauth_key_return_address
Definition ptrauth.h:35
@ ptrauth_key_process_independent_code
Definition ptrauth.h:20
@ ptrauth_key_process_dependent_code
Definition ptrauth.h:23
@ ptrauth_key_process_independent_data
Definition ptrauth.h:26
@ ptrauth_key_cxx_vtable_pointer
Definition ptrauth.h:43
@ ptrauth_key_objc_super_pointer
Definition ptrauth.h:50
@ ptrauth_key_asia
Definition ptrauth.h:14
@ ptrauth_key_init_fini_pointer
Definition ptrauth.h:59
@ ptrauth_key_objc_isa_pointer
Definition ptrauth.h:49
@ ptrauth_key_objc_sel_pointer
Definition ptrauth.h:53
@ ptrauth_key_asda
Definition ptrauth.h:16
@ ptrauth_key_objc_class_ro_pointer
Definition ptrauth.h:56
@ ptrauth_key_asib
Definition ptrauth.h:15
@ ptrauth_key_process_dependent_data
Definition ptrauth.h:29
@ ptrauth_key_function_pointer
Definition ptrauth.h:39
@ ptrauth_key_asdb
Definition ptrauth.h:17