1*2c2f96dcSApple OSS Distributions /*
2*2c2f96dcSApple OSS Distributions * Copyright (c) 2022 Apple Computer, Inc. All rights reserved.
3*2c2f96dcSApple OSS Distributions *
4*2c2f96dcSApple OSS Distributions * @APPLE_LICENSE_HEADER_START@
5*2c2f96dcSApple OSS Distributions *
6*2c2f96dcSApple OSS Distributions * The contents of this file constitute Original Code as defined in and
7*2c2f96dcSApple OSS Distributions * are subject to the Apple Public Source License Version 1.1 (the
8*2c2f96dcSApple OSS Distributions * "License"). You may not use this file except in compliance with the
9*2c2f96dcSApple OSS Distributions * License. Please obtain a copy of the License at
10*2c2f96dcSApple OSS Distributions * http://www.apple.com/publicsource and read it before using this file.
11*2c2f96dcSApple OSS Distributions *
12*2c2f96dcSApple OSS Distributions * This Original Code and all software distributed under the License are
13*2c2f96dcSApple OSS Distributions * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14*2c2f96dcSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15*2c2f96dcSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16*2c2f96dcSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17*2c2f96dcSApple OSS Distributions * License for the specific language governing rights and limitations
18*2c2f96dcSApple OSS Distributions * under the License.
19*2c2f96dcSApple OSS Distributions *
20*2c2f96dcSApple OSS Distributions * @APPLE_LICENSE_HEADER_END@
21*2c2f96dcSApple OSS Distributions */
22*2c2f96dcSApple OSS Distributions
23*2c2f96dcSApple OSS Distributions #include <stdarg.h>
24*2c2f96dcSApple OSS Distributions #include <stdatomic.h>
25*2c2f96dcSApple OSS Distributions #include <os/overflow.h>
26*2c2f96dcSApple OSS Distributions #include <machine/atomic.h>
27*2c2f96dcSApple OSS Distributions #include <mach/vm_param.h>
28*2c2f96dcSApple OSS Distributions #include <mach/vm_map.h>
29*2c2f96dcSApple OSS Distributions #include <mach/shared_region.h>
30*2c2f96dcSApple OSS Distributions #include <vm/vm_kern.h>
31*2c2f96dcSApple OSS Distributions #include <kern/zalloc.h>
32*2c2f96dcSApple OSS Distributions #include <kern/kalloc.h>
33*2c2f96dcSApple OSS Distributions #include <kern/assert.h>
34*2c2f96dcSApple OSS Distributions #include <kern/locks.h>
35*2c2f96dcSApple OSS Distributions #include <kern/recount.h>
36*2c2f96dcSApple OSS Distributions #include <kern/sched_prim.h>
37*2c2f96dcSApple OSS Distributions #include <kern/lock_rw.h>
38*2c2f96dcSApple OSS Distributions #include <libkern/libkern.h>
39*2c2f96dcSApple OSS Distributions #include <libkern/section_keywords.h>
40*2c2f96dcSApple OSS Distributions #include <libkern/coretrust/coretrust.h>
41*2c2f96dcSApple OSS Distributions #include <libkern/amfi/amfi.h>
42*2c2f96dcSApple OSS Distributions #include <pexpert/pexpert.h>
43*2c2f96dcSApple OSS Distributions #include <sys/vm.h>
44*2c2f96dcSApple OSS Distributions #include <sys/proc.h>
45*2c2f96dcSApple OSS Distributions #include <sys/codesign.h>
46*2c2f96dcSApple OSS Distributions #include <sys/code_signing.h>
47*2c2f96dcSApple OSS Distributions #include <uuid/uuid.h>
48*2c2f96dcSApple OSS Distributions #include <IOKit/IOBSD.h>
49*2c2f96dcSApple OSS Distributions
50*2c2f96dcSApple OSS Distributions #if CONFIG_SPTM
51*2c2f96dcSApple OSS Distributions /*
52*2c2f96dcSApple OSS Distributions * The TrustedExecutionMonitor environment works in tandem with the SPTM to provide code
53*2c2f96dcSApple OSS Distributions * signing and memory isolation enforcement for data structures critical to ensuring that
54*2c2f96dcSApple OSS Distributions * all code executed on the system is authorized to do so.
55*2c2f96dcSApple OSS Distributions *
56*2c2f96dcSApple OSS Distributions * Unless the data is managed by TXM itself, XNU needs to page-align everything, make the
57*2c2f96dcSApple OSS Distributions * relevant type transfer, and then reference the memory as read-only.
58*2c2f96dcSApple OSS Distributions *
59*2c2f96dcSApple OSS Distributions * TXM enforces concurrency on its side, but through the use of try-locks. Upon a failure
60*2c2f96dcSApple OSS Distributions * in acquiring the lock, TXM will panic. As a result, in order to ensure single-threaded
61*2c2f96dcSApple OSS Distributions * behavior, the kernel also has to take some locks on its side befor calling into TXM.
62*2c2f96dcSApple OSS Distributions */
63*2c2f96dcSApple OSS Distributions #include <sys/trusted_execution_monitor.h>
64*2c2f96dcSApple OSS Distributions #include <pexpert/arm64/board_config.h>
65*2c2f96dcSApple OSS Distributions
66*2c2f96dcSApple OSS Distributions /* Lock group used for all locks within the kernel for TXM */
67*2c2f96dcSApple OSS Distributions LCK_GRP_DECLARE(txm_lck_grp, "txm_code_signing_lck_grp");
68*2c2f96dcSApple OSS Distributions
69*2c2f96dcSApple OSS Distributions #pragma mark Utilities
70*2c2f96dcSApple OSS Distributions
71*2c2f96dcSApple OSS Distributions /* Number of thread stacks is known at build-time */
72*2c2f96dcSApple OSS Distributions #define NUM_TXM_THREAD_STACKS (MAX_CPUS)
73*2c2f96dcSApple OSS Distributions txm_thread_stack_t thread_stacks[NUM_TXM_THREAD_STACKS] = {0};
74*2c2f96dcSApple OSS Distributions
75*2c2f96dcSApple OSS Distributions /* Singly-linked-list head for thread stacks */
76*2c2f96dcSApple OSS Distributions SLIST_HEAD(thread_stack_head, _txm_thread_stack) thread_stacks_head =
77*2c2f96dcSApple OSS Distributions SLIST_HEAD_INITIALIZER(thread_stacks_head);
78*2c2f96dcSApple OSS Distributions
79*2c2f96dcSApple OSS Distributions static decl_lck_mtx_data(, thread_stacks_lock);
80*2c2f96dcSApple OSS Distributions static void *thread_stack_event = NULL;
81*2c2f96dcSApple OSS Distributions
82*2c2f96dcSApple OSS Distributions static void
setup_thread_stacks(void)83*2c2f96dcSApple OSS Distributions setup_thread_stacks(void)
84*2c2f96dcSApple OSS Distributions {
85*2c2f96dcSApple OSS Distributions extern const sptm_bootstrap_args_xnu_t *SPTMArgs;
86*2c2f96dcSApple OSS Distributions txm_thread_stack_t *thread_stack = NULL;
87*2c2f96dcSApple OSS Distributions
88*2c2f96dcSApple OSS Distributions /* Initialize each thread stack and add it to the list */
89*2c2f96dcSApple OSS Distributions for (uint32_t i = 0; i < NUM_TXM_THREAD_STACKS; i++) {
90*2c2f96dcSApple OSS Distributions thread_stack = &thread_stacks[i];
91*2c2f96dcSApple OSS Distributions
92*2c2f96dcSApple OSS Distributions /* Acquire the thread stack virtual mapping */
93*2c2f96dcSApple OSS Distributions thread_stack->thread_stack_papt = SPTMArgs->txm_thread_stacks[i];
94*2c2f96dcSApple OSS Distributions
95*2c2f96dcSApple OSS Distributions /* Acquire the thread stack physical page */
96*2c2f96dcSApple OSS Distributions thread_stack->thread_stack_phys = (uintptr_t)kvtophys_nofail(
97*2c2f96dcSApple OSS Distributions thread_stack->thread_stack_papt);
98*2c2f96dcSApple OSS Distributions
99*2c2f96dcSApple OSS Distributions /* Resolve the pointer to the thread stack data */
100*2c2f96dcSApple OSS Distributions thread_stack->thread_stack_data =
101*2c2f96dcSApple OSS Distributions (TXMThreadStack_t*)(thread_stack->thread_stack_papt + (PAGE_SIZE - 1024));
102*2c2f96dcSApple OSS Distributions
103*2c2f96dcSApple OSS Distributions /* Add thread stack to the list head */
104*2c2f96dcSApple OSS Distributions SLIST_INSERT_HEAD(&thread_stacks_head, thread_stack, link);
105*2c2f96dcSApple OSS Distributions }
106*2c2f96dcSApple OSS Distributions
107*2c2f96dcSApple OSS Distributions /* Initialize the thread stacks lock */
108*2c2f96dcSApple OSS Distributions lck_mtx_init(&thread_stacks_lock, &txm_lck_grp, 0);
109*2c2f96dcSApple OSS Distributions }
110*2c2f96dcSApple OSS Distributions
111*2c2f96dcSApple OSS Distributions static txm_thread_stack_t*
acquire_thread_stack(void)112*2c2f96dcSApple OSS Distributions acquire_thread_stack(void)
113*2c2f96dcSApple OSS Distributions {
114*2c2f96dcSApple OSS Distributions txm_thread_stack_t *thread_stack = NULL;
115*2c2f96dcSApple OSS Distributions
116*2c2f96dcSApple OSS Distributions /* Lock the thread stack list */
117*2c2f96dcSApple OSS Distributions lck_mtx_lock(&thread_stacks_lock);
118*2c2f96dcSApple OSS Distributions
119*2c2f96dcSApple OSS Distributions while (SLIST_EMPTY(&thread_stacks_head) == true) {
120*2c2f96dcSApple OSS Distributions lck_mtx_sleep(
121*2c2f96dcSApple OSS Distributions &thread_stacks_lock,
122*2c2f96dcSApple OSS Distributions LCK_SLEEP_DEFAULT,
123*2c2f96dcSApple OSS Distributions &thread_stack_event,
124*2c2f96dcSApple OSS Distributions THREAD_UNINT);
125*2c2f96dcSApple OSS Distributions }
126*2c2f96dcSApple OSS Distributions
127*2c2f96dcSApple OSS Distributions if (SLIST_EMPTY(&thread_stacks_head) == true) {
128*2c2f96dcSApple OSS Distributions panic("unable to acquire a thread stack for TXM");
129*2c2f96dcSApple OSS Distributions }
130*2c2f96dcSApple OSS Distributions
131*2c2f96dcSApple OSS Distributions /* Use the first available thread stack */
132*2c2f96dcSApple OSS Distributions thread_stack = SLIST_FIRST(&thread_stacks_head);
133*2c2f96dcSApple OSS Distributions
134*2c2f96dcSApple OSS Distributions /* Remove the thread stack from the list */
135*2c2f96dcSApple OSS Distributions SLIST_REMOVE_HEAD(&thread_stacks_head, link);
136*2c2f96dcSApple OSS Distributions
137*2c2f96dcSApple OSS Distributions /* Unlock the thread stack list */
138*2c2f96dcSApple OSS Distributions lck_mtx_unlock(&thread_stacks_lock);
139*2c2f96dcSApple OSS Distributions
140*2c2f96dcSApple OSS Distributions /* Associate the thread stack with the current thread */
141*2c2f96dcSApple OSS Distributions thread_associate_txm_thread_stack(thread_stack->thread_stack_phys);
142*2c2f96dcSApple OSS Distributions
143*2c2f96dcSApple OSS Distributions return thread_stack;
144*2c2f96dcSApple OSS Distributions }
145*2c2f96dcSApple OSS Distributions
146*2c2f96dcSApple OSS Distributions static void
release_thread_stack(txm_thread_stack_t * thread_stack)147*2c2f96dcSApple OSS Distributions release_thread_stack(
148*2c2f96dcSApple OSS Distributions txm_thread_stack_t* thread_stack)
149*2c2f96dcSApple OSS Distributions {
150*2c2f96dcSApple OSS Distributions /* Remove the TXM thread stack association with the current thread */
151*2c2f96dcSApple OSS Distributions thread_disassociate_txm_thread_stack(thread_stack->thread_stack_phys);
152*2c2f96dcSApple OSS Distributions
153*2c2f96dcSApple OSS Distributions /* Lock the thread stack list */
154*2c2f96dcSApple OSS Distributions lck_mtx_lock(&thread_stacks_lock);
155*2c2f96dcSApple OSS Distributions
156*2c2f96dcSApple OSS Distributions /* Add the thread stack at the list head */
157*2c2f96dcSApple OSS Distributions SLIST_INSERT_HEAD(&thread_stacks_head, thread_stack, link);
158*2c2f96dcSApple OSS Distributions
159*2c2f96dcSApple OSS Distributions /* Unlock the thread stack list */
160*2c2f96dcSApple OSS Distributions lck_mtx_unlock(&thread_stacks_lock);
161*2c2f96dcSApple OSS Distributions
162*2c2f96dcSApple OSS Distributions /* Wake up any threads waiting to acquire a thread stack */
163*2c2f96dcSApple OSS Distributions thread_wakeup(&thread_stack_event);
164*2c2f96dcSApple OSS Distributions }
165*2c2f96dcSApple OSS Distributions
166*2c2f96dcSApple OSS Distributions static kern_return_t
txm_parse_return(TXMReturn_t txm_ret)167*2c2f96dcSApple OSS Distributions txm_parse_return(
168*2c2f96dcSApple OSS Distributions TXMReturn_t txm_ret)
169*2c2f96dcSApple OSS Distributions {
170*2c2f96dcSApple OSS Distributions switch (txm_ret.returnCode) {
171*2c2f96dcSApple OSS Distributions case kTXMSuccess:
172*2c2f96dcSApple OSS Distributions return KERN_SUCCESS;
173*2c2f96dcSApple OSS Distributions
174*2c2f96dcSApple OSS Distributions case kTXMReturnOutOfMemory:
175*2c2f96dcSApple OSS Distributions return KERN_RESOURCE_SHORTAGE;
176*2c2f96dcSApple OSS Distributions
177*2c2f96dcSApple OSS Distributions case kTXMReturnNotFound:
178*2c2f96dcSApple OSS Distributions return KERN_NOT_FOUND;
179*2c2f96dcSApple OSS Distributions
180*2c2f96dcSApple OSS Distributions default:
181*2c2f96dcSApple OSS Distributions return KERN_FAILURE;
182*2c2f96dcSApple OSS Distributions }
183*2c2f96dcSApple OSS Distributions }
184*2c2f96dcSApple OSS Distributions
185*2c2f96dcSApple OSS Distributions static void
txm_print_return(TXMKernelSelector_t selector,TXMReturn_t txm_ret)186*2c2f96dcSApple OSS Distributions txm_print_return(
187*2c2f96dcSApple OSS Distributions TXMKernelSelector_t selector,
188*2c2f96dcSApple OSS Distributions TXMReturn_t txm_ret)
189*2c2f96dcSApple OSS Distributions {
190*2c2f96dcSApple OSS Distributions if (txm_ret.returnCode == kTXMSuccess) {
191*2c2f96dcSApple OSS Distributions return;
192*2c2f96dcSApple OSS Distributions } else if (txm_ret.returnCode == kTXMReturnTrustCache) {
193*2c2f96dcSApple OSS Distributions printf("TXM [Error]: TrustCache: selector: %u | 0x%02X | 0x%02X | %u\n",
194*2c2f96dcSApple OSS Distributions selector, txm_ret.tcRet.component, txm_ret.tcRet.error, txm_ret.tcRet.uniqueError);
195*2c2f96dcSApple OSS Distributions } else if (txm_ret.returnCode == kTXMReturnCodeSignature) {
196*2c2f96dcSApple OSS Distributions printf("TXM [Error]: CodeSignature: selector: %u | 0x%02X | 0x%02X | %u\n",
197*2c2f96dcSApple OSS Distributions selector, txm_ret.csRet.component, txm_ret.csRet.error, txm_ret.csRet.uniqueError);
198*2c2f96dcSApple OSS Distributions } else if (txm_ret.returnCode == kTXMReturnCodeErrno) {
199*2c2f96dcSApple OSS Distributions printf("TXM [Error]: Errno: selector: %u | %d\n",
200*2c2f96dcSApple OSS Distributions selector, txm_ret.errnoRet);
201*2c2f96dcSApple OSS Distributions } else {
202*2c2f96dcSApple OSS Distributions printf("TXM [Error]: selector: %u | %u\n",
203*2c2f96dcSApple OSS Distributions selector, txm_ret.returnCode);
204*2c2f96dcSApple OSS Distributions }
205*2c2f96dcSApple OSS Distributions }
206*2c2f96dcSApple OSS Distributions
207*2c2f96dcSApple OSS Distributions #pragma mark Page Allocation
208*2c2f96dcSApple OSS Distributions
209*2c2f96dcSApple OSS Distributions static void
txm_add_page(void)210*2c2f96dcSApple OSS Distributions txm_add_page(void)
211*2c2f96dcSApple OSS Distributions {
212*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
213*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorAddFreeListPage,
214*2c2f96dcSApple OSS Distributions .failure_fatal = true,
215*2c2f96dcSApple OSS Distributions .num_input_args = 1
216*2c2f96dcSApple OSS Distributions };
217*2c2f96dcSApple OSS Distributions
218*2c2f96dcSApple OSS Distributions /* Allocate a page from the VM -- transfers page to TXM internally */
219*2c2f96dcSApple OSS Distributions vm_map_address_t phys_addr = pmap_txm_allocate_page();
220*2c2f96dcSApple OSS Distributions
221*2c2f96dcSApple OSS Distributions /* Add this page to the TXM free list */
222*2c2f96dcSApple OSS Distributions txm_kernel_call(&txm_call, phys_addr);
223*2c2f96dcSApple OSS Distributions }
224*2c2f96dcSApple OSS Distributions
225*2c2f96dcSApple OSS Distributions #pragma mark Calls
226*2c2f96dcSApple OSS Distributions
227*2c2f96dcSApple OSS Distributions static void
txm_kernel_call_registers_setup(txm_call_t * parameters,sptm_call_regs_t * registers,va_list args)228*2c2f96dcSApple OSS Distributions txm_kernel_call_registers_setup(
229*2c2f96dcSApple OSS Distributions txm_call_t *parameters,
230*2c2f96dcSApple OSS Distributions sptm_call_regs_t *registers,
231*2c2f96dcSApple OSS Distributions va_list args)
232*2c2f96dcSApple OSS Distributions {
233*2c2f96dcSApple OSS Distributions /*
234*2c2f96dcSApple OSS Distributions * We are only ever allowed a maximum of 7 arguments for calling into TXM.
235*2c2f96dcSApple OSS Distributions * This is because the SPTM dispatch only sets up registers x0-x7 for the
236*2c2f96dcSApple OSS Distributions * call, and x0 is always reserved for passing in a thread stack for TXM
237*2c2f96dcSApple OSS Distributions * to operate on.
238*2c2f96dcSApple OSS Distributions */
239*2c2f96dcSApple OSS Distributions
240*2c2f96dcSApple OSS Distributions switch (parameters->num_input_args) {
241*2c2f96dcSApple OSS Distributions case 7:
242*2c2f96dcSApple OSS Distributions registers->x1 = va_arg(args, uintptr_t);
243*2c2f96dcSApple OSS Distributions registers->x2 = va_arg(args, uintptr_t);
244*2c2f96dcSApple OSS Distributions registers->x3 = va_arg(args, uintptr_t);
245*2c2f96dcSApple OSS Distributions registers->x4 = va_arg(args, uintptr_t);
246*2c2f96dcSApple OSS Distributions registers->x5 = va_arg(args, uintptr_t);
247*2c2f96dcSApple OSS Distributions registers->x6 = va_arg(args, uintptr_t);
248*2c2f96dcSApple OSS Distributions registers->x7 = va_arg(args, uintptr_t);
249*2c2f96dcSApple OSS Distributions break;
250*2c2f96dcSApple OSS Distributions
251*2c2f96dcSApple OSS Distributions case 6:
252*2c2f96dcSApple OSS Distributions registers->x1 = va_arg(args, uintptr_t);
253*2c2f96dcSApple OSS Distributions registers->x2 = va_arg(args, uintptr_t);
254*2c2f96dcSApple OSS Distributions registers->x3 = va_arg(args, uintptr_t);
255*2c2f96dcSApple OSS Distributions registers->x4 = va_arg(args, uintptr_t);
256*2c2f96dcSApple OSS Distributions registers->x5 = va_arg(args, uintptr_t);
257*2c2f96dcSApple OSS Distributions registers->x6 = va_arg(args, uintptr_t);
258*2c2f96dcSApple OSS Distributions break;
259*2c2f96dcSApple OSS Distributions
260*2c2f96dcSApple OSS Distributions case 5:
261*2c2f96dcSApple OSS Distributions registers->x1 = va_arg(args, uintptr_t);
262*2c2f96dcSApple OSS Distributions registers->x2 = va_arg(args, uintptr_t);
263*2c2f96dcSApple OSS Distributions registers->x3 = va_arg(args, uintptr_t);
264*2c2f96dcSApple OSS Distributions registers->x4 = va_arg(args, uintptr_t);
265*2c2f96dcSApple OSS Distributions registers->x5 = va_arg(args, uintptr_t);
266*2c2f96dcSApple OSS Distributions break;
267*2c2f96dcSApple OSS Distributions
268*2c2f96dcSApple OSS Distributions case 4:
269*2c2f96dcSApple OSS Distributions registers->x1 = va_arg(args, uintptr_t);
270*2c2f96dcSApple OSS Distributions registers->x2 = va_arg(args, uintptr_t);
271*2c2f96dcSApple OSS Distributions registers->x3 = va_arg(args, uintptr_t);
272*2c2f96dcSApple OSS Distributions registers->x4 = va_arg(args, uintptr_t);
273*2c2f96dcSApple OSS Distributions break;
274*2c2f96dcSApple OSS Distributions
275*2c2f96dcSApple OSS Distributions case 3:
276*2c2f96dcSApple OSS Distributions registers->x1 = va_arg(args, uintptr_t);
277*2c2f96dcSApple OSS Distributions registers->x2 = va_arg(args, uintptr_t);
278*2c2f96dcSApple OSS Distributions registers->x3 = va_arg(args, uintptr_t);
279*2c2f96dcSApple OSS Distributions break;
280*2c2f96dcSApple OSS Distributions
281*2c2f96dcSApple OSS Distributions case 2:
282*2c2f96dcSApple OSS Distributions registers->x1 = va_arg(args, uintptr_t);
283*2c2f96dcSApple OSS Distributions registers->x2 = va_arg(args, uintptr_t);
284*2c2f96dcSApple OSS Distributions break;
285*2c2f96dcSApple OSS Distributions
286*2c2f96dcSApple OSS Distributions case 1:
287*2c2f96dcSApple OSS Distributions registers->x1 = va_arg(args, uintptr_t);
288*2c2f96dcSApple OSS Distributions break;
289*2c2f96dcSApple OSS Distributions
290*2c2f96dcSApple OSS Distributions case 0:
291*2c2f96dcSApple OSS Distributions break;
292*2c2f96dcSApple OSS Distributions
293*2c2f96dcSApple OSS Distributions default:
294*2c2f96dcSApple OSS Distributions panic("invalid number of arguments to TXM: selector: %u | %u",
295*2c2f96dcSApple OSS Distributions parameters->selector, parameters->num_input_args);
296*2c2f96dcSApple OSS Distributions }
297*2c2f96dcSApple OSS Distributions }
298*2c2f96dcSApple OSS Distributions
299*2c2f96dcSApple OSS Distributions static TXMReturn_t
txm_kernel_call_internal(txm_call_t * parameters,va_list args)300*2c2f96dcSApple OSS Distributions txm_kernel_call_internal(
301*2c2f96dcSApple OSS Distributions txm_call_t *parameters,
302*2c2f96dcSApple OSS Distributions va_list args)
303*2c2f96dcSApple OSS Distributions {
304*2c2f96dcSApple OSS Distributions TXMReturn_t txm_ret = (TXMReturn_t){.returnCode = kTXMReturnGeneric};
305*2c2f96dcSApple OSS Distributions sptm_call_regs_t txm_registers = {0};
306*2c2f96dcSApple OSS Distributions txm_thread_stack_t *thread_stack = NULL;
307*2c2f96dcSApple OSS Distributions const TXMThreadStack_t *thread_stack_data = NULL;
308*2c2f96dcSApple OSS Distributions const TXMSharedContextData_t *shared_context_data = NULL;
309*2c2f96dcSApple OSS Distributions
310*2c2f96dcSApple OSS Distributions /* Obtain a stack for this call */
311*2c2f96dcSApple OSS Distributions thread_stack = acquire_thread_stack();
312*2c2f96dcSApple OSS Distributions thread_stack_data = thread_stack->thread_stack_data;
313*2c2f96dcSApple OSS Distributions shared_context_data = &thread_stack_data->sharedData;
314*2c2f96dcSApple OSS Distributions
315*2c2f96dcSApple OSS Distributions /* Setup argument registers */
316*2c2f96dcSApple OSS Distributions txm_registers.x0 = thread_stack->thread_stack_phys;
317*2c2f96dcSApple OSS Distributions txm_kernel_call_registers_setup(parameters, &txm_registers, args);
318*2c2f96dcSApple OSS Distributions
319*2c2f96dcSApple OSS Distributions /* Track resource usage */
320*2c2f96dcSApple OSS Distributions recount_enter_secure();
321*2c2f96dcSApple OSS Distributions
322*2c2f96dcSApple OSS Distributions /* Call into TXM */
323*2c2f96dcSApple OSS Distributions txm_enter(parameters->selector, &txm_registers);
324*2c2f96dcSApple OSS Distributions
325*2c2f96dcSApple OSS Distributions recount_leave_secure();
326*2c2f96dcSApple OSS Distributions
327*2c2f96dcSApple OSS Distributions txm_ret = (TXMReturn_t){.rawValue = shared_context_data->txmReturnCode};
328*2c2f96dcSApple OSS Distributions parameters->txm_ret = txm_ret;
329*2c2f96dcSApple OSS Distributions
330*2c2f96dcSApple OSS Distributions if (parameters->txm_ret.returnCode == kTXMSuccess) {
331*2c2f96dcSApple OSS Distributions parameters->num_return_words = shared_context_data->txmNumReturnWords;
332*2c2f96dcSApple OSS Distributions if (parameters->num_return_words > kTXMStackReturnWords) {
333*2c2f96dcSApple OSS Distributions panic("received excessive return words from TXM: selector: %u | %llu",
334*2c2f96dcSApple OSS Distributions parameters->selector, parameters->num_return_words);
335*2c2f96dcSApple OSS Distributions }
336*2c2f96dcSApple OSS Distributions
337*2c2f96dcSApple OSS Distributions for (uint64_t i = 0; i < parameters->num_return_words; i++) {
338*2c2f96dcSApple OSS Distributions parameters->return_words[i] = shared_context_data->txmReturnWords[i];
339*2c2f96dcSApple OSS Distributions }
340*2c2f96dcSApple OSS Distributions }
341*2c2f96dcSApple OSS Distributions
342*2c2f96dcSApple OSS Distributions /* Release the thread stack as it is no longer needed */
343*2c2f96dcSApple OSS Distributions release_thread_stack(thread_stack);
344*2c2f96dcSApple OSS Distributions thread_stack_data = NULL;
345*2c2f96dcSApple OSS Distributions shared_context_data = NULL;
346*2c2f96dcSApple OSS Distributions
347*2c2f96dcSApple OSS Distributions return txm_ret;
348*2c2f96dcSApple OSS Distributions }
349*2c2f96dcSApple OSS Distributions
350*2c2f96dcSApple OSS Distributions kern_return_t
txm_kernel_call(txm_call_t * parameters,...)351*2c2f96dcSApple OSS Distributions txm_kernel_call(
352*2c2f96dcSApple OSS Distributions txm_call_t *parameters, ...)
353*2c2f96dcSApple OSS Distributions {
354*2c2f96dcSApple OSS Distributions TXMReturn_t txm_ret = (TXMReturn_t){.returnCode = kTXMReturnGeneric};
355*2c2f96dcSApple OSS Distributions kern_return_t ret = KERN_DENIED;
356*2c2f96dcSApple OSS Distributions va_list args;
357*2c2f96dcSApple OSS Distributions
358*2c2f96dcSApple OSS Distributions /* Start the variadic arguments list */
359*2c2f96dcSApple OSS Distributions va_start(args, parameters);
360*2c2f96dcSApple OSS Distributions
361*2c2f96dcSApple OSS Distributions do {
362*2c2f96dcSApple OSS Distributions txm_ret = txm_kernel_call_internal(parameters, args);
363*2c2f96dcSApple OSS Distributions if (txm_ret.returnCode == kTXMReturnOutOfMemory) {
364*2c2f96dcSApple OSS Distributions if (parameters->selector == kTXMKernelSelectorAddFreeListPage) {
365*2c2f96dcSApple OSS Distributions panic("received out-of-memory error when adding a free page to TXM");
366*2c2f96dcSApple OSS Distributions }
367*2c2f96dcSApple OSS Distributions txm_add_page();
368*2c2f96dcSApple OSS Distributions }
369*2c2f96dcSApple OSS Distributions } while (txm_ret.returnCode == kTXMReturnOutOfMemory);
370*2c2f96dcSApple OSS Distributions
371*2c2f96dcSApple OSS Distributions /* Clean up the variadic arguments list */
372*2c2f96dcSApple OSS Distributions va_end(args);
373*2c2f96dcSApple OSS Distributions
374*2c2f96dcSApple OSS Distributions /* Print all TXM logs from the log buffer */
375*2c2f96dcSApple OSS Distributions if (parameters->skip_logs == false) {
376*2c2f96dcSApple OSS Distributions txm_print_logs();
377*2c2f96dcSApple OSS Distributions }
378*2c2f96dcSApple OSS Distributions
379*2c2f96dcSApple OSS Distributions /* Print the return code from TXM -- only prints for an error */
380*2c2f96dcSApple OSS Distributions if (parameters->failure_silent != true) {
381*2c2f96dcSApple OSS Distributions if (parameters->failure_code_silent != txm_ret.returnCode) {
382*2c2f96dcSApple OSS Distributions txm_print_return(parameters->selector, txm_ret);
383*2c2f96dcSApple OSS Distributions }
384*2c2f96dcSApple OSS Distributions }
385*2c2f96dcSApple OSS Distributions
386*2c2f96dcSApple OSS Distributions /*
387*2c2f96dcSApple OSS Distributions * To ease the process of calling into TXM, and to also reduce the number of
388*2c2f96dcSApple OSS Distributions * lines of code for each call site, the txm_call_t offers some properties
389*2c2f96dcSApple OSS Distributions * we can enforce over here. Go through these, and panic in case they aren't
390*2c2f96dcSApple OSS Distributions * honored.
391*2c2f96dcSApple OSS Distributions *
392*2c2f96dcSApple OSS Distributions * NOTE: We check for "<" instead of "!=" for the number of return words we
393*2c2f96dcSApple OSS Distributions * get back from TXM since this helps in forward development. If the kernel
394*2c2f96dcSApple OSS Distributions * and TXM are proceeding at different project cadences, we do not want to
395*2c2f96dcSApple OSS Distributions * gate adding more return words from TXM on the kernel first adopting the
396*2c2f96dcSApple OSS Distributions * new number of return words.
397*2c2f96dcSApple OSS Distributions */
398*2c2f96dcSApple OSS Distributions ret = txm_parse_return(txm_ret);
399*2c2f96dcSApple OSS Distributions
400*2c2f96dcSApple OSS Distributions if (parameters->failure_fatal && (ret != KERN_SUCCESS)) {
401*2c2f96dcSApple OSS Distributions panic("received fatal error for a selector from TXM: selector: %u | 0x%0llX",
402*2c2f96dcSApple OSS Distributions parameters->selector, txm_ret.rawValue);
403*2c2f96dcSApple OSS Distributions } else if (parameters->num_return_words < parameters->num_output_args) {
404*2c2f96dcSApple OSS Distributions /* Only panic if return was a success */
405*2c2f96dcSApple OSS Distributions if (ret == KERN_SUCCESS) {
406*2c2f96dcSApple OSS Distributions panic("received fewer than expected return words from TXM: selector: %u | %llu",
407*2c2f96dcSApple OSS Distributions parameters->selector, parameters->num_return_words);
408*2c2f96dcSApple OSS Distributions }
409*2c2f96dcSApple OSS Distributions }
410*2c2f96dcSApple OSS Distributions
411*2c2f96dcSApple OSS Distributions return ret;
412*2c2f96dcSApple OSS Distributions }
413*2c2f96dcSApple OSS Distributions
414*2c2f96dcSApple OSS Distributions void
txm_transfer_region(vm_address_t addr,vm_size_t size)415*2c2f96dcSApple OSS Distributions txm_transfer_region(
416*2c2f96dcSApple OSS Distributions vm_address_t addr,
417*2c2f96dcSApple OSS Distributions vm_size_t size)
418*2c2f96dcSApple OSS Distributions {
419*2c2f96dcSApple OSS Distributions vm_address_t addr_end = 0;
420*2c2f96dcSApple OSS Distributions vm_size_t size_aligned = round_page(size);
421*2c2f96dcSApple OSS Distributions
422*2c2f96dcSApple OSS Distributions if ((addr & PAGE_MASK) != 0) {
423*2c2f96dcSApple OSS Distributions panic("attempted to transfer non-page-aligned memory to TXM: %p", (void*)addr);
424*2c2f96dcSApple OSS Distributions } else if (os_add_overflow(addr, size_aligned, &addr_end)) {
425*2c2f96dcSApple OSS Distributions panic("overflow on range to be transferred to TXM: %p | %lu",
426*2c2f96dcSApple OSS Distributions (void*)addr, size);
427*2c2f96dcSApple OSS Distributions }
428*2c2f96dcSApple OSS Distributions
429*2c2f96dcSApple OSS Distributions /* Make the memory read-only first (transfer will panic otherwise) */
430*2c2f96dcSApple OSS Distributions vm_protect(kernel_map, addr, size_aligned, false, VM_PROT_READ);
431*2c2f96dcSApple OSS Distributions
432*2c2f96dcSApple OSS Distributions /* Transfer each physical page to be TXM_DEFAULT */
433*2c2f96dcSApple OSS Distributions for (vm_address_t page = addr; page < addr_end; page += PAGE_SIZE) {
434*2c2f96dcSApple OSS Distributions pmap_txm_transfer_page(page);
435*2c2f96dcSApple OSS Distributions }
436*2c2f96dcSApple OSS Distributions }
437*2c2f96dcSApple OSS Distributions
438*2c2f96dcSApple OSS Distributions void
txm_reclaim_region(vm_address_t addr,vm_size_t size)439*2c2f96dcSApple OSS Distributions txm_reclaim_region(
440*2c2f96dcSApple OSS Distributions vm_address_t addr,
441*2c2f96dcSApple OSS Distributions vm_size_t size)
442*2c2f96dcSApple OSS Distributions {
443*2c2f96dcSApple OSS Distributions vm_address_t addr_end = 0;
444*2c2f96dcSApple OSS Distributions vm_size_t size_aligned = round_page(size);
445*2c2f96dcSApple OSS Distributions
446*2c2f96dcSApple OSS Distributions if ((addr & PAGE_MASK) != 0) {
447*2c2f96dcSApple OSS Distributions panic("attempted to reclaim non-page-aligned memory from TXM: %p", (void*)addr);
448*2c2f96dcSApple OSS Distributions } else if (os_add_overflow(addr, size_aligned, &addr_end)) {
449*2c2f96dcSApple OSS Distributions panic("overflow on range to be reclaimed from TXM: %p | %lu",
450*2c2f96dcSApple OSS Distributions (void*)addr, size);
451*2c2f96dcSApple OSS Distributions }
452*2c2f96dcSApple OSS Distributions
453*2c2f96dcSApple OSS Distributions /*
454*2c2f96dcSApple OSS Distributions * We can only reclaim once TXM has transferred the memory range back to the
455*2c2f96dcSApple OSS Distributions * kernel. Hence, we simply try and switch permissions to read-write. If TXM
456*2c2f96dcSApple OSS Distributions * hasn't transferred pages, this then should panic.
457*2c2f96dcSApple OSS Distributions */
458*2c2f96dcSApple OSS Distributions vm_protect(kernel_map, addr, size_aligned, false, VM_PROT_READ | VM_PROT_WRITE);
459*2c2f96dcSApple OSS Distributions }
460*2c2f96dcSApple OSS Distributions
461*2c2f96dcSApple OSS Distributions static SECURITY_READ_ONLY_LATE(const char*) txm_log_page = NULL;
462*2c2f96dcSApple OSS Distributions static SECURITY_READ_ONLY_LATE(const uint32_t*) txm_log_head = NULL;
463*2c2f96dcSApple OSS Distributions static SECURITY_READ_ONLY_LATE(const uint32_t*) txm_log_sync = NULL;
464*2c2f96dcSApple OSS Distributions
465*2c2f96dcSApple OSS Distributions static decl_lck_mtx_data(, log_lock);
466*2c2f96dcSApple OSS Distributions static uint32_t log_head = 0;
467*2c2f96dcSApple OSS Distributions
468*2c2f96dcSApple OSS Distributions void
txm_print_logs(void)469*2c2f96dcSApple OSS Distributions txm_print_logs(void)
470*2c2f96dcSApple OSS Distributions {
471*2c2f96dcSApple OSS Distributions uint32_t start_index = 0;
472*2c2f96dcSApple OSS Distributions uint32_t end_index = 0;
473*2c2f96dcSApple OSS Distributions
474*2c2f96dcSApple OSS Distributions /*
475*2c2f96dcSApple OSS Distributions * The design here is very simple. TXM keeps adding slots to its circular buffer
476*2c2f96dcSApple OSS Distributions * and the kernel attempts to read each one and print it, maintaining its own head
477*2c2f96dcSApple OSS Distributions * for the log.
478*2c2f96dcSApple OSS Distributions *
479*2c2f96dcSApple OSS Distributions * This design is by nature lazy. TXM doesn't know or care if the kernel has gone
480*2c2f96dcSApple OSS Distributions * through and printed any of the logs, so it'll just keep writing into its buffer
481*2c2f96dcSApple OSS Distributions * and then circle around when it becomes full.
482*2c2f96dcSApple OSS Distributions *
483*2c2f96dcSApple OSS Distributions * This is fine most of the time since there are a decent amount of slots in the
484*2c2f96dcSApple OSS Distributions * log buffer. We mostly have an issue when TXM is adding so many logs so quickly
485*2c2f96dcSApple OSS Distributions * such that it wraps around and starts overwriting logs which haven't been seen
486*2c2f96dcSApple OSS Distributions * by the kernel. If this were to happen, TXM's log head may circle around the
487*2c2f96dcSApple OSS Distributions * head maintained by the kernel, causing a lot of logs to be missed, since the
488*2c2f96dcSApple OSS Distributions * kernel only attempts the number of logs in-between the two heads.
489*2c2f96dcSApple OSS Distributions *
490*2c2f96dcSApple OSS Distributions * The fix for that is complicated, and until we see an actual impact, we're going
491*2c2f96dcSApple OSS Distributions * to keep the simpler design in place.
492*2c2f96dcSApple OSS Distributions */
493*2c2f96dcSApple OSS Distributions
494*2c2f96dcSApple OSS Distributions /* Return if the logging hasn't been setup yet */
495*2c2f96dcSApple OSS Distributions if (txm_log_sync == NULL) {
496*2c2f96dcSApple OSS Distributions return;
497*2c2f96dcSApple OSS Distributions }
498*2c2f96dcSApple OSS Distributions
499*2c2f96dcSApple OSS Distributions /*
500*2c2f96dcSApple OSS Distributions * Holding the log lock and printing can cause lots of issues since printing can
501*2c2f96dcSApple OSS Distributions * be rather slow. While we make it a point to keep the logging buffer quiet, some
502*2c2f96dcSApple OSS Distributions * actions (such as loading trust caches) are still very chatty.
503*2c2f96dcSApple OSS Distributions *
504*2c2f96dcSApple OSS Distributions * As a result, we optimize this routine to ensure that the lock itself isn't held
505*2c2f96dcSApple OSS Distributions * for very long. All we need to do within the critical section is calculate the
506*2c2f96dcSApple OSS Distributions * starting and ending index of the log buffer. The actual printing doesn't need
507*2c2f96dcSApple OSS Distributions * to be done with the lock held.
508*2c2f96dcSApple OSS Distributions */
509*2c2f96dcSApple OSS Distributions lck_mtx_lock(&log_lock);
510*2c2f96dcSApple OSS Distributions
511*2c2f96dcSApple OSS Distributions start_index = log_head;
512*2c2f96dcSApple OSS Distributions end_index = os_atomic_load(txm_log_head, relaxed) % kTXMLogSlots;
513*2c2f96dcSApple OSS Distributions
514*2c2f96dcSApple OSS Distributions /* Update the log head with the new index */
515*2c2f96dcSApple OSS Distributions log_head = end_index;
516*2c2f96dcSApple OSS Distributions
517*2c2f96dcSApple OSS Distributions /* Release the log lock */
518*2c2f96dcSApple OSS Distributions lck_mtx_unlock(&log_lock);
519*2c2f96dcSApple OSS Distributions
520*2c2f96dcSApple OSS Distributions if (start_index != end_index) {
521*2c2f96dcSApple OSS Distributions /* Use load acquire here to sync up with all writes to the buffer */
522*2c2f96dcSApple OSS Distributions os_atomic_load(txm_log_sync, acquire);
523*2c2f96dcSApple OSS Distributions
524*2c2f96dcSApple OSS Distributions while (start_index != end_index) {
525*2c2f96dcSApple OSS Distributions const char *slot = txm_log_page + (start_index * kTXMLogSlotSize);
526*2c2f96dcSApple OSS Distributions
527*2c2f96dcSApple OSS Distributions /* We add newlines after each log statement since TXM does not */
528*2c2f96dcSApple OSS Distributions printf("%s\n", slot);
529*2c2f96dcSApple OSS Distributions
530*2c2f96dcSApple OSS Distributions start_index = (start_index + 1) % kTXMLogSlots;
531*2c2f96dcSApple OSS Distributions }
532*2c2f96dcSApple OSS Distributions }
533*2c2f96dcSApple OSS Distributions }
534*2c2f96dcSApple OSS Distributions
535*2c2f96dcSApple OSS Distributions #pragma mark Initialization
536*2c2f96dcSApple OSS Distributions
537*2c2f96dcSApple OSS Distributions SECURITY_READ_ONLY_LATE(const TXMReadOnlyData_t*) txm_ro_data = NULL;
538*2c2f96dcSApple OSS Distributions SECURITY_READ_ONLY_LATE(const TXMStatistics_t*) txm_stats = NULL;
539*2c2f96dcSApple OSS Distributions SECURITY_READ_ONLY_LATE(const CSConfig_t*) txm_cs_config = NULL;
540*2c2f96dcSApple OSS Distributions
541*2c2f96dcSApple OSS Distributions SECURITY_READ_ONLY_LATE(bool*) developer_mode_enabled = NULL;
542*2c2f96dcSApple OSS Distributions static SECURITY_READ_ONLY_LATE(bool) code_signing_enabled = true;
543*2c2f96dcSApple OSS Distributions static SECURITY_READ_ONLY_LATE(uint32_t) managed_signature_size = 0;
544*2c2f96dcSApple OSS Distributions
545*2c2f96dcSApple OSS Distributions static decl_lck_mtx_data(, compilation_service_lock);
546*2c2f96dcSApple OSS Distributions static decl_lck_mtx_data(, unregister_sync_lock);
547*2c2f96dcSApple OSS Distributions
548*2c2f96dcSApple OSS Distributions static void
get_logging_info(void)549*2c2f96dcSApple OSS Distributions get_logging_info(void)
550*2c2f96dcSApple OSS Distributions {
551*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
552*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorGetLogInfo,
553*2c2f96dcSApple OSS Distributions .failure_fatal = true,
554*2c2f96dcSApple OSS Distributions .num_output_args = 3
555*2c2f96dcSApple OSS Distributions };
556*2c2f96dcSApple OSS Distributions txm_kernel_call(&txm_call);
557*2c2f96dcSApple OSS Distributions
558*2c2f96dcSApple OSS Distributions txm_log_page = (const char*)txm_call.return_words[0];
559*2c2f96dcSApple OSS Distributions txm_log_head = (const uint32_t*)txm_call.return_words[1];
560*2c2f96dcSApple OSS Distributions txm_log_sync = (const uint32_t*)txm_call.return_words[2];
561*2c2f96dcSApple OSS Distributions }
562*2c2f96dcSApple OSS Distributions
563*2c2f96dcSApple OSS Distributions static void
get_code_signing_info(void)564*2c2f96dcSApple OSS Distributions get_code_signing_info(void)
565*2c2f96dcSApple OSS Distributions {
566*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
567*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorGetCodeSigningInfo,
568*2c2f96dcSApple OSS Distributions .failure_fatal = true,
569*2c2f96dcSApple OSS Distributions .num_output_args = 6
570*2c2f96dcSApple OSS Distributions };
571*2c2f96dcSApple OSS Distributions txm_kernel_call(&txm_call);
572*2c2f96dcSApple OSS Distributions
573*2c2f96dcSApple OSS Distributions /*
574*2c2f96dcSApple OSS Distributions * Not using txm_call.return_words[0] for now. This was previously the
575*2c2f96dcSApple OSS Distributions * code_signing_enabled field, but we've since switched to acquiring that
576*2c2f96dcSApple OSS Distributions * value from TXM's read-only data.
577*2c2f96dcSApple OSS Distributions *
578*2c2f96dcSApple OSS Distributions * Not using txm_call.return_words[4] for now. This was previously the
579*2c2f96dcSApple OSS Distributions * txm_cs_config field, but we've since switched to acquiring that value
580*2c2f96dcSApple OSS Distributions * from TXM's read-only data.
581*2c2f96dcSApple OSS Distributions */
582*2c2f96dcSApple OSS Distributions
583*2c2f96dcSApple OSS Distributions developer_mode_enabled = (bool*)txm_call.return_words[1];
584*2c2f96dcSApple OSS Distributions txm_stats = (TXMStatistics_t*)txm_call.return_words[2];
585*2c2f96dcSApple OSS Distributions managed_signature_size = (uint32_t)txm_call.return_words[3];
586*2c2f96dcSApple OSS Distributions txm_ro_data = (TXMReadOnlyData_t*)txm_call.return_words[5];
587*2c2f96dcSApple OSS Distributions
588*2c2f96dcSApple OSS Distributions /* Set code_signing_disabled based on read-only data */
589*2c2f96dcSApple OSS Distributions code_signing_enabled = txm_ro_data->codeSigningDisabled == false;
590*2c2f96dcSApple OSS Distributions
591*2c2f96dcSApple OSS Distributions /* Set txm_cs_config based on read-only data */
592*2c2f96dcSApple OSS Distributions txm_cs_config = &txm_ro_data->CSConfiguration;
593*2c2f96dcSApple OSS Distributions }
594*2c2f96dcSApple OSS Distributions
595*2c2f96dcSApple OSS Distributions static void
set_shared_region_base_address(void)596*2c2f96dcSApple OSS Distributions set_shared_region_base_address(void)
597*2c2f96dcSApple OSS Distributions {
598*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
599*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorSetSharedRegionBaseAddress,
600*2c2f96dcSApple OSS Distributions .failure_fatal = true,
601*2c2f96dcSApple OSS Distributions .num_input_args = 2,
602*2c2f96dcSApple OSS Distributions };
603*2c2f96dcSApple OSS Distributions
604*2c2f96dcSApple OSS Distributions txm_kernel_call(&txm_call,
605*2c2f96dcSApple OSS Distributions SHARED_REGION_BASE,
606*2c2f96dcSApple OSS Distributions SHARED_REGION_SIZE);
607*2c2f96dcSApple OSS Distributions }
608*2c2f96dcSApple OSS Distributions
609*2c2f96dcSApple OSS Distributions void
code_signing_init(void)610*2c2f96dcSApple OSS Distributions code_signing_init(void)
611*2c2f96dcSApple OSS Distributions {
612*2c2f96dcSApple OSS Distributions /* Setup the thread stacks used by TXM */
613*2c2f96dcSApple OSS Distributions setup_thread_stacks();
614*2c2f96dcSApple OSS Distributions
615*2c2f96dcSApple OSS Distributions /* Setup the logging lock */
616*2c2f96dcSApple OSS Distributions lck_mtx_init(&log_lock, &txm_lck_grp, 0);
617*2c2f96dcSApple OSS Distributions
618*2c2f96dcSApple OSS Distributions /* Setup TXM logging information */
619*2c2f96dcSApple OSS Distributions get_logging_info();
620*2c2f96dcSApple OSS Distributions
621*2c2f96dcSApple OSS Distributions /* Setup code signing configuration */
622*2c2f96dcSApple OSS Distributions get_code_signing_info();
623*2c2f96dcSApple OSS Distributions
624*2c2f96dcSApple OSS Distributions /* Setup all the other locks we need */
625*2c2f96dcSApple OSS Distributions lck_mtx_init(&compilation_service_lock, &txm_lck_grp, 0);
626*2c2f96dcSApple OSS Distributions lck_mtx_init(&unregister_sync_lock, &txm_lck_grp, 0);
627*2c2f96dcSApple OSS Distributions
628*2c2f96dcSApple OSS Distributions /*
629*2c2f96dcSApple OSS Distributions * We need to let TXM know what the shared region base address is going
630*2c2f96dcSApple OSS Distributions * to be for this boot.
631*2c2f96dcSApple OSS Distributions */
632*2c2f96dcSApple OSS Distributions set_shared_region_base_address();
633*2c2f96dcSApple OSS Distributions
634*2c2f96dcSApple OSS Distributions /* Require signed code when monitor is enabled */
635*2c2f96dcSApple OSS Distributions if (code_signing_enabled == true) {
636*2c2f96dcSApple OSS Distributions cs_debug_fail_on_unsigned_code = 1;
637*2c2f96dcSApple OSS Distributions }
638*2c2f96dcSApple OSS Distributions }
639*2c2f96dcSApple OSS Distributions
640*2c2f96dcSApple OSS Distributions void
txm_enter_lockdown_mode(void)641*2c2f96dcSApple OSS Distributions txm_enter_lockdown_mode(void)
642*2c2f96dcSApple OSS Distributions {
643*2c2f96dcSApple OSS Distributions #if kTXMKernelAPIVersion >= 3
644*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
645*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorEnterLockdownMode,
646*2c2f96dcSApple OSS Distributions .failure_fatal = true,
647*2c2f96dcSApple OSS Distributions };
648*2c2f96dcSApple OSS Distributions
649*2c2f96dcSApple OSS Distributions txm_kernel_call(&txm_call);
650*2c2f96dcSApple OSS Distributions #endif
651*2c2f96dcSApple OSS Distributions }
652*2c2f96dcSApple OSS Distributions
653*2c2f96dcSApple OSS Distributions #pragma mark Developer Mode
654*2c2f96dcSApple OSS Distributions
655*2c2f96dcSApple OSS Distributions void
txm_toggle_developer_mode(bool state)656*2c2f96dcSApple OSS Distributions txm_toggle_developer_mode(bool state)
657*2c2f96dcSApple OSS Distributions {
658*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
659*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorDeveloperModeToggle,
660*2c2f96dcSApple OSS Distributions .failure_fatal = true,
661*2c2f96dcSApple OSS Distributions .num_input_args = 1
662*2c2f96dcSApple OSS Distributions };
663*2c2f96dcSApple OSS Distributions
664*2c2f96dcSApple OSS Distributions txm_kernel_call(&txm_call, state);
665*2c2f96dcSApple OSS Distributions }
666*2c2f96dcSApple OSS Distributions
667*2c2f96dcSApple OSS Distributions #pragma mark Code Signing and Provisioning Profiles
668*2c2f96dcSApple OSS Distributions
669*2c2f96dcSApple OSS Distributions bool
txm_code_signing_enabled(void)670*2c2f96dcSApple OSS Distributions txm_code_signing_enabled(void)
671*2c2f96dcSApple OSS Distributions {
672*2c2f96dcSApple OSS Distributions return code_signing_enabled;
673*2c2f96dcSApple OSS Distributions }
674*2c2f96dcSApple OSS Distributions
675*2c2f96dcSApple OSS Distributions vm_size_t
txm_managed_code_signature_size(void)676*2c2f96dcSApple OSS Distributions txm_managed_code_signature_size(void)
677*2c2f96dcSApple OSS Distributions {
678*2c2f96dcSApple OSS Distributions return managed_signature_size;
679*2c2f96dcSApple OSS Distributions }
680*2c2f96dcSApple OSS Distributions
681*2c2f96dcSApple OSS Distributions kern_return_t
txm_register_provisioning_profile(const void * profile_blob,const size_t profile_blob_size,void ** profile_obj)682*2c2f96dcSApple OSS Distributions txm_register_provisioning_profile(
683*2c2f96dcSApple OSS Distributions const void *profile_blob,
684*2c2f96dcSApple OSS Distributions const size_t profile_blob_size,
685*2c2f96dcSApple OSS Distributions void **profile_obj)
686*2c2f96dcSApple OSS Distributions {
687*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
688*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorRegisterProvisioningProfile,
689*2c2f96dcSApple OSS Distributions .num_input_args = 2,
690*2c2f96dcSApple OSS Distributions .num_output_args = 1
691*2c2f96dcSApple OSS Distributions };
692*2c2f96dcSApple OSS Distributions vm_address_t payload_addr = 0;
693*2c2f96dcSApple OSS Distributions kern_return_t ret = KERN_DENIED;
694*2c2f96dcSApple OSS Distributions
695*2c2f96dcSApple OSS Distributions /* We need to allocate page-wise in order to transfer the range to TXM */
696*2c2f96dcSApple OSS Distributions ret = kmem_alloc(kernel_map, &payload_addr, profile_blob_size,
697*2c2f96dcSApple OSS Distributions KMA_KOBJECT | KMA_DATA, VM_KERN_MEMORY_SECURITY);
698*2c2f96dcSApple OSS Distributions if (ret != KERN_SUCCESS) {
699*2c2f96dcSApple OSS Distributions printf("unable to allocate memory for profile payload: %d\n", ret);
700*2c2f96dcSApple OSS Distributions goto exit;
701*2c2f96dcSApple OSS Distributions }
702*2c2f96dcSApple OSS Distributions
703*2c2f96dcSApple OSS Distributions /* Copy the contents into the allocation */
704*2c2f96dcSApple OSS Distributions memcpy((void*)payload_addr, profile_blob, profile_blob_size);
705*2c2f96dcSApple OSS Distributions
706*2c2f96dcSApple OSS Distributions /* Transfer the memory range to TXM */
707*2c2f96dcSApple OSS Distributions txm_transfer_region(payload_addr, profile_blob_size);
708*2c2f96dcSApple OSS Distributions
709*2c2f96dcSApple OSS Distributions ret = txm_kernel_call(&txm_call, payload_addr, profile_blob_size);
710*2c2f96dcSApple OSS Distributions if (ret == KERN_SUCCESS) {
711*2c2f96dcSApple OSS Distributions *profile_obj = (void*)txm_call.return_words[0];
712*2c2f96dcSApple OSS Distributions }
713*2c2f96dcSApple OSS Distributions
714*2c2f96dcSApple OSS Distributions exit:
715*2c2f96dcSApple OSS Distributions if ((ret != KERN_SUCCESS) && (payload_addr != 0)) {
716*2c2f96dcSApple OSS Distributions /* Reclaim this memory range */
717*2c2f96dcSApple OSS Distributions txm_reclaim_region(payload_addr, profile_blob_size);
718*2c2f96dcSApple OSS Distributions
719*2c2f96dcSApple OSS Distributions /* Free the memory range */
720*2c2f96dcSApple OSS Distributions kmem_free(kernel_map, payload_addr, profile_blob_size);
721*2c2f96dcSApple OSS Distributions payload_addr = 0;
722*2c2f96dcSApple OSS Distributions }
723*2c2f96dcSApple OSS Distributions
724*2c2f96dcSApple OSS Distributions return ret;
725*2c2f96dcSApple OSS Distributions }
726*2c2f96dcSApple OSS Distributions
727*2c2f96dcSApple OSS Distributions kern_return_t
txm_unregister_provisioning_profile(void * profile_obj)728*2c2f96dcSApple OSS Distributions txm_unregister_provisioning_profile(
729*2c2f96dcSApple OSS Distributions void *profile_obj)
730*2c2f96dcSApple OSS Distributions {
731*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
732*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorUnregisterProvisioningProfile,
733*2c2f96dcSApple OSS Distributions .num_input_args = 1,
734*2c2f96dcSApple OSS Distributions .num_output_args = 2
735*2c2f96dcSApple OSS Distributions };
736*2c2f96dcSApple OSS Distributions vm_address_t profile_addr = 0;
737*2c2f96dcSApple OSS Distributions vm_size_t profile_size = 0;
738*2c2f96dcSApple OSS Distributions kern_return_t ret = KERN_DENIED;
739*2c2f96dcSApple OSS Distributions
740*2c2f96dcSApple OSS Distributions ret = txm_kernel_call(&txm_call, profile_obj);
741*2c2f96dcSApple OSS Distributions if (ret != KERN_SUCCESS) {
742*2c2f96dcSApple OSS Distributions return ret;
743*2c2f96dcSApple OSS Distributions }
744*2c2f96dcSApple OSS Distributions
745*2c2f96dcSApple OSS Distributions profile_addr = txm_call.return_words[0];
746*2c2f96dcSApple OSS Distributions profile_size = txm_call.return_words[1];
747*2c2f96dcSApple OSS Distributions
748*2c2f96dcSApple OSS Distributions /* Reclaim this memory range */
749*2c2f96dcSApple OSS Distributions txm_reclaim_region(profile_addr, profile_size);
750*2c2f96dcSApple OSS Distributions
751*2c2f96dcSApple OSS Distributions /* Free the memory range */
752*2c2f96dcSApple OSS Distributions kmem_free(kernel_map, profile_addr, profile_size);
753*2c2f96dcSApple OSS Distributions
754*2c2f96dcSApple OSS Distributions return KERN_SUCCESS;
755*2c2f96dcSApple OSS Distributions }
756*2c2f96dcSApple OSS Distributions
757*2c2f96dcSApple OSS Distributions kern_return_t
txm_associate_provisioning_profile(void * sig_obj,void * profile_obj)758*2c2f96dcSApple OSS Distributions txm_associate_provisioning_profile(
759*2c2f96dcSApple OSS Distributions void *sig_obj,
760*2c2f96dcSApple OSS Distributions void *profile_obj)
761*2c2f96dcSApple OSS Distributions {
762*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
763*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorAssociateProvisioningProfile,
764*2c2f96dcSApple OSS Distributions .num_input_args = 2,
765*2c2f96dcSApple OSS Distributions };
766*2c2f96dcSApple OSS Distributions
767*2c2f96dcSApple OSS Distributions return txm_kernel_call(&txm_call, sig_obj, profile_obj);
768*2c2f96dcSApple OSS Distributions }
769*2c2f96dcSApple OSS Distributions
770*2c2f96dcSApple OSS Distributions kern_return_t
txm_disassociate_provisioning_profile(void * sig_obj)771*2c2f96dcSApple OSS Distributions txm_disassociate_provisioning_profile(
772*2c2f96dcSApple OSS Distributions void *sig_obj)
773*2c2f96dcSApple OSS Distributions {
774*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
775*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorDisassociateProvisioningProfile,
776*2c2f96dcSApple OSS Distributions .num_input_args = 1,
777*2c2f96dcSApple OSS Distributions };
778*2c2f96dcSApple OSS Distributions
779*2c2f96dcSApple OSS Distributions /*
780*2c2f96dcSApple OSS Distributions * Take the unregistration sync lock.
781*2c2f96dcSApple OSS Distributions * For more information: rdar://99205627.
782*2c2f96dcSApple OSS Distributions */
783*2c2f96dcSApple OSS Distributions lck_mtx_lock(&unregister_sync_lock);
784*2c2f96dcSApple OSS Distributions
785*2c2f96dcSApple OSS Distributions /* Disassociate the profile from the signature */
786*2c2f96dcSApple OSS Distributions kern_return_t ret = txm_kernel_call(&txm_call, sig_obj);
787*2c2f96dcSApple OSS Distributions
788*2c2f96dcSApple OSS Distributions /* Release the unregistration sync lock */
789*2c2f96dcSApple OSS Distributions lck_mtx_unlock(&unregister_sync_lock);
790*2c2f96dcSApple OSS Distributions
791*2c2f96dcSApple OSS Distributions return ret;
792*2c2f96dcSApple OSS Distributions }
793*2c2f96dcSApple OSS Distributions
794*2c2f96dcSApple OSS Distributions void
txm_set_compilation_service_cdhash(const uint8_t cdhash[CS_CDHASH_LEN])795*2c2f96dcSApple OSS Distributions txm_set_compilation_service_cdhash(
796*2c2f96dcSApple OSS Distributions const uint8_t cdhash[CS_CDHASH_LEN])
797*2c2f96dcSApple OSS Distributions {
798*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
799*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorAuthorizeCompilationServiceCDHash,
800*2c2f96dcSApple OSS Distributions .num_input_args = 1,
801*2c2f96dcSApple OSS Distributions };
802*2c2f96dcSApple OSS Distributions
803*2c2f96dcSApple OSS Distributions lck_mtx_lock(&compilation_service_lock);
804*2c2f96dcSApple OSS Distributions txm_kernel_call(&txm_call, cdhash);
805*2c2f96dcSApple OSS Distributions lck_mtx_unlock(&compilation_service_lock);
806*2c2f96dcSApple OSS Distributions }
807*2c2f96dcSApple OSS Distributions
808*2c2f96dcSApple OSS Distributions bool
txm_match_compilation_service_cdhash(const uint8_t cdhash[CS_CDHASH_LEN])809*2c2f96dcSApple OSS Distributions txm_match_compilation_service_cdhash(
810*2c2f96dcSApple OSS Distributions const uint8_t cdhash[CS_CDHASH_LEN])
811*2c2f96dcSApple OSS Distributions {
812*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
813*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorMatchCompilationServiceCDHash,
814*2c2f96dcSApple OSS Distributions .failure_silent = true,
815*2c2f96dcSApple OSS Distributions .num_input_args = 1,
816*2c2f96dcSApple OSS Distributions .num_output_args = 1,
817*2c2f96dcSApple OSS Distributions };
818*2c2f96dcSApple OSS Distributions kern_return_t ret = KERN_DENIED;
819*2c2f96dcSApple OSS Distributions
820*2c2f96dcSApple OSS Distributions /* Be safe and take the lock (avoid thread collisions) */
821*2c2f96dcSApple OSS Distributions lck_mtx_lock(&compilation_service_lock);
822*2c2f96dcSApple OSS Distributions ret = txm_kernel_call(&txm_call, cdhash);
823*2c2f96dcSApple OSS Distributions lck_mtx_unlock(&compilation_service_lock);
824*2c2f96dcSApple OSS Distributions
825*2c2f96dcSApple OSS Distributions if (ret == KERN_SUCCESS) {
826*2c2f96dcSApple OSS Distributions return true;
827*2c2f96dcSApple OSS Distributions }
828*2c2f96dcSApple OSS Distributions return false;
829*2c2f96dcSApple OSS Distributions }
830*2c2f96dcSApple OSS Distributions
831*2c2f96dcSApple OSS Distributions void
txm_set_local_signing_public_key(const uint8_t public_key[XNU_LOCAL_SIGNING_KEY_SIZE])832*2c2f96dcSApple OSS Distributions txm_set_local_signing_public_key(
833*2c2f96dcSApple OSS Distributions const uint8_t public_key[XNU_LOCAL_SIGNING_KEY_SIZE])
834*2c2f96dcSApple OSS Distributions {
835*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
836*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorSetLocalSigningPublicKey,
837*2c2f96dcSApple OSS Distributions .num_input_args = 1,
838*2c2f96dcSApple OSS Distributions };
839*2c2f96dcSApple OSS Distributions
840*2c2f96dcSApple OSS Distributions txm_kernel_call(&txm_call, public_key);
841*2c2f96dcSApple OSS Distributions }
842*2c2f96dcSApple OSS Distributions
843*2c2f96dcSApple OSS Distributions uint8_t*
txm_get_local_signing_public_key(void)844*2c2f96dcSApple OSS Distributions txm_get_local_signing_public_key(void)
845*2c2f96dcSApple OSS Distributions {
846*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
847*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorGetLocalSigningPublicKey,
848*2c2f96dcSApple OSS Distributions .num_output_args = 1,
849*2c2f96dcSApple OSS Distributions };
850*2c2f96dcSApple OSS Distributions kern_return_t ret = KERN_DENIED;
851*2c2f96dcSApple OSS Distributions
852*2c2f96dcSApple OSS Distributions ret = txm_kernel_call(&txm_call);
853*2c2f96dcSApple OSS Distributions if (ret != KERN_SUCCESS) {
854*2c2f96dcSApple OSS Distributions return NULL;
855*2c2f96dcSApple OSS Distributions }
856*2c2f96dcSApple OSS Distributions
857*2c2f96dcSApple OSS Distributions return (uint8_t*)txm_call.return_words[0];
858*2c2f96dcSApple OSS Distributions }
859*2c2f96dcSApple OSS Distributions
860*2c2f96dcSApple OSS Distributions void
txm_unrestrict_local_signing_cdhash(const uint8_t cdhash[CS_CDHASH_LEN])861*2c2f96dcSApple OSS Distributions txm_unrestrict_local_signing_cdhash(
862*2c2f96dcSApple OSS Distributions const uint8_t cdhash[CS_CDHASH_LEN])
863*2c2f96dcSApple OSS Distributions {
864*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
865*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorAuthorizeLocalSigningCDHash,
866*2c2f96dcSApple OSS Distributions .num_input_args = 1,
867*2c2f96dcSApple OSS Distributions };
868*2c2f96dcSApple OSS Distributions
869*2c2f96dcSApple OSS Distributions txm_kernel_call(&txm_call, cdhash);
870*2c2f96dcSApple OSS Distributions }
871*2c2f96dcSApple OSS Distributions
872*2c2f96dcSApple OSS Distributions kern_return_t
txm_register_code_signature(const vm_address_t signature_addr,const vm_size_t signature_size,const vm_offset_t code_directory_offset,const char * signature_path,void ** sig_obj,vm_address_t * txm_signature_addr)873*2c2f96dcSApple OSS Distributions txm_register_code_signature(
874*2c2f96dcSApple OSS Distributions const vm_address_t signature_addr,
875*2c2f96dcSApple OSS Distributions const vm_size_t signature_size,
876*2c2f96dcSApple OSS Distributions const vm_offset_t code_directory_offset,
877*2c2f96dcSApple OSS Distributions const char *signature_path,
878*2c2f96dcSApple OSS Distributions void **sig_obj,
879*2c2f96dcSApple OSS Distributions vm_address_t *txm_signature_addr)
880*2c2f96dcSApple OSS Distributions {
881*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
882*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorRegisterCodeSignature,
883*2c2f96dcSApple OSS Distributions .num_input_args = 3,
884*2c2f96dcSApple OSS Distributions .num_output_args = 2,
885*2c2f96dcSApple OSS Distributions };
886*2c2f96dcSApple OSS Distributions kern_return_t ret = KERN_DENIED;
887*2c2f96dcSApple OSS Distributions
888*2c2f96dcSApple OSS Distributions /*
889*2c2f96dcSApple OSS Distributions * TXM performs more exhaustive validation of the code signature and figures
890*2c2f96dcSApple OSS Distributions * out the best code directory to use on its own. As a result, this offset here
891*2c2f96dcSApple OSS Distributions * is not used.
892*2c2f96dcSApple OSS Distributions */
893*2c2f96dcSApple OSS Distributions (void)code_directory_offset;
894*2c2f96dcSApple OSS Distributions
895*2c2f96dcSApple OSS Distributions /*
896*2c2f96dcSApple OSS Distributions * If the signature is large enough to not fit within TXM's managed signature
897*2c2f96dcSApple OSS Distributions * size, then we need to transfer it over so it is owned by TXM.
898*2c2f96dcSApple OSS Distributions */
899*2c2f96dcSApple OSS Distributions if (signature_size > txm_managed_code_signature_size()) {
900*2c2f96dcSApple OSS Distributions txm_transfer_region(signature_addr, signature_size);
901*2c2f96dcSApple OSS Distributions }
902*2c2f96dcSApple OSS Distributions
903*2c2f96dcSApple OSS Distributions ret = txm_kernel_call(
904*2c2f96dcSApple OSS Distributions &txm_call,
905*2c2f96dcSApple OSS Distributions signature_addr,
906*2c2f96dcSApple OSS Distributions signature_size,
907*2c2f96dcSApple OSS Distributions signature_path);
908*2c2f96dcSApple OSS Distributions
909*2c2f96dcSApple OSS Distributions if (ret != KERN_SUCCESS) {
910*2c2f96dcSApple OSS Distributions goto exit;
911*2c2f96dcSApple OSS Distributions }
912*2c2f96dcSApple OSS Distributions
913*2c2f96dcSApple OSS Distributions *sig_obj = (void*)txm_call.return_words[0];
914*2c2f96dcSApple OSS Distributions *txm_signature_addr = txm_call.return_words[1];
915*2c2f96dcSApple OSS Distributions
916*2c2f96dcSApple OSS Distributions exit:
917*2c2f96dcSApple OSS Distributions if ((ret != KERN_SUCCESS) && (signature_size > txm_managed_code_signature_size())) {
918*2c2f96dcSApple OSS Distributions txm_reclaim_region(signature_addr, signature_size);
919*2c2f96dcSApple OSS Distributions }
920*2c2f96dcSApple OSS Distributions
921*2c2f96dcSApple OSS Distributions return ret;
922*2c2f96dcSApple OSS Distributions }
923*2c2f96dcSApple OSS Distributions
924*2c2f96dcSApple OSS Distributions kern_return_t
txm_unregister_code_signature(void * sig_obj)925*2c2f96dcSApple OSS Distributions txm_unregister_code_signature(
926*2c2f96dcSApple OSS Distributions void *sig_obj)
927*2c2f96dcSApple OSS Distributions {
928*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
929*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorUnregisterCodeSignature,
930*2c2f96dcSApple OSS Distributions .failure_fatal = true,
931*2c2f96dcSApple OSS Distributions .num_input_args = 1,
932*2c2f96dcSApple OSS Distributions .num_output_args = 2,
933*2c2f96dcSApple OSS Distributions };
934*2c2f96dcSApple OSS Distributions TXMCodeSignature_t *cs_obj = sig_obj;
935*2c2f96dcSApple OSS Distributions vm_address_t signature_addr = 0;
936*2c2f96dcSApple OSS Distributions vm_size_t signature_size = 0;
937*2c2f96dcSApple OSS Distributions bool txm_managed = false;
938*2c2f96dcSApple OSS Distributions
939*2c2f96dcSApple OSS Distributions /* Check if the signature memory is TXM managed */
940*2c2f96dcSApple OSS Distributions txm_managed = cs_obj->sptmType != TXM_BULK_DATA;
941*2c2f96dcSApple OSS Distributions
942*2c2f96dcSApple OSS Distributions /*
943*2c2f96dcSApple OSS Distributions * Take the unregistration sync lock.
944*2c2f96dcSApple OSS Distributions * For more information: rdar://99205627.
945*2c2f96dcSApple OSS Distributions */
946*2c2f96dcSApple OSS Distributions lck_mtx_lock(&unregister_sync_lock);
947*2c2f96dcSApple OSS Distributions
948*2c2f96dcSApple OSS Distributions /* Unregister the signature from TXM -- cannot fail */
949*2c2f96dcSApple OSS Distributions txm_kernel_call(&txm_call, sig_obj);
950*2c2f96dcSApple OSS Distributions
951*2c2f96dcSApple OSS Distributions /* Release the unregistration sync lock */
952*2c2f96dcSApple OSS Distributions lck_mtx_unlock(&unregister_sync_lock);
953*2c2f96dcSApple OSS Distributions
954*2c2f96dcSApple OSS Distributions signature_addr = txm_call.return_words[0];
955*2c2f96dcSApple OSS Distributions signature_size = txm_call.return_words[1];
956*2c2f96dcSApple OSS Distributions
957*2c2f96dcSApple OSS Distributions /* Reclaim the memory range in case we need to */
958*2c2f96dcSApple OSS Distributions if (txm_managed == false) {
959*2c2f96dcSApple OSS Distributions txm_reclaim_region(signature_addr, signature_size);
960*2c2f96dcSApple OSS Distributions }
961*2c2f96dcSApple OSS Distributions
962*2c2f96dcSApple OSS Distributions return KERN_SUCCESS;
963*2c2f96dcSApple OSS Distributions }
964*2c2f96dcSApple OSS Distributions
965*2c2f96dcSApple OSS Distributions kern_return_t
txm_verify_code_signature(void * sig_obj)966*2c2f96dcSApple OSS Distributions txm_verify_code_signature(
967*2c2f96dcSApple OSS Distributions void *sig_obj)
968*2c2f96dcSApple OSS Distributions {
969*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
970*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorValidateCodeSignature,
971*2c2f96dcSApple OSS Distributions .num_input_args = 1,
972*2c2f96dcSApple OSS Distributions };
973*2c2f96dcSApple OSS Distributions kern_return_t ret = KERN_DENIED;
974*2c2f96dcSApple OSS Distributions
975*2c2f96dcSApple OSS Distributions /*
976*2c2f96dcSApple OSS Distributions * Verification of the code signature may perform a trust cache look up.
977*2c2f96dcSApple OSS Distributions * In order to avoid any collisions with threads which may be loading a
978*2c2f96dcSApple OSS Distributions * trust cache, we take a reader lock on the trust cache runtime.
979*2c2f96dcSApple OSS Distributions */
980*2c2f96dcSApple OSS Distributions
981*2c2f96dcSApple OSS Distributions lck_rw_lock_shared(&txm_trust_cache_lck);
982*2c2f96dcSApple OSS Distributions ret = txm_kernel_call(&txm_call, sig_obj);
983*2c2f96dcSApple OSS Distributions lck_rw_unlock_shared(&txm_trust_cache_lck);
984*2c2f96dcSApple OSS Distributions
985*2c2f96dcSApple OSS Distributions return ret;
986*2c2f96dcSApple OSS Distributions }
987*2c2f96dcSApple OSS Distributions
988*2c2f96dcSApple OSS Distributions kern_return_t
txm_reconstitute_code_signature(void * sig_obj,vm_address_t * unneeded_addr,vm_size_t * unneeded_size)989*2c2f96dcSApple OSS Distributions txm_reconstitute_code_signature(
990*2c2f96dcSApple OSS Distributions void *sig_obj,
991*2c2f96dcSApple OSS Distributions vm_address_t *unneeded_addr,
992*2c2f96dcSApple OSS Distributions vm_size_t *unneeded_size)
993*2c2f96dcSApple OSS Distributions {
994*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
995*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorReconstituteCodeSignature,
996*2c2f96dcSApple OSS Distributions .failure_fatal = true,
997*2c2f96dcSApple OSS Distributions .num_input_args = 1,
998*2c2f96dcSApple OSS Distributions .num_output_args = 2,
999*2c2f96dcSApple OSS Distributions };
1000*2c2f96dcSApple OSS Distributions vm_address_t return_addr = 0;
1001*2c2f96dcSApple OSS Distributions vm_size_t return_size = 0;
1002*2c2f96dcSApple OSS Distributions
1003*2c2f96dcSApple OSS Distributions /* Reconstitute the code signature -- cannot fail */
1004*2c2f96dcSApple OSS Distributions txm_kernel_call(&txm_call, sig_obj);
1005*2c2f96dcSApple OSS Distributions
1006*2c2f96dcSApple OSS Distributions return_addr = txm_call.return_words[0];
1007*2c2f96dcSApple OSS Distributions return_size = txm_call.return_words[1];
1008*2c2f96dcSApple OSS Distributions
1009*2c2f96dcSApple OSS Distributions /* Reclaim the memory region if we need to */
1010*2c2f96dcSApple OSS Distributions if ((return_addr != 0) && (return_size != 0)) {
1011*2c2f96dcSApple OSS Distributions txm_reclaim_region(return_addr, return_size);
1012*2c2f96dcSApple OSS Distributions }
1013*2c2f96dcSApple OSS Distributions
1014*2c2f96dcSApple OSS Distributions *unneeded_addr = return_addr;
1015*2c2f96dcSApple OSS Distributions *unneeded_size = return_size;
1016*2c2f96dcSApple OSS Distributions
1017*2c2f96dcSApple OSS Distributions return KERN_SUCCESS;
1018*2c2f96dcSApple OSS Distributions }
1019*2c2f96dcSApple OSS Distributions
1020*2c2f96dcSApple OSS Distributions #pragma mark Address Spaces
1021*2c2f96dcSApple OSS Distributions
1022*2c2f96dcSApple OSS Distributions kern_return_t
txm_register_address_space(pmap_t pmap,uint16_t addr_space_id,TXMAddressSpaceFlags_t flags)1023*2c2f96dcSApple OSS Distributions txm_register_address_space(
1024*2c2f96dcSApple OSS Distributions pmap_t pmap,
1025*2c2f96dcSApple OSS Distributions uint16_t addr_space_id,
1026*2c2f96dcSApple OSS Distributions TXMAddressSpaceFlags_t flags)
1027*2c2f96dcSApple OSS Distributions {
1028*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
1029*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorRegisterAddressSpace,
1030*2c2f96dcSApple OSS Distributions .failure_fatal = true,
1031*2c2f96dcSApple OSS Distributions .num_input_args = 2,
1032*2c2f96dcSApple OSS Distributions .num_output_args = 1,
1033*2c2f96dcSApple OSS Distributions };
1034*2c2f96dcSApple OSS Distributions TXMAddressSpace_t *txm_addr_space = NULL;
1035*2c2f96dcSApple OSS Distributions
1036*2c2f96dcSApple OSS Distributions /* Register the address space -- cannot fail */
1037*2c2f96dcSApple OSS Distributions txm_kernel_call(&txm_call, addr_space_id, flags);
1038*2c2f96dcSApple OSS Distributions
1039*2c2f96dcSApple OSS Distributions /* Set the address space object within the PMAP */
1040*2c2f96dcSApple OSS Distributions txm_addr_space = (TXMAddressSpace_t*)txm_call.return_words[0];
1041*2c2f96dcSApple OSS Distributions pmap_txm_set_addr_space(pmap, txm_addr_space);
1042*2c2f96dcSApple OSS Distributions
1043*2c2f96dcSApple OSS Distributions return KERN_SUCCESS;
1044*2c2f96dcSApple OSS Distributions }
1045*2c2f96dcSApple OSS Distributions
1046*2c2f96dcSApple OSS Distributions kern_return_t
txm_unregister_address_space(pmap_t pmap)1047*2c2f96dcSApple OSS Distributions txm_unregister_address_space(
1048*2c2f96dcSApple OSS Distributions pmap_t pmap)
1049*2c2f96dcSApple OSS Distributions {
1050*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
1051*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorUnregisterAddressSpace,
1052*2c2f96dcSApple OSS Distributions .failure_fatal = true,
1053*2c2f96dcSApple OSS Distributions .num_input_args = 1,
1054*2c2f96dcSApple OSS Distributions };
1055*2c2f96dcSApple OSS Distributions TXMAddressSpace_t *txm_addr_space = pmap_txm_addr_space(pmap);
1056*2c2f96dcSApple OSS Distributions
1057*2c2f96dcSApple OSS Distributions /*
1058*2c2f96dcSApple OSS Distributions * Take the unregistration sync lock.
1059*2c2f96dcSApple OSS Distributions * For more information: rdar://99205627.
1060*2c2f96dcSApple OSS Distributions */
1061*2c2f96dcSApple OSS Distributions lck_mtx_lock(&unregister_sync_lock);
1062*2c2f96dcSApple OSS Distributions
1063*2c2f96dcSApple OSS Distributions /* Unregister the address space -- cannot fail */
1064*2c2f96dcSApple OSS Distributions txm_kernel_call(&txm_call, txm_addr_space);
1065*2c2f96dcSApple OSS Distributions
1066*2c2f96dcSApple OSS Distributions /* Release the unregistration sync lock */
1067*2c2f96dcSApple OSS Distributions lck_mtx_unlock(&unregister_sync_lock);
1068*2c2f96dcSApple OSS Distributions
1069*2c2f96dcSApple OSS Distributions /* Remove the address space from the pmap */
1070*2c2f96dcSApple OSS Distributions pmap_txm_set_addr_space(pmap, NULL);
1071*2c2f96dcSApple OSS Distributions
1072*2c2f96dcSApple OSS Distributions return KERN_SUCCESS;
1073*2c2f96dcSApple OSS Distributions }
1074*2c2f96dcSApple OSS Distributions
1075*2c2f96dcSApple OSS Distributions kern_return_t
txm_associate_code_signature(pmap_t pmap,void * sig_obj,const vm_address_t region_addr,const vm_size_t region_size,const vm_offset_t region_offset)1076*2c2f96dcSApple OSS Distributions txm_associate_code_signature(
1077*2c2f96dcSApple OSS Distributions pmap_t pmap,
1078*2c2f96dcSApple OSS Distributions void *sig_obj,
1079*2c2f96dcSApple OSS Distributions const vm_address_t region_addr,
1080*2c2f96dcSApple OSS Distributions const vm_size_t region_size,
1081*2c2f96dcSApple OSS Distributions const vm_offset_t region_offset)
1082*2c2f96dcSApple OSS Distributions {
1083*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
1084*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorAssociateCodeSignature,
1085*2c2f96dcSApple OSS Distributions .num_input_args = 5,
1086*2c2f96dcSApple OSS Distributions };
1087*2c2f96dcSApple OSS Distributions TXMAddressSpace_t *txm_addr_space = pmap_txm_addr_space(pmap);
1088*2c2f96dcSApple OSS Distributions kern_return_t ret = KERN_DENIED;
1089*2c2f96dcSApple OSS Distributions
1090*2c2f96dcSApple OSS Distributions /*
1091*2c2f96dcSApple OSS Distributions * Associating a code signature may require exclusive access to the TXM address
1092*2c2f96dcSApple OSS Distributions * space lock within TXM.
1093*2c2f96dcSApple OSS Distributions */
1094*2c2f96dcSApple OSS Distributions pmap_txm_acquire_exclusive_lock(pmap);
1095*2c2f96dcSApple OSS Distributions
1096*2c2f96dcSApple OSS Distributions /*
1097*2c2f96dcSApple OSS Distributions * If the address space in question is a nested address space, then all associations
1098*2c2f96dcSApple OSS Distributions * need to go into the shared region base range. The VM layer is inconsistent with
1099*2c2f96dcSApple OSS Distributions * how it makes associations with TXM vs. how it maps pages into the shared region.
1100*2c2f96dcSApple OSS Distributions *
1101*2c2f96dcSApple OSS Distributions * For TXM, the associations are made without taking the base range into account,
1102*2c2f96dcSApple OSS Distributions * but when mappings are entered into the shared region, the base range is taken
1103*2c2f96dcSApple OSS Distributions * into account. To normalize this, we add the base range address here.
1104*2c2f96dcSApple OSS Distributions */
1105*2c2f96dcSApple OSS Distributions vm_address_t adjusted_region_addr = region_addr;
1106*2c2f96dcSApple OSS Distributions if (txm_addr_space->addrSpaceID.type == kTXMAddressSpaceIDTypeSharedRegion) {
1107*2c2f96dcSApple OSS Distributions adjusted_region_addr += SHARED_REGION_BASE;
1108*2c2f96dcSApple OSS Distributions }
1109*2c2f96dcSApple OSS Distributions
1110*2c2f96dcSApple OSS Distributions /*
1111*2c2f96dcSApple OSS Distributions * The VM tries a bunch of weird mappings within launchd for some platform code
1112*2c2f96dcSApple OSS Distributions * which isn't mapped contiguously. These mappings don't succeed, but the failure
1113*2c2f96dcSApple OSS Distributions * is fairly harmless since everything seems to work. However, since the call to
1114*2c2f96dcSApple OSS Distributions * TXM fails, we make a series of logs. Hence, for launchd, we suppress failure
1115*2c2f96dcSApple OSS Distributions * logs.
1116*2c2f96dcSApple OSS Distributions */
1117*2c2f96dcSApple OSS Distributions if (txm_addr_space->addrSpaceID.type == kTXMAddressSpaceIDTypeAddressSpace) {
1118*2c2f96dcSApple OSS Distributions /* TXMTODO: Scope this to launchd better */
1119*2c2f96dcSApple OSS Distributions txm_call.failure_code_silent = kTXMReturnPlatformCodeMapping;
1120*2c2f96dcSApple OSS Distributions }
1121*2c2f96dcSApple OSS Distributions
1122*2c2f96dcSApple OSS Distributions /* Check if the main region has been set on the address space */
1123*2c2f96dcSApple OSS Distributions bool main_region_set = txm_addr_space->mainRegion != NULL;
1124*2c2f96dcSApple OSS Distributions bool main_region_set_after = false;
1125*2c2f96dcSApple OSS Distributions
1126*2c2f96dcSApple OSS Distributions ret = txm_kernel_call(
1127*2c2f96dcSApple OSS Distributions &txm_call,
1128*2c2f96dcSApple OSS Distributions txm_addr_space,
1129*2c2f96dcSApple OSS Distributions sig_obj,
1130*2c2f96dcSApple OSS Distributions adjusted_region_addr,
1131*2c2f96dcSApple OSS Distributions region_size,
1132*2c2f96dcSApple OSS Distributions region_offset);
1133*2c2f96dcSApple OSS Distributions
1134*2c2f96dcSApple OSS Distributions /*
1135*2c2f96dcSApple OSS Distributions * If the main region wasn't set on the address space before hand, but this new
1136*2c2f96dcSApple OSS Distributions * call into TXM was successful and sets the main region, it means this signature
1137*2c2f96dcSApple OSS Distributions * object is associated with the main region on the address space. With this, we
1138*2c2f96dcSApple OSS Distributions * can now set the appropriate trust level on the PMAP.
1139*2c2f96dcSApple OSS Distributions */
1140*2c2f96dcSApple OSS Distributions if (ret == KERN_SUCCESS) {
1141*2c2f96dcSApple OSS Distributions main_region_set_after = txm_addr_space->mainRegion != NULL;
1142*2c2f96dcSApple OSS Distributions }
1143*2c2f96dcSApple OSS Distributions
1144*2c2f96dcSApple OSS Distributions /* Unlock the TXM address space lock */
1145*2c2f96dcSApple OSS Distributions pmap_txm_release_exclusive_lock(pmap);
1146*2c2f96dcSApple OSS Distributions
1147*2c2f96dcSApple OSS Distributions /* Check if we should set the trust level on the PMAP */
1148*2c2f96dcSApple OSS Distributions if (!main_region_set && main_region_set_after) {
1149*2c2f96dcSApple OSS Distributions const TXMCodeSignature_t *cs_obj = sig_obj;
1150*2c2f96dcSApple OSS Distributions const SignatureValidation_t *sig = &cs_obj->sig;
1151*2c2f96dcSApple OSS Distributions
1152*2c2f96dcSApple OSS Distributions /*
1153*2c2f96dcSApple OSS Distributions * This is gross, as we're dereferencing into a private data structure type.
1154*2c2f96dcSApple OSS Distributions * There are 2 ways to clean this up in the future:
1155*2c2f96dcSApple OSS Distributions * 1. Import libCodeSignature, so we can use "codeSignatureGetTrustLevel".
1156*2c2f96dcSApple OSS Distributions * 2. Cache the trust level on the address space within TXM and then use it.
1157*2c2f96dcSApple OSS Distributions */
1158*2c2f96dcSApple OSS Distributions pmap_txm_set_trust_level(pmap, sig->trustLevel);
1159*2c2f96dcSApple OSS Distributions }
1160*2c2f96dcSApple OSS Distributions
1161*2c2f96dcSApple OSS Distributions return ret;
1162*2c2f96dcSApple OSS Distributions }
1163*2c2f96dcSApple OSS Distributions
1164*2c2f96dcSApple OSS Distributions kern_return_t
txm_allow_jit_region(pmap_t pmap)1165*2c2f96dcSApple OSS Distributions txm_allow_jit_region(
1166*2c2f96dcSApple OSS Distributions pmap_t pmap)
1167*2c2f96dcSApple OSS Distributions {
1168*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
1169*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorAllowJITRegion,
1170*2c2f96dcSApple OSS Distributions .num_input_args = 1,
1171*2c2f96dcSApple OSS Distributions };
1172*2c2f96dcSApple OSS Distributions TXMAddressSpace_t *txm_addr_space = pmap_txm_addr_space(pmap);
1173*2c2f96dcSApple OSS Distributions kern_return_t ret = KERN_DENIED;
1174*2c2f96dcSApple OSS Distributions
1175*2c2f96dcSApple OSS Distributions pmap_txm_acquire_shared_lock(pmap);
1176*2c2f96dcSApple OSS Distributions ret = txm_kernel_call(&txm_call, txm_addr_space);
1177*2c2f96dcSApple OSS Distributions pmap_txm_release_shared_lock(pmap);
1178*2c2f96dcSApple OSS Distributions
1179*2c2f96dcSApple OSS Distributions return ret;
1180*2c2f96dcSApple OSS Distributions }
1181*2c2f96dcSApple OSS Distributions
1182*2c2f96dcSApple OSS Distributions kern_return_t
txm_associate_jit_region(pmap_t pmap,const vm_address_t region_addr,const vm_size_t region_size)1183*2c2f96dcSApple OSS Distributions txm_associate_jit_region(
1184*2c2f96dcSApple OSS Distributions pmap_t pmap,
1185*2c2f96dcSApple OSS Distributions const vm_address_t region_addr,
1186*2c2f96dcSApple OSS Distributions const vm_size_t region_size)
1187*2c2f96dcSApple OSS Distributions {
1188*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
1189*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorAssociateJITRegion,
1190*2c2f96dcSApple OSS Distributions .num_input_args = 3,
1191*2c2f96dcSApple OSS Distributions };
1192*2c2f96dcSApple OSS Distributions TXMAddressSpace_t *txm_addr_space = pmap_txm_addr_space(pmap);
1193*2c2f96dcSApple OSS Distributions kern_return_t ret = KERN_DENIED;
1194*2c2f96dcSApple OSS Distributions
1195*2c2f96dcSApple OSS Distributions /*
1196*2c2f96dcSApple OSS Distributions * Associating a JIT region may require exclusive access to the TXM address
1197*2c2f96dcSApple OSS Distributions * space lock within TXM.
1198*2c2f96dcSApple OSS Distributions */
1199*2c2f96dcSApple OSS Distributions pmap_txm_acquire_exclusive_lock(pmap);
1200*2c2f96dcSApple OSS Distributions
1201*2c2f96dcSApple OSS Distributions ret = txm_kernel_call(
1202*2c2f96dcSApple OSS Distributions &txm_call,
1203*2c2f96dcSApple OSS Distributions txm_addr_space,
1204*2c2f96dcSApple OSS Distributions region_addr,
1205*2c2f96dcSApple OSS Distributions region_size);
1206*2c2f96dcSApple OSS Distributions
1207*2c2f96dcSApple OSS Distributions /* Unlock the TXM address space lock */
1208*2c2f96dcSApple OSS Distributions pmap_txm_release_exclusive_lock(pmap);
1209*2c2f96dcSApple OSS Distributions
1210*2c2f96dcSApple OSS Distributions return ret;
1211*2c2f96dcSApple OSS Distributions }
1212*2c2f96dcSApple OSS Distributions
1213*2c2f96dcSApple OSS Distributions kern_return_t
txm_address_space_debugged(pmap_t pmap)1214*2c2f96dcSApple OSS Distributions txm_address_space_debugged(
1215*2c2f96dcSApple OSS Distributions pmap_t pmap)
1216*2c2f96dcSApple OSS Distributions {
1217*2c2f96dcSApple OSS Distributions TXMAddressSpace_t *txm_addr_space = pmap_txm_addr_space(pmap);
1218*2c2f96dcSApple OSS Distributions bool debug_regions_allowed = false;
1219*2c2f96dcSApple OSS Distributions
1220*2c2f96dcSApple OSS Distributions /*
1221*2c2f96dcSApple OSS Distributions * We do not actually need to trap into the monitor for this function for
1222*2c2f96dcSApple OSS Distributions * now. It might be a tad bit more secure to actually trap into the monitor
1223*2c2f96dcSApple OSS Distributions * as it implicitly verifies all of our pointers, but since this is a simple
1224*2c2f96dcSApple OSS Distributions * state check against the address space, the real policy around it lies
1225*2c2f96dcSApple OSS Distributions * within the kernel still, in which case entering the monitor doesn't
1226*2c2f96dcSApple OSS Distributions * really provide much more security.
1227*2c2f96dcSApple OSS Distributions */
1228*2c2f96dcSApple OSS Distributions
1229*2c2f96dcSApple OSS Distributions pmap_txm_acquire_shared_lock(pmap);
1230*2c2f96dcSApple OSS Distributions debug_regions_allowed = os_atomic_load(&txm_addr_space->allowsInvalidCode, relaxed);
1231*2c2f96dcSApple OSS Distributions pmap_txm_release_shared_lock(pmap);
1232*2c2f96dcSApple OSS Distributions
1233*2c2f96dcSApple OSS Distributions if (debug_regions_allowed == true) {
1234*2c2f96dcSApple OSS Distributions return KERN_SUCCESS;
1235*2c2f96dcSApple OSS Distributions }
1236*2c2f96dcSApple OSS Distributions return KERN_DENIED;
1237*2c2f96dcSApple OSS Distributions }
1238*2c2f96dcSApple OSS Distributions
1239*2c2f96dcSApple OSS Distributions kern_return_t
txm_associate_debug_region(pmap_t pmap,const vm_address_t region_addr,const vm_size_t region_size)1240*2c2f96dcSApple OSS Distributions txm_associate_debug_region(
1241*2c2f96dcSApple OSS Distributions pmap_t pmap,
1242*2c2f96dcSApple OSS Distributions const vm_address_t region_addr,
1243*2c2f96dcSApple OSS Distributions const vm_size_t region_size)
1244*2c2f96dcSApple OSS Distributions {
1245*2c2f96dcSApple OSS Distributions /*
1246*2c2f96dcSApple OSS Distributions * This function is an interesting one. There is no need for us to make
1247*2c2f96dcSApple OSS Distributions * a call into TXM for this one and instead, all we need to do here is
1248*2c2f96dcSApple OSS Distributions * to verify that the TXM address space actually allows debug regions to
1249*2c2f96dcSApple OSS Distributions * be mapped in or not.
1250*2c2f96dcSApple OSS Distributions */
1251*2c2f96dcSApple OSS Distributions (void)region_addr;
1252*2c2f96dcSApple OSS Distributions (void)region_size;
1253*2c2f96dcSApple OSS Distributions
1254*2c2f96dcSApple OSS Distributions kern_return_t ret = txm_address_space_debugged(pmap);
1255*2c2f96dcSApple OSS Distributions if (ret != KERN_SUCCESS) {
1256*2c2f96dcSApple OSS Distributions printf("address space does not allow creating debug regions\n");
1257*2c2f96dcSApple OSS Distributions }
1258*2c2f96dcSApple OSS Distributions
1259*2c2f96dcSApple OSS Distributions return ret;
1260*2c2f96dcSApple OSS Distributions }
1261*2c2f96dcSApple OSS Distributions
1262*2c2f96dcSApple OSS Distributions kern_return_t
txm_allow_invalid_code(pmap_t pmap)1263*2c2f96dcSApple OSS Distributions txm_allow_invalid_code(
1264*2c2f96dcSApple OSS Distributions pmap_t pmap)
1265*2c2f96dcSApple OSS Distributions {
1266*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
1267*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorAllowInvalidCode,
1268*2c2f96dcSApple OSS Distributions .num_input_args = 1,
1269*2c2f96dcSApple OSS Distributions };
1270*2c2f96dcSApple OSS Distributions TXMAddressSpace_t *txm_addr_space = pmap_txm_addr_space(pmap);
1271*2c2f96dcSApple OSS Distributions kern_return_t ret = KERN_DENIED;
1272*2c2f96dcSApple OSS Distributions
1273*2c2f96dcSApple OSS Distributions /*
1274*2c2f96dcSApple OSS Distributions * Allowing invalid code may require exclusive access to the TXM address
1275*2c2f96dcSApple OSS Distributions * space lock within TXM.
1276*2c2f96dcSApple OSS Distributions */
1277*2c2f96dcSApple OSS Distributions
1278*2c2f96dcSApple OSS Distributions pmap_txm_acquire_exclusive_lock(pmap);
1279*2c2f96dcSApple OSS Distributions ret = txm_kernel_call(&txm_call, txm_addr_space);
1280*2c2f96dcSApple OSS Distributions pmap_txm_release_exclusive_lock(pmap);
1281*2c2f96dcSApple OSS Distributions
1282*2c2f96dcSApple OSS Distributions return ret;
1283*2c2f96dcSApple OSS Distributions }
1284*2c2f96dcSApple OSS Distributions
1285*2c2f96dcSApple OSS Distributions kern_return_t
txm_get_trust_level_kdp(pmap_t pmap,uint32_t * trust_level)1286*2c2f96dcSApple OSS Distributions txm_get_trust_level_kdp(
1287*2c2f96dcSApple OSS Distributions pmap_t pmap,
1288*2c2f96dcSApple OSS Distributions uint32_t *trust_level)
1289*2c2f96dcSApple OSS Distributions {
1290*2c2f96dcSApple OSS Distributions CSTrust_t txm_trust_level = kCSTrustUntrusted;
1291*2c2f96dcSApple OSS Distributions
1292*2c2f96dcSApple OSS Distributions kern_return_t ret = pmap_txm_get_trust_level_kdp(pmap, &txm_trust_level);
1293*2c2f96dcSApple OSS Distributions if (ret != KERN_SUCCESS) {
1294*2c2f96dcSApple OSS Distributions return ret;
1295*2c2f96dcSApple OSS Distributions }
1296*2c2f96dcSApple OSS Distributions
1297*2c2f96dcSApple OSS Distributions if (trust_level != NULL) {
1298*2c2f96dcSApple OSS Distributions *trust_level = txm_trust_level;
1299*2c2f96dcSApple OSS Distributions }
1300*2c2f96dcSApple OSS Distributions return KERN_SUCCESS;
1301*2c2f96dcSApple OSS Distributions }
1302*2c2f96dcSApple OSS Distributions
1303*2c2f96dcSApple OSS Distributions kern_return_t
txm_address_space_exempt(const pmap_t pmap)1304*2c2f96dcSApple OSS Distributions txm_address_space_exempt(
1305*2c2f96dcSApple OSS Distributions const pmap_t pmap)
1306*2c2f96dcSApple OSS Distributions {
1307*2c2f96dcSApple OSS Distributions if (pmap_performs_stage2_translations(pmap) == true) {
1308*2c2f96dcSApple OSS Distributions return KERN_SUCCESS;
1309*2c2f96dcSApple OSS Distributions }
1310*2c2f96dcSApple OSS Distributions
1311*2c2f96dcSApple OSS Distributions return KERN_DENIED;
1312*2c2f96dcSApple OSS Distributions }
1313*2c2f96dcSApple OSS Distributions
1314*2c2f96dcSApple OSS Distributions kern_return_t
txm_fork_prepare(pmap_t old_pmap,pmap_t new_pmap)1315*2c2f96dcSApple OSS Distributions txm_fork_prepare(
1316*2c2f96dcSApple OSS Distributions pmap_t old_pmap,
1317*2c2f96dcSApple OSS Distributions pmap_t new_pmap)
1318*2c2f96dcSApple OSS Distributions {
1319*2c2f96dcSApple OSS Distributions /*
1320*2c2f96dcSApple OSS Distributions * We'll add support for this as the need for it becomes more important.
1321*2c2f96dcSApple OSS Distributions * TXMTODO: Complete this implementation.
1322*2c2f96dcSApple OSS Distributions */
1323*2c2f96dcSApple OSS Distributions (void)old_pmap;
1324*2c2f96dcSApple OSS Distributions (void)new_pmap;
1325*2c2f96dcSApple OSS Distributions
1326*2c2f96dcSApple OSS Distributions return KERN_SUCCESS;
1327*2c2f96dcSApple OSS Distributions }
1328*2c2f96dcSApple OSS Distributions
1329*2c2f96dcSApple OSS Distributions kern_return_t
txm_acquire_signing_identifier(const void * sig_obj,const char ** signing_id)1330*2c2f96dcSApple OSS Distributions txm_acquire_signing_identifier(
1331*2c2f96dcSApple OSS Distributions const void *sig_obj,
1332*2c2f96dcSApple OSS Distributions const char **signing_id)
1333*2c2f96dcSApple OSS Distributions {
1334*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
1335*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorAcquireSigningIdentifier,
1336*2c2f96dcSApple OSS Distributions .num_input_args = 1,
1337*2c2f96dcSApple OSS Distributions .num_output_args = 1,
1338*2c2f96dcSApple OSS Distributions .failure_fatal = true,
1339*2c2f96dcSApple OSS Distributions };
1340*2c2f96dcSApple OSS Distributions
1341*2c2f96dcSApple OSS Distributions /* Get the signing ID -- should not fail */
1342*2c2f96dcSApple OSS Distributions txm_kernel_call(&txm_call, sig_obj);
1343*2c2f96dcSApple OSS Distributions
1344*2c2f96dcSApple OSS Distributions if (signing_id != NULL) {
1345*2c2f96dcSApple OSS Distributions *signing_id = (const char*)txm_call.return_words[0];
1346*2c2f96dcSApple OSS Distributions }
1347*2c2f96dcSApple OSS Distributions return KERN_SUCCESS;
1348*2c2f96dcSApple OSS Distributions }
1349*2c2f96dcSApple OSS Distributions
1350*2c2f96dcSApple OSS Distributions #pragma mark Entitlements
1351*2c2f96dcSApple OSS Distributions
1352*2c2f96dcSApple OSS Distributions kern_return_t
txm_associate_kernel_entitlements(void * sig_obj,const void * kernel_entitlements)1353*2c2f96dcSApple OSS Distributions txm_associate_kernel_entitlements(
1354*2c2f96dcSApple OSS Distributions void *sig_obj,
1355*2c2f96dcSApple OSS Distributions const void *kernel_entitlements)
1356*2c2f96dcSApple OSS Distributions {
1357*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
1358*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorAssociateKernelEntitlements,
1359*2c2f96dcSApple OSS Distributions .num_input_args = 2,
1360*2c2f96dcSApple OSS Distributions .failure_fatal = true,
1361*2c2f96dcSApple OSS Distributions };
1362*2c2f96dcSApple OSS Distributions
1363*2c2f96dcSApple OSS Distributions /* Associate the kernel entitlements -- should not fail */
1364*2c2f96dcSApple OSS Distributions txm_kernel_call(&txm_call, sig_obj, kernel_entitlements);
1365*2c2f96dcSApple OSS Distributions
1366*2c2f96dcSApple OSS Distributions return KERN_SUCCESS;
1367*2c2f96dcSApple OSS Distributions }
1368*2c2f96dcSApple OSS Distributions
1369*2c2f96dcSApple OSS Distributions kern_return_t
txm_resolve_kernel_entitlements(pmap_t pmap,const void ** kernel_entitlements)1370*2c2f96dcSApple OSS Distributions txm_resolve_kernel_entitlements(
1371*2c2f96dcSApple OSS Distributions pmap_t pmap,
1372*2c2f96dcSApple OSS Distributions const void **kernel_entitlements)
1373*2c2f96dcSApple OSS Distributions {
1374*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
1375*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorResolveKernelEntitlementsAddressSpace,
1376*2c2f96dcSApple OSS Distributions .skip_logs = true,
1377*2c2f96dcSApple OSS Distributions .num_input_args = 1,
1378*2c2f96dcSApple OSS Distributions .num_output_args = 1,
1379*2c2f96dcSApple OSS Distributions .failure_silent = true,
1380*2c2f96dcSApple OSS Distributions };
1381*2c2f96dcSApple OSS Distributions TXMAddressSpace_t *txm_addr_space = NULL;
1382*2c2f96dcSApple OSS Distributions kern_return_t ret = KERN_DENIED;
1383*2c2f96dcSApple OSS Distributions
1384*2c2f96dcSApple OSS Distributions if (pmap == pmap_txm_kernel_pmap()) {
1385*2c2f96dcSApple OSS Distributions return KERN_NOT_FOUND;
1386*2c2f96dcSApple OSS Distributions }
1387*2c2f96dcSApple OSS Distributions txm_addr_space = pmap_txm_addr_space(pmap);
1388*2c2f96dcSApple OSS Distributions
1389*2c2f96dcSApple OSS Distributions pmap_txm_acquire_shared_lock(pmap);
1390*2c2f96dcSApple OSS Distributions ret = txm_kernel_call(&txm_call, txm_addr_space);
1391*2c2f96dcSApple OSS Distributions pmap_txm_release_shared_lock(pmap);
1392*2c2f96dcSApple OSS Distributions
1393*2c2f96dcSApple OSS Distributions if ((ret == KERN_SUCCESS) && (kernel_entitlements != NULL)) {
1394*2c2f96dcSApple OSS Distributions *kernel_entitlements = (const void*)txm_call.return_words[0];
1395*2c2f96dcSApple OSS Distributions }
1396*2c2f96dcSApple OSS Distributions return ret;
1397*2c2f96dcSApple OSS Distributions }
1398*2c2f96dcSApple OSS Distributions
1399*2c2f96dcSApple OSS Distributions kern_return_t
txm_accelerate_entitlements(void * sig_obj,CEQueryContext_t * ce_ctx)1400*2c2f96dcSApple OSS Distributions txm_accelerate_entitlements(
1401*2c2f96dcSApple OSS Distributions void *sig_obj,
1402*2c2f96dcSApple OSS Distributions CEQueryContext_t *ce_ctx)
1403*2c2f96dcSApple OSS Distributions {
1404*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
1405*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorAccelerateEntitlements,
1406*2c2f96dcSApple OSS Distributions .num_input_args = 1,
1407*2c2f96dcSApple OSS Distributions .num_output_args = 1,
1408*2c2f96dcSApple OSS Distributions };
1409*2c2f96dcSApple OSS Distributions kern_return_t ret = KERN_DENIED;
1410*2c2f96dcSApple OSS Distributions
1411*2c2f96dcSApple OSS Distributions ret = txm_kernel_call(&txm_call, sig_obj);
1412*2c2f96dcSApple OSS Distributions if ((ret == KERN_SUCCESS) && (ce_ctx != NULL)) {
1413*2c2f96dcSApple OSS Distributions *ce_ctx = (CEQueryContext_t)txm_call.return_words[0];
1414*2c2f96dcSApple OSS Distributions }
1415*2c2f96dcSApple OSS Distributions
1416*2c2f96dcSApple OSS Distributions return ret;
1417*2c2f96dcSApple OSS Distributions }
1418*2c2f96dcSApple OSS Distributions
1419*2c2f96dcSApple OSS Distributions #pragma mark Image4
1420*2c2f96dcSApple OSS Distributions
1421*2c2f96dcSApple OSS Distributions void*
txm_image4_storage_data(__unused size_t * allocated_size)1422*2c2f96dcSApple OSS Distributions txm_image4_storage_data(
1423*2c2f96dcSApple OSS Distributions __unused size_t *allocated_size)
1424*2c2f96dcSApple OSS Distributions {
1425*2c2f96dcSApple OSS Distributions /*
1426*2c2f96dcSApple OSS Distributions * AppleImage4 builds a variant of TXM which TXM should link against statically
1427*2c2f96dcSApple OSS Distributions * thereby removing the need for the kernel to allocate some data on behalf of
1428*2c2f96dcSApple OSS Distributions * the kernel extension.
1429*2c2f96dcSApple OSS Distributions */
1430*2c2f96dcSApple OSS Distributions panic("unsupported AppleImage4 interface");
1431*2c2f96dcSApple OSS Distributions }
1432*2c2f96dcSApple OSS Distributions
1433*2c2f96dcSApple OSS Distributions void
txm_image4_set_nonce(const img4_nonce_domain_index_t ndi,const img4_nonce_t * nonce)1434*2c2f96dcSApple OSS Distributions txm_image4_set_nonce(
1435*2c2f96dcSApple OSS Distributions const img4_nonce_domain_index_t ndi,
1436*2c2f96dcSApple OSS Distributions const img4_nonce_t *nonce)
1437*2c2f96dcSApple OSS Distributions {
1438*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
1439*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorImage4SetNonce,
1440*2c2f96dcSApple OSS Distributions .failure_fatal = true,
1441*2c2f96dcSApple OSS Distributions .num_input_args = 2,
1442*2c2f96dcSApple OSS Distributions };
1443*2c2f96dcSApple OSS Distributions
1444*2c2f96dcSApple OSS Distributions txm_kernel_call(&txm_call, ndi, nonce);
1445*2c2f96dcSApple OSS Distributions }
1446*2c2f96dcSApple OSS Distributions
1447*2c2f96dcSApple OSS Distributions void
txm_image4_roll_nonce(const img4_nonce_domain_index_t ndi)1448*2c2f96dcSApple OSS Distributions txm_image4_roll_nonce(
1449*2c2f96dcSApple OSS Distributions const img4_nonce_domain_index_t ndi)
1450*2c2f96dcSApple OSS Distributions {
1451*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
1452*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorImage4RollNonce,
1453*2c2f96dcSApple OSS Distributions .failure_fatal = true,
1454*2c2f96dcSApple OSS Distributions .num_input_args = 1,
1455*2c2f96dcSApple OSS Distributions };
1456*2c2f96dcSApple OSS Distributions
1457*2c2f96dcSApple OSS Distributions txm_kernel_call(&txm_call, ndi);
1458*2c2f96dcSApple OSS Distributions }
1459*2c2f96dcSApple OSS Distributions
1460*2c2f96dcSApple OSS Distributions errno_t
txm_image4_copy_nonce(const img4_nonce_domain_index_t ndi,img4_nonce_t * nonce_out)1461*2c2f96dcSApple OSS Distributions txm_image4_copy_nonce(
1462*2c2f96dcSApple OSS Distributions const img4_nonce_domain_index_t ndi,
1463*2c2f96dcSApple OSS Distributions img4_nonce_t *nonce_out)
1464*2c2f96dcSApple OSS Distributions {
1465*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
1466*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorImage4GetNonce,
1467*2c2f96dcSApple OSS Distributions .num_input_args = 1,
1468*2c2f96dcSApple OSS Distributions .num_output_args = 1,
1469*2c2f96dcSApple OSS Distributions };
1470*2c2f96dcSApple OSS Distributions const img4_nonce_t *nonce = NULL;
1471*2c2f96dcSApple OSS Distributions TXMReturn_t txm_ret = {0};
1472*2c2f96dcSApple OSS Distributions kern_return_t ret = KERN_DENIED;
1473*2c2f96dcSApple OSS Distributions
1474*2c2f96dcSApple OSS Distributions ret = txm_kernel_call(&txm_call, ndi);
1475*2c2f96dcSApple OSS Distributions if (ret != KERN_SUCCESS) {
1476*2c2f96dcSApple OSS Distributions txm_ret = txm_call.txm_ret;
1477*2c2f96dcSApple OSS Distributions if (txm_ret.returnCode != kTXMReturnCodeErrno) {
1478*2c2f96dcSApple OSS Distributions return EPERM;
1479*2c2f96dcSApple OSS Distributions }
1480*2c2f96dcSApple OSS Distributions return txm_ret.errnoRet;
1481*2c2f96dcSApple OSS Distributions }
1482*2c2f96dcSApple OSS Distributions
1483*2c2f96dcSApple OSS Distributions /* Acquire a pointer to the nonce from TXM */
1484*2c2f96dcSApple OSS Distributions nonce = (const img4_nonce_t*)txm_call.return_words[0];
1485*2c2f96dcSApple OSS Distributions
1486*2c2f96dcSApple OSS Distributions if (nonce_out) {
1487*2c2f96dcSApple OSS Distributions *nonce_out = *nonce;
1488*2c2f96dcSApple OSS Distributions }
1489*2c2f96dcSApple OSS Distributions return 0;
1490*2c2f96dcSApple OSS Distributions }
1491*2c2f96dcSApple OSS Distributions
1492*2c2f96dcSApple OSS Distributions errno_t
txm_image4_execute_object(img4_runtime_object_spec_index_t obj_spec_index,const img4_buff_t * payload,const img4_buff_t * manifest)1493*2c2f96dcSApple OSS Distributions txm_image4_execute_object(
1494*2c2f96dcSApple OSS Distributions img4_runtime_object_spec_index_t obj_spec_index,
1495*2c2f96dcSApple OSS Distributions const img4_buff_t *payload,
1496*2c2f96dcSApple OSS Distributions const img4_buff_t *manifest)
1497*2c2f96dcSApple OSS Distributions {
1498*2c2f96dcSApple OSS Distributions /* Not supported within TXM yet */
1499*2c2f96dcSApple OSS Distributions (void)obj_spec_index;
1500*2c2f96dcSApple OSS Distributions (void)payload;
1501*2c2f96dcSApple OSS Distributions (void)manifest;
1502*2c2f96dcSApple OSS Distributions
1503*2c2f96dcSApple OSS Distributions printf("image4 object execution isn't supported by TXM\n");
1504*2c2f96dcSApple OSS Distributions return ENOSYS;
1505*2c2f96dcSApple OSS Distributions }
1506*2c2f96dcSApple OSS Distributions
1507*2c2f96dcSApple OSS Distributions errno_t
txm_image4_copy_object(img4_runtime_object_spec_index_t obj_spec_index,vm_address_t object_out,size_t * object_length)1508*2c2f96dcSApple OSS Distributions txm_image4_copy_object(
1509*2c2f96dcSApple OSS Distributions img4_runtime_object_spec_index_t obj_spec_index,
1510*2c2f96dcSApple OSS Distributions vm_address_t object_out,
1511*2c2f96dcSApple OSS Distributions size_t *object_length)
1512*2c2f96dcSApple OSS Distributions {
1513*2c2f96dcSApple OSS Distributions /* Not supported within TXM yet */
1514*2c2f96dcSApple OSS Distributions (void)obj_spec_index;
1515*2c2f96dcSApple OSS Distributions (void)object_out;
1516*2c2f96dcSApple OSS Distributions (void)object_length;
1517*2c2f96dcSApple OSS Distributions
1518*2c2f96dcSApple OSS Distributions printf("image4 object copying isn't supported by TXM\n");
1519*2c2f96dcSApple OSS Distributions return ENOSYS;
1520*2c2f96dcSApple OSS Distributions }
1521*2c2f96dcSApple OSS Distributions
1522*2c2f96dcSApple OSS Distributions const void*
txm_image4_get_monitor_exports(void)1523*2c2f96dcSApple OSS Distributions txm_image4_get_monitor_exports(void)
1524*2c2f96dcSApple OSS Distributions {
1525*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
1526*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorImage4GetExports,
1527*2c2f96dcSApple OSS Distributions .failure_fatal = true,
1528*2c2f96dcSApple OSS Distributions .num_output_args = 1,
1529*2c2f96dcSApple OSS Distributions };
1530*2c2f96dcSApple OSS Distributions
1531*2c2f96dcSApple OSS Distributions txm_kernel_call(&txm_call);
1532*2c2f96dcSApple OSS Distributions return (const void*)txm_call.return_words[0];
1533*2c2f96dcSApple OSS Distributions }
1534*2c2f96dcSApple OSS Distributions
1535*2c2f96dcSApple OSS Distributions errno_t
txm_image4_set_release_type(const char * release_type)1536*2c2f96dcSApple OSS Distributions txm_image4_set_release_type(
1537*2c2f96dcSApple OSS Distributions const char *release_type)
1538*2c2f96dcSApple OSS Distributions {
1539*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
1540*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorImage4SetReleaseType,
1541*2c2f96dcSApple OSS Distributions .failure_fatal = true,
1542*2c2f96dcSApple OSS Distributions .num_input_args = 1,
1543*2c2f96dcSApple OSS Distributions };
1544*2c2f96dcSApple OSS Distributions
1545*2c2f96dcSApple OSS Distributions /* Set the release type -- cannot fail */
1546*2c2f96dcSApple OSS Distributions txm_kernel_call(&txm_call, release_type);
1547*2c2f96dcSApple OSS Distributions
1548*2c2f96dcSApple OSS Distributions return 0;
1549*2c2f96dcSApple OSS Distributions }
1550*2c2f96dcSApple OSS Distributions
1551*2c2f96dcSApple OSS Distributions errno_t
txm_image4_set_bnch_shadow(const img4_nonce_domain_index_t ndi)1552*2c2f96dcSApple OSS Distributions txm_image4_set_bnch_shadow(
1553*2c2f96dcSApple OSS Distributions const img4_nonce_domain_index_t ndi)
1554*2c2f96dcSApple OSS Distributions {
1555*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
1556*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorImage4SetBootNonceShadow,
1557*2c2f96dcSApple OSS Distributions .failure_fatal = true,
1558*2c2f96dcSApple OSS Distributions .num_input_args = 1,
1559*2c2f96dcSApple OSS Distributions };
1560*2c2f96dcSApple OSS Distributions
1561*2c2f96dcSApple OSS Distributions /* Set the release type -- cannot fail */
1562*2c2f96dcSApple OSS Distributions txm_kernel_call(&txm_call, ndi);
1563*2c2f96dcSApple OSS Distributions
1564*2c2f96dcSApple OSS Distributions return 0;
1565*2c2f96dcSApple OSS Distributions }
1566*2c2f96dcSApple OSS Distributions
1567*2c2f96dcSApple OSS Distributions #pragma mark Image4 - New
1568*2c2f96dcSApple OSS Distributions
1569*2c2f96dcSApple OSS Distributions static inline bool
_txm_image4_monitor_trap_supported(image4_cs_trap_t selector)1570*2c2f96dcSApple OSS Distributions _txm_image4_monitor_trap_supported(
1571*2c2f96dcSApple OSS Distributions image4_cs_trap_t selector)
1572*2c2f96dcSApple OSS Distributions {
1573*2c2f96dcSApple OSS Distributions switch (selector) {
1574*2c2f96dcSApple OSS Distributions #if kTXMImage4APIVersion >= 1
1575*2c2f96dcSApple OSS Distributions case IMAGE4_CS_TRAP_KMOD_SET_RELEASE_TYPE:
1576*2c2f96dcSApple OSS Distributions case IMAGE4_CS_TRAP_KMOD_PIN_ROOT:
1577*2c2f96dcSApple OSS Distributions case IMAGE4_CS_TRAP_KMOD_EVALUATE_TRUST:
1578*2c2f96dcSApple OSS Distributions case IMAGE4_CS_TRAP_NONCE_SET:
1579*2c2f96dcSApple OSS Distributions case IMAGE4_CS_TRAP_NONCE_ROLL:
1580*2c2f96dcSApple OSS Distributions case IMAGE4_CS_TRAP_IMAGE_ACTIVATE:
1581*2c2f96dcSApple OSS Distributions return true;
1582*2c2f96dcSApple OSS Distributions #endif
1583*2c2f96dcSApple OSS Distributions
1584*2c2f96dcSApple OSS Distributions default:
1585*2c2f96dcSApple OSS Distributions return false;
1586*2c2f96dcSApple OSS Distributions }
1587*2c2f96dcSApple OSS Distributions }
1588*2c2f96dcSApple OSS Distributions
1589*2c2f96dcSApple OSS Distributions kern_return_t
txm_image4_transfer_region(image4_cs_trap_t selector,vm_address_t region_addr,vm_size_t region_size)1590*2c2f96dcSApple OSS Distributions txm_image4_transfer_region(
1591*2c2f96dcSApple OSS Distributions image4_cs_trap_t selector,
1592*2c2f96dcSApple OSS Distributions vm_address_t region_addr,
1593*2c2f96dcSApple OSS Distributions vm_size_t region_size)
1594*2c2f96dcSApple OSS Distributions {
1595*2c2f96dcSApple OSS Distributions if (_txm_image4_monitor_trap_supported(selector) == true) {
1596*2c2f96dcSApple OSS Distributions txm_transfer_region(region_addr, region_size);
1597*2c2f96dcSApple OSS Distributions }
1598*2c2f96dcSApple OSS Distributions return KERN_SUCCESS;
1599*2c2f96dcSApple OSS Distributions }
1600*2c2f96dcSApple OSS Distributions
1601*2c2f96dcSApple OSS Distributions kern_return_t
txm_image4_reclaim_region(image4_cs_trap_t selector,vm_address_t region_addr,vm_size_t region_size)1602*2c2f96dcSApple OSS Distributions txm_image4_reclaim_region(
1603*2c2f96dcSApple OSS Distributions image4_cs_trap_t selector,
1604*2c2f96dcSApple OSS Distributions vm_address_t region_addr,
1605*2c2f96dcSApple OSS Distributions vm_size_t region_size)
1606*2c2f96dcSApple OSS Distributions {
1607*2c2f96dcSApple OSS Distributions if (_txm_image4_monitor_trap_supported(selector) == true) {
1608*2c2f96dcSApple OSS Distributions txm_reclaim_region(region_addr, region_size);
1609*2c2f96dcSApple OSS Distributions }
1610*2c2f96dcSApple OSS Distributions return KERN_SUCCESS;
1611*2c2f96dcSApple OSS Distributions }
1612*2c2f96dcSApple OSS Distributions
1613*2c2f96dcSApple OSS Distributions errno_t
txm_image4_monitor_trap(image4_cs_trap_t selector,__unused const void * input_data,__unused size_t input_size)1614*2c2f96dcSApple OSS Distributions txm_image4_monitor_trap(
1615*2c2f96dcSApple OSS Distributions image4_cs_trap_t selector,
1616*2c2f96dcSApple OSS Distributions __unused const void *input_data,
1617*2c2f96dcSApple OSS Distributions __unused size_t input_size)
1618*2c2f96dcSApple OSS Distributions {
1619*2c2f96dcSApple OSS Distributions #if kTXMKernelAPIVersion >= 2
1620*2c2f96dcSApple OSS Distributions txm_call_t txm_call = {
1621*2c2f96dcSApple OSS Distributions .selector = kTXMKernelSelectorImage4Dispatch,
1622*2c2f96dcSApple OSS Distributions .num_input_args = 5,
1623*2c2f96dcSApple OSS Distributions };
1624*2c2f96dcSApple OSS Distributions
1625*2c2f96dcSApple OSS Distributions kern_return_t ret = txm_kernel_call(
1626*2c2f96dcSApple OSS Distributions &txm_call, selector,
1627*2c2f96dcSApple OSS Distributions input_data, input_size,
1628*2c2f96dcSApple OSS Distributions NULL, NULL);
1629*2c2f96dcSApple OSS Distributions
1630*2c2f96dcSApple OSS Distributions /* Return 0 for success */
1631*2c2f96dcSApple OSS Distributions if (ret == KERN_SUCCESS) {
1632*2c2f96dcSApple OSS Distributions return 0;
1633*2c2f96dcSApple OSS Distributions }
1634*2c2f96dcSApple OSS Distributions
1635*2c2f96dcSApple OSS Distributions /* Check for an errno_t return */
1636*2c2f96dcSApple OSS Distributions if (txm_call.txm_ret.returnCode == kTXMReturnCodeErrno) {
1637*2c2f96dcSApple OSS Distributions if (txm_call.txm_ret.errnoRet == 0) {
1638*2c2f96dcSApple OSS Distributions panic("image4 dispatch: unexpected success errno_t: %llu", selector);
1639*2c2f96dcSApple OSS Distributions }
1640*2c2f96dcSApple OSS Distributions return txm_call.txm_ret.errnoRet;
1641*2c2f96dcSApple OSS Distributions }
1642*2c2f96dcSApple OSS Distributions
1643*2c2f96dcSApple OSS Distributions /* Return a generic error */
1644*2c2f96dcSApple OSS Distributions return EPERM;
1645*2c2f96dcSApple OSS Distributions #else
1646*2c2f96dcSApple OSS Distributions printf("image4 dispatch: traps not supported: %llu\n", selector);
1647*2c2f96dcSApple OSS Distributions return ENOSYS;
1648*2c2f96dcSApple OSS Distributions #endif
1649*2c2f96dcSApple OSS Distributions }
1650*2c2f96dcSApple OSS Distributions
1651*2c2f96dcSApple OSS Distributions #endif /* CONFIG_SPTM */
1652