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