1*1b191cb5SApple OSS Distributions /*
2*1b191cb5SApple OSS Distributions * Copyright (c) 2000,2008-2009 Apple Inc. All rights reserved.
3*1b191cb5SApple OSS Distributions *
4*1b191cb5SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1b191cb5SApple OSS Distributions *
6*1b191cb5SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*1b191cb5SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*1b191cb5SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*1b191cb5SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*1b191cb5SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*1b191cb5SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*1b191cb5SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*1b191cb5SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*1b191cb5SApple OSS Distributions *
15*1b191cb5SApple OSS Distributions * Please obtain a copy of the License at
16*1b191cb5SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1b191cb5SApple OSS Distributions *
18*1b191cb5SApple OSS Distributions * The Original Code and all software distributed under the License are
19*1b191cb5SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1b191cb5SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1b191cb5SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1b191cb5SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1b191cb5SApple OSS Distributions * Please see the License for the specific language governing rights and
24*1b191cb5SApple OSS Distributions * limitations under the License.
25*1b191cb5SApple OSS Distributions *
26*1b191cb5SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1b191cb5SApple OSS Distributions */
28*1b191cb5SApple OSS Distributions /*
29*1b191cb5SApple OSS Distributions * Copyright (c) 1997 Apple Inc.
30*1b191cb5SApple OSS Distributions *
31*1b191cb5SApple OSS Distributions */
32*1b191cb5SApple OSS Distributions #include <libkern/c++/OSMetaClass.h>
33*1b191cb5SApple OSS Distributions #include <libkern/c++/OSKext.h>
34*1b191cb5SApple OSS Distributions #include <libkern/c++/OSLib.h>
35*1b191cb5SApple OSS Distributions #include <libkern/c++/OSSymbol.h>
36*1b191cb5SApple OSS Distributions #include <IOKit/IOKitDebug.h>
37*1b191cb5SApple OSS Distributions
38*1b191cb5SApple OSS Distributions #include <sys/cdefs.h>
39*1b191cb5SApple OSS Distributions #if defined(HAS_APPLE_PAC)
40*1b191cb5SApple OSS Distributions #include <ptrauth.h>
41*1b191cb5SApple OSS Distributions #define PTRAUTH_STRIP_STRUCTOR(x) ((uintptr_t) ptrauth_strip(ptrauth_nop_cast(void *, (x)), ptrauth_key_function_pointer))
42*1b191cb5SApple OSS Distributions #else /* defined(HAS_APPLE_PAC) */
43*1b191cb5SApple OSS Distributions #define PTRAUTH_STRIP_STRUCTOR(x) ((uintptr_t) (x))
44*1b191cb5SApple OSS Distributions #endif /* !defined(HAS_APPLE_PAC) */
45*1b191cb5SApple OSS Distributions
46*1b191cb5SApple OSS Distributions __BEGIN_DECLS
47*1b191cb5SApple OSS Distributions
48*1b191cb5SApple OSS Distributions #include <string.h>
49*1b191cb5SApple OSS Distributions #include <mach/mach_types.h>
50*1b191cb5SApple OSS Distributions #include <libkern/kernel_mach_header.h>
51*1b191cb5SApple OSS Distributions #include <libkern/prelink.h>
52*1b191cb5SApple OSS Distributions #include <stdarg.h>
53*1b191cb5SApple OSS Distributions
54*1b191cb5SApple OSS Distributions #if KASAN
55*1b191cb5SApple OSS Distributions #include <san/kasan.h>
56*1b191cb5SApple OSS Distributions #endif
57*1b191cb5SApple OSS Distributions
58*1b191cb5SApple OSS Distributions
59*1b191cb5SApple OSS Distributions #if PRAGMA_MARK
60*1b191cb5SApple OSS Distributions #pragma mark Constants &c.
61*1b191cb5SApple OSS Distributions #endif /* PRAGMA_MARK */
62*1b191cb5SApple OSS Distributions OSKextLogSpec kOSRuntimeLogSpec =
63*1b191cb5SApple OSS Distributions kOSKextLogErrorLevel |
64*1b191cb5SApple OSS Distributions kOSKextLogLoadFlag |
65*1b191cb5SApple OSS Distributions kOSKextLogKextBookkeepingFlag;
66*1b191cb5SApple OSS Distributions
67*1b191cb5SApple OSS Distributions #if PRAGMA_MARK
68*1b191cb5SApple OSS Distributions #pragma mark Logging Bootstrap
69*1b191cb5SApple OSS Distributions #endif /* PRAGMA_MARK */
70*1b191cb5SApple OSS Distributions /*********************************************************************
71*1b191cb5SApple OSS Distributions * kern_os Logging Bootstrap
72*1b191cb5SApple OSS Distributions *
73*1b191cb5SApple OSS Distributions * We can't call in to OSKext until the kernel's C++ environment is up
74*1b191cb5SApple OSS Distributions * and running, so let's mask those references with a check variable.
75*1b191cb5SApple OSS Distributions * We print unconditionally if C++ isn't up, but if that's the case
76*1b191cb5SApple OSS Distributions * we've generally hit a serious error in kernel init!
77*1b191cb5SApple OSS Distributions *********************************************************************/
78*1b191cb5SApple OSS Distributions static bool gKernelCPPInitialized = false;
79*1b191cb5SApple OSS Distributions
80*1b191cb5SApple OSS Distributions #define OSRuntimeLog(kext, flags, format, args ...) \
81*1b191cb5SApple OSS Distributions do { \
82*1b191cb5SApple OSS Distributions if (gKernelCPPInitialized) { \
83*1b191cb5SApple OSS Distributions OSKextLog((kext), (flags), (format), ## args); \
84*1b191cb5SApple OSS Distributions } else { \
85*1b191cb5SApple OSS Distributions printf((format), ## args); \
86*1b191cb5SApple OSS Distributions } \
87*1b191cb5SApple OSS Distributions } while (0)
88*1b191cb5SApple OSS Distributions
89*1b191cb5SApple OSS Distributions #if PRAGMA_MARK
90*1b191cb5SApple OSS Distributions #pragma mark Libkern Init
91*1b191cb5SApple OSS Distributions #endif /* PRAGMA_MARK */
92*1b191cb5SApple OSS Distributions /*********************************************************************
93*1b191cb5SApple OSS Distributions * Libkern Init
94*1b191cb5SApple OSS Distributions *********************************************************************/
95*1b191cb5SApple OSS Distributions
96*1b191cb5SApple OSS Distributions #if __GNUC__ >= 3
97*1b191cb5SApple OSS Distributions void __dead2
__cxa_pure_virtual(void)98*1b191cb5SApple OSS Distributions __cxa_pure_virtual( void )
99*1b191cb5SApple OSS Distributions {
100*1b191cb5SApple OSS Distributions panic("%s", __FUNCTION__);
101*1b191cb5SApple OSS Distributions }
102*1b191cb5SApple OSS Distributions #else
103*1b191cb5SApple OSS Distributions void __dead2
__pure_virtual(void)104*1b191cb5SApple OSS Distributions __pure_virtual( void )
105*1b191cb5SApple OSS Distributions {
106*1b191cb5SApple OSS Distributions panic("%s", __FUNCTION__);
107*1b191cb5SApple OSS Distributions }
108*1b191cb5SApple OSS Distributions #endif
109*1b191cb5SApple OSS Distributions
110*1b191cb5SApple OSS Distributions extern lck_grp_t * IOLockGroup;
111*1b191cb5SApple OSS Distributions extern kmod_info_t g_kernel_kmod_info;
112*1b191cb5SApple OSS Distributions
113*1b191cb5SApple OSS Distributions enum {
114*1b191cb5SApple OSS Distributions kOSSectionNamesDefault = 0,
115*1b191cb5SApple OSS Distributions kOSSectionNamesBuiltinKext = 1,
116*1b191cb5SApple OSS Distributions kOSSectionNamesCount = 2,
117*1b191cb5SApple OSS Distributions };
118*1b191cb5SApple OSS Distributions enum {
119*1b191cb5SApple OSS Distributions kOSSectionNameInitializer = 0,
120*1b191cb5SApple OSS Distributions kOSSectionNameFinalizer = 1,
121*1b191cb5SApple OSS Distributions kOSSectionNameCount = 2
122*1b191cb5SApple OSS Distributions };
123*1b191cb5SApple OSS Distributions
124*1b191cb5SApple OSS Distributions static const char *
125*1b191cb5SApple OSS Distributions gOSStructorSectionNames[kOSSectionNamesCount][kOSSectionNameCount] = {
126*1b191cb5SApple OSS Distributions { SECT_MODINITFUNC, SECT_MODTERMFUNC },
127*1b191cb5SApple OSS Distributions { kBuiltinInitSection, kBuiltinTermSection }
128*1b191cb5SApple OSS Distributions };
129*1b191cb5SApple OSS Distributions
130*1b191cb5SApple OSS Distributions void
OSlibkernInit(void)131*1b191cb5SApple OSS Distributions OSlibkernInit(void)
132*1b191cb5SApple OSS Distributions {
133*1b191cb5SApple OSS Distributions // This must be called before calling OSRuntimeInitializeCPP.
134*1b191cb5SApple OSS Distributions OSMetaClassBase::initialize();
135*1b191cb5SApple OSS Distributions
136*1b191cb5SApple OSS Distributions g_kernel_kmod_info.address = (vm_address_t) &_mh_execute_header;
137*1b191cb5SApple OSS Distributions
138*1b191cb5SApple OSS Distributions if (kOSReturnSuccess != OSRuntimeInitializeCPP(NULL)) {
139*1b191cb5SApple OSS Distributions // &g_kernel_kmod_info, gOSSectionNamesStandard, 0, 0)) {
140*1b191cb5SApple OSS Distributions panic("OSRuntime: C++ runtime failed to initialize.");
141*1b191cb5SApple OSS Distributions }
142*1b191cb5SApple OSS Distributions
143*1b191cb5SApple OSS Distributions gKernelCPPInitialized = true;
144*1b191cb5SApple OSS Distributions
145*1b191cb5SApple OSS Distributions return;
146*1b191cb5SApple OSS Distributions }
147*1b191cb5SApple OSS Distributions
148*1b191cb5SApple OSS Distributions __END_DECLS
149*1b191cb5SApple OSS Distributions
150*1b191cb5SApple OSS Distributions #if PRAGMA_MARK
151*1b191cb5SApple OSS Distributions #pragma mark C++ Runtime Load/Unload
152*1b191cb5SApple OSS Distributions #endif /* PRAGMA_MARK */
153*1b191cb5SApple OSS Distributions /*********************************************************************
154*1b191cb5SApple OSS Distributions * kern_os C++ Runtime Load/Unload
155*1b191cb5SApple OSS Distributions *********************************************************************/
156*1b191cb5SApple OSS Distributions
157*1b191cb5SApple OSS Distributions typedef void (*structor_t)(void);
158*1b191cb5SApple OSS Distributions
159*1b191cb5SApple OSS Distributions static bool
OSRuntimeCallStructorsInSection(OSKext * theKext,kmod_info_t * kmodInfo,void * metaHandle,kernel_segment_command_t * segment,const char * sectionName,uintptr_t textStart,uintptr_t textEnd)160*1b191cb5SApple OSS Distributions OSRuntimeCallStructorsInSection(
161*1b191cb5SApple OSS Distributions OSKext * theKext,
162*1b191cb5SApple OSS Distributions kmod_info_t * kmodInfo,
163*1b191cb5SApple OSS Distributions void * metaHandle,
164*1b191cb5SApple OSS Distributions kernel_segment_command_t * segment,
165*1b191cb5SApple OSS Distributions const char * sectionName,
166*1b191cb5SApple OSS Distributions uintptr_t textStart,
167*1b191cb5SApple OSS Distributions uintptr_t textEnd)
168*1b191cb5SApple OSS Distributions {
169*1b191cb5SApple OSS Distributions kernel_section_t * section;
170*1b191cb5SApple OSS Distributions bool result = TRUE;
171*1b191cb5SApple OSS Distributions
172*1b191cb5SApple OSS Distributions for (section = firstsect(segment);
173*1b191cb5SApple OSS Distributions section != NULL;
174*1b191cb5SApple OSS Distributions section = nextsect(segment, section)) {
175*1b191cb5SApple OSS Distributions if (strncmp(section->sectname, sectionName, sizeof(section->sectname) - 1)) {
176*1b191cb5SApple OSS Distributions continue;
177*1b191cb5SApple OSS Distributions }
178*1b191cb5SApple OSS Distributions if (section->size == 0) {
179*1b191cb5SApple OSS Distributions continue;
180*1b191cb5SApple OSS Distributions }
181*1b191cb5SApple OSS Distributions
182*1b191cb5SApple OSS Distributions structor_t * structors = (structor_t *)section->addr;
183*1b191cb5SApple OSS Distributions if (!structors) {
184*1b191cb5SApple OSS Distributions continue;
185*1b191cb5SApple OSS Distributions }
186*1b191cb5SApple OSS Distributions
187*1b191cb5SApple OSS Distributions structor_t structor;
188*1b191cb5SApple OSS Distributions uintptr_t value;
189*1b191cb5SApple OSS Distributions unsigned long num_structors = section->size / sizeof(structor_t);
190*1b191cb5SApple OSS Distributions unsigned int hit_null_structor = 0;
191*1b191cb5SApple OSS Distributions unsigned long firstIndex = 0;
192*1b191cb5SApple OSS Distributions
193*1b191cb5SApple OSS Distributions if (textStart) {
194*1b191cb5SApple OSS Distributions // bsearch for any in range
195*1b191cb5SApple OSS Distributions unsigned long baseIdx;
196*1b191cb5SApple OSS Distributions unsigned long lim;
197*1b191cb5SApple OSS Distributions firstIndex = num_structors;
198*1b191cb5SApple OSS Distributions for (lim = num_structors, baseIdx = 0; lim; lim >>= 1) {
199*1b191cb5SApple OSS Distributions structor = structors[baseIdx + (lim >> 1)];
200*1b191cb5SApple OSS Distributions if (!structor) {
201*1b191cb5SApple OSS Distributions panic("%s: null structor", kmodInfo->name);
202*1b191cb5SApple OSS Distributions }
203*1b191cb5SApple OSS Distributions value = PTRAUTH_STRIP_STRUCTOR(structor);
204*1b191cb5SApple OSS Distributions if ((value >= textStart) && (value < textEnd)) {
205*1b191cb5SApple OSS Distributions firstIndex = (baseIdx + (lim >> 1));
206*1b191cb5SApple OSS Distributions // scan back for the first in range
207*1b191cb5SApple OSS Distributions for (; firstIndex; firstIndex--) {
208*1b191cb5SApple OSS Distributions structor = structors[firstIndex - 1];
209*1b191cb5SApple OSS Distributions value = PTRAUTH_STRIP_STRUCTOR(structor);
210*1b191cb5SApple OSS Distributions if ((value < textStart) || (value >= textEnd)) {
211*1b191cb5SApple OSS Distributions break;
212*1b191cb5SApple OSS Distributions }
213*1b191cb5SApple OSS Distributions }
214*1b191cb5SApple OSS Distributions break;
215*1b191cb5SApple OSS Distributions }
216*1b191cb5SApple OSS Distributions if (textStart > value) {
217*1b191cb5SApple OSS Distributions // move right
218*1b191cb5SApple OSS Distributions baseIdx += (lim >> 1) + 1;
219*1b191cb5SApple OSS Distributions lim--;
220*1b191cb5SApple OSS Distributions }
221*1b191cb5SApple OSS Distributions // else move left
222*1b191cb5SApple OSS Distributions }
223*1b191cb5SApple OSS Distributions baseIdx = (baseIdx + (lim >> 1));
224*1b191cb5SApple OSS Distributions }
225*1b191cb5SApple OSS Distributions for (;
226*1b191cb5SApple OSS Distributions (firstIndex < num_structors)
227*1b191cb5SApple OSS Distributions && (!metaHandle || OSMetaClass::checkModLoad(metaHandle));
228*1b191cb5SApple OSS Distributions firstIndex++) {
229*1b191cb5SApple OSS Distributions if ((structor = structors[firstIndex])) {
230*1b191cb5SApple OSS Distributions value = PTRAUTH_STRIP_STRUCTOR(structor);
231*1b191cb5SApple OSS Distributions if ((textStart && (value < textStart))
232*1b191cb5SApple OSS Distributions || (textEnd && (value >= textEnd))) {
233*1b191cb5SApple OSS Distributions break;
234*1b191cb5SApple OSS Distributions }
235*1b191cb5SApple OSS Distributions (*structor)();
236*1b191cb5SApple OSS Distributions } else if (!hit_null_structor) {
237*1b191cb5SApple OSS Distributions hit_null_structor = 1;
238*1b191cb5SApple OSS Distributions OSRuntimeLog(theKext, kOSRuntimeLogSpec,
239*1b191cb5SApple OSS Distributions "Null structor in kext %s segment %s!",
240*1b191cb5SApple OSS Distributions kmodInfo->name, section->segname);
241*1b191cb5SApple OSS Distributions }
242*1b191cb5SApple OSS Distributions }
243*1b191cb5SApple OSS Distributions if (metaHandle) {
244*1b191cb5SApple OSS Distributions result = OSMetaClass::checkModLoad(metaHandle);
245*1b191cb5SApple OSS Distributions }
246*1b191cb5SApple OSS Distributions break;
247*1b191cb5SApple OSS Distributions } /* for (section...) */
248*1b191cb5SApple OSS Distributions return result;
249*1b191cb5SApple OSS Distributions }
250*1b191cb5SApple OSS Distributions
251*1b191cb5SApple OSS Distributions /*********************************************************************
252*1b191cb5SApple OSS Distributions *********************************************************************/
253*1b191cb5SApple OSS Distributions kern_return_t
OSRuntimeFinalizeCPP(OSKext * theKext)254*1b191cb5SApple OSS Distributions OSRuntimeFinalizeCPP(
255*1b191cb5SApple OSS Distributions OSKext * theKext)
256*1b191cb5SApple OSS Distributions {
257*1b191cb5SApple OSS Distributions kern_return_t result = KMOD_RETURN_FAILURE;
258*1b191cb5SApple OSS Distributions void * metaHandle = NULL;// do not free
259*1b191cb5SApple OSS Distributions kernel_mach_header_t * header;
260*1b191cb5SApple OSS Distributions kernel_segment_command_t * segment;
261*1b191cb5SApple OSS Distributions kmod_info_t * kmodInfo;
262*1b191cb5SApple OSS Distributions const char ** sectionNames;
263*1b191cb5SApple OSS Distributions uintptr_t textStart;
264*1b191cb5SApple OSS Distributions uintptr_t textEnd;
265*1b191cb5SApple OSS Distributions
266*1b191cb5SApple OSS Distributions textStart = 0;
267*1b191cb5SApple OSS Distributions textEnd = 0;
268*1b191cb5SApple OSS Distributions sectionNames = gOSStructorSectionNames[kOSSectionNamesDefault];
269*1b191cb5SApple OSS Distributions if (theKext) {
270*1b191cb5SApple OSS Distributions if (!theKext->isCPPInitialized()) {
271*1b191cb5SApple OSS Distributions result = KMOD_RETURN_SUCCESS;
272*1b191cb5SApple OSS Distributions goto finish;
273*1b191cb5SApple OSS Distributions }
274*1b191cb5SApple OSS Distributions kmodInfo = theKext->kmod_info;
275*1b191cb5SApple OSS Distributions if (!kmodInfo || !kmodInfo->address) {
276*1b191cb5SApple OSS Distributions result = kOSKextReturnInvalidArgument;
277*1b191cb5SApple OSS Distributions goto finish;
278*1b191cb5SApple OSS Distributions }
279*1b191cb5SApple OSS Distributions header = (kernel_mach_header_t *)kmodInfo->address;
280*1b191cb5SApple OSS Distributions if (theKext->flags.builtin) {
281*1b191cb5SApple OSS Distributions header = (kernel_mach_header_t *)g_kernel_kmod_info.address;
282*1b191cb5SApple OSS Distributions textStart = kmodInfo->address;
283*1b191cb5SApple OSS Distributions textEnd = textStart + kmodInfo->size;
284*1b191cb5SApple OSS Distributions sectionNames = gOSStructorSectionNames[kOSSectionNamesBuiltinKext];
285*1b191cb5SApple OSS Distributions }
286*1b191cb5SApple OSS Distributions } else {
287*1b191cb5SApple OSS Distributions kmodInfo = &g_kernel_kmod_info;
288*1b191cb5SApple OSS Distributions header = (kernel_mach_header_t *)kmodInfo->address;
289*1b191cb5SApple OSS Distributions }
290*1b191cb5SApple OSS Distributions
291*1b191cb5SApple OSS Distributions /* OSKext checks for this condition now, but somebody might call
292*1b191cb5SApple OSS Distributions * this function directly (the symbol is exported....).
293*1b191cb5SApple OSS Distributions */
294*1b191cb5SApple OSS Distributions if (OSMetaClass::modHasInstance(kmodInfo->name)) {
295*1b191cb5SApple OSS Distributions // xxx - Don't log under errors? this is more of an info thing
296*1b191cb5SApple OSS Distributions OSRuntimeLog(theKext, kOSRuntimeLogSpec,
297*1b191cb5SApple OSS Distributions "Can't tear down kext %s C++; classes have instances:",
298*1b191cb5SApple OSS Distributions kmodInfo->name);
299*1b191cb5SApple OSS Distributions OSKext::reportOSMetaClassInstances(kmodInfo->name, kOSRuntimeLogSpec);
300*1b191cb5SApple OSS Distributions result = kOSMetaClassHasInstances;
301*1b191cb5SApple OSS Distributions goto finish;
302*1b191cb5SApple OSS Distributions }
303*1b191cb5SApple OSS Distributions
304*1b191cb5SApple OSS Distributions /* Tell the meta class system that we are starting to unload.
305*1b191cb5SApple OSS Distributions * metaHandle isn't actually needed on the finalize path,
306*1b191cb5SApple OSS Distributions * so we don't check it here, even though OSMetaClass::postModLoad() will
307*1b191cb5SApple OSS Distributions * return a failure (it only does actual work on the init path anyhow).
308*1b191cb5SApple OSS Distributions */
309*1b191cb5SApple OSS Distributions metaHandle = OSMetaClass::preModLoad(kmodInfo->name);
310*1b191cb5SApple OSS Distributions
311*1b191cb5SApple OSS Distributions OSSymbol::checkForPageUnload((void *)kmodInfo->address,
312*1b191cb5SApple OSS Distributions (void *)(kmodInfo->address + kmodInfo->size));
313*1b191cb5SApple OSS Distributions
314*1b191cb5SApple OSS Distributions header = (kernel_mach_header_t *)kmodInfo->address;
315*1b191cb5SApple OSS Distributions segment = firstsegfromheader(header);
316*1b191cb5SApple OSS Distributions
317*1b191cb5SApple OSS Distributions for (segment = firstsegfromheader(header);
318*1b191cb5SApple OSS Distributions segment != NULL;
319*1b191cb5SApple OSS Distributions segment = nextsegfromheader(header, segment)) {
320*1b191cb5SApple OSS Distributions OSRuntimeCallStructorsInSection(theKext, kmodInfo, NULL, segment,
321*1b191cb5SApple OSS Distributions sectionNames[kOSSectionNameFinalizer], textStart, textEnd);
322*1b191cb5SApple OSS Distributions }
323*1b191cb5SApple OSS Distributions
324*1b191cb5SApple OSS Distributions (void)OSMetaClass::postModLoad(metaHandle);
325*1b191cb5SApple OSS Distributions
326*1b191cb5SApple OSS Distributions if (theKext) {
327*1b191cb5SApple OSS Distributions theKext->setCPPInitialized(false);
328*1b191cb5SApple OSS Distributions }
329*1b191cb5SApple OSS Distributions result = KMOD_RETURN_SUCCESS;
330*1b191cb5SApple OSS Distributions finish:
331*1b191cb5SApple OSS Distributions return result;
332*1b191cb5SApple OSS Distributions }
333*1b191cb5SApple OSS Distributions
334*1b191cb5SApple OSS Distributions #if defined(HAS_APPLE_PAC)
335*1b191cb5SApple OSS Distributions #if !KASAN
336*1b191cb5SApple OSS Distributions /*
337*1b191cb5SApple OSS Distributions * Place this function in __KLD,__text on non-kasan builds so it gets unmapped
338*1b191cb5SApple OSS Distributions * after CTRR lockdown.
339*1b191cb5SApple OSS Distributions */
340*1b191cb5SApple OSS Distributions __attribute__((noinline, section("__KLD,__text")))
341*1b191cb5SApple OSS Distributions #endif
342*1b191cb5SApple OSS Distributions static void
OSRuntimeSignStructorsInSegment(kernel_segment_command_t * segment)343*1b191cb5SApple OSS Distributions OSRuntimeSignStructorsInSegment(kernel_segment_command_t *segment)
344*1b191cb5SApple OSS Distributions {
345*1b191cb5SApple OSS Distributions kernel_section_t * section;
346*1b191cb5SApple OSS Distributions structor_t * structors;
347*1b191cb5SApple OSS Distributions volatile structor_t structor;
348*1b191cb5SApple OSS Distributions size_t idx, num_structors;
349*1b191cb5SApple OSS Distributions
350*1b191cb5SApple OSS Distributions for (section = firstsect(segment);
351*1b191cb5SApple OSS Distributions section != NULL;
352*1b191cb5SApple OSS Distributions section = nextsect(segment, section)) {
353*1b191cb5SApple OSS Distributions if ((S_MOD_INIT_FUNC_POINTERS != (SECTION_TYPE & section->flags))
354*1b191cb5SApple OSS Distributions && (S_MOD_TERM_FUNC_POINTERS != (SECTION_TYPE & section->flags))) {
355*1b191cb5SApple OSS Distributions continue;
356*1b191cb5SApple OSS Distributions }
357*1b191cb5SApple OSS Distributions structors = (structor_t *)section->addr;
358*1b191cb5SApple OSS Distributions if (!structors) {
359*1b191cb5SApple OSS Distributions continue;
360*1b191cb5SApple OSS Distributions }
361*1b191cb5SApple OSS Distributions num_structors = section->size / sizeof(structor_t);
362*1b191cb5SApple OSS Distributions for (idx = 0; idx < num_structors; idx++) {
363*1b191cb5SApple OSS Distributions structor = structors[idx];
364*1b191cb5SApple OSS Distributions if (NULL == structor) {
365*1b191cb5SApple OSS Distributions continue;
366*1b191cb5SApple OSS Distributions }
367*1b191cb5SApple OSS Distributions structor = ptrauth_strip(structor, ptrauth_key_function_pointer);
368*1b191cb5SApple OSS Distributions structor = ptrauth_sign_unauthenticated(structor, ptrauth_key_function_pointer, ptrauth_function_pointer_type_discriminator(void (*)(void)));
369*1b191cb5SApple OSS Distributions structors[idx] = structor;
370*1b191cb5SApple OSS Distributions }
371*1b191cb5SApple OSS Distributions } /* for (section...) */
372*1b191cb5SApple OSS Distributions }
373*1b191cb5SApple OSS Distributions #endif
374*1b191cb5SApple OSS Distributions
375*1b191cb5SApple OSS Distributions /*********************************************************************
376*1b191cb5SApple OSS Distributions *********************************************************************/
377*1b191cb5SApple OSS Distributions void
OSRuntimeSignStructors(kernel_mach_header_t * header __unused)378*1b191cb5SApple OSS Distributions OSRuntimeSignStructors(
379*1b191cb5SApple OSS Distributions kernel_mach_header_t * header __unused)
380*1b191cb5SApple OSS Distributions {
381*1b191cb5SApple OSS Distributions #if defined(HAS_APPLE_PAC)
382*1b191cb5SApple OSS Distributions
383*1b191cb5SApple OSS Distributions kernel_segment_command_t * segment;
384*1b191cb5SApple OSS Distributions
385*1b191cb5SApple OSS Distributions for (segment = firstsegfromheader(header);
386*1b191cb5SApple OSS Distributions segment != NULL;
387*1b191cb5SApple OSS Distributions segment = nextsegfromheader(header, segment)) {
388*1b191cb5SApple OSS Distributions OSRuntimeSignStructorsInSegment(segment);
389*1b191cb5SApple OSS Distributions } /* for (segment...) */
390*1b191cb5SApple OSS Distributions #endif /* !defined(XXX) && defined(HAS_APPLE_PAC) */
391*1b191cb5SApple OSS Distributions }
392*1b191cb5SApple OSS Distributions
393*1b191cb5SApple OSS Distributions /*********************************************************************
394*1b191cb5SApple OSS Distributions *********************************************************************/
395*1b191cb5SApple OSS Distributions void
OSRuntimeSignStructorsInFileset(kernel_mach_header_t * fileset_header __unused)396*1b191cb5SApple OSS Distributions OSRuntimeSignStructorsInFileset(
397*1b191cb5SApple OSS Distributions kernel_mach_header_t * fileset_header __unused)
398*1b191cb5SApple OSS Distributions {
399*1b191cb5SApple OSS Distributions #if defined(HAS_APPLE_PAC)
400*1b191cb5SApple OSS Distributions struct load_command *lc;
401*1b191cb5SApple OSS Distributions
402*1b191cb5SApple OSS Distributions lc = (struct load_command *)((uintptr_t)fileset_header + sizeof(*fileset_header));
403*1b191cb5SApple OSS Distributions for (uint32_t i = 0; i < fileset_header->ncmds; i++,
404*1b191cb5SApple OSS Distributions lc = (struct load_command *)((uintptr_t)lc + lc->cmdsize)) {
405*1b191cb5SApple OSS Distributions if (lc->cmd == LC_FILESET_ENTRY) {
406*1b191cb5SApple OSS Distributions struct fileset_entry_command *fse;
407*1b191cb5SApple OSS Distributions kernel_mach_header_t *mh;
408*1b191cb5SApple OSS Distributions
409*1b191cb5SApple OSS Distributions fse = (struct fileset_entry_command *)(uintptr_t)lc;
410*1b191cb5SApple OSS Distributions mh = (kernel_mach_header_t *)((uintptr_t)fse->vmaddr);
411*1b191cb5SApple OSS Distributions OSRuntimeSignStructors(mh);
412*1b191cb5SApple OSS Distributions } else if (lc->cmd == LC_SEGMENT_64) {
413*1b191cb5SApple OSS Distributions /*
414*1b191cb5SApple OSS Distributions * Slide/adjust all LC_SEGMENT_64 commands in the fileset
415*1b191cb5SApple OSS Distributions * (and any sections in those segments)
416*1b191cb5SApple OSS Distributions */
417*1b191cb5SApple OSS Distributions kernel_segment_command_t *seg;
418*1b191cb5SApple OSS Distributions seg = (kernel_segment_command_t *)(uintptr_t)lc;
419*1b191cb5SApple OSS Distributions OSRuntimeSignStructorsInSegment(seg);
420*1b191cb5SApple OSS Distributions }
421*1b191cb5SApple OSS Distributions }
422*1b191cb5SApple OSS Distributions
423*1b191cb5SApple OSS Distributions #endif /* defined(HAS_APPLE_PAC) */
424*1b191cb5SApple OSS Distributions }
425*1b191cb5SApple OSS Distributions
426*1b191cb5SApple OSS Distributions /*********************************************************************
427*1b191cb5SApple OSS Distributions *********************************************************************/
428*1b191cb5SApple OSS Distributions kern_return_t
OSRuntimeInitializeCPP(OSKext * theKext)429*1b191cb5SApple OSS Distributions OSRuntimeInitializeCPP(
430*1b191cb5SApple OSS Distributions OSKext * theKext)
431*1b191cb5SApple OSS Distributions {
432*1b191cb5SApple OSS Distributions kern_return_t result = KMOD_RETURN_FAILURE;
433*1b191cb5SApple OSS Distributions kernel_mach_header_t * header = NULL;
434*1b191cb5SApple OSS Distributions void * metaHandle = NULL;// do not free
435*1b191cb5SApple OSS Distributions bool load_success = true;
436*1b191cb5SApple OSS Distributions kernel_segment_command_t * segment = NULL;// do not free
437*1b191cb5SApple OSS Distributions kernel_segment_command_t * failure_segment = NULL; // do not free
438*1b191cb5SApple OSS Distributions kmod_info_t * kmodInfo;
439*1b191cb5SApple OSS Distributions const char ** sectionNames;
440*1b191cb5SApple OSS Distributions uintptr_t textStart;
441*1b191cb5SApple OSS Distributions uintptr_t textEnd;
442*1b191cb5SApple OSS Distributions
443*1b191cb5SApple OSS Distributions textStart = 0;
444*1b191cb5SApple OSS Distributions textEnd = 0;
445*1b191cb5SApple OSS Distributions sectionNames = gOSStructorSectionNames[kOSSectionNamesDefault];
446*1b191cb5SApple OSS Distributions if (theKext) {
447*1b191cb5SApple OSS Distributions if (theKext->isCPPInitialized()) {
448*1b191cb5SApple OSS Distributions result = KMOD_RETURN_SUCCESS;
449*1b191cb5SApple OSS Distributions goto finish;
450*1b191cb5SApple OSS Distributions }
451*1b191cb5SApple OSS Distributions
452*1b191cb5SApple OSS Distributions kmodInfo = theKext->kmod_info;
453*1b191cb5SApple OSS Distributions if (!kmodInfo || !kmodInfo->address) {
454*1b191cb5SApple OSS Distributions result = kOSKextReturnInvalidArgument;
455*1b191cb5SApple OSS Distributions goto finish;
456*1b191cb5SApple OSS Distributions }
457*1b191cb5SApple OSS Distributions header = (kernel_mach_header_t *)kmodInfo->address;
458*1b191cb5SApple OSS Distributions
459*1b191cb5SApple OSS Distributions if (theKext->flags.builtin) {
460*1b191cb5SApple OSS Distributions header = (kernel_mach_header_t *)g_kernel_kmod_info.address;
461*1b191cb5SApple OSS Distributions textStart = kmodInfo->address;
462*1b191cb5SApple OSS Distributions textEnd = textStart + kmodInfo->size;
463*1b191cb5SApple OSS Distributions sectionNames = gOSStructorSectionNames[kOSSectionNamesBuiltinKext];
464*1b191cb5SApple OSS Distributions }
465*1b191cb5SApple OSS Distributions } else {
466*1b191cb5SApple OSS Distributions kmodInfo = &g_kernel_kmod_info;
467*1b191cb5SApple OSS Distributions header = (kernel_mach_header_t *)kmodInfo->address;
468*1b191cb5SApple OSS Distributions }
469*1b191cb5SApple OSS Distributions
470*1b191cb5SApple OSS Distributions /* Tell the meta class system that we are starting the load
471*1b191cb5SApple OSS Distributions */
472*1b191cb5SApple OSS Distributions metaHandle = OSMetaClass::preModLoad(kmodInfo->name);
473*1b191cb5SApple OSS Distributions assert(metaHandle);
474*1b191cb5SApple OSS Distributions if (!metaHandle) {
475*1b191cb5SApple OSS Distributions goto finish;
476*1b191cb5SApple OSS Distributions }
477*1b191cb5SApple OSS Distributions
478*1b191cb5SApple OSS Distributions /* NO GOTO PAST HERE. */
479*1b191cb5SApple OSS Distributions
480*1b191cb5SApple OSS Distributions /* Scan the header for all constructor sections, in any
481*1b191cb5SApple OSS Distributions * segment, and invoke the constructors within those sections.
482*1b191cb5SApple OSS Distributions */
483*1b191cb5SApple OSS Distributions for (segment = firstsegfromheader(header);
484*1b191cb5SApple OSS Distributions segment != NULL && load_success;
485*1b191cb5SApple OSS Distributions segment = nextsegfromheader(header, segment)) {
486*1b191cb5SApple OSS Distributions /* Record the current segment in the event of a failure.
487*1b191cb5SApple OSS Distributions */
488*1b191cb5SApple OSS Distributions failure_segment = segment;
489*1b191cb5SApple OSS Distributions load_success = OSRuntimeCallStructorsInSection(
490*1b191cb5SApple OSS Distributions theKext, kmodInfo, metaHandle, segment,
491*1b191cb5SApple OSS Distributions sectionNames[kOSSectionNameInitializer],
492*1b191cb5SApple OSS Distributions textStart, textEnd);
493*1b191cb5SApple OSS Distributions } /* for (segment...) */
494*1b191cb5SApple OSS Distributions
495*1b191cb5SApple OSS Distributions /* We failed so call all of the destructors. We must do this before
496*1b191cb5SApple OSS Distributions * calling OSMetaClass::postModLoad() as the OSMetaClass destructors
497*1b191cb5SApple OSS Distributions * will alter state (in the metaHandle) used by that function.
498*1b191cb5SApple OSS Distributions */
499*1b191cb5SApple OSS Distributions if (!load_success) {
500*1b191cb5SApple OSS Distributions /* Scan the header for all destructor sections, in any
501*1b191cb5SApple OSS Distributions * segment, and invoke the constructors within those sections.
502*1b191cb5SApple OSS Distributions */
503*1b191cb5SApple OSS Distributions for (segment = firstsegfromheader(header);
504*1b191cb5SApple OSS Distributions segment != failure_segment && segment != NULL;
505*1b191cb5SApple OSS Distributions segment = nextsegfromheader(header, segment)) {
506*1b191cb5SApple OSS Distributions OSRuntimeCallStructorsInSection(theKext, kmodInfo, NULL, segment,
507*1b191cb5SApple OSS Distributions sectionNames[kOSSectionNameFinalizer], textStart, textEnd);
508*1b191cb5SApple OSS Distributions } /* for (segment...) */
509*1b191cb5SApple OSS Distributions }
510*1b191cb5SApple OSS Distributions
511*1b191cb5SApple OSS Distributions /* Now, regardless of success so far, do the post-init registration
512*1b191cb5SApple OSS Distributions * and cleanup. If we had to call the unloadCPP function, static
513*1b191cb5SApple OSS Distributions * destructors have removed classes from the stalled list so no
514*1b191cb5SApple OSS Distributions * metaclasses will actually be registered.
515*1b191cb5SApple OSS Distributions */
516*1b191cb5SApple OSS Distributions result = OSMetaClass::postModLoad(metaHandle);
517*1b191cb5SApple OSS Distributions
518*1b191cb5SApple OSS Distributions /* If we've otherwise been fine up to now, but OSMetaClass::postModLoad()
519*1b191cb5SApple OSS Distributions * fails (typically due to a duplicate class), tear down all the C++
520*1b191cb5SApple OSS Distributions * stuff from the kext. This isn't necessary for libkern/OSMetaClass stuff,
521*1b191cb5SApple OSS Distributions * but may be necessary for other C++ code. We ignore the return value
522*1b191cb5SApple OSS Distributions * because it's only a fail when there are existing instances of libkern
523*1b191cb5SApple OSS Distributions * classes, and there had better not be any created on the C++ init path.
524*1b191cb5SApple OSS Distributions */
525*1b191cb5SApple OSS Distributions if (load_success && result != KMOD_RETURN_SUCCESS) {
526*1b191cb5SApple OSS Distributions (void)OSRuntimeFinalizeCPP(theKext); //kmodInfo, sectionNames, textStart, textEnd);
527*1b191cb5SApple OSS Distributions }
528*1b191cb5SApple OSS Distributions
529*1b191cb5SApple OSS Distributions if (theKext && load_success && result == KMOD_RETURN_SUCCESS) {
530*1b191cb5SApple OSS Distributions theKext->setCPPInitialized(true);
531*1b191cb5SApple OSS Distributions }
532*1b191cb5SApple OSS Distributions finish:
533*1b191cb5SApple OSS Distributions return result;
534*1b191cb5SApple OSS Distributions }
535*1b191cb5SApple OSS Distributions
536*1b191cb5SApple OSS Distributions /*********************************************************************
537*1b191cb5SApple OSS Distributions * Unload a kernel segment.
538*1b191cb5SApple OSS Distributions *********************************************************************/
539*1b191cb5SApple OSS Distributions
540*1b191cb5SApple OSS Distributions void
OSRuntimeUnloadCPPForSegment(kernel_segment_command_t * segment)541*1b191cb5SApple OSS Distributions OSRuntimeUnloadCPPForSegment(
542*1b191cb5SApple OSS Distributions kernel_segment_command_t * segment)
543*1b191cb5SApple OSS Distributions {
544*1b191cb5SApple OSS Distributions OSRuntimeCallStructorsInSection(NULL, &g_kernel_kmod_info, NULL, segment,
545*1b191cb5SApple OSS Distributions gOSStructorSectionNames[kOSSectionNamesDefault][kOSSectionNameFinalizer], 0, 0);
546*1b191cb5SApple OSS Distributions }
547*1b191cb5SApple OSS Distributions
548*1b191cb5SApple OSS Distributions #if PRAGMA_MARK
549*1b191cb5SApple OSS Distributions #pragma mark C++ Allocators & Deallocators
550*1b191cb5SApple OSS Distributions #endif /* PRAGMA_MARK */
551*1b191cb5SApple OSS Distributions /*********************************************************************
552*1b191cb5SApple OSS Distributions * C++ Allocators & Deallocators
553*1b191cb5SApple OSS Distributions *********************************************************************/
554*1b191cb5SApple OSS Distributions __typed_allocators_ignore_push
555*1b191cb5SApple OSS Distributions
556*1b191cb5SApple OSS Distributions void *
operator new(size_t size)557*1b191cb5SApple OSS Distributions operator new(size_t size)
558*1b191cb5SApple OSS Distributions {
559*1b191cb5SApple OSS Distributions assert(size);
560*1b191cb5SApple OSS Distributions return kheap_alloc(KERN_OS_MALLOC, size,
561*1b191cb5SApple OSS Distributions Z_VM_TAG_BT(Z_WAITOK_ZERO, VM_KERN_MEMORY_LIBKERN));
562*1b191cb5SApple OSS Distributions }
563*1b191cb5SApple OSS Distributions
564*1b191cb5SApple OSS Distributions void
operator delete(void * addr)565*1b191cb5SApple OSS Distributions operator delete(void * addr)
566*1b191cb5SApple OSS Distributions #if __cplusplus >= 201103L
567*1b191cb5SApple OSS Distributions noexcept
568*1b191cb5SApple OSS Distributions #endif
569*1b191cb5SApple OSS Distributions {
570*1b191cb5SApple OSS Distributions kheap_free_addr(KERN_OS_MALLOC, addr);
571*1b191cb5SApple OSS Distributions return;
572*1b191cb5SApple OSS Distributions }
573*1b191cb5SApple OSS Distributions
574*1b191cb5SApple OSS Distributions void *
operator new[](unsigned long size)575*1b191cb5SApple OSS Distributions operator new[](unsigned long size)
576*1b191cb5SApple OSS Distributions {
577*1b191cb5SApple OSS Distributions return kheap_alloc(KERN_OS_MALLOC, size,
578*1b191cb5SApple OSS Distributions Z_VM_TAG_BT(Z_WAITOK_ZERO, VM_KERN_MEMORY_LIBKERN));
579*1b191cb5SApple OSS Distributions }
580*1b191cb5SApple OSS Distributions
581*1b191cb5SApple OSS Distributions void
operator delete[](void * ptr)582*1b191cb5SApple OSS Distributions operator delete[](void * ptr)
583*1b191cb5SApple OSS Distributions #if __cplusplus >= 201103L
584*1b191cb5SApple OSS Distributions noexcept
585*1b191cb5SApple OSS Distributions #endif
586*1b191cb5SApple OSS Distributions {
587*1b191cb5SApple OSS Distributions if (ptr) {
588*1b191cb5SApple OSS Distributions #if KASAN
589*1b191cb5SApple OSS Distributions /*
590*1b191cb5SApple OSS Distributions * Unpoison the C++ array cookie inserted (but not removed) by the
591*1b191cb5SApple OSS Distributions * compiler on new[].
592*1b191cb5SApple OSS Distributions */
593*1b191cb5SApple OSS Distributions kasan_unpoison_cxx_array_cookie(ptr);
594*1b191cb5SApple OSS Distributions #endif
595*1b191cb5SApple OSS Distributions kheap_free_addr(KERN_OS_MALLOC, ptr);
596*1b191cb5SApple OSS Distributions }
597*1b191cb5SApple OSS Distributions return;
598*1b191cb5SApple OSS Distributions }
599*1b191cb5SApple OSS Distributions
600*1b191cb5SApple OSS Distributions #if __cplusplus >= 201103L
601*1b191cb5SApple OSS Distributions
602*1b191cb5SApple OSS Distributions void
operator delete(void * addr,size_t sz)603*1b191cb5SApple OSS Distributions operator delete(void * addr, size_t sz) noexcept
604*1b191cb5SApple OSS Distributions {
605*1b191cb5SApple OSS Distributions kheap_free(KERN_OS_MALLOC, addr, sz);
606*1b191cb5SApple OSS Distributions }
607*1b191cb5SApple OSS Distributions
608*1b191cb5SApple OSS Distributions void
operator delete[](void * addr,size_t sz)609*1b191cb5SApple OSS Distributions operator delete[](void * addr, size_t sz) noexcept
610*1b191cb5SApple OSS Distributions {
611*1b191cb5SApple OSS Distributions if (addr) {
612*1b191cb5SApple OSS Distributions kheap_free(KERN_OS_MALLOC, addr, sz);
613*1b191cb5SApple OSS Distributions }
614*1b191cb5SApple OSS Distributions }
615*1b191cb5SApple OSS Distributions
616*1b191cb5SApple OSS Distributions __typed_allocators_ignore_pop
617*1b191cb5SApple OSS Distributions
618*1b191cb5SApple OSS Distributions #endif /* __cplusplus >= 201103L */
619*1b191cb5SApple OSS Distributions
620*1b191cb5SApple OSS Distributions /* PR-6481964 - The compiler is going to check for size overflows in calls to
621*1b191cb5SApple OSS Distributions * new[], and if there is an overflow, it will call __throw_length_error.
622*1b191cb5SApple OSS Distributions * This is an unrecoverable error by the C++ standard, so we must panic here.
623*1b191cb5SApple OSS Distributions *
624*1b191cb5SApple OSS Distributions * We have to put the function inside the std namespace because of how the
625*1b191cb5SApple OSS Distributions * compiler expects the name to be mangled.
626*1b191cb5SApple OSS Distributions */
627*1b191cb5SApple OSS Distributions namespace std {
628*1b191cb5SApple OSS Distributions void __dead2
__throw_length_error(const char * msg __unused)629*1b191cb5SApple OSS Distributions __throw_length_error(const char *msg __unused)
630*1b191cb5SApple OSS Distributions {
631*1b191cb5SApple OSS Distributions panic("Size of array created by new[] has overflowed");
632*1b191cb5SApple OSS Distributions }
633*1b191cb5SApple OSS Distributions };
634