通過高通平臺簡單總結的許可權問題
android 5.x開始,引入了非常嚴格的selinux許可權管理機制,我們經常會遇到因為selinux許可權問題造成的各種avc denied困擾。有時候我們在添加了一些驅動或應用的時候,發現不能用,但是程式本身查不出問題來,那就要抓log看一下了,很多時候就是因為沒有新增許可權,下面簡單的總結了一下
一、
遇到許可權問題,在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" ino=1263 scontext=u:r:kernel:s0 tcontext=u:object_r:block_device:s0 tclass=blk_file permissive=0
分析過程:
缺少什麼許可權: { 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.
audit(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. 有時候avc denied的log不是一次性顯示所有問題,要等你解決一個許可權問題之後,才會提示另外一個許可權問題。
比如提示確實某個目錄的read許可權,你加入read之後,再顯示缺少write許可權,要你一次次一次試,一次一次加。
這時你可以簡單粗暴寫個rw_dir_perms,這個許可權包含了{open search write ...}等等很多許可權。
可以檢視external/sepolicy/global_macros來了解更多許可權宣告;
2. 要加入的許可權很多時,可以用中括號,比如
allow engsetmacaddr vfat:dir { search write add_name create};
3. 遇到問題不確定是否由於selinux問題造成,可先在adb shell 下,輸入setenforce 0,讓selinux失效,看是否問題還出現。以此可以澄清是非selinux造成的問題。
二、
以上基本是對已經存在的程序增加許可權,但對第三方程序改如何新增一個全新的te檔案並賦予許可權呢?
以寫mac地址的setmacaddr執行檔案為例(這個執行檔Android原生不存在,自行新增的):
1. 在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缺什麼一步一步補什麼來的。
---------------------
作者:s_jason
來源:CSDN
原文:https://blog.csdn.net/s_jason/article/details/73864350
版權宣告:本文為博主原創文章,轉載請附上博文連結!