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