Android 10 如何关闭selinux权限

由于应用层访问设备节点的时候,因为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);
}

在/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()
函数,将该函数直接返回 false。
bool IsEnforcing() {
+ return false;
该方式适用 user 和 userdebug 版本,系统重启仍然有效

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值