TShopping
標題:
Android 5.x 權限問題SELINUX解決方法
[打印本頁]
作者:
woff
時間:
2017-11-3 16:47
標題:
Android 5.x 權限問題SELINUX解決方法
Android 5.x 權限問題解決方法 (from
http://blog.5ibc.net/p/49302.html
)
一、android 5.x開始,引入了非常嚴格的selinux權限管理機制,我們經常會遇到因為selinux權限問題造成的各種avc denied困擾。
本文結合具體案例,講解如何根據log來快速解決90%的權限問題。
遇到權限問題,在logcat或者kernel的log中一定會打印avc denied提示缺少什麼權限,
Command:
cat /proc/kmsg | grep avc 或 dmesg | grep avc
解決原則是:缺什麼補什麼,一步一步補到沒有avc denied為止。
下面給出四個案例:
1.
audit(0.0:67): avc: denied { write } for path="/dev/block/vold/93:96" dev="tmpfs"
複製代碼
分析過程:
缺少什麼權限: { write }權限,
誰缺少權限: scontext=u:r:kernel:s0,
對哪個文件缺少權限:tcontext=u:object_r:block_device
什麼類型的文件: tclass=blk_file
解決方法:kernel.te
allow kernel block_device:blk_file write;
複製代碼
2.
audit(0.0:53): avc: denied { execute } for path="/data/data/com.mofing/qt-reserved-files/plugins/platforms/libgnustl_shared.so" dev="nandl" ino=115502 scontext= u:r:platform_app:s0 tcontext=u:object_r:app_data_file:s0 tclass=file permissive=0
複製代碼
解決方法 :platform_app.te
allow platform_app app_data_file:file execute;
複製代碼
3.
audit(1444651438.800:8): avc: denied { search } for pid=158 comm="setmacaddr" name="/" dev="nandi" ino=1 scontext=u:r:engsetmacaddr:s0 tcontext=u:object_r: vfat:s0 tclass=dir permissive=0
複製代碼
解決方法 :engsetmacaddr.te
allow engsetmacaddr vfat:dir { search write add_name create };
複製代碼
或者
allow engsetmacaddr vfat:dir create_dir_perms;
複製代碼
4.a
udit(1441759284.810:5): avc: denied { read } for pid=1494 comm="sdcard" name="0" dev="nandk" ino=245281 scontext=u:r:sdcardd:s0 tcontext=u:object_r: system_data_file:s0 tclass=dir permissive=0
複製代碼
解決方法 :sdcardd.te
allow sdcardd system_data_file:dir read;
複製代碼
或者
allow sdcardd system_data_file:dir rw_dir_perms
複製代碼
(rw_dir_perms包含read write,可以參考external/sepolicy/global_macros的定義聲明)
通過這四個案例,我們可以總結出一般規律,
以第4個為例
允許某個scontext對某個tcontext擁有某個權限
我們的log重新排列一下,
scontext=u:r:sdcardd
tcontext=u:object_r:system_data_file:s0
tclass=dir
avc: denied { read }
複製代碼
得到萬能套用公式如下:
在scontext所指的te文件中加入類似如下內容:
scontext tcontext tclass avc denied
allow sdcardd system_data_file : dir read
複製代碼
以上以.te為後綴的文件都在external/sepolicy/或者device/softwinner/xxxx-commm/sepolicy/下,修改之後,都要重刷boot.img。
1.
type=1400 audit(4.840:7): avc: denied { getattr } for pid=204 comm=”cameraserver” path=”/vendor” dev=”rootfs” ino=7096 scontext=u:r:cameraserver:s0 tcontext=u:object_r:rootfs:s0 tclass=lnk_file permissive=0
重點在於
紅字,
這個範例簡單的講,
cameraserver 對 rootfs 的 lnk_file 缺少了 {
getattr
} 的權限 ,
只需要在
cameraserver.te
中
新增
allow cameraserver rootfs:lnk_file { getattr };
複製代碼
即可
2.
type=1400 audit(28.360:14): avc: denied { execute } for pid=704 comm=”sh” name=”toybox” dev=”mmcblk3p5″ ino=368 scontext=u:r:demo:s0 tcontext=u:object_r:toolbox_exec:s0 tclass=file permissive=0
#type=1400 audit(28.130:14): avc: denied { read open } for pid=718 comm=”sh” path=”/system/bin/toybox” dev=”mmcblk3p5″ ino=368 scontext=u:r:demo:s0 tcontext=u:object_r:toolbox_exec:s0 tclass=file permissive=0
複製代碼
在這個範例中
demo 對 toolbox_exec 的 file 缺少了 { execute read open } 的權限 ,
(多個權限設定,
只需要以大括號框住 ,
空格隔開即可)
找到demo.te
加入
allow demo toolbox_exec:file { execute read open };
複製代碼
補充說明:
有時候avc denied的log不是一次性顯示所有問題,要等你解決一個權限問題之後,才會提示另外一個權限問題。
比如提示確實某個目錄的read權限,你加入read之後,再顯示缺少write權限,要你一次次一次試,一次一次加。
這時你可以簡單粗暴寫個rw_dir_perms,這個權限包含了{open search write ...}等等很多權限。
可以查看external/sepolicy/global_macros來了解更多權限聲明;
要加入的權限很多時,可以用中括號,比如
allow engsetmacaddr vfat:dir { search write add_name create};
複製代碼
遇到問題不確定是否由於selinux問題造成,可先在adb shell 下,輸入setenforce 0,讓selinux失效,看是否問題還出現。以此可以澄清是非selinux造成的問題。
二、以上基本是對已經存在的進程增加權限,但對第三方進程改如何新增一個全新的te文件並賦予權限呢?
以寫mac地址的setmacaddr執行文件為例(這個執行檔android原生不存在,自行添加的):
在external/sepolicy/file_contexts中,參考其他進程聲明一個:
/system/bin/install-recovery.sh u:object_r:install_recovery_exec:s0
/system/bin/dex2oat u:object_r:dex2oat_exec:s0
/system/bin/patchoat u:object_r:dex2oat_exec:s0
/system/bin/setmacaddr u:object_r:engsetmacaddr_exec:s0
複製代碼
指定setmacaddr的路徑,並指定一個名字,一定要以_exec結尾
2.參考其他文件在external/sepolicy/ 創建engsetmacaddr.te文件,內容如下:
type engsetmacaddr, domain;
type engsetmacaddr_exec, exec_type, file_type;
init_daemon_domain(engsetmacaddr)
allow engsetmacaddr vfat:dir { search write add_name create};
allow engsetmacaddr vfat:file { create read write open };
allow engsetmacaddr engsetmacaddr:capability dac_override;
allow engsetmacaddr shell_exec:file { execute read open execute_no_trans};
allow engsetmacaddr system_data_file:dir { write add_name remove_name };
allow engsetmacaddr system_data_file:file { create execute_no_trans write open setattr};
allow engsetmacaddr system_file:file { execute_no_trans};
複製代碼
以上賦予的權限全部是根據avc denied的log缺什麼一步一步補什麼來的。
歡迎光臨 TShopping (http://www.tshopping.com.tw/)
Powered by Discuz! X3.2