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