|
當在開發程式時,用開發工具Android Studio
搬移App所有檔案到SD卡,會出現logcat
搬移好了之後,常常無法運行,
但是Android Studio的logcat無法顯示出哪裡出問題,
常常是使用者不對或是SELINUX通道設置錯誤,
這時怎麼找出問題呢?
可用
adb shell
su root
dmesg | grep 'avc:'
或
cat /proc/kmsg
如:Seeing denials:- dmesg | grep avc: # current boot
- cat /proc/last_kmsg | grep avc: # prior boot
複製代碼 找服務domian就會列出相關服務
以 angry birds為例
該App的pkg_name=com.rovio.angrybirds
首先改變他的所有權人
- chown u0_a248:u0_a248 com.rovio.angrybirds
複製代碼
接著點桌面上的angrybirds圖標
這時angrybirds不會運行,卡住了
- dmesg | grep 'angrybirds'
複製代碼
他說明 avc: denied { search } 看起來是通道關閉,找不到路徑
com.rovio.angrybirds 拒絕路徑"mmcblk1p2" 是SD卡的ext分割區
scontext=u:r:untrusted_app:s0:c512,c768 來源屬性
tcontext=u:object_r:unlabeled:s0 目的屬性
下面給出四個案例:
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缺什麼一步一步補什麼來的。
參考文章
https://events.static.linuxfound ... android_smalley.pdf
|
|