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