xref: /xnu-8020.121.3/iokit/bsddev/IOKitBSDInit.cpp (revision fdd8201d7b966f0c3ea610489d29bd841d358941)
1 /*
2  * Copyright (c) 1998-2021 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 #include <IOKit/IOBSD.h>
29 #include <IOKit/IOLib.h>
30 #include <IOKit/IOService.h>
31 #include <IOKit/IOCatalogue.h>
32 #include <IOKit/IODeviceTreeSupport.h>
33 #include <IOKit/IOKitKeys.h>
34 #include <IOKit/IONVRAM.h>
35 #include <IOKit/IOPlatformExpert.h>
36 #include <IOKit/IOUserClient.h>
37 #include <libkern/c++/OSAllocation.h>
38 
39 extern "C" {
40 #include <libkern/amfi/amfi.h>
41 #include <sys/codesign.h>
42 #include <vm/pmap.h>
43 #include <vm/vm_map.h>
44 #include <pexpert/pexpert.h>
45 #include <kern/clock.h>
46 #if CONFIG_KDP_INTERACTIVE_DEBUGGING
47 #include <kern/debug.h>
48 #endif
49 #include <mach/machine.h>
50 #include <uuid/uuid.h>
51 #include <sys/vnode_internal.h>
52 #include <sys/mount.h>
53 
54 // how long to wait for matching root device, secs
55 #if DEBUG
56 #define ROOTDEVICETIMEOUT       120
57 #else
58 #define ROOTDEVICETIMEOUT       60
59 #endif
60 
61 extern dev_t mdevadd(int devid, uint64_t base, unsigned int size, int phys);
62 extern dev_t mdevlookup(int devid);
63 extern void mdevremoveall(void);
64 extern int mdevgetrange(int devid, uint64_t *base, uint64_t *size);
65 extern void di_root_ramfile(IORegistryEntry * entry);
66 extern int IODTGetDefault(const char *key, void *infoAddr, unsigned int infoSize);
67 extern boolean_t cpuid_vmm_present(void);
68 
69 #define ROUNDUP(a, b) (((a) + ((b) - 1)) & (~((b) - 1)))
70 
71 #define IOPOLLED_COREFILE       (CONFIG_KDP_INTERACTIVE_DEBUGGING)
72 
73 #if defined(XNU_TARGET_OS_BRIDGE)
74 #define kIOCoreDumpPath         "/private/var/internal/kernelcore"
75 #elif defined(XNU_TARGET_OS_OSX)
76 #define kIOCoreDumpPath         "/System/Volumes/VM/kernelcore"
77 #else
78 #define kIOCoreDumpPath         "/private/var/vm/kernelcore"
79 #endif
80 
81 #define SYSTEM_NVRAM_PREFIX     "40A0DDD2-77F8-4392-B4A3-1E7304206516:"
82 
83 #if CONFIG_KDP_INTERACTIVE_DEBUGGING
84 /*
85  * Touched by IOFindBSDRoot() if a RAMDisk is used for the root device.
86  */
87 extern uint64_t kdp_core_ramdisk_addr;
88 extern uint64_t kdp_core_ramdisk_size;
89 
90 /*
91  * A callback to indicate that the polled-mode corefile is now available.
92  */
93 extern kern_return_t kdp_core_polled_io_polled_file_available(IOCoreFileAccessCallback access_data, void *access_context, void *recipient_context);
94 
95 /*
96  * A callback to indicate that the polled-mode corefile is no longer available.
97  */
98 extern kern_return_t kdp_core_polled_io_polled_file_unavailable(void);
99 #endif
100 
101 #if IOPOLLED_COREFILE
102 static void IOOpenPolledCoreFile(thread_call_param_t __unused, thread_call_param_t corefilename);
103 
104 thread_call_t corefile_open_call = NULL;
105 #endif
106 
107 kern_return_t
IOKitBSDInit(void)108 IOKitBSDInit( void )
109 {
110 	IOService::publishResource("IOBSD");
111 
112 #if IOPOLLED_COREFILE
113 	corefile_open_call = thread_call_allocate_with_options(IOOpenPolledCoreFile, NULL, THREAD_CALL_PRIORITY_KERNEL, THREAD_CALL_OPTIONS_ONCE);
114 #endif
115 
116 	return kIOReturnSuccess;
117 }
118 
119 void
IOServicePublishResource(const char * property,boolean_t value)120 IOServicePublishResource( const char * property, boolean_t value )
121 {
122 	if (value) {
123 		IOService::publishResource( property, kOSBooleanTrue );
124 	} else {
125 		IOService::getResourceService()->removeProperty( property );
126 	}
127 }
128 
129 boolean_t
IOServiceWaitForMatchingResource(const char * property,uint64_t timeout)130 IOServiceWaitForMatchingResource( const char * property, uint64_t timeout )
131 {
132 	OSDictionary *      dict = NULL;
133 	IOService *         match = NULL;
134 	boolean_t           found = false;
135 
136 	do {
137 		dict = IOService::resourceMatching( property );
138 		if (!dict) {
139 			continue;
140 		}
141 		match = IOService::waitForMatchingService( dict, timeout );
142 		if (match) {
143 			found = true;
144 		}
145 	} while (false);
146 
147 	if (dict) {
148 		dict->release();
149 	}
150 	if (match) {
151 		match->release();
152 	}
153 
154 	return found;
155 }
156 
157 boolean_t
IOCatalogueMatchingDriversPresent(const char * property)158 IOCatalogueMatchingDriversPresent( const char * property )
159 {
160 	OSDictionary *      dict = NULL;
161 	OSOrderedSet *      set = NULL;
162 	SInt32              generationCount = 0;
163 	boolean_t           found = false;
164 
165 	do {
166 		dict = OSDictionary::withCapacity(1);
167 		if (!dict) {
168 			continue;
169 		}
170 		dict->setObject( property, kOSBooleanTrue );
171 		set = gIOCatalogue->findDrivers( dict, &generationCount );
172 		if (set && (set->getCount() > 0)) {
173 			found = true;
174 		}
175 	} while (false);
176 
177 	if (dict) {
178 		dict->release();
179 	}
180 	if (set) {
181 		set->release();
182 	}
183 
184 	return found;
185 }
186 
187 OSDictionary *
IOBSDNameMatching(const char * name)188 IOBSDNameMatching( const char * name )
189 {
190 	OSDictionary *      dict;
191 	const OSSymbol *    str = NULL;
192 
193 	do {
194 		dict = IOService::serviceMatching( gIOServiceKey );
195 		if (!dict) {
196 			continue;
197 		}
198 		str = OSSymbol::withCString( name );
199 		if (!str) {
200 			continue;
201 		}
202 		dict->setObject( kIOBSDNameKey, (OSObject *) str );
203 		str->release();
204 
205 		return dict;
206 	} while (false);
207 
208 	if (dict) {
209 		dict->release();
210 	}
211 	if (str) {
212 		str->release();
213 	}
214 
215 	return NULL;
216 }
217 
218 OSDictionary *
IOUUIDMatching(void)219 IOUUIDMatching( void )
220 {
221 	return IOService::resourceMatching( "boot-uuid-media" );
222 }
223 
224 OSDictionary *
IONetworkNamePrefixMatching(const char * prefix)225 IONetworkNamePrefixMatching( const char * prefix )
226 {
227 	OSDictionary *       matching;
228 	OSDictionary *   propDict = NULL;
229 	const OSSymbol * str      = NULL;
230 	char networkType[128];
231 
232 	do {
233 		matching = IOService::serviceMatching( "IONetworkInterface" );
234 		if (matching == NULL) {
235 			continue;
236 		}
237 
238 		propDict = OSDictionary::withCapacity(1);
239 		if (propDict == NULL) {
240 			continue;
241 		}
242 
243 		str = OSSymbol::withCString( prefix );
244 		if (str == NULL) {
245 			continue;
246 		}
247 
248 		propDict->setObject( "IOInterfaceNamePrefix", (OSObject *) str );
249 		str->release();
250 		str = NULL;
251 
252 		// see if we're contrained to netroot off of specific network type
253 		if (PE_parse_boot_argn( "network-type", networkType, 128 )) {
254 			str = OSSymbol::withCString( networkType );
255 			if (str) {
256 				propDict->setObject( "IONetworkRootType", str);
257 				str->release();
258 				str = NULL;
259 			}
260 		}
261 
262 		if (matching->setObject( gIOPropertyMatchKey,
263 		    (OSObject *) propDict ) != true) {
264 			continue;
265 		}
266 
267 		propDict->release();
268 		propDict = NULL;
269 
270 		return matching;
271 	} while (false);
272 
273 	if (matching) {
274 		matching->release();
275 	}
276 	if (propDict) {
277 		propDict->release();
278 	}
279 	if (str) {
280 		str->release();
281 	}
282 
283 	return NULL;
284 }
285 
286 static bool
IORegisterNetworkInterface(IOService * netif)287 IORegisterNetworkInterface( IOService * netif )
288 {
289 	// A network interface is typically named and registered
290 	// with BSD after receiving a request from a user space
291 	// "namer". However, for cases when the system needs to
292 	// root from the network, this registration task must be
293 	// done inside the kernel and completed before the root
294 	// device is handed to BSD.
295 
296 	IOService *    stack;
297 	OSNumber *     zero    = NULL;
298 	OSString *     path    = NULL;
299 	OSDictionary * dict    = NULL;
300 	OSDataAllocation<char> pathBuf;
301 	int            len;
302 	enum { kMaxPathLen = 512 };
303 
304 	do {
305 		stack = IOService::waitForService(
306 			IOService::serviceMatching("IONetworkStack"));
307 		if (stack == NULL) {
308 			break;
309 		}
310 
311 		dict = OSDictionary::withCapacity(3);
312 		if (dict == NULL) {
313 			break;
314 		}
315 
316 		zero = OSNumber::withNumber((UInt64) 0, 32);
317 		if (zero == NULL) {
318 			break;
319 		}
320 
321 		pathBuf = OSDataAllocation<char>( kMaxPathLen, OSAllocateMemory );
322 		if (!pathBuf) {
323 			break;
324 		}
325 
326 		len = kMaxPathLen;
327 		if (netif->getPath( pathBuf.data(), &len, gIOServicePlane )
328 		    == false) {
329 			break;
330 		}
331 
332 		path = OSString::withCStringNoCopy(pathBuf.data());
333 		if (path == NULL) {
334 			break;
335 		}
336 
337 		dict->setObject( "IOInterfaceUnit", zero );
338 		dict->setObject( kIOPathMatchKey, path );
339 
340 		stack->setProperties( dict );
341 	}while (false);
342 
343 	if (zero) {
344 		zero->release();
345 	}
346 	if (path) {
347 		path->release();
348 	}
349 	if (dict) {
350 		dict->release();
351 	}
352 
353 	return netif->getProperty( kIOBSDNameKey ) != NULL;
354 }
355 
356 OSDictionary *
IOOFPathMatching(const char * path,char * buf,int maxLen)357 IOOFPathMatching( const char * path, char * buf, int maxLen )
358 {
359 	OSDictionary *      matching = NULL;
360 	OSString *          str;
361 	char *              comp;
362 	int                 len;
363 
364 	do {
365 		len = ((int) strlen( kIODeviceTreePlane ":" ));
366 		maxLen -= len;
367 		if (maxLen <= 0) {
368 			continue;
369 		}
370 
371 		strlcpy( buf, kIODeviceTreePlane ":", len + 1 );
372 		comp = buf + len;
373 
374 		len = ((int) strnlen( path, INT_MAX ));
375 		maxLen -= len;
376 		if (maxLen <= 0) {
377 			continue;
378 		}
379 		strlcpy( comp, path, len + 1 );
380 
381 		matching = OSDictionary::withCapacity( 1 );
382 		if (!matching) {
383 			continue;
384 		}
385 
386 		str = OSString::withCString( buf );
387 		if (!str) {
388 			continue;
389 		}
390 		matching->setObject( kIOPathMatchKey, str );
391 		str->release();
392 
393 		return matching;
394 	} while (false);
395 
396 	if (matching) {
397 		matching->release();
398 	}
399 
400 	return NULL;
401 }
402 
403 static int didRam = 0;
404 enum { kMaxPathBuf = 512, kMaxBootVar = 128 };
405 
406 bool
IOGetBootUUID(char * uuid)407 IOGetBootUUID(char *uuid)
408 {
409 	IORegistryEntry *entry;
410 	OSData *uuid_data = NULL;
411 	bool result = false;
412 
413 	if ((entry = IORegistryEntry::fromPath("/chosen", gIODTPlane))) {
414 		uuid_data = (OSData *)entry->getProperty("boot-uuid");
415 		if (uuid_data) {
416 			unsigned int length = uuid_data->getLength();
417 			if (length <= sizeof(uuid_string_t)) {
418 				/* ensure caller's buffer is fully initialized: */
419 				bzero(uuid, sizeof(uuid_string_t));
420 				/* copy the content of uuid_data->getBytesNoCopy() into uuid */
421 				memcpy(uuid, uuid_data->getBytesNoCopy(), length);
422 				/* guarantee nul-termination: */
423 				uuid[sizeof(uuid_string_t) - 1] = '\0';
424 				result = true;
425 			} else {
426 				uuid = NULL;
427 			}
428 		}
429 		OSSafeReleaseNULL(entry);
430 	}
431 	return result;
432 }
433 
434 bool
IOGetApfsPrebootUUID(char * uuid)435 IOGetApfsPrebootUUID(char *uuid)
436 {
437 	IORegistryEntry *entry;
438 	OSData *uuid_data = NULL;
439 	bool result = false;
440 
441 	if ((entry = IORegistryEntry::fromPath("/chosen", gIODTPlane))) {
442 		uuid_data = (OSData *)entry->getProperty("apfs-preboot-uuid");
443 
444 		if (uuid_data) {
445 			unsigned int length = uuid_data->getLength();
446 			if (length <= sizeof(uuid_string_t)) {
447 				/* ensure caller's buffer is fully initialized: */
448 				bzero(uuid, sizeof(uuid_string_t));
449 				/* copy the content of uuid_data->getBytesNoCopy() into uuid */
450 				memcpy(uuid, uuid_data->getBytesNoCopy(), length);
451 				/* guarantee nul-termination: */
452 				uuid[sizeof(uuid_string_t) - 1] = '\0';
453 				result = true;
454 			} else {
455 				uuid = NULL;
456 			}
457 		}
458 		OSSafeReleaseNULL(entry);
459 	}
460 	return result;
461 }
462 
463 bool
IOGetAssociatedApfsVolgroupUUID(char * uuid)464 IOGetAssociatedApfsVolgroupUUID(char *uuid)
465 {
466 	IORegistryEntry *entry;
467 	OSData *uuid_data = NULL;
468 	bool result = false;
469 
470 	if ((entry = IORegistryEntry::fromPath("/chosen", gIODTPlane))) {
471 		uuid_data = (OSData *)entry->getProperty("associated-volume-group");
472 
473 		if (uuid_data) {
474 			unsigned int length = uuid_data->getLength();
475 
476 			if (length <= sizeof(uuid_string_t)) {
477 				/* ensure caller's buffer is fully initialized: */
478 				bzero(uuid, sizeof(uuid_string_t));
479 				/* copy the content of uuid_data->getBytesNoCopy() into uuid */
480 				memcpy(uuid, uuid_data->getBytesNoCopy(), length);
481 				/* guarantee nul-termination: */
482 				uuid[sizeof(uuid_string_t) - 1] = '\0';
483 				result = true;
484 			} else {
485 				uuid = NULL;
486 			}
487 		}
488 		OSSafeReleaseNULL(entry);
489 	}
490 	return result;
491 }
492 
493 bool
IOGetBootObjectsPath(char * path_prefix)494 IOGetBootObjectsPath(char *path_prefix)
495 {
496 	IORegistryEntry *entry;
497 	OSData *path_prefix_data = NULL;
498 	bool result = false;
499 
500 	if ((entry = IORegistryEntry::fromPath("/chosen", gIODTPlane))) {
501 		path_prefix_data = (OSData *)entry->getProperty("boot-objects-path");
502 
503 		if (path_prefix_data) {
504 			unsigned int length = path_prefix_data->getLength();
505 
506 			if (length <= MAXPATHLEN) {
507 				/* ensure caller's buffer is fully initialized: */
508 				bzero(path_prefix, MAXPATHLEN);
509 				/* copy the content of path_prefix_data->getBytesNoCopy() into path_prefix */
510 				memcpy(path_prefix, path_prefix_data->getBytesNoCopy(), length);
511 				/* guarantee nul-termination: */
512 				path_prefix[MAXPATHLEN - 1] = '\0';
513 				result = true;
514 			} else {
515 				path_prefix = NULL;
516 			}
517 		}
518 		OSSafeReleaseNULL(entry);
519 	}
520 	return result;
521 }
522 
523 /*
524  * Set NVRAM to boot into the right flavor of Recovery,
525  * optionally passing a UUID of a volume that failed to boot.
526  * If `reboot` is true, reboot immediately.
527  *
528  * Returns true if `mode` was understood, false otherwise.
529  * (Does not return if `reboot` is true.)
530  */
531 boolean_t
IOSetRecoveryBoot(bsd_bootfail_mode_t mode,uuid_t volume_uuid,boolean_t reboot)532 IOSetRecoveryBoot(bsd_bootfail_mode_t mode, uuid_t volume_uuid, boolean_t reboot)
533 {
534 	IODTNVRAM *nvram = NULL;
535 	const OSSymbol *boot_command_sym = NULL;
536 	OSString *boot_command_recover = NULL;
537 
538 	if (mode == BSD_BOOTFAIL_SEAL_BROKEN) {
539 		const char *boot_mode = "ssv-seal-broken";
540 		uuid_string_t volume_uuid_str;
541 
542 		// Set `recovery-broken-seal-uuid = <volume_uuid>`.
543 		if (volume_uuid) {
544 			uuid_unparse_upper(volume_uuid, volume_uuid_str);
545 
546 			if (!PEWriteNVRAMProperty(SYSTEM_NVRAM_PREFIX "recovery-broken-seal-uuid",
547 			    volume_uuid_str, sizeof(uuid_string_t))) {
548 				IOLog("Failed to write recovery-broken-seal-uuid to NVRAM.\n");
549 			}
550 		}
551 
552 		// Set `recovery-boot-mode = ssv-seal-broken`.
553 		if (!PEWriteNVRAMProperty(SYSTEM_NVRAM_PREFIX "recovery-boot-mode", boot_mode,
554 		    (const unsigned int) strlen(boot_mode))) {
555 			IOLog("Failed to write recovery-boot-mode to NVRAM.\n");
556 		}
557 	} else if (mode == BSD_BOOTFAIL_MEDIA_MISSING) {
558 		const char *boot_picker_reason = "missing-boot-media";
559 
560 		// Set `boot-picker-bringup-reason = missing-boot-media`.
561 		if (!PEWriteNVRAMProperty(SYSTEM_NVRAM_PREFIX "boot-picker-bringup-reason",
562 		    boot_picker_reason, (const unsigned int) strlen(boot_picker_reason))) {
563 			IOLog("Failed to write boot-picker-bringup-reason to NVRAM.\n");
564 		}
565 
566 		// Set `boot-command = recover-system`.
567 
568 		// Construct an OSSymbol and an OSString to be the (key, value) pair
569 		// we write to NVRAM. Unfortunately, since our value must be an OSString
570 		// instead of an OSData, we cannot use PEWriteNVRAMProperty() here.
571 		boot_command_sym = OSSymbol::withCStringNoCopy(SYSTEM_NVRAM_PREFIX "boot-command");
572 		boot_command_recover = OSString::withCStringNoCopy("recover-system");
573 		if (boot_command_sym == NULL || boot_command_recover == NULL) {
574 			IOLog("Failed to create boot-command strings.\n");
575 			goto do_reboot;
576 		}
577 
578 		// Wait for NVRAM to be readable...
579 		nvram = OSDynamicCast(IODTNVRAM, IOService::waitForService(
580 			    IOService::serviceMatching("IODTNVRAM")));
581 		if (nvram == NULL) {
582 			IOLog("Failed to acquire IODTNVRAM object.\n");
583 			goto do_reboot;
584 		}
585 
586 		// Wait for NVRAM to be writable...
587 		if (!IOServiceWaitForMatchingResource("IONVRAM", UINT64_MAX)) {
588 			IOLog("Failed to wait for IONVRAM service.\n");
589 			// attempt the work anyway...
590 		}
591 
592 		// Write the new boot-command to NVRAM, and sync if successful.
593 		if (!nvram->setProperty(boot_command_sym, boot_command_recover)) {
594 			IOLog("Failed to save new boot-command to NVRAM.\n");
595 		} else {
596 			nvram->sync();
597 		}
598 	} else {
599 		IOLog("Unknown mode: %d\n", mode);
600 		return false;
601 	}
602 
603 	// Clean up and reboot!
604 do_reboot:
605 	if (boot_command_recover != NULL) {
606 		boot_command_recover->release();
607 	}
608 
609 	if (boot_command_sym != NULL) {
610 		boot_command_sym->release();
611 	}
612 
613 	if (reboot) {
614 		IOLog("\nAbout to reboot into Recovery!\n");
615 		(void)PEHaltRestart(kPERestartCPU);
616 	}
617 
618 	return true;
619 }
620 
621 int
IOGetVMMPresent(void)622 IOGetVMMPresent(void)
623 {
624 	int hv_vmm_present = 0;
625 
626 #if defined(__arm64__)
627 	if (IODTGetDefault("vmm-present", &hv_vmm_present, sizeof(hv_vmm_present)) < 0) {
628 		return 0;
629 	}
630 
631 	if (hv_vmm_present != 0) {
632 		hv_vmm_present = 1;
633 	}
634 #elif defined(__x86_64__)
635 	hv_vmm_present = cpuid_vmm_present();
636 #endif
637 
638 	return hv_vmm_present;
639 }
640 
641 kern_return_t
IOFindBSDRoot(char * rootName,unsigned int rootNameSize,dev_t * root,u_int32_t * oflags)642 IOFindBSDRoot( char * rootName, unsigned int rootNameSize,
643     dev_t * root, u_int32_t * oflags )
644 {
645 	mach_timespec_t     t;
646 	IOService *         service;
647 	IORegistryEntry *   regEntry;
648 	OSDictionary *      matching = NULL;
649 	OSString *          iostr;
650 	OSNumber *          off;
651 	OSData *            data = NULL;
652 
653 	UInt32              flags = 0;
654 	int                 mnr, mjr;
655 	const char *        mediaProperty = NULL;
656 	char *              rdBootVar;
657 	OSDataAllocation<char> str;
658 	const char *        look = NULL;
659 	int                 len;
660 	bool                debugInfoPrintedOnce = false;
661 	bool                needNetworkKexts = false;
662 	const char *        uuidStr = NULL;
663 
664 	static int          mountAttempts = 0;
665 
666 	int xchar, dchar;
667 
668 	// stall here for anyone matching on the IOBSD resource to finish (filesystems)
669 	matching = IOService::serviceMatching(gIOResourcesKey);
670 	assert(matching);
671 	matching->setObject(gIOResourceMatchedKey, gIOBSDKey);
672 
673 	if ((service = IOService::waitForMatchingService(matching, 30ULL * kSecondScale))) {
674 		OSSafeReleaseNULL(service);
675 	} else {
676 		IOLog("!BSD\n");
677 	}
678 	matching->release();
679 	matching = NULL;
680 
681 	if (mountAttempts++) {
682 		IOLog("mount(%d) failed\n", mountAttempts);
683 		IOSleep( 5 * 1000 );
684 	}
685 
686 	str = OSDataAllocation<char>( kMaxPathBuf + kMaxBootVar, OSAllocateMemory );
687 	if (!str) {
688 		return kIOReturnNoMemory;
689 	}
690 	rdBootVar = str.data() + kMaxPathBuf;
691 
692 	if (!PE_parse_boot_argn("rd", rdBootVar, kMaxBootVar )
693 	    && !PE_parse_boot_argn("rootdev", rdBootVar, kMaxBootVar )) {
694 		rdBootVar[0] = 0;
695 	}
696 
697 	if ((regEntry = IORegistryEntry::fromPath( "/chosen", gIODTPlane ))) {
698 		do {
699 			di_root_ramfile(regEntry);
700 			OSObject* unserializedContainer = NULL;
701 			data = OSDynamicCast(OSData, regEntry->getProperty( "root-matching" ));
702 			if (data) {
703 				unserializedContainer = OSUnserializeXML((char *)data->getBytesNoCopy());
704 				matching = OSDynamicCast(OSDictionary, unserializedContainer);
705 				if (matching) {
706 					continue;
707 				}
708 			}
709 			OSSafeReleaseNULL(unserializedContainer);
710 
711 			data = (OSData *) regEntry->getProperty( "boot-uuid" );
712 			if (data) {
713 				uuidStr = (const char*)data->getBytesNoCopy();
714 				OSString *uuidString = OSString::withCString( uuidStr );
715 
716 				// match the boot-args boot-uuid processing below
717 				if (uuidString) {
718 					IOLog("rooting via boot-uuid from /chosen: %s\n", uuidStr);
719 					IOService::publishResource( "boot-uuid", uuidString );
720 					uuidString->release();
721 					matching = IOUUIDMatching();
722 					mediaProperty = "boot-uuid-media";
723 					continue;
724 				} else {
725 					uuidStr = NULL;
726 				}
727 			}
728 		} while (false);
729 		OSSafeReleaseNULL(regEntry);
730 	}
731 
732 //
733 //	See if we have a RAMDisk property in /chosen/memory-map.  If so, make it into a device.
734 //	It will become /dev/mdx, where x is 0-f.
735 //
736 
737 	if (!didRam) {                                                                                           /* Have we already build this ram disk? */
738 		didRam = 1;                                                                                             /* Remember we did this */
739 		if ((regEntry = IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane ))) {        /* Find the map node */
740 			data = (OSData *)regEntry->getProperty("RAMDisk");      /* Find the ram disk, if there */
741 			if (data) {                                                                                      /* We found one */
742 				uintptr_t *ramdParms;
743 				ramdParms = (uintptr_t *)data->getBytesNoCopy();        /* Point to the ram disk base and size */
744 #if __LP64__
745 #define MAX_PHYS_RAM    (((uint64_t)UINT_MAX) << 12)
746 				if (ramdParms[1] > MAX_PHYS_RAM) {
747 					panic("ramdisk params");
748 				}
749 #endif /* __LP64__ */
750 				(void)mdevadd(-1, ml_static_ptovirt(ramdParms[0]) >> 12, (unsigned int) (ramdParms[1] >> 12), 0);        /* Initialize it and pass back the device number */
751 			}
752 			regEntry->release();                                                            /* Toss the entry */
753 		}
754 	}
755 
756 //
757 //	Now check if we are trying to root on a memory device
758 //
759 
760 	if ((rdBootVar[0] == 'm') && (rdBootVar[1] == 'd') && (rdBootVar[3] == 0)) {
761 		dchar = xchar = rdBootVar[2];                                                   /* Get the actual device */
762 		if ((xchar >= '0') && (xchar <= '9')) {
763 			xchar = xchar - '0';                                    /* If digit, convert */
764 		} else {
765 			xchar = xchar & ~' ';                                                           /* Fold to upper case */
766 			if ((xchar >= 'A') && (xchar <= 'F')) {                          /* Is this a valid digit? */
767 				xchar = (xchar & 0xF) + 9;                                              /* Convert the hex digit */
768 				dchar = dchar | ' ';                                                    /* Fold to lower case */
769 			} else {
770 				xchar = -1;                                                                     /* Show bogus */
771 			}
772 		}
773 		if (xchar >= 0) {                                                                                /* Do we have a valid memory device name? */
774 			OSSafeReleaseNULL(matching);
775 			*root = mdevlookup(xchar);                                                      /* Find the device number */
776 			if (*root >= 0) {                                                                        /* Did we find one? */
777 				rootName[0] = 'm';                                                              /* Build root name */
778 				rootName[1] = 'd';                                                              /* Build root name */
779 				rootName[2] = (char) dchar;                                                     /* Build root name */
780 				rootName[3] = 0;                                                                /* Build root name */
781 				IOLog("BSD root: %s, major %d, minor %d\n", rootName, major(*root), minor(*root));
782 				*oflags = 0;                                                                    /* Show that this is not network */
783 
784 #if CONFIG_KDP_INTERACTIVE_DEBUGGING
785 				/* retrieve final ramdisk range and initialize KDP variables */
786 				if (mdevgetrange(xchar, &kdp_core_ramdisk_addr, &kdp_core_ramdisk_size) != 0) {
787 					IOLog("Unable to retrieve range for root memory device %d\n", xchar);
788 					kdp_core_ramdisk_addr = 0;
789 					kdp_core_ramdisk_size = 0;
790 				}
791 #endif
792 
793 				goto iofrootx;                                                                  /* Join common exit... */
794 			}
795 			panic("IOFindBSDRoot: specified root memory device, %s, has not been configured", rdBootVar); /* Not there */
796 		}
797 	}
798 
799 	if ((!matching) && rdBootVar[0]) {
800 		// by BSD name
801 		look = rdBootVar;
802 		if (look[0] == '*') {
803 			look++;
804 		}
805 
806 		if (strncmp( look, "en", strlen( "en" )) == 0) {
807 			matching = IONetworkNamePrefixMatching( "en" );
808 			needNetworkKexts = true;
809 		} else if (strncmp( look, "uuid", strlen( "uuid" )) == 0) {
810 			OSDataAllocation<char> uuid( kMaxBootVar, OSAllocateMemory );
811 
812 			if (uuid) {
813 				OSString *uuidString;
814 
815 				if (!PE_parse_boot_argn( "boot-uuid", uuid.data(), kMaxBootVar )) {
816 					panic( "rd=uuid but no boot-uuid=<value> specified" );
817 				}
818 				uuidString = OSString::withCString(uuid.data());
819 				if (uuidString) {
820 					IOService::publishResource( "boot-uuid", uuidString );
821 					uuidString->release();
822 					IOLog("\nWaiting for boot volume with UUID %s\n", uuid.data());
823 					matching = IOUUIDMatching();
824 					mediaProperty = "boot-uuid-media";
825 				}
826 			}
827 		} else {
828 			matching = IOBSDNameMatching( look );
829 		}
830 	}
831 
832 	if (!matching) {
833 		OSString * astring;
834 		// Match any HFS media
835 
836 		matching = IOService::serviceMatching( "IOMedia" );
837 		assert(matching);
838 		astring = OSString::withCStringNoCopy("Apple_HFS");
839 		if (astring) {
840 			matching->setObject("Content", astring);
841 			astring->release();
842 		}
843 	}
844 
845 	if (gIOKitDebug & kIOWaitQuietBeforeRoot) {
846 		IOLog( "Waiting for matching to complete\n" );
847 		IOService::getPlatform()->waitQuiet();
848 	}
849 
850 	if (matching) {
851 		OSSerialize * s = OSSerialize::withCapacity( 5 );
852 
853 		if (matching->serialize( s )) {
854 			IOLog( "Waiting on %s\n", s->text());
855 		}
856 		s->release();
857 	}
858 
859 	char namep[8];
860 	if (needNetworkKexts
861 	    || PE_parse_boot_argn("-s", namep, sizeof(namep))) {
862 		IOService::startDeferredMatches();
863 	}
864 
865 	do {
866 		t.tv_sec = ROOTDEVICETIMEOUT;
867 		t.tv_nsec = 0;
868 		matching->retain();
869 		service = IOService::waitForService( matching, &t );
870 		if ((!service) || (mountAttempts == 10)) {
871 #if !XNU_TARGET_OS_OSX || !defined(__arm64__)
872 			PE_display_icon( 0, "noroot");
873 			IOLog( "Still waiting for root device\n" );
874 #endif
875 
876 			if (!debugInfoPrintedOnce) {
877 				debugInfoPrintedOnce = true;
878 				if (gIOKitDebug & kIOLogDTree) {
879 					IOLog("\nDT plane:\n");
880 					IOPrintPlane( gIODTPlane );
881 				}
882 				if (gIOKitDebug & kIOLogServiceTree) {
883 					IOLog("\nService plane:\n");
884 					IOPrintPlane( gIOServicePlane );
885 				}
886 				if (gIOKitDebug & kIOLogMemory) {
887 					IOPrintMemory();
888 				}
889 			}
890 
891 #if XNU_TARGET_OS_OSX && defined(__arm64__)
892 			// The disk isn't found - have the user pick from System Recovery.
893 			(void)IOSetRecoveryBoot(BSD_BOOTFAIL_MEDIA_MISSING, NULL, true);
894 #endif
895 		}
896 	} while (!service);
897 
898 	OSSafeReleaseNULL(matching);
899 
900 	if (service && mediaProperty) {
901 		service = (IOService *)service->getProperty(mediaProperty);
902 	}
903 
904 	mjr = 0;
905 	mnr = 0;
906 
907 	// If the IOService we matched to is a subclass of IONetworkInterface,
908 	// then make sure it has been registered with BSD and has a BSD name
909 	// assigned.
910 
911 	if (service
912 	    && service->metaCast( "IONetworkInterface" )
913 	    && !IORegisterNetworkInterface( service )) {
914 		service = NULL;
915 	}
916 
917 	if (service) {
918 		len = kMaxPathBuf;
919 		service->getPath( str.data(), &len, gIOServicePlane );
920 		IOLog("Got boot device = %s\n", str.data());
921 
922 		iostr = (OSString *) service->getProperty( kIOBSDNameKey );
923 		if (iostr) {
924 			strlcpy( rootName, iostr->getCStringNoCopy(), rootNameSize );
925 		}
926 		off = (OSNumber *) service->getProperty( kIOBSDMajorKey );
927 		if (off) {
928 			mjr = off->unsigned32BitValue();
929 		}
930 		off = (OSNumber *) service->getProperty( kIOBSDMinorKey );
931 		if (off) {
932 			mnr = off->unsigned32BitValue();
933 		}
934 
935 		if (service->metaCast( "IONetworkInterface" )) {
936 			flags |= 1;
937 		}
938 	} else {
939 		IOLog( "Wait for root failed\n" );
940 		strlcpy( rootName, "en0", rootNameSize );
941 		flags |= 1;
942 	}
943 
944 	IOLog( "BSD root: %s", rootName );
945 	if (mjr) {
946 		IOLog(", major %d, minor %d\n", mjr, mnr );
947 	} else {
948 		IOLog("\n");
949 	}
950 
951 	*root = makedev( mjr, mnr );
952 	*oflags = flags;
953 
954 iofrootx:
955 
956 	IOService::setRootMedia(service);
957 
958 	if ((gIOKitDebug & (kIOLogDTree | kIOLogServiceTree | kIOLogMemory)) && !debugInfoPrintedOnce) {
959 		IOService::getPlatform()->waitQuiet();
960 		if (gIOKitDebug & kIOLogDTree) {
961 			IOLog("\nDT plane:\n");
962 			IOPrintPlane( gIODTPlane );
963 		}
964 		if (gIOKitDebug & kIOLogServiceTree) {
965 			IOLog("\nService plane:\n");
966 			IOPrintPlane( gIOServicePlane );
967 		}
968 		if (gIOKitDebug & kIOLogMemory) {
969 			IOPrintMemory();
970 		}
971 	}
972 
973 	return kIOReturnSuccess;
974 }
975 
976 void
IOSetImageBoot(void)977 IOSetImageBoot(void)
978 {
979 	// this will unhide all IOMedia, without waiting for kernelmanagement to start
980 	IOService::setRootMedia(NULL);
981 }
982 
983 bool
IORamDiskBSDRoot(void)984 IORamDiskBSDRoot(void)
985 {
986 	char rdBootVar[kMaxBootVar];
987 	if (PE_parse_boot_argn("rd", rdBootVar, kMaxBootVar )
988 	    || PE_parse_boot_argn("rootdev", rdBootVar, kMaxBootVar )) {
989 		if ((rdBootVar[0] == 'm') && (rdBootVar[1] == 'd') && (rdBootVar[3] == 0)) {
990 			return true;
991 		}
992 	}
993 	return false;
994 }
995 
996 void
IOSecureBSDRoot(const char * rootName)997 IOSecureBSDRoot(const char * rootName)
998 {
999 #if CONFIG_SECURE_BSD_ROOT
1000 	IOReturn         result;
1001 	IOPlatformExpert *pe;
1002 	OSDictionary     *matching;
1003 	const OSSymbol   *functionName = OSSymbol::withCStringNoCopy("SecureRootName");
1004 
1005 	matching = IOService::serviceMatching("IOPlatformExpert");
1006 	assert(matching);
1007 	pe = (IOPlatformExpert *) IOService::waitForMatchingService(matching, 30ULL * kSecondScale);
1008 	matching->release();
1009 	assert(pe);
1010 	// Returns kIOReturnNotPrivileged is the root device is not secure.
1011 	// Returns kIOReturnUnsupported if "SecureRootName" is not implemented.
1012 	result = pe->callPlatformFunction(functionName, false, (void *)rootName, (void *)NULL, (void *)NULL, (void *)NULL);
1013 	functionName->release();
1014 	OSSafeReleaseNULL(pe);
1015 
1016 	if (result == kIOReturnNotPrivileged) {
1017 		mdevremoveall();
1018 	}
1019 
1020 #endif  // CONFIG_SECURE_BSD_ROOT
1021 }
1022 
1023 void *
IOBSDRegistryEntryForDeviceTree(char * path)1024 IOBSDRegistryEntryForDeviceTree(char * path)
1025 {
1026 	return IORegistryEntry::fromPath(path, gIODTPlane);
1027 }
1028 
1029 void
IOBSDRegistryEntryRelease(void * entry)1030 IOBSDRegistryEntryRelease(void * entry)
1031 {
1032 	IORegistryEntry * regEntry = (IORegistryEntry *)entry;
1033 
1034 	if (regEntry) {
1035 		regEntry->release();
1036 	}
1037 	return;
1038 }
1039 
1040 const void *
IOBSDRegistryEntryGetData(void * entry,char * property_name,int * packet_length)1041 IOBSDRegistryEntryGetData(void * entry, char * property_name,
1042     int * packet_length)
1043 {
1044 	OSData *            data;
1045 	IORegistryEntry *   regEntry = (IORegistryEntry *)entry;
1046 
1047 	data = (OSData *) regEntry->getProperty(property_name);
1048 	if (data) {
1049 		*packet_length = data->getLength();
1050 		return data->getBytesNoCopy();
1051 	}
1052 	return NULL;
1053 }
1054 
1055 kern_return_t
IOBSDGetPlatformUUID(uuid_t uuid,mach_timespec_t timeout)1056 IOBSDGetPlatformUUID( uuid_t uuid, mach_timespec_t timeout )
1057 {
1058 	IOService * resources;
1059 	OSString *  string;
1060 
1061 	resources = IOService::waitForService( IOService::resourceMatching( kIOPlatformUUIDKey ), (timeout.tv_sec || timeout.tv_nsec) ? &timeout : NULL );
1062 	if (resources == NULL) {
1063 		return KERN_OPERATION_TIMED_OUT;
1064 	}
1065 
1066 	string = (OSString *) IOService::getPlatform()->getProvider()->getProperty( kIOPlatformUUIDKey );
1067 	if (string == NULL) {
1068 		return KERN_NOT_SUPPORTED;
1069 	}
1070 
1071 	uuid_parse( string->getCStringNoCopy(), uuid );
1072 
1073 	return KERN_SUCCESS;
1074 }
1075 } /* extern "C" */
1076 
1077 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1078 
1079 #include <sys/conf.h>
1080 #include <sys/vnode.h>
1081 #include <sys/vnode_internal.h>
1082 #include <sys/fcntl.h>
1083 #include <IOKit/IOPolledInterface.h>
1084 #include <IOKit/IOBufferMemoryDescriptor.h>
1085 
1086 IOPolledFileIOVars * gIOPolledCoreFileVars;
1087 kern_return_t gIOPolledCoreFileOpenRet = kIOReturnNotReady;
1088 IOPolledCoreFileMode_t gIOPolledCoreFileMode = kIOPolledCoreFileModeNotInitialized;
1089 
1090 #if IOPOLLED_COREFILE
1091 
1092 #if defined(XNU_TARGET_OS_BRIDGE)
1093 // On bridgeOS allocate a 150MB corefile and leave 150MB free
1094 #define kIOCoreDumpSize         150ULL*1024ULL*1024ULL
1095 #define kIOCoreDumpFreeSize     150ULL*1024ULL*1024ULL
1096 
1097 #elif !defined(XNU_TARGET_OS_OSX) /* defined(XNU_TARGET_OS_BRIDGE) */
1098 // On embedded devices with >3GB DRAM we allocate a 500MB corefile
1099 // otherwise allocate a 350MB corefile. Leave 350 MB free
1100 
1101 #define kIOCoreDumpMinSize      350ULL*1024ULL*1024ULL
1102 #define kIOCoreDumpLargeSize    500ULL*1024ULL*1024ULL
1103 
1104 #define kIOCoreDumpFreeSize     350ULL*1024ULL*1024ULL
1105 
1106 #else /* defined(XNU_TARGET_OS_BRIDGE) */
1107 // on macOS devices allocate a corefile sized at 1GB / 32GB of DRAM,
1108 // fallback to a 1GB corefile and leave at least 1GB free
1109 #define kIOCoreDumpMinSize              1024ULL*1024ULL*1024ULL
1110 #define kIOCoreDumpIncrementalSize      1024ULL*1024ULL*1024ULL
1111 
1112 #define kIOCoreDumpFreeSize     1024ULL*1024ULL*1024ULL
1113 
1114 // on older macOS devices we allocate a 1MB file at boot
1115 // to store a panic time stackshot
1116 #define kIOStackshotFileSize    1024ULL*1024ULL
1117 
1118 #endif /* defined(XNU_TARGET_OS_BRIDGE) */
1119 
1120 static IOPolledCoreFileMode_t
GetCoreFileMode()1121 GetCoreFileMode()
1122 {
1123 	if (on_device_corefile_enabled()) {
1124 		return kIOPolledCoreFileModeCoredump;
1125 	} else if (panic_stackshot_to_disk_enabled()) {
1126 		return kIOPolledCoreFileModeStackshot;
1127 	} else {
1128 		return kIOPolledCoreFileModeDisabled;
1129 	}
1130 }
1131 
1132 static void
IOCoreFileGetSize(uint64_t * ideal_size,uint64_t * fallback_size,uint64_t * free_space_to_leave,IOPolledCoreFileMode_t mode)1133 IOCoreFileGetSize(uint64_t *ideal_size, uint64_t *fallback_size, uint64_t *free_space_to_leave, IOPolledCoreFileMode_t mode)
1134 {
1135 	unsigned int requested_corefile_size = 0;
1136 
1137 	*ideal_size = *fallback_size = *free_space_to_leave = 0;
1138 
1139 #if defined(XNU_TARGET_OS_BRIDGE)
1140 #pragma unused(mode)
1141 	*ideal_size = *fallback_size = kIOCoreDumpSize;
1142 	*free_space_to_leave = kIOCoreDumpFreeSize;
1143 #elif !defined(XNU_TARGET_OS_OSX) /* defined(XNU_TARGET_OS_BRIDGE) */
1144 #pragma unused(mode)
1145 	*ideal_size = *fallback_size = kIOCoreDumpMinSize;
1146 
1147 	if (max_mem > (3 * 1024ULL * 1024ULL * 1024ULL)) {
1148 		*ideal_size = kIOCoreDumpLargeSize;
1149 	}
1150 
1151 	*free_space_to_leave = kIOCoreDumpFreeSize;
1152 #else /* defined(XNU_TARGET_OS_BRIDGE) */
1153 	if (mode == kIOPolledCoreFileModeCoredump) {
1154 		*ideal_size = *fallback_size = kIOCoreDumpMinSize;
1155 		if (kIOCoreDumpIncrementalSize != 0 && max_mem > (32 * 1024ULL * 1024ULL * 1024ULL)) {
1156 			*ideal_size = ((ROUNDUP(max_mem, (32 * 1024ULL * 1024ULL * 1024ULL)) / (32 * 1024ULL * 1024ULL * 1024ULL)) * kIOCoreDumpIncrementalSize);
1157 		}
1158 		*free_space_to_leave = kIOCoreDumpFreeSize;
1159 	} else if (mode == kIOPolledCoreFileModeStackshot) {
1160 		*ideal_size = *fallback_size = *free_space_to_leave = kIOStackshotFileSize;
1161 	}
1162 #endif /* defined(XNU_TARGET_OS_BRIDGE) */
1163 	// If a custom size was requested, override the ideal and requested sizes
1164 	if (PE_parse_boot_argn("corefile_size_mb", &requested_corefile_size, sizeof(requested_corefile_size))) {
1165 		IOLog("Boot-args specify %d MB kernel corefile\n", requested_corefile_size);
1166 
1167 		*ideal_size = *fallback_size = (requested_corefile_size * 1024ULL * 1024ULL);
1168 	}
1169 
1170 	return;
1171 }
1172 
1173 static IOReturn
IOAccessCoreFileData(void * context,boolean_t write,uint64_t offset,int length,void * buffer)1174 IOAccessCoreFileData(void *context, boolean_t write, uint64_t offset, int length, void *buffer)
1175 {
1176 	errno_t vnode_error = 0;
1177 	vfs_context_t vfs_context;
1178 	vnode_t vnode_ptr = (vnode_t) context;
1179 
1180 	vfs_context = vfs_context_kernel();
1181 	vnode_error = vn_rdwr(write ? UIO_WRITE : UIO_READ, vnode_ptr, (caddr_t)buffer, length, offset,
1182 	    UIO_SYSSPACE, IO_SWAP_DISPATCH | IO_SYNC | IO_NOCACHE | IO_UNIT, vfs_context_ucred(vfs_context), NULL, vfs_context_proc(vfs_context));
1183 
1184 	if (vnode_error) {
1185 		IOLog("Failed to %s the corefile. Error %d\n", write ? "write to" : "read from", vnode_error);
1186 		return kIOReturnError;
1187 	}
1188 
1189 	return kIOReturnSuccess;
1190 }
1191 
1192 static void
IOOpenPolledCoreFile(thread_call_param_t __unused,thread_call_param_t corefilename)1193 IOOpenPolledCoreFile(thread_call_param_t __unused, thread_call_param_t corefilename)
1194 {
1195 	assert(corefilename != NULL);
1196 
1197 	IOReturn err;
1198 	char *filename = (char *) corefilename;
1199 	uint64_t corefile_size_bytes = 0, corefile_fallback_size_bytes = 0, free_space_to_leave_bytes = 0;
1200 	IOPolledCoreFileMode_t mode_to_init = GetCoreFileMode();
1201 
1202 	if (gIOPolledCoreFileVars) {
1203 		return;
1204 	}
1205 	if (!IOPolledInterface::gMetaClass.getInstanceCount()) {
1206 		return;
1207 	}
1208 
1209 	if (mode_to_init == kIOPolledCoreFileModeDisabled) {
1210 		gIOPolledCoreFileMode = kIOPolledCoreFileModeDisabled;
1211 		return;
1212 	}
1213 
1214 	// We'll overwrite this once we open the file, we update this to mark that we have made
1215 	// it past initialization
1216 	gIOPolledCoreFileMode = kIOPolledCoreFileModeClosed;
1217 
1218 	IOCoreFileGetSize(&corefile_size_bytes, &corefile_fallback_size_bytes, &free_space_to_leave_bytes, mode_to_init);
1219 
1220 	do {
1221 		err = IOPolledFileOpen(filename, kIOPolledFileCreate, corefile_size_bytes, free_space_to_leave_bytes,
1222 		    NULL, 0, &gIOPolledCoreFileVars, NULL, NULL, NULL);
1223 		if (kIOReturnSuccess == err) {
1224 			break;
1225 		} else if (kIOReturnNoSpace == err) {
1226 			IOLog("Failed to open corefile of size %llu MB (low disk space)",
1227 			    (corefile_size_bytes / (1024ULL * 1024ULL)));
1228 			if (corefile_size_bytes == corefile_fallback_size_bytes) {
1229 				gIOPolledCoreFileOpenRet = err;
1230 				return;
1231 			}
1232 		} else {
1233 			IOLog("Failed to open corefile of size %llu MB (returned error 0x%x)\n",
1234 			    (corefile_size_bytes / (1024ULL * 1024ULL)), err);
1235 			gIOPolledCoreFileOpenRet = err;
1236 			return;
1237 		}
1238 
1239 		err = IOPolledFileOpen(filename, kIOPolledFileCreate, corefile_fallback_size_bytes, free_space_to_leave_bytes,
1240 		    NULL, 0, &gIOPolledCoreFileVars, NULL, NULL, NULL);
1241 		if (kIOReturnSuccess != err) {
1242 			IOLog("Failed to open corefile of size %llu MB (returned error 0x%x)\n",
1243 			    (corefile_fallback_size_bytes / (1024ULL * 1024ULL)), err);
1244 			gIOPolledCoreFileOpenRet = err;
1245 			return;
1246 		}
1247 	} while (false);
1248 
1249 	gIOPolledCoreFileOpenRet = IOPolledFilePollersSetup(gIOPolledCoreFileVars, kIOPolledPreflightCoreDumpState);
1250 	if (kIOReturnSuccess != gIOPolledCoreFileOpenRet) {
1251 		IOPolledFileClose(&gIOPolledCoreFileVars, 0, NULL, 0, 0, 0);
1252 		IOLog("IOPolledFilePollersSetup for corefile failed with error: 0x%x\n", err);
1253 	} else {
1254 		IOLog("Opened corefile of size %llu MB\n", (corefile_size_bytes / (1024ULL * 1024ULL)));
1255 		gIOPolledCoreFileMode = mode_to_init;
1256 	}
1257 
1258 	// Provide the "polled file available" callback with a temporary way to read from the file
1259 	(void) IOProvideCoreFileAccess(kdp_core_polled_io_polled_file_available, NULL);
1260 
1261 	return;
1262 }
1263 
1264 kern_return_t
IOProvideCoreFileAccess(IOCoreFileAccessRecipient recipient,void * recipient_context)1265 IOProvideCoreFileAccess(IOCoreFileAccessRecipient recipient, void *recipient_context)
1266 {
1267 	kern_return_t error = kIOReturnSuccess;
1268 	errno_t vnode_error = 0;
1269 	vfs_context_t vfs_context;
1270 	vnode_t vnode_ptr;
1271 
1272 	if (!recipient) {
1273 		return kIOReturnBadArgument;
1274 	}
1275 
1276 	if (kIOReturnSuccess != gIOPolledCoreFileOpenRet) {
1277 		return kIOReturnNotReady;
1278 	}
1279 
1280 	// Open the kernel corefile
1281 	vfs_context = vfs_context_kernel();
1282 	vnode_error = vnode_open(kIOCoreDumpPath, (FREAD | FWRITE | O_NOFOLLOW), 0600, 0, &vnode_ptr, vfs_context);
1283 	if (vnode_error) {
1284 		IOLog("Failed to open the corefile. Error %d\n", vnode_error);
1285 		return kIOReturnError;
1286 	}
1287 
1288 	// Call the recipient function
1289 	error = recipient(IOAccessCoreFileData, (void *)vnode_ptr, recipient_context);
1290 
1291 	// Close the kernel corefile
1292 	vnode_close(vnode_ptr, FREAD | FWRITE, vfs_context);
1293 
1294 	return error;
1295 }
1296 
1297 static void
IOClosePolledCoreFile(void)1298 IOClosePolledCoreFile(void)
1299 {
1300 	// Notify kdp core that the corefile is no longer available
1301 	(void) kdp_core_polled_io_polled_file_unavailable();
1302 
1303 	gIOPolledCoreFileOpenRet = kIOReturnNotOpen;
1304 	gIOPolledCoreFileMode = kIOPolledCoreFileModeClosed;
1305 	IOPolledFilePollersClose(gIOPolledCoreFileVars, kIOPolledPostflightCoreDumpState);
1306 	IOPolledFileClose(&gIOPolledCoreFileVars, 0, NULL, 0, 0, 0);
1307 }
1308 
1309 #endif /* IOPOLLED_COREFILE */
1310 
1311 extern "C" void
IOBSDMountChange(struct mount * mp,uint32_t op)1312 IOBSDMountChange(struct mount * mp, uint32_t op)
1313 {
1314 #if IOPOLLED_COREFILE
1315 	uint64_t flags;
1316 	char path[128];
1317 	int pathLen;
1318 	vnode_t vn;
1319 	int result;
1320 
1321 	switch (op) {
1322 	case kIOMountChangeMount:
1323 	case kIOMountChangeDidResize:
1324 
1325 		if (gIOPolledCoreFileVars) {
1326 			break;
1327 		}
1328 		flags = vfs_flags(mp);
1329 		if (MNT_RDONLY & flags) {
1330 			break;
1331 		}
1332 		if (!(MNT_LOCAL & flags)) {
1333 			break;
1334 		}
1335 
1336 		vn = vfs_vnodecovered(mp);
1337 		if (!vn) {
1338 			break;
1339 		}
1340 		pathLen = sizeof(path);
1341 		result = vn_getpath(vn, &path[0], &pathLen);
1342 		vnode_put(vn);
1343 		if (0 != result) {
1344 			break;
1345 		}
1346 		if (!pathLen) {
1347 			break;
1348 		}
1349 #if defined(XNU_TARGET_OS_BRIDGE)
1350 		// on bridgeOS systems we put the core in /private/var/internal. We don't
1351 		// want to match with /private/var because /private/var/internal is often mounted
1352 		// over /private/var
1353 		if ((pathLen - 1) < (int) strlen("/private/var/internal")) {
1354 			break;
1355 		}
1356 #endif
1357 		if (0 != strncmp(path, kIOCoreDumpPath, pathLen - 1)) {
1358 			break;
1359 		}
1360 
1361 		thread_call_enter1(corefile_open_call, (void *) kIOCoreDumpPath);
1362 		break;
1363 
1364 	case kIOMountChangeUnmount:
1365 	case kIOMountChangeWillResize:
1366 		if (gIOPolledCoreFileVars && (mp == kern_file_mount(gIOPolledCoreFileVars->fileRef))) {
1367 			thread_call_cancel_wait(corefile_open_call);
1368 			IOClosePolledCoreFile();
1369 		}
1370 		break;
1371 	}
1372 #endif /* IOPOLLED_COREFILE */
1373 }
1374 
1375 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1376 
1377 
1378 extern "C"
1379 OS_ALWAYS_INLINE
1380 boolean_t
IOCurrentTaskHasEntitlement(const char * entitlement)1381 IOCurrentTaskHasEntitlement(const char * entitlement)
1382 {
1383 	return IOTaskHasEntitlement(NULL, entitlement);
1384 }
1385 
1386 extern "C" boolean_t
IOTaskHasEntitlement(task_t task,const char * entitlement)1387 IOTaskHasEntitlement(task_t task, const char * entitlement)
1388 {
1389 	// Don't do this
1390 	if (task == kernel_task || entitlement == NULL) {
1391 		return false;
1392 	}
1393 	size_t entlen = strlen(entitlement);
1394 	CEQuery_t query = {
1395 		CESelectDictValueDynamic((const uint8_t*)entitlement, entlen),
1396 		CEMatchBool(true)
1397 	};
1398 
1399 #if PMAP_CS_ENABLE && !CONFIG_X86_64_COMPAT
1400 	if (pmap_cs_enabled()) {
1401 		if (task == NULL || task == current_task()) {
1402 			// NULL task means current task, which translated to the current pmap
1403 			return pmap_query_entitlements(NULL, query, 2, NULL);
1404 		}
1405 		vm_map_t task_map = get_task_map_reference(task);
1406 		if (task_map) {
1407 			pmap_t pmap = vm_map_get_pmap(task_map);
1408 			if (pmap && pmap_query_entitlements(pmap, query, 2, NULL)) {
1409 				vm_map_deallocate(task_map);
1410 				return true;
1411 			}
1412 			vm_map_deallocate(task_map);
1413 		}
1414 		return false;
1415 	}
1416 #endif
1417 	if (task == NULL) {
1418 		task = current_task();
1419 	}
1420 
1421 	proc_t p = (proc_t)get_bsdtask_info(task);
1422 
1423 	if (p == NULL) {
1424 		return false;
1425 	}
1426 
1427 	struct cs_blob* csblob = csproc_get_blob(p);
1428 	if (csblob == NULL) {
1429 		return false;
1430 	}
1431 
1432 	void* osents = csblob_os_entitlements_get(csblob);
1433 	if (osents == NULL) {
1434 		return false;
1435 	}
1436 
1437 	if (!amfi) {
1438 		panic("CoreEntitlements: (IOTask): No AMFI\n");
1439 	}
1440 
1441 	return amfi->OSEntitlements_query(osents, (uint8_t*)csblob_get_cdhash(csblob), query, 2) == amfi->CoreEntitlements.kNoError;
1442 }
1443 
1444 extern "C" boolean_t
IOVnodeHasEntitlement(vnode_t vnode,int64_t off,const char * entitlement)1445 IOVnodeHasEntitlement(vnode_t vnode, int64_t off, const char *entitlement)
1446 {
1447 	OSObject * obj;
1448 	off_t offset = (off_t)off;
1449 
1450 	obj = IOUserClient::copyClientEntitlementVnode(vnode, offset, entitlement);
1451 	if (!obj) {
1452 		return false;
1453 	}
1454 	obj->release();
1455 	return obj != kOSBooleanFalse;
1456 }
1457 
1458 extern "C" char *
IOVnodeGetEntitlement(vnode_t vnode,int64_t off,const char * entitlement)1459 IOVnodeGetEntitlement(vnode_t vnode, int64_t off, const char *entitlement)
1460 {
1461 	OSObject *obj = NULL;
1462 	OSString *str = NULL;
1463 	size_t len;
1464 	char *value = NULL;
1465 	off_t offset = (off_t)off;
1466 
1467 	obj = IOUserClient::copyClientEntitlementVnode(vnode, offset, entitlement);
1468 	if (obj != NULL) {
1469 		str = OSDynamicCast(OSString, obj);
1470 		if (str != NULL) {
1471 			len = str->getLength() + 1;
1472 			value = (char *)kalloc_data(len, Z_WAITOK);
1473 			strlcpy(value, str->getCStringNoCopy(), len);
1474 		}
1475 		obj->release();
1476 	}
1477 	return value;
1478 }
1479