1*c54f35caSApple OSS Distributions// Copyright (c) 2019-2020 Apple Inc. 2*c54f35caSApple OSS Distributions 3*c54f35caSApple OSS Distributions#include <darwintest.h> 4*c54f35caSApple OSS Distributions#include <sys/sysctl.h> 5*c54f35caSApple OSS Distributions 6*c54f35caSApple OSS Distributions#include <Foundation/Foundation.h> 7*c54f35caSApple OSS Distributions#include <IOKit/IOCFSerialize.h> 8*c54f35caSApple OSS Distributions#include <IOKit/IOKitLib.h> 9*c54f35caSApple OSS Distributions 10*c54f35caSApple OSS DistributionsT_GLOBAL_META(T_META_NAMESPACE("xnu.iokit"), 11*c54f35caSApple OSS Distributions T_META_RUN_CONCURRENTLY(true)); 12*c54f35caSApple OSS Distributions 13*c54f35caSApple OSS Distributionsstatic kern_return_t 14*c54f35caSApple OSS Distributionsbuild_ioregistry_by_catalog_send_data(const char *match_name, 15*c54f35caSApple OSS Distributions const char *userclient_name, const char *service_name) 16*c54f35caSApple OSS Distributions{ 17*c54f35caSApple OSS Distributions NSArray *rootCatalogueArray = @[@{ 18*c54f35caSApple OSS Distributions @kIOProviderClassKey: @kIOResourcesClass, 19*c54f35caSApple OSS Distributions @kIOClassKey: (NSString * __nonnull)@(service_name), 20*c54f35caSApple OSS Distributions @kIOUserClientClassKey: (NSString * __nonnull)@(userclient_name), 21*c54f35caSApple OSS Distributions @kIOMatchCategoryKey: (NSString * __nonnull)@(match_name) 22*c54f35caSApple OSS Distributions }]; 23*c54f35caSApple OSS Distributions 24*c54f35caSApple OSS Distributions CFDataRef cfData = IOCFSerialize((__bridge CFTypeRef)rootCatalogueArray, 25*c54f35caSApple OSS Distributions kIOCFSerializeToBinary); 26*c54f35caSApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(cfData, "IOCFSerialize root catalogue array"); 27*c54f35caSApple OSS Distributions kern_return_t kret = IOCatalogueSendData(MACH_PORT_NULL, 1, 28*c54f35caSApple OSS Distributions (const char *)CFDataGetBytePtr(cfData), 29*c54f35caSApple OSS Distributions (uint32_t)CFDataGetLength(cfData)); 30*c54f35caSApple OSS Distributions CFRelease(cfData); 31*c54f35caSApple OSS Distributions return kret; 32*c54f35caSApple OSS Distributions} 33*c54f35caSApple OSS Distributions 34*c54f35caSApple OSS Distributionsstatic bool 35*c54f35caSApple OSS Distributionstest_open_ioregistry(const char *match_name, const char *service_name, 36*c54f35caSApple OSS Distributions bool exploit) 37*c54f35caSApple OSS Distributions{ 38*c54f35caSApple OSS Distributions kern_return_t kret; 39*c54f35caSApple OSS Distributions bool ioreg_found = false; 40*c54f35caSApple OSS Distributions CFStringRef cfstrMatchName = NULL; 41*c54f35caSApple OSS Distributions io_connect_t conn = IO_OBJECT_NULL; 42*c54f35caSApple OSS Distributions io_iterator_t iter = IO_OBJECT_NULL, obj = IO_OBJECT_NULL; 43*c54f35caSApple OSS Distributions CFMutableDictionaryRef service_info = NULL, properties = NULL; 44*c54f35caSApple OSS Distributions 45*c54f35caSApple OSS Distributions service_info = IOServiceMatching(service_name); 46*c54f35caSApple OSS Distributions kret = IOServiceGetMatchingServices(kIOMasterPortDefault, service_info, &iter); 47*c54f35caSApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kret, "IOServiceGetMatchingServices"); 48*c54f35caSApple OSS Distributions cfstrMatchName = CFStringCreateWithCString(kCFAllocatorDefault, 49*c54f35caSApple OSS Distributions match_name, kCFStringEncodingUTF8); 50*c54f35caSApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(cfstrMatchName, 51*c54f35caSApple OSS Distributions "created CFString from match name"); 52*c54f35caSApple OSS Distributions 53*c54f35caSApple OSS Distributions while ((obj = IOIteratorNext(iter)) != 0) { 54*c54f35caSApple OSS Distributions kret = IORegistryEntryCreateCFProperties(obj, &properties, 55*c54f35caSApple OSS Distributions kCFAllocatorDefault, kNilOptions); 56*c54f35caSApple OSS Distributions if (kret != KERN_SUCCESS) { 57*c54f35caSApple OSS Distributions T_LOG("IORegistryEntryCreateCFProperties fails, 0x%08X", 58*c54f35caSApple OSS Distributions (uint32_t)kret); 59*c54f35caSApple OSS Distributions IOObjectRelease(obj); 60*c54f35caSApple OSS Distributions continue; 61*c54f35caSApple OSS Distributions } 62*c54f35caSApple OSS Distributions 63*c54f35caSApple OSS Distributions CFStringRef value = CFDictionaryGetValue(properties, CFSTR("IOMatchCategory")); 64*c54f35caSApple OSS Distributions if (value && CFGetTypeID(value) == CFStringGetTypeID() && 65*c54f35caSApple OSS Distributions CFEqual(value, cfstrMatchName)) { 66*c54f35caSApple OSS Distributions ioreg_found = true; 67*c54f35caSApple OSS Distributions } else { 68*c54f35caSApple OSS Distributions IOObjectRelease(obj); 69*c54f35caSApple OSS Distributions continue; 70*c54f35caSApple OSS Distributions } 71*c54f35caSApple OSS Distributions 72*c54f35caSApple OSS Distributions if (!exploit) { 73*c54f35caSApple OSS Distributions IOObjectRelease(obj); 74*c54f35caSApple OSS Distributions break; 75*c54f35caSApple OSS Distributions } 76*c54f35caSApple OSS Distributions 77*c54f35caSApple OSS Distributions T_LOG("try to exploit by opening service, possibly panic..."); 78*c54f35caSApple OSS Distributions IOServiceOpen(obj, mach_task_self(), 0, &conn); 79*c54f35caSApple OSS Distributions IOObjectRelease(obj); 80*c54f35caSApple OSS Distributions 81*c54f35caSApple OSS Distributions break; 82*c54f35caSApple OSS Distributions } 83*c54f35caSApple OSS Distributions 84*c54f35caSApple OSS Distributions CFRelease(cfstrMatchName); 85*c54f35caSApple OSS Distributions 86*c54f35caSApple OSS Distributions if (properties) { 87*c54f35caSApple OSS Distributions CFRelease(properties); 88*c54f35caSApple OSS Distributions } 89*c54f35caSApple OSS Distributions 90*c54f35caSApple OSS Distributions if (iter != IO_OBJECT_NULL) { 91*c54f35caSApple OSS Distributions IOObjectRelease(iter); 92*c54f35caSApple OSS Distributions } 93*c54f35caSApple OSS Distributions 94*c54f35caSApple OSS Distributions if (conn != IO_OBJECT_NULL) { 95*c54f35caSApple OSS Distributions IOServiceClose(conn); 96*c54f35caSApple OSS Distributions } 97*c54f35caSApple OSS Distributions 98*c54f35caSApple OSS Distributions return ioreg_found; 99*c54f35caSApple OSS Distributions} 100*c54f35caSApple OSS Distributions 101*c54f35caSApple OSS DistributionsT_DECL(io_catalog_send_data_test, 102*c54f35caSApple OSS Distributions "build an IORegistry entry with mismatching IOService and " 103*c54f35caSApple OSS Distributions "IOUserClientClass by IOCatalogueSendData to check for DoS in " 104*c54f35caSApple OSS Distributions "IOCatalogueSendData") 105*c54f35caSApple OSS Distributions{ 106*c54f35caSApple OSS Distributions kern_return_t kret = build_ioregistry_by_catalog_send_data("fooBar", 107*c54f35caSApple OSS Distributions "IOSurfaceRootUserClient", "IOReportHub"); 108*c54f35caSApple OSS Distributions#if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) 109*c54f35caSApple OSS Distributions int development = 0; 110*c54f35caSApple OSS Distributions size_t development_size = sizeof(development); 111*c54f35caSApple OSS Distributions 112*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.development", &development, 113*c54f35caSApple OSS Distributions &development_size, NULL, 0), "sysctl kern.development"); 114*c54f35caSApple OSS Distributions 115*c54f35caSApple OSS Distributions if (development) { 116*c54f35caSApple OSS Distributions T_EXPECT_MACH_SUCCESS(kret, "IOCatalogueSendData should " 117*c54f35caSApple OSS Distributions "return success with development kernel"); 118*c54f35caSApple OSS Distributions } else { 119*c54f35caSApple OSS Distributions /* this trick to build an entry by io_catalog_send_data should fail */ 120*c54f35caSApple OSS Distributions T_EXPECT_EQ(kret, kIOReturnNotPrivileged, "build an entry with" 121*c54f35caSApple OSS Distributions " mismatch IOService and IOUserClientClass by IOCatalogueSendData " 122*c54f35caSApple OSS Distributions "should fail as kIOReturnNotPrivileged in none-dev kernel without kernelmanagerd"); 123*c54f35caSApple OSS Distributions } 124*c54f35caSApple OSS Distributions#else 125*c54f35caSApple OSS Distributions T_EXPECT_MACH_SUCCESS(kret, 126*c54f35caSApple OSS Distributions "IOCatalogueSendData should return success with kernelmanagerd"); 127*c54f35caSApple OSS Distributions#endif /* (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) */ 128*c54f35caSApple OSS Distributions T_EXPECT_FALSE(test_open_ioregistry("fooBar", "IOReportHub", false), 129*c54f35caSApple OSS Distributions "mismatched entry built by IOCatalogueSendData should not be opened"); 130*c54f35caSApple OSS Distributions} 131