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