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