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