Android 系统添加SELinux权限
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 deniedkenel 中打印出的錯誤信息如下
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\rockchip\common\sepolicy\shell.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 中的 4313 行中不允許的規則中刪除添加的權限,用大括號括起來
path:\external\sepolicy\app.te
neverallow {appdomain -shell}?
此時可以在 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\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}?
此時 adb 和 apk 中都能正常讀寫 gpio。
?
下面是添加權限的完整補丁
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
總結
以上是生活随笔為你收集整理的Android 系统添加SELinux权限的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux 网络的一些书籍
- 下一篇: 如何成为一个伟大的 JavaScript