TShopping

 找回密碼
 註冊
搜索
查看: 1499|回復: 0

[教學] Android 5-7 下執行App,如何知道該App為什麼無法運行

  [複製鏈接]
發表於 2018-6-5 23:43:27 | 顯示全部樓層 |閱讀模式
 
Push to Facebook
當在開發程式時,用開發工具Android Studio
搬移App所有檔案到SD卡,會出現logcat
搬移好了之後,常常無法運行,
但是Android Studio的logcat無法顯示出哪裡出問題,
常常是使用者不對或是SELINUX通道設置錯誤,
這時怎麼找出問題呢?

可用
adb shell
su root
dmesg | grep 'avc:'

cat /proc/kmsg
如:Seeing denials:
  1. dmesg | grep avc: # current boot
  2. cat /proc/last_kmsg | grep avc: # prior boot
複製代碼
找服務domian
  1. ps -Z | grep :init:
複製代碼
就會列出相關服務

以 angry birds為例
該App的pkg_name=com.rovio.angrybirds

首先改變他的所有權人
  1. chown u0_a248:u0_a248 com.rovio.angrybirds
複製代碼

接著點桌面上的angrybirds圖標
這時angrybirds不會運行,卡住了

  1. dmesg | grep 'angrybirds'
複製代碼
avc.png

他說明  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



 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-3-19 12:57 , Processed in 0.092316 second(s), 26 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表