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