作为 root 从二进制 运行 执行 exec() 的权限被拒绝

Permission denied on exec() from a binary running as root

我有一个 C 二进制文件,我 运行 在 Android.

中使用 root 用户 (su -c binary_path)

一切正常,直到二进制文件尝试 exec*() 另一个二进制文件。它实际上适用于大多数设备,但在某些设备上出现 EACCES 错误。

C 二进制文件实际上是这样启动的:

execlp("su","su","-c",binary_path,NULL);

在某些时候,二进制文件将尝试进行这些调用(简化):

fork();

...

// child here

execlp("sh","sh","-c",script,NULL);

实际在不同的 Android 6.0 设备、Nexus 9 和 S7 上进行了测试。 Nexus 9 正常,S7 失败。

所以我检查了以下所有权限和安全上下文,没有发现任何区别:

/system/bin
/system/bin/sh
/system/bin/ls
<library_path>
/su/bin/su

还检查了二进制文件 运行 UID/GID = 0,在两个设备上都是正确的。

在 logcat 中,我没有看到任何针对缺少权限或违反安全策略的审核。

编辑:刚刚验证了二进制文件 运行ning 的安全上下文:

$ps -Z
u:r:init:s0          root ...

无论 exec() 是否有效,两种设备都一样。

EDIT2:在设备上失败,/proc/kmsg 在尝试 exec() 时包含此内容:

Restricted making process. PID = 8868(<binary>) PPID = 8340(<binary>)

没有来自selinux的avc,在AOSP源代码中找不到这段文字

在 Google 上搜索 "Restricted making process" 后,我偶然发现了适用于 S5 和 S6(不是 S7)的三星内核。

if(CHECK_ROOT_UID(current))
    if(sec_restrict_fork())
    {
        PRINT_LOG("Restricted making process. PID = %d(%s) "
                        "PPID = %d(%s)\n",
            current->pid, current->comm,
            current->parent->pid, current->parent->comm);
        return -EACCES;
    }

并且 sec_restrict_fork() 包含以下内容:

if (sec_check_execpath(current->mm, "/data/")) {
    ret = 1;
    goto out;
}

因此在三星设备上失败,而在其他设备上没有。