]> git.saurik.com Git - apple/xnu.git/blob - libkern/OSKextLib.cpp
xnu-6153.61.1.tar.gz
[apple/xnu.git] / libkern / OSKextLib.cpp
1 /*
2 * Copyright (c) 2008-2016 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 extern "C" {
30 #include <libkern/OSKextLibPrivate.h>
31 #include <libkern/mkext.h>
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" {
40 #if PRAGMA_MARK
41 #pragma mark C-based kext interface (loading/loaded kexts only)
42 #endif
43 /*********************************************************************
44 *********************************************************************/
45 kern_return_t
46 OSKextLoadKextWithIdentifier(const char * bundle_id)
47 {
48 return OSKext::loadKextWithIdentifier(bundle_id);
49 }
50
51 uint32_t OSKextGetLoadTagForIdentifier(const char * kextIdentifier);
52 /*********************************************************************
53 *********************************************************************/
54 uint32_t
55 OSKextGetLoadTagForIdentifier(const char * kextIdentifier)
56 {
57 uint32_t result = kOSKextInvalidLoadTag;
58 OSKext * theKext = NULL; // must release
59
60 if (!kextIdentifier) {
61 goto finish;
62 }
63
64 theKext = OSKext::lookupKextWithIdentifier(kextIdentifier);
65 if (theKext && theKext->isLoaded()) {
66 result = theKext->getLoadTag();
67 }
68 finish:
69 if (theKext) {
70 theKext->release();
71 }
72 return result;
73 }
74
75 /*********************************************************************
76 *********************************************************************/
77 OSReturn
78 OSKextRetainKextWithLoadTag(uint32_t loadTag)
79 {
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 }
108 finish:
109 return result;
110 }
111
112 /*********************************************************************
113 *********************************************************************/
114 OSReturn
115 OSKextReleaseKextWithLoadTag(uint32_t loadTag)
116 {
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()?
146 finish:
147 return result;
148 }
149
150 /*********************************************************************
151 * Not to be called by the kext being unloaded!
152 *********************************************************************/
153 OSReturn
154 OSKextUnloadKextWithLoadTag(uint32_t loadTag)
155 {
156 return OSKext::removeKextWithLoadTag(loadTag,
157 /* terminateServicesAndRemovePersonalitiesFlag */ false);
158 }
159
160
161 #if PRAGMA_MARK
162 #pragma mark Kext Requests
163 #endif
164 /*********************************************************************
165 * Kext Requests
166 *********************************************************************/
167 OSReturn
168 OSKextRequestResource(
169 const char * kextIdentifier,
170 const char * resourceName,
171 OSKextRequestResourceCallback callback,
172 void * context,
173 OSKextRequestTag * requestTagOut)
174 {
175 return OSKext::requestResource(kextIdentifier, resourceName,
176 callback, context, requestTagOut);
177 }
178
179 /*********************************************************************
180 *********************************************************************/
181 OSReturn
182 OSKextCancelRequest(
183 OSKextRequestTag requestTag,
184 void ** contextOut)
185 {
186 return OSKext::cancelRequest(requestTag, contextOut);
187 }
188
189 #if PRAGMA_MARK
190 #pragma mark MIG Functions & Wrappers
191 #endif
192 /*********************************************************************
193 * IMPORTANT: vm_map_copyout_size() consumes the requestIn copy
194 * object on success. Therefore once it has been invoked successfully,
195 * this routine *must* return KERN_SUCCESS, regardless of our actual
196 * result. Our contract with the caller is that requestIn must be
197 * caller-deallocated if we return an error. We use op_result to return
198 * the real result of our work.
199 *********************************************************************/
200 kern_return_t
201 kext_request(
202 host_priv_t hostPriv,
203 /* in only */ uint32_t clientLogSpec,
204 /* in only */ vm_offset_t requestIn,
205 /* in only */ mach_msg_type_number_t requestLengthIn,
206 /* out only */ vm_offset_t * responseOut,
207 /* out only */ mach_msg_type_number_t * responseLengthOut,
208 /* out only */ vm_offset_t * logDataOut,
209 /* out only */ mach_msg_type_number_t * logDataLengthOut,
210 /* out only */ kern_return_t * op_result)
211 {
212 kern_return_t result = KERN_FAILURE;
213 vm_map_address_t map_addr = 0; // do not free/deallocate
214 char * request = NULL;// must vm_deallocate
215
216 mkext2_header * mkextHeader = NULL;// do not release
217 bool isMkext = false;
218
219 char * response = NULL;// must kmem_free
220 uint32_t responseLength = 0;
221 char * logData = NULL;// must kmem_free
222 uint32_t logDataLength = 0;
223
224 /* MIG doesn't pass "out" parameters as empty, so clear them immediately
225 * just in case, or MIG will try to copy out bogus data.
226 */
227 *op_result = KERN_FAILURE;
228 *responseOut = 0;
229 *responseLengthOut = 0;
230 *logDataOut = 0;
231 *logDataLengthOut = 0;
232
233 /* Check for input. Don't discard what isn't there, though.
234 */
235 if (!requestLengthIn || !requestIn) {
236 OSKextLog(/* kext */ NULL,
237 kOSKextLogErrorLevel |
238 kOSKextLogIPCFlag,
239 "Invalid request from user space (no data).");
240 *op_result = KERN_INVALID_ARGUMENT;
241 goto finish;
242 }
243
244 result = vm_map_copyout_size(kernel_map, &map_addr, (vm_map_copy_t)requestIn, requestLengthIn);
245 if (result != KERN_SUCCESS) {
246 OSKextLog(/* kext */ NULL,
247 kOSKextLogErrorLevel |
248 kOSKextLogIPCFlag,
249 "vm_map_copyout() failed for request from user space.");
250 /*
251 * If we return an error it is our caller's responsibility to
252 * deallocate the requestIn copy object, so do not deallocate it
253 * here. See comment above.
254 */
255 goto finish;
256 }
257 request = CAST_DOWN(char *, map_addr);
258
259 /* Check if request is an mkext; this is always a load request
260 * and requires root access. If it isn't an mkext, see if it's
261 * an XML request, and check the request to see if that requires
262 * root access.
263 */
264 if (requestLengthIn > sizeof(mkext2_header)) {
265 mkextHeader = (mkext2_header *)request;
266 if (MKEXT_GET_MAGIC(mkextHeader) == MKEXT_MAGIC &&
267 MKEXT_GET_SIGNATURE(mkextHeader) == MKEXT_SIGN) {
268 isMkext = true;
269 }
270 }
271
272 if (isMkext) {
273 #ifdef SECURE_KERNEL
274 // xxx - something tells me if we have a secure kernel we don't even
275 // xxx - want to log a message here. :-)
276 *op_result = KERN_NOT_SUPPORTED;
277 goto finish;
278 #else
279 // xxx - can we find out if calling task is kextd?
280 // xxx - can we find the name of the calling task?
281 if (hostPriv == HOST_PRIV_NULL) {
282 OSKextLog(/* kext */ NULL,
283 kOSKextLogErrorLevel |
284 kOSKextLogLoadFlag | kOSKextLogIPCFlag,
285 "Attempt by non-root process to load a kext.");
286 *op_result = kOSKextReturnNotPrivileged;
287 goto finish;
288 }
289
290 *op_result = OSKext::loadFromMkext((OSKextLogSpec)clientLogSpec,
291 request, requestLengthIn,
292 &logData, &logDataLength);
293
294 #endif /* defined(SECURE_KERNEL) */
295 } else {
296 /* If the request isn't an mkext, then is should be XML. Parse it
297 * if possible and hand the request over to OSKext.
298 */
299 *op_result = OSKext::handleRequest(hostPriv,
300 (OSKextLogSpec)clientLogSpec,
301 request, requestLengthIn,
302 &response, &responseLength,
303 &logData, &logDataLength);
304 }
305
306 if (response && responseLength > 0) {
307 kern_return_t copyin_result;
308
309 copyin_result = vm_map_copyin(kernel_map,
310 CAST_USER_ADDR_T(response), responseLength,
311 /* src_destroy */ false, (vm_map_copy_t *)responseOut);
312 if (copyin_result == KERN_SUCCESS) {
313 *responseLengthOut = responseLength;
314 } else {
315 OSKextLog(/* kext */ NULL,
316 kOSKextLogErrorLevel |
317 kOSKextLogIPCFlag,
318 "Failed to copy response to request from user space.");
319 *op_result = copyin_result; // xxx - should we map to our own code?
320 *responseOut = 0;
321 *responseLengthOut = 0;
322 goto finish;
323 }
324 }
325
326 if (logData && logDataLength > 0) {
327 kern_return_t copyin_result;
328
329 copyin_result = vm_map_copyin(kernel_map,
330 CAST_USER_ADDR_T(logData), logDataLength,
331 /* src_destroy */ false, (vm_map_copy_t *)logDataOut);
332 if (copyin_result == KERN_SUCCESS) {
333 *logDataLengthOut = logDataLength;
334 } else {
335 OSKextLog(/* kext */ NULL,
336 kOSKextLogErrorLevel |
337 kOSKextLogIPCFlag,
338 "Failed to copy log data for request from user space.");
339 *op_result = copyin_result; // xxx - should we map to our own code?
340 *logDataOut = 0;
341 *logDataLengthOut = 0;
342 goto finish;
343 }
344 }
345
346 finish:
347 if (request) {
348 (void)vm_deallocate(kernel_map, (vm_offset_t)request, requestLengthIn);
349 }
350 if (response) {
351 /* 11981737 - clear uninitialized data in last page */
352 kmem_free(kernel_map, (vm_offset_t)response, round_page(responseLength));
353 }
354 if (logData) {
355 /* 11981737 - clear uninitialized data in last page */
356 kmem_free(kernel_map, (vm_offset_t)logData, round_page(logDataLength));
357 }
358
359 return result;
360 }
361
362 /*********************************************************************
363 * Gets the vm_map for the current kext
364 *********************************************************************/
365 extern vm_offset_t segPRELINKTEXTB;
366 extern unsigned long segSizePRELINKTEXT;
367 extern int kth_started;
368 extern vm_map_t g_kext_map;
369
370 vm_map_t
371 kext_get_vm_map(kmod_info_t *info)
372 {
373 vm_map_t kext_map = NULL;
374
375 /* Set the vm map */
376 if ((info->address >= segPRELINKTEXTB) &&
377 (info->address < (segPRELINKTEXTB + segSizePRELINKTEXT))) {
378 kext_map = kernel_map;
379 } else {
380 kext_map = g_kext_map;
381 }
382
383 return kext_map;
384 }
385
386
387 #if PRAGMA_MARK
388 /********************************************************************/
389 #pragma mark Weak linking support
390 /********************************************************************/
391 #endif
392 void
393 kext_weak_symbol_referenced(void)
394 {
395 panic("A kext referenced an unresolved weak symbol\n");
396 }
397
398 const void * const gOSKextUnresolved = (const void *)&kext_weak_symbol_referenced;
399
400 #if PRAGMA_MARK
401 #pragma mark Kernel-Internal C Functions
402 #endif
403 /*********************************************************************
404 * Called from startup.c.
405 *********************************************************************/
406 void
407 OSKextRemoveKextBootstrap(void)
408 {
409 OSKext::removeKextBootstrap();
410 return;
411 }
412
413 #if CONFIG_DTRACE
414 /*********************************************************************
415 *********************************************************************/
416 void
417 OSKextRegisterKextsWithDTrace(void)
418 {
419 OSKext::registerKextsWithDTrace();
420 return;
421 }
422 #endif /* CONFIG_DTRACE */
423
424 /*********************************************************************
425 *********************************************************************/
426 void
427 kext_dump_panic_lists(int (*printf_func)(const char * fmt, ...))
428 {
429 OSKext::printKextPanicLists(printf_func);
430 return;
431 }
432
433 #if PRAGMA_MARK
434 #pragma mark Kmod Compatibility Functions
435 #endif
436 /*********************************************************************
437 **********************************************************************
438 * KMOD COMPATIBILITY FUNCTIONS *
439 * (formerly in kmod.c, or C++ bridges from) *
440 **********************************************************************
441 **********************************************************************
442 * These two functions are used in various places in the kernel, but
443 * are not exported. We might rename them at some point to start with
444 * kext_ or OSKext.
445 *
446 * kmod_panic_dump() must not be called outside of a panic context.
447 * kmod_dump_log() must not be called in a panic context.
448 *********************************************************************/
449 void
450 kmod_panic_dump(vm_offset_t * addr, unsigned int cnt)
451 {
452 extern int paniclog_append_noflush(const char *format, ...) __printflike(1, 2);
453
454 OSKext::printKextsInBacktrace(addr, cnt, &paniclog_append_noflush, 0);
455
456 return;
457 }
458
459 /********************************************************************/
460 void kmod_dump_log(vm_offset_t *addr, unsigned int cnt, boolean_t doUnslide);
461
462 void
463 kmod_dump_log(
464 vm_offset_t * addr,
465 unsigned int cnt,
466 boolean_t doUnslide)
467 {
468 uint32_t flags = OSKext::kPrintKextsLock;
469 if (doUnslide) {
470 flags |= OSKext::kPrintKextsUnslide;
471 }
472 OSKext::printKextsInBacktrace(addr, cnt, &printf, flags);
473 }
474
475 void *
476 OSKextKextForAddress(const void *addr)
477 {
478 return OSKext::kextForAddress(addr);
479 }
480
481
482 /*********************************************************************
483 * Compatibility implementation for kmod_get_info() host_priv routine.
484 * Only supported on old 32-bit architectures.
485 *********************************************************************/
486
487 #if PRAGMA_MARK
488 #pragma mark Loaded Kext Summary
489 #endif
490
491 void
492 OSKextLoadedKextSummariesUpdated(void)
493 {
494 // Do nothing.
495 }
496 };