SELinux開關
adb在線修改seLinux(也可以改配置文件徹底關閉)
$ getenforce; //獲取當前seLinux狀態,Enforcing(表示已打開),Permissive(表示已關閉)
$ setenforce 1; //打開seLinux
$ setenforce 0; //關閉seLinux
命令查看selinux權限
查看客體的安全上下文:客體是死的,如文件、屬性、套接字等 ls -Z 查看安全上下文
查看主體的安全上下文:主體是指進程,是活的,是安全行為的發起者 ps -Z 查看安全上下文
標志性log 格式:
avc: denied { 操作權限 } for pid=7201 comm=“進程名” scontext=u:r:源類型:s0 tcontext=u:r:目標類型:s0 tclass=訪問類型 permissive=0
在相應源類型.te文件,添加如下格式的一行語句:
格式:allow 源類型 目標類型:訪問類型 {操作權限};
示例:
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:s0
什么類型:tclass=blk_file
kernel.te: allow kernel block_device:blk_file write;
(寫操作一般還伴隨open、append等,所以一般使用w_file_perms宏替代單一的write)(如需同時申請多個權限可用花括號包起來放句尾,如 {read write} )
常修改的文件位置
/system/sepolicy/下面的public/是平台公共策略,private/是平台私有策略,vendor/是供應商策略
也有直接在/vendor/下的,也有在/device/下的
selinux編譯產物在AndroidR上的位置
system/etc/selinux system_ext/etc/selinux product/etc/selinux vendor/etc/selinux odm/etc/selinux
安裝 policycoreutils工具使用 :
1.linux or 虛擬機 2. su切進root 3.sudo apt install policycoreutils 4.su切出root 5.policycoreutils 的audit2allow命令可以將selinux報錯信息轉換為配置,從而解決問題。
示例:
# audit2allow -i selog.txt
#============= init ==============
allow init sysfs:file write;
#============= priv_app ==============
allow priv_app oplus_nw_exported_system_prop:file read;
(selog.txt中存放的是“avc: denied”報錯打印,audit2allow不但會給出權限配置,而且還會給出配置的位置)
開發和調試時的分析
- 主要步驟為| 確認–添加–驗證 |如下:
- 確認:關閉selinux再運行確認是否是selinux引起 或者 看kernellog、eventlog、mainlog中“avc”log且“permissive=0”的log。(因為有時只是報log而已,permissive=1是selinux並未阻止的)
- 添加:從github上可下載sepolicy-inject安裝進手機直接使用命令動態注入懷疑缺少的權限,快速驗證。
- 驗證:驗證是否問題還出現。
- 可以根據 ino 這個打印出來的 inode number 號來查看是報那個節點沒有權限
# ls -i /sys/devices/system/cpu/cpu0/online //查看文件的 inode number
35941 /sys/devices/system/cpu/cpu0/online
# ls -i /proc/uptime
4026532103 /proc/uptime
/sys/devices # find ./ -inum 35941 //根據 inode number 查找對應的文件
./system/cpu/cpu0/online
/proc # find ./ -inum 4026532103
./uptime
- 查看文件的selinux屬性
/sys/class/wakeup # ls -la -Z
total 0
drwxr-xr-x 2 root root u:object_r:sysfs_wakeup:s0 0 1970-01-01 08:00 . //selinux配置使用sysfs_wakeup
drwxr-xr-x 112 root root u:object_r:sysfs:s0 0 1970-01-01 08:00 .. //selinux配置使用sysfs
lrwxrwxrwx 1 root root u:object_r:sysfs:s0 0 2020-08-28 20:04 wakeup0 -> ../../devices/platform/.../c440000.qcom,spmi:qcom,pm8150@0:qcom,power-on@800/wakeup/wakeup0
常用通配符
-表示除去某項內容
*表示全部
~表示取反除了此項之外的所有
selinux文件說明和語法
- 所有客體類別和客體操作都在android/system/sepolicy/private/security_classes和access_vectors中有羅列。
- file_context文件是設置節點或文件的selinux屬性類型的,.te文件是設置訪問權限的。通俗的理解,file_context是定義屬性的類型,.te文件是使用屬性類型。
(1)file_contexts 給所有的自定義變量(包括文件節點、設備節點、可執行程序)賦了具體的值
(2)device.te 聲明了file_contexts中為設備節點的變量
(3)file.te 聲明了file_contexts中為文件類型的變量
(4)很多.te文件都是以進程名來命名的
- file.te語法
type httpd_user_content_t, file_type, httpdcontent;
分成兩條語句進行表述:
#定義 httpd_user_content_t :
type httpd_user_content_t;
#關聯屬性:
typeattribute httpd_user_content_t file_type, httpdcontent;