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