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