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