Sepolicy在調(diào)試階段真心很煩,有時(shí)因?yàn)橐粋€(gè)sepolicy就導(dǎo)致結(jié)果與預(yù)期不一樣。
1. Sepolicy規(guī)則
Sepolicy 規(guī)則
allow sourcecontext targetcontext:class {}
- sourcecontext: scontext
- targetcontext: tcontext
- class: tclass
Sepolicy 文件存放的位置
AOSP 7.1
system/sepolicy/
-
OEM產(chǎn)商一般會有overlay, 如Nvidia
device/nvidia/common/sepolicy_n/
2. 臨時(shí)disable
adb shell setenforce 0
# 設(shè)置SELinux 成為permissive模式(SELinux開啟,但對違反selinux規(guī)則的行為只記錄,不會阻止)
adb shell setenforce 1
#設(shè)置SELinux 成為enforcing模式 (SELinux開啟)
可以通過 getenforce 來獲得當(dāng)前系統(tǒng) selinux 的狀態(tài)
如果是
- Enforcing enabled
- Permissive disabled
缺點(diǎn): adb reboot后系統(tǒng)恢復(fù)成原來的狀態(tài)
3. 在代碼中永久關(guān)閉 selinux
代碼文件 system/core/init/init.cpp
找到函數(shù) selinux_initialize
static void selinux_initialize(bool in_kernel_domain) {
bool is_enforcing = selinux_is_enforcing();
}
將 bool is_enforcing = selinux_is_enforcing();
改為
bool is_enforcing = false; //selinux_is_enforcing();
即可,
這樣即使系統(tǒng)重啟也會被永久disabled掉了
注意: 編譯出來的 init可執(zhí)行文件是在設(shè)備的根目錄下,而不是/system/bin/下
且init 會被打包進(jìn) boot.img, 而非 system.img.
4. Selinux規(guī)則
4.1 binder_call
05-16 12:41:23.390 W/BootAnimation( 114): type=1400 audit(0.0:6): avc: denied { call } for
scontext=u:r:bootanim:s0 tcontext=u:r:mediaserver:s0 tclass=binder permissive=0
需要在bootanim.te
里加入
binder_call(bootanim, mediaserver)
4.2 allow
avc: denied { find } for service=media.audio_flinger pid=665 uid=1003
scontext=u:r:bootanim:s0 tcontext=u:object_r:audioserver_service:s0 tclass=service_manager permissive=0
需要在bootanim.te
中加入
allow bootanim audioserver_service:service_manager find
5. 驗(yàn)證修改成功
通過編譯后可以在 out/debug/target/product/xxx/root
目錄下找到file_contexts.bin
最后通過工具 sefcontext將file_contexts.bin 轉(zhuǎn)換成 file_context
sefcontext file_contexts.bin -o file_contexts
也可通過 sefcontext_compile來打包
sefcontext_compile file_contexts
sefcontext可執(zhí)行文件請查閱 file_contexts.bin和file_contexts轉(zhuǎn)換工具
具體百度網(wǎng)盤:鏈接:http://pan.baidu.com/s/1pLgyRur 密碼:khat