天天看點

Android SELinux avc dennied權限問題解決方法1. 概述2. 調試确認SELinux問題3. 具體案例分析4. 萬能公式5. TIPS6. 進階進階

這篇文字本人原創于2015年,并作為原廠釋出文檔release,當時并未上傳部落格,估計已經被很多網友發表了。

1. 概述

SELinux是Google從android 5.0開始,強制引入的一套非常嚴格的權限管理機制,主要用于增強系統的安全性。

然而,在開發中,我們經常會遇到由于SELinux造成的各種權限不足,即使擁有“萬能的root權限”,也不能擷取全部的權限。本文旨在結合具體案例,講解如何根據log來快速解決90%的SELinux權限問題。

2. 調試确認SELinux問題

為了澄清是否因為SELinux導緻的問題,可先執行:

setenforce 0 (臨時禁用掉SELinux)

getenforce  (得到結果為Permissive)

如果問題消失了,基本可以确認是SELinux造成的權限問題,需要通過正規的方式來解決權限問題。

遇到權限問題,在logcat或者kernel的log中一定會列印avc denied提示缺少什麼權限,可以通過指令過濾出所有的avc denied,再根據這些log各個擊破:

cat /proc/kmsg | grep avc 

dmesg | grep avc

例如:

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

可以看到有avc denied,且最後有permissive=0,表示不允許。

3. 具體案例分析

解決原則是:缺什麼權限補什麼,一步一步補到沒有avc denied為止。

解決權限問題需要修改的權限檔案如下位置,以.te結尾

A:Android/devicesoftwinner/astar-common/sepolicy/*.te

B:Android/external/sepolicy/*.te

其中,A是對B的overlay(覆寫),能在A修改的盡量在A修改,盡量避免修改B,修改B可能會導緻CTS fail問題,修改A不會影響CTS測試。

(如果不需要深入了解,請直接跳到萬能公式這一章閱讀更簡潔)

下面給出四個案例:

案例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程序對block_device類型的blk_file缺少write權限。

解決方法:在上文A位置,找到kernel.te這個檔案,加入以下内容:

allow  kernel  block_device:blk_file  write;

make installclean後重新編譯,刷boot.img才會生效。

案例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

分析過程:

缺少什麼權限:      { execute}權限,

誰缺少權限:        scontext = u:r:platform_app:s0

對哪個檔案缺少權限:tcontext = u:object_r:app_data_file

什麼類型的檔案:    tclass= file

完整的意思: platform_app程序對app_data_file類型的file缺少execute權限。

解決方法:在上文A位置,找到platform_app.te這個檔案,加入以下内容:

allow  platform_app  app_data_file:file  execute;

make installclean後重新編譯,刷boot.img才會生效。

案例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;

(create_dir_perms包含search write add_name create可參考external/sepolicy/global_macros的定義聲明)

案例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. 萬能公式

通過這四個案例,我們可以總結出一般規律,

以第案例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

某個scontext對某個tclass類型的tcontext缺乏某個權限,我們需要允許這個權限:

我們的log重新排列一下,

scontext = u:r:sdcardd

tcontex t= u:object_r:system_data_file:s0

tclass = dir

avc: denied { read }

得到萬能套用公式如下:

在scontext所指的.te檔案(例如sdcardd.te)中加入類似如下allowe内容:

5. TIPS

1. 以上以.te為字尾的檔案都在以下位置:

A:Android/devicesoftwinner/astar-common/sepolicy/*.te

B:Android/external/sepolicy/*.te

其中,A是對B的overlay(覆寫),能在A修改的盡量在A修改,修改B可能會導緻CTS fail問題,修改A不會影響CTS測試。修改之後,為了節約驗證時間,隻重刷boot.img即可看效果;

2. 有時候avc denied的log不是一次性暴露所有權限問題,要等解決一個權限問題之後,才會暴露另外一個權限問題。比如提示缺少某個目錄的read權限,加入read之後,才顯示缺少write權限,要一次次一次試,一次一次加,時間成本極大。

針對dir缺少的任何權限,建議賦予create_dir_perms,基本涵蓋對dir的所有權限,比如:

{ open search write read rename create rmdir getattr }等等。

針對file缺少的任何權限,建議賦予rwx_file_perms,基本涵蓋對file的所有權限,比如:

包含{ open read write open execute getattr create ioctl }等等。

更多内容請參考external/sepolicy/global_macros來了解更多權限聲明。

3. 要加入的權限很多時,可以用中括号,比如:

allow engsetmacaddr  vfat:dir { search write add_name create};

4. 修改A位置的.te檔案遇到編譯錯誤怎麼辦?

(首先請排除拼寫錯誤)說明此項權限是SELinux明确禁止的,也是Google CTS禁止的,如果産品不需要過CTS,可以修改。一般來說,編譯出錯的log會提示相關哪個檔案哪一行出錯,檔案位置一定會在B裡的.te檔案。比如B規定了以下neverallow,

neverallow system_server sdcard_type:dir { open read write };

那麼system_server是不能擁有這些權限的,如果賦予這些權限就編譯報錯,解決方法是根據編譯錯誤提示的行号,把這一句注釋掉即可。

6. 進階進階

6.1. 建立.te安全政策檔案方法

以上基本是對已經存在的程序增權重限,但對第三方程序改如何新增一個全新的te檔案并賦予權限呢?

以寫mac位址的setmacaddr執行檔案為例(這個執行檔android原生不存在,自行添加的):

在init.xxx.rc中如下服務:

service engsetmacaddr  /system/bin/setmacaddr  /data/misc/wifi/wifimac.txt

    class main

    disabled

oneshot

1. 在device/softwinner/astar-common/sepolicy/file_contexts中,參考其他程序聲明一個scontext:

……

/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的路徑,并指定一個名字,一定要以service名+_exec結尾

2.參考其.te檔案在device/softwinner/astar-common/sepolicy/file_contexts 建立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缺什麼一步一步補什麼來的。

6.2. 新裝置節點增加通路權限

驅動建立了一個新的裝置節點,即使權限是777,android層也是沒有通路權限的。

下面以一個/dev/wifi_bt節點為示範,讓此節點被使用者空間的system_server程序通路。

1. 編輯devicesoftwinner/astar-common/sepolicy/device.te,仿照這個檔案裡的寫法,定義一個dev_type類型的wifi_bt_device裝置:

type misc_block_device, dev_type;

type private_block_device, dev_type;

……

type wf_bt_device, dev_type;  

2. 編輯file_contexts.te,将/dev/wf_bt節點聲明為第1步定義的wf_bt_device:

/dev/block/by-name/misc         u:object_r:misc_block_device:s0

/dev/block/by-name/alog         u:object_r:log_block_device:s0

/dev/block/by-name/private      u:object_r:private_block_device:s0

# We add here  

/dev/wf_bt              u:object_r:wf_bt_device:s0  

3. 在system_server.te,根據dmesg | grep avc允許system_server對wf_bt_device這個節點可讀可寫:

# Read/Write to /proc/net/xt_qtaguid/ctrl and and /dev/xt_qtaguid.  

allow system_server qtaguid_proc:file rw_file_perms;  

allow system_server qtaguid_device:chr_file rw_file_perms;  

 ……

allow system_server wf_bt_device:chr_file rw_file_perms;  

其他程序如需通路/dev/wf_bt節點,依樣畫葫蘆,增加對wf_bt_device的權限即可。