本文为博主原创文章,转载请注明出处:https://i.cnblogs.com/EditPosts.aspx?postid=11185476ios
CPU:RK3288git
系统:Android 5.1shell
SELinux 主要由美国国家安全局开发。2.6 及以上版本的 Linux 内核都已经集成了 SELinux 模块。安全
经过虚拟文件系统 proc 来读写 gpio 的方法很是受欢迎,不只由于其省去了 hal 层、 jni 层的代码,并且能够直接经过 adb shell 来读写 gpio。app
在 user 版本,虽然驱动代码中已经将节点权限设置为 777 或者 666,可是在 adb shell 中写 gpio 时会报出权限不足的问题。dom
最快的解决办法是将系统编译成 userdebug 版本或者 eng 版本,也能够对系统 root。ide
下面是编译成 user 版本的解决方法,前提是驱动已经OK。post
cust_gpios 是驱动中经过 proc_mkdir 建立的目录,relay 是经过 proc_create 建立的节点,对应一个方向为输出 gpioui
一、在 adb 中读,没有问题,可是写 gpio 会报出权限不足spa
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 类型的文件
二、添加 shell 权限,scontext 对应的是 shell ,因此须要在 shell.te 最后面中添加
path:\device\rockchip\common\sepolicy\shell.te
allow shell proc:file write;
三、添加后编译会报错以下,这是由于添加了不容许的规则
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:\external\sepolicy\app.te
neverallow {appdomain -shell} proc:dir_file_class_set write;
此时能够在 adb 来经过 proc 虚拟文件系统正常读写 gpio
四、操做 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\rockchip\common\sepolicy\untrusted_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:\external\sepolicy\app.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.