1*4f1223e8SApple OSS Distributions /*
2*4f1223e8SApple OSS Distributions * Copyright (c) 2008-2016 Apple Inc. All rights reserved.
3*4f1223e8SApple OSS Distributions *
4*4f1223e8SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*4f1223e8SApple OSS Distributions *
6*4f1223e8SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*4f1223e8SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*4f1223e8SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*4f1223e8SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*4f1223e8SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*4f1223e8SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*4f1223e8SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*4f1223e8SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*4f1223e8SApple OSS Distributions *
15*4f1223e8SApple OSS Distributions * Please obtain a copy of the License at
16*4f1223e8SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*4f1223e8SApple OSS Distributions *
18*4f1223e8SApple OSS Distributions * The Original Code and all software distributed under the License are
19*4f1223e8SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*4f1223e8SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*4f1223e8SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*4f1223e8SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*4f1223e8SApple OSS Distributions * Please see the License for the specific language governing rights and
24*4f1223e8SApple OSS Distributions * limitations under the License.
25*4f1223e8SApple OSS Distributions *
26*4f1223e8SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*4f1223e8SApple OSS Distributions */
28*4f1223e8SApple OSS Distributions
29*4f1223e8SApple OSS Distributions extern "C" {
30*4f1223e8SApple OSS Distributions #include <libkern/OSKextLibPrivate.h>
31*4f1223e8SApple OSS Distributions #include <libkern/mkext.h>
32*4f1223e8SApple OSS Distributions };
33*4f1223e8SApple OSS Distributions
34*4f1223e8SApple OSS Distributions #include <kern/telemetry.h>
35*4f1223e8SApple OSS Distributions #include <libkern/c++/OSContainers.h>
36*4f1223e8SApple OSS Distributions #include <libkern/c++/OSKext.h>
37*4f1223e8SApple OSS Distributions #include <libkern/OSKextLib.h>
38*4f1223e8SApple OSS Distributions #include <libkern/OSKextLibPrivate.h>
39*4f1223e8SApple OSS Distributions #include <vm/vm_kern_xnu.h>
40*4f1223e8SApple OSS Distributions #include <vm/vm_map_xnu.h>
41*4f1223e8SApple OSS Distributions
42*4f1223e8SApple OSS Distributions extern "C" {
43*4f1223e8SApple OSS Distributions #if PRAGMA_MARK
44*4f1223e8SApple OSS Distributions #pragma mark C-based kext interface (loading/loaded kexts only)
45*4f1223e8SApple OSS Distributions #endif
46*4f1223e8SApple OSS Distributions /*********************************************************************
47*4f1223e8SApple OSS Distributions *********************************************************************/
48*4f1223e8SApple OSS Distributions kern_return_t
OSKextLoadKextWithIdentifier(const char * bundle_id)49*4f1223e8SApple OSS Distributions OSKextLoadKextWithIdentifier(const char * bundle_id)
50*4f1223e8SApple OSS Distributions {
51*4f1223e8SApple OSS Distributions return OSKext::loadKextWithIdentifier(bundle_id);
52*4f1223e8SApple OSS Distributions }
53*4f1223e8SApple OSS Distributions
54*4f1223e8SApple OSS Distributions uint32_t OSKextGetLoadTagForIdentifier(const char * kextIdentifier);
55*4f1223e8SApple OSS Distributions /*********************************************************************
56*4f1223e8SApple OSS Distributions *********************************************************************/
57*4f1223e8SApple OSS Distributions uint32_t
OSKextGetLoadTagForIdentifier(const char * kextIdentifier)58*4f1223e8SApple OSS Distributions OSKextGetLoadTagForIdentifier(const char * kextIdentifier)
59*4f1223e8SApple OSS Distributions {
60*4f1223e8SApple OSS Distributions uint32_t result = kOSKextInvalidLoadTag;
61*4f1223e8SApple OSS Distributions OSKext * theKext = NULL; // must release
62*4f1223e8SApple OSS Distributions
63*4f1223e8SApple OSS Distributions if (!kextIdentifier) {
64*4f1223e8SApple OSS Distributions goto finish;
65*4f1223e8SApple OSS Distributions }
66*4f1223e8SApple OSS Distributions
67*4f1223e8SApple OSS Distributions theKext = OSKext::lookupKextWithIdentifier(kextIdentifier);
68*4f1223e8SApple OSS Distributions if (theKext && theKext->isLoaded()) {
69*4f1223e8SApple OSS Distributions result = theKext->getLoadTag();
70*4f1223e8SApple OSS Distributions }
71*4f1223e8SApple OSS Distributions finish:
72*4f1223e8SApple OSS Distributions if (theKext) {
73*4f1223e8SApple OSS Distributions theKext->release();
74*4f1223e8SApple OSS Distributions }
75*4f1223e8SApple OSS Distributions return result;
76*4f1223e8SApple OSS Distributions }
77*4f1223e8SApple OSS Distributions
78*4f1223e8SApple OSS Distributions /*********************************************************************
79*4f1223e8SApple OSS Distributions *********************************************************************/
80*4f1223e8SApple OSS Distributions
81*4f1223e8SApple OSS Distributions // FIXME: Implementation of this function is hidden from the static analyzer.
82*4f1223e8SApple OSS Distributions // The analyzer is worried about the lack of release and suggests
83*4f1223e8SApple OSS Distributions // refactoring the code into the typical non-owning container pattern.
84*4f1223e8SApple OSS Distributions // Feel free to remove the #ifndef and address the warning!
85*4f1223e8SApple OSS Distributions #ifndef __clang_analyzer__
86*4f1223e8SApple OSS Distributions OSReturn
OSKextRetainKextWithLoadTag(uint32_t loadTag)87*4f1223e8SApple OSS Distributions OSKextRetainKextWithLoadTag(uint32_t loadTag)
88*4f1223e8SApple OSS Distributions {
89*4f1223e8SApple OSS Distributions OSReturn result = kOSKextReturnNotFound;
90*4f1223e8SApple OSS Distributions OSKext * theKext = NULL;// do not release; as this function is a retain
91*4f1223e8SApple OSS Distributions
92*4f1223e8SApple OSS Distributions if (loadTag == kOSKextInvalidLoadTag) {
93*4f1223e8SApple OSS Distributions result = kOSKextReturnInvalidArgument;
94*4f1223e8SApple OSS Distributions goto finish;
95*4f1223e8SApple OSS Distributions }
96*4f1223e8SApple OSS Distributions theKext = OSKext::lookupKextWithLoadTag(loadTag);
97*4f1223e8SApple OSS Distributions if (theKext) {
98*4f1223e8SApple OSS Distributions result = kOSReturnSuccess;
99*4f1223e8SApple OSS Distributions
100*4f1223e8SApple OSS Distributions OSKextLog(theKext,
101*4f1223e8SApple OSS Distributions kOSKextLogDebugLevel |
102*4f1223e8SApple OSS Distributions kOSKextLogKextBookkeepingFlag,
103*4f1223e8SApple OSS Distributions "Kext %s (load tag %d) has been retained.",
104*4f1223e8SApple OSS Distributions theKext->getIdentifierCString(),
105*4f1223e8SApple OSS Distributions loadTag);
106*4f1223e8SApple OSS Distributions
107*4f1223e8SApple OSS Distributions /* Call this after so a log message about autounload comes second.
108*4f1223e8SApple OSS Distributions */
109*4f1223e8SApple OSS Distributions theKext->setAutounloadEnabled(true);
110*4f1223e8SApple OSS Distributions } else {
111*4f1223e8SApple OSS Distributions OSKextLog(theKext,
112*4f1223e8SApple OSS Distributions kOSKextLogErrorLevel |
113*4f1223e8SApple OSS Distributions kOSKextLogKextBookkeepingFlag,
114*4f1223e8SApple OSS Distributions "Can't retain kext with load tag %d - no such kext is loaded.",
115*4f1223e8SApple OSS Distributions loadTag);
116*4f1223e8SApple OSS Distributions }
117*4f1223e8SApple OSS Distributions finish:
118*4f1223e8SApple OSS Distributions return result;
119*4f1223e8SApple OSS Distributions }
120*4f1223e8SApple OSS Distributions #endif // __clang_analyzer__
121*4f1223e8SApple OSS Distributions
122*4f1223e8SApple OSS Distributions /*********************************************************************
123*4f1223e8SApple OSS Distributions *********************************************************************/
124*4f1223e8SApple OSS Distributions
125*4f1223e8SApple OSS Distributions // FIXME: Implementation of this function is hidden from the static analyzer.
126*4f1223e8SApple OSS Distributions // The analyzer is worried about the double release and suggests
127*4f1223e8SApple OSS Distributions // refactoring the code into the typical non-owning container pattern.
128*4f1223e8SApple OSS Distributions // Feel free to remove the #ifndef and address the warning!
129*4f1223e8SApple OSS Distributions #ifndef __clang_analyzer__
130*4f1223e8SApple OSS Distributions OSReturn
OSKextReleaseKextWithLoadTag(uint32_t loadTag)131*4f1223e8SApple OSS Distributions OSKextReleaseKextWithLoadTag(uint32_t loadTag)
132*4f1223e8SApple OSS Distributions {
133*4f1223e8SApple OSS Distributions OSReturn result = kOSKextReturnNotFound;
134*4f1223e8SApple OSS Distributions OSKext * theKext = NULL; // must release twice!
135*4f1223e8SApple OSS Distributions
136*4f1223e8SApple OSS Distributions if (loadTag == kOSKextInvalidLoadTag) {
137*4f1223e8SApple OSS Distributions result = kOSKextReturnInvalidArgument;
138*4f1223e8SApple OSS Distributions goto finish;
139*4f1223e8SApple OSS Distributions }
140*4f1223e8SApple OSS Distributions theKext = OSKext::lookupKextWithLoadTag(loadTag);
141*4f1223e8SApple OSS Distributions if (theKext) {
142*4f1223e8SApple OSS Distributions result = kOSReturnSuccess;
143*4f1223e8SApple OSS Distributions OSKext::considerUnloads(); // schedule autounload pass
144*4f1223e8SApple OSS Distributions theKext->release(); // do the release the caller wants
145*4f1223e8SApple OSS Distributions theKext->release(); // now do the release on the lookup
146*4f1223e8SApple OSS Distributions OSKextLog(theKext,
147*4f1223e8SApple OSS Distributions kOSKextLogDebugLevel |
148*4f1223e8SApple OSS Distributions kOSKextLogKextBookkeepingFlag,
149*4f1223e8SApple OSS Distributions "Kext %s (load tag %d) has been released.",
150*4f1223e8SApple OSS Distributions theKext->getIdentifierCString(),
151*4f1223e8SApple OSS Distributions loadTag);
152*4f1223e8SApple OSS Distributions } else {
153*4f1223e8SApple OSS Distributions OSKextLog(theKext,
154*4f1223e8SApple OSS Distributions kOSKextLogErrorLevel |
155*4f1223e8SApple OSS Distributions kOSKextLogKextBookkeepingFlag,
156*4f1223e8SApple OSS Distributions "Can't release kext with load tag %d - no such kext is loaded.",
157*4f1223e8SApple OSS Distributions loadTag);
158*4f1223e8SApple OSS Distributions }
159*4f1223e8SApple OSS Distributions
160*4f1223e8SApple OSS Distributions // xxx - should I check that the refcount of the OSKext is above the lower bound?
161*4f1223e8SApple OSS Distributions // xxx - do we want a OSKextGetRetainCountOfKextWithLoadTag()?
162*4f1223e8SApple OSS Distributions finish:
163*4f1223e8SApple OSS Distributions return result;
164*4f1223e8SApple OSS Distributions }
165*4f1223e8SApple OSS Distributions #endif // __clang_analyzer__
166*4f1223e8SApple OSS Distributions
167*4f1223e8SApple OSS Distributions /*********************************************************************
168*4f1223e8SApple OSS Distributions * Not to be called by the kext being unloaded!
169*4f1223e8SApple OSS Distributions *********************************************************************/
170*4f1223e8SApple OSS Distributions OSReturn
OSKextUnloadKextWithLoadTag(uint32_t loadTag)171*4f1223e8SApple OSS Distributions OSKextUnloadKextWithLoadTag(uint32_t loadTag)
172*4f1223e8SApple OSS Distributions {
173*4f1223e8SApple OSS Distributions return OSKext::removeKextWithLoadTag(loadTag,
174*4f1223e8SApple OSS Distributions /* terminateServicesAndRemovePersonalitiesFlag */ false);
175*4f1223e8SApple OSS Distributions }
176*4f1223e8SApple OSS Distributions
177*4f1223e8SApple OSS Distributions
178*4f1223e8SApple OSS Distributions #if PRAGMA_MARK
179*4f1223e8SApple OSS Distributions #pragma mark Kext Requests
180*4f1223e8SApple OSS Distributions #endif
181*4f1223e8SApple OSS Distributions /*********************************************************************
182*4f1223e8SApple OSS Distributions * Kext Requests
183*4f1223e8SApple OSS Distributions *********************************************************************/
184*4f1223e8SApple OSS Distributions OSReturn
OSKextRequestResource(const char * kextIdentifier,const char * resourceName,OSKextRequestResourceCallback callback,void * context,OSKextRequestTag * requestTagOut)185*4f1223e8SApple OSS Distributions OSKextRequestResource(
186*4f1223e8SApple OSS Distributions const char * kextIdentifier,
187*4f1223e8SApple OSS Distributions const char * resourceName,
188*4f1223e8SApple OSS Distributions OSKextRequestResourceCallback callback,
189*4f1223e8SApple OSS Distributions void * context,
190*4f1223e8SApple OSS Distributions OSKextRequestTag * requestTagOut)
191*4f1223e8SApple OSS Distributions {
192*4f1223e8SApple OSS Distributions return OSKext::requestResource(kextIdentifier, resourceName,
193*4f1223e8SApple OSS Distributions callback, context, requestTagOut);
194*4f1223e8SApple OSS Distributions }
195*4f1223e8SApple OSS Distributions
196*4f1223e8SApple OSS Distributions /*********************************************************************
197*4f1223e8SApple OSS Distributions *********************************************************************/
198*4f1223e8SApple OSS Distributions OSReturn
OSKextCancelRequest(OSKextRequestTag requestTag,void ** contextOut)199*4f1223e8SApple OSS Distributions OSKextCancelRequest(
200*4f1223e8SApple OSS Distributions OSKextRequestTag requestTag,
201*4f1223e8SApple OSS Distributions void ** contextOut)
202*4f1223e8SApple OSS Distributions {
203*4f1223e8SApple OSS Distributions return OSKext::cancelRequest(requestTag, contextOut);
204*4f1223e8SApple OSS Distributions }
205*4f1223e8SApple OSS Distributions
206*4f1223e8SApple OSS Distributions #if PRAGMA_MARK
207*4f1223e8SApple OSS Distributions #pragma mark MIG Functions & Wrappers
208*4f1223e8SApple OSS Distributions #endif
209*4f1223e8SApple OSS Distributions /*********************************************************************
210*4f1223e8SApple OSS Distributions * IMPORTANT: vm_map_copyout_size() consumes the requestIn copy
211*4f1223e8SApple OSS Distributions * object on success. Therefore once it has been invoked successfully,
212*4f1223e8SApple OSS Distributions * this routine *must* return KERN_SUCCESS, regardless of our actual
213*4f1223e8SApple OSS Distributions * result. Our contract with the caller is that requestIn must be
214*4f1223e8SApple OSS Distributions * caller-deallocated if we return an error. We use op_result to return
215*4f1223e8SApple OSS Distributions * the real result of our work.
216*4f1223e8SApple OSS Distributions *********************************************************************/
217*4f1223e8SApple OSS Distributions kern_return_t
kext_request(host_priv_t hostPriv,uint32_t clientLogSpec,vm_offset_t requestIn,mach_msg_type_number_t requestLengthIn,vm_offset_t * responseOut,mach_msg_type_number_t * responseLengthOut,vm_offset_t * logDataOut,mach_msg_type_number_t * logDataLengthOut,kern_return_t * op_result)218*4f1223e8SApple OSS Distributions kext_request(
219*4f1223e8SApple OSS Distributions host_priv_t hostPriv,
220*4f1223e8SApple OSS Distributions /* in only */ uint32_t clientLogSpec,
221*4f1223e8SApple OSS Distributions /* in only */ vm_offset_t requestIn,
222*4f1223e8SApple OSS Distributions /* in only */ mach_msg_type_number_t requestLengthIn,
223*4f1223e8SApple OSS Distributions /* out only */ vm_offset_t * responseOut,
224*4f1223e8SApple OSS Distributions /* out only */ mach_msg_type_number_t * responseLengthOut,
225*4f1223e8SApple OSS Distributions /* out only */ vm_offset_t * logDataOut,
226*4f1223e8SApple OSS Distributions /* out only */ mach_msg_type_number_t * logDataLengthOut,
227*4f1223e8SApple OSS Distributions /* out only */ kern_return_t * op_result)
228*4f1223e8SApple OSS Distributions {
229*4f1223e8SApple OSS Distributions kern_return_t result = KERN_FAILURE;
230*4f1223e8SApple OSS Distributions vm_map_address_t map_addr = 0; // do not free/deallocate
231*4f1223e8SApple OSS Distributions char * request = NULL;// must vm_deallocate
232*4f1223e8SApple OSS Distributions
233*4f1223e8SApple OSS Distributions mkext2_header * mkextHeader = NULL;// do not release
234*4f1223e8SApple OSS Distributions bool isMkext = false;
235*4f1223e8SApple OSS Distributions
236*4f1223e8SApple OSS Distributions char * response = NULL;// must kmem_free
237*4f1223e8SApple OSS Distributions uint32_t responseLength = 0;
238*4f1223e8SApple OSS Distributions char * logData = NULL;// must kmem_free
239*4f1223e8SApple OSS Distributions uint32_t logDataLength = 0;
240*4f1223e8SApple OSS Distributions
241*4f1223e8SApple OSS Distributions /* MIG doesn't pass "out" parameters as empty, so clear them immediately
242*4f1223e8SApple OSS Distributions * just in case, or MIG will try to copy out bogus data.
243*4f1223e8SApple OSS Distributions */
244*4f1223e8SApple OSS Distributions *op_result = KERN_FAILURE;
245*4f1223e8SApple OSS Distributions *responseOut = 0;
246*4f1223e8SApple OSS Distributions *responseLengthOut = 0;
247*4f1223e8SApple OSS Distributions *logDataOut = 0;
248*4f1223e8SApple OSS Distributions *logDataLengthOut = 0;
249*4f1223e8SApple OSS Distributions
250*4f1223e8SApple OSS Distributions /* Check for input. Don't discard what isn't there, though.
251*4f1223e8SApple OSS Distributions */
252*4f1223e8SApple OSS Distributions if (!requestLengthIn || !requestIn) {
253*4f1223e8SApple OSS Distributions OSKextLog(/* kext */ NULL,
254*4f1223e8SApple OSS Distributions kOSKextLogErrorLevel |
255*4f1223e8SApple OSS Distributions kOSKextLogIPCFlag,
256*4f1223e8SApple OSS Distributions "Invalid request from user space (no data).");
257*4f1223e8SApple OSS Distributions *op_result = KERN_INVALID_ARGUMENT;
258*4f1223e8SApple OSS Distributions goto finish;
259*4f1223e8SApple OSS Distributions }
260*4f1223e8SApple OSS Distributions
261*4f1223e8SApple OSS Distributions result = vm_map_copyout_size(kernel_map, &map_addr, (vm_map_copy_t)requestIn, requestLengthIn);
262*4f1223e8SApple OSS Distributions if (result != KERN_SUCCESS) {
263*4f1223e8SApple OSS Distributions OSKextLog(/* kext */ NULL,
264*4f1223e8SApple OSS Distributions kOSKextLogErrorLevel |
265*4f1223e8SApple OSS Distributions kOSKextLogIPCFlag,
266*4f1223e8SApple OSS Distributions "vm_map_copyout() failed for request from user space.");
267*4f1223e8SApple OSS Distributions /*
268*4f1223e8SApple OSS Distributions * If we return an error it is our caller's responsibility to
269*4f1223e8SApple OSS Distributions * deallocate the requestIn copy object, so do not deallocate it
270*4f1223e8SApple OSS Distributions * here. See comment above.
271*4f1223e8SApple OSS Distributions */
272*4f1223e8SApple OSS Distributions goto finish;
273*4f1223e8SApple OSS Distributions }
274*4f1223e8SApple OSS Distributions request = CAST_DOWN(char *, map_addr);
275*4f1223e8SApple OSS Distributions
276*4f1223e8SApple OSS Distributions /* Check if request is an mkext; this is always a load request
277*4f1223e8SApple OSS Distributions * and requires root access. If it isn't an mkext, see if it's
278*4f1223e8SApple OSS Distributions * an XML request, and check the request to see if that requires
279*4f1223e8SApple OSS Distributions * root access.
280*4f1223e8SApple OSS Distributions */
281*4f1223e8SApple OSS Distributions if (requestLengthIn > sizeof(mkext2_header)) {
282*4f1223e8SApple OSS Distributions mkextHeader = (mkext2_header *)request;
283*4f1223e8SApple OSS Distributions if (MKEXT_GET_MAGIC(mkextHeader) == MKEXT_MAGIC &&
284*4f1223e8SApple OSS Distributions MKEXT_GET_SIGNATURE(mkextHeader) == MKEXT_SIGN) {
285*4f1223e8SApple OSS Distributions isMkext = true;
286*4f1223e8SApple OSS Distributions }
287*4f1223e8SApple OSS Distributions }
288*4f1223e8SApple OSS Distributions
289*4f1223e8SApple OSS Distributions if (isMkext) {
290*4f1223e8SApple OSS Distributions #if defined(SECURE_KERNEL) || !CONFIG_KXLD
291*4f1223e8SApple OSS Distributions // xxx - something tells me if we have a secure kernel we don't even
292*4f1223e8SApple OSS Distributions // xxx - want to log a message here. :-)
293*4f1223e8SApple OSS Distributions *op_result = KERN_NOT_SUPPORTED;
294*4f1223e8SApple OSS Distributions goto finish;
295*4f1223e8SApple OSS Distributions #else
296*4f1223e8SApple OSS Distributions // xxx - can we find out if calling task is kextd?
297*4f1223e8SApple OSS Distributions // xxx - can we find the name of the calling task?
298*4f1223e8SApple OSS Distributions if (hostPriv == HOST_PRIV_NULL) {
299*4f1223e8SApple OSS Distributions OSKextLog(/* kext */ NULL,
300*4f1223e8SApple OSS Distributions kOSKextLogErrorLevel |
301*4f1223e8SApple OSS Distributions kOSKextLogLoadFlag | kOSKextLogIPCFlag,
302*4f1223e8SApple OSS Distributions "Attempt by non-root process to load a kext.");
303*4f1223e8SApple OSS Distributions *op_result = kOSKextReturnNotPrivileged;
304*4f1223e8SApple OSS Distributions goto finish;
305*4f1223e8SApple OSS Distributions }
306*4f1223e8SApple OSS Distributions
307*4f1223e8SApple OSS Distributions *op_result = OSKext::loadFromMkext((OSKextLogSpec)clientLogSpec,
308*4f1223e8SApple OSS Distributions request, requestLengthIn,
309*4f1223e8SApple OSS Distributions &logData, &logDataLength);
310*4f1223e8SApple OSS Distributions
311*4f1223e8SApple OSS Distributions #endif /* defined(SECURE_KERNEL) */
312*4f1223e8SApple OSS Distributions } else {
313*4f1223e8SApple OSS Distributions /* If the request isn't an mkext, then is should be XML. Parse it
314*4f1223e8SApple OSS Distributions * if possible and hand the request over to OSKext.
315*4f1223e8SApple OSS Distributions */
316*4f1223e8SApple OSS Distributions *op_result = OSKext::handleRequest(hostPriv,
317*4f1223e8SApple OSS Distributions (OSKextLogSpec)clientLogSpec,
318*4f1223e8SApple OSS Distributions request, requestLengthIn,
319*4f1223e8SApple OSS Distributions &response, &responseLength,
320*4f1223e8SApple OSS Distributions &logData, &logDataLength);
321*4f1223e8SApple OSS Distributions }
322*4f1223e8SApple OSS Distributions
323*4f1223e8SApple OSS Distributions if (response && responseLength > 0) {
324*4f1223e8SApple OSS Distributions kern_return_t copyin_result;
325*4f1223e8SApple OSS Distributions
326*4f1223e8SApple OSS Distributions copyin_result = vm_map_copyin(kernel_map,
327*4f1223e8SApple OSS Distributions CAST_USER_ADDR_T(response), responseLength,
328*4f1223e8SApple OSS Distributions /* src_destroy */ false, (vm_map_copy_t *)responseOut);
329*4f1223e8SApple OSS Distributions if (copyin_result == KERN_SUCCESS) {
330*4f1223e8SApple OSS Distributions *responseLengthOut = responseLength;
331*4f1223e8SApple OSS Distributions } else {
332*4f1223e8SApple OSS Distributions OSKextLog(/* kext */ NULL,
333*4f1223e8SApple OSS Distributions kOSKextLogErrorLevel |
334*4f1223e8SApple OSS Distributions kOSKextLogIPCFlag,
335*4f1223e8SApple OSS Distributions "Failed to copy response to request from user space.");
336*4f1223e8SApple OSS Distributions *op_result = copyin_result; // xxx - should we map to our own code?
337*4f1223e8SApple OSS Distributions *responseOut = 0;
338*4f1223e8SApple OSS Distributions *responseLengthOut = 0;
339*4f1223e8SApple OSS Distributions goto finish;
340*4f1223e8SApple OSS Distributions }
341*4f1223e8SApple OSS Distributions }
342*4f1223e8SApple OSS Distributions
343*4f1223e8SApple OSS Distributions if (logData && logDataLength > 0) {
344*4f1223e8SApple OSS Distributions kern_return_t copyin_result;
345*4f1223e8SApple OSS Distributions
346*4f1223e8SApple OSS Distributions copyin_result = vm_map_copyin(kernel_map,
347*4f1223e8SApple OSS Distributions CAST_USER_ADDR_T(logData), logDataLength,
348*4f1223e8SApple OSS Distributions /* src_destroy */ false, (vm_map_copy_t *)logDataOut);
349*4f1223e8SApple OSS Distributions if (copyin_result == KERN_SUCCESS) {
350*4f1223e8SApple OSS Distributions *logDataLengthOut = logDataLength;
351*4f1223e8SApple OSS Distributions } else {
352*4f1223e8SApple OSS Distributions OSKextLog(/* kext */ NULL,
353*4f1223e8SApple OSS Distributions kOSKextLogErrorLevel |
354*4f1223e8SApple OSS Distributions kOSKextLogIPCFlag,
355*4f1223e8SApple OSS Distributions "Failed to copy log data for request from user space.");
356*4f1223e8SApple OSS Distributions *op_result = copyin_result; // xxx - should we map to our own code?
357*4f1223e8SApple OSS Distributions *logDataOut = 0;
358*4f1223e8SApple OSS Distributions *logDataLengthOut = 0;
359*4f1223e8SApple OSS Distributions goto finish;
360*4f1223e8SApple OSS Distributions }
361*4f1223e8SApple OSS Distributions }
362*4f1223e8SApple OSS Distributions
363*4f1223e8SApple OSS Distributions finish:
364*4f1223e8SApple OSS Distributions if (request) {
365*4f1223e8SApple OSS Distributions (void)vm_deallocate(kernel_map, (vm_offset_t)request, requestLengthIn);
366*4f1223e8SApple OSS Distributions }
367*4f1223e8SApple OSS Distributions if (response) {
368*4f1223e8SApple OSS Distributions /* 11981737 - clear uninitialized data in last page */
369*4f1223e8SApple OSS Distributions kmem_free(kernel_map, (vm_offset_t)response, round_page(responseLength));
370*4f1223e8SApple OSS Distributions }
371*4f1223e8SApple OSS Distributions if (logData) {
372*4f1223e8SApple OSS Distributions /* 11981737 - clear uninitialized data in last page */
373*4f1223e8SApple OSS Distributions kmem_free(kernel_map, (vm_offset_t)logData, round_page(logDataLength));
374*4f1223e8SApple OSS Distributions }
375*4f1223e8SApple OSS Distributions
376*4f1223e8SApple OSS Distributions return result;
377*4f1223e8SApple OSS Distributions }
378*4f1223e8SApple OSS Distributions
379*4f1223e8SApple OSS Distributions /*********************************************************************
380*4f1223e8SApple OSS Distributions * Gets the vm_map for the current kext
381*4f1223e8SApple OSS Distributions *********************************************************************/
382*4f1223e8SApple OSS Distributions extern vm_offset_t segPRELINKTEXTB;
383*4f1223e8SApple OSS Distributions extern vm_offset_t segLINKB;
384*4f1223e8SApple OSS Distributions extern unsigned long segSizePRELINKTEXT;
385*4f1223e8SApple OSS Distributions
386*4f1223e8SApple OSS Distributions vm_map_t
kext_get_vm_map(kmod_info_t * info)387*4f1223e8SApple OSS Distributions kext_get_vm_map(kmod_info_t *info)
388*4f1223e8SApple OSS Distributions {
389*4f1223e8SApple OSS Distributions vm_map_t kext_map = NULL;
390*4f1223e8SApple OSS Distributions kc_format_t kcformat;
391*4f1223e8SApple OSS Distributions
392*4f1223e8SApple OSS Distributions if (PE_get_primary_kc_format(&kcformat) && kcformat == KCFormatFileset) {
393*4f1223e8SApple OSS Distributions /* Check if the kext is from the boot KC */
394*4f1223e8SApple OSS Distributions assert(segLINKB >= (segPRELINKTEXTB + segSizePRELINKTEXT));
395*4f1223e8SApple OSS Distributions if ((info->address >= segPRELINKTEXTB) &&
396*4f1223e8SApple OSS Distributions (info->address < segLINKB)) {
397*4f1223e8SApple OSS Distributions kext_map = kernel_map;
398*4f1223e8SApple OSS Distributions } else {
399*4f1223e8SApple OSS Distributions kext_map = g_kext_map;
400*4f1223e8SApple OSS Distributions }
401*4f1223e8SApple OSS Distributions } else {
402*4f1223e8SApple OSS Distributions if ((info->address >= segPRELINKTEXTB) &&
403*4f1223e8SApple OSS Distributions (info->address < (segPRELINKTEXTB + segSizePRELINKTEXT))) {
404*4f1223e8SApple OSS Distributions kext_map = kernel_map;
405*4f1223e8SApple OSS Distributions } else {
406*4f1223e8SApple OSS Distributions kext_map = g_kext_map;
407*4f1223e8SApple OSS Distributions }
408*4f1223e8SApple OSS Distributions }
409*4f1223e8SApple OSS Distributions
410*4f1223e8SApple OSS Distributions return kext_map;
411*4f1223e8SApple OSS Distributions }
412*4f1223e8SApple OSS Distributions
413*4f1223e8SApple OSS Distributions
414*4f1223e8SApple OSS Distributions #if PRAGMA_MARK
415*4f1223e8SApple OSS Distributions /********************************************************************/
416*4f1223e8SApple OSS Distributions #pragma mark Weak linking support
417*4f1223e8SApple OSS Distributions /********************************************************************/
418*4f1223e8SApple OSS Distributions #endif
419*4f1223e8SApple OSS Distributions void
kext_weak_symbol_referenced(void)420*4f1223e8SApple OSS Distributions kext_weak_symbol_referenced(void)
421*4f1223e8SApple OSS Distributions {
422*4f1223e8SApple OSS Distributions panic("A kext referenced an unresolved weak symbol");
423*4f1223e8SApple OSS Distributions }
424*4f1223e8SApple OSS Distributions
425*4f1223e8SApple OSS Distributions const void * const gOSKextUnresolved = (const void *)&kext_weak_symbol_referenced;
426*4f1223e8SApple OSS Distributions
427*4f1223e8SApple OSS Distributions #if PRAGMA_MARK
428*4f1223e8SApple OSS Distributions #pragma mark Kernel-Internal C Functions
429*4f1223e8SApple OSS Distributions #endif
430*4f1223e8SApple OSS Distributions /*********************************************************************
431*4f1223e8SApple OSS Distributions * Called from startup.c.
432*4f1223e8SApple OSS Distributions *********************************************************************/
433*4f1223e8SApple OSS Distributions void
OSKextRemoveKextBootstrap(void)434*4f1223e8SApple OSS Distributions OSKextRemoveKextBootstrap(void)
435*4f1223e8SApple OSS Distributions {
436*4f1223e8SApple OSS Distributions OSKext::removeKextBootstrap();
437*4f1223e8SApple OSS Distributions return;
438*4f1223e8SApple OSS Distributions }
439*4f1223e8SApple OSS Distributions
440*4f1223e8SApple OSS Distributions #if CONFIG_DTRACE
441*4f1223e8SApple OSS Distributions /*********************************************************************
442*4f1223e8SApple OSS Distributions *********************************************************************/
443*4f1223e8SApple OSS Distributions void
OSKextRegisterKextsWithDTrace(void)444*4f1223e8SApple OSS Distributions OSKextRegisterKextsWithDTrace(void)
445*4f1223e8SApple OSS Distributions {
446*4f1223e8SApple OSS Distributions OSKext::registerKextsWithDTrace();
447*4f1223e8SApple OSS Distributions return;
448*4f1223e8SApple OSS Distributions }
449*4f1223e8SApple OSS Distributions #endif /* CONFIG_DTRACE */
450*4f1223e8SApple OSS Distributions
451*4f1223e8SApple OSS Distributions /*********************************************************************
452*4f1223e8SApple OSS Distributions *********************************************************************/
453*4f1223e8SApple OSS Distributions void
kext_dump_panic_lists(int (* printf_func)(const char * fmt,...))454*4f1223e8SApple OSS Distributions kext_dump_panic_lists(int (*printf_func)(const char * fmt, ...))
455*4f1223e8SApple OSS Distributions {
456*4f1223e8SApple OSS Distributions OSKext::printKextPanicLists(printf_func);
457*4f1223e8SApple OSS Distributions return;
458*4f1223e8SApple OSS Distributions }
459*4f1223e8SApple OSS Distributions
460*4f1223e8SApple OSS Distributions #if PRAGMA_MARK
461*4f1223e8SApple OSS Distributions #pragma mark Kmod Compatibility Functions
462*4f1223e8SApple OSS Distributions #endif
463*4f1223e8SApple OSS Distributions /*********************************************************************
464*4f1223e8SApple OSS Distributions **********************************************************************
465*4f1223e8SApple OSS Distributions * KMOD COMPATIBILITY FUNCTIONS *
466*4f1223e8SApple OSS Distributions * (formerly in kmod.c, or C++ bridges from) *
467*4f1223e8SApple OSS Distributions **********************************************************************
468*4f1223e8SApple OSS Distributions **********************************************************************
469*4f1223e8SApple OSS Distributions * These two functions are used in various places in the kernel, but
470*4f1223e8SApple OSS Distributions * are not exported. We might rename them at some point to start with
471*4f1223e8SApple OSS Distributions * kext_ or OSKext.
472*4f1223e8SApple OSS Distributions *
473*4f1223e8SApple OSS Distributions * kmod_panic_dump() must not be called outside of a panic context.
474*4f1223e8SApple OSS Distributions * kmod_dump_log() must not be called in a panic context.
475*4f1223e8SApple OSS Distributions *********************************************************************/
476*4f1223e8SApple OSS Distributions void
kmod_panic_dump(vm_offset_t * addr,unsigned int cnt)477*4f1223e8SApple OSS Distributions kmod_panic_dump(vm_offset_t * addr, unsigned int cnt)
478*4f1223e8SApple OSS Distributions {
479*4f1223e8SApple OSS Distributions extern int paniclog_append_noflush(const char *format, ...) __printflike(1, 2);
480*4f1223e8SApple OSS Distributions
481*4f1223e8SApple OSS Distributions OSKext::printKextsInBacktrace(addr, cnt, &paniclog_append_noflush, 0);
482*4f1223e8SApple OSS Distributions
483*4f1223e8SApple OSS Distributions return;
484*4f1223e8SApple OSS Distributions }
485*4f1223e8SApple OSS Distributions
486*4f1223e8SApple OSS Distributions void
telemetry_backtrace_add_kexts(char * buf,size_t buflen,uintptr_t * frames,uint32_t framecnt)487*4f1223e8SApple OSS Distributions telemetry_backtrace_add_kexts(
488*4f1223e8SApple OSS Distributions char *buf,
489*4f1223e8SApple OSS Distributions size_t buflen,
490*4f1223e8SApple OSS Distributions uintptr_t *frames,
491*4f1223e8SApple OSS Distributions uint32_t framecnt)
492*4f1223e8SApple OSS Distributions {
493*4f1223e8SApple OSS Distributions __block size_t pos = 0;
494*4f1223e8SApple OSS Distributions
495*4f1223e8SApple OSS Distributions OSKext::foreachKextInBacktrace(frames, framecnt, OSKext::kPrintKextsLock,
496*4f1223e8SApple OSS Distributions ^(OSKextLoadedKextSummary *summary, uint32_t index __unused){
497*4f1223e8SApple OSS Distributions uuid_string_t uuid;
498*4f1223e8SApple OSS Distributions uint64_t tmpAddr;
499*4f1223e8SApple OSS Distributions uint64_t tmpSize;
500*4f1223e8SApple OSS Distributions
501*4f1223e8SApple OSS Distributions (void) uuid_unparse(summary->uuid, uuid);
502*4f1223e8SApple OSS Distributions
503*4f1223e8SApple OSS Distributions #if defined(__arm__) || defined(__arm64__)
504*4f1223e8SApple OSS Distributions tmpAddr = summary->text_exec_address;
505*4f1223e8SApple OSS Distributions tmpSize = summary->text_exec_size;
506*4f1223e8SApple OSS Distributions #else
507*4f1223e8SApple OSS Distributions tmpAddr = summary->address;
508*4f1223e8SApple OSS Distributions tmpSize = summary->size;
509*4f1223e8SApple OSS Distributions #endif
510*4f1223e8SApple OSS Distributions tmpAddr -= vm_kernel_stext;
511*4f1223e8SApple OSS Distributions pos += scnprintf(buf + pos, buflen - pos, "%s@%llx:%llx\n",
512*4f1223e8SApple OSS Distributions uuid, tmpAddr, tmpAddr + tmpSize - 1);
513*4f1223e8SApple OSS Distributions });
514*4f1223e8SApple OSS Distributions }
515*4f1223e8SApple OSS Distributions
516*4f1223e8SApple OSS Distributions /********************************************************************/
517*4f1223e8SApple OSS Distributions void kmod_dump_log(vm_offset_t *addr, unsigned int cnt, boolean_t doUnslide);
518*4f1223e8SApple OSS Distributions
519*4f1223e8SApple OSS Distributions void
kmod_dump_log(vm_offset_t * addr,unsigned int cnt,boolean_t doUnslide)520*4f1223e8SApple OSS Distributions kmod_dump_log(
521*4f1223e8SApple OSS Distributions vm_offset_t * addr,
522*4f1223e8SApple OSS Distributions unsigned int cnt,
523*4f1223e8SApple OSS Distributions boolean_t doUnslide)
524*4f1223e8SApple OSS Distributions {
525*4f1223e8SApple OSS Distributions uint32_t flags = OSKext::kPrintKextsLock;
526*4f1223e8SApple OSS Distributions if (doUnslide) {
527*4f1223e8SApple OSS Distributions flags |= OSKext::kPrintKextsUnslide;
528*4f1223e8SApple OSS Distributions }
529*4f1223e8SApple OSS Distributions OSKext::printKextsInBacktrace(addr, cnt, &printf, flags);
530*4f1223e8SApple OSS Distributions }
531*4f1223e8SApple OSS Distributions
532*4f1223e8SApple OSS Distributions void *
OSKextKextForAddress(const void * addr)533*4f1223e8SApple OSS Distributions OSKextKextForAddress(const void *addr)
534*4f1223e8SApple OSS Distributions {
535*4f1223e8SApple OSS Distributions return OSKext::kextForAddress(addr);
536*4f1223e8SApple OSS Distributions }
537*4f1223e8SApple OSS Distributions
538*4f1223e8SApple OSS Distributions kern_return_t
OSKextGetLoadedKextSummaryForAddress(const void * addr,OSKextLoadedKextSummary * summary)539*4f1223e8SApple OSS Distributions OSKextGetLoadedKextSummaryForAddress(
540*4f1223e8SApple OSS Distributions const void * addr,
541*4f1223e8SApple OSS Distributions OSKextLoadedKextSummary * summary)
542*4f1223e8SApple OSS Distributions {
543*4f1223e8SApple OSS Distributions return OSKext::summaryForAddressExt(addr, summary);
544*4f1223e8SApple OSS Distributions }
545*4f1223e8SApple OSS Distributions
546*4f1223e8SApple OSS Distributions /*********************************************************************
547*4f1223e8SApple OSS Distributions * Compatibility implementation for kmod_get_info() host_priv routine.
548*4f1223e8SApple OSS Distributions * Only supported on old 32-bit architectures.
549*4f1223e8SApple OSS Distributions *********************************************************************/
550*4f1223e8SApple OSS Distributions
551*4f1223e8SApple OSS Distributions #if PRAGMA_MARK
552*4f1223e8SApple OSS Distributions #pragma mark Loaded Kext Summary
553*4f1223e8SApple OSS Distributions #endif
554*4f1223e8SApple OSS Distributions
555*4f1223e8SApple OSS Distributions void
OSKextLoadedKextSummariesUpdated(void)556*4f1223e8SApple OSS Distributions OSKextLoadedKextSummariesUpdated(void)
557*4f1223e8SApple OSS Distributions {
558*4f1223e8SApple OSS Distributions // Do nothing.
559*4f1223e8SApple OSS Distributions }
560*4f1223e8SApple OSS Distributions };
561