说在前面,文章大部分参考https://source.android.com/security/selinux/。
1.一些基本概念
SELinux 运行的原则是:所有没有明确指定为allow的操作都会被拒绝。
两种模式:
Permissive mode, 会有denials的log,但是不会强制执行。在前期porting的时候可以使用这种mode。
Enforcing mode, 会有denials的log,也会强制执行。在基本跑通之后还是尽快切到这个mode,尽早发现更多问题。
policy rule:
Sockets, files, and processes 都有label: user:role:type:mls_level 。
RULE_VARIANT SOURCE_TYPES TARGET_TYPES : CLASSES PERMISSIONS
例如:allow domains types:classes permissions
- Domain – 进程的标签。也叫一个domain type,因为就是进程的type.
- Type – object(file,socket等)的标签
- Class – 被访问的object的类型(例如,文件,socket等)
- Permission – 执行的操作(读,写等)
这些rules会在.te文件中,并且type 和attribute的声明也是在te文件中。
关于attribute:
我们可以指定一组domain或者type为一个attribute. 如果一个rule是针对一个attribute,那么也就对关联到这个attribute的domain和type都适用。
Domain attribute和所有的进程的domain相关联;
File_type attribute和所有的file type相关联。
context file:
Context 文件是用来给object打标签的文件。
- file_contexts 是给file分派标签的文件。如果需要添加一个file_contexts,可以通过重新build image或者对需要修改标签的文件运行restorecon 。
- genfs_contexts 给filesystems分配标签,比如proc或者vfat不支持attributes.
- property_contexts 给Android 系统的property分派标签来控制可以设置这些property的进程。这个设置在开机时init会解析。
- service_contexts 给Android binder service 分派标签来控制哪些进程可以给这个service添加(注册)和查找(检查)binder reference。在启动时servicemanager 会读取这个设置。
- seapp_contexts 给app进程和/data/data目录分派标签. 这个设置是在开机由installd读取或者在每个app launch 的之后被zygote读取,
- mac_permissions.xml 依照app的signature或者package name 给app分配一个seinfo标签。这个设置会在启动的时候由system_server读取。
2. source code中的sepolicy
在Android 8.0和更高的版本中,添加policy到BOARD_SEPOLICY_DIRS
只会添加到vendor image。
- system/sepolicy/public.包含了导出给vendor-specific policy使用的policy。Public policy 的目的是在不同版本之间保持一致,所以可以在定制策略中包含任何/public的内容。因此,可以放到/public的policy的type会更加严格。可以把这当成平台导出的policy API:任何处理/system和/vendor之间接口的东西都属于这里。
- system/sepolicy/private. 包含了system image 运行所必需的策略,但是vendor image不应该知道这些策略。
- system/sepolicy/vendor.给/vendor的组件提供的policy,但是存在于核心平台tree(不是 device-specific目录) 。这是build系统区分设备和全局组件的一个工件 ; 从概念上来说这个其实是下面描述的device-specific policy 的一部分。
- device/manufacturer/device-name/sepolicy. 包含了device-specific policy 。还包含设备对policy的定制化,也就是在Android 8.0和更高的版本中对应于vendor image中的组件的policy。这部分是需要在mk中添加到BOARD_SEPOLICY_DIRS 中的。也是ODM需要添加的部分。
3. 在机台中的sepolicy相关文件
•Vendor/etc/selinux
1).两个cil 文件
1.1 plat_pub_versioned.cil -->public platform policy
1.2 vendor_sepolicy.cil-->vendor and BOARD_SEPOLICY_DIRS policy
2). 一些contexts 文件
vendor_file_contexts vendor_hwservice_contexts vendor_property_contexts vendor_seapp_contexts vndservice_contexts -->主要是BOARD_SEPOLICY_DIRS下面的contexts几个文件拼起来的
3.)其他几个文件
plat_sepolicy_vers.txt
precompiled_sepolicy
precompiled_sepolicy.plat_and_mapping.sha256
vendor_mac_permissions.xml
•System/etc/selinux
├── mapping
│ ├── 26.0.cil-->system/sepolicy/private/compat/26.0/26.0.cil
│ ├── 27.0.cil-->system/sepolicy/private/compat/27.0/27.0.cil
│ └── 28.0.cil-->system/sepolicy/public中的type 映射的cil. 格式为:
(typeattributeset proc_pid_max_28_0 (proc_pid_max))
(expandtypeattribute (proc_pid_max_28_0) true)
(typeattribute proc_pid_max_28_0)
├── plat_and_mapping_sepolicy.cil.sha256 -->校验值
├── plat_file_contexts -->system/sepolicy/private/+vendor/app-prebuilt/data/sepolicy/
+device/qcom/sepolicy/private/
├── plat_hwservice_contexts-->system/sepolicy/private/hwservice_contexts
├── plat_mac_permissions.xml--> system/sepolicy/private/mac_permissions.xml
├── plat_property_contexts-->system/sepolicy/private/+device/qcom/sepolicy/private/
├── plat_seapp_contexts-->system/sepolicy/private/seapp_contexts
├── plat_service_contexts-->system/sepolicy/private/+vendor/app-prebuilt/data/sepolicy/
+device/qcom/sepolicy/private/
├── selinux_denial_metadata-->sytsem/sepolicy/private/bug_map
└── plat_sepolicy.cil-->system/sepolicy/private/ +system/sepolicy/public中的policy
4.兼容性
在Android p中,有一些为了之后的兼容性而有一些做法。
4.1. public中的type映射为_vN的attribute(其中vN是版本。)
(typeattributeset foo_vN (foo)) -->在vN.cil中
4.2. Platform-public policy导出为allow source_foo target_bar:class perm; 会被包入到vendor policy中。在编译的时候这会转化为vendor image 中的policy(在转化后的CIL中):
(allow source_foo_vN target_bar_vN (class (perm))) -->在plat_pub_versioned.cil和plat_sepolicy.cil中
4.3 对于platform 升级,会有以下几种情况。
4.3.1.Same type
例如/dev/binder,/dev/binder在所有的版本中的label都是binder_device。这体现在转换之后的policy中如下:
binder_device_v1 … binder_device_vN
从v1->v2升级时,platform policy必须包含:
type binder_device; -> (type binder_device) (in CIL)
在v1的映射文件中(CIL, v1.cil中):
(typeattributeset binder_device_v1 (binder_device))
在v2的映射文件中(CIL,v2.cil中):
(typeattributeset binder_device_v2 (binder_device))
在v1的vendor policy中(CIL,plat_plat_versioned.cil):
(typeattribute binder_device_v1)
(allow binder_device_v1 …)
在v2的vendor policy中(CIL,plat_plat_versioned.cil) :
(typeattribute binder_device_v2)
(allow binder_device_v2 …)
4.3.2.New types
例如新加的type为sysfs_A ,v1->v2升级时,platform policy(both plat_pub_versioned.cil和plat_sepolicy.cil)必须包含:
type sysfs_A; -> (type sysfs_A) (in CIL)
type sysfs; (type sysfs) (in CIL)
在v1的映射文件中(CIL,v1.cil中):
(typeattributeset sysfs_v1 (sysfs sysfs_A))
在v2的映射文件中(CIL,v2.cil中):
(typeattributeset sysfs_v2 (sysfs))
(typeattributeset sysfs_A_v2 (sysfs_A))
在v1的vendor policy中(CIL,plat_pub_versioned.cil中):
(typeattribute sysfs_v1)
(allow … sysfs_v1 …)
在v2的vendor policy中(CIL,plat_pub_versioned.cil中):
(typeattribute sysfs_A_v2)
(allow … sysfs_A_v2 …)
(typeattribute sysfs_v2)
(allow … sysfs_v2 …)
4.3.3 Removed types
(typeattribute sysfs_v1)
(allow … sysfs_v1 …)
4.3.3.1.Example Version 1: Collapsing types (removing sysfs_A)
v1->v2升级时,platform policy必须包含:
type sysfs; (type sysfs) (in CIL)
在v1的映射文件中(CIL):
(typeattributeset sysfs_v1 (sysfs))
(type sysfs_A) # in case vendors used the sysfs_A label on objects
(typeattributeset sysfs_A_v1 (sysfs sysfs_A))
在v2的映射文件中(CIL):
(typeattributeset sysfs_v2 (sysfs))
在v1的vendor policy中(CIL):
(typeattribute sysfs_A_v1)
(allow … sysfs_A_v1 …)
(typeattribute sysfs_v1)
(allow … sysfs_v1 …)
在v2的vendor policy中(CIL):
(typeattribute sysfs_v2)
(allow … sysfs_v2 …)
4.3.3.2.Example Version 2: Removing completely (foo type)
v1->v2升级时,platform policy必须包含:
# nothing - we got rid of the type
在v1的映射文件中(CIL):
(type foo) #needed in case vendors used the foo label on objects
(typeattributeset foo_v1 (foo))
在v2的映射文件中(CIL):
# nothing - get rid of it
在v1的vendor policy中(CIL):
(typeattribute foo_v1)
(allow foo …)
(typeattribute sysfs_v1)
(allow sysfs_v1 …)
在v2的vendor policy中(CIL):
(typeattribute sysfs_v2)
(allow sysfs_v2 …)
PS:其实主要是参考官网上的内容做的一点整理。后面后更多理解再来编辑。