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