我正在编写一个套接字过滤器 kext,我想忽略以 root 身份建立的任何连接。在 OS X Lion 之前,以下代码可以完美运行:
static boolean_t is_root() {
proc_t p = proc_self();
boolean_t isRoot = proc_suser(p);
proc_rele(p);
return isRoot;
}
但现在对于 Lion 和 Mountain Lion,is_root()
函数始终返回 true。在 Snow Leopard 中,它的工作方式正如我想象的那样。
以下是我如何在套接字过滤器事件处理程序中测试该函数的示例:
int debugPid = proc_selfpid();
if (is_root()) {
printf("%u (root)\n", debugPid);
} else {
printf("%u (user)\n", debugPid);
}
但输出总是显示“root”,例如:
2012-11-15 3:48:00.000 PM kernel[0]: 29879 (root)
进行连接的应用程序是 Twitter(通过 PID 确认)。 Twitter 以普通用户权限运行,而不是 root。
是否有更好/正确的方法来确定套接字连接背后的进程是否具有root权限?
最佳答案
根据bsd/sys/proc.h
(link):
/* this routine returns error if the process is not one with super user privileges */
int proc_suser(proc_t p);
因此,返回 0
表示该进程具有 root 权限,否则返回非零。
你想要:
static boolean_t is_root() {
proc_t p = proc_self();
int error = proc_suser(p);
proc_rele(p);
return error == 0;
}
关于macos - 如何确定进程在网络内核扩展中是否具有 root 权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13398912/