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