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