【问题标题】:Getting Bundle Identifier for KEXT from .kext Directory Programmatically以编程方式从 .kext 目录获取 KEXT 的包标识符
【发布时间】:2018-12-15 12:28:57
【问题描述】:

我正在构建一个内核扩展并为其创建一个 .kext 目录。 从另一个库 API,我使用 KextManager APIs 将此 kext 加载到内核中。

一切看起来都很好,这是我正在加载 kext 的代码片段:

CFStringRef path = CFStringCreateWithCString(kCFAllocatorDefault, "awesome.kext", kCFStringEncodingUTF8);
CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, path, kCFURLPOSIXPathStyle, true);
OSReturn result  = KextManagerLoadKextWithURL(url, NULL);

效果很好,但是我需要知道我的包标识符才能控制套接字连接,并且可能稍后使用KextManagerUnloadKextWithIdentifier(kextId) API 卸载 kext。

所以我知道 XXX.kext/Contents/Info.plist 包含带有捆绑标识符的 CFBundleIdentifier 密钥,但我需要某种方式以编程方式(某些 API?)获取它,而不是读取 Info.plist 文件并解析它。

我还尝试将CFBundleIdentifier 字符串值替换为其他值并加载到内核中,它仍然可以正常工作,所以看起来Info.plist 无论如何都不可靠。

有什么相关的吗?有什么建议?谢谢! :)

【问题讨论】:

    标签: objective-c c macos kernel kernel-extension


    【解决方案1】:

    我最近发现了 2 个有用的方法,都基于 KextManagerCopyLoadedKextInfo API。

    void handle_kext(const void* key, const void* value, void* context)
    {
        // OSBundlePath - CFString (this is merely a hint stored in the kernel; the kext is not guaranteed to be at this path)
        CFStringRef bundle_path_key = CFStringCreateWithCString(kCFAllocatorDefault, "OSBundlePath", kCFStringEncodingUTF8);
    
        const char* bundle_id_cstring = CFStringGetCStringPtr(key, kCFStringEncodingUTF8);
        const char* bundle_path_cstring = CFStringGetCStringPtr(CFDictionaryGetValue(value, bundle_path_key, kCFStringEncodingUTF8);
    
        // #1 Approach: Compare your 'I-Want-This-BundleID' with loaded kext path
        // #2 Approach: Compare your 'I-Want-This-BundlePath' with loaded kext ID
    }
    
    void some_func()
    {
        CFDictionaryRef loaded_kexts = KextManagerCopyLoadedKextInfo(NULL, NULL);
        CFDictionaryApplyFunction(loaded_kexts, handle_kext, NULL);
    }
    

    【讨论】:

      猜你喜欢
      • 2012-10-23
      • 2014-11-11
      • 1970-01-01
      • 2010-12-24
      • 2011-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多