xref: /xnu-12377.81.4/libkern/ptrauth_utils.c (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1 /*
2  * Copyright (c) 2020 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #include <IOKit/IOLib.h>
30 #include <kern/debug.h> // for panic()
31 
32 #include <libkern/ptrauth_utils.h>
33 
34 /*
35  * On ptrauth systems, ptrauth_utils_sign_blob_generic is implemented
36  * in osfmk/arm64/machine_routines_asm.s
37  */
38 
39 #if !__has_feature(ptrauth_calls)
40 ptrauth_generic_signature_t
ptrauth_utils_sign_blob_generic(__unused const void * ptr,__unused size_t len_bytes,__unused uint64_t data,__unused int flags)41 ptrauth_utils_sign_blob_generic(__unused const void * ptr, __unused size_t len_bytes, __unused uint64_t data, __unused int flags)
42 {
43 	return 0;
44 }
45 #endif // __has_feature(ptrauth_calls)
46 
47 
48 /*
49  * ptrauth_utils_auth_blob_generic
50  *
51  * Authenticate signature produced by ptrauth_utils_sign_blob_generic
52  */
53 
54 #if __has_feature(ptrauth_calls)
55 __attribute__((noinline))
56 void
ptrauth_utils_auth_blob_generic(const void * ptr,size_t len_bytes,uint64_t data,int flags,ptrauth_generic_signature_t signature)57 ptrauth_utils_auth_blob_generic(const void * ptr, size_t len_bytes, uint64_t data, int flags, ptrauth_generic_signature_t signature)
58 {
59 	ptrauth_generic_signature_t calculated_signature = 0;
60 
61 	if (ptr == NULL) {
62 		if (flags & PTRAUTH_NON_NULL) {
63 			panic("ptrauth_utils_auth_blob_generic: ptr must not be NULL");
64 		} else {
65 			return;
66 		}
67 	}
68 
69 	if ((calculated_signature = ptrauth_utils_sign_blob_generic(ptr, len_bytes, data, flags)) == signature) {
70 		return;
71 	} else {
72 		panic("signature mismatch for %lu bytes at %p, calculated %lx vs %lx", len_bytes,
73 		    ptr,
74 		    calculated_signature,
75 		    signature);
76 	}
77 }
78 #else
79 void
ptrauth_utils_auth_blob_generic(__unused const void * ptr,__unused size_t len_bytes,__unused uint64_t data,__unused int flags,__unused ptrauth_generic_signature_t signature)80 ptrauth_utils_auth_blob_generic(__unused const void * ptr, __unused size_t len_bytes, __unused uint64_t data, __unused int flags, __unused ptrauth_generic_signature_t signature)
81 {
82 	return;
83 }
84 #endif // __has_feature(ptrauth_calls)
85