xref: /xnu-11417.121.6/tests/pac_exception_entitlement.c (revision a1e26a70f38d1d7daa7b49b258e2f8538ad81650)
1 /*
2  * Copyright (c) 2023 Apple Computer, 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 <darwintest.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <mach/exception_types.h>
33 #include <sys/wait.h>
34 #include <sys/sysctl.h>
35 #include <sys/code_signing.h>
36 
37 #include "exc_helpers.h"
38 #include "test_utils.h"
39 
40 T_GLOBAL_META(
41 	T_META_NAMESPACE("xnu.arm"),
42 	T_META_RADAR_COMPONENT_NAME("xnu"),
43 	T_META_RADAR_COMPONENT_VERSION("arm"),
44 	T_META_OWNER("ghackmann"),
45 	T_META_REQUIRES_SYSCTL_EQ("hw.optional.ptrauth", 1),
46 	T_META_IGNORECRASHES(".*pac_exception_entitlement.*"),
47 	XNU_T_META_SOC_SPECIFIC
48 	);
49 
50 #if __arm64e__
51 static size_t
exception_handler(mach_port_t task __unused,mach_port_t thread __unused,exception_type_t type __unused,mach_exception_data_t codes __unused)52 exception_handler(mach_port_t task __unused, mach_port_t thread __unused,
53     exception_type_t type __unused, mach_exception_data_t codes __unused)
54 {
55 	T_ASSERT_FAIL("kernel ran exception handler instead of terminating process");
56 }
57 
58 /*
59  * To trigger PAC failure, we create a validly-signed pointer and then flip an
60  * arbitrary PAC bit before accessing it.  The exact number and location of PAC
61  * bits depends on the device configuration.  For instruction pointers, the PAC
62  * field always includes bit 63.  For data pointers, it may start as low as bit
63  * 54: bits 63-56 can be carved out for software tagging, and bit 55 is always
64  * reserved for addressing.
65  *
66  * Real-world software should use ptrauth.h when it needs to manually sign or
67  * auth pointers.  But for testing purposes we need clang to emit specific
68  * ptrauth instructions, so we use inline asm here instead.
69  *
70  * Likewise clang would normally combine the "naked" auth and brk testcases as
71  * part of a sequence like:
72  *
73  *     output = auth(...);
74  *     if (output is poisoned) {
75  *         brk(PTRAUTH_FAILURE_COMMENT);
76  *     }
77  *
78  * On auth failure, CPUs that implement FEAT_FPAC will trap immediately at the
79  * auth instruction, and CPUs without FEAT_FPAC will trap at the later brk
80  * instruction.  But again, for testing purposes we want these to be two
81  * discrete cases.  (On FPAC-enabled CPUs, the kernel should treat *both* traps
82  * as ptrauth failure, even if we don't expect the latter to be reachable in
83  * real-world software.)
84  */
85 
86 static void
naked_auth(void)87 naked_auth(void)
88 {
89 	asm volatile (
90                 "mov	x0, #0"                 "\n"
91                 "paciza	x0"                     "\n"
92                 "eor	x0, x0, (1 << 63)"      "\n"
93                 "autiza	x0"
94                 :
95                 :
96                 : "x0"
97         );
98 }
99 
100 static void
ptrauth_brk(void)101 ptrauth_brk(void)
102 {
103 	asm volatile ("brk 0xc470");
104 }
105 
106 static void
combined_branch_auth(void)107 combined_branch_auth(void)
108 {
109 	asm volatile (
110                 "adr	x0, 1f"                 "\n"
111                 "paciza	x0"                     "\n"
112                 "eor	x0, x0, (1 << 63)"      "\n"
113                 "braaz	x0"                     "\n"
114         "1:"
115                 :
116                 :
117                 : "x0"
118         );
119 }
120 
121 static void
combined_load_auth(void)122 combined_load_auth(void)
123 {
124 	asm volatile (
125                 "mov	x0, sp"                 "\n"
126                 "pacdza	x0"                     "\n"
127                 "eor	x0, x0, (1 << 54)"      "\n"
128                 "ldraa	x0, [x0]"               "\n"
129                 :
130                 :
131                 : "x0"
132         );
133 }
134 
135 static void
run_pac_exception_test(void (* ptrauth_failure_fn)(void))136 run_pac_exception_test(void (*ptrauth_failure_fn)(void))
137 {
138 
139 	pid_t pid = fork();
140 	T_QUIET; T_ASSERT_POSIX_SUCCESS(pid, "fork");
141 
142 	if (pid == 0) {
143 		mach_port_t exc_port = create_exception_port(EXC_MASK_BAD_ACCESS | EXC_MASK_BREAKPOINT);
144 		run_exception_handler(exc_port, exception_handler);
145 
146 		ptrauth_failure_fn();
147 		/* ptrauth_failure_fn() should have raised an uncatchable exception */
148 		T_FAIL("child ran to completion");
149 	} else {
150 		int status;
151 		int err = waitpid(pid, &status, 0);
152 		T_QUIET; T_ASSERT_POSIX_SUCCESS(err, "waitpid");
153 
154 		T_EXPECT_TRUE(WIFSIGNALED(status), "child terminated due to signal");
155 		T_EXPECT_EQ(SIGKILL, WTERMSIG(status), "child terminated due to SIGKILL");
156 	}
157 }
158 #endif //__arm64e__
159 
160 T_DECL(pac_exception_naked_auth,
161     "Test the com.apple.private.pac.exception entitlement (naked auth failure)",
162     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_FPAC", 1), T_META_TAG_VM_NOT_ELIGIBLE)
163 {
164 #if __arm64e__
165 	run_pac_exception_test(naked_auth);
166 #else
167 	T_SKIP("Running on non-arm64e target, skipping...");
168 #endif
169 }
170 
171 
172 T_DECL(pac_exception_ptrauth_brk,
173     "Test the com.apple.private.pac.exception entitlement (brk with comment indicating ptrauth failure)", T_META_TAG_VM_NOT_ELIGIBLE)
174 {
175 #if __arm64e__
176 	run_pac_exception_test(ptrauth_brk);
177 #else
178 	T_SKIP("Running on non-arm64e target, skipping...");
179 #endif
180 }
181 
182 T_DECL(pac_exception_combined_branch_auth,
183     "Test the com.apple.private.pac.exception entitlement (combined branch + auth failure)", T_META_TAG_VM_NOT_ELIGIBLE)
184 {
185 #if __arm64e__
186 	run_pac_exception_test(combined_branch_auth);
187 #else
188 	T_SKIP("Running on non-arm64e target, skipping...");
189 #endif
190 }
191 
192 T_DECL(pac_exception_combined_load_auth,
193     "Test the com.apple.private.pac.exception entitlement (combined branch + auth failure)", T_META_TAG_VM_NOT_ELIGIBLE)
194 {
195 #if __arm64e__
196 	run_pac_exception_test(combined_load_auth);
197 #else
198 	T_SKIP("Running on non-arm64e target, skipping...");
199 #endif
200 }
201