Android 10 如何关闭selinux权限

Android 10 如何关闭selinux权限,第1张

由于应用层访问设备节点的时候,因为selinux权限问题而访问不了,所以就先关闭selinux
代码路径:
system/core/init/selinux.cpp
1、在/selinux.cpp文件中SelinuxInitialize()方法初始化selinux权限问题

void SelinuxInitialize() {
    Timer t;

    LOG(INFO) << "Loading SELinux policy";
    if (!LoadPolicy()) {
        LOG(FATAL) << "Unable to load SELinux policy";
    }

    bool kernel_enforcing = (security_getenforce() == 1);
    bool is_enforcing = IsEnforcing();
    if (kernel_enforcing != is_enforcing) {
        if (security_setenforce(is_enforcing)) {
            PLOG(FATAL) << "security_setenforce(%s) failed" << (is_enforcing ? "true" : "false");
        }
    }

    if (auto result = WriteFile("/sys/fs/selinux/checkreqprot", "0"); !result) {
        LOG(FATAL) << "Unable to write to /sys/fs/selinux/checkreqprot: " << result.error();
    }

    // init's first stage can't set properties, so pass the time to the second stage.
    setenv("INIT_SELINUX_TOOK", std::to_string(t.duration().count()).c_str(), 1);
}

2、在/selinux.cpp文件中IsEnforcing()方法中直接返回false就是关闭selinux权限了

bool IsEnforcing() {
+	 return false;
    {
        int fd(open("/mboot/selinux", O_RDONLY | O_CLOEXEC | O_BINARY));
        if (fd != -1) {
            char v = 0xff;
            if (read(fd, &v, 1) < 0)
                PLOG(ERROR) << "Failed to read /mboot/selinux";
            close(fd);
            LOG(WARNING) << "/mboot/selinux is " << v;
            return v == '1';
        }
    }
    if (ALLOW_PERMISSIVE_SELINUX) {
        return StatusFromCmdline() == SELINUX_ENFORCING;
    }
    return true;
}

设置版本为 permissive 模式,有三种方式
方法一、命令行方式

adb root; adb shell setenforce 0
该方式仅适用于 userdebug 版本,系统重启修改会失效

方法二、 修改 dts bootargs 参数

board 对应的 dts 文件里,在 bootargs 参数里增加 androidboot.selinux=permissive 字段。
该方式仅适用于 userdebug 版本,系统重启仍然有效.

方法三、修改 init 代码

可以修改 system/core/init/selinux.cpp 文件里的 IsEnforcing()
函数,将该函数直接返回 falsebool IsEnforcing() {
+ return false;
该方式适用 user 和 userdebug 版本,系统重启仍然有效

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/992552.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存