SELinux LOG分析及添加權限

adb修改selinux

Enforcing(表示已打開),Permissive(表示已關閉)

getenforce; //獲取當前selinux狀態

setenforce 1; //打開selinux

setenforce 0; //關閉selinux

 

從kernel中徹底關閉

修改/linux/android/kernel/arch/arm64/configs/xxx_defconfig文件(xxx一般爲產品名),去掉CONFIG_SECURITY_SELINUX = y的設置項

 

sepolicy中添加權限

  • 修改依據,通過指令cat /proc/kmsg | grep denied,或Kernel 的log中定位到的標誌性log
  • 修改步驟
    • 找相應的源類型.te文件,此文件可能存放的路徑:
      • linux/android/external/sepolicy
      • linux/android/device/qcom/sepolicy/common
      • device/xxx/sepolicy(與device相關)
    • 標誌性log
avc: denied  { 操作權限  }  for pid=7201  comm=“進程名”  scontext=u:r:源類型:s0  tcontext=u:r:目標類型:s0  tclass=訪問類型 permissive=0

 在相應源類型.te文件,添加如下格式的一行語句(結尾有分號)

格式:allow  源類型 目標類型:訪問類型 {操作權限};

 

實例 : 

kernel log:

avc: denied {getattr read} for pid=7201 comm="xxx.xxx" scontext=u:r:system_app:s0 tcontext=u:r:shell_data_file:s0 tclass=dir permissive=0

修改方案

在system_app.te文件中,添加下面語句:
allow system_app shell_data_file:dir{getattr read};

 

修改Sepolicy後出現“Error While Expanding policy”

在系統添加某個*.te或在te文件中添加某個selinux權限後,build會出現如下error:

genfscon proc /driver/thermal u:object_r:proc_thermal:s0
libsepol.report_failure: neverallow on line 429 of system/sepolicy/private/app.te (or line 21317 of policy.conf) violated by allow system_app system_file:file { write };
libsepol.report_failure: neverallow on line 406 of system/sepolicy/public/domain.te (or line 8484 of policy.conf) violated by allow system_app system_file:file { write };
libsepol.check_assertions: 2 neverallow failures occurred
Error while expanding policy

這是因爲在/system/sepolicy/private/app.te和system/sepolicy/public/domain.te文件中添加了一些neverallow rules,導致編譯檢查的時候出現錯誤

neverallow appdomain system_file:dir_file_class_set {create write setattr relabelfrom relabelto append unlink link rename};

只需要在上面的規則中去掉添加的allow xx system_file:file { write };中的xx,具體方式是在nerverallow中用{}裏用-xx排除某個,即不需要有此規則:

neverallow {appdomain -system_app} system_file:dir_file_class_set {create write setattr relabelfrom relabelto append unlink link rename};

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章