Android 系统添加SELinux权限

本文为博主原创文章,转载请注明出处:https://i.cnblogs.com/EditPosts.aspx?postid=11185476

CPU:RK3288

系统:Android 5.1

SELinux 主要由美国国家安全局开发。2.6 及以上版本的 Linux 内核都已经集成了 SELinux 模块。

通过虚拟文件系统 proc 来读写 gpio 的方法非常受欢迎,不仅因为其省去了 hal 层、 jni 层的代码,而且可以直接通过 adb shell 来读写 gpio。

在 user 版本,虽然驱动代码中已经将节点权限设置为 777 或者 666,但是在 adb shell 中写 gpio 时会报出权限不足的问题。

最快的解决办法是将系统编译成 userdebug 版本或者 eng 版本,也可以对系统 root。

下面是编译成 user 版本的解决方法,前提是驱动已经OK。

cust_gpios 是驱动中通过 proc_mkdir 创建的目录,relay 是通过 proc_create 创建的节点,对应一个方向为输出 gpio

1、在 adb 中读,没有问题,但是写 gpio 会报出权限不足

shell@rk3288:/proc/cust_gpios $ cat relay
cat relay
1
shell@rk3288:/proc/cust_gpios $ echo 0 > relay
echo 0 > relay
/system/bin/sh: can't create relay: Permission denied

kenel 中打印出的错误信息如下

type=1400 audit(1358758415.820:7): avc: denied { write } for pid=1229 comm="sh" name="relay" dev="proc" ino=4026533065 scontext=u:r:shell:s0 tcontext=u:object_r:proc:s0 tclass=file permissive=0

解释:

write:表示没有 write 权限

shell:shell 中缺少权限,文件名与此相同,xxx.te

proc:proc 文件系统缺少权限

file:file 类型的文件

2、添加 shell 权限,scontext 对应的是 shell ,所以需要在 shell.te 最后面中添加

path:device ockchipcommonsepolicyshell.te

allow shell proc:file write;

3、添加后编译会报错如下,这是因为添加了不允许的规则

libsepol.report_failure: neverallow on line 344 of external/sepolicy/app.te (or line 4313 of policy.conf) violated by allow shell proc:file { write };

需要在 app.te 中的 344 行中不允许的规则中删除添加的权限,用大括号括起来

path:externalsepolicyapp.te

neverallow {appdomain -shell}
    proc:dir_file_class_set write;

此时可以在 adb 来通过 proc 虚拟文件系统正常读写 gpio

4、操作 gpio 最终要由上层 apk 来读写,同样写 gpio 时,kernel 和 logcat 都会报出权限不足,解决方法与上面类似

type=1400 audit(1358758857.090:8): avc: denied { write } for pid=1208 comm="ron.gpiocontorl" name="relay" dev="proc" ino=4026533065 scontext=u:r:untrusted_app:s0 tcontext=u:object_r:proc:s0 tclass=file permissive=0

添加权限

path:device ockchipcommonsepolicyuntrusted_app.te

allow untrusted_app proc:file write;

添加后编译报错

libsepol.report_failure: neverallow on line 344 of external/sepolicy/app.te (or line 4313 of policy.conf) violated by allow untrusted_app proc:file { write };


删除不允许的权限

path:externalsepolicyapp.te

neverallow {appdomain -shell -untrusted_app}
    proc:dir_file_class_set write;

此时 adb 和 apk 中都能正常读写 gpio。

下面是 RK3288 Android 5.1 添加权限的完整补丁

diff --git a/device/rockchip/common/sepolicy/shell.te b/device/rockchip/common/sepolicy/shell.te
index be7a221..f382240 100644
--- a/device/rockchip/common/sepolicy/shell.te
+++ b/device/rockchip/common/sepolicy/shell.te
@@ -3,3 +3,4 @@ allow shell toolbox_exec:file { read getattr open execute execute_no_trans };
 allow shell logcat_exec:file { read getattr open execute execute_no_trans };
 allow shell serial_device:chr_file rw_file_perms;
 allow shell proc_cpuinfo:file mounton;
+allow shell proc:file write;

diff --git a/device/rockchip/common/sepolicy/untrusted_app.te b/device/rockchip/common/sepolicy/untrusted_app.te
index 8c0f740..69cfc0d 100644
--- a/device/rockchip/common/sepolicy/untrusted_app.te
+++ b/device/rockchip/common/sepolicy/untrusted_app.te
@@ -6,3 +6,4 @@ allow untrusted_app kernel:system { module_request };
 allow untrusted_app binfmt_misc:dir { search };
 allow untrusted_app binfmt_misc:file { read open };
 allow untrusted_app video_device:chr_file { read write open ioctl };
+allow untrusted_app proc:file write;

diff --git a/external/sepolicy/app.te b/external/sepolicy/app.te
index ca08d74..0400047 100644
--- a/external/sepolicy/app.te
+++ b/external/sepolicy/app.te
@@ -340,7 +340,7 @@ neverallow { appdomain -shell } efs_file:dir_file_class_set read;
 # Write to various pseudo file systems.
 neverallow { appdomain -bluetooth -nfc }
     sysfs:dir_file_class_set write;
-neverallow appdomain
+neverallow {appdomain -shell -untrusted_app}
     proc:dir_file_class_set write;
 
 # Access to syslog(2) or /proc/kmsg.
原文地址:https://www.cnblogs.com/lialong1st/p/11185476.html