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