xref: /xnu-11215.61.5/tests/pac_exception_entitlement.c (revision 4f1223e81cd707a65cc109d0b8ad6653699da3c4)
1*4f1223e8SApple OSS Distributions /*
2*4f1223e8SApple OSS Distributions  * Copyright (c) 2023 Apple Computer, Inc. All rights reserved.
3*4f1223e8SApple OSS Distributions  *
4*4f1223e8SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*4f1223e8SApple OSS Distributions  *
6*4f1223e8SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*4f1223e8SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*4f1223e8SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*4f1223e8SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*4f1223e8SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*4f1223e8SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*4f1223e8SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*4f1223e8SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*4f1223e8SApple OSS Distributions  *
15*4f1223e8SApple OSS Distributions  * Please obtain a copy of the License at
16*4f1223e8SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*4f1223e8SApple OSS Distributions  *
18*4f1223e8SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*4f1223e8SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*4f1223e8SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*4f1223e8SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*4f1223e8SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*4f1223e8SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*4f1223e8SApple OSS Distributions  * limitations under the License.
25*4f1223e8SApple OSS Distributions  *
26*4f1223e8SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*4f1223e8SApple OSS Distributions  */
28*4f1223e8SApple OSS Distributions 
29*4f1223e8SApple OSS Distributions #include <darwintest.h>
30*4f1223e8SApple OSS Distributions #include <stdlib.h>
31*4f1223e8SApple OSS Distributions #include <unistd.h>
32*4f1223e8SApple OSS Distributions #include <mach/exception_types.h>
33*4f1223e8SApple OSS Distributions #include <sys/wait.h>
34*4f1223e8SApple OSS Distributions #include <sys/sysctl.h>
35*4f1223e8SApple OSS Distributions #include <sys/code_signing.h>
36*4f1223e8SApple OSS Distributions 
37*4f1223e8SApple OSS Distributions #include "exc_helpers.h"
38*4f1223e8SApple OSS Distributions #include "test_utils.h"
39*4f1223e8SApple OSS Distributions 
40*4f1223e8SApple OSS Distributions T_GLOBAL_META(
41*4f1223e8SApple OSS Distributions 	T_META_NAMESPACE("xnu.arm"),
42*4f1223e8SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
43*4f1223e8SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("arm"),
44*4f1223e8SApple OSS Distributions 	T_META_OWNER("ghackmann"),
45*4f1223e8SApple OSS Distributions 	T_META_REQUIRES_SYSCTL_EQ("hw.optional.ptrauth", 1),
46*4f1223e8SApple OSS Distributions 	T_META_IGNORECRASHES(".*pac_exception_entitlement.*"),
47*4f1223e8SApple OSS Distributions 	XNU_T_META_SOC_SPECIFIC
48*4f1223e8SApple OSS Distributions 	);
49*4f1223e8SApple OSS Distributions 
50*4f1223e8SApple OSS Distributions #if __arm64e__
51*4f1223e8SApple OSS Distributions 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*4f1223e8SApple OSS Distributions exception_handler(mach_port_t task __unused, mach_port_t thread __unused,
53*4f1223e8SApple OSS Distributions     exception_type_t type __unused, mach_exception_data_t codes __unused)
54*4f1223e8SApple OSS Distributions {
55*4f1223e8SApple OSS Distributions 	T_ASSERT_FAIL("kernel ran exception handler instead of terminating process");
56*4f1223e8SApple OSS Distributions }
57*4f1223e8SApple OSS Distributions 
58*4f1223e8SApple OSS Distributions /*
59*4f1223e8SApple OSS Distributions  * To trigger PAC failure, we create a validly-signed pointer and then flip an
60*4f1223e8SApple OSS Distributions  * arbitrary PAC bit before accessing it.  The exact number and location of PAC
61*4f1223e8SApple OSS Distributions  * bits depends on the device configuration.  For instruction pointers, the PAC
62*4f1223e8SApple OSS Distributions  * field always includes bit 63.  For data pointers, it may start as low as bit
63*4f1223e8SApple OSS Distributions  * 54: bits 63-56 can be carved out for software tagging, and bit 55 is always
64*4f1223e8SApple OSS Distributions  * reserved for addressing.
65*4f1223e8SApple OSS Distributions  *
66*4f1223e8SApple OSS Distributions  * Real-world software should use ptrauth.h when it needs to manually sign or
67*4f1223e8SApple OSS Distributions  * auth pointers.  But for testing purposes we need clang to emit specific
68*4f1223e8SApple OSS Distributions  * ptrauth instructions, so we use inline asm here instead.
69*4f1223e8SApple OSS Distributions  *
70*4f1223e8SApple OSS Distributions  * Likewise clang would normally combine the "naked" auth and brk testcases as
71*4f1223e8SApple OSS Distributions  * part of a sequence like:
72*4f1223e8SApple OSS Distributions  *
73*4f1223e8SApple OSS Distributions  *     output = auth(...);
74*4f1223e8SApple OSS Distributions  *     if (output is poisoned) {
75*4f1223e8SApple OSS Distributions  *         brk(PTRAUTH_FAILURE_COMMENT);
76*4f1223e8SApple OSS Distributions  *     }
77*4f1223e8SApple OSS Distributions  *
78*4f1223e8SApple OSS Distributions  * On auth failure, CPUs that implement FEAT_FPAC will trap immediately at the
79*4f1223e8SApple OSS Distributions  * auth instruction, and CPUs without FEAT_FPAC will trap at the later brk
80*4f1223e8SApple OSS Distributions  * instruction.  But again, for testing purposes we want these to be two
81*4f1223e8SApple OSS Distributions  * discrete cases.  (On FPAC-enabled CPUs, the kernel should treat *both* traps
82*4f1223e8SApple OSS Distributions  * as ptrauth failure, even if we don't expect the latter to be reachable in
83*4f1223e8SApple OSS Distributions  * real-world software.)
84*4f1223e8SApple OSS Distributions  */
85*4f1223e8SApple OSS Distributions 
86*4f1223e8SApple OSS Distributions static void
naked_auth(void)87*4f1223e8SApple OSS Distributions naked_auth(void)
88*4f1223e8SApple OSS Distributions {
89*4f1223e8SApple OSS Distributions 	asm volatile (
90*4f1223e8SApple OSS Distributions                 "mov	x0, #0"                 "\n"
91*4f1223e8SApple OSS Distributions                 "paciza	x0"                     "\n"
92*4f1223e8SApple OSS Distributions                 "eor	x0, x0, (1 << 63)"      "\n"
93*4f1223e8SApple OSS Distributions                 "autiza	x0"
94*4f1223e8SApple OSS Distributions                 :
95*4f1223e8SApple OSS Distributions                 :
96*4f1223e8SApple OSS Distributions                 : "x0"
97*4f1223e8SApple OSS Distributions         );
98*4f1223e8SApple OSS Distributions }
99*4f1223e8SApple OSS Distributions 
100*4f1223e8SApple OSS Distributions static void
ptrauth_brk(void)101*4f1223e8SApple OSS Distributions ptrauth_brk(void)
102*4f1223e8SApple OSS Distributions {
103*4f1223e8SApple OSS Distributions 	asm volatile ("brk 0xc470");
104*4f1223e8SApple OSS Distributions }
105*4f1223e8SApple OSS Distributions 
106*4f1223e8SApple OSS Distributions static void
combined_branch_auth(void)107*4f1223e8SApple OSS Distributions combined_branch_auth(void)
108*4f1223e8SApple OSS Distributions {
109*4f1223e8SApple OSS Distributions 	asm volatile (
110*4f1223e8SApple OSS Distributions                 "adr	x0, 1f"                 "\n"
111*4f1223e8SApple OSS Distributions                 "paciza	x0"                     "\n"
112*4f1223e8SApple OSS Distributions                 "eor	x0, x0, (1 << 63)"      "\n"
113*4f1223e8SApple OSS Distributions                 "braaz	x0"                     "\n"
114*4f1223e8SApple OSS Distributions         "1:"
115*4f1223e8SApple OSS Distributions                 :
116*4f1223e8SApple OSS Distributions                 :
117*4f1223e8SApple OSS Distributions                 : "x0"
118*4f1223e8SApple OSS Distributions         );
119*4f1223e8SApple OSS Distributions }
120*4f1223e8SApple OSS Distributions 
121*4f1223e8SApple OSS Distributions static void
combined_load_auth(void)122*4f1223e8SApple OSS Distributions combined_load_auth(void)
123*4f1223e8SApple OSS Distributions {
124*4f1223e8SApple OSS Distributions 	asm volatile (
125*4f1223e8SApple OSS Distributions                 "mov	x0, sp"                 "\n"
126*4f1223e8SApple OSS Distributions                 "pacdza	x0"                     "\n"
127*4f1223e8SApple OSS Distributions                 "eor	x0, x0, (1 << 54)"      "\n"
128*4f1223e8SApple OSS Distributions                 "ldraa	x0, [x0]"               "\n"
129*4f1223e8SApple OSS Distributions                 :
130*4f1223e8SApple OSS Distributions                 :
131*4f1223e8SApple OSS Distributions                 : "x0"
132*4f1223e8SApple OSS Distributions         );
133*4f1223e8SApple OSS Distributions }
134*4f1223e8SApple OSS Distributions 
135*4f1223e8SApple OSS Distributions static void
run_pac_exception_test(void (* ptrauth_failure_fn)(void))136*4f1223e8SApple OSS Distributions run_pac_exception_test(void (*ptrauth_failure_fn)(void))
137*4f1223e8SApple OSS Distributions {
138*4f1223e8SApple OSS Distributions 
139*4f1223e8SApple OSS Distributions 	pid_t pid = fork();
140*4f1223e8SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(pid, "fork");
141*4f1223e8SApple OSS Distributions 
142*4f1223e8SApple OSS Distributions 	if (pid == 0) {
143*4f1223e8SApple OSS Distributions 		mach_port_t exc_port = create_exception_port(EXC_MASK_BAD_ACCESS | EXC_MASK_BREAKPOINT);
144*4f1223e8SApple OSS Distributions 		run_exception_handler(exc_port, exception_handler);
145*4f1223e8SApple OSS Distributions 
146*4f1223e8SApple OSS Distributions 		ptrauth_failure_fn();
147*4f1223e8SApple OSS Distributions 		/* ptrauth_failure_fn() should have raised an uncatchable exception */
148*4f1223e8SApple OSS Distributions 		T_FAIL("child ran to completion");
149*4f1223e8SApple OSS Distributions 	} else {
150*4f1223e8SApple OSS Distributions 		int status;
151*4f1223e8SApple OSS Distributions 		int err = waitpid(pid, &status, 0);
152*4f1223e8SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(err, "waitpid");
153*4f1223e8SApple OSS Distributions 
154*4f1223e8SApple OSS Distributions 		T_EXPECT_TRUE(WIFSIGNALED(status), "child terminated due to signal");
155*4f1223e8SApple OSS Distributions 		T_EXPECT_EQ(SIGKILL, WTERMSIG(status), "child terminated due to SIGKILL");
156*4f1223e8SApple OSS Distributions 	}
157*4f1223e8SApple OSS Distributions }
158*4f1223e8SApple OSS Distributions #endif //__arm64e__
159*4f1223e8SApple OSS Distributions 
160*4f1223e8SApple OSS Distributions T_DECL(pac_exception_naked_auth,
161*4f1223e8SApple OSS Distributions     "Test the com.apple.private.pac.exception entitlement (naked auth failure)",
162*4f1223e8SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_FPAC", 1), T_META_TAG_VM_NOT_ELIGIBLE)
163*4f1223e8SApple OSS Distributions {
164*4f1223e8SApple OSS Distributions #if __arm64e__
165*4f1223e8SApple OSS Distributions 	run_pac_exception_test(naked_auth);
166*4f1223e8SApple OSS Distributions #else
167*4f1223e8SApple OSS Distributions 	T_SKIP("Running on non-arm64e target, skipping...");
168*4f1223e8SApple OSS Distributions #endif
169*4f1223e8SApple OSS Distributions }
170*4f1223e8SApple OSS Distributions 
171*4f1223e8SApple OSS Distributions 
172*4f1223e8SApple OSS Distributions T_DECL(pac_exception_ptrauth_brk,
173*4f1223e8SApple OSS Distributions     "Test the com.apple.private.pac.exception entitlement (brk with comment indicating ptrauth failure)", T_META_TAG_VM_NOT_ELIGIBLE)
174*4f1223e8SApple OSS Distributions {
175*4f1223e8SApple OSS Distributions #if __arm64e__
176*4f1223e8SApple OSS Distributions 	run_pac_exception_test(ptrauth_brk);
177*4f1223e8SApple OSS Distributions #else
178*4f1223e8SApple OSS Distributions 	T_SKIP("Running on non-arm64e target, skipping...");
179*4f1223e8SApple OSS Distributions #endif
180*4f1223e8SApple OSS Distributions }
181*4f1223e8SApple OSS Distributions 
182*4f1223e8SApple OSS Distributions T_DECL(pac_exception_combined_branch_auth,
183*4f1223e8SApple OSS Distributions     "Test the com.apple.private.pac.exception entitlement (combined branch + auth failure)", T_META_TAG_VM_NOT_ELIGIBLE)
184*4f1223e8SApple OSS Distributions {
185*4f1223e8SApple OSS Distributions #if __arm64e__
186*4f1223e8SApple OSS Distributions 	run_pac_exception_test(combined_branch_auth);
187*4f1223e8SApple OSS Distributions #else
188*4f1223e8SApple OSS Distributions 	T_SKIP("Running on non-arm64e target, skipping...");
189*4f1223e8SApple OSS Distributions #endif
190*4f1223e8SApple OSS Distributions }
191*4f1223e8SApple OSS Distributions 
192*4f1223e8SApple OSS Distributions T_DECL(pac_exception_combined_load_auth,
193*4f1223e8SApple OSS Distributions     "Test the com.apple.private.pac.exception entitlement (combined branch + auth failure)", T_META_TAG_VM_NOT_ELIGIBLE)
194*4f1223e8SApple OSS Distributions {
195*4f1223e8SApple OSS Distributions #if __arm64e__
196*4f1223e8SApple OSS Distributions 	run_pac_exception_test(combined_load_auth);
197*4f1223e8SApple OSS Distributions #else
198*4f1223e8SApple OSS Distributions 	T_SKIP("Running on non-arm64e target, skipping...");
199*4f1223e8SApple OSS Distributions #endif
200*4f1223e8SApple OSS Distributions }
201