IOKit 和 TrustedBSD 政策

IOKit and TrustedBSD policy

如何在 IOKit 内核扩展中使用来自 TrustedBSD 的 MAC 策略?

我已经有一个可用的 IOKit 扩展,我想向它添加一些策略。

为了测试,我写了两个虚拟扩展,一个使用 IOKit 和一个通用的。
通用扩展完美无缺地工作,而 IOKit 正在为符号 mac_policy_registermac_policy_unregister.

生成 link 错误
$ sudo kextutil -tn /tmp/MACPolicy.kext
kxld[com.Test.MACPolicy]: The following symbols are unresolved for this kext:
kxld[com.Test.MACPolicy]:   mac_policy_register(mac_policy_conf*, unsigned int*, void*)
kxld[com.Test.MACPolicy]:   mac_policy_unregister(unsigned int)
Link failed (error code 5).
Check library declarations for your kext with kextlibs(8).

$ sudo kextlibs -v 6 -undef-symbols /tmp/MACPolicy.kext
Kext user-space log filter changed from 0xff2 to 0xfff.
Kext kernel-space log filter changed from 0xff2 to 0xfff.
Kext library architecture set to x86_64.
Kext library architecture is x86_64 (unchanged).
For all architectures:
    com.apple.kpi.iokit = 15.4
    com.apple.kpi.libkern = 15.4

For x86_64:
    2 symbols not found in any library kext:
    __Z21mac_policy_unregisterj
    __Z19mac_policy_registerP15mac_policy_confPjPv

我已经将指定的库添加到我的 Info.plist,以及 com.apple.kpi.dsepcom.apple.kpi.unsupportedcom.apple.kpi.mach 或它们的任意组合,但没有成功。

我能找到的关于这个的所有信息都是这个 thread on the darwin-kernel 讨论列表。

我现在的目标是 OS X 10.11。

注意缺失的符号是如何像 C++ 函数一样被破坏的,但问题中的函数实际上是普通的 C 函数。这意味着当您从 C++ 调用它们时,它们使用的声明缺少 extern "C" 链接说明符。 MAC header 不考虑 C++,因此当从 .cpp 文件中包含它们时,您需要将它们明确地包装在 extern "C" 块中,如下所示:

extern "C" {
#include <security/mac_policy.h>
}

如果您的 #include 在混合的 C/C++ header 文件中,您需要像往常一样使用 #ifdef __cplusplus 使其以 C++ 编译为条件。

您应该知道,尽管事实上这个功能 是官方添加的 ,基于 OS X v10.11 API差异Kernel Changes for Objective-C

不幸的是,从 High Sierra (10.13) MAC 政策 API 开始,第三次完全关闭派对开发商。 Apple 从自己的文档中删除了 mac_policy_registermac_policy_unregistermac_policy_confmac_policy_ops 和其他主要 MAC 政策部分的所有引用。