2 * Copyright (c) 2008-2016 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 #include <libkern/OSKextLibPrivate.h>
31 #include <libkern/mkext.h>
34 #include <libkern/c++/OSContainers.h>
35 #include <libkern/c++/OSKext.h>
36 #include <libkern/OSKextLib.h>
37 #include <libkern/OSKextLibPrivate.h>
41 #pragma mark C-based kext interface (loading/loaded kexts only)
43 /*********************************************************************
44 *********************************************************************/
46 OSKextLoadKextWithIdentifier(const char * bundle_id
)
48 return OSKext::loadKextWithIdentifier(bundle_id
);
51 uint32_t OSKextGetLoadTagForIdentifier(const char * kextIdentifier
);
52 /*********************************************************************
53 *********************************************************************/
55 OSKextGetLoadTagForIdentifier(const char * kextIdentifier
)
57 uint32_t result
= kOSKextInvalidLoadTag
;
58 OSKext
* theKext
= NULL
; // must release
60 if (!kextIdentifier
) {
64 theKext
= OSKext::lookupKextWithIdentifier(kextIdentifier
);
65 if (theKext
&& theKext
->isLoaded()) {
66 result
= theKext
->getLoadTag();
75 /*********************************************************************
76 *********************************************************************/
78 // FIXME: Implementation of this function is hidden from the static analyzer.
79 // The analyzer is worried about the lack of release and suggests
80 // refactoring the code into the typical non-owning container pattern.
81 // Feel free to remove the #ifndef and address the warning!
82 #ifndef __clang_analyzer__
84 OSKextRetainKextWithLoadTag(uint32_t loadTag
)
86 OSReturn result
= kOSKextReturnNotFound
;
87 OSKext
* theKext
= NULL
;// do not release; as this function is a retain
89 if (loadTag
== kOSKextInvalidLoadTag
) {
90 result
= kOSKextReturnInvalidArgument
;
93 theKext
= OSKext::lookupKextWithLoadTag(loadTag
);
95 result
= kOSReturnSuccess
;
98 kOSKextLogDebugLevel
|
99 kOSKextLogKextBookkeepingFlag
,
100 "Kext %s (load tag %d) has been retained.",
101 theKext
->getIdentifierCString(),
104 /* Call this after so a log message about autounload comes second.
106 theKext
->setAutounloadEnabled(true);
109 kOSKextLogErrorLevel
|
110 kOSKextLogKextBookkeepingFlag
,
111 "Can't retain kext with load tag %d - no such kext is loaded.",
117 #endif // __clang_analyzer__
119 /*********************************************************************
120 *********************************************************************/
122 // FIXME: Implementation of this function is hidden from the static analyzer.
123 // The analyzer is worried about the double release and suggests
124 // refactoring the code into the typical non-owning container pattern.
125 // Feel free to remove the #ifndef and address the warning!
126 #ifndef __clang_analyzer__
128 OSKextReleaseKextWithLoadTag(uint32_t loadTag
)
130 OSReturn result
= kOSKextReturnNotFound
;
131 OSKext
* theKext
= NULL
; // must release twice!
133 if (loadTag
== kOSKextInvalidLoadTag
) {
134 result
= kOSKextReturnInvalidArgument
;
137 theKext
= OSKext::lookupKextWithLoadTag(loadTag
);
139 result
= kOSReturnSuccess
;
140 OSKext::considerUnloads(); // schedule autounload pass
141 theKext
->release(); // do the release the caller wants
142 theKext
->release(); // now do the release on the lookup
144 kOSKextLogDebugLevel
|
145 kOSKextLogKextBookkeepingFlag
,
146 "Kext %s (load tag %d) has been released.",
147 theKext
->getIdentifierCString(),
151 kOSKextLogErrorLevel
|
152 kOSKextLogKextBookkeepingFlag
,
153 "Can't release kext with load tag %d - no such kext is loaded.",
157 // xxx - should I check that the refcount of the OSKext is above the lower bound?
158 // xxx - do we want a OSKextGetRetainCountOfKextWithLoadTag()?
162 #endif // __clang_analyzer__
164 /*********************************************************************
165 * Not to be called by the kext being unloaded!
166 *********************************************************************/
168 OSKextUnloadKextWithLoadTag(uint32_t loadTag
)
170 return OSKext::removeKextWithLoadTag(loadTag
,
171 /* terminateServicesAndRemovePersonalitiesFlag */ false);
176 #pragma mark Kext Requests
178 /*********************************************************************
180 *********************************************************************/
182 OSKextRequestResource(
183 const char * kextIdentifier
,
184 const char * resourceName
,
185 OSKextRequestResourceCallback callback
,
187 OSKextRequestTag
* requestTagOut
)
189 return OSKext::requestResource(kextIdentifier
, resourceName
,
190 callback
, context
, requestTagOut
);
193 /*********************************************************************
194 *********************************************************************/
197 OSKextRequestTag requestTag
,
200 return OSKext::cancelRequest(requestTag
, contextOut
);
204 #pragma mark MIG Functions & Wrappers
206 /*********************************************************************
207 * IMPORTANT: vm_map_copyout_size() consumes the requestIn copy
208 * object on success. Therefore once it has been invoked successfully,
209 * this routine *must* return KERN_SUCCESS, regardless of our actual
210 * result. Our contract with the caller is that requestIn must be
211 * caller-deallocated if we return an error. We use op_result to return
212 * the real result of our work.
213 *********************************************************************/
216 host_priv_t hostPriv
,
217 /* in only */ uint32_t clientLogSpec
,
218 /* in only */ vm_offset_t requestIn
,
219 /* in only */ mach_msg_type_number_t requestLengthIn
,
220 /* out only */ vm_offset_t
* responseOut
,
221 /* out only */ mach_msg_type_number_t
* responseLengthOut
,
222 /* out only */ vm_offset_t
* logDataOut
,
223 /* out only */ mach_msg_type_number_t
* logDataLengthOut
,
224 /* out only */ kern_return_t
* op_result
)
226 kern_return_t result
= KERN_FAILURE
;
227 vm_map_address_t map_addr
= 0; // do not free/deallocate
228 char * request
= NULL
;// must vm_deallocate
230 mkext2_header
* mkextHeader
= NULL
;// do not release
231 bool isMkext
= false;
233 char * response
= NULL
;// must kmem_free
234 uint32_t responseLength
= 0;
235 char * logData
= NULL
;// must kmem_free
236 uint32_t logDataLength
= 0;
238 /* MIG doesn't pass "out" parameters as empty, so clear them immediately
239 * just in case, or MIG will try to copy out bogus data.
241 *op_result
= KERN_FAILURE
;
243 *responseLengthOut
= 0;
245 *logDataLengthOut
= 0;
247 /* Check for input. Don't discard what isn't there, though.
249 if (!requestLengthIn
|| !requestIn
) {
250 OSKextLog(/* kext */ NULL
,
251 kOSKextLogErrorLevel
|
253 "Invalid request from user space (no data).");
254 *op_result
= KERN_INVALID_ARGUMENT
;
258 result
= vm_map_copyout_size(kernel_map
, &map_addr
, (vm_map_copy_t
)requestIn
, requestLengthIn
);
259 if (result
!= KERN_SUCCESS
) {
260 OSKextLog(/* kext */ NULL
,
261 kOSKextLogErrorLevel
|
263 "vm_map_copyout() failed for request from user space.");
265 * If we return an error it is our caller's responsibility to
266 * deallocate the requestIn copy object, so do not deallocate it
267 * here. See comment above.
271 request
= CAST_DOWN(char *, map_addr
);
273 /* Check if request is an mkext; this is always a load request
274 * and requires root access. If it isn't an mkext, see if it's
275 * an XML request, and check the request to see if that requires
278 if (requestLengthIn
> sizeof(mkext2_header
)) {
279 mkextHeader
= (mkext2_header
*)request
;
280 if (MKEXT_GET_MAGIC(mkextHeader
) == MKEXT_MAGIC
&&
281 MKEXT_GET_SIGNATURE(mkextHeader
) == MKEXT_SIGN
) {
287 #if defined(SECURE_KERNEL) || !CONFIG_KXLD
288 // xxx - something tells me if we have a secure kernel we don't even
289 // xxx - want to log a message here. :-)
290 *op_result
= KERN_NOT_SUPPORTED
;
293 // xxx - can we find out if calling task is kextd?
294 // xxx - can we find the name of the calling task?
295 if (hostPriv
== HOST_PRIV_NULL
) {
296 OSKextLog(/* kext */ NULL
,
297 kOSKextLogErrorLevel
|
298 kOSKextLogLoadFlag
| kOSKextLogIPCFlag
,
299 "Attempt by non-root process to load a kext.");
300 *op_result
= kOSKextReturnNotPrivileged
;
304 *op_result
= OSKext::loadFromMkext((OSKextLogSpec
)clientLogSpec
,
305 request
, requestLengthIn
,
306 &logData
, &logDataLength
);
308 #endif /* defined(SECURE_KERNEL) */
310 /* If the request isn't an mkext, then is should be XML. Parse it
311 * if possible and hand the request over to OSKext.
313 *op_result
= OSKext::handleRequest(hostPriv
,
314 (OSKextLogSpec
)clientLogSpec
,
315 request
, requestLengthIn
,
316 &response
, &responseLength
,
317 &logData
, &logDataLength
);
320 if (response
&& responseLength
> 0) {
321 kern_return_t copyin_result
;
323 copyin_result
= vm_map_copyin(kernel_map
,
324 CAST_USER_ADDR_T(response
), responseLength
,
325 /* src_destroy */ false, (vm_map_copy_t
*)responseOut
);
326 if (copyin_result
== KERN_SUCCESS
) {
327 *responseLengthOut
= responseLength
;
329 OSKextLog(/* kext */ NULL
,
330 kOSKextLogErrorLevel
|
332 "Failed to copy response to request from user space.");
333 *op_result
= copyin_result
; // xxx - should we map to our own code?
335 *responseLengthOut
= 0;
340 if (logData
&& logDataLength
> 0) {
341 kern_return_t copyin_result
;
343 copyin_result
= vm_map_copyin(kernel_map
,
344 CAST_USER_ADDR_T(logData
), logDataLength
,
345 /* src_destroy */ false, (vm_map_copy_t
*)logDataOut
);
346 if (copyin_result
== KERN_SUCCESS
) {
347 *logDataLengthOut
= logDataLength
;
349 OSKextLog(/* kext */ NULL
,
350 kOSKextLogErrorLevel
|
352 "Failed to copy log data for request from user space.");
353 *op_result
= copyin_result
; // xxx - should we map to our own code?
355 *logDataLengthOut
= 0;
362 (void)vm_deallocate(kernel_map
, (vm_offset_t
)request
, requestLengthIn
);
365 /* 11981737 - clear uninitialized data in last page */
366 kmem_free(kernel_map
, (vm_offset_t
)response
, round_page(responseLength
));
369 /* 11981737 - clear uninitialized data in last page */
370 kmem_free(kernel_map
, (vm_offset_t
)logData
, round_page(logDataLength
));
376 /*********************************************************************
377 * Gets the vm_map for the current kext
378 *********************************************************************/
379 extern vm_offset_t segPRELINKTEXTB
;
380 extern vm_offset_t segLINKB
;
381 extern unsigned long segSizePRELINKTEXT
;
382 extern vm_map_t g_kext_map
;
385 kext_get_vm_map(kmod_info_t
*info
)
387 vm_map_t kext_map
= NULL
;
388 kc_format_t kcformat
;
390 if (PE_get_primary_kc_format(&kcformat
) && kcformat
== KCFormatFileset
) {
391 /* Check if the kext is from the boot KC */
392 assert(segLINKB
>= (segPRELINKTEXTB
+ segSizePRELINKTEXT
));
393 if ((info
->address
>= segPRELINKTEXTB
) &&
394 (info
->address
< segLINKB
)) {
395 kext_map
= kernel_map
;
397 kext_map
= g_kext_map
;
400 if ((info
->address
>= segPRELINKTEXTB
) &&
401 (info
->address
< (segPRELINKTEXTB
+ segSizePRELINKTEXT
))) {
402 kext_map
= kernel_map
;
404 kext_map
= g_kext_map
;
413 /********************************************************************/
414 #pragma mark Weak linking support
415 /********************************************************************/
418 kext_weak_symbol_referenced(void)
420 panic("A kext referenced an unresolved weak symbol\n");
423 const void * const gOSKextUnresolved
= (const void *)&kext_weak_symbol_referenced
;
426 #pragma mark Kernel-Internal C Functions
428 /*********************************************************************
429 * Called from startup.c.
430 *********************************************************************/
432 OSKextRemoveKextBootstrap(void)
434 OSKext::removeKextBootstrap();
439 /*********************************************************************
440 *********************************************************************/
442 OSKextRegisterKextsWithDTrace(void)
444 OSKext::registerKextsWithDTrace();
447 #endif /* CONFIG_DTRACE */
449 /*********************************************************************
450 *********************************************************************/
452 kext_dump_panic_lists(int (*printf_func
)(const char * fmt
, ...))
454 OSKext::printKextPanicLists(printf_func
);
459 #pragma mark Kmod Compatibility Functions
461 /*********************************************************************
462 **********************************************************************
463 * KMOD COMPATIBILITY FUNCTIONS *
464 * (formerly in kmod.c, or C++ bridges from) *
465 **********************************************************************
466 **********************************************************************
467 * These two functions are used in various places in the kernel, but
468 * are not exported. We might rename them at some point to start with
471 * kmod_panic_dump() must not be called outside of a panic context.
472 * kmod_dump_log() must not be called in a panic context.
473 *********************************************************************/
475 kmod_panic_dump(vm_offset_t
* addr
, unsigned int cnt
)
477 extern int paniclog_append_noflush(const char *format
, ...) __printflike(1, 2);
479 OSKext::printKextsInBacktrace(addr
, cnt
, &paniclog_append_noflush
, 0);
484 /********************************************************************/
485 void kmod_dump_log(vm_offset_t
*addr
, unsigned int cnt
, boolean_t doUnslide
);
493 uint32_t flags
= OSKext::kPrintKextsLock
;
495 flags
|= OSKext::kPrintKextsUnslide
;
497 OSKext::printKextsInBacktrace(addr
, cnt
, &printf
, flags
);
501 OSKextKextForAddress(const void *addr
)
503 return OSKext::kextForAddress(addr
);
507 /*********************************************************************
508 * Compatibility implementation for kmod_get_info() host_priv routine.
509 * Only supported on old 32-bit architectures.
510 *********************************************************************/
513 #pragma mark Loaded Kext Summary
517 OSKextLoadedKextSummariesUpdated(void)