13ca3bd55SApple OSS Distributions /*
288cc0b97SApple OSS Distributions * Copyright (c) 2008-2016 Apple Inc. All rights reserved.
33ca3bd55SApple OSS Distributions *
43ca3bd55SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
53ca3bd55SApple OSS Distributions *
63ca3bd55SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
73ca3bd55SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
83ca3bd55SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
93ca3bd55SApple OSS Distributions * compliance with the License. The rights granted to you under the License
103ca3bd55SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
113ca3bd55SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
123ca3bd55SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
133ca3bd55SApple OSS Distributions * terms of an Apple operating system software license agreement.
143ca3bd55SApple OSS Distributions *
153ca3bd55SApple OSS Distributions * Please obtain a copy of the License at
163ca3bd55SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
173ca3bd55SApple OSS Distributions *
183ca3bd55SApple OSS Distributions * The Original Code and all software distributed under the License are
193ca3bd55SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
203ca3bd55SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
213ca3bd55SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
223ca3bd55SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
233ca3bd55SApple OSS Distributions * Please see the License for the specific language governing rights and
243ca3bd55SApple OSS Distributions * limitations under the License.
253ca3bd55SApple OSS Distributions *
263ca3bd55SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
273ca3bd55SApple OSS Distributions */
283ca3bd55SApple OSS Distributions
293ca3bd55SApple OSS Distributions extern "C" {
303ca3bd55SApple OSS Distributions #include <libkern/OSKextLibPrivate.h>
313ca3bd55SApple OSS Distributions #include <libkern/mkext.h>
323ca3bd55SApple OSS Distributions };
333ca3bd55SApple OSS Distributions
3494d3b452SApple OSS Distributions #include <kern/telemetry.h>
353ca3bd55SApple OSS Distributions #include <libkern/c++/OSContainers.h>
363ca3bd55SApple OSS Distributions #include <libkern/c++/OSKext.h>
373ca3bd55SApple OSS Distributions #include <libkern/OSKextLib.h>
383ca3bd55SApple OSS Distributions #include <libkern/OSKextLibPrivate.h>
39*8d741a5dSApple OSS Distributions #include <vm/vm_kern_xnu.h>
40*8d741a5dSApple OSS Distributions #include <vm/vm_map_xnu.h>
413ca3bd55SApple OSS Distributions
423ca3bd55SApple OSS Distributions extern "C" {
433ca3bd55SApple OSS Distributions #if PRAGMA_MARK
443ca3bd55SApple OSS Distributions #pragma mark C-based kext interface (loading/loaded kexts only)
453ca3bd55SApple OSS Distributions #endif
463ca3bd55SApple OSS Distributions /*********************************************************************
473ca3bd55SApple OSS Distributions *********************************************************************/
48a5e72196SApple OSS Distributions kern_return_t
OSKextLoadKextWithIdentifier(const char * bundle_id)49a5e72196SApple OSS Distributions OSKextLoadKextWithIdentifier(const char * bundle_id)
503ca3bd55SApple OSS Distributions {
513ca3bd55SApple OSS Distributions return OSKext::loadKextWithIdentifier(bundle_id);
523ca3bd55SApple OSS Distributions }
533ca3bd55SApple OSS Distributions
5488cc0b97SApple OSS Distributions uint32_t OSKextGetLoadTagForIdentifier(const char * kextIdentifier);
553ca3bd55SApple OSS Distributions /*********************************************************************
563ca3bd55SApple OSS Distributions *********************************************************************/
573ca3bd55SApple OSS Distributions uint32_t
OSKextGetLoadTagForIdentifier(const char * kextIdentifier)583ca3bd55SApple OSS Distributions OSKextGetLoadTagForIdentifier(const char * kextIdentifier)
593ca3bd55SApple OSS Distributions {
603ca3bd55SApple OSS Distributions uint32_t result = kOSKextInvalidLoadTag;
613ca3bd55SApple OSS Distributions OSKext * theKext = NULL; // must release
623ca3bd55SApple OSS Distributions
633ca3bd55SApple OSS Distributions if (!kextIdentifier) {
643ca3bd55SApple OSS Distributions goto finish;
653ca3bd55SApple OSS Distributions }
663ca3bd55SApple OSS Distributions
673ca3bd55SApple OSS Distributions theKext = OSKext::lookupKextWithIdentifier(kextIdentifier);
683ca3bd55SApple OSS Distributions if (theKext && theKext->isLoaded()) {
693ca3bd55SApple OSS Distributions result = theKext->getLoadTag();
703ca3bd55SApple OSS Distributions }
713ca3bd55SApple OSS Distributions finish:
72a5e72196SApple OSS Distributions if (theKext) {
73a5e72196SApple OSS Distributions theKext->release();
74a5e72196SApple OSS Distributions }
753ca3bd55SApple OSS Distributions return result;
763ca3bd55SApple OSS Distributions }
773ca3bd55SApple OSS Distributions
783ca3bd55SApple OSS Distributions /*********************************************************************
793ca3bd55SApple OSS Distributions *********************************************************************/
80bb611c8fSApple OSS Distributions
81bb611c8fSApple OSS Distributions // FIXME: Implementation of this function is hidden from the static analyzer.
82bb611c8fSApple OSS Distributions // The analyzer is worried about the lack of release and suggests
83bb611c8fSApple OSS Distributions // refactoring the code into the typical non-owning container pattern.
84bb611c8fSApple OSS Distributions // Feel free to remove the #ifndef and address the warning!
85bb611c8fSApple OSS Distributions #ifndef __clang_analyzer__
86a5e72196SApple OSS Distributions OSReturn
OSKextRetainKextWithLoadTag(uint32_t loadTag)87a5e72196SApple OSS Distributions OSKextRetainKextWithLoadTag(uint32_t loadTag)
883ca3bd55SApple OSS Distributions {
893ca3bd55SApple OSS Distributions OSReturn result = kOSKextReturnNotFound;
903ca3bd55SApple OSS Distributions OSKext * theKext = NULL;// do not release; as this function is a retain
913ca3bd55SApple OSS Distributions
923ca3bd55SApple OSS Distributions if (loadTag == kOSKextInvalidLoadTag) {
933ca3bd55SApple OSS Distributions result = kOSKextReturnInvalidArgument;
943ca3bd55SApple OSS Distributions goto finish;
953ca3bd55SApple OSS Distributions }
963ca3bd55SApple OSS Distributions theKext = OSKext::lookupKextWithLoadTag(loadTag);
973ca3bd55SApple OSS Distributions if (theKext) {
983ca3bd55SApple OSS Distributions result = kOSReturnSuccess;
993ca3bd55SApple OSS Distributions
1003ca3bd55SApple OSS Distributions OSKextLog(theKext,
1013ca3bd55SApple OSS Distributions kOSKextLogDebugLevel |
1023ca3bd55SApple OSS Distributions kOSKextLogKextBookkeepingFlag,
1033ca3bd55SApple OSS Distributions "Kext %s (load tag %d) has been retained.",
1043ca3bd55SApple OSS Distributions theKext->getIdentifierCString(),
1053ca3bd55SApple OSS Distributions loadTag);
1063ca3bd55SApple OSS Distributions
1073ca3bd55SApple OSS Distributions /* Call this after so a log message about autounload comes second.
1083ca3bd55SApple OSS Distributions */
1093ca3bd55SApple OSS Distributions theKext->setAutounloadEnabled(true);
1103ca3bd55SApple OSS Distributions } else {
1113ca3bd55SApple OSS Distributions OSKextLog(theKext,
1123ca3bd55SApple OSS Distributions kOSKextLogErrorLevel |
1133ca3bd55SApple OSS Distributions kOSKextLogKextBookkeepingFlag,
1143ca3bd55SApple OSS Distributions "Can't retain kext with load tag %d - no such kext is loaded.",
1153ca3bd55SApple OSS Distributions loadTag);
1163ca3bd55SApple OSS Distributions }
1173ca3bd55SApple OSS Distributions finish:
1183ca3bd55SApple OSS Distributions return result;
1193ca3bd55SApple OSS Distributions }
120bb611c8fSApple OSS Distributions #endif // __clang_analyzer__
1213ca3bd55SApple OSS Distributions
1223ca3bd55SApple OSS Distributions /*********************************************************************
1233ca3bd55SApple OSS Distributions *********************************************************************/
124bb611c8fSApple OSS Distributions
125bb611c8fSApple OSS Distributions // FIXME: Implementation of this function is hidden from the static analyzer.
126bb611c8fSApple OSS Distributions // The analyzer is worried about the double release and suggests
127bb611c8fSApple OSS Distributions // refactoring the code into the typical non-owning container pattern.
128bb611c8fSApple OSS Distributions // Feel free to remove the #ifndef and address the warning!
129bb611c8fSApple OSS Distributions #ifndef __clang_analyzer__
130a5e72196SApple OSS Distributions OSReturn
OSKextReleaseKextWithLoadTag(uint32_t loadTag)131a5e72196SApple OSS Distributions OSKextReleaseKextWithLoadTag(uint32_t loadTag)
1323ca3bd55SApple OSS Distributions {
1333ca3bd55SApple OSS Distributions OSReturn result = kOSKextReturnNotFound;
1343ca3bd55SApple OSS Distributions OSKext * theKext = NULL; // must release twice!
1353ca3bd55SApple OSS Distributions
1363ca3bd55SApple OSS Distributions if (loadTag == kOSKextInvalidLoadTag) {
1373ca3bd55SApple OSS Distributions result = kOSKextReturnInvalidArgument;
1383ca3bd55SApple OSS Distributions goto finish;
1393ca3bd55SApple OSS Distributions }
1403ca3bd55SApple OSS Distributions theKext = OSKext::lookupKextWithLoadTag(loadTag);
1413ca3bd55SApple OSS Distributions if (theKext) {
1423ca3bd55SApple OSS Distributions result = kOSReturnSuccess;
1433ca3bd55SApple OSS Distributions OSKext::considerUnloads(); // schedule autounload pass
1443ca3bd55SApple OSS Distributions theKext->release(); // do the release the caller wants
1453ca3bd55SApple OSS Distributions theKext->release(); // now do the release on the lookup
1463ca3bd55SApple OSS Distributions OSKextLog(theKext,
1473ca3bd55SApple OSS Distributions kOSKextLogDebugLevel |
1483ca3bd55SApple OSS Distributions kOSKextLogKextBookkeepingFlag,
1493ca3bd55SApple OSS Distributions "Kext %s (load tag %d) has been released.",
1503ca3bd55SApple OSS Distributions theKext->getIdentifierCString(),
1513ca3bd55SApple OSS Distributions loadTag);
1523ca3bd55SApple OSS Distributions } else {
1533ca3bd55SApple OSS Distributions OSKextLog(theKext,
1543ca3bd55SApple OSS Distributions kOSKextLogErrorLevel |
1553ca3bd55SApple OSS Distributions kOSKextLogKextBookkeepingFlag,
1563ca3bd55SApple OSS Distributions "Can't release kext with load tag %d - no such kext is loaded.",
1573ca3bd55SApple OSS Distributions loadTag);
1583ca3bd55SApple OSS Distributions }
1593ca3bd55SApple OSS Distributions
1603ca3bd55SApple OSS Distributions // xxx - should I check that the refcount of the OSKext is above the lower bound?
1613ca3bd55SApple OSS Distributions // xxx - do we want a OSKextGetRetainCountOfKextWithLoadTag()?
1623ca3bd55SApple OSS Distributions finish:
1633ca3bd55SApple OSS Distributions return result;
1643ca3bd55SApple OSS Distributions }
165bb611c8fSApple OSS Distributions #endif // __clang_analyzer__
1663ca3bd55SApple OSS Distributions
1673ca3bd55SApple OSS Distributions /*********************************************************************
1683ca3bd55SApple OSS Distributions * Not to be called by the kext being unloaded!
1693ca3bd55SApple OSS Distributions *********************************************************************/
170a5e72196SApple OSS Distributions OSReturn
OSKextUnloadKextWithLoadTag(uint32_t loadTag)171a5e72196SApple OSS Distributions OSKextUnloadKextWithLoadTag(uint32_t loadTag)
1723ca3bd55SApple OSS Distributions {
1733ca3bd55SApple OSS Distributions return OSKext::removeKextWithLoadTag(loadTag,
1743ca3bd55SApple OSS Distributions /* terminateServicesAndRemovePersonalitiesFlag */ false);
1753ca3bd55SApple OSS Distributions }
1763ca3bd55SApple OSS Distributions
1773ca3bd55SApple OSS Distributions
1783ca3bd55SApple OSS Distributions #if PRAGMA_MARK
1793ca3bd55SApple OSS Distributions #pragma mark Kext Requests
1803ca3bd55SApple OSS Distributions #endif
1813ca3bd55SApple OSS Distributions /*********************************************************************
1823ca3bd55SApple OSS Distributions * Kext Requests
1833ca3bd55SApple OSS Distributions *********************************************************************/
184a5e72196SApple OSS Distributions OSReturn
OSKextRequestResource(const char * kextIdentifier,const char * resourceName,OSKextRequestResourceCallback callback,void * context,OSKextRequestTag * requestTagOut)185a5e72196SApple OSS Distributions OSKextRequestResource(
1863ca3bd55SApple OSS Distributions const char * kextIdentifier,
1873ca3bd55SApple OSS Distributions const char * resourceName,
1883ca3bd55SApple OSS Distributions OSKextRequestResourceCallback callback,
1893ca3bd55SApple OSS Distributions void * context,
1903ca3bd55SApple OSS Distributions OSKextRequestTag * requestTagOut)
1913ca3bd55SApple OSS Distributions {
1923ca3bd55SApple OSS Distributions return OSKext::requestResource(kextIdentifier, resourceName,
1933ca3bd55SApple OSS Distributions callback, context, requestTagOut);
1943ca3bd55SApple OSS Distributions }
1953ca3bd55SApple OSS Distributions
1963ca3bd55SApple OSS Distributions /*********************************************************************
1973ca3bd55SApple OSS Distributions *********************************************************************/
198a5e72196SApple OSS Distributions OSReturn
OSKextCancelRequest(OSKextRequestTag requestTag,void ** contextOut)199a5e72196SApple OSS Distributions OSKextCancelRequest(
2003ca3bd55SApple OSS Distributions OSKextRequestTag requestTag,
2013ca3bd55SApple OSS Distributions void ** contextOut)
2023ca3bd55SApple OSS Distributions {
2033ca3bd55SApple OSS Distributions return OSKext::cancelRequest(requestTag, contextOut);
2043ca3bd55SApple OSS Distributions }
2053ca3bd55SApple OSS Distributions
2063ca3bd55SApple OSS Distributions #if PRAGMA_MARK
2073ca3bd55SApple OSS Distributions #pragma mark MIG Functions & Wrappers
2083ca3bd55SApple OSS Distributions #endif
2093ca3bd55SApple OSS Distributions /*********************************************************************
210a5e72196SApple OSS Distributions * IMPORTANT: vm_map_copyout_size() consumes the requestIn copy
211a5e72196SApple OSS Distributions * object on success. Therefore once it has been invoked successfully,
212a5e72196SApple OSS Distributions * this routine *must* return KERN_SUCCESS, regardless of our actual
213a5e72196SApple OSS Distributions * result. Our contract with the caller is that requestIn must be
214a5e72196SApple OSS Distributions * caller-deallocated if we return an error. We use op_result to return
215a5e72196SApple OSS Distributions * the real result of our work.
2163ca3bd55SApple OSS Distributions *********************************************************************/
217a5e72196SApple 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)218a5e72196SApple OSS Distributions kext_request(
2193ca3bd55SApple OSS Distributions host_priv_t hostPriv,
2203ca3bd55SApple OSS Distributions /* in only */ uint32_t clientLogSpec,
2213ca3bd55SApple OSS Distributions /* in only */ vm_offset_t requestIn,
2223ca3bd55SApple OSS Distributions /* in only */ mach_msg_type_number_t requestLengthIn,
2233ca3bd55SApple OSS Distributions /* out only */ vm_offset_t * responseOut,
2243ca3bd55SApple OSS Distributions /* out only */ mach_msg_type_number_t * responseLengthOut,
2253ca3bd55SApple OSS Distributions /* out only */ vm_offset_t * logDataOut,
2263ca3bd55SApple OSS Distributions /* out only */ mach_msg_type_number_t * logDataLengthOut,
2273ca3bd55SApple OSS Distributions /* out only */ kern_return_t * op_result)
2283ca3bd55SApple OSS Distributions {
2293ca3bd55SApple OSS Distributions kern_return_t result = KERN_FAILURE;
2303ca3bd55SApple OSS Distributions vm_map_address_t map_addr = 0; // do not free/deallocate
2313ca3bd55SApple OSS Distributions char * request = NULL;// must vm_deallocate
2323ca3bd55SApple OSS Distributions
2333ca3bd55SApple OSS Distributions mkext2_header * mkextHeader = NULL;// do not release
2343ca3bd55SApple OSS Distributions bool isMkext = false;
2353ca3bd55SApple OSS Distributions
2363ca3bd55SApple OSS Distributions char * response = NULL;// must kmem_free
2373ca3bd55SApple OSS Distributions uint32_t responseLength = 0;
2383ca3bd55SApple OSS Distributions char * logData = NULL;// must kmem_free
2393ca3bd55SApple OSS Distributions uint32_t logDataLength = 0;
2403ca3bd55SApple OSS Distributions
2413ca3bd55SApple OSS Distributions /* MIG doesn't pass "out" parameters as empty, so clear them immediately
2423ca3bd55SApple OSS Distributions * just in case, or MIG will try to copy out bogus data.
2433ca3bd55SApple OSS Distributions */
2443ca3bd55SApple OSS Distributions *op_result = KERN_FAILURE;
245a5e72196SApple OSS Distributions *responseOut = 0;
2463ca3bd55SApple OSS Distributions *responseLengthOut = 0;
247a5e72196SApple OSS Distributions *logDataOut = 0;
2483ca3bd55SApple OSS Distributions *logDataLengthOut = 0;
2493ca3bd55SApple OSS Distributions
2503ca3bd55SApple OSS Distributions /* Check for input. Don't discard what isn't there, though.
2513ca3bd55SApple OSS Distributions */
2523ca3bd55SApple OSS Distributions if (!requestLengthIn || !requestIn) {
2533ca3bd55SApple OSS Distributions OSKextLog(/* kext */ NULL,
2543ca3bd55SApple OSS Distributions kOSKextLogErrorLevel |
2553ca3bd55SApple OSS Distributions kOSKextLogIPCFlag,
2563ca3bd55SApple OSS Distributions "Invalid request from user space (no data).");
2573ca3bd55SApple OSS Distributions *op_result = KERN_INVALID_ARGUMENT;
2583ca3bd55SApple OSS Distributions goto finish;
2593ca3bd55SApple OSS Distributions }
2603ca3bd55SApple OSS Distributions
261a5e72196SApple OSS Distributions result = vm_map_copyout_size(kernel_map, &map_addr, (vm_map_copy_t)requestIn, requestLengthIn);
2623ca3bd55SApple OSS Distributions if (result != KERN_SUCCESS) {
2633ca3bd55SApple OSS Distributions OSKextLog(/* kext */ NULL,
2643ca3bd55SApple OSS Distributions kOSKextLogErrorLevel |
2653ca3bd55SApple OSS Distributions kOSKextLogIPCFlag,
2663ca3bd55SApple OSS Distributions "vm_map_copyout() failed for request from user space.");
267a5e72196SApple OSS Distributions /*
268a5e72196SApple OSS Distributions * If we return an error it is our caller's responsibility to
269a5e72196SApple OSS Distributions * deallocate the requestIn copy object, so do not deallocate it
270a5e72196SApple OSS Distributions * here. See comment above.
271a5e72196SApple OSS Distributions */
2723ca3bd55SApple OSS Distributions goto finish;
2733ca3bd55SApple OSS Distributions }
2743ca3bd55SApple OSS Distributions request = CAST_DOWN(char *, map_addr);
2753ca3bd55SApple OSS Distributions
2763ca3bd55SApple OSS Distributions /* Check if request is an mkext; this is always a load request
2773ca3bd55SApple OSS Distributions * and requires root access. If it isn't an mkext, see if it's
2783ca3bd55SApple OSS Distributions * an XML request, and check the request to see if that requires
2793ca3bd55SApple OSS Distributions * root access.
2803ca3bd55SApple OSS Distributions */
2813ca3bd55SApple OSS Distributions if (requestLengthIn > sizeof(mkext2_header)) {
2823ca3bd55SApple OSS Distributions mkextHeader = (mkext2_header *)request;
2833ca3bd55SApple OSS Distributions if (MKEXT_GET_MAGIC(mkextHeader) == MKEXT_MAGIC &&
2843ca3bd55SApple OSS Distributions MKEXT_GET_SIGNATURE(mkextHeader) == MKEXT_SIGN) {
2853ca3bd55SApple OSS Distributions isMkext = true;
2863ca3bd55SApple OSS Distributions }
2873ca3bd55SApple OSS Distributions }
2883ca3bd55SApple OSS Distributions
2893ca3bd55SApple OSS Distributions if (isMkext) {
290bb611c8fSApple OSS Distributions #if defined(SECURE_KERNEL) || !CONFIG_KXLD
2913ca3bd55SApple OSS Distributions // xxx - something tells me if we have a secure kernel we don't even
2923ca3bd55SApple OSS Distributions // xxx - want to log a message here. :-)
2933ca3bd55SApple OSS Distributions *op_result = KERN_NOT_SUPPORTED;
2943ca3bd55SApple OSS Distributions goto finish;
2953ca3bd55SApple OSS Distributions #else
2963ca3bd55SApple OSS Distributions // xxx - can we find out if calling task is kextd?
2973ca3bd55SApple OSS Distributions // xxx - can we find the name of the calling task?
2983ca3bd55SApple OSS Distributions if (hostPriv == HOST_PRIV_NULL) {
2993ca3bd55SApple OSS Distributions OSKextLog(/* kext */ NULL,
3003ca3bd55SApple OSS Distributions kOSKextLogErrorLevel |
3013ca3bd55SApple OSS Distributions kOSKextLogLoadFlag | kOSKextLogIPCFlag,
3023ca3bd55SApple OSS Distributions "Attempt by non-root process to load a kext.");
3033ca3bd55SApple OSS Distributions *op_result = kOSKextReturnNotPrivileged;
3043ca3bd55SApple OSS Distributions goto finish;
3053ca3bd55SApple OSS Distributions }
3063ca3bd55SApple OSS Distributions
3073ca3bd55SApple OSS Distributions *op_result = OSKext::loadFromMkext((OSKextLogSpec)clientLogSpec,
3083ca3bd55SApple OSS Distributions request, requestLengthIn,
3093ca3bd55SApple OSS Distributions &logData, &logDataLength);
3103ca3bd55SApple OSS Distributions
3113ca3bd55SApple OSS Distributions #endif /* defined(SECURE_KERNEL) */
3123ca3bd55SApple OSS Distributions } else {
3133ca3bd55SApple OSS Distributions /* If the request isn't an mkext, then is should be XML. Parse it
3143ca3bd55SApple OSS Distributions * if possible and hand the request over to OSKext.
3153ca3bd55SApple OSS Distributions */
3163ca3bd55SApple OSS Distributions *op_result = OSKext::handleRequest(hostPriv,
3173ca3bd55SApple OSS Distributions (OSKextLogSpec)clientLogSpec,
3183ca3bd55SApple OSS Distributions request, requestLengthIn,
3193ca3bd55SApple OSS Distributions &response, &responseLength,
3203ca3bd55SApple OSS Distributions &logData, &logDataLength);
3213ca3bd55SApple OSS Distributions }
3223ca3bd55SApple OSS Distributions
3233ca3bd55SApple OSS Distributions if (response && responseLength > 0) {
3243ca3bd55SApple OSS Distributions kern_return_t copyin_result;
3253ca3bd55SApple OSS Distributions
3263ca3bd55SApple OSS Distributions copyin_result = vm_map_copyin(kernel_map,
3273ca3bd55SApple OSS Distributions CAST_USER_ADDR_T(response), responseLength,
3283ca3bd55SApple OSS Distributions /* src_destroy */ false, (vm_map_copy_t *)responseOut);
3293ca3bd55SApple OSS Distributions if (copyin_result == KERN_SUCCESS) {
3303ca3bd55SApple OSS Distributions *responseLengthOut = responseLength;
3313ca3bd55SApple OSS Distributions } else {
3323ca3bd55SApple OSS Distributions OSKextLog(/* kext */ NULL,
3333ca3bd55SApple OSS Distributions kOSKextLogErrorLevel |
3343ca3bd55SApple OSS Distributions kOSKextLogIPCFlag,
3353ca3bd55SApple OSS Distributions "Failed to copy response to request from user space.");
3363ca3bd55SApple OSS Distributions *op_result = copyin_result; // xxx - should we map to our own code?
337a5e72196SApple OSS Distributions *responseOut = 0;
3383ca3bd55SApple OSS Distributions *responseLengthOut = 0;
3393ca3bd55SApple OSS Distributions goto finish;
3403ca3bd55SApple OSS Distributions }
3413ca3bd55SApple OSS Distributions }
3423ca3bd55SApple OSS Distributions
3433ca3bd55SApple OSS Distributions if (logData && logDataLength > 0) {
3443ca3bd55SApple OSS Distributions kern_return_t copyin_result;
3453ca3bd55SApple OSS Distributions
3463ca3bd55SApple OSS Distributions copyin_result = vm_map_copyin(kernel_map,
3473ca3bd55SApple OSS Distributions CAST_USER_ADDR_T(logData), logDataLength,
3483ca3bd55SApple OSS Distributions /* src_destroy */ false, (vm_map_copy_t *)logDataOut);
3493ca3bd55SApple OSS Distributions if (copyin_result == KERN_SUCCESS) {
3503ca3bd55SApple OSS Distributions *logDataLengthOut = logDataLength;
3513ca3bd55SApple OSS Distributions } else {
3523ca3bd55SApple OSS Distributions OSKextLog(/* kext */ NULL,
3533ca3bd55SApple OSS Distributions kOSKextLogErrorLevel |
3543ca3bd55SApple OSS Distributions kOSKextLogIPCFlag,
3553ca3bd55SApple OSS Distributions "Failed to copy log data for request from user space.");
3563ca3bd55SApple OSS Distributions *op_result = copyin_result; // xxx - should we map to our own code?
357a5e72196SApple OSS Distributions *logDataOut = 0;
3583ca3bd55SApple OSS Distributions *logDataLengthOut = 0;
3593ca3bd55SApple OSS Distributions goto finish;
3603ca3bd55SApple OSS Distributions }
3613ca3bd55SApple OSS Distributions }
3623ca3bd55SApple OSS Distributions
3633ca3bd55SApple OSS Distributions finish:
3643ca3bd55SApple OSS Distributions if (request) {
3653ca3bd55SApple OSS Distributions (void)vm_deallocate(kernel_map, (vm_offset_t)request, requestLengthIn);
3663ca3bd55SApple OSS Distributions }
3673ca3bd55SApple OSS Distributions if (response) {
368186b8fceSApple OSS Distributions /* 11981737 - clear uninitialized data in last page */
369186b8fceSApple OSS Distributions kmem_free(kernel_map, (vm_offset_t)response, round_page(responseLength));
3703ca3bd55SApple OSS Distributions }
3713ca3bd55SApple OSS Distributions if (logData) {
372186b8fceSApple OSS Distributions /* 11981737 - clear uninitialized data in last page */
373186b8fceSApple OSS Distributions kmem_free(kernel_map, (vm_offset_t)logData, round_page(logDataLength));
3743ca3bd55SApple OSS Distributions }
3753ca3bd55SApple OSS Distributions
3763ca3bd55SApple OSS Distributions return result;
3773ca3bd55SApple OSS Distributions }
3783ca3bd55SApple OSS Distributions
3793ca3bd55SApple OSS Distributions /*********************************************************************
3803ca3bd55SApple OSS Distributions * Gets the vm_map for the current kext
3813ca3bd55SApple OSS Distributions *********************************************************************/
38288cc0b97SApple OSS Distributions extern vm_offset_t segPRELINKTEXTB;
383bb611c8fSApple OSS Distributions extern vm_offset_t segLINKB;
38488cc0b97SApple OSS Distributions extern unsigned long segSizePRELINKTEXT;
3853ca3bd55SApple OSS Distributions
3863ca3bd55SApple OSS Distributions vm_map_t
kext_get_vm_map(kmod_info_t * info)3873ca3bd55SApple OSS Distributions kext_get_vm_map(kmod_info_t *info)
3883ca3bd55SApple OSS Distributions {
3893ca3bd55SApple OSS Distributions vm_map_t kext_map = NULL;
390bb611c8fSApple OSS Distributions kc_format_t kcformat;
3913ca3bd55SApple OSS Distributions
392bb611c8fSApple OSS Distributions if (PE_get_primary_kc_format(&kcformat) && kcformat == KCFormatFileset) {
393bb611c8fSApple OSS Distributions /* Check if the kext is from the boot KC */
394bb611c8fSApple OSS Distributions assert(segLINKB >= (segPRELINKTEXTB + segSizePRELINKTEXT));
395bb611c8fSApple OSS Distributions if ((info->address >= segPRELINKTEXTB) &&
396bb611c8fSApple OSS Distributions (info->address < segLINKB)) {
397bb611c8fSApple OSS Distributions kext_map = kernel_map;
398bb611c8fSApple OSS Distributions } else {
399bb611c8fSApple OSS Distributions kext_map = g_kext_map;
400bb611c8fSApple OSS Distributions }
401bb611c8fSApple OSS Distributions } else {
40288cc0b97SApple OSS Distributions if ((info->address >= segPRELINKTEXTB) &&
403a5e72196SApple OSS Distributions (info->address < (segPRELINKTEXTB + segSizePRELINKTEXT))) {
4043ca3bd55SApple OSS Distributions kext_map = kernel_map;
4053ca3bd55SApple OSS Distributions } else {
4063ca3bd55SApple OSS Distributions kext_map = g_kext_map;
4073ca3bd55SApple OSS Distributions }
408bb611c8fSApple OSS Distributions }
4093ca3bd55SApple OSS Distributions
4103ca3bd55SApple OSS Distributions return kext_map;
4113ca3bd55SApple OSS Distributions }
4123ca3bd55SApple OSS Distributions
4133ca3bd55SApple OSS Distributions
4143ca3bd55SApple OSS Distributions #if PRAGMA_MARK
4153ca3bd55SApple OSS Distributions /********************************************************************/
4163ca3bd55SApple OSS Distributions #pragma mark Weak linking support
4173ca3bd55SApple OSS Distributions /********************************************************************/
4183ca3bd55SApple OSS Distributions #endif
4193ca3bd55SApple OSS Distributions void
kext_weak_symbol_referenced(void)4203ca3bd55SApple OSS Distributions kext_weak_symbol_referenced(void)
4213ca3bd55SApple OSS Distributions {
422e6231be0SApple OSS Distributions panic("A kext referenced an unresolved weak symbol");
4233ca3bd55SApple OSS Distributions }
4243ca3bd55SApple OSS Distributions
425a5e72196SApple OSS Distributions const void * const gOSKextUnresolved = (const void *)&kext_weak_symbol_referenced;
4263ca3bd55SApple OSS Distributions
4273ca3bd55SApple OSS Distributions #if PRAGMA_MARK
4283ca3bd55SApple OSS Distributions #pragma mark Kernel-Internal C Functions
4293ca3bd55SApple OSS Distributions #endif
4303ca3bd55SApple OSS Distributions /*********************************************************************
4313ca3bd55SApple OSS Distributions * Called from startup.c.
4323ca3bd55SApple OSS Distributions *********************************************************************/
433a5e72196SApple OSS Distributions void
OSKextRemoveKextBootstrap(void)434a5e72196SApple OSS Distributions OSKextRemoveKextBootstrap(void)
4353ca3bd55SApple OSS Distributions {
4363ca3bd55SApple OSS Distributions OSKext::removeKextBootstrap();
4373ca3bd55SApple OSS Distributions return;
4383ca3bd55SApple OSS Distributions }
4393ca3bd55SApple OSS Distributions
440855239e5SApple OSS Distributions #if CONFIG_DTRACE
441855239e5SApple OSS Distributions /*********************************************************************
442855239e5SApple OSS Distributions *********************************************************************/
443a5e72196SApple OSS Distributions void
OSKextRegisterKextsWithDTrace(void)444a5e72196SApple OSS Distributions OSKextRegisterKextsWithDTrace(void)
445855239e5SApple OSS Distributions {
446855239e5SApple OSS Distributions OSKext::registerKextsWithDTrace();
447855239e5SApple OSS Distributions return;
448855239e5SApple OSS Distributions }
449855239e5SApple OSS Distributions #endif /* CONFIG_DTRACE */
450855239e5SApple OSS Distributions
4513ca3bd55SApple OSS Distributions /*********************************************************************
4523ca3bd55SApple OSS Distributions *********************************************************************/
453a5e72196SApple OSS Distributions void
kext_dump_panic_lists(int (* printf_func)(const char * fmt,...))454a5e72196SApple OSS Distributions kext_dump_panic_lists(int (*printf_func)(const char * fmt, ...))
4553ca3bd55SApple OSS Distributions {
4563ca3bd55SApple OSS Distributions OSKext::printKextPanicLists(printf_func);
4573ca3bd55SApple OSS Distributions return;
4583ca3bd55SApple OSS Distributions }
4593ca3bd55SApple OSS Distributions
4603ca3bd55SApple OSS Distributions #if PRAGMA_MARK
4613ca3bd55SApple OSS Distributions #pragma mark Kmod Compatibility Functions
4623ca3bd55SApple OSS Distributions #endif
4633ca3bd55SApple OSS Distributions /*********************************************************************
4643ca3bd55SApple OSS Distributions **********************************************************************
4653ca3bd55SApple OSS Distributions * KMOD COMPATIBILITY FUNCTIONS *
4663ca3bd55SApple OSS Distributions * (formerly in kmod.c, or C++ bridges from) *
4673ca3bd55SApple OSS Distributions **********************************************************************
4683ca3bd55SApple OSS Distributions **********************************************************************
4693ca3bd55SApple OSS Distributions * These two functions are used in various places in the kernel, but
4703ca3bd55SApple OSS Distributions * are not exported. We might rename them at some point to start with
4713ca3bd55SApple OSS Distributions * kext_ or OSKext.
4723ca3bd55SApple OSS Distributions *
4733ca3bd55SApple OSS Distributions * kmod_panic_dump() must not be called outside of a panic context.
4743ca3bd55SApple OSS Distributions * kmod_dump_log() must not be called in a panic context.
4753ca3bd55SApple OSS Distributions *********************************************************************/
4763ca3bd55SApple OSS Distributions void
kmod_panic_dump(vm_offset_t * addr,unsigned int cnt)4773ca3bd55SApple OSS Distributions kmod_panic_dump(vm_offset_t * addr, unsigned int cnt)
4783ca3bd55SApple OSS Distributions {
47976e12aa3SApple OSS Distributions extern int paniclog_append_noflush(const char *format, ...) __printflike(1, 2);
4803ca3bd55SApple OSS Distributions
48176e12aa3SApple OSS Distributions OSKext::printKextsInBacktrace(addr, cnt, &paniclog_append_noflush, 0);
48276e12aa3SApple OSS Distributions
4833ca3bd55SApple OSS Distributions return;
4843ca3bd55SApple OSS Distributions }
4853ca3bd55SApple OSS Distributions
48694d3b452SApple OSS Distributions void
telemetry_backtrace_add_kexts(char * buf,size_t buflen,uintptr_t * frames,uint32_t framecnt)48794d3b452SApple OSS Distributions telemetry_backtrace_add_kexts(
48894d3b452SApple OSS Distributions char *buf,
48994d3b452SApple OSS Distributions size_t buflen,
49094d3b452SApple OSS Distributions uintptr_t *frames,
49194d3b452SApple OSS Distributions uint32_t framecnt)
49294d3b452SApple OSS Distributions {
49394d3b452SApple OSS Distributions __block size_t pos = 0;
49494d3b452SApple OSS Distributions
49594d3b452SApple OSS Distributions OSKext::foreachKextInBacktrace(frames, framecnt, OSKext::kPrintKextsLock,
49694d3b452SApple OSS Distributions ^(OSKextLoadedKextSummary *summary, uint32_t index __unused){
49794d3b452SApple OSS Distributions uuid_string_t uuid;
49894d3b452SApple OSS Distributions uint64_t tmpAddr;
49994d3b452SApple OSS Distributions uint64_t tmpSize;
50094d3b452SApple OSS Distributions
50194d3b452SApple OSS Distributions (void) uuid_unparse(summary->uuid, uuid);
50294d3b452SApple OSS Distributions
50394d3b452SApple OSS Distributions #if defined(__arm__) || defined(__arm64__)
50494d3b452SApple OSS Distributions tmpAddr = summary->text_exec_address;
50594d3b452SApple OSS Distributions tmpSize = summary->text_exec_size;
50694d3b452SApple OSS Distributions #else
50794d3b452SApple OSS Distributions tmpAddr = summary->address;
50894d3b452SApple OSS Distributions tmpSize = summary->size;
50994d3b452SApple OSS Distributions #endif
51094d3b452SApple OSS Distributions tmpAddr -= vm_kernel_stext;
51194d3b452SApple OSS Distributions pos += scnprintf(buf + pos, buflen - pos, "%s@%llx:%llx\n",
51294d3b452SApple OSS Distributions uuid, tmpAddr, tmpAddr + tmpSize - 1);
51394d3b452SApple OSS Distributions });
51494d3b452SApple OSS Distributions }
51594d3b452SApple OSS Distributions
5163ca3bd55SApple OSS Distributions /********************************************************************/
517a3bb9fccSApple OSS Distributions void kmod_dump_log(vm_offset_t *addr, unsigned int cnt, boolean_t doUnslide);
5183ca3bd55SApple OSS Distributions
5193ca3bd55SApple OSS Distributions void
kmod_dump_log(vm_offset_t * addr,unsigned int cnt,boolean_t doUnslide)5203ca3bd55SApple OSS Distributions kmod_dump_log(
5213ca3bd55SApple OSS Distributions vm_offset_t * addr,
522a3bb9fccSApple OSS Distributions unsigned int cnt,
523a3bb9fccSApple OSS Distributions boolean_t doUnslide)
5243ca3bd55SApple OSS Distributions {
52576e12aa3SApple OSS Distributions uint32_t flags = OSKext::kPrintKextsLock;
526a5e72196SApple OSS Distributions if (doUnslide) {
527a5e72196SApple OSS Distributions flags |= OSKext::kPrintKextsUnslide;
528a5e72196SApple OSS Distributions }
52976e12aa3SApple OSS Distributions OSKext::printKextsInBacktrace(addr, cnt, &printf, flags);
5303ca3bd55SApple OSS Distributions }
5313ca3bd55SApple OSS Distributions
53288cc0b97SApple OSS Distributions void *
OSKextKextForAddress(const void * addr)53388cc0b97SApple OSS Distributions OSKextKextForAddress(const void *addr)
53488cc0b97SApple OSS Distributions {
53588cc0b97SApple OSS Distributions return OSKext::kextForAddress(addr);
53688cc0b97SApple OSS Distributions }
53788cc0b97SApple OSS Distributions
5381031c584SApple OSS Distributions kern_return_t
OSKextGetLoadedKextSummaryForAddress(const void * addr,OSKextLoadedKextSummary * summary)5391031c584SApple OSS Distributions OSKextGetLoadedKextSummaryForAddress(
5401031c584SApple OSS Distributions const void * addr,
5411031c584SApple OSS Distributions OSKextLoadedKextSummary * summary)
5421031c584SApple OSS Distributions {
5431031c584SApple OSS Distributions return OSKext::summaryForAddressExt(addr, summary);
5441031c584SApple OSS Distributions }
54588cc0b97SApple OSS Distributions
5463ca3bd55SApple OSS Distributions /*********************************************************************
5473ca3bd55SApple OSS Distributions * Compatibility implementation for kmod_get_info() host_priv routine.
5483ca3bd55SApple OSS Distributions * Only supported on old 32-bit architectures.
5493ca3bd55SApple OSS Distributions *********************************************************************/
550855239e5SApple OSS Distributions
551855239e5SApple OSS Distributions #if PRAGMA_MARK
552855239e5SApple OSS Distributions #pragma mark Loaded Kext Summary
553855239e5SApple OSS Distributions #endif
554855239e5SApple OSS Distributions
555855239e5SApple OSS Distributions void
OSKextLoadedKextSummariesUpdated(void)556855239e5SApple OSS Distributions OSKextLoadedKextSummariesUpdated(void)
557855239e5SApple OSS Distributions {
558855239e5SApple OSS Distributions // Do nothing.
559855239e5SApple OSS Distributions }
5603ca3bd55SApple OSS Distributions };
561