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