Android SELiunx 權(quán)限添加

最近做了個(gè)需求,是調(diào)節(jié)背光的亮度,改變背光的波形,進(jìn)而影響到了亮度,達(dá)到省電的目的。
核心邏輯:1.識(shí)別當(dāng)前是哪個(gè)應(yīng)用;2.根據(jù)白名單,向節(jié)點(diǎn)寫入對(duì)應(yīng)的值;3.通過(guò)節(jié)點(diǎn)不同調(diào)節(jié)背光的亮度。
寫節(jié)點(diǎn)會(huì)涉及 SELiunx 權(quán)限,因?yàn)闀r(shí)間緊迫,SELiunx 權(quán)限較麻煩,驗(yàn)證需整編,費(fèi)時(shí),所以我臨時(shí)禁用掉 SELinux,先把功能調(diào)通。禁用掉 SELinux 如下:
adb shell
setenforce 0 (臨時(shí)禁用掉SELinux)
getenforce (得到結(jié)果為Permissive)
功能調(diào)通好了專門來(lái)搞 SELinux 權(quán)限,在《如何實(shí)現(xiàn)一個(gè) System Services?》有提到 SELinux 權(quán)限是很折騰的,這個(gè)需求 SELinux 權(quán)限我整了兩天時(shí)間,期間各種嘗試,編譯報(bào)錯(cuò),焦急地等待驗(yàn)證...
我們需要向節(jié)點(diǎn) /sys/kernel/display/abcd 寫值。
分析
SELinux 權(quán)限問(wèn)題可以在 Log 里搜到關(guān)鍵字 avc,內(nèi)容如下:
Binder:890_13: type=1400 audit(0.0:678): avc: denied { write } for name="abcd" dev="sysfs" ino=16074 scontext=u:r:system_server:s0 tcontext=u:object_r:sysfs:s0 tclass=file permissive=0
1、誰(shuí)缺少權(quán)限
scontext=u:r:system_server:s0 #system_server 缺少權(quán)限,然后就在system_server.te修改
scontext=u:r:audioserver:s0
2、對(duì)什么缺少權(quán)限
tcontext=u:object_r:sysfs:s0 #對(duì) sysfs 缺少權(quán)限
tcontext=u:object_r:sysfs:s0
3、缺少什么權(quán)限
denied { write } #缺少 write 權(quán)限
4、什么類型的權(quán)限
tclass=file #file 類型的權(quán)限
修改
找到 system/sepolicy/private/system_server.te,添加如下:
allow system_server sysfs:file { write };
上面編譯時(shí)報(bào)錯(cuò)的在 system 中的 domain.te 中有個(gè)重復(fù)的定義:
libsepol.report_failure: neverallow on line 97 of system/sepolicy/private/coredomain.te (or line 36477 of policy.conf) violated by allow system_server sysfs:file { write };
libsepol.check_assertions: 1 neverallow failures occurred
看 coredomain.te
full_treble_only(`
# /proc
neverallow {
coredomain
-init
-vold
} proc:file no_rw_file_perms;
# /sys
neverallow {
coredomain
-init
-ueventd
-vold
} sysfs:file no_rw_file_perms;
}
sysfs 這個(gè)權(quán)限看樣子不能直接使用,可以使用別名替換,修改如下:
找到 device/平臺(tái)名/system/private/file_contexts 文件添加
# tcontext=u:object_r:sysfs:s0
/sys/kernel/display/abcd u:object_r:wxl_abcd:s0 #wxl_abcd替換sysfs,wxl_abcd隨便取
device/平臺(tái)名/system/public/file.te 中添加
type wxl_cabc, fs_type,sysfs_type;
然后可以在 device/平臺(tái)名/system/private/system_server.te 文件中添加
allow system_server wxl_abcd:file { r_file_perms w_file_perms rw_file_perms };
添加 write 或者 read 的權(quán)限要注意 open 的權(quán)限,最后使用 r_file_perms、w_file_perms、rw_file_perms。
