SELinux规则添加进阶

SELinux规则添加进阶

前言

在上一篇中,我们已经了解了一个常规avc denied日志信息该如何应对。当时挖的两个坑,今天来填一下:

  • 添加的SELinux规则是否存在权限放大;
  • 添加的SELinux规则是否触犯neverallow;
    实际上,这是一类问题,今天正好遇到了对应的需求,就以此为例,进行以下介绍:

背景

由于功能开发需要,现需要为system_server进程添加对/sys/class/ieee80211/phy0/index节点读取的SELinux规则;

抓取信息

按照管理,我们先抓取日志,并过滤avc字段;
经过筛选,可定位到如下这条日志:

12-31 16:06:44.566   946   946 W WifiStateMachin: type=1400 audit(0.0:77): avc: denied { read } for name="index" dev="sysfs" ino=34138 scontext=u:r:system_server:s0 tcontext=u:object_r:sysfs:s0 tclass=file permissive=0

分析信息

如果此时使用audit2allow工具解析,会得到如下一行规则:

#============= system_server ==============
allow system_server sysfs:file read;

经验告诉我,system_server是不能直接拥有对sysfs的读文件权限的;
不信可以添加后编译,发现编译报错:

FAILED: out/target/product/blueline/obj/ETC/sepolicy_neverallows_intermediates/sepolicy_neverallows 
/bin/bash -c "(rm -f out/target/product/blueline/obj/ETC/sepolicy_neverallows_intermediates/sepolicy_neverallows ) && (ASAN_OPTIONS=detect_leaks=0 out/host/linux-x86/bin/checkpolicy -M -c 		30 -o out/target/product/blueline/obj/ETC/sepolicy_neverallows_intermediates/sepolicy_neverallows out/target/product/blueline/obj/ETC/sepolicy_neverallows_intermediates/policy.conf )"
libsepol.report_failure: neverallow on line 31 of system/sepolicy/private/domain.te (or line 26738 of policy.conf) violated by allow system_server sysfs:file { read };
libsepol.check_assertions: 1 neverallow failures occurred
Error while expanding policy

可见system/sepolicy/private/domain.te第31行定义了一条neverallow的规则:

  # /sys
  neverallow {
    coredomain
    -init
    -ueventd
    -vold
  } sysfs:file no_rw_file_perms;

翻译过来即:所有主体为coredomain(除了init,ueventd,vold)的进程,均不能对sysfs进行文件读写操作;
那么实际上,这里就同时遇到了上述的两条情况:

  1. system_serversysfs的文件读权限,显然太大了;
  2. system_serversysfs的文件读权限,触犯了AOSP预设的neverallow规则;

添加规则

通常情况下,这种问题可以通过细化/sys/class/ieee80211/phy0/index节点的标签,来缩小权限作用范围,同时避免触犯neverallow规则;
具体操作如下:

  1. 首先,我们需要确认,/sys/class/ieee80211/phy0/index节点是否是一个实际存在的节点,使用readlink -f获取真实路径;
    # readlink -f /sys/class/ieee80211/phy0/index                                                
    /sys/devices/platform/soc/18800000.qcom,icnss/ieee80211/phy0/index
    
  2. 我们知道,/sys/proc等路径下的节点是运行时生成的,而不是实际存在的文件,因此这类路径下的标签,不能使用file_contexts来定义,而是使用genfs_contexts定义,示例规则如下:
    genfscon sysfs /devices/platform/soc/18800000.qcom,icnss/ieee80211/phy0/index            u:object_r:sysfs_zsui:s0
    
  3. 然后在file.te中添加sysfs_zsui标签的定义:
    type sysfs_zsui, sysfs_type, fs_type;
    
  4. 最后在system_server.te文件中添加system_server的规则:
    allow system_server sysfs_zsui:file read;
    

验证

由于新增了标签与规则,建议全编译后刷机验证(单推规则可能不生效)
开机后查看其标签类型:

blueline:/ # ls -alZ /sys/class/ieee80211/phy0/index          
-r--r--r-- 1 root root u:object_r:sysfs_zsui:s0 4096 2022-07-26 05:53 /sys/class/ieee80211/phy0/index

总结

整个过程比较简单,但是需要注意如下几点:

  1. 添加规则前(特别是/sys/proc等路径下运行时生成的路径)一定要使用readlink命令来确认其真实路径,添加到软链接后的路径上是不生效的;
  2. 添加规则尽量不要加载根标签上(例如procfssysfs),大概率会触犯neverallow规则;
  3. 与之对应的,为特定路径创建一个子标签(对应此例中zsui_sysfs),并继承自根标签(对应此例中sysfs),这样再添加对此子标签的规则,则可以避免权限放大的问题;

你可能感兴趣的:(Android,bash,linux,junit)