Is it possible to synchronously request configuration information within the Start method of DriverKit?

Is there a way to synchronously retrieve configuration information from the app or read configuration information from a file within the Start method of DriverKit?

I have attempted to use OSMappedFile to read a file, but my driver crashes or I receive the error message "Sandbox: com.injection.epusbfilter.dext(20610) deny(1) file-read-data /private/tmp/driverkit_config.txt" in the console, even though I have set com.apple.security.app-sandbox to false.

    OSMappedFile *mappedFile;
    
    do {
        const char *path = "/private/tmp/cfg";
        // 创建 OSMappedFile 实例
        kern_return_t result = OSMappedFile::createFromPath(path, 0, 0, &mappedFile);
        if (result != KERN_SUCCESS) {
            Log("Failed to create and map the file.");
            ret = -1;
            break;
        }
        
        *size = mappedFile->size();
        
        // 获取映射到内存中的数据
        char *charData = reinterpret_cast<char *>(const_cast<void *>(mappedFile->data()));
        Log("get cfg:%s", charData);
        if (strlen(charData) > 0) {
            if (charData[0] == '1') {
                ret = 1;
                break;
            }
        }
        else {
            ret = -2;
            break;
        }
        
    } while(false);
    
    mappedFile->free();

Replies

I think the short answer might be "no". dexts are always sandboxed. You generally can't read or write files outside the sandbox. If a user explicitly grants permission to read or write a file, you can get a security scoped bookmark for that file in your app, and give it to your app to resolve.

You should be able to use the property interface to read/write configuration data. Use CopyProperties from your driver's Start(), to read the properties, and implement SetProperties in your dext.

To alter the properties from the app, you should be able to use IORegistryEntrySetCFProperties from IOKitLib.h, but you may need to use IOConnectSetCFProperty or IOConnectSetCFProperties. Those two need a connection, which you get from IOServiceOpen, which in turn requires that you can make a user client from your dext, so you would have to implement code to create a user client object.

I haven't tried any of these (at least not for a long while) and I can't find any sample code which does this. Good luck.

To alter the properties from the app, you should be able to use IORegistryEntrySetCFProperties from IOKitLib.h, but you may need to use IOConnectSetCFProperty or IOConnectSetCFProperties.

To accomplish this, need to first establish a connection while the dext is running. I want to retrieve messages from an app or a file that the app has written to before the Start method is invoked.

In any case, I would like to retrieve configuration information returned by the app within the Start method, which may have changed during the app's runtime.

I have tried to use Mach RPC, but it seems that DriverKit does not support it. Is it possible to modify the dext's Info.plist in the app and add custom content so that it can be read in DriverKit? @ssmith_c