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