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